Skip to content

Commit

Permalink
fix: rows in dataframe
Browse files Browse the repository at this point in the history
  • Loading branch information
nishaq503 committed May 10, 2024
1 parent 0219c1c commit d4342fb
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 4 deletions.
3 changes: 3 additions & 0 deletions features/rt-cetsa-intensity-extraction-tool/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ bfio = "^2.3.6"
bump2version = "^1.0.1"
pre-commit = "^3.1.0"
pytest = "^7.2.1"
scikit-image = "0.22.0"
tifffile = "^2024.5.10"
polus_images_segmentation_rt_cetsa_plate_extraction = { path = "../../segmentation/rt-cetsa-plate-extraction-tool", develop = true }

[build-system]
requires = ["poetry-core"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@ def index_to_battleship(x: int, y: int, size: PlateSize) -> str:
row = "A"
row = row + string.ascii_uppercase[y % 26]

# TODO: uncomment this when we are ready to deploy, this is the standard notation
# if size.value >= 96:

return f"{row}{x+1}"


Expand All @@ -122,6 +119,7 @@ def build_df(

# sort intensities by temperature
intensities.sort(key=lambda x: x[0])
rows: list[list[float]] = [[a, *b] for a, b in intensities]

# build header
header = ["Temperature"]
Expand All @@ -134,7 +132,7 @@ def build_df(
header.append(index_to_battleship(x, y, plate_size))

# build DataFrame
df = pandas.DataFrame(intensities, columns=header)
df = pandas.DataFrame(rows, columns=header)

# Set the temperature as the index
df.set_index("Temperature", inplace=True)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Tests for the tool."""
79 changes: 79 additions & 0 deletions features/rt-cetsa-intensity-extraction-tool/tests/test_plate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import pathlib
import numpy
from skimage.draw import disk
from skimage.transform import rotate
from tifffile import imwrite

from polus.images.features.rt_cetsa_intensity_extraction import build_df
from polus.images.segmentation.rt_cetsa_plate_extraction import extract_plate


def gen_plate(
size: tuple[int, int],
radius: int,
trim: int,
angle: int,
inp_dir: pathlib.Path,
) -> numpy.ndarray:
"""Generate a plate image."""

# Calculate the size of the plate
well_size = 2 * (radius + trim)
plate_size = (size[0] * well_size, size[1] * well_size)
plate = numpy.zeros(plate_size, dtype=numpy.uint8)

# Generate the wells
x_centers = numpy.arange(radius + trim, plate_size[0], well_size)
y_centers = numpy.arange(radius + trim, plate_size[1], well_size)
for xc in x_centers:
for yc in y_centers:
rr, cc = disk((xc, yc), radius)
plate[rr, cc] = 1

# Add noise to the wells
rng = numpy.random.default_rng()
noise = rng.poisson(16, size=plate.shape).astype(numpy.uint8)
plate *= noise

background = rng.poisson(2, size=plate.shape).astype(numpy.uint8)
plate += background

# Rotate the plate
plate = rotate(plate, -angle, resize=True, cval=0)

# Pad the plate with zeros
pad = trim * min(size)
plate = numpy.pad(plate, pad_width=pad, mode="constant", constant_values=0)

return plate


def test_tool():
data_dir = pathlib.Path(__file__).parent.parent / "data"
inp_dir = data_dir / "input"
out_dir = data_dir / "output"

size = (16, 24)
radius = 20
trim = 5
angle = 15

paths = []
for i in range(1, 4):
p_path = inp_dir / f"plate_{i}.tif"
m_path = inp_dir / f"mask_{i}.tif"

plate = gen_plate(size, radius, trim, angle, inp_dir)
imwrite(p_path, plate)

plate, mask = extract_plate(p_path)
imwrite(p_path, plate)
imwrite(m_path, mask)

paths.append((p_path, m_path))

df = build_df(paths)
path = out_dir / "intensities.csv"
df.to_csv(path)

assert path.exists()

0 comments on commit d4342fb

Please sign in to comment.