ax = kwargs.pop('<STR_LIT>', None)<EOL>ax = mir_eval.display.__get_axes(ax=ax)[<NUM_LIT:0>]<EOL>times, values = annotation.to_interval_values()<EOL>indices = np.unique([v['<STR_LIT:index>'] for v in values])<EOL>for idx in indices:<EOL><INDENT>rows = [i for (i, v) in enumerate(values) if v['<STR_LIT:index>'] == idx]<EOL>freqs = np.asarray([values[r]['<STR_LIT>'] for r in rows])<EOL>unvoiced = ~np.asarray([values[r]['<STR_LIT>'] for r in rows])<EOL>freqs[unvoiced] *= -<NUM_LIT:1><EOL>ax = mir_eval.display.pitch(times[rows, <NUM_LIT:0>], freqs, unvoiced=True,<EOL>ax=ax,<EOL>**kwargs)<EOL><DEDENT>return ax<EOL>


<EOL>patterns = defaultdict(lambda: defaultdict(list))<EOL>for time, observation in zip(*ann.to_event_values()):<EOL><INDENT>pattern_id = observation['<STR_LIT>']<EOL>occurrence_id = observation['<STR_LIT>']<EOL>obs = (time, observation['<STR_LIT>'])<EOL>patterns[pattern_id][occurrence_id].append(obs)<EOL><DEDENT>return [list(_.values()) for _ in six.itervalues(patterns)]<EOL>

Convert a pattern_jku annotation object to mir_eval format.

    Parameters
    ----------
    ann : jams.Annotation
        Must have `namespace='pattern_jku'`

    Returns
    -------
    patterns : list of list of tuples
        - `patterns[x]` is a list containing all occurrences of pattern x

        - `patterns[x][y]` is a list containing all notes for
           occurrence y of pattern x

        - `patterns[x][y][z]` contains a time-note tuple
          `(time, midi note)`


namespace = '<STR_LIT>'<EOL>ref = coerce_annotation(ref, namespace)<EOL>est = coerce_annotation(est, namespace)<EOL>ref_intervals, ref_p = ref.to_interval_values()<EOL>est_intervals, est_p = est.to_interval_values()<EOL>ref_pitches = np.asarray([p['<STR_LIT>'] * (-<NUM_LIT:1>)**(~p['<STR_LIT>']) for p in ref_p])<EOL>est_pitches = np.asarray([p['<STR_LIT>'] * (-<NUM_LIT:1>)**(~p['<STR_LIT>']) for p in est_p])<EOL>return mir_eval.transcription.evaluate(<EOL>ref_intervals, ref_pitches, est_intervals, est_pitches, **kwargs)<EOL>

r'''Note transcription evaluation

    Parameters
    ----------
    ref : jams.Annotation
        Reference annotation object
    est : jams.Annotation
        Estimated annotation object
    kwargs
        Additional keyword arguments

    Returns
    -------
    scores : dict
        Dictionary of scores, where the key is the metric name (str) and
        the value is the (float) score achieved.

    See Also
    --------
    mir_eval.transcription.evaluate

    Examples
    --------
    >>> # Load in the JAMS objects
    >>> ref_jam = jams.load('reference.jams')
    >>> est_jam = jams.load('estimated.jams')
    >>> # Select the first relevant annotations. You can use any annotation
    >>> # type that can be converted to pitch_contour (such as pitch_midi)
    >>> ref_ann = ref_jam.search(namespace='pitch_contour')[0]
    >>> est_ann = est_jam.search(namespace='note_hz')[0]
    >>> scores = jams.eval.transcription(ref_ann, est_ann)


ann = convert(ann, namespace)<EOL>ann.validate(strict=True)<EOL>return ann<EOL>

Validate that the annotation has the correct namespace,
    and is well-formed.

    If the annotation is not of the correct namespace, automatic conversion
    is attempted.

    Parameters
    ----------
    ann : jams.Annotation
        The annotation object in question

    namespace : str
        The namespace pattern to match `ann` against

    Returns
    -------
    ann_coerced: jams.Annotation
        The annotation coerced to the target namespace

    Raises
    ------
    NamespaceError
        If `ann` does not match the proper namespace

    SchemaError
        If `ann` fails schema validation

    See Also
    --------
    jams.nsconvert.convert


namespace = '<STR_LIT>'<EOL>ref = coerce_annotation(ref, namespace)<EOL>est = coerce_annotation(est, namespace)<EOL>ref_times, _ = ref.to_event_values()<EOL>est_times, _ = est.to_event_values()<EOL>return mir_eval.beat.evaluate(ref_times, est_times, **kwargs)<EOL>

r'''Beat tracking evaluation

    Parameters
    ----------
    ref : jams.Annotation
        Reference annotation object
    est : jams.Annotation
        Estimated annotation object
    kwargs
        Additional keyword arguments

    Returns
    -------
    scores : dict
        Dictionary of scores, where the key is the metric name (str) and
        the value is the (float) score achieved.

    See Also
    --------
    mir_eval.beat.evaluate

    Examples
    --------
    >>> # Load in the JAMS objects
    >>> ref_jam = jams.load('reference.jams')
    >>> est_jam = jams.load('estimated.jams')
    >>> # Select the first relevant annotations
    >>> ref_ann = ref_jam.search(namespace='beat')[0]
    >>> est_ann = est_jam.search(namespace='beat')[0]
    >>> scores = jams.eval.beat(ref_ann, est_ann)


namespace = '<STR_LIT>'<EOL>ref = coerce_annotation(ref, namespace)<EOL>est = coerce_annotation(est, namespace)<EOL>ref_hier, ref_hier_lab = hierarchy_flatten(ref)<EOL>est_hier, est_hier_lab = hierarchy_flatten(est)<EOL>return mir_eval.hierarchy.evaluate(ref_hier, ref_hier_lab,<EOL>est_hier, est_hier_lab,<EOL>**kwargs)<EOL>

r'''Multi-level segmentation evaluation

    Parameters
    ----------
    ref : jams.Annotation
        Reference annotation object
    est : jams.Annotation
        Estimated annotation object
    kwargs
        Additional keyword arguments

    Returns
    -------
    scores : dict
        Dictionary of scores, where the key is the metric name (str) and
        the value is the (float) score achieved.

    See Also
    --------
    mir_eval.hierarchy.evaluate

    Examples
    --------
    >>> # Load in the JAMS objects
    >>> ref_jam = jams.load('reference.jams')
    >>> est_jam = jams.load('estimated.jams')
    >>> # Select the first relevant annotations
    >>> ref_ann = ref_jam.search(namespace='multi_segment')[0]
    >>> est_ann = est_jam.search(namespace='multi_segment')[0]
    >>> scores = jams.eval.hierarchy(ref_ann, est_ann)


ref = coerce_annotation(ref, '<STR_LIT>')<EOL>est = coerce_annotation(est, '<STR_LIT>')<EOL>ref_tempi = np.asarray([o.value for o in ref])<EOL>ref_weight = ref.data[<NUM_LIT:0>].confidence<EOL>est_tempi = np.asarray([o.value for o in est])<EOL>return mir_eval.tempo.evaluate(ref_tempi, ref_weight, est_tempi, **kwargs)<EOL>

r'''Tempo evaluation

    Parameters
    ----------
    ref : jams.Annotation
        Reference annotation object
    est : jams.Annotation
        Estimated annotation object
    kwargs
        Additional keyword arguments

    Returns
    -------
    scores : dict
        Dictionary of scores, where the key is the metric name (str) and
        the value is the (float) score achieved.

    See Also
    --------
    mir_eval.tempo.evaluate

    Examples
    --------
    >>> # Load in the JAMS objects
    >>> ref_jam = jams.load('reference.jams')
    >>> est_jam = jams.load('estimated.jams')
    >>> # Select the first relevant annotations
    >>> ref_ann = ref_jam.search(namespace='tempo')[0]
    >>> est_ann = est_jam.search(namespace='tempo')[0]
    >>> scores = jams.eval.tempo(ref_ann, est_ann)


annotation.namespace = '<STR_LIT>'<EOL>data = annotation.pop_data()<EOL>for obs in data:<EOL><INDENT>annotation.append(time=obs.time, duration=obs.duration,<EOL>confidence=obs.confidence,<EOL>value=<NUM_LIT:12> * (np.log2(obs.value) - np.log2(<NUM_LIT>)) + <NUM_LIT>)<EOL><DEDENT>return annotation<EOL>

Convert a pitch_hz annotation to pitch_midi


annotation.namespace = '<STR_LIT>'<EOL>return annotation<EOL>

Convert any segmentation to open label space


annotation.namespace = '<STR_LIT>'<EOL>data = annotation.pop_data()<EOL>for obs in data:<EOL><INDENT>annotation.append(time=obs.time, duration=obs.duration,<EOL>confidence=obs.confidence,<EOL>value=obs.value['<STR_LIT>'])<EOL><DEDENT>return annotation<EOL>


annotation.namespace = '<STR_LIT>'<EOL>data = annotation.pop_data()<EOL>for obs in data:<EOL><INDENT>annotation.append(time=obs.time, duration=obs.duration,<EOL>confidence=obs.confidence,<EOL>value=dict(index=<NUM_LIT:0>,<EOL>frequency=np.abs(obs.value),<EOL>voiced=obs.value > <NUM_LIT:0>))<EOL><DEDENT>return annotation<EOL>

Convert a pitch_hz annotation to a contour


def register(func):<EOL><INDENT>'''<STR_LIT>'''<EOL>__CONVERSION__[target][source] = func<EOL>return func<EOL><DEDENT>return register<EOL>

A decorator to register namespace conversions.

    Usage
    -----
    >>> @conversion('tag_open', 'tag_.*')
    ... def tag_to_open(annotation):
    ...     annotation.namespace = 'tag_open'
    ...     return annotation


for obs in records:<EOL><INDENT>if isinstance(obs, Observation):<EOL><INDENT>self.append(**obs._asdict())<EOL><DEDENT>else:<EOL><INDENT>self.append(**obs)<EOL><DEDENT><DEDENT>

Add observations from row-major storage.

        This is primarily useful for deserializing sparsely packed data.

        Parameters
        ----------
        records : iterable of dicts or Observations
            Each element of `records` corresponds to one observation.


data = self.data<EOL>self.data = SortedKeyList(key=self._key)<EOL>return data<EOL>

Replace this observation's data with a fresh container.

        Returns
        -------
        annotation_data : SortedKeyList
            The original annotation data container


def append(self, time=None, duration=None, value=None, confidence=None):

self.data.add(Observation(time=float(time),<EOL>duration=float(duration),<EOL>value=value,<EOL>confidence=confidence))<EOL>

Append an observation to the data field

        Parameters
        ----------
        time : float >= 0
        duration : float >= 0
            The time and duration of the new observation, in seconds
        value
        confidence
            The value and confidence of the new observations.

            Types and values should conform to the namespace of the
            Annotation object.

        Examples
        --------
        >>> ann = jams.Annotation(namespace='chord')
        >>> ann.append(time=3, duration=2, value='E#')


def __wrapper(func, *args, **kwargs):<EOL><INDENT>'''<STR_LIT>'''<EOL>code = six.get_function_code(func)<EOL>warnings.warn_explicit(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>.format(func.__module__, func.__name__,<EOL>version, version_removed),<EOL>category=DeprecationWarning,<EOL>filename=code.co_filename,<EOL>lineno=code.co_firstlineno + <NUM_LIT:1><EOL>)<EOL>return func(*args, **kwargs)<EOL><DEDENT>return decorator(__wrapper)<EOL>

This is a decorator which can be used to mark functions
    as deprecated.

    It will result in a warning being emitted when the function is used.


def slice(self, start_time, end_time, strict=False):

sliced_array = AnnotationArray()<EOL>for ann in self:<EOL><INDENT>sliced_array.append(ann.slice(start_time, end_time, strict=strict))<EOL><DEDENT>return sliced_array<EOL>

Slice every annotation contained in the annotation array using
`Annotation.slice`
and return as a new AnnotationArray

See `Annotation.slice` for details about slicing. This function does
not modify the annotations in the original annotation array.

Parameters
----------
start_time : float
    The desired start time for slicing in seconds.
end_time
    The desired end time for slicing in seconds. Must be greater than
    ``start_time``.
strict : bool
    When ``False`` (default) observations that lie at the boundaries of
    the slicing range (see `Annotation.slice` for details) will have
    their time and/or duration adjusted such that only the part of the
    observation that lies within the trim range is kept. When ``True``
    such observations are discarded and not included in the sliced
    annotation.

Returns
-------
sliced_array : AnnotationArray
    An annotation array where every annotation has been sliced.


return self.__class__.__name__<EOL>

The type (class name) of a derived JObject type


indent = len(self.type) + <NUM_LIT:2><EOL>jstr = '<STR_LIT>' + '<STR_LIT:U+0020>' * indent<EOL>props = self._display_properties()<EOL>params = jstr.join('<STR_LIT>'.format(p, summary(self[p],<EOL>indent=indent))<EOL>for (p, dp) in props)<EOL>return '<STR_LIT>'.format(self.type, params)<EOL>

Render the object alongside its attributes.


return cls(**kwargs)<EOL>

Initialize the object from a dictionary of values


def to_samples(self, times, confidence=False):

times = np.asarray(times)<EOL>if times.ndim != <NUM_LIT:1> or np.any(times < <NUM_LIT:0>):<EOL><INDENT>raise ParameterError('<STR_LIT>')<EOL><DEDENT>idx = np.argsort(times)<EOL>samples = times[idx]<EOL>values = [list() for _ in samples]<EOL>confidences = [list() for _ in samples]<EOL>for obs in self.data:<EOL><INDENT>start = np.searchsorted(samples, obs.time)<EOL>end = np.searchsorted(samples, obs.time + obs.duration, side='<STR_LIT:right>')<EOL>for i in range(start, end):<EOL><INDENT>values[idx[i]].append(obs.value)<EOL>confidences[idx[i]].append(obs.confidence)<EOL><DEDENT><DEDENT>if confidence:<EOL><INDENT>return values, confidences<EOL><DEDENT>else:<EOL><INDENT>return values<EOL><DEDENT>

Sample the annotation at specified times.

        Parameters
        ----------
        times : np.ndarray, non-negative, ndim=1
            The times (in seconds) to sample the annotation

        confidence : bool
            If `True`, return both values and confidences.
            If `False` (default) only return values.

        Returns
        -------
        values : list
            `values[i]` is a list of observation values for intervals
            that cover `times[i]`.

        confidence : list (optional)
            `confidence` values corresponding to `values`


filtered_dict = dict()<EOL>for k, item in six.iteritems(self.__dict__):<EOL><INDENT>if k.startswith('<STR_LIT:_>') or k == '<STR_LIT>':<EOL><INDENT>continue<EOL><DEDENT>if hasattr(item, '<STR_LIT>'):<EOL><INDENT>filtered_dict[k] = item.__json__<EOL><DEDENT>else:<EOL><INDENT>filtered_dict[k] = serialize_obj(item)<EOL><DEDENT><DEDENT>return filtered_dict<EOL>

