Skip to content

Instantly share code, notes, and snippets.

View ThomasParistech's full-sized avatar
🚴

Thomas Rouch ThomasParistech

🚴
View GitHub Profile
import numpy as np
from scipy.stats import norm
def weighted_kde(x_data: np.ndarray, x_prediction: np.ndarray) -> np.ndarray:
h = silverman_bandwidth(x_data) # Required to evaluate CDF
area_values = norm.cdf(1.0, x_prediction, h) - norm.cdf(0.0, x_prediction, h)
basic_densities = basic_kde(x_data, x_prediction, h)
return basic_densities / area_values
import numpy as np
def reflective_kde(x_data: np.ndarray, x_prediction: np.ndarray) -> np.ndarray:
h = silverman_bandwidth(x_data) # Compute before adding reflected data
x_data_augmented = np.stack((-x_data, x_data, 2-x_data))
reflective_densities = basic_kde(x_data_augmented, x_prediction, h)
# Discard left and right reflected samples and normalize density by 1/3
return 3 * reflective_densities
import numpy as np
from scipy.special import logit
def transformed_kde(x_data: np.ndarray, x_prediction: np.ndarray) -> np.ndarray:
x_data_logit = logit(x_data)
x_prediction_logit = logit(x_prediction)
densities_logit = basic_kde(x_data_logit, x_prediction_logit)
return densities_logit / (x_prediction * (1.0-x_prediction))
@ThomasParistech
ThomasParistech / basic_kde.py
Last active February 25, 2024 23:05
basic_kde
import numpy as np
from scipy.stats import norm
def silverman_bandwidth(x_data: np.ndarray) -> float:
return (4/(3*x_data.shape[0]))**0.2 * np.std(x_data)
def basic_kde(x_data: np.ndarray, x_prediction: np.ndarray) -> np.ndarray:
"""Perform Gaussian Kernel Density Estimation.
import time
from multiprocessing.sharedctypes import Synchronized
from multiprocessing.synchronize import Event as EventClass
import nvidia_smi # nvidia-ml-py3
from medium_utils import get_pid_ram_used_bytes
from medium_utils import get_total_ram_available_bytes
from medium_utils import get_total_ram_used_bytes
import inspect
import os
from functools import wraps
from typing import Any
from typing import Callable
from typing import cast
from typing import TypeVar
def get_function_name(func: Callable) -> str:
import os
from multiprocessing import Event
from multiprocessing import Process
from multiprocessing import Value
from multiprocessing.sharedctypes import Synchronized
from typing import Optional
class MemoryScope:
"""Use as context `with MemoryScope(name):`."""
def bytes2human(number: int, decimal_unit: bool = True) -> str:
"""Convert number of bytes in a human readable string.
>>> bytes2human(10000, True)
'10.00 KB'
>>> bytes2human(10000, False)
'9.77 KiB'
Args:
number (int): Number of bytes
from functools import lru_cache
import nvidia_smi
@lru_cache()
def is_gpu_available() -> bool:
"""Check if nvidia-smi is available."""
try:
nvidia_smi.nvmlInit()
nvidia_smi.nvmlShutdown()
import nvidia_smi
nvidia_smi.nvmlInit()
assert nvidia_smi.nvmlDeviceGetCount() == 1 # Assume a single GPU
handle = nvidia_smi.nvmlDeviceGetHandleByIndex(0)
gpu_info = nvidia_smi.nvmlDeviceGetMemoryInfo(handle)
total_vram = int(gpu_info.total)
used_vram = int(gpu_info.used)