htimes, hlabels = hierarchy_flatten(annotation)<EOL>htimes = [np.asarray(_) for _ in htimes]<EOL>return mir_eval.display.hierarchy(htimes, hlabels, **kwargs)<EOL>

Plotting wrapper for hierarchical segmentations


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>


times, values = annotation.to_interval_values()<EOL>if any(values):<EOL><INDENT>labels = values<EOL><DEDENT>else:<EOL><INDENT>labels = None<EOL><DEDENT>return mir_eval.display.events(times, labels=labels, **kwargs)<EOL>


times, values = annotation.to_interval_values()<EOL>labels = [_['<STR_LIT>'] for _ in values]<EOL>return mir_eval.display.events(times, labels=labels, **kwargs)<EOL>


times, midi = annotation.to_interval_values()<EOL>return mir_eval.display.piano_roll(times, midi=midi, **kwargs)<EOL>


def display(annotation, meta=True, **kwargs):

for namespace, func in six.iteritems(VIZ_MAPPING):<EOL><INDENT>try:<EOL><INDENT>ann = coerce_annotation(annotation, namespace)<EOL>axes = func(ann, **kwargs)<EOL>axes.set_title(annotation.namespace)<EOL>if meta:<EOL><INDENT>description = pprint_jobject(annotation.annotation_metadata, indent=<NUM_LIT:2>)<EOL>anchored_box = AnchoredText(description.strip('<STR_LIT:\n>'),<EOL>loc=<NUM_LIT:2>,<EOL>frameon=True,<EOL>bbox_to_anchor=(<NUM_LIT>, <NUM_LIT:1.0>),<EOL>bbox_transform=axes.transAxes,<EOL>borderpad=<NUM_LIT:0.0>)<EOL>axes.add_artist(anchored_box)<EOL>axes.figure.subplots_adjust(right=<NUM_LIT>)<EOL><DEDENT>return axes<EOL><DEDENT>except NamespaceError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>raise NamespaceError('<STR_LIT>'<EOL>.format(annotation.namespace))<EOL>

Visualize a jams annotation through mir_eval

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

    meta : bool
        If `True`, include annotation metadata in the figure

    kwargs
        Additional keyword arguments to mir_eval.display functions

    Returns
    -------
    ax
        Axis handles for the new display

    Raises
    ------
    NamespaceError
        If the annotation cannot be visualized


def display_multi(annotations, fig_kw=None, meta=True, **kwargs):

if fig_kw is None:<EOL><INDENT>fig_kw = dict()<EOL><DEDENT>fig_kw.setdefault('<STR_LIT>', True)<EOL>fig_kw.setdefault('<STR_LIT>', True)<EOL>display_annotations = []<EOL>for ann in annotations:<EOL><INDENT>for namespace in VIZ_MAPPING:<EOL><INDENT>if can_convert(ann, namespace):<EOL><INDENT>display_annotations.append(ann)<EOL>break<EOL><DEDENT><DEDENT><DEDENT>if not len(display_annotations):<EOL><INDENT>raise ParameterError('<STR_LIT>')<EOL><DEDENT>fig, axs = plt.subplots(nrows=len(display_annotations), ncols=<NUM_LIT:1>, **fig_kw)<EOL>if len(display_annotations) == <NUM_LIT:1>:<EOL><INDENT>axs = [axs]<EOL><DEDENT>for ann, ax in zip(display_annotations, axs):<EOL><INDENT>kwargs['<STR_LIT>'] = ax<EOL>display(ann, meta=meta, **kwargs)<EOL><DEDENT>return fig, axs<EOL>

Display multiple annotations with shared axes

    Parameters
    ----------
    annotations : jams.AnnotationArray
        A collection of annotations to display

    fig_kw : dict
        Keyword arguments to `plt.figure`

    meta : bool
        If `True`, display annotation metadata for each annotation

    kwargs
        Additional keyword arguments to the `mir_eval.display` routines

    Returns
    -------
    fig
        The created figure
    axs
        List of subplot axes corresponding to each displayed annotation


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_times, _ = ref.to_event_values()<EOL>est_times, _ = est.to_event_values()<EOL>return mir_eval.onset.evaluate(ref_times, est_times, **kwargs)<EOL>

r'''Onset 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.onset.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='onset')[0]
    >>> est_ann = est_jam.search(namespace='onset')[0]
    >>> scores = jams.eval.onset(ref_ann, est_ann)


namespace = '<STR_LIT>'<EOL>ref = coerce_annotation(ref, namespace)<EOL>est = coerce_annotation(est, namespace)<EOL>ref_interval, ref_value = ref.to_interval_values()<EOL>est_interval, est_value = est.to_interval_values()<EOL>return mir_eval.chord.evaluate(ref_interval, ref_value,<EOL>est_interval, est_value, **kwargs)<EOL>

r'''Chord 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.chord.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='chord')[0]
    >>> est_ann = est_jam.search(namespace='chord')[0]
    >>> scores = jams.eval.chord(ref_ann, est_ann)


namespace = '<STR_LIT>'<EOL>ref = coerce_annotation(ref, namespace)<EOL>est = coerce_annotation(est, namespace)<EOL>ref_interval, ref_value = ref.to_interval_values()<EOL>est_interval, est_value = est.to_interval_values()<EOL>return mir_eval.segment.evaluate(ref_interval, ref_value,<EOL>est_interval, est_value, **kwargs)<EOL>

r'''Segment 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.segment.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='segment_.*')[0]
    >>> est_ann = est_jam.search(namespace='segment_.*')[0]
    >>> scores = jams.eval.segment(ref_ann, est_ann)


intervals, values = annotation.to_interval_values()<EOL>ordering = dict()<EOL>for interval, value in zip(intervals, values):<EOL><INDENT>level = value['<STR_LIT>']<EOL>if level not in ordering:<EOL><INDENT>ordering[level] = dict(intervals=list(), labels=list())<EOL><DEDENT>ordering[level]['<STR_LIT>'].append(interval)<EOL>ordering[level]['<STR_LIT>'].append(value['<STR_LIT:label>'])<EOL><DEDENT>levels = sorted(list(ordering.keys()))<EOL>hier_intervals = [ordering[level]['<STR_LIT>'] for level in levels]<EOL>hier_labels = [ordering[level]['<STR_LIT>'] for level in levels]<EOL>return hier_intervals, hier_labels<EOL>

Flatten a multi_segment annotation into mir_eval style.

    Parameters
    ----------
    annotation : jams.Annotation
        An annotation in the `multi_segment` namespace

    Returns
    -------
    hier_intervalss : list
        A list of lists of intervals, ordered by increasing specificity.

    hier_labels : list
        A list of lists of labels, ordered by increasing specificity.


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)


namespace = '<STR_LIT>'<EOL>ref = coerce_annotation(ref, namespace)<EOL>est = coerce_annotation(est, namespace)<EOL>ref_times, ref_p = ref.to_event_values()<EOL>est_times, est_p = est.to_event_values()<EOL>ref_freq = np.asarray([p['<STR_LIT>'] * (-<NUM_LIT:1>)**(~p['<STR_LIT>']) for p in ref_p])<EOL>est_freq = np.asarray([p['<STR_LIT>'] * (-<NUM_LIT:1>)**(~p['<STR_LIT>']) for p in est_p])<EOL>return mir_eval.melody.evaluate(ref_times, ref_freq,<EOL>est_times, est_freq,<EOL>**kwargs)<EOL>

r'''Melody extraction 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.melody.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='pitch_contour')[0]
    >>> est_ann = est_jam.search(namespace='pitch_contour')[0]
    >>> scores = jams.eval.melody(ref_ann, est_ann)


