Exemple #1
0
 def setUpContainer(self):
     self.device = Device(name='dev1')
     self.optical_channel = OpticalChannel('optchan1',
                                           'a fake OpticalChannel', 500.)
     return ImagingPlane('imgpln1', self.optical_channel,
                         'a fake ImagingPlane', self.device, 600., 300.,
                         'GFP', 'somewhere in the brain')
Exemple #2
0
 def buildPlaneSegmentationNoRois(self):
     """ Return an PlaneSegmentation and set related objects """
     ts = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
     self.image_series = ImageSeries(name='test_iS',
                                     dimension=[2],
                                     external_file=['images.tiff'],
                                     starting_frame=[1, 2, 3],
                                     format='tiff',
                                     timestamps=ts)
     self.device = Device(name='dev1')
     self.optical_channel = OpticalChannel(
         name='test_optical_channel',
         description='optical channel description',
         emission_lambda=500.)
     self.imaging_plane = ImagingPlane(
         name='test_imaging_plane',
         optical_channel=self.optical_channel,
         description='imaging plane description',
         device=self.device,
         excitation_lambda=600.,
         imaging_rate=300.,
         indicator='GFP',
         location='somewhere in the brain',
         reference_frame='a frame to refer to')
     return PlaneSegmentation(description='description',
                              imaging_plane=self.imaging_plane,
                              name='test_plane_seg_name',
                              reference_images=self.image_series)
Exemple #3
0
def CreatePlaneSegmentation():
    w, h = 5, 5
    img_mask = [[[1.0 for x in range(w)] for y in range(h)],
                [[2.0 for x in range(w)] for y in range(h)]]
    pix_mask = [[1, 2, 1.0], [3, 4, 1.0], [5, 6, 1.0], [7, 8, 2.0],
                [9, 10, 2.0]]

    iSS = ImageSeries(name='test_iS',
                      data=np.ones((2, 2, 2)),
                      unit='unit',
                      external_file=['external_file'],
                      starting_frame=[1, 2, 3],
                      format='tiff',
                      timestamps=[1., 2.])

    oc = OpticalChannel('test_optical_channel', 'description', 500.)
    device = Device(name='device_name')
    ip = ImagingPlane('test_imaging_plane', oc, 'description', device, 600.,
                      300., 'indicator', 'location', (1, 2, 1, 2, 3), 4.0,
                      'unit', 'reference_frame')

    pS = PlaneSegmentation('description', ip, 'test_name', iSS)
    pS.add_roi(pixel_mask=pix_mask[0:3], image_mask=img_mask[0])
    pS.add_roi(pixel_mask=pix_mask[3:5], image_mask=img_mask[1])
    return pS
Exemple #4
0
    def test_init(self):
        w, h = 5, 5
        img_mask = [[[1.0 for x in range(w)] for y in range(h)],
                    [[2.0 for x in range(w)] for y in range(h)]]
        pix_mask = [[1, 2, 1.0], [3, 4, 1.0], [5, 6, 1.0], [7, 8, 2.0],
                    [9, 10, 2.0]]

        iSS = ImageSeries(name='test_iS',
                          source='a hypothetical source',
                          data=list(),
                          unit='unit',
                          external_file=['external_file'],
                          starting_frame=[1, 2, 3],
                          format='tiff',
                          timestamps=list())

        oc = OpticalChannel('test_optical_channel', 'test_source',
                            'description', 500.)
        ip = ImagingPlane('test_imaging_plane', 'test_source', oc,
                          'description', 'device', 600., 'imaging_rate',
                          'indicator', 'location', (1, 2, 1, 2, 3), 4.0,
                          'unit', 'reference_frame')

        pS = PlaneSegmentation('test source', 'description', ip, 'test_name',
                               iSS)
        pS.add_roi("1234", pix_mask[0:3], img_mask[0])
        pS.add_roi("5678", pix_mask[3:5], img_mask[1])

        self.assertEqual(pS.description, 'description')
        self.assertEqual(pS.source, 'test source')

        self.assertEqual(pS.imaging_plane, ip)
        self.assertEqual(pS.reference_images, iSS)
        self.assertEqual(pS.pixel_masks.data, pix_mask)
        self.assertEqual(pS.image_masks.data, img_mask)
