Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/vandeplaslab/koyo
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz-migas committed Dec 9, 2024
2 parents b2ca187 + 4fac5e7 commit 1ec847c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/koyo/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,19 @@ def reshape_array_from_coordinates(

def flatten_array_from_coordinates(array: np.ndarray, coordinates: np.ndarray, offset: int = 0) -> np.ndarray:
"""Flatten array based on xy coordinates."""
try:
return array[coordinates[:, 1] - offset, coordinates[:, 0] - offset]
except IndexError:
return array[coordinates[:, 0] - offset, coordinates[:, 1] - offset]
if array.ndim == 2:
try:
return array[coordinates[:, 1] - offset, coordinates[:, 0] - offset]
except IndexError:
return array[coordinates[:, 0] - offset, coordinates[:, 1] - offset]
else:
try:
res = array[:, coordinates[:, 1] - offset, coordinates[:, 0] - offset]
except IndexError:
res = array[:, coordinates[:, 0] - offset, coordinates[:, 1] - offset]
# need to swap axes
res = np.swapaxes(res, 0, 1)
return res


def reshape_array_batch(
Expand Down
12 changes: 12 additions & 0 deletions src/koyo/rand.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
"""Random number utilities."""

from __future__ import annotations

from contextlib import contextmanager

import numpy as np
Expand All @@ -8,6 +12,14 @@ def get_random_seed() -> int:
return np.random.randint(0, np.iinfo(np.int32).max - 1, 1)[0]


def get_random_state(n: int = 1) -> int | list[int]:
"""Retrieve random state(s)."""
from random import randint

state = [randint(0, 2**32 - 1) for _ in range(n)]
return state if n > 1 else state[0]


@contextmanager
def temporary_seed(seed: int, skip_if_negative_one: bool = False):
"""Temporarily set numpy seed."""
Expand Down
16 changes: 16 additions & 0 deletions src/koyo/system.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""System utilities."""

import inspect
import os
import platform
import sys
Expand Down Expand Up @@ -81,3 +82,18 @@ def get_cli_path(name: str, env_key: str = "", default: str = "") -> str:
if default:
return default
raise RuntimeError(f"Could not find '{name}' executable.")


def who_called_me() -> tuple[str, str, int]:
"""Get the file name, function name, and line number of the caller."""
# Get the current frame
current_frame = inspect.currentframe()
# Get the caller's frame
caller_frame = current_frame.f_back

# Extract file name, line number, and function name
file_name = caller_frame.f_code.co_filename
line_number = caller_frame.f_lineno
function_name = caller_frame.f_code.co_name
print(f"Called from file: {file_name}, function: {function_name}, line: {line_number}")
return file_name, function_name, line_number

0 comments on commit 1ec847c

Please sign in to comment.