Skip to content

Commit 442dd08

Browse files
Add CLI script for data conversion (#13)
* Add CLI script for data conversion * Add pyproject.toml and filter max-projection tifs from glob * Fix tests
1 parent c58180e commit 442dd08

File tree

9 files changed

+89
-28
lines changed

9 files changed

+89
-28
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ jobs:
2323
with:
2424
environment-file: environment.yaml
2525

26-
# - name: Install package
27-
# shell: bash -l {0}
28-
# run: pip install -e .
26+
- name: Install package
27+
shell: bash -l {0}
28+
run: pip install -e .
2929

3030
- name: Run tests
3131
shell: bash -l {0}

README.md

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Data processing for light-sheet microscopy, specifically for data from [Flamingo
44

55
The `flamingo_tools` library implements functionality for:
66
- converting the lightsheet data into a format compatible with [BigDataViewer](https://imagej.net/plugins/bdv/) and [BigStitcher](https://imagej.net/plugins/bigstitcher/).
7+
- Cell / nucleus segmentation via a 3D U-net.
78
- ... and more functionality is planned!
89

910
This is work in progress!
@@ -12,25 +13,38 @@ This is work in progress!
1213
## Requirements & Installation
1314

1415
You need a python environment with the following dependencies: [pybdv](https://github.com/constantinpape/pybdv) and [z5py](https://github.com/constantinpape/z5).
15-
You can for example install these dependencies with [mamba](https://github.com/mamba-org/mamba) (a faster implementation of [conda](https://docs.conda.io/en/latest/)) via:
16+
You install these dependencies with [mamba](https://github.com/mamba-org/mamba) or [conda](https://docs.conda.io/en/latest/) via:
1617
```bash
17-
$ mamba install -c conda-forge z5py pybdv
18+
conda install -c conda-forge z5py pybdv
1819
```
19-
You can also set up a new environment with these dependencies using the file `environment.yaml`:
20+
(for an existing conda environment). You can also set up a new environment with all required dependencies using the file `environment.yaml`:
2021
```bash
21-
$ mamba env create -f environment.yaml
22+
conda env create -f environment.yaml
23+
```
24+
This will create the environment `flamingo`, which you can then activate via `conda activate flamingo`.
25+
Finally, to install `flamingo_tools` into the environment run
26+
```bash
27+
pip install -e .
2228
```
2329

2430
## Usage
2531

26-
We provide the follwoing scripts:
32+
We provide a command line tool, `convert_flamingo`, for converting data from the flamingo microscope to a data format compatible with BigDataViewer / BigStitcher:
33+
```bash
34+
convert_flamingo -i /path/to/data -o /path/to/output.n5 --file_ext .tif
35+
```
36+
Here, `/path/to/data` is the filepath to the folder with the flamingo data to be converted, `/path/to/output.n5` is the filepath where the converted data will be stored, and `--file_ext .tif` declares that the files are stored as tif stacks.
37+
Use `--file_ext .raw` isntead if the data is stored in raw files.
38+
39+
The data will be converted to the [bdv.n5 format](https://github.com/bigdataviewer/bigdataviewer-core/blob/master/BDV%20N5%20format.md).
40+
It can be opened with BigDataViewer via `Plugins->BigDataViewer->Open XML/HDF5`.
41+
Or with BigStitcher as described [here](https://imagej.net/plugins/bigstitcher/open-existing).
42+
43+
You can also check out the following example scripts:
2744
- `create_synthetic_data.py`: create small synthetic test data to check that the scripts work.
28-
- `convert_flamingo_data.py`: convert flamingo data to a file format comatible with BigDataViewer / BigStitcher via command line interface. Run `python convert_flamingo_data.py -h` for details.
2945
- `convert_flamingo_data_examples.py`: convert flamingo data to a file format comatible with BigDataViewer / BigStitcher with parameters defined in the python script. Contains two example functions:
3046
- `convert_synthetic_data` to convert the synthetic data created via `create_synthetic_data.py`.
3147
- `convert_flamingo_data_moser` to convert sampled flamingo data from the Moser group.
3248
- `load_data.py`: Example script for how to load sub-regions from the converted data into python.
3349

34-
The data will be converted to the [bdv.n5 format](https://github.com/bigdataviewer/bigdataviewer-core/blob/master/BDV%20N5%20format.md).
35-
It can be opened with BigDataViewer via `Plugins->BigDataViewer->Open XML/HDF5`.
36-
Or with BigStitcher as described [here](https://imagej.net/plugins/bigstitcher/open-existing).
50+
For advanced examples to segment data with a U-Net, check out the `scripts` folder.

convert_flamingo_data.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

environment.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: lightsheet
1+
name: flamingo
22

33
channels:
44
- pytorch

flamingo_tools/data_conversion.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,9 @@ def convert_lightsheet_to_bdv(
300300
convert_to_ome_zarr = True
301301

302302
files = sorted(glob(os.path.join(root, f"**/*{file_ext}"), recursive=True))
303+
if file_ext == ".tif":
304+
# We need to exlcude the max-projetion tifs that are saved alongside the tifs.
305+
files = [ff for ff in files if "_MP.tif" not in ff]
303306
# Raise an error if we could not find any files.
304307
if len(files) == 0:
305308
raise ValueError(f"Could not find any files in {root} with extension {file_ext}.")
@@ -409,9 +412,14 @@ def convert_lightsheet_to_bdv_cli():
409412
)
410413

411414
args = parser.parse_args()
415+
if args.metadata_pattern == "":
416+
metadata_pattern = None
417+
else:
418+
metadata_pattern = args.metadata_pattern
419+
412420
convert_lightsheet_to_bdv(
413421
root=args.input_root,
414422
out_path=args.out_path,
415423
file_ext=args.file_ext,
416-
metadata_file_name_pattern=args.metadata_pattern
424+
metadata_file_name_pattern=metadata_pattern
417425
)

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[build-system]
2+
requires = ["setuptools>=64.0", "wheel"]
3+
build-backend = "setuptools.build_meta"

setup.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import runpy
22
from setuptools import setup, find_packages
33

4-
version = runpy.run_path('flamingo_tools/version.py')['__version__']
5-
setup(name='flamingo_tools',
6-
packages=find_packages(exclude=['test']),
7-
version=version,
8-
author='Constantin Pape',
9-
license='MIT')
4+
version = runpy.run_path("flamingo_tools/version.py")["__version__"]
5+
setup(
6+
name="flamingo_tools",
7+
packages=find_packages(exclude=["test"]),
8+
version=version,
9+
author="Constantin Pape",
10+
license="MIT",
11+
entry_points={
12+
"console_scripts": [
13+
"convert_flamingo = flamingo_tools.data_conversion:convert_lightsheet_to_bdv_cli"
14+
]
15+
}
16+
)

test/test_cli.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import os
2+
import unittest
3+
from shutil import rmtree
4+
5+
import z5py
6+
7+
from subprocess import run
8+
9+
10+
class TestCLI(unittest.TestCase):
11+
folder = "./tmp"
12+
13+
def setUp(self):
14+
from flamingo_tools import create_test_data
15+
16+
# TODO Create flamingo metadata.
17+
create_test_data(self.folder)
18+
19+
def tearDown(self):
20+
rmtree(self.folder)
21+
22+
def test_convert_flamingo(self):
23+
out_path = os.path.join(self.folder, "converted_data.n5")
24+
cmd = ["convert_flamingo", "-i", self.folder, "-o", out_path, "--metadata_pattern", ""]
25+
run(cmd)
26+
27+
self.assertTrue(os.path.exists(out_path))
28+
xml_path = out_path.replace(".n5", ".xml")
29+
self.assertTrue(os.path.exists(xml_path))
30+
with z5py.File(out_path, "r") as f:
31+
self.assertTrue("setup0" in f)
32+
33+
34+
if __name__ == "__main__":
35+
unittest.main()

test/test_data_conversion.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
import os
2-
import sys
32
import unittest
43
from shutil import rmtree
54

6-
sys.path.append("..")
7-
85

96
class TestDataConversion(unittest.TestCase):
107
folder = "./tmp"
118

129
def setUp(self):
1310
from flamingo_tools import create_test_data
1411

15-
# TODO metadata
12+
# TODO Create flamingo metadata.
1613
create_test_data(self.folder)
1714

1815
def tearDown(self):
@@ -22,7 +19,7 @@ def test_convert_lightsheet_to_bdv(self):
2219
from flamingo_tools import convert_lightsheet_to_bdv
2320

2421
out_path = os.path.join(self.folder, "converted_data.n5")
25-
convert_lightsheet_to_bdv(self.folder, out_path=out_path)
22+
convert_lightsheet_to_bdv(self.folder, out_path=out_path, metadata_file_name_pattern=None)
2623

2724
self.assertTrue(os.path.exists(out_path))
2825
xml_path = out_path.replace(".n5", ".xml")

0 commit comments

Comments
 (0)