Skip to content

Instantly share code, notes, and snippets.

View danieljfarrell's full-sized avatar

Daniel danieljfarrell

View GitHub Profile
@danieljfarrell
danieljfarrell / gist:faf7c4cafd683db13cbc
Created March 30, 2015 12:32
Ray line segment intersection in Python using Numpy
import numpy as np
def magnitude(vector):
return np.sqrt(np.dot(np.array(vector),np.array(vector)))
def norm(vector):
return np.array(vector)/magnitude(np.array(vector))
def lineRayIntersectionPoint(rayOrigin, rayDirection, point1, point2):
"""
@danieljfarrell
danieljfarrell / addColumn_PyTables.py
Created September 4, 2015 15:17 — forked from swarbhanu/addColumn_PyTables.py
Example showing how to add a column in PyTables
from tables import *
# Isolated the copying logic
def append_column(table, group, name, column):
"""Returns a copy of `table` with an empty `column` appended named `name`."""
description = table.description._v_colObjects.copy()
description[name] = column
copy = Table(group, table.name+"_copy", description)
@danieljfarrell
danieljfarrell / feature_matrix.py
Created December 3, 2015 07:36
Convert a feature-list Pandas data frame to a feature-matrix.
import pandas as pd
from sklearn.preprocessing import MultiLabelBinarizer
# Feature-list data frame
df = pd.DataFrame(columns = ["features"], index=['Item 1', 'Item 2'])
df['features'] = [["A", "B"], ["C", "D"]]
# Use scikits-learn to create feature matrix and feature names
mlb = MultiLabelBinarizer()
feature_column_name = 'features'
@danieljfarrell
danieljfarrell / 95conf.py
Created February 19, 2016 12:26
Python code implementing some of MATLAB's nlparci functionality (Nonlinear regression parameter confidence intervals)
def non_linear_parameters_95_percent_confidence_interval(fvec, jac):
"""Returns the 95% confidence interval on parameters from
non-linear fit results."""
# residual sum of squares
rss = np.sum(fvec**2)
# number of data points and parameters
n, p = jac.shape
# the statistical degrees of freedom
nmp = n - p
# mean residual error
@danieljfarrell
danieljfarrell / gist:d1092dbdb5a46c864145
Created February 29, 2016 15:39
Command line instructions to prevent Finder.app from writing .Spotlight-V100 .Trashes ._.Trashes .disk .fseventsd to a mounted network disk
// http://apple.stackexchange.com/questions/6707/how-to-stop-os-x-from-writing-spotlight-and-trash-files-to-memory-cards-and-usb
mdutil -i off /Volumes/yourUSBstick
cd /Volumes/yourUSBstick
rm -rf .{,_.}{fseventsd,Spotlight-V*,Trashes}
mkdir .fseventsd
touch .fseventsd/no_log .metadata_never_index .Trashes
cd -
@danieljfarrell
danieljfarrell / run_test_robot.py
Created March 29, 2016 11:54
Reading/writing stdout/stdin
import os, psutil, subprocess
# Usage:
# This script MUST be run from the root of the
# teraview-test-robot directory for example using,
# python run_test_robot_cmd.py
def make_test_robot_process():
"""Creates the test robot process."""
REL_TEST_ROBOT_EXE = "bin\\Debug\\test-robot.exe"
@danieljfarrell
danieljfarrell / make_frequency_information.py
Created April 19, 2016 15:24
APS make_frequency_information function.
def make_frequency_information(self):
"""NB kUseNewFFTMethod = True for MATLAB algorithm."""
reference = self.measurement.reference
if len(reference) > 1:
dt = self.measurement.optical_delay_spacing
t_offset = self.measurement.optical_delay_offset
f_max = 1./(2*dt)
if kUseNewFFTMethod:
freq = np.linspace(0.0, f_max, len(reference) + 1)
else:
@danieljfarrell
danieljfarrell / experimental_filtering.py
Created April 19, 2016 15:33
Experimental reference filtering.
def remove_reference_signal_with_experimental_reference_method_with_extended_FFT(
signal,
reference,
optical_delay_spacing,
ref_calibration=None):
"""Removes reference signal using extended FFT method."""
# transform reference signal to frequency domain
t_points = reference.shape[0]
ref_spec = np.fft.rfft(reference, reference.shape[0]*2)
signal_spec = np.fft.rfft(signal, signal.shape[0]*2)
@danieljfarrell
danieljfarrell / tree_editor_multiple_selection.py
Created October 24, 2016 10:14
tree_editor_multiple_selection.py -- Example of a tree editor demonstrating multiple selection
# tree_editor.py -- Example of a tree editor
from traits.api \
import HasTraits, Str, Regex, List, Instance, Any, Property
from traitsui.api \
import TreeEditor, TreeNode, View, Item, VSplit, \
HGroup, Handler, Group, ModelView
from traitsui.menu \
import Menu, Action, Separator
from traitsui.wx.tree_editor \
@danieljfarrell
danieljfarrell / parse_iso8601_badly.py
Created January 5, 2017 15:18
Cheap and cheerful parser for iso8601 with zero flexibility.
import datetime
import time
now = datetime.datetime.now()
now_iso = now.isoformat()
def parse_iso8601_badly(iso_str):
date_str, time_str = iso_str.split('T')
d = dict()
d.update(zip(['hour', 'minute', 'second'],