Changeset 182
- Timestamp:
- 07/24/08 10:39:35 (4 months ago)
- Files:
-
- trunk/src/spikes.py (modified) (25 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/spikes.py
r181 r182 10 10 except Exception: 11 11 print "Warning: pylab not present" 12 12 13 13 14 class SpikeTrain(object): … … 194 195 raise Exception("No spikes in the SpikeTrain !") 195 196 196 # Return a time axis between t_start and t_stop according to a time_bin197 197 def time_axis(self, time_bin): 198 """ 199 Return a time axis between t_start and t_stop according to a time_bin 200 """ 198 201 return numpy.arange(self.t_start, self.t_stop, time_bin) 199 202 200 # Generate a raster plot with the SpikeTrain in a subwindow of interest,201 # defined by t_start and t_stop. Those are not the global t_start and t_stop202 # of the SpikeTrain objects. If not defined, we use the one of the SpikeTrain203 # object204 203 def raster_plot(self, t_start=None, t_stop=None, color='b'): 204 """ 205 Generate a raster plot with the SpikeTrain in a subwindow of interest, 206 defined by t_start and t_stop. Those are not the global t_start and t_stop 207 of the SpikeTrain objects. If not defined, we use the one of the SpikeTrain 208 object 209 """ 205 210 if t_start is None: 206 211 t_start = self.t_start … … 397 402 return len(self.spiketrains) 398 403 399 # Add a SpikeTrain object to the SpikeList400 404 def append(self, id, spiketrain): 405 """ 406 Add a SpikeTrain object to the SpikeList 407 """ 401 408 assert isinstance(spiketrain, SpikeTrain), "A SpikeList object can only contain SpikeTrain objects" 402 409 if id in self.id_list: … … 408 415 self.__calc_startstop() 409 416 410 # Returns the time parameters of the SpikeList (t_start, t_stop, dt)411 417 def get_time_parameters(self): 418 """ 419 Returns the time parameters of the SpikeList (t_start, t_stop, dt) 420 """ 412 421 return (self.t_start, self.t_stop, self.dt) 413 422 … … 416 425 return numpy.arange(self.t_start, self.t_stop, time_bin) 417 426 418 # Allow the concatenation of a list of SpikeLists to the current SpikeList419 427 def concatenate(self, SpikeList_list): 428 """ 429 Concatenation of a list of SpikeLists to the current SpikeList 430 """ 420 431 # We check that Spike Lists have similar time_axis 421 432 sl_= SpikeList_list[0] … … 427 438 self.append(id, sl.spiketrains[id]) 428 439 429 # Generate a new SpikeList truncated from a particular sublist of cells440 430 441 def idsubSpikeList(self, id_list): 442 """ 443 Generate a new SpikeList truncated from a particular sublist of cells 444 """ 431 445 # We check what are the elements that are in self.id_list and not in 432 446 # id_list. We remove such elements from the SpikeList … … 441 455 return new_SpkList 442 456 443 # Generate a new SpikeList truncated from particular time boundaries444 457 def timesubSpikeList(self, t_start, t_stop): 445 458 """ 459 Generate a new SpikeList truncated from particular time boundaries 446 460 returns a new SpikeList 447 461 … … 458 472 ####################################################################### 459 473 460 # Return the list of all the isi vectors for all the SpikeTrains objects 461 # within the SpikeList. If display is True, then it plots the distribution 462 # of the ISI 474 463 475 def isi(self, nbins=100, display=False): 476 """ 477 Return the list of all the isi vectors for all the SpikeTrains objects 478 within the SpikeList. If display is True, then it plots the distribution 479 of the ISI 480 """ 464 481 isis = [] 465 482 for idx,id in enumerate(self.id_list): … … 489 506 return numpy.histogram(isis, bins=bins) 490 507 491 # Return the list of all the cv coefficients for all the SpikeTrains objects 492 # within the SpikeList. If display is True, then it plots the distribution 493 # of the CVs 508 494 509 def cv_isi(self, nbins=100, display=False): 510 """ 511 Return the list of all the cv coefficients for all the SpikeTrains objects 512 within the SpikeList. If display is True, then it plots the distribution 513 of the CVs 514 """ 495 515 cvs_isi = [] 496 516 for idx,id in enumerate(self.id_list): … … 517 537 return numpy.arange(self.t_start, self.t_stop, time_bin) 518 538 519 520 # Return the mean firing rate averaged accross all SpikeTrains521 539 def mean_rate(self, t_start=None, t_stop=None): 522 540 """ 523 Mean rate for the population541 Return the mean firing rate averaged accross all SpikeTrains 524 542 525 543 see mean_rates … … 527 545 return numpy.mean(self.mean_rates(t_start, t_stop)) 528 546 529 # Returns the std of the mean firing rate averaged accross all SpikeTrains530 547 def mean_rate_std(self, t_start=None, t_stop=None): 531 548 """ 532 Std of the Mean firing rate for the population549 Std deviation of the Mean firing rate averaged accross all SpikeTrains 533 550 534 551 see mean_rate … … 539 556 """ returns a vector of the size of id_list giving the mean rate for each neuron 540 557 541 542 558 see SpikeTrain.mean_rate 543 559 """ … … 547 563 548 564 return rates 549 550 # Return a vector with all the mean firing rates for all SpikeTrains. 551 # If display is True, then it plots the distribution of the rates 565 552 566 def rate_distribution(self, nbins=25, normalize=True, display=False): 567 """ 568 Return a vector with all the mean firing rates for all SpikeTrains. 569 If display is True, then it plots the distribution of the rates 570 """ 553 571 #rates = numpy.zeros(self.N(), float) 554 572 #for idx,id in enumerate(self.id_list): … … 563 581 pylab.xlabel("Average Firing Rate (Hz)", size="x-large") 564 582 565 # Generate an array with all the spike_histograms of all the SpikeTrains566 # objects within the SpikeList. If display is True, then it plots the567 # mean firing rate of the all population along time568 583 def spike_histogram(self, time_bin, normalized=False, display=False): 584 """ 585 Generate an array with all the spike_histograms of all the SpikeTrains 586 objects within the SpikeList. If display is True, then it plots the 587 mean firing rate of the all population along time 588 """ 569 589 nbins = numpy.ceil((self.t_stop-self.t_start)/time_bin) 570 590 firing_rate = numpy.zeros((self.N(),nbins), float) … … 580 600 pylab.ylabel("Mean Number of Spikes per bin", size="x-large") 581 601 pylab.xlabel("Time (ms)", size="x-large") 582 583 602 584 603 def firing_rate(self, time_bin, display=False): … … 611 630 return fano_factors 612 631 613 # Allow to translate a gid (ranging from 0 to N*M) into 614 # a position on a N*M grid. dims should be given as a tuple 615 # (N,M) 632 616 633 def id2position(self, id, dims): 634 """ 635 Allow to translate a gid (ranging from 0 to N*M) into 636 a position on a N*M grid. dims should be given as a tuple 637 (N,M) 638 """ 617 639 # Then we translate it in 2D 618 640 if len(dims) == 1: … … 623 645 return (x,y) 624 646 625 # Generate a map of the activity during t_start and t_stop. 626 # If dims is a tuple, then cells are placed on a grid of size 627 # (N,M), else if dims is an array of size (2,nb_cells) with the 628 # x (first line) and y (second line) flotting positions of the cells, 629 # we generate a scatter plot. bounds is a parameters allowing to specify 630 # the range of the colorbar 647 631 648 def activity_map(self, dims, bounds=None, display=False): 649 """ 650 Generate a map of the activity during t_start and t_stop. 651 If dims is a tuple, then cells are placed on a grid of size 652 (N,M), else if dims is an array of size (2,nb_cells) with the 653 x (first line) and y (second line) flotting positions of the cells, 654 we generate a scatter plot. bounds is a parameters allowing to specify 655 the range of the colorbar 656 """ 632 657 if isinstance(dims, tuple) or isinstance(dims, list): 633 658 activity_map = numpy.zeros(dims,float) … … 661 686 pylab.clim(bounds) 662 687 663 # Function to generate an array of cross correlation between computed664 # between pairs of cells within the SpikeTrains. pairs should be therefore665 # a list of (cell_id_1, cell_id_2). If display is True, then if plots the666 # averaged cross correlation over all those pairs667 688 def pairwise_correlations(self, pairs, time_bin=1., display=False): 689 """ 690 Function to generate an array of cross correlation between computed 691 between pairs of cells within the SpikeTrains. pairs should be therefore 692 a list of (cell_id_1, cell_id_2). If display is True, then if plots the 693 averaged cross correlation over all those pairs 694 """ 668 695 if len(pairs[0]) != len(pairs[1]): 669 696 raise Exception("Pairs should have the same number of elements") … … 691 718 return results 692 719 693 694 # Function to extract the variance of the firing rate along time,695 # if events are binned with a time bin.696 720 def mean_rate_variance(self, time_bin): 721 """ 722 Function to extract the variance of the firing rate along time, 723 if events are binned with a time bin. 724 """ 697 725 firing_rate = self.firing_rate(time_bin) 698 726 return numpy.var(sum(firing_rate)/self.N()) 699 727 700 # Function to extract the covariance of the firing rate along time,701 # if events are binned with a time bin. We need a second SpikeList702 # object with same time parameters to calculate the covariance703 728 def mean_rate_covariance(self, SpkList, time_bin): 729 """ 730 Function to extract the covariance of the firing rate along time, 731 if events are binned with a time bin. We need a second SpikeList 732 object with same time parameters to calculate the covariance 733 """ 704 734 if not isinstance(SpkList, SpikeList): 705 735 raise Exception("Error, argument should be a SpikeList object") … … 713 743 cov = sum(frate_1*frate_2)/N-sum(frate_1)*sum(frate_2)/(N*N) 714 744 return cov 715 716 745 717 746 def raster_plot(self, id_list=None, t_start=None, t_stop=None, colors='b', subplot=None, size=1): … … 790 819 for fname in files: os.remove(fname) 791 820 792 793 794 821 def pw_corr_pearson(self,edge,bins,number_of_neuron_pairs): 795 822 """ … … 801 828 [neuron_ids_array, spike_times_array] = self.as_list_id_list_time() 802 829 803 804 805 830 # Pairwise correlation 806 831 neuron_ids_unique = numpy.unique(neuron_ids_array) … … 888 913 return self.firing_rate(self.dt) 889 914 890 891 915 def as_pyNN_SpikeArray(self): 892 916 """ … … 896 920 897 921 """ 898 899 922 spike_array = list([ [] for i in range(self.N())]) 900 923 for spike in spikes: … … 922 945 return output_DATA 923 946 924 # Function to read data. Should be optimize to deal with errors in the file925 # that may append sometimes, when nest1 does not save the entire last line.926 # It assumes that the data have been produced by pyNN under the format927 # time (ms) gids (for raster)928 # value (unit) gids (for continuous recordings like Vm, current, conductances)929 947 def readFile(filename, sepchar = "\t", skipchar = '#'): 948 """ 949 Function to read data. Should be optimize to deal with errors in the file 950 that may append sometimes, when nest1 does not save the entire last line. 951 It assumes that the data have been produced by pyNN under the format 952 time (ms) gids (for raster) 953 value (unit) gids (for continuous recordings like Vm, current, conductances) 954 """ 930 955 myfile = open(filename, "r", 10000) 931 956 contents = myfile.readlines()

