def plotmosaic(data,sizex,sizey,percentile=1,printing='',display='keep',**kwargs): #. plots mosaic of sizex by sizey spectral image slices. no_of_tiles=sizex*sizey # taking care of missing or extra tiles imsizex,imsizey,imsizez=data['SI'].shape pixperslice=imsizez//no_of_tiles extras=sp.remainder(imsizez,no_of_tiles) #print(imsizex,imsizey,imsizez,no_of_tiles,pixperslice,extras) #rebin the original array newd=bin_ndarray(data['SI'][:,:,0:imsizez-extras],[imsizex,imsizey,no_of_tiles]) # make tiled image line=[] impo=[] for j in range(sizey): line=newd[:,:,0+j*sizex] for i in range(1,sizex): imp=newd[:,:,i+j*sizex] line=np.concatenate((line,imp), axis=1) if j==0 : impo=line else : impo=np.concatenate((impo, line), axis=0) #plot plt.figure(figsize=[2.5*sizex*imsizex/imsizey,2*sizey*imsizey/imsizey]) ax=plt.imshow(impo, vmin=np.percentile(impo,1), vmax=np.percentile(impo,99), cmap='gray') plt.colorbar() #make labels tt=np.arange(pixperslice//2,(imsizez-extras),pixperslice) #print(tt) aa=np.array(data['wavelength'])[tt] aas = ["%6.0f" % n for n in aa] for i in range(no_of_tiles): col=np.remainder(i,sizex) row=i//sizex plt.text(col*imsizex,(row+0.95)*imsizey, aas[i]+'nm', color='orange', fontsize=12) plt.xticks([]) plt.yticks([]) plt.title(data['filename']) plt.axis('tight') if printing=='pdf': outname =data['path'] + data['filename'][:-5]+'mosaic.pdf' plt.savefig(outname,dpi=150, orientation='landscape') if display!='keep': plt.close() return return impo,aas##########################
def clean_up_xticklabels(start, length): # convenience function to make nice xticklabels. We want the # labels on only multiples of 5 axis_coords = range(length) TSS_coords = range(start, start + length) indices = find([sp.remainder(coord, 5) == 0 for coord in TSS_coords]) xtick_labels = [str(TSS_coords[ind]) for ind in indices] return indices, xtick_labels
def clean_up_xticklabels(start, length): # convenience function to make nice xticklabels. We want the # labels on only multiples of 5 axis_coords = range(length) TSS_coords = range(start,start+length) indices = find([sp.remainder(coord,5)==0 for coord in TSS_coords]) xtick_labels = [str(TSS_coords[ind]) for ind in indices] return indices, xtick_labels
def updates(self): self.num+=1 self.pbar.setValue((100.0*self.num)/self.max) self.label.setText("Trajectories completed: "+ str(self.num)+"/"+str(self.max)) if self.num>=self.wait and remainder(self.num,self.wait)==0: nwt=datetime.datetime.now() diff=((nwt.day-self.st.day)*86400+(nwt.hour-self.st.hour)*(60**2)+(nwt.minute-self.st.minute)*60+(nwt.second-self.st.second))*(self.max-self.num)/(1.0*self.num) secs=datetime.timedelta(seconds=ceil(diff)) dd = datetime.datetime(1,1,1) + secs time_string="%02d:%02d:%02d:%02d" % (dd.day-1,dd.hour,dd.minute,dd.second) self.estlabel.setText("Est. time remaining: "+time_string)
def find_start_and_end_of_hit(vec_x, fs, vec_idx_transients): idx_start = [] idx_end = [] L_x = len(vec_x) # ___ some user parameters___ T_window = 5 # sec L_window = int(math.floor(T_window * fs)) if sp.remainder(L_window, 2) == 1: L_window = L_window + 1 L_half_window = L_window / 2 for cur_idx_transient in vec_idx_transients: idx_start = int(cur_idx_transient)- L_half_window idx_end = int(cur_idx_transient) + L_half_window if idx_start < 0: idx_start = 0 if idx_end > L_x: idx_end = L_x print("idx_start: {}".format(idx_start) ) print("idx_end: {}".format(idx_end)) #pdb.set_trace() cur_vec_x = vec_x[idx_start:idx_end+1] vec_envelope = np.abs(sgn.hilbert(vec_x, axis=0)) plt.figure(2) plt.plot(cur_vec_x) plt.hold(True) plt.plot(vec_envelope) plt.legend(['input signal', 'hilbert envelope']) plt.show() # calculate a threshold #if b_debug # tempfig('transient start search'); # plot([x vec_envelope]); #end # whiten the signal via lpc # vec_a = [1, 2, 3] # lpc(x,32) (vec_a, vec_prediction_error, vec_k) = talkbox.lpc(vec_x, 32, axis=0) vec_prediction_error = sgn.lfilter(vec_a, 1, vec_x, axis=0) # print(vec_prediction_error) plt.figure(3) plt.plot(vec_prediction_error) plt.title('prediction error') plt.show() #vec_signal_whitened = scipy.filter([0 -a(2:end)], 1, x); #vec_signal_diff = x- vec_signal_whitened; L_vec_x = len(vec_x) idx_mid = (L_vec_x-1) / 2; if sp.mod(idx_mid, 1) > 0: idx_mid = sp.floor(idx_mid) vec_prediction_error_power = vec_prediction_error**2 threshold = 0.9 * np.max(vec_prediction_error_power) #idx_temp_to_the_left = idx_mid - find(vec_prediction_error_power(idx_mid:-1:1) > threshold, 1, 'first') + 1; idx_temp_to_the_left = idx_mid - sp.nonzero(vec_prediction_error_power[idx_mid:0:-1] > threshold)[0] + 1 #idx_temp_to_the_right = find(vec_prediction_error_power(idx_mid:end) > threshold, 1, 'first') + idx_mid - 1; idx_temp_to_the_right = idx_mid+ sp.nonzero(vec_prediction_error_power[idx_mid-1:L_vec_x] > threshold)[0] if len(idx_temp_to_the_left) == 0: idx_start = idx_temp_to_the_right[0] elif len(idx_temp_to_the_right) == 0: idx_start = idx_temp_to_the_left[0] else: # which one is closer? if abs(idx_temp_to_the_left[0] - idx_mid) > abs(idx_temp_to_the_right[0] - idx_mid): idx_start = idx_temp_to_the_right[0] else: idx_start = idx_temp_to_the_left[0] #todo: think about this idx_start_decay = idx_start idx_start_transient = idx_start # #if b_debug # tempfig('transient start search'); # hold on; plot(idx_start, vec_envelope(idx_start), 'Marker', 'o', 'MarkerFaceColor', 'r'); # plot(vec_signal_whitened, 'g'); # hold off; # tempfig('prediction error'); # plot(vec_prediction_error_power); # hold on; # line([1 length(vec_prediction_error_power)], repmat(threshold, 1, 2), 'Color', 'r', 'LineWidth', 2); hold off; #end # filter that thing order_filter_smooth = 300. vec_b_filter_smooth = 1 / order_filter_smooth * sp.ones(order_filter_smooth,) vec_a_filter_smooth = [1] #disp(vec_b_filter_smooth) #vec_b_smooth = 1/order_smoothing_filter * ones(order_smoothing_filter, 1); #vec_a_smooth = 1; #x_envelope_smoothed = filtfilt(vec_b_smooth, vec_a_smooth, x_envelope); vec_envelope_smoothed = sgn.filtfilt(vec_b_filter_smooth, vec_a_filter_smooth, vec_envelope, padtype=None) #tempfig('selected waveform'); #% subplot(211); #hold on, plot((0:length(x)-1) / fs, x_envelope_smoothed, 'r'); hold off; # # try to find the beginning of the decay phase temp_max = np.max(vec_envelope_smoothed, axis=0) # disp(temp_max) #temp_max = max(x_envelope_smoothed); #tempfig('selected waveform'); hold on; #% subplot(211); #plot((idx_start_decay-1) / fs, temp_max, 'Marker', 'o', 'MarkerSize', 15, 'Color', 'g'); # #% tempfig('selected waveform'); hold on; #% subplot(212); #% plot((0:length(x)-2) / fs, diff((x_envelope_smoothed)), 'r'); hold off; # #tempfig('envelope histogram'); #hist(x_envelope_smoothed, 100); # # find the end of the decay phase threshold = np.percentile(vec_envelope_smoothed[idx_start_decay:L_vec_x], 30) #threshold = quantile(x_envelope_smoothed(idx_start_decay:end), 0.3); #idx_end_decay = find(x_envelope_smoothed(idx_start_decay:end) <= threshold, 1, 'first') + idx_start_decay-1 ; idx_end_decay = sp.nonzero(vec_envelope_smoothed[idx_start_decay:L_vec_x] <= threshold)[0][0] + idx_start_decay #tempfig('selected waveform'); hold on; #% subplot(211); #plot((idx_end_decay-1) / fs, x_envelope_smoothed(idx_end_decay), 'Marker', 'o', 'MarkerSize', 15, 'Color', 'g'); # # #% now model the decay #% (to estimate the decay time) #if true # val_start = (x_envelope_smoothed(idx_start_decay)); # val_end = (x_envelope_smoothed(idx_end_decay)); # decay_constant = 1*( log(val_end) - log(val_start) ) / (idx_end_decay - idx_start_decay); #else # val_start = x_envelope_smoothed(idx_start_decay); # #end # # % plot the model # x_model = (val_start) * exp(1*decay_constant * (0:(idx_end_decay - idx_start_decay))'); # tempfig('selected waveform'); hold on; # plot((idx_start_decay:idx_end_decay) / fs, x_model, 'k'); # # tau_decay_ms = -1 / (decay_constant * fs) * 1000 # idx_end_transient = idx_end_decay print('idx_start_transient: {}'.format(idx_start_transient)) print('idx_end_transient: {}'.format(idx_end_transient)) return (idx_start_transient, idx_end_transient)
def fastMonteCarlo(self, mig, kw): dic_init = self.dic_init ### Get parameters try: if sp.remainder(len(dic_init['fastMonteCarlo']), 2) != 0: raise nb_fMC = int(dic_init['fastMonteCarlo'][0]) seed_fMC = int(dic_init['fastMonteCarlo'][1]) sp.random.seed(seed=seed_fMC) nb_expected_values = sp.floor_divide( len(dic_init['fastMonteCarlo']) - 2, 2) for i in range(nb_expected_values): key = dic_init['fastMonteCarlo'][2 * (i + 1)] val = dic_init['fastMonteCarlo'][2 * (i + 1) + 1] if len(key) > 4 and key[:4] == 'fix_': kw[key] = bool(val) kw['error_' + key[4:]] = 0. elif len(key) > 5 and key[:5] == 'free_': kw[key] = bool(val) else: val = float(val) mig.values[key] = val kw[key] = val except Exception as error: print( ' ERROR::picca/py/picca/fitter/Chi2.py:: error in fast Monte-Carlo = ', error) print(' Exit') sys.exit(0) ### Get bes fit if not self.auto is None: rp = self.auto.rp rt = self.auto.rt z = self.auto.z bestFit_auto = self.cosmo.valueAuto(rp, rt, z, { p: mig.values[p] for p in self.cosmo.pglob + self.cosmo.pauto }) ### Metals met = None if not self.met is None: met = self.met.valueAuto(mig.values) bestFit_auto += met bestFit_auto = sp.dot(self.auto.dm, bestFit_auto) ### Broadband bb = None if not self.bb is None: p, b = self.bb.value(self.auto.da - bestFit_auto[self.auto.cuts]) bb = self.bb(rt, rp, p) if self.dic_init['distort_bb_auto']: bb = sp.dot(self.auto.dm, bb) bestFit_auto += bb if not self.cross is None: rp = self.cross.rp rt = self.cross.rt z = self.cross.z bestFit_cross = self.cosmo.valueCross(rp, rt, z, { p: mig.values[p] for p in self.cosmo.pglob + self.cosmo.pcross }) ### Metals met = None if not self.met is None: met = self.met.valueCross(mig.values) bestFit_cross += met bestFit_cross = sp.dot(self.cross.dm, bestFit_cross) ### Broadband bb = None if not self.bb_cross is None: p, b = self.bb_cross.value( self.cross.da - bestFit_cross[self.cross.cuts], mig.values['drp']) bb = self.bb_cross(rt, rp, mig.values['drp'], p) if self.dic_init['distort_bb_cross']: bb = sp.dot(self.cross.dm, bb) bestFit_cross += bb if not self.autoQSO is None: rp = self.autoQSO.rp rt = self.autoQSO.rt z = self.autoQSO.z bestFit_autoQSO = self.cosmo.valueAutoQSO(rp, rt, z, { p: mig.values[p] for p in self.cosmo.pglob + self.cosmo.pautoQSO }) bestFit_autoQSO = sp.dot(self.autoQSO.dm, bestFit_autoQSO) ### File to save into output_name = dic_init['output_prefix'] output_name += "save.pars.fastMonteCarlo" f = open(output_name, "w", 0) f.write("#\n#seed = {}\n#\n".format(seed_fMC)) for p in mig.parameters: f.write("{} ".format(p)) f.write("chi2 ") for p in mig.parameters: f.write("{} ".format("err_" + p)) f.write("err_chi2\n") ### Get realisation fastMonteCarlo for i in range(nb_fMC): print(' fastMonteCarlo: ', i, ' over ', nb_fMC) sys.stdout.flush() ### Get realisation fastMonteCarlo if not dic_init['data_auto'] is None: self.auto.get_realisation_fastMonteCarlo(bestFit=bestFit_auto) if not dic_init['data_cross'] is None: self.cross.get_realisation_fastMonteCarlo( bestFit=bestFit_cross) if not dic_init['data_autoQSO'] is None: self.autoQSO.get_realisation_fastMonteCarlo( bestFit=bestFit_autoQSO) ### Fit mig_fMC = iminuit.Minuit(self, forced_parameters=self.pname, errordef=1, print_level=0, **kw) try: mig_fMC.migrad() chi2_result = mig_fMC.get_fmin().fval except: chi2_result = sp.nan ### Save for p in mig_fMC.parameters: f.write("{} ".format(mig_fMC.values[p])) f.write("{} ".format(chi2_result)) for p in mig_fMC.parameters: f.write("{} ".format(mig_fMC.errors[p])) f.write("{}\n".format(0.)) f.close() return