コード例 #1
0
ファイル: test_cell.py プロジェクト: RackiLab/ColiCoords
    def setUp(self):
        f_path = os.path.dirname(os.path.realpath(__file__))
        self.cell_list = load(
            os.path.join(f_path, 'test_data', 'test_synth_cell_storm.hdf5'))
        self.cell = self.cell_list[0]

        data = Data()
        data.add_data(self.cell.data.binary_img, 'binary')
        self.empty_cell = Cell(data)
コード例 #2
0
    def setUp(self):
        f_path = os.path.dirname(os.path.realpath(__file__))
        self.cell_list = load(
            os.path.join(f_path, 'test_data', 'test_synth_cell_storm.hdf5'))
        self.cell = self.cell_list[0]

        x = np.arange(20)
        y = np.exp(-x / 5)

        img_3d = self.cell.data.data_dict['fluorescence'][
            np.newaxis, :, :] * y[:, np.newaxis, np.newaxis]
        self.cell.data.add_data(img_3d, 'fluorescence', 'flu_3d')

        data = Data()
        data.add_data(self.cell.data.binary_img, 'binary')
        self.empty_cell = Cell(data)
コード例 #3
0
def _load_cell(cell_grp):
    """Load the cell object from `cell_grp`"""
    data_obj = Data()
    data_grp = cell_grp['data']

    for key in list(data_grp.keys()):
        grp = data_grp[key]
        data_arr = grp[key]
        dclass = grp.attrs.get('dclass').decode('UTF-8')
        data_obj.add_data(data_arr[:], dclass=dclass, name=key)

    c = Cell(data_obj, init_coords=False)

    attr_grp = cell_grp['attributes']
    attr_dict = dict(attr_grp.attrs.items())

    for a in ['r', 'xl', 'xr', 'coeff']:
        setattr(c.coords, a, attr_dict.get(a))
    c.coords.shape = c.data.shape

    name = attr_dict.get('name').decode('UTF-8')
    c.name = name if name is not 'None' else None

    return c
コード例 #4
0
def _load_deprecated(file_path):
    ext = os.path.splitext(file_path)[1]
    if ext == '.cc':
        with h5py.File(file_path, 'r') as f:

            data_obj = Data()
            data_grp = f['data']
            for key in list(data_grp.keys()):
                grp = data_grp[key]
                data_arr = grp[key]
                dclass = grp.attrs.get('dclass').decode('UTF-8')
                data_obj.add_data(data_arr, dclass=dclass, name=key)

            c = Cell(data_obj)

            attr_grp = f['attributes']
            attr_dict = dict(attr_grp.attrs.items())
            for a in ['r', 'xl', 'xr', 'coeff']:
                setattr(c.coords, a, attr_dict.get(a))
            c.name = attr_dict.get('label')

        return c
    else:
        raise ValueError('Invalid file type')
コード例 #5
0
    def test_rotation_storm(self):
        for cell in self.storm_cells_1:
            for th in np.arange(90, 370, 90):
                data_r = cell.data.copy().rotate(th)
                flu = data_r.data_dict['fluorescence']
                storm = data_r.data_dict['storm']
                x, y = storm['x'], storm['y']

                nc = Cell(data_r, init_coords=False)
                nc.coords.shape = data_r.shape
                x_fl = np.sum(nc.coords.x_coords * flu) / np.sum(flu)
                y_fl = np.sum(nc.coords.y_coords * flu) / np.sum(flu)

                self.assertAlmostEqual(x[0], np.array(x_fl), 2)
                self.assertAlmostEqual(y[0], np.array(y_fl), 2)
コード例 #6
0
    def setUp(self):
        self.data = load_testdata('ds1')
        f_path = os.path.dirname(os.path.realpath(__file__))
        self.storm_cells_1 = load(
            os.path.join(f_path, 'test_data/test_single_spot_storm.hdf5'))
        self.storm_cells_2 = load(
            os.path.join(f_path, 'test_data/test_double_spot_storm.hdf5'))

        cells_no_flu = []
        for c in self.storm_cells_2:
            d = Data()
            d.add_data(c.data.binary_img, 'binary')
            d.add_data(c.data.data_dict['storm_1'], 'storm', 'storm_1')
            d.add_data(c.data.data_dict['storm_2'], 'storm', 'storm_2')
            cell = Cell(d)
            cells_no_flu.append(cell)

        self.storm_cells_2_no_flu = CellList(cells_no_flu)
コード例 #7
0
class TestCellListSTORM(ArrayTestCase):
    def setUp(self):
        f_path = os.path.dirname(os.path.realpath(__file__))
        self.cell_list = load(
            os.path.join(f_path, 'test_data', 'test_synth_cell_storm.hdf5'))
        self.cell = self.cell_list[0]

        x = np.arange(20)
        y = np.exp(-x / 5)

        img_3d = self.cell.data.data_dict['fluorescence'][
            np.newaxis, :, :] * y[:, np.newaxis, np.newaxis]
        self.cell.data.add_data(img_3d, 'fluorescence', 'flu_3d')

        data = Data()
        data.add_data(self.cell.data.binary_img, 'binary')
        self.empty_cell = Cell(data)

    def test_l_dist(self):
        nbins = 50
        with self.assertRaises(IndexError):
            x, y = self.empty_cell.l_dist(nbins)
        with self.assertRaises(
                ValueError):  # todo refactor to stay as KeyError?
            x, y = self.cell.l_dist(nbins, data_name='notexisting')
        with self.assertRaises(ValueError):
            x, y = self.cell.l_dist(nbins, method='notexisting')

        storm_int_sum = np.sum(self.cell.data.data_dict['storm']['intensity'])

        x, y = self.cell.l_dist(nbins, data_name='storm', r_max=20)
        self.assertEqual(np.sum(y), storm_int_sum)

        x, y = self.cell.l_dist(nbins,
                                data_name='storm',
                                method='box',
                                r_max=20)
        self.assertEqual(np.sum(y), storm_int_sum)

        x, y = self.cell.l_dist(nbins,
                                data_name='storm',
                                method='box',
                                storm_weight=True,
                                r_max=20)
        self.assertEqual(np.sum(y), storm_int_sum)

        x, y = self.cell.l_dist(nbins, data_name='fluorescence')

        x, y = self.cell.l_dist(nbins, data_name='fluorescence', method='box')

        x, y = self.cell.l_dist(
            nbins,
            method='box',
        )
        x, y = self.cell.l_dist(nbins,
                                method='box',
                                l_mean=0.75 * self.cell.length,
                                norm_x=True)
        x, y = self.cell.l_dist(nbins, method='box', norm_x=True)
        x, y = self.cell.l_dist(nbins, method='box', r_max=np.inf)
        x, y = self.cell.l_dist(nbins, method='box', r_max=1)
        x, y = self.cell.l_dist(nbins, method='box', r_max=0)

        x, y_list = self.cell.l_dist(nbins, data_name='flu_3d')
        self.assertEqual(len(y_list), 20)

    def test_l_classify(self):
        p, b, m = self.cell.l_classify(data_name='storm')
        total = len(self.cell.data.data_dict['storm'])
        self.assertEqual(p + b + m, total)

        p, b, m = self.cell.l_classify()
        total = len(self.cell.data.data_dict['storm'])
        self.assertEqual(p + b + m, total)

    def test_r_dist(self):
        stop = 15
        step = 0.5
        with self.assertRaises(IndexError):
            x, y = self.empty_cell.r_dist(stop, step)
        with self.assertRaises(
                ValueError):  # todo refactor to stay as KeyError?
            x, y = self.cell.r_dist(stop, step, data_name='notexisting')
        with self.assertRaises(ValueError):
            x, y = self.cell.r_dist(stop, step, method='notexisting')

        stop = 15
        step = 0.5
        bins_box = np.arange(0, stop + step, step) + 0.5 * step
        bins = np.arange(0, stop + step, step)
        storm_int_sum = np.sum(self.cell.data.data_dict['storm']['intensity'])

        x, y = self.cell.r_dist(data_name='storm', stop=stop, step=step)
        self.assertArrayEqual(bins_box, x)
        self.assertEqual(np.sum(y), storm_int_sum)

        x, y = self.cell.r_dist(data_name='storm',
                                stop=stop,
                                step=step,
                                storm_weight=True)
        self.assertArrayEqual(bins_box, x)
        self.assertEqual(np.sum(y), storm_int_sum)

        x, y = self.cell.r_dist(stop, step, data_name='fluorescence')
        self.assertArrayEqual(bins, x)

        x, y = self.cell.r_dist(stop,
                                step,
                                data_name='fluorescence',
                                limit_l='full')
        x, y = self.cell.r_dist(stop,
                                step,
                                data_name='fluorescence',
                                limit_l='poles')

        with self.assertRaises(AssertionError):
            x, y = self.cell.r_dist(stop,
                                    step,
                                    data_name='fluorescence',
                                    limit_l=0)
        with self.assertRaises(AssertionError):
            x, y = self.cell.r_dist(stop,
                                    step,
                                    data_name='fluorescence',
                                    limit_l=1)

        x, y = self.cell.r_dist(stop,
                                step,
                                data_name='fluorescence',
                                limit_l=0.5)
        self.assertArrayEqual(bins, x)
        x, y = self.cell.r_dist(stop,
                                step,
                                data_name='fluorescence',
                                limit_l=1e-3)
        self.assertArrayEqual(bins, x)
        x, y = self.cell.r_dist(stop,
                                step,
                                data_name='fluorescence',
                                limit_l=1e-10)
        self.assertArrayEqual(bins, x)
        x, y = self.cell.r_dist(stop,
                                step,
                                data_name='fluorescence',
                                limit_l=1 - 1e-10)
        self.assertArrayEqual(bins, x)

    def test_phi_dist(self):
        step = 0.5
        with self.assertRaises(IndexError):
            x, yl, yr = self.empty_cell.phi_dist(step)
        with self.assertRaises(
                ValueError):  # todo refactor to stay as KeyError?
            x, yl, yr = self.cell.phi_dist(step, data_name='notexisting')
        with self.assertRaises(ValueError):
            x, yl, yr = self.cell.phi_dist(step, method='notexisting')

        stop = 180
        bins_box = np.arange(0, stop + step, step) + 0.5 * step
        bins = np.arange(0, stop + step, step)

        x, y = self.cell.data.data_dict['storm'][
            'x'], self.cell.data.data_dict['storm']['y']
        lc, rc, psi = self.cell.coords.transform(x, y)

        b = np.logical_and(psi != 0, psi != 180)  # only localizations at poles
        storm_int_sum = np.sum(
            self.cell.data.data_dict['storm']['intensity'][b])

        x, yl, yr = self.cell.phi_dist(step, data_name='storm', r_max=np.inf)
        self.assertArrayEqual(bins_box, x)
        self.assertEqual(np.sum(yl + yr), storm_int_sum)

        x, yl, yr = self.cell.phi_dist(step,
                                       data_name='storm',
                                       storm_weight=True,
                                       r_max=np.inf)
        self.assertArrayEqual(bins_box, x)
        self.assertEqual(np.sum(yl + yr), storm_int_sum)

        x, yl, yr = self.cell.phi_dist(step, data_name='storm', r_max=0)
        self.assertEqual(np.sum(yl + yr), 0)

        x, yl, yr = self.cell.phi_dist(step, data_name='fluorescence')
        self.assertArrayEqual(bins, x)

        x, yl, yr = self.cell.phi_dist(step,
                                       data_name='fluorescence',
                                       r_min=-5)
        x, yl, yr = self.cell.phi_dist(step, data_name='fluorescence', r_max=0)
        x, yl, yr = self.cell.phi_dist(step,
                                       data_name='fluorescence',
                                       r_max=0,
                                       r_min=5)
        self.assertEqual(np.sum(yl + yr), 0)
コード例 #8
0
ファイル: test_cell.py プロジェクト: RackiLab/ColiCoords
class TestCellListSTORM(ArrayTestCase):
    def setUp(self):
        f_path = os.path.dirname(os.path.realpath(__file__))
        self.cell_list = load(
            os.path.join(f_path, 'test_data', 'test_synth_cell_storm.hdf5'))
        self.cell = self.cell_list[0]

        data = Data()
        data.add_data(self.cell.data.binary_img, 'binary')
        self.empty_cell = Cell(data)

    def test_l_dist(self):
        nbins = 50
        with self.assertRaises(IndexError):
            x, y = self.empty_cell.l_dist(nbins)
        with self.assertRaises(
                ValueError):  # todo refactor to stay as KeyError?
            x, y = self.cell.l_dist(nbins, data_name='notexisting')
        with self.assertRaises(ValueError):
            x, y = self.cell.l_dist(nbins, method='notexisting')

        storm_int_sum = np.sum(self.cell.data.data_dict['storm']['intensity'])

        x, y = self.cell.l_dist(nbins, data_name='storm', r_max=20)
        self.assertEqual(np.sum(y), storm_int_sum)

        x, y = self.cell.l_dist(nbins,
                                data_name='storm',
                                method='box',
                                r_max=20)
        self.assertEqual(np.sum(y), storm_int_sum)

        x, y = self.cell.l_dist(nbins,
                                data_name='storm',
                                method='box',
                                storm_weight=True,
                                r_max=20)
        self.assertEqual(np.sum(y), storm_int_sum)

        x, y = self.cell.l_dist(nbins, data_name='fluorescence')

        x, y = self.cell.l_dist(nbins, data_name='fluorescence', method='box')

        x, y = self.cell.l_dist(
            nbins,
            method='box',
        )
        x, y = self.cell.l_dist(nbins,
                                method='box',
                                l_mean=0.75 * self.cell.length,
                                norm_x=True)
        x, y = self.cell.l_dist(nbins, method='box', norm_x=True)
        x, y = self.cell.l_dist(nbins, method='box', r_max=np.inf)
        x, y = self.cell.l_dist(nbins, method='box', r_max=1)
        x, y = self.cell.l_dist(nbins, method='box', r_max=0)

    def test_l_classify(self):
        pass

    def test_r_dist(self):
        stop = 15
        step = 0.5
        with self.assertRaises(IndexError):
            x, y = self.empty_cell.r_dist(stop, step)
        with self.assertRaises(
                ValueError):  # todo refactor to stay as KeyError?
            x, y = self.cell.r_dist(stop, step, data_name='notexisting')
        with self.assertRaises(ValueError):
            x, y = self.cell.r_dist(stop, step, method='notexisting')

        stop = 15
        step = 0.5
        bins_box = np.arange(0, stop + step, step) + 0.5 * step
        bins = np.arange(0, stop + step, step)
        storm_int_sum = np.sum(self.cell.data.data_dict['storm']['intensity'])

        x, y = self.cell.r_dist(data_name='storm', stop=stop, step=step)
        self.assertArrayEqual(bins_box, x)
        self.assertEqual(np.sum(y), storm_int_sum)

        x, y = self.cell.r_dist(data_name='storm',
                                stop=stop,
                                step=step,
                                storm_weight=True)
        self.assertArrayEqual(bins_box, x)
        self.assertEqual(np.sum(y), storm_int_sum)

        x, y = self.cell.r_dist(stop, step, data_name='fluorescence')
        self.assertArrayEqual(bins, x)

        x, y = self.cell.r_dist(stop,
                                step,
                                data_name='fluorescence',
                                limit_l='full')
        x, y = self.cell.r_dist(stop,
                                step,
                                data_name='fluorescence',
                                limit_l='poles')

        with self.assertRaises(AssertionError):
            x, y = self.cell.r_dist(stop,
                                    step,
                                    data_name='fluorescence',
                                    limit_l=0)
        with self.assertRaises(AssertionError):
            x, y = self.cell.r_dist(stop,
                                    step,
                                    data_name='fluorescence',
                                    limit_l=1)

        x, y = self.cell.r_dist(stop,
                                step,
                                data_name='fluorescence',
                                limit_l=0.5)
        self.assertArrayEqual(bins, x)
        x, y = self.cell.r_dist(stop,
                                step,
                                data_name='fluorescence',
                                limit_l=1e-3)
        self.assertArrayEqual(bins, x)
        x, y = self.cell.r_dist(stop,
                                step,
                                data_name='fluorescence',
                                limit_l=1e-10)
        self.assertArrayEqual(bins, x)
        x, y = self.cell.r_dist(stop,
                                step,
                                data_name='fluorescence',
                                limit_l=1 - 1e-10)
        self.assertArrayEqual(bins, x)