Skip to content

Instantly share code, notes, and snippets.

View connerxyz's full-sized avatar

Conner Cowling connerxyz

View GitHub Profile
@connerxyz
connerxyz / d3-scaling-tick-number-with-window-inner-width.js
Created August 8, 2019 21:20
d3.js: v5: scaling number of ticks with window's inner width
var xScale = d3.scaleTime()
.domain(d3.extent(data, function(d) { return d.key; })) // Input
.range([0, width]); // output
// Quantize 400px - 1600px window range
var tickScale = d3.scaleQuantize()
.domain([400, 1600])
.range([3, 7, 10]);
// Set number of ticks based on window height
@connerxyz
connerxyz / d3-dynamic-discrete-values-to-continuous-color.js
Created August 8, 2019 18:24
d3.js: v5: mapping dynamic sets of discrete values to continuous color ranges
// An array of discrete values (possibly dynamically generated)
var discreteValues = ['John', 'Jack', 'Jill', 'Jane', 'Jim', 'Joan'];
// 1st scale establishes relationship between continuous [0,1] and
// the array index of the discrete values
var preColor = d3.scaleLinear()
.domain([0, discreteValues.length - 1])
.range([0, 1]);
console.log(preColor(discreteValues.indexOf('John'))) // 0
console.log(preColor(discreteValues.indexOf('Jack'))) // 0.2
console.log(preColor(discreteValues.indexOf('Joan'))) // 1.0
@connerxyz
connerxyz / env.save.sh
Last active May 28, 2019 13:55
A script for exporting conda environment definitions to handy files.
#!/bin/bash
# Usage string
usage() {
cat << USAGE
This script exists to serialize a local conda environment.
The output files produced by this script are version-controllable, allowing
for the local conda env definition to included with the repository, distributed,
and recreated on other hosts using either conda *or* pip.
@connerxyz
connerxyz / store-magic-for-meta-pattern.md
Last active September 16, 2021 15:04
Using %store magic to capture meta within/across Jupyter notebooks

Overview

The %store magic allows data to be stored in the ipython database, outside of a notebook's particular kernel, so it can be accessed from other notebooks.

Instructions

Initialization

If you're executing only one notebook, or this is the

@connerxyz
connerxyz / dict-to-json.hdfs.py
Created April 26, 2019 04:55
Write a Python dict to a single file in HDFS as JSON
# You've got a Python dict in the driver program or shell
x = {"a":1,"b":2}
# You want to write it to a single HDFS file in JSON format
sc.parallelize([x]).toDF().coalesce(1).write.mode('append').json(/hdfs/path/...)
@connerxyz
connerxyz / semantic-ui-sidebar-example.html
Last active June 29, 2018 21:29
Semantic UI material style overlay sidebar example
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<title>Semantic UI Sidebar Example</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.3.2/semantic.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.3.2/semantic.min.js"></script>
</head>
<body>
@connerxyz
connerxyz / README.md
Last active June 28, 2018 16:20
Porter stemmer: Python's NLTK vs. Java's OpenNLP: How to get *almost* identical Porter stemmers

Porter stemmer: Python's NLTK vs. Java's OpenNLP:

How to get almost equivalent Porter stemmers between Java and Python.

One remaining difference is how they handle non-dictionary words ending in "s":

Assuming you tokenize the following string after lowercase normalization using [a-z]\w+...

"EMI"s request for an appeal should be denied until a final judgment is entered. Document filed by Michael Robertson. (js) (Entered: 02/01/2012)"

"""
Remove *all* default, hard-coded, CSS classes and inline styling attached to tables when calling df.to_html(),
and add your own classes to each tag ad hoc.
This will probably be extremely simple in the future when the styling API is more mature. For now, we bleach it.
May need to `conda install bleach` or `pip install bleach`
"""
import pandas as pd
@connerxyz
connerxyz / app.py
Last active June 13, 2018 20:14
A modular pattern for injecting your own template utils (i.e. functions) into Flask's template contexts
from flask import Flask
app = Flask(__name__)
# Register core.utils with all template contexts
import utils
@app.context_processor
def utils_context():
return utils.registry
@connerxyz
connerxyz / mkdocs-video-renders.md
Last active October 31, 2017 02:35
Experimenting with rendering video via Markdown/MkDocs (and IPython.display)

Rendering Video w/ MkDocs

Render video via HTML5 <video> tags in markdown cell?

Method 1

Using the <source> element: