def __init__(self, get_filenames, n_merge_bins=None, plot_bins_step=4, spread_data=False, neg_shift=True): Struct.__init__(self, get_filenames=get_filenames, n_merge_bins=n_merge_bins, plot_bins_step=plot_bins_step, spread_data=spread_data, neg_shift=neg_shift) self.current = Struct(filenames=None, dir_base=None, base_names=None) self.source_data = Struct(data=None, fdata=None, bins=None)
def __init__(self, model_class, n_components, parameters, constraints, solver_conf, output_dir): Struct.__init__(self, model_class=model_class, n_components=n_components, parameters=parameters, constraints=constraints, solver_conf=solver_conf, output_dir=output_dir)
def __init__(self, model_class, n_components, parameters, constraints, solver_conf, output_dir): Struct.__init__( self, model_class=model_class, n_components=n_components, parameters=parameters, constraints=constraints, solver_conf=solver_conf, output_dir=output_dir, )
def __call__(self): for filenames in self.get_filenames: dir_base, base_names = io.split_dir_base(filenames) print '======================================================' print 'directory base:', dir_base data = io.load_data(filenames) if self.n_merge_bins is not None: data = tr.merge_bins(data, self.n_merge_bins) print 'angles range:', data[:, 0].min(), data[:, 0].max() data = tr.fix_increasing(tr.fix_range(tr.transform_2pi(data))) print 'transformed angles range:', data[0, 0], data[-1, 0] print 'data range:', data[:, 1].min(), data[:, 1].max() # Simulate the "random process" the histogram was done from. counts = tr.get_counts_from_lengths(data[:, 1]) fdata = tr.spread_by_counts(data[:, 0], counts, trivial=self.spread_data == False) print 'simulated counts range:', counts.min(), counts.max() ddata = np.sort( tr.transform_pi_deg(data[:, 0], neg_shift=self.neg_shift)) dd = ddata[1] - ddata[0] all_bins = np.r_[ddata - 1e-8, ddata[-1] + dd] bins = all_bins[::self.plot_bins_step] self.current = Struct(filenames=filenames, dir_base=dir_base, base_names=base_names) self.source_data = Struct(counts=counts, data=data, fdata=fdata, bins=bins) yield self.source_data
def parse_row(self, row): """ Parse single hitogram row. """ n_components = int(row[12]) off = 13 + 3 * n_components params = np.array(map(float, row[13:off])).reshape((n_components, 3)) item = Struct( dir_base=row[0], converged=int(row[1]), fit_criteria=map(float, row[2:5]), chisquare=map(float, row[5:12]), n_components=n_components, params=params, base_names=[ii.strip() for ii in row[off + 1].split(',')]) assert (len(item.base_names) == int(row[off])) return item
def write_row(self, dir_base, base_names, chisquare, params, converged, fit_criteria): n_components = params.shape[0] item = Struct(dir_base=dir_base, base_names=base_names, chisquare=chisquare, params=params, converged=converged, n_components=n_components, fit_criteria=fit_criteria) self.items.append(item) fd = open(self.log_name, 'a') writer = csv.writer(fd) writer.writerow([dir_base, int(converged)] + fit_criteria + chisquare + [n_components] + params.ravel().tolist() + [len(base_names), ', '.join(base_names)]) fd.close()
def write_row(self, dir_base, base_names, area_angles): x0, xm, x1, area1, area2 = area_angles item = Struct(dir_base=dir_base, base_names=base_names, x0=x0, xm=xm, x1=x1, area1=area1, area2=area2) self.items.append(item) fd = open(self.log_name, 'a') writer = csv.writer(fd) writer.writerow( [dir_base] + [x0, 0.5 * (x0 + xm), xm, 0.5 * (xm + x1), x1, area1, area2] + [len(base_names), ', '.join(base_names)]) fd.close()
def make_summary(logs): """ Make a summary table of all results. The best parameter set according to each criterion is selected. Parameters ---------- logs : list of CSVLog A log with fitting results for each parameter set. Returns ------- summary : dict The summary dictionary with data directory base names as keys. """ dir_bases = [item.dir_base for item in logs[0].items] summary = {} for ii, dir_base in enumerate(dir_bases): # Results for dir_base for all parameter sets. items = [log.items[ii] for log in logs] criteria = [item.fit_criteria for item in items] pset_ids = np.array(criteria).argsort(axis=0) converged = np.take([item.converged for item in items], pset_ids) probs = np.take([item.params[:, 2] for item in items], pset_ids, axis=0) summary[dir_base] = Struct( sorted=zip(pset_ids, converged, probs), criteria=criteria, pset_ids=pset_ids, chisquare=[item.chisquare for item in items]) return summary
def __init__(self, psets): Struct.__init__(self, psets=psets)