d_real = disc(real_data) d_fake = disc(fake_data) disc_loss = F.relu(1 - d_real).mean() + F.relu(1 + d_fake).mean() if disc_loss > 0: disc_loss.backward(torch.ones(1)) optim_disc.step() print(i, disc_loss.cpu().detach().numpy(), "{0:.2f}s".format(time() - tic)) tic = time() if i % 100 == 0: gen.eval() for j in range(1): a, b, c = gen(z_test) a = a.cpu().detach().numpy().transpose(0, 2, 1).reshape( -1, 3, 2).transpose(0, 2, 1).astype(np.float64) b = b.cpu().detach().numpy().transpose(0, 2, 1).reshape( -1, 3, 2).transpose(0, 2, 1).astype(np.float64) c = c.cpu().detach().numpy().transpose(0, 2, 1).reshape( -1, 3, 2).transpose(0, 2, 1).astype(np.float64) raster([[a, b, c]]) plt.savefig("outs/test{:010d}.png".format(i)) plt.close() gen.train()
d_fake = disc(fake_data[0], fake_data[1], fake_data[2]) d_real = disc(a, pad(b, a), pad(c, a)) disc_loss = F.relu(1 - d_real).mean() + F.relu(1 + d_fake).mean() if disc_loss > 0: disc_loss.backward(torch.ones(1)) optim_disc.step() print(i, disc_loss.cpu().detach().numpy(), "{0:.2f}s".format(time() - tic)) tic = time() if i % 100 == 0: gen.eval() for j in range(1): a, b, c = gen.forward([1, latent_dim, num_curves], z_test) a = a.cpu().detach().numpy().transpose(0, 2, 1).reshape( -1, 3, 2).transpose(0, 2, 1).astype(np.float64) b = b.cpu().detach().numpy().transpose(0, 2, 1).reshape( -1, 3, 2).transpose(0, 2, 1).astype(np.float64) #c = c.cpu().detach().numpy().transpose(0, 2, 1).reshape( # -1, 3, 2).transpose(0, 2, 1).astype(np.float64) raster([[a, b]]) #, c]]) plt.savefig("outs/test{:010d}.png".format(i)) plt.close() gen.train()
def fr_for_units_by_led(location): base, sess = os.path.split(location) with open(os.path.join(location, 'spike_and_trials.pickle'), 'rb') as f: data = pickle.load(f) stepname, durations = get_LED_stepname(sess) framechan, shift = get_frame_channel(sess) LEDchan = get_LED_channel(sess) trial_numbers = numpy.asarray(data['trial_records']['trial_number']) step_names = numpy.asarray(data['trial_records']['step_name']) contrasts = numpy.asarray(data['trial_records']['contrast']) max_durations = numpy.asarray(data['trial_records']['max_duration']) phases = numpy.asarray(data['trial_records']['phase']) orientations = numpy.asarray(data['trial_records']['orientation']) led_status = numpy.multiply( numpy.asarray(data['trial_records']['led_on']), numpy.nan_to_num(numpy.asarray( data['trial_records']['led_intensity']))) which_step = step_names == stepname trial_numbers = trial_numbers[which_step] step_names = step_names[which_step] contrasts = contrasts[which_step] max_durations = max_durations[which_step] phases = phases[which_step] orientations = orientations[which_step] led_status = led_status[which_step] frame_start_index = [] frame_end_index = [] for trial_number in trial_numbers: event_for_trial = data['trial_records']['events']['ttl_events'][ trial_number] frame_start_index.append(event_for_trial[LEDchan]['rising'][0]) frame_end_index.append(event_for_trial[LEDchan]['falling'][0]) frame_start_index = numpy.asarray(frame_start_index) frame_end_index = numpy.asarray(frame_end_index) unit_responses_that_session = {} unit_responses_that_session['session'] = sess unit_responses_that_session['subject'] = get_subject_from_session(sess) for i, unit in enumerate(data['spike_records']['units']): if not unit['manual_quality'] in ['good', 'mua']: continue unit_details = {} unit_details['uid'] = get_unit_id(sess, unit['shank_no'], unit['cluster_id']) unit_details['manual_quality'] = unit['manual_quality'] unit_details['unit_depth'] = get_unit_depth(sess, unit['y_loc']) unit_details['unit_depth'] = get_unit_dist_to_LED(sess, unit['y_loc']) spike_time = numpy.squeeze(numpy.asarray(unit['spike_time'])) spike_raster = {} spike_raster_expanded = {} # t-100ms to t+500 ms for trial_number in trial_numbers: frame_start_time = frame_start_index[trial_numbers == trial_number][0] / 30000 frame_end_time = frame_end_index[trial_numbers == trial_number][0] / 30000 spike_raster[trial_number] = spike_time[numpy.bitwise_and( spike_time > frame_start_time, spike_time < frame_end_time)] - frame_start_time spike_raster_expanded[trial_number] = spike_time[numpy.bitwise_and( spike_time > (frame_start_time - 0.5), spike_time < (frame_end_time + 1))] - frame_start_time unit_details['spike_raster'] = spike_raster unit_details['spike_raster_expanded'] = spike_raster_expanded # plot by LED LED_OFFs = led_status == 0 trial_led_off = trial_numbers[LED_OFFs] trial_led_on = trial_numbers[numpy.bitwise_not(LED_OFFs)] events_led_off = [] events_led_on = [] for tr in trial_led_off: events_led_off.append(spike_raster_expanded[tr]) for tr in trial_led_on: events_led_on.append(spike_raster_expanded[tr]) # pdb.set_trace() # isi = numpy.diff(unit['spike_time'],axis=0) # histc, binedge = numpy.histogram(isi,range=(0,0.1),bins=100) # fig = plt.figure(figsize=(3,3),dpi=200,facecolor='none',edgecolor='none') # plt.clf() # plt.bar(binedge[0:100]*1000,histc,align='edge',edgecolor='none',color='k') # plt.show() fig = plt.figure(figsize=(3, 3), dpi=200, facecolor='none', edgecolor='none') plt.clf() ax_off = plt.subplot(211) raster(events_led_off) ax_on = plt.subplot(212) raster(events_led_on, color='b') plt.show() print('done_unit') unit_responses_that_session[i] = unit_details print('done session')