Skip to content

Commit

Permalink
Fix particle layers clashing when created by different tools (#162)
Browse files Browse the repository at this point in the history
* separate surface generation by name to avoid some unnecessary overlapping

* fix test
  • Loading branch information
brisvag authored Jan 25, 2024
1 parent 0649ea6 commit 753bc79
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
16 changes: 11 additions & 5 deletions src/blik/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def get_reader(path):


def _construct_positions_layer(
coords, features, scale, exp_id, p_id, source, **pt_kwargs
coords, features, scale, exp_id, p_id, source, name_suffix, **pt_kwargs
):
feat_defaults = (
pd.DataFrame(features.iloc[-1].to_dict(), index=[0])
Expand All @@ -34,7 +34,7 @@ def _construct_positions_layer(
return (
coords,
{
"name": f"{exp_id} - particle positions",
"name": f"{exp_id} - {name_suffix} positions",
"features": features,
"feature_defaults": feat_defaults,
"face_color": "teal",
Expand All @@ -52,7 +52,9 @@ def _construct_positions_layer(
)


def _construct_orientations_layer(coords, features, scale, exp_id, p_id, source):
def _construct_orientations_layer(
coords, features, scale, exp_id, p_id, source, name_suffix
):
if coords is None:
vec_data = None
vec_color = "blue"
Expand All @@ -62,7 +64,7 @@ def _construct_orientations_layer(coords, features, scale, exp_id, p_id, source)
return (
vec_data,
{
"name": f"{exp_id} - particle orientations",
"name": f"{exp_id} - {name_suffix} orientations",
"edge_color": vec_color,
"length": 100 / np.array(scale),
"edge_width": 10 / np.array(scale),
Expand All @@ -83,6 +85,7 @@ def construct_particle_layer_tuples(
exp_id,
p_id=None,
source="",
name_suffix="",
**pt_kwargs,
):
"""
Expand All @@ -109,6 +112,7 @@ def construct_particle_layer_tuples(
exp_id=exp_id,
p_id=p_id,
source=source,
name_suffix=name_suffix,
**pt_kwargs,
)
ori = _construct_orientations_layer(
Expand All @@ -117,6 +121,7 @@ def construct_particle_layer_tuples(
scale=scale,
exp_id=exp_id,
p_id=p_id,
name_suffix=name_suffix,
source=source,
)

Expand All @@ -125,7 +130,7 @@ def construct_particle_layer_tuples(
return [pos, ori]


def read_particles(particles):
def read_particles(particles, name_suffix="particle"):
"""Takes a valid poseset and converts it into napari layers."""
coords = particles.position

Expand All @@ -152,6 +157,7 @@ def read_particles(particles):
scale=px_size,
exp_id=particles.experiment_id,
source=particles.source,
name_suffix=name_suffix,
)


Expand Down
6 changes: 5 additions & 1 deletion src/blik/widgets/main_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,11 @@ def new(
for lay in layers:
if lay.metadata["experiment_id"] == exp_id:
layers = construct_particle_layer_tuples(
coords=None, features=None, scale=lay.scale[0], exp_id=exp_id
coords=None,
features=None,
scale=lay.scale[0],
exp_id=exp_id,
name_suffix="picked",
)
return layer_tuples_to_layers(layers)
elif l_type == "surface_picking":
Expand Down
2 changes: 2 additions & 0 deletions src/blik/widgets/picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ def surface_particles(
scale=surface.scale[0],
exp_id=exp_id,
face_color_cycle=colors,
name_suffix="surface picked",
)


Expand Down Expand Up @@ -310,6 +311,7 @@ def filament_particles(
features=features,
scale=filament.scale[0],
exp_id=exp_id,
name_suffix="filament picked",
)


Expand Down
4 changes: 2 additions & 2 deletions tests/test_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def test_main_widget(make_napari_viewer):
# add new picking
wdg[1].l_type.value = "particles"
wdg[1]()
assert viewer.layers[-2].name == "test - particle positions"
assert viewer.layers[-1].name == "test - particle orientations"
assert viewer.layers[-2].name == "test - picked positions"
assert viewer.layers[-1].name == "test - picked orientations"

# add layer manually
lay = viewer.add_points(name="test_points")
Expand Down

0 comments on commit 753bc79

Please sign in to comment.