sim-ballena 0.1.0__tar.gz → 0.1.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of sim-ballena might be problematic. Click here for more details.

@@ -4,9 +4,11 @@ version = 4
4
4
 
5
5
  [[package]]
6
6
  name = "SimBallenaSNN"
7
- version = "0.1.0"
7
+ version = "0.1.1"
8
8
  dependencies = [
9
9
  "pyo3",
10
+ "rand",
11
+ "rand_distr",
10
12
  ]
11
13
 
12
14
  [[package]]
@@ -15,6 +17,24 @@ version = "1.5.0"
15
17
  source = "registry+https://github.com/rust-lang/crates.io-index"
16
18
  checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
17
19
 
20
+ [[package]]
21
+ name = "cfg-if"
22
+ version = "1.0.4"
23
+ source = "registry+https://github.com/rust-lang/crates.io-index"
24
+ checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
25
+
26
+ [[package]]
27
+ name = "getrandom"
28
+ version = "0.3.4"
29
+ source = "registry+https://github.com/rust-lang/crates.io-index"
30
+ checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd"
31
+ dependencies = [
32
+ "cfg-if",
33
+ "libc",
34
+ "r-efi",
35
+ "wasip2",
36
+ ]
37
+
18
38
  [[package]]
19
39
  name = "heck"
20
40
  version = "0.5.0"
@@ -36,6 +56,12 @@ version = "0.2.180"
36
56
  source = "registry+https://github.com/rust-lang/crates.io-index"
37
57
  checksum = "bcc35a38544a891a5f7c865aca548a982ccb3b8650a5b06d0fd33a10283c56fc"
38
58
 
59
+ [[package]]
60
+ name = "libm"
61
+ version = "0.2.16"
62
+ source = "registry+https://github.com/rust-lang/crates.io-index"
63
+ checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981"
64
+
39
65
  [[package]]
40
66
  name = "memoffset"
41
67
  version = "0.9.1"
@@ -45,6 +71,16 @@ dependencies = [
45
71
  "autocfg",
46
72
  ]
47
73
 
74
+ [[package]]
75
+ name = "num-traits"
76
+ version = "0.2.19"
77
+ source = "registry+https://github.com/rust-lang/crates.io-index"
78
+ checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
79
+ dependencies = [
80
+ "autocfg",
81
+ "libm",
82
+ ]
83
+
48
84
  [[package]]
49
85
  name = "once_cell"
50
86
  version = "1.21.3"
@@ -57,6 +93,15 @@ version = "1.13.0"
57
93
  source = "registry+https://github.com/rust-lang/crates.io-index"
58
94
  checksum = "f89776e4d69bb58bc6993e99ffa1d11f228b839984854c7daeb5d37f87cbe950"
59
95
 
96
+ [[package]]
97
+ name = "ppv-lite86"
98
+ version = "0.2.21"
99
+ source = "registry+https://github.com/rust-lang/crates.io-index"
100
+ checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9"
101
+ dependencies = [
102
+ "zerocopy",
103
+ ]
104
+
60
105
  [[package]]
61
106
  name = "proc-macro2"
62
107
  version = "1.0.105"
@@ -136,6 +181,51 @@ dependencies = [
136
181
  "proc-macro2",
137
182
  ]
138
183
 
