Example #1
1
    def to_orange_network(self):
        """Convert the current network to >>Orange<< NetworkX standard. To use
        :obj:`Orange.network` in Orange widgets, set node IDs to be range 
        [0, no_of_nodes - 1].
        
        """

        G = self.__class__()
        node_list = sorted(self.nodes())
        node_to_index = dict(zip(node_list, range(self.number_of_nodes())))
        index_to_node = dict(zip(range(self.number_of_nodes()), node_list))

        G.add_nodes_from(zip(range(self.number_of_nodes()), [copy.deepcopy(self.node[nid]) for nid in node_list]))
        G.add_edges_from(
            ((node_to_index[u], node_to_index[v], copy.deepcopy(self.edge[u][v])) for u, v in self.edges())
        )

        for id in G.node.keys():
            G.node[id]["old_id"] = index_to_node[id]

        if self.items():
            G.set_items(self.items())

        if self.links():
            G.set_links(self.links())

        return G
Example #2
1
def rectilinear(vertices, periodic=(), name="rect"):
    "rectilinear mesh"

    ndims = len(vertices)
    indices = numpy.ogrid[tuple(slice(len(n) - 1) for n in vertices)]
    domainelem = element.Element(ndims=ndims, vertices=[])

    vertexfmt = name + "(" + ",".join("%%%dd" % len(str(len(n) - 1)) for n in vertices) + ")"
    vertexobjs = util.objmap(
        lambda *index: element.PrimaryVertex(vertexfmt % index), *numpy.ogrid[tuple(slice(len(n)) for n in vertices)]
    )
    for idim in periodic:
        tmp = numeric.bringforward(vertexobjs, idim)
        tmp[-1] = tmp[0]

    structure = util.objmap(
        lambda *index: element.QuadElement(
            ndims=ndims,
            parent=(
                domainelem,
                element.AffineTransformation(
                    offset=[n[i] for n, i in zip(vertices, index)],
                    transform=numpy.diag([n[i + 1] - n[i] for n, i in zip(vertices, index)]),
                ),
            ),
            vertices=vertexobjs[tuple(slice(i, i + 2) for i in index)].ravel(),
        ),
        *indices
    )
    topo = topology.StructuredTopology(structure)
    coords = GridFunc(domainelem, structure, vertices)
    if periodic:
        topo = topo.make_periodic(periodic)
    return topo, coords
Example #3
1
    def from_arrays(cls, arrays, column_names=None, **kwargs):
        """Produce :class:`ColumnDataSource` from array-like data.

        Returns:
            :class:`ColumnDataSource`
        """
        # handle list of arrays
        if any(cls.is_list_arrays(array) for array in arrays):
            list_of_arrays = copy(arrays)
            arrays = list(chain.from_iterable(arrays))
            column_names = column_names or gen_column_names(len(arrays))
            cols = copy(column_names)
            dims = kwargs.get("dims", None) or DEFAULT_DIMS

            # derive column selections
            for dim, list_of_array in zip(dims, list_of_arrays):
                sel = [cols.pop(0) for _ in list_of_array]
                kwargs[dim] = sel
        else:
            column_names = column_names or gen_column_names(len(arrays))

        # try to replace auto names with Series names
        for i, array in enumerate(arrays):
            if isinstance(array, pd.Series):
                name = array.name
                if name not in column_names and name is not None:
                    column_names[i] = name

        table = {column_name: array for column_name, array in zip(column_names, arrays)}
        return cls(df=pd.DataFrame.from_dict(data=table), **kwargs)
Example #4
1
File: sql.py Project: Will-So/blaze
def compute_up(expr, data, **kwargs):
    names = data.c.keys()
    assert names == expr._child.fields
    d = dict(zip(names, getattr(data, "inner_columns", data.c)))
    return sa.select(
        d[col].label(new_col) if col != new_col else d[col] for col, new_col in zip(expr._child.fields, expr.fields)
    )