r"""Return the JObject as a set of native data types for serialization.

        Note: attributes beginning with underscores are suppressed.

        This also skips the `annotations` field, which will be validated separately.


def query_pop(query, prefix, sep='<STR_LIT:.>'):

terms = query.split(sep)<EOL>if terms[<NUM_LIT:0>] == prefix:<EOL><INDENT>terms = terms[<NUM_LIT:1>:]<EOL><DEDENT>return sep.join(terms)<EOL>

Pop a prefix from a query string.

    Parameters
    ----------
    query : str
        The query string

    prefix : str
        The prefix string to pop, if it exists

    sep : str
        The string to separate fields

    Returns
    -------
    popped : str
        `query` with a `prefix` removed from the front (if found)
        or `query` if the prefix was not found

    Examples
    --------
    >>> query_pop('Annotation.namespace', 'Annotation')
    'namespace'
    >>> query_pop('namespace', 'Annotation')
    'namespace'


match = False<EOL>r_query = {}<EOL>myself = self.__class__.__name__<EOL>for k, value in six.iteritems(kwargs):<EOL><INDENT>k_pop = query_pop(k, myself)<EOL>if k_pop:<EOL><INDENT>r_query[k_pop] = value<EOL><DEDENT><DEDENT>if not r_query:<EOL><INDENT>return False<EOL><DEDENT>for key in r_query:<EOL><INDENT>if hasattr(self, key):<EOL><INDENT>match |= match_query(getattr(self, key), r_query[key])<EOL><DEDENT><DEDENT>if not match:<EOL><INDENT>for attr in dir(self):<EOL><INDENT>obj = getattr(self, attr)<EOL>if isinstance(obj, JObject):<EOL><INDENT>match |= obj.search(**r_query)<EOL><DEDENT><DEDENT><DEDENT>return match<EOL>

Query this object (and its descendants).

        Parameters
        ----------
        kwargs
            Each `(key, value)` pair encodes a search field in `key`
            and a target value in `value`.

            `key` must be a string, and should correspond to a property in
            the JAMS object hierarchy, e.g., 'Annotation.namespace` or `email`

            `value` must be either an object (tested for equality), a
            string describing a search pattern (regular expression), or a
            lambda function which evaluates to `True` if the candidate
            object matches the search criteria and `False` otherwise.

        Returns
        -------
        match : bool
            `True` if any of the search keys match the specified value,
            `False` otherwise, or if the search keys do not exist
            within the object.

        Examples
        --------
        >>> J = jams.JObject(foo=5, needle='quick brown fox')
        >>> J.search(needle='.*brown.*')
        True
        >>> J.search(needle='.*orange.*')
        False
        >>> J.search(badger='.*brown.*')
        False
        >>> J.search(foo=5)
        True
        >>> J.search(foo=10)
        False
        >>> J.search(foo=lambda x: x < 10)
        True
        >>> J.search(foo=lambda x: x > 10)
        False


if not isinstance(obs, Observation):<EOL><INDENT>raise JamsError('<STR_LIT>'.format(obs))<EOL><DEDENT>return obs.time<EOL>

Provides sorting index for Observation objects


return pd.DataFrame.from_records(list(self.data),<EOL>columns=['<STR_LIT:time>', '<STR_LIT>',<EOL>'<STR_LIT:value>', '<STR_LIT>'])<EOL>

Convert this annotation to a pandas dataframe.

        Returns
        -------
        df : pd.DataFrame
            Columns are `time, duration, value, confidence`.
            Each row is an observation, and rows are sorted by
            ascending `time`.


return self.__dict__[key]<EOL>


n = len(self.data)<EOL>div_id = _get_divid(self)<EOL>out = r'''<STR_LIT>'''.format(div_id, self.namespace, n)<EOL>out += r'''<STR_LIT>'''.format(div_id)<EOL>out += r'''<STR_LIT>'''.format(self.annotation_metadata._repr_html_())<EOL>out += r'''<STR_LIT>'''.format(self.sandbox._repr_html_())<EOL>out += r'''<STR_LIT>'''.format(self.namespace, n)<EOL>out += r'''<STR_LIT>'''<EOL>if max_rows is None or n <= max_rows:<EOL><INDENT>out += self._fmt_rows(<NUM_LIT:0>, n)<EOL><DEDENT>else:<EOL><INDENT>out += self._fmt_rows(<NUM_LIT:0>, max_rows//<NUM_LIT:2>)<EOL>out += r'''<STR_LIT>'''<EOL>out += self._fmt_rows(n-max_rows//<NUM_LIT:2>, n)<EOL><DEDENT>out += r'''<STR_LIT>'''<EOL>out += r'''<STR_LIT>'''<EOL>out += r'''<STR_LIT>'''<EOL>return out<EOL>

Render this annotation list in HTML

        Returns
        -------
        rendered : str
            An HTML table containing this annotation's data.


<EOL>if self.file_metadata.duration is None:<EOL><INDENT>raise JamsError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>if (start_time < <NUM_LIT:0> or<EOL>start_time > float(self.file_metadata.duration) or<EOL>end_time < start_time or<EOL>end_time > float(self.file_metadata.duration)):<EOL><INDENT>raise ParameterError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'.format(float(self.file_metadata.duration)))<EOL><DEDENT>jam_sliced = JAMS(annotations=None,<EOL>file_metadata=self.file_metadata,<EOL>sandbox=self.sandbox)<EOL>jam_sliced.annotations = self.annotations.slice(<EOL>start_time, end_time, strict=strict)<EOL>jam_sliced.file_metadata.duration = end_time - start_time<EOL>if '<STR_LIT>' not in jam_sliced.sandbox.keys():<EOL><INDENT>jam_sliced.sandbox.update(<EOL>slice=[{'<STR_LIT>': start_time, '<STR_LIT>': end_time}])<EOL><DEDENT>else:<EOL><INDENT>jam_sliced.sandbox.slice.append(<EOL>{'<STR_LIT>': start_time, '<STR_LIT>': end_time})<EOL><DEDENT>return jam_sliced<EOL>

Slice all the annotations inside the jam and return as a new `JAMS`
object.

See `Annotation.slice` for details about how the annotations
are sliced.

This operation is also documented in the jam-level sandbox
with a list keyed by ``JAMS.sandbox.slice`` containing a tuple for each
jam-level slice of the form ``(start_time, end_time)``.

Since slicing is implemented using trimming, the operation will also be
documented in ``JAMS.sandbox.trim`` as described in `JAMS.trim`.

This function also copies over all of the file metadata from the
original jam.

Note: slicing will affect the duration of the jam, i.e. the new value
of ``JAMS.file_metadata.duration`` will be ``end_time - start_time``.

Parameters
----------
start_time : float
    The desired start time for slicing in seconds.
end_time
    The desired end time for slicing in seconds. Must be greater than
    ``start_time``.
strict : bool
    When ``False`` (default) observations that lie at the boundaries of
    the slicing range (see `Annotation.slice` for details), will have
    their time and/or duration adjusted such that only the part of the
    observation that lies within the slice range is kept. When ``True``
    such observations are discarded and not included in the sliced
    annotation.

Returns
-------
jam_sliced: JAMS
    The sliced jam with sliced annotations, returned as a new
    JAMS object.


if schema.is_dense(self.namespace):<EOL><INDENT>dense_records = dict()<EOL>for field in Observation._fields:<EOL><INDENT>dense_records[field] = []<EOL><DEDENT>for obs in self.data:<EOL><INDENT>for key, val in six.iteritems(obs._asdict()):<EOL><INDENT>dense_records[key].append(serialize_obj(val))<EOL><DEDENT><DEDENT>return dense_records<EOL><DEDENT>else:<EOL><INDENT>return [serialize_obj(_) for _ in self.data]<EOL><DEDENT>

r"""JSON-serialize the observation sequence.


super(AnnotationArray, self).__init__()<EOL>if annotations is None:<EOL><INDENT>annotations = list()<EOL><DEDENT>self.extend([Annotation(**obj) for obj in annotations])<EOL>

Create an AnnotationArray.

        Parameters
        ----------
        annotations: list
            List of Annotations, or appropriately formated dicts
            is consistent with Annotation.


def __init__(self, curator=None, version='<STR_LIT>', corpus='<STR_LIT>', annotator=None,<EOL>annotation_tools='<STR_LIT>', annotation_rules='<STR_LIT>', validation='<STR_LIT>',<EOL>data_source='<STR_LIT>'):

super(AnnotationMetadata, self).__init__()<EOL>if curator is None:<EOL><INDENT>curator = Curator()<EOL><DEDENT>if annotator is None:<EOL><INDENT>annotator = JObject()<EOL><DEDENT>self.curator = Curator(**curator)<EOL>self.annotator = JObject(**annotator)<EOL>self.version = version<EOL>self.corpus = corpus<EOL>self.annotation_tools = annotation_tools<EOL>self.annotation_rules = annotation_rules<EOL>self.validation = validation<EOL>self.data_source = data_source<EOL>

Create an AnnotationMetadata object.

        Parameters
        ----------
        curator: Curator, default=None
            Object documenting a name and email address for the person of
            correspondence.

        version: string, default=''
            Version of this annotation.

        annotator: dict, default=None
            Sandbox for information about the specific annotator, such as
            musical experience, skill level, principal instrument, etc.

        corpus: str, default=''
            Collection assignment.

        annotation_tools: str, default=''
            Description of the tools used to create the annotation.

        annotation_rules: str, default=''
            Description of the rules provided to the annotator.

        validation: str, default=''
            Methods for validating the integrity of the data.

        data_source: str, default=''
            Description of where the data originated, e.g. 'Manual Annotation'.


for name, value in six.iteritems(kwargs):<EOL><INDENT>setattr(self, name, value)<EOL><DEDENT>

Update the attributes of a JObject.

        Parameters
        ----------
        kwargs
            Keyword arguments of the form `attribute=new_value`

        Examples
        --------
        >>> J = jams.JObject(foo=5)
        >>> J.dumps()
        '{"foo": 5}'
        >>> J.update(bar='baz')
        >>> J.dumps()
        '{"foo": 5, "bar": "baz"}'


if hasattr(obj, '<STR_LIT>'):<EOL><INDENT>rep = obj.__summary__()<EOL><DEDENT>elif isinstance(obj, SortedKeyList):<EOL><INDENT>rep = '<STR_LIT>'.format(len(obj))<EOL><DEDENT>else:<EOL><INDENT>rep = repr(obj)<EOL><DEDENT>return rep.replace('<STR_LIT:\n>', '<STR_LIT:\n>' + '<STR_LIT:U+0020>' * indent)<EOL>

Helper function to format repr strings for JObjects and friends.

    Parameters
    ----------
    obj
        The object to repr

    indent : int >= 0
        indent each new line by `indent` spaces

    Returns
    -------
    r : str
        If `obj` has a `__summary__` method, it is used.

        If `obj` is a `SortedKeyList`, then it returns a description
        of the length of the list.

        Otherwise, `repr(obj)`.


def __init__(self, title='<STR_LIT>', artist='<STR_LIT>', release='<STR_LIT>', duration=None,<EOL>identifiers=None, jams_version=None):

super(FileMetadata, self).__init__()<EOL>if jams_version is None:<EOL><INDENT>jams_version = __VERSION__<EOL><DEDENT>if identifiers is None:<EOL><INDENT>identifiers = Sandbox()<EOL><DEDENT>self.title = title<EOL>self.artist = artist<EOL>self.release = release<EOL>self.duration = duration<EOL>self.identifiers = Sandbox(**identifiers)<EOL>self.jams_version = jams_version<EOL>

Create a file-level Metadata object.

        Parameters
        ----------
        title: str
            Name of the recording.

        artist: str
            Name of the artist / musician.

        release: str
            Name of the release

        duration: number >= 0
            Time duration of the file, in seconds.

        identifiers : jams.Sandbox
            Sandbox of identifier keys (eg, musicbrainz ids)

        jams_version: str
            Version of the JAMS Schema.


self.append_records([dict(time=t, duration=d, value=v, confidence=c)<EOL>for (t, d, v, c)<EOL>in six.moves.zip(columns['<STR_LIT:time>'],<EOL>columns['<STR_LIT>'],<EOL>columns['<STR_LIT:value>'],<EOL>columns['<STR_LIT>'])])<EOL>

Add observations from column-major storage.

        This is primarily used for deserializing densely packed data.

        Parameters
        ----------
        columns : dict of lists
            Keys must be `time, duration, value, confidence`,
            and each much be a list of equal length.


super(JObject, self).__init__()<EOL>for name, value in six.iteritems(kwargs):<EOL><INDENT>setattr(self, name, value)<EOL><DEDENT>

Construct a new JObject

        Parameters
        ----------
        kwargs
            Each keyword argument becomes an attribute with the specified value

        Examples
        --------
        >>> J = jams.JObject(foo=5)
        >>> J.foo
        5
        >>> dict(J)
        {'foo': 5}


schema = load_json(schema_file)<EOL>for jams_file in jams_files:<EOL><INDENT>try:<EOL><INDENT>jams = load_json(jams_file)<EOL>jsonschema.validate(jams, schema)<EOL>print('<STR_LIT>'.format(jams_file))<EOL><DEDENT>except jsonschema.ValidationError as exc:<EOL><INDENT>print('<STR_LIT>'.format(jams_file))<EOL>print(exc)<EOL><DEDENT><DEDENT>


with open(filename, '<STR_LIT:r>') as fdesc:<EOL><INDENT>return json.load(fdesc)<EOL><DEDENT>


def mkclick(freq, sr=<NUM_LIT>, duration=<NUM_LIT:0.1>):

times = np.arange(int(sr * duration))<EOL>click = np.sin(<NUM_LIT:2> * np.pi * times * freq / float(sr))<EOL>click *= np.exp(- times / (<NUM_LIT> * sr))<EOL>return click<EOL>

Generate a click sample.

    This replicates functionality from mir_eval.sonify.clicks,
    but exposes the target frequency and duration.


def piano_roll(annotation, sr=<NUM_LIT>, length=None, **kwargs):

intervals, pitches = annotation.to_interval_values()<EOL>pitch_map = {f: idx for idx, f in enumerate(np.unique(pitches))}<EOL>gram = np.zeros((len(pitch_map), len(intervals)))<EOL>for col, f in enumerate(pitches):<EOL><INDENT>gram[pitch_map[f], col] = <NUM_LIT:1><EOL><DEDENT>return filter_kwargs(mir_eval.sonify.time_frequency,<EOL>gram, pitches, intervals,<EOL>sr, length=length, **kwargs)<EOL>

