def notify(self, key): import seaborn as sns y_shifts, x_shifts = (MotionCorrection() & key).fetch1( 'y_shifts', 'x_shifts') fps, is_slow_stack = (StackInfo.ROI() & key).fetch1('fps', 'is_slow') num_slices, num_frames = y_shifts.shape fps = fps * (num_slices if is_slow_stack else 1) seconds = np.arange(num_frames) / fps with sns.axes_style('white'): fig, axes = plt.subplots(2, 1, figsize=(13, 10), sharex=True, sharey=True) axes[0].set_title('Shifts in y for all slices') axes[0].set_ylabel('Pixels') axes[0].plot(seconds, y_shifts.T) axes[1].set_title('Shifts in x for all slices') axes[1].set_ylabel('Pixels') axes[1].set_xlabel('Seconds') axes[1].plot(seconds, x_shifts.T) fig.tight_layout() img_filename = '/tmp/' + key_hash(key) + '.png' fig.savefig(img_filename) plt.close(fig) sns.reset_orig() msg = 'MotionCorrection for `{}` has been populated.'.format(key) (notify.SlackUser() & (experiment.Session() & key)).notify( msg, file=img_filename, file_title='motion shifts')
def notify(self, key, frames): import imageio msg = 'Eye for `{}` has been populated. You can add a tracking task now. '.format(key) img_filename = '/tmp/' + key_hash(key) + '.gif' frames = frames.transpose([2, 0, 1]) frames = [imresize(img, 0.25) for img in frames] imageio.mimsave(img_filename, frames, duration=0.5) (notify.SlackUser() & (experiment.Session() & key)).notify(msg, file=img_filename, file_title='preview frames')
def notify(self, key, frames): import imageio video_filename = '/tmp/' + key_hash(key) + '.gif' frames = [imresize(img, 0.25) for img in frames.transpose([2, 0, 1])] imageio.mimsave(video_filename, frames, duration=0.5) msg = 'eye frames for {animal_id}-{session}-{scan_idx}'.format(**key) slack_user = notify.SlackUser() & (experiment.Session() & key) slack_user.notify(file=video_filename, file_title=msg, channel='#pipeline_quality')
def notify(self, key, frames): import imageio video_filename = '/tmp/' + key_hash(key) + '.gif' frames = [imresize(img, 0.25) for img in frames.transpose([2, 0, 1])] imageio.mimsave(video_filename, frames, duration=0.5) msg = 'posture frames for {animal_id}-{session}-{scan_idx}'.format(**key) slack_user = notify.SlackUser() & (experiment.Session() & key) slack_user.notify(file=video_filename, file_title=msg, channel='#pipeline_quality')
def notify(self, key): import imageio volume = (self & key).get_stack(channel=key['channel']) volume = volume[::int(volume.shape[0] / 8)] # volume at 8 diff depths video_filename = '/tmp/' + key_hash(key) + '.gif' imageio.mimsave(video_filename, float2uint8(volume), duration=1) msg = 'CorrectedStack for {} has been populated.'.format(key) (notify.SlackUser() & (experiment.Session() & key)).notify( msg, file=video_filename, file_title='stitched ROI')
def temporary_image(array, key): import matplotlib matplotlib.rcParams['backend'] = 'Agg' import matplotlib.pyplot as plt import seaborn as sns with sns.axes_style('white'): plt.matshow(array, cmap='gray') plt.axis('off') filename = '/tmp/' + key_hash(key) + '.png' plt.savefig(filename) sns.reset_orig() return filename
def notify(self, key): time, velocity = (self & key).fetch1('treadmill_time', 'treadmill_vel') fig = plt.figure() plt.plot(time, velocity) plt.ylabel('Treadmill velocity (cm/sec)') plt.xlabel('Seconds') img_filename = '/tmp/' + key_hash(key) + '.png' fig.savefig(img_filename) plt.close(fig) msg = 'treadmill velocity for {animal_id}-{session}-{scan_idx}'.format( **key) slack_user = notify.SlackUser() & (experiment.Session() & key) slack_user.notify(file=img_filename, file_title=msg)
def notify(self, key): import matplotlib.pyplot as plt time, velocity = (self & key).fetch1('treadmill_time', 'treadmill_vel') fig = plt.figure() plt.plot(time, velocity) plt.ylabel('Treadmill velocity (cm/sec)') plt.xlabel('Seconds') img_filename = '/tmp/' + key_hash(key) + '.png' fig.savefig(img_filename) plt.close(fig) msg = 'treadmill velocity for {animal_id}-{session}-{scan_idx}'.format(**key) slack_user = notify.SlackUser() & (experiment.Session() & key) slack_user.notify(file=img_filename, file_title=msg)
def notify(self, key, summary_frames, mean_intensities, contrasts): """ Sends slack notification for a single slice + channel combination. """ # Send summary frames import imageio video_filename = '/tmp/' + key_hash(key) + '.gif' percentile_99th = np.percentile(summary_frames, 99.5) summary_frames = np.clip(summary_frames, None, percentile_99th) summary_frames = float2uint8(summary_frames).transpose([2, 0, 1]) imageio.mimsave(video_filename, summary_frames, duration=0.4) msg = 'Quality for `{}` has been populated.'.format(key) (notify.SlackUser() & (experiment.Session() & key)).notify( msg, file=video_filename, file_title='summary frames') # Send intensity and contrasts figsize = (min(4, contrasts.shape[1] / 10 + 1), contrasts.shape[0] / 30 + 1) # set heuristically fig, axes = plt.subplots(1, 2, figsize=figsize, sharex=True, sharey=True) fig.tight_layout() axes[0].set_title('Mean intensity', size='small') axes[0].imshow(mean_intensities) axes[0].set_ylabel('Slices') axes[0].set_xlabel('Frames') axes[1].set_title('Contrast (99 - 1 percentile)', size='small') axes[1].imshow(contrasts) axes[1].set_xlabel('Frames') img_filename = '/tmp/' + key_hash(key) + '.png' fig.savefig(img_filename, bbox_inches='tight') plt.close(fig) (notify.SlackUser() & (experiment.Session() & key)).notify( file=img_filename, file_title='quality images')
def notify(self, key): ts, temperatures = (self & key).fetch1('temp_time', 'temperatures') import matplotlib.pyplot as plt fig = plt.figure(figsize=(10, 5)) plt.plot(ts, temperatures) plt.ylabel('Temperature (C)') plt.xlabel('Seconds') img_filename = '/tmp/' + key_hash(key) + '.png' fig.savefig(img_filename) plt.close(fig) msg = 'temperature for {animal_id}-{session}-{scan_idx}'.format(**key) slack_user = notify.SlackUser() & (experiment.Session() & key) slack_user.notify(file=img_filename, file_title=msg)
def _make_tuples(self, key): import matplotlib.pyplot as plt import matplotlib.ticker as ticker # Get all field keys and timestamps field_key, field_ts, field_nframes, field_fps = ((experiment.Scan() * meso.ScanInfo() * meso.ScanInfo.Field().proj()).proj('nframes', 'fps', 'scan_ts', scan_session='session') & key & 'field < 10').fetch('KEY', 'scan_ts', 'nframes', 'fps', order_by='scan_ts') if len(field_key) == 0: print('Warning: No fields selected for', key) return initial_time = str(field_ts[0]) field_ts = [(ts - field_ts[0]).seconds for ts in field_ts] # in hours field_duration = field_nframes / field_fps # Plot fig = plt.figure(figsize=(20, 8)) for fk, ft, fd in zip(field_key, field_ts, field_duration): zs = (RegistrationOverTime() & key & fk).fetch('reg_z', order_by='frame_id') ts = ft + np.linspace(0, 1, len(zs) + 2)[1:-1] * fd plt.plot(ts / 3600, zs) plt.title('Registered zs for {animal_id}-{scan_session} starting {t}'.format(t=initial_time, **key)) plt.ylabel('Registered zs') plt.xlabel('Hours') # Plot formatting plt.gca().invert_yaxis() plt.gca().yaxis.set_major_locator(ticker.MultipleLocator(10)) plt.grid(linestyle='--', alpha=0.8) # Notify img_filename = '/tmp/' + key_hash(key) + '.png' fig.savefig(img_filename, bbox_inches='tight') plt.close(fig) msg = 'registration over time for {animal_id}-{scan_session} into {animal_id}-{stack_session}-{stack_idx}'.format(**key) slack_user = notify.SlackUser() & (experiment.Session() & key & {'session': key['stack_session']}) slack_user.notify(file=img_filename, file_title=msg, channel='#pipeline_quality') self.insert1(key, ignore_extra_fields=True)