Skip to content

Commit

Permalink
created default package with less dependencies (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
bengtl authored Aug 5, 2024
1 parent 1c0bc8f commit f7ce613
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 37 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.venv
tests/__pycache__/
src/microjson/__pycache__/
poetry.lock
34 changes: 26 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,36 @@ where = ["src"]

[tool.poetry.dependencies]
python = ">=3.9.15,<3.12"
bfio = {version = "2.3.6", extras = ["all"]}
scyjava = "^1.9.1"
typer = "^0.7.0"
scipy = ">=1.8,<1.9.2"
filepattern = "^2.0.1"
fastapi = "^0.103.1"
llvmlite = "^0.40.1"
vaex = "^4.17.0"
pydantic = "^2.3.0"
scikit-image = "0.20.0"
geojson-pydantic = "^1.0.2"


[tool.poetry.dependencies.bfio]
version = "2.3.6"
extras = ["all"]
optional = true

[tool.poetry.dependencies.scipy]
version = ">=1.8,<1.9.2"
optional = true

[tool.poetry.dependencies.filepattern]
version = "^2.0.1"
optional = true

[tool.poetry.dependencies.vaex]
version = "^4.17.0"
optional = true

[tool.poetry.extras]
all = [
"bfio",
"scipy",
"filepattern",
"vaex"
]

[tool.poetry.dev-dependencies]
pytest = "^6.2.4"
nox = "^2022.1.7"
Expand Down
4 changes: 1 addition & 3 deletions src/microjson/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from .model import MicroJSON, GeoJSON
from .utils import gather_example_files
from .utils import OmeMicrojsonModel
from .utils import MicrojsonBinaryModel


__version__ = "0.1.11"
15 changes: 15 additions & 0 deletions src/microjson/fileutils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import os

def gather_example_files(directory) -> list:
"""
Gather all the .json files in a directory and its subdirectories.
"""
files = []
# Walk through the directory
for dirpath, _, filenames in os.walk(directory):
# Filter to just the .json files
example_files = [
os.path.join(dirpath, f) for f in filenames if f.endswith(".json")
]
files.extend(example_files)
return files
35 changes: 16 additions & 19 deletions src/microjson/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,43 @@
import json
import logging
import numpy as np
import vaex

import re
import shutil
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import as_completed
from itertools import product
from scipy import ndimage

import skimage as sk
from skimage import measure
from skimage import morphology
from bfio import BioReader

from sys import platform
from pathlib import Path
from typing import Any
from typing import Union
import microjson.model as mj
from multiprocessing import cpu_count
import filepattern as fp

import warnings
from microjson import MicroJSON
from pydantic import ValidationError
from microjson.model import Feature, Properties
from typing import Union
import pydantic

#define conditional imports
try:
from bfio import BioReader
import filepattern as fp
from scipy import ndimage
import vaex
except ImportError as e:
print("""Packages bfio, filepattern, scipy, vaex not installed
please install using pip install microjson[all]""")
raise e


warnings.filterwarnings("ignore")

logger = logging.getLogger(__name__)
Expand All @@ -41,21 +53,6 @@
NUM_THREADS = max(cpu_count() // 2, 2)


def gather_example_files(directory) -> list:
"""
Gather all the .json files in a directory and its subdirectories.
"""
files = []
# Walk through the directory
for dirpath, _, filenames in os.walk(directory):
# Filter to just the .json files
example_files = [
os.path.join(dirpath, f) for f in filenames if f.endswith(".json")
]
files.extend(example_files)
return files


class OmeMicrojsonModel:
"""Generate JSON of segmentations polygon using microjson python package.
Expand Down
2 changes: 1 addition & 1 deletion tests/test_geojson_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest
from jsonschema import validate

from microjson.utils import gather_example_files
from microjson.fileutils import gather_example_files

# Load the generated GeoJSON schema
with open("geojson_schema.json") as f:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_microjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pytest
from pydantic import ValidationError
from microjson.model import MicroJSON, GeoJSON
from microjson.utils import gather_example_files
from microjson.fileutils import gather_example_files


# Define the directories containing the example JSON files
Expand Down
2 changes: 1 addition & 1 deletion tests/test_original_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import jsonschema
import pytest
from jsonschema import validate
from microjson.utils import gather_example_files
from microjson.fileutils import gather_example_files

# Load the original GeoJSON schema
with open("external/GeoJSON.json") as f:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_provenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pytest
from pydantic import ValidationError
from microjson.model import MicroJSON, GeoJSON
from microjson.utils import gather_example_files
from microjson.fileutils import gather_example_files


# Define the directories containing the example JSON files
Expand Down
2 changes: 1 addition & 1 deletion tests/test_tilejson.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pytest
from pydantic import ValidationError
from microjson.tile import TileJSON
from microjson.utils import gather_example_files
from microjson.fileutils import gather_example_files


# Define the directories containing the example JSON files
Expand Down
29 changes: 27 additions & 2 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,35 @@
from typing import Union
import numpy as np
import pytest
from microjson.utils import OmeMicrojsonModel, MicrojsonBinaryModel
import skimage as sk
HAS_BFIO = False
try:
import bfio
HAS_BFIO = True
except ImportError:
pass

# Marker registration
def pytest_configure(config): # Function to register marker
config.addinivalue_line("markers", "requires_extra(name): mark test as needing an extra")


def clean_directories() -> None:
"""Remove all temporary directories."""
for d in Path(".").cwd().iterdir():
if d.is_dir() and d.name.startswith("tmp"):
shutil.rmtree(d)

@pytest.fixture(autouse=True)
def skip_if_extra_not_installed(request):
marker = request.node.get_closest_marker("requires_extra")
if marker:
extra_name = marker.args[0]
try:
__import__(extra_name)
except ImportError:
pytest.skip(f"Skipping test because '{extra_name}' extra is not installed.")


@pytest.fixture()
def inp_dir() -> Union[str, Path]:
Expand Down Expand Up @@ -58,8 +78,9 @@ def get_params(request: pytest.FixtureRequest) -> pytest.FixtureRequest:
"""To get the parameter of the ome to json."""
return request.param


@pytest.mark.skipif(not HAS_BFIO, reason="Skipping test because 'bfio' extra is not installed.")
def test_OmeMicrojsonModel(synthetic_images: Path, output_directory: Path, get_params) -> None:
from microjson.utils import OmeMicrojsonModel
"""Testing of converting binary images to microjson of objects polygon coordinates."""

inp_dir = synthetic_images
Expand All @@ -77,7 +98,9 @@ def test_OmeMicrojsonModel(synthetic_images: Path, output_directory: Path, get_p

clean_directories()

@pytest.mark.skipif(not HAS_BFIO, reason="Skipping test because 'bfio' extra is not installed.")
def test_MicrojsonBinaryModel(synthetic_images: Path, output_directory: Path, get_params:list[str]) -> None:
from microjson.utils import OmeMicrojsonModel, MicrojsonBinaryModel
"""Testing of converting microjson of polygon coordinates of objects back to binary images."""

inp_dir = synthetic_images
Expand All @@ -103,7 +126,9 @@ def test_MicrojsonBinaryModel(synthetic_images: Path, output_directory: Path, ge
def XOR(x, y):
return x ^ y

@pytest.mark.skipif(not HAS_BFIO, reason="Skipping test because 'bfio' extra is not installed.")
def test_roundtrip(synthetic_images: Path, output_directory: Path) -> None:
from microjson.utils import OmeMicrojsonModel, MicrojsonBinaryModel
"""Testing of reconstructed images from polygon coordinates with original images."""

inp_dir = synthetic_images
Expand Down

0 comments on commit f7ce613

Please sign in to comment.