def test_tracks_controls_color_by(qtbot): """Check updating of the color_by combobox.""" inital_color_by = 'time' with pytest.warns(UserWarning) as wrn: layer = Tracks( _TRACKS, properties=_PROPERTIES, color_by=inital_color_by ) assert "Previous color_by key 'time' not present" in str(wrn[0].message) qtctrl = QtTracksControls(layer) qtbot.addWidget(qtctrl) # verify the color_by argument is initialized correctly assert layer.color_by == inital_color_by assert qtctrl.color_by_combobox.currentText() == inital_color_by # update color_by from the layer model layer_update_color_by = 'speed' layer.color_by = layer_update_color_by assert layer.color_by == layer_update_color_by assert qtctrl.color_by_combobox.currentText() == layer_update_color_by # update color_by from the qt controls qt_update_color_by = 'track_id' speed_index = qtctrl.color_by_combobox.findText( qt_update_color_by, Qt.MatchFixedString ) qtctrl.color_by_combobox.setCurrentIndex(speed_index) assert layer.color_by == qt_update_color_by assert qtctrl.color_by_combobox.currentText() == qt_update_color_by
def test_track_layer_properties_changed_colorby(): """Test behaviour when changes to properties invalidate current color_by""" properties_dict_1 = {'time': np.arange(100), 'prop1': np.arange(100)} properties_dict_2 = {'time': np.arange(100), 'prop2': np.arange(100)} data = np.zeros((100, 4)) data[:, 1] = np.arange(100) layer = Tracks(data, properties=properties_dict_1, color_by='prop1') # test warning is raised with pytest.warns(UserWarning): layer.properties = properties_dict_2 # test default fallback assert layer.color_by == 'track_id'
def test_track_layer_reset_data(): """Test changing data once layer is instantiated.""" data = np.zeros((100, 4)) data[:, 1] = np.arange(100) data[50:, 0] = 1 properties = {'time': data[:, 1]} graph = {1: [0]} layer = Tracks(data, graph=graph, properties=properties) cropped_data = data[:10, :] layer.data = cropped_data assert np.all(layer.data == cropped_data) assert layer.graph == {}
def test_track_layer_properties(): """Test properties.""" data = np.zeros((100, 4)) data[:, 1] = np.arange(100) properties = {'time': data[:, 1]} layer = Tracks(data, properties=properties) assert layer.properties == properties
def test_track_layer_properties(properties): """Test properties.""" data = np.zeros((100, 4)) data[:, 1] = np.arange(100) layer = Tracks(data, properties=properties) for k, v in properties.items(): np.testing.assert_equal(layer.properties[k], v)
def test_track_layer_graph(): """Test track layer graph.""" data = np.zeros((100, 4)) data[:, 1] = np.arange(100) data[50:, 0] = 1 graph = {1: [0]} layer = Tracks(data, graph=graph) assert layer.graph == graph
def test_tracks_float_time_index(): """Test Tracks layer instantiation with floating point time values""" coords = np.random.normal(loc=50, size=(100, 2)) time = np.random.normal(loc=50, size=(100, 1)) track_id = np.zeros((100, 1)) track_id[50:] = 1 data = np.concatenate((track_id, time, coords), axis=1) Tracks(data)
def test_malformed_graph(): """Test for malformed graph.""" data = np.zeros((100, 4)) data[:, 1] = np.arange(100) data[50:, 0] = 1 graph = {1: [0], 2: [33]} with pytest.raises(ValueError): Tracks(data, graph=graph)
def test_track_layer_data_flipped(): """Test data flipped.""" data = np.zeros((100, 4)) data[:, 1] = np.arange(100) data[:, 0] = np.arange(100) data = np.flip(data, axis=0) layer = Tracks(data) assert np.all(layer.data == np.flip(data, axis=0))
def test_get_root(): """Test getting the graph root.""" data = np.random.random(size=(max(TEST_GRAPH_LINEAR) + 1, 4)) data[:, 0] = np.arange(data.shape[0]) data[:, 1] = np.arange(data.shape[0]) tracks = Tracks(data, graph=TEST_GRAPH) root_id = graph.get_root_id(tracks, TEST_GRAPH_ROOT) assert root_id == TEST_GRAPH_ROOT
def test_track_layer_colorby_nonexistent(): """Test error handling for non-existent properties with color_by""" data = np.zeros((100, 4)) data[:, 1] = np.arange(100) non_existant_property = 'not_a_valid_key' assert non_existant_property not in properties_dict.keys() with pytest.raises(ValueError): Tracks(data, properties=properties_dict, color_by=non_existant_property)
def show_tree(tracks: Tracks, event: Event) -> None: self.tracks = tracks cursor_position = event.position track_id = tracks.get_value(cursor_position, world=True) if track_id is not None: # Setting this property automatically triggers re-drawing of the # tree and property graph self.track_id = track_id self.draw_current_time_line()
def test_2dt_tracks(): """Test instantiating Tracks layer with 2D+t data.""" data, properties = tracks_2d() layer = Tracks(data, properties=properties) assert layer.ndim == 3 assert np.all([np.all(ld == d) for ld, d in zip(layer.data, data)]) assert np.all( [np.all(lp == p) for lp, p in zip(layer.properties, properties)] )
def test_build_subgraph(): """Test building the subgraph using a `napari.layers.Tracks` layer as input.""" data = np.random.random(size=(max(TEST_GRAPH_LINEAR) + 1, 4)) data[:, 0] = np.arange(data.shape[0]) data[:, 1] = np.arange(data.shape[0]) tracks = Tracks(data, graph=TEST_GRAPH) nodes = graph.build_subgraph(tracks, TEST_GRAPH_ROOT) subgraph = [node.ID for node in nodes] assert subgraph == TEST_GRAPH_LINEAR
def test_tracks_graph_cleanup(): """ Test if graph data can be cleaned up without any issue. There was problems with the shader buffer once, see issue #4155. """ tracks_data = [ [1, 0, 236, 0], [1, 1, 236, 100], [1, 2, 236, 200], [2, 3, 436, 500], [2, 4, 436, 1000], [3, 3, 636, 500], [3, 4, 636, 1000], ] graph = {1: [], 2: [1], 3: [1]} layer = Tracks(tracks_data, graph=graph) visual = VispyTracksLayer(layer) layer.graph = {} assert visual.node._subvisuals[2]._pos is None assert visual.node._subvisuals[2]._connect is None
def test_tracks_length_change(): """Test changing length properties of tracks""" track_length = 1000 data = np.zeros((track_length, 4)) layer = Tracks(data) layer.tail_length = track_length assert layer.tail_length == track_length assert layer._max_length == track_length layer = Tracks(data) layer.head_length = track_length assert layer.head_length == track_length assert layer._max_length == track_length
def test_malformed_id(): """Test for malformed track ID.""" data = np.random.random((100, 4)) data[:, 1] = np.arange(100) with pytest.raises(ValueError): Tracks(data)
def test_track_layer_data_nonzero_starting_time(timestamps): """Test data with sparse timestamps or not starting at zero.""" data = np.zeros((100, 4)) data[:, 1] = timestamps layer = Tracks(data) assert np.all(layer.data == data)
def test_track_layer_data(): """Test data.""" data = np.zeros((100, 4)) data[:, 1] = np.arange(100) layer = Tracks(data) assert np.all(layer.data == data)
def test_track_layer_name(): """Test track name.""" data = np.zeros((1, 4)) layer = Tracks(data, name='test_tracks') assert layer.name == 'test_tracks'
def test_tracks_layer_3dt_ndim(data): """Test instantiating Tracks layer, check 3D+t dimensionality.""" layer = Tracks(data) assert layer.ndim == 4
def test_malformed_timestamps(): """Test for malformed track timestamps.""" data = np.random.random((100, 4)) data[:, 0] = 0 with pytest.raises(ValueError): Tracks(data)
def test_tracks_layer_3dt_ndim(): """Test instantiating Tracks layer, check 3D+t dimensionality.""" data = [np.zeros((1, 4))] layer = Tracks(data) assert layer.ndim == 4