Skip to content

Instantly share code, notes, and snippets.

@d1manson
d1manson / zotero_word_formatting.vba
Last active January 17, 2016 12:32
set color of zotero references in MSWord using VBA
Sub FormatZoteroRefs()
Set objUndo = Application.UndoRecord
objUndo.StartCustomRecord ("Format Zotero refs")
For Each myStoryRange In ActiveDocument.StoryRanges
For ii = 1 To myStoryRange.Fields.Count
If InStr(1, LTrim(myStoryRange.Fields(ii)), "ADDIN ZOTERO_ITEM CSL_CITATION") = 1 Then
Set rng = myStoryRange.Fields(ii).Result
rng.Font.ColorIndex = 9 ' this seems to be dark blue
@d1manson
d1manson / radix_sort.py
Last active December 3, 2015 15:36
vectorized radix sort, but a lot slower than just np.sort
"""
If the data is for *small* integers, then you can use counting sort with
np.bincount and np.repeat.
Note that this implementation is currently limtied to non-negative ints.
To deal with negative ints, you could just add a step at the end which
finds the first negative number and then moves all negative (newly sorted)
numbers to the front...for example:
[ 0 15 15 17 19 26 34 84 99 -12 -11 -3]
|------------|
@d1manson
d1manson / spyder.py
Last active October 5, 2015 17:17
failed attempts to put a button in the title bar of a qt application
"""
This is a mess of stuff that never worked.
I put it here main for my own reference, but it might be helpful to someone.
"""
try:
import ctypes
WM_NCCALCSIZE = 0x0083
WM_ACTIVATE = 0x0006
SWP_FRAMECHANGED = 0x0020
<html>
<head>
<script src="bower_components/webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="bower_components/polymer/polymer.html">
<dom-module id="x-child">
<template>
<div style="background:#ccc;display: inline-block;">
prop_read_write: <span>[[prop_read_write]]</span><br>
prop_read_only: <span>[[prop_read_only]]</span><br>
@d1manson
d1manson / anova.py
Last active July 30, 2021 12:27
perform two-way repeated measures anova in python using {car} package in R, via rpy2
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
_dummy_data_anova2 = pd.DataFrame(np.random.rand(30,4) + [[0., 1., 0.2, 0.8]],
columns=pd.MultiIndex.from_tuples([
('a0','b0'),('a0','b1'),('a1','b0'),('a1','b1')],
names=['factor_a', 'factor_b']),
index=range(30))
@d1manson
d1manson / row_indexer.py
Last active February 15, 2016 13:05
pandas Row_Indexer - syntactic sugar (with large overhead) for assigning values to columns within a single row of a DataFrame
class RowIndexer(object):
def __init__(self, obj, idx, **kwargs):
"""
This class is syntactic sugar for performing a slow iteration over
rows in a pandas DataFrame, where each row needs to have some (slow)
computation performed and the results assigned to multiple columns
in the row. Here is a bare-bones example::
df = ... #create a DataFram
@d1manson
d1manson / append_docstring.py
Created March 5, 2015 17:01
decorator for appending docstring of one or more objects to a function
import inspect
def append_docstring(*others):
"""
Appends one or more other objects' docstrings to the decorated function.
Title, location and args info is provided in addition to basic docstring.
TODO: Compare the location of `other` to `foo` and only show the
neccessary suffix. Also, try and get the args/kwargs and
type printed nicely like spyder does.
@d1manson
d1manson / images_in_pandas.md
Last active April 19, 2018 23:10
show images when displaying 2d arrays in iPython and Pandas

As of writing, this is only possible with my fork of pandas, but hopefully it will make its way into the main pandas stable branch.

The purpose of the fork is to let you specify a custom html formatter for individual columns in a data frame.


In this example we create a formatting function which takes a numpy array and returns a string of the form <img src='--base64-encoded-data'/>. This means that the numpy array is displayed as an image.

Below is the code we use to define our custom format function. Note that the function takes a single element from the data frame and returns and html string::

@d1manson
d1manson / list_views.py
Created February 27, 2015 23:09
a couple of classes for creating read/write views onto a list/array ...you cant do this in numpy
# -*- coding: utf-8 -*-
"""
Module contents:
# index_view
# masked_list
@author: daniel
"""