Skip to content

Instantly share code, notes, and snippets.

@sausheong
sausheong / 3d.rb
Created December 28, 2019 13:22
3D
#!/usr/bin/env ruby
require ‘rubygems’
require ‘rmagick’
include Magick
left = ImageList.new(ARGV[0]).gamma_correct(1,0,0)
right = ImageList.new(ARGV[1]).gamma_correct(0,1,1)
anaglyph = left.composite right, CenterGravity, ScreenCompositeOp
@sausheong
sausheong / .rb
Created December 29, 2019 02:39
waveform
reader = Audio::MPEG::Decoder.new
File.open('mrt_closing.mp3', 'rb') do |input|
File.open('out.wav', 'wb') do |output|
reader.decode(input, output)
end
end
@sausheong
sausheong / .rb
Created December 29, 2019 02:44
waveform 2
FasterCSV.open('wavdata.csv', 'w') do |csv|
csv << %w(ch1 ch2 combined)
File.open('out.wav') do |file|
while !file.eof?
if file.read(4) == 'data'
length = file.read(4).unpack('l').first
wavedata = StringIO.new file.read(length)
while !wavedata.eof?
ch1, ch2 = wavedata.read(4).unpack('ss')
csv << [ch1, ch2,ch1+ch2]
@sausheong
sausheong / .rb
Created December 29, 2019 02:45
waveform 3
script=<<-EOF
png(file='/Users/sausheong/projects/wavform/mrtplot.png', height=800, width=600, res=72)
par(mfrow=c(3,1),cex=1.1)
wav_data <- read.csv(file='/Users/sausheong/projects/wavform/wavdata.csv', header=TRUE)
plot(wav_data$combined, type='n', main='Channel 1', xlab='Time', ylab='Frequency')
lines(wav_data$ch1)
plot(wav_data$combined, type='n', main='Channel 2', xlab='Time', ylab='Frequency')
lines(wav_data$ch2)
plot(wav_data$combined, type='n', main='Channel 1 + Channel 2', xlab='Time', ylab='Frequency')
lines(wav_data$combined)
@sausheong
sausheong / task.py
Last active January 3, 2020 14:28
todo.ipynb
from recordtype import recordtype
# weight is the priority of the task
# duration is how long it will take to complete the task
# due is the due date of the task
# done is the date when the task is completed (starts with 0,
# set to whenever the task is done)
Task = recordtype('Task', 'weight duration due done')
import numpy as np
# create todo tasks
def create_tasks(num):
tasks = []
distribution = np.round(np.random.exponential(5,10000))
for i in range(num):
weight = np.random.randint(1,100)
due = np.random.choice(distribution)+1
duration = np.random.randint(0,due)
# Tallies up the results from running the simulation. Calculates:
# 1. percentage of completed tasks
# 2. percentage of important tasks completed
# 3. percentage of tasks completed in time
def tally(completed_tasks, all_tasks):
# percentage of tasks completed
completed = len(completed_tasks)/len(all_tasks)
# set the 75th percentile of all tasks based on weight
# as the important tasks
def duration(task): return task.duration
# run a single simulation
def simulate(ratio, *tasks):
deadline = ratio * sum(list(map(duration, tasks)))
completed_tasks = []
elapsed_time = 0
for task in tasks:
elapsed_time += task.duration
if elapsed_time <= deadline:
def add(a, b): return tuple(map(lambda x, y: x + y, a, b))
def divide(a, b): return tuple([x/b for x in a])
# Run multiple iterations of the simulation
def run(ratio, iterations, num, algorithm):
results, final_results = (0,0,0), (0,0,0)
for i in range(iterations):
tasks = create_tasks(num)
results = simulate(ratio, *tuple(algorithm(*tasks)))
%matplotlib inline
import matplotlib.pyplot as plt
def chart(data, title):
labels=np.array(['Completed Tasks', 'Completed Weight', 'In Time Tasks'])
angles=np.linspace(0, 2*np.pi, len(labels), endpoint=False)
angles=np.concatenate((angles,[angles[0]]))
colors = ['blue', 'red', 'green', 'violet', 'blueviolet', 'orange', 'deepskyblue']
for d, t, c in zip(data, title, colors):