Skip to content

Instantly share code, notes, and snippets.

View adriancaruana's full-sized avatar
🤔
Thinking

Adrian Caruana adriancaruana

🤔
Thinking
View GitHub Profile
@adriancaruana
adriancaruana / estimate_from_histogram.py
Last active January 12, 2024 05:38
Estimate the percentile and ratio values of a distribution from its histogram
import numpy as np
from numpy.typing import ArrayLike, NDArray
def clip_histogram(histogram: NDArray, bin_edges: NDArray, threshold: float):
# Apply threshold to the distribution to exclude all values below the threshold. Note: Since
# we're using histograms, we might have the threshold fall within a bin, so we need to
# interpolate the value of the bin to the threshold, and artificially reduce the ratio for
# the bin by the fractional amount of the bin that's below the threshold.
@adriancaruana
adriancaruana / get_package_versions.py
Created November 29, 2022 00:28
This gist can be used to get the major and minor versions of pip-installed packages in your environment.
#!/usr/bin/python3
# Generate requirements.txt automatically from live environment
# Run like this: ./get_package_versions.py >> requirements.txt
# Requires pip>=22.* (if fails, try: python3 -m pip install --upgrade pip)
from pip._internal.metadata import get_default_environment
wildcard_bugfix = lambda ver: ".".join(ver.split(".")[:2]) + ".*"
reqs = "\n".join(
f"{package.metadata_dict['name']}=={wildcard_bugfix(package.metadata_dict['version'])}"
for package in sorted(list(get_default_environment().iter_installed_distributions()), key=lambda x: str(x).lower())
)