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

Update nrtk and pybsm version #157

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ For more details on setting up a development environment see [DEVELOPMENT docs](
1. Merge `main` to `release` with a _merge commit_.
2. Run "Create Release" workflow with workflow from `release` branch.
3. Merge `release` to `main` with a _merge commit_.
4. Check package versions in Conda Feedstock [meta.yaml file](https://github.com/conda-forge/nrtk-explorer-feedstock/blob/main/recipe/meta.yaml)

[1]: https://trame.readthedocs.io/en/latest/
[2]: https://www.kitware.com/
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ dependencies = [
"accelerate",
"numpy",
"Pillow",
"pybsm>=0.6,<=0.9.0",
"pybsm==0.10.2",
"scikit-learn>=1.6.0",
"smqtk_image_io",
"tabulate",
Expand All @@ -42,7 +42,7 @@ dependencies = [
"transformers",
"datasets[vision]",
"umap-learn",
"nrtk[headless]>=0.12.0,<=0.16.0",
"nrtk[headless]==0.18.0",
"trame-annotations>=0.4.0",
]

Expand Down
4 changes: 0 additions & 4 deletions src/nrtk_explorer/app/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from trame_server import Server

import nrtk_explorer.library.transforms as trans
import nrtk_explorer.library.nrtk_transforms as nrtk_trans
import nrtk_explorer.library.yaml_transforms as nrtk_yaml
from nrtk_explorer.library import object_detector
from nrtk_explorer.library.app_config import process_config
Expand Down Expand Up @@ -173,9 +172,6 @@ def delete_meta_state(old_ids, new_ids):
"identity": trans.IdentityTransform,
}

if nrtk_trans.nrtk_transforms_available():
self._transform_classes["nrtk_pybsm"] = nrtk_trans.NrtkPybsmTransform

# Add transform from YAML definition
self._transform_classes.update(nrtk_yaml.generate_transforms())

Expand Down
72 changes: 1 addition & 71 deletions src/nrtk_explorer/library/nrtk_transforms.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,14 @@
from typing import Any, Dict, Optional, TYPE_CHECKING

import numpy as np
import logging
from PIL import Image as ImageModule
from PIL.Image import Image
from nrtk_explorer.library.transforms import ImageTransform, ParameterDescription

ENABLED_NRTK_TRANSFORMS = True

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

try:
from pybsm.otf import dark_current_from_density
from nrtk.impls.perturb_image.pybsm.perturber import PybsmPerturber, PybsmSensor, PybsmScenario
from nrtk.impls.perturb_image.pybsm.perturber import PybsmSensor, PybsmScenario
except ImportError:
logger.info("Disabling NRTK transforms due to missing library/failing imports")
ENABLED_NRTK_TRANSFORMS = False

if TYPE_CHECKING:
PybsmPerturberType = PybsmPerturber
else:
PybsmPerturberType = None

PybsmPerturberArg = Optional[PybsmPerturberType]


def nrtk_transforms_available():
return ENABLED_NRTK_TRANSFORMS


# copied from https://github.com/Kitware/nrtk/blob/main/tests/impls/test_pybsm_utils.py
Expand Down Expand Up @@ -144,54 +125,3 @@ def create_sample_scenario():

def create_sample_sensor_and_scenario():
return dict(sensor=create_sample_sensor(), scenario=create_sample_scenario())


class NrtkPybsmTransform(ImageTransform):
def __init__(self, perturber: PybsmPerturberArg = None):
if perturber is None:
kwargs = create_sample_sensor_and_scenario()
perturber = PybsmPerturber(**kwargs)

self._perturber: PybsmPerturber = perturber

def get_parameters(self) -> dict[str, Any]:
return {
"D": self._perturber.sensor.D,
"f": self._perturber.sensor.f,
}

def set_parameters(self, params: Dict[str, Any]):
self._perturber.sensor.D = params["D"]
self._perturber.sensor.f = params["f"]

@classmethod
def get_parameters_description(cls) -> Dict[str, ParameterDescription]:
aperture_description: ParameterDescription = {
"type": "float",
"label": "Effective Aperture (m)",
"default": None,
"description": None,
"options": None,
}

focal_description: ParameterDescription = {
"type": "float",
"label": "Focal Length (m)",
"default": None,
"description": None,
"options": None,
}

return {
"D": aperture_description,
"f": focal_description,
}

def execute(self, input: Image, *input_args: Any) -> Image:
if len(input_args) == 0:
input_args = ({"img_gsd": 0.15},)

input_array = np.asarray(input)
output_array = self._perturber.perturb(input_array, *input_args)

return ImageModule.fromarray(output_array)
4 changes: 2 additions & 2 deletions src/nrtk_explorer/library/nrtk_transforms.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ nrtk_pybsm_detector_otf:
type: float
label: Focal length (m)

nrtk_pybsm_2:
nrtk_pybsm:
perturber: nrtk.impls.perturb_image.pybsm.perturber.PybsmPerturber
perturber_kwargs: nrtk_explorer.library.nrtk_transforms.create_sample_sensor_and_scenario
exec_default_args: [{ img_gsd: 0.15 }]
exec_default_args: [None, { img_gsd: 0.15 }]
description:
D:
_path: [sensor, D]
Expand Down
2 changes: 1 addition & 1 deletion src/nrtk_explorer/library/yaml_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,6 @@ def execute(self, input, *input_args):
input_args = self.exec_args

input_array = np.asarray(input)
output_array = self._perturber.perturb(input_array, *input_args)
output_array, unknown = self._perturber.perturb(input_array, *input_args)

return ImageModule.fromarray(output_array)
2 changes: 1 addition & 1 deletion tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ def test_gaussian_blur():

def test_pybsm():
transforms = generate_transforms()
pybsm = transforms["nrtk_pybsm_2"]()
pybsm = transforms["nrtk_pybsm"]()
pybsm.set_parameters({"D": 0.25, "f": 4.0})
pybsm.execute(get_image())
Loading