shutil.copy(stimfile, copy_dir) # get frame indices frame_indices = [] with open(idx_txt, "r") as it: for line in it: frame_indices.append(int(line.strip())) start_extr = frame_indices[0] end_extr = frame_indices[-1] # extract and convert v.t1 - v.t2 range for idx in range(start_extr, (end_extr)): # extract frame using RawReader unconv_frame = rdr.get_frame(idx) # trim junk pixels off of top trimmed_frame = unconv_frame[junk:, :] if args.flop: trimmed_frame = np.fliplr(trimmed_frame) # convert to fan shape conv_frame = conv.convert(np.flipud(trimmed_frame)) ready_frame = np.flipud(conv_frame) # create frame handle and save to copy dir fh = basename + "." + "{0:05d}".format(idx) + ".bmp" out_img = Image.fromarray(ready_frame) out_img.save(os.path.join(copy_dir, fh))
print(header) # visualize components on (approximately) converted fan, if desired header.w = 127 # input image width header.h = 1020 #255 # input image height header.sf = 4000000 # for ultrasonix this is the vec-freq value probe = Probe() probe.radius = 10000 # based on '10' in transducer model number probe.numElements = 128 # based on '128' in transducer model number probe.pitch = 185 #205 # guess based on Ultrasonix C9-5/10 transducer c = Converter(header, probe) image_shape = (1020,127)#(255,127) rdr = RawReader(rf, nscanlines=nscanlines, npoints = npoints) print(rf) for f in np.arange(0,(last_idx+1)): d = rdr.get_frame(f).reshape(image_shape) mag = np.max(d) - np.min(d) d = (d-np.min(d))/mag*255 pcn = np.flipud(c.as_bmp(np.flipud(d))) #pcn = c.as_bmp(d) plt.title("Frame{:}, {:}".format((f+1),stim)) plt.imshow(pcn, cmap="Greys_r") file_ending = "subj{:}-{:}-{:}.png".format(subject, stim,(f)) ultradir = os.path.join(os.path.basename(barename),file_ending) savepath = os.path.join(expdir,ultradir) print(savepath) plt.savefig(savepath) # # consec_diffs = []
frameperc = int(last_idx * midperc) print(frameperc) frame_start = frameperc - prebuf frame_end = frameperc + postbuf print(frame_start) print(frame_end) consec_diffs = [] for f in np.arange(frame_start, frame_end): # old method of getting frames # minuend = get_frame(rf,f+1, myframesize, med_filter = True) # subtrahend = get_frame(rf, f, myframesize, med_filter = True) rdr = RawReader( rf, nscanlines=nscanlines, npoints=npoints ) # or whatever the appropriate parameters actually are minuend = rdr.get_frame(f + 1) subtrahend = rdr.get_frame(f) cdiff = minuend - subtrahend cdiffnorm = np.linalg.norm(cdiff) consec_diffs.append(cdiffnorm) # print(consec_diffs) mindiff, mindiff_idx = min( (val, idx) for (idx, val) in enumerate(consec_diffs)) # print(mindiff) # print(mindiff_idx) rl_frame_idx = frame_start + mindiff_idx print(rl_frame_idx) # get frame, and check for NaN frames change = 0 discard_acq = False while True:
diff_list = [] diff2_list = [] for frame in sync_pm.tier('pulse_idx'): diff = abs(frame.t1 - mid_timepoint) diff_list.append(diff) for frame in sync_pm.tier('raw_data_idx'): diff2 = abs(frame.t1 - mid_timepoint) diff2_list.append(diff2) mid_pulse_idx_num = min(enumerate(diff_list), key=itemgetter(1))[0] mid_raw_data_idx_num = min(enumerate(diff2_list), key=itemgetter(1))[0] # get frame, and check for NaN frames change = 0 discard_acq = False while True: pre_rawdata = rdr.get_frame(mid_pulse_idx_num) if pre_rawdata is None: mid_pulse_idx_num -= 1 mid_raw_data_idx_num -= 1 # TODO: necessary? change += 1 if change > threshhold: with open(logfile, "a") as log: log.write(acq + "\t" + stim + "\t" + phone + "\t" + "discarded" + "\t" + "passed threshhold") print("Frame change threshhold passed; acq {} discarded". format(acq)) discard_acq = True break else: pass else:
rlframes = [] for w in np.arange(0, len(prebufs)): frame_start = frameperc - prebufs[w] frame_end = frameperc + postbufs[w] # print(frame_start) # print(frame_end) consec_diffs = [] for f in np.arange(frame_start, frame_end): # old method of getting frames # minuend = get_frame(rf,f+1, myframesize, med_filter = True) # subtrahend = get_frame(rf, f, myframesize, med_filter = True) rdr = RawReader( rf, nscanlines=nscanlines, npoints=npoints ) # or whatever the appropriate parameters actually are minuend = rdr.get_frame(f + 1) # minuend = ndimage.median_filter(minuend, 3) subtrahend = rdr.get_frame(f) # subtrahend = ndimage.median_filter(subtrahend, 3) cdiff = minuend - subtrahend cdiffnorm = np.linalg.norm(cdiff) consec_diffs.append(cdiffnorm) # print(consec_diffs) mindiff, mindiff_idx = min( (val, idx) for (idx, val) in enumerate(consec_diffs)) # print(mindiff) # print(mindiff_idx) rl_frame_idx = frame_start + mindiff_idx print(rl_frame_idx) rlframes.append(rl_frame_idx)
diff2_list = [] for frame in sync_pm.tier('pulse_idx'): diff = abs(frame.t1 - midpoint) diff_list.append(diff) for frame in sync_pm.tier('raw_data_idx'): diff2 = abs(frame.t1 - midpoint) diff2_list.append(diff2) # TODO rewrite this chunk, temporary fix added mid_pulse_idx_num = min(enumerate(diff_list), key=itemgetter(1))[0] mid_raw_data_idx_num = min(enumerate(diff2_list), key=itemgetter(1))[0] # get midpoint frame; discard if out of recorded range try: raw = rdr.get_frame(mid_pulse_idx_num - 1) # temporary fix except IndexError: # thrown by RawReader.rdr if no frame at timepoint # issue warning and move entire acq to discards folder print("No frame available in {}, discarding".format(acq)) rdr.close() if not os.path.isdir(disc): os.mkdir(disc) shutil.copytree(parent, os.path.join(disc, acq)) shutil.rmtree(parent) continue trim = raw[junk:, :] # flop if needed if args.flop: trim = np.fliplr(trim)