Exemple #5
0
 def addContainer(self, file):
     dev1 = file.create_device('dev1', 'dev1 description')
     oc = OpticalChannel('optchan1', 'a fake OpticalChannel', 3.14)
     ip = file.create_imaging_plane('imgpln1',
                                    oc,
                                    description='a fake ImagingPlane',
                                    device=dev1,
                                    excitation_lambda=6.28,
                                    indicator='GFP',
                                    location='somewhere in the brain',
                                    imaging_rate=2.718)
     data = np.ones((3, 3, 3))
     timestamps = list(range(10))
     fov = [2.0, 2.0, 5.0]
     tps = TwoPhotonSeries('test_2ps',
                           ip,
                           data,
                           'image_unit',
                           'raw',
                           fov,
                           1.7,
                           3.4,
                           timestamps=timestamps,
                           dimension=[200, 200])
     file.add_acquisition(tps)
     return tps
Exemple #6
0
    def buildPlaneSegmentation(self):
        w, h = 5, 5
        img_mask = [[[1.0 for x in range(w)] for y in range(h)],
                    [[2.0 for x in range(w)] for y in range(h)]]
        pix_mask = [(1, 2, 1.0), (3, 4, 1.0), (5, 6, 1.0), (7, 8, 2.0),
                    (9, 10, 2.)]

        ts = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
        self.image_series = ImageSeries(name='test_iS',
                                        source='a hypothetical source',
                                        dimension=[2],
                                        external_file=['images.tiff'],
                                        starting_frame=[1, 2, 3],
                                        format='tiff',
                                        timestamps=ts)

        self.optical_channel = OpticalChannel('test_optical_channel',
                                              'optical channel source',
                                              'optical channel description',
                                              500.)
        self.imaging_plane = ImagingPlane(
            'test_imaging_plane', 'ophys integration tests',
            self.optical_channel, 'imaging plane description',
            'imaging_device_1', 600., '2.718', 'GFP', 'somewhere in the brain',
            (1, 2, 1, 2, 3), 4.0, 'manifold unit', 'A frame to refer to')

        self.img_mask = deepcopy(img_mask)
        self.pix_mask = deepcopy(pix_mask)
        pS = PlaneSegmentation('integration test PlaneSegmentation',
                               'plane segmentation description',
                               self.imaging_plane, 'test_plane_seg_name',
                               self.image_series)
        pS.add_roi('1234', pix_mask[0:3], img_mask[0])
        pS.add_roi('5678', pix_mask[3:5], img_mask[1])
        return pS
Exemple #7
0
    def __init__(self,
                 nwbfile,
                 emission_lambda=np.nan,
                 excitation_lambda=np.nan,
                 frame_rate=np.nan,
                 indicator='unknown',
                 location='unknown'):

        super(OphysProcessing2NWB, self).__init__(nwbfile)

        device = Device('microscope')
        self.nwbfile.add_device(device)
        optical_channel = OpticalChannel('OpticalChannel',
                                         'description',
                                         emission_lambda=emission_lambda)
        imaging_plane = self.nwbfile.create_imaging_plane(
            'ImagingPlane',
            optical_channel,
            description='description',
            device=device,
            excitation_lambda=excitation_lambda,
            imaging_rate=frame_rate,
            indicator=indicator,
            location=location)

        self.ophys_mod = self.nwbfile.create_processing_module(
            'ophys', 'contains optical physiology processed data')
        img_seg = ImageSegmentation()
        self.ophys_mod.add_data_interface(img_seg)
        self.ps = img_seg.create_plane_segmentation(
            'output from segmenting my favorite imaging plane', imaging_plane,
            'PlaneSegmentation')
Exemple #8
0
    def test_init(self):
        oc = OpticalChannel('test_name', 'description', 500.)
        self.assertEqual(oc.description, 'description')
        self.assertEqual(oc.emission_lambda, 500.)

        device = Device(name='device_name')
        ip = ImagingPlane('test_imaging_plane', oc, 'description', device, 600.,
                          300., 'indicator', 'location', (50, 100, 3), 4.0, 'unit', 'reference_frame')
        self.assertEqual(ip.optical_channel[0], oc)
        self.assertEqual(ip.device, device)
        self.assertEqual(ip.excitation_lambda, 600.)
        self.assertEqual(ip.imaging_rate, 300.)
        self.assertEqual(ip.indicator, 'indicator')
        self.assertEqual(ip.location, 'location')
        self.assertEqual(ip.manifold, (50, 100, 3))
        self.assertEqual(ip.conversion, 4.0)
        self.assertEqual(ip.unit, 'unit')
        self.assertEqual(ip.reference_frame, 'reference_frame')

        tPS = TwoPhotonSeries('test_tPS', unit='unit', field_of_view=[2., 3.],
                              imaging_plane=ip, pmt_gain=1.0, scan_line_rate=2.0, external_file=['external_file'],
                              starting_frame=[1, 2, 3], format='tiff', timestamps=list())
        self.assertEqual(tPS.name, 'test_tPS')
        self.assertEqual(tPS.unit, 'unit')
        self.assertEqual(tPS.field_of_view, [2., 3.])
        self.assertEqual(tPS.imaging_plane, ip)
        self.assertEqual(tPS.pmt_gain, 1.0)
        self.assertEqual(tPS.scan_line_rate, 2.0)
        self.assertEqual(tPS.external_file, ['external_file'])
        self.assertEqual(tPS.starting_frame, [1, 2, 3])
        self.assertEqual(tPS.format, 'tiff')
        self.assertIsNone(tPS.dimension)
Exemple #9
0
    def _get_imaging_plane(self):
        """Add new / return existing Imaging Plane"""
        meta_imgplane = self.metadata['Ophys']['ImagingPlane'][0]
        if meta_imgplane['name'] in self.nwbfile.imaging_planes:
            imaging_plane = self.nwbfile.imaging_planes[meta_imgplane['name']]
        else:
            with h5py.File(self.source_paths['path_processed'], 'r') as f:
                if 'depth' in f:
                    description = 'high zoom'
                else:
                    description = 'low zoom'
                animal_id = str(int(f['aid'][0]))
                subject_info = subjects_info[animal_id]
                indicator = subject_info['indicator']
                imaging_rate = 1 / f['dto'][0]

                # Create Imaging Plane
                meta_optch = meta_imgplane['optical_channel'][0]
                optical_channel = OpticalChannel(**meta_optch)
                imaging_plane = self.nwbfile.create_imaging_plane(
                    name='imaging_plane',
                    optical_channel=optical_channel,
                    description=description,
                    device=self.nwbfile.devices[meta_imgplane['device']],
                    excitation_lambda=meta_imgplane['excitation_lambda'],
                    indicator=indicator,
                    location=meta_imgplane['location'],
                    imaging_rate=imaging_rate,
                )

        return imaging_plane
Exemple #10
0
def CreatePlaneSegmentation():
    w, h = 5, 5
    img_mask = [[0 for x in range(w)] for y in range(h)]
    w, h = 5, 2
    pix_mask = [[0 for x in range(w)] for y in range(h)]
    pix_mask_weight = [0 for x in range(w)]
    iSS = ImageSeries(name='test_iS',
                      source='a hypothetical source',
                      data=list(),
                      unit='unit',
                      external_file=['external_file'],
                      starting_frame=[1, 2, 3],
                      format='tiff',
                      timestamps=list())

    roi1 = ROI('roi1', 'test source', 'roi description1', pix_mask,
               pix_mask_weight, img_mask, iSS)
    roi2 = ROI('roi2', 'test source', 'roi description2', pix_mask,
               pix_mask_weight, img_mask, iSS)
    roi_list = (roi1, roi2)

    oc = OpticalChannel('test_optical_channel', 'test_source', 'description',
                        'emission_lambda')
    ip = ImagingPlane('test_imaging_plane', 'test_source', oc, 'description',
                      'device', 'excitation_lambda', 'imaging_rate',
                      'indicator', 'location', (1, 2, 1, 2, 3), 4.0, 'unit',
                      'reference_frame')

    ps = PlaneSegmentation('name', 'test source', 'description', roi_list, ip,
                           iSS)
    return ps
Exemple #11
0
 def test_init(self):
     oc = OpticalChannel(name='test_optical_channel',
                         description='description',
                         emission_lambda=500.)
     self.assertEqual(oc.name, 'test_optical_channel')
     self.assertEqual(oc.description, 'description')
     self.assertEqual(oc.emission_lambda, 500.)
Exemple #12
0
def CreatePlaneSegmentation():
    w, h = 5, 5
    img_mask = [[[1.0 for x in range(w)] for y in range(h)],
                [[2.0 for x in range(w)] for y in range(h)]]
    pix_mask = [[1, 2, 1.0], [3, 4, 1.0], [5, 6, 1.0], [7, 8, 2.0],
                [9, 10, 2.0]]

    iSS = ImageSeries(name='test_iS',
                      source='a hypothetical source',
                      data=list(),
                      unit='unit',
                      external_file=['external_file'],
                      starting_frame=[1, 2, 3],
                      format='tiff',
                      timestamps=list())

    oc = OpticalChannel('test_optical_channel', 'test_source', 'description',
                        'emission_lambda')
    ip = ImagingPlane('test_imaging_plane', 'test_source', oc, 'description',
                      'device', 'excitation_lambda', 'imaging_rate',
                      'indicator', 'location', (1, 2, 1, 2, 3), 4.0, 'unit',
                      'reference_frame')

    pS = PlaneSegmentation('test source', 'description', ip, 'test_name', iSS)
    pS.add_roi(pix_mask[0:3], img_mask[0])
    pS.add_roi(pix_mask[3:5], img_mask[1])
    return pS
Exemple #13
0
 def addContainer(self, file):
     dev1 = file.create_device('imaging_device_1', 'dev1 description')
     oc = OpticalChannel('optchan1', 'a fake OpticalChannel', 3.14)
     ip = file.create_imaging_plane('imgpln1', oc, 'a fake ImagingPlane',
                                    dev1, 6.28, 2.718, 'GFP',
                                    'somewhere in the brain')
     return ip
Exemple #14
0
 def addContainer(self, file):
     oc = OpticalChannel('optchan1', 'unit test TestImagingPlaneIO',
                         'a fake OpticalChannel', '3.14')
     ip = ImagingPlane('imgpln1', 'unit test TestImagingPlaneIO', oc,
                       'a fake ImagingPlane', 'imaging_device_1', '6.28',
                       '2.718', 'GFP', 'somewhere in the brain')
     file.set_imaging_plane(ip)
     return ip
Exemple #15
0
 def make_imaging_plane(self):
     self.device = Device(name='dev1')
     self.optical_channel = OpticalChannel('optchan1',
                                           'a fake OpticalChannel', 500.)
     self.imaging_plane = ImagingPlane('imgpln1', self.optical_channel,
                                       'a fake ImagingPlane', self.device,
                                       600., 300., 'GFP',
                                       'somewhere in the brain')
Exemple #16
0
 def make_imaging_plane(self, source):
     self.optical_channel = OpticalChannel('optchan1', source,
                                           'a fake OpticalChannel', 500.)
     self.imaging_plane = ImagingPlane('imgpln1', source,
                                       self.optical_channel,
                                       'a fake ImagingPlane',
                                       'imaging_device_1', 600., '2.718',
                                       'GFP', 'somewhere in the brain')
