Path: blob/master/deprecated/notebooks/pf_guided_neural_decoding.ipynb
1192 views
Kernel: Python 3
#Particle filtering for neural decoding (SSM with Poisson likelihood)
Code is from https://github.com/nchopin/particles/blob/master/book/filtering/neurodecoding.py
In [6]:
Out[6]:
fatal: destination path 'particles' already exists and is not an empty directory.
In [7]:
Out[7]:
neuro_ESS_vs_time.pdf neuro_filt_expectation.pdf particles sample_data
In [9]:
Out[9]:
/content/particles
In [10]:
Out[10]:
book _config.yml docs LICENSE particles requirements.txt
CHANGELOG CONTRIBUTING.md INSTALL papers README.md setup.py
In [11]:
Out[11]:
Processing /content/particles
DEPRECATION: A future pip version will change local packages to be built in-place without first copying to a temporary directory. We recommend you use --use-feature=in-tree-build to test your packages with this new behavior before it becomes the default.
pip 21.3 will remove support for this functionality. You can find discussion regarding this at https://github.com/pypa/pip/issues/7555.
Requirement already satisfied: numpy>=1.18 in /usr/local/lib/python3.7/dist-packages (from particles==0.2) (1.19.5)
Requirement already satisfied: scipy>=1.7 in /root/.local/lib/python3.7/site-packages (from particles==0.2) (1.7.1)
Requirement already satisfied: numba in /usr/local/lib/python3.7/dist-packages (from particles==0.2) (0.51.2)
Requirement already satisfied: joblib in /usr/local/lib/python3.7/dist-packages (from particles==0.2) (1.0.1)
Requirement already satisfied: setuptools in /usr/local/lib/python3.7/dist-packages (from numba->particles==0.2) (57.4.0)
Requirement already satisfied: llvmlite<0.35,>=0.34.0.dev0 in /usr/local/lib/python3.7/dist-packages (from numba->particles==0.2) (0.34.0)
Building wheels for collected packages: particles
Building wheel for particles (setup.py) ... done
Created wheel for particles: filename=particles-0.2-py3-none-any.whl size=573163 sha256=ae0ac2b8f94e82aab34a73340dd7d064700f7fb92155179f806ad0567f16acd0
Stored in directory: /tmp/pip-ephem-wheel-cache-1v76wafm/wheels/c4/ec/4d/9651be18bff1d8c3beaff376421029d3d43569a79306f8a862
Successfully built particles
Installing collected packages: particles
Attempting uninstall: particles
Found existing installation: particles 0.2
Uninstalling particles-0.2:
Successfully uninstalled particles-0.2
Successfully installed particles-0.2
MIME type unknown not supported
In [12]:
In [3]:
Out[3]:
---------------------------------------------------------------------------
KeyboardInterrupt Traceback (most recent call last)
/content/particles/book/filtering/neurodecoding.py in <module>()
152 N = 10**4; nruns = 50
153 results = particles.multiSMC(fk=models, N=N, nruns=nruns, nprocs=1,
--> 154 collect=[Moments], store_history=True)
155
156 ## PLOTS
/root/.local/lib/python3.7/site-packages/particles/core.py in multiSMC(nruns, nprocs, out_func, collect, **args)
524 return utils.multiplexer(f=f, nruns=nruns, nprocs=nprocs, seeding=True,
525 protected_args={'collect': collect},
--> 526 **args)
/root/.local/lib/python3.7/site-packages/particles/utils.py in multiplexer(f, nruns, nprocs, seeding, protected_args, **args)
263 op['seed'] = seed
264 # the actual work happens here
--> 265 return distribute_work(f, inputs, outputs, nprocs=nprocs)
/root/.local/lib/python3.7/site-packages/particles/utils.py in distribute_work(f, inputs, outputs, nprocs, out_key)
169 if nprocs <= 1:
170 return [add_to_dict(op, f(**ip), key=out_key)
--> 171 for ip, op in zip(inputs, outputs)]
172
173 delayed_f = joblib.delayed(f)
/root/.local/lib/python3.7/site-packages/particles/utils.py in <listcomp>(.0)
169 if nprocs <= 1:
170 return [add_to_dict(op, f(**ip), key=out_key)
--> 171 for ip, op in zip(inputs, outputs)]
172
173 delayed_f = joblib.delayed(f)
/root/.local/lib/python3.7/site-packages/particles/utils.py in __call__(self, **kwargs)
206 if seed:
207 random.seed(seed)
--> 208 return self.func(**kwargs)
209
210
/root/.local/lib/python3.7/site-packages/particles/core.py in __call__(self, **kwargs)
433 def __call__(self, **kwargs):
434 pf = SMC(**kwargs)
--> 435 pf.run()
436 return self.fun(pf)
437
/root/.local/lib/python3.7/site-packages/particles/utils.py in timed_method(self, **kwargs)
86 def timed_method(self, **kwargs):
87 starting_time = time.perf_counter()
---> 88 out = method(self, **kwargs)
89 self.cpu_time = time.perf_counter() - starting_time
90 return out
/root/.local/lib/python3.7/site-packages/particles/core.py in run(self)
419 command only.
420 """
--> 421 for _ in self:
422 pass
423
/root/.local/lib/python3.7/site-packages/particles/core.py in __next__(self)
392 else:
393 self.resample_move()
--> 394 self.reweight_particles()
395 self.compute_summaries()
396 self.t += 1
/root/.local/lib/python3.7/site-packages/particles/core.py in reweight_particles(self)
334
335 def reweight_particles(self):
--> 336 self.wgts = self.wgts.add(self.fk.logG(self.t, self.Xp, self.X))
337
338 def resample_move(self):
/root/.local/lib/python3.7/site-packages/particles/state_space_models.py in logG(self, t, xp, x)
332
333 def logG(self, t, xp, x):
--> 334 return self.ssm.PY(t, xp, x).logpdf(self.data[t])
335
336 def Gamma0(self, u):
/root/.local/lib/python3.7/site-packages/particles/distributions.py in logpdf(self, x)
858
859 def logpdf(self, x):
--> 860 return sum([d.logpdf(x[..., i]) for i, d in enumerate(self.dists)])
861 # ellipsis: in case x is of shape (d) instead of (N, d)
862
/root/.local/lib/python3.7/site-packages/particles/distributions.py in <listcomp>(.0)
858
859 def logpdf(self, x):
--> 860 return sum([d.logpdf(x[..., i]) for i, d in enumerate(self.dists)])
861 # ellipsis: in case x is of shape (d) instead of (N, d)
862
/root/.local/lib/python3.7/site-packages/particles/distributions.py in logpdf(self, x)
470
471 def logpdf(self, x):
--> 472 return stats.poisson.logpmf(x, self.rate)
473
474 def ppf(self, u):
/root/.local/lib/python3.7/site-packages/scipy/stats/_distn_infrastructure.py in logpmf(self, k, *args, **kwds)
3178 k = asarray((k-loc))
3179 cond0 = self._argcheck(*args)
-> 3180 cond1 = (k >= _a) & (k <= _b) & self._nonzero(k, *args)
3181 cond = cond0 & cond1
3182 output = empty(shape(cond), 'd')
KeyboardInterrupt:
In [13]:
In [14]:
In [15]:
Out[15]:
<matplotlib.legend.Legend at 0x7f6732a6fe90>
In [16]:
Out[16]:
<matplotlib.legend.Legend at 0x7f6731d3cb90>
In [17]:
Out[17]:
In [18]:
Out[18]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f6733b6fc50>
In [19]:
Out[19]:
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:9: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.
if __name__ == '__main__':