Skip to content

Instantly share code, notes, and snippets.

View kcarnold's full-sized avatar

Kenneth C. Arnold kcarnold

  • Grand Rapids, MI
View GitHub Profile
@kcarnold
kcarnold / README.md
Created March 12, 2015 19:19
Distance Scaling

This is a simple implementation of MDS. Dragging an item constrains it; double-clicking removes the constraint. The cost is shown in the top left.

The data is eurodist from R.

@kcarnold
kcarnold / README.md
Last active August 29, 2015 14:16
Adjustable metric

Compare colors by hue, saturation, or lightness. The sliders adjust the weights. It seems like the main effect is whether the weight is 0 or not.

The embedding is simply links to the 5 nearest neighbors, according to that metric.

@kcarnold
kcarnold / README.md
Last active August 29, 2015 14:17
Affinity propagation
@kcarnold
kcarnold / incremental_moments.py
Created July 16, 2015 22:05
Incremental Moments
import numpy as np
class IncrementalMoments(object):
'''Knuth's incremental mean and variance computation, applied elementwise.'''
def __init__(self, shape, initial_items=[]):
self.n = 0
self.mean = np.zeros(shape)
self.m2 = np.zeros(shape)
self.update_seq(initial_items)
@kcarnold
kcarnold / blacken_notebook.py
Created November 2, 2018 19:59
Format all code in a notebook with Black
# https://black.readthedocs.io/en/stable/
import json
import black
import re
magic_re = re.compile(r'^%', flags=re.MULTILINE)
ipymagic = '##IpyMagic##'
filename = 'notebook.ipynb'
@kcarnold
kcarnold / email-to-printer.applescript
Created November 12, 2019 22:37
AppleScript to email a PDF to the Calvin University print queue (use with "Run AppleScript" action in Automator, in either a Print Plugin or a Quick Action)
on run {input, parameters}
set PDFs to input
set msubject to "to print: "
repeat with aFile in PDFs
tell application "Finder"
set filename to ((name of aFile) as string)
end tell
set msubject to msubject & filename & ","
end repeat
tell application "Microsoft Outlook"
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@kcarnold
kcarnold / vecpile.py
Created February 29, 2020 20:25
VecPile
class VecPile:
"""An attribute-accesed collection that asserts that all of its elements have the same length.
Useful for keeping several collections together, such as vectors with labels, or several different representations of the same data."""
def __init__(self, **kw):
for k, v in kw.items():
setattr(self, k, v)
@staticmethod
@kcarnold
kcarnold / sort_names.py
Created May 13, 2020 16:02
Sort a list of names alphabetically by last name (from clipboard)
print(
', '.join(
sorted(
subprocess.check_output('pbpaste').decode('utf-8').split('\n'),
key=lambda name: name.split()[-1])
)
)
@kcarnold
kcarnold / aiosched.py
Created April 9, 2022 15:22
Integrating Python's sched module with asyncio
import sched
import time
import asyncio
import datetime
start_time = time.time()
def tick(a='default'):
print("From tick", time.time() - start_time, a)