Exemple #17
0
 def setUpContainer(self):
     self.optical_channel = OpticalChannel('optchan1',
                                           'unit test TestImagingPlaneIO',
                                           'a fake OpticalChannel', 500.)
     return ImagingPlane('imgpln1', 'unit test TestImagingPlaneIO',
                         self.optical_channel, 'a fake ImagingPlane',
                         'imaging_device_1', 600., '2.718', 'GFP',
                         'somewhere in the brain')
Exemple #18
0
    def test_add_fretseries(self):
        # Create and add device
        device = Device(name='Device')
        self.nwbfile.add_device(device)

        # Create optical channels
        opt_ch_d = OpticalChannel(name='optical_channel',
                                  description='optical_channel_description',
                                  emission_lambda=529.)
        opt_ch_a = OpticalChannel(name='optical_channel',
                                  description='optical_channel_description',
                                  emission_lambda=633.)

        # Create FRET
        fs_d = FRETSeries(name='donor',
                          fluorophore='mCitrine',
                          optical_channel=opt_ch_d,
                          device=device,
                          description='description of donor series',
                          data=np.random.randn(100, 10, 10),
                          rate=200.,
                          unit='no unit')
        fs_a = FRETSeries(name='acceptor',
                          fluorophore='mKate2',
                          optical_channel=opt_ch_a,
                          device=device,
                          description='description of acceptor series',
                          data=np.random.randn(100, 10, 10),
                          rate=200.,
                          unit='no unit')

        fret = FRET(name='FRET',
                    excitation_lambda=482.,
                    donor=fs_d,
                    acceptor=fs_a)
        self.nwbfile.add_acquisition(fret)

        filename = 'test_fret.nwb'

        with NWBHDF5IO(filename, 'w') as io:
            io.write(self.nwbfile)

        with NWBHDF5IO(filename, mode='r', load_namespaces=True) as io:
            io.read()

        os.remove(filename)
Exemple #19
0
 def test_args(self):
     oc = OpticalChannel('test_name', 'description', 500.)
     device = Device(name='device_name')
     ip = ImagingPlane('test_imaging_plane', oc, 'description', device, 600.,
                       300., 'indicator', 'location', (50, 100, 3), 4.0, 'unit', 'reference_frame')
     with self.assertRaises(ValueError):  # no data or external file
         TwoPhotonSeries('test_tPS', unit='unit', field_of_view=[2., 3.],
                         imaging_plane=ip, pmt_gain=1.0, scan_line_rate=2.0,
                         starting_frame=[1, 2, 3], format='tiff', timestamps=[1., 2.])
Exemple #20
0
    def getBoilerPlateObjects(self):

        iSS = ImageSeries(name='test_iS', data=np.ones((2, 2, 2)), unit='unit',
                          external_file=['external_file'], starting_frame=[1, 2, 3], format='tiff', timestamps=list())

        device = Device(name='device_name')
        oc = OpticalChannel('test_optical_channel', 'description', 500.)
        ip = ImagingPlane('test_imaging_plane', oc, 'description', device, 600.,
                          300., 'indicator', 'location', (1, 2, 1, 2, 3), 4.0, 'unit', 'reference_frame')
        return iSS, ip
Exemple #21
0
    def test_conversion_deprecated(self):
        oc = OpticalChannel('test_name', 'description', 500.)
        self.assertEqual(oc.description, 'description')
        self.assertEqual(oc.emission_lambda, 500.)

        device = Device(name='device_name')

        msg = "The 'conversion' argument is deprecated in favor of 'origin_coords' and 'grid_spacing'."
        with self.assertWarnsWith(DeprecationWarning, msg):
            ImagingPlane('test_imaging_plane', oc, 'description', device, 600., 300., 'indicator', 'location',
                         None, 2.0)
Exemple #22
0
    def addContainer(self, file):
        dev1 = file.create_device('imaging_device_1', 'dev1 description')
        oc = OpticalChannel('optchan1', 'a fake OpticalChannel', 3.14)
        ip = file.create_imaging_plane('imgpln1',
                                       oc,
                                       description='a fake ImagingPlane',
                                       device=dev1,
                                       excitation_lambda=6.28,
                                       indicator='GFP',
                                       location='somewhere in the brain',
                                       imaging_rate=2.718)

        return ip
    def create_optical_channel(self, metadata=None):

        input_kwargs = dict(name='OpticalChannel',
                            description='no description',
                            emission_lambda=np.nan)

        if metadata is None and 'Ophys' in self.metadata and 'OpticalChannel' in self.metadata[
                'Ophys']:
            metadata = self.metadata['Ophys']['OpticalChannel']

        input_kwargs.update(metadata)

        return OpticalChannel(**input_kwargs)
Exemple #24
0
 def setUpContainer(self):
     """ Return the test ImagingPlane to read/write """
     self.device = Device(name='dev1')
     self.optical_channel = OpticalChannel('optchan1',
                                           'a fake OpticalChannel', 500.)
     return ImagingPlane('imgpln1',
                         self.optical_channel,
                         'a fake ImagingPlane',
                         self.device,
                         600.,
                         300.,
                         'GFP',
                         'somewhere in the brain',
                         reference_frame='unknonwn')
Exemple #25
0
 def make_imaging_plane(self):
     """ Make an ImagingPlane and related objects """
     self.device = Device(name='dev1')
     self.optical_channel = OpticalChannel('optchan1',
                                           'a fake OpticalChannel', 500.)
     self.imaging_plane = ImagingPlane('imgpln1',
                                       self.optical_channel,
                                       'a fake ImagingPlane',
                                       self.device,
                                       600.,
                                       300.,
                                       'GFP',
                                       'somewhere in the brain',
                                       reference_frame='unknown')
Exemple #26
0
 def make_imaging_plane(self):
     """ Make an ImagingPlane and related objects """
     self.device = Device(name='dev1')
     self.optical_channel = OpticalChannel(
         name='optchan1',
         description='a fake OpticalChannel',
         emission_lambda=500.)
     self.imaging_plane = ImagingPlane(name='imgpln1',
                                       optical_channel=self.optical_channel,
                                       description='a fake ImagingPlane',
                                       device=self.device,
                                       excitation_lambda=600.,
                                       imaging_rate=300.,
                                       indicator='GFP',
                                       location='somewhere in the brain',
                                       reference_frame='unknown')
Exemple #27
0
 def setBoilerPlateObjects(self):
     ts = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
     self.image_series = ImageSeries(name='test_iS', dimension=[2],
                                     external_file=['images.tiff'],
                                     starting_frame=[1, 2, 3], format='tiff', timestamps=ts)
     self.device = Device(name='dev1')
     self.optical_channel = OpticalChannel('test_optical_channel',
                                           'optical channel description', 500.)
     self.imaging_plane = ImagingPlane('test_imaging_plane',
                                       self.optical_channel,
                                       'imaging plane description',
                                       self.device,
                                       600., 300., 'GFP', 'somewhere in the brain',
                                       (1, 2, 1, 2, 3), 4.0, 'manifold unit', 'A frame to refer to')
     return PlaneSegmentation('description', self.imaging_plane, 'test_plane_seg_name',
                              self.image_series)
