def batch(self, function, tech): try: filenames = utils.getFilenames('Load ' + tech + ' images', [('Tiff file', '*.tif;*.tiff')], self.folder) nfiles = len(filenames) function(filenames[0]) corrArray = np.zeros((nfiles, self.n[0], self.n[1])) # Expand correlation array so it matches data shape corrExp = np.empty((nfiles, self.initShape[0], self.initShape[1]), dtype=np.single) corrExp[:] = np.nan ringsExp = np.empty((nfiles, self.initShape[0], self.initShape[1]), dtype=np.single) ringsExp[:] = np.nan path = os.path.split(filenames[0])[0] folder = os.path.split(path)[1] self.folderStatus.setText('Processing folder ' + path) print('Processing folder', path) t0 = time.time() # Make results directory if it doesn't exist resultsDir = os.path.join(path, 'results') if not os.path.exists(resultsDir): os.makedirs(resultsDir) resNames = [utils.insertFolder(p, 'results') for p in filenames] for i in np.arange(nfiles): print(os.path.split(filenames[i])[1]) self.fileStatus.setText(os.path.split(filenames[i])[1]) function(filenames[i]) self.ringFinder(False, batch=True) corrArray[i] = self.localCorr bound = (np.array(self.initShape) - self.crop).astype(np.int) edge = bound - self.remanent corrExp[i, self.crop:edge[0], self.crop:edge[1]] = self.localCorrBig # Save correlation values array corrName = utils.insertSuffix(resNames[i], '_correlation') tiff.imsave(corrName, corrExp[i], software='Gollum', imagej=True, resolution=(1000 / self.pxSize, 1000 / self.pxSize), metadata={ 'spacing': 1, 'unit': 'um' }) # Saving ring images ringsExp[corrExp < self.corrThres] = 0 ringsExp[corrExp >= self.corrThres] = 1 for i in np.arange(nfiles): # Save correlation values array ringName = utils.insertSuffix(resNames[i], '_rings') tiff.imsave(ringName, ringsExp[i], software='Gollum', imagej=True, resolution=(1000 / self.pxSize, 1000 / self.pxSize), metadata={ 'spacing': 1, 'unit': 'um' }) # save configuration file in the results folder tools.saveConfig(self, os.path.join(resultsDir, 'config')) # plot histogram of the correlation values hrange = (np.min(np.nan_to_num(corrArray)), np.max(np.nan_to_num(corrArray))) y, x, _ = plt.hist(corrArray.flatten(), bins=20, range=hrange) x = (x[1:] + x[:-1]) / 2 # Save data array as txt corrArrayFlat = corrArray.flatten() validCorr = corrArrayFlat[~np.isnan(corrArrayFlat)] validArr = np.repeat(np.arange(nfiles), np.prod(self.n)) validArr = validArr[~np.isnan(corrArrayFlat)] valuesTxt = os.path.join(resultsDir, folder + 'corr_values.txt') corrByN = np.stack((validCorr, validArr), 1) np.savetxt(valuesTxt, corrByN, fmt='%f\t%i') groupedCorr = [ validCorr[np.where(validArr == i)] for i in np.arange(nfiles) ] groupedCorr = [x for x in groupedCorr if len(x) > 0] nfiles = len(groupedCorr) meanCorrs = [np.mean(d) for d in groupedCorr] validCorrRing = validCorr[np.where(validCorr > self.corrThres)] validArrRing = validArr[np.where(validCorr > self.corrThres)] groupedCorrRing = [ validCorrRing[np.where(validArrRing == i)] for i in np.arange(nfiles) ] ringFracs = [ len(groupedCorrRing[i]) / len(groupedCorr[i]) for i in np.arange(nfiles) ] meanRingCorrs = np.array([np.mean(d) for d in groupedCorrRing]) meanRingCorrs = meanRingCorrs[~np.isnan(meanRingCorrs)] n = corrArray.size - np.count_nonzero(np.isnan(corrArray)) nring = np.sum(validCorr > self.corrThres) validRingCorr = validCorr[validCorr > self.corrThres] ringFrac = nring / n # Err estimation: stat err (binomial distribution, p=ringFrac) statVar = ringFrac * (1 - ringFrac) / n fracStd = math.sqrt(statVar) statCorrVar = np.var(validCorr) / n corrStd = math.sqrt(statCorrVar) statRingCorrVar = np.var(validRingCorr) / nring ringCorrStd = math.sqrt(statRingCorrVar) # Plotting plt.style.use('ggplot') plt.figure(figsize=(10, 7.5)) plt.bar(x, y, align='center', width=(x[1] - x[0]), color="#3F5D7D") plt.plot((self.corrThres, self.corrThres), (0, np.max(y)), '--', color='r', linewidth=2) text = ('Pearson coefficient threshold = {0:.2f} \n' 'n = {1}; nrings = {2} \n' 'PSS fraction = {3:.2f} $\pm$ {4:.2f} \n' 'mean coefficient = {5:.3f} $\pm$ {6:.3f}\n' 'mean ring coefficient = {7:.3f} $\pm$ {8:.3f}') text = text.format(self.corrThres, n, nring, np.mean(ringFracs), fracStd, np.mean(meanCorrs), corrStd, np.mean(meanRingCorrs), ringCorrStd) plt.text(0.75 * plt.axis()[1], 0.83 * plt.axis()[3], text, horizontalalignment='center', verticalalignment='center', bbox=dict(facecolor='white'), fontsize=20) plt.xlabel('Pearson correlation coefficient', fontsize=35) plt.tick_params(axis='both', labelsize=25) plt.grid() plt.tight_layout() plt.savefig(os.path.join(resultsDir, folder + 'corr_hist.pdf'), dpi=300) plt.savefig(os.path.join(resultsDir, folder + 'corr_hist.png'), dpi=300) plt.close() folder = os.path.split(path)[1] text = 'Folder ' + folder + ' done in {0:.0f} seconds' print(text.format(time.time() - t0)) self.folderStatus.setText(text.format(time.time() - t0)) self.fileStatus.setText(' ') except IndexError: self.fileStatus.setText('No file selected!')
def updateConfig(self): tools.saveConfig(self)