Skip to content

Commit

Permalink
refactor: remove Optional and more generic dirs, fix redundant code line
Browse files Browse the repository at this point in the history
  • Loading branch information
seankmartin committed Oct 22, 2024
1 parent a066566 commit f1a179a
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 20 deletions.
1 change: 0 additions & 1 deletion cryoet_data_portal_neuroglancer/models/json_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ def _create_shader_and_controls(self) -> dict[str, Any]:
self.contrast_limits = self._compute_contrast_limits()
if self.threedee_contrast_limits is None:
self.threedee_contrast_limits = self.contrast_limits
self.threedee_contrast_limits = self.threedee_contrast_limits
if self.has_volume_rendering_shader:
shader_builder = ImageWithVolumeRenderingShaderBuilder(
contrast_limits=self.contrast_limits,
Expand Down
27 changes: 12 additions & 15 deletions cryoet_data_portal_neuroglancer/precompute/contrast_limits.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import logging
from pathlib import Path
from typing import Literal, Optional
from typing import Literal

import dask.array as da
import numpy as np
from scipy.signal import find_peaks
from scipy.spatial.distance import euclidean
from sklearn.mixture import GaussianMixture

from cryoet_data_portal_neuroglancer.utils import ParameterOptimizer
Expand Down Expand Up @@ -60,14 +61,10 @@ def compute_contrast_limits(
return calculator.compute_contrast_limit()


def _euclidean_distance(x: tuple[float, float], y: tuple[float, float]) -> float:
return np.sqrt(((x[0] - y[0]) ** 2) + ((x[1] - y[1]) ** 2))


def _restrict_volume_around_central_z_slice(
volume: "np.ndarray",
central_z_slice: Optional[int] = None,
z_radius: Optional[int] = 5,
central_z_slice: int | None = None,
z_radius: int | None = 5,
) -> "np.ndarray":
"""Restrict a 3D volume to a region around a central z-slice.
Expand All @@ -78,7 +75,7 @@ def _restrict_volume_around_central_z_slice(
central_z_slice: int or None, optional.
The central z-slice around which to restrict the volume.
By default None, in which case the central z-slice is the middle slice.
z_radius: int, optional.
z_radius: int or None, optional.
The number of z-slices to include above and below the central z-slice.
By default 5,
If it is None, it is auto computed - but this can be problematic for large volumes.
Expand Down Expand Up @@ -112,7 +109,7 @@ def _restrict_volume_around_central_z_slice(

class ContrastLimitCalculator:

def __init__(self, volume: Optional["np.ndarray"] = None):
def __init__(self, volume: "np.ndarray" | None = None):
"""Initialize the contrast limit calculator.
Parameters
Expand All @@ -125,8 +122,8 @@ def __init__(self, volume: Optional["np.ndarray"] = None):
def set_volume_and_z_limits(
self,
volume: "np.ndarray",
central_z_slice: Optional[int] = None,
z_radius: Optional[int] = None,
central_z_slice: int | None = None,
z_radius: int | None = None,
) -> None:
"""Set the volume and z-limits for calculating contrast limits.
Expand All @@ -146,8 +143,8 @@ def set_volume_and_z_limits(

def trim_volume_around_central_zslice(
self,
central_z_slice: Optional[int] = None,
z_radius: Optional[int] = None,
central_z_slice: int | None = None,
z_radius: int | None = None,
) -> None:
"""Trim the volume around a central z-slice.
Expand Down Expand Up @@ -261,7 +258,7 @@ def _objective(params):
return best, contrast_limits

def objective_function(self, params, real_limits):
return _euclidean_distance(
return euclidean(
self._objective_function(params),
real_limits,
)
Expand Down Expand Up @@ -412,7 +409,7 @@ def _define_parameter_space(self, parameter_optimizer):

class CDFContrastLimitCalculator(ContrastLimitCalculator):

def __init__(self, volume: Optional["np.ndarray"] = None):
def __init__(self, volume: "np.ndarray" | None = None):
"""Initialize the contrast limit calculator.
Parameters
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import multiprocessing as mp
from multiprocessing import shared_memory
from typing import Optional

import numpy as np
from tqdm import tqdm
Expand Down Expand Up @@ -41,7 +40,7 @@ def volume_render(
contrast_limits: tuple[float, float],
exponential_gain: float = 0.0,
depth_samples: int = 64,
num_workers: Optional[int] = None,
num_workers: int | None = None,
):
if num_workers is None:
num_workers = mp.cpu_count()
Expand Down
2 changes: 1 addition & 1 deletion manual_tests/tuning_contrast_limits_from_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
# Set up logging - level is info
# logging.basicConfig(level=logging.INFO, force=True)

OUTPUT_FOLDER = "/media/starfish/LargeSSD/data/cryoET/data/FromAPI"
OUTPUT_FOLDER = Path.cwd() / "contrast_limits_from_api"

id_to_path_map = {
1000: "1000/16.zarr",
Expand Down
2 changes: 1 addition & 1 deletion manual_tests/volume_contrast_limits.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# Set up logging - level is info
# logging.basicConfig(level=logging.INFO, force=True)

OUTPUT_FOLDER = "/media/starfish/LargeSSD/data/cryoET/data/FromAPI"
OUTPUT_FOLDER = Path.cwd() / "volume_contrast_limits"

id_to_path_map = {
773: "773/Position_513.zarr",
Expand Down

0 comments on commit f1a179a

Please sign in to comment.