Skip to content

Instantly share code, notes, and snippets.

View yinleon's full-sized avatar

Leon Yin yinleon

View GitHub Profile
@yinleon
yinleon / image_grid.py
Created September 2, 2020 21:03
A helpful function to display grids of images in Jupyter Notebooks.
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
img_width, img_height = 400, 300
def resize_img_to_array(img, img_shape=(244, 244)):
img_array = np.array(
img.resize(
img_shape,
@yinleon
yinleon / muiltiprocessing_files.py
Last active February 25, 2021 00:14
This is a Python routine to help read many files using multiprocessing. `files` can be a list of file paths and `file_parser_func` can be any function that reads a file and returns a list.
from multiprocessing import Pool
from tqdm import tqdm
import pandas as pd
# def file_parser_func(fn : str):
# return pd.read_csv(fn).to_dict('records')
# files = ['a.csv', 'b.csv']
data = []
with Pool(processes=8) as pool:
@yinleon
yinleon / get_all_keys.py
Created March 12, 2018 21:45
A function to traverse and list all keys in a JSON or Dictionary
def get_all_keys(d, key=[]):
'''
A recursive function that traverses json keys in a dict `d`,
and prints the path to all keys
'''
if not isinstance(d, dict):
print(''.join(['["' + k + '"]' for k in key]))
return
for k, v in d.items():

Keybase proof

I hereby claim:

  • I am yinleon on github.
  • I am leonleon (https://keybase.io/leonleon) on keybase.
  • I have a public key ASD5y56XsVBxKct1HzreWZgFR0TuM59fIbcWfreau0am_Ao

To claim this, I am signing this object:

@yinleon
yinleon / twitter_streaming_api_quoted_retweet
Created April 14, 2017 19:52
A tweet that was a retweeted quoted status. Strings obfuscated with <OBFUSCATED>, numbers obfuscated with 666.
{
"favorite_count": 666,
"lang": "en",
"coordinates": null,
"quoted_status": {
"favorite_count": 666,
"lang": "en",
"coordinates": null,
"extended_tweet": {
"display_text_range": [
@yinleon
yinleon / twitter_streaming_api_tweet.json
Created April 14, 2017 19:42
A regular tweet from the streaming api. Strings obfuscated with <OBFUSCATE>, and numbers obfuscated with 666
{
"entities": {
"symbols": [],
"hashtags": [
{
"indices": [
66,
66
],
"text": "<OBFUSCATED>"
@yinleon
yinleon / twitter_streaming_api_retweet.json
Last active May 8, 2018 20:53
The syntax of a json body for a retweet from the streaming API.
{"contributors": null,
"coordinates": null,
"created_at": "Thu Jan 19 16:14:30 +0000 2017",
"entities": {"hashtags": [{"indices": [66, 66], "text": "<OBFUSCATED>"},
{"indices": [66, 66], "text": "<OBFUSCATED>"}],
"symbols": [],
"urls": [],
"user_mentions": [{"id": 666,
"id_str": "<OBFUSCATED>",
"indices": [66, 66],
@yinleon
yinleon / index.html
Last active December 2, 2016 18:30
Big Bang
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.links line {
stroke: #999;
stroke-opacity: 0.6;
}
.nodes circle {
This file has been truncated, but you can view the full file.
[
{
"lat": -89,
"lon": -180,
"d18o": -1.0000000150474662e+30
},
{
"lat": -89,
"lon": -179,
"d18o": -1.0000000150474662e+30
@yinleon
yinleon / data.json
Last active September 24, 2016 16:14
An interactive map of seawater isotopes.
This file has been truncated, but you can view the full file.
[
{
"index": "0",
"lon": "31.4",
"lat": "81.27",
"month": "7",
"year": "1987",
"depth": "3.0",
"temp": "-1.66",
"sal": "34.09",