Debian Codegraph @

brian2/synapses/cythonspikequeue.pyx

raw
1 # cython: language_level = 3
2 # distutils: language = c++
3
4 from libcpp.vector cimport vector
5 from libcpp.pair cimport pair
6 from libcpp.string cimport string
7 import cython
8 from cython.operator import dereference
9 from cython.operator cimport dereference
10 from cpython.pycapsule cimport PyCapsule_New
11
12 cimport numpy as np
13 import numpy as np
14
15 np.import_array()
16
17 cdef extern from "stdint_compat.h":
18 # Longness only used for type promotion
19 # Actual compile time size used for conversion
20 ctypedef signed int int32_t
21 ctypedef signed long int64_t
22
23 ctypedef pair[int, vector[vector[int32_t]]] state_pair
24
25 ctypedef fused float_array:
26 np.ndarray[double, ndim=1, mode='c']
27 np.ndarray[float, ndim=1, mode='c']
28
29 cdef extern from "spikequeue.h":
30 cdef cppclass CSpikeQueue:
31 CSpikeQueue(int, int) except +
32 void prepare[scalar](scalar*, int, int32_t*, int, double)
33 void push(int32_t *, int)
34 state_pair _full_state()
35 void _restore_from_full_state(state_pair)
36 vector[int32_t]* peek()
37 void advance()
38
39 cdef class SpikeQueue:
40 # TODO: Currently, the data type for dt and delays is fixed
41 cdef CSpikeQueue *thisptr
42 cdef readonly tuple _state_tuple
43
44 def __cinit__(self, int source_start, int source_end):
45 self.thisptr = new CSpikeQueue(source_start, source_end)
46
47 def __init__(self, source_start, source_end):
48 self._state_tuple = (source_start, source_end, np.int32)
49
50 def __dealloc__(self):
51 del self.thisptr
52
53 def _full_state(self):
54 return self.thisptr._full_state()
55
56 def get_capsule(self):
57 """
58 Returns a PyCapsule object wrapping the underlying C++ CSpikeQueue pointer.
59
60 PyCapsules are used to safely pass raw C/C++ pointers between Python modules
61 or C extensions without exposing the actual implementation details to Python.
62 """
63 return PyCapsule_New(<void*>self.thisptr, "CSpikeQueue", NULL)
64
65 cdef object __weakref__ # Allows weak references to the SpikeQueue
66
67 def _restore_from_full_state(self, state):
68 cdef vector[vector[int32_t]] empty_queue
69 cdef state_pair empty_state
70 if state is not None:
71 self.thisptr._restore_from_full_state(state)
72 else:
73 empty_queue = vector[vector[int32_t]]()
74 empty_state = (0, empty_queue)
75 self.thisptr._restore_from_full_state(empty_state)
76
77 def prepare(self, float_array real_delays,
78 double dt,
79 np.ndarray[int32_t, ndim=1, mode='c'] sources):
80 if real_delays.dtype == np.float32:
81 self.thisptr.prepare(<float*>real_delays.data,
82 real_delays.shape[0],
83 <int32_t*>sources.data,
84 sources.shape[0], dt)
85 else:
86 self.thisptr.prepare(<double*>real_delays.data,
87 real_delays.shape[0],
88 <int32_t*>sources.data,
89 sources.shape[0], dt)
90
91 def push(self, np.ndarray[int32_t, ndim=1, mode='c'] spikes):
92 self.thisptr.push(<int32_t*>spikes.data, spikes.shape[0])
93
94 def peek(self):
95 # will only be used if the queue has size > 0
96 cdef int32_t* spikes_data
97 cdef np.npy_intp shape[1]
98 # This should create a numpy array from a std::vector<int> without
99 # copying -- &spikes[0] is guaranteed to point to a contiguous array
100 # according to the C++ standard.
101 cdef vector[int32_t]* spikes = self.thisptr.peek()
102 cdef size_t spikes_size = dereference(spikes).size()
103 if spikes_size == 0:
104 return np.empty(0, dtype=np.int32)
105 else:
106 spikes_data = <int32_t*>(&(dereference(spikes)[0]))
107 shape[0] = spikes_size
108 return np.PyArray_SimpleNewFromData(1, shape, np.NPY_INT32, spikes_data)
109
110 def advance(self):
111 self.thisptr.advance()