Sonify a piano-roll

    This uses mir_eval.sonify.time_frequency, and is appropriate
    for sparse transcription data, e.g., annotations in the `note_midi`
    namespace.


def sonify(annotation, sr=<NUM_LIT>, duration=None, **kwargs):

length = None<EOL>if duration is None:<EOL><INDENT>duration = annotation.duration<EOL><DEDENT>if duration is not None:<EOL><INDENT>length = int(duration * sr)<EOL><DEDENT>if annotation.namespace in SONIFY_MAPPING:<EOL><INDENT>ann = coerce_annotation(annotation, annotation.namespace)<EOL>return SONIFY_MAPPING[annotation.namespace](ann,<EOL>sr=sr,<EOL>length=length,<EOL>**kwargs)<EOL><DEDENT>for namespace, func in six.iteritems(SONIFY_MAPPING):<EOL><INDENT>try:<EOL><INDENT>ann = coerce_annotation(annotation, namespace)<EOL>return func(ann, sr=sr, length=length, **kwargs)<EOL><DEDENT>except NamespaceError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>raise NamespaceError('<STR_LIT>'<EOL>.format(annotation.namespace))<EOL>

Sonify a jams annotation through mir_eval

    Parameters
    ----------
    annotation : jams.Annotation
        The annotation to sonify

    sr = : positive number
        The sampling rate of the output waveform

    duration : float (optional)
        Optional length (in seconds) of the output waveform

    kwargs
        Additional keyword arguments to mir_eval.sonify functions

    Returns
    -------
    y_sonified : np.ndarray
        The waveform of the sonified annotation

    Raises
    ------
    NamespaceError
        If the annotation has an un-sonifiable namespace


def clicks(annotation, sr=<NUM_LIT>, length=None, **kwargs):

interval, _ = annotation.to_interval_values()<EOL>return filter_kwargs(mir_eval.sonify.clicks, interval[:, <NUM_LIT:0>],<EOL>fs=sr, length=length, **kwargs)<EOL>

Sonify events with clicks.

    This uses mir_eval.sonify.clicks, and is appropriate for instantaneous
    events such as beats or segment boundaries.


def chord(annotation, sr=<NUM_LIT>, length=None, **kwargs):

intervals, chords = annotation.to_interval_values()<EOL>return filter_kwargs(mir_eval.sonify.chords,<EOL>chords, intervals,<EOL>fs=sr, length=length,<EOL>**kwargs)<EOL>

Sonify chords

    This uses mir_eval.sonify.chords.


jam_comments = jam.file_metadata.__json__<EOL>ann_comments = ann.annotation_metadata.__json__<EOL>return json.dumps({'<STR_LIT>': jam_comments,<EOL>'<STR_LIT>': ann_comments},<EOL>indent=<NUM_LIT:2>)<EOL>

Get the metadata from a jam and an annotation, combined as a string.

    Parameters
    ----------
    jam : JAMS
        The jams object

    ann : Annotation
        An annotation object

    Returns
    -------
    comments : str
        The jam.file_metadata and ann.annotation_metadata, combined and serialized


def convert_jams(jams_file, output_prefix, csv=False, comment_char='<STR_LIT:#>', namespaces=None):

if namespaces is None:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>jam = jams.load(jams_file)<EOL>counter = collections.Counter()<EOL>annotations = []<EOL>for query in namespaces:<EOL><INDENT>annotations.extend(jam.search(namespace=query))<EOL><DEDENT>if csv:<EOL><INDENT>suffix = '<STR_LIT>'<EOL>sep = '<STR_LIT:U+002C>'<EOL><DEDENT>else:<EOL><INDENT>suffix = '<STR_LIT>'<EOL>sep = '<STR_LIT:\t>'<EOL><DEDENT>for ann in annotations:<EOL><INDENT>index = counter[ann.namespace]<EOL>counter[ann.namespace] += <NUM_LIT:1><EOL>filename = os.path.extsep.join([get_output_name(output_prefix,<EOL>ann.namespace,<EOL>index),<EOL>suffix])<EOL>comment = get_comments(jam, ann)<EOL>lab_dump(ann, comment, filename, sep, comment_char)<EOL><DEDENT>

Convert jams to labs.

    Parameters
    ----------
    jams_file : str
        The path on disk to the jams file in question

    output_prefix : str
        The file path prefix of the outputs

    csv : bool
        Whether to output in csv (True) or lab (False) format

    comment_char : str
        The character used to denote comments

    namespaces : list-like
        The set of namespace patterns to match for output


def get_output_name(output_prefix, namespace, index):

return '<STR_LIT>'.format(output_prefix, namespace, index)<EOL>

Get the output name (prefix)

    Parameters
    ----------
    output_prefix : str
        The path prefix of the target filename

    namespace : str
        The namespace of the annotation in question

    index : int
        The index number of this annotation within the namespace

    Returns
    -------
    output_name : str
        "output_prefix__namespace__index"


def pprint(arr, columns=('<STR_LIT>', '<STR_LIT>'),<EOL>names=('<STR_LIT>', '<STR_LIT>'),<EOL>max_rows=<NUM_LIT:32>, precision=<NUM_LIT:2>):

if max_rows is True:<EOL><INDENT>pd.set_option('<STR_LIT>', <NUM_LIT:1000>)<EOL><DEDENT>elif type(max_rows) is int:<EOL><INDENT>pd.set_option('<STR_LIT>', max_rows)<EOL><DEDENT>pd.set_option('<STR_LIT>', precision)<EOL>df = pd.DataFrame(arr.flatten(), index=arr['<STR_LIT:id>'].flatten(),<EOL>columns=columns)<EOL>df.columns = names<EOL>return df.style.format({names[<NUM_LIT:0>]: '<STR_LIT>',<EOL>names[<NUM_LIT:1>]: '<STR_LIT>'})<EOL>

Create a pandas DataFrame from a numpy ndarray.

By default use temp and lum with max rows of 32 and precision of 2.

arr - An numpy.ndarray.
columns - The columns to include in the pandas DataFrame. Defaults to
          temperature and luminosity.
names - The column names for the pandas DataFrame. Defaults to
        Temperature and Luminosity.
max_rows - If max_rows is an integer then set the pandas
           display.max_rows option to that value. If max_rows
           is True then set display.max_rows option  to 1000.
precision - An integer to set the pandas precision option.


def __init__(self, query=None, table=None):

if table is None:<EOL><INDENT>if query:<EOL><INDENT>super().__init__(query=query)<EOL><DEDENT>else:<EOL><INDENT>super().__init__(self.query)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>super().__init__(table=table)<EOL><DEDENT>

Initialize the data using the default query for Berkeley 20,
or a provided query xor table.