184
+ [[package]]
185
+ name = "r-efi"
186
+ version = "5.3.0"
187
+ source = "registry+https://github.com/rust-lang/crates.io-index"
188
+ checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
189
+
190
+ [[package]]
191
+ name = "rand"
192
+ version = "0.9.2"
193
+ source = "registry+https://github.com/rust-lang/crates.io-index"
194
+ checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1"
195
+ dependencies = [
196
+ "rand_chacha",
197
+ "rand_core",
198
+ ]
199
+
200
+ [[package]]
201
+ name = "rand_chacha"
202
+ version = "0.9.0"
203
+ source = "registry+https://github.com/rust-lang/crates.io-index"
204
+ checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
205
+ dependencies = [
206
+ "ppv-lite86",
207
+ "rand_core",
208
+ ]
209
+
210
+ [[package]]
211
+ name = "rand_core"
212
+ version = "0.9.5"
213
+ source = "registry+https://github.com/rust-lang/crates.io-index"
214
+ checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c"
215
+ dependencies = [
216
+ "getrandom",
217
+ ]
218
+
219
+ [[package]]
220
+ name = "rand_distr"
221
+ version = "0.5.1"
222
+ source = "registry+https://github.com/rust-lang/crates.io-index"
223
+ checksum = "6a8615d50dcf34fa31f7ab52692afec947c4dd0ab803cc87cb3b0b4570ff7463"
224
+ dependencies = [
225
+ "num-traits",
226
+ "rand",
227
+ ]
228
+
139
229
  [[package]]
140
230
  name = "rustversion"
141
231
  version = "1.0.22"
@@ -170,3 +260,38 @@ name = "unindent"
170
260
  version = "0.2.4"
171
261
  source = "registry+https://github.com/rust-lang/crates.io-index"
172
262
  checksum = "7264e107f553ccae879d21fbea1d6724ac785e8c3bfc762137959b5802826ef3"
263
+
264
+ [[package]]
265
+ name = "wasip2"
266
+ version = "1.0.2+wasi-0.2.9"
267
+ source = "registry+https://github.com/rust-lang/crates.io-index"
268
+ checksum = "9517f9239f02c069db75e65f174b3da828fe5f5b945c4dd26bd25d89c03ebcf5"
269
+ dependencies = [
270
+ "wit-bindgen",
271
+ ]
272
+
273
+ [[package]]
274
+ name = "wit-bindgen"
275
+ version = "0.51.0"
276
+ source = "registry+https://github.com/rust-lang/crates.io-index"
277
+ checksum = "d7249219f66ced02969388cf2bb044a09756a083d0fab1e566056b04d9fbcaa5"
278
+
279
+ [[package]]
280
+ name = "zerocopy"
281
+ version = "0.8.36"
282
+ source = "registry+https://github.com/rust-lang/crates.io-index"
283
+ checksum = "dafd85c832c1b68bbb4ec0c72c7f6f4fc5179627d2bc7c26b30e4c0cc11e76cc"
284
+ dependencies = [
285
+ "zerocopy-derive",
286
+ ]
287
+
288
+ [[package]]
289
+ name = "zerocopy-derive"
290
+ version = "0.8.36"
291
+ source = "registry+https://github.com/rust-lang/crates.io-index"
292
+ checksum = "7cb7e4e8436d9db52fbd6625dbf2f45243ab84994a72882ec8227b99e72b439a"
293
+ dependencies = [
294
+ "proc-macro2",
295
+ "quote",
296
+ "syn",
297
+ ]
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "SimBallenaSNN"
3
- version = "0.1.0"
3
+ version = "0.1.1"
4
4
  edition = "2024"
5
5
 
6
6
  # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -10,3 +10,5 @@ crate-type = ["cdylib"]
10
10
 
11
11
  [dependencies]
12
12
  pyo3 = "0.27.0"
13
+ rand = "0.9.2"
14
+ rand_distr = "0.5.1"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sim_ballena
3
- Version: 0.1.0
3
+ Version: 0.1.1
4
4
  Classifier: Programming Language :: Rust
5
5
  Classifier: Programming Language :: Python :: Implementation :: CPython
6
6
  Classifier: Programming Language :: Python :: Implementation :: PyPy
