def _visualizeMatrix(self, e=None): views = [] self.isComputed() inputCTFs = self.protocol.inputCTFs shape = len(inputCTFs) #number of methods _matrix = np.zeros(shape=(shape, shape)) ticksLablesMajorX = [None] * shape ticksLablesMajorY = [None] * shape for ctf in self.setOfCTFsConst: m1 = int(self.extract(ctf.getAttributeValue('method1'))) - 1 m2 = int(self.extract(ctf.getAttributeValue('method2'))) - 1 _matrix[m1][m2] += 1 _matrix[m2][m1] = _matrix[m1][m2] #rather inefficient but since outputCTF is not ordered... if ticksLablesMajorX[m1] is None: ticksLablesMajorX[m1] = "(%d)" % (m1 + 1) ticksLablesMajorY[m1] = ctf.getAttributeValue('method1') if ticksLablesMajorX[m2] is None: ticksLablesMajorX[m2] = "(%d)" % (m2 + 1) ticksLablesMajorY[m2] = ctf.getAttributeValue('method2') plotter = EmPlotter(fontsize=14) resolution = self.resolutionThreshold.get() plotter.createSubPlot( "# micrographs with resolution (A)\n lower than %d" % (resolution), "Method #", "Method") # plotter.plotMatrix(_matrix,cmap='seismic_r' plotter.plotMatrix(_matrix, cmap='Greens', xticksLablesMajor=ticksLablesMajorX, rotationX=0, yticksLablesMajor=ticksLablesMajorY) views.append(plotter) return views
def visualize(self, obj, **kwargs): model = PKPDAllometricScale() model.load(obj.fnScale.get()) x = np.log10(np.asarray(model.X)) xlabel = "%s [%s]" % (model.predictor, model.predictorUnits) for varName, varUnits in model.scaled_vars: plotter = EmPlotter() y = np.log10(np.asarray(model.Y[varName])) ylabel = "%s [%s]" % (varName, varUnits) ax = plotter.createSubPlot(varName, xlabel, ylabel) ax.plot(x, y, '.', label='Species') ax.plot(x, np.log10(model.models[varName][0])+x*model.models[varName][1],'r', label='R2=%f'%model.qualifiers[varName][0]) leg = ax.legend(loc='upper right') if leg: leg.draggable() plotter.show() for varName, varUnits in model.averaged_vars: plotter = EmPlotter() y = np.asarray(model.Y[varName]) ylabel = "%s [%s]" % (varName, varUnits) ax = plotter.createSubPlot("Scatter Plot", xlabel, ylabel) ax.plot(x, y, '.', label='Species') ax.plot(x, model.models[varName][0]*np.ones(x.shape),'r',label='Std=%f'%model.qualifiers[varName][0]) leg = ax.legend(loc='upper right') if leg: leg.draggable() plotter.show()
def _visualizeMatrix(self,e=None): views = [] self.isComputed() inputCTFs=self.protocol.inputCTFs shape=len(inputCTFs)#number of methods _matrix = np.zeros(shape=(shape, shape)) ticksLablesMajorX=[None] * shape ticksLablesMajorY=[None] * shape for ctf in self.setOfCTFsConst: m1 = int(self.extract(ctf.getAttributeValue('method1')))-1 m2 = int(self.extract(ctf.getAttributeValue('method2')))-1 _matrix[m1][m2] += 1 _matrix[m2][m1] = _matrix[m1][m2] #rather inefficient but since outputCTF is not ordered... if ticksLablesMajorX[m1] is None: ticksLablesMajorX[m1] = "(%d)"%(m1+1) ticksLablesMajorY[m1] = ctf.getAttributeValue('method1') if ticksLablesMajorX[m2] is None: ticksLablesMajorX[m2] = "(%d)"%(m2+1) ticksLablesMajorY[m2] = ctf.getAttributeValue('method2') plotter = EmPlotter(fontsize=14) resolution=self.resolutionThreshold.get() plotter.createSubPlot("# micrographs with resolution (A)\n lower than %d"%(resolution), "Method #", "Method") # plotter.plotMatrix(_matrix,cmap='seismic_r' plotter.plotMatrix(_matrix,cmap='Greens' , xticksLablesMajor=ticksLablesMajorX,rotationX=0 , yticksLablesMajor=ticksLablesMajorY) views.append(plotter) return views
def _visualizeHistogram(self, e=None): views = [] self.isComputed() numberOfBins = self.visualizeHistogram.get() numberOfBins = min(numberOfBins, self.setOfCTFsConst.getSize()) plotter = EmPlotter() plotter.createSubPlot("Resolution Discrepancies histogram", "Resolution (A)", "# of Comparisons") resolution = [ctf.resolution.get() for ctf in self.setOfCTFsConst] plotter.plotHist(resolution, nbins=numberOfBins) return views.append(plotter)
def _visualizeHistogram(self, e=None): views = [] self.isComputed() numberOfBins = self.visualizeHistogram.get() numberOfBins = min(numberOfBins, self.setOfCTFsConst.getSize()) plotter = EmPlotter() plotter.createSubPlot("Resolution Discrepancies histogram", "Resolution (A)", "# of Comparisons") resolution = [ctf.resolution.get() for ctf in self.setOfCTFsConst] plotter.plotHist(resolution, nbins=numberOfBins) return views.append(plotter)
def _visualizeHistogram(self, e=None): views = [] numberOfBins = self.visualizeHistogram.get() numberOfBins = min(numberOfBins, self.protocol.outputCTF.getSize()) plotter = EmPlotter() plotter.createSubPlot("Resolution Discrepancies histogram", "Resolution (A)", "# of Comparisons") resolution = [ctf.getResolution() for ctf in self.protocol.outputCTF] plotter.plotHist(resolution, nbins=numberOfBins) # ROB: why do I need this show? plotter.show() return views.append(plotter)
def _plotHistogram(self, param=None): imageFile = self.protocol._getFileName(FN_RESOLMAP) img = ImageHandler().read(imageFile) imgData = img.getData() imgList = imgData.flatten() imgListNoZero = filter(lambda x: x > 0, imgList) nbins = 30 plotter = EmPlotter(x=1, y=1, mainTitle=" ") plotter.createSubPlot("Resolution histogram", "Resolution (A)", "# of Counts") fig = plotter.plotHist(imgListNoZero, nbins) return [plotter]
def _plotHistogram(self, param=None): """ First we parse the MSA plt: first column: cumulative percent. second column: iteration number. """ iters = [] cumPercents = [] fn = self.protocol.getOutputPlt() with open(fn) as f: lines_after_2 = f.readlines()[2:] for line in lines_after_2: values = line.split() cumPercents.append(float(values[0])) iters.append(int(float(values[1]))) f.close() width = 0.85 xplotter = EmPlotter() a = xplotter.createSubPlot( 'Behaviour of sum of eigenvalues during analysis', 'Iteration number', '%') a.bar(iters, cumPercents, width, color='b') return [xplotter]
def _showSSNR(self, paramName=None): nrefs = len(self._refsList) gridsize = self._getGridSize(nrefs) xplotter = EmPlotter(x=gridsize[0], y=gridsize[1]) for ref3d in self._refsList: plot_title = 'Resolution SSNR, for Class %s' % ref3d a = xplotter.createSubPlot(plot_title, 'Angstroms^-1', 'sqrt(SSNR)', yformat=False) legendName = [] for it in self._iterations: if self.protocol.IS_REFINE: fn = self.protocol._getFileName('output_vol_par', iter=it) else: fn = self.protocol._getFileName('output_vol_par_class', iter=it, ref=ref3d) if exists(fn): self._plotSSNR(a, fn) legendName.append('iter %d' % it) xplotter.showLegend(legendName) a.grid(True) return [xplotter]
def _visualize(self, obj, **kwargs): fnResults = self.protocol._getPath("results.txt") if exists(fnResults): X, Y, _, _ = self.protocol.getXYValues(False) minX = min(X) maxX = max(X) step = (maxX - minX) / 50 xValues = np.arange(minX, maxX + step, step) yValues = self.protocol.evalFunction(xValues) plotter = EmPlotter() varNameX = self.protocol.labelX.get() varNameY = self.protocol.labelY.get() ax = plotter.createSubPlot( "Regression Plot", "%s [%s]" % (varNameX, strUnit( self.protocol.experiment.variables[varNameX].units.unit)), "%s [%s]" % (varNameY, strUnit( self.protocol.experiment.variables[varNameY].units.unit))) ax.plot(xValues, yValues) ax.plot(X, Y, 'o') return [plotter] else: return [ self.errorMessage("Result file '%s' not produced yet. " % fnResults) ]
def createCtfPlot(ctfSet, ctfId): from pyworkflow.utils.path import removeExt ctfModel = ctfSet[ctfId] psdFn = ctfModel.getPsdFile() fn = removeExt(psdFn) + "_avrot.txt" gridsize = [1, 1] xplotter = EmPlotter(x=gridsize[0], y=gridsize[1], windowTitle='CTF Fitting') plot_title = "CTF Fitting" a = xplotter.createSubPlot(plot_title, 'pixels^-1', 'CTF', yformat=False) legendName = [] for i in range(1, 5): _plotCurve(a, i, fn) if i == 1: legendName.append('rotational avg. No Astg') elif i == 2: legendName.append('rotational avg.') elif i == 3: legendName.append('CTF Fit') elif i == 4: legendName.append('Cross Correlation') elif i == 5: legendName.append('2sigma cross correlation of noise') xplotter.showLegend(legendName) a.grid(True) xplotter.show()
def _plotFactorMaps(self, param=None): # Parse the file fn = self.protocol._getFileName('imcFile') f = open(fn) values = f.readline().split() n = int(values[0]) # Number of images nf = int(values[1]) # Number of factors x = self.firstFactor.get() y = self.secondFactor.get() xFactors = [] yFactors = [] i = 0 while i < n: imgFactors = [] while len(imgFactors) < nf: values = f.readline().split() imgFactors += [float(v) for v in values] xFactors.append(imgFactors[x-1]) yFactors.append(imgFactors[y-1]) i += 1 f.close() # Create the plot xplotter = EmPlotter(1,1) a = xplotter.createSubPlot("Factor %d vs %d" % (x, y), "Factor %d" % x, "Factor %d" % y) a.plot(xFactors, yFactors, 'o') return [xplotter]
def visualize(self, obj, **kwargs): prot = obj fnProfiles = prot._getPath("profiles.txt") fh = open(fnProfiles,"r") state = 0 legends = ['Iliver', 'Iinlet', 'Isys'] for line in fh: if state==0: tokens = line.split("::") title = tokens[1].strip() I3=[] state=1 elif state==1: tokens=line.strip().split() if len(tokens)==0: plotter = EmPlotter() ax = plotter.createSubPlot("Simulation", "t [h]", "[I] [mg/mL]") t = np.asarray(I3[0],dtype=np.float64)/60 for n in range(1,len(I3)): y = np.asarray(I3[n],dtype=np.float64) ax.plot(t, y, label=legends[n-1]) ax.legend() plotter.show() state=0 else: if len(I3)==0: for n in range(len(tokens)): I3.append([]) for n in range(len(tokens)): I3[n].append(tokens[n]) fh.close()
def _visualize(self, obj, **kwargs): fnResults = self.protocol._getPath("results.txt") if exists(fnResults): X, Y, _, _ = self.protocol.getXYValues(False) minX = min(X) maxX = max(X) step = (maxX - minX) / 50 xValues = np.arange(minX, maxX+step, step) yValues = self.protocol.evalFunction(xValues) plotter = EmPlotter() varNameX = self.protocol.labelX.get() varNameY = self.protocol.labelY.get() ax = plotter.createSubPlot("Regression Plot", "%s [%s]"%(varNameX,strUnit(self.protocol.experiment.variables[varNameX].units.unit)), "%s [%s]"%(varNameY,strUnit(self.protocol.experiment.variables[varNameY].units.unit))) ax.plot(xValues, yValues) ax.plot(X, Y, 'o') return [plotter] else: return [self.errorMessage("Result file '%s' not produced yet. " % fnResults)]
def _showFSC(self, paramName=None): threshold = self.resolutionThresholdFSC.get() nrefs = len(self._refsList) gridsize = self._getGridSize(nrefs) xplotter = EmPlotter(x=gridsize[0], y=gridsize[1], windowTitle='Resolution FSC') if self.protocol.IS_REFINE: plot_title = 'FSC' a = xplotter.createSubPlot(plot_title, 'Angstroms^-1', 'FSC', yformat=False) legends = [] show = False for it in self._iterations: parFn = self.protocol._getFileName('output_vol_par', iter=it) if exists(parFn): show = True self._plotFSC(a, parFn) legends.append('iter %d' % it) xplotter.showLegend(legends) if show: if threshold < self.maxFrc: a.plot([self.minInv, self.maxInv],[threshold, threshold], color='black', linestyle='--') a.grid(True) else: raise Exception("Set a valid iteration to show its FSC") else: for ref3d in self._refsList: plot_title = 'class %s' % ref3d a = xplotter.createSubPlot(plot_title, 'Angstroms^-1', 'FSC', yformat=False) legends = [] for it in self._iterations: parFn = self.protocol._getFileName('output_vol_par_class', iter=it, ref=ref3d) if exists(parFn): show = True self._plotFSC(a, parFn) legends.append('iter %d' % it) xplotter.showLegend(legends) if show: if threshold < self.maxFrc: a.plot([self.minInv, self.maxInv],[threshold, threshold], color='black', linestyle='--') a.grid(True) else: raise Exception("Set a valid iteration to show its FSC") return [xplotter]
def _showFSC(self, paramName=None): threshold = self.resolutionThresholdFSC.get() gridsize = self._getGridSize(1) xplotter = EmPlotter(x=gridsize[0], y=gridsize[1], windowTitle='Resolution FSC') plot_title = 'FSC' a = xplotter.createSubPlot(plot_title, 'Angstroms^-1', 'FSC', yformat=False) legends = [] show = False for it in self._iterations: if self.resolutionPlotsFSC.get() == FSC_UNMASK: fscUnmask = self.protocol._getFileName('fscUnmasked',run=self.protocol._getRun(), iter=it) if os.path.exists(fscUnmask): show = True self._plotFSC(a, fscUnmask) legends.append('unmasked map iter %d' % it) xplotter.showLegend(legends) elif self.resolutionPlotsFSC.get() == FSC_MASK: fscMask = self.protocol._getFileName('fscMasked',run=self.protocol._getRun(), iter=it) if os.path.exists(fscMask): show = True self._plotFSC(a, fscMask) legends.append('masked map iter %d' % it) xplotter.showLegend(legends) elif self.resolutionPlotsFSC.get() == FSC_MASKTIGHT: fscMaskTight = self.protocol._getFileName('fscMaskedTight',run=self.protocol._getRun(), iter=it) if os.path.exists(fscMaskTight): show = True self._plotFSC(a, fscMaskTight) legends.append('masked tight map iter %d' % it) xplotter.showLegend(legends) elif self.resolutionPlotsFSC.get() == FSC_ALL: fscUnmask = self.protocol._getFileName('fscUnmasked',run=self.protocol._getRun(), iter=it) fscMask = self.protocol._getFileName('fscMasked',run=self.protocol._getRun(), iter=it) fscMaskTight = self.protocol._getFileName('fscMaskedTight',run=self.protocol._getRun(), iter=it) if os.path.exists(fscUnmask): show = True self._plotFSC(a, fscUnmask) legends.append('unmasked map iter %d' % it) self._plotFSC(a, fscMask) legends.append('masked map iter %d' % it) self._plotFSC(a, fscMaskTight) legends.append('masked tight map iter %d' % it) xplotter.showLegend(legends) if show: if threshold < self.maxFrc: a.plot([self.minInv, self.maxInv],[threshold, threshold], color='black', linestyle='--') a.grid(True) else: raise Exception("Set a valid iteration to show its FSC") return [xplotter]
def _showFSC(self, paramName=None): threshold = self.resolutionThresholdFSC.get() gridsize = self._getGridSize(1) xplotter = EmPlotter(x=gridsize[0], y=gridsize[1], windowTitle='Resolution FSC') plot_title = 'FSC' a = xplotter.createSubPlot(plot_title, 'Angstroms^-1', 'FSC', yformat=False) legends = [] show = False for it in self._iterations: if self.resolutionPlotsFSC.get() == FSC_UNMASK: fscUnmask = self.protocol._getFileName('fscUnmasked',run=self.protocol._getRun(), iter=it) if os.path.exists(fscUnmask): show = True self._plotFSC(a, fscUnmask) legends.append('unmasked map iter %d' % it) xplotter.showLegend(legends) elif self.resolutionPlotsFSC.get() == FSC_MASK: fscMask = self.protocol._getFileName('fscMasked',run=self.protocol._getRun(), iter=it) if os.path.exists(fscMask): show = True self._plotFSC(a, fscMask) legends.append('masked map iter %d' % it) xplotter.showLegend(legends) elif self.resolutionPlotsFSC.get() == FSC_MASKTIGHT: fscMaskTight = self.protocol._getFileName('fscMaskedTight',run=self.protocol._getRun(), iter=it) if os.path.exists(fscMaskTight): show = True self._plotFSC(a, fscMaskTight) legends.append('masked tight map iter %d' % it) xplotter.showLegend(legends) elif self.resolutionPlotsFSC.get() == FSC_ALL: fscUnmask = self.protocol._getFileName('fscUnmasked',run=self.protocol._getRun(), iter=it) fscMask = self.protocol._getFileName('fscMasked',run=self.protocol._getRun(), iter=it) fscMaskTight = self.protocol._getFileName('fscMaskedTight',run=self.protocol._getRun(), iter=it) if os.path.exists(fscUnmask): show = True self._plotFSC(a, fscUnmask) legends.append('unmasked map iter %d' % it) self._plotFSC(a, fscMask) legends.append('masked map iter %d' % it) self._plotFSC(a, fscMaskTight) legends.append('masked tight map iter %d' % it) xplotter.showLegend(legends) if show: if threshold < self.maxFrc: a.plot([self.minInv, self.maxInv],[threshold, threshold], color='black', linestyle='--') a.grid(True) else: raise Exception("Set a valid iteration to show its FSC") return [xplotter]
def _plot(varLabelX, varLabelY, x, y, yp): plotter = EmPlotter() ax = plotter.createSubPlot("Plot", varLabelX, varLabelY) xValues = _values(x, useLog=self.useTimeLog()) ax.plot(xValues, _values(y), 'x', label="Observations") idx = np.argsort(xValues) ypValues = _values(yp) ax.plot(np.asarray(xValues)[idx], np.asarray(ypValues)[idx], 'g', label="Fit") leg = ax.legend() if leg: leg.draggable() plotter.show()
def _onPlotClick(self, e=None): selection = self.tree.selection() n = len(selection) if n < 1 or n > 2: self.showError("Select one or two variables to plot.") else: plotter = EmPlotter() varX = self._variables[selection[0]] xValues = self.observations[:, varX.index] def _label(var): return "%s [%s]" % (var.varName, var.unitStr) if n == 1: plotter.createSubPlot("Histogram", _label(varX), "Count") plotter.plotHist(xValues, 50) else: # n == 2 varY = self._variables[selection[1]] yValues = self.observations[:, varY.index] ax = plotter.createSubPlot("Scatter Plot", _label(varX), _label(varY)) ax.plot(xValues, yValues, '.') plotter.show()
def _onPlotClick(self, e=None): selection = self.tree.selection() n = len(selection) if n < 1 or n > 2: self.showError("Select one or two variables to plot.") else: plotter = EmPlotter() varX = self._variables[selection[0]] xValues = self.observations[:, varX.index] def _label(var): return "%s [%s]" % (var.varName, var.unitStr) if n == 1: plotter.createSubPlot("Histogram", _label(varX), "Count") plotter.plotHist(xValues, 50) else: # n == 2 varY = self._variables[selection[1]] yValues = self.observations[:, varY.index] ax = plotter.createSubPlot("Scatter Plot", _label(varX), _label(varY)) ax.plot(xValues, yValues, '.') plotter.show()
def visualize(self, obj, **kwargs): model = PKPDAllometricScale() model.load(obj.fnScale.get()) x = np.log10(np.asarray(model.X)) xlabel = "%s [%s]" % (model.predictor, model.predictorUnits) for varName, varUnits in model.scaled_vars: plotter = EmPlotter() y = np.log10(np.asarray(model.Y[varName])) ylabel = "%s [%s]" % (varName, varUnits) ax = plotter.createSubPlot(varName, xlabel, ylabel) ax.plot(x, y, '.', label='Species') ax.plot(x, np.log10(model.models[varName][0]) + x * model.models[varName][1], 'r', label='R2=%f' % model.qualifiers[varName][0]) leg = ax.legend(loc='upper right') if leg: leg.draggable() plotter.show() for varName, varUnits in model.averaged_vars: plotter = EmPlotter() y = np.asarray(model.Y[varName]) ylabel = "%s [%s]" % (varName, varUnits) ax = plotter.createSubPlot("Scatter Plot", xlabel, ylabel) ax.plot(x, y, '.', label='Species') ax.plot(x, model.models[varName][0] * np.ones(x.shape), 'r', label='Std=%f' % model.qualifiers[varName][0]) leg = ax.legend(loc='upper right') if leg: leg.draggable() plotter.show()
def _showGuinier(self, volume): nrefs = len(self._refsList) gridsize = self._getGridSize(nrefs) guinierFn = volume + ".guinier" d2 = self._getGuinierValue(guinierFn, 0) legends = ["lnFweighted ln(F)", "corrected ln(F)", "model"] xplotter = EmPlotter(*gridsize, windowTitle='Guinier Plots') subPlot = xplotter.createSubPlot(basename(volume), 'd^-2(A^-2)', 'ln(F)', yformat=False) for i, legend in enumerate(legends): y = self._getGuinierValue(guinierFn, i+2) subPlot.plot(d2, y) xplotter.showLegend(legends) subPlot.grid(True) return xplotter
def _showFSC(self, paramName=None): threshold = self.resolutionThresholdFSC.get() iterations = self._getIterations() groups = self._getGroups() if self.groupFSC == 0: # group by iterations files = [(it, self._getFinalPath('fscdoc_%02d.stk' % it)) for it in iterations] legendPrefix = 'iter' else: it = iterations[-1] legendPrefix = 'group' def group(f): # retrieve the group number return int(f.split('_')[-1].split('.')[0]) groupFiles = glob(self._getFinalPath('fscdoc_%02d_???.stk' % it)) groupFiles.sort() files = [(group(f), f) for f in groupFiles if group(f) in groups] if not files: #empty files return [ self.errorMessage("Please select valid groups to display", title="Wrong groups selection") ] plotter = EmPlotter(x=1, y=1, windowTitle='Resolution FSC') a = plotter.createSubPlot("FSC", 'Angstroms^-1', 'FSC', yformat=False) #fscFile = self._getFinalPath('fscdoc_%02d.stk' % iterations[0]) legends = [] for it, fscFile in files: if os.path.exists(fscFile): self._plotFSC(a, fscFile) legends.append('%s %d' % (legendPrefix, it)) else: print "Missing file: ", fscFile if threshold < self.maxfsc: a.plot([self.minInv, self.maxInv], [threshold, threshold], color='black', linestyle='--') plotter.showLegend(legends) a.grid(True) return [plotter]
def createCtfPlot(ctfSet, ctfId): ctfModel = ctfSet[ctfId] psdFn = ctfModel.getPsdFile() fn = pwutils.removeExt(psdFn) + "_EPA.txt" gridsize = [1, 1] xplotter = EmPlotter(x=gridsize[0], y=gridsize[1], windowTitle='CTF Fitting') plot_title = "CTF Fitting" a = xplotter.createSubPlot(plot_title, 'Resolution (Angstroms)', 'CTF', yformat=False) a.invert_xaxis() for i in range(1, 5): _plotCurve(a, i, fn) xplotter.showLegend(['simulated CTF', 'equiphase avg.', 'equiphase avg. - bg', 'cross correlation']) a.grid(True) xplotter.show()
def _showGuinier(self, volume): nrefs = len(self._refsList) gridsize = self._getGridSize(nrefs) guinierFn = volume + ".guinier" d2 = self._getGuinierValue(guinierFn, 0) legends = ["lnFweighted ln(F)", "corrected ln(F)", "model"] xplotter = EmPlotter(*gridsize, windowTitle='Guinier Plots') subPlot = xplotter.createSubPlot(basename(volume), 'd^-2(A^-2)', 'ln(F)', yformat=False) for i, legend in enumerate(legends): y = self._getGuinierValue(guinierFn, i + 2) subPlot.plot(d2, y) xplotter.showLegend(legends) subPlot.grid(True) return xplotter
def createCtfPlot(ctfSet, ctfId): ctfModel = ctfSet[ctfId] psdFn = ctfModel.getPsdFile() fn = removeExt(psdFn) + "_avrot.txt" gridsize = [1, 1] xplotter = EmPlotter(x=gridsize[0], y=gridsize[1], windowTitle='CTF Fitting') plot_title = "CTF Fitting" a = xplotter.createSubPlot(plot_title, 'pixels^-1', 'CTF', yformat=False) legendName = ['rotational avg. No Astg', 'rotational avg.', 'CTF Fit', 'Cross Correlation', '2sigma cross correlation of noise'] for i in range(1, 6): _plotCurve(a, i, fn) xplotter.showLegend(legendName) a.grid(True) xplotter.show()
def createCtfPlot(ctfSet, ctfId): ctfModel = ctfSet[ctfId] psdFn = ctfModel.getPsdFile() fn = removeExt(psdFn) + "_avrot.txt" gridsize = [1, 1] xplotter = EmPlotter(x=gridsize[0], y=gridsize[1], windowTitle='CTF Fitting') plot_title = "CTF Fitting" a = xplotter.createSubPlot(plot_title, 'pixels^-1', 'CTF', yformat=False) legendName = [ 'rotational avg. No Astg', 'rotational avg.', 'CTF Fit', 'Cross Correlation', '2sigma cross correlation of noise' ] for i in range(1, 6): _plotCurve(a, i, fn) xplotter.showLegend(legendName) a.grid(True) xplotter.show()
def _showSSNR(self, paramName=None): nrefs = len(self._refsList) gridsize = self._getGridSize(nrefs) xplotter = EmPlotter(x=gridsize[0], y=gridsize[1]) for ref3d in self._refsList: plot_title = 'Resolution SSNR, for Class %s' % ref3d a = xplotter.createSubPlot(plot_title, 'Angstroms^-1', 'sqrt(SSNR)', yformat=False) legendName = [] for it in self._iterations: if self.protocol.IS_REFINE: fn = self.protocol._getFileName('output_vol_par', iter=it) else: fn = self.protocol._getFileName('output_vol_par_class', iter=it, ref=ref3d) if exists(fn): self._plotSSNR(a, fn) legendName.append('iter %d' % it) xplotter.showLegend(legendName) a.grid(True) return [xplotter]
def _onPlotSummaryClick(self, e=None): sampleKeys = self.samplesTree.selection() n = len(sampleKeys) if n == 1: self.showInfo("Please select several samples to plot.") else: if n > 1: samples = [self.experiment.samples[k] for k in sampleKeys] else: samples = self.experiment.samples.values() dataDict = {} # key will be time values for s in samples: xValues, yValues = self.getPlotValues(s) for x, y in izip(xValues, yValues): if x in dataDict: dataDict[x].append(y) else: dataDict[x] = [y] sortedTime = sorted(dataDict.keys()) # We will store five values (min, 25%, 50%, 75%, max) # for each of the time entries computed percentileList = [0, 25, 50, 75, 100] Y = np.zeros((len(sortedTime), 5)) for i, t in enumerate(sortedTime): Y[i,:] = np.percentile(dataDict[t], percentileList) plotter = EmPlotter() ax = plotter.createSubPlot("Summary Plot", self.getTimeLabel(), self.getMeasureLabel()) ax.plot(sortedTime, Y[:, 0], 'r--', label="Minimum") ax.plot(sortedTime, Y[:, 1], 'b--', label="25%") ax.plot(sortedTime, Y[:, 2], 'g', label="50% (Median)") ax.plot(sortedTime, Y[:, 3], 'b--', label="75%") ax.plot(sortedTime, Y[:, 4], 'r--', label="Maximum") ax.legend() plotter.show()
def _showFSC(self, paramName=None): threshold = self.resolutionThresholdFSC.get() iterations = self._getIterations() groups = self._getGroups() if self.groupFSC == 0: # group by iterations files = [(it, self._getFinalPath('fscdoc_%02d.stk' % it)) for it in iterations] legendPrefix = 'iter' else: it = iterations[-1] legendPrefix = 'group' def group(f): # retrieve the group number return int(f.split('_')[-1].split('.')[0]) groupFiles = glob(self._getFinalPath('fscdoc_%02d_???.stk' % it)) groupFiles.sort() files = [(group(f), f) for f in groupFiles if group(f) in groups] if not files: #empty files return [self.errorMessage("Please select valid groups to display", title="Wrong groups selection")] plotter = EmPlotter(x=1, y=1, windowTitle='Resolution FSC') a = plotter.createSubPlot("FSC", 'Angstroms^-1', 'FSC', yformat=False) #fscFile = self._getFinalPath('fscdoc_%02d.stk' % iterations[0]) legends = [] for it, fscFile in files: if os.path.exists(fscFile): self._plotFSC(a, fscFile) legends.append('%s %d' % (legendPrefix, it)) else: print "Missing file: ", fscFile if threshold < self.maxfsc: a.plot([self.minInv, self.maxInv],[threshold, threshold], color='black', linestyle='--') plotter.showLegend(legends) a.grid(True) return [plotter]
def createCtfPlot(ctfSet, ctfId): ctfModel = ctfSet[ctfId] psdFn = ctfModel.getPsdFile() fn = pwutils.removeExt(psdFn) + "_EPA.txt" gridsize = [1, 1] xplotter = EmPlotter(x=gridsize[0], y=gridsize[1], windowTitle='CTF Fitting') plot_title = "CTF Fitting" a = xplotter.createSubPlot(plot_title, 'Resolution (Angstroms)', 'CTF', yformat=False) a.invert_xaxis() for i in range(1, 5): _plotCurve(a, i, fn) xplotter.showLegend([ 'simulated CTF', 'equiphase avg.', 'equiphase avg. - bg', 'cross correlation' ]) a.grid(True) xplotter.show()
def _plotHistogram(self, param=None): """ First we parse the MSA plt: first column: cumulative percent. second column: iteration number. """ iters = [] cumPercents = [] fn = self.protocol.getOutputPlt() with open(fn) as f: lines_after_2 = f.readlines()[2:] for line in lines_after_2: values = line.split() cumPercents.append(float(values[0])) iters.append(int(float(values[1]))) f.close() width = 0.85 xplotter = EmPlotter() a = xplotter.createSubPlot('Behaviour of sum of eigenvalues during analysis', 'Iteration number', '%') a.bar(iters, cumPercents, width, color='b') return [xplotter]
def _plotHistogram(self, param=None): """ First we parse the cas_EIG file and we read: first line: take the number of eigen values. then one line per factor and we read the percent and cumulative percent. """ from numpy import arange from matplotlib.ticker import FormatStrFormatter fn = self.protocol._getFileName('eigFile') f = open(fn) values = f.readline().split() n = int(values[0]) # Number of factors factors = arange(1, n+1) percents = [] cumPercents = [] for i in factors: values = f.readline().split() percents.append(float(values[1])) cumPercents.append(float(values[2])) f.close() width = 0.85 xplotter = EmPlotter() a = xplotter.createSubPlot('Eigenvalues histogram', 'Eigenvalue number', '%') a.set_xticks(factors + 0.45) a.xaxis.set_major_formatter(FormatStrFormatter('%1.0f')) bars = a.bar(factors, percents, width, color='b') for i, rect in enumerate(bars): h = rect.get_height() a.text(rect.get_x()+rect.get_width()/2., h+0.3, '%d' % cumPercents[i], ha='center', va='bottom') a.set_ylim([0, percents[0] + 5]) return [xplotter]
class PKPDResponsiveDialog(dialog.Dialog): def __init__(self, parent, title, **kwargs): """ From kwargs: message: message tooltip to show when browsing. selected: the item that should be selected. validateSelectionCallback: a callback function to validate selected items. """ self.values = [] self.plotter = None self.targetProtocol = kwargs['targetProtocol'] self.experiment = self.targetProtocol.experiment self.varNameX = kwargs['varNameX'] self.varNameY = kwargs['varNameY'] self.provider = SamplesTreeProvider(self.experiment) self.model = self.loadModel() self.validateSelectionCallback = kwargs.get( 'validateSelectionCallback', None) self.setLabels() dialog.Dialog.__init__(self, parent, title, buttons=[('Select', dialog.RESULT_YES), ('Cancel', dialog.RESULT_CANCEL)]) def setLabels(self): pass def loadModel(self): model = self.targetProtocol.createModel() model.setExperiment(self.experiment) # if hasattr(self.protODE, "deltaT"): # model.deltaT = self.protODE.deltaT.get() model.setXVar(self.varNameX) model.setYVar(self.varNameY) return model def body(self, bodyFrame): bodyFrame.config(bg='white') gui.configureWeigths(bodyFrame) self._createSamplesFrame(bodyFrame) self._createSlidersFrame(bodyFrame) self._createLogsFrame(bodyFrame) def _createSamplesFrame(self, content): frame = tk.Frame(content, bg='white') #frame = tk.LabelFrame(content, text='General') lfSamples = tk.LabelFrame(frame, text="Samples", bg='white') gui.configureWeigths(frame) lfSamples.grid(row=0, column=0, sticky='news', padx=5, pady=5) self.samplesTree = self._addBoundTree(lfSamples, self.provider, 10) self.samplesTree.itemClick = self._onSampleChanged frame.grid(row=0, column=0, sticky='news', padx=5, pady=5) def _createSlidersFrame(self, content): frame = tk.Frame(content, bg='white') lfBounds = tk.LabelFrame(frame, text="Parameter Bounds", bg='white') gui.configureWeigths(frame) i = 0 self.sliders = {} paramUnits = self.targetProtocol.parameterUnits for paramName, bounds in self.targetProtocol.getParameterBounds( ).iteritems(): bounds = bounds or (0, 1) slider = MinMaxSlider(lfBounds, "%s [%s]" % (paramName, strUnit(paramUnits[i])), bounds[0], bounds[1], callback=self._onVarChanged) slider.grid(row=i, column=0, padx=5, pady=5) self.sliders[paramName] = slider i += 1 lfBounds.grid(row=0, column=0, sticky='news', padx=5, pady=5) frame.grid(row=0, column=1, sticky='news', padx=5, pady=5) def _createLogsFrame(self, content): frame = tk.Frame(content) def addVar(text, col, varName): varFrame = tk.Frame(frame) varFrame.grid(row=0, column=col, sticky='new') label = tk.Label(varFrame, text=text) #, font=self.fontBold) label.grid(row=0, column=0, padx=5, pady=2, sticky='nw') combo = tk.Label(varFrame, text=varName, width=10) combo.grid(row=0, column=1, sticky='nw', padx=5, pady=5) radioVar = tk.IntVar() radio = tk.Checkbutton(varFrame, text='Log10', variable=radioVar) radio.grid(row=0, column=2, sticky='nw', padx=5, pady=5) return combo, radio, radioVar self.timeWidget = addVar('Time variable', 0, self.varNameX) self.timeWidget[2].trace('w', self._onLogChanged) self.measureWidget = addVar('Measure variable', 1, self.varNameY) measureVar = self.measureWidget[2] measureVar.set(True) measureVar.trace('w', self._onLogChanged) frame.grid(row=1, column=0, columnspan=2, sticky='news', padx=5, pady=5) def _addBoundTree(self, parent, provider, height): bt = BoundTree(parent, provider, height=height) bt.grid(row=0, column=0, sticky='news', padx=5, pady=5) gui.configureWeigths(parent) return bt def apply(self): self.values = [] def _onVarChanged(self, *args): sampleKeys = self.samplesTree.selection() if sampleKeys: self.computeFit() self.plotResults() else: dialog.showInfo("Warning", "Please select some sample(s) to plot.", self) def computeFit(self): currentParams = [] for paramName in self.targetProtocol.getParameterNames(): currentParams.append(self.sliders[paramName].getValue()) self.targetProtocol.setParameters(currentParams) self.ypValues = self.targetProtocol.forwardModel( currentParams, self.xpValues) def getBoundsList(self): boundList = [] for paramName in self.targetProtocol.getParameterNames(): boundList.append(self.sliders[paramName].getMinMax()) return boundList def useTimeLog(self): return self.timeWidget[2].get() def useMeasureLog(self): return self.measureWidget[2].get() def getUnits(self, varName): return self.experiment.variables[varName].getUnitsString() def getLabel(self, varName, useLog): varLabel = '%s [%s]' % (varName, self.getUnits(varName)) if useLog: varLabel = "log10(%s)" % varLabel return varLabel def getTimeLabel(self): return self.getLabel(self.varNameX, self.useTimeLog()) def getMeasureLabel(self): return self.getLabel(self.varNameY, self.useMeasureLog()) def computePlotValues(self, xValues, yValues): useMeasureLog = self.useMeasureLog() useTimeLog = self.useTimeLog() if not (useMeasureLog or useTimeLog): newXValues = xValues newYValues = yValues else: # If log will be used either for time or measure var # we need to filter elements larger than 0 newXValues = [] newYValues = [] def _value(v, useLog): if useLog: return math.log10(v) if v > 0 else None return v for x, y in izip(xValues, yValues): x = _value(x, useTimeLog) y = _value(y, useMeasureLog) if x is not None and y is not None: newXValues.append(x) newYValues.append(y) return newXValues, newYValues def _updateModel(self): """ This function should be called whenever the sample changes """ pass def _onLogChanged(self, *args): # We will treat a log change as a sample change to plot self._onSampleChanged() def _onSampleChanged(self, e=None): sampleKeys = self.samplesTree.selection() if sampleKeys: # When the sample is changed we need to re-compute (with log or not) # the x, y values self.sample = self.experiment.samples[sampleKeys[0]] self.xValues, self.yValues = self.sample.getXYValues( self.varNameX, self.varNameY) self.newXValues, self.newYValues = self.computePlotValues( self.xValues[0], self.yValues[0]) self._updateModel() self.computeFit() self.plotResults() else: dialog.showInfo("Warning", "Please select some sample(s) to plot.", self) def plotResults(self): if self.plotter is None or self.plotter.isClosed(): self.plotter = EmPlotter() doShow = True else: doShow = False ax = self.plotter.getLastSubPlot() self.plotter.clear() ax = self.plotter.createSubPlot("Sample: %s" % self.sample.sampleName, self.getTimeLabel(), self.getMeasureLabel()) self.newXPValues, self.newYPValues = self.computePlotValues( self.xpValues[0], self.ypValues[0]) ax.plot(self.newXValues, self.newYValues, 'x', label="Observations") ax.plot(self.newXPValues, self.newYPValues, label="Fit") ax.legend() if doShow: self.plotter.show() else: self.plotter.draw() def destroy(self): """Destroy the window""" if not (self.plotter is None or self.plotter.isClosed()): self.plotter.close() dialog.Dialog.destroy(self)
class PKPDResponsiveDialog(dialog.Dialog): def __init__(self, parent, title, **kwargs): """ From kwargs: message: message tooltip to show when browsing. selected: the item that should be selected. validateSelectionCallback: a callback function to validate selected items. """ self.values = [] self.plotter = None self.targetProtocol = kwargs['targetProtocol'] self.experiment = self.targetProtocol.experiment self.varNameX = kwargs['varNameX'] self.varNameY = kwargs['varNameY'] self.provider = SamplesTreeProvider(self.experiment) self.model = self.loadModel() self.validateSelectionCallback = kwargs.get('validateSelectionCallback', None) self.setLabels() dialog.Dialog.__init__(self, parent, title, buttons=[('Select', dialog.RESULT_YES), ('Cancel', dialog.RESULT_CANCEL)]) def setLabels(self): pass def loadModel(self): model = self.targetProtocol.createModel() model.setExperiment(self.experiment) # if hasattr(self.protODE, "deltaT"): # model.deltaT = self.protODE.deltaT.get() model.setXVar(self.varNameX) model.setYVar(self.varNameY) return model def body(self, bodyFrame): bodyFrame.config(bg='white') gui.configureWeigths(bodyFrame) self._createSamplesFrame(bodyFrame) self._createSlidersFrame(bodyFrame) self._createLogsFrame(bodyFrame) def _createSamplesFrame(self, content): frame = tk.Frame(content, bg='white') #frame = tk.LabelFrame(content, text='General') lfSamples = tk.LabelFrame(frame, text="Samples", bg='white') gui.configureWeigths(frame) lfSamples.grid(row=0, column=0, sticky='news', padx=5, pady=5) self.samplesTree = self._addBoundTree(lfSamples, self.provider, 10) self.samplesTree.itemClick = self._onSampleChanged frame.grid(row=0, column=0, sticky='news', padx=5, pady=5) def _createSlidersFrame(self, content): frame = tk.Frame(content, bg='white') lfBounds = tk.LabelFrame(frame, text="Parameter Bounds", bg='white') gui.configureWeigths(frame) i = 0 self.sliders = {} paramUnits = self.targetProtocol.parameterUnits for paramName, bounds in self.targetProtocol.getParameterBounds().iteritems(): bounds = bounds or (0, 1) slider = MinMaxSlider(lfBounds, "%s [%s]"%(paramName,strUnit(paramUnits[i])), bounds[0], bounds[1], callback=self._onVarChanged) slider.grid(row=i, column=0, padx=5, pady=5) self.sliders[paramName] = slider i += 1 lfBounds.grid(row=0, column=0, sticky='news', padx=5, pady=5) frame.grid(row=0, column=1, sticky='news', padx=5, pady=5) def _createLogsFrame(self, content): frame = tk.Frame(content) def addVar(text, col, varName): varFrame = tk.Frame(frame) varFrame.grid(row=0, column=col, sticky='new') label = tk.Label(varFrame, text=text)#, font=self.fontBold) label.grid(row=0, column=0, padx=5, pady=2, sticky='nw') combo = tk.Label(varFrame, text=varName, width=10) combo.grid(row=0, column=1, sticky='nw', padx=5, pady=5) radioVar = tk.IntVar() radio = tk.Checkbutton(varFrame, text='Log10', variable=radioVar) radio.grid(row=0, column=2, sticky='nw', padx=5, pady=5) return combo, radio, radioVar self.timeWidget = addVar('Time variable', 0, self.varNameX) self.timeWidget[2].trace('w', self._onLogChanged) self.measureWidget = addVar('Measure variable', 1, self.varNameY) measureVar = self.measureWidget[2] measureVar.set(True) measureVar.trace('w', self._onLogChanged) frame.grid(row=1, column=0, columnspan=2, sticky='news', padx=5, pady=5) def _addBoundTree(self, parent, provider, height): bt = BoundTree(parent, provider, height=height) bt.grid(row=0, column=0, sticky='news', padx=5, pady=5) gui.configureWeigths(parent) return bt def apply(self): self.values = [] def _onVarChanged(self, *args): sampleKeys = self.samplesTree.selection() if sampleKeys: self.computeFit() self.plotResults() else: dialog.showInfo("Warning","Please select some sample(s) to plot.",self) def computeFit(self): currentParams = [] for paramName in self.targetProtocol.getParameterNames(): currentParams.append(self.sliders[paramName].getValue()) self.targetProtocol.setParameters(currentParams) self.ypValues = self.targetProtocol.forwardModel(currentParams, self.xpValues) def getBoundsList(self): boundList = [] for paramName in self.targetProtocol.getParameterNames(): boundList.append(self.sliders[paramName].getMinMax()) return boundList def useTimeLog(self): return self.timeWidget[2].get() def useMeasureLog(self): return self.measureWidget[2].get() def getUnits(self, varName): return self.experiment.variables[varName].getUnitsString() def getLabel(self, varName, useLog): varLabel = '%s [%s]' % (varName, self.getUnits(varName)) if useLog: varLabel = "log10(%s)" % varLabel return varLabel def getTimeLabel(self): return self.getLabel(self.varNameX, self.useTimeLog()) def getMeasureLabel(self): return self.getLabel(self.varNameY, self.useMeasureLog()) def computePlotValues(self, xValues, yValues): useMeasureLog = self.useMeasureLog() useTimeLog = self.useTimeLog() if not (useMeasureLog or useTimeLog): newXValues = xValues newYValues = yValues else: # If log will be used either for time or measure var # we need to filter elements larger than 0 newXValues = [] newYValues = [] def _value(v, useLog): if useLog: return math.log10(v) if v > 0 else None return v for x, y in izip(xValues, yValues): x = _value(x, useTimeLog) y = _value(y, useMeasureLog) if x is not None and y is not None: newXValues.append(x) newYValues.append(y) return newXValues, newYValues def _updateModel(self): """ This function should be called whenever the sample changes """ pass def _onLogChanged(self, *args): # We will treat a log change as a sample change to plot self._onSampleChanged() def _onSampleChanged(self, e=None): sampleKeys = self.samplesTree.selection() if sampleKeys: # When the sample is changed we need to re-compute (with log or not) # the x, y values self.sample = self.experiment.samples[sampleKeys[0]] self.xValues, self.yValues = self.sample.getXYValues(self.varNameX, self.varNameY) self.newXValues, self.newYValues = self.computePlotValues(self.xValues[0], self.yValues[0]) self._updateModel() self.computeFit() self.plotResults() else: dialog.showInfo("Warning","Please select some sample(s) to plot.",self) def plotResults(self): if self.plotter is None or self.plotter.isClosed(): self.plotter = EmPlotter() doShow = True else: doShow = False ax = self.plotter.getLastSubPlot() self.plotter.clear() ax = self.plotter.createSubPlot("Sample: %s" % self.sample.sampleName, self.getTimeLabel(), self.getMeasureLabel()) self.newXPValues, self.newYPValues = self.computePlotValues(self.xpValues[0], self.ypValues[0]) ax.plot(self.newXValues, self.newYValues, 'x', label="Observations") ax.plot(self.newXPValues, self.newYPValues, label="Fit") ax.legend() if doShow: self.plotter.show() else: self.plotter.draw() def destroy(self): """Destroy the window""" if not (self.plotter is None or self.plotter.isClosed()): self.plotter.close() dialog.Dialog.destroy(self)
def _showFSC(self, paramName=None): threshold = self.resolutionThresholdFSC.get() nrefs = len(self._refsList) gridsize = self._getGridSize(nrefs) xplotter = EmPlotter(x=gridsize[0], y=gridsize[1], windowTitle='Resolution FSC') if self.protocol.IS_REFINE: plot_title = 'FSC' a = xplotter.createSubPlot(plot_title, 'Angstroms^-1', 'FSC', yformat=False) legends = [] show = False for it in self._iterations: parFn = self.protocol._getFileName('output_vol_par', iter=it) if exists(parFn): show = True self._plotFSC(a, parFn) legends.append('iter %d' % it) xplotter.showLegend(legends) if show: if threshold < self.maxFrc: a.plot([self.minInv, self.maxInv], [threshold, threshold], color='black', linestyle='--') a.grid(True) else: raise Exception("Set a valid iteration to show its FSC") else: for ref3d in self._refsList: plot_title = 'class %s' % ref3d a = xplotter.createSubPlot(plot_title, 'Angstroms^-1', 'FSC', yformat=False) legends = [] for it in self._iterations: parFn = self.protocol._getFileName('output_vol_par_class', iter=it, ref=ref3d) if exists(parFn): show = True self._plotFSC(a, parFn) legends.append('iter %d' % it) xplotter.showLegend(legends) if show: if threshold < self.maxFrc: a.plot([self.minInv, self.maxInv], [threshold, threshold], color='black', linestyle='--') a.grid(True) else: raise Exception("Set a valid iteration to show its FSC") return [xplotter]
def visualize(self, obj, **kwargs): prot = obj fnProfiles = prot._getPath("profiles.txt") fh = open(fnProfiles,"r") Rtype = [] Rlegends = [] R = [] state = 0 for line in fh: if state==0: tokens = line.split("::") Rtype.append(tokens[0]) Rlegends.append(tokens[1].strip()) Ri=[] state=1 elif state==1: tokens=line.strip().split() if len(tokens)==0: R.append(Ri) state=0 else: if len(Ri)==0: for n in range(len(tokens)): Ri.append([]) for n in range(len(tokens)): Ri[n].append(tokens[n]) fh.close() plotter = None previousType = "" for legend, Ri, Rtypei in izip(Rlegends, R, Rtype): if plotter is None or Rtypei!=previousType: if previousType!="": self.addLimits(plotter,previousType,minX,maxX) plotter = EmPlotter() doShow = True if Rtypei=="ReversibleLiver" or Rtypei=="TimeDependentLiver" or Rtypei=="InductionLiver" or Rtypei=="StaticLiver" or Rtypei=="TransporterLiver": Ilabel="[Ih] [uM]" elif Rtypei=="ReversibleGut" or Rtypei=="TimeDependentGut" or Rtypei=="InductionGut" or Rtypei=="StaticGut" or Rtypei=="TransporterGut": Ilabel="[Ig] [uM]" elif Rtypei=="TransporterRenal": Ilabel="[Cmax] [uM]" ax = plotter.createSubPlot("Plot", Ilabel, "R") previousType = Rtypei minX = None maxX = None else: doShow = False ax = plotter.getLastSubPlot() x = np.asarray(Ri[0],dtype=np.float64) if len(Ri)==2: y=np.asarray(Ri[1],dtype=np.float64) else: y=np.asarray(Ri[2],dtype=np.float64) ax.plot(x, y, label=legend) minXi = np.min(x) maxXi = np.max(x) if minX==None: minX=minXi maxX=maxXi minX=min(minXi,minX) maxX=min(maxXi,maxX) leg = ax.legend() if leg: leg.draggable() if doShow: plotter.show() else: plotter.draw() self.addLimits(plotter,previousType,minX,maxX)
class ExperimentWindow(gui.Window): """ This class creates a Window that will display some Point's contained in a Data object. It will allow to launch 1D, 2D and 3D plots by selecting any combination of the x1, x2...xn from the Point dimension. Points can be selected by either Click and Drag in the Scatter plot or.. by creating an Expression. Finally, there is a button 'Create Cluster' that will call a callback fuction to take care of it. """ def __init__(self, **kwargs): gui.Window.__init__(self, minsize=(420, 200), **kwargs) self.experiment = kwargs.get('experiment') self.fitting = kwargs.get('fitting', None) self.callback = kwargs.get('callback', None) content = tk.Frame(self.root) self._createContent(content) content.grid(row=0, column=0, sticky='news') content.columnconfigure(0, weight=1) content.rowconfigure(0, weight=1) self.plotter = None def _createContent(self, content): # Create and fill the frame containing the Experiment # info, variables and doses p = tk.PanedWindow(content, orient=tk.VERTICAL, bg='white') p.grid(row=0, column=0, sticky='news', padx=5, pady=5) self._createTopFrame(p) # Create the middle frame containing the Samples Box self._createSamplesFrame(p) # Create the last frame with the buttons self._createButtonsFrame(content) #self._updateSelectionLabel() def _createTopFrame(self, content): frame = tk.Frame(content) frame.columnconfigure(0, weight=1) frame.rowconfigure(0, weight=1) tab = ttk.Notebook(frame) tab.grid(row=0, column=0, sticky='news', padx=5, pady=5) def addTab(label): lf = tk.Frame(tab) tab.add(lf, text=label) return lf lfGeneral = addTab('General') self._addLabel(lfGeneral, 'Title', 0, 0) self._titleVar = tk.StringVar() self._titleVar.set(self.experiment.general['title']) titleEntry = tk.Entry(lfGeneral, width=26, textvariable=self._titleVar, bg='white') titleEntry.grid(row=0, column=1, sticky='nw', padx=5, pady=(5, 0)) self._addLabel(lfGeneral, 'Comment', 1, 0) commentText = gui.text.Text(lfGeneral, width=30, height=3, bg='white') commentText.setText(self.experiment.general['comment']) commentText.grid(row=1, column=1, sticky='nw', padx=5, pady=(5, 0)) self._commentText = commentText lfVars = addTab('Variables') self.varsTree = self._addBoundTree(lfVars, VariablesTreeProvider, 5) lfVias = addTab('Vias') self.viasTree = self._addBoundTree(lfVias, ViasTreeProvider, 5) lfDoses = addTab('Doses') self.dosesTree = self._addBoundTree(lfDoses, DosesTreeProvider, 5) lfGroups = addTab('Groups') self.groupsTree = self._addBoundTree(lfGroups, GroupsTreeProvider, 5) # Fitting tab if self.fitting: tabFitting = addTab('Fitting') t = TaggedText(tabFitting, width=80, height=10, bg='white') t.grid(row=0, column=0, sticky='nw', padx=10, pady=10) t.setText('Select one of the samples to see more information.') t.setReadOnly(True) self.fittingText = t # Buttons for fitting buttonsFrame = self._createFittingButtonsFrame(tabFitting) buttonsFrame.grid(row=0, column=1, sticky='news', padx=5, pady=5) #frame.grid(row=0, column=0, sticky='news', padx=5, pady=(10, 5)) content.add(frame, sticky='news', padx=5, pady=5) def _createSamplesFrame(self, content): frame = tk.Frame(content) #frame = tk.LabelFrame(content, text='General') lfSamples = tk.LabelFrame(frame, text='Samples') gui.configureWeigths(frame) lfSamples.grid(row=0, column=0, sticky='news', padx=5, pady=5) self.samplesTree = self._addBoundTree(lfSamples, SamplesTreeProvider, 10, fitting=self.fitting) self.samplesTree.itemDoubleClick = self._onSampleDoubleClick self.samplesTree.itemClick = self._onSampleClick plotFrame = tk.Frame(lfSamples) plotFrame.grid(row=1, column=0, sticky='ws', padx=5, pady=5) # Add a combobox with the variable for time timeVars = [v.varName for v in self.experiment.variables.values() if v.role == v.ROLE_TIME] timeVars += [v.varName for v in self.experiment.variables.values() if v.role == v.ROLE_MEASUREMENT] measureVars = [v.varName for v in self.experiment.variables.values() if v.role == v.ROLE_MEASUREMENT] def addVar(text, col, choices): varFrame = tk.Frame(plotFrame) varFrame.grid(row=0, column=col, sticky='new') label = tk.Label(varFrame, text=text, font=self.fontBold) label.grid(row=0, column=0, padx=5, pady=2, sticky='nw') combo = ComboBox(varFrame, choices, width=10) combo.grid(row=0, column=1, sticky='nw', padx=5, pady=5) radioVar = tk.IntVar() radio = tk.Checkbutton(varFrame, text='Log10', variable=radioVar) radio.grid(row=0, column=2, sticky='nw', padx=5, pady=5) return combo, radio, radioVar self.timeWidget = addVar('Time variable', 0, timeVars) self.measureWidget = addVar('Measure variable', 1, measureVars) self.measureWidget[2].set(True) self.plotButton = Button(plotFrame, ' Plot ', font=self.fontBold, command=self._onPlotClick, tooltip='Select one or more samples to plot ' 'their measures of the selected ' 'variables (optionally in log).') self.plotButton.grid(row=0, column=2, sticky='ne', padx=5) self.plotSummaryButton = Button(plotFrame, ' Summary Plot ', font=self.fontBold, command=self._onPlotSummaryClick, tooltip='Select several samples to plot' ' their statistics' ' (min, max, avg and std).') self.plotSummaryButton.grid(row=1, column=2, sticky='ne', padx=5, pady=5) #frame.grid(row=1, column=0, sticky='news', padx=5, pady=5) content.add(frame, sticky='news', padx=5, pady=5) def _createButtonsFrame(self, content): frame = tk.Frame(content) gui.configureWeigths(frame) buttonsFrame = tk.Frame(frame) buttonsFrame.grid(row=0, column=0, sticky='ne') closeButton = Button(buttonsFrame, 'Close', command=self.close, imagePath='fa-times.png') closeButton.grid(row=0, column=0, sticky='ne', padx=5) self.newButton = HotButton(buttonsFrame, ' New Experiment ', command=self._onCreateClick, tooltip='Create a new experiment with the ' 'selected samples. You can also edit' 'title and comment.') self.newButton.grid(row=0, column=1, sticky='ne', padx=5) frame.grid(row=1, column=0, sticky='news', padx=5, pady=5) def _createFittingButtonsFrame(self, content): buttonsFrame = tk.Frame(content) buttonsFrame.grid(row=0, column=0, sticky='ne') plotValuesButton = Button(buttonsFrame, 'Plot Fit', command=self._onPlotFitClick) plotValuesButton.grid(row=0, column=0, sticky='sew', padx=5, pady=5) openValuesButton = Button(buttonsFrame, 'Open Fit', command=self._onOpenFitClick) openValuesButton.grid(row=1, column=0, sticky='sew', padx=5, pady=5) return buttonsFrame def _addLabel(self, parent, text, r, c): label = tk.Label(parent, text=text, font=self.fontBold) label.grid(row=r, column=c, padx=5, pady=5, sticky='ne') return label def _addBoundTree(self, parent, ProviderClass, height, **kwargs): bt = BoundTree(parent, ProviderClass(self.experiment, **kwargs), height=height) bt.grid(row=0, column=0, sticky='news', padx=5, pady=5) gui.configureWeigths(parent) return bt def getUnits(self, varName): return self.experiment.variables[varName].getUnitsString() def getLabel(self, varName, useLog): varLabel = '%s [%s]' % (varName, self.getUnits(varName)) if useLog: varLabel = "log10(%s)" % varLabel return varLabel def getTimeVarName(self): return self.timeWidget[0].getText() def useTimeLog(self): return self.timeWidget[2].get() def getTimeLabel(self): return self.getLabel(self.getTimeVarName(), self.useTimeLog()) def getMeasureVarName(self): return self.measureWidget[0].getText() def useMeasureLog(self): return self.measureWidget[2].get() def getMeasureLabel(self): return self.getLabel(self.getMeasureVarName(), self.useMeasureLog()) def getPlotValues(self, sample): xValues, yValues = sample.getXYValues(self.getTimeVarName(), self.getMeasureVarName()) xValues=xValues[0] # From [array(...)] to array(...) yValues=yValues[0] useMeasureLog = self.useMeasureLog() useTimeLog = self.useTimeLog() if not (useMeasureLog or useTimeLog): return xValues, yValues # If log will be used either for time or measure var # we need to filter elements larger than 0 newXValues = [] newYValues = [] def _value(v, useLog): if useLog: return math.log10(v) if v > 0 else None return v for x, y in izip(xValues, yValues): x = _value(x, useTimeLog) y = _value(y, useMeasureLog) if x is not None and y is not None: newXValues.append(x) newYValues.append(y) return newXValues, newYValues def _onPlotClick(self, e=None): sampleKeys = self.samplesTree.selection() if sampleKeys: if self.plotter is None or self.plotter.isClosed(): self.plotter = EmPlotter() doShow = True ax = self.plotter.createSubPlot("Plot", self.getTimeLabel(), self.getMeasureLabel()) self.plotDict = {} else: doShow = False ax = self.plotter.getLastSubPlot() samples = [self.experiment.samples[k] for k in sampleKeys] for s in samples: if not s.sampleName in self.plotDict: x, y = self.getPlotValues(s) ax.plot(x, y, label=s.sampleName) self.plotDict[s.sampleName] = True leg = ax.legend() if leg: leg.draggable() if doShow: self.plotter.show() else: self.plotter.draw() else: self.showInfo("Please select some sample(s) to plot.") def _onPlotSummaryClick(self, e=None): sampleKeys = self.samplesTree.selection() n = len(sampleKeys) if n == 1: self.showInfo("Please select several samples to plot.") else: if n > 1: samples = [self.experiment.samples[k] for k in sampleKeys] else: samples = self.experiment.samples.values() dataDict = {} # key will be time values for s in samples: xValues, yValues = self.getPlotValues(s) for x, y in izip(xValues, yValues): if x in dataDict: dataDict[x].append(y) else: dataDict[x] = [y] sortedTime = sorted(dataDict.keys()) # We will store five values (min, 25%, 50%, 75%, max) # for each of the time entries computed percentileList = [0, 25, 50, 75, 100] Y = np.zeros((len(sortedTime), 5)) for i, t in enumerate(sortedTime): Y[i,:] = np.percentile(dataDict[t], percentileList) plotter = EmPlotter() ax = plotter.createSubPlot("Summary Plot", self.getTimeLabel(), self.getMeasureLabel()) ax.plot(sortedTime, Y[:, 0], 'r--', label="Minimum") ax.plot(sortedTime, Y[:, 1], 'b--', label="25%") ax.plot(sortedTime, Y[:, 2], 'g', label="50% (Median)") ax.plot(sortedTime, Y[:, 3], 'b--', label="75%") ax.plot(sortedTime, Y[:, 4], 'r--', label="Maximum") ax.legend() plotter.show() def _onCreateClick(self, e=None): sampleKeys = self.samplesTree.selection() if sampleKeys and self.callback: self.callback() else: self.showInfo("Please select some sample(s) to create a new experiment.") def _onSampleClick(self, obj): # In fitting mode we need to display some information for # the given sample fit if self.fitting: selection = self.samplesTree.selection() if selection: sampleKey = selection[0] sampleFit = self.fitting.getSampleFit(sampleKey) textMsg = sampleFit.getBasicInfo() else: textMsg = 'Select one of the samples to see more information.' self.fittingText.setReadOnly(False) self.fittingText.setText(textMsg) self.fittingText.setReadOnly(True) if not (self.plotter is None or self.plotter.isClosed()): self._onPlotClick() def _onSampleDoubleClick(self, obj): sampleKeys = self.samplesTree.selection() if sampleKeys: MeasureWindow(masterWindow=self, sample=self.experiment.samples[sampleKeys[0]]).show() def _onPlotFitClick(self, e=None): sampleKeys = self.samplesTree.selection() if sampleKeys: def _value(v, useLog): if useLog: return math.log10(v) if v > 0 else None return v def _values(values, useLog=self.useMeasureLog()): return [_value(float(x), useLog) for x in values] def _plot(varLabelX, varLabelY, x, y, yp): plotter = EmPlotter() ax = plotter.createSubPlot("Plot", varLabelX, varLabelY) xValues = _values(x, useLog=self.useTimeLog()) ax.plot(xValues, _values(y), 'x', label="Observations") idx = np.argsort(xValues) ypValues = _values(yp) ax.plot(np.asarray(xValues)[idx], np.asarray(ypValues)[idx], 'g', label="Fit") leg = ax.legend() if leg: leg.draggable() plotter.show() # Get first selected element fit = self.fitting.getSampleFit(sampleKeys[0]) if fit==None: return varLabelX = self.getLabel(self.fitting.predictor.varName, self.useTimeLog()) if type(self.fitting.predicted)==list: i=0 for v in self.fitting.predicted: varLabelY = self.getLabel(v.varName, self.useMeasureLog()) _plot(varLabelX, varLabelY, fit.x[i], fit.y[i], fit.yp[i]) i+=1 else: varLabelY = self.getLabel(self.fitting.predicted.varName, self.useMeasureLog()) _plot(varLabelX, varLabelY, fit.x[0], fit.y[0], fit.yp[0]) else: self.showInfo("Please select some sample(s) to plot.") def _onOpenFitClick(self, e=None): sampleKeys = self.samplesTree.selection() if sampleKeys: # Get first selected element fit = self.fitting.getSampleFit(sampleKeys[0]) FitWindow(masterWindow=self, fitting=self.fitting, sampleFit=fit).show() else: self.showInfo("Please select some sample(s) to plot.") def _onClosing(self): if self.plotter: self.plotter.close() gui.Window._onClosing(self)