Skip to content

Commit

Permalink
Apply ruff linting
Browse files Browse the repository at this point in the history
  • Loading branch information
jessicasyu committed Sep 4, 2024
1 parent 9be2368 commit a4c1ece
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 118 deletions.
11 changes: 10 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ build-backend = "poetry.core.masonry.api"
disable = [
"missing-module-docstring",
]
good-names = ["i", "j", "k", "x", "y", "z", "ds", "dt", "ax"]
good-names = ["i", "j", "k", "x", "y", "z", "u", "v", "w", "ds", "dt", "ax"]

[tool.pylint.design]
max-args = 10 # maximum number of arguments for function / method
Expand Down Expand Up @@ -90,7 +90,16 @@ max-args = 10
"tests/*.py" = [
"D", # pydocstyle
"PT009", # pytest-unittest-assertion
"PT027", # pytest-unittest-raises-assertion
"INP001", # implicit-namespace-package
"ANN201", # missing-return-type-undocumented-public-function
"S311", # suspicious-non-cryptographic-random-usage
]

[tool.coverage.report]
exclude_lines = [
"pragma: no cover",
"if TYPE_CHECKING:",
]

[tool.tox]
Expand Down
2 changes: 1 addition & 1 deletion src/arcade_collection/input/generate_setup_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def make_setup_file(
Contents of ARCADE setup file.
"""

root = ET.fromstring("<set></set>")
root = ET.fromstring("<set></set>") # noqa: S314
series = ET.SubElement(
root,
"series",
Expand Down
25 changes: 14 additions & 11 deletions src/arcade_collection/output/convert_model_units.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
from __future__ import annotations

import re
from typing import Optional, Union
from typing import TYPE_CHECKING

import pandas as pd
if TYPE_CHECKING:
import pandas as pd


def convert_model_units(
data: pd.DataFrame,
ds: Optional[float],
dt: Optional[float],
regions: Optional[Union[list[str], str]] = None,
ds: float | None,
dt: float | None,
regions: list[str] | str | None = None,
) -> None:
"""
Converts data from simulation units to true units.
Convert data from simulation units to true units.
Simulations use spatial unit of voxels and temporal unit of ticks. Spatial
resolution (microns/voxel) and temporal resolution (hours/tick) are used to
Expand Down Expand Up @@ -87,7 +90,7 @@ def convert_model_units(

def convert_temporal_units(data: pd.DataFrame, dt: float) -> None:
"""
Converts temporal data from simulation units to true units.
Convert temporal data from simulation units to true units.
Simulations use temporal unit of ticks. Temporal resolution (hours/tick) is
used to convert data to true units.
Expand All @@ -112,9 +115,9 @@ def convert_temporal_units(data: pd.DataFrame, dt: float) -> None:
data["time"] = round(dt * data["TICK"], 2)


def convert_spatial_units(data: pd.DataFrame, ds: float, region: Optional[str] = None) -> None:
def convert_spatial_units(data: pd.DataFrame, ds: float, region: str | None = None) -> None:
"""
Converts spatial data from simulation units to true units.
Convert spatial data from simulation units to true units.
Simulations use spatial unit of voxels. Spatial resolution (microns/voxel)
is used to convert data to true units.
Expand Down Expand Up @@ -183,7 +186,7 @@ def convert_spatial_units(data: pd.DataFrame, ds: float, region: Optional[str] =

def estimate_temporal_resolution(key: str) -> float:
"""
Estimates temporal resolution based on condition key.
Estimate temporal resolution based on condition key.
If the key contains ``DT##``, where ``##`` denotes the temporal resolution
in minutes/tick, temporal resolution is estimated from ``##``. Otherwise,
Expand All @@ -206,7 +209,7 @@ def estimate_temporal_resolution(key: str) -> float:

def estimate_spatial_resolution(key: str) -> float:
"""
Estimates spatial resolution based on condition key.
Estimate spatial resolution based on condition key.
If the key contains ``DS##``, where ``##`` denotes the spatial resolution
in micron/voxel, spatial resolution is estimated from ``##``. Otherwise,
Expand Down
19 changes: 13 additions & 6 deletions src/arcade_collection/output/extract_tick_json.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
from __future__ import annotations

import json
import tarfile
from typing import Optional, Union
from typing import TYPE_CHECKING

import numpy as np

if TYPE_CHECKING:
import tarfile


def extract_tick_json(
tar: tarfile.TarFile,
key: str,
tick: Union[int, float],
extension: Optional[str] = None,
field: Optional[str] = None,
tick: float,
extension: str | None = None,
field: str | None = None,
) -> list:
"""
Extract json for specified tick from tar archive.
Expand Down Expand Up @@ -60,7 +64,10 @@ def extract_tick_json(
else:
member = tar.extractfile(f"{key}{formatted_tick}.{extension}.json")

assert member is not None
if member is None:
message = "File does not exist in archive."
raise RuntimeError(message)

tick_json = json.loads(member.read().decode("utf-8"))

if isinstance(tick, float):
Expand Down
8 changes: 3 additions & 5 deletions src/arcade_collection/output/get_location_voxels.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Optional
from __future__ import annotations


def get_location_voxels(location: dict, region: Optional[str] = None) -> list[tuple[int, int, int]]:
def get_location_voxels(location: dict, region: str | None = None) -> list[tuple[int, int, int]]:
"""
Get list of voxels from location for specified region.
Expand All @@ -22,11 +22,9 @@ def get_location_voxels(location: dict, region: Optional[str] = None) -> list[tu
List of x, y, z voxels.
"""

voxels = [
return [
(x, y, z)
for loc in location["location"]
for x, y, z in loc["voxels"]
if not region or loc["region"] == region
]

return voxels
21 changes: 6 additions & 15 deletions src/arcade_collection/output/parse_growth_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ def parse_growth_file(tar: tarfile.TarFile) -> pd.DataFrame:
]
)

timepoints_df = pd.DataFrame(all_timepoints, columns=GROWTH_COLUMNS)

return timepoints_df
return pd.DataFrame(all_timepoints, columns=GROWTH_COLUMNS)


def parse_growth_timepoint(data: dict, seed: int) -> list:
Expand Down Expand Up @@ -110,8 +108,10 @@ def parse_growth_timepoint(data: dict, seed: int) -> list:
Parameters
----------
timepoint
data
Original simulation data.
seed
Random seed.
Returns
-------
Expand All @@ -123,23 +123,14 @@ def parse_growth_timepoint(data: dict, seed: int) -> list:
time = data["time"]

for location, cells in data["cells"]:
u, v, w, z = location

for cell in cells:
_, population, state, position, volume, cycles = cell

if len(cycles) == 0:
cycle = None
else:
cycle = np.mean(cycles)
cycle = None if len(cycles) == 0 else np.mean(cycles)

data_list = [
time,
seed,
u,
v,
w,
z,
*location,
position,
population,
CELL_STATES[state],
Expand Down
18 changes: 9 additions & 9 deletions src/arcade_collection/output/parse_locations_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def parse_locations_file(tar: tarfile.TarFile, regions: list[str]) -> pd.DataFra
return pd.DataFrame(all_locations, columns=columns)


def parse_location_tick(tick: int, locations: dict, regions: list[str]) -> list:
def parse_location_tick(tick: int, location: dict, regions: list[str]) -> list:
"""
Parse location data for a single simulation tick.
Expand Down Expand Up @@ -105,7 +105,7 @@ def parse_location_tick(tick: int, locations: dict, regions: list[str]) -> list:
----------
tick
Simulation tick.
loc
location
Original location data.
regions
List of regions.
Expand All @@ -116,31 +116,31 @@ def parse_location_tick(tick: int, locations: dict, regions: list[str]) -> list:
Parsed location data.
"""

if "center" in locations:
voxels = np.array([voxel for region in locations["location"] for voxel in region["voxels"]])
if "center" in location:
voxels = np.array([voxel for region in location["location"] for voxel in region["voxels"]])
mins = np.min(voxels, axis=0)
maxs = np.max(voxels, axis=0)
parsed = [locations["id"], tick, *locations["center"], *mins, *maxs]
parsed = [location["id"], tick, *location["center"], *mins, *maxs]
else:
parsed = [locations["id"], tick, -1, -1, -1, -1, -1, -1, -1, -1, -1]
parsed = [location["id"], tick, -1, -1, -1, -1, -1, -1, -1, -1, -1]

for reg in regions:
region_voxels = np.array(
[
voxel
for region in locations["location"]
for region in location["location"]
for voxel in region["voxels"]
if region["region"] == reg
]
)

if len(region_voxels) == 0:
parsed = parsed + [-1, -1, -1, -1, -1, -1, -1, -1, -1]
parsed = [*parsed, -1, -1, -1, -1, -1, -1, -1, -1, -1]
continue

center = [int(value + 0.5) for value in region_voxels.mean(axis=0)]
mins = np.min(region_voxels, axis=0)
maxs = np.max(region_voxels, axis=0)
parsed = parsed + [*center, *mins, *maxs]
parsed = [*parsed, *center, *mins, *maxs]

return parsed
4 changes: 3 additions & 1 deletion tests/arcade_collection/input/test_convert_to_cells_file.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import unittest

import numpy as np
Expand All @@ -16,7 +18,7 @@
DEFAULT_REGION_NAME = "DEFAULT"


def make_samples(cell_id, volume, height, region):
def make_samples(cell_id: int, volume: int, height: int, region: str | None):
return pd.DataFrame(
{
"id": [cell_id] * volume,
Expand Down
Loading

0 comments on commit a4c1ece

Please sign in to comment.