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

Protect imports in script better #977

Merged
merged 1 commit into from
Nov 7, 2023
Merged
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
188 changes: 99 additions & 89 deletions romancal/scripts/static_preview.py
Original file line number Diff line number Diff line change
@@ -1,99 +1,109 @@
from pathlib import Path
from typing import Optional
from typing import TYPE_CHECKING, Optional

import asdf
import numpy
from typing_extensions import Annotated

try:
import typer
from stpreview.downsample import downsample_asdf_to
from stpreview.image import north_pole_angle, percentile_normalization, write_image
except (ImportError, ModuleNotFoundError):
raise ImportError(
'SDP requirements not installed; do `pip install "romancal[sdp]"`'
)

app = typer.Typer()


@app.command()
def preview(
input: Annotated[Path, typer.Argument(help="path to ASDF file with 2D image data")],
output: Annotated[
Optional[Path], typer.Argument(help="path to output image file")
] = None,
shape: Annotated[
Optional[tuple[int, int]],
typer.Argument(help="desired pixel resolution of output image"),
] = (1080, 1080),
compass: Annotated[
Optional[bool], typer.Option(help="whether to draw a north arrow on the image")
] = True,
):
"""
create a preview image with a north arrow overlay indicating orientation
"""

if output is None:
output = Path.cwd()
if output.is_dir():
output = output / f"{input.stem}.png"

with asdf.open(input) as file:
model = file["roman"]["meta"]["model_type"]
if "image" not in model.lower():
raise NotImplementedError(f'"{model}" model not supported')
wcs = file["roman"]["meta"]["wcs"]

data = downsample_asdf_to(input=input, shape=shape, func=numpy.nanmean)

write_image(
data,
output,
shape=shape,
normalization=percentile_normalization(data, percentile=90),
colormap="afmhot",
north_arrow_angle=north_pole_angle(wcs).degree - 90,
)


@app.command()
def thumbnail(
input: Annotated[Path, typer.Argument(help="path to ASDF file with 2D image data")],
output: Annotated[
Optional[Path], typer.Argument(help="path to output image file")
] = None,
shape: Annotated[
Optional[tuple[int, int]],
typer.Argument(help="desired pixel resolution of output image"),
] = (300, 300),
compass: Annotated[
Optional[bool], typer.Option(help="whether to draw a north arrow on the image")
] = False,
):
if output is None:
output = Path.cwd()
if output.is_dir():
output = output / f"{input.stem}_thumb.png"

with asdf.open(input) as file:
model = file["roman"]["meta"]["model_type"]
if "image" not in model.lower():
raise NotImplementedError(f'"{model}" model not supported')

data = downsample_asdf_to(input=input, shape=shape, func=numpy.nanmean)

write_image(
data,
output,
shape=shape,
normalization=percentile_normalization(data, percentile=90),
colormap="afmhot",
)

if TYPE_CHECKING:
from typing_extensions import Annotated


def command():
try:
import typer
from stpreview.downsample import downsample_asdf_to
from stpreview.image import (
north_pole_angle,
percentile_normalization,
write_image,
)
except (ImportError, ModuleNotFoundError):
raise ImportError(
'SDP requirements not installed; do `pip install "romancal[sdp]"`'
)

app = typer.Typer()

@app.command()
def preview(
input: Annotated[
Path, typer.Argument(help="path to ASDF file with 2D image data")
],
output: Annotated[
Optional[Path], typer.Argument(help="path to output image file")
] = None,
shape: Annotated[
Optional[tuple[int, int]],
typer.Argument(help="desired pixel resolution of output image"),
] = (1080, 1080),
compass: Annotated[
Optional[bool],
typer.Option(help="whether to draw a north arrow on the image"),
] = True,
):
"""
create a preview image with a north arrow overlay indicating orientation
"""

if output is None:
output = Path.cwd()
if output.is_dir():
output = output / f"{input.stem}.png"

with asdf.open(input) as file:
model = file["roman"]["meta"]["model_type"]
if "image" not in model.lower():
raise NotImplementedError(f'"{model}" model not supported')
wcs = file["roman"]["meta"]["wcs"]

data = downsample_asdf_to(input=input, shape=shape, func=numpy.nanmean)

write_image(
data,
output,
shape=shape,
normalization=percentile_normalization(data, percentile=90),
colormap="afmhot",
north_arrow_angle=north_pole_angle(wcs).degree - 90,
)

@app.command()
def thumbnail(
input: Annotated[
Path, typer.Argument(help="path to ASDF file with 2D image data")
],
output: Annotated[
Optional[Path], typer.Argument(help="path to output image file")
] = None,
shape: Annotated[
Optional[tuple[int, int]],
typer.Argument(help="desired pixel resolution of output image"),
] = (300, 300),
compass: Annotated[
Optional[bool],
typer.Option(help="whether to draw a north arrow on the image"),
] = False,
):
if output is None:
output = Path.cwd()
if output.is_dir():
output = output / f"{input.stem}_thumb.png"

with asdf.open(input) as file:
model = file["roman"]["meta"]["model_type"]
if "image" not in model.lower():
raise NotImplementedError(f'"{model}" model not supported')

data = downsample_asdf_to(input=input, shape=shape, func=numpy.nanmean)

write_image(
data,
output,
shape=shape,
normalization=percentile_normalization(data, percentile=90),
colormap="afmhot",
)

app()


Expand Down