|
| 1 | +import datajoint as dj |
| 2 | +import numpy as np |
| 3 | +import os |
| 4 | +from skimage import io |
| 5 | + |
| 6 | +# import the mouse-session schema |
| 7 | +from tutorial_pipeline.mouse_session import schema, Mouse, Session |
| 8 | + |
| 9 | + |
| 10 | +# Table definitions |
| 11 | + |
| 12 | +@schema |
| 13 | +class Scan(dj.Manual): |
| 14 | + definition = """ |
| 15 | + -> Session |
| 16 | + scan_idx : int # scan index |
| 17 | + --- |
| 18 | + depth : float # depth of this scan |
| 19 | + wavelength : float # wavelength used |
| 20 | + laser_power : float # power of the laser used |
| 21 | + fps : float # frames per second |
| 22 | + file_name : varchar(128) # name of the tif file |
| 23 | + """ |
| 24 | + |
| 25 | + |
| 26 | +@schema |
| 27 | +class AverageFrame(dj.Imported): |
| 28 | + definition = """ |
| 29 | + -> Scan |
| 30 | + --- |
| 31 | + average_frame : longblob # average fluorescence across frames |
| 32 | + """ |
| 33 | + def make(self, key): # key is the primary key of one of the entries in the table `Scan` |
| 34 | + # fetch data directory from table Session |
| 35 | + data_path = (Session & key).fetch1('data_path') |
| 36 | + |
| 37 | + # fetch data file name from table Scan |
| 38 | + file_name = (Scan & key).fetch1('file_name') |
| 39 | + |
| 40 | + # load the file |
| 41 | + im = io.imread(os.path.join(data_path, file_name)) |
| 42 | + # compute the average image across the frames |
| 43 | + avg_image = np.mean(im, axis=0) |
| 44 | + |
| 45 | + # Now prepare the entry as a dictionary with all fields defined in the table. |
| 46 | + key['average_frame'] = avg_image # inherit the primary key from the table Scan |
| 47 | + |
| 48 | + # insert entry with the method `insert1()` |
| 49 | + self.insert1(key) |
| 50 | + |
| 51 | + print('\tPopulated Scan {mouse_id} - {session_date} - {scan_idx}'.format(**key)) |
| 52 | + |
| 53 | +Scan.insert([ |
| 54 | + {'mouse_id': 0, 'session_date': '2017-05-15', 'scan_idx': 1, |
| 55 | + 'depth': 150, 'wavelength': 920, 'laser_power': 26, 'fps': 15, 'file_name': 'example_scan_01.tif'}, |
| 56 | + {'mouse_id': 0, 'session_date': '2017-05-15', 'scan_idx': 2, |
| 57 | + 'depth': 200, 'wavelength': 920, 'laser_power': 24, 'fps': 15, 'file_name': 'example_scan_02.tif'}, |
| 58 | + {'mouse_id': 100, 'session_date': '2017-05-25', 'scan_idx': 1, |
| 59 | + 'depth': 150, 'wavelength': 920, 'laser_power': 25, 'fps': 15, 'file_name': 'example_scan_03.tif'}], |
| 60 | + skip_duplicates=True) |
| 61 | + |
| 62 | +AverageFrame.populate() |
0 commit comments