<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_patterns = pattern_to_mireval(ref)<EOL>est_patterns = pattern_to_mireval(est)<EOL>return mir_eval.pattern.evaluate(ref_patterns, est_patterns, **kwargs)<EOL>

r'''Pattern detection 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.pattern.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='pattern_jku')[0]
    >>> est_ann = est_jam.search(namespace='pattern_jku')[0]
    >>> scores = jams.eval.pattern(ref_ann, est_ann)


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)


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


<EOL>annotation.validate(strict=True)<EOL>if annotation.namespace == target_namespace:<EOL><INDENT>return annotation<EOL><DEDENT>if target_namespace in __CONVERSION__:<EOL><INDENT>annotation = deepcopy(annotation)<EOL>for source in __CONVERSION__[target_namespace]:<EOL><INDENT>if annotation.search(namespace=source):<EOL><INDENT>return __CONVERSION__[target_namespace][source](annotation)<EOL><DEDENT><DEDENT><DEDENT>raise NamespaceError('<STR_LIT>'<EOL>'<STR_LIT>'.format(annotation.namespace,<EOL>target_namespace))<EOL>

Convert a given annotation to the target namespace.

    Parameters
    ----------
    annotation : jams.Annotation
        An annotation object

    target_namespace : str
        The target namespace

    Returns
    -------
    mapped_annotation : jams.Annotation
        if `annotation` already belongs to `target_namespace`, then
        it is returned directly.

        otherwise, `annotation` is copied and automatically converted
        to the target namespace.

    Raises
    ------
    SchemaError
        if the input annotation fails to validate

    NamespaceError
        if no conversion is possible

    Examples
    --------
    Convert frequency measurements in Hz to MIDI

    >>> ann_midi = jams.convert(ann_hz, 'note_midi')

    And back to Hz

    >>> ann_hz2 = jams.convert(ann_midi, 'note_hz')


<EOL>if annotation.namespace == target_namespace:<EOL><INDENT>return True<EOL><DEDENT>if target_namespace in __CONVERSION__:<EOL><INDENT>for source in __CONVERSION__[target_namespace]:<EOL><INDENT>if annotation.search(namespace=source):<EOL><INDENT>return True<EOL><DEDENT><DEDENT><DEDENT>return False<EOL>

Test if an annotation can be mapped to a target namespace

    Parameters
    ----------
    annotation : jams.Annotation
        An annotation object

    target_namespace : str
        The target namespace

    Returns
    -------
    True
        if `annotation` can be automatically converted to
        `target_namespace`

    False
        otherwise


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


annotation = pitch_midi_to_hz(annotation)<EOL>return pitch_hz_to_contour(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=<NUM_LIT> * (<NUM_LIT>**((obs.value - <NUM_LIT>)/<NUM_LIT>)))<EOL><DEDENT>return annotation<EOL>

Convert a pitch_midi annotation to pitch_hz


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


Convert any tag annotation 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, value=obs.value['<STR_LIT:label>'])<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=obs.value['<STR_LIT>'])<EOL><DEDENT>return annotation<EOL>


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.


@contextlib.contextmanager<EOL>def _open(name_or_fdesc, mode='<STR_LIT:r>', fmt='<STR_LIT>'):

open_map = {'<STR_LIT>': open,<EOL>'<STR_LIT>': open,<EOL>'<STR_LIT>': gzip.open,<EOL>'<STR_LIT>': gzip.open}<EOL>if hasattr(name_or_fdesc, '<STR_LIT>') or hasattr(name_or_fdesc, '<STR_LIT>'):<EOL><INDENT>yield name_or_fdesc<EOL><DEDENT>elif isinstance(name_or_fdesc, six.string_types):<EOL><INDENT>if fmt == '<STR_LIT>':<EOL><INDENT>_, ext = os.path.splitext(name_or_fdesc)<EOL>ext = ext[<NUM_LIT:1>:]<EOL><DEDENT>else:<EOL><INDENT>ext = fmt<EOL><DEDENT>try:<EOL><INDENT>ext = ext.lower()<EOL>if ext in ['<STR_LIT>', '<STR_LIT>'] and '<STR_LIT:t>' not in mode:<EOL><INDENT>mode = '<STR_LIT>'.format(mode)<EOL><DEDENT>with open_map[ext](name_or_fdesc, mode=mode) as fdesc:<EOL><INDENT>yield fdesc<EOL><DEDENT><DEDENT>except KeyError:<EOL><INDENT>raise ParameterError('<STR_LIT>'<EOL>'<STR_LIT>'.format(ext))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise ParameterError('<STR_LIT>'<EOL>'<STR_LIT>'.format(name_or_fdesc))<EOL><DEDENT>

An intelligent wrapper for ``open``.

    Parameters
    ----------
    name_or_fdesc : string-type or open file descriptor
        If a string type, refers to the path to a file on disk.

        If an open file descriptor, it is returned as-is.

    mode : string
        The mode with which to open the file.
        See ``open`` for details.

    fmt : string ['auto', 'jams', 'json', 'jamz']
        The encoding for the input/output stream.

        If `auto`, the format is inferred from the filename extension.

        Otherwise, use the specified coding.

    See Also
    --------
    open
    gzip.open


def load(path_or_file, validate=True, strict=True, fmt='<STR_LIT>'):

with _open(path_or_file, mode='<STR_LIT:r>', fmt=fmt) as fdesc:<EOL><INDENT>jam = JAMS(**json.load(fdesc))<EOL><DEDENT>if validate:<EOL><INDENT>jam.validate(strict=strict)<EOL><DEDENT>return jam<EOL>

r"""Load a JAMS Annotation from a file.

    Parameters
    ----------
    path_or_file : str or file-like
        Path to the JAMS file to load
        OR
        An open file handle to load from.

    validate : bool
        Attempt to validate the JAMS object

    strict : bool
        if `validate == True`, enforce strict schema validation

    fmt : str ['auto', 'jams', 'jamz']
        The encoding format of the input

        If `auto`, encoding is inferred from the file name.

        If the input is an open file handle, `jams` encoding
        is used.

    Returns
    -------
    jam : JAMS
        The loaded JAMS object

    Raises
    ------
    SchemaError
        if `validate == True`, `strict==True`, and validation fails

    See also
    --------
    JAMS.validate
    JAMS.save

    Examples
    --------
    >>> # Load a jams object from a file name
    >>> J = jams.load('data.jams')
    >>> # Or from an open file descriptor
    >>> with open('data.jams', 'r') as fdesc:
    ...     J = jams.load(fdesc)
    >>> # Non-strict validation
    >>> J = jams.load('data.jams', strict=False)
    >>> # No validation at all
    >>> J = jams.load('data.jams', validate=False)


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'


if six.callable(query):<EOL><INDENT>return query(string)<EOL><DEDENT>elif (isinstance(query, six.string_types) and<EOL>isinstance(string, six.string_types)):<EOL><INDENT>return re.match(query, string) is not None<EOL><DEDENT>else:<EOL><INDENT>return query == string<EOL><DEDENT>

Test if a string matches a query.

    Parameters
    ----------
    string : str
        The string to test

    query : string, callable, or object
        Either a regular expression, callable function, or object.

    Returns
    -------
    match : bool
        `True` if:
        - `query` is a callable and `query(string) == True`
        - `query` is a regular expression and `re.match(query, string)`
        - or `string == query` for any other query

        `False` otherwise


if isinstance(obj, np.integer):<EOL><INDENT>return int(obj)<EOL><DEDENT>elif isinstance(obj, np.floating):<EOL><INDENT>return float(obj)<EOL><DEDENT>elif isinstance(obj, np.ndarray):<EOL><INDENT>return obj.tolist()<EOL><DEDENT>elif isinstance(obj, list):<EOL><INDENT>return [serialize_obj(x) for x in obj]<EOL><DEDENT>elif isinstance(obj, Observation):<EOL><INDENT>return {k: serialize_obj(v) for k, v in six.iteritems(obj._asdict())}<EOL><DEDENT>return obj<EOL>

Custom serialization functionality for working with advanced data types.

    - numpy arrays are converted to lists
    - lists are recursively serialized element-wise


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)`.


global __DIVID_COUNT__<EOL>__DIVID_COUNT__ += <NUM_LIT:1><EOL>return '<STR_LIT>'.format(id(obj), __DIVID_COUNT__)<EOL>

Static function to get a unique id for an object.
    This is used in HTML rendering to ensure unique div ids for each call
    to display an object


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}


return schema.JAMS_SCHEMA['<STR_LIT>'].get(self.type, None)<EOL>

The schema definition for this JObject, if it exists.

        Returns
        -------
        schema : dict or None


filtered_dict = dict()<EOL>for k, item in six.iteritems(self.__dict__):<EOL><INDENT>if k.startswith('<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.


return cls(**kwargs)<EOL>

Initialize the object from a dictionary of values


return self.__dict__[key]<EOL>


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 sorted([(k, k) for k in self.__dict__])<EOL>

Returns a list of tuples (key, display_name)
        for properties of this object


return json.dumps(self.__json__, **kwargs)<EOL>

Serialize the JObject to a string.

        Parameters
        ----------
        kwargs
            Keyword arguments to json.dumps

        Returns
        -------
        object_str : str
            Serialized JObject

        See Also
        --------
        json.dumps
        loads

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


return self.__dict__.keys()<EOL>

Return a list of the attributes of the object.

        Returns
        -------
        keys : list
            The attributes of the object

        Examples
        --------
        >>> J = jams.JObject(foo=5, bar='baz')
        >>> J.keys()
        ['foo', 'bar']


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"}'


return self.__class__.__name__<EOL>

The type (class name) of a derived JObject type


return cls.__json_init__(**json.loads(string))<EOL>

De-serialize a JObject

        Parameters
        ----------
        string : str
            A serialized (JSON string) JObject

        Returns
        -------
        J : JObject
            The input string reconstructed as a JObject

        See Also
        --------
        json.loads
        dumps

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


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


valid = True<EOL>try:<EOL><INDENT>jsonschema.validate(self.__json__, self.__schema__)<EOL><DEDENT>except jsonschema.ValidationError as invalid:<EOL><INDENT>if strict:<EOL><INDENT>raise SchemaError(str(invalid))<EOL><DEDENT>else:<EOL><INDENT>warnings.warn(str(invalid))<EOL><DEDENT>valid = False<EOL><DEDENT>return valid<EOL>

Validate a JObject against its schema

        Parameters
        ----------
        strict : bool
            Enforce strict schema validation

        Returns
        -------
        valid : bool
            True if the jam validates
            False if not, and `strict==False`

        Raises
        ------
        SchemaError
            If `strict==True` and `jam` fails validation


def __init__(self, namespace, data=None, annotation_metadata=None,<EOL>sandbox=None, time=<NUM_LIT:0>, duration=None):

super(Annotation, self).__init__()<EOL>if annotation_metadata is None:<EOL><INDENT>annotation_metadata = AnnotationMetadata()<EOL><DEDENT>self.annotation_metadata = AnnotationMetadata(**annotation_metadata)<EOL>self.namespace = namespace<EOL>self.data = SortedKeyList(key=self._key)<EOL>if data is not None:<EOL><INDENT>if isinstance(data, dict):<EOL><INDENT>self.append_columns(data)<EOL><DEDENT>else:<EOL><INDENT>self.append_records(data)<EOL><DEDENT><DEDENT>if sandbox is None:<EOL><INDENT>sandbox = Sandbox()<EOL><DEDENT>self.sandbox = Sandbox(**sandbox)<EOL>self.time = time<EOL>self.duration = duration<EOL>

Create an Annotation.

        Note that, if an argument is None, an empty Annotation is created in
        its place. Additionally, a dictionary matching the expected structure
        of the arguments will be parsed (i.e. instantiating from JSON).

        Parameters
        ----------
        namespace : str
            The namespace for this annotation

        data : dict of lists, list of dicts, or list of Observations
            Data for the new annotation

        annotation_metadata : AnnotationMetadata (or dict), default=None.
            Metadata corresponding to this Annotation.

        sandbox : Sandbox (dict), default=None
            Miscellaneous information; keep to native datatypes if possible.

        time : non-negative number
            The starting time for this annotation

        duration : non-negative number
            The duration of this annotation


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#')


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.


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.


<EOL>ann_schema = schema.namespace_array(self.namespace)<EOL>valid = True<EOL>try:<EOL><INDENT>jsonschema.validate(self.__json_light__(data=False),<EOL>schema.JAMS_SCHEMA)<EOL>data_ser = [serialize_obj(obs) for obs in self.data]<EOL>jsonschema.validate(data_ser, ann_schema)<EOL><DEDENT>except jsonschema.ValidationError as invalid:<EOL><INDENT>if strict:<EOL><INDENT>raise SchemaError(str(invalid))<EOL><DEDENT>else:<EOL><INDENT>warnings.warn(str(invalid))<EOL><DEDENT>valid = False<EOL><DEDENT>return valid<EOL>

Validate this annotation object against the JAMS schema,
        and its data against the namespace schema.

        Parameters
        ----------
        strict : bool
            If `True`, then schema violations will cause an Exception.
            If `False`, then schema violations will issue a warning.

        Returns
        -------
        valid : bool
            `True` if the object conforms to schema.
            `False` if the object fails to conform to schema,
            but `strict == False`.

        Raises
        ------
        SchemaError
            If `strict == True` and the object fails validation

        See Also
        --------
        JObject.validate


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

<EOL>if end_time <= start_time:<EOL><INDENT>raise ParameterError(<EOL>'<STR_LIT>')<EOL><DEDENT>if self.duration is None:<EOL><INDENT>orig_time = start_time<EOL>orig_duration = end_time - start_time<EOL>warnings.warn(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>orig_time = self.time<EOL>orig_duration = self.duration<EOL><DEDENT>if start_time > (orig_time + orig_duration) or (end_time < orig_time):<EOL><INDENT>warnings.warn(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL>trim_start = self.time<EOL>trim_end = trim_start<EOL><DEDENT>else:<EOL><INDENT>trim_start = max(orig_time, start_time)<EOL>trim_end = min(orig_time + orig_duration, end_time)<EOL><DEDENT>ann_trimmed = Annotation(<EOL>self.namespace,<EOL>data=None,<EOL>annotation_metadata=self.annotation_metadata,<EOL>sandbox=self.sandbox,<EOL>time=trim_start,<EOL>duration=trim_end - trim_start)<EOL>for obs in self.data:<EOL><INDENT>obs_start = obs.time<EOL>obs_end = obs_start + obs.duration<EOL>if obs_start < trim_end and obs_end > trim_start:<EOL><INDENT>new_start = max(obs_start, trim_start)<EOL>new_end = min(obs_end, trim_end)<EOL>new_duration = new_end - new_start<EOL>if ((not strict) or<EOL>(new_start == obs_start and new_end == obs_end)):<EOL><INDENT>ann_trimmed.append(time=new_start,<EOL>duration=new_duration,<EOL>value=obs.value,<EOL>confidence=obs.confidence)<EOL><DEDENT><DEDENT><DEDENT>if '<STR_LIT>' not in ann_trimmed.sandbox.keys():<EOL><INDENT>ann_trimmed.sandbox.update(<EOL>trim=[{'<STR_LIT>': start_time, '<STR_LIT>': end_time,<EOL>'<STR_LIT>': trim_start, '<STR_LIT>': trim_end}])<EOL><DEDENT>else:<EOL><INDENT>ann_trimmed.sandbox.trim.append(<EOL>{'<STR_LIT>': start_time, '<STR_LIT>': end_time,<EOL>'<STR_LIT>': trim_start, '<STR_LIT>': trim_end})<EOL><DEDENT>return ann_trimmed<EOL>

Trim the annotation and return as a new `Annotation` object.

Trimming will result in the new annotation only containing observations
that occur in the intersection of the time range spanned by the
annotation and the time range specified by the user. The new annotation
will span the time range ``[trim_start, trim_end]`` where
``trim_start = max(self.time, start_time)`` and ``trim_end =
min(self.time + self.duration, end_time)``.

If ``strict=False`` (default) observations that start before
``trim_start`` and end after it will be trimmed such that they start at
``trim_start``, and similarly observations that start before
``trim_end`` and end after it will be trimmed to end at ``trim_end``.
If ``strict=True`` such borderline observations will be discarded.

The new duration of the annotation will be ``trim_end - trim_start``.

Note that if the range defined by ``[start_time, end_time]``
doesn't intersect with the original time range spanned by the
annotation the resulting annotation will contain no observations, will
have the same start time as the original annotation and have duration
0.

This function also copies over all the annotation metadata from the
original annotation and documents the trim operation by adding a list
of tuples to the annotation's sandbox keyed by
``Annotation.sandbox.trim`` which documents each trim operation with a
tuple ``(start_time, end_time, trim_start, trim_end)``.

Parameters
----------
start_time : float
    The desired start time for the trimmed annotation in seconds.
end_time
    The desired end time for the trimmed annotation in seconds. Must be
    greater than ``start_time``.
strict : bool
    When ``False`` (default) observations that lie at the boundaries of
    the trimming range (given by ``[trim_start, trim_end]`` as
    described above), i.e. observations that start before and end after
    either the trim start or end time, 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 trimmed annotation.

Returns
-------
ann_trimmed : Annotation
    The trimmed annotation, returned as a new jams.Annotation object.
    If the trim range specified by ``[start_time, end_time]`` does not
    intersect at all with the original time range of the annotation a
    warning will be issued and the returned annotation will be empty.

Raises
------
ParameterError
    If ``end_time`` is not greater than ``start_time``.

Examples
--------
>>> ann = jams.Annotation(namespace='tag_open', time=2, duration=8)
>>> ann.append(time=2, duration=2, value='one')
>>> ann.append(time=4, duration=2, value='two')
>>> ann.append(time=6, duration=2, value='three')
>>> ann.append(time=7, duration=2, value='four')
>>> ann.append(time=8, duration=2, value='five')
>>> ann_trim = ann.trim(5, 8, strict=False)
>>> print(ann_trim.time, ann_trim.duration)
(5, 3)
>>> ann_trim.to_dataframe()
   time  duration  value confidence
0     5         1    two       None
1     6         2  three       None
2     7         1   four       None
>>> ann_trim_strict = ann.trim(5, 8, strict=True)
>>> print(ann_trim_strict.time, ann_trim_strict.duration)
(5, 3)
>>> ann_trim_strict.to_dataframe()
   time  duration  value confidence
0     6         2  three       None


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

<EOL>sliced_ann = self.trim(start_time, end_time, strict=strict)<EOL>raw_data = sliced_ann.pop_data()<EOL>for obs in raw_data:<EOL><INDENT>new_time = max(<NUM_LIT:0>, obs.time - start_time)<EOL>sliced_ann.append(time=new_time,<EOL>duration=obs.duration,<EOL>value=obs.value,<EOL>confidence=obs.confidence)<EOL><DEDENT>ref_time = sliced_ann.time<EOL>slice_start = ref_time<EOL>slice_end = ref_time + sliced_ann.duration<EOL>if '<STR_LIT>' not in sliced_ann.sandbox.keys():<EOL><INDENT>sliced_ann.sandbox.update(<EOL>slice=[{'<STR_LIT>': start_time, '<STR_LIT>': end_time,<EOL>'<STR_LIT>': slice_start, '<STR_LIT>': slice_end}])<EOL><DEDENT>else:<EOL><INDENT>sliced_ann.sandbox.slice.append(<EOL>{'<STR_LIT>': start_time, '<STR_LIT>': end_time,<EOL>'<STR_LIT>': slice_start, '<STR_LIT>': slice_end})<EOL><DEDENT>sliced_ann.time = max(<NUM_LIT:0>, ref_time - start_time)<EOL>return sliced_ann<EOL>

Slice the annotation and return as a new `Annotation` object.

Slicing has the same effect as trimming (see `Annotation.trim`) except
that while trimming does not modify the start time of the annotation or
the observations it contains, slicing will set the new annotation's
start time to ``max(0, trimmed_annotation.time - start_time)`` and the
start time of its observations will be set with respect to this new
reference start time.

This function documents the slice operation by adding a list of tuples
to the annotation's sandbox keyed by ``Annotation.sandbox.slice`` which
documents each slice operation with a tuple
``(start_time, end_time, slice_start, slice_end)``, where
``slice_start`` and ``slice_end`` are given by ``trim_start`` and
``trim_end`` (see `Annotation.trim`).

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

This function is useful for example when trimming an audio file,
allowing the user to trim the annotation while ensuring all time
information matches the new trimmed audio file.

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 slice (see `Annotation.trim` 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
-------
sliced_ann : Annotation
    The sliced annotation.

See Also
--------
Annotation.trim

Examples
--------
>>> ann = jams.Annotation(namespace='tag_open', time=2, duration=8)
>>> ann.append(time=2, duration=2, value='one')
>>> ann.append(time=4, duration=2, value='two')
>>> ann.append(time=6, duration=2, value='three')
>>> ann.append(time=7, duration=2, value='four')
>>> ann.append(time=8, duration=2, value='five')
>>> ann_slice = ann.slice(5, 8, strict=False)
>>> print(ann_slice.time, ann_slice.duration)
(0, 3)
>>> ann_slice.to_dataframe()
   time  duration  value confidence
0   0.0       1.0    two       None
1   1.0       2.0  three       None
2   2.0       1.0   four       None
>>> ann_slice_strict = ann.slice(5, 8, strict=True)
>>> print(ann_slice_strict.time, ann_slice_strict.duration)
(0, 3)
>>> ann_slice_strict.to_dataframe()
   time  duration  value confidence
0   1.0       2.0  three       None


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


ints, vals = [], []<EOL>for obs in self.data:<EOL><INDENT>ints.append([obs.time, obs.time + obs.duration])<EOL>vals.append(obs.value)<EOL><DEDENT>if not ints:<EOL><INDENT>return np.empty(shape=(<NUM_LIT:0>, <NUM_LIT:2>), dtype=float), []<EOL><DEDENT>return np.array(ints), vals<EOL>

Extract observation data in a `mir_eval`-friendly format.

        Returns
        -------
        intervals : np.ndarray [shape=(n, 2), dtype=float]
            Start- and end-times of all valued intervals

            `intervals[i, :] = [time[i], time[i] + duration[i]]`

        labels : list
            List view of value field.


ints, vals = [], []<EOL>for obs in self.data:<EOL><INDENT>ints.append(obs.time)<EOL>vals.append(obs.value)<EOL><DEDENT>return np.array(ints), vals<EOL>

Extract observation data in a `mir_eval`-friendly format.

        Returns
        -------
        times : np.ndarray [shape=(n,), dtype=float]
            Start-time of all observations

        labels : list
            List view of value field.


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`.


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`


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.


return self.to_html(max_rows=max_rows)<EOL>

Render annotation as HTML.  See also: `to_html()`


filtered_dict = dict()<EOL>for k, item in six.iteritems(self.__dict__):<EOL><INDENT>if k.startswith('<STR_LIT:_>'):<EOL><INDENT>continue<EOL><DEDENT>elif k == '<STR_LIT:data>':<EOL><INDENT>if data:<EOL><INDENT>filtered_dict[k] = self.__json_data__<EOL><DEDENT>else:<EOL><INDENT>filtered_dict[k] = []<EOL><DEDENT><DEDENT>elif hasattr(item, '<STR_LIT>'):<EOL><INDENT>filtered_dict[k] = item.__json__<EOL><DEDENT>else:<EOL><INDENT>filtered_dict[k] = 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.


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.


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


def __init__(self, name='<STR_LIT>', email='<STR_LIT>'):

super(Curator, self).__init__()<EOL>self.name = name<EOL>self.email = email<EOL>

Create a Curator.

        Parameters
        ----------
        name: str, default=''
            Common name of the curator.

        email: str, default=''
            An email address corresponding to the curator.


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'.


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.


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.


results = AnnotationArray()<EOL>for annotation in self:<EOL><INDENT>if annotation.search(**kwargs):<EOL><INDENT>results.append(annotation)<EOL><DEDENT><DEDENT>return results<EOL>

Filter the annotation array down to only those Annotation
        objects matching the query.

        Parameters
        ----------
        kwargs : search parameters
            See JObject.search

        Returns
        -------
        results : AnnotationArray
            An annotation array of the objects matching the query

        See Also
        --------
        JObject.search


<EOL>if isinstance(idx, (int, slice)):<EOL><INDENT>return list.__getitem__(self, idx)<EOL><DEDENT>elif isinstance(idx, six.string_types) or six.callable(idx):<EOL><INDENT>return self.search(namespace=idx)<EOL><DEDENT>elif isinstance(idx, tuple):<EOL><INDENT>return self.search(namespace=idx[<NUM_LIT:0>])[idx[<NUM_LIT:1>]]<EOL><DEDENT>raise IndexError('<STR_LIT>'.format(idx))<EOL>

Overloaded getitem for syntactic search sugar


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

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

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

Parameters
----------
start_time : float
    The desired start time for the trimmed annotations in seconds.
end_time
    The desired end time for trimmed annotations in seconds. Must be
    greater than ``start_time``.
strict : bool
    When ``False`` (default) observations that lie at the boundaries of
    the trimming range (see `Annotation.trim` 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 trimmed
    annotation.

Returns
-------
trimmed_array : AnnotationArray
    An annotation array where every annotation has been trimmed.


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.


def __init__(self, annotations=None, file_metadata=None, sandbox=None):

super(JAMS, self).__init__()<EOL>if file_metadata is None:<EOL><INDENT>file_metadata = FileMetadata()<EOL><DEDENT>if sandbox is None:<EOL><INDENT>sandbox = Sandbox()<EOL><DEDENT>self.annotations = AnnotationArray(annotations=annotations)<EOL>self.file_metadata = FileMetadata(**file_metadata)<EOL>self.sandbox = Sandbox(**sandbox)<EOL>

Create a Jams object.

        Parameters
        ----------
        annotations : list of Annotations
            Zero or more Annotation objects

        file_metadata : FileMetadata (or dict), default=None
            Metadata corresponding to the audio file.

        sandbox : Sandbox (or dict), default=None
            Unconstrained global sandbox for additional information.


def add(self, jam, on_conflict='<STR_LIT>'):

if on_conflict not in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT:ignore>']:<EOL><INDENT>raise ParameterError("<STR_LIT>"<EOL>"<STR_LIT>".format(on_conflict))<EOL><DEDENT>if not self.file_metadata == jam.file_metadata:<EOL><INDENT>if on_conflict == '<STR_LIT>':<EOL><INDENT>self.file_metadata = jam.file_metadata<EOL><DEDENT>elif on_conflict == '<STR_LIT>':<EOL><INDENT>raise JamsError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT><DEDENT>self.annotations.extend(jam.annotations)<EOL>self.sandbox.update(**jam.sandbox)<EOL>

Add the contents of another jam to this object.

        Note that, by default, this method fails if file_metadata is not
        identical and raises a ValueError; either resolve this manually
        (because conflicts should almost never happen), force an 'overwrite',
        or tell the method to 'ignore' the metadata of the object being added.

        Parameters
        ----------
        jam: JAMS object
            Object to add to this jam

        on_conflict: str, default='fail'
            Strategy for resolving metadata conflicts; one of
                ['fail', 'overwrite', or 'ignore'].

        Raises
        ------
        ParameterError
            if `on_conflict` is an unknown value

        JamsError
            If a conflict is detected and `on_conflict='fail'`


return self.annotations.search(**kwargs)<EOL>

Search a JAMS object for matching objects.

        Parameters
        ----------
        kwargs : keyword arguments
            Keyword query

        Returns
        -------
        AnnotationArray
            All annotation objects in this JAMS which match the query

        See Also
        --------
        JObject.search
        AnnotationArray.search

        Examples
        --------
        A simple query to get all beat annotations

        >>> beats = my_jams.search(namespace='beat')


def save(self, path_or_file, strict=True, fmt='<STR_LIT>'):

self.validate(strict=strict)<EOL>with _open(path_or_file, mode='<STR_LIT:w>', fmt=fmt) as fdesc:<EOL><INDENT>json.dump(self.__json__, fdesc, indent=<NUM_LIT:2>)<EOL><DEDENT>

Serialize annotation as a JSON formatted stream to file.

        Parameters
        ----------
        path_or_file : str or file-like
            Path to save the JAMS object on disk
            OR
            An open file descriptor to write into

        strict : bool
            Force strict schema validation

        fmt : str ['auto', 'jams', 'jamz']
            The output encoding format.

            If `auto`, it is inferred from the file name.

            If the input is an open file handle, `jams` encoding
            is used.

        Raises
        ------
        SchemaError
            If `strict == True` and the JAMS object fails schema
            or namespace validation.

        See also
        --------
        validate


valid = True<EOL>try:<EOL><INDENT>jsonschema.validate(self.__json_light__, schema.JAMS_SCHEMA)<EOL>for ann in self.annotations:<EOL><INDENT>if isinstance(ann, Annotation):<EOL><INDENT>valid &= ann.validate(strict=strict)<EOL><DEDENT>else:<EOL><INDENT>msg = '<STR_LIT>'.format(ann)<EOL>valid = False<EOL>if strict:<EOL><INDENT>raise SchemaError(msg)<EOL><DEDENT>else:<EOL><INDENT>warnings.warn(str(msg))<EOL><DEDENT><DEDENT><DEDENT><DEDENT>except jsonschema.ValidationError as invalid:<EOL><INDENT>if strict:<EOL><INDENT>raise SchemaError(str(invalid))<EOL><DEDENT>else:<EOL><INDENT>warnings.warn(str(invalid))<EOL><DEDENT>valid = False<EOL><DEDENT>return valid<EOL>

Validate a JAMS object against the schema.

        Parameters
        ----------
        strict : bool
            If `True`, an exception will be raised on validation failure.
            If `False`, a warning will be raised on validation failure.

        Returns
        -------
        valid : bool
            `True` if the object passes schema validation.
            `False` otherwise.

        Raises
        ------
        SchemaError
            If `strict==True` and the JAMS object does not match the schema

        See Also
        --------
        jsonschema.validate


<EOL>if self.file_metadata.duration is None:<EOL><INDENT>raise JamsError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>if not (<NUM_LIT:0> <= start_time <= end_time <= float(<EOL>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_trimmed = JAMS(annotations=None,<EOL>file_metadata=self.file_metadata,<EOL>sandbox=self.sandbox)<EOL>jam_trimmed.annotations = self.annotations.trim(<EOL>start_time, end_time, strict=strict)<EOL>if '<STR_LIT>' not in jam_trimmed.sandbox.keys():<EOL><INDENT>jam_trimmed.sandbox.update(<EOL>trim=[{'<STR_LIT>': start_time, '<STR_LIT>': end_time}])<EOL><DEDENT>else:<EOL><INDENT>jam_trimmed.sandbox.trim.append(<EOL>{'<STR_LIT>': start_time, '<STR_LIT>': end_time})<EOL><DEDENT>return jam_trimmed<EOL>

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

See `Annotation.trim` for details about how the annotations
are trimmed.

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

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

Note: trimming does not affect the duration of the jam, i.e. the value
of ``JAMS.file_metadata.duration`` will be the same for the original
and trimmed jams.

Parameters
----------
start_time : float
    The desired start time for the trimmed annotations in seconds.
end_time
    The desired end time for trimmed annotations in seconds. Must be
    greater than ``start_time``.
strict : bool
    When ``False`` (default) observations that lie at the boundaries of
    the trimming range (see `Annotation.trim` 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 trimmed
    annotation.

Returns
-------
jam_trimmed : JAMS
    The trimmed jam with trimmed annotations, returned as a new JAMS
    object.


<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.


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.


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


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>


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 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 downbeat(annotation, sr=<NUM_LIT>, length=None, **kwargs):

beat_click = mkclick(<NUM_LIT> * <NUM_LIT:2>, sr=sr)<EOL>downbeat_click = mkclick(<NUM_LIT> * <NUM_LIT:3>, sr=sr)<EOL>intervals, values = annotation.to_interval_values()<EOL>beats, downbeats = [], []<EOL>for time, value in zip(intervals[:, <NUM_LIT:0>], values):<EOL><INDENT>if value['<STR_LIT>'] == <NUM_LIT:1>:<EOL><INDENT>downbeats.append(time)<EOL><DEDENT>else:<EOL><INDENT>beats.append(time)<EOL><DEDENT><DEDENT>if length is None:<EOL><INDENT>length = int(sr * np.max(intervals)) + len(beat_click) + <NUM_LIT:1><EOL><DEDENT>y = filter_kwargs(mir_eval.sonify.clicks,<EOL>np.asarray(beats),<EOL>fs=sr, length=length, click=beat_click)<EOL>y += filter_kwargs(mir_eval.sonify.clicks,<EOL>np.asarray(downbeats),<EOL>fs=sr, length=length, click=downbeat_click)<EOL>return y<EOL>


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

<EOL>PENT = [<NUM_LIT:1>, <NUM_LIT>/<NUM_LIT>, <NUM_LIT>/<NUM_LIT:3>, <NUM_LIT>/<NUM_LIT:2>, <NUM_LIT>/<NUM_LIT:9>]<EOL>DURATION = <NUM_LIT:0.1><EOL>h_int, _ = hierarchy_flatten(annotation)<EOL>if length is None:<EOL><INDENT>length = int(sr * (max(np.max(_) for _ in h_int) + <NUM_LIT:1.> / DURATION) + <NUM_LIT:1>)<EOL><DEDENT>y = <NUM_LIT:0.0><EOL>for ints, (oc, scale) in zip(h_int, product(range(<NUM_LIT:3>, <NUM_LIT:3> + len(h_int)),<EOL>PENT)):<EOL><INDENT>click = mkclick(<NUM_LIT> * scale * oc, sr=sr, duration=DURATION)<EOL>y = y + filter_kwargs(mir_eval.sonify.clicks,<EOL>np.unique(ints),<EOL>fs=sr, length=length,<EOL>click=click)<EOL><DEDENT>return y<EOL>


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.


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

<EOL>times = defaultdict(list)<EOL>freqs = defaultdict(list)<EOL>for obs in annotation:<EOL><INDENT>times[obs.value['<STR_LIT:index>']].append(obs.time)<EOL>freqs[obs.value['<STR_LIT:index>']].append(obs.value['<STR_LIT>'] *<EOL>(-<NUM_LIT:1>)**(~obs.value['<STR_LIT>']))<EOL><DEDENT>y_out = <NUM_LIT:0.0><EOL>for ix in times:<EOL><INDENT>y_out = y_out + filter_kwargs(mir_eval.sonify.pitch_contour,<EOL>np.asarray(times[ix]),<EOL>np.asarray(freqs[ix]),<EOL>fs=sr, length=length,<EOL>**kwargs)<EOL>if length is None:<EOL><INDENT>length = len(y_out)<EOL><DEDENT><DEDENT>return y_out<EOL>

Sonify pitch contours.

    This uses mir_eval.sonify.pitch_contour, and should only be applied
    to pitch annotations using the pitch_contour namespace.

    Each contour is sonified independently, and the resulting waveforms
    are summed together.


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 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"


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 lab_dump(ann, comment, filename, sep, comment_char):

intervals, values = ann.to_interval_values()<EOL>frame = pd.DataFrame(columns=['<STR_LIT>', '<STR_LIT>', '<STR_LIT>'],<EOL>data={'<STR_LIT>': intervals[:, <NUM_LIT:0>],<EOL>'<STR_LIT>': intervals[:, <NUM_LIT:1>],<EOL>'<STR_LIT>': values})<EOL>with open(filename, '<STR_LIT:w>') as fdesc:<EOL><INDENT>for line in comment.split('<STR_LIT:\n>'):<EOL><INDENT>fdesc.write('<STR_LIT>'.format(comment_char, line))<EOL><DEDENT>frame.to_csv(path_or_buf=fdesc, index=False, sep=sep)<EOL><DEDENT>

Save an annotation as a lab/csv.

    Parameters
    ----------
    ann : Annotation
        The annotation object

    comment : str
        The comment string header

    filename : str
        The output filename

    sep : str
        The separator string for output

    comment_char : str
        The character used to denote comments


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 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.


def __init__(self, file_like, data_types=None):

pass<EOL>


base_url = os.environ['<STR_LIT>']<EOL>host = urllib.parse.urlparse(base_url).netloc<EOL>if port is None:<EOL><INDENT>return host<EOL><DEDENT>service_url_path = os.environ['<STR_LIT>']<EOL>proxy_url_path = '<STR_LIT>' % port<EOL>user_url = urllib.parse.urljoin(base_url, service_url_path)<EOL>full_url = urllib.parse.urljoin(user_url, proxy_url_path)<EOL>return full_url<EOL>

Callable to configure Bokeh's show method when a proxy must be
configured.

If port is None we're asking about the URL
for the origin header.


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.


def _diagram(plot_figure, source=None, color='<STR_LIT>', line_color='<STR_LIT>',<EOL>xaxis_label='<STR_LIT>', yaxis_label='<STR_LIT>', name=None):

plot_figure.circle(x='<STR_LIT:x>', y='<STR_LIT:y>', source=source,<EOL>size=<NUM_LIT:5>, color=color, alpha=<NUM_LIT:1>, name=name,<EOL>line_color=line_color, line_width=<NUM_LIT:0.5>)<EOL>plot_figure.xaxis.axis_label = xaxis_label<EOL>plot_figure.yaxis.axis_label = yaxis_label<EOL>plot_figure.yaxis.formatter = NumeralTickFormatter()<EOL>

Use a :class:`~bokeh.plotting.figure.Figure` and x and y collections
    to create an H-R diagram.


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.


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


pf_image = figure(x_range=(<NUM_LIT:0>, <NUM_LIT:1>), y_range=(<NUM_LIT:0>, <NUM_LIT:1>),<EOL>title='<STR_LIT>'.format(cluster.name))<EOL>pf_image.image_url(url=[cluster.image_path],<EOL>x=<NUM_LIT:0>, y=<NUM_LIT:0>, w=<NUM_LIT:1>, h=<NUM_LIT:1>, anchor='<STR_LIT>')<EOL>pf_image.toolbar_location = None<EOL>pf_image.axis.visible = False<EOL>return pf_image<EOL>

Given a cluster create a Bokeh plot figure using the
cluster's image.


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.


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.


data = round_arr_teff_luminosity(data)<EOL>temps = data['<STR_LIT>']<EOL>x_range = [<NUM_LIT> * np.amax(temps), <NUM_LIT> * np.amin(temps)]<EOL>lums = data['<STR_LIT>']<EOL>y_range = [<NUM_LIT> * np.amin(lums), <NUM_LIT:2> * np.amax(lums)]<EOL>return (x_range, y_range)<EOL>

Given a numpy array calculate what the ranges of the H-R
diagram should be.


def hr_diagram_from_data(data, x_range, y_range):

_, color_mapper = hr_diagram_color_helper([])<EOL>data_dict = {<EOL>'<STR_LIT:x>': list(data['<STR_LIT>']),<EOL>'<STR_LIT:y>': list(data['<STR_LIT>']),<EOL>'<STR_LIT>': list(data['<STR_LIT>'])<EOL>}<EOL>source = ColumnDataSource(data=data_dict)<EOL>pf = figure(y_axis_type='<STR_LIT>', x_range=x_range, y_range=y_range)<EOL>_diagram(source=source, plot_figure=pf,<EOL>color={'<STR_LIT>': '<STR_LIT>', '<STR_LIT>': color_mapper},<EOL>xaxis_label='<STR_LIT>',<EOL>yaxis_label='<STR_LIT>')<EOL>show_with_bokeh_server(pf)<EOL>

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


if not title:<EOL><INDENT>title = '<STR_LIT>'<EOL><DEDENT>return TextInput(value=cluster.name, title=title)<EOL>

Create an :class:`~bokeh.models.widgets.TextInput` using
the cluster.name as the default value and title.

If no title is provided use, 'Type in the name of your cluster
and press Enter/Return:'.


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.


min_temp = self.temperature_range_slider.value[<NUM_LIT:0>]<EOL>max_temp = self.temperature_range_slider.value[<NUM_LIT:1>]<EOL>temp_mask = np.logical_and(<EOL>self.cluster.catalog['<STR_LIT>'] >= min_temp,<EOL>self.cluster.catalog['<STR_LIT>'] <= max_temp<EOL>)<EOL>min_lum = self.luminosity_range_slider.value[<NUM_LIT:0>]<EOL>max_lum = self.luminosity_range_slider.value[<NUM_LIT:1>]<EOL>lum_mask = np.logical_and(<EOL>self.cluster.catalog['<STR_LIT>'] >= min_lum,<EOL>self.cluster.catalog['<STR_LIT>'] <= max_lum<EOL>)<EOL>selected_mask = np.isin(self.cluster.catalog['<STR_LIT:id>'], self.selection_ids)<EOL>filter_mask = temp_mask & lum_mask & selected_mask<EOL>self.filtered_data = self.cluster.catalog[filter_mask].data<EOL>self.source.data = {<EOL>'<STR_LIT:id>': list(self.filtered_data['<STR_LIT:id>']),<EOL>'<STR_LIT>': list(self.filtered_data['<STR_LIT>']),<EOL>'<STR_LIT>': list(self.filtered_data['<STR_LIT>']),<EOL>'<STR_LIT>': list(self.filtered_data['<STR_LIT>'])<EOL>}<EOL>logging.debug("<STR_LIT>", self.filtered_data)<EOL>

Filter the cluster data catalog into the filtered_data
catalog, which is what is shown in the H-R diagram.

Filter on the values of the sliders, as well as the lasso
selection in the skyviewer.


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.


return (-<NUM_LIT> + temp * <NUM_LIT> - temp**<NUM_LIT:2> * <NUM_LIT><EOL>+ temp**<NUM_LIT:3> * <NUM_LIT>)<EOL>

Calculate Bolometric Correction Using the Teff from the previous equation.
This correction is for main sequence stars of the Teff range given above.


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


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.


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.


while self.ws_client.connected():<EOL><INDENT>if self.die:<EOL><INDENT>break<EOL><DEDENT>if self.pause:<EOL><INDENT>sleep(<NUM_LIT:5>)<EOL>continue<EOL><DEDENT>message = self.ws_client.receive()<EOL>if message is None:<EOL><INDENT>break<EOL><DEDENT>message_type = message['<STR_LIT:type>']<EOL>if message_type  == '<STR_LIT:error>':<EOL><INDENT>continue<EOL><DEDENT>if message['<STR_LIT>'] <= self.sequence:<EOL><INDENT>continue<EOL><DEDENT>if message_type == '<STR_LIT>':<EOL><INDENT>self._handle_open(message)<EOL><DEDENT>elif message_type == '<STR_LIT>':<EOL><INDENT>self._handle_match(message)<EOL><DEDENT>elif message_type == '<STR_LIT>':<EOL><INDENT>self._handle_done(message)<EOL><DEDENT>elif message_type == '<STR_LIT>':<EOL><INDENT>self._handle_change(message)<EOL><DEDENT>else:<EOL><INDENT>continue<EOL><DEDENT><DEDENT>self.ws_client.disconnect()<EOL>

Handles real-time updates to the order book.


self.die = True<EOL>

Makes sure the real-time thread dies and the WebSocket disconnects.


self.pause = True<EOL>

Stops real-time updates until resume is called.


self.pause = False<EOL>


return self.book<EOL>

Returns the Real-Time Order Book.

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


return urljoin(self.API_URI, '<STR_LIT:/>'.join(map(quote, parts)))<EOL>

Creates fully qualified endpoint URIs.

        :param parts: the string parts that form the request URI


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


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


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

raise NotImplementedError<EOL>


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

Performs HTTP GET requests.

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


Performs HTTP POST requests.

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


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


if self.is_initial:<EOL><INDENT>return True<EOL><DEDENT>if self.before:<EOL><INDENT>if self.before_cursor:<EOL><INDENT>return True<EOL><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if self.after_cursor:<EOL><INDENT>return True<EOL><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT><DEDENT>

Checks if a next message is possible.

        :returns: True if a next message is possible, otherwise False
        :rtype: bool


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


return not self.before<EOL>

Returns True if the direction is set to after.

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


self.before = True<EOL>


self.before = False<EOL>


raise NotImplementedError<EOL>

Abstract method - must be overriden. Performs the endpoint operation


return self.before_cursor<EOL>

Acquires the before cursor.

        :returns: the before cursor


return self.after_cursor<EOL>

Acquires the after cursor.

        :returns: the after cursor


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>')<EOL>


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


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


return self._place_order(type='<STR_LIT>',<EOL>side=size,<EOL>product_id=product_id,<EOL>size=size,<EOL>funds=funds,<EOL>client_oid=client_oid,<EOL>stp=stp)<EOL>


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


return self._delete('<STR_LIT>', data={'<STR_LIT>':product_id})<EOL>


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


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


def _deposit_withdraw(self, type, amount, coinbase_account_id):

data = {<EOL>'<STR_LIT:type>':type,<EOL>'<STR_LIT>':amount,<EOL>'<STR_LIT>':coinbase_account_id<EOL>}<EOL>return self._post('<STR_LIT>', data=data)<EOL>


def deposit(self, amount, coinbase_account_id):

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


def withdraw(self, amount, coinbase_account_id):


return self._new_report(start_date,<EOL>'<STR_LIT>',<EOL>end_date,<EOL>account_id,<EOL>product_id,<EOL>format,<EOL>email)<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>', report_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>self.is_initial = False<EOL>self.before_cursor = response.headers.get('<STR_LIT>', None)<EOL>self.after_cursor = response.headers.get('<STR_LIT>', None)<EOL>return self._handle_response(response).json()<EOL>

Sends an HTTP request to the REST API and receives the requested data.
        Additionally sets up pagination cursors.

        :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


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>


def get_product_order_book(self, level=None, product_id='<STR_LIT>'):

return self._get('<STR_LIT>', product_id, '<STR_LIT>', params={'<STR_LIT>':level})<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>')<EOL>


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


uri = self._create_api_uri(*relative_path_parts)<EOL>response = get(uri, params=self._get_params(**kwargs))<EOL>self.is_initial = False<EOL>self.before_cursor = response.headers.get('<STR_LIT>', None)<EOL>self.after_cursor = response.headers.get('<STR_LIT>', None)<EOL>return self._handle_response(response).json()<EOL>


next = self.receive()<EOL>if next:<EOL><INDENT>return next<EOL><DEDENT>raise StopIteration<EOL>

Iterator function for Python 3.

        :returns: the next message in the sequence
        :rtype: dict
        :raises StopIteration: if the WebSocket is not connected


return loads(message)<EOL>

Makes sure messages are Pythonic.

        :param str message: raw message
        :returns: Pythonic message
        :rtype: dict


while True:<EOL><INDENT>with self._lock:<EOL><INDENT>if self.connected():<EOL><INDENT>self._ws.ping()<EOL><DEDENT>else:<EOL><INDENT>self.disconnect()<EOL>self._thread = None<EOL>return<EOL><DEDENT><DEDENT>sleep(<NUM_LIT:30>)<EOL><DEDENT>

Used exclusively as a thread which keeps the WebSocket alive.