Skip to content

Instantly share code, notes, and snippets.

tianhuil /
Created May 20, 2023 18:43
Deploying to Heroku with a M1 Mac


Build image with linux platform for heroku servers. Replace {NAME_OF_HEROKU_APP} with your own tag:

docker buildx build --platform linux/amd64 -t {NAME_OF_HEROKU_APP} .

Tag your app with the url for your apps registry. make sure to use the name of your Heroku app in the url and tag name:

tianhuil / typeAssertions.ts
Created March 15, 2022 18:58
Type Assertions in typescript
// Type assertions, before I discovered
type AssertExtends<A, B> = A extends B ? true : false
export const assertExtends = <A, B>(_: AssertExtends<A, B>): void => {}
export const assertInstance = <B>(
a: any,
_: AssertExtends<typeof a, B>
): void => {}
assertExtends<'abc', string>(true)
tianhuil /
Last active November 25, 2021 04:53
Poetry Install
# Poetry needs to be installed globally
poetry init
poetry add \
matplotlib \
pandas \
wordcloud \
tqdm \
python-dotenv \
tianhuil /
Created September 20, 2021 13:11 — forked from uraimo/
Configure your Mac to use DNS over TLS
$ git push server main
+ export DOKKU_HOST_ROOT=/home/dokku
+ DOKKU_HOST_ROOT=/home/dokku
++ . /etc/os-release
++ echo ubuntu
+ export DOCKER_BIN=docker
+ DOCKER_BIN=docker
tianhuil / starbucks_us_locations.csv
Created June 10, 2020 01:16 — forked from dankohn/starbucks_us_locations.csv
8902 locations of US Starbucks with addresses, latitude, and longitude
We can't make this file beautiful and searchable because it's too large.
-149.8935557,61.21759217,Starbucks - AK - Anchorage 00001,"601 West Street_601 West 5th Avenue_Anchorage, Alaska 99501_907-277-2477"
-149.9054948,61.19533942,Starbucks - AK - Anchorage 00002,"Carrs-Anchorage #1805_1650 W Northern Lights Blvd_Anchorage, Alaska 99503_907-339-0500"
-149.7522,61.2297,Starbucks - AK - Anchorage 00003,"Elmendorf AFB_Bldg 5800 Westover Avenue_Anchorage, Alaska 99506"
-149.8643361,61.19525062,Starbucks - AK - Anchorage 00004,"Fred Meyer - Anchorage #11_1000 E Northern Lights Blvd_Anchorage, Alaska 995084283_907-264-9600"
-149.8379726,61.13751355,Starbucks - AK - Anchorage 00005,"Fred Meyer - Anchorage #656_2300 Abbott Road_Anchorage, Alaska 99507_907-365-2000"
-149.9092788,61.13994658,Starbucks - AK - Anchorage 00006,"Fred Meyer - Anchorage (Dimond) #71_2000 W Dimond Blvd_Anchorage, Alaska 995151400_907-267-6700"
-149.7364877,61.19533265,Starbucks - AK - Anchorage 00007,"Safeway-Anchorage #1817_7731 E Northern Lights Blvd_Anchorage, Alaska 99504_907-331-1700"
tianhuil /
Last active June 1, 2020 19:45
Practically solving the shape

Suppose we wish to understand if a given X/Y pair falls within a set of non-intersecting arbitrary polygons. For example, we are trying to see if a given lat,long falls within a Michigan minor civil division. There are 1500+ such divisions each one of which could be a polygon with hundreds or thousands of points. The following is a practical solution that is fast enough to return to a user.

  1. Break up Michigan into $N$ box oriented tiles. We can easily calculate if x, y will only fall within a tile based on the value (x % WIDTH, y % HEIGHT). Select $N$ such that most (e.g. ~60%?) tiles are fully contained within a polygon, and the vast majority (e.g. 95%) are in no more than 4 polygons. I imagine this would happen easy at $N$ less than 100,000.
  2. Preprocess the polygon shape files using Shapely to determine a map
tianhuil /
Last active November 12, 2019 19:02
Data Sharding (useful preprocessing for dask)
import gzip
import os
from itertools import islice
import argparse
# from
def grouper(iterable, n):
iterator = iter(iterable)
while True:
group = tuple(islice(iterator, n))
tianhuil /
Created November 9, 2019 05:20
Turns classifier's `predict_proba` into a transform
from sklearn.base import BaseEstimator, TransformerMixin
class ClassifierTransform(BaseEstimator, TransformerMixin):
def __init__(self, clf):
self.clf = clf
def fit(self, X, y=None):, y)
return self
tianhuil / Residua
Last active November 8, 2019 22:54
This is a Residual Regressor and Residual Classifier
from sklearn.base import BaseEstimator, ClassifierMixin, RegressorMixin
class _ResidualEstimator(BaseEstimator):
def __init__(self, base, residual):
self.base = base
self.residual = residual
def fit(self, X, y):, y), y - self.base.predict(X))