def run_calculations(self): """Collect the settings and launch the calculation.""" statusmsg = self.statusbar.showMessage button_ok = self.bb_ok_cancel.button(QtGui.QDialogButtonBox.Ok) button_ok.setEnabled(False) infile = str(self.le_infile.text()) outfile = filehandle(str(self.le_outfile.text()), 'w') xmax = self.dsb_1.value() ymax = self.dsb_2.value() if (xmax + ymax) == 0: statusmsg('Input size required!') button_ok.setEnabled(True) return xtgt = self.sb_1.value() ytgt = self.sb_2.value() set_loglevel(self.sl_verbosity.value()) statusmsg('Parsing XML file...') spots = StatisticsSpots(infile) spots.set_limits(xmax=xmax, ymax=ymax) statusmsg('Generating bitmap...') bitmap = spots.gen_bitmap((xtgt, ytgt), delta=50) statusmsg('Writing output file...') savetxt(outfile, bitmap, fmt='%i') statusmsg('Finished writing bitmap (%i x %i).' % (xtgt, ytgt)) button_ok.setEnabled(True)
def run_calculations(self): """Collect the settings and launch the calculation.""" statusmsg = self.statusbar.showMessage button_ok = self.bb_ok_cancel.button(QtGui.QDialogButtonBox.Ok) button_ok.setEnabled(False) in_csv = str(self.le_infile.text()) out_csv = filehandle(str(self.le_outfile.text()), 'w') set_loglevel(self.sl_verbosity.value()) statusmsg('Parsing junction files...') junction = vp.CellJunction(in_csv) statusmsg('Writing output files...') junction.write_output(out_csv, in_csv) if (self.cb_option.checkState() == 2): statusmsg('Showing plot...') plot.junction(junction, True, False) statusmsg('Finished.') button_ok.setEnabled(True)
def gen_stats(f_in, f_out, label=False, deltas=[1], thresh=0, verbosity=0): """Parse and process tracks and calculate statistics from the data.""" # default loglevel is 30 while 20 and 10 show more details loglevel = (3 - verbosity) * 10 log.setLevel(loglevel) log.warn("Infile: %s" % f_in) log.debug("Outfile: %s" % f_out) log.info("Stepping width(s): %s" % deltas) log.info("Angle threshold: %s" % thresh) ppr = pprint.PrettyPrinter(indent=4) ######### tracks parsing ######### # TODO: parsing can be done in a nicer way be reading the header lines via # csvreader.next(), checking for the expected values and the number of # tracks and then directly reading the trackpoints into a numpy ndarray... mtrack2_file = filehandle(f_in, "r") csvreader = csv.reader(mtrack2_file, delimiter="\t") # parse all lines into memory # NOTE: this is bad if the files get too large, but we haven't seen result # files from MTrack2 that are bigger than a couple of MB. data = [] for row in csvreader: data.append([parse_cell(x) for x in row]) # data.append(row) # start parsing the header header = [] header.append(data.pop(0)) header.append(data.pop(0)) if not header[0][0] == "Frame": # exit because file is broken... raise SystemExit("Unable to find correct header, stopping.") log.debug("Header:\n%s\n" % ppr.pformat(header)) # second line is 'Tracks 1 to N', so we can read the total number there: trackmax = int(header[1][0].split(" ")[3]) log.info("Total number of tracks: %s" % ppr.pformat(trackmax)) # last N lines are the stats per track trackstats = [] while True: # pop returns the last element if no index is given cur = data.pop() if cur[0] == "Track": # remove one more line (empty), then we're done cur = data.pop() break else: trackstats.append(cur) # as we parsed from the last element, we need to reverse the list trackstats.reverse() log.warn("Track statistics:\n%s" % ppr.pformat(trackstats)) # this code can help debugging problematic files: # for row in data: # try: # np.array(row, dtype='float') # except ValueError: # raise SystemExit(row) # create the ndarray from the remaining data while removing column 0 # (indices), and every subsequent third column (flags) todelete = range(0, (trackmax + 1) * 3, 3) npdata = np.delete(data, todelete, axis=1) npdata_bool = npdata > 0 ######### tracks processing (combining etc.) ######### tracklen = [0] * trackmax t_overlap = npdata_bool[:, 0] for track in range(trackmax): tracklen[track] = sum(npdata_bool[:, track * 2]) t_overlap = t_overlap * npdata_bool[:, track * 2] if trackmax > 1 and sum(t_overlap) > 0: raise SystemExit("*** WARNING: Found overlapping tracks! ***") t_combined = np.zeros((npdata.shape[0], 2)) for track in range(trackmax): t_combined += npdata[:, track * 2 : (track + 1) * 2] comb_mask = np.zeros(t_combined.shape[0]) for i, row in enumerate(t_combined): if (row == [0.0, 0.0]).all(): # print 'row %i is zerooooo' % i comb_mask[i] = True t_combined = np.ma.compress_rows(np.ma.array(t_combined, mask=np.repeat(comb_mask, 2))) ######### calculations ######### mov_v = {} mov_n = {} rot = {} rot_t = {} outdata = t_combined if label: label = "pos_x\tpos_y" for step in deltas: # calculate movement vectors (mov_v): mov_v[step] = movement_vectors(t_combined, step) # calculate vector normals (mov_n): mov_n[step] = np.zeros((mov_v[step].shape[0], 1)) for pos in range(1, mov_n[step].shape[0]): mov_n[step][pos] = np.linalg.norm(mov_v[step][pos]) # calculate rotation: rot[step] = calc_rotation(mov_v[step], mov_n[step], step) # for the movement vectors all values need to be written to the output, # but it is not necessary to repeat them for every stepping, so they # are only added for stepping '1': if step == 1: outdata = np.hstack((outdata, mov_v[1])) if label: label += "\tdelta_x\tdelta_y" outdata = np.hstack((outdata, mov_n[step], rot[step])) # threshold rotation angles: if thresh > 0: rot_t[step] = np.where(abs(rot[step]) > thresh, rot[step], 0) outdata = np.hstack((outdata, rot_t[step])) if label: label += "\tdistance_%s\tangle_%s" % (step, step) if thresh > 0: label += "\tthresholded_angle_%s" % step if label: log.info("label: %s" % label) _save_results(f_out, outdata, label) log.warn("Wrote results to '%s'" % filename(f_out))