Example #5
1
    def merge(left, right, func):
        # Effectively computes [func(x, y) for x, y in zip(left, right)]
        # Assume func(x, x) == x
        if left is right:
            return left

        if left is None:
            left, right = right, left

        default = left.default
        merge = _TreeListSub.merge
        if right is None:
            direct = [func(x, default) for x in left.direct]
            children = [merge(child, None, func) for child in left.children]
            if direct == left.direct and children == left.children:
                return left
            return _TreeListSub(default, direct, children)

        direct = [func(x, y) for x, y in zip(left.direct, right.direct)]
        children = [merge(c1, c2, func) for c1, c2 in zip(left.children, right.children)]
        if direct == left.direct and children == left.children:
            return left
        if direct == right.direct and children == right.children:
            return right
        return _TreeListSub(default, direct, children)
Example #6
1
    def post(self):
        # array of time strings: "08:00 AM"
        bannedTimes = json.loads(self.request.get("bannedTimes"))
        # array of all bannedDays checkboxes (booleans)
        bannedDays = json.loads(self.request.get("bannedDays"))

        badIvals = []
        # each 2 time strings corresponds to 5 bannedDays checkboxes
        for dayBools, times in zip(misc.iterGroups(bannedDays, 5), misc.iterGroups(bannedTimes, 2)):
            badIvals += parseJSInterval(times, dayBools)

        subCodes = json.loads(self.request.get("subCodes"))
        nums = json.loads(self.request.get("nums"))
        curCRNs = json.loads(self.request.get("curCRNs"))

        classes = [t + (year, season) for t in zip(subCodes, nums)]

        try:
            clsToSections = courses.planSchedule(classes, badIvals, curCRNs)
        except:
            self.response.out.write(json.dumps({}))
        else:
            for cls, sections in clsToSections.items():
                clsToSections[cls[0] + " " + str(cls[1])] = sections
                del clsToSections[cls]

                for sec in sections:
                    sec["Intervals"] = [courses.reprInterval(i) for i in sec["Intervals"]]

            self.response.out.write(json.dumps(clsToSections))
Example #7
1
        def _print_summrized_result(result):
            raw = result["data"]["raw"]
            table_cols = ["action", "min", "median", "90%ile", "95%ile", "max", "avg", "success", "count"]
            float_cols = ["min", "median", "90%ile", "95%ile", "max", "avg"]
            formatters = dict(zip(float_cols, [cliutils.pretty_float_formatter(col, 3) for col in float_cols]))
            table_rows = []

            actions_data = utils.get_atomic_actions_data(raw)
            for action in actions_data:
                durations = actions_data[action]
                if durations:
                    data = [
                        action,
                        round(min(durations), 3),
                        round(utils.median(durations), 3),
                        round(utils.percentile(durations, 0.90), 3),
                        round(utils.percentile(durations, 0.95), 3),
                        round(max(durations), 3),
                        round(utils.mean(durations), 3),
                        "%.1f%%" % (len(durations) * 100.0 / len(raw)),
                        len(raw),
                    ]
                else:
                    data = [action, None, None, None, None, None, None, "0.0%", len(raw)]
                table_rows.append(rutils.Struct(**dict(zip(table_cols, data))))

            cliutils.print_list(
                table_rows,
                fields=table_cols,
                formatters=formatters,
                table_label="Response Times (sec)",
                sortby_index=None,
            )
Example #8
1
def solve(A, *B):
    A = asarray(A)
    assert A.ndim == 2
    B = [asarray(b) for b in B]
    assert all(b.shape[0] == A.shape[0] and b.ndim in (1, 2) for b in B)
    S = [
        slice(i, i + b.shape[1]) if b.ndim == 2 else i
        for b, i in zip(B, numpy.cumsum([0] + [b[0].size for b in B[:-1]]))
    ]
    if not isrational(A) or not all(isrational(b) for b in B):
        A = A.astype(float)
        B = numpy.concatenate([b.astype(float).reshape(len(b), -1) for b in B], axis=1)
        Y = numpy.linalg.solve(A, B)
        X = [Y[:, s] for s in S]
    else:
        Ab = numpy.concatenate([A.numer] + [b.numer.reshape(len(b), -1) for b in B], axis=1)
        n = A.shape[1]
        for icol in range(n):
            if not Ab[icol, icol]:
                Ab[icol:] = Ab[icol + numpy.argsort([abs(v) if v else numpy.inf for v in Ab[icol:, icol]])]
            Ab[:icol] = Ab[:icol] * Ab[icol, icol] - Ab[:icol, icol, numpy.newaxis] * Ab[icol, :]
            Ab[icol + 1 :] = Ab[icol + 1 :] * Ab[icol, icol] - Ab[icol + 1 :, icol, numpy.newaxis] * Ab[icol, :]
        if Ab[n:].any():
            raise numpy.linalg.LinAlgError("linear system has no solution")
        w = numpy.diag(Ab[:n, :n])
        denom = gcd(*w)
        numer = Ab[:n, n:] * (denom // w[:, numpy.newaxis])
        X = [Rational(numer[:, s] * A.denom, denom * b.denom) for (s, b) in zip(S, B)]
        assert not any((dot(A, x) - b).numer.any() for (x, b) in zip(X, B))
    if len(B) == 1:
        X, = X
    return X
Example #9
0
    def add_lines(self, levels, colors, linewidths, erase=True):
        """
        Draw lines on the colorbar.

        *colors* and *linewidths* must be scalars or
        sequences the same length as *levels*.

        Set *erase* to False to add lines without first
        removing any previously added lines.
        """
        y = self._locate(levels)
        igood = (y < 1.001) & (y > -0.001)
        y = y[igood]
        if cbook.iterable(colors):
            colors = np.asarray(colors)[igood]
        if cbook.iterable(linewidths):
            linewidths = np.asarray(linewidths)[igood]
        N = len(y)
        x = np.array([0.0, 1.0])
        X, Y = np.meshgrid(x, y)
        if self.orientation == "vertical":
            xy = [list(zip(X[i], Y[i])) for i in range(N)]
        else:
            xy = [list(zip(Y[i], X[i])) for i in range(N)]
        col = collections.LineCollection(xy, linewidths=linewidths)

        if erase and self.lines:
            for lc in self.lines:
                lc.remove()
            self.lines = []
        self.lines.append(col)
        col.set_color(colors)
        self.ax.add_collection(col)
Example #10
0
def shuffleMerge0(keys1, values1, keys2, values2):
    # this version uses list.sort (and its usually slower)
    import bisect

    lastKey1 = keys1[-1]
    lastKey2 = keys2[-1]
    swap = lastKey1 > lastKey2
    if swap:
        # pr  "swapping", lastKey1, lastKey2
        (keys1, values1, keys2, values2, lastKey1, lastKey2) = (keys2, values2, keys1, values1, lastKey2, lastKey1)
    # now lastKey1 is in range of keys2: truncate keys2
    # merge all of keys1/values1
    part1 = zip(keys1, values1)
    # pr  "part1", part1
    # merge part of keys2, values2
    endindex = bisect.bisect(keys2, lastKey1)
    # pr  "endindex", endindex, len(keys2), lastKey1, keys2
    part2 = zip(keys2[:endindex], values2[:endindex])
    # pr  "part2", part2
    all = part1 + part2
    all.sort()
    # pr  "all", all
    mergedKeys = [k for (k, v) in all]
    mergedValues = [v for (k, v) in all]
    keys1 = None
    values1 = None
    keys2 = keys2[endindex:]
    values2 = values2[endindex:]
    # pr  "before swap", (keys1, values1, keys2, values2)
    if swap:
        # pr  "unswapping"
        (keys1, values1, keys2, values2) = (keys2, values2, keys1, values1)
    # pr  "after swap", (keys1, values1, keys2, values2)
    return (mergedKeys, mergedValues, keys1, values1, keys2, values2)
def _mle(N, M, training_outputs, training_states, pseudo_initial, pseudo_transition, pseudo_emission):
    # p_initial is the probability that a sequence of states starts
    # off with a particular one.
    p_initial = numpy.zeros(N)
    if pseudo_initial:
        p_initial = p_initial + pseudo_initial
    for states in training_states:
        p_initial[states[0]] += 1
    p_initial = _normalize(p_initial)

    # p_transition is the probability that a state leads to the next
    # one.  C(i,j)/C(i) where i and j are states.
    p_transition = numpy.zeros((N, N))
    if pseudo_transition:
        p_transition = p_transition + pseudo_transition
    for states in training_states:
        for n in range(len(states) - 1):
            i, j = states[n], states[n + 1]
            p_transition[i, j] += 1
    for i in range(len(p_transition)):
        p_transition[i, :] = p_transition[i, :] / sum(p_transition[i, :])

    # p_emission is the probability of an output given a state.
    # C(s,o)|C(s) where o is an output and s is a state.
    p_emission = numpy.zeros((N, M))
    if pseudo_emission:
        p_emission = p_emission + pseudo_emission
    p_emission = numpy.ones((N, M))
    for outputs, states in zip(training_outputs, training_states):
        for o, s in zip(outputs, states):
            p_emission[s, o] += 1
    for i in range(len(p_emission)):
        p_emission[i, :] = p_emission[i, :] / sum(p_emission[i, :])

    return p_initial, p_transition, p_emission
Example #12
0
def gf_add(f, g, p, K):
    """
    Add polynomials in ``GF(p)[x]``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_add

    >>> gf_add([3, 2, 4], [2, 2, 2], 5, ZZ)
    [4, 1]

    """
    if not f:
        return g
    if not g:
        return f

    df = gf_degree(f)
    dg = gf_degree(g)

    if df == dg:
        return gf_strip([(a + b) % p for a, b in zip(f, g)])
    else:
        k = abs(df - dg)

        if df > dg:
            h, f = f[:k], f[k:]
        else:
            h, g = g[:k], g[k:]

        return h + [(a + b) % p for a, b in zip(f, g)]
    def find_substep(target, source, value, step_size, error):
        # recursive end condition
        if step_size < 0.0001:
            # new_source = source[:]
            # substep_shift(new_source, value)
            # return new_source
            return value

        # check plus shift
        source_plus = source[:]
        substep_shift(source_plus, value + step_size)
        p_error = 0
        for vals in zip(target, source_plus):
            p_error += pow(vals[0] - vals[1], 2)

        # check minus shift
        source_minus = source[:]
        substep_shift(source_minus, value - step_size)
        m_error = 0
        for vals in zip(target, source_minus):
            m_error += pow(vals[0] - vals[1], 2)

        # take best out of plus shift, minus shift, and no shift
        if p_error < m_error:
            if p_error < error:
                return find_substep(target, source, value + step_size, step_size / 2, p_error)
            else:
                return find_substep(target, source, value, step_size / 2, error)
        else:
            if m_error < error:
                return find_substep(target, source, value - step_size, step_size / 2, m_error)
            else:
                return find_substep(target, source, value, step_size / 2, error)
Example #14
0
def PlotNumberOfTags(corpus):
    word_tag_dict = defaultdict(set)

    for (word, tag) in corpus:
        word_tag_dict[word].add(tag)
    # using Counter for efficiency (leaner than FreqDist)
    C = Counter(len(val) for val in word_tag_dict.itervalues())

    pylab.subplot(211)
    pylab.plot(C.keys(), C.values(), "-go", label="Linear Scale")
    pylab.suptitle("Word Ambiguity:")
    pylab.title("Number of Words by Possible Tag Number")
    pylab.box("off")  # for better appearance
    pylab.grid("on")  # for better appearance
    pylab.ylabel("Words With This Number of Tags (Linear)")
    pylab.legend(loc=0)
    # add value tags
    for x, y in zip(C.keys(), C.values()):
        pylab.annotate(str(y), (x, y + 0.5))

    pylab.subplot(212)
    pylab.plot(C.keys(), C.values(), "-bo", label="Logarithmic Scale")
    pylab.yscale("log")  # to make the graph more readable, for the log graph version
    pylab.box("off")  # for better appearance
    pylab.grid("on")  # for better appearance
    pylab.xlabel("Number of Tags per Word")
    pylab.ylabel("Words With This Number of Tags (Log)")
    pylab.legend(loc=0)
    # add value tags
    for x, y in zip(C.keys(), C.values()):
        pylab.annotate(str(y), (x, y + 0.5))

    pylab.show()
Example #15
0
    def SetAllFieldTypes(self, package, desc_proto, scope):
        """Sets all the descriptor's fields's types.

    This method also sets the containing types on any extensions.

    Args:
      package: The current package of desc_proto.
      desc_proto: The message descriptor to update.
      scope: Enclosing scope of available types.
    """

        package = _PrefixWithDot(package)

        main_desc = self._GetTypeFromScope(package, desc_proto.name, scope)

        if package == ".":
            nested_package = _PrefixWithDot(desc_proto.name)
        else:
            nested_package = ".".join([package, desc_proto.name])

        for field_proto, field_desc in zip(desc_proto.field, main_desc.fields):
            self.SetFieldType(field_proto, field_desc, nested_package, scope)

        for extension_proto, extension_desc in zip(desc_proto.extension, main_desc.extensions):
            extension_desc.containing_type = self._GetTypeFromScope(nested_package, extension_proto.extendee, scope)
            self.SetFieldType(extension_proto, extension_desc, nested_package, scope)

        for nested_type in desc_proto.nested_type:
            self.SetAllFieldTypes(nested_package, nested_type, scope)
Example #16
0
def hierarchical(keys):
    """
    Iterates over dimension values in keys, taking two sets
    of dimension values at a time to determine whether two
    consecutive dimensions have a one-to-many relationship.
    If they do a mapping between the first and second dimension
    values is returned. Returns a list of n-1 mappings, between
    consecutive dimensions.
    """
    ndims = len(keys[0])
    if ndims <= 1:
        return True
    dim_vals = list(zip(*keys))
    combinations = (zip(*dim_vals[i : i + 2]) for i in range(ndims - 1))
    hierarchies = []
    for combination in combinations:
        hierarchy = True
        store1 = defaultdict(list)
        store2 = defaultdict(list)
        for v1, v2 in combination:
            if v2 not in store2[v1]:
                store2[v1].append(v2)
            previous = store1[v2]
            if previous and previous[0] != v1:
                hierarchy = False
                break
            if v1 not in store1[v2]:
                store1[v2].append(v1)
        hierarchies.append(store2 if hierarchy else {})
    return hierarchies
Example #17
0
    def _infer_graph(self, inputs, clusters):
        """Maps input to closest cluster and the score.

    Args:
      inputs: list of input Tensors.
      clusters: Tensor of cluster centers.

    Returns:
      List of tuple, where each value in tuple corresponds to a value in inp.
      The tuple has following three elements:
      all_scores: distance of each input to each cluster center.
      score: distance of each input to closest cluster center.
      cluster_idx: index of cluster center closest to the corresponding input.
    """
        assert isinstance(inputs, list)
        # Pairwise distances are used only by transform(). In all other cases, this
        # sub-graph is not evaluated.
        scores = self._distance_graph(inputs, clusters, self._distance_metric)
        output = []
        if self._distance_metric == COSINE_DISTANCE and not self._clusters_l2_normalized():
            # The cosine distance between normalized vectors x and y is the same as
            # 2 * squared_euclidian_distance. We are using this fact and reusing the
            # nearest_neighbors op.
            # TODO(ands): Support COSINE distance in nearest_neighbors and remove
            # this.
            with ops.colocate_with(clusters):
                clusters = tf.nn.l2_normalize(clusters, dim=1)
        for inp, score in zip(inputs, scores):
            with ops.colocate_with(inp):
                (indices, distances) = gen_clustering_ops.nearest_neighbors(inp, clusters, 1)
                if self._distance_metric == COSINE_DISTANCE:
                    distances *= 0.5
                output.append((score, tf.squeeze(distances), tf.squeeze(indices)))
        return zip(*output)
Example #18
0
def gf_sub(f, g, p, K):
    """
    Subtract polynomials in ``GF(p)[x]``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_sub

    >>> gf_sub([3, 2, 4], [2, 2, 2], 5, ZZ)
    [1, 0, 2]

    """
    if not g:
        return f
    if not f:
        return gf_neg(g, p, K)

    df = gf_degree(f)
    dg = gf_degree(g)

    if df == dg:
        return gf_strip([(a - b) % p for a, b in zip(f, g)])
    else:
        k = abs(df - dg)

        if df > dg:
            h, f = f[:k], f[k:]
        else:
            h, g = gf_neg(g[:k], p, K), g[k:]

        return h + [(a - b) % p for a, b in zip(f, g)]
def fetch():
    mongo_client = MongoClient("localhost", PORT)
    mcollin = mongo_client[DB][COLL]
    # mcollout = mongo_client[DB][COLL_OUT]

    for phase, fpkl in zip(
        ["climb", "descend", "cruise", "ground"], [fpkl_climb, fpkl_descend, fpkl_cruise, fpkl_ground]
    ):

        print "process phase: %s.." % phase

        res = mcollin.find({"phase": phase})

        samples = []
        total = res.count()
        count = 0

        for r in res:
            count += 1
            data = np.asarray(r["data"])

            alts = data[:, 3].astype(int).tolist()
            spds = data[:, 4].astype(int).tolist()

            samples.extend(zip(alts, spds))

            sys.stdout.write("Progress : %d of %d   \r" % (count, total))
            sys.stdout.flush()

        print "data fetched, pickleing"
        pickle.dump(samples, open(fpkl, "wb"))

    print "processing data completed"
Example #20
0
def drawGrid(x1, y1, x2, y2, nx, ny):
    """Draw a rectangular grid of lines
        
    The rectangle has (x1,y1) and and (x2,y2) as opposite corners.
    There are (nx,ny) subdivisions along the (x,y)-axis. So the grid
    has (nx+1) * (ny+1) lines. nx=ny=1 draws a rectangle.
    nx=0 draws 1 vertical line (at x1). nx=-1 draws no vertical lines.
    ny=0 draws 1 horizontal line (at y1). ny=-1 draws no horizontal lines.
    """
    GL.glBegin(GL.GL_LINES)
    ix = range(nx + 1)
    if nx == 0:
        jx = [1]
        nx = 1
    else:
        jx = ix[::-1]
    for i, j in zip(ix, jx):
        x = (i * x2 + j * x1) / nx
        GL.glVertex2f(x, y1)
        GL.glVertex2f(x, y2)

    iy = range(ny + 1)
    if ny == 0:
        jy = [1]
        ny = 1
    else:
        jy = iy[::-1]
    for i, j in zip(iy, jy):
        y = (i * y2 + j * y1) / ny
        GL.glVertex2f(x1, y)
        GL.glVertex2f(x2, y)
    GL.glEnd()
Example #21
0
 def _print_iterations_data(result):
     raw_data = result["data"]["raw"]
     headers = ["iteration", "full duration"]
     float_cols = ["full duration"]
     atomic_actions = []
     for row in raw_data:
         # find first non-error result to get atomic actions names
         if not row["error"] and "atomic_actions" in row:
             atomic_actions = row["atomic_actions"].keys()
     for row in raw_data:
         if row["atomic_actions"]:
             for (c, a) in enumerate(atomic_actions, 1):
                 action = "%(no)i. %(action)s" % {"no": c, "action": a}
                 headers.append(action)
                 float_cols.append(action)
             break
     table_rows = []
     formatters = dict(zip(float_cols, [cliutils.pretty_float_formatter(col, 3) for col in float_cols]))
     for (c, r) in enumerate(raw_data, 1):
         dlist = [c]
         dlist.append(r["duration"])
         if r["atomic_actions"]:
             for action in atomic_actions:
                 dlist.append(r["atomic_actions"].get(action) or 0)
         table_rows.append(rutils.Struct(**dict(zip(headers, dlist))))
     cliutils.print_list(table_rows, fields=headers, formatters=formatters)
     print()
Example #22
0
    def change_layer_order(self, ax=None):
        if ax is None:
            ax = self.select_subplot()
        set_pdfs = self.settings["{0:d}.{1:d}".format(ax.row, ax.col)]["pdfs"]
        # find current order
        pdfs = []
        layers = []
        for pdf in set_pdfs:
            if "layer" in set_pdfs[pdf]:
                layers.append(set_pdfs[pdf]["layer"])
                pdfs.append(pdf)
        old_layers = list(zip(*sorted(zip(pdfs, layers), key=lambda x: x[1], reverse=True))[0])
        # get new order (e.g. 1 0 2)
        m = Menu(
            options=old_layers,
            exit_str=None,
            header="Enter a new order for the constraints.\n" + "The current order (top to bottom) is:",
        )
        new_layer_order = m.get_order()
        new_layer_order.reverse()

        # should clear subplot before plotting contours again,
        # but don't want to change other plot elements

        # re-plot pdfs in new order
        for i in new_layer_order:
            self.plot_2d_pdf(ax, ax.pdfs[old_layers[i]], layer=-1)
Example #23
0
 def setUp(self):
     self.a = np.array(
         zip(np.arange(10), np.arange(50, 60), np.arange(100, 110)), dtype=[("a", int), ("b", int), ("c", int)]
     )
     self.b = np.array(
         zip(np.arange(5, 15), np.arange(65, 75), np.arange(100, 110)), dtype=[("a", int), ("b", int), ("d", int)]
     )
Example #24
0
def extractvitalstrings(temp_iunits, data, stopword):
    joinvital, dict_vitals, numval = [], [], 1
    t = re.compile(r"\([A-Z]+\s+([^)(]*)\)*")
    for c, d in zip(temp_iunits, data):  ## repeat this for each iunit.
        d = re.sub(
            r"\(DT\s+.*?\)|\(IN\s+.*?\)|\(TO\s+.*?\)|\(VBP\s+.*?\)|\(MD\s+.*?\)", "", d
        )  ## remove the unimportant words.
        vitalstring = re.compile(r"\([A-Z]+\s+([^\(\)]+?)\)").findall(d)
        vitals = re.compile(r"\(NP\s+((\([^\(]+?\){1}\s*)+)").findall(d)  ##extract related words.
        for i in vitals:
            if i != "":
                st = t.findall(i[0])
                st = [i.lower() for i in st if i.lower() not in stopword]  ## remove stopwords + query words.
                if len(st) > 0 and st not in dict_vitals:
                    if type(st) is list:
                        dict_vitals.append(st)
                    else:
                        dict_vitals.append([st])
        [joinvital.extend(el) for el in dict_vitals]
        for j in vitalstring:
            lower_word = j.lower()
            if lower_word not in stopword and lower_word not in joinvital and j not in dict_vitals:
                if type(j) is list:
                    dict_vitals.append(j)
                else:
                    dict_vitals.append([lower_word])
        dict_vital[numval] = dict(zip(range(1, len(dict_vitals) + 1), dict_vitals))
        numval = numval + 1
        dict_vitals = []
Example #25
0
    def batch_tag(self, sentences):
        # Write the test corpus to a temporary file
        (fd, test_file) = mkstemp(".txt", "test")
        self.write_test_corpus(sentences, os.fdopen(fd, "w"))

        try:
            # Run mallet on the test file.
            stdout, stderr = call_mallet(
                [
                    self._RUN_CRF,
                    "--model-file",
                    os.path.abspath(self.crf_info.model_filename),
                    "--test-file",
                    test_file,
                ],
                stdout="pipe",
            )

            # Decode the output
            labels = self.parse_mallet_output(stdout)

            # strip __start__ and __end__
            if self.crf_info.add_start_state and self.crf_info.add_end_state:
                labels = [labs[1:-1] for labs in labels]
            elif self.crf_info.add_start_state:
                labels = [labs[1:] for labs in labels]
            elif self.crf_info.add_end_state:
                labels = [labs[:-1] for labs in labels]

            # Combine the labels and the original sentences.
            return [zip(sent, label) for (sent, label) in zip(sentences, labels)]

        finally:
            os.remove(test_file)
Example #26
0
 def setUp(cls):
     cls.a = np.array(
         zip(np.arange(10), np.arange(50, 60), np.arange(100, 110)), dtype=[("a", int), ("b", int), ("c", int)]
     )
     cls.b = np.array(
         zip(np.arange(10), np.arange(65, 75), np.arange(100, 110)), dtype=[("a", int), ("b", int), ("d", int)]
     )
Example #27
0
def test_binary_blocks_cutting_plane():
    # testing cutting plane ssvm on easy binary dataset
    # generate graphs explicitly for each example
    for inference_method in get_installed(["dai", "lp", "qpbo", "ad3", "ogm"]):
        print("testing %s" % inference_method)
        X, Y = generate_blocks(n_samples=3)
        crf = GraphCRF(inference_method=inference_method)
        clf = NSlackSSVM(model=crf, max_iter=20, C=100, check_constraints=True, break_on_bad=False, n_jobs=1)
        x1, x2, x3 = X
        y1, y2, y3 = Y
        n_states = len(np.unique(Y))
        # delete some rows to make it more fun
        x1, y1 = x1[:, :-1], y1[:, :-1]
        x2, y2 = x2[:-1], y2[:-1]
        # generate graphs
        X_ = [x1, x2, x3]
        G = [make_grid_edges(x) for x in X_]

        # reshape / flatten x and y
        X_ = [x.reshape(-1, n_states) for x in X_]
        Y = [y.ravel() for y in [y1, y2, y3]]

        X = zip(X_, G)

        clf.fit(X, Y)
        Y_pred = clf.predict(X)
        for y, y_pred in zip(Y, Y_pred):
            assert_array_equal(y, y_pred)
Example #28
0
    def test_two_keys_two_vars(self):
        a = np.array(
            zip(np.tile([10, 11], 5), np.repeat(np.arange(5), 2), np.arange(50, 60), np.arange(10, 20)),
            dtype=[("k", int), ("a", int), ("b", int), ("c", int)],
        )

        b = np.array(
            zip(np.tile([10, 11], 5), np.repeat(np.arange(5), 2), np.arange(65, 75), np.arange(0, 10)),
            dtype=[("k", int), ("a", int), ("b", int), ("c", int)],
        )

        control = np.array(
            [
                (10, 0, 50, 65, 10, 0),
                (11, 0, 51, 66, 11, 1),
                (10, 1, 52, 67, 12, 2),
                (11, 1, 53, 68, 13, 3),
                (10, 2, 54, 69, 14, 4),
                (11, 2, 55, 70, 15, 5),
                (10, 3, 56, 71, 16, 6),
                (11, 3, 57, 72, 17, 7),
                (10, 4, 58, 73, 18, 8),
                (11, 4, 59, 74, 19, 9),
            ],
            dtype=[("k", int), ("a", int), ("b1", int), ("b2", int), ("c1", int), ("c2", int)],
        )
        test = join_by(["a", "k"], a, b, r1postfix="1", r2postfix="2", jointype="inner")
        assert_equal(test.dtype, control.dtype)
        assert_equal(test, control)
 def explode0(scene):
     centers = [CCOMB(S1(UKPOL(obj))) for obj in scene]
     scalings = len(centers) * [S([1, 2, 3])([sx, sy, sz])]
     scaledCenters = [UK(APPLY(pair)) for pair in zip(scalings, [MK(p) for p in centers])]
     translVectors = [VECTDIFF((p, q)) for (p, q) in zip(scaledCenters, centers)]
     translations = [T([1, 2, 3])(v) for v in translVectors]
     return STRUCT([t(obj) for (t, obj) in zip(translations, scene)])
Example #30
0
def check_concat_with_shape(shapes, dimension):
    n = len(shapes)
    # forward
    target_dim = 0
    for shape in shapes:
        target_dim += shape[dimension]

    inputs = [mx.symbol.Variable("arg%d" % i) for i in range(n)]
    out = mx.symbol.Concat(*inputs, name="conc", dim=dimension)
    arr = [mx.nd.empty(shape) for shape in shapes]
    for i in range(n):
        arr[i][:] = shapes[i][dimension]
    arr_np = [np.copy(narray.asnumpy()) for narray in arr]
    arr_grad = [mx.nd.empty(shape) for shape in shapes]
    args = out.list_arguments()
    arg_shapes, out_shapes, aux_shapes = out.infer_shape(**dict(zip(args, shapes)))
    out_grad = mx.nd.empty(out_shapes[0])
    exec1 = out.bind(mx.Context("cpu"), args=arr, args_grad=arr_grad)
    exec1.forward()
    out1 = exec1.outputs[0]
    ret = np.concatenate([narray.asnumpy() for narray in arr], axis=dimension)
    assert same(out1.asnumpy(), ret)
    # backward
    out1.copyto(out_grad)
    out_grad[:] += 1
    exec1.backward([out_grad])
    for grad, np_grad in zip(arr_grad, arr_np):
        assert same(grad.asnumpy(), np_grad + 1)