Exemple #28
0
    def test_init(self):
        oc = OpticalChannel('test_name', 'test_source', 'description',
                            'emission_lambda')
        self.assertEqual(oc.description, 'description')
        self.assertEqual(oc.emission_lambda, 'emission_lambda')

        ip = ImagingPlane('test_imaging_plane', 'test source', oc,
                          'description', 'device', 'excitation_lambda',
                          'imaging_rate', 'indicator', 'location',
                          (1, 2, 1, 2, 3), 4.0, 'unit', 'reference_frame')
        self.assertEqual(ip.optical_channel[0], oc)
        self.assertEqual(ip.device, 'device')
        self.assertEqual(ip.excitation_lambda, 'excitation_lambda')
        self.assertEqual(ip.imaging_rate, 'imaging_rate')
        self.assertEqual(ip.indicator, 'indicator')
        self.assertEqual(ip.location, 'location')
        self.assertEqual(ip.manifold, (1, 2, 1, 2, 3))
        self.assertEqual(ip.conversion, 4.0)
        self.assertEqual(ip.unit, 'unit')
        self.assertEqual(ip.reference_frame, 'reference_frame')

        tPS = TwoPhotonSeries('test_tPS',
                              'a hypothetical source',
                              data=list(),
                              unit='unit',
                              field_of_view=list(),
                              imaging_plane=ip,
                              pmt_gain=1.0,
                              scan_line_rate=2.0,
                              external_file=['external_file'],
                              starting_frame=[1, 2, 3],
                              format='tiff',
                              timestamps=list())
        self.assertEqual(tPS.name, 'test_tPS')
        self.assertEqual(tPS.source, 'a hypothetical source')
        self.assertEqual(tPS.unit, 'unit')
        self.assertEqual(tPS.field_of_view, list())
        self.assertEqual(tPS.imaging_plane, ip)
        self.assertEqual(tPS.pmt_gain, 1.0)
        self.assertEqual(tPS.scan_line_rate, 2.0)
        self.assertEqual(tPS.external_file, ['external_file'])
        self.assertEqual(tPS.starting_frame, [1, 2, 3])
        self.assertEqual(tPS.format, 'tiff')
        self.assertEqual(tPS.dimension, [np.nan])
Exemple #29
0
    def add_two_photon_series(imaging, nwbfile, metadata, buffer_size=10):
        """
        Auxiliary static method for nwbextractor.
        Adds two photon series from imaging object as TwoPhotonSeries to nwbfile object.
        """
        metadata = dict_recursive_update(get_default_nwb_metadata(), metadata)
        metadata = update_dict(metadata,
                               NwbImagingExtractor.get_nwb_metadata(imaging))
        # Tests if ElectricalSeries already exists in acquisition
        nwb_es_names = [ac for ac in nwbfile.acquisition]
        opts = metadata["Ophys"]["TwoPhotonSeries"][0]
        if opts["name"] not in nwb_es_names:
            # retrieve device
            device = nwbfile.devices[list(nwbfile.devices.keys())[0]]
            metadata["Ophys"]["ImagingPlane"][0]["optical_channel"] = [
                OpticalChannel(**i) for i in metadata["Ophys"]["ImagingPlane"]
                [0]["optical_channel"]
            ]
            metadata["Ophys"]["ImagingPlane"][0] = update_dict(
                metadata["Ophys"]["ImagingPlane"][0], {"device": device})

            imaging_plane = nwbfile.create_imaging_plane(
                **metadata["Ophys"]["ImagingPlane"][0])

            def data_generator(imaging):
                for i in range(imaging.get_num_frames()):
                    yield imaging.get_frames(frame_idxs=[i]).T

            data = H5DataIO(
                DataChunkIterator(data_generator(imaging),
                                  buffer_size=buffer_size),
                compression=True,
            )

            # using internal data. this data will be stored inside the NWB file
            two_p_series_kwargs = update_dict(
                metadata["Ophys"]["TwoPhotonSeries"][0],
                dict(data=data, imaging_plane=imaging_plane),
            )
            ophys_ts = TwoPhotonSeries(**two_p_series_kwargs)

            nwbfile.add_acquisition(ophys_ts)

        return nwbfile
    def cicada_create_optical_channel(self):
        required_metadata = [
            "optical_channel_name", "optical_channel_description",
            "optical_channel_emission_lambda"
        ]

        metadata_to_add = list(
            set(required_metadata) -
            set(list(self.data['ophys_metadata'].keys())))
        for i in metadata_to_add:
            self.data["ophys_metadata"] = self.add_required_metadata(
                self.data["ophys_metadata"], i, "ophys")

        self.optical_channel = OpticalChannel(
            name=self.data['ophys_metadata'].get("optical_channel_name"),
            description=self.data['ophys_metadata'].get(
                "optical_channel_description"),
            emission_lambda=self.data['ophys_metadata'].get(
                "optical_channel_emission_lambda"))