Skip to content

Commit

Permalink
refactor: add type hints for literal strings (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
yxlao authored Jan 10, 2025
1 parent 9a3410d commit dd474ab
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions camtools/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import cv2
from . import sanity
from . import colormap
from typing import Tuple, List, Optional, Union
from typing import Tuple, List, Optional, Union, Literal
from jaxtyping import Float, UInt8, UInt16, Int


Expand Down Expand Up @@ -778,7 +778,7 @@ def get_scales(im_height, max_lines, font, line_text_h_ratio):

def vstack_images(
ims: List[Float[np.ndarray, "h w 3"]],
alignment: str = "left",
alignment: Literal["left", "center", "right"] = "left",
background_color: Tuple[float, float, float] = (1.0, 1.0, 1.0),
) -> Float[np.ndarray, "h_stacked w_stacked 3"]:
"""
Expand Down
4 changes: 2 additions & 2 deletions camtools/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import cv2
import numpy as np
from pathlib import Path
from typing import Union, Optional
from typing import Union, Optional, Literal
from jaxtyping import UInt8, Float

from . import sanity
Expand Down Expand Up @@ -185,7 +185,7 @@ def imwrite_depth(

def imread(
im_path: Union[str, Path],
alpha_mode: Optional[str] = None,
alpha_mode: Optional[Literal["keep", "ignore", "white", "black"]] = None,
) -> Union[
Float[np.ndarray, "h w"],
Float[np.ndarray, "h w 3"],
Expand Down
4 changes: 2 additions & 2 deletions camtools/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from skimage.metrics import peak_signal_noise_ratio
from skimage.metrics import structural_similarity
from pathlib import Path
from typing import Tuple, Optional, Union
from typing import Tuple, Optional, Union, Literal
from jaxtyping import Float

from . import image
Expand Down Expand Up @@ -200,7 +200,7 @@ def load_im_pd_im_gt_im_mask_for_eval(
im_pd_path: Union[str, Path],
im_gt_path: Union[str, Path],
im_mask_path: Optional[Union[str, Path]] = None,
alpha_mode: str = "white",
alpha_mode: Literal["white", "keep"] = "white",
) -> Tuple[
Float[np.ndarray, "h w 3"], Float[np.ndarray, "h w 3"], Float[np.ndarray, "h w"]
]:
Expand Down
12 changes: 6 additions & 6 deletions camtools/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Functions for controlled rendering of 3D geometries to images or depth images.
"""

from typing import List, Tuple, Optional
from typing import List, Tuple, Optional, Literal

import numpy as np
import open3d as o3d
Expand Down Expand Up @@ -422,7 +422,7 @@ class _TextRenderer:
"mono": None,
}

def __init__(self, font_type: str = "tex"):
def __init__(self, font_type: Literal["tex", "serif", "sans", "mono"] = "tex"):
"""
Initializes the renderer with a specific font type.
"""
Expand Down Expand Up @@ -543,10 +543,10 @@ def render(
def render_text(
text: str,
font_size: int = 72,
font_type: str = "tex",
font_type: Literal["tex", "serif", "sans", "mono"] = "tex",
font_color: Tuple[float, float, float] = (0, 0, 0),
tight_layout: bool = False,
multiline_alignment: str = "left",
multiline_alignment: Literal["left", "center", "right"] = "left",
padding_tblr: Tuple[int, int, int, int] = (0, 0, 0, 0),
) -> Float[np.ndarray, "h w"]:
"""
Expand Down Expand Up @@ -604,9 +604,9 @@ def render_text(
def render_texts(
texts: List[str],
font_size: int = 72,
font_type: str = "tex",
font_type: Literal["tex", "serif", "sans", "mono"] = "tex",
font_color: Tuple[float, float, float] = (0.0, 0.0, 0.0),
multiline_alignment: str = "center",
multiline_alignment: Literal["left", "center", "right"] = "center",
same_height: bool = False,
same_width: bool = False,
padding_tblr: Tuple[int, int, int, int] = (0, 0, 0, 0),
Expand Down

0 comments on commit dd474ab

Please sign in to comment.