def plot(data, pull=None, **kwargs): loc = kwargs.get('legend', 'best') title = kwargs.get('title','') FEC = kwargs.get('FEC',False) displayFRET = kwargs.get('show_fret', hasattr(data,'fret')) hold=kwargs.pop('hold', None) if hold is not None: plt.hold(hold) if hasattr(data, '_to_plot'): args, kwargs = data._to_plot() return _subplot(*args, **kwargs) if not pull and hasTrapData(data): pull = TrapData.fromObject(data) if not displayFRET and data is not None: num = 1 elif hasFretData(data): num = 2 else: num = 0 if pull: num += 1 if num==0: raise ValueError("Don't know how to plot argument: missing named fields") layout = iter((num,1,x) for x in range(1,num+1)) ax1 = None if hasFretData(data): plt.subplot(*next(layout)) not hold and plt.cla() plt.hold(True) ax1 = _subplot(data.time, data.donor, label='donor') _subplot(data.time, data.acceptor, label='acceptor', axes=('','counts')) plt.hold(hold) plt.legend(loc=loc,ncol=2,prop={'size':'small'}) if displayFRET: _subplot(data.time, data.fret, layout=next(layout), axes=('Seconds','FRET')) ax2 = None if pull: x_coord,x_label = (pull.ext,'Extension (nm)') if FEC else (pull.sep,'Separation (nm)') ax2 = _subplot(x_coord, pull.f, '.', layout=next(layout), axes=(x_label,'Force (pN)')) first_plot = ax1 if ax1 else ax2 first_plot.set_title(title) plt.show()
def fromFile(cls, strfile, fretfile, metadata): assert strfile or fretfile assert isinstance(strfile, str) assert isinstance(fretfile, (str,type(None))) metadata = nesteddict.from_dict(metadata) trap = TrapData.fromFile(strfile) fret = FretData.fromFile(fretfile) if fretfile else None trap.metadata.setdefault('date', today()) trap_datetime = trap.metadata['date'] metadata.setdefault('date', to_date(trap_datetime)) metadata.setdefault('datetime', trap_datetime) metadata['filename'] = fileIO.splitext(strfile)[0] newCls = cls(trap, fret, metadata) assert isinstance(newCls, cls) assert getattr(newCls, 'filename', None) is not None return newCls
def fromFile(cls, strfile, fretfile, metadata): assert strfile or fretfile assert isinstance(strfile, str) assert isinstance(fretfile, (str, type(None))) metadata = nesteddict.from_dict(metadata) trap = TrapData.fromFile(strfile) fret = FretData.fromFile(fretfile) if fretfile else None trap.metadata.setdefault('date', today()) trap_datetime = trap.metadata['date'] metadata.setdefault('date', to_date(trap_datetime)) metadata.setdefault('datetime', trap_datetime) metadata['filename'] = fileIO.splitext(strfile)[0] newCls = cls(trap, fret, metadata) assert isinstance(newCls, cls) assert getattr(newCls, 'filename', None) is not None return newCls
def collapse(self, trap_sorted_by='ext', fret_sorted_by='time'): "Collapse experiments into a single experiment with all data appended together." assert isinstance(trap_sorted_by, str) assert isinstance(fret_sorted_by, str) if not self._all_elements_have_attr('trap'): raise AttributeError( 'All experiments in ExpList must have attribute "trap"') filtered_by_fret = self.has_attr('fret') num_with_fret = len(filtered_by_fret) fret_data = None if num_with_fret == len(self): fret_data = FretData.aggregate(self.getattrs('fret'), fret_sorted_by) elif num_with_fret > 0: logger.warning('Not all experiments have fret: not collapsing fret data!') trap_data = TrapData.aggregate(self.getattrs('trap'), sort_by=trap_sorted_by) fname = self[0].filename or '' fname += '_collapsed' if fname else 'collapsed' return Pulling(trap_data, fret_data, dict(filename=fname, collapsed=True))
def plot(data, pull=None, style=None, **kwargs): """Plot FretData and/or TrapData as stacked subplots of counts, FRET, and FEC/FDC @data: datatypes.AbstractData @pull: datatypes.AbstractData @style: dict or str """ if isinstance(style, str): style = Style.with_default_style(style) elif isinstance(style, dict): style = Style(**style) else: style = Style() loc = kwargs.pop('legend', 'best') title = kwargs.pop('title','') label = kwargs.pop('label', '') displayFRET = kwargs.pop('show_fret', True) and hasattr(data,'fret') kwargs.setdefault('markersize', 2) hold=kwargs.pop('hold', None) if hold is not None: plt.hold(hold) if not pull and hasTrapData(data): pull = TrapData.fromObject(data) num = 0 if displayFRET: num += 2 if pull: num += 1 FEC = kwargs.pop('FEC', num==1) else: FEC = kwargs.pop('FEC', False) if num == 0: raise ValueError("Don't know how to plot arguments: need TrapData or FretData") if data is None and pull is not None: layout = iter([(num,1,num)]) else: layout = iter((num,1,x) for x in range(1,num+1)) ax1 = None donor, acceptor = 'donor', 'acceptor' if displayFRET: plt.subplot(*next(layout)) not hold and plt.cla() plt.hold(True) ax1 = subplot(data.time, data.donor, style[donor], label=donor, **kwargs) subplot(data.time, data.acceptor, style[acceptor], label=acceptor, axes=('Time (s)','Counts'), **kwargs) plt.hold(hold) if loc is not None: plt.legend(loc=loc,ncol=2,prop={'size':'small'}) if displayFRET: subplot(data.time, data.fret, style['fret'], layout=next(layout), axes=('Time (s)','FRET'), **kwargs) plt.ylim(-0.1, 1.1) ax2 = None if pull: trap_style = Style.TRAP_STYLE_NAME x_coord,x_label = (pull.ext,'Extension (nm)') if FEC else (pull.sep,'Separation (nm)') ax2 = subplot(x_coord, pull.f, style[trap_style], layout=next(layout), axes=(x_label,'Force (pN)'), label=label, **kwargs) if loc is not None: plt.legend(loc=loc,ncol=2,prop={'size':'small'}) first_plot = ax1 or ax2 if title: first_plot.set_title(title)
def plot(data, pull=None, style=None, **kwargs): """Plot FretData and/or TrapData as stacked subplots of counts, FRET, and FEC/FDC @data: datatypes.AbstractData @pull: datatypes.AbstractData @style: dict or str """ if isinstance(style, str): style = Style.with_default_style(style) elif isinstance(style, dict): style = Style(**style) else: style = Style() loc = kwargs.pop('legend', 'best') title = kwargs.pop('title', '') label = kwargs.pop('label', '') displayFRET = kwargs.pop('show_fret', True) and hasattr(data, 'fret') kwargs.setdefault('markersize', 2) hold = kwargs.pop('hold', None) if hold is not None: plt.hold(hold) if not pull and hasTrapData(data): pull = TrapData.fromObject(data) num = 0 if displayFRET: num += 2 if pull: num += 1 FEC = kwargs.pop('FEC', num == 1) else: FEC = kwargs.pop('FEC', False) if num == 0: raise ValueError( "Don't know how to plot arguments: need TrapData or FretData") if data is None and pull is not None: layout = iter([(num, 1, num)]) else: layout = iter((num, 1, x) for x in range(1, num + 1)) ax1 = None donor, acceptor = 'donor', 'acceptor' if displayFRET: plt.subplot(*next(layout)) not hold and plt.cla() plt.hold(True) ax1 = subplot(data.time, data.donor, style[donor], label=donor, **kwargs) subplot(data.time, data.acceptor, style[acceptor], label=acceptor, axes=('Time (s)', 'Counts'), **kwargs) plt.hold(hold) if loc is not None: plt.legend(loc=loc, ncol=2, prop={'size': 'small'}) if displayFRET: subplot(data.time, data.fret, style['fret'], layout=next(layout), axes=('Time (s)', 'FRET'), **kwargs) plt.ylim(-0.1, 1.1) ax2 = None if pull: trap_style = Style.TRAP_STYLE_NAME x_coord, x_label = (pull.ext, 'Extension (nm)') if FEC else (pull.sep, 'Separation (nm)') ax2 = subplot(x_coord, pull.f, style[trap_style], layout=next(layout), axes=(x_label, 'Force (pN)'), label=label, **kwargs) if loc is not None: plt.legend(loc=loc, ncol=2, prop={'size': 'small'}) first_plot = ax1 or ax2 if title: first_plot.set_title(title)