def test_BIC(self): """BIC loss function""" # -2*Likelihood == BIC for a sample size of 1 m = self.FakePointModel(dt=.02) sol = m.solve() s = ddm.Sample(aa([.02]), aa([])) expected = -np.log(np.sum(sol.corr)/m.dt) assert np.isclose(ddm.models.LossBIC(sample=s, dt=m.dt, T_dur=m.T_dur, nparams=1, samplesize=1).loss(m), 2*ddm.models.LossLikelihood(sample=s, dt=m.dt, T_dur=m.T_dur).loss(m))
def test_LossSquaredError(self): """Squared error loss function""" # Should be zero for empty sample when all undecided m = self.FakeUndecidedModel() s = ddm.Sample(aa([]), aa([]), undecided=1) assert ddm.models.LossSquaredError(sample=s, dt=m.dt, T_dur=m.T_dur).loss(m) == 0 # Can also be determined precisely for the point model m = self.FakePointModel() sol = m.solve() err = ddm.models.LossSquaredError(sample=s, dt=m.dt, T_dur=m.T_dur).loss(m) assert np.isclose(err, np.sum(sol.corr)**2 + np.sum(sol.err)**2)
def test_ICArbitrary(self): """Arbitrary starting conditions from a distribution""" # Make sure we get out the same distribution we put in m = ddm.Model() unif = ddm.models.ICUniform() unif_a = ddm.models.ICArbitrary(unif.get_IC(m.x_domain({}))) assert np.all(unif.get_IC(m.x_domain({})) == unif_a.get_IC(m.x_domain({}))) point = ddm.models.ICPointSourceCenter() point_a = ddm.models.ICArbitrary(point.get_IC(m.x_domain({}))) assert np.all(point.get_IC(m.x_domain({})) == point_a.get_IC(m.x_domain({}))) # Make sure the distribution integrates to 1 fails(lambda : ddm.models.ICArbitrary(aa([.1, .1, 0, 0, 0]))) fails(lambda : ddm.models.ICArbitrary(aa([0, .6, .6, 0]))) assert ddm.models.ICArbitrary(aa([1]))
def arrange_metric_windows(self): """ Determine the grid placement of metric figure windows and assign figure numbers. Should only be called once during the object's creation. """ #see if there are any other figures open so as not to overwrite their #contents figures=[manager.canvas.figure \ for manager in matplotlib._pylab_helpers.Gcf.get_all_fig_managers()] int_fig_offset = max(0,len(figures) - 1) for metric in self.ls_metrics: if metric.plot_enabled: #make row offset adjustment metric.figure_location += self.grid_size[0] * self.row_offset metric.w_size = self.resolutions[self.desktop] / self.grid_size int_row_offset = int((metric.figure_location + .1) / self.grid_size[0]) int_col_offset = int(mod(metric.figure_location, self.grid_size[0])) metric.w_coords = aa([int_col_offset * metric.w_size[0] + \ self.desktop * self.resolutions[self.desktop][0], \ int_row_offset * metric.w_size[1]]) #creating the Metrics' figure windows metric.figure_number = int_fig_offset + metric.figure_location metric.figure = plt.figure(metric.figure_number) wm = plt.get_current_fig_manager() wm.window.wm_geometry(str(metric.w_size[0]) + "x" + \ str(metric.w_size[1]) + "+" + \ str(metric.w_coords[0]) + "+" + \ str(metric.w_coords[1]))
def test_LossLikelihood(self): """Likelihood loss function""" # We can calculate likelihood for this simple case m = self.FakePointModel(dt=.02) sol = m.solve() s = ddm.Sample(aa([.02]), aa([])) expected = -np.log(np.sum(sol.corr)/m.dt) assert np.isclose(expected, ddm.models.LossLikelihood(sample=s, dt=m.dt, T_dur=m.T_dur).loss(m)) # And for the uniform case we can assert equivalence m = self.FakeUniformModel() s1 = ddm.Sample(aa([.02, .05, .07, .12]), aa([.33, .21])) s2 = ddm.Sample(aa([.13, .1, .02]), aa([.66, .15, .89])) assert np.isclose(ddm.models.LossLikelihood(sample=s1, dt=m.dt, T_dur=m.T_dur).loss(m), ddm.models.LossLikelihood(sample=s2, dt=m.dt, T_dur=m.T_dur).loss(m))
def setUp(self): self.samps = { # Empty sample "empty": ddm.Sample(aa([]), aa([]), 0), # Simple sample "simple": ddm.Sample(aa([1, 2]), aa([.5, .7]), 0), # Sample with conditions "conds": ddm.Sample(aa([1, 2, 3]), aa([]), 0, cond1=(aa([1, 1, 2]), aa([]))), # Sample with conditions and explicitly showing undecided "condsexp": ddm.Sample(aa([1, 2, 3]), aa([]), 0, cond1=(aa([1, 1, 2]), aa([]), aa([]))), # Sample with undecided "undec": ddm.Sample(aa([1, 2]), aa([.5, .7]), 2), # Sample with undecided and conditions "undeccond": ddm.Sample(aa([1, 2, 3]), aa([]), 3, cond1=(aa([1, 1, 2]), aa([]), aa([2, 2, 1]))), # For the adding test "adda": ddm.Sample(aa([1]), aa([2, 4]), 3, cond1=(aa(["a"]), aa(["a", "b"]), aa(["a", "b", "b"]))), "addb": ddm.Sample(aa([1.5, 2, 1]), aa([]), 1, cond1=(aa(["b", "b", "c"]), aa([]), aa(["d"]))), # Two conditions "two": ddm.Sample(aa([1]), aa([2]), 1, conda=(aa(["a"]), aa(["b"]), aa(["a"])), condb=(aa([1]), aa([2]), aa([2]))), }
def __init__(self, ps_parameters, str_section): """Class constructor for Results. """ super(Results,self).__init__(ps_parameters,str_section) self.ls_metric_names = self.get_val('metrics',False) if self.ls_metric_names.__class__.__name__ == 'str': self.ls_metric_names = [self.ls_metric_names] self.ls_metrics = [sf.create_section(ps_parameters, self.ls_metric_names[i]) \ for i in arange(len(self.ls_metric_names))] self.ls_metrics_csv = [self.ls_metrics[i] for i in arange(len(self.ls_metrics)) if self.ls_metrics[i].has_csv] self.ls_metrics_no_csv = [self.ls_metrics[i] for i in arange(len(self.ls_metrics)) if not self.ls_metrics[i].has_csv] self.grid_size = aa([self.get_val('figuregridwidth',True), \ self.get_val('figuregridheight',True)], dtype = np.int) self.desktop = self.get_val('desktop',True) self.row_offset = self.get_val('rowoffset',True) self.int_overlap = max(5,self.get_val('overlap',True)) self.save_interval = self.get_val('saveinterval',True) self.output_directory = self.get_val('outputdirectory',False) #default to the configuration file's filename as a prefix self.output_filename = self.get_val('outputfilename',False, default_value=self.get_params_fname(False)) self.overwrite_results = self.get_val('overwriteresults',True) self.zeros = self.get_val('zeros', True, DEFAULT_ZEROS) self.display_enabled = not self.get_val('disablefigures',True) self.monitor_count=0 self.resolutions=tuple() #get screen info if self.display_enabled: #try to find a display screen = os.popen("xrandr -q -d :0").readlines() if len(screen)>0: ls_res=re.findall(' connected [0-9]*x[0-9]*', ''.join(screen)) ls_res=re.findall('[0-9]*x[0-9]*', ''.join(ls_res)) #the ordering of these assumes the screens are in the same order #if this is not the case, simply rearrange your screens ;-) self.resolutions=tuple([np.array(res.split('x'),'uint16') for res in ls_res]) self.monitor_count = len(self.resolutions) self.desktop=np.mod(self.desktop,self.monitor_count) self.arrange_metric_windows() #figure out the coordinates else: #turn display off self.monitor_count=0 self.display_enabled = False #multiple viewport/desktop support not enabled, so wrapping the desktop to the range #of available monitors #create a folder in the output directory with the current minute's time stamp if self.output_directory=='': print ('Not writing results to file no output dir specified') return None st = '/' + self.output_filename self.output_directory += st + '/' if not os.path.exists(self.output_directory): os.makedirs(self.output_directory) if not self.overwrite_results: #old timestamping method, keep in case this is deemed better in the future # st += '/' + datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d_%H-%M-%S') #get the count of the current numbered directories files_enumerated = enumerate(os.walk(self.output_directory)) files,dir_info=files_enumerated.next() results_count_string=str(len(dir_info[1])).zfill(self.zeros) self.output_directory += '/' + results_count_string + '/' if not os.path.exists(self.output_directory): os.makedirs(self.output_directory) #save the parameters to this folder as ini, and write as csv self.ps_parameters.write(self.output_directory + self.output_filename + '_config.ini') self.ps_parameters.write_csv(self.output_directory + self.output_filename + '_config.' + DEFAULT_CSV_EXT)
def setUp(self): # You need some gymnastics to get numpy to accept an array of # same-length tuples _tuple_same_length = np.empty(3, dtype=object) _tuple_same_length[:] = [(3, 1, 2), (3, 3, 3), (5, 4, 3)] self.samps = { # Empty sample "empty": ddm.Sample(aa([]), aa([]), 0), # Simple sample "simple": ddm.Sample(aa([1, 2]), aa([.5, .7]), 0), # Sample with conditions "conds": ddm.Sample(aa([1, 2, 3]), aa([]), 0, cond1=(aa([1, 1, 2]), aa([]))), # Sample with conditions as strings "condsstr": ddm.Sample(aa([1, 2, 3]), aa([]), 0, cond1=(aa(["x", "yy", "z z z"]), aa([]))), # Sample with conditions as tuples "condstuple": ddm.Sample(aa([1, 2, 3]), aa([]), 0, cond1=(aa([(3, 1, 2), (5, 5), (1, 1, 1, 1, 1)], dtype=object), aa([]))), # Sample with conditions as tuples which are the same length "condstuplesame": ddm.Sample(aa([1, 2, 3]), aa([]), 0, cond1=(_tuple_same_length, aa([]))), # Sample with conditions and explicitly showing undecided "condsexp": ddm.Sample(aa([1, 2, 3]), aa([]), 0, cond1=(aa([1, 1, 2]), aa([]), aa([]))), # Sample with undecided "undec": ddm.Sample(aa([1, 2]), aa([.5, .7]), 2), # Sample with undecided and conditions "undeccond": ddm.Sample(aa([1, 2, 3]), aa([]), 3, cond1=(aa([1, 1, 2]), aa([]), aa([2, 2, 1]))), # For the adding test "adda": ddm.Sample(aa([1]), aa([2, 4]), 3, cond1=(aa(["a"]), aa(["a", "b"]), aa(["a", "b", "b"]))), "addb": ddm.Sample(aa([1.5, 2, 1]), aa([]), 1, cond1=(aa(["b", "b", "c"]), aa([]), aa(["d"]))), # Two conditions "two": ddm.Sample(aa([1]), aa([2]), 1, conda=(aa(["a"]), aa(["b"]), aa(["a"])), condb=(aa([1]), aa([2]), aa([2]))), }