dsa-helpers 0.4.dev3__tar.gz → 0.4.dev4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. {dsa_helpers-0.4.dev3/dsa_helpers.egg-info → dsa_helpers-0.4.dev4}/PKG-INFO +8 -2
  2. {dsa_helpers-0.4.dev3 → dsa_helpers-0.4.dev4}/README.md +4 -2
  3. {dsa_helpers-0.4.dev3 → dsa_helpers-0.4.dev4}/dsa_helpers/__init__.py +1 -1
  4. dsa_helpers-0.4.dev4/dsa_helpers/ml/__init__.py +2 -0
  5. dsa_helpers-0.4.dev4/dsa_helpers/ml/datasets/SegFormerSegmentationDataset.py +33 -0
  6. dsa_helpers-0.4.dev4/dsa_helpers/ml/datasets/__init__.py +3 -0
  7. dsa_helpers-0.4.dev4/dsa_helpers/ml/datasets/utils.py +42 -0
  8. dsa_helpers-0.4.dev4/dsa_helpers/ml/metrics/__init__.py +3 -0
  9. dsa_helpers-0.4.dev4/dsa_helpers/ml/metrics/binary_dice_coefficient_metric.py +22 -0
  10. dsa_helpers-0.4.dev4/dsa_helpers/ml/transforms/__init__.py +1 -0
  11. dsa_helpers-0.4.dev4/dsa_helpers/ml/transforms/segformer_transforms.py +23 -0
  12. dsa_helpers-0.4.dev4/dsa_helpers/utils.py +28 -0
  13. {dsa_helpers-0.4.dev3 → dsa_helpers-0.4.dev4/dsa_helpers.egg-info}/PKG-INFO +8 -2
  14. {dsa_helpers-0.4.dev3 → dsa_helpers-0.4.dev4}/dsa_helpers.egg-info/SOURCES.txt +12 -2
  15. {dsa_helpers-0.4.dev3 → dsa_helpers-0.4.dev4}/dsa_helpers.egg-info/requires.txt +4 -0
  16. {dsa_helpers-0.4.dev3 → dsa_helpers-0.4.dev4}/pyproject.toml +6 -2
  17. {dsa_helpers-0.4.dev3 → dsa_helpers-0.4.dev4}/requirements.txt +5 -1
  18. dsa_helpers-0.4.dev4/tests/app/Dockerfile +18 -0
  19. dsa_helpers-0.4.dev4/tests/app/app.py +55 -0
  20. dsa_helpers-0.4.dev4/tests/docker-compose.yaml +31 -0
  21. dsa_helpers-0.4.dev3/dsa_helpers/ml/__init__.py +0 -2
  22. dsa_helpers-0.4.dev3/readthedocs.yaml +0 -27
  23. {dsa_helpers-0.4.dev3 → dsa_helpers-0.4.dev4}/.gitignore +0 -0
  24. {dsa_helpers-0.4.dev3 → dsa_helpers-0.4.dev4}/LICENSE +0 -0
  25. {dsa_helpers-0.4.dev3 → dsa_helpers-0.4.dev4}/app.py +0 -0
  26. {dsa_helpers-0.4.dev3 → dsa_helpers-0.4.dev4}/dsa_helpers/dash/__init__.py +0 -0
  27. {dsa_helpers-0.4.dev3 → dsa_helpers-0.4.dev4}/dsa_helpers/dash/header.py +0 -0
  28. {dsa_helpers-0.4.dev3 → dsa_helpers-0.4.dev4}/dsa_helpers/dash/header_callbacks.py +0 -0
  29. {dsa_helpers-0.4.dev3 → dsa_helpers-0.4.dev4}/dsa_helpers/girder_utils.py +0 -0
  30. {dsa_helpers-0.4.dev3 → dsa_helpers-0.4.dev4}/dsa_helpers/image_utils.py +0 -0
  31. {dsa_helpers-0.4.dev3 → dsa_helpers-0.4.dev4}/dsa_helpers/imread.py +0 -0
  32. {dsa_helpers-0.4.dev3 → dsa_helpers-0.4.dev4}/dsa_helpers/imwrite.py +0 -0
  33. {dsa_helpers-0.4.dev3 → dsa_helpers-0.4.dev4}/dsa_helpers/login.py +0 -0
  34. {dsa_helpers-0.4.dev3 → dsa_helpers-0.4.dev4}/dsa_helpers/ml/object_detection/__init__.py +0 -0
  35. {dsa_helpers-0.4.dev3 → dsa_helpers-0.4.dev4}/dsa_helpers/ml/object_detection/get_pearce_roi_images.py +0 -0
  36. {dsa_helpers-0.4.dev3 → dsa_helpers-0.4.dev4}/dsa_helpers/ml/object_detection/tile_image.py +0 -0
  37. {dsa_helpers-0.4.dev3 → dsa_helpers-0.4.dev4}/dsa_helpers/ml/object_detection/utils.py +0 -0
  38. {dsa_helpers-0.4.dev3 → dsa_helpers-0.4.dev4}/dsa_helpers/mongo_utils.py +0 -0
  39. {dsa_helpers-0.4.dev3 → dsa_helpers-0.4.dev4}/dsa_helpers.egg-info/dependency_links.txt +0 -0
  40. {dsa_helpers-0.4.dev3 → dsa_helpers-0.4.dev4}/dsa_helpers.egg-info/top_level.txt +0 -0
  41. {dsa_helpers-0.4.dev3 → dsa_helpers-0.4.dev4}/setup.cfg +0 -0
  42. {dsa_helpers-0.4.dev3 → dsa_helpers-0.4.dev4}/tests/test.ipynb +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: dsa_helpers
