def vis3Dfit(fitted, vol, model, ptcode, ctcode, showAxis, **kwargs): """Visualize ellipse fit in 3D with CT volume in current axis input: fitted = _fit3D output = (pp3, plane, pp3_2, e3) """ from stentseg.utils import fitting import numpy as np pp3,plane,pp3_2,e3 = fitted[0],fitted[1],fitted[2],fitted[3] a = vv.gca() # show_ctvolume(vol, model, showVol=showVol, clim=clim0, isoTh=isoTh) show_ctvolume(vol, model, **kwargs) vv.xlabel('x (mm)');vv.ylabel('y (mm)');vv.zlabel('z (mm)') vv.title('Ellipse fit for model %s - %s' % (ptcode[7:], ctcode)) a.axis.axisColor= 1,1,1 a.bgcolor= 0,0,0 a.daspect= 1, 1, -1 # z-axis flipped a.axis.visible = showAxis # For visualization, calculate 4 points on rectangle that lies on the plane x1, x2 = pp3.min(0)[0]-0.3, pp3.max(0)[0]+0.3 y1, y2 = pp3.min(0)[1]-0.3, pp3.max(0)[1]+0.3 p1 = x1, y1, -(x1*plane[0] + y1*plane[1] + plane[3]) / plane[2] p2 = x2, y1, -(x2*plane[0] + y1*plane[1] + plane[3]) / plane[2] p3 = x2, y2, -(x2*plane[0] + y2*plane[1] + plane[3]) / plane[2] p4 = x1, y2, -(x1*plane[0] + y2*plane[1] + plane[3]) / plane[2] vv.plot(pp3, ls='', ms='.', mc='y', mw = 10) vv.plot(fitting.project_from_plane(pp3_2, plane), lc='r', ls='', ms='.', mc='r', mw=9) # vv.plot(fitting.project_from_plane(fitting.sample_circle(c3), plane), lc='r', lw=2) vv.plot(fitting.project_from_plane(fitting.sample_ellipse(e3), plane), lc='b', lw=2) vv.plot(np.array([p1, p2, p3, p4, p1]), lc='g', lw=2) # vv.legend('3D points', 'Projected points', 'Circle fit', 'Ellipse fit', 'Plane fit') vv.legend('3D points', 'Projected points', 'Ellipse fit', 'Plane fit')
def DisplayOptimization (self) : """ Display the progress of optimization """ wx.Yield() # abort, if requested if self.need_abort : return def GetValueColourIter (d) : return izip_longest( d.itervalues(), ['r', 'g', 'b', 'k'], fillvalue='y' ) visvis.cla(); visvis.clf() visvis.subplot(211) # Plot optimization statistics for values, colour in GetValueColourIter(self.optimization_log) : try : visvis.plot ( values, lc=colour ) except Exception : pass visvis.xlabel ('iteration') visvis.ylabel ('Objective function') visvis.legend( self.optimization_log.keys() ) # Display reference signal visvis.subplot(212) # Plot reference signal for values, colour in GetValueColourIter(self.log_reference_signal) : try : visvis.plot ( values, lc=colour ) except Exception : pass visvis.xlabel ('iteration') visvis.ylabel ("Signal from reference pulse") visvis.legend( ["channel %d" % x for x in self.log_reference_signal.keys()] )
def vis2Dfit(fitted, ptcode, ctcode, showAxis): """Visualize ellipse fit in 2D in current axis input: fitted = _fit3D output = (pp3, plane, pp3_2, e3) """ from stentseg.utils import fitting import numpy as np plane,pp3_2,e3 = fitted[1],fitted[2],fitted[3] # endpoints axis x0,y0,res1,res2,phi = e3[0], e3[1], e3[2], e3[3], e3[4] dxax1 = np.cos(phi)*res1 dyax1 = np.sin(phi)*res1 dxax2 = np.cos(phi+0.5*np.pi)*res2 dyax2 = np.sin(phi+0.5*np.pi)*res2 p1ax1, p2ax1 = (x0+dxax1, y0+dyax1), (x0-dxax1, y0-dyax1) p1ax2, p2ax2 = (x0+dxax2, y0+dyax2), (x0-dxax2, y0-dyax2) a = vv.gca() vv.xlabel('x (mm)');vv.ylabel('y (mm)') vv.title('Ellipse fit for model %s - %s' % (ptcode[7:], ctcode)) a.axis.axisColor= 0,0,0 a.bgcolor= 0,0,0 a.axis.visible = showAxis a.daspectAuto = False a.axis.showGrid = True vv.plot(pp3_2, ls='', ms='.', mc='r', mw=9) vv.plot(fitting.sample_ellipse(e3), lc='b', lw=2) vv.plot(np.array([p1ax1, p2ax1]), lc='w', lw=2) # major axis vv.plot(np.array([p1ax2, p2ax2]), lc='w', lw=2) # minor axis vv.legend('3D points projected to plane', 'Ellipse fit on projected points')
def _Plot(self, event): # Make sure our figure is the active one # If only one figure, this is not necessary. #vv.figure(self.fig.nr) # Clear it vv.clf() # Plot vv.plot([1,2,3,1,6]) vv.legend(['this is a line'])
def AnalyzeTotalFluorescence (self, event=None) : """ `analyse_button` was clicked """ # Get current settings settings = self.GetSettings() # Apply peak finding filter signal = self.peak_finders[ settings["peak_finder"] ](self.total_fluorescence) # Scale to (0,1) signal -= signal.min() signal = signal / signal.max() ########################################################################## # Partition signal into segments that are above the background noise #signal = gaussian_filter(total_fluorescence, sigma=0.5) background_cutoff = settings["background_cutoff"] segments = [ [] ] for num, is_segment in enumerate( signal > background_cutoff ) : if is_segment : # this index is in the segment segments[-1].append( num ) elif len(segments[-1]) : # this condition is not to add empty segments # Start new segments segments.append( [] ) # Find peaks as weighted average of the segment peaks = [ np.average(self.positions[S], weights=self.total_fluorescence[S]) for S in segments if len(S) ] ########################################################################## # Saving the positions self.chanel_positions_ctrl.SetValue( ", ".join( "%2.4f" % p for p in peaks ) ) ########################################################################## # Plot acquired data visvis.cla(); visvis.clf() visvis.plot( self.positions, signal ) visvis.plot( peaks, background_cutoff*np.ones(len(peaks)), ls=None, ms='+', mc='r', mw=20) visvis.plot( [self.positions.min(), self.positions.max()], [background_cutoff, background_cutoff], lc='r', ls='--', ms=None) visvis.legend( ["measured signal", "peaks found", "background cut-off"] ) visvis.ylabel( "total fluorescence") visvis.xlabel( 'position (mm)')
def _Plot(self, event): # Make sure our figure is the active one # If only one figure, this is not necessary. # vv.figure(self.fig.nr) # Clear it vv.clf() song = wave.open("C:\Users\Mario\Desktop\infoadex antonio\\20150617070001.wav", 'r') frames = song.getnframes() ch = song.getnchannels() song_f = song.readframes(ch*frames) data = np.fromstring(song_f, np.int16) print data # Plot # vv.plot([1,2,3,1,6]) vv.plot(data) vv.legend(['this is a line'])
def _Plot(self, event): # update status text self.status.SetLabel("CPU:%.1f\nMemory:%.1f" % (cpudata[-1], vmemdata[-1])) # Make sure our figure is the active one # If only one figure, this is not necessary. #vv.figure(self.fig.nr) # Clear it vv.clf() # Plot a = vv.subplot(211) vv.plot(cpudata, lw=2, lc="c", mc ='y', ms='d') vv.legend("cpu percent ") a.axis.showGrid = True a.axis.xlabel = "CPU Usage Percent" a.axis.ylabel = "sampling time line" a = vv.subplot(212) vv.plot(vmemdata, lw=2, mew=1, lc='m', mc ='y', ms='d' ) vv.legend("virtual memory") a.axis.showGrid = True
def _Plot(self, event): # update status text self.status.SetLabel("CPU:%.1f\nMemory:%.1f" % (cpudata[-1], vmemdata[-1])) # Make sure our figure is the active one # If only one figure, this is not necessary. #vv.figure(self.fig.nr) # Clear it vv.clf() # Plot a = vv.subplot(211) vv.plot(cpudata, lw=2, lc="c", mc='y', ms='d') vv.legend("cpu percent ") a.axis.showGrid = True a.axis.xlabel = "CPU Usage Percent" a.axis.ylabel = "sampling time line" a = vv.subplot(212) vv.plot(vmemdata, lw=2, mew=1, lc='m', mc='y', ms='d') vv.legend("virtual memory") a.axis.showGrid = True
def show(self): import visvis as vv # If there are many tests, make a selection if len(self._tests) > 1000: tests = random.sample(self._tests, 1000) else: tests = self._tests # Get ticks nn = [test[0] for test in tests] # Create figure vv.figure(1) vv.clf() # Prepare kwargs plotKwargsText = {'ms': '.', 'mc': 'b', 'mw': 5, 'ls': ''} plotKwargsBin = {'ms': '.', 'mc': 'r', 'mw': 5, 'ls': ''} # File size against number of elements vv.subplot(221) vv.plot(nn, [test[2][0] for test in tests], **plotKwargsText) vv.plot(nn, [test[2][1] for test in tests], **plotKwargsBin) vv.legend('text', 'binary') vv.title('File size') # Speed against number of elements vv.subplot(223) vv.plot(nn, [test[1][4] for test in tests], **plotKwargsText) vv.plot(nn, [test[1][6] for test in tests], **plotKwargsBin) vv.legend('text', 'binary') vv.title('Save time') # Speed (file) against number of elements vv.subplot(224) vv.plot(nn, [test[1][5] for test in tests], **plotKwargsText) vv.plot(nn, [test[1][7] for test in tests], **plotKwargsBin) vv.legend('text', 'binary') vv.title('Load time')
def show(self): import visvis as vv # If there are many tests, make a selection if len(self._tests) > 1000: tests = random.sample(self._tests, 1000) else: tests = self._tests # Get ticks nn = [test[0] for test in tests] # Create figure vv.figure(1) vv.clf() # Prepare kwargs plotKwargsText = {'ms':'.', 'mc':'b', 'mw':5, 'ls':''} plotKwargsBin = {'ms':'.', 'mc':'r', 'mw':5, 'ls':''} # File size against number of elements vv.subplot(221) vv.plot(nn, [test[2][0] for test in tests], **plotKwargsText) vv.plot(nn, [test[2][1] for test in tests], **plotKwargsBin) vv.legend('text', 'binary') vv.title('File size') # Speed against number of elements vv.subplot(223) vv.plot(nn, [test[1][4] for test in tests], **plotKwargsText) vv.plot(nn, [test[1][6] for test in tests], **plotKwargsBin) vv.legend('text', 'binary') vv.title('Save time') # Speed (file) against number of elements vv.subplot(224) vv.plot(nn, [test[1][5] for test in tests], **plotKwargsText) vv.plot(nn, [test[1][7] for test in tests], **plotKwargsBin) vv.legend('text', 'binary') vv.title('Load time')
def identify_peaks_valleys(midpoints_peaks_valleys, model, vol, vis=True): """ Given a cloud of points containing 2 peak and 2 valley points for R1 and R2, identify and return these locations. Uses clustering and x,y,z """ # detect clusters to further label locations kmeans = KMeans(n_clusters=4) kmeans.fit(midpoints_peaks_valleys) centroids = kmeans.cluster_centers_ # x,y,z labels = kmeans.labels_ # identify left, right, ant, post centroid (assumes origin is prox right anterior) left = list(centroids[:, 0]).index(max(centroids[:, 0])) # max x value right = list(centroids[:, 0]).index(min(centroids[:, 0])) anterior = list(centroids[:, 1]).index(min(centroids[:, 1])) # min y value posterior = list(centroids[:, 1]).index(max(centroids[:, 1])) # get points into grouped arrays cLeft, cRight, cAnterior, cPosterior = [], [], [], [] for i, p in enumerate(midpoints_peaks_valleys): if labels[i] == left: cLeft.append(tuple(p.flat)) elif labels[i] == right: cRight.append(tuple(p.flat)) elif labels[i] == anterior: cAnterior.append(tuple(p.flat)) elif labels[i] == posterior: cPosterior.append(tuple(p.flat)) cLeft = np.asarray(cLeft) cRight = np.asarray(cRight) cAnterior = np.asarray(cAnterior) cPosterior = np.asarray(cPosterior) # divide into R1 R2 R1_left = cLeft[list(cLeft[:, 2]).index(min( cLeft[:, 2]))] # min z for R1; valley R2_left = cLeft[list(cLeft[:, 2]).index(max(cLeft[:, 2]))] # valley R1_right = cRight[list(cRight[:, 2]).index(min( cRight[:, 2]))] # min z for R1; valley R2_right = cRight[list(cRight[:, 2]).index(max(cRight[:, 2]))] # valley R1_ant = cAnterior[list(cAnterior[:, 2]).index(min( cAnterior[:, 2]))] # min z for R1; peak R2_ant = cAnterior[list(cAnterior[:, 2]).index(max(cAnterior[:, 2]))] # peak R1_post = cPosterior[list(cPosterior[:, 2]).index(min( cPosterior[:, 2]))] # min z for R1; peak R2_post = cPosterior[list(cPosterior[:, 2]).index(max(cPosterior[:, 2]))] # peak if vis == True: # visualize identified locations f = vv.figure(1) vv.clf() f.position = 968.00, 30.00, 944.00, 1002.00 a = vv.gca() colors = ['r', 'g', 'b', 'm', 'c', 'y', 'w', 'k'] for i, p in enumerate([ R1_left, R2_left, R1_right, R2_right, R1_ant, R2_ant, R1_post, R2_post ]): vv.plot(p[0], p[1], p[2], ms='.', ls='', mc=colors[i], mw=14) vv.legend('R1 left', 'R2 left', 'R1 right', 'R2 right', 'R1 ant', 'R2 ant', 'R1 post', 'R2 post') show_ctvolume(vol, model, showVol='MIP', clim=(0, 2500)) pick3d(vv.gca(), vol) model.Draw(mc='b', mw=10, lc='g') for i in range(len(midpoints_peaks_valleys)): vv.plot(midpoints_peaks_valleys[i], ms='.', ls='', mc=colors[labels[i]], mw=6) a.axis.axisColor = 1, 1, 1 a.bgcolor = 0, 0, 0 a.daspect = 1, 1, -1 # z-axis flipped a.axis.visible = True return R1_left, R2_left, R1_right, R2_right, R1_ant, R2_ant, R1_post, R2_post
def FitSpectralScans (self, scans, voltages, pixels_edges) : """ Perform fitting to the pulse shaper's mask transmission coefficient """ def FitIndividualPixel (voltages, pixel, modulation, p0=None) : """ Find fit for individual pixel with initial guess for phase function given by `modulation`. `voltages` voltage values for which `pixel` was measured `pixel` measured transmission (to be fitted) `p0` is the initial guess for fitting parametres """ def GetVM (V0, V1, m0, m1) : """ Return voltage and phase modulation from parameters """ M = m0 + m1*modulation V = np.linspace(V0, V1, M.size) return V, M def FittedTransmission (voltages, offset, amplitude, *params) : """ Return the transmission function for a shaper """ V, M = GetVM(*params) return amplitude*np.cos( pchip_interpolate(V,M,voltages) )**2 + offset # Set fitting parameters to their default values if p0 is None : p0 = [0., 1., voltages.min(), voltages.max(), 0., 1.] # Fitting the transmission try : popt, _ = curve_fit(FittedTransmission, voltages, pixel, p0=p0) except RuntimeError : popt = p0 # Get fitting error fitting_error = np.sum( ( FittedTransmission(voltages, *popt) - pixel )**2 ) return fitting_error, GetVM(*popt[2:]), popt ############################################################################ # Selecting the voltage range V_min = max( voltages_trial.min(), voltages.min() ) V_max = min( voltages_trial.max(), voltages.max() ) indx = np.nonzero( (V_min <= voltages)&(voltages <= V_max) ) # Number of calibration points lying within the voltage region num_vol_trial = np.sum( (V_min <= voltages_trial)&(voltages_trial <= V_max) ) if num_vol_trial < 2 : num_vol_trial = 2 # Re-sample modulation provided by CRi so that the voltage is equidistantly spaced resampled_vis_modulation = pchip_interpolate(voltages_trial, vis_modulation, np.linspace(V_min, V_max, min(len(voltages), num_vol_trial) ) ) resampled_nir_modulation = pchip_interpolate(voltages_trial, nir_modulation, np.linspace(V_min, V_max, min(len(voltages), num_vol_trial) ) ) # Normalizing scans scans -= scans.min(axis=0); scans /= scans.max(axis=0) # Bin the spectrum into pixels spectral_slices = map( lambda begin,end : scans[:,begin:end].mean(axis=1), pixels_edges[:-1], pixels_edges[1:] ) # List containing calibration data for each pixel calibration = [] # Initial guesses for fitting vis_p0 = None; nir_p0 = None; # Fit individual pulse shaper pixels them for pixel_num, pixel in enumerate(spectral_slices) : # Smoothing and normalizing each pixel #pixel = gaussian_filter(pixel,sigma=1) pixel -= pixel.min(); pixel /= pixel.max() # Fit the pixel by using the vis calibration curve as the initial guess vis_err, vis_calibration, vis_p0 = FitIndividualPixel(voltages[indx], pixel[indx], resampled_vis_modulation, vis_p0) # Fit the pixel by using the nir calibration curve as the initial guess nir_err, nir_calibration, nir_p0 = FitIndividualPixel(voltages[indx], pixel[indx], resampled_nir_modulation, nir_p0) # Choose the best fit if nir_err > vis_err : calibation_voltage, calibration_phase = vis_calibration fit_err = vis_err else : calibation_voltage, calibration_phase = nir_calibration fit_err = nir_err ###################### Plot ######################## visvis.clf() # Plot measured data visvis.plot( voltages, pixel, lc='r',ms='*', mc='r') # Plot fitted data plot_voltages = np.linspace( calibation_voltage.min(), calibation_voltage.max(), 500) transmission_fit = np.cos( pchip_interpolate(calibation_voltage, calibration_phase, plot_voltages) )**2 visvis.plot( plot_voltages, transmission_fit, lc='b') visvis.title ('Calibrating pixel %d / %d' % (pixel_num, len(spectral_slices)-1) ) visvis.legend(['measured', 'fitted']) visvis.xlabel ('voltages') visvis.ylabel ('Transmission coefficient') self.fig.DrawNow() ############ Save the calibration data ################## calibration.append( ( calibation_voltage, calibration_phase, fit_err ) ) return calibration
def ExtractPhaseFunc (self, event=None, filename=None) : """ This function is the last stage of calibration, when measured data is mathematically processed to obtain the calibration curves. """ # If filename is not specified, open the file dialogue if filename is None : filename = self.LoadSettings (title="Load calibration file...") # Update the name of pulse shaper calibration file import os self.SettingsNotebook.PulseShaper.SetSettings({"calibration_file_name" : os.path.abspath(filename)}) visvis.clf() # Loading the file calibration file with h5py.File(filename, 'a') as calibration_file : ############### Loading data #################### wavelengths = calibration_file["calibration_settings/wavelengths"][...] fixed_voltage = calibration_file["calibration_settings/fixed_voltage"][...] pulse_shaper_pixel_num = calibration_file["calibration_settings/pulse_shaper_pixel_num"][...] initial_pixel = calibration_file["settings/CalibrateShaper/initial_pixel"][...] final_pixel = calibration_file["settings/CalibrateShaper/final_pixel"][...] pixel_bundle_width = calibration_file["settings/CalibrateShaper/pixel_bundle_width"][...] pixel_to_lamba = str(calibration_file["settings/CalibrateShaper/pixel_to_lamba"][...]) # Convert to dict pixel_to_lamba = eval( "{%s}" % pixel_to_lamba ) # Loading scans of slave and masker masks master_mask_scans = []; slave_mask_scans = [] for key, spectrum in calibration_file["spectra_from_uniform_masks"].items() : master_volt, slave_volt = map(int, key.split('_')[-2:]) if master_volt == fixed_voltage : slave_mask_scans.append( (slave_volt, spectrum[...]) ) if slave_volt == fixed_voltage : master_mask_scans.append( (master_volt, spectrum[...]) ) # Sort by voltage master_mask_scans.sort(); slave_mask_scans.sort() # Extract spectral scans and voltages for each mask master_mask_voltage, master_mask_scans = zip(*master_mask_scans) master_mask_voltage = np.array(master_mask_voltage); master_mask_scans = np.array(master_mask_scans) slave_mask_voltage, slave_mask_scans = zip(*slave_mask_scans) slave_mask_voltage = np.array(slave_mask_voltage); slave_mask_scans = np.array(slave_mask_scans) ################### Find the edges of pixels ################# # function that converts pixel number to pulse shaper # `pixel_to_lamba` is a dictionary with key = pixel, value = lambda deg = 1 #min(1,len(pixel_to_lamba)-1) print np.polyfit(pixel_to_lamba.keys(), pixel_to_lamba.values(), 1 ) pixel2lambda_func = np.poly1d( np.polyfit(pixel_to_lamba.keys(), pixel_to_lamba.values(), deg=deg ) ) #lambda2pixel_func = np.poly1d( np.polyfit(pixel_to_lamba.values(), pixel_to_lamba.keys(), deg=deg ) ) # Get pixels_edges in shaper pixels pixels_edges_num = np.arange(initial_pixel, final_pixel, pixel_bundle_width) if pixels_edges_num[-1] < final_pixel : pixels_edges_num = np.append( pixels_edges_num, [final_pixel]) # Get pixels_edges in logical_pixel_lambda pixels_edges = pixel2lambda_func(pixels_edges_num) # Get pixels_edges in positions of the spectrometer spectra pixels_edges = np.abs(wavelengths - pixels_edges[:,np.newaxis]).argmin(axis=1) # Sorting indx = np.argsort(pixels_edges) pixels_edges = pixels_edges[indx] pixels_edges_num = pixels_edges_num[indx] # Plot visvis.cla(); visvis.clf() visvis.plot( pixel_to_lamba.values(), pixel_to_lamba.keys(), ls=None, ms='*', mc='g', mw=15) visvis.plot ( wavelengths[pixels_edges], pixels_edges_num, ls='-',lc='r') visvis.xlabel('wavelength (nm)') visvis.ylabel ('pulse shaper pixel') visvis.legend( ['measured', 'interpolated']) self.fig.DrawNow() ################ Perform fitting of the phase masks ################# master_mask_fits = self.FitSpectralScans( master_mask_scans, master_mask_voltage, pixels_edges ) slave_mask_fits = self.FitSpectralScans( slave_mask_scans, slave_mask_voltage, pixels_edges ) ################# Perform fitting of dispersion and phase functions ################# master_mask_TC_fitted, master_mask_scans, master_mask_disp_ph_curves = \ self.GetDispersionPhaseCurves (wavelengths, master_mask_scans, master_mask_voltage, pixels_edges, master_mask_fits, pixel2lambda_func, pulse_shaper_pixel_num ) slave_mask_TC_fitted, slave_mask_scans, slave_mask_disp_ph_curves = \ self.GetDispersionPhaseCurves (wavelengths, slave_mask_scans, slave_mask_voltage, pixels_edges, slave_mask_fits, pixel2lambda_func, pulse_shaper_pixel_num ) ################ Saving fitting parameters #################### ################################################################# # Save surface calibration try : del calibration_file["calibrated_surface"] except KeyError : pass CalibratedSurfaceGroupe = calibration_file.create_group("calibrated_surface") master_mask_calibrated_surface = CalibratedSurfaceGroupe.create_group("master_mask") for key, value in master_mask_disp_ph_curves.items() : master_mask_calibrated_surface[key] = value slave_mask_calibrated_surface = CalibratedSurfaceGroupe.create_group("slave_mask") for key, value in slave_mask_disp_ph_curves.items() : slave_mask_calibrated_surface[key] = value ################################################################# # Clear the group, if it exits try : del calibration_file["calibrated_pixels"] except KeyError : pass # This group is self consistent, thus the redundancy in saved data (with respect to other group in the file) CalibratedPixelsGroupe = calibration_file.create_group ("calibrated_pixels") CalibratedPixelsGroupe["pixels_edges"] = pixels_edges # Finding spectral bounds for each pixel pixels_spectral_bounds = zip( wavelengths[pixels_edges[:-1]], wavelengths[pixels_edges[1:]] ) # Pulse shaper pixel bounds pixels_bounds = zip(pixels_edges_num[:-1], pixels_edges_num[1:]) PixelsGroup = CalibratedPixelsGroupe.create_group("pixels") for pixel_num, master_mask_calibration, slave_mask_calibration, \ spectral_bound, pixel_bound in zip( range(len(master_mask_fits)), \ master_mask_fits, slave_mask_fits, pixels_spectral_bounds, pixels_bounds ) : pixel = PixelsGroup.create_group("pixel_%d" % pixel_num) pixel["spectral_bound"] = spectral_bound pixel["pixel_bound"] = pixel_bound # Saving fitted calibration data in the tabular form pixel["voltage_master_mask"] = master_mask_calibration[0] pixel["phase_master_mask"] = master_mask_calibration[1] pixel["voltage_slave_mask"] = slave_mask_calibration[0] pixel["phase_slave_mask"] = slave_mask_calibration[1] ################ Plotting results of calibration #################### visvis.cla(); visvis.clf(); visvis.subplot(2,2,1) visvis.imshow( master_mask_scans, cm=visvis.CM_JET ) visvis.title ("Master mask (measured data)") visvis.ylabel ('voltage'); visvis.xlabel ('wavelength (nm)') visvis.subplot(2,2,3) visvis.imshow( master_mask_TC_fitted, cm=visvis.CM_JET ) visvis.title ("Master mask (fitted data)") visvis.ylabel ('voltage'); visvis.xlabel ('wavelength (nm)') visvis.subplot(2,2,2) visvis.imshow( slave_mask_scans, cm=visvis.CM_JET ) visvis.title ("Slave mask (measured data)") visvis.ylabel ('voltage'); visvis.xlabel ('wavelength (nm)') visvis.subplot(2,2,4) visvis.imshow( slave_mask_TC_fitted, cm=visvis.CM_JET ) visvis.title ("Slave mask (fitted data)") visvis.ylabel ('voltage'); visvis.xlabel ('wavelength (nm)')
""" legend('name1', 'name2', 'name3', ..., axes=None) Can also be called with a single argument being a tuple/list of strings. Set the string labels for the legend. If no string labels are given, the legend wibject is hidden again. See also the Axes.legend property. """ # Get axes axes = None if 'axes' in kwargs: axes = kwargs['axes'] if axes is None: axes = vv.gca() if len(value) == 1 and isinstance(value[0], (list, tuple)): value = value[0] # Apply what was given axes.legend = value if __name__ == '__main__': vv.plot([1,2,3,1]) vv.plot([2,3,1,4],lc='r') vv.legend(['line one', 'line two']) vv.legend('line three', 'line four') # or
def ExtractPhaseFunc(self, event=None, filename=None): """ This function is the last stage of calibration, when measured data is mathematically processed to obtain the calibration curves. """ # If filename is not specified, open the file dialogue if filename is None: filename = self.LoadSettings(title="Load calibration file...") # Update the name of pulse shaper calibration file import os self.SettingsNotebook.PulseShaper.SetSettings( {"calibration_file_name": os.path.abspath(filename)}) visvis.clf() # Loading the file calibration file with h5py.File(filename, 'a') as calibration_file: ############### Loading data #################### wavelengths = calibration_file["calibration_settings/wavelengths"][ ...] fixed_voltage = calibration_file[ "calibration_settings/fixed_voltage"][...] pulse_shaper_pixel_num = calibration_file[ "calibration_settings/pulse_shaper_pixel_num"][...] initial_pixel = calibration_file[ "settings/CalibrateShaper/initial_pixel"][...] final_pixel = calibration_file[ "settings/CalibrateShaper/final_pixel"][...] pixel_bundle_width = calibration_file[ "settings/CalibrateShaper/pixel_bundle_width"][...] pixel_to_lamba = str( calibration_file["settings/CalibrateShaper/pixel_to_lamba"][ ...]) # Convert to dict pixel_to_lamba = eval("{%s}" % pixel_to_lamba) # Loading scans of slave and masker masks master_mask_scans = [] slave_mask_scans = [] for key, spectrum in calibration_file[ "spectra_from_uniform_masks"].items(): master_volt, slave_volt = map(int, key.split('_')[-2:]) if master_volt == fixed_voltage: slave_mask_scans.append((slave_volt, spectrum[...])) if slave_volt == fixed_voltage: master_mask_scans.append((master_volt, spectrum[...])) # Sort by voltage master_mask_scans.sort() slave_mask_scans.sort() # Extract spectral scans and voltages for each mask master_mask_voltage, master_mask_scans = zip(*master_mask_scans) master_mask_voltage = np.array(master_mask_voltage) master_mask_scans = np.array(master_mask_scans) slave_mask_voltage, slave_mask_scans = zip(*slave_mask_scans) slave_mask_voltage = np.array(slave_mask_voltage) slave_mask_scans = np.array(slave_mask_scans) ################### Find the edges of pixels ################# # function that converts pixel number to pulse shaper # `pixel_to_lamba` is a dictionary with key = pixel, value = lambda deg = 1 #min(1,len(pixel_to_lamba)-1) print np.polyfit(pixel_to_lamba.keys(), pixel_to_lamba.values(), 1) pixel2lambda_func = np.poly1d( np.polyfit(pixel_to_lamba.keys(), pixel_to_lamba.values(), deg=deg)) #lambda2pixel_func = np.poly1d( np.polyfit(pixel_to_lamba.values(), pixel_to_lamba.keys(), deg=deg ) ) # Get pixels_edges in shaper pixels pixels_edges_num = np.arange(initial_pixel, final_pixel, pixel_bundle_width) if pixels_edges_num[-1] < final_pixel: pixels_edges_num = np.append(pixels_edges_num, [final_pixel]) # Get pixels_edges in logical_pixel_lambda pixels_edges = pixel2lambda_func(pixels_edges_num) # Get pixels_edges in positions of the spectrometer spectra pixels_edges = np.abs(wavelengths - pixels_edges[:, np.newaxis]).argmin(axis=1) # Sorting indx = np.argsort(pixels_edges) pixels_edges = pixels_edges[indx] pixels_edges_num = pixels_edges_num[indx] # Plot visvis.cla() visvis.clf() visvis.plot(pixel_to_lamba.values(), pixel_to_lamba.keys(), ls=None, ms='*', mc='g', mw=15) visvis.plot(wavelengths[pixels_edges], pixels_edges_num, ls='-', lc='r') visvis.xlabel('wavelength (nm)') visvis.ylabel('pulse shaper pixel') visvis.legend(['measured', 'interpolated']) self.fig.DrawNow() ################ Perform fitting of the phase masks ################# master_mask_fits = self.FitSpectralScans(master_mask_scans, master_mask_voltage, pixels_edges) slave_mask_fits = self.FitSpectralScans(slave_mask_scans, slave_mask_voltage, pixels_edges) ################# Perform fitting of dispersion and phase functions ################# master_mask_TC_fitted, master_mask_scans, master_mask_disp_ph_curves = \ self.GetDispersionPhaseCurves (wavelengths, master_mask_scans, master_mask_voltage, pixels_edges, master_mask_fits, pixel2lambda_func, pulse_shaper_pixel_num ) slave_mask_TC_fitted, slave_mask_scans, slave_mask_disp_ph_curves = \ self.GetDispersionPhaseCurves (wavelengths, slave_mask_scans, slave_mask_voltage, pixels_edges, slave_mask_fits, pixel2lambda_func, pulse_shaper_pixel_num ) ################ Saving fitting parameters #################### ################################################################# # Save surface calibration try: del calibration_file["calibrated_surface"] except KeyError: pass CalibratedSurfaceGroupe = calibration_file.create_group( "calibrated_surface") master_mask_calibrated_surface = CalibratedSurfaceGroupe.create_group( "master_mask") for key, value in master_mask_disp_ph_curves.items(): master_mask_calibrated_surface[key] = value slave_mask_calibrated_surface = CalibratedSurfaceGroupe.create_group( "slave_mask") for key, value in slave_mask_disp_ph_curves.items(): slave_mask_calibrated_surface[key] = value ################################################################# # Clear the group, if it exits try: del calibration_file["calibrated_pixels"] except KeyError: pass # This group is self consistent, thus the redundancy in saved data (with respect to other group in the file) CalibratedPixelsGroupe = calibration_file.create_group( "calibrated_pixels") CalibratedPixelsGroupe["pixels_edges"] = pixels_edges # Finding spectral bounds for each pixel pixels_spectral_bounds = zip(wavelengths[pixels_edges[:-1]], wavelengths[pixels_edges[1:]]) # Pulse shaper pixel bounds pixels_bounds = zip(pixels_edges_num[:-1], pixels_edges_num[1:]) PixelsGroup = CalibratedPixelsGroupe.create_group("pixels") for pixel_num, master_mask_calibration, slave_mask_calibration, \ spectral_bound, pixel_bound in zip( range(len(master_mask_fits)), \ master_mask_fits, slave_mask_fits, pixels_spectral_bounds, pixels_bounds ) : pixel = PixelsGroup.create_group("pixel_%d" % pixel_num) pixel["spectral_bound"] = spectral_bound pixel["pixel_bound"] = pixel_bound # Saving fitted calibration data in the tabular form pixel["voltage_master_mask"] = master_mask_calibration[0] pixel["phase_master_mask"] = master_mask_calibration[1] pixel["voltage_slave_mask"] = slave_mask_calibration[0] pixel["phase_slave_mask"] = slave_mask_calibration[1] ################ Plotting results of calibration #################### visvis.cla() visvis.clf() visvis.subplot(2, 2, 1) visvis.imshow(master_mask_scans, cm=visvis.CM_JET) visvis.title("Master mask (measured data)") visvis.ylabel('voltage') visvis.xlabel('wavelength (nm)') visvis.subplot(2, 2, 3) visvis.imshow(master_mask_TC_fitted, cm=visvis.CM_JET) visvis.title("Master mask (fitted data)") visvis.ylabel('voltage') visvis.xlabel('wavelength (nm)') visvis.subplot(2, 2, 2) visvis.imshow(slave_mask_scans, cm=visvis.CM_JET) visvis.title("Slave mask (measured data)") visvis.ylabel('voltage') visvis.xlabel('wavelength (nm)') visvis.subplot(2, 2, 4) visvis.imshow(slave_mask_TC_fitted, cm=visvis.CM_JET) visvis.title("Slave mask (fitted data)") visvis.ylabel('voltage') visvis.xlabel('wavelength (nm)')
def FitSpectralScans(self, scans, voltages, pixels_edges): """ Perform fitting to the pulse shaper's mask transmission coefficient """ def FitIndividualPixel(voltages, pixel, modulation, p0=None): """ Find fit for individual pixel with initial guess for phase function given by `modulation`. `voltages` voltage values for which `pixel` was measured `pixel` measured transmission (to be fitted) `p0` is the initial guess for fitting parametres """ def GetVM(V0, V1, m0, m1): """ Return voltage and phase modulation from parameters """ M = m0 + m1 * modulation V = np.linspace(V0, V1, M.size) return V, M def FittedTransmission(voltages, offset, amplitude, *params): """ Return the transmission function for a shaper """ V, M = GetVM(*params) return amplitude * np.cos(pchip_interpolate( V, M, voltages))**2 + offset # Set fitting parameters to their default values if p0 is None: p0 = [0., 1., voltages.min(), voltages.max(), 0., 1.] # Fitting the transmission try: popt, _ = curve_fit(FittedTransmission, voltages, pixel, p0=p0) except RuntimeError: popt = p0 # Get fitting error fitting_error = np.sum( (FittedTransmission(voltages, *popt) - pixel)**2) return fitting_error, GetVM(*popt[2:]), popt ############################################################################ # Selecting the voltage range V_min = max(voltages_trial.min(), voltages.min()) V_max = min(voltages_trial.max(), voltages.max()) indx = np.nonzero((V_min <= voltages) & (voltages <= V_max)) # Number of calibration points lying within the voltage region num_vol_trial = np.sum((V_min <= voltages_trial) & (voltages_trial <= V_max)) if num_vol_trial < 2: num_vol_trial = 2 # Re-sample modulation provided by CRi so that the voltage is equidistantly spaced resampled_vis_modulation = pchip_interpolate( voltages_trial, vis_modulation, np.linspace(V_min, V_max, min(len(voltages), num_vol_trial))) resampled_nir_modulation = pchip_interpolate( voltages_trial, nir_modulation, np.linspace(V_min, V_max, min(len(voltages), num_vol_trial))) # Normalizing scans scans -= scans.min(axis=0) scans /= scans.max(axis=0) # Bin the spectrum into pixels spectral_slices = map( lambda begin, end: scans[:, begin:end].mean(axis=1), pixels_edges[:-1], pixels_edges[1:]) # List containing calibration data for each pixel calibration = [] # Initial guesses for fitting vis_p0 = None nir_p0 = None # Fit individual pulse shaper pixels them for pixel_num, pixel in enumerate(spectral_slices): # Smoothing and normalizing each pixel #pixel = gaussian_filter(pixel,sigma=1) pixel -= pixel.min() pixel /= pixel.max() # Fit the pixel by using the vis calibration curve as the initial guess vis_err, vis_calibration, vis_p0 = FitIndividualPixel( voltages[indx], pixel[indx], resampled_vis_modulation, vis_p0) # Fit the pixel by using the nir calibration curve as the initial guess nir_err, nir_calibration, nir_p0 = FitIndividualPixel( voltages[indx], pixel[indx], resampled_nir_modulation, nir_p0) # Choose the best fit if nir_err > vis_err: calibation_voltage, calibration_phase = vis_calibration fit_err = vis_err else: calibation_voltage, calibration_phase = nir_calibration fit_err = nir_err ###################### Plot ######################## visvis.clf() # Plot measured data visvis.plot(voltages, pixel, lc='r', ms='*', mc='r') # Plot fitted data plot_voltages = np.linspace(calibation_voltage.min(), calibation_voltage.max(), 500) transmission_fit = np.cos( pchip_interpolate(calibation_voltage, calibration_phase, plot_voltages))**2 visvis.plot(plot_voltages, transmission_fit, lc='b') visvis.title('Calibrating pixel %d / %d' % (pixel_num, len(spectral_slices) - 1)) visvis.legend(['measured', 'fitted']) visvis.xlabel('voltages') visvis.ylabel('Transmission coefficient') self.fig.DrawNow() ############ Save the calibration data ################## calibration.append( (calibation_voltage, calibration_phase, fit_err)) return calibration
for t in np.arange(0, 1, 0.01): # NOTE: if I write this out, I'll get a cubic spline! c_1 = (0.5 * t**2 - 0.5 * t) * (1 - t) c0 = (-t**2 + 1) * (1 - t) + (0.5 * t**2 - 1.5 * t + 1) * t c1 = (0.5 * t**2 + 0.5 * t) * (1 - t) + (-t**2 + 2 * t) * t c2 = (0.5 * t**2 - 0.5 * t) * t res3.append(t, c_1 * pp[0] + c0 * pp[1] + c1 * pp[2] + c2 * pp[3]) # Combine by adding # res3 = 0.5*(res1 + res2) # To compare, cardinal spline resC = PointSet(2) for t in np.arange(0, 1, 0.01): cc = pirt.get_cubic_spline_coefs(t, 0) resC.append(t, cc[0] * pp[0] + cc[1] * pp[1] + cc[2] * pp[2] + cc[3] * pp[3]) # Show vv.figure(1) vv.clf() vv.plot(res1, lc='r', lw=1) vv.plot(res2, lc='b', lw=1) vv.plot(res3, lc='g', lw=2) vv.plot(resC, lc='m', lw=5, ls=':') vv.plot([-1, 0, 1, 2], pp, lc='k', ls='--') # vv.legend('From the left', 'From the right', 'Combined', 'Catmull-rom') vv.use().Run()
""" legend('name1', 'name2', 'name3', ..., axes=None) Can also be called with a single argument being a tuple/list of strings. Set the string labels for the legend. If no string labels are given, the legend wibject is hidden again. See also the Axes.legend property. """ # Get axes axes = None if 'axes' in kwargs: axes = kwargs['axes'] if axes is None: axes = vv.gca() if len(value) == 1 and isinstance(value[0], (list, tuple)): value = value[0] # Apply what was given axes.legend = value if __name__ == '__main__': vv.plot([1, 2, 3, 1]) vv.plot([2, 3, 1, 4], lc='r') vv.legend(['line one', 'line two']) vv.legend('line three', 'line four') # or
from stentseg.utils.datahandling import select_dir import matplotlib.pyplot as plt import matplotlib as mpl import prettyplotlib as ppl f = vv.figure() f.position = 0, 22, 1077, 273 # B1 (A=0.6, T=0.8, N=20, top=0.35) ax1 = vv.subplot(121) #; vv.title('profile1') plot_pattern(*(tt1, aa1)) ax1.axis.showGrid = False ax1.axis.showBox = False ax1.SetLimits(rangeX=(-0.01, 2), rangeY=(-0.02, 2.5)) vv.xlabel('time (s)') vv.ylabel('position (mm)') vv.legend('B1 (A=0.6, T=0.8)') # B5 (A=2.0, T=0.8, N=20, top=0.35, extra=(0.7, 0.8, 0.05)) ax5 = vv.subplot(122) #; vv.title('profile5') plot_pattern(*(tt5, aa5)) ax5.axis.showGrid = False ax5.SetLimits(rangeX=(-0.01, 2), rangeY=(-0.02, 2.5)) ax5.axis.showBox = False vv.xlabel('time (s)') vv.ylabel('position (mm)') vv.legend('B5 (A=2.0, T=0.8, extra=0.7, 0.8, 0.05)') f.relativeFontSize = 1.2 if False: exceldir = select_dir(r'C:\Users\Maaike\Desktop', r'D:\Profiles\koenradesma\Desktop')
def _Plot(self, *args): vv.figure(self.figure.nr) vv.clf() vv.plot([1, 2, 3, 1, 6]) vv.legend(['this is a line'])
import visvis as vv fig = vv.clf() fig.position = 300, 300, 1000, 600 # 2D vis a = vv.subplot(121) a.daspectAuto = False a.axis.showGrid = True vv.title('2D fitting') vv.xlabel('x'); vv.ylabel('y') # Plot vv.plot(pp2, ls='', ms='.', mc='k') # vv.plot(sample_circle(c2), lc='r', lw=2) vv.plot(sample_ellipse(e2), lc='b', lw=2) # vv.legend('2D points', 'Circle fit', 'Ellipse fit') vv.legend('2D points', 'Ellipse fit') # 3D vis a = vv.subplot(122) a.daspectAuto = False a.axis.showGrid = True vv.title('3D fitting') vv.xlabel('x'); vv.ylabel('y'); vv.zlabel('z') # Plot vv.plot(pp3, ls='', ms='.', mc='k') vv.plot(project_from_plane(pp3_2, plane), lc='r', ls='', ms='.', mc='r', mw=4) # vv.plot(project_from_plane(sample_circle(c3), plane), lc='r', lw=2) vv.plot(project_from_plane(sample_ellipse(e3), plane), lc='b', lw=2) vv.plot(np.array([p1, p2, p3, p4, p1]), lc='g', lw=2) # vv.legend('3D points', 'Projected points', 'Circle fit', 'Ellipse fit', 'Plane fit') vv.legend('3D points', 'Projected points', 'Ellipse fit', 'Plane fit')
def _Plot(self, *args): vv.figure(self.figure.nr) vv.clf() vv.plot([1,2,3,1,6]) vv.legend(['this is a line'])
def AnalyzeTotalFluorescence(self, event=None): """ `analyse_button` was clicked """ # Get current settings settings = self.GetSettings() # Apply peak finding filter signal = self.peak_finders[settings["peak_finder"]]( self.total_fluorescence) # Scale to (0,1) signal -= signal.min() signal = signal / signal.max() ########################################################################## # Partition signal into segments that are above the background noise #signal = gaussian_filter(total_fluorescence, sigma=0.5) background_cutoff = settings["background_cutoff"] segments = [[]] for num, is_segment in enumerate(signal > background_cutoff): if is_segment: # this index is in the segment segments[-1].append(num) elif len(segments[-1] ): # this condition is not to add empty segments # Start new segments segments.append([]) # Find peaks as weighted average of the segment peaks = [ np.average(self.positions[S], weights=self.total_fluorescence[S]) for S in segments if len(S) ] ########################################################################## # Saving the positions self.chanel_positions_ctrl.SetValue(", ".join("%2.4f" % p for p in peaks)) ########################################################################## # Plot acquired data visvis.cla() visvis.clf() visvis.plot(self.positions, signal) visvis.plot(peaks, background_cutoff * np.ones(len(peaks)), ls=None, ms='+', mc='r', mw=20) visvis.plot( [self.positions.min(), self.positions.max()], [background_cutoff, background_cutoff], lc='r', ls='--', ms=None) visvis.legend(["measured signal", "peaks found", "background cut-off"]) visvis.ylabel("total fluorescence") visvis.xlabel('position (mm)')