Changeset 210
- Timestamp:
- 09/24/08 10:19:05 (4 months ago)
- Files:
-
- branches/cleanup/src/signals.py (modified) (47 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/cleanup/src/signals.py
r209 r210 127 127 return self.spike_times[i:j] 128 128 129 def copy(self): 130 """ 131 Return a copy of the SpikeTrain object 132 """ 133 return SpikeTrain(self.spike_times, self.dt, self.t_start, self.t_stop) 134 129 135 def __getdisplay__(self,display): 130 136 """ … … 345 351 pylab.draw() 346 352 353 def add_offset(self, offset): 354 """ 355 Add an offset to the SpikeTrain object. t_start and t_stop are 356 shifted from offset, so does all the spike times. 357 358 Inputs: 359 offset - the time offset, in ms 360 361 Examples: 362 >>>spktrain = SpikeTrain(arange(0,100,10)) 363 >>>spktrain.add_offset(50) 364 >>>spklist.spike_times 365 >>>[ 50., 60., 70., 80., 90., 100., 110., 366 120., 130., 140.] 367 """ 368 self.t_start += offset 369 self.t_stop += offset 370 self.spike_times += offset 347 371 348 372 def time_slice(self, t_start, t_stop): … … 611 635 SpikeList, loadSpikeList 612 636 """ 613 self.id_list = id_list 614 self.t_start = t_start 615 self.t_stop = t_stop 616 self.dt = dt 637 self.t_start = t_start 638 self.t_stop = t_stop 639 self.dt = dt 617 640 self.dimensions = dims 618 self.N = len(id_list)619 641 self.spiketrains = {} 620 642 … … 622 644 self.spiketrains[id] = [] 623 645 for id,time in spikes: 624 if id in self.id_list : #id_list can be a subset of the list of recorded neurons646 if id in self.id_list(): #id_list can be a subset of the list of recorded neurons 625 647 self.spiketrains[id].append(time) 626 648 … … 632 654 self.__calc_startstop() 633 655 656 def id_list(self): 657 """ 658 Return the list of all the cells ids contained in the 659 SpikeList object 660 """ 661 return numpy.array(self.spiketrains.keys()) 662 663 def copy(self): 664 """ 665 Return a copy of the SpikeList object 666 """ 667 # Maybe not optimal, should be optimized 668 spklist = SpikeList([], [], self.dt, self.t_start, self.t_stop, self.dimensions) 669 for id in self.id_list(): 670 spklist.append(id, self.spiketrains[id]) 671 return spklist 672 634 673 def __calc_startstop(self): 635 674 """ … … 640 679 if len(self) > 0: 641 680 if self.t_start is None: 642 start_times = numpy.array([self.spiketrains[idx].t_start for idx in self.id_list ])681 start_times = numpy.array([self.spiketrains[idx].t_start for idx in self.id_list()]) 643 682 self.t_start = numpy.min(start_times) 644 683 logging.debug("Warning, t_start is infered from the data : %f" %self.t_start) … … 646 685 self.spiketrains[id].t_start = self.t_start 647 686 if self.t_stop is None: 648 stop_times = numpy.array([self.spiketrains[idx].t_stop for idx in self.id_list ])687 stop_times = numpy.array([self.spiketrains[idx].t_stop for idx in self.id_list()]) 649 688 self.t_stop = numpy.max(stop_times) 650 689 logging.debug("Warning, t_stop is infered from the data : %f" %self.t_stop) … … 657 696 return self.spiketrains[id] 658 697 659 #def __getslice__(self, i, j): 698 def __getslice__(self, i, j): 699 """ 700 Return a new SpikeList object with all the ids between i and j 701 """ 702 ids = numpy.where((self.id_list() > i) and (self.id_list() < j)) 703 return self.id_slice(ids) 704 660 705 #def __setslice__(self, i, j): 661 706 … … 663 708 assert isinstance(spktrain, SpikeTrain), "A SpikeList object can only contain SpikeTrain objects" 664 709 self.spiketrains[id] = spktrain 665 if not id in self.id_list:666 self.id_list.append(id)667 710 self.__calc_startstop() 668 711 … … 673 716 return len(self.spiketrains) 674 717 718 def __sub_id_list(self, sub_list=None): 719 if sub_list == None: 720 return self.id_list() 721 if type(sub_list) == int: 722 return numpy.random.permutation(self.id_list())[0:sub_list] 723 if type(sub_list) == list: 724 return sub_list 725 675 726 def __getdisplay__(self,display): 676 727 """ … … 725 776 """ 726 777 assert isinstance(spktrain, SpikeTrain), "A SpikeList object can only contain SpikeTrain objects" 727 if id in self.id_list :778 if id in self.id_list(): 728 779 raise Exception("id %d already present in SpikeList. Use __setitem__ (spk[id]=...) instead()" %id) 729 780 else: 730 781 self.spiketrains[id] = spktrain.time_slice(self.t_start, self.t_stop) 731 self.id_list.append(id) 732 self.N += 1 733 782 734 783 def get_time_parameters(self): 735 784 """ … … 770 819 raise Exception("Spike Lists should have similar time_axis") 771 820 for sl in spklists: 772 for id in sl.id_list :821 for id in sl.id_list(): 773 822 self.append(id, sl.spiketrains[id]) 774 823 … … 781 830 """ 782 831 for id, spiketrain in spikelist.spiketrains.items(): 783 if id in self.id_list :832 if id in self.id_list(): 784 833 self.spiketrains[id].merge(spiketrain, relative_times) 785 834 else: … … 810 859 """ 811 860 new_SpkList = SpikeList([], [], self.dt, self.t_start, self.t_stop, self.dimensions) 812 if isinstance(id_list, int): 813 id_list = numpy.random.permutation(self.id_list)[0:id_list] 861 id_list = self.__sub_id_list(id_list) 814 862 for id in id_list: 815 863 try: … … 831 879 """ 832 880 new_SpkList = SpikeList([], [], self.dt, t_start, t_stop, self.dimensions) 833 for id in self.id_list :881 for id in self.id_list(): 834 882 new_SpkList.append(id, self.spiketrains[id].time_slice(t_start, t_stop)) 835 883 new_SpkList.__calc_startstop() 836 884 return new_SpkList 837 885 886 def add_offset(self, offset): 887 """ 888 Add an offset to the whole SpikeList object. t_start and t_stop are 889 shifted from offset, so does all the SpikeTrain. 890 891 Inputs: 892 offset - the time offset, in ms 893 894 Examples: 895 >>>spklist.t_start 896 >>> 1000 897 >>>spklist.add_offset(50) 898 >>>spklist.t_start 899 >>> 1050 900 """ 901 self.t_start += offset 902 self.t_stop += offset 903 for id in self.id_list(): 904 self.spiketrains[id].add_offset(offset) 838 905 839 906 def save(self, filename, method="text"): … … 870 937 """ 871 938 isis = [] 872 for id in self.id_list :939 for id in self.id_list(): 873 940 isis.append(self.spiketrains[id].isi()) 874 941 return isis … … 915 982 """ 916 983 cvs_isi = [] 917 for id x,id in enumerate(self.id_list):984 for id in self.id_list(): 918 985 isi = self.spiketrains[id].isi() 919 986 if len(isi) > 1: … … 1008 1075 """ 1009 1076 rates = [] 1010 for id in self.id_list :1077 for id in self.id_list(): 1011 1078 rates.append(self.spiketrains[id].mean_rate(t_start, t_stop)) 1012 1079 return rates … … 1059 1126 """ 1060 1127 nbins = self.time_axis(time_bin) 1061 spike_hist = numpy.zeros((self.N, len(nbins)), float) 1128 N = len(self) 1129 spike_hist = numpy.zeros((N, len(nbins)), float) 1062 1130 subplot = self.__getdisplay__(display) 1063 for idx,id in enumerate(self.id_list ):1131 for idx,id in enumerate(self.id_list()): 1064 1132 spike_hist[idx,:] = self.spiketrains[id].time_histogram(time_bin, normalized) 1065 1133 if not subplot or not ENABLE_PLOTS: … … 1072 1140 xlabel = "Time (ms)" 1073 1141 self.__labels__(subplot, xlabel, ylabel) 1074 subplot.plot(self.time_axis(time_bin),numpy.sum(spike_hist, axis=0)/ self.N,**kwargs)1142 subplot.plot(self.time_axis(time_bin),numpy.sum(spike_hist, axis=0)/N,**kwargs) 1075 1143 pylab.draw() 1076 1144 … … 1123 1191 """ 1124 1192 fano_factors = [] 1125 for id in self.id_list :1193 for id in self.id_list(): 1126 1194 try: 1127 1195 fano_factors.append(self.spiketrains[id].fano_factor_isi()) … … 1200 1268 activity_map = numpy.zeros(self.dimensions, float) 1201 1269 rates = spklist.mean_rates() 1202 for id in spklist.id_list :1270 for id in spklist.id_list(): 1203 1271 position = spklist.id2position(id) 1204 1272 activity_map[position] = rates[id] … … 1283 1351 pylab.draw() 1284 1352 1353 1354 def CrossCorrZero(self, pairs, spklist=None, time_bin=1., mode="auto"): 1355 #Just the Aertsen Value at 0 delay 1356 t_start = float(time_parameters[0]) 1357 t_stop = float(time_parameters[1]) 1358 1359 spk1 = CutSpikeList(f1, time_parameters) 1360 spk2 = CutSpikeList(f2, time_parameters) 1361 1362 if ((size(spk1)==0) or (size(spk2)==0)): 1363 return 0 1364 1365 if mode == "auto": 1366 if (selected_set==None): 1367 selected_set = GetCells(spk1) 1368 if isinstance(selected_set,int): 1369 all_positions = GetCells(spk1) 1370 selected_set = numpy.random.permutation(all_positions)[0:selected_set] 1371 spk1 = CutSpikeList2(spk1,selected_set) 1372 spk2 = CutSpikeList2(spk2,selected_set) 1373 cells_id = sort(selected_set) 1374 for idx in xrange(len(spk1[0])): 1375 spk1[1][idx] = numpy.where(cells_id == spk1[1][idx])[0] 1376 for idx in xrange(len(spk2[0])): 1377 spk2[1][idx] = numpy.where(cells_id == spk2[1][idx])[0] 1378 nb_neur = len(selected_set) 1379 1380 if mode == "cross": 1381 # If nothing is entered, we perform the cross correlation between the two 1382 # whole spike trains 1383 if (selected_set==None): 1384 cell_1 = GetCells(spk1) 1385 cell_2 = GetCells(spk2) 1386 N = min(len(cell_1),len(cell_2)) 1387 cell_1 = cell_1[0:N] 1388 cell_2 = cell_2[0:N] 1389 1390 # If we want to select only X pairs of cells to performs the auto 1391 # correlation, we select those random pairs. 1392 if isinstance(selected_set,int): 1393 N = selected_set 1394 all_positions = GetCells(spk1) 1395 shuffle = numpy.random.permutation(all_positions) 1396 cell_1 = shuffle[0:N] 1397 # If we perform a cross correlation within the same spike file, 1398 # we prevent the same neuron to be in the two signals, to have a real 1399 # non overlapping cross correlation 1400 if not(any(equal(spk1,spk2) == False)): 1401 shuffle = numpy.random.permutation(all_positions) 1402 cell_2 = shuffle[0:N] 1403 else: 1404 # Otherwise we don't care... 1405 all_positions = GetCells(spk2) 1406 shuffle = numpy.random.permutation(all_positions) 1407 cell_2 = shuffle[0:N] 1408 spk1 = CutSpikeList2(spk1,cell_1) 1409 spk2 = CutSpikeList2(spk2,cell_2) 1410 cells_id_1 = sort(cell_1) 1411 cells_id_2 = sort(cell_2) 1412 for count, cell in enumerate(cells_id_1): 1413 spk1[1][numpy.where(cell == spk1[1])] = count 1414 for count, cell in enumerate(cells_id_2): 1415 spk2[1][numpy.where(cell == spk2[1])] = count 1416 nb_neur = len(cells_id_1) 1417 1418 1419 num_bins = int((t_stop-t_start)/time_bin)+1 1420 mat_neur1 = numpy.zeros((num_bins,nb_neur)) 1421 mat_neur2 = numpy.zeros((num_bins,nb_neur)) 1422 1423 timespk1 = numpy.array(((spk1[0] - t_start)/time_bin),'int') 1424 timespk2 = numpy.array(((spk2[0] - t_start)/time_bin),'int') 1425 1426 mat_neur1[timespk1,spk1[1]] = 1 1427 mat_neur2[timespk2,spk2[1]] = 1 1428 Z = float(sum(sum(mat_neur1*mat_neur2))) 1429 1430 N = float(num_bins*nb_neur) 1431 X = float(len(spk1[0])) 1432 Y = float(len(spk2[0])) 1433 1434 X=X/N 1435 Y=Y/N 1436 Z=Z/N 1437 1438 return (Z-X*Y)/sqrt((X*(1-X))*(Y*(1-Y))) 1285 1439 1286 1440 def VictorPurpuraDistance(self, id_list, spklist=None, cost=0.5): … … 1407 1561 subplot = self.__getdisplay__(display) 1408 1562 if id_list == None: 1409 id_list = self.id_list 1563 id_list = self.id_list() 1410 1564 spk = self 1411 el if id_list != self.id_list:1565 else: 1412 1566 spk = self.id_slice(id_list) 1413 1567 … … 1423 1577 idx = numpy.where((spike_times >= t_start) & (spike_times <= t_stop))[0] 1424 1578 if len(spike_times) > 0: 1425 logging.debug("Plotting %d points for %s" % (len(spike_times), self.label))1426 1579 subplot.scatter(spike_times, ids, **kwargs) 1427 1580 xlabel = "Time (ms)" … … 1596 1749 time and id. 1597 1750 1598 relative is a boolean to say if a relative representation of the spikes times 1599 compared to t_start is needed 1600 quantized is a boolean to round the spikes_times. 1601 1602 For exemple: 1603 spk.convert("[times, ids]") will return a list of two elements, the 1751 Inputs: 1752 relative - a boolean to say if a relative representation of the spikes 1753 times compared to t_start is needed 1754 quantized - a boolean to round the spikes_times. 1755 1756 Examples: 1757 >>>spk.convert("[times, ids]") will return a list of two elements, the 1604 1758 first one being the array of all the spikes, the second the array of all the 1605 1759 corresponding ids 1606 spk.convert("[(time,id)]") will return a list of tuples (time, id)1760 >>>spk.convert("[(time,id)]") will return a list of tuples (time, id) 1607 1761 1608 1762 See also … … 1697 1851 return len(self.signal) 1698 1852 1853 def copy(self): 1854 """ 1855 Return a copy of the AnalogSignal object 1856 """ 1857 return AnalogSignal(self.signal, self.dt, self.t_start, self.t_stop) 1858 1699 1859 def time_axis(self): 1700 1860 """ … … 1719 1879 class AnalogSignalList(object): 1720 1880 """ 1721 AnalogSignalList(signals, id_list, dt=None, t_start=None, t_stop=None )1881 AnalogSignalList(signals, id_list, dt=None, t_start=None, t_stop=None, dims=None) 1722 1882 1723 1883 Return a AnalogSignalList object which will be a list of AnalogSignal objects. … … 1738 1898 """ 1739 1899 def __init__(self, signals, id_list, dt=None, t_start=None, t_stop=None, dims=None): 1740 self.id_list = id_list1741 self.N = len(id_list)1742 1900 self.t_start = t_start 1743 1901 self.t_stop = t_stop … … 1759 1917 self.__calc_startstop() 1760 1918 1919 def id_list(self): 1920 """ 1921 Return the list of all the cells ids contained in the 1922 SpikeList object 1923 """ 1924 return numpy.array(self.analog_signals.keys()) 1925 1926 def copy(self): 1927 """ 1928 Return a copy of the AnalogSignalList object 1929 """ 1930 # Maybe not optimal, should be optimized 1931 aslist = AnalogSignalList([], [], self.dt, self.t_start, self.t_stop, self.dimensions) 1932 for id in self.id_list(): 1933 aslist.append(id, self.analog_signals[id]) 1934 return aslist 1761 1935 1762 1936 def __calc_startstop(self): … … 1768 1942 if len(self) > 0: 1769 1943 if self.t_start is None: 1770 start_times = numpy.array([self.analog_signals[idx].t_start for idx in self.id_list ])1944 start_times = numpy.array([self.analog_signals[idx].t_start for idx in self.id_list()]) 1771 1945 self.t_start = numpy.min(start_times) 1772 1946 logging.debug("Warning, t_start is infered from the data : %f" %self.t_start) … … 1774 1948 self.analog_signals[id].t_start = self.t_start 1775 1949 if self.t_stop is None: 1776 stop_times = numpy.array([self.analog_signals[idx].t_stop for idx in self.id_list ])1950 stop_times = numpy.array([self.analog_signals[idx].t_stop for idx in self.id_list()]) 1777 1951 self.t_stop = numpy.max(stop_times) 1778 1952 logging.debug("Warning, t_stop is infered from the data : %f" %self.t_stop) … … 1825 1999 return len(self.analog_signals) 1826 2000 1827 def __ get_id_list(self, sub_list=None):2001 def __sub_id_list(self, sub_list=None): 1828 2002 if sub_list == None: 1829 return self.id_list 2003 return self.id_list() 1830 2004 if type(sub_list) == int: 1831 return numpy.random.permutation(self.id_list )[0:sub_list]2005 return numpy.random.permutation(self.id_list())[0:sub_list] 1832 2006 if type(sub_list) == list: 1833 2007 return sub_list … … 1848 2022 """ 1849 2023 assert isinstance(signal, AnalogSignal), "An AnalogSignalList object can only contain AnalogSignal objects" 1850 if id in self.id_list :2024 if id in self.id_list(): 1851 2025 raise Exception("Id already present in AnalogSignalList. Use setitem instead()") 1852 2026 else: 1853 2027 self.analog_signals[id] = signal 1854 self.id_list.append(id)1855 self.N += 11856 2028 self.__calc_startstop() 1857 2029 … … 1876 2048 """ 1877 2049 new_AnalogSignalList = AnalogSignalList([], [], self.dt, self.t_start, self.t_stop, self.dimensions) 1878 if isinstance(id_list, int): 1879 id_list = numpy.random.permutation(self.id_list)[0:id_list] 2050 id_list = self.__sub_id_list(id_list) 1880 2051 for id in id_list: 1881 2052 try: … … 1897 2068 """ 1898 2069 new_AnalogSignalList = AnalogSignalList([], [], self.dt, t_start, t_stop, self.dimensions) 1899 for id in self.id_list :2070 for id in self.id_list(): 1900 2071 new_AnalogSignalList.append(id, self.analog_signals[id].time_slice(t_start, t_stop)) 1901 2072 new_AnalogSignalList.__calc_startstop() … … 1942 2113 """ 1943 2114 subplot = self.__getdisplay__(display) 1944 id_list = self._AnalogSignalList__ get_id_list(id_list)2115 id_list = self._AnalogSignalList__sub_id_list(id_list) 1945 2116 time_axis = self.time_axis() 1946 2117 if not subplot or not ENABLE_PLOTS: … … 1977 2148 """ 1978 2149 subplot = self.__getdisplay__(display) 1979 id_list = self._AnalogSignalList__ get_id_list(id_list)2150 id_list = self._AnalogSignalList__sub_id_list(id_list) 1980 2151 time_axis = self.time_axis() 1981 2152 if not subplot or not ENABLE_PLOTS: … … 2008 2179 """ 2009 2180 subplot = self.__getdisplay__(display) 2010 id_list = self._AnalogSignalList__ get_id_list(id_list)2181 id_list = self._AnalogSignalList__sub_id_list(id_list) 2011 2182 time_axis = self.time_axis() 2012 2183 if not subplot or not ENABLE_PLOTS: … … 2039 2210 Inputs: 2040 2211 filename - the name of the spike file 2041 id_list - the list of the recorded ids. Can be an int (meaning cells in the range (0,..,N)),2042 or a list.2212 id_list - the list of the recorded ids. Can be an int (meaning cells in 2213 the range (0,..,N)), or a list. 2043 2214 dims - if the cells were aranged on a 2/3D grid, a tuple with the dimensions 2044 2215 dt - the discretization step, in ms … … 2046 2217 t_stop - end of the simulation, in ms 2047 2218 2048 If dims, dt, t_start, t_stop or id_list are None, they will be infered from either the data or from the header. 2049 All times are in milliseconds. The format of the file (text, pickle or hdf5) will be inferred automatically 2219 If dims, dt, t_start, t_stop or id_list are None, they will be infered from either 2220 the data or from the header. 2221 All times are in milliseconds. 2222 The format of the file (text, pickle or hdf5) will be inferred automatically 2050 2223 """ 2051 2224 spike_loader = io.SpikeListIO(filename) … … 2064 2237 Inputs: 2065 2238 filename - the name of the spike file 2066 id_list - the list of the recorded ids. Can be an int (meaning cells in the range (0,..,N)),2067 or a list.2239 id_list - the list of the recorded ids. Can be an int (meaning cells in 2240 the range (0,..,N)), or a list. 2068 2241 dims - if the cells were aranged on a 2/3D grid, a tuple with the dimensions 2069 2242 dt - the discretization step, in ms … … 2071 2244 t_stop - end of the simulation, in ms 2072 2245 2073 If dims, dt, t_start, t_stop or id_list are None, they will be infered from either the data or from the header. 2074 All times are in milliseconds. The format of the file (text, pickle or hdf5) will be inferred automatically 2246 If dims, dt, t_start, t_stop or id_list are None, they will be infered from either 2247 the data or from the header. 2248 All times are in milliseconds. 2249 The format of the file (text, pickle or hdf5) will be inferred automatically 2075 2250 """ 2076 2251 analogsignal_loader = io.AnalogSignalIO(filename) … … 2088 2263 Inputs: 2089 2264 filename - the name of the spike file 2090 id_list - the list of the recorded ids. Can be an int (meaning cells in the range (0,..,N)),2091 or a list.2265 id_list - the list of the recorded ids. Can be an int (meaning cells in 2266 the range (0,..,N)), or a list. 2092 2267 dims - if the cells were aranged on a 2/3D grid, a tuple with the dimensions 2093 2268 dt - the discretization step, in ms … … 2095 2270 t_stop - end of the simulation, in ms 2096 2271 2097 If dims, dt, t_start, t_stop or id_list are None, they will be infered from either the data or from the header. 2098 All times are in milliseconds. The format of the file (text, pickle or hdf5) will be inferred automatically 2272 If dims, dt, t_start, t_stop or id_list are None, they will be infered from either 2273 the data or from the header. 2274 All times are in milliseconds. 2275 The format of the file (text, pickle or hdf5) will be inferred automatically 2099 2276 """ 2100 2277 analogsignal_loader = io.AnalogSignalIO(filename) … … 2113 2290 Inputs: 2114 2291 filename - the name of the spike file 2115 id_list - the list of the recorded ids. Can be an int (meaning cells in the range (0,..,N)),2116 or a list.2292 id_list - the list of the recorded ids. Can be an int (meaning cells in 2293 the range (0,..,N)), or a list. 2117 2294 dims - if the cells were aranged on a 2/3D grid, a tuple with the dimensions 2118 2295 dt - the discretization step, in ms … … 2120 2297 t_stop - end of the simulation, in ms 2121 2298 2122 If dims, dt, t_start, t_stop or id_list are None, they will be infered from either the data or from the header. 2123 All times are in milliseconds. The format of the file (text, pickle or hdf5) will be inferred automatically 2299 If dims, dt, t_start, t_stop or id_list are None, they will be infered from either 2300 the data or from the header. 2301 All times are in milliseconds. 2302 The format of the file (text, pickle or hdf5) will be inferred automatically 2124 2303 """ 2125 2304 analogsignal_loader = io.AnalogSignalIO(filename)

