Skip to content

Commit

Permalink
fix all the recently broken xyz inversions
Browse files Browse the repository at this point in the history
  • Loading branch information
brisvag committed Dec 1, 2023
1 parent 28f8b5e commit 38a5c45
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
25 changes: 10 additions & 15 deletions src/blik/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def _construct_positions_layer(
)
feat_defaults["orientation"] = np.array(Rotation.identity(), dtype=object)
return (
coords,
invert_xyz(coords),
{
"name": f"{exp_id} - particle positions",
"features": features,
Expand All @@ -55,10 +55,8 @@ def _construct_orientations_layer(coords, features, scale, exp_id, p_id, source)
vec_data = None
vec_color = "blue"
else:
vec_data, vec_color = generate_vectors(
invert_xyz(coords), features["orientation"]
)
vec_data = invert_xyz(vec_data)
vec_data, vec_color = generate_vectors(coords, features["orientation"])
vec_data = invert_xyz(vec_data) # napari works in zyx order
return (
vec_data,
{
Expand Down Expand Up @@ -88,8 +86,7 @@ def construct_particle_layer_tuples(
"""
Constructs particle layer tuples from particle data.
Data is assumed to already by in zyx napari format, while features is
the normal poseset dataframe.
Data should be still in xyz format (will be flipped to zyx).
"""
# unique id so we can connect layers safely
p_id = p_id if p_id is not None else uuid1()
Expand Down Expand Up @@ -128,8 +125,7 @@ def construct_particle_layer_tuples(

def read_particles(particles):
"""Takes a valid poseset and converts it into napari layers."""
# order is zyx in napari
coords = invert_xyz(particles.position)
coords = particles.position

if particles.features is not None:
features = particles.features.copy(deep=False)
Expand All @@ -138,14 +134,13 @@ def read_particles(particles):

px_size = particles.pixel_spacing
if not px_size:
warnings.warn("unknown pixel spacing, setting to 1 Angstrom")
warnings.warn("unknown pixel spacing, setting to 1 Angstrom", stacklevel=2)
px_size = 1

if particles.shift is not None:
shifts = invert_xyz(particles.shift)
coords = coords + shifts
shift_cols = ["shift_z", "shift_y", "shift_x"]
features[shift_cols] = shifts
coords = coords + particles.shifts
shift_cols = ["shift_x", "shift_y", "shift_z"]
features[shift_cols] = particles.shifts
if particles.orientation is not None:
features["orientation"] = np.asarray(particles.orientation, dtype=object)

Expand All @@ -161,7 +156,7 @@ def read_particles(particles):
def read_image(image):
px_size = image.pixel_spacing
if not px_size:
warnings.warn("unknown pixel spacing, setting to 1 Angstrom")
warnings.warn("unknown pixel spacing, setting to 1 Angstrom", stacklevel=2)
px_size = 1
return (
image.data,
Expand Down
9 changes: 6 additions & 3 deletions src/blik/widgets/main_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from scipy.spatial.transform import Rotation

from ..reader import construct_particle_layer_tuples
from ..utils import generate_vectors, layer_tuples_to_layers
from ..utils import generate_vectors, invert_xyz, layer_tuples_to_layers


def _get_choices(wdg, condition=None):
Expand Down Expand Up @@ -47,8 +47,11 @@ def _update_vectors():
obj = p.features["orientation"].astype(object)
obj[pd.isnull(obj)] = Rotation.identity()
p.features["orientation"] = obj
vec_data, vec_color = generate_vectors(p.data[...], p.features["orientation"])
v.data = vec_data
# invert xyz and zyx back and forth because calculation happens in xyz space
vec_data, vec_color = generate_vectors(
invert_xyz(p.data), p.features["orientation"]
)
v.data = invert_xyz(vec_data)
v.edge_color = vec_color

p.events.set_data.disconnect(_update_vectors)
Expand Down
23 changes: 16 additions & 7 deletions src/blik/widgets/picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from scipy.spatial.transform import Rotation

from ..reader import construct_particle_layer_tuples
from ..utils import invert_xyz


def _generate_surface_grids_from_shapes_layer(
Expand All @@ -29,11 +30,17 @@ def _generate_surface_grids_from_shapes_layer(
if inside_points is None:
inside_point = None
else:
inside_point = inside_points.data[0] if len(inside_points.data) else None
inside_point = (
invert_xyz(inside_points.data[0]) if len(inside_points.data) else None
)
for _, surf in surface_shapes.features.groupby("surface_id"):
lines = data_array[surf.index]
# sort by z so lines can be added in between at a later point
lines = [line.astype(float) for line in sorted(lines, key=lambda x: x[0, 0])]
# also invert xyz so we operate in back in xyz world and not napari inverted world
lines = [
invert_xyz(line.astype(float))
for line in sorted(lines, key=lambda x: x[0, 0])
]

try:
surface_grids.append(
Expand Down Expand Up @@ -61,7 +68,7 @@ def _resample_surfaces(image_layer, surface_grids, spacing, thickness, masked):
volumes = []
for surf in surface_grids:
vol = sample_volume_around_surface(
compute(image_layer.data)[0],
compute(image_layer.data)[0].T, # transpose to go back to xyz world
surface=surf,
sampling_thickness=thickness,
sampling_spacing=spacing,
Expand All @@ -74,12 +81,13 @@ def _resample_surfaces(image_layer, surface_grids, spacing, thickness, masked):

def _generate_filaments_from_points_layer(filament_picks):
"""create a new filament representation from picked points."""
return HelicalFilament(points=filament_picks.data.astype(float))
# invert xyz to go back to xyz world
return HelicalFilament(points=invert_xyz(filament_picks.data.astype(float)))


def _resample_filament(image_layer, filament, spacing, thickness):
return sample_volume_along_spline(
compute(image_layer.data)[0],
compute(image_layer.data)[0].T, # transpose to go back to xyz world
spline=filament,
sampling_shape=(thickness, thickness),
sampling_spacing=spacing,
Expand Down Expand Up @@ -132,8 +140,9 @@ def surface(
if colormap.shape[0] == 1:
values += 1

# invert_xyz to go back to napari world (also invert faces order to preserve normals)
surface_layer_tuple = (
(vert, faces, values),
(invert_xyz(vert), invert_xyz(faces), values),
{
"name": f"{exp_id} - surface",
"metadata": {
Expand Down Expand Up @@ -240,7 +249,7 @@ def filament(

exp_id = points.metadata["experiment_id"]

path = filament.sample(n_samples=len(points.data) * 50)
path = invert_xyz(filament.sample(n_samples=len(points.data) * 50))
shapes_layer_tuple = (
[path],
{
Expand Down

0 comments on commit 38a5c45

Please sign in to comment.