def _prepare_graph(g, g_colors, q_cls, q_arg, adjust_graph):

g = _test_graph(g)<EOL>if adjust_graph:<EOL><INDENT>pos = nx.get_node_attributes(g, '<STR_LIT>')<EOL>ans = nx.to_dict_of_dicts(g)<EOL>g = adjacency2graph(ans, adjust=<NUM_LIT:2>, is_directed=g.is_directed())<EOL>g = QueueNetworkDiGraph(g)<EOL>if len(pos) > <NUM_LIT:0>:<EOL><INDENT>g.set_pos(pos)<EOL><DEDENT><DEDENT>g.new_vertex_property('<STR_LIT>')<EOL>g.new_vertex_property('<STR_LIT>')<EOL>g.new_vertex_property('<STR_LIT>')<EOL>g.new_vertex_property('<STR_LIT>')<EOL>g.new_edge_property('<STR_LIT>')<EOL>g.new_edge_property('<STR_LIT>')<EOL>g.new_edge_property('<STR_LIT>')<EOL>g.new_edge_property('<STR_LIT>')<EOL>queues = _set_queues(g, q_cls, q_arg, '<STR_LIT>' in g.vertex_properties())<EOL>if '<STR_LIT>' not in g.vertex_properties():<EOL><INDENT>g.set_pos()<EOL><DEDENT>for k, e in enumerate(g.edges()):<EOL><INDENT>g.set_ep(e, '<STR_LIT>', <NUM_LIT>)<EOL>g.set_ep(e, '<STR_LIT>', <NUM_LIT:8>)<EOL>if e[<NUM_LIT:0>] == e[<NUM_LIT:1>]:<EOL><INDENT>g.set_ep(e, '<STR_LIT>', queues[k].colors['<STR_LIT>'])<EOL><DEDENT>else:<EOL><INDENT>g.set_ep(e, '<STR_LIT>', queues[k].colors['<STR_LIT>'])<EOL><DEDENT><DEDENT>for v in g.nodes():<EOL><INDENT>g.set_vp(v, '<STR_LIT>', <NUM_LIT:1>)<EOL>g.set_vp(v, '<STR_LIT>', <NUM_LIT:8>)<EOL>e = (v, v)<EOL>if g.is_edge(e):<EOL><INDENT>g.set_vp(v, '<STR_LIT>', queues[g.edge_index[e]]._current_color(<NUM_LIT:2>))<EOL>g.set_vp(v, '<STR_LIT>', queues[g.edge_index[e]]._current_color())<EOL><DEDENT>else:<EOL><INDENT>g.set_vp(v, '<STR_LIT>', g_colors['<STR_LIT>'])<EOL>g.set_vp(v, '<STR_LIT>', g_colors['<STR_LIT>'])<EOL><DEDENT><DEDENT>return g, queues<EOL>

Prepares a graph for use in :class:`.QueueNetwork`.

    This function is called by ``__init__`` in the
    :class:`.QueueNetwork` class. It creates the :class:`.QueueServer`
    instances that sit on the edges, and sets various edge and node
    properties that are used when drawing the graph.

    Parameters
    ----------
    g : :any:`networkx.DiGraph`, :class:`numpy.ndarray`, dict, \
        ``None``,  etc.
        Any object that networkx can turn into a
        :any:`DiGraph<networkx.DiGraph>`
    g_colors : dict
        A dictionary of colors. The specific keys used are
        ``vertex_color`` and ``vertex_fill_color`` for vertices that
        do not have any loops. Set :class:`.QueueNetwork` for the
        default values passed.
    q_cls : dict
        A dictionary where the keys are integers that represent an edge
        type, and the values are :class:`.QueueServer` classes.
    q_args : dict
        A dictionary where the keys are integers that represent an edge
        type, and the values are the arguments that are used when
        creating an instance of that :class:`.QueueServer` class.
    adjust_graph : bool
        Specifies whether the graph will be adjusted using
        :func:`.adjacency2graph`.

    Returns
    -------
    g : :class:`.QueueNetworkDiGraph`
    queues : list
        A list of :class:`QueueServers<.QueueServer>` where
        ``queues[k]`` is the ``QueueServer`` that sets on the edge with
        edge index ``k``.

    Notes
    -----
    The graph ``g`` should have the ``edge_type`` edge property map.
    If it does not then an ``edge_type`` edge property is
    created and set to 1.

    The following properties are set by each queue: ``vertex_color``,
    ``vertex_fill_color``, ``vertex_fill_color``, ``edge_color``.
    See :class:`.QueueServer` for more on setting these values.

    The following properties are assigned as a properties to the graph;
    their default values for each edge or vertex is shown:

        * ``vertex_pen_width``: ``1``,
        * ``vertex_size``: ``8``,
        * ``edge_control_points``: ``[]``
        * ``edge_marker_size``: ``8``
        * ``edge_pen_width``: ``1.25``

    Raises
    ------
    TypeError
        Raised when the parameter ``g`` is not of a type that can be
        made into a :any:`networkx.DiGraph`.


lat1, lon1 = latlon1<EOL>lat2, lon2 = latlon2<EOL>dlon = lon2 - lon1<EOL>dlat = lat2 - lat1<EOL>R = <NUM_LIT>  <EOL>a = np.sin(dlat / <NUM_LIT:2>)**<NUM_LIT:2> + np.cos(lat1) * np.cos(lat2) * (np.sin(dlon / <NUM_LIT:2>))**<NUM_LIT:2><EOL>c = <NUM_LIT:2> * np.pi * R * np.arctan2(np.sqrt(a), np.sqrt(<NUM_LIT:1> - a)) / <NUM_LIT><EOL>return c<EOL>

Calculates the distance between two points on earth.


def generate_random_graph(num_vertices=<NUM_LIT>, prob_loop=<NUM_LIT:0.5>, **kwargs):

g = minimal_random_graph(num_vertices, **kwargs)<EOL>for v in g.nodes():<EOL><INDENT>e = (v, v)<EOL>if not g.is_edge(e):<EOL><INDENT>if np.random.uniform() < prob_loop:<EOL><INDENT>g.add_edge(*e)<EOL><DEDENT><DEDENT><DEDENT>g = set_types_random(g, **kwargs)<EOL>return g<EOL>

Creates a random graph where the edges have different types.

    This method calls :func:`.minimal_random_graph`, and then adds
    a loop to each vertex with ``prob_loop`` probability. It then
    calls :func:`.set_types_random` on the resulting graph.

    Parameters
    ----------
    num_vertices : int (optional, default: 250)
        The number of vertices in the graph.
    prob_loop : float (optional, default: 0.5)
        The probability that a loop gets added to a vertex.
    **kwargs :
        Any parameters to send to :func:`.minimal_random_graph` or
        :func:`.set_types_random`.

    Returns
    -------
    :class:`.QueueNetworkDiGraph`
        A graph with the position of the vertex set as a property.
        The position property is called ``pos``. Also, the ``edge_type``
        edge property is set for each edge.

    Examples
    --------
    The following generates a directed graph with 50 vertices where half
    the edges are type 1 and 1/4th are type 2 and 1/4th are type 3:

    >>> import queueing_tool as qt
    >>> pTypes = {1: 0.5, 2: 0.25, 3: 0.25}
    >>> g = qt.generate_random_graph(100, proportions=pTypes, seed=17)
    >>> non_loops = [e for e in g.edges() if e[0] != e[1]]
    >>> p1 = np.sum([g.ep(e, 'edge_type') == 1 for e in non_loops])
    >>> float(p1) / len(non_loops) # doctest: +ELLIPSIS
    0.486...
    >>> p2 = np.sum([g.ep(e, 'edge_type') == 2 for e in non_loops])
    >>> float(p2) / len(non_loops) # doctest: +ELLIPSIS
    0.249...
    >>> p3 = np.sum([g.ep(e, 'edge_type') == 3 for e in non_loops])
    >>> float(p3) / len(non_loops) # doctest: +ELLIPSIS
    0.264...

    To make an undirected graph with 25 vertices where there are 4
    different edge types with random proportions:

    >>> p = np.random.rand(4)
    >>> p = p / sum(p)
    >>> p = {k + 1: p[k] for k in range(4)}
    >>> g = qt.generate_random_graph(num_vertices=25, is_directed=False, proportions=p)

    Note that none of the edge types in the above example are 0. It is
    recommended use edge type indices starting at 1, since 0 is
    typically used for terminal edges.


def set_types_random(g, proportions=None, loop_proportions=None, seed=None,<EOL>**kwargs):

g = _test_graph(g)<EOL>if isinstance(seed, numbers.Integral):<EOL><INDENT>np.random.seed(seed)<EOL><DEDENT>if proportions is None:<EOL><INDENT>proportions = {k: <NUM_LIT:1.> / <NUM_LIT:3> for k in range(<NUM_LIT:1>, <NUM_LIT:4>)}<EOL><DEDENT>if loop_proportions is None:<EOL><INDENT>loop_proportions = {k: <NUM_LIT:1.> / <NUM_LIT:4> for k in range(<NUM_LIT:4>)}<EOL><DEDENT>edges = [e for e in g.edges() if e[<NUM_LIT:0>] != e[<NUM_LIT:1>]]<EOL>loops = [e for e in g.edges() if e[<NUM_LIT:0>] == e[<NUM_LIT:1>]]<EOL>props = list(proportions.values())<EOL>lprops = list(loop_proportions.values())<EOL>if not np.isclose(sum(props), <NUM_LIT:1.0>):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if not np.isclose(sum(lprops), <NUM_LIT:1.0>):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>eTypes = {}<EOL>types = list(proportions.keys())<EOL>values = np.random.choice(types, size=len(edges), replace=True, p=props)<EOL>for k, e in enumerate(edges):<EOL><INDENT>eTypes[e] = values[k]<EOL><DEDENT>types = list(loop_proportions.keys())<EOL>values = np.random.choice(types, size=len(loops), replace=True, p=lprops)<EOL>for k, e in enumerate(loops):<EOL><INDENT>eTypes[e] = values[k]<EOL><DEDENT>g.new_edge_property('<STR_LIT>')<EOL>for e in g.edges():<EOL><INDENT>g.set_ep(e, '<STR_LIT>', eTypes[e])<EOL><DEDENT>return g<EOL>

Randomly sets ``edge_type`` (edge type) properties of the graph.

    This function randomly assigns each edge a type. The probability of
    an edge being a specific type is proscribed in the
    ``proportions``, ``loop_proportions`` variables.

    Parameters
    ----------
    g : :any:`networkx.DiGraph`, :class:`numpy.ndarray`, dict, etc.
        Any object that :any:`DiGraph<networkx.DiGraph>` accepts.
    proportions : dict (optional, default: ``{k: 0.25 for k in range(1, 4)}``)
        A dictionary of edge types and proportions, where the keys are
        the types and the values are the proportion of non-loop edges
        that are expected to be of that type. The values can must sum
        to one.
    loop_proportions : dict (optional, default: ``{k: 0.25 for k in range(4)}``)
        A dictionary of edge types and proportions, where the keys are
        the types and the values are the proportion of loop edges
        that are expected to be of that type. The values can must sum
        to one.
    seed : int (optional)
        An integer used to initialize numpy's psuedorandom number
        generator.
    **kwargs :
        Unused.

    Returns
    -------
    :class:`.QueueNetworkDiGraph`
        Returns the a graph with an ``edge_type`` edge property.

    Raises
    ------
    TypeError
        Raised when the parameter ``g`` is not of a type that can be
        made into a :any:`networkx.DiGraph`.

    ValueError
        Raises a :exc:`~ValueError` if the ``pType`` values do not sum
        to one.

    Notes
    -----
    If ``pTypes`` is not explicitly specified in the arguments, then it
    defaults to four types in the graph (types 0, 1, 2, and 3). It sets
    non-loop edges to be either 1, 2, or 3 33\% chance, and loops are
    types 0, 1, 2, 3 with 25\% chance.


return (not os.path.exists(derived)<EOL>or os.stat(derived).st_mtime < os.stat(original).st_mtime)<EOL>

Returns True if derivative is out-of-date wrt original,
both of which are full file paths.


def makefig(code, code_path, output_dir, output_base, config):

<EOL>default_dpi = {'<STR_LIT>': <NUM_LIT>, '<STR_LIT>': <NUM_LIT:200>, '<STR_LIT>': <NUM_LIT:50>}<EOL>formats = []<EOL>for fmt in config.plot_formats:<EOL><INDENT>if isinstance(fmt, str):<EOL><INDENT>formats.append((fmt, default_dpi.get(fmt, <NUM_LIT>)))<EOL><DEDENT>elif type(fmt) in (tuple, list) and len(fmt)==<NUM_LIT:2>:<EOL><INDENT>formats.append((str(fmt[<NUM_LIT:0>]), int(fmt[<NUM_LIT:1>])))<EOL><DEDENT>else:<EOL><INDENT>raise PlotError('<STR_LIT>' % fmt)<EOL><DEDENT><DEDENT>code_pieces = split_code_at_show(code)<EOL>all_exists = True<EOL>img = ImageFile(output_base, output_dir)<EOL>for format, dpi in formats:<EOL><INDENT>if out_of_date(code_path, img.filename(format)):<EOL><INDENT>all_exists = False<EOL>break<EOL><DEDENT>img.formats.append(format)<EOL><DEDENT>if all_exists:<EOL><INDENT>return [(code, [img])]<EOL><DEDENT>results = []<EOL>all_exists = True<EOL>for i, code_piece in enumerate(code_pieces):<EOL><INDENT>images = []<EOL>for j in range(<NUM_LIT:1000>):<EOL><INDENT>img = ImageFile('<STR_LIT>' % (output_base, i, j), output_dir)<EOL>for format, dpi in formats:<EOL><INDENT>if out_of_date(code_path, img.filename(format)):<EOL><INDENT>all_exists = False<EOL>break<EOL><DEDENT>img.formats.append(format)<EOL><DEDENT>if not all_exists:<EOL><INDENT>all_exists = (j > <NUM_LIT:0>)<EOL>break<EOL><DEDENT>images.append(img)<EOL><DEDENT>if not all_exists:<EOL><INDENT>break<EOL><DEDENT>results.append((code_piece, images))<EOL><DEDENT>if all_exists:<EOL><INDENT>return results<EOL><DEDENT>results = []<EOL>ns = {}<EOL>for i, code_piece in enumerate(code_pieces):<EOL><INDENT>plt.close('<STR_LIT:all>')<EOL>run_code(code_piece, code_path, ns)<EOL>images = []<EOL>fig_managers = _pylab_helpers.Gcf.get_all_fig_managers()<EOL>for j, figman in enumerate(fig_managers):<EOL><INDENT>if len(fig_managers) == <NUM_LIT:1> and len(code_pieces) == <NUM_LIT:1>:<EOL><INDENT>img = ImageFile(output_base, output_dir)<EOL><DEDENT>else:<EOL><INDENT>img = ImageFile("<STR_LIT>" % (output_base, i, j),<EOL>output_dir)<EOL><DEDENT>images.append(img)<EOL>for format, dpi in formats:<EOL><INDENT>try:<EOL><INDENT>figman.canvas.figure.savefig(img.filename(format), dpi=dpi)<EOL><DEDENT>except exceptions.BaseException as err:<EOL><INDENT>raise PlotError(traceback.format_exc())<EOL><DEDENT>img.formats.append(format)<EOL><DEDENT><DEDENT>results.append((code_piece, images))<EOL><DEDENT>return results<EOL>

Run a pyplot script *code* and save the images under *output_dir*
with file names derived from *output_base*


parts = []<EOL>is_doctest = contains_doctest(text)<EOL>part = []<EOL>for line in text.split("<STR_LIT:\n>"):<EOL><INDENT>if (not is_doctest and line.strip() == '<STR_LIT>') or(is_doctest and line.strip() == '<STR_LIT>'):<EOL><INDENT>part.append(line)<EOL>parts.append("<STR_LIT:\n>".join(part))<EOL>part = []<EOL><DEDENT>else:<EOL><INDENT>part.append(line)<EOL><DEDENT><DEDENT>if "<STR_LIT:\n>".join(part).strip():<EOL><INDENT>parts.append("<STR_LIT:\n>".join(part))<EOL><DEDENT>return parts<EOL>


self._write(t.name)<EOL>self._write("<STR_LIT:=>")<EOL>self._dispatch(t.expr)<EOL>

Keyword value assignment within function calls and definitions.


<EOL>self._fill("<STR_LIT>")<EOL>self._write(t.modname)<EOL>self._write("<STR_LIT>")<EOL>for i, (name,asname) in enumerate(t.names):<EOL><INDENT>if i != <NUM_LIT:0>:<EOL><INDENT>self._write("<STR_LIT:U+002CU+0020>")<EOL><DEDENT>self._write(name)<EOL>if asname is not None:<EOL><INDENT>self._write("<STR_LIT>"+asname)<EOL><DEDENT><DEDENT>

Handle "from xyz import foo, bar as baz".


self._dispatch(t.expr)<EOL>self._write('<STR_LIT:.>'+t.attrname)<EOL>

Handle assigning an attribute of an object


if t.decorators is not None:<EOL><INDENT>self._fill("<STR_LIT:@>")<EOL>self._dispatch(t.decorators)<EOL><DEDENT>self._fill("<STR_LIT>"+t.name + "<STR_LIT:(>")<EOL>defaults = [None] * (len(t.argnames) - len(t.defaults)) + list(t.defaults)<EOL>for i, arg in enumerate(zip(t.argnames, defaults)):<EOL><INDENT>self._write(arg[<NUM_LIT:0>])<EOL>if arg[<NUM_LIT:1>] is not None:<EOL><INDENT>self._write('<STR_LIT:=>')<EOL>self._dispatch(arg[<NUM_LIT:1>])<EOL><DEDENT>if i < len(t.argnames)-<NUM_LIT:1>:<EOL><INDENT>self._write('<STR_LIT:U+002CU+0020>')<EOL><DEDENT><DEDENT>self._write("<STR_LIT:)>")<EOL>if self._single_func:<EOL><INDENT>self._do_indent = False<EOL><DEDENT>self._enter()<EOL>self._dispatch(t.code)<EOL>self._leave()<EOL>self._do_indent = True<EOL>


self._fill("<STR_LIT>")<EOL>for i, (name,asname) in enumerate(t.names):<EOL><INDENT>if i != <NUM_LIT:0>:<EOL><INDENT>self._write("<STR_LIT:U+002CU+0020>")<EOL><DEDENT>self._write(name)<EOL>if asname is not None:<EOL><INDENT>self._write("<STR_LIT>"+asname)<EOL><DEDENT><DEDENT>


if self._do_indent:<EOL><INDENT>self._write("<STR_LIT:\n>"+"<STR_LIT:U+0020>"*self._indent + text)<EOL><DEDENT>else:<EOL><INDENT>self._write(text)<EOL><DEDENT>

Indent a piece of text, according to the current indentation level


self._write("<STR_LIT>")<EOL>self._indent += <NUM_LIT:1><EOL>

Print ':', and increase the indentation.


if isinstance(t.expr, (Div, Mul, Sub, Add)):<EOL><INDENT>self._write('<STR_LIT:(>')<EOL>self._dispatch(t.expr)<EOL>self._write('<STR_LIT:)>')<EOL><DEDENT>else:<EOL><INDENT>self._dispatch(t.expr)<EOL><DEDENT>self._write('<STR_LIT:.>'+t.attrname)<EOL>

Handle getting an attribute of an object


self._dispatch(t.node)<EOL>self._write("<STR_LIT:(>")<EOL>comma = False<EOL>for e in t.args:<EOL><INDENT>if comma: self._write("<STR_LIT:U+002CU+0020>")<EOL>else: comma = True<EOL>self._dispatch(e)<EOL><DEDENT>if t.star_args:<EOL><INDENT>if comma: self._write("<STR_LIT:U+002CU+0020>")<EOL>else: comma = True<EOL>self._write("<STR_LIT:*>")<EOL>self._dispatch(t.star_args)<EOL><DEDENT>if t.dstar_args:<EOL><INDENT>if comma: self._write("<STR_LIT:U+002CU+0020>")<EOL>else: comma = True<EOL>self._write("<STR_LIT>")<EOL>self._dispatch(t.dstar_args)<EOL><DEDENT>self._write("<STR_LIT:)>")<EOL>


def _parse_index(self, section, content):

def strip_each_in(lst):<EOL><INDENT>return [s.strip() for s in lst]<EOL><DEDENT>out = {}<EOL>section = section.split('<STR_LIT>')<EOL>if len(section) > <NUM_LIT:1>:<EOL><INDENT>out['<STR_LIT:default>'] = strip_each_in(section[<NUM_LIT:1>].split('<STR_LIT:U+002C>'))[<NUM_LIT:0>]<EOL><DEDENT>for line in content:<EOL><INDENT>line = line.split('<STR_LIT::>')<EOL>if len(line) > <NUM_LIT:2>:<EOL><INDENT>out[line[<NUM_LIT:1>]] = strip_each_in(line[<NUM_LIT:2>].split('<STR_LIT:U+002C>'))<EOL><DEDENT><DEDENT>return out<EOL>

.. index: default
   :refguide: something, else, and more


if self._is_at_section():<EOL><INDENT>return<EOL><DEDENT>while True:<EOL><INDENT>summary = self._doc.read_to_next_empty_line()<EOL>summary_str = "<STR_LIT:U+0020>".join([s.strip() for s in summary]).strip()<EOL>if re.compile('<STR_LIT>').match(summary_str):<EOL><INDENT>self['<STR_LIT>'] = summary_str<EOL>if not self._is_at_section():<EOL><INDENT>continue<EOL><DEDENT><DEDENT>break<EOL><DEDENT>if summary is not None:<EOL><INDENT>self['<STR_LIT>'] = summary<EOL><DEDENT>if not self._is_at_section():<EOL><INDENT>self['<STR_LIT>'] = self._read_to_next_section()<EOL><DEDENT>


items = []<EOL>def parse_item_name(text):<EOL><INDENT>"""<STR_LIT>"""<EOL>m = self._name_rgx.match(text)<EOL>if m:<EOL><INDENT>g = m.groups()<EOL>if g[<NUM_LIT:1>] is None:<EOL><INDENT>return g[<NUM_LIT:3>], None<EOL><DEDENT>else:<EOL><INDENT>return g[<NUM_LIT:2>], g[<NUM_LIT:1>]<EOL><DEDENT><DEDENT>raise ValueError("<STR_LIT>" % text)<EOL><DEDENT>def push_item(name, rest):<EOL><INDENT>if not name:<EOL><INDENT>return<EOL><DEDENT>name, role = parse_item_name(name)<EOL>items.append((name, list(rest), role))<EOL>del rest[:]<EOL><DEDENT>current_func = None<EOL>rest = []<EOL>for line in content:<EOL><INDENT>if not line.strip(): continue<EOL>m = self._name_rgx.match(line)<EOL>if m and line[m.end():].strip().startswith('<STR_LIT::>'):<EOL><INDENT>push_item(current_func, rest)<EOL>current_func, line = line[:m.end()], line[m.end():]<EOL>rest = [line.split('<STR_LIT::>', <NUM_LIT:1>)[<NUM_LIT:1>].strip()]<EOL>if not rest[<NUM_LIT:0>]:<EOL><INDENT>rest = []<EOL><DEDENT><DEDENT>elif not line.startswith('<STR_LIT:U+0020>'):<EOL><INDENT>push_item(current_func, rest)<EOL>current_func = None<EOL>if '<STR_LIT:U+002C>' in line:<EOL><INDENT>for func in line.split('<STR_LIT:U+002C>'):<EOL><INDENT>if func.strip():<EOL><INDENT>push_item(func, [])<EOL><DEDENT><DEDENT><DEDENT>elif line.strip():<EOL><INDENT>current_func = line<EOL><DEDENT><DEDENT>elif current_func is not None:<EOL><INDENT>rest.append(line.strip())<EOL><DEDENT><DEDENT>push_item(current_func, rest)<EOL>return items<EOL>

func_name : Descriptive text
    continued text
another_func_name : Descriptive text
func_name1, func_name2, :meth:`func_name`, func_name3


def add(self, string, start, end, line):

if string.strip():<EOL><INDENT>self.start_lineno = min(self.start_lineno, start[<NUM_LIT:0>])<EOL>self.end_lineno = max(self.end_lineno, end[<NUM_LIT:0>])<EOL><DEDENT>


for prev, block in zip(self.blocks[:-<NUM_LIT:1>], self.blocks[<NUM_LIT:1>:]):<EOL><INDENT>if not block.is_comment:<EOL><INDENT>self.index[block.start_lineno] = prev<EOL><DEDENT><DEDENT>

Make the index mapping lines of actual code to their associated
        prefix comments.


if self.empty:<EOL><INDENT>return []<EOL><DEDENT>rows = list(self.tbody)<EOL>observations = []<EOL>for row_observation, row_details in zip(rows[::<NUM_LIT:2>], rows[<NUM_LIT:1>::<NUM_LIT:2>]):<EOL><INDENT>data = {}<EOL>cells = OBSERVATION_XPATH(row_observation)<EOL>data['<STR_LIT:name>'] = _clean_cell(cells[<NUM_LIT:0>])<EOL>data['<STR_LIT:date>'] = _clean_cell(cells[<NUM_LIT:1>])<EOL>data['<STR_LIT>'] = _clean_cell(cells[<NUM_LIT:3>])<EOL>data['<STR_LIT>'] = _clean_cell(cells[<NUM_LIT:6>])<EOL>cells = DETAILS_XPATH(row_details)<EOL>data['<STR_LIT>'] = _clean_cell(cells[<NUM_LIT:0>])<EOL>data['<STR_LIT>'] = _clean_cell(cells[<NUM_LIT:3>]).replace('<STR_LIT:None>', '<STR_LIT>')<EOL>data['<STR_LIT>'] = _clean_cell(cells[<NUM_LIT:4>])<EOL>data['<STR_LIT>'] = _clean_cell(cells[<NUM_LIT:5>])<EOL>observations.append(data)<EOL><DEDENT>return observations<EOL>

Parses the HTML table into a list of dictionaries, each of which
represents a single observation.


page_number = <NUM_LIT:1><EOL>observations = []<EOL>while True:<EOL><INDENT>logger.info('<STR_LIT>', page_number)<EOL>response = requests.get(WEBOBS_RESULTS_URL, params={<EOL>'<STR_LIT>': observer_code,<EOL>'<STR_LIT>': <NUM_LIT:200>,<EOL>'<STR_LIT>': '<STR_LIT:all>',<EOL>'<STR_LIT>': page_number,<EOL>})<EOL>logger.debug(response.request.url)<EOL>parser = WebObsResultsParser(response.text)<EOL>observations.extend(parser.get_observations())<EOL>if '<STR_LIT>' not in response.text:<EOL><INDENT>break<EOL><DEDENT>page_number += <NUM_LIT:1><EOL><DEDENT>return observations<EOL>

Downloads all variable star observations by a given observer.

Performs a series of HTTP requests to AAVSO's WebObs search and
downloads the results page by page. Each page is then passed to
:py:class:`~pyaavso.parsers.webobs.WebObsResultsParser` and parse results
are added to the final observation list.


table = self.conn.create_table(<EOL>name=self.get_table_name(),<EOL>schema=self.get_schema(),<EOL>read_units=self.get_read_units(),<EOL>write_units=self.get_write_units(),<EOL>)<EOL>if table.status != '<STR_LIT>':<EOL><INDENT>table.refresh(wait_for_active=True, retry_seconds=<NUM_LIT:1>)<EOL><DEDENT>return table<EOL>

