Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor scripts into a CLI #6

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open

Refactor scripts into a CLI #6

wants to merge 18 commits into from

Conversation

thompsonmj
Copy link
Contributor

@thompsonmj thompsonmj commented Sep 18, 2024

This PR intends to provide functionality as a user-friendly CLI.

  • Preprocessing - resize images while preserving nested directory hierarchy (if any)
  • Segmentation
  • Prep for color calibration and landmarking

Installation can be done by cloning the repo or directly with pip install git+https://github.com/Imageomics/wing-segmentation.git@feature/start-cli.

@thompsonmj
Copy link
Contributor Author

@ramirezmichelle, @egrace479 please let me know if this looks on track with how functionality should go. Refer to the info in the README for how to test and what expected behavior will be.

For example, using the default setting for the output directory and trying various other settings, I get the following organization with test data:

$ ls data/
Jiggins_data  Jiggins_data_resize  meier_images  meier_images_resize
$ ls data/Jiggins_data_resize/
128x128  256x256
$ ls data/Jiggins_data_resize/256x256/
area  cubic
$ ls -1 data/Jiggins_data_resize/256x256/area/
'Heliconius erato ssp. amalfreda'
...
'Heliconius melpomene ssp. vulcanus'
metadata.json
$ ls -1 data/Jiggins_data_resize/256x256/area/Heliconius\ erato\ ssp.\ amalfreda/
CAM021007_d.png
...
CAM021227_d.png
$ cat data/Jiggins_data_resize/256x256/area/metadata.json | jq
{
  "source_directory": "data/Jiggins_data/",
  "output_directory": "/local/scratch/thompson.4509/projects/wing-segmentation/data/Jiggins_data_resize/256x256/area",
  "resize_dimensions": [
    256,
    256
  ],
  "interpolation_method": "area",
  "num_workers": 16,
  "timestamp": "2024-09-19 14:38:56"
}

It automatically handles nested vs flat data under the source directory and provides a metadata.json file in the base of the output dir (in the parent of any nested subdirs) to keep track of what was done.

I will do segmentation next.

@ramirezmichelle ramirezmichelle marked this pull request as ready for review September 24, 2024 18:27
This large commit implements most desired functionality. Additional testing and fine-tuning is needed for user input validation.
@thompsonmj
Copy link
Contributor Author

The functionality takes care of segmentation and cropped image prep for landmarking.

Copy link
Member

@egrace479 egrace479 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First pass, these commands need to be updated to match changes in 53a147d.

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
Copy link
Member

@egrace479 egrace479 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, looks a little better with "console" instead of "bash" formatting for this part.

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@thompsonmj
Copy link
Contributor Author

Thanks! Yes, the README will need rewritten once the user options are settled.

Still allows Segmenter to be imported by a user, but bypasses its heavy imports until called directly
@thompsonmj thompsonmj requested a review from egrace479 December 2, 2024 20:16
Copy link
Member

@egrace479 egrace479 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some preliminary thoughts on presentation/argument structure.

Comment on lines +336 to +356
To use and continue building the CLI features, set up and activate a virtual environment, and build interactively with `pip install -e .[dev]`.

> [!TIP]
> [`uv`](https://github.com/astral-sh/uv) is a fast, Rust-based package manager.
> If using on an HPC system, you may install `uv` into your user path or a conda environment (latter illustrated here).

```console
conda create -n uv -c conda-forge --solver=libmamba python=3.10 uv -y
```
```console
conda activate uv
```
```console
uv venv
```
```console
source .venv/bin/activate # or source .venv/Scripts/activate on Windows
```
```console
uv pip install -e .[dev]
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
To use and continue building the CLI features, set up and activate a virtual environment, and build interactively with `pip install -e .[dev]`.
> [!TIP]
> [`uv`](https://github.com/astral-sh/uv) is a fast, Rust-based package manager.
> If using on an HPC system, you may install `uv` into your user path or a conda environment (latter illustrated here).
```console
conda create -n uv -c conda-forge --solver=libmamba python=3.10 uv -y
```
```console
conda activate uv
```
```console
uv venv
```
```console
source .venv/bin/activate # or source .venv/Scripts/activate on Windows
```
```console
uv pip install -e .[dev]
```
To use and continue building the CLI features, set up and activate a virtual environment, and build interactively with
```
pip install -e .[dev]
```

I don't think this is an appropriate place to introduce uv. It would be a great addition to our Helpful Tools page in the Guide, and you could link to that with a suggestion that uv pip install -e .[dev] is a faster option.

Also, when I tried to run pip install -e .[dev] I got zsh: no matches found: .[dev], but pip install -e . worked as expected.

Comment on lines +318 to +332
Inspecting data in the `seg_viz/`, we can see that the 1024x1024 products have segmentation masks that differ from the 512x512 and 256x256 products.

1024x1024 (Run 8e9ae0a2):

![1024 resize segmentation result visualization](readme_images/STRI_WOM_0011_V_viz_1024.png)

512x512 (Run 3354acb9):

![512 resize segmentation result visualization](readme_images/STRI_WOM_0011_V_viz_512.png)

256x256 (Run 0f27d745):

![256 resize segmentation result visualization](readme_images/STRI_WOM_0011_V_viz_256.png)

A potential fix for this could be to add padding to the bounding boxes (with the `--bbox-padding` option) wherever results are inconsistent with expectations.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The underlying image should be cited. I'm still waiting on Christopher for the appropriate licensing and citation for the STRI images (see PR #5).

Comment on lines +50 to +51
output_group.add_argument('--outputs-base-dir', default=None, help='Base path to store outputs.')
output_group.add_argument('--custom-output-dir', default=None, help='Fully custom directory to store all output files.')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly is the difference between these?

Comment on lines +30 to +31
resize_group.add_argument('--resize-mode', choices=['distort', 'pad'], default=None,
help='Resizing mode. "distort" resizes without preserving aspect ratio, "pad" preserves aspect ratio and adds padding if necessary.')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
resize_group.add_argument('--resize-mode', choices=['distort', 'pad'], default=None,
help='Resizing mode. "distort" resizes without preserving aspect ratio, "pad" preserves aspect ratio and adds padding if necessary.')
resize_group.add_argument('--resize-mode', choices=['distort', 'pad'], default=None,
help='Resizing mode. "distort" resizes without preserving aspect ratio, "pad" preserves aspect ratio and adds padding if necessary. Required with --size.')

--size SIZE [SIZE ...]
Target size. Provide one value for square dimensions or two for width and height. (default: None)
--resize-mode {distort,pad}
Resizing mode. "distort" resizes without preserving aspect ratio, "pad" preserves aspect ratio and adds padding if necessary.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Resizing mode. "distort" resizes without preserving aspect ratio, "pad" preserves aspect ratio and adds padding if necessary.
Resizing mode. "distort" resizes without preserving aspect ratio, "pad" preserves aspect ratio and adds padding if necessary. Required with --size.

Matching suggestion made at arg definition.

Comment on lines +108 to +109
if args.custom_output_dir and args.outputs_base_dir:
parser.error('Cannot specify both --outputs-base-dir and --custom-output-dir. Choose one.')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this not handled by the mutually exclusive group?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants