|
| 1 | +import sys, os |
| 2 | +import glob, re |
| 3 | +import pickle |
| 4 | +import numpy as np |
| 5 | +import matplotlib.pyplot as plt |
| 6 | +from natsort import natsorted |
| 7 | +import mokas_stackimages as msi |
| 8 | +import mokas_bubbles as mkb |
| 9 | +from mokas_colors import get_colors |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +class RunBubbles: |
| 14 | + def __init__(self, rootDir, subdir_pattern): |
| 15 | + self.rootDir = rootDir |
| 16 | + # Get the initial parameters |
| 17 | + bubbles_ini = mkb.Bubbles_ini(rootDir) |
| 18 | + self.imParameters = bubbles_ini.imParameters |
| 19 | + self.experiments = bubbles_ini.experiments |
| 20 | + self.thresholds = bubbles_ini.thresholds |
| 21 | + try: |
| 22 | + erase_small_events_percent = bubbles_ini.erase_small_events_percent |
| 23 | + except: |
| 24 | + erase_small_events_percent = None |
| 25 | + self.erase_small_events_percent = erase_small_events_percent |
| 26 | + |
| 27 | + # Get the directories based on the pattern |
| 28 | + sub_dirs = natsorted(glob.glob1(rootDir, subdir_pattern)) |
| 29 | + sub_dirs = np.array(sub_dirs) |
| 30 | + # Prepare to find a match |
| 31 | + q = subdir_pattern.replace("*", "(.*)") |
| 32 | + all_experiments = [np.int(re.search(q, sd).group(1)) for sd in sub_dirs] |
| 33 | + jj = np.array([x in self.experiments for x in all_experiments]) |
| 34 | + self.sub_dirs = sub_dirs[jj] |
| 35 | + #sdirs = [subdir_pattern.replace("*", str(i)) for i in self.experiments] |
| 36 | + #self.sub_dirs = [sd for sd in sdirs if sd in sub_dirs] |
| 37 | + self.filenames = [d + bubbles_ini.filename_suffix for d in self.sub_dirs] |
| 38 | + print("There are %i files to analyse" % len(self.filenames)) |
| 39 | + if self.experiments is None: |
| 40 | + self.n_experiments = len(self.sub_dirs) |
| 41 | + self.experiments = range(self.sub_dirs) |
| 42 | + else: |
| 43 | + self.n_experiments = len(self.experiments) |
| 44 | + print(self.filenames[0]) |
| 45 | + self.full_title = ", ".join(self.filenames[0].split("_")[1:4]) |
| 46 | + |
| 47 | + |
| 48 | + def plot_results(self, plot_contours=True): |
| 49 | + """ |
| 50 | + Plot the different images for creep in bubbles |
| 51 | + """ |
| 52 | + # Prepare to plot |
| 53 | + plt.close("all") |
| 54 | + self.figs = [] |
| 55 | + self.imArray_collector = {} |
| 56 | + # We choose to plot ALL measurements |
| 57 | + rows1, cols1 = 1, self.n_experiments |
| 58 | + self.fig1, self.axs1 = plt.subplots(rows1, cols1, sharex=True, sharey=True, squeeze=False) # ColorImages |
| 59 | + self.figs.append(self.fig1) |
| 60 | + self.fig2, self.axs2 = plt.subplots(self.n_experiments, 1, sharey=True, squeeze=False) # Histograms |
| 61 | + self.figs.append(self.fig2) |
| 62 | + self.fig3, self.axs3 = plt.subplots(cols1, 3*rows1, sharex=True, sharey=True, squeeze=False) # events and clusters |
| 63 | + self.figs.append(self.fig3) |
| 64 | + #for n in range(self.n_experiments): |
| 65 | + #allParameters = self.wireParameters.copy() |
| 66 | + |
| 67 | + for n, experiment in enumerate(self.experiments): |
| 68 | + sub_dir = self.sub_dirs[n] |
| 69 | + title = str(experiment).rjust(2, "0") |
| 70 | + self.imParameters['subDirs'] = [self.rootDir, sub_dir, "", "", ""] |
| 71 | + filename = self.filenames[n] |
| 72 | + self.imParameters['pattern'] = filename |
| 73 | + print("#" * 50) |
| 74 | + print("Experiment # %i: %s" % (experiment, filename)) |
| 75 | + print("The threshold applied is %f" %(self.thresholds[n])) |
| 76 | + #imArray = StackImages(**self.imParameters) |
| 77 | + #allParameters.update(self.imParameters) |
| 78 | + |
| 79 | + imArray = mkb.Bubbles(**self.imParameters) |
| 80 | + if n == 0: |
| 81 | + nImages, rows, cols = imArray.shape |
| 82 | + pColor = get_colors(nImages, 'pastel', norm=True) |
| 83 | + self.imArray_collector[experiment] = imArray |
| 84 | + imArray.showColorImage(self.thresholds[n], palette= 'random', #pColor, |
| 85 | + plot_contours=True, plotHist=None, |
| 86 | + fig=self.fig1, ax=self.axs1[0, n], |
| 87 | + title=title, noSwitchColor='black') |
| 88 | + imArray.plotHistogram(imArray._switchTimesOverThreshold, |
| 89 | + fig=self.fig2, ax=self.axs2[n, 0], |
| 90 | + title=title, ylabel=None) |
| 91 | + # imArray.find_contours(lines_color='k', remove_bordering=True, plot_centers_of_mass=False, |
| 92 | + # invert_y_axis=False, plot_rays=False, |
| 93 | + # fig=self.fig3, ax=self.axs3[n], title=title) |
| 94 | + #imArray.get_stats_prop() |
| 95 | + imArray.getEventsAndClusters(method='edges') |
| 96 | + # TO BE FIXED |
| 97 | + axs = self.axs3[n,0], self.axs3[n,1], self.axs3[n,2] |
| 98 | + imArray.plotEventsAndClustersMaps(fig=self.fig3, axs=axs) |
| 99 | + |
| 100 | + suptitle = " - ".join(self.rootDir.split("/")[-2:]) |
| 101 | + for fig in self.figs: |
| 102 | + fig.suptitle(suptitle, fontsize=30) |
| 103 | + plt.show() |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + def save_hdf5(self): |
| 108 | + for experiment in self.imArray_collector: |
| 109 | + bubble = self.imArray_collector[experiment] |
| 110 | + data = [bubble.cluster2D_start, bubble.cluster2D_end, bubble._switchTimes2D, bubble._switchSteps2D] |
| 111 | + labels = ['cluster2D_start', 'cluster2D_end', 'switchTimes2D', 'switchSteps2D'] |
| 112 | + bubble.hdf5.save_data(data, labels, dtype=np.int16) |
| 113 | + # Save histogram |
| 114 | + hist = [bubble.N_hist, bubble.bins_hist] |
| 115 | + hist_labels = ['N_hist', 'bins_hist'] |
| 116 | + bubble.hdf5.save_data(hist, hist_labels, dtype=np.float32) |
| 117 | + # Save contours |
| 118 | + bubble.hdf5.save_data(bubble.contours, 'contours', dtype=np.float32) |
| 119 | + # Save waiting time histogram |
| 120 | + try: |
| 121 | + bubble.hdf5.save_data(bubble.waiting_times_hist, 'waiting_time', dtype=np.int) |
| 122 | + except: |
| 123 | + pass |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + def save_figs(self): |
| 128 | + res_dir = os.path.join(self.rootDir, 'Results') |
| 129 | + if not os.path.isdir(res_dir): |
| 130 | + os.mkdir(res_dir) |
| 131 | + out_string = "_".join([str(e) for e in self.experiments]) |
| 132 | + filename = os.path.join(res_dir, "events_and_clusters_exp%s.png" % out_string) |
| 133 | + self.fig3.savefig(filename) |
| 134 | + |
| 135 | + |
0 commit comments