Hook point for overriding how the CounterPool creates a new table
in DynamooDB


def __init__(self, aws_access_key=None, aws_secret_key=None, table_name=None, schema=None, read_units=None, write_units=None, auto_create_table=True, ):

self.conn = self.get_conn(aws_access_key, aws_secret_key)<EOL>self.table_name = table_name or self.table_name<EOL>self.schema = schema or self.schema<EOL>self.read_units = read_units or self.read_units<EOL>self.write_units = write_units or self.write_units<EOL>self.auto_create_table = auto_create_table<EOL>super(CounterPool, self).__init__()<EOL>

:aws_access_key:
    AWS Acccess Key ID with permissions to use DynamoDB
:aws_secret_key:
    AWS Access Secret Key for the given Access Key ID
:table_name:
    The DynamoDB table that should be used to store this pool's
    counters.  See http://bit.ly/DynamoDBModel for details on
    DynamoDB's data model.
:schema:
    The schema that will be used to create a table if one does not
    already exist.  See the `boto`<http://bit.ly/BotoCreateTable>_
    docs for details on what's expected for a schema.
:read_units:
    Read throughput to be set when a table is created.  See
    http://bit.ly/DynamoThoughput for details on Dynamo's provisioned
    throughput system.
:write_units:
    Write throughput to be set when a table is created.
:auto_create_table:
    Should Albertson create a dynamodb table if the provided
    `table_name` doesn't exist.


return self.write_units<EOL>

Hook point for overriding how the CounterPool determines the write
throughput units to set on a newly created table.


def get_counter(self, name, start=<NUM_LIT:0>):

item = self.get_item(hash_key=name, start=start)<EOL>counter = Counter(dynamo_item=item, pool=self)<EOL>return counter<EOL>

Gets the DynamoDB item behind a counter and ties it to a Counter
instace.


def get_item(self, hash_key, start=<NUM_LIT:0>, extra_attrs=None):

table = self.get_table()<EOL>try:<EOL><INDENT>item = table.get_item(hash_key=hash_key)<EOL><DEDENT>except DynamoDBKeyNotFoundError:<EOL><INDENT>item = None<EOL><DEDENT>if item is None:<EOL><INDENT>item = self.create_item(<EOL>hash_key=hash_key,<EOL>start=start,<EOL>extra_attrs=extra_attrs,<EOL>)<EOL><DEDENT>return item<EOL>

Hook point for overriding how the CouterPool fetches a DynamoDB item
for a given counter.


if not self.schema:<EOL><INDENT>raise NotImplementedError(<EOL>'<STR_LIT>'<EOL>)<EOL><DEDENT>return self.conn.create_schema(**self.schema)<EOL>

Hook point for overriding how the CounterPool determines the schema
to be used when creating a missing table.


task_id = self.celery_self.name<EOL>if self.include_args:<EOL><INDENT>merged_args = str(self.args) + str([(k, self.kwargs[k]) for k in sorted(self.kwargs)])<EOL>task_id += '<STR_LIT>'.format(hashlib.md5(merged_args.encode('<STR_LIT:utf-8>')).hexdigest())<EOL><DEDENT>return task_id<EOL>

Return the unique identifier (string) of a task instance.


_state._register_app = self.original_register_app  <EOL>if not hasattr(app, '<STR_LIT>'):<EOL><INDENT>app.extensions = dict()<EOL><DEDENT>if '<STR_LIT>' in app.extensions:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>app.extensions['<STR_LIT>'] = _CeleryState(self, app)<EOL>super(Celery, self).__init__(app.import_name, broker=app.config['<STR_LIT>'])<EOL>if '<STR_LIT>' in app.config:<EOL><INDENT>self._preconf['<STR_LIT>'] = app.config['<STR_LIT>']<EOL><DEDENT>self.conf.update(app.config)<EOL>task_base = self.Task<EOL>class ContextTask(task_base):<EOL><INDENT>def __call__(self, *_args, **_kwargs):<EOL><INDENT>with app.app_context():<EOL><INDENT>return task_base.__call__(self, *_args, **_kwargs)<EOL><DEDENT><DEDENT><DEDENT>setattr(ContextTask, '<STR_LIT>', True)<EOL>setattr(self, '<STR_LIT>', ContextTask)<EOL>

Actual method to read celery settings from app configuration and initialize the celery instance.

        :param app: Flask application instance.


self.delete_group(self.task_identifier)<EOL>


pass<EOL>


Called when the Celery worker thread is ready to do work.

    This is to avoid race conditions since everything is in one python process.


config = dict()<EOL>if os.environ.get('<STR_LIT>') == '<STR_LIT>':<EOL><INDENT>config['<STR_LIT>'] = '<STR_LIT>'<EOL><DEDENT>elif os.environ.get('<STR_LIT>') == '<STR_LIT>':<EOL><INDENT>config['<STR_LIT>'] = '<STR_LIT>'<EOL>config['<STR_LIT>'] = config['<STR_LIT>']<EOL><DEDENT>elif os.environ.get('<STR_LIT>', '<STR_LIT>').startswith('<STR_LIT>'):<EOL><INDENT>config['<STR_LIT>'] = '<STR_LIT>' + os.environ['<STR_LIT>'].split('<STR_LIT:U+002C>', <NUM_LIT:1>)[<NUM_LIT:1>]<EOL>config['<STR_LIT>'] = config['<STR_LIT>']<EOL><DEDENT>elif os.environ.get('<STR_LIT>') == '<STR_LIT>':<EOL><INDENT>config['<STR_LIT>'] = '<STR_LIT>'<EOL><DEDENT>elif os.environ.get('<STR_LIT>') == '<STR_LIT>':<EOL><INDENT>config['<STR_LIT>'] = '<STR_LIT>'<EOL><DEDENT>elif os.environ.get('<STR_LIT>') == '<STR_LIT>':<EOL><INDENT>config['<STR_LIT>'] = '<STR_LIT>'<EOL><DEDENT>elif os.environ.get('<STR_LIT>') == '<STR_LIT>':<EOL><INDENT>config['<STR_LIT>'] = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>if os.environ.get('<STR_LIT>') == '<STR_LIT>':<EOL><INDENT>config['<STR_LIT>'] = '<STR_LIT>'<EOL><DEDENT>elif os.environ.get('<STR_LIT>') == '<STR_LIT>':<EOL><INDENT>config['<STR_LIT>'] = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>file_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), '<STR_LIT>')<EOL>config['<STR_LIT>'] = '<STR_LIT>' + file_path<EOL><DEDENT>config['<STR_LIT>'] = '<STR_LIT>' + config['<STR_LIT>']<EOL>config['<STR_LIT>'] = '<STR_LIT>' + config['<STR_LIT>']<EOL><DEDENT>if '<STR_LIT>' in config and '<STR_LIT>' not in config:<EOL><INDENT>config['<STR_LIT>'] = config['<STR_LIT>']<EOL><DEDENT>return config<EOL>

Generate a Flask config dict with settings for a specific broker based on an environment variable.

    To be merged into app.config.

    :return: Flask config to be fed into app.config.update().
    :rtype: dict


return x - y<EOL>


@celery.task(bind=True)<EOL>@single_instance(include_args=True, lock_timeout=<NUM_LIT:20>)<EOL>def mul(x, y):

return x * y<EOL>


return x + y<EOL>


@celery.task(bind=True, soft_time_limit=<NUM_LIT>)<EOL>@single_instance<EOL>def add3(x, y):

return x + y<EOL>


project = __import__(IMPORT, fromlist=['<STR_LIT>'])<EOL>for expected, var in [('<STR_LIT>', '<STR_LIT>'), (LICENSE, '<STR_LIT>'), (VERSION, '<STR_LIT>')]:<EOL><INDENT>if getattr(project, var) != expected:<EOL><INDENT>raise SystemExit('<STR_LIT>'.format(var))<EOL><DEDENT><DEDENT>if not re.compile(r'<STR_LIT>' % VERSION, re.MULTILINE).search(readme()):<EOL><INDENT>raise SystemExit('<STR_LIT>')<EOL><DEDENT>if INSTALL_REQUIRES:<EOL><INDENT>contents = readme('<STR_LIT>')<EOL>section = re.compile(r'<STR_LIT>', re.DOTALL).findall(contents)<EOL>if not section:<EOL><INDENT>raise SystemExit('<STR_LIT>')<EOL><DEDENT>in_tox = re.findall(r'<STR_LIT>', section[<NUM_LIT:0>])<EOL>if INSTALL_REQUIRES != in_tox:<EOL><INDENT>raise SystemExit('<STR_LIT>')<EOL><DEDENT><DEDENT>


following = []<EOL>try:<EOL><INDENT>for (nick, url) in self.cfg.items("<STR_LIT>"):<EOL><INDENT>source = Source(nick, url)<EOL>following.append(source)<EOL><DEDENT><DEDENT>except configparser.NoSectionError as e:<EOL><INDENT>logger.debug(e)<EOL><DEDENT>return following<EOL>

A :class:`list` of all :class:`Source` objects.


file = os.path.join(Config.config_dir, Config.config_name)<EOL>return cls.from_file(file)<EOL>

Make a guess about the config file location an try loading it.


if not self.cfg.has_section("<STR_LIT>"):<EOL><INDENT>self.cfg.add_section("<STR_LIT>")<EOL><DEDENT>self.cfg.set("<STR_LIT>", source.nick, source.url)<EOL>self.write_config()<EOL>

Adds a new :class:`Source` to the config’s following section.


url = self.cfg.get("<STR_LIT>", nick, fallback=None)<EOL>return Source(nick, url) if url else None<EOL>

Returns the :class:`Source` of the given nick.

        :param str nick: nickname for which will be searched in the config


with open(self.config_file, "<STR_LIT:w>") as config_file:<EOL><INDENT>self.cfg.write(config_file)<EOL><DEDENT>

Writes `self.cfg` to `self.config_file`.


default_map = {<EOL>"<STR_LIT>": {<EOL>"<STR_LIT>": self.check_following,<EOL>"<STR_LIT>": self.timeout,<EOL>"<STR_LIT>": self.porcelain,<EOL>},<EOL>"<STR_LIT>": {<EOL>"<STR_LIT>": self.twtfile,<EOL>},<EOL>"<STR_LIT>": {<EOL>"<STR_LIT>": self.use_pager,<EOL>"<STR_LIT>": self.use_cache,<EOL>"<STR_LIT>": self.limit_timeline,<EOL>"<STR_LIT>": self.timeout,<EOL>"<STR_LIT>": self.sorting,<EOL>"<STR_LIT>": self.porcelain,<EOL>"<STR_LIT>": self.twtfile,<EOL>"<STR_LIT>": self.timeline_update_interval,<EOL>},<EOL>"<STR_LIT>": {<EOL>"<STR_LIT>": self.use_pager,<EOL>"<STR_LIT>": self.use_cache,<EOL>"<STR_LIT>": self.limit_timeline,<EOL>"<STR_LIT>": self.timeout,<EOL>"<STR_LIT>": self.sorting,<EOL>"<STR_LIT>": self.porcelain,<EOL>"<STR_LIT>": self.timeline_update_interval,<EOL>}<EOL>}<EOL>return default_map<EOL>

Maps config options to the default values used by click, returns :class:`dict`.


try:<EOL><INDENT>del self.cache[url]<EOL>self.mark_updated()<EOL>return True<EOL><DEDENT>except KeyError:<EOL><INDENT>return False<EOL><DEDENT>


def __init__(self, cache_file, cache, update_interval):

self.cache_file = cache_file<EOL>self.cache = cache<EOL>self.update_interval = update_interval<EOL>

Initializes new :class:`Cache` object.

        :param str cache_file: full path to the loaded cache file.
        :param ~shelve.Shelve cache: a Shelve object, with cache loaded.
        :param int update_interval: number of seconds the cache is considered to be
                                    up-to-date without calling any external resources.


def add_tweets(self, url, last_modified, tweets):

try:<EOL><INDENT>self.cache[url] = {"<STR_LIT>": last_modified, "<STR_LIT>": tweets}<EOL>self.mark_updated()<EOL>return True<EOL><DEDENT>except TypeError:<EOL><INDENT>return False<EOL><DEDENT>


if not self.is_valid:<EOL><INDENT>self.cache["<STR_LIT>"] = timestamp()<EOL><DEDENT>

Mark cache as updated at current *NIX timestamp


@classmethod<EOL><INDENT>def discover(cls, *args, **kwargs):<DEDENT>

file = os.path.join(Cache.cache_dir, Cache.cache_name)<EOL>return cls.from_file(file, *args, **kwargs)<EOL>

Make a guess about the cache file location an try loading it.


try:<EOL><INDENT>tweets = self.cache[url]["<STR_LIT>"]<EOL>self.mark_updated()<EOL>return sorted(tweets, reverse=True)[:limit]<EOL><DEDENT>except KeyError:<EOL><INDENT>return []<EOL><DEDENT>


try:<EOL><INDENT>self.cache.close()<EOL>return True<EOL><DEDENT>except AttributeError:<EOL><INDENT>return False<EOL><DEDENT>


try:<EOL><INDENT>return self.cache["<STR_LIT>"]<EOL><DEDENT>except KeyError:<EOL><INDENT>return <NUM_LIT:0><EOL><DEDENT>

Returns *NIX timestamp of last update of the cache.


return make_aware(dateutil.parser.parse(string))<EOL>


def parse_tweet(raw_tweet, source, now=None):

if now is None:<EOL><INDENT>now = datetime.now(timezone.utc)<EOL><DEDENT>raw_created_at, text = raw_tweet.split("<STR_LIT:\t>", <NUM_LIT:1>)<EOL>created_at = parse_iso8601(raw_created_at)<EOL>if created_at > now:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>return Tweet(click.unstyle(text.strip()), created_at, source)<EOL>

Parses a single raw tweet line from a twtxt file
and returns a :class:`Tweet` object.

:param str raw_tweet: a single raw tweet line
:param Source source: the source of the given tweet
:param Datetime now: the current datetime

:returns: the parsed tweet
:rtype: Tweet


ctx.forward(timeline)<EOL>


source = Source(nick, url)<EOL>sources = ctx.obj['<STR_LIT>'].following<EOL>if not force:<EOL><INDENT>if source.nick in (source.nick for source in sources):<EOL><INDENT>click.confirm("<STR_LIT>".format(<EOL>click.style(source.nick, bold=True)), default=False, abort=True)<EOL><DEDENT>_, status = get_remote_status([source])[<NUM_LIT:0>]<EOL>if not status or status.status_code != <NUM_LIT:200>:<EOL><INDENT>click.confirm("<STR_LIT>".format(<EOL>click.style(source.nick, bold=True),<EOL>click.style(source.url, bold=True)), default=False, abort=True)<EOL><DEDENT><DEDENT>ctx.obj['<STR_LIT>'].add_source(source)<EOL>click.echo("<STR_LIT>".format(<EOL>click.style(source.nick, bold=True)))<EOL>


@click.group()<EOL>@click.option("<STR_LIT>", "<STR_LIT:-c>",<EOL>type=click.Path(exists=True, file_okay=True, readable=True, writable=True, resolve_path=True),<EOL>help="<STR_LIT>")<EOL>@click.option("<STR_LIT>", "<STR_LIT>",<EOL>is_flag=True, default=False,<EOL>help="<STR_LIT>")<EOL>@click.version_option()<EOL>@click.pass_context<EOL>def cli(ctx, config, verbose):

init_logging(debug=verbose)<EOL>if ctx.invoked_subcommand == "<STR_LIT>":<EOL><INDENT>return  <EOL><DEDENT>try:<EOL><INDENT>if config:<EOL><INDENT>conf = Config.from_file(config)<EOL><DEDENT>else:<EOL><INDENT>conf = Config.discover()<EOL><DEDENT><DEDENT>except ValueError as e:<EOL><INDENT>if "<STR_LIT>" in str(e):<EOL><INDENT>click.echo("<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>click.echo("<STR_LIT>")<EOL><DEDENT>sys.exit()<EOL><DEDENT>ctx.default_map = conf.build_default_map()<EOL>ctx.obj = {'<STR_LIT>': conf}<EOL>

Decentralised, minimalist microblogging service for hackers.


sources = ctx.obj['<STR_LIT>'].following<EOL>if check:<EOL><INDENT>sources = get_remote_status(sources, timeout)<EOL>for (source, status) in sources:<EOL><INDENT>click.echo(style_source_with_status(source, status, porcelain))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>sources = sorted(sources, key=lambda source: source.nick)<EOL>for source in sources:<EOL><INDENT>click.echo(style_source(source, porcelain))<EOL><DEDENT><DEDENT>

Return the list of sources you’re following.


source = ctx.obj['<STR_LIT>'].get_source_by_nick(nick)<EOL>try:<EOL><INDENT>with Cache.discover() as cache:<EOL><INDENT>cache.remove_tweets(source.url)<EOL><DEDENT><DEDENT>except OSError as e:<EOL><INDENT>logger.debug(e)<EOL><DEDENT>ret_val = ctx.obj['<STR_LIT>'].remove_source_by_nick(nick)<EOL>if ret_val:<EOL><INDENT>click.echo("<STR_LIT>".format(<EOL>click.style(source.nick, bold=True)))<EOL><DEDENT>else:<EOL><INDENT>click.echo("<STR_LIT>".format(<EOL>click.style(nick, bold=True)))<EOL><DEDENT>

Remove an existing source from your followings.


def handle_mention(match):<EOL><INDENT>name, url = match.groups()<EOL>return format_callback(name, url)<EOL><DEDENT>return mention_re.sub(handle_mention, text)<EOL>

Searches the given text for mentions generated by `expand_mention()` and returns a human-readable form.

    For example:
    "@<bob http://example.org/twtxt.txt>" will result in "@bob"

    If you follow a source: source.nick will be bold
    If you are the mentioned source: source.nick will be bold and coloured
    If nothing from the above is true: nick will be unstyled
    If nothing from the above is true and nick is not given: url will be used


if embed_names:<EOL><INDENT>mention_format = "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>mention_format = "<STR_LIT>"<EOL><DEDENT>def handle_mention(match):<EOL><INDENT>source = get_source_by_name(match.group(<NUM_LIT:1>))<EOL>if source is None:<EOL><INDENT>return "<STR_LIT>".format(match.group(<NUM_LIT:1>))<EOL><DEDENT>return mention_format.format(<EOL>name=source.nick,<EOL>url=source.url)<EOL><DEDENT>return short_mention_re.sub(handle_mention, text)<EOL>

Searches the given text for mentions and expands them.

    For example:
    "@source.nick" will be expanded to "@<source.nick source.url>".


now = datetime.now(timezone.utc)<EOL>tense = "<STR_LIT>" if self.created_at > now else "<STR_LIT>"<EOL>return "<STR_LIT>".format(humanize.naturaldelta(now - self.created_at), tense)<EOL>

Return human-readable relative time string.


try:<EOL><INDENT>import versiontools<EOL><DEDENT>except ImportError:<EOL><INDENT>return None<EOL><DEDENT>else:<EOL><INDENT>return str(versiontools.Version.from_expression(self.name))<EOL><DEDENT>

Get a live version string using versiontools


X = check_array(X)<EOL>if self.scale_by_median:<EOL><INDENT>self.median_ = np.median(X[np.triu_indices_from(X, k=<NUM_LIT:1>)],<EOL>overwrite_input=True)<EOL><DEDENT>elif hasattr(self, '<STR_LIT>'):<EOL><INDENT>del self.median_<EOL><DEDENT>return self<EOL>

If scale_by_median, find :attr:`median_`; otherwise, do nothing.

Parameters
----------
X : array
    The raw pairwise distances.


n = self.flip_.shape[<NUM_LIT:0>]<EOL>if X.ndim != <NUM_LIT:2> or X.shape[<NUM_LIT:1>] != n:<EOL><INDENT>msg = "<STR_LIT>"<EOL>raise TypeError(msg.format(self.flip_.shape[<NUM_LIT:0>]))<EOL><DEDENT>return np.dot(X, self.flip_)<EOL>

Transforms X according to the linear transformation corresponding to
flipping the input eigenvalues.

Parameters
----------
X : array, shape [n_test, n]
    The test similarities to training points.

Returns
-------
Xt : array, shape [n_test, n]
    The transformed test similarites to training points.


n = X.shape[<NUM_LIT:0>]<EOL>if X.shape != (n, n):<EOL><INDENT>raise TypeError("<STR_LIT>")<EOL><DEDENT>self.train_ = X<EOL>memory = get_memory(self.memory)<EOL>lo, = memory.cache(scipy.linalg.eigvalsh)(X, eigvals=(<NUM_LIT:0>, <NUM_LIT:0>))<EOL>self.shift_ = max(self.min_eig - lo, <NUM_LIT:0>)<EOL>return self<EOL>

Learn the transformation to shifted eigenvalues. Only depends
on the input dimension.

Parameters
----------
X : array, shape [n, n]
    The *symmetric* input similarities.


n = self.train_.shape[<NUM_LIT:0>]<EOL>if X.ndim != <NUM_LIT:2> or X.shape[<NUM_LIT:1>] != n:<EOL><INDENT>msg = "<STR_LIT>"<EOL>raise TypeError(msg.format(n))<EOL><DEDENT>if self.copy:<EOL><INDENT>X = X.copy()<EOL><DEDENT>if self.shift_ != <NUM_LIT:0> and X is self.train_ or (<EOL>X.shape == self.train_.shape and np.allclose(X, self.train_)):<EOL><INDENT>X[xrange(n), xrange(n)] += self.shift_<EOL><DEDENT>return X<EOL>

Transforms X according to the linear transformation corresponding to
shifting the input eigenvalues to all be at least ``self.min_eig``.

Parameters
----------
X : array, shape [n_test, n]
    The test similarities to training points.

Returns
-------
Xt : array, shape [n_test, n]
    The transformed test similarites to training points. Only different
    from X if X is the training data.


n = X.shape[<NUM_LIT:0>]<EOL>if X.shape != (n, n):<EOL><INDENT>raise TypeError("<STR_LIT>")<EOL><DEDENT>memory = get_memory(self.memory)<EOL>discard_X = not self.copy and self.negatives_likely<EOL>vals, vecs = memory.cache(scipy.linalg.eigh, ignore=['<STR_LIT>'])(<EOL>X, overwrite_a=discard_X)<EOL>vals = vals[:, None]<EOL>self.clip_ = np.dot(vecs, np.sign(vals) * vecs.T)<EOL>if discard_X or vals[<NUM_LIT:0>, <NUM_LIT:0>] < <NUM_LIT:0>:<EOL><INDENT>del X<EOL>np.abs(vals, out=vals)<EOL>X = np.dot(vecs, vals * vecs.T)<EOL>del vals, vecs<EOL>X = Symmetrize(copy=False).fit_transform(X)<EOL><DEDENT>return X<EOL>

Flips the negative eigenvalues of X.

Parameters
----------
X : array, shape [n, n]
    The *symmetric* input similarities. If X is asymmetric, it will be
    treated as if it were symmetric based on its lower-triangular part.

Returns
-------
Xt : array, shape [n, n]
    The transformed training similarities.


n = self.clip_.shape[<NUM_LIT:0>]<EOL>if X.ndim != <NUM_LIT:2> or X.shape[<NUM_LIT:1>] != n:<EOL><INDENT>msg = "<STR_LIT>"<EOL>raise TypeError(msg.format(self.clip_.shape[<NUM_LIT:0>]))<EOL><DEDENT>return np.dot(X, self.clip_)<EOL>

Transforms X according to the linear transformation corresponding to
clipping the input eigenvalues.

Parameters
----------
X : array, shape [n_test, n]
    The test similarities to training points.

Returns
-------
Xt : array, shape [n_test, n]
    The transformed test similarites to training points.


self.features_ = as_features(X, stack=True, bare=True)<EOL>return self<EOL>

Specify the data to which kernel values should be computed.

Parameters
----------
X : list of arrays or :class:`skl_groups.features.Features`
    The bags to compute "to".


X = as_features(X, stack=True, bare=True)<EOL>Y = self.features_<EOL>if X.dim != Y.dim:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>.format(X.dim, Y.dim))<EOL><DEDENT>pointwise = pairwise_kernels(X.stacked_features, Y.stacked_features,<EOL>metric=self.kernel,<EOL>filter_params=True,<EOL>**self._get_kernel_params())<EOL>K = np.empty((len(X), len(Y)))<EOL>for i in range(len(X)):<EOL><INDENT>for j in range(len(Y)):<EOL><INDENT>K[i, j] = pointwise[X._boundaries[i]:X._boundaries[i+<NUM_LIT:1>],<EOL>Y._boundaries[j]:Y._boundaries[j+<NUM_LIT:1>]].mean()<EOL><DEDENT><DEDENT>return K<EOL>

Compute kernels from X to :attr:`features_`.

Parameters
----------
X : list of arrays or :class:`skl_groups.features.Features`
    The bags to compute "from". Must have same dimension as
    :attr:`features_`.

Returns
-------
K : array of shape ``[len(X), len(features_)]``
    The kernel evaluations from X to :attr:`features_`.


X = as_features(X, stack=True)<EOL>X_new = self.transformer.transform(X.stacked_features, **params)<EOL>return self._gather_outputs(X, X_new)<EOL>

Transform the stacked points.

Parameters
----------
X : :class:`Features` or list of bag feature arrays
    New data to transform.

any other keyword argument :
    Passed on as keyword arguments to the transformer's ``transform()``.

Returns
-------
X_new : :class:`Features`
    Transformed features.


X = check_array(X, copy=self.copy)<EOL>X *= self.scale_<EOL>X += self.min_<EOL>if self.truncate:<EOL><INDENT>np.maximum(self.feature_range[<NUM_LIT:0>], X, out=X)<EOL>np.minimum(self.feature_range[<NUM_LIT:1>], X, out=X)<EOL><DEDENT>return X<EOL>

Scaling features of X according to feature_range.

        Parameters
        ----------
        X : array-like with shape [n_samples, n_features]
            Input data that will be transformed.


def quadratic(Ks, dim, rhos, required=None):

<EOL>N = rhos.shape[<NUM_LIT:0>]<EOL>Ks = np.asarray(Ks)<EOL>Bs = (Ks - <NUM_LIT:1>) / np.pi ** (dim / <NUM_LIT:2>) * gamma(dim / <NUM_LIT:2> + <NUM_LIT:1>)  <EOL>est = Bs / (N - <NUM_LIT:1>) * np.mean(rhos ** (-dim), axis=<NUM_LIT:0>)<EOL>return est<EOL>

r'''
    Estimates \int p^2 based on kNN distances.

    In here because it's used in the l2 distance, above.

    Returns array of shape (num_Ks,).


def alpha_div(alphas, Ks, dim, num_q, rhos, nus):

return _get_alpha_div(alphas, Ks, dim)(num_q, rhos, nus)<EOL>

r'''
    Estimate the alpha divergence between distributions:
        \int p^\alpha q^(1-\alpha)
    based on kNN distances.

    Used in Renyi, Hellinger, Bhattacharyya, Tsallis divergences.

    Enforces that estimates are >= 0.

    Returns divergence estimates with shape (num_alphas, num_Ks).


def renyi(alphas, Ks, dim, required, min_val=np.spacing(<NUM_LIT:1>),<EOL>clamp=True, to_self=False):

alphas = np.reshape(alphas, (-<NUM_LIT:1>, <NUM_LIT:1>))<EOL>est = required<EOL>est = np.maximum(est, min_val)  <EOL>np.log(est, out=est)<EOL>est /= alphas - <NUM_LIT:1><EOL>if clamp:<EOL><INDENT>np.maximum(est, <NUM_LIT:0>, out=est)<EOL><DEDENT>return est<EOL>

r'''
    Estimate the Renyi-alpha divergence between distributions, based on kNN
    distances:  1/(\alpha-1) \log \int p^alpha q^(1-\alpha)

    If the inner integral is less than min_val (default ``np.spacing(1)``),
    uses the log of min_val instead.

    If clamp (the default), enforces that the estimates are nonnegative by
    replacing any negative estimates with 0.

    Returns an array of shape (num_alphas, num_Ks).


def hellinger(Ks, dim, required, clamp=True, to_self=False):

bc = required<EOL>est = <NUM_LIT:1> - bc<EOL>np.maximum(est, <NUM_LIT:0>, out=est)<EOL>if clamp:<EOL><INDENT>np.minimum(est, <NUM_LIT:1>, out=est)<EOL><DEDENT>np.sqrt(est, out=est)<EOL>return est<EOL>

r'''
    Estimate the Hellinger distance between distributions, based on kNN
    distances:  \sqrt{1 - \int \sqrt{p q}}

    Always enforces 0 <= H, to be able to sqrt; if clamp, also enforces
    H <= 1.

    Returns a vector: one element for each K.


X = as_features(X, stack=True, bare=True)<EOL>Y = self.features_<EOL>Ks = np.asarray(self.Ks)<EOL>if X.dim != Y.dim:<EOL><INDENT>msg = "<STR_LIT>"<EOL>raise ValueError(msg.format(Y.dim, X.dim))<EOL><DEDENT>memory = self.memory<EOL>if isinstance(memory, string_types):<EOL><INDENT>memory = Memory(cachedir=memory, verbose=<NUM_LIT:0>)<EOL><DEDENT>est = memory.cache(_est_divs, ignore=['<STR_LIT>', '<STR_LIT>', '<STR_LIT>'])<EOL>output, self.rhos_ = est(<EOL>X, Y, self.indices_, getattr(self, '<STR_LIT>', None),<EOL>self.div_funcs, Ks,<EOL>self.do_sym, self.clamp, self.version, self.min_dist,<EOL>self._flann_args(), self._n_jobs)<EOL>return output<EOL>

r'''
        Computes the divergences from X to :attr:`features_`.

        Parameters
        ----------
        X : list of bag feature arrays or :class:`skl_groups.features.Features`
            The bags to search "from".

        Returns
        -------
        divs : array of shape ``[len(div_funcs), len(Ks), len(X), len(features_)] + ([2] if do_sym else [])``
            The divergences from X to :attr:`features_`.
            ``divs[d, k, i, j]`` is the ``div_funcs[d]`` divergence
            from ``X[i]`` to ``fetaures_[j]`` using a K of ``Ks[k]``.
            If ``do_sym``, ``divs[d, k, i, j, 0]`` is
            :math:`D_{d,k}( X_i \| \texttt{features_}_j)` and
            ``divs[d, k, i, j, 1]`` is :math:`D_{d,k}(\texttt{features_}_j \| X_i)`.


funcs = {}<EOL>metas = {}<EOL>meta_deps = defaultdict(set)<EOL>def add_func(func, alpha=None, pos=None):<EOL><INDENT>needs_alpha = getattr(func, '<STR_LIT>', False)<EOL>is_meta = hasattr(func, '<STR_LIT>')<EOL>d = metas if is_meta else funcs<EOL>if func not in d:<EOL><INDENT>if needs_alpha:<EOL><INDENT>args = {'<STR_LIT>': [alpha], '<STR_LIT>': [pos]}<EOL><DEDENT>else:<EOL><INDENT>args = {'<STR_LIT>': None, '<STR_LIT>': [pos]}<EOL><DEDENT>if not is_meta:<EOL><INDENT>d[func] = _FuncInfo(**args)<EOL><DEDENT>else:<EOL><INDENT>d[func] = _MetaFuncInfo(deps=[], **args)<EOL>for req in func.needs_results:<EOL><INDENT>if callable(req.alpha):<EOL><INDENT>req_alpha = req.alpha(alpha)<EOL><DEDENT>else:<EOL><INDENT>req_alpha = req.alpha<EOL><DEDENT>add_func(req.func, alpha=req_alpha)<EOL>meta_deps[func].add(req.func)<EOL>meta_deps[req.func]  <EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>info = d[func]<EOL>if not needs_alpha:<EOL><INDENT>if pos is not None:<EOL><INDENT>if info.pos != [None]:<EOL><INDENT>msg = "<STR_LIT>"<EOL>raise ValueError(msg.format(func_name))<EOL><DEDENT>info.pos[<NUM_LIT:0>] = pos<EOL><DEDENT><DEDENT>else:  <EOL><INDENT>try:<EOL><INDENT>idx = info.alphas.index(alpha)<EOL><DEDENT>except ValueError:<EOL><INDENT>info.alphas.append(alpha)<EOL>info.pos.append(pos)<EOL>if is_meta:<EOL><INDENT>for req in func.needs_results:<EOL><INDENT>if callable(req.alpha):<EOL><INDENT>req_alpha = req.alpha(alpha)<EOL><DEDENT>else:<EOL><INDENT>req_alpha = req.alpha<EOL><DEDENT>add_func(req.func, alpha=req_alpha)<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>if pos is not None:<EOL><INDENT>if info.pos[idx] is not None:<EOL><INDENT>msg = "<STR_LIT>"<EOL>raise ValueError(msg.format(func_name, alpha))<EOL><DEDENT>info.pos[idx] = pos<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>for i, spec in enumerate(specs):<EOL><INDENT>func_name, alpha = (spec.split('<STR_LIT::>', <NUM_LIT:1>) + [None])[:<NUM_LIT:2>]<EOL>if alpha is not None:<EOL><INDENT>alpha = float(alpha)<EOL><DEDENT>try:<EOL><INDENT>func = func_mapping[func_name]<EOL><DEDENT>except KeyError:<EOL><INDENT>msg = "<STR_LIT>"<EOL>raise ValueError(msg.format(func_name))<EOL><DEDENT>needs_alpha = getattr(func, '<STR_LIT>', False)<EOL>if needs_alpha and alpha is None:<EOL><INDENT>msg = "<STR_LIT>"<EOL>raise ValueError(msg.format(func_name, spec))<EOL><DEDENT>elif not needs_alpha and alpha is not None:<EOL><INDENT>msg = "<STR_LIT>"<EOL>raise ValueError(msg.format(func_name, spec))<EOL><DEDENT>add_func(func, alpha, i)<EOL><DEDENT>meta_counter = itertools.count(-<NUM_LIT:1>, step=-<NUM_LIT:1>)<EOL>for info in itertools.chain(itervalues(funcs), itervalues(metas)):<EOL><INDENT>for i, pos in enumerate(info.pos):<EOL><INDENT>if pos is None:<EOL><INDENT>info.pos[i] = next(meta_counter)<EOL><DEDENT><DEDENT><DEDENT>for func, info in iteritems(metas):<EOL><INDENT>deps = info.deps<EOL>assert deps == []<EOL>for req in func.needs_results:<EOL><INDENT>f = req.func<EOL>req_info = (metas if hasattr(f, '<STR_LIT>') else funcs)[f]<EOL>if req.alpha is not None:<EOL><INDENT>if callable(req.alpha):<EOL><INDENT>req_alpha = req.alpha(info.alphas)<EOL><DEDENT>else:<EOL><INDENT>req_alpha = req.alpha<EOL><DEDENT>find_alpha = np.vectorize(req_info.alphas.index, otypes=[int])<EOL>pos = np.asarray(req_info.pos)[find_alpha(req_alpha)]<EOL>if np.isscalar(pos):<EOL><INDENT>deps.append(pos[()])<EOL><DEDENT>else:<EOL><INDENT>deps.extend(pos)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>pos, = req_info.pos<EOL>deps.append(pos)<EOL><DEDENT><DEDENT><DEDENT>meta_order = topological_sort(meta_deps)<EOL>metas_ordered = OrderedDict(<EOL>(f, metas[f]) for f in meta_order if hasattr(f, '<STR_LIT>'))<EOL>return funcs, metas_ordered, -next(meta_counter) - <NUM_LIT:1><EOL>

Set up the different functions we need to call.

Returns:
    - a dict mapping base estimator functions to _FuncInfo objects.
      If the function needs_alpha, then the alphas attribute is an array
      of alpha values and pos is a corresponding array of indices.
      Otherwise, alphas is None and pos is a list containing a single index.
      Indices are >= 0 if they correspond to something in a spec,
      and negative if they're just used for a meta estimator but not
      directly requested.
    - an OrderedDict mapping functions to _MetaFuncInfo objects.
      alphas and pos are like for _FuncInfo; deps is a list of indices
      which should be passed to the estimator. Note that these might be
      other meta functions; this list is guaranteed to be in an order
      such that all dependencies are resolved before calling that function.
      If no such order is possible, raise ValueError.
    - the number of meta-only results

# TODO: update doctests for _parse_specs

>>> _parse_specs(['renyi:.8', 'hellinger', 'renyi:.9'])
({<function alpha_div at 0x10954f848>:
        _FuncInfo(alphas=[0.8, 0.5, 0.9], pos=[-1, -2, -3])},
 OrderedDict([
    (<function hellinger at 0x10954fc80>,
        _MetaFuncInfo(alphas=None, pos=[1], deps=[array(-2)])),
    (<function renyi at 0x10954fcf8>,
        _MetaFuncInfo(alphas=[0.8, 0.9], pos=[0, 2], deps=[-1, -3]))
 ]), 3)

>>> _parse_specs(['renyi:.8', 'hellinger', 'renyi:.9', 'l2'])
({<function alpha_div at 0x10954f848>:
    _FuncInfo(alphas=[0.8, 0.5, 0.9], pos=[-1, -2, -3]),
  <function linear at 0x10954f758>: _FuncInfo(alphas=None, pos=[-4])
 }, OrderedDict([
    (<function hellinger at 0x10954fc80>,
        _MetaFuncInfo(alphas=None, pos=[1], deps=[array(-2)])),
    (<function l2 at 0x10954fde8>,
        _MetaFuncInfo(alphas=None, pos=[3], deps=[-4])),
    (<function renyi at 0x10954fcf8>,
        _MetaFuncInfo(alphas=[0.8, 0.9], pos=[0, 2], deps=[-1, -3]))
 ]), 4)

>>> _parse_specs(['renyi:.8', 'hellinger', 'renyi:.9', 'l2', 'linear'])
({<function alpha_div at 0x10954f848>:
    _FuncInfo(alphas=[0.8, 0.5, 0.9], pos=[-1, -2, -3]),
  <function linear at 0x10954f758>: _FuncInfo(alphas=None, pos=[4])
 }, OrderedDict([
    (<function hellinger at 0x10954fc80>,
        _MetaFuncInfo(alphas=None, pos=[1], deps=[array(-2)])),
    (<function l2 at 0x10954fde8>,
        _MetaFuncInfo(alphas=None, pos=[3], deps=[4])),
    (<function renyi at 0x10954fcf8>,
        _MetaFuncInfo(alphas=[0.8, 0.9], pos=[0, 2], deps=[-1, -3]))
 ]), 3)


def l2(Ks, dim, X_rhos, Y_rhos, required, clamp=True, to_self=False):

n_X = len(X_rhos)<EOL>n_Y = len(Y_rhos)<EOL>linears = required<EOL>assert linears.shape == (<NUM_LIT:1>, Ks.size, n_X, n_Y, <NUM_LIT:2>)<EOL>X_quadratics = np.empty((Ks.size, n_X), dtype=np.float32)<EOL>for i, rho in enumerate(X_rhos):<EOL><INDENT>X_quadratics[:, i] = quadratic(Ks, dim, rho)<EOL><DEDENT>Y_quadratics = np.empty((Ks.size, n_Y), dtype=np.float32)<EOL>for j, rho in enumerate(Y_rhos):<EOL><INDENT>Y_quadratics[:, j] = quadratic(Ks, dim, rho)<EOL><DEDENT>est = -linears.sum(axis=<NUM_LIT:4>)<EOL>est += X_quadratics[None, :, :, None]<EOL>est += Y_quadratics[None, :, None, :]<EOL>np.maximum(est, <NUM_LIT:0>, out=est)<EOL>np.sqrt(est, out=est)<EOL>if to_self:<EOL><INDENT>est[:, :, xrange(n_X), xrange(n_Y)] = <NUM_LIT:0><EOL><DEDENT>return est[:, :, :, :, None]<EOL>

r'''
    Estimates the L2 distance between distributions, via
        \int (p - q)^2 = \int p^2 - \int p q - \int q p + \int q^2.

    \int pq and \int qp are estimated with the linear function (in both
    directions), while \int p^2 and \int q^2 are estimated via the quadratic
    function below.

    Always clamps negative estimates of l2^2 to 0, because otherwise the sqrt
    would break.


def jensen_shannon(Ks, dim, X_rhos, Y_rhos, required,<EOL>clamp=True, to_self=False):

X_ns = np.array([rho.shape[<NUM_LIT:0>] for rho in X_rhos])<EOL>Y_ns = np.array([rho.shape[<NUM_LIT:0>] for rho in Y_rhos])<EOL>n_X = X_ns.size<EOL>n_Y = Y_ns.size<EOL>cores = required<EOL>assert cores.shape == (<NUM_LIT:1>, Ks.size, n_X, n_Y, <NUM_LIT:2>)<EOL>X_bits = np.empty((Ks.size, n_X), dtype=np.float32)<EOL>for i, rho in enumerate(X_rhos):<EOL><INDENT>X_bits[:, i] = dim * np.mean(np.log(rho), axis=<NUM_LIT:0>)<EOL><DEDENT>X_bits += np.log(X_ns - <NUM_LIT:1>)[np.newaxis, :]<EOL>Y_bits = np.empty((Ks.size, n_Y), dtype=np.float32)<EOL>for j, rho in enumerate(Y_rhos):<EOL><INDENT>Y_bits[:, j] = dim * np.mean(np.log(rho), axis=<NUM_LIT:0>)<EOL><DEDENT>Y_bits += np.log(Y_ns - <NUM_LIT:1>)[np.newaxis, :]<EOL>est = cores.sum(axis=<NUM_LIT:4>)<EOL>est -= X_bits.reshape(<NUM_LIT:1>, Ks.size, n_X, <NUM_LIT:1>)<EOL>est -= Y_bits.reshape(<NUM_LIT:1>, Ks.size, <NUM_LIT:1>, n_Y)<EOL>est /= <NUM_LIT:2><EOL>est += np.log(-<NUM_LIT:1> + X_ns[None, None, :, None] + Y_ns[None, None, None, :])<EOL>est += psi(Ks)[None, :, None, None]<EOL>if to_self:<EOL><INDENT>est[:, :, xrange(n_X), xrange(n_Y)] = <NUM_LIT:0><EOL><DEDENT>if clamp:  <EOL><INDENT>np.maximum(<NUM_LIT:0>, est, out=est)<EOL>np.minimum(np.log(<NUM_LIT:2>), est, out=est)<EOL><DEDENT>return est[:, :, :, :, None]<EOL>

r'''
    Estimate the difference between the Shannon entropy of an equally-weighted
    mixture between X and Y and the mixture of the Shannon entropies:

        JS(X, Y) = H[ (X + Y) / 2 ] - (H[X] + H[Y]) / 2

    We use a special case of the Hino-Murata weighted information estimator with
    a fixed M = n \alpha, about equivalent to the K-nearest-neighbor approach
    used for the other estimators:

        Hideitsu Hino and Noboru Murata (2013).
        Information estimators for weighted observations. Neural Networks.
        http://linkinghub.elsevier.com/retrieve/pii/S0893608013001676

    The estimator for JS(X, Y) is:

        log volume of the unit ball - log M + log(n + m - 1) + digamma(M)
        + 1/2 mean_X( d * log radius of largest ball in X+Y around X_i
                                with no more than M/(n+m-1) weight
                                where X points have weight 1 / (2 n - 1)
                                  and Y points have weight n / (m (2 n - 1))
                      - digamma(# of neighbors in that ball) )
        + 1/2 mean_Y( d * log radius of largest ball in X+Y around Y_i
                                with no more than M/(n+m-1) weight
                                where X points have weight m / (n (2 m - 1))
                                  and Y points have weight 1 / (2 m - 1)
                      - digamma(# of neighbors in that ball) )

        - 1/2 (log volume of the unit ball - log M + log(n - 1) + digamma(M))
        - 1/2 mean_X( d * log radius of the largest ball in X around X_i
                                with no more than M/(n-1) weight
                                where X points have weight 1 / (n - 1))
                      - digamma(# of neighbors in that ball) )

        - 1/2 (log volume of the unit ball - log M + log(m - 1) + digamma(M))
        - 1/2 mean_Y( d * log radius of the largest ball in Y around Y_i
                                with no more than M/(n-1) weight
                                where X points have weight 1 / (m - 1))
                      - digamma(# of neighbors in that ball) )

        =

        log(n + m - 1) + digamma(M)
        + 1/2 mean_X( d * log radius of largest ball in X+Y around X_i
                                with no more than M/(n+m-1) weight
                                where X points have weight 1 / (2 n - 1)
                                  and Y points have weight n / (m (2 n - 1))
                      - digamma(# of neighbors in that ball) )
        + 1/2 mean_Y( d * log radius of largest ball in X+Y around Y_i
                                with no more than M/(n+m-1) weight
                                where X points have weight m / (n (2 m - 1))
                                  and Y points have weight 1 / (2 m - 1)
                      - digamma(# of neighbors in that ball) )
        - 1/2 [log(n-1) + mean_X( d * log rho_M(X_i) )]
        - 1/2 [log(m-1) + mean_Y( d * log rho_M(Y_i) )]


return _get_linear(Ks, dim)(num_q, rhos, nus)<EOL>

r'''
    Estimates the linear inner product \int p q between two distributions,
    based on kNN distances.


def jensen_shannon_core(Ks, dim, num_q, rhos, nus):

ns = np.array([rhos.shape[<NUM_LIT:0>], num_q])<EOL>return _get_jensen_shannon_core(Ks, dim, ns)[<NUM_LIT:0>](num_q, rhos, nus)<EOL>

r'''
    Estimates
          1/2 mean_X( d * log radius of largest ball in X+Y around X_i
                                with no more than M/(n+m-1) weight
                                where X points have weight 1 / (2 n - 1)
                                  and Y points have weight n / (m (2 n - 1))
                      - digamma(# of neighbors in that ball))

    This is the core pairwise component of the estimator of Jensen-Shannon
    divergence based on the Hino-Murata weighted information estimator. See
    the docstring for jensen_shannon for an explanation.


self._check_fitted()<EOL>return self.inds_.shape[<NUM_LIT:1>]<EOL>

The dimension of the inputs, once fitted.


return self<EOL>

Do nothing; this is a stateless transformer.


X = as_features(X)<EOL>return np.vstack([np.mean(bag, axis=<NUM_LIT:0>) for bag in X])<EOL>

Transform a list of bag features into a matrix of its mean features.

Parameters
----------
X : :class:`skl_groups.features.Features` or list of bag feature arrays
    Data to transform.

Returns
-------
X_new : array, shape ``[len(X), X.dim]``
    X transformed into its means.


self._check_fitted()<EOL>X = as_features(X, stack=True)<EOL>assignments = self.kmeans_fit_.predict(X.stacked_features)<EOL>return self._group_assignments(X, assignments)<EOL>

Transform a list of bag features into its bag-of-words representation.

Parameters
----------
X : :class:`skl_groups.features.Features` or list of bag feature arrays
    New data to transform.

Returns
-------
X_new : integer array, shape [len(X), kmeans.n_clusters]
    X transformed into the new space.


X = as_features(X, stack=True)<EOL>self.kmeans_fit_ = copy(self.kmeans)<EOL>assignments = self.kmeans_fit_.fit_predict(X.stacked_features) <EOL>return self._group_assignments(X, assignments)<EOL>

Compute clustering and transform a list of bag features into its
bag-of-words representation. Like calling fit(X) and then transform(X),
but more efficient.

Parameters
----------
X : :class:`skl_groups.features.Features` or list of bag feature arrays
    New data to transform.

Returns
-------
X_new : integer array, shape [len(X), kmeans.n_clusters]
    X transformed into the new space.


self.kmeans_fit_ = copy(self.kmeans)<EOL>X = as_features(X, stack=True)<EOL>self.kmeans_fit_.fit(X.stacked_features) <EOL>return self<EOL>

Choose the codewords based on a training set.

Parameters
----------
X : :class:`skl_groups.features.Features` or list of arrays of shape ``[n_samples[i], n_features]``
    Training set. If a Features object, it will be stacked.


self._check_fitted()<EOL>return self.kmeans_fit_.cluster_centers_<EOL>

The selected codewords; shape [n_codewords, n_features].


<EOL>self.logger.info('<STR_LIT>'.format(idx))<EOL>

Update the current state.

Parameters
----------
idx : int
    The current state through the process.


return np.isscalar(x) and is_integer_type(x)<EOL>

Checks whether the argument is a single integer.


self.logger.info(json.dumps(['<STR_LIT>']))<EOL>


return np.isscalar(x) and is_categorical_type(x)<EOL>

Checks whether the argument is a single integer or boolean.


return self.features[<NUM_LIT:0>].dtype<EOL>


if not self.meta:<EOL><INDENT>return self<EOL><DEDENT>elif self.stacked:<EOL><INDENT>return Features(self.stacked_features, self.n_pts, copy=False)<EOL><DEDENT>else:<EOL><INDENT>return Features(self.features, copy=False)<EOL><DEDENT>

Make a Features object with no metadata; points to the same features.


if self.stacked:<EOL><INDENT>return<EOL><DEDENT>self._boundaries = bounds = np.r_[<NUM_LIT:0>, np.cumsum(self.n_pts)]<EOL>self.stacked_features = stacked = np.vstack(self.features)<EOL>self.features = np.array(<EOL>[stacked[bounds[i-<NUM_LIT:1>]:bounds[i]] for i in xrange(<NUM_LIT:1>, len(bounds))],<EOL>dtype=object)<EOL>self.stacked = True<EOL>

If unstacked, convert to stacked. If stacked, do nothing.


for f, sha1 in files:<EOL><INDENT>yield "<STR_LIT>".format(sha1, f)<EOL><DEDENT>for d, sha1 in dirs:<EOL><INDENT>yield "<STR_LIT>".format(sha1, d)<EOL><DEDENT>


dir_hash = {}<EOL>for root, dirs, files in os.walk(path, topdown=False):<EOL><INDENT>f_hash = ((f, hash_file(join(root, f))) for f in files)<EOL>d_hash = ((d, dir_hash[join(root, d)]) for d in dirs)<EOL>dir_hash[join(*split(root))] = _mktree(f_hash, d_hash)<EOL><DEDENT>return dir_hash[path]<EOL>

Write directory at path to Git index, return its SHA1 as a string.


return lorem_ipsum.title(words_quantity=words_quantity)<EOL>

An alias for lorem_ipsum.title(words_quantity)


result = first_name()<EOL>if with_num:<EOL><INDENT>result += str(random.randint(<NUM_LIT>, <NUM_LIT>))<EOL><DEDENT>return result.lower()<EOL>

Return a random user name.

    Basically it's lowercased result of
    :py:func:`~forgery_py.forgery.name.first_name()` with a number appended
    if `with_num`.


result = random.choice(get_dictionary('<STR_LIT>')).strip()<EOL>result += '<STR_LIT:.>' + top_level_domain()<EOL>return result.lower()<EOL>

Return a random domain name.

    Lowercased result of :py:func:`~forgery_py.forgery.name.company_name()`
    plus :py:func:`~top_level_domain()`.


<EOL>magnitude = <NUM_LIT:16> ** <NUM_LIT:4><EOL>return "<STR_LIT::>".join(("<STR_LIT>" % random.randint(<NUM_LIT:0>, magnitude) for _ in range(<NUM_LIT:8>)))<EOL>

Return a random IPv6 address for Python versions prior to 3.3.


result = words(quantity=words_quantity)<EOL>result += random.choice('<STR_LIT>')<EOL>return result.capitalize()<EOL>

Return a random sentence to be used as e.g. an e-mail subject.


global _words<EOL>if not _words:<EOL><INDENT>_words = '<STR_LIT:U+0020>'.join(get_dictionary('<STR_LIT>')).lower().replace('<STR_LIT:\n>', '<STR_LIT>')<EOL>_words = re.sub(r'<STR_LIT>', '<STR_LIT>', _words)<EOL>_words = _words.split('<STR_LIT:U+0020>')<EOL><DEDENT>result = random.sample(_words, quantity)<EOL>if as_list:<EOL><INDENT>return result<EOL><DEDENT>else:<EOL><INDENT>return '<STR_LIT:U+0020>'.join(result)<EOL><DEDENT>


s = re.sub(r'<STR_LIT:\n>', '<STR_LIT:U+0020>',  s.lower())<EOL>return re.sub(r'<STR_LIT>', '<STR_LIT>', s)<EOL>

Return a lowercased string with non alphabetic chars removed.

    White spaces are not to be removed.


def paragraphs(quantity=<NUM_LIT:2>, separator='<STR_LIT>', wrap_start='<STR_LIT>', wrap_end='<STR_LIT>',<EOL>html=False, sentences_quantity=<NUM_LIT:3>, as_list=False):

if html:<EOL><INDENT>wrap_start = '<STR_LIT>'<EOL>wrap_end = '<STR_LIT>'<EOL>separator = '<STR_LIT>'<EOL><DEDENT>result = []<EOL>try:<EOL><INDENT>for _ in xrange(<NUM_LIT:0>, quantity):<EOL><INDENT>result.append(wrap_start +<EOL>sentences(sentences_quantity) +<EOL>wrap_end)<EOL><DEDENT><DEDENT>except NameError:<EOL><INDENT>for _ in range(<NUM_LIT:0>, quantity):<EOL><INDENT>result.append(wrap_start +<EOL>sentences(sentences_quantity) +<EOL>wrap_end)<EOL><DEDENT><DEDENT>if as_list:<EOL><INDENT>return result<EOL><DEDENT>else:<EOL><INDENT>return separator.join(result)<EOL><DEDENT>


def paragraph(separator='<STR_LIT>', wrap_start='<STR_LIT>', wrap_end='<STR_LIT>',<EOL>html=False, sentences_quantity=<NUM_LIT:3>):

return paragraphs(quantity=<NUM_LIT:1>, separator=separator, wrap_start=wrap_start,<EOL>wrap_end=wrap_end, html=html,<EOL>sentences_quantity=sentences_quantity)<EOL>


return characters(quantity=<NUM_LIT:1>)<EOL>


return random.choice(get_dictionary('<STR_LIT>')).strip()<EOL>

Return a random location name, e.g. ``MI6 Headquarters``.


result = random.choice(get_dictionary('<STR_LIT>')).strip()<EOL>result = result.replace('<STR_LIT>', job_title_suffix())<EOL>return result<EOL>


Return a random name suffix, e.g. ``Jr``.


return random.randint(<NUM_LIT:1>, month_length)<EOL>

Return a random day number in a `month_length` days long month.


def datetime(past=False, min_delta=<NUM_LIT:0>, max_delta=<NUM_LIT:20>):

timedelta = dt.timedelta(days=_delta(past, min_delta, max_delta))<EOL>return dt.datetime.today() + timedelta<EOL>

Return a random `dt.dt` object. Delta args are days.


def year(past=False, min_delta=<NUM_LIT:0>, max_delta=<NUM_LIT:20>):

return dt.date.today().year + _delta(past, min_delta, max_delta)<EOL>


return "<STR_LIT>" % float(money(min=min, max=max))<EOL>

Return a random sum of money with a dollar sign as a prefix.


value = random.choice(range(min * <NUM_LIT:100>, max * <NUM_LIT:100>))<EOL>return "<STR_LIT>" % (float(value) / <NUM_LIT:100>)<EOL>

Return a str of decimal with two digits after a decimal mark.


return random.randint(<NUM_LIT:0>, <NUM_LIT>) - <NUM_LIT><EOL>

Return a random latitude's degrees component in the range [-180, +180].

    Latitude's degree is an int.


return latitude_minutes()<EOL>

Return a random longitude's minutes component in the range [0, 60).

    longitude's minutes is an int.


return random.choice(['<STR_LIT:N>', '<STR_LIT:S>'])<EOL>

Return a random a latitude's direction component.

    Latitude's direction is denoted as either "N" (north) or "S" (south).


return random.uniform(<NUM_LIT:0.0>, <NUM_LIT:1.0>) * <NUM_LIT> - <NUM_LIT><EOL>

Return a random latitude in the range of [-90.0, +90.0].

    Latitude is a float.


return random.choice(['<STR_LIT:E>', '<STR_LIT>'])<EOL>

Return a random longitude's direction component.

    Longitude's direction is denoted as either "E" (east) or "W" (west).


Return random frequency rate.

    Frequency rate is taken from the `frequencies` dictionary.


if not salt:<EOL><INDENT>salt = str(datetime.utcnow())<EOL><DEDENT>try:<EOL><INDENT>dk = hashlib.pbkdf2_hmac('<STR_LIT>', password.encode(), salt.encode(), <NUM_LIT>)<EOL>hexdigest = binascii.hexlify(dk).decode('<STR_LIT:utf-8>')<EOL><DEDENT>except AttributeError:<EOL><INDENT>dk = hashlib.sha1()<EOL>dk.update(password.encode() + salt.encode())<EOL>hexdigest = dk.hexdigest()<EOL><DEDENT>return hexdigest<EOL>

Return SHA1 hexdigest of a password (optionally salted with a string).


return '<STR_LIT>'.join(random.sample(HEX_DIGITS, <NUM_LIT:6>))<EOL>


return '<STR_LIT>'.join(random.sample(HEX_DIGITS, <NUM_LIT:3>))<EOL>

Return random short HEX color (e.g. `FFF` color).


def password(at_least=<NUM_LIT:6>, at_most=<NUM_LIT:12>, lowercase=True,<EOL>uppercase=True, digits=True, spaces=False, punctuation=False):

return text(at_least=at_least, at_most=at_most, lowercase=lowercase,<EOL>uppercase=uppercase, digits=digits, spaces=spaces,<EOL>punctuation=punctuation)<EOL>

Return a random string for use as a password.


return random.choice(list(CARDS.keys()))<EOL>


ogrn = "<STR_LIT>".join(map(str, [random.randint(<NUM_LIT:1>, <NUM_LIT:9>) for _ in range(<NUM_LIT>)]))<EOL>ogrn += str((int(ogrn) % <NUM_LIT> % <NUM_LIT:10>))<EOL>return ogrn<EOL>

Return a random government registration ID for a person.


mask11 = [<NUM_LIT:7>, <NUM_LIT:2>, <NUM_LIT:4>, <NUM_LIT:10>, <NUM_LIT:3>, <NUM_LIT:5>, <NUM_LIT:9>, <NUM_LIT:4>, <NUM_LIT:6>, <NUM_LIT:8>]<EOL>mask12 = [<NUM_LIT:3>, <NUM_LIT:7>, <NUM_LIT:2>, <NUM_LIT:4>, <NUM_LIT:10>, <NUM_LIT:3>, <NUM_LIT:5>, <NUM_LIT:9>, <NUM_LIT:4>, <NUM_LIT:6>, <NUM_LIT:8>]<EOL>inn = [random.randint(<NUM_LIT:1>, <NUM_LIT:9>) for _ in range(<NUM_LIT:12>)]<EOL>weighted11 = [v * mask11[i] for i, v in enumerate(inn[:-<NUM_LIT:2>])]<EOL>inn[<NUM_LIT:10>] = sum(weighted11) % <NUM_LIT:11> % <NUM_LIT:10><EOL>weighted12 = [v * mask12[i] for i, v in enumerate(inn[:-<NUM_LIT:1>])]<EOL>inn[<NUM_LIT:11>] = sum(weighted12) % <NUM_LIT:11> % <NUM_LIT:10><EOL>return "<STR_LIT>".join(map(str, inn))<EOL>

Return a random taxation ID number for a natural person.


if (type in TYPES) and type == '<STR_LIT>':<EOL><INDENT>return person_ogrn()<EOL><DEDENT>else:<EOL><INDENT>return legal_ogrn()<EOL><DEDENT>

Return a random government registration ID for either a person or a company.

    Further information on the topic can be found in [1] (in russian).
    [1]: https://ru.wikipedia.org/wiki/Основной_государственный_регистрационный_номер


if (type in TYPES) and type == '<STR_LIT>':<EOL><INDENT>return person_inn()<EOL><DEDENT>else:<EOL><INDENT>return legal_inn()<EOL><DEDENT>

Return a random taxation ID number for either a person or a company.

    Further information on the topic can be found in [1] (in russian).
    [1]: https://ru.wikipedia.org/wiki/Идентификационный_номер_налогоплательщика


mask = [<NUM_LIT:2>, <NUM_LIT:4>, <NUM_LIT:10>, <NUM_LIT:3>, <NUM_LIT:5>, <NUM_LIT:9>, <NUM_LIT:4>, <NUM_LIT:6>, <NUM_LIT:8>]<EOL>inn = [random.randint(<NUM_LIT:1>, <NUM_LIT:9>) for _ in range(<NUM_LIT:10>)]<EOL>weighted = [v * mask[i] for i, v in enumerate(inn[:-<NUM_LIT:1>])]<EOL>inn[<NUM_LIT:9>] = sum(weighted) % <NUM_LIT:11> % <NUM_LIT:10><EOL>return "<STR_LIT>".join(map(str, inn))<EOL>

Return a random taxation ID number for a company.


return internet.email_address(user=user)<EOL>

An alias for internet.email_address(user).


return lorem_ipsum.title(words_quantity=words_quantity)<EOL>

An alias for lorem_ipsum.title(words_quantity)


def body(quantity=<NUM_LIT:2>, separator='<STR_LIT>', wrap_start='<STR_LIT>', wrap_end='<STR_LIT>',<EOL>html=False, sentences_quantity=<NUM_LIT:3>, as_list=False):

return lorem_ipsum.paragraphs(quantity=quantity, separator=separator,<EOL>wrap_start=wrap_start, wrap_end=wrap_end,<EOL>html=html,<EOL>sentences_quantity=sentences_quantity,<EOL>as_list=as_list)<EOL>


return random.choice(get_dictionary('<STR_LIT>')).strip()<EOL>

Return a random language name, e.g. ``Polish``.


Return a random US abbreviated state name.


return '<STR_LIT>' % (street_number(), street_name(), street_suffix())<EOL>

Return a random street address.

    Equivalent of ``street_number() + ' ' +
    street_name() + ' ' + street_suffix()``.


Return random Canadian province or territory abbreviation.


return [array.tolist() for array in iterable_of_arrays]<EOL>

Converts an iterable of permutation matrices given as NumPy
    arrays into a list of lists.


print('<STR_LIT>'.format(tag))<EOL>msg = '<STR_LIT>'.format(tag)<EOL>Popen(['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', msg, tag]).wait()<EOL>


process = Popen(['<STR_LIT>', '<STR_LIT>'], stdout=PIPE)<EOL>return set(process.communicate()[<NUM_LIT:0>].splitlines())<EOL>


with open(filename) as f:<EOL><INDENT>match = re.search(r"<STR_LIT>" % pattern, f.read())<EOL><DEDENT>if match:<EOL><INDENT>before, version, after = match.groups()<EOL>return version<EOL><DEDENT>fail('<STR_LIT>'.format(pattern, filename))<EOL>

Gets the current version from the specified file.

    This function assumes the file includes a string of the form::

        <pattern> = <version>


return Popen(['<STR_LIT>', '<STR_LIT>', '<STR_LIT>']).wait() == <NUM_LIT:0><EOL>

Returns ``True`` if and only if there are no uncommitted changes.


try:<EOL><INDENT>parts = [int(n) for n in version.split('<STR_LIT:.>')]<EOL><DEDENT>except ValueError:<EOL><INDENT>fail('<STR_LIT>')<EOL><DEDENT>if len(parts) != <NUM_LIT:3>:<EOL><INDENT>fail('<STR_LIT>')<EOL><DEDENT>PARTS = {'<STR_LIT>': <NUM_LIT:0>, '<STR_LIT>': <NUM_LIT:1>, '<STR_LIT>': <NUM_LIT:2>}<EOL>index = PARTS[which] if which in PARTS else <NUM_LIT:2><EOL>before, middle, after = parts[:index], parts[index], parts[index + <NUM_LIT:1>:]<EOL>middle += <NUM_LIT:1><EOL>return '<STR_LIT:.>'.join(str(n) for n in before + [middle] + after)<EOL>

Returns the result of incrementing `version`.

    If `which` is not specified, the "patch" part of the version number will be
    incremented.  If `which` is specified, it must be ``'major'``, ``'minor'``,
    or ``'patch'``. If it is one of these three strings, the corresponding part
    of the version number will be incremented instead of the patch number.

    Returns a string representing the next version number.

    Example::

        >>> bump_version('2.7.1')
        '2.7.2'
        >>> bump_version('2.7.1', 'minor')
        '2.8.0'
        >>> bump_version('2.7.1', 'major')
        '3.0.0'


result = np.zeros_like(D)<EOL>result[D.nonzero()] = <NUM_LIT:1><EOL>return result<EOL>

Returns the Boolean matrix in the same shape as `D` with ones exactly
    where there are nonzero entries in `D`.

    `D` must be a NumPy array.


return np.hstack((left, right))<EOL>

Convenience function for ``numpy.hstack((left, right))``.


m, n = D.shape<EOL>if m != n:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(m, n))<EOL><DEDENT>indices = list(itertools.product(range(m), range(n)))<EOL>coefficients = []<EOL>permutations = []<EOL>S = D.astype('<STR_LIT:float>')<EOL>while not np.all(S == <NUM_LIT:0>):<EOL><INDENT>W = to_pattern_matrix(S)<EOL>X = to_bipartite_matrix(W)<EOL>G = from_numpy_matrix(X)<EOL>left_nodes = range(n)<EOL>M = maximum_matching(G, left_nodes)<EOL>M = {u: v % n for u, v in M.items() if u < n}<EOL>P = to_permutation_matrix(M)<EOL>q = min(S[i, j] for (i, j) in indices if P[i, j] == <NUM_LIT:1>)<EOL>coefficients.append(q)<EOL>permutations.append(P)<EOL>S -= q * P<EOL>S[np.abs(S) < TOLERANCE] = <NUM_LIT:0.0><EOL><DEDENT>return list(zip(coefficients, permutations))<EOL>

Returns the Birkhoff--von Neumann decomposition of the doubly
    stochastic matrix `D`.

    The input `D` must be a square NumPy array representing a doubly
    stochastic matrix (that is, a matrix whose entries are nonnegative
    reals and whose row sums and column sums are all 1). Each doubly
    stochastic matrix is a convex combination of at most ``n ** 2``
    permutation matrices, where ``n`` is the dimension of the input
    array.

    The returned value is a list of pairs whose length is at most ``n **
    2``. In each pair, the first element is a real number in the interval **(0,
    1]** and the second element is a NumPy array representing a permutation
    matrix. This represents the doubly stochastic matrix as a convex
    combination of the permutation matrices.

    The input matrix may also be a scalar multiple of a doubly
    stochastic matrix, in which case the row sums and column sums must
    each be *c*, for some positive real number *c*. This may be useful
    in avoiding precision issues: given a doubly stochastic matrix that
    will have many entries close to one, multiply it by a large positive
    integer. The returned permutation matrices will be the same
    regardless of whether the given matrix is a doubly stochastic matrix
    or a scalar multiple of a doubly stochastic matrix, but in the
    latter case, the coefficients will all be scaled by the appropriate
    scalar multiple, and their sum will be that scalar instead of one.

    For example::

        >>> import numpy as np
        >>> from birkhoff import birkhoff_von_neumann_decomposition as decomp
        >>> D = np.ones((2, 2))
        >>> zipped_pairs = decomp(D)
        >>> coefficients, permutations = zip(*zipped_pairs)
        >>> coefficients
        (1.0, 1.0)
        >>> permutations[0]
        array([[ 1.,  0.],
               [ 0.,  1.]])
        >>> permutations[1]
        array([[ 0.,  1.],
               [ 1.,  0.]])
        >>> zipped_pairs = decomp(D / 2)  # halve each value in the matrix
        >>> coefficients, permutations = zip(*zipped_pairs)
        >>> coefficients  # will be half as large as before
        (0.5, 0.5)
        >>> permutations[0]  # will be the same as before
        array([[ 1.,  0.],
               [ 0.,  1.]])
        >>> permutations[1]
        array([[ 0.,  1.],
               [ 1.,  0.]])

    The returned list of pairs is given in the order computed by the algorithm
    (so in particular they are not sorted in any way).


return np.vstack((top, bottom))<EOL>

Convenience function for ``numpy.vstack((top, bottom))``.


if HAVECONF:<EOL><INDENT>self.open(DBDATA, DBUSER, DBPASS, DBHOST)<EOL><DEDENT>else:<EOL><INDENT>LOGERROR("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>

This opens the database connection using the default database parameters
given in the ~/.astrobase/astrobase.conf file.


if len(self.cursors.keys()) == <NUM_LIT:0>:<EOL><INDENT>self.connection.autocommit = True<EOL><DEDENT>else:<EOL><INDENT>raise AttributeError('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>

This sets the database connection to autocommit. Must be called before
any cursors have been instantiated.


self.connection = None<EOL>self.user = None<EOL>self.database = None<EOL>self.host = None<EOL>self.cursors = {}<EOL>if database and user and password and host:<EOL><INDENT>self.open(database, user, password, host)<EOL><DEDENT>

Constructor for this class.

        Parameters
        ----------

        database : str
            Name of the database to connect to.

        user : str
            User name of the database server user.

        password : str
            Password for the database server user.

        host : str
            Database hostname or IP address to connect to.

        Returns
        -------

        `LCDB` object instance


if not self.connection.closed:<EOL><INDENT>self.connection.rollback()<EOL><DEDENT>else:<EOL><INDENT>raise AttributeError('<STR_LIT>' %<EOL>self.database)<EOL><DEDENT>

This just calls the connection's commit method.


def log_posterior_transit(theta, params, model, t, flux, err_flux, priorbounds):

lp = _log_prior_transit(theta, priorbounds)<EOL>if not np.isfinite(lp):<EOL><INDENT>return -np.inf<EOL><DEDENT>else:<EOL><INDENT>return lp + _log_likelihood_transit(theta, params, model, t, flux,<EOL>err_flux, priorbounds)<EOL><DEDENT>

Evaluate posterior probability given proposed model parameters and
the observed flux timeseries.


def _log_likelihood_transit(theta, params, model, t, flux, err_flux,<EOL>priorbounds):

u = []<EOL>for ix, key in enumerate(sorted(priorbounds.keys())):<EOL><INDENT>if key == '<STR_LIT>':<EOL><INDENT>params.rp = theta[ix]<EOL><DEDENT>elif key == '<STR_LIT>':<EOL><INDENT>params.t0 = theta[ix]<EOL><DEDENT>elif key == '<STR_LIT>':<EOL><INDENT>params.a = theta[ix]<EOL><DEDENT>elif key == '<STR_LIT>':<EOL><INDENT>params.inc = theta[ix]<EOL><DEDENT>elif key == '<STR_LIT>':<EOL><INDENT>params.per = theta[ix]<EOL><DEDENT>elif key == '<STR_LIT>':<EOL><INDENT>params.per = theta[ix]<EOL><DEDENT>elif key == '<STR_LIT>':<EOL><INDENT>params.w = theta[ix]<EOL><DEDENT>elif key == '<STR_LIT>':<EOL><INDENT>u.append(theta[ix])<EOL><DEDENT>elif key == '<STR_LIT>':<EOL><INDENT>u.append(theta[ix])<EOL>params.u = u<EOL><DEDENT><DEDENT>lc = model.light_curve(params)<EOL>residuals = flux - lc<EOL>log_likelihood = -<NUM_LIT:0.5>*(<EOL>np.sum((residuals/err_flux)**<NUM_LIT:2> + np.log(<NUM_LIT:2>*np.pi*(err_flux)**<NUM_LIT:2>))<EOL>)<EOL>return log_likelihood<EOL>

Given a batman TransitModel and its proposed parameters (theta), update the
batman params object with the proposed parameters and evaluate the gaussian
likelihood.

Note: the priorbounds are only needed to parse theta.


stimes, smags, serrs = sigclip_magseries(times, mags, errs,<EOL>sigclip=sigclip,<EOL>magsarefluxes=magsarefluxes)<EOL>nzind = np.nonzero(serrs)<EOL>stimes, smags, serrs = stimes[nzind], smags[nzind], serrs[nzind]<EOL>transitperiod, transitepoch, transitdepth = transitparams[<NUM_LIT:0>:<NUM_LIT:3>]<EOL>if transitepoch is None:<EOL><INDENT>if verbose:<EOL><INDENT>LOGWARNING('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>try:<EOL><INDENT>spfit = spline_fit_magseries(times, mags, errs, transitperiod,<EOL>sigclip=sigclip,<EOL>magsarefluxes=magsarefluxes,<EOL>verbose=verbose)<EOL>transitepoch = spfit['<STR_LIT>']['<STR_LIT>']<EOL><DEDENT>except Exception as e:<EOL><INDENT>sgfit = savgol_fit_magseries(times, mags, errs, transitperiod,<EOL>sigclip=sigclip,<EOL>magsarefluxes=magsarefluxes,<EOL>verbose=verbose)<EOL>transitepoch = sgfit['<STR_LIT>']['<STR_LIT>']<EOL><DEDENT>finally:<EOL><INDENT>if transitepoch is None:<EOL><INDENT>LOGERROR("<STR_LIT>"<EOL>"<STR_LIT>")<EOL>returndict = {<EOL>'<STR_LIT>':'<STR_LIT>',<EOL>'<STR_LIT>':{<EOL>'<STR_LIT>':transitparams,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':None,<EOL>},<EOL>'<STR_LIT>':np.nan,<EOL>'<STR_LIT>':np.nan,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':{<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':magsarefluxes,<EOL>},<EOL>}<EOL>return returndict<EOL><DEDENT>else:<EOL><INDENT>if transitepoch.size > <NUM_LIT:1>:<EOL><INDENT>if verbose:<EOL><INDENT>LOGWARNING(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>transitparams[<NUM_LIT:1>] = transitepoch[<NUM_LIT:0>]<EOL><DEDENT>else:<EOL><INDENT>if verbose:<EOL><INDENT>LOGWARNING(<EOL>'<STR_LIT>'<EOL>% transitepoch<EOL>)<EOL><DEDENT>transitparams[<NUM_LIT:1>] = transitepoch.item()<EOL><DEDENT><DEDENT><DEDENT><DEDENT>if magsarefluxes:<EOL><INDENT>if transitdepth < <NUM_LIT:0.0>:<EOL><INDENT>transitparams[<NUM_LIT:2>] = -transitdepth<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if transitdepth > <NUM_LIT:0.0>:<EOL><INDENT>transitparams[<NUM_LIT:2>] = -transitdepth<EOL><DEDENT><DEDENT>try:<EOL><INDENT>leastsqfit = spleastsq(transits.trapezoid_transit_residual,<EOL>transitparams,<EOL>args=(stimes, smags, serrs),<EOL>full_output=True)<EOL><DEDENT>except Exception as e:<EOL><INDENT>leastsqfit = None<EOL><DEDENT>if leastsqfit and leastsqfit[-<NUM_LIT:1>] in (<NUM_LIT:1>,<NUM_LIT:2>,<NUM_LIT:3>,<NUM_LIT:4>):<EOL><INDENT>finalparams = leastsqfit[<NUM_LIT:0>]<EOL>covxmatrix = leastsqfit[<NUM_LIT:1>]<EOL>fitmags, phase, ptimes, pmags, perrs, n_transitpoints = (<EOL>transits.trapezoid_transit_func(<EOL>finalparams,<EOL>stimes, smags, serrs,<EOL>get_ntransitpoints=True<EOL>)<EOL>)<EOL>fitchisq = np.sum(<EOL>((fitmags - pmags)*(fitmags - pmags)) / (perrs*perrs)<EOL>)<EOL>fitredchisq = fitchisq/(len(pmags) - len(finalparams) - <NUM_LIT:1>)<EOL>residuals = leastsqfit[<NUM_LIT:2>]['<STR_LIT>']<EOL>residualvariance = (<EOL>np.sum(residuals*residuals)/(pmags.size - finalparams.size)<EOL>)<EOL>if covxmatrix is not None:<EOL><INDENT>covmatrix = residualvariance*covxmatrix<EOL>stderrs = np.sqrt(np.diag(covmatrix))<EOL><DEDENT>else:<EOL><INDENT>LOGERROR('<STR_LIT>')<EOL>stderrs = None<EOL><DEDENT>if verbose:<EOL><INDENT>LOGINFO(<EOL>'<STR_LIT>' %<EOL>(fitchisq, fitredchisq)<EOL>)<EOL><DEDENT>fperiod, fepoch = finalparams[:<NUM_LIT:2>]<EOL>returndict = {<EOL>'<STR_LIT>':'<STR_LIT>',<EOL>'<STR_LIT>':{<EOL>'<STR_LIT>':transitparams,<EOL>'<STR_LIT>':finalparams,<EOL>'<STR_LIT>':stderrs,<EOL>'<STR_LIT>':leastsqfit,<EOL>'<STR_LIT>':fitmags,<EOL>'<STR_LIT>':fepoch,<EOL>'<STR_LIT>':n_transitpoints<EOL>},<EOL>'<STR_LIT>':fitchisq,<EOL>'<STR_LIT>':fitredchisq,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':{<EOL>'<STR_LIT>':phase,<EOL>'<STR_LIT>':ptimes,<EOL>'<STR_LIT>':pmags,<EOL>'<STR_LIT>':perrs,<EOL>'<STR_LIT>':magsarefluxes,<EOL>},<EOL>}<EOL>if plotfit and isinstance(plotfit, str):<EOL><INDENT>make_fit_plot(phase, pmags, perrs, fitmags,<EOL>fperiod, ptimes.min(), fepoch,<EOL>plotfit,<EOL>magsarefluxes=magsarefluxes)<EOL>returndict['<STR_LIT>'] = plotfit<EOL><DEDENT>return returndict<EOL><DEDENT>else:<EOL><INDENT>LOGERROR('<STR_LIT>')<EOL>returndict = {<EOL>'<STR_LIT>':'<STR_LIT>',<EOL>'<STR_LIT>':{<EOL>'<STR_LIT>':transitparams,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':leastsqfit,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':<NUM_LIT:0><EOL>},<EOL>'<STR_LIT>':np.nan,<EOL>'<STR_LIT>':np.nan,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':{<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':magsarefluxes,<EOL>},<EOL>}<EOL>return returndict<EOL><DEDENT>

This fits a trapezoid transit model to a magnitude time series.

    Parameters
    ----------

    times,mags,errs : np.array
        The input mag/flux time-series to fit a trapezoid planet-transit model
        to.

    period : float
        The period to use for the model fit.

    transitparams : list of floats
        These are initial parameters for the transit model fit. A list of the
        following form is required::

            transitparams = [transitperiod (time),
                             transitepoch (time),
                             transitdepth (flux or mags),
                             transitduration (phase),
                             ingressduration (phase)]

        - for magnitudes -> `transitdepth` should be < 0
        - for fluxes     -> `transitdepth` should be > 0

        If `transitepoch` is None, this function will do an initial spline fit
        to find an approximate minimum of the phased light curve using the given
        period.

        The `transitdepth` provided is checked against the value of
        `magsarefluxes`. if `magsarefluxes = True`, the `transitdepth` is forced
        to be > 0; if `magsarefluxes` = False, the `transitdepth` is forced to
        be < 0.

    sigclip : float or int or sequence of two floats/ints or None
        If a single float or int, a symmetric sigma-clip will be performed using
        the number provided as the sigma-multiplier to cut out from the input
        time-series.

        If a list of two ints/floats is provided, the function will perform an
        'asymmetric' sigma-clip. The first element in this list is the sigma
        value to use for fainter flux/mag values; the second element in this
        list is the sigma value to use for brighter flux/mag values. For
        example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma
        dimmings and greater than 3-sigma brightenings. Here the meaning of
        "dimming" and "brightening" is set by *physics* (not the magnitude
        system), which is why the `magsarefluxes` kwarg must be correctly set.

        If `sigclip` is None, no sigma-clipping will be performed, and the
        time-series (with non-finite elems removed) will be passed through to
        the output.

    magsarefluxes : bool
        If True, will treat the input values of `mags` as fluxes for purposes of
        plotting the fit and sig-clipping.

    plotfit : str or False
        If this is a string, this function will make a plot for the fit to the
        mag/flux time-series and writes the plot to the path specified here.

    ignoreinitfail : bool
        If this is True, ignores the initial failure to find a set of optimized
        Fourier parameters using the global optimization function and proceeds
        to do a least-squares fit anyway.

    verbose : bool
        If True, will indicate progress and warn of any problems.

    Returns
    -------

    dict
        This function returns a dict containing the model fit parameters, the
        minimized chi-sq value and the reduced chi-sq value. The form of this
        dict is mostly standardized across all functions in this module::

            {
                'fittype':'traptransit',
                'fitinfo':{
                    'initialparams':the initial transit params provided,
                    'finalparams':the final model fit transit params ,
                    'finalparamerrs':formal errors in the params,
                    'leastsqfit':the full tuple returned by scipy.leastsq,
                    'fitmags': the model fit mags,
                    'fitepoch': the epoch of minimum light for the fit,
                    'ntransitpoints': the number of LC points in transit phase
                },
                'fitchisq': the minimized value of the fit's chi-sq,
                'fitredchisq':the reduced chi-sq value,
                'fitplotfile': the output fit plot if fitplot is not None,
                'magseries':{
                    'times':input times in phase order of the model,
                    'phase':the phases of the model mags,
                    'mags':input mags/fluxes in the phase order of the model,
                    'errs':errs in the phase order of the model,
                    'magsarefluxes':input value of magsarefluxes kwarg
                }
            }


def _get_value(quantitystr, fitparams, fixedparams):

<EOL>fitparamskeys, fixedparamskeys = fitparams.keys(), fixedparams.keys()<EOL>if quantitystr in fitparamskeys:<EOL><INDENT>quantity = fitparams[quantitystr]<EOL><DEDENT>elif quantitystr in fixedparamskeys:<EOL><INDENT>quantity = fixedparams[quantitystr]<EOL><DEDENT>return quantity<EOL>

This decides if a value is to be fit for or is fixed in a model fit.

    When you want to get the value of some parameter, but you're not sure if
    it's being fit or if it is fixed. then, e.g. for `period`::

        period_value = _get_value('period', fitparams, fixedparams)