3
- Version: 0.4.dev3
3
+ Version: 0.4.dev4
4
4
  Summary: Utility functions for working with the DSA girder client.
5
5
  Author-email: "Juan C. Vizcarra" <jvizcar@emory.edu>
6
6
  Maintainer-email: "Juan C. Vizcarra" <jvizcar@emory.edu>
@@ -22,6 +22,10 @@ Requires-Dist: dash
22
22
  Requires-Dist: dash_bootstrap_components
23
23
  Requires-Dist: shapely
24
24
  Requires-Dist: geopandas
25
+ Requires-Dist: torch
26
+ Requires-Dist: datasets
27
+ Requires-Dist: transformers
28
+ Requires-Dist: torchvision
25
29
 
26
30
  # Digital Slide Archive (DSA) Helpers
27
31
  Digital Slide Archive Python utility library.
@@ -36,9 +40,11 @@ This Python PyPI package is found [here](https://pypi.org/project/dsa-helpers/).
36
40
  1. Install requirements for building and distributing package:
37
41
  ```
38
42
  $ python3 -m pip install --upgrade build # for building package
39
- $ python3 -m pip install --upgrade build # for uploading to PyPI
43
+ $ python3 -m pip install --upgrade twine # for uploading to PyPI
40
44
  ```
41
45
  2. When ready to build:
42
46
  - Modify "pyproject.toml" file, change the "version" key to a different value than a version already pushed.
43
47
  - Run ```$ python3 -m build```. This will create a dist directory and put your new wheel and tar distribution files there.
44
48
  3. To install locally for development: ```$ pip3 install dist/dsa_helpers-*.whl```, choosing the wheel version you want to test.
49
+ 4. Upload to PyPI using twine: ```python3 -m twine upload --repository pypi dist/*```
50
+ * You can specify specific files to upload, the line above pushes everything in dist directory.
@@ -11,9 +11,11 @@ This Python PyPI package is found [here](https://pypi.org/project/dsa-helpers/).
11
11
  1. Install requirements for building and distributing package:
12
12
  ```
13
13
  $ python3 -m pip install --upgrade build # for building package
14
- $ python3 -m pip install --upgrade build # for uploading to PyPI
14
+ $ python3 -m pip install --upgrade twine # for uploading to PyPI
15
15
  ```
16
16
  2. When ready to build:
17
17
  - Modify "pyproject.toml" file, change the "version" key to a different value than a version already pushed.
18
18
  - Run ```$ python3 -m build```. This will create a dist directory and put your new wheel and tar distribution files there.
19
- 3. To install locally for development: ```$ pip3 install dist/dsa_helpers-*.whl```, choosing the wheel version you want to test.
19
+ 3. To install locally for development: ```$ pip3 install dist/dsa_helpers-*.whl```, choosing the wheel version you want to test.
20
+ 4. Upload to PyPI using twine: ```python3 -m twine upload --repository pypi dist/*```
21
+ * You can specify specific files to upload, the line above pushes everything in dist directory.
@@ -5,4 +5,4 @@ from .imwrite import imwrite
5
5
  # Modules that should be available.
6
6
  # from . import girder_utils, image_utils, dash
7
7
 
8
- __all__ = ["girder_utils", "image_utils", "dash"]
8
+ __all__ = ["girder_utils", "image_utils", "dash", "ml"]
@@ -0,0 +1,2 @@
1
+ # Modules that can be imported.
2
+ __all__ = ["object_detection", "datasets", "metrics", "transforms"]
@@ -0,0 +1,33 @@
1
+ from torch.utils.data import Dataset
2
+ import pandas as pd
3
+ from PIL import Image
4
+ import numpy as np
5
+
6
+
7
+ class SegFormerSegmentationDataset(Dataset):
8
+ """PyTorch dataset class for semantic segmentation using HuggingFaces SegFormer
9
+ model.
10
+
11
+ """
12
+
13
+ def __init__(self, df: pd.DataFrame):
14
+ """Initiate an instance of the segmentation dataset.
15
+
16
+ Args:
17
+ df (pandas.DataFrame): A pandas DataFrame with columns "fp" and "mask_fp".
18
+
19
+ """
20
+ self.image_files = df["fp"].tolist()
21
+ self.mask_files = df["mask_fp"].tolist()
22
+ self.df = df # store potential metadata
23
+
24
+ def __len__(self):
25
+ return len(self.image_files)
26
+
27
+ def __getitem__(self, idx):
28
+ # Get the filepath to the image and its mask.
29
+ image_path = self.image_files[idx]
30
+ mask_path = self.mask_files[idx]
31
+
32
+ # Return image & mask as PIL images in a dictionary.
33
+ return {"pixel_values": Image.open(image_path), "label": Image.open(mask_path)}
@@ -0,0 +1,3 @@
1
+ from .SegFormerSegmentationDataset import SegFormerSegmentationDataset
2
+
3
+ __all__ = ["SegFormerSegmentationDataset", "utils"]
@@ -0,0 +1,42 @@
1
+ # Utility functions, usualy for creating Datasets in specific formats.
2
+ import pandas as pd
3
+ from .SegFormerSegmentationDataset import SegFormerSegmentationDataset
4
+ from datasets import Dataset, Features, Image
5
+
6
+
7
+ def dataset_generator(dataset):
8
+ """Yield a dataset."""
9
+ for item in dataset:
10
+ yield item
11
+
12
+
13
+ def create_segformer_segmentation_dataset(
14
+ df: pd.DataFrame | str, low_memory: bool = True, transforms=None
15
+ ):
16
+ """Create a SegFormer segmentation dataset from a DataFrame.
17
+
18
+ Args:
19
+ df (pd.DataFrame | str): A pandas DataFrame with columns "fp" and "mask_fp" or a
20
+ path to a CSV file.
21
+ low_memory (bool): Whether to read the CSV file in low memory mode.
22
+ transforms: A function that takes in a batch of samples (dictionary with
23
+ pixel_values and label keys) and returns a transformed batch.
24
+
25
+ Returns:
26
+ A Dataset object to be used for HuggingFaces SegFormer model training.
27
+
28
+ """
29
+ if isinstance(df, str):
30
+ df = pd.read_csv(df, low_memory=low_memory)
31
+
32
+ dataset = SegFormerSegmentationDataset(df)
33
+
34
+ dataset = Dataset.from_generator(
35
+ generator=lambda: dataset_generator(dataset),
36
+ features=Features({"pixel_values": Image(), "label": Image()}), # Example shape
37
+ )
38
+
39
+ if transforms is not None:
40
+ dataset = dataset.set_transform(transforms)
41
+
42
+ return dataset
@@ -0,0 +1,3 @@
1
+ from .binary_dice_coefficient_metric import binary_dice_coefficient_metric
2
+
3
+ __all__ = ["binary_dice_coefficient_metric"]
@@ -0,0 +1,22 @@
1
+ import torch
2
+ from torch import nn
3
+ from ...utils import binary_dice_coefficient
4
+
5
+
6
+ def binary_dice_coefficient_metric(eval_pred):
7
+ with torch.no_grad():
8
+ logits, labels = eval_pred # assumed both are numpy
9
+
10
+ logits_tensor = torch.from_numpy(logits)
11
+
12
+ # Scale the logits to the size of the label.
13
+ logits_tensor = nn.functional.interpolate(
14
+ logits_tensor,
15
+ size=labels.shape[-2:],
16
+ mode="bilinear",
17
+ align_corners=False,
18
+ ).argmax(dim=1)
19
+
20
+ pred_labels = logits_tensor.detach().cpu().numpy()
21
+
22
+ return {"dice_coefficient": binary_dice_coefficient(pred_labels, labels)}
@@ -0,0 +1 @@
1
+ __all__ = ["segformer_transforms"]
@@ -0,0 +1,23 @@
1
+ from transformers import SegformerImageProcessor
2
+ from torchvision.transforms import ColorJitter
3
+
4
+
5
+ def train_transforms(example_batch):
6
+ """Default transforms for training images."""
7
+ processor = SegformerImageProcessor() # required for using SegFormer model.
8
+ jitter = ColorJitter(brightness=0.25, contrast=0.25, saturation=0.25, hue=0.1)
9
+
10
+ images = [jitter(x) for x in example_batch["pixel_values"]]
11
+ labels = [x for x in example_batch["label"]]
12
+ inputs = processor(images, labels)
13
+ return inputs
14
+
15
+
16
+ def val_transforms(example_batch):
17
+ """Default transforms for validation images."""
18
+ processor = SegformerImageProcessor() # required for using SegFormer model.
19
+
20
+ images = [x for x in example_batch["pixel_values"]]
21
+ labels = [x for x in example_batch["label"]]
22
+ inputs = processor(images, labels)
23
+ return inputs
@@ -0,0 +1,28 @@
1
+ # Basic utility functions.
2
+ import numpy as np
3
+
4
+
5
+ def binary_dice_coefficient(mask1: np.ndarray, mask2: np.ndarray) -> float:
6
+ """Calculate the dice coefficient between two binary masks.
7
+
8
+ Args:
9
+ mask1 (np.ndarray): A binary mask.
10
+ mask2 (np.ndarray): A binary mask.
11
+
12
+ Returns:
13
+ float: The dice coefficient between the two masks.
14
+
15
+ """
16
+ # Convert the masks to binary.
17
+ mask1 = mask1 > 0
18
+ mask2 = mask2 > 0
19
+
20
+ # Calculate dice.
21
+ intersection = np.sum(mask1 * mask2)
22
+
23
+ sum_masks = np.sum(mask1) + np.sum(mask2)
24
+
25
+ if sum_masks == 0:
26
+ return 1.0 # Both masks are empty
27
+
28
+ return 2.0 * intersection / sum_masks
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: dsa_helpers
3
- Version: 0.4.dev3
3
+ Version: 0.4.dev4
4
4
  Summary: Utility functions for working with the DSA girder client.
5
5
  Author-email: "Juan C. Vizcarra" <jvizcar@emory.edu>
6
6
  Maintainer-email: "Juan C. Vizcarra" <jvizcar@emory.edu>
@@ -22,6 +22,10 @@ Requires-Dist: dash
22
22
  Requires-Dist: dash_bootstrap_components
23
23
  Requires-Dist: shapely
24
24
  Requires-Dist: geopandas
25
+ Requires-Dist: torch
26
+ Requires-Dist: datasets
27
+ Requires-Dist: transformers
28
+ Requires-Dist: torchvision
25
29
 
26
30
  # Digital Slide Archive (DSA) Helpers
27
31
  Digital Slide Archive Python utility library.
@@ -36,9 +40,11 @@ This Python PyPI package is found [here](https://pypi.org/project/dsa-helpers/).
36
40
  1. Install requirements for building and distributing package:
37
41
  ```
38
42
  $ python3 -m pip install --upgrade build # for building package
39
- $ python3 -m pip install --upgrade build # for uploading to PyPI
43
+ $ python3 -m pip install --upgrade twine # for uploading to PyPI
40
44
  ```
41
45
  2. When ready to build:
42
46
  - Modify "pyproject.toml" file, change the "version" key to a different value than a version already pushed.
43
47
  - Run ```$ python3 -m build```. This will create a dist directory and put your new wheel and tar distribution files there.
44
48
  3. To install locally for development: ```$ pip3 install dist/dsa_helpers-*.whl```, choosing the wheel version you want to test.
49
+ 4. Upload to PyPI using twine: ```python3 -m twine upload --repository pypi dist/*```
50
+ * You can specify specific files to upload, the line above pushes everything in dist directory.
@@ -3,7 +3,6 @@ LICENSE
3
3
  README.md
4
4
  app.py
5
5
  pyproject.toml
6
- readthedocs.yaml
7
6
  requirements.txt
8
7
  dsa_helpers/__init__.py
9
8
  dsa_helpers/girder_utils.py
@@ -12,6 +11,7 @@ dsa_helpers/imread.py
12
11
  dsa_helpers/imwrite.py
13
12
  dsa_helpers/login.py
14
13
  dsa_helpers/mongo_utils.py
14
+ dsa_helpers/utils.py
15
15
  dsa_helpers.egg-info/PKG-INFO
16
16
  dsa_helpers.egg-info/SOURCES.txt
17
17
  dsa_helpers.egg-info/dependency_links.txt
@@ -21,8 +21,18 @@ dsa_helpers/dash/__init__.py
21
21
  dsa_helpers/dash/header.py
22
22
  dsa_helpers/dash/header_callbacks.py
23
23
  dsa_helpers/ml/__init__.py
24
+ dsa_helpers/ml/datasets/SegFormerSegmentationDataset.py
25
+ dsa_helpers/ml/datasets/__init__.py
26
+ dsa_helpers/ml/datasets/utils.py
27
+ dsa_helpers/ml/metrics/__init__.py
28
+ dsa_helpers/ml/metrics/binary_dice_coefficient_metric.py
24
29
  dsa_helpers/ml/object_detection/__init__.py
25
30
  dsa_helpers/ml/object_detection/get_pearce_roi_images.py
26
31
  dsa_helpers/ml/object_detection/tile_image.py
27
32
  dsa_helpers/ml/object_detection/utils.py
28
- tests/test.ipynb
33
+ dsa_helpers/ml/transforms/__init__.py
34
+ dsa_helpers/ml/transforms/segformer_transforms.py
35
+ tests/docker-compose.yaml
36
+ tests/test.ipynb
37
+ tests/app/Dockerfile
38
+ tests/app/app.py
@@ -9,3 +9,7 @@ dash
9
9
  dash_bootstrap_components
10
10
  shapely
11
11
  geopandas
12
+ torch
13
+ datasets
14
+ transformers
15
+ torchvision
@@ -14,7 +14,11 @@ dependencies = [
14
14
  'dash',
15
15
  'dash_bootstrap_components',
16
16
  'shapely',
17
- 'geopandas'
17
+ 'geopandas',
18
+ "torch",
19
+ "datasets",
20
+ "transformers",
21
+ "torchvision"
18
22
  ]
19
23
  name = "dsa_helpers"
20
24
  authors = [
@@ -31,7 +35,7 @@ classifiers = [
31
35
  "License :: OSI Approved :: MIT License",
32
36
  ]
33
37
  keywords = ["dsa", "girder", "girder_client"]
34
- version = "v0.4dev3"
38
+ version = "v0.4dev4"
35
39
 
36
40
  [project.urls]
37
41
  Repository = "https://github.com/Gutman-Lab/dsa-python-utils"
@@ -8,4 +8,8 @@ jupyter
8
8
  pillow
9
9
  dash
10
10
  dash_bootstrap_components
11
- python-dotenv
11
+ python-dotenv
12
+ torch
13
+ datasets
14
+ transformers
15
+ torchvision
@@ -0,0 +1,18 @@
1
+ FROM python:3.11-slim-buster
2
+
3
+ ENV PYTHONUNBUFFERED True
4
+
5
+ RUN apt-get update && apt-get install ffmpeg libsm6 libxext6 -y
6
+
7
+ WORKDIR /app
8
+
9
+ COPY requirements.txt /app
10
+
11
+ RUN pip install --upgrade pip
12
+
13
+ RUN python -m pip install pymongo==4.9.1
14
+
15
+ RUN pip install --no-cache-dir -r requirements.txt
16
+
17
+ COPY dsa_helpers-0.4.dev1-py3-none-any.whl /app
18
+ RUN pip install dsa_helpers-0.4.dev1-py3-none-any.whl
@@ -0,0 +1,55 @@
1
+ from dash import html, Dash, callback, Output, Input, State, no_update
2
+ import dash_bootstrap_components as dbc
3
+ from dsa_helpers.dash.header import get_header
4
+ from os import getenv
5
+ from dash_ag_grid import AgGrid
6
+
7
+ table = AgGrid(
8
+ id="table",
9
+ columnDefs=[],
10
+ rowData=[],
11
+ )
12
+
13
+ app = Dash(
14
+ __name__,
15
+ external_stylesheets=[
16
+ dbc.themes.BOOTSTRAP,
17
+ ],
18
+ title="Test Application",
19
+ )
20
+
21
+ app.layout = html.Div(
22
+ [
23
+ get_header(
24
+ getenv("DSA_API_URL"),
25
+ title="Test Application",
26
+ store_id="user-store",
27
+ ),
28
+ dbc.Row(
29
+ [dbc.Col(dbc.Button("Get items", id="get-item-btn"), width="auto")],
30
+ justify="start",
31
+ ),
32
+ table
33
+ ]
34
+ )
35
+
36
+
37
+ @callback(
38
+ [
39
+ Output("table", "columnDefs"),
40
+ Output("table", "rowData"),
41
+ ],
42
+ Input("get-item-btn", "n_clicks"),
43
+ prevent_initial_call=True,
44
+ )
45
+ def get_items(n_clicks):
46
+ """Get the items."""
47
+
48
+
49
+
50
+ if __name__ == "__main__":
51
+ app.run(
52
+ host="0.0.0.0",
53
+ port=8060,
54
+ debug=True,
55
+ )
@@ -0,0 +1,31 @@
1
+ volumes:
2
+ mongodb-data-volume:
3
+
4
+ services:
5
+ mongodb:
6
+ image: mongo:7.0.6-jammy
7
+ restart: always
8
+ platform: linux/amd64
9
+ container_name: mongodb
10
+ volumes:
11
+ - mongodb-data-volume:/data/db
12
+ ports:
13
+ - 27017:27017
14
+ env_file: .env
15
+ command: [ "--quiet", "--maxConns", "1000" ]
16
+ logging:
17
+ options:
18
+ max-size: "10M"
19
+ max-file: "5"
20
+
21
+ dash:
22
+ build: app/
23
+ platform: linux/amd64
24
+ volumes:
25
+ - ./app:/app
26
+ ports:
27
+ - 8060:8060
28
+ env_file: .env
29
+ command: python app.py
30
+ depends_on:
31
+ - mongodb
@@ -1,2 +0,0 @@
1
- # Modules that can be imported.
2
- __all__ = ["object_detection"]
@@ -1,27 +0,0 @@
1
- # .readthedocs.yaml
2
- # Read the Docs configuration file
3
- # See https://docs.readthedocs.io/en/stable/config-file/v2.html for details
4
-
5
- # Required
6
- version: 2
7
-
8
- # Set the OS, Python version and other tools you might need
9
- build:
10
- tools:
11
- python: "3.9"
12
-
13
- # Build documentation in the "docs/" directory with Sphinx
14
- sphinx:
15
- configuration: docs/conf.py
16
-
17
- # Optionally build your docs in additional formats such as PDF and ePub
18
- # formats:
19
- # - pdf
20
- # - epub
21
-
22
- # Optional but recommended, declare the Python requirements required
23
- # to build your documentation
24
- # See https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html
25
- # python:
26
- # install:
27
- # - requirements: docs/requirements.txt
File without changes
File without changes
File without changes