Skip to content

Instantly share code, notes, and snippets.

View eladn's full-sized avatar

Elad Nachmias eladn

  • CS Faculty @ Technion
View GitHub Profile
@eladn
eladn / find_sampled_quadrilateral_vertices.py
Created January 14, 2024 18:38
Find four extreme vertices of a quadrilateral given a set of sampled points within it.
import numpy as np
import matplotlib
def find_extreme_vertices_of_sampled_quadrilateral(points: np.ndarray) -> np.ndarray: # points.shape: [:, 2]
assert points.ndim == 2 and points.shape[-1] == 2
min_x, min_y = min(points[:, 0]), min(points[:, 1])
max_x, max_y = max(points[:, 0]), max(points[:, 1])
is_min_x = np.isclose(points[:, 0], min_x)
min_y_at_min_x, max_y_at_min_x = min(points[is_min_x][..., 1]), max(points[is_min_x][..., 1])
@eladn
eladn / online_moments_accumulator_1d.py
Last active October 23, 2023 10:47
Python mean & variance moments accumulator; numerically stable; based on Welford's algorithm
__author__ = "Elad Nachmias"
__email__ = "eladnah@gmail.com"
__date__ = "2023-10-19"
import copy
import dataclasses
from typing import Tuple, Optional
import numpy as np
@eladn
eladn / producer_consumer_ray_queue.py
Last active October 21, 2023 17:18
Python ray queue for producer-consumer pattern; prefetching chunks for periodically consumption
__author__ = "Elad Nachmias"
__email__ = "eladnah@gmail.com"
__date__ = "2023-10-19"
import time
from typing import Generic, List, Optional, TypeVar
import asyncio
import threading
import ray
@eladn
eladn / concurrent_file_saver.py
Created October 19, 2023 08:45
Python util for saving file to disk concurrently in the background without blocking the main process
__author__ = "Elad Nachmias"
__email__ = "eladnah@gmail.com"
__date__ = "2023-10-19"
import threading
import queue
import time
import lzma
import pickle
from concurrent.futures import CancelledError
@eladn
eladn / memory_watchdog.py
Last active October 19, 2023 09:17
Python memory watchdog - Monitors a process' memory consumption; Gracefully triggers an exception in the monitored process if exceeds memory usage soft limit or kills the process if exceeds the hard limit
__author__ = "Elad Nachmias"
__email__ = "eladnah@gmail.com"
__date__ = "2023-10-18"
import multiprocessing
import os
import signal
import time
import psutil
import warnings
@eladn
eladn / access_nested_dict_by_dot_key.py
Created October 18, 2023 11:20
Python functions for accessing a nested dictionary by a dot-separated key for reading and setting values and creating missing dictionaries in nested levels.
__author__ = "Elad Nachmias"
__email__ = "eladnah@gmail.com"
__date__ = "2023-10-18"
from typing import Dict, Any
def access_nested_dict_by_dot_key(
dct: Dict, key: str, create_missing: bool = False, replace_nones: bool = False) -> Any:
"""
@eladn
eladn / recursively_structured_merge.py
Created October 18, 2023 10:56
Python function for recursively traversing structured objects for performing a merge of two given objects
__author__ = "Elad Nachmias"
__email__ = "eladnah@gmail.com"
__date__ = "2023-10-18"
import copy
from typing import Any, Tuple
import dataclasses
import numpy as np
@eladn
eladn / exception_info.py
Last active May 27, 2024 22:19
Python util for representing exception information as primitive values including tracebacks and local variables
__author__ = "Elad Nachmias"
__email__ = "eladnah@gmail.com"
__date__ = "2023-10-18"
import dataclasses
import itertools
import sys
import traceback
import linecache
from traceback import StackSummary, walk_tb, FrameSummary
@eladn
eladn / heapdict.py
Last active October 18, 2023 10:25
Python implementation for heap-dict, allowing accessing items by lookup-key for removal or modifying
__author__ = "Elad Nachmias"
__email__ = "eladnah@gmail.com"
__date__ = "2023-10-18"
from operator import lt
from dataclasses import dataclass
from typing import TypeVar, MutableMapping, Generic, List, Dict, Iterable, Tuple, Any, Callable, Optional, Collection
# Note: The key & value are kept as generic types.
@eladn
eladn / subsets_batch_sampler.py
Created October 18, 2023 10:15
PyTorch batch sampler for non-balanced subsets
__author__ = "Elad Nachmias"
__email__ = "eladnah@gmail.com"
__date__ = "2023-10-18"
import dataclasses
import enum
import itertools
import warnings
from collections import defaultdict, Counter
from typing import Optional, Dict, List, Sequence, Iterable, Union, Set, Tuple