Skip to content

Commit

Permalink
changes in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
dokato committed Aug 20, 2015
1 parent cced108 commit 3eaee0e
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 43 deletions.
37 changes: 27 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,41 @@ Python connectivity module.
It is a part of [GSOC 2015](http://www.google-melange.com/gsoc/project/details/google/gsoc2015/dokato/5649050225344512) project.
Project blog is [here](http://dokato.github.io/connpy-blog/).

## Content

* Data - wrapper of your data
* Connectivity - classes with connectivity estimators
* Mvar fitting - this submodule includes some MVAR algorithms

## License
BSD 2-clause

## Installation

Documentation on [ReadTheDocs](http://connectivipy.readthedocs.org/).

## Content
## Installation

Option 1: using GIT

> git clone https://github.com/dokato/connectivipy.git
> cd connectivipy
> python setup.py install
* Data
* Connectivity
* Mvar fitting
Option 2: Download ZIP from the menu on the right, unzip it and go
in terminal to that folder. Than just execute:
> python setup install
##Changelog
###Changelog

### 0.34
#### 0.34
* short-time statistics
* documentation in sphinx ready on readthedocs
* visualization improved
* conversion to trans3d
* more examples

### 0.31
#### 0.31
* connectivity methods: gDTF, gPDC, Coherency, PSI, GCI
* new mvar estimation criterion: FPE
* statistics for multitrial (bootstrap) and normal case (surrogate data)
Expand All @@ -30,15 +47,15 @@ Documentation on [ReadTheDocs](http://connectivipy.readthedocs.org/).
* data plotting
* working example

### 0.2
#### 0.2
* connectivity methods: DTF, PDC, Partial Coherence, iPDC
* mvar estimation criterions
* mvar class static

### 0.1
#### 0.1
* data class with simple preprocessing methods
* mvar class almost done

### 0.05
#### 0.05
* project structure
* basic fitting
9 changes: 3 additions & 6 deletions connectivipy/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ def plot_data(self, trial=0, show=True):
*show* = True : boolean
show the plot or not
'''

time = np.arange(0,self.__length)*1./self.__fs
if self.__multitrial:
plotdata = self.__data[:,:,trial]
Expand Down Expand Up @@ -437,13 +436,12 @@ def export_trans3d(self, filename='conn_trnas3d.dat', freq_band=[]):
*freq_band* = [] : list
frequency range [from_value, to_value] in Hz.
'''
trdef_str = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, -8, 0, 0, 0, 1]
tr_str = [0.0051, 0.0074, -3.4523, 0., -2.7277, 2.1161, 0.0005, 0., 2.1161, 2.7276, 0., -8., 0., 0., 0., 1.]
content = ";electrodes = " + " ".join(self.__channames)
content += "\r\n;start = -0.500000\r\n"
content += ";samplerate = 12\r\n"
content += ";transform_default = " + " ".join([ str(x) for x in trdef_str[:self.__chans]]) + "\r\n"
content += ";transform = " + " ".join([ str(x) for x in tr_str[:self.__chans]]) + "\r\n"
# two following lines define initial position of head model
content += ";transform_default = 1 0 0 0 0 1 0 0 0 0 1 -8 0 0 0 1\r\n"
content += ";transform = 0.005148315144289768 0.007407087180879943 -3.4522594293647604 0.0 -2.727691946877633 2.116098522619611 0.000472475639 0.0 2.1160923126171305 2.7276819307525173 0.009008154977586572 -8.0 0.000000 0.000000 0.000000 1.000000\r\n"
content += "\r\n"
# integrate value of estimator in given frequency band
freqs = np.linspace(0, int(self.__fs/2), self.__estim.shape[0])
Expand All @@ -460,7 +458,6 @@ def export_trans3d(self, filename='conn_trnas3d.dat', freq_band=[]):
fl.write(content)

# auxiliary methods:

def __make_params_dict(self, args):
"""
Making list of parameters from *self._parameters*
Expand Down
1 change: 0 additions & 1 deletion connectivipy/mvarmodel.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
#! /usr/bin/env python

import numpy as np
Expand Down
42 changes: 29 additions & 13 deletions doc/tutorial.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
.. _tutorial:

Tutorial
Tutorials
==================

All examples and tutorial will go here.
Installation
########

The easiest way is to use *GIT* and just execute:

.. code-block:: bash
$ git clone https://github.com/dokato/connectivipy.git
$ cd connectivipy
$ python setup.py install
Data class example
########
Expand All @@ -14,10 +23,14 @@ Data class example
import numpy as np
import connectivipy as cp
from connectivipy.mvar.fitting import mvar_gen
from connectivipy import mvar_gen
# MVAR model coefficients
### MVAR model coefficients
# let's build mvar model matrix
A = np.zeros((2, 5, 5))
# 2 - first dimension is model order
# 5 - second and third dimensions mean number of channels
A[0, 0, 0] = 0.95 * 2**0.5
A[1, 0, 0] = -0.9025
A[0, 1, 0] = -0.5
Expand All @@ -28,36 +41,39 @@ Data class example
A[0, 4, 3] = -0.25 * 2**0.5
A[0, 4, 4] = 0.25 * 2**0.5
# multitrial signal generation
ysig = np.zeros((5,10**3,5))
# multitrial signal generation from a matrix above
# let's generate 5-channel signal with 1000 data points
# and 5 trials using function mvar_gen
ysig = np.zeros((5, 10**3, 5))
ysig[:,:,0] = mvar_gen(A,10**3)
ysig[:,:,1] = mvar_gen(A,10**3)
ysig[:,:,2] = mvar_gen(A,10**3)
ysig[:,:,3] = mvar_gen(A,10**3)
ysig[:,:,4] = mvar_gen(A,10**3)
# connectivity analysis
#### connectivity analysis
data = cp.Data(ysig,128, ["Fp1", "Fp2","Cz", "O1","O2"])
# you may want to plot data (in multitrial case average along trials
# is shown)
# you may want to plot data (in multitrial case only one trial is shown)
data.plot_data()
# fit mvar using specific algorithm
# fit mvar using Yule-Walker algorithm and order 2
data.fit_mvar(2,'yw')
# you can capture fitted parameters and residual matrix
ar, vr = data.mvar_coefficients
# connectivity estimators
# now we investigate connectivity using gDTF
gdtf_values = data.conn('gdtf')
#gdtf_significance = data.significance(Nrep=200, alpha=0.05)
gdtf_significance = data.significance(Nrep=200, alpha=0.05)
data.plot_conn('gDTF')
# short time version with default parameters
pdc_shorttime = data.short_time_conn('pdc', nfft=1, no=10)
pdc_shorttime = data.short_time_conn('pdc', nfft=100, no=10)
data.plot_short_time_conn("PDC")
How to use specific classes
########

Expand Down
26 changes: 16 additions & 10 deletions examples/example1.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@

import numpy as np
import connectivipy as cp
from connectivipy.mvar.fitting import mvar_gen
from connectivipy import mvar_gen

# MVAR model coefficients
### MVAR model coefficients

# let's build mvar model matrix
A = np.zeros((2, 5, 5))
# 2 - first dimension is model order
# 5 - second and third dimensions mean number of channels
A[0, 0, 0] = 0.95 * 2**0.5
A[1, 0, 0] = -0.9025
A[0, 1, 0] = -0.5
Expand All @@ -17,30 +21,32 @@
A[0, 4, 3] = -0.25 * 2**0.5
A[0, 4, 4] = 0.25 * 2**0.5

# multitrial signal generation
ysig = np.zeros((5,10**3,5))
# multitrial signal generation from a matrix above
# let's generate 5-channel signal with 1000 data points
# and 5 trials using function mvar_gen
ysig = np.zeros((5, 10**3, 5))
ysig[:,:,0] = mvar_gen(A,10**3)
ysig[:,:,1] = mvar_gen(A,10**3)
ysig[:,:,2] = mvar_gen(A,10**3)
ysig[:,:,3] = mvar_gen(A,10**3)
ysig[:,:,4] = mvar_gen(A,10**3)

# connectivity analysis
#### connectivity analysis

data = cp.Data(ysig,128, ["Fp1", "Fp2","Cz", "O1","O2"])

# you may want to plot data (in multitrial case average along trials
# is shown)
# you may want to plot data (in multitrial case only one trial is shown)
data.plot_data()

# fit mvar using specific algorithm
# fit mvar using Yule-Walker algorithm and order 2
data.fit_mvar(2,'yw')

# you can capture fitted parameters and residual matrix
ar, vr = data.mvar_coefficients

# connectivity estimators
# now we investigate connectivity using gDTF
gdtf_values = data.conn('gdtf')
#gdtf_significance = data.significance(Nrep=200, alpha=0.05)
gdtf_significance = data.significance(Nrep=200, alpha=0.05)
data.plot_conn('gDTF')

# short time version with default parameters
Expand Down
7 changes: 4 additions & 3 deletions examples/example2.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import numpy as np
import matplotlib.pyplot as plt
import connectivipy as cp
from connectivipy.mvar.fitting import mvar_gen
from connectivipy import mvar_gen

fs = 256.
acf = np.zeros((3,3,3))
# matrix shape meaning (p,k,k) k - number of channels,
# matrix shape meaning
# (p,k,k) k - number of channels,
# p - order of mvar parameters

acf[0,0,0] = 0.3
Expand All @@ -22,7 +23,7 @@
# assign static class cp.Mvar to variable mv
mv = cp.Mvar

# find best model order
# find best model order using Vieira-Morf algorithm
best, crit = mv.order_akaike(y,15,'vm')
plt.plot(1+np.arange(len(crit)),crit,'g')
plt.show()
Expand Down

0 comments on commit 3eaee0e

Please sign in to comment.