예제 #1
0
    def test_simple(self):
        length = 128
        frames = 3
        x, y = _get_dummy_tracking_data(length, frames=frames)
        num_objects = len(np.unique(y)) - 1
        model = DummyModel()

        _ = tracking.CellTracker(x, y, model=model)

        # test data with bad rank
        with pytest.raises(ValueError):
            tracking.CellTracker(np.random.random((32, 32, 1)),
                                 np.random.randint(num_objects,
                                                   size=(32, 32, 1)),
                                 model=model)

        # test mismatched x and y shape
        with pytest.raises(ValueError):
            tracking.CellTracker(np.random.random((3, 32, 32, 1)),
                                 np.random.randint(num_objects,
                                                   size=(2, 32, 32, 1)),
                                 model=model)

        # test bad features
        with pytest.raises(ValueError):
            tracking.CellTracker(x, y, model=model, features=None)

        # test bad data_format
        with pytest.raises(ValueError):
            tracking.CellTracker(x, y, model=model, data_format='invalid')
예제 #2
0
    def test__sub_area(self):
        length = 128
        frames = 3
        model = DummyModel()

        # TODO: Fix for channels_first
        for data_format in ('channels_last', ):  # 'channels_first'):
            x, y = _get_dummy_tracking_data(length,
                                            frames=frames,
                                            data_format=data_format)

            tracker = tracking.CellTracker(x,
                                           y,
                                           model=model,
                                           data_format=data_format)

            for f in range(frames):
                if data_format == 'channels_first':
                    xf = x[:, f]
                    yf = y[:, f]
                else:
                    xf = x[f]
                    yf = y[f]

                    sub = tracker._sub_area(xf, yf, 1)
                    expected_shape = tracker.get_feature_shape('neighborhood')
                    assert sub.shape == expected_shape
    def test_simple(self):
        data_format = 'channels_last'
        frames = 3
        labels_per_frame = 5
        y = get_annotated_movie(img_size=256,
                                labels_per_frame=labels_per_frame,
                                frames=frames,
                                mov_type='sequential',
                                seed=0,
                                data_format=data_format)
        x = np.random.random(y.shape)
        num_objects = len(np.unique(y)) - 1
        model = DummyModel()
        encoder = DummyEncoder()

        _ = tracking.CellTracker(x,
                                 y,
                                 tracking_model=model,
                                 neighborhood_encoder=encoder)

        # test data with bad rank
        with pytest.raises(ValueError):
            tracking.CellTracker(np.random.random((32, 32, 1)),
                                 np.random.randint(num_objects,
                                                   size=(32, 32, 1)),
                                 tracking_model=model,
                                 neighborhood_encoder=encoder)

        # test mismatched x and y shape
        with pytest.raises(ValueError):
            tracking.CellTracker(np.random.random((3, 32, 32, 1)),
                                 np.random.randint(num_objects,
                                                   size=(2, 32, 32, 1)),
                                 tracking_model=model,
                                 neighborhood_encoder=encoder)

        # test bad data_format
        with pytest.raises(ValueError):
            tracking.CellTracker(x,
                                 y,
                                 tracking_model=model,
                                 neighborhood_encoder=encoder,
                                 data_format='invalid')
예제 #4
0
    def test_get_feature_shape(self):
        length = 128
        frames = 3
        x, y = _get_dummy_tracking_data(length, frames=frames)
        model = DummyModel()

        for data_format in ('channels_first', 'channels_last'):
            tracker = tracking.CellTracker(x,
                                           y,
                                           model=model,
                                           data_format=data_format)

            for f in tracker.features:
                shape = tracker.get_feature_shape(f)
                assert isinstance(shape, tuple)

            with pytest.raises(ValueError):
                tracker.get_feature_shape('bad feature name')