@@ -0,0 +1,132 @@
1
+ use pyo3::prelude::*;
2
+ use pyo3::exceptions::PyValueError;
3
+ use crate::utils::vec_of_tuples;
4
+ use rand_distr::{Exp, Distribution};
5
+
6
+ /* ================ */
7
+ /* === INSTANCE === */
8
+ /* ================ */
9
+
10
+ #[pyclass]
11
+ pub struct Instance{
12
+ spikes: Vec<(f64,usize)>
13
+ }
14
+
15
+ #[pymethods]
16
+ impl Instance{
17
+ #[new]
18
+ fn new(obj: &Bound<'_,PyAny>)->PyResult<Self>{
19
+ let mut spikes = match vec_of_tuples::<f64>(obj){
20
+ Ok(v) => v,
21
+ Err(e) => return Err(e)
22
+ };
23
+
24
+ spikes.sort_by(|a,b|a.0.partial_cmp(&b.0).unwrap());
25
+
26
+ Ok(Self{spikes})
27
+ }
28
+
29
+ fn __str__(&self)->String{
30
+ format!("Input(count={})",self.spikes.len())
31
+ }
32
+
33
+ fn __repr__(&self)->String{
34
+ self.__str__()
35
+ }
36
+
37
+ pub fn get(&self)->&Vec<(f64,usize)>{
38
+ &self.spikes
39
+ }
40
+ }
41
+
42
+ impl Instance{
43
+ pub fn from_poisson(spikes:Vec<(f64,usize)>)->Self{
44
+ Self{spikes}
45
+ }
46
+ }
47
+
48
+ /* ================= */
49
+ /* ==== POISSON ==== */
50
+ /* ================= */
51
+
52
+ #[pyclass]
53
+ pub struct PoissonGenerator{
54
+ max_time : f64,
55
+ spike_times : Vec<Vec<f64>>,
56
+ }
57
+
58
+ #[pymethods]
59
+ impl PoissonGenerator{
60
+ #[new]
61
+ fn new(rates: Vec<f64>, max_time:f64)->Self{
62
+ let mut spike_times:Vec<Vec<f64>> = Vec::new();
63
+ for rate in rates{
64
+ spike_times.push( Self::poisson_process(rate, max_time) );
65
+ }
66
+ Self{max_time, spike_times}
67
+ }
68
+
69
+ fn to_instance(&self)->Instance{
70
+ let mut all_spikes:Vec<(f64,usize)> = Vec::new();
71
+ for (ch,spikes) in self.spike_times.iter().enumerate(){
72
+ let instance_view:Vec<(f64,usize)> = spikes.iter().map(|t|(*t,ch)).collect();
73
+ all_spikes.extend( instance_view );
74
+ }
75
+ Instance::from_poisson( all_spikes )
76
+ }
77
+
78
+ fn get_spikes(&self)->Vec<Vec<f64>>{
79
+ self.spike_times.clone()
80
+ }
81
+
82
+ fn concat<'py>(mut this:PyRefMut<'py, Self>, other:&PoissonGenerator)->PyResult<PyRefMut<'py,Self>>{
83
+
84
+ // check both generators have same dimentions
85
+ let mut spikes_other = other.get_spikes();
86
+ if spikes_other.len() != this.spike_times.len(){
87
+ return Err(PyValueError::new_err("PoissonGenerator must have the same number of inputs"))
88
+ }
89
+ // offset
90
+ for ch in spikes_other.iter_mut(){
91
+ for spk in ch.iter_mut(){
92
+ *spk += this.max_time;
93
+ }
94
+ }
95
+ // concat
96
+ for ch_idx in 0..spikes_other.len(){
97
+ this.spike_times[ch_idx].extend( spikes_other[ch_idx].clone() );
98
+ }
99
+ this.max_time += other.get_max_time();
100
+ Ok(this)
101
+ }
102
+
103
+ fn __str__(&self)->String{
104
+ format!("PoissonGenerator(n_inputs:{}, max_time:{})", self.spike_times.len(), self.max_time)
105
+ }
106
+
107
+ fn __repr__(&self)->String{
108
+ self.__str__()
109
+ }
110
+
111
+ }
112
+
113
+ impl PoissonGenerator{
114
+ fn poisson_process(rate:f64, max_time:f64)->Vec<f64>{
115
+ let mut rng = rand::rng();
116
+ let poi = Exp::new(rate).unwrap();
117
+
118
+ let mut spikes:Vec<f64> = Vec::new();
119
+ let mut t = poi.sample( &mut rng );
120
+ while t<max_time{
121
+ spikes.push(t);
122
+ t += poi.sample( &mut rng );
123
+ }
124
+ spikes
125
+ }
126
+
127
+ pub fn get_max_time(&self)->f64{
128
+ self.max_time
129
+ }
130
+
131
+
132
+ }
@@ -14,6 +14,7 @@ fn sim_ballena(m: &Bound<'_, PyModule>) -> PyResult<()> {
14
14
  m.add_class::<neurons::Lif>()?;
15
15
  m.add_class::<networks::Network>()?;
16
16
  m.add_class::<instances::Instance>()?;
17
+ m.add_class::<instances::PoissonGenerator>()?;
17
18
  // m.add_function( wrap_pyfunction!(networks::network,m)? )?;
18
19
 
19
20
  Ok(())
@@ -2,9 +2,31 @@
2
2
  "cells": [
3
3
  {
4
4
  "cell_type": "code",
5
- "execution_count": 10,
5
+ "execution_count": 1,
6
6
  "metadata": {},
7
- "outputs": [],
7
+ "outputs": [
8
+ {
9
+ "name": "stdout",
10
+ "output_type": "stream",
11
+ "text": [
12
+ "\n",
13
+ " -- N E S T --\n",
14
+ " Copyright (C) 2004 The NEST Initiative\n",
15
+ "\n",
16
+ " Version: 3.9.0\n",
17
+ " Built: Sep 26 2025 04:43:22\n",
18
+ "\n",
19
+ " This program is provided AS IS and comes with\n",
20
+ " NO WARRANTY. See the file LICENSE for details.\n",
21
+ "\n",
22
+ " Problems or suggestions?\n",
23
+ " Visit https://www.nest-simulator.org\n",
24
+ "\n",
25
+ " Type 'nest.help()' to find out more about NEST.\n",
26
+ "\n"
27
+ ]
28
+ }
29
+ ],
8
30
  "source": [
9
31
  "import nest\n",
10
32
  "import matplotlib.pyplot as plt\n",
@@ -16,7 +38,7 @@
16
38
  },
17
39
  {
18
40
  "cell_type": "code",
19
- "execution_count": null,
41
+ "execution_count": 2,
20
42
  "metadata": {},
21
43
  "outputs": [],
22
44
  "source": [
@@ -35,7 +57,7 @@
35
57
  },
36
58
  {
37
59
  "cell_type": "code",
38
- "execution_count": 13,
60
+ "execution_count": 3,
39
61
  "metadata": {},
40
62
  "outputs": [],
41
63
  "source": [
@@ -101,7 +123,7 @@
101
123
  },
102
124
  {
103
125
  "cell_type": "code",
104
- "execution_count": 14,
126
+ "execution_count": 4,
105
127
  "metadata": {},
106
128
  "outputs": [
107
129
  {
@@ -127,7 +149,7 @@
127
149
  },
128
150
  {
129
151
  "cell_type": "code",
130
- "execution_count": 15,
152
+ "execution_count": 5,
131
153
  "metadata": {},
132
154
  "outputs": [],
133
155
  "source": [
@@ -183,50 +205,49 @@
183
205
  },
184
206
  {
185
207
  "cell_type": "code",
186
- "execution_count": 21,
208
+ "execution_count": 6,
187
209
  "metadata": {},
188
210
  "outputs": [
189
211
  {
190
- "data": {
191
- "text/plain": [
192
- "<Instance at 0x74fcd1e09380>"
193
- ]
194
- },
195
- "execution_count": 21,
196
- "metadata": {},
197
- "output_type": "execute_result"
212
+ "name": "stdout",
213
+ "output_type": "stream",
214
+ "text": [
215
+ "t_ballena 0.8343455791473389\n",
216
+ "t_nest 7.572166204452515\n",
217
+ "ballena es 9.075575389505754 veces mas rapido\n"
218
+ ]
198
219
  }
199
220
  ],
200
221
  "source": [
201
- "inputs = spike_generator(100,0.01,100)\n",
202
- "inputs = ballena.Instance( inputs )\n",
203
- "inputs"
222
+ "# inputs = spike_generator(100,0.01,100)\n",
223
+ "\n",
224
+ "# t_ballena = simulate_ballena_fast(inputs, w=4, tau=4, max_time=100, epochs=5000)\n",
225
+ "# t_nest = simulate_nest_fast(inputs, w=4, tau=4, max_time=100, epochs=5000)\n",
226
+ "\n",
227
+ "# print('t_ballena', t_ballena)\n",
228
+ "# print('t_nest', t_nest)\n",
229
+ "# print(f'ballena es {t_nest/t_ballena} veces mas rapido')"
204
230
  ]
205
231
  },
206
232
  {
207
233
  "cell_type": "code",
208
- "execution_count": null,
234
+ "execution_count": 26,
209
235
  "metadata": {},
210
236
  "outputs": [
211
237
  {
212
- "name": "stdout",
213
- "output_type": "stream",
214
- "text": [
215
- "t_ballena 0.7835497856140137\n",
216
- "t_nest 7.289111375808716\n",
217
- "ballena es 9.302678029701386 veces mas rapido\n"
218
- ]
238
+ "data": {
239
+ "text/plain": [
240
+ "<PoissonGenerator at 0x76a489929f70>"
241
+ ]
242
+ },
243
+ "execution_count": 26,
244
+ "metadata": {},
245
+ "output_type": "execute_result"
219
246
  }
220
247
  ],
221
248
  "source": [
222
- "inputs = spike_generator(100,0.01,100)\n",
223
- "\n",
224
- "t_ballena = simulate_ballena_fast(inputs, w=4, tau=4, max_time=100, epochs=5000)\n",
225
- "t_nest = simulate_nest_fast(inputs, w=4, tau=4, max_time=100, epochs=5000)\n",
226
- "\n",
227
- "print('t_ballena', t_ballena)\n",
228
- "print('t_nest', t_nest)\n",
229
- "print(f'ballena es {t_nest/t_ballena} veces mas rapido')"
249
+ "poisson = ballena.PoissonGenerator( [100,1000,500], 0.05 )\n",
250
+ "poisson"
230
251
  ]
231
252
  }
232
253
  ],
@@ -1,36 +0,0 @@
1
- use pyo3::prelude::*;
2
- use crate::utils::vec_of_tuples;
3
-
4
- #[pyclass]
5
- pub struct Instance{
6
- spikes: Vec<(f64,usize)>
7
- }
8
-
9
- #[pymethods]
10
- impl Instance{
11
- #[new]
12
- fn new(obj: &Bound<'_,PyAny>)->PyResult<Self>{
13
- let mut spikes = match vec_of_tuples::<f64>(obj){
14
- Ok(v) => v,
15
- Err(e) => return Err(e)
16
- };
17
-
18
-
19
- spikes.sort_by(|a,b|a.0.partial_cmp(&b.0).unwrap());
20
-
21
- Ok(Self{spikes})
22
- }
23
-
24
- fn __str__(&self)->String{
25
- format!("Input(count={})",self.spikes.len())
26
- }
27
-
28
- fn __repr__(&self)->String{
29
- self.__str__()
30
- }
31
-
32
- pub fn get(&self)->&Vec<(f64,usize)>{
33
- &self.spikes
34
- }
35
- }
36
-
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes