Skip to content

Instantly share code, notes, and snippets.

View VehpuS's full-sized avatar
🦔

Moshe Jonathan Gordon Radian VehpuS

🦔
View GitHub Profile
@VehpuS
VehpuS / range_from_slice.py
Last active April 21, 2024 11:07
Given a python slice object and a given length, return a range representing the indices that should be returned for that slice for a collection of that length (useful for implementing __getitem__ for slice as index)
def range_from_slice_overcomplicated(slc: slice, length: int) -> range:
step = 1 if slc.step is None else slc.step
assert step != 0, "slice step cannot be zero"
default_start = 0 if step > 0 else length - 1
default_end = length if step > 0 else 0
return range(
default_start if slc.start is None else (length + slc.start) if slc.start < 0 else slc.start,
default_end if slc.stop is None else max(-1, (length + slc.stop)) if slc.stop < 0 else min(slc.stop, length),
step
)
@VehpuS
VehpuS / singing-synthesis.ipynb
Created August 6, 2022 18:13
singing-synthesis.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@VehpuS
VehpuS / accessing-datasets-rasterio.md
Created June 8, 2022 08:35
Accessing datasets located in buffers using MemoryFile and ZipMemoryFile (based on https://github.com/rasterio/rasterio/issues/977)

Rasterio has different ways to access datasets located on disk or at network addresses and datasets located in memory buffers. This document explains the former once again and then introduces the latter for the first time.

Accessing datasets on your filesystem

To access datasets on disk, give a filesystem path to rasterio.open().

import rasterio

# Open a dataset located in a local file.
with rasterio.open('data/RGB.byte.tif') as dataset:
@VehpuS
VehpuS / downsample-multiband-arr.py
Created June 2, 2022 09:49
downsample a multiband image represented as a 3d numpy array (bands, height, width)
MASKED_VAL = -1 # Will depend on the band data (in my case it was always positive)
width = im.shape[-1]
height = im.shape[-2]
b = math.ceil(max(width, height)/factor)
dest_dim = b * factor
width_pad = dest_dim - width
height_pad = dest_dim - height
@VehpuS
VehpuS / muse-v1-sdk-documentation.html
Created May 19, 2022 20:32
After luckily finding a wayback version of the Muse V1 SDK (which can be accessed by interfacing with the muse-io executable - accessible here https://github.com/VehpuS/muse_tools) - I'm copying the data documentation for future users: https://web.archive.org/web/20181105231756/http://developer.choosemuse.com/tools/available-data#Concentration
<html>
<head>
</head>
<body>
<div class="toc-content">
<p class="large">This is a list of all the data available from MuseIO. For the data available from LibMuse, please see the API Reference for your platform.</p>
<div class="admonition-note-weak">NOTE: Different data parameters like sampling rate, resolution, and more can be configured using <a title="Headband Configuration Presets" href="https://web.archive.org/web/20181105231756/http://dev.choosemuse.com/hardware-firmware/headband-configuration-presets">preset commands</a> that are sent when a connection to Muse is established.</div>
<h2><span id="EEG_Data"><b>EEG Data</b></span></h2>
<div class="toc-step-content">
@VehpuS
VehpuS / git-crypt.md
Created May 3, 2022 17:24
Git Crypt HOWTO

What is git-crypt

In short, git-crypt is a tool that allows you to encrypt and decrypt files with GPG keys in the background of Git commands. Once the tool is properly configured, the files are encrypted on push to and decrypted when fetched from the remote server.

NOTE: git-crypt is not a super secure encryption, and does not replace the need to protect the repo behind 2FA. It's use is to allow us to share code level data that should not be saved with our codebase (i.e. external credentials) but that we still wish to share with the team.

Setup

Based on this guide.

Git-crypt and GPG installation

@VehpuS
VehpuS / mod-a-b.js
Created March 10, 2022 17:13
Calculating modulo with other arithmetic functions (thanks to https://stackoverflow.com/questions/35155598/unable-to-use-in-glsl for the idea)
// neither recursion nor looping is necessary to compute a mod b. That's easily done by a - (b * floor(a/b)).
const mod = (a, b) => a - (b * floor(a/b))
// Simple, but powerful when running on platforms that lack the function but have the floor function (i.e. GLSL for deck.gl)
// Thanks to https://stackoverflow.com/questions/35155598/unable-to-use-in-glsl for the idea
@VehpuS
VehpuS / awaitable-img.ts
Created February 13, 2022 15:31
Awaitable loaded image object - useful for drawing to canvas, getting dimensions
const getLoadedImg = (url: string) => new Promise<HTMLImageElement>(resolve => {
const img = new Image();
img.crossOrigin = "anonymous";
img.src = url
img.onload = function () {
resolve(img);
}
});
@VehpuS
VehpuS / gdal-install-mac.md
Created January 21, 2022 08:47
How to install GDAL on a new Mac (hint: not brew, surprisingly)

Common

On Mac OS X you can install the “GDAL Complete” Framework from kyngchaos.com.

GDAL applications are run through the Mac OS X Terminal. The first time you install the GDAL package there is one additional step to make sure you can access these programs. Open the Terminal application and run the following commands:

New Oses (zsh):

vi ~/.zshenv
@VehpuS
VehpuS / concurrent_executor_map_order_proof.py
Created November 2, 2021 09:21
Proof that concurrent.future's executor.map returns values in order
import time
import random
import concurrent.futures
e = concurrent.futures.ThreadPoolExecutor(4)
s = range(10)
def sleep_and_return(i):
time.sleep(random.randint(3,7))
return i