def optimize(self,obj=np.max,index=0): c3_increment = self.coef_3_step_size*self.coef_3_multiplier c2_increment = self.coef_2_step_size*self.coef_2_multiplier c3_vec = np.arange(self.coef_3_min,self.coef_3_max+c3_increment,c3_increment) c2_vec = np.arange(self.coef_2_min,self.coef_2_max+c2_increment,c2_increment) scores = np.ones((len(c3_vec),len(c2_vec)))*obj(self.bscan) winning_score = -np.inf c3_winner = self.dispersion_coefs[0] c2_winner = self.dispersion_coefs[1] for idx3,c3 in enumerate(c3_vec): for idx2,c2 in enumerate(c2_vec): coefs = [c3,c2,0.0,0.0] try: test = self.proc_cache[(index,c3,c2)] except KeyError as ke: test = np.abs(process(self.raw_vol[index,:,:],self.k_in,self.k_out,coefs)[Z_CUTON:Z_CUTOFF,X_START:]).T self.proc_cache[(index,c3,c2)] = test score = obj(test) scores[idx3,idx2] = score if score>winning_score: c3_winner = c3 c2_winner = c2 plt.imshow(scores,interpolation='none') plt.pause(.1) plt.colorbar() self.coef_3_text.setValue(c3_winner/self.coef_3_multiplier) self.coef_2_text.setValue(c2_winner/self.coef_2_multiplier) self.change_coefs() self.show_bscan()
def show_bscan(self,index=None): if index is None: index = self.index try: self.bscan = self.proc_cache[(index,self.dispersion_coefs[0],self.dispersion_coefs[1])] except KeyError as ke: self.bscan = np.abs(process(self.raw_vol[index,:,:],self.k_in,self.k_out,self.dispersion_coefs)[Z_CUTON:Z_CUTOFF,X_START:]).T self.proc_cache[(index,self.dispersion_coefs[0],self.dispersion_coefs[1])] = self.bscan self.compute_stats() self.set_coef_labels() self.canvas.setImage(self.bscan)
def __init__(self, unp_list, auto=False): self.h5_list = [] self.coefs = [] self.k_ins = [] self.k_outs = [] self.n_slows = [] winners = [] for u in unp_list: h5fn = u.replace(".unp", "") + ".hdf5" h5 = H5(h5fn) self.h5_list.append(h5) self.k_ins.append(h5["k_in"][:]) self.k_outs.append(h5["k_out"][:]) self.coefs.append(h5["dispersion/coefficients"][:]) self.n_slows.append(h5["config/n_slow"].value) for h5, kin, kout, n_slow in zip(self.h5_list, self.k_ins, self.k_outs, self.n_slows): scores = [] for idx, coef in enumerate(self.coefs): f = h5["raw_data"][0, int(n_slow / 2), 5:-5:, :] p = np.abs(process(f, kin, kout, coef)) profile = np.mean(p, axis=1) pmax = np.max(profile) pmin = np.min(profile) pcontrast = (pmax - pmin) / (pmax + pmin) pgrad = np.max(np.abs(np.diff(profile))) c = 2 score = pgrad * (pmax + c * pcontrast) scores.append(score) if not auto: print "%d\t%0.1f\t%0.3f\t%0.1f\t%0.1f" % (idx + 1, pmax, pcontrast, pgrad, score) plt.figure() plt.subplot(1, 2, 1) clim = np.percentile(p, (5, 99.9)) plt.imshow(p, interpolation="none", aspect="auto", clim=clim, cmap="gray") plt.subplot(1, 2, 2) plt.plot(profile) if not auto: plt.show() winner = int(raw_input("Choose best figure number: ")) - 1 else: # print 'scores :',scores winner = np.argmax(scores) print "winner: %d" % winner, self.coefs[winner] winners.append(winner) print "winners:", winners mfw = max(set(winners), key=winners.count) print "most frequent winner: ", mfw
def optimize2(self,obj=None,index=None): if obj is None: obj = self.gradient if index is None: index = self.index c3_increment = self.coef_3_step_size*self.coef_3_multiplier c2_increment = self.coef_2_step_size*self.coef_2_multiplier #c3_vec = np.arange(self.coef_3_min,self.coef_3_max+c3_increment,c3_increment) #c2_vec = np.arange(self.coef_2_min,self.coef_2_max+c2_increment,c2_increment) c3_vec = np.arange(self.dispersion_coefs[0]-c3_increment*N_STEPS,self.dispersion_coefs[0]+c3_increment*(N_STEPS+1),c3_increment) c2_vec = np.arange(self.dispersion_coefs[1]-c2_increment*N_STEPS,self.dispersion_coefs[1]+c2_increment*(N_STEPS+1),c2_increment) c3_max = np.max(c3_vec) c2_max = np.max(c2_vec) c3_min = np.min(c3_vec) c2_min = np.min(c2_vec) c3 = self.dispersion_coefs[0] c2 = self.dispersion_coefs[1] scores = np.ones((len(c3_vec),len(c2_vec)))*obj(self.bscan) print c3_min,c3,c3_max print c2_min,c2,c2_max while c3_min<=c3<=c3_max and c2_min<=c2<=c2_max: print c3_min<=c3<=c3_max print c2_min<=c2<=c2_max best_score = -np.inf for d3 in range(-1,2): for d2 in range(-1,2): coefs = [c3+d3*c3_increment,c2+d2*c2_increment,0.0,0.0] try: test = self.proc_cache[(index,c3,c2)] except KeyError as ke: test = np.abs(process(self.raw_vol[index,:,:],self.k_in,self.k_out,coefs)[Z_CUTON:Z_CUTOFF,X_START:]).T self.proc_cache[(index,c3,c2)] = test score = obj(test) if score>best_score: best_d3 = d3 best_d2 = d2 best_score = score c3 = c3+best_d3*c3_increment c2 = c2+best_d2*c2_increment i3 = np.argmin(np.abs(c3-c3_vec)) i2 = np.argmin(np.abs(c2-c2_vec)) scores[i3,i2] = best_score self.canvas.setImage(scores) if best_d3==0 and best_d2==0: break c3_winner = c3 c2_winner = c2 self.coef_3_text.setValue(c3_winner/self.coef_3_multiplier) self.coef_2_text.setValue(c2_winner/self.coef_2_multiplier) self.change_coefs() self.show_bscan()