output_notebook(INLINE, hide_banner=True)<EOL>if debug:<EOL><INDENT>_setup_logging(logging.DEBUG)<EOL>logging.debug('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>_setup_logging(logging.WARNING)<EOL><DEDENT>if '<STR_LIT>' not in os.environ:<EOL><INDENT>global jupyter_proxy_url<EOL>jupyter_proxy_url = '<STR_LIT>'<EOL>logging.info('<STR_LIT>')<EOL><DEDENT>

Called at the start of notebook execution to setup the environment.

    This will configure bokeh, and setup the logging library to be
    reasonable.


temps, lums = round_teff_luminosity(cluster)<EOL>x, y = temps, lums<EOL>colors, color_mapper = hr_diagram_color_helper(temps)<EOL>x_range = [max(x) + max(x) * <NUM_LIT>, min(x) - min(x) * <NUM_LIT>]<EOL>source = ColumnDataSource(data=dict(x=x, y=y, color=colors))<EOL>pf = figure(y_axis_type='<STR_LIT>', x_range=x_range, name='<STR_LIT>',<EOL>tools='<STR_LIT>',<EOL>title='<STR_LIT>'.format(cluster.name))<EOL>pf.select(BoxSelectTool).select_every_mousemove = False<EOL>pf.select(LassoSelectTool).select_every_mousemove = False<EOL>hover = pf.select(HoverTool)[<NUM_LIT:0>]<EOL>hover.tooltips = [("<STR_LIT>", "<STR_LIT>"),<EOL>("<STR_LIT>", "<STR_LIT>")]<EOL>_diagram(source=source, plot_figure=pf, name='<STR_LIT>',<EOL>color={'<STR_LIT>': '<STR_LIT>', '<STR_LIT>': color_mapper},<EOL>xaxis_label='<STR_LIT>',<EOL>yaxis_label='<STR_LIT>')<EOL>return pf<EOL>

Given a cluster create a Bokeh plot figure creating an
H-R diagram.


cluster = get_hr_data(cluster_name)<EOL>temps, lums = round_teff_luminosity(cluster)<EOL>x, y = temps, lums<EOL>colors, color_mapper = hr_diagram_color_helper(temps)<EOL>x_range = [max(x) + max(x) * <NUM_LIT>, min(x) - min(x) * <NUM_LIT>]<EOL>source = ColumnDataSource(data=dict(x=x, y=y, color=colors), name='<STR_LIT>')<EOL>source_selected = ColumnDataSource(data=dict(x=[], y=[], color=[]),<EOL>name='<STR_LIT>')<EOL>pf = figure(y_axis_type='<STR_LIT>', x_range=x_range,<EOL>tools='<STR_LIT>',<EOL>title='<STR_LIT>'.format(cluster.name))<EOL>_diagram(source=source, plot_figure=pf, name='<STR_LIT>', color={'<STR_LIT>':<EOL>'<STR_LIT>', '<STR_LIT>': color_mapper},<EOL>xaxis_label='<STR_LIT>',<EOL>yaxis_label='<STR_LIT>')<EOL>pf_selected = figure(y_axis_type='<STR_LIT>', y_range=pf.y_range,<EOL>x_range=x_range,<EOL>tools='<STR_LIT>',<EOL>title='<STR_LIT>'.format(cluster.name))<EOL>_diagram(source=source_selected, plot_figure=pf_selected, name='<STR_LIT>',<EOL>color={'<STR_LIT>': '<STR_LIT>', '<STR_LIT>': color_mapper},<EOL>xaxis_label='<STR_LIT>',<EOL>yaxis_label='<STR_LIT>')<EOL>source.callback = CustomJS(args=dict(source_selected=source_selected),<EOL>code="""<STR_LIT>""")<EOL>show_with_bokeh_server(row(pf, pf_selected))<EOL>

Given a cluster create two Bokeh plot based H-R diagrams.
The Selection in the left H-R diagram will show up on the
right one.


cluster = get_hr_data(cluster_name)<EOL>pf = hr_diagram_figure(cluster)<EOL>show_with_bokeh_server(pf)<EOL>

Create a :class:`~bokeh.plotting.figure.Figure` to create an H-R
    diagram using the cluster_name; then show it.

    Re


temps = [round(t, -<NUM_LIT:1>) for t in teff(cluster)]<EOL>lums = [round(l, <NUM_LIT:3>) for l in luminosity(cluster)]<EOL>return temps, lums<EOL>

Returns rounded teff and luminosity lists.


x, y = get_hr_data(cluster_name)<EOL>y_range = [max(y) + <NUM_LIT:0.5>, min(y) - <NUM_LIT>]<EOL>pf = figure(y_range=y_range, title=cluster_name)<EOL>_diagram(x, y, pf)<EOL>show_with_bokeh_server(pf)<EOL>

Create a :class:`~bokeh.plotting.figure.Figure` to create an H-R
    diagram using the cluster_name; then show it.


colors = []<EOL>for t in teffs:<EOL><INDENT>if t >= <NUM_LIT>:<EOL><INDENT>colors.append('<STR_LIT>')  <EOL><DEDENT>elif t >= <NUM_LIT>:<EOL><INDENT>colors.append('<STR_LIT>')  <EOL><DEDENT>elif t >= <NUM_LIT>:<EOL><INDENT>colors.append('<STR_LIT>')  <EOL><DEDENT>elif t >= <NUM_LIT>:<EOL><INDENT>colors.append('<STR_LIT>')  <EOL><DEDENT>else:<EOL><INDENT>colors.append('<STR_LIT>')  <EOL><DEDENT><DEDENT>return colors<EOL>

Conventional color descriptions of stars.
Source: https://en.wikipedia.org/wiki/Stellar_classification


arr['<STR_LIT>'] = np.around(arr['<STR_LIT>'], -<NUM_LIT:1>)<EOL>arr['<STR_LIT>'] = np.around(arr['<STR_LIT>'], <NUM_LIT:3>)<EOL>return arr<EOL>

Return the numpy array with rounded teff and luminosity columns.


b_vs, _ = cluster.stars()<EOL>teffs = []<EOL>for b_v in b_vs:<EOL><INDENT>b_v -= cluster.eb_v<EOL>if b_v > -<NUM_LIT>:<EOL><INDENT>x = (<NUM_LIT> - b_v) / <NUM_LIT><EOL><DEDENT>else:<EOL><INDENT>x = (<NUM_LIT> - math.sqrt(<NUM_LIT> + <NUM_LIT> * b_v)) / <NUM_LIT><EOL><DEDENT>teffs.append(math.pow(<NUM_LIT:10>, x))<EOL><DEDENT>return teffs<EOL>

Calculate Teff for main sequence stars ranging from Teff 3500K - 8000K. Use
[Fe/H] of the cluster, if available.

Returns a list of Teff values.


teffs = teff(cluster)<EOL>lums = luminosity(cluster)<EOL>arr = cluster.to_array()<EOL>i = <NUM_LIT:0><EOL>for row in arr:<EOL><INDENT>row['<STR_LIT>'][<NUM_LIT:0>] = np.array([lums[i]], dtype='<STR_LIT:f>')<EOL>row['<STR_LIT>'][<NUM_LIT:0>] = np.array([teffs[i]], dtype='<STR_LIT:f>')<EOL>i += <NUM_LIT:1><EOL><DEDENT>arr = round_arr_teff_luminosity(arr)<EOL>return arr<EOL>

Create a numpy.ndarray with all observed fields and
computed teff and luminosity values.


return self.book<EOL>

Returns the Real-Time Order Book.

        :returns: the Real-Time Order Book
        :rtype: dict


self.pause = False<EOL>


return self._request('<STR_LIT>', *args, **kwargs)<EOL>

Performs HTTP POST requests.

        :param args: arguments
        :param kwargs: argument keywords
        :returns: requested data
        :raises APIError: for non-2xx responses


if not str(response.status_code).startswith('<STR_LIT:2>'):<EOL><INDENT>raise get_api_error(response)<EOL><DEDENT>return response<EOL>

Returns the given response or raises an APIError for non-2xx responses.

        :param requests.Response response: HTTP response
        :returns: requested data
        :rtype: requests.Response
        :raises APIError: for non-2xx responses


if isinstance(time, str):<EOL><INDENT>return time<EOL><DEDENT>elif isinstance(time, datetime):<EOL><INDENT>return time.strftime('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>return None<EOL><DEDENT>

Makes sure we have proper ISO 8601 time.

        :param time: either already ISO 8601 a string or datetime.datetime
        :returns: ISO 8601 time
        :rtype: str


return self.before<EOL>

Returns True if the direction is set to before.

        :returns: True if the direction is set to before, otherwise False
        :rtype: bool


Performs HTTP GET requests.

        :param args: arguments
        :param kwargs: argument keywords
        :returns: requested data
        :raises APIError: for non-2xx responses


return self.after_cursor<EOL>

Acquires the after cursor.

        :returns: the after cursor


next = self.endpoint()<EOL>if next is not None:<EOL><INDENT>return next<EOL><DEDENT>raise StopIteration<EOL>

Iterator function for Python 3.

        :returns: the next message in the sequence
        :raises StopIteration: if there are no more messages


self.before = True<EOL>


self.before = False<EOL>


def _request(self, method, *relative_path_parts, **kwargs):

raise NotImplementedError<EOL>


def withdraw(self, amount, coinbase_account_id):

return self._deposit_withdraw('<STR_LIT>', amount, coinbase_account_id)<EOL>


return self._new_report(start_date,<EOL>'<STR_LIT>',<EOL>end_date,<EOL>product_id,<EOL>account_id,<EOL>format,<EOL>email)<EOL>


return self._get('<STR_LIT>', account_id, '<STR_LIT>')<EOL>


return self._delete('<STR_LIT>', order_id)<EOL>


return self._get('<STR_LIT>', order_id)<EOL>


uri = self._create_api_uri(*relative_path_parts)<EOL>if method == '<STR_LIT>':<EOL><INDENT>response = get(uri, auth=self.auth, params=kwargs.get('<STR_LIT>', None))<EOL><DEDENT>elif method == '<STR_LIT>':<EOL><INDENT>response = post(uri, auth=self.auth, json=kwargs.get('<STR_LIT:data>', None))<EOL><DEDENT>else:<EOL><INDENT>response = delete(uri, auth=self.auth, json=kwargs.get('<STR_LIT:data>', None))<EOL><DEDENT>return self._handle_response(response).json()<EOL>

Sends an HTTP request to the REST API and receives the requested data.

        :param str method: HTTP method name
        :param relative_path_parts: the relative paths for the request URI
        :param kwargs: argument keywords
        :returns: requested data
        :raises APIError: for non-2xx responses


return self._get('<STR_LIT>', params={'<STR_LIT:status>':status})<EOL>


return self._get('<STR_LIT>', product_id, '<STR_LIT>')<EOL>


def get_historic_trades(self, start, end, granularity, product_id='<STR_LIT>'):

params = {<EOL>'<STR_LIT:start>':self._format_iso_time(start),<EOL>'<STR_LIT:end>':self._format_iso_time(end),<EOL>'<STR_LIT>':granularity<EOL>}<EOL>return self._get('<STR_LIT>', product_id, '<STR_LIT>', params=params)<EOL>

`<https://docs.exchange.coinbase.com/#get-historic-rates>`_

        :param start: either datetime.datetime or str in ISO 8601
        :param end: either datetime.datetime or str in ISO 8601
        :pram int granularity: desired timeslice in seconds
        :returns: desired data


return self._get('<STR_LIT:time>')<EOL>


return self._get('<STR_LIT>')<EOL>


uri = self._create_api_uri(*relative_path_parts)<EOL>response = get(uri, params=kwargs.get('<STR_LIT>', None))<EOL>return self._handle_response(response).json()<EOL>


if self._ws:<EOL><INDENT>return self._ws.connected<EOL><DEDENT>return False<EOL>

Checks if we are connected to the WebSocket Feed.

        :returns: True if connected, otherwise False
        :rtype: bool


if self.connected():<EOL><INDENT>return self._format_message(self._ws.recv())<EOL><DEDENT>return None<EOL>

Receive the next message in the sequence.

        :returns: the next message in the sequence, None if not connected
        :rtype: dict


if not self.connected():<EOL><INDENT>self._ws = create_connection(self.WS_URI)<EOL>message = {<EOL>'<STR_LIT:type>':self.WS_TYPE,<EOL>'<STR_LIT>':self.WS_PRODUCT_ID<EOL>}<EOL>self._ws.send(dumps(message))<EOL>with self._lock:<EOL><INDENT>if not self._thread:<EOL><INDENT>thread = Thread(target=self._keep_alive_thread, args=[])<EOL>thread.start()<EOL><DEDENT><DEDENT><DEDENT>

Connects and subscribes to the WebSocket Feed.


if self.connected():<EOL><INDENT>self._ws.close()<EOL>self._ws = None<EOL><DEDENT>


return True<EOL>

Loaded state of the page.

        By default the driver will try to wait for any page loads to be
        complete, however it's not uncommon for it to return early. To address
        this you can override :py:attr:`loaded` to return ``True`` when the
        page has finished loading.

        :return: ``True`` if page is loaded, else ``False``.
        :rtype: bool

        Usage (Selenium)::

          from pypom import Page
          from selenium.webdriver.common.by import By

          class Mozilla(Page):

              @property
              def loaded(self):
                  body = self.find_element(By.TAG_NAME, 'body')
                  return 'loaded' in body.get_attribute('class')

        Usage (Splinter)::

          from pypom import Page

          class Mozilla(Page):

              def loaded(self):
                  body = self.find_element('tag', 'body')
                  return 'loaded' in body['class']

        Examples::

            # wait for the seed_url value to be in the current URL
            self.seed_url in self.selenium.current_url


def find_elements(self, strategy, locator):

return self.driver_adapter.find_elements(strategy, locator)<EOL>

Finds elements on the page.

        :param strategy: Location strategy to use. See :py:class:`~selenium.webdriver.common.by.By` or :py:attr:`~pypom.splinter_driver.ALLOWED_STRATEGIES`.
        :param locator: Location of target elements.
        :type strategy: str
        :type locator: str
        :return: List of :py:class:`~selenium.webdriver.remote.webelement.WebElement` or :py:class:`~splinter.element_list.ElementList`
        :rtype: list


warn("<STR_LIT>", DeprecationWarning, stacklevel=<NUM_LIT:2>)<EOL>return self.driver<EOL>


def is_element_displayed(self, strategy, locator):

return self.driver_adapter.is_element_displayed(strategy, locator)<EOL>

Checks whether an element is displayed.

        :param strategy: Location strategy to use. See :py:class:`~selenium.webdriver.common.by.By` or :py:attr:`~pypom.splinter_driver.ALLOWED_STRATEGIES`.
        :param locator: Location of target element.
        :type strategy: str
        :type locator: str
        :return: ``True`` if element is displayed, else ``False``.
        :rtype: bool


def is_element_present(self, strategy, locator):

return self.driver_adapter.is_element_present(strategy, locator)<EOL>

Checks whether an element is present.

        :param strategy: Location strategy to use. See :py:class:`~selenium.webdriver.common.by.By` or :py:attr:`~pypom.splinter_driver.ALLOWED_STRATEGIES`.
        :param locator: Location of target element.
        :type strategy: str
        :type locator: str
        :return: ``True`` if element is present, else ``False``.
        :rtype: bool


return True<EOL>

Loaded state of the page region.

        You may need to initialise your page region before it's ready for you
        to interact with it. If this is the case, you can override
        :py:attr:`loaded` to return ``True`` when the region has finished
        loading.

        :return: ``True`` if page is loaded, else ``False``.
        :rtype: bool

        Usage (Selenium)::

          from pypom import Page, Region
          from selenium.webdriver.common.by import By

          class Mozilla(Page):
              URL_TEMPLATE = 'https://www.mozilla.org/'

              @property
              def newsletter(self):
                  return Newsletter(self)

              class Newsletter(Region):
                  _root_locator = (By.ID, 'newsletter-form')

                  @property
                  def loaded(self):
                      return 'loaded' in self.root.get_attribute('class')

        Usage (Splinter)::

          from pypom import Page, Region

          class Mozilla(Page):
              URL_TEMPLATE = 'https://www.mozilla.org/'

              @property
              def newsletter(self):
                  return Newsletter(self)

              class Newsletter(Region):
                  _root_locator = ('id', 'newsletter-form')

                  @property
                  def loaded(self):
                      return 'loaded' in self.root['class']


return self.driver_adapter.is_element_displayed(<EOL>strategy, locator, root=self.root<EOL>)<EOL>


return self.driver_adapter.is_element_present(strategy, locator, root=self.root)<EOL>


def find_element(self, strategy, locator):

return self.driver_adapter.find_element(strategy, locator, root=self.root)<EOL>

Finds an element on the page.

        :param strategy: Location strategy to use. See :py:class:`~selenium.webdriver.common.by.By` or :py:attr:`~pypom.splinter_driver.ALLOWED_STRATEGIES`.
        :param locator: Location of target element.
        :type strategy: str
        :type locator: str
        :return: An element.
        :rytpe: :py:class:`~selenium.webdriver.remote.webelement.WebElement` or :py:class:`~splinter.driver.webdriver.WebDriverElement`


return self.driver_adapter.find_elements(strategy, locator, root=self.root)<EOL>


Called after waiting for the region to load


def find_element(strategy, locator, root=None):

Finds an element on the page.

        :param strategy: Location strategy to use (type depends on the driver implementation)
        :param locator: Location of target element.
        :param root: (optional) root node.
        :type strategy: str
        :type locator: str
        :type root: web element object or None.
        :return: web element object
        :rtype: it depends on the driver implementation


Returns a WebDriverWait like property for a given timeout.

        :param timeout: Timeout used by WebDriverWait like calls
        :type timeout: int


def find_elements(strategy, locator, root=None):

Finds elements on the page.

        :param strategy: Location strategy to use (type depends on the driver implementation)
        :param locator: Location of target elements.
        :param root: (optional) root node.
        :type strategy: str
        :type locator: str
        :type root: web element object or None.
        :return: iterable of web element objects
        :rtype: iterable (if depends on the driver implementation)


def is_element_present(strategy, locator, root=None):

Checks whether an element is present.

        :param strategy: Location strategy to use (type depends on the driver implementation)
        :param locator: Location of target element.
        :param root: (optional) root node.
        :type strategy: str
        :type locator: str
        :type root: web element object or None.
        :return: ``True`` if element is present, else ``False``.
        :rtype: bool


Open the page.
        Navigates to :py:attr:`url`


def find_elements(self, strategy, locator, root=None):

if root is not None:<EOL><INDENT>return root.find_elements(strategy, locator)<EOL><DEDENT>return self.driver.find_elements(strategy, locator)<EOL>

Finds elements on the page.

        :param strategy: Location strategy to use. See :py:class:`~selenium.webdriver.common.by.By` for valid values.
        :param locator: Location of target elements.
        :param root: (optional) root node.
        :type strategy: str
        :type locator: str
        :type root: str :py:class:`~selenium.webdriver.remote.webelement.WebElement` object or None.
        :return: List of :py:class:`~selenium.webdriver.remote.webelement.WebElement` objects.
        :rtype: list


return WebDriverWait(self.driver, timeout)<EOL>

Returns a WebDriverWait like property for a given timeout.

        :param timeout: Timeout used by WebDriverWait calls
        :type timeout: int


def is_element_present(self, strategy, locator, root=None):

try:<EOL><INDENT>return self.find_element(strategy, locator, root=root)<EOL><DEDENT>except NoSuchElementException:<EOL><INDENT>return False<EOL><DEDENT>

Checks whether an element is present.

        :param strategy: Location strategy to use. See :py:class:`~selenium.webdriver.common.by.By` for valid values.
        :param locator: Location of target element.
        :param root: (optional) root node.
        :type strategy: str
        :type locator: str
        :type root: str :py:class:`~selenium.webdriver.remote.webelement.WebElement` object or None.
        :return: ``True`` if element is present, else ``False``.
        :rtype: bool


def is_element_displayed(self, strategy, locator, root=None):

try:<EOL><INDENT>return self.find_element(strategy, locator, root=root).is_displayed()<EOL><DEDENT>except NoSuchElementException:<EOL><INDENT>return False<EOL><DEDENT>

Checks whether an element is displayed.

        :param strategy: Location strategy to use. See :py:class:`~selenium.webdriver.common.by.By` for valid values.
        :param locator: Location of target element.
        :param root: (optional) root node.
        :type strategy: str
        :type locator: str
        :type root: str :py:class:`~selenium.webdriver.remote.webelement.WebElement` object or None.
        :return: ``True`` if element is displayed, else ``False``.
        :rtype: bool


node = root or self.driver<EOL>if strategy in ALLOWED_STRATEGIES:<EOL><INDENT>return getattr(node, "<STR_LIT>" + strategy)(locator)<EOL><DEDENT>raise UsageError("<STR_LIT>")<EOL>

Finds elements on the page.

        :param strategy: Location strategy to use. See pypom.splinter_driver.ALLOWED_STRATEGIES for valid values.
        :param locator: Location of target elements.
        :type strategy: str
        :type locator: str
        :return: List of :py:class:`~splinter.driver.webdriver.WebDriverElement`
        :rtype: :py:class:`splinter.element_list.ElementList`


def get(self, query, responseformat="<STR_LIT>", verbosity="<STR_LIT:body>", build=True):

<EOL>if build:<EOL><INDENT>full_query = self._construct_ql_query(<EOL>query, responseformat=responseformat, verbosity=verbosity<EOL>)<EOL><DEDENT>else:<EOL><INDENT>full_query = query<EOL><DEDENT>if self.debug:<EOL><INDENT>logging.getLogger().info(query)<EOL><DEDENT>r = self._get_from_overpass(full_query)<EOL>content_type = r.headers.get("<STR_LIT>")<EOL>if self.debug:<EOL><INDENT>print(content_type)<EOL><DEDENT>if content_type == "<STR_LIT>":<EOL><INDENT>result = []<EOL>reader = csv.reader(StringIO(r.text), delimiter="<STR_LIT:\t>")<EOL>for row in reader:<EOL><INDENT>result.append(row)<EOL><DEDENT>return result<EOL><DEDENT>elif content_type in ("<STR_LIT>", "<STR_LIT>", "<STR_LIT>"):<EOL><INDENT>return r.text<EOL><DEDENT>elif content_type == "<STR_LIT:application/json>":<EOL><INDENT>response = json.loads(r.text)<EOL><DEDENT>if not build:<EOL><INDENT>return response<EOL><DEDENT>if "<STR_LIT>" not in response:<EOL><INDENT>raise UnknownOverpassError("<STR_LIT>")<EOL><DEDENT>overpass_remark = response.get("<STR_LIT>", None)<EOL>if overpass_remark and overpass_remark.startswith("<STR_LIT>"):<EOL><INDENT>raise ServerRuntimeError(overpass_remark)<EOL><DEDENT>if responseformat is not "<STR_LIT>":<EOL><INDENT>return response<EOL><DEDENT>return self._as_geojson(response["<STR_LIT>"])<EOL>

Pass in an Overpass query in Overpass QL.


def search(self, feature_type, regex=False):

raise NotImplementedError()<EOL>


if self._device.product == '<STR_LIT>':<EOL><INDENT>return <NUM_LIT:4><EOL><DEDENT>return (sensor + <NUM_LIT:1>) * <NUM_LIT:2><EOL>

Lookup the number of sensors on the device by product name.


if self._ports:<EOL><INDENT>return self._ports<EOL><DEDENT>return '<STR_LIT>'<EOL>


data = self._device.read(ENDPOINT, REQ_INT_LEN, timeout=TIMEOUT)<EOL>LOGGER.debug('<STR_LIT>', data)<EOL>return data<EOL>


if (self._device.product == '<STR_LIT>') or(self._device.product == '<STR_LIT>'):<EOL><INDENT>return <NUM_LIT:1><EOL><DEDENT>return <NUM_LIT:2><EOL>


bus_id = device.bus<EOL>dev_id = device.address<EOL>for dirent in os.listdir(USB_SYS_PREFIX):<EOL><INDENT>matches = re.match(USB_PORTS_STR + '<STR_LIT:$>', dirent)<EOL>if matches:<EOL><INDENT>bus_str = readattr(dirent, '<STR_LIT>')<EOL>if bus_str:<EOL><INDENT>busnum = float(bus_str)<EOL><DEDENT>else:<EOL><INDENT>busnum = None<EOL><DEDENT>dev_str = readattr(dirent, '<STR_LIT>')<EOL>if dev_str:<EOL><INDENT>devnum = float(dev_str)<EOL><DEDENT>else:<EOL><INDENT>devnum = None<EOL><DEDENT>if busnum == bus_id and devnum == dev_id:<EOL><INDENT>return str(matches.groups()[<NUM_LIT:1>])<EOL><DEDENT><DEDENT><DEDENT>

Find the port chain a device is plugged on.

This is done by searching sysfs for a device that matches the device
bus/address combination.

Useful when the underlying usb lib does not return device.port_number for
whatever reason.


try:<EOL><INDENT>if reset_device:<EOL><INDENT>self._device.reset()<EOL><DEDENT>for interface in [<NUM_LIT:0>,<NUM_LIT:1>]:<EOL><INDENT>if self._device.is_kernel_driver_active(interface):<EOL><INDENT>LOGGER.debug('<STR_LIT>'<EOL>'<STR_LIT>', interface, self._device, self._ports)<EOL>self._device.detach_kernel_driver(interface)<EOL><DEDENT><DEDENT>self._device.set_configuration()<EOL>usb.util.claim_interface(self._device, INTERFACE)<EOL>self._control_transfer(COMMANDS['<STR_LIT>'])<EOL>self._interrupt_read()<EOL>self._control_transfer(COMMANDS['<STR_LIT>'])<EOL>temp_data = self._interrupt_read()<EOL>if self._device.product == '<STR_LIT>':<EOL><INDENT>humidity_data = temp_data<EOL><DEDENT>else:<EOL><INDENT>humidity_data = None<EOL><DEDENT>data = {'<STR_LIT>': temp_data, '<STR_LIT>': humidity_data}<EOL>usb.util.dispose_resources(self._device)<EOL>return data<EOL><DEDENT>except usb.USBError as err:<EOL><INDENT>if not reset_device:<EOL><INDENT>LOGGER.warning("<STR_LIT>", err, self._device)<EOL>return self.get_data(True)<EOL><DEDENT>if "<STR_LIT>" in str(err):<EOL><INDENT>raise Exception(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>LOGGER.error(err)<EOL>raise<EOL><DEDENT><DEDENT>


return self._devices<EOL>

Get a list of all devices attached to this handler


def get_temperature(self, format='<STR_LIT>', sensor=<NUM_LIT:0>):

results = self.get_temperatures(sensors=[sensor,])<EOL>if format == '<STR_LIT>':<EOL><INDENT>return results[sensor]['<STR_LIT>']<EOL><DEDENT>elif format == '<STR_LIT>':<EOL><INDENT>return results[sensor]['<STR_LIT>']<EOL><DEDENT>elif format == '<STR_LIT>':<EOL><INDENT>return results[sensor]['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>


if self._bus:<EOL><INDENT>return self._bus<EOL><DEDENT>return '<STR_LIT>'<EOL>


return u"<STR_LIT>"<EOL>


skipped_error_codes = "<STR_LIT>"<EOL>if self.current.kind == tk.COMMENT:<EOL><INDENT>if "<STR_LIT>" in self.current.value:<EOL><INDENT>skipped_error_codes = "<STR_LIT>".join(self.current.value.split("<STR_LIT>")[<NUM_LIT:1>:])<EOL><DEDENT>elif self.current.value.startswith("<STR_LIT>"):<EOL><INDENT>skipped_error_codes = "<STR_LIT:all>"<EOL><DEDENT><DEDENT>return skipped_error_codes<EOL>

Parse a definition comment for noqa skips.


return chain([self], *self.children)<EOL>


<EOL>for decorator in self.decorators:<EOL><INDENT>if re.compile(r"<STR_LIT>".format(self.name)).match(decorator.name):<EOL><INDENT>return False<EOL><DEDENT><DEDENT>name_is_public = (<EOL>not self.name.startswith("<STR_LIT:_>")<EOL>or self.name in VARIADIC_MAGIC_METHODS<EOL>or self.is_magic<EOL>)<EOL>return self.parent.is_public and name_is_public<EOL>

Return True iff this method should be considered public.


def code_mapping(level, msg, default=<NUM_LIT>):

try:<EOL><INDENT>return code_mappings_by_level[level][msg]<EOL><DEDENT>except KeyError:<EOL><INDENT>pass<EOL><DEDENT>if msg.count('<STR_LIT:">') == <NUM_LIT:2> and '<STR_LIT>' in msg and msg.endswith('<STR_LIT>'):<EOL><INDENT>txt = msg[: msg.index('<STR_LIT>')]<EOL>return code_mappings_by_level[level].get(txt, default)<EOL><DEDENT>return default<EOL>


next_token = self.stream.move()<EOL>assert next_token.kind == kind<EOL>

Consume one token and verify it is of the expected kind.


return re.compile(r"<STR_LIT>").sub(r"<STR_LIT>", string).lower()<EOL>


assert self.current.value == "<STR_LIT>"<EOL>self.consume(tk.NAME)<EOL>if self.current.value != "<STR_LIT:=>":<EOL><INDENT>raise AllError("<STR_LIT>")<EOL><DEDENT>self.consume(tk.OP)<EOL>if self.current.value not in "<STR_LIT>":<EOL><INDENT>raise AllError("<STR_LIT>")<EOL><DEDENT>self.consume(tk.OP)<EOL>self.all = []<EOL>all_content = "<STR_LIT:(>"<EOL>while self.current.kind != tk.OP or self.current.value not in "<STR_LIT>":<EOL><INDENT>if self.current.kind in (tk.NL, tk.COMMENT):<EOL><INDENT>pass<EOL><DEDENT>elif self.current.kind == tk.STRING or self.current.value == "<STR_LIT:U+002C>":<EOL><INDENT>all_content += self.current.value<EOL><DEDENT>else:<EOL><INDENT>raise AllError(<EOL>"<STR_LIT>".format(<EOL>self.current.kind<EOL>)<EOL>)<EOL><DEDENT>self.stream.move()<EOL><DEDENT>self.consume(tk.OP)<EOL>all_content += "<STR_LIT:)>"<EOL>try:<EOL><INDENT>self.all = eval(all_content, {})<EOL><DEDENT>except BaseException as e:<EOL><INDENT>raise AllError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>".format(all_content, e)<EOL>)<EOL><DEDENT>

Parse the __all__ definition in a module.


<EOL>if self.err is not None:<EOL><INDENT>assert self.source is None<EOL>msg = "<STR_LIT>" % (<EOL>rst_prefix,<EOL>rst_fail_load,<EOL>"<STR_LIT>" % self.err,<EOL>)<EOL>yield <NUM_LIT:0>, <NUM_LIT:0>, msg, type(self)<EOL>module = []<EOL><DEDENT>try:<EOL><INDENT>module = parse(StringIO(self.source), self.filename)<EOL><DEDENT>except SyntaxError as err:<EOL><INDENT>msg = "<STR_LIT>" % (<EOL>rst_prefix,<EOL>rst_fail_parse,<EOL>"<STR_LIT>" % err,<EOL>)<EOL>yield <NUM_LIT:0>, <NUM_LIT:0>, msg, type(self)<EOL>module = []<EOL><DEDENT>except AllError:<EOL><INDENT>msg = "<STR_LIT>" % (<EOL>rst_prefix,<EOL>rst_fail_all,<EOL>"<STR_LIT>",<EOL>)<EOL>yield <NUM_LIT:0>, <NUM_LIT:0>, msg, type(self)<EOL>module = []<EOL><DEDENT>for definition in module:<EOL><INDENT>if not definition.docstring:<EOL><INDENT>continue<EOL><DEDENT>try:<EOL><INDENT>unindented = trim(dequote_docstring(definition.docstring))<EOL>rst_errors = list(rst_lint.lint(unindented))<EOL><DEDENT>except Exception as err:<EOL><INDENT>msg = "<STR_LIT>" % (<EOL>rst_prefix,<EOL>rst_fail_lint,<EOL>"<STR_LIT>" % (definition.name, err),<EOL>)<EOL>yield definition.start, <NUM_LIT:0>, msg, type(self)<EOL>continue<EOL><DEDENT>for rst_error in rst_errors:<EOL><INDENT>if rst_error.level <= <NUM_LIT:1>:<EOL><INDENT>continue<EOL><DEDENT>msg = rst_error.message.split("<STR_LIT:\n>", <NUM_LIT:1>)[<NUM_LIT:0>]<EOL>code = code_mapping(rst_error.level, msg)<EOL>assert code < <NUM_LIT:100>, code<EOL>code += <NUM_LIT:100> * rst_error.level<EOL>msg = "<STR_LIT>" % (rst_prefix, code, msg)<EOL>yield definition.start + rst_error.line, <NUM_LIT:0>, msg, type(self)<EOL><DEDENT><DEDENT>

Use docutils to check docstrings are valid RST.


start = self.line<EOL>self.consume(tk.NAME)<EOL>name = self.current.value<EOL>self.log.debug("<STR_LIT>", class_.__name__, name)<EOL>self.stream.move()<EOL>if self.current.kind == tk.OP and self.current.value == "<STR_LIT:(>":<EOL><INDENT>parenthesis_level = <NUM_LIT:0><EOL>while True:<EOL><INDENT>if self.current.kind == tk.OP:<EOL><INDENT>if self.current.value == "<STR_LIT:(>":<EOL><INDENT>parenthesis_level += <NUM_LIT:1><EOL><DEDENT>elif self.current.value == "<STR_LIT:)>":<EOL><INDENT>parenthesis_level -= <NUM_LIT:1><EOL>if parenthesis_level == <NUM_LIT:0>:<EOL><INDENT>break<EOL><DEDENT><DEDENT><DEDENT>self.stream.move()<EOL><DEDENT><DEDENT>if self.current.kind != tk.OP or self.current.value != "<STR_LIT::>":<EOL><INDENT>self.leapfrog(tk.OP, value="<STR_LIT::>")<EOL><DEDENT>else:<EOL><INDENT>self.consume(tk.OP)<EOL><DEDENT>if self.current.kind in (tk.NEWLINE, tk.COMMENT):<EOL><INDENT>skipped_error_codes = self.parse_skip_comment()<EOL>self.leapfrog(tk.INDENT)<EOL>assert self.current.kind != tk.INDENT<EOL>docstring = self.parse_docstring()<EOL>decorators = self._accumulated_decorators<EOL>self.log.debug("<STR_LIT>", decorators)<EOL>self._accumulated_decorators = []<EOL>self.log.debug("<STR_LIT>")<EOL>children = list(self.parse_definitions(class_))<EOL>self.log.debug("<STR_LIT>", name)<EOL>end = self.line - <NUM_LIT:1><EOL><DEDENT>else:  <EOL><INDENT>skipped_error_codes = "<STR_LIT>"<EOL>docstring = self.parse_docstring()<EOL>decorators = []  <EOL>children = []<EOL>end = self.line<EOL>self.leapfrog(tk.NEWLINE)<EOL><DEDENT>definition = class_(<EOL>name,<EOL>self.source,<EOL>start,<EOL>end,<EOL>decorators,<EOL>docstring,<EOL>children,<EOL>None,<EOL>skipped_error_codes,<EOL>)<EOL>for child in definition.children:<EOL><INDENT>child.parent = definition<EOL><DEDENT>self.log.debug(<EOL>"<STR_LIT>",<EOL>class_.__name__,<EOL>name,<EOL>self.current.kind,<EOL>self.current.value,<EOL>)<EOL>return definition<EOL>

Parse a definition and return its value in a `class_` object.


self._generator = tk.generate_tokens(filelike.readline)<EOL>self.current = Token(*next(self._generator, None))<EOL>self.line = self.current.start[<NUM_LIT:0>]<EOL>self.log = log<EOL>self.got_logical_newline = True<EOL>


return (<EOL>not self.name.startswith("<STR_LIT:_>")<EOL>and self.parent.is_class<EOL>and self.parent.is_public<EOL>)<EOL>

Return True iff this class should be considered public.


super(Token, self).__init__(*args)<EOL>self.kind = TokenKind(self.kind)<EOL>


Exception.__init__(<EOL>self,<EOL>message<EOL>+ textwrap.dedent(<EOL>"""<STR_LIT>"""<EOL>),<EOL>)<EOL>

Initialize the error with a more specific message.


self.log.debug(<EOL>"<STR_LIT>", self.current.kind, self.current.value<EOL>)<EOL>while self.current.kind in (tk.COMMENT, tk.NEWLINE, tk.NL):<EOL><INDENT>self.stream.move()<EOL>self.log.debug(<EOL>"<STR_LIT>",<EOL>self.current.kind,<EOL>self.current.value,<EOL>)<EOL><DEDENT>if self.current.kind == tk.STRING:<EOL><INDENT>docstring = self.current.value<EOL>self.stream.move()<EOL>return docstring<EOL><DEDENT>return None<EOL>

Parse a single docstring and return its value.


def parse_definitions(self, class_, all=False):

while self.current is not None:<EOL><INDENT>self.log.debug(<EOL>"<STR_LIT>",<EOL>self.current.kind,<EOL>self.current.value,<EOL>)<EOL>self.log.debug("<STR_LIT>", self.stream.got_logical_newline)<EOL>if all and self.current.value == "<STR_LIT>":<EOL><INDENT>self.parse_all()<EOL><DEDENT>elif (<EOL>self.current.kind == tk.OP<EOL>and self.current.value == "<STR_LIT:@>"<EOL>and self.stream.got_logical_newline<EOL>):<EOL><INDENT>self.consume(tk.OP)<EOL>self.parse_decorators()<EOL><DEDENT>elif self.current.value in ["<STR_LIT>", "<STR_LIT:class>"]:<EOL><INDENT>yield self.parse_definition(class_._nest(self.current.value))<EOL><DEDENT>elif self.current.kind == tk.INDENT:<EOL><INDENT>self.consume(tk.INDENT)<EOL>for definition in self.parse_definitions(class_):<EOL><INDENT>yield definition<EOL><DEDENT><DEDENT>elif self.current.kind == tk.DEDENT:<EOL><INDENT>self.consume(tk.DEDENT)<EOL>return<EOL><DEDENT>elif self.current.value == "<STR_LIT>":<EOL><INDENT>self.parse_from_import_statement()<EOL><DEDENT>else:<EOL><INDENT>self.stream.move()<EOL><DEDENT><DEDENT>

Parse multiple definitions and yield them.


self.log.debug("<STR_LIT>")<EOL>is_future_import = self._parse_from_import_source()<EOL>self._parse_from_import_names(is_future_import)<EOL>

Parse a 'from x import y' statement.

        The purpose is to find __future__ statements.


while True:<EOL><INDENT>if self.current is not None:<EOL><INDENT>yield self.current<EOL><DEDENT>else:<EOL><INDENT>return<EOL><DEDENT>self.move()<EOL><DEDENT>


return not self.name.startswith("<STR_LIT:_>") or self.name.startswith("<STR_LIT>")<EOL>


assert self.current.value == "<STR_LIT>", self.current.value<EOL>self.stream.move()<EOL>is_future_import = self.current.value == "<STR_LIT>"<EOL>self.stream.move()<EOL>while (<EOL>self.current is not None<EOL>and self.current.kind in (tk.DOT, tk.NAME, tk.OP)<EOL>and self.current.value != "<STR_LIT>"<EOL>):<EOL><INDENT>self.stream.move()<EOL><DEDENT>if self.current is None or self.current.value != "<STR_LIT>":<EOL><INDENT>return False<EOL><DEDENT>self.check_current(value="<STR_LIT>")<EOL>assert self.current.value == "<STR_LIT>", self.current.value<EOL>self.stream.move()<EOL>return is_future_import<EOL>

Parse the 'from x import' part in a 'from x import y' statement.

        Return true iff `x` is __future__.


name = []<EOL>arguments = []<EOL>at_arguments = False<EOL>while self.current is not None:<EOL><INDENT>self.log.debug(<EOL>"<STR_LIT>",<EOL>self.current.kind,<EOL>self.current.value,<EOL>)<EOL>if self.current.kind == tk.NAME and self.current.value in ["<STR_LIT>", "<STR_LIT:class>"]:<EOL><INDENT>break<EOL><DEDENT>elif self.current.kind == tk.OP and self.current.value == "<STR_LIT:@>":<EOL><INDENT>self._accumulated_decorators.append(<EOL>Decorator("<STR_LIT>".join(name), "<STR_LIT>".join(arguments))<EOL>)<EOL>name = []<EOL>arguments = []<EOL>at_arguments = False<EOL><DEDENT>elif self.current.kind == tk.OP and self.current.value == "<STR_LIT:(>":<EOL><INDENT>at_arguments = True<EOL><DEDENT>elif self.current.kind == tk.OP and self.current.value == "<STR_LIT:)>":<EOL><INDENT>pass<EOL><DEDENT>elif self.current.kind == tk.NEWLINE or self.current.kind == tk.NL:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>if not at_arguments:<EOL><INDENT>name.append(self.current.value)<EOL><DEDENT>else:<EOL><INDENT>arguments.append(self.current.value)<EOL><DEDENT><DEDENT>self.stream.move()<EOL><DEDENT>self._accumulated_decorators.append(<EOL>Decorator("<STR_LIT>".join(name), "<STR_LIT>".join(arguments))<EOL>)<EOL>

Called after first @ is found.

        Parse decorators into self._accumulated_decorators.
        Continue to do so until encountering the 'def' or 'class' start token.


def _ordinal_metric(_v1, _v2, i1, i2, n_v):

if i1 > i2:<EOL><INDENT>i1, i2 = i2, i1<EOL><DEDENT>return (np.sum(n_v[i1:(i2 + <NUM_LIT:1>)]) - (n_v[i1] + n_v[i2]) / <NUM_LIT:2>) ** <NUM_LIT:2><EOL>


return np.array([[sum(<NUM_LIT:1> for rate in unit if rate == v) for v in value_domain] for unit in reliability_data.T])<EOL>

Return the value counts given the reliability data.

    Parameters
    ----------
    reliability_data : ndarray, with shape (M, N)
        Reliability data matrix which has the rate the i coder gave to the j unit, where M is the number of raters
        and N is the unit count.
        Missing rates are represented with `np.nan`.

    value_domain : array_like, with shape (V,)
        Possible values the units can take.

    Returns
    -------
    value_counts : ndarray, with shape (N, V)
        Number of coders that assigned a certain value to a determined unit, where N is the number of units
        and V is the value count.


def alpha(reliability_data=None, value_counts=None, value_domain=None, level_of_measurement='<STR_LIT>',<EOL>dtype=np.float64):

if (reliability_data is None) == (value_counts is None):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if value_counts is None:<EOL><INDENT>if type(reliability_data) is not np.ndarray:<EOL><INDENT>reliability_data = np.array(reliability_data)<EOL><DEDENT>value_domain = value_domain or np.unique(reliability_data[~np.isnan(reliability_data)])<EOL>value_counts = _reliability_data_to_value_counts(reliability_data, value_domain)<EOL><DEDENT>else:  <EOL><INDENT>if value_domain:<EOL><INDENT>assert value_counts.shape[<NUM_LIT:1>] == len(value_domain),"<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>value_domain = tuple(range(value_counts.shape[<NUM_LIT:1>]))<EOL><DEDENT><DEDENT>distance_metric = _distance_metric(level_of_measurement)<EOL>o = _coincidences(value_counts, value_domain, dtype=dtype)<EOL>n_v = np.sum(o, axis=<NUM_LIT:0>)<EOL>n = np.sum(n_v)<EOL>e = _random_coincidences(value_domain, n, n_v)<EOL>d = _distances(value_domain, distance_metric, n_v)<EOL>return <NUM_LIT:1> - np.sum(o * d) / np.sum(e * d)<EOL>

Compute Krippendorff's alpha.

    See https://en.wikipedia.org/wiki/Krippendorff%27s_alpha for more information.

    Parameters
    ----------
    reliability_data : array_like, with shape (M, N)
        Reliability data matrix which has the rate the i coder gave to the j unit, where M is the number of raters
        and N is the unit count.
        Missing rates are represented with `np.nan`.
        If it's provided then `value_counts` must not be provided.

    value_counts : ndarray, with shape (N, V)
        Number of coders that assigned a certain value to a determined unit, where N is the number of units
        and V is the value count.
        If it's provided then `reliability_data` must not be provided.

    value_domain : array_like, with shape (V,)
        Possible values the units can take.
        If the level of measurement is not nominal, it must be ordered.
        If `reliability_data` is provided, then the default value is the ordered list of unique rates that appear.
        Else, the default value is `list(range(V))`.

    level_of_measurement : string or callable
        Steven's level of measurement of the variable.
        It must be one of 'nominal', 'ordinal', 'interval', 'ratio' or a callable.

    dtype : data-type
        Result and computation data-type.

    Returns
    -------
    alpha : `dtype`
        Scalar value of Krippendorff's alpha of type `dtype`.

    Examples
    --------
    >>> reliability_data = [[np.nan, np.nan, np.nan, np.nan, np.nan, 3, 4, 1, 2, 1, 1, 3, 3, np.nan, 3],
    ...                     [1, np.nan, 2, 1, 3, 3, 4, 3, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan],
    ...                     [np.nan, np.nan, 2, 1, 3, 4, 4, np.nan, 2, 1, 1, 3, 3, np.nan, 4]]
    >>> print(round(alpha(reliability_data=reliability_data, level_of_measurement='nominal'), 6))
    0.691358
    >>> print(round(alpha(reliability_data=reliability_data, level_of_measurement='interval'), 6))
    0.810845
    >>> value_counts = np.array([[1, 0, 0, 0],
    ...                          [0, 0, 0, 0],
    ...                          [0, 2, 0, 0],
    ...                          [2, 0, 0, 0],
    ...                          [0, 0, 2, 0],
    ...                          [0, 0, 2, 1],
    ...                          [0, 0, 0, 3],
    ...                          [1, 0, 1, 0],
    ...                          [0, 2, 0, 0],
    ...                          [2, 0, 0, 0],
    ...                          [2, 0, 0, 0],
    ...                          [0, 0, 2, 0],
    ...                          [0, 0, 2, 0],
    ...                          [0, 0, 0, 0],
    ...                          [0, 0, 1, 1]])
    >>> print(round(alpha(value_counts=value_counts, level_of_measurement='nominal'), 6))
    0.691358
    >>> # The following examples were extracted from
    >>> # https://www.statisticshowto.datasciencecentral.com/wp-content/uploads/2016/07/fulltext.pdf, page 8.
    >>> reliability_data = [[1, 2, 3, 3, 2, 1, 4, 1, 2, np.nan, np.nan, np.nan],
    ...                     [1, 2, 3, 3, 2, 2, 4, 1, 2, 5, np.nan, 3.],
    ...                     [np.nan, 3, 3, 3, 2, 3, 4, 2, 2, 5, 1, np.nan],
    ...                     [1, 2, 3, 3, 2, 4, 4, 1, 2, 5, 1, np.nan]]
    >>> print(round(alpha(reliability_data, level_of_measurement='ordinal'), 3))
    0.815
    >>> print(round(alpha(reliability_data, level_of_measurement='ratio'), 3))
    0.797


def _interval_metric(v1, v2, **_kwargs):

return (v1 - v2) ** <NUM_LIT:2><EOL>


def _distances(value_domain, distance_metric, n_v):

return np.array([[distance_metric(v1, v2, i1=i1, i2=i2, n_v=n_v)<EOL>for i2, v2 in enumerate(value_domain)]<EOL>for i1, v1 in enumerate(value_domain)])<EOL>

Distances of the different possible values.

    Parameters
    ----------
    value_domain : array_like, with shape (V,)
        Possible values V the units can take.
        If the level of measurement is not nominal, it must be ordered.

    distance_metric : callable
        Callable that return the distance of two given values.

    n_v : ndarray, with shape (V,)
        Number of pairable elements for each value.

    Returns
    -------
    d : ndarray, with shape (V, V)
        Distance matrix for each value pair.


with open(resultsFile) as f:<EOL><INDENT>raw=f.read().split("<STR_LIT:\n>")<EOL><DEDENT>foldersByDay={}<EOL>for line in raw:<EOL><INDENT>folder=line.split('<STR_LIT:">')[<NUM_LIT:1>]+"<STR_LIT:\\>"<EOL>line=[]+line.split('<STR_LIT:">')[<NUM_LIT:2>].split("<STR_LIT:U+002CU+0020>")<EOL>for day in line[<NUM_LIT:1>:]:<EOL><INDENT>if not day in foldersByDay:<EOL><INDENT>foldersByDay[day]=[]<EOL><DEDENT>foldersByDay[day]=foldersByDay[day]+[folder]<EOL><DEDENT><DEDENT>nActiveDays=len(foldersByDay)<EOL>dayFirst=sorted(foldersByDay.keys())[<NUM_LIT:0>]<EOL>dayLast=sorted(foldersByDay.keys())[-<NUM_LIT:1>]<EOL>dayFirst=datetime.datetime.strptime(dayFirst, "<STR_LIT>" )<EOL>dayLast=datetime.datetime.strptime(dayLast, "<STR_LIT>" )<EOL>nDays = (dayLast - dayFirst).days + <NUM_LIT:1><EOL>emptyDays=<NUM_LIT:0><EOL>for deltaDays in range(nDays):<EOL><INDENT>day=dayFirst+datetime.timedelta(days=deltaDays)<EOL>stamp=datetime.datetime.strftime(day, "<STR_LIT>" )<EOL>if not stamp in foldersByDay:<EOL><INDENT>foldersByDay[stamp]=[]<EOL>emptyDays+=<NUM_LIT:1><EOL><DEDENT><DEDENT>percActive=nActiveDays/nDays*<NUM_LIT:100><EOL>print("<STR_LIT>"%(nActiveDays,nDays,percActive))<EOL>return foldersByDay<EOL>

returns a dict of active folders with days as keys.


modifiedTime=os.path.getmtime(fname)<EOL>stamp=time.strftime('<STR_LIT>', time.localtime(modifiedTime))<EOL>return stamp<EOL>

return "YYYY-MM-DD" when the file was modified.


assert len(char)==<NUM_LIT:1> and type(char)==str, "<STR_LIT>"<EOL>assert os.path.exists(path), "<STR_LIT>"<EOL>files = sorted(os.listdir(path))<EOL>files = [x for x in files if len(x)><NUM_LIT> and x[<NUM_LIT:4>]+x[<NUM_LIT:7>]+x[<NUM_LIT:10>]=='<STR_LIT>']<EOL>for fname in files:<EOL><INDENT>fname2 = list(fname)<EOL>fname2[<NUM_LIT:11>]=char<EOL>fname2="<STR_LIT>".join(fname2)<EOL>if fname==fname2:<EOL><INDENT>print(fname, "<STR_LIT>", fname2)<EOL><DEDENT>else:<EOL><INDENT>print(fname, "<STR_LIT>", fname2)<EOL><INDENT>fname=os.path.join(path,fname)<EOL>fname2=os.path.join(path,fname2)<EOL>if not os.path.exists(fname2):<EOL><INDENT>os.rename(fname,fname2)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>return<EOL>

Given ABFs and TIFs formatted long style, rename each of them to prefix their number with a different number.

Example: 2017_10_11_0011.abf
Becomes: 2017_10_11_?011.abf
where ? can be any character.


Add comment lines/text to an existing plot. Defaults to seconds.
Call after a plot has been made, and after margins have been set.


Load an ABF and get ready to plot stuff.


plot every sweep of an ABF file (with comments).


<EOL>exts=['<STR_LIT>','<STR_LIT>']<EOL>for fname in [x for x in self.files1 if cm.ext(x) in exts]:<EOL><INDENT>ID="<STR_LIT>"<EOL>if len(fname)><NUM_LIT:8> and fname[:<NUM_LIT:8>] in self.IDs:<EOL><INDENT>ID=fname[:<NUM_LIT:8>]<EOL><DEDENT>fname2=ID+"<STR_LIT>"+fname<EOL>if not fname2 in self.files2:<EOL><INDENT>self.log.info("<STR_LIT>"%fname2)<EOL>shutil.copy(os.path.join(self.folder1,fname),os.path.join(self.folder2,fname2))<EOL><DEDENT>if not fname[:<NUM_LIT:8>]+"<STR_LIT>" in self.files1:<EOL><INDENT>self.log.error("<STR_LIT>",fname)<EOL><DEDENT><DEDENT>exts=['<STR_LIT>','<STR_LIT>']<EOL>for fname in [x for x in self.files1 if cm.ext(x) in exts]:<EOL><INDENT>ID="<STR_LIT>"<EOL>if len(fname)><NUM_LIT:8> and fname[:<NUM_LIT:8>] in self.IDs:<EOL><INDENT>ID=fname[:<NUM_LIT:8>]<EOL><DEDENT>fname2=ID+"<STR_LIT>"+fname+"<STR_LIT>"<EOL>if not fname2 in self.files2:<EOL><INDENT>self.log.info("<STR_LIT>"%fname2)<EOL>imaging.TIF_to_jpg(os.path.join(self.folder1,fname),saveAs=os.path.join(self.folder2,fname2))<EOL><DEDENT>if not fname[:<NUM_LIT:8>]+"<STR_LIT>" in self.files1:<EOL><INDENT>self.log.error("<STR_LIT>",fname)<EOL><DEDENT><DEDENT>

run this to turn all folder1 TIFs and JPGs into folder2 data.
TIFs will be treated as micrographs and converted to JPG with enhanced
contrast. JPGs will simply be copied over.


analyze every unanalyzed ABF in the folder.


Reanalyze data for a single ABF. Also remakes child and parent html.


if type(abfID) is str:<EOL><INDENT>abfID=[abfID]<EOL><DEDENT>for thisABFid in cm.abfSort(abfID):<EOL><INDENT>parentID=cm.parent(self.groups,thisABFid)<EOL>saveAs=os.path.abspath("<STR_LIT>"%(self.folder2,parentID))<EOL>if overwrite is False and os.path.basename(saveAs) in self.files2:<EOL><INDENT>continue<EOL><DEDENT>filesByType=cm.filesByType(self.groupFiles[parentID])<EOL>html="<STR_LIT>"<EOL>html+='<STR_LIT>'<EOL>html+='<STR_LIT>'%parentID<EOL>html+='<STR_LIT>'%os.path.abspath(self.folder1+"<STR_LIT:/>"+parentID+"<STR_LIT>")<EOL>html+='<STR_LIT>'<EOL>catOrder=["<STR_LIT>","<STR_LIT>","<STR_LIT>","<STR_LIT>"]<EOL>categories=cm.list_order_by(filesByType.keys(),catOrder)<EOL>for category in [x for x in categories if len(filesByType[x])]:<EOL><INDENT>if category=='<STR_LIT>':<EOL><INDENT>html+="<STR_LIT>"<EOL><DEDENT>elif category=='<STR_LIT>':<EOL><INDENT>html+="<STR_LIT>"<EOL><DEDENT>elif category=='<STR_LIT>':<EOL><INDENT>html+="<STR_LIT>"<EOL><DEDENT>elif category=='<STR_LIT>':<EOL><INDENT>html+="<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>html+="<STR_LIT>"<EOL><DEDENT>for fname in filesByType[category]:<EOL><INDENT>html+=self.htmlFor(fname)<EOL><DEDENT>html+='<STR_LIT>'*<NUM_LIT:3><EOL><DEDENT>print("<STR_LIT>",saveAs,'<STR_LIT>')<EOL>style.save(html,saveAs,launch=launch)<EOL><DEDENT>

generate a generic flat file html for an ABF parent. You could give
this a single ABF ID, its parent ID, or a list of ABF IDs.
If a child ABF is given, the parent will automatically be used.


rescanNeeded=False<EOL>for fname in smartSort(self.fnames):<EOL><INDENT>if fname in self.fnames2:<EOL><INDENT>continue<EOL><DEDENT>ext=os.path.splitext(fname)[<NUM_LIT:1>].lower()<EOL>if ext in ["<STR_LIT>","<STR_LIT>"]:<EOL><INDENT>if not fname in self.abfFolder2:<EOL><INDENT>self.log.debug("<STR_LIT>",fname)<EOL>shutil.copy(os.path.join(self.abfFolder,fname),os.path.join(self.abfFolder2,fname))<EOL>rescanNeeded=True<EOL><DEDENT><DEDENT>if ext in ["<STR_LIT>","<STR_LIT>"]:<EOL><INDENT>if not fname+"<STR_LIT>" in self.fnames2:<EOL><INDENT>self.log.debug("<STR_LIT>",fname)<EOL>swhlab.swh_image.TIF_to_jpg(os.path.join(self.abfFolder,fname),saveAs=os.path.join(self.abfFolder2,fname+"<STR_LIT>"))<EOL>rescanNeeded=True<EOL><DEDENT><DEDENT><DEDENT>if rescanNeeded:<EOL><INDENT>self.log.debug("<STR_LIT>")<EOL>self.log.debug("<STR_LIT>")<EOL>self.folderScan()<EOL><DEDENT>

convert every .image we find to a ./swhlab/ image


if not ID in self.cells:<EOL><INDENT>self.log.error("<STR_LIT>",ID)<EOL>return<EOL><DEDENT>htmlFname=os.path.abspath(self.abfFolder2+"<STR_LIT:/>"+ID+"<STR_LIT>")<EOL>html="<STR_LIT>"%ID<EOL>npics=<NUM_LIT:0><EOL>for childID in [os.path.splitext(x)[<NUM_LIT:0>] for x in self.fnamesByCell[ID]]:<EOL><INDENT>pics=[x for x in self.fnames2 if x.startswith(childID) and os.path.splitext(x)[<NUM_LIT:1>].lower() in ["<STR_LIT>","<STR_LIT>"]]<EOL>html+="<STR_LIT>"%(os.path.abspath(self.abfFolder+'<STR_LIT:/>'+childID+"<STR_LIT>"))<EOL>for i,pic in enumerate(pics):<EOL><INDENT>html+='<STR_LIT>'%(pic,pic)<EOL>npics+=<NUM_LIT:1><EOL><DEDENT>html+="<STR_LIT>"<EOL><DEDENT>style.save(html,htmlFname)<EOL>self.log.info("<STR_LIT>",htmlFname,npics)<EOL>

generate ./swhlab/xxyxxzzz.html for a single given abf.
Input can be an ABF file path of ABF ID.


return<EOL>

Single page generator designed for easy gruop comparisons.


byExt={"<STR_LIT>":[],"<STR_LIT>":[],"<STR_LIT>":[]} <EOL>for fname in fnames:<EOL><INDENT>ext = os.path.splitext(fname)[<NUM_LIT:1>].replace("<STR_LIT:.>",'<STR_LIT>').lower()<EOL>if not ext in byExt.keys():<EOL><INDENT>byExt[ext]=[]<EOL><DEDENT>byExt[ext]=byExt[ext]+[fname]<EOL><DEDENT>return byExt<EOL>

given a list of files, return a dict organized by extension.


html="""<STR_LIT>"""%version.__version__<EOL>for parent in smartSort(self.fnamesByCell.keys()):<EOL><INDENT>html+='<STR_LIT>'%(parent,parent)<EOL>for child in self.fnamesByCell[parent]:<EOL><INDENT>fullpath=os.path.join(self.abfFolder,child)<EOL>protocol = swhlab.swh_abf.abfProtocol(fullpath)<EOL>html+='<STR_LIT>'%(fullpath,protocol)<EOL><DEDENT><DEDENT>style.save(html,self.abfFolder2+"<STR_LIT>")<EOL>return<EOL>


self.makePics() <EOL>html='<STR_LIT>'%os.path.basename(self.abfFolder)<EOL>for ID in smartSort(self.fnamesByCell.keys()):<EOL><INDENT>link='<STR_LIT>'<EOL>if ID+"<STR_LIT>" in self.fnames2:<EOL><INDENT>link='<STR_LIT>'%ID<EOL><DEDENT>html+=('<STR_LIT>'%(link,ID)) <EOL>if showChildren:<EOL><INDENT>for fname in self.fnamesByCell[ID]:<EOL><INDENT>thisID=os.path.splitext(fname)[<NUM_LIT:0>]<EOL>files2=[x for x in self.fnames2 if x.startswith(thisID) and not x.endswith("<STR_LIT>")]<EOL>html+='<STR_LIT>'%thisID <EOL>if len(files2):<EOL><INDENT>html+='<STR_LIT>'%len(files2) <EOL><DEDENT>html+='<STR_LIT>'<EOL><DEDENT>html+="<STR_LIT>"<EOL><DEDENT><DEDENT>style.save(html,self.abfFolder2+"<STR_LIT>")<EOL>self.html_index_splash() <EOL>style.frames(self.abfFolder2+"<STR_LIT>",launch=launch)<EOL>

generate list of cells with links. keep this simple.
automatically generates splash page and regnerates frames.


byCell={}<EOL>fnames=smartSort(fnames)<EOL>days = list(set([elem[:<NUM_LIT:5>] for elem in fnames if elem.endswith("<STR_LIT>")])) <EOL>for day in smartSort(days):<EOL><INDENT>parent=None<EOL>for i,fname in enumerate([elem for elem in fnames if elem.startswith(day) and elem.endswith("<STR_LIT>")]):<EOL><INDENT>ID=os.path.splitext(fname)[<NUM_LIT:0>]<EOL>if len([x for x in fnames if x.startswith(ID)])-<NUM_LIT:1>:<EOL><INDENT>parent=ID<EOL><DEDENT>if not parent in byCell:<EOL><INDENT>byCell[parent]=[]<EOL><DEDENT>byCell[parent]=byCell[parent]+[fname]<EOL><DEDENT><DEDENT>return byCell<EOL>

given files and cells, return a dict of files grouped by cell.


html=html_top+html+html_bot<EOL>html=html.replace("<STR_LIT>",swhlab.common.datetimeToString())<EOL>if fname is None:<EOL><INDENT>fname = tempfile.gettempdir()+"<STR_LIT>"<EOL>launch=True<EOL><DEDENT>fname=os.path.abspath(fname)<EOL>with open(fname,'<STR_LIT:w>') as f:<EOL><INDENT>f.write(html)<EOL><DEDENT>global stylesheetSaved<EOL>stylesheetPath=os.path.join(os.path.dirname(fname),"<STR_LIT>")<EOL>if not os.path.exists(stylesheetPath) or stylesheetSaved is False:<EOL><INDENT>with open(stylesheetPath,'<STR_LIT:w>') as f:<EOL><INDENT>f.write(stylesheet)<EOL>stylesheetSaved=True<EOL><DEDENT><DEDENT>if launch:<EOL><INDENT>webbrowser.open(fname)<EOL><DEDENT>

wrap HTML in a top and bottom (with css) and save to disk.


def kernel_gaussian(size=<NUM_LIT:100>, sigma=None, forwardOnly=False):

return a 1d gassuan array of a given size and sigma.
If sigma isn't given, it will be 1/10 of the size, which is usually good.
Note that this is fully numpy, and doesn't use scipy.


if timer is None:<EOL><INDENT>return time.time()<EOL><DEDENT>else:<EOL><INDENT>took=time.time()-timer<EOL>if took<<NUM_LIT:1>:<EOL><INDENT>return "<STR_LIT>"%(took*<NUM_LIT>)<EOL><DEDENT>elif took<<NUM_LIT>:<EOL><INDENT>return "<STR_LIT>"%(took)<EOL><DEDENT>else:<EOL><INDENT>return "<STR_LIT>"%(took/<NUM_LIT>)<EOL><DEDENT><DEDENT>

simple timer. returns a time object, or a string.


IDs=list(IDs)<EOL>monO=[]<EOL>monN=[]<EOL>monD=[]<EOL>good=[]<EOL>for ID in IDs:<EOL><INDENT>if ID is None:<EOL><INDENT>continue<EOL><DEDENT>if '<STR_LIT:o>' in ID:<EOL><INDENT>monO.append(ID)<EOL><DEDENT>elif '<STR_LIT:n>' in ID:<EOL><INDENT>monN.append(ID)<EOL><DEDENT>elif '<STR_LIT:d>' in ID:<EOL><INDENT>monD.append(ID)<EOL><DEDENT>else:<EOL><INDENT>good.append(ID)<EOL><DEDENT><DEDENT>return sorted(good)+sorted(monO)+sorted(monN)+sorted(monD)<EOL>

given a list of goofy ABF names, return it sorted intelligently.
This places things like 16o01001 after 16901001.


fname=userFolder()+"<STR_LIT>"<EOL>if os.path.exists(fname):<EOL><INDENT>abfFname=open(fname).read().strip()<EOL>if os.path.exists(abfFname) or abfFname.endswith("<STR_LIT>"):<EOL><INDENT>return abfFname<EOL><DEDENT><DEDENT>return os.path.abspath(os.sep)<EOL>


This applies a kernel to a signal through convolution and returns the result.

Some magic is done at the edges so the result doesn't apprach zero:
    1. extend the signal's edges with len(kernel)/2 duplicated values
    2. perform the convolution ('same' mode)
    3. slice-off the ends we added
    4. return the same number of points as the original


l=list(l)<EOL>if value in l:<EOL><INDENT>l.remove(value)<EOL>l.append(value)<EOL><DEDENT>return l<EOL>

if the value is in the list, move it to the back and return it.


if "<STR_LIT:.>" in fname:<EOL><INDENT>return os.path.splitext(fname)[<NUM_LIT:1>]<EOL><DEDENT>return fname<EOL>


halt everything until user input. Use this sparingly.


return [x.lower() for x in l if type(x) is str]<EOL>

given a list of strings, make them all lowercase.


fname=userFolder()+"<STR_LIT>"<EOL>with open(fname,'<STR_LIT:w>') as f:<EOL><INDENT>f.write(os.path.abspath(abfFname))<EOL><DEDENT>return<EOL>


def kernel_gaussian(self, sizeMS, sigmaMS=None, forwardOnly=False):

sigmaMS=sizeMS/<NUM_LIT:10> if sigmaMS is None else sigmaMS<EOL>size,sigma=sizeMS*self.pointsPerMs,sigmaMS*self.pointsPerMs<EOL>self.kernel=swhlab.common.kernel_gaussian(size,sigma,forwardOnly)<EOL>return self.kernel<EOL>


self.setsweep(sweep)<EOL>return list(self.protoX),list(self.protoY)<EOL>

given a sweep, return the protocol as [Xs,Ys].
This is good for plotting/recreating the protocol trace.
There may be duplicate numbers.


Recreate the command stimulus (protocol) for the current sweep.
It's not stored point by point (that's a waste of time and memory!)
Instead it's stored as a few (x,y) points which can be easily graphed.

TODO: THIS
for segment in abf.ABFreader.read_protocol():
    for analogsignal in segment.analogsignals:
        print(analogsignal)
        plt.plot(analogsignal)
        plt.show()
        plt.close('all')


fname=os.path.abspath(fname)<EOL>basename=os.path.basename(fname)<EOL>return os.path.splitext(basename)[<NUM_LIT:0>]<EOL>

given a filename, return the ABFs ID string.


assert self.kernel is not None<EOL>return swhlab.common.convolve(self.sweepY,self.kernel)<EOL>

Get the filtered sweepY of the current sweep.
Only works if self.kernel has been generated.


if sweepLast is None:<EOL><INDENT>sweepLast=self.sweeps-<NUM_LIT:1><EOL><DEDENT>nSweeps=sweepLast-sweepFirst+<NUM_LIT:1><EOL>runningSum=np.zeros(len(self.sweepY))<EOL>self.log.debug("<STR_LIT>",sweepFirst,sweepLast)<EOL>for sweep in np.arange(nSweeps)+sweepFirst:<EOL><INDENT>self.setsweep(sweep)<EOL>runningSum+=self.sweepY.flatten()<EOL><DEDENT>average=runningSum/nSweeps<EOL>return average<EOL>

Return a sweep which is the average of multiple sweeps.
For now, standard deviation is lost.


if setsweep:<EOL><INDENT>self.setsweep(setsweep)<EOL><DEDENT>if t2 is None or t2>self.sweepLength:<EOL><INDENT>t2=self.sweepLength<EOL>self.log.debug("<STR_LIT>",t2)<EOL><DEDENT>t1=max(t1,<NUM_LIT:0>)<EOL>if t1>t2:<EOL><INDENT>self.log.error("<STR_LIT>")<EOL>return False<EOL><DEDENT>I1,I2=int(t1*self.pointsPerSec),int(t2*self.pointsPerSec)<EOL>if I1==I2:<EOL><INDENT>return np.nan<EOL><DEDENT>return np.average(self.sweepY[I1:I2])<EOL>

return the average of part of the current sweep.


self.setsweep(sweep)<EOL>return list(self.protoSeqX),list(self.protoSeqY)<EOL>

given a sweep, return the protocol as condensed sequence.
This is better for comparing similarities and determining steps.
There should be no duplicate numbers.


f=open(fname,'<STR_LIT:rb>')<EOL>raw=f.read(<NUM_LIT:30>*<NUM_LIT:1000>) <EOL>f.close()<EOL>raw=raw.decode("<STR_LIT:utf-8>","<STR_LIT:ignore>")<EOL>raw=raw.split("<STR_LIT>")[<NUM_LIT:1>].split("<STR_LIT>")[<NUM_LIT:0>]<EOL>protocol = os.path.basename(raw) <EOL>protocolID = protocol.split("<STR_LIT:U+0020>")[<NUM_LIT:0>] <EOL>return protocolID<EOL>

Determine the protocol used to record an ABF file


return self.sweepY-self.sweepYfiltered()<EOL>

return the sweep with sweepYfiltered subtracted from it.


given the bytestring ABF header, make and launch HTML.


for sweep in range(self.sweeps):<EOL><INDENT>self.setsweep(sweep)<EOL>yield self.sweep<EOL><DEDENT>


Generate HTML containing information about NeoIO objects.
This is useful when trying to figure out how to extract data from ABFs.


experiment: generic VC time course experiment.


self.ensureDetection()<EOL>bySweepTimes=[[]]*self.abf.sweeps<EOL>for sweep in range(self.abf.sweeps):<EOL><INDENT>sweepTimes=[]<EOL>for ap in self.APs:<EOL><INDENT>if ap["<STR_LIT>"]==sweep:<EOL><INDENT>sweepTimes.append(ap["<STR_LIT>"])<EOL><DEDENT><DEDENT>bySweepTimes[sweep]=sweepTimes<EOL><DEDENT>bySweepFreqs=[[]]*self.abf.sweeps<EOL>for i,times in enumerate(bySweepTimes):<EOL><INDENT>if len(times)<<NUM_LIT:2>:<EOL><INDENT>continue<EOL><DEDENT>diffs=np.array(times[<NUM_LIT:1>:])-np.array(times[:-<NUM_LIT:1>])<EOL>bySweepFreqs[i]=np.array(<NUM_LIT:1>/diffs).tolist()<EOL><DEDENT>if feature == "<STR_LIT>":<EOL><INDENT>return bySweepFreqs<EOL><DEDENT>elif feature == "<STR_LIT>":<EOL><INDENT>result=np.zeros(self.abf.sweeps) <EOL>for i,freqs in enumerate(bySweepFreqs):<EOL><INDENT>if len(freqs):<EOL><INDENT>result[i]=freqs[<NUM_LIT:0>]<EOL><DEDENT><DEDENT>return result<EOL><DEDENT>elif feature == "<STR_LIT>":<EOL><INDENT>return bySweepTimes<EOL><DEDENT>elif feature == "<STR_LIT:count>":<EOL><INDENT>result=np.zeros(self.abf.sweeps) <EOL>for i,times in enumerate(bySweepTimes):<EOL><INDENT>result[i]=len(bySweepTimes[i])<EOL><DEDENT>return result<EOL><DEDENT>elif feature == "<STR_LIT>":<EOL><INDENT>result=np.zeros(self.abf.sweeps) <EOL>for i,freqs in enumerate(bySweepFreqs):<EOL><INDENT>if len(freqs):<EOL><INDENT>result[i]=np.nanmean(freqs)<EOL><DEDENT><DEDENT>return result<EOL><DEDENT>elif feature == "<STR_LIT>":<EOL><INDENT>result=np.zeros(self.abf.sweeps) <EOL>for i,freqs in enumerate(bySweepFreqs):<EOL><INDENT>if len(freqs):<EOL><INDENT>result[i]=np.nanmedian(freqs)<EOL><DEDENT><DEDENT>return result<EOL><DEDENT>else:<EOL><INDENT>self.log.error("<STR_LIT>",feature)<EOL>return None<EOL><DEDENT>

returns AP info by sweep arranged as a list (by sweep).

feature:
    * "freqs" - list of instantaneous frequencies by sweep.
    * "firsts" - list of first instantaneous frequency by sweep.
    * "times" - list of times of each AP in the sweep.
    * "count" - numer of APs per sweep.
    * "average" - average instanteous frequency per sweep.
    * "median" - median instanteous frequency per sweep.


self.ensureDetection()<EOL>times=[]<EOL>for ap in self.APs:<EOL><INDENT>times.append(ap["<STR_LIT:T>"])<EOL><DEDENT>return np.array(sorted(times))<EOL>

return an array of times (in sec) of all APs.


self.log = logging.getLogger("<STR_LIT>")<EOL>self.log.setLevel(swhlab.loglevel)<EOL>if abf in [None,False,'<STR_LIT>']:<EOL><INDENT>self.log.error("<STR_LIT>",str(abf))<EOL>return<EOL><DEDENT>if type(abf) is str:<EOL><INDENT>self.log.debug("<STR_LIT>")<EOL>abf=ABF(abf)<EOL><DEDENT>self.abf=abf<EOL>self.detect_over = <NUM_LIT:50> <EOL>self.detect_time1 = <NUM_LIT:0> <EOL>self.detect_time2 = abf.sweepLength <EOL>self.APs=False<EOL>

Load an ABF and get ready to do AP detection.
After detect(), all AP data is stored as a list of dicts in AP.APs


if self.APs is False: <EOL><INDENT>self.APs=[] <EOL><DEDENT>for i,ap in enumerate(self.APs):<EOL><INDENT>if ap["<STR_LIT>"]==sweep:<EOL><INDENT>self.APs[i]=None<EOL><DEDENT><DEDENT>if self.APs.count(None):<EOL><INDENT>self.log.debug("<STR_LIT>",self.APs.count(None))<EOL>while None in self.APs:<EOL><INDENT>self.APs.remove(None)<EOL><DEDENT><DEDENT>self.log.debug("<STR_LIT>",len(self.APs))<EOL>self.abf.derivative=True<EOL>self.abf.setsweep(sweep)<EOL>Is = cm.where_cross(self.abf.sweepD,self.detect_over)<EOL>self.log.debug("<STR_LIT>"%len(Is))<EOL>for i,I in enumerate(Is):<EOL><INDENT>if np.min(self.abf.sweepD[I:I+<NUM_LIT:2>*self.abf.pointsPerMs])>-<NUM_LIT:10>:<EOL><INDENT>Is[i]=<NUM_LIT:0><EOL><DEDENT><DEDENT>Is=Is[np.nonzero(Is)]<EOL>self.log.debug("<STR_LIT>"%len(Is))<EOL>for i,I in enumerate(Is):<EOL><INDENT>stepBack=<NUM_LIT:0><EOL>while(self.abf.sweepD[I-stepBack])><NUM_LIT:10> and stepBack/self.abf.pointsPerMs<<NUM_LIT:1>: <EOL><INDENT>stepBack+=<NUM_LIT:1><EOL><DEDENT>Is[i]-=stepBack<EOL><DEDENT>sweepAPs=[]<EOL>for i,I in enumerate(Is):<EOL><INDENT>try:<EOL><INDENT>timeInSweep=I/self.abf.pointsPerSec<EOL>if timeInSweep<self.detect_time1 or timeInSweep>self.detect_time2:<EOL><INDENT>continue <EOL><DEDENT>ap={} <EOL>ap["<STR_LIT>"]=sweep <EOL>ap["<STR_LIT:I>"]=I <EOL>ap["<STR_LIT>"]=I/self.abf.pointsPerSec <EOL>ap["<STR_LIT:T>"]=ap["<STR_LIT>"]+self.abf.sweepInterval*sweep <EOL>ap["<STR_LIT>"]=self.abf.sweepY[I] <EOL>chunk=self.abf.sweepD[I:I+<NUM_LIT:5>*self.abf.pointsPerMs] <EOL>I_toNegTen=np.where(chunk<-<NUM_LIT:10>)[<NUM_LIT:0>][<NUM_LIT:0>]<EOL>chunk=self.abf.sweepD[I+I_toNegTen:I+I_toNegTen+<NUM_LIT:10>*self.abf.pointsPerMs] <EOL>if not max(chunk)>-<NUM_LIT:10>:<EOL><INDENT>self.log.debug("<STR_LIT>"%ap["<STR_LIT:T>"])<EOL>self.log.error("<STR_LIT>")<EOL>continue <EOL><DEDENT>I_recover=np.where(chunk>-<NUM_LIT:10>)[<NUM_LIT:0>][<NUM_LIT:0>]+I_toNegTen+I <EOL>ap["<STR_LIT>"]=[I,I_recover] <EOL>ap["<STR_LIT>"]=(I_recover-I)/self.abf.pointsPerMs <EOL>chunk=self.abf.sweepD[ap["<STR_LIT>"][<NUM_LIT:0>]:ap["<STR_LIT>"][<NUM_LIT:1>]]<EOL>ap["<STR_LIT>"]=np.max(chunk)<EOL>ap["<STR_LIT>"]=np.where(chunk==ap["<STR_LIT>"])[<NUM_LIT:0>][<NUM_LIT:0>]+I<EOL>ap["<STR_LIT>"]=np.min(chunk)<EOL>ap["<STR_LIT>"]=np.where(chunk==ap["<STR_LIT>"])[<NUM_LIT:0>][<NUM_LIT:0>]+I<EOL>if ap["<STR_LIT>"]<<NUM_LIT:10> or ap["<STR_LIT>"]>-<NUM_LIT:10>:<EOL><INDENT>self.log.debug("<STR_LIT>")<EOL>self.log.error("<STR_LIT>")<EOL>continue<EOL><DEDENT>chunkSize=self.abf.pointsPerMs*<NUM_LIT:10> <EOL>if len(Is)-<NUM_LIT:1>>i and Is[i+<NUM_LIT:1>]<(I+chunkSize): <EOL><INDENT>chunkSize=Is[i+<NUM_LIT:1>]-I <EOL><DEDENT>if chunkSize<(self.abf.pointsPerMs*<NUM_LIT:2>):<EOL><INDENT>continue <EOL><DEDENT>ap["<STR_LIT>"]=[I,I+chunkSize] <EOL>chunk=self.abf.sweepY[I:I+chunkSize]<EOL>ap["<STR_LIT>"]=np.max(chunk)<EOL>ap["<STR_LIT>"]=np.where(chunk==ap["<STR_LIT>"])[<NUM_LIT:0>][<NUM_LIT:0>]+I<EOL>chunkForMin=np.copy(chunk) <EOL>chunkForMin[:ap["<STR_LIT>"]-I]=np.inf <EOL>ap["<STR_LIT>"]=np.min(chunkForMin) <EOL>ap["<STR_LIT>"]=np.where(chunkForMin==ap["<STR_LIT>"])[<NUM_LIT:0>][<NUM_LIT:0>]+I<EOL>if ap["<STR_LIT>"]<ap["<STR_LIT>"]:<EOL><INDENT>self.log.error("<STR_LIT>")<EOL>self.log.error("<STR_LIT>") <EOL>self.log.error("<STR_LIT>")<EOL><DEDENT>if (len(chunk))-((I+len(chunk))-ap["<STR_LIT>"])<<NUM_LIT:10>:<EOL><INDENT>self.log.error("<STR_LIT>")<EOL>self.log.error("<STR_LIT>")<EOL>self.log.error("<STR_LIT>")<EOL><DEDENT>ap["<STR_LIT>"]=(ap["<STR_LIT>"]-I)/self.abf.pointsPerMs <EOL>ap["<STR_LIT>"]=(ap["<STR_LIT>"]-ap["<STR_LIT>"])/self.abf.pointsPerMs <EOL>ap["<STR_LIT>"]=np.average([ap["<STR_LIT>"],ap["<STR_LIT>"]]) <EOL>ap["<STR_LIT>"]=cm.where_cross(chunk,ap["<STR_LIT>"])[<NUM_LIT:0>]+I <EOL>ap["<STR_LIT>"]=cm.where_cross(-chunk,-ap["<STR_LIT>"])[<NUM_LIT:1>]+I <EOL>ap["<STR_LIT>"]=(ap["<STR_LIT>"]-ap["<STR_LIT>"])/self.abf.pointsPerMs <EOL>sweepAPs.extend([ap])<EOL><DEDENT>except Exception as e:<EOL><INDENT>self.log.error("<STR_LIT>",i,len(Is))<EOL>self.log.error(cm.exceptionToString(e))<EOL><DEDENT><DEDENT>self.log.debug("<STR_LIT>",len(sweepAPs))<EOL>self.APs.extend(sweepAPs)<EOL>self.abf.derivative=False<EOL>


if self.APs==False:<EOL><INDENT>self.log.debug("<STR_LIT>")<EOL>self.detect()<EOL><DEDENT>

run this before analysis. Checks if event detection occured.
If not, runs AP detection on all sweeps.