Skip to content

Commit

Permalink
Updated rename wells and testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph Schull committed Jul 12, 2024
1 parent 66e5d12 commit 75a9327
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
18 changes: 12 additions & 6 deletions iohub/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,15 @@ def convert(input, output, grid_layout, chunks):
@click.option(
"-i",
"--input",
type=click.Path(exists=True, file_okay=True, dir_okay=False),
"zarrfile",
type=click.Path(exists=True, file_okay=True, dir_okay=True),
required=True,
help="Path to the input Zarr file.",
)
@click.option(
"-c",
"--csv",
"csvfile",
type=click.File("r"),
required=True,
help="Path to the CSV file containing well names.",
Expand All @@ -116,6 +118,7 @@ def rename_wells_cli(csvfile, zarrfile):
The CSV file should have two columns: old_well_path and new_well_path.
"""

names = []

csvreader = csv.reader(csvfile)
Expand All @@ -131,13 +134,16 @@ def rename_wells_cli(csvfile, zarrfile):

modified = []

for old_well_path, new_well_path in names:
print(f"names: {names}")

for name in names:
old_well_path, new_well_path = name[0], name[1]
for well in plate.metadata.wells:
if well.path == old_well_path and well not in modified:
if str(well.path) == str(old_well_path) and well not in modified:
print(f"Renaming {old_well_path} to {new_well_path}...")
try:
plate.rename_well(
well, old_well_path, new_well_path, modified, False
)
plate.rename_well(well, old_well_path, new_well_path)
modified.append(well)
print(f"Well {old_well_path} renamed to {new_well_path}")
except ValueError as e:
click.echo(f"Error: {e}", err=True)
10 changes: 4 additions & 6 deletions iohub/ngff.py
Original file line number Diff line number Diff line change
Expand Up @@ -1579,10 +1579,6 @@ def rename_well(
Old name of well
new_well_path : str
New name of well
modified : dict, optional
Tracks modified wells
single well: bool, optional
True: renaming one well, False: renaming multiple wells
"""
old_row, old_column = old_well_path.split("/")
new_row, new_column = new_well_path.split("/")
Expand All @@ -1592,8 +1588,10 @@ def rename_well(
if old_well_path not in well_paths:
raise ValueError(f"Well '{old_well_path}' not found in plate.")

else:
well = self[old_well_path]
elif old_well_path in well_paths:
well = next(
w for w in self.metadata.wells if w.path == old_well_path
)

if well.path == old_well_path:
well.path = new_well_path # update well metadata
Expand Down
32 changes: 29 additions & 3 deletions tests/cli/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import csv
import re
from unittest.mock import patch

from click.testing import CliRunner
import pytest
from click.testing import CliRunner

from iohub._version import __version__
from iohub.cli.cli import cli

from tests.conftest import (
hcs_ref,
mm2gamma_ome_tiffs,
ndtiff_v2_datasets,
ndtiff_v3_labeled_positions,
hcs_ref,
)


Expand Down Expand Up @@ -114,3 +114,29 @@ def test_rename_wells_help():
result = runner.invoke(cli, cmd)
assert result.exit_code == 0
assert "containing well names" in result.output


def test_rename_wells_basic():
runner = CliRunner()
test_zarr = "/hpc/mydata/joseph.schull/stitched_phase.zarr"
test_csv = "/hpc/mydata/joseph.schull/update_well_names.csv"
cmd = ["rename-wells", "-i", test_zarr, "-c", test_csv]
result = runner.invoke(cli, cmd)
print(result.output)
assert result.exit_code == 0


def test_rename_wells_completion():
runner = CliRunner()
test_zarr = "/hpc/mydata/joseph.schull/stitched_phase.zarr"
test_csv = "/hpc/mydata/joseph.schull/update_well_names.csv"
cmd = ["rename-wells", "-i", test_zarr, "-c", test_csv]
result = runner.invoke(cli, cmd)
with open(test_csv, mode="r") as infile:
reader = csv.reader(infile)
names = list(reader)
for oldname, newname in names:
expected_message = f"Well {oldname} renamed to {newname}"
assert (
expected_message in result.output
), f"Missing expected message: {expected_message}"

0 comments on commit 75a9327

Please sign in to comment.