Skip to content

Instantly share code, notes, and snippets.

View clbarnes's full-sized avatar

Chris Barnes clbarnes

View GitHub Profile
@clbarnes
clbarnes / copy_cache.py
Created January 18, 2023 16:53
functools.lru_cache wrapper which copies cached return values
from functools import lru_cache, wraps
from typing import Optional, Callable
from copy import copy, deepcopy
def copy_cache(deep: bool = True, maxsize: Optional[int] = 128, typed: bool = False):
"""Decorator factory wrapping functools.lru_cache, which copies return values.
Use this for functions with mutable return values.
@clbarnes
clbarnes / dfbuilder.py
Created October 10, 2022 14:26
Build dataframes row-wise in an ergonomic and reasonably efficient way
#!/usr/bin/env python3
"""Module for building pandas DataFrames by row."""
from collections.abc import Sequence, Collection
import typing as tp
import pandas as pd
class DataFrameBuilder:
def __init__(
@clbarnes
clbarnes / les.sh
Created May 17, 2022 10:21
les: ls if it's a directory, less if it's a file
#!/bin/sh
set -e
if [ $# -eq 0 ]; then
>&2 echo "No argument given, using ls"
exec ls
elif [ -d "$1" ]; then
>&2 echo "Directory detected, using ls"
exec ls "$1"
elif [ -f "$1" ]; then
>&2 echo "File detected, using less"
@clbarnes
clbarnes / makepool.py
Last active January 3, 2024 11:06
Make a zpool command for creating large pools
#!/usr/bin/env python3
"""
Make a `zpool create ...` command for a set of identical disks,
calculating vdev size etc. from the given configuration.
You will need to know the disk model name;
you should be able to identify it from the output of
`lsblk -d -e 7 -o NAME,SIZE,STATE,MODEL`.
"""
import getpass
from argparse import ArgumentParser
@clbarnes
clbarnes / neuron_n5_copy.py
Created February 7, 2022 13:53
Copy a minimum number of N5 chunks to visualise a navis neuron of interest
#%%
import os
import shutil
from pathlib import Path
import navis
import numpy as np
from scipy.ndimage import binary_fill_holes
# in nm, for converting world coords to voxel indices
resolution = np.array([3.8, 3.8, 50])
@clbarnes
clbarnes / add_downscales.py
Last active May 17, 2022 10:53
DEPRECATED, use https://github.com/clbarnes/bdv_meta/blob/main/add_downsamples.py | Script for (somewhat safely) adding metadata required by BigDataViewer (and optionally n5-viewer) to multiscale datasets stored in N5 containers.
#!/usr/bin/env python
"""
Script for (somewhat safely) adding metadata required by BigDataViewer
(and optionally n5-viewer) to multiscale datasets stored in N5 containers.
"""
from dataclasses import dataclass
import json
from pathlib import Path
from argparse import ArgumentParser
import re
@clbarnes
clbarnes / cultskill.py
Last active December 14, 2021 16:55
Take game results from Monderdome and apply trueskill-esque ratings
#!/usr/bin/env python
"""
Script for taking results from matches and applying a trueskill-esque rating system.
Results can either be returned as rankings, or as a plot of probability distributions of ratings.
The input is a columnar TSV.
In each column, the cells are: ISO-8601 datetime of the match, one team's score, and
then the team's members (one per cell) in arbitrary order.
Therefore, there are 2 columns per match, which share a datetime not shared by any other
matches.
@clbarnes
clbarnes / timethin.py
Last active June 15, 2021 18:02
Python CLI script to filter strings with datetimes in them (e.g. names of snapshot files) based on some retention policy; largely untested
#!/usr/bin/env python
from argparse import ArgumentParser
import datetime as dt
from typing import Iterable, List
from _strptime import TimeRE
import re
import sys
import logging
from dataclasses import dataclass
from collections import defaultdict
@clbarnes
clbarnes / circuit_sim_case.js
Created June 10, 2021 08:07
Test case for the circuit simulation widget
this.units = [
{
name: "PNs",
color: "#000000",
// PN KC MBON latMBON FBN FB2IN MBIN
w: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
k: 1.5,
th: 5.0,
I_tonic: 0,
I_stim: 10.0,
@clbarnes
clbarnes / conftest.py
Created May 7, 2021 09:19
Temporary directories for in-package doctests managed by pytest
"""Configuration for in-package doctests.
Should be importable (but not useful)
without development dependencies.
Where these files up and when they are deleted is documented
`here <https://pytest.org/en/stable/tmpdir.html#the-default-base-temporary-directory>`_.
"""
from pathlib import Path