Skip to content

Instantly share code, notes, and snippets.

View gdementen's full-sized avatar

Gaëtan de Menten gdementen

  • Federal Planning Bureau
  • Belgium
View GitHub Profile
@gdementen
gdementen / gil.py
Last active December 17, 2015 15:59
How to release the GIL inside a numba function through ctypes
from ctypes import pythonapi, c_void_p
from numba import autojit
savethread = pythonapi.PyEval_SaveThread
savethread.argtypes = []
savethread.restype = c_void_p
restorethread = pythonapi.PyEval_RestoreThread
restorethread.argtypes = [c_void_p]
restorethread.restype = None
@gdementen
gdementen / recursive_ctypes_struct.py
Last active December 17, 2015 15:59
recursive ctypes structures make numba fail to compile
line 91, in from_ctypes_type
return from_ctypes_type(ctypes_type._type_).pointer()
File "c:\soft\Python27-32b\Lib\site-packages\numba\support\ctypes_support.py",
line 97, in from_ctypes_type
for name, field_type in ctypes_type._fields_]
File "c:\soft\Python27-32b\Lib\site-packages\numba\support\ctypes_support.py",
line 91, in from_ctypes_type
return from_ctypes_type(ctypes_type._type_).pointer()
File "c:\soft\Python27-32b\Lib\site-packages\numba\support\ctypes_support.py",
line 88, in from_ctypes_type
@gdementen
gdementen / crash.py
Last active December 17, 2015 19:29
Crashing Python with numba jit()
import numpy as np
from numba import jit, double, autojit, void
def func(result, a):
for i in range(len(result)):
result[i] = a[i]
func_nb = jit(void[:](double[:], double[:]))(func)
#func_nb = autojit(func)
@gdementen
gdementen / mt.py
Last active May 29, 2021 14:13
Example of multithreading a numba function by releasing the GIL through ctypes
import ast
from timeit import repeat
import threading
from ctypes import pythonapi, c_void_p
import math
import numpy as np
try:
import numexpr as ne
nthreads = ne.ncores
@gdementen
gdementen / sidewalk.patch
Created July 3, 2014 13:35
Alexis Eidelman Sidewalk Patch for LIAM2
### Eclipse Workspace Patch 1.0
#P liam2
Index: src/alignment.py
===================================================================
--- src/alignment.py (revision 960)
+++ src/alignment.py (working copy)
@@ -2,6 +2,7 @@
from itertools import izip
import os
@gdementen
gdementen / default.patch
Created July 3, 2014 13:42
Alexis Eidelman Default values Patch for LIAM2
diff --git a/src_liam/data.py b/src_liam/data.py
index 31fe79b..44f11b6 100644
--- a/src_liam/data.py
+++ b/src_liam/data.py
@@ -35,7 +35,7 @@ def append_carray_to_table(array, table, numlines=None, buffersize=10 * MB):
class ColumnArray(object):
- def __init__(self, array=None):
+ def __init__(self, array=None, default_values=None):
@gdementen
gdementen / test.yml
Created February 11, 2015 12:39
Test case for cont_regr
entities:
person:
fields:
- age: int
- gender: bool
- test: {type: float, initialdata: false}
processes:
test: cont_regr(age, mult=0.5, filter=gender)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@gdementen
gdementen / strange_sum.py
Last active July 29, 2016 08:19
Strange sum
>>> l = [4873.40374783,
5821.07686377,
5781.99852348,
5555.53769711,
0.0,
0.0,
0.0,
0.0]
>>> a = np.array(l)
>>> b = np.empty((8, 2))
@gdementen
gdementen / petowner_comprehension1.py
Last active September 16, 2016 19:36 — forked from kentquirk/petowner_comprehension1.py
Set up output first
# 1) like others pointed out, you should use a defaultdict in this case
# 2) otherwise use setdefault
output = {}
for element in elements:
output.setdefault(element["owner"], []).append(element["pet"])
# 3) if, for some reason, this does not work in your case, use a dict comprehension (if available for your version of Python, 2.7+ I think)
output = {e["owner"]: [] for e in elements}
for element in elements: