Skip to content

Instantly share code, notes, and snippets.

View tsvikas's full-sized avatar

Tsvika Shapira tsvikas

View GitHub Profile
@tsvikas
tsvikas / safe_pandas_concats.py
Last active December 23, 2024 15:49
safe predictable pd.concat
import typing
from collections.abc import Iterable, Mapping
import pandas as pd
HashableT = typing.TypeVar("HashableT", bound=typing.Hashable)
T = TypeVar("T", pd.Series, pd.DataFrame)
@tsvikas
tsvikas / analyze data from llm-arena
Last active October 31, 2024 10:38
download and analyze data from llm-arena
# this file uses a leading space in its name, to give the gist a nice name
this gist is some python code to reproduce the LLM arena leaderboard
it is based on https://colab.research.google.com/drive/1KdwokPjirkTmpO_P1WByFNFiqxWQquwH
but convert the huge json it uses to a smaller parquet file
@tsvikas
tsvikas / generate_images.py
Last active July 4, 2024 01:38
generate images with dall-e
"""
Generate images using Dall-E, save the parameters and the output easily to file.
Usage:
```python
import os; os.environ["OPENAI_API_KEY"] = "SECRET_KEY" # set your API key
from generate_images import GeneratedImage, GeneratedImagesFile # import this code
img = GeneratedImage.generate("Astronaut") # generate an image
img # display the generated image + metadata in Jupyter
img.save_image("astronaut.png") # save a specific image
@tsvikas
tsvikas / github_contributor_search.py
Last active December 23, 2024 15:50
check if a user is a contributor to a github repo
import argparse
import requests
def check_contributors(repo, username, token):
_owner, _repo_name = repo.split("/")
base_url = (
"https://api.github.com/repos/{}/contributors?anon=1&per_page=100&page={}"
)
headers = {
@tsvikas
tsvikas / joblib_parallel_with_tqdm.py
Last active January 29, 2025 00:41
joblib.Parallel, but with a tqdm progressbar
import tqdm
from joblib import Parallel, delayed
class ParallelTqdm(Parallel):
"""joblib.Parallel, but with a tqdm progressbar
Additional parameters:
----------------------
n_jobs: int, default: 0
@tsvikas
tsvikas / git_emojies
Last active February 11, 2025 18:01
like https://gitmoji.dev/ , but split into commit types
<!---
commit types from: https://github.com/conventional-changelog/commitlint/tree/master/%40commitlint/config-conventional
and https://commitlint.js.org/reference/prompt.html
-->
# API changes
## feat: A new feature
✨:sparkles:Introduce new features.
💄:lipstick:Add or update the UI and style files.
📈:chart-with-upwards-trend:Add or update analytics or track code.
@tsvikas
tsvikas / rl_probe_enviroments.py
Last active December 23, 2024 15:52
Gym debugging enviroments from Andy Jones's blog [https://andyljones.com/posts/rl-debugging.html]
"""
based on https://andyljones.com/posts/rl-debugging.html
Documentation is quoted from that blogpost.
The usual advice to people writing RL algorithms is to use a simple environment
like the classic control ones from the Gym. Thing is, these envs have the same
problem as looking at loss curves: at best they give you a noisy indicator, and
if the noisy indicator looks poor you don't know why it looks poor. They don't
localise errors.
@tsvikas
tsvikas / multiprocess_pool_updates.py
Created January 18, 2023 21:26
tools like multiprocess.map()
import multiprocessing as mp
import queue
import signal
from functools import partial
from typing import Callable, Iterable
def get_queue() -> queue.Queue:
"""
return a multi-processing queue
@tsvikas
tsvikas / pathgroup.py
Last active January 18, 2023 21:48
PathGroup is a group of python Path objects
from itertools import chain
from pathlib import Path
class PathGroup(list):
def __getitem__(self, y):
res = super().__getitem__(y)
return PathGroup(res) if (type(res) == list) else res
def glob(self, pattern):
return PathGroup(chain.from_iterable(sorted(p.glob(pattern)) for p in self))
def __getattr__(self, name):
@tsvikas
tsvikas / xr_modules.py
Created January 20, 2020 14:59
register a module (i.e. ndimage) as dataarray accessor
import functools
import xarray as xr
def register_dataarray_module(module, accessor_name=None):
if accessor_name is None:
accessor_name = module.__name__.split(".")[-1]
callable_funcs = [name for name in dir(module) if callable(getattr(module, name))]