Changeset 250

Show
Ignore:
Timestamp:
10/30/08 17:37:26 (2 months ago)
Author:
pierre
Message:

Move plotting function of the signals.py file to the plotting file. Sounds like more logical :-)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/plotting.py

    r219 r250  
    5151            if draw: 
    5252                ax.add_line(side) 
     53 
     54 
     55 
     56############################################################# 
     57## Utility function for the plots. Common to all the objects 
     58## They are called when we try to do plots 
     59############################################################# 
     60 
     61def get_display(display): 
     62    """ 
     63    Return a pylab object with a plot() function to draw the plots. 
     64     
     65    Inputs: 
     66        display - if True, a new figure is created. Otherwise, if display is a 
     67                  subplot object, this object is returned. 
     68    """ 
     69    if display is False: 
     70        return None 
     71    elif display is True: 
     72        pylab.figure() 
     73        return pylab 
     74    else: 
     75        return display 
     76     
     77def set_labels(subplot, xlabel, ylabel): 
     78    """ 
     79    Function to put some labels on a plot 
     80     
     81    Inputs: 
     82        subplot - the targeted plot 
     83        xlabel  - a string for the x label 
     84        ylabel  - a string for the y label 
     85    """ 
     86    if hasattr(subplot, 'xlabel'): 
     87        subplot.xlabel(xlabel) 
     88        subplot.ylabel(ylabel) 
     89    else: 
     90        subplot.set_xlabel(xlabel) 
     91        subplot.set_ylabel(ylabel) 
     92 
     93 
     94def set_axis_limits(subplot, xmin, xmax, ymin, ymax): 
     95    """ 
     96    Function to set the axis on a plot 
     97     
     98    Inputs: 
     99        subplot - the targeted plot 
     100        xmin, xmax  - the limits of the x axis 
     101        ymin, ymax  - the limits of the y axis 
     102    """ 
     103    if hasattr(subplot, 'xlim'): 
     104        subplot.xlim(xmin, xmax) 
     105        subplot.ylim(ymin, ymax) 
     106    else: 
     107        subplot.set_xlim(xmin, xmax) 
     108        subplot.set_ylim(ymin, ymax) 
     109 
     110 
     111 
     112 
     113 
     114 
    53115 
    54116 
  • trunk/src/signals.py

    r249 r250  
    88from NeuroTools import analysis 
    99from NeuroTools import io 
     10from NeuroTools.plotting import get_display, set_axis_limits, set_labels 
    1011 
    1112numpy_version = numpy.__version__.split(".")[0:2] 
     
    4849 
    4950 
    50 ############################################################# 
    51 ## Utility function for the plots. Common to all the objects 
    52 ## They are called when we try to do plots 
    53 ############################################################# 
    54  
    55 def __getdisplay__(display): 
    56     """ 
    57     Return a pylab object with a plot() function to draw the plots. 
    58      
    59     Inputs: 
    60         display - if True, a new figure is created. Otherwise, if display is a 
    61                   subplot object, this object is returned. 
    62     """ 
    63     if display is False: 
    64         return None 
    65     elif display is True: 
    66         pylab.figure() 
    67         return pylab 
    68     else: 
    69         return display 
    70      
    71 def __labels__(subplot, xlabel, ylabel): 
    72     """ 
    73     Function to put some labels on a plot 
    74      
    75     Inputs: 
    76         subplot - the targeted plot 
    77         xlabel  - a string for the x label 
    78         ylabel  - a string for the y label 
    79     """ 
    80     if hasattr(subplot, 'xlabel'): 
    81         subplot.xlabel(xlabel) 
    82         subplot.ylabel(ylabel) 
    83     else: 
    84         subplot.set_xlabel(xlabel) 
    85         subplot.set_ylabel(ylabel) 
    86  
    87  
    88 def __axis__(subplot, xmin, xmax, ymin, ymax): 
    89     """ 
    90     Function to set the axis on a plot 
    91      
    92     Inputs: 
    93         subplot - the targeted plot 
    94         xmin, xmax  - the limits of the x axis 
    95         ymin, ymax  - the limits of the y axis 
    96     """ 
    97     if hasattr(subplot, 'xlim'): 
    98         subplot.xlim(xmin, xmax) 
    99         subplot.ylim(ymin, ymax) 
    100     else: 
    101         subplot.set_xlim(xmin, xmax) 
    102         subplot.set_ylim(ymin, ymax) 
    10351 
    10452 
     
    419367        if t_stop is None:  t_stop = self.t_stop 
    420368        spikes = numpy.extract((self.spike_times >= t_start) & (self.spike_times <= t_stop), self.spike_times) 
    421         subplot = __getdisplay__(display) 
     369        subplot = get_display(display) 
    422370        if not subplot or not ENABLE_PLOTS: 
    423371            print MATPLOTLIB_ERROR 
     
    427375                xlabel = "Time (ms)" 
    428376                ylabel = "Neurons #" 
    429                 __labels__(subplot, xlabel, ylabel) 
     377                set_labels(subplot, xlabel, ylabel) 
    430378                subplot.scatter(spikes,numpy.ones(len(spikes)),**kwargs) 
    431379                pylab.draw() 
     
    803751 
    804752    def __getitem__(self, id): 
    805         return self.spiketrains[id] 
     753        if id in self.id_list(): 
     754            return self.spiketrains[id] 
     755        else: 
     756            raise Exception("id %d is not present in the SpikeList. See id_list()" %id) 
    806757     
    807758    def __getslice__(self, i, j): 
     
    11551106        else: 
    11561107            values, xaxis = numpy.histogram(isis, bins=bins, normed=True) 
    1157         subplot       = __getdisplay__(display) 
     1108        subplot       = get_display(display) 
    11581109        if not subplot or not ENABLE_PLOTS: 
    11591110            return values, xaxis 
     
    11611112            xlabel = "Inter Spike Interval (ms)" 
    11621113            ylabel = "% of Neurons" 
    1163             __labels__(subplot, xlabel, ylabel) 
     1114            set_labels(subplot, xlabel, ylabel) 
    11641115            subplot.plot(xaxis, values, **kwargs) 
    11651116            pylab.draw() 
     
    12091160        else: 
    12101161            values, xaxis = numpy.histogram(cvs, bins=bins, normed=True) 
    1211         subplot       = __getdisplay__(display) 
     1162        subplot       = get_display(display) 
    12121163        if not subplot or not ENABLE_PLOTS: 
    12131164            return values, xaxis 
     
    12151166            xlabel = " CV ISI" 
    12161167            ylabel = "% of Neurons" 
    1217             __labels__(subplot, xlabel, ylabel) 
     1168            set_labels(subplot, xlabel, ylabel) 
    12181169            subplot.plot(xaxis, values, **kwargs) 
    12191170            pylab.draw() 
     
    12951246        """ 
    12961247        rates   = self.mean_rates() 
    1297         subplot = __getdisplay__(display) 
     1248        subplot = get_display(display) 
    12981249        if not subplot or not ENABLE_PLOTS: 
    12991250            return rates 
     
    13061257            xlabel = "Average Firing Rate (Hz)" 
    13071258            ylabel = "% of Neurons" 
    1308             __labels__(subplot, xlabel, ylabel) 
     1259            set_labels(subplot, xlabel, ylabel) 
    13091260            subplot.plot(xaxis, values, **kwargs) 
    13101261            pylab.draw() 
     
    13351286            M -= 1 
    13361287        spike_hist = numpy.zeros((N, M), float) 
    1337         subplot    = __getdisplay__(display) 
     1288        subplot    = get_display(display) 
    13381289        for idx,id in enumerate(self.id_list()): 
    13391290            spike_hist[idx,:] = self.spiketrains[id].time_histogram(time_bin, normalized) 
     
    13461297                ylabel = "Spikes per bin" 
    13471298            xlabel = "Time (ms)" 
    1348             __labels__(subplot, xlabel, ylabel) 
     1299            set_labels(subplot, xlabel, ylabel) 
    13491300            axis = self.time_axis(time_bin) 
    13501301            if newnum: 
     
    14611412            activity_movie 
    14621413        """ 
    1463         subplot = __getdisplay__(display) 
     1414        subplot = get_display(display) 
    14641415         
    14651416        if t_start == None:  
     
    15251476            pairwise_pearson_corrcoeff, pairwise_cc_zero 
    15261477        """ 
    1527         subplot = __getdisplay__(display) 
     1478        subplot = get_display(display) 
    15281479         
    15291480        if spklist == None: 
     
    15721523            ylabel  = "Cross Correlation" 
    15731524            subplot.plot(xaxis, results, **kwargs) 
    1574             __labels__(subplot, xlabel, ylabel) 
     1525            set_labels(subplot, xlabel, ylabel) 
    15751526            pylab.draw() 
    15761527 
     
    16141565        if spklist == None: 
    16151566            spklist = self 
    1616         subplot = __getdisplay__(display) 
     1567        subplot = get_display(display) 
    16171568 
    16181569        spk1, spk2, pairs = self.__select_with_pairs__(pairs, self, spklist) 
     
    16581609                ylabel  = "Normalized CC" 
    16591610                subplot.plot(xaxis+self.t_start, cc_time, **kwargs) 
    1660                 __labels__(subplot, xlabel, ylabel) 
     1611                set_labels(subplot, xlabel, ylabel) 
    16611612                pylab.draw() 
    16621613        else: 
     
    17861737            SpikeTrain.raster_plot 
    17871738        """ 
    1788         subplot = __getdisplay__(display) 
     1739        subplot = get_display(display) 
    17891740        if id_list == None:  
    17901741            id_list = self.id_list() 
     
    18071758            xlabel = "Time (ms)" 
    18081759            ylabel = "Neuron #" 
    1809             __labels__(subplot, xlabel, ylabel) 
     1760            set_labels(subplot, xlabel, ylabel) 
    18101761            min_id = numpy.min(spk.id_list()) 
    18111762            max_id = numpy.max(spk.id_list()) 
    18121763            length = t_stop - t_start 
    1813             __axis__(subplot, t_start-0.05*length, t_stop+0.05*length, min_id-2, max_id+2) 
     1764            set_axis_limits(subplot, t_start-0.05*length, t_stop+0.05*length, min_id-2, max_id+2) 
    18141765            pylab.draw() 
    18151766 
     
    18491800            activity_map 
    18501801        """ 
    1851         subplot = __getdisplay__(display) 
     1802        subplot = get_display(display) 
    18521803        if t_start is None: t_start = self.t_start 
    18531804        if t_stop is None:  t_stop  = self.t_stop 
     
    22122163            raise Exception("No Analog Signals !") 
    22132164     
    2214     def __getitem__(self, i): 
    2215         return self.analog_signals[i] 
     2165    def __getitem__(self, id): 
     2166        if id in self.id_list(): 
     2167            return self.analog_signals[id] 
     2168        else: 
     2169            raise Exception("id %d is not present in the AnalogSignal. See id_list()" %id) 
    22162170 
    22172171    def __setitem__(self, i, val): 
     
    24232377            >> aslist.plot(5, v_thresh = -50, display=z, kwargs={'color':'r'}) 
    24242378        """ 
    2425         subplot   = __getdisplay__(display) 
     2379        subplot   = get_display(display) 
    24262380        id_list   = self._AnalogSignalList__sub_id_list(id_list) 
    24272381        time_axis = self.time_axis()   
     
    24312385            xlabel = "Time (ms)" 
    24322386            ylabel = "Membrane Potential (mV)" 
    2433             __labels__(subplot, xlabel, ylabel) 
     2387            set_labels(subplot, xlabel, ylabel) 
    24342388            for id in id_list: 
    24352389                to_be_plot = self.analog_signals[id].signal 
     
    24582412            >> aslist.plot(5, display=z, kwargs={'color':'r'}) 
    24592413        """ 
    2460         subplot   = __getdisplay__(display) 
     2414        subplot   = get_display(display) 
    24612415        id_list   = self._AnalogSignalList__sub_id_list(id_list) 
    24622416        time_axis = self.time_axis()   
     
    24662420            xlabel = "Time (ms)" 
    24672421            ylabel = "Current (nA)" 
    2468             __labels__(subplot, xlabel, ylabel) 
     2422            set_labels(subplot, xlabel, ylabel) 
    24692423            for id in id_list: 
    24702424                subplot.plot(time_axis, self.analog_signals[id].signal, **kwargs) 
     
    24732427 
    24742428class ConductanceList(AnalogSignalList): 
    2475  
    24762429 
    24772430    def plot(self, id_list=None, v_thresh=None, display=True, kwargs={}): 
     
    24902443            >> aslist.plot(5, display=z, kwargs={'color':'r'}) 
    24912444        """ 
    2492         subplot   = __getdisplay__(display) 
     2445        subplot   = get_display(display) 
    24932446        id_list   = self._AnalogSignalList__sub_id_list(id_list) 
    24942447        time_axis = self.time_axis()   
     
    24982451            xlabel = "Time (ms)" 
    24992452            ylabel = "Conductance (nS)" 
    2500             __labels__(subplot, xlabel, ylabel) 
     2453            set_labels(subplot, xlabel, ylabel) 
    25012454            for id in id_list: 
    25022455                subplot.plot(time_axis, self.analog_signals[id].signal, **kwargs)