Skip to content

Instantly share code, notes, and snippets.

@d1manson
d1manson / bridged-worker.js
Last active August 31, 2022 16:18
BuildBridgedWorker is a function that makes communication between worker and main thread simple. It also means you can keep all related code in the same file, which is convenient.
/* EXAMPLE:
var workerCode = function(){
"use strict;" //this will become the first line of the worker
CalculateSomething(a,b,c,d){
var v = a+b+c+d; //trivial calculation
main.DisplayResult(v,"hello");
}
@d1manson
d1manson / BlobWorker.js
Last active June 27, 2018 11:28
Using an HTML5 Blob, you can get around the same-origin security restriction for Web Workers. The only slight complication is the need to return a Worker-like interface synchrounously. But this is actually fairly simple. Requires jQuery $ for ajax call. IE doesn't permit making workers from blob-urls, but other browsers do.
Worker = (function(){
var nativeWorker = Worker;
var BlobWorker = function(){
this.queuedCallList = [];
this.trueWorker = null;
this.onmessage = null;
}
BlobWorker.prototype.postMessage = function(){
if(this.trueWorker)
@d1manson
d1manson / reloadd.py
Last active November 15, 2023 16:50
Python - reload a module and update the methods for pre-existing versions of its class(es).
# -*- coding: utf-8 -*-
import sys
from types import ModuleType, ClassType
def can_reload_methods(klass):
klass.__CAN_RELOAD_METHODS__ = True
return klass
def reloadd(m):
@d1manson
d1manson / monobrow.py
Last active August 29, 2015 13:58
wrap matplotlib for piping to main process from subprocess (incomplete) http://stackoverflow.com/q/22819254/2399799
# -*- coding: utf-8 -*-
"""
The purpose of this module is to do monoprocessing for a single package when using multiprocessing.
It is designed with matplotlib in mind, but most of it is pretty general.
The idea is that you may have a block of code that you want to run in parallel
processes, but because of some "small" thing (i.e. something that isn't computationally expensive)
you can't do it easily without some major, ugly, alterations to your existing code. Monobrow.py
injects all the ugly stuff for you at the point you launch the processes, so you don't need to
alter your base code at all.
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 25 13:26:15 2014
@author: daniel
"""
from functools import partial
class LazyDictProperty(object):
"""
@d1manson
d1manson / exception_aggregator.py
Last active August 29, 2015 14:08
Collect exceptions & warnings together with custom state string, and print an aggregated summary at the end
# -*- coding: utf-8 -*-
"""
This works but it's pretty ugly in places, the html/str representation specifically.
"""
from __future__ import print_function
import sys
import warnings
from traceback import format_list, extract_tb, extract_stack
@d1manson
d1manson / pso.py
Last active November 20, 2018 11:07
fork of pyswarm (particle swarm optimization in python) - this version has been vectorised/optimised more than the original
import numpy as np
def pso(func, lb, ub, ieqcons=[], f_ieqcons=None, args=(), kwargs={},
func_takes_multiple=False, swarmsize=100, omega=0.5, phip=0.5, phig=0.5,
maxiter=100, minstep=1e-8, minfunc=1e-8, debug=False, info=True):
"""
Perform a particle swarm optimization (PSO)
Parameters
==========
@d1manson
d1manson / theta_mle_climer.py
Last active August 29, 2015 14:14
Python impl. of Climer et al. 2014, a max-likelihood estimation of temporal autocorrelogram parameters (for in vivo neuronal spiking)
# -*- coding: utf-8 -*-
"""
Adapted from the Matlab code acompanying the following paper:
Climer, J. R., DiTullio, R., Newman, E. L., Hasselmo, M. E., Eden, U. T.
(2014), Examination of rhythmicity of extracellularly recorded neurons
in the entorhinal cortex. Hippocampus, Epub ahead of print.
doi: 10.1002/hipo.22383.
Matlab code take from: github.com/jrclimer/mle_rhythmicity, on 02-feb-2015.
@d1manson
d1manson / accumarray.py
Last active August 29, 2015 14:15
[!!!SUPERSEDED BY ACTUAL REPOSITORY - https://github.com/ml31415/accumarray !!!] Matlab-like accumarray in Python
"""
!!!!!!SUPERSEDED BY ACTUAL REPOSITORY!!!!!!
https://github.com/ml31415/accumarray
!!!!!!SUPERSEDED BY ACTUAL REPOSITORY!!!!!!
"""
import numpy as np
@d1manson
d1manson / pearsonr_confidence.py
Last active August 29, 2015 14:15
compute pearson p-value using permutations and/or resampling rather than analytically
# -*- coding: utf-8 -*-
"""
Module contents:
# pearsonr_permutations
# pearsonr_sign_bootstrap
# permute_multi <-- used by pearsonr_permutations
DM, Feb 2015.
"""
import numpy as np