예제 #5
0
    def test_fetch_tracked_features(self):
        length = 128
        frames = 5

        # TODO: Fix for channels_first
        for data_format in ('channels_last', ):  # 'channels_first'):

            x, y = _get_dummy_tracking_data(length,
                                            frames=frames,
                                            data_format=data_format)

            for track_length in (1, frames // 2 + 1, frames + 1):
                tracker = tracking.CellTracker(x,
                                               y,
                                               model=DummyModel(),
                                               track_length=track_length,
                                               data_format=data_format)

                tracker._initialize_tracks()

                axis = tracker.channel_axis

                tracked_features = tracker.fetch_tracked_features()

                for feature_name in tracker.features:
                    feature_shape = tracker.get_feature_shape(feature_name)
                    feature = tracked_features[feature_name]

                    axis = tracker.channel_axis
                    assert feature.shape[axis] == feature_shape[axis]
                    assert feature.shape[0] == len(tracker.tracks)
                    assert feature.shape[tracker.time_axis + 1] == track_length

                tracked_features = tracker.fetch_tracked_features(
                    before_frame=frames // 2 + 1)

                for feature_name in tracker.features:
                    feature_shape = tracker.get_feature_shape(feature_name)
                    feature = tracked_features[feature_name]

                    axis = tracker.channel_axis
                    assert feature.shape[axis] == feature_shape[axis]
                    assert feature.shape[0] == len(tracker.tracks)
                    assert feature.shape[tracker.time_axis + 1] == track_length
    def test_track_cells(self):
        frames = 10
        track_length = 3
        labels_per_frame = 3

        # TODO: test detected divisions
        # TODO: test creating new track

        # TODO: Fix for channels_first
        for data_format in ('channels_last', ):  # 'channels_first'):

            y1 = get_annotated_movie(img_size=256,
                                     labels_per_frame=labels_per_frame,
                                     frames=frames,
                                     mov_type='sequential',
                                     seed=1,
                                     data_format=data_format)
            y2 = get_annotated_movie(img_size=256,
                                     labels_per_frame=labels_per_frame * 2,
                                     frames=frames,
                                     mov_type='sequential',
                                     seed=2,
                                     data_format=data_format)
            y3 = get_annotated_movie(img_size=256,
                                     labels_per_frame=labels_per_frame,
                                     frames=frames,
                                     mov_type='sequential',
                                     seed=3,
                                     data_format=data_format)

            y = np.concatenate((y1, y2, y3))

            x = np.random.random(y.shape)

            tracker = tracking.CellTracker(x,
                                           y,
                                           tracking_model=DummyModel(),
                                           neighborhood_encoder=DummyEncoder(),
                                           track_length=track_length,
                                           data_format=data_format)

            tracker.track_cells()

            # test tracker.dataframe
            df = tracker.dataframe(cell_type='test-value')
            assert isinstance(df, pd.DataFrame)
            assert 'cell_type' in df.columns  # pylint: disable=E1135

            # test incorrect values in tracker.dataframe
            with pytest.raises(ValueError):
                tracker.dataframe(bad_value=-1)

            try:
                # test tracker.postprocess
                tempdir = tempfile.mkdtemp()  # create dir
                path = os.path.join(tempdir, 'postprocess.xyz')
                tracker.postprocess(filename=path)
                post_saved_path = os.path.join(tempdir, 'postprocess.trk')
                assert os.path.isfile(post_saved_path)

                # test tracker.dump
                path = os.path.join(tempdir, 'test.xyz')
                tracker.dump(path)
                dump_saved_path = os.path.join(tempdir, 'test.trk')
                assert os.path.isfile(dump_saved_path)

                # utility tests for loading trk files
                # TODO: move utility tests into utils_test.py

                # test trk_folder_to_trks
                utils.trk_folder_to_trks(tempdir,
                                         os.path.join(tempdir, 'all.trks'))
                assert os.path.isfile(os.path.join(tempdir, 'all.trks'))

                # test load_trks
                data = utils.load_trks(post_saved_path)
                assert isinstance(data['lineages'], list)
                assert all(isinstance(d, dict) for d in data['lineages'])
                np.testing.assert_equal(data['X'], tracker.X)
                np.testing.assert_equal(data['y'], tracker.y_tracked)
                # load trks instead of trk
                data = utils.load_trks(os.path.join(tempdir, 'all.trks'))

                # test trks_stats
                utils.trks_stats(os.path.join(tempdir, 'test.trk'))
            finally:
                try:
                    shutil.rmtree(tempdir)  # delete directory
                except OSError as exc:
                    if exc.errno != errno.ENOENT:  # no such file or directory
                        raise  # re-raise exception
예제 #7
0
    def test_track_cells(self):
        length = 128
        frames = 5
        track_length = 2

        features = ['appearance', 'neighborhood', 'regionprop', 'distance']

        # TODO: Fix for channels_first
        for data_format in ('channels_last', ):  # 'channels_first'):

            x, y = _get_dummy_tracking_data(length,
                                            frames=frames,
                                            data_format=data_format)

            tracker = tracking.CellTracker(x,
                                           y,
                                           model=DummyModel(),
                                           track_length=track_length,
                                           data_format=data_format,
                                           features=features)

            tracker.track_cells()

            # test tracker.dataframe
            df = tracker.dataframe(cell_type='test-value')
            assert isinstance(df, pd.DataFrame)
            assert 'cell_type' in df.columns  # pylint: disable=E1135

            # test incorrect values in tracker.dataframe
            with pytest.raises(ValueError):
                tracker.dataframe(bad_value=-1)

            try:
                # test tracker.postprocess
                tempdir = tempfile.mkdtemp()  # create dir
                path = os.path.join(tempdir, 'postprocess.xyz')
                tracker.postprocess(filename=path)
                post_saved_path = os.path.join(tempdir, 'postprocess.trk')
                assert os.path.isfile(post_saved_path)

                # test tracker.dump
                path = os.path.join(tempdir, 'test.xyz')
                tracker.dump(path)
                dump_saved_path = os.path.join(tempdir, 'test.trk')
                assert os.path.isfile(dump_saved_path)

                # utility tests for loading trk files
                # TODO: move utility tests into utils_test.py

                # test trk_folder_to_trks
                utils.trk_folder_to_trks(tempdir,
                                         os.path.join(tempdir, 'all.trks'))
                assert os.path.isfile(os.path.join(tempdir, 'all.trks'))

                # test load_trks
                data = utils.load_trks(post_saved_path)
                assert isinstance(data['lineages'], list)
                assert all(isinstance(d, dict) for d in data['lineages'])
                np.testing.assert_equal(data['X'], tracker.x)
                np.testing.assert_equal(data['y'], tracker.y_tracked)
                # load trks instead of trk
                data = utils.load_trks(os.path.join(tempdir, 'all.trks'))

                # test trks_stats
                utils.trks_stats(os.path.join(tempdir, 'test.trk'))
            finally:
                try:
                    shutil.rmtree(tempdir)  # delete directory
                except OSError as exc:
                    if exc.errno != errno.ENOENT:  # no such file or directory
                        raise  # re-raise exception