Beispiel #1
0
def _lower_neighbors(
    dist_mat: array_like_2d, max_scale: tc.any(int, float)
) -> tc.list_of(tc.list_of(np.int32)):
    """
    Converts a distance matrix to neighbor information.

    Takes a square, possibly lower triangular, and returns a list of lists of neighbor indices,
    for neighbors up to the specified scale.

    Parameters
    ----------
    dist_mat: 2D array
        the distance matrix, which may be lower triangular
    max_scale: float
        the highest scale (distance) to consider

    Returns
    -------
    neighbors: list of lists of int
    """
    d = sp.lil_matrix(dist_mat)
    d[d == 0] = sys.float_info.epsilon
    d[np.diag_indices(d.shape[0])] = 0
    d[d > max_scale] = 0
    d = sp.tril(d)
    result = [[] for i in range(d.shape[0])]
    for k, v in np.transpose(d.nonzero()):
        result[k].append(v)
    return result
Beispiel #2
0
    def __init__(self,
                 *,
                 timeout: optional(float) = None,
                 interface: optional(str) = None,
                 protocol: optional(str) = None,
                 parameters: optional(dict) = None,
                 description: optional(str) = None,
                 log_levels: optional(list_of(int)) = None):

        # infinite "requests" can only be anonymous, "normal" requests
        # having a deadline must be assigned an interface and protocol

        self._start = time()
        if timeout is not None:
            self._deadline = self._start + timeout
            assert interface is not None and protocol is not None, \
                   "request with deadline from unspecified interface/protocol"
        else:
            self._deadline = None
            assert interface is None and protocol is None, \
                   "infinite request from specific interface/protocol"

        self._interface, self._protocol = interface, protocol
        self._parameters = parameters or {}

        request_time = strftime("%Y%m%d%H%M%S")
        random_id = b2a_hex(urandom(6)).decode("ascii").upper()
        self._unique_id = "RQ-{0:s}-{1:s}".format(request_time, random_id)
        self._description = description
        self._log_levels = (log_levels or [])[:]
Beispiel #3
0
def _add_cofaces(lower_neighbors: tc.list_of(tc.list_of(np.int32)),
                 max_dim: int, dist_mat: array_like_2d, start: int):
    """
    Returns all cofaces for the given start node.

    Cofaces are represented by lists of indices, paired with their stepwise distance from the start node.

    Parameters
    ----------
    lower_neighbors: list of lists of int
        neighbors for each index, as returned by the `_lower_neighbors` function
    max_dim: int
        the largest simplex dimension to consider
    dist_mat: 2D array
        the distance matrix, which may be lower triangular

    Returns
    -------
    simplices: list of (coface, distance) pairs
    """
    # TODO: iterative implementation (maybe), since Python doesn't have tailcall elimination
    simplices = []

    def coface(tau, tau_dist, N):
        simplices.append((tau, tau_dist))
        if len(tau) >= max_dim + 1 or len(N) == 0:
            return
        else:
            for v in N:
                sigma = tau + [v]
                M = [val for val in N if val in lower_neighbors[v]]
                # get the distance at which sigma appears
                sigma_dist = max(tau_dist, *[dist_mat[u][v] for u in tau])
                coface(sigma, sigma_dist, M)

    coface([start], 0, lower_neighbors[start])
    return simplices
 def foo_l(x: tc.list_of(int)) -> tc.list_of(float):
     return list(map(float, x))
Beispiel #5
0
    def _reload(self, filename, request):

        re = self._module is not None and "re" or ""

        self._loader.log.message("{0:s}loading module {1:s} from {2:s}".\
                                 format(re, self._name, filename))
        try:

            # managing imports requires holding a global lock

            acquire_imp_lock()
            try:

                if self._name in sys_modules:
                    raise ModuleAlreadyImportedError("module {0:s} has already been "
                                                     "imported".format(self._name))

                with open(filename, "rb") as module:

                    ext = os_path.splitext(filename)[1]
                    assert valid_module_ext(ext)

                    if ext == ".py":

                        # as a simple guard against picking up incomplete files,
                        # being simultaneously written to, we require the modules
                        # to end with # EOF

                        try:
                            next(filter(lambda s: s.rstrip() == b"# EOF", module))
                        except StopIteration:
                            raise ModuleFileIncompleteError("file {0:s} is incomplete, does not "
                                                            "end with # EOF".format(filename))
                        else:
                            module.seek(0) # rewind the file

                    # actually import the module, it can already be broken
                    # again at this point, but we don't care

                    try:
                        load_module(self._name, module, filename, ("", "rb", ext == ".py" and PY_SOURCE or PY_COMPILED))
                    except Exception as e:
                        raise ModuleFileBrokenError("file {0:s} is broken: {1:s}".format(filename, e))

                # the pmnc-accessible modules are invisible in sys.modules

                module = sys_modules.pop(self._name)

                # see if the loaded module has __all__ attribute,
                # and if not provide a default empty list

                all = getattr(module, "__all__", None)
                if all is None:
                    setattr(module, "__all__", [])
                    self._loader.log.warning("module {0:s} has no __all__ attribute and will "
                                             "export no methods or classes".format(self._name))
                else:
                    assert list_of(str)(all), "__all__ attribute must be a list of strings"

                # append self_test method to a list of accessible
                # methods for the module being tested

                if request.self_test == self._name:
                    all.append("self_test")

            finally:
                release_imp_lock()

            # the module has been successfully loaded from the file

            try:

                # the newly imported module should have just one reference to it

                if getrefcount(module) != 2:
                    raise ModuleWithDependenciesError("the newly loaded module {0:s} has "
                                                      "unexpected dependencies".format(self._name))

                # a module containing __reloadable__ = False is assumed
                # to have state and hence be not reloadable

                reloadable = bool(getattr(module, "__reloadable__", True))

                # the imported module is instrumented with pmnc and others

                setattr(module, "pmnc", ModuleLoaderProxy(self._loader, self._name))

                setattr(module, "__node__", self._loader._node_name)
                setattr(module, "__cage__", self._loader._cage_name)
                setattr(module, "__module__", self._name)
                setattr(module, "__cage_dir__", self._loader._cage_directory)

                # success, the methods cache is cleared and the previous version is discarded

                with self._lock:
                    self._attrs.clear()

                self._module, module, self._reloadable = module, self._module, reloadable

            finally:
                del module

        except ModuleLoaderError as e:
            if self._module is not None:
                self._loader.log.message("reloading of module {0:s} failed: {1:s} (the error "
                                         "is ignored)".format(self._name, str(e)))
            else:
                raise
        else:
            self._loader.log.message("module {0:s} has been {1:s}loaded{2:s}".format(self._name, re,
                                     not reloadable and " (not reloadable)" or ""))
Beispiel #6
0
 def __init__(self, cursor_ids: list_of(int)):
     MongoDB_Request.__init__(self)
     self._cursor_ids = cursor_ids
Beispiel #7
0
 def __init__(self, collection: str, documents: list_of(dict)):
     MongoDB_Request.__init__(self)
     self._collection = collection
     self._documents = documents
def foo(x: tc.list_of((tc.has("^[01]+$"), int))) -> bool:
    return functools.reduce(lambda r, e: r and int(e[0], 2) == e[1],
                            x, True)
with expected(InputParameterError("foo() has got an incompatible value for x: ()")):
    foo(())

###################

@typecheck
def foo(x: tc.list_of((tc.has("^[01]+$"), int))) -> bool:
    return functools.reduce(lambda r, e: r and int(e[0], 2) == e[1],
                            x, True)

assert foo([("1010", 10), ("0101", 5)])
assert not foo([("1010", 10), ("0111", 77)])

###################

assert tc.list_of(tc.optional(tc.has("^foo$")))(["foo", None, "foo"]) and \
       tc.list_of(tc.optional(tc.has("^foo$"))).check(["foo", None, "foo"])

assert not tc.list_of(tc.optional(tc.has("^foo$")))(["123", None, "foo"]) and \
       not tc.list_of(tc.optional(tc.has("^foo$"))).check(["123", None, "foo"])

print("ok")

############################################################################

print("sequence_of: ", end="")

@typecheck
def foo(x: tc.sequence_of(int)) -> tc.sequence_of(float):
    return list(map(float, x))
Beispiel #10
0
# noinspection PyPackageRequirements
import typecheck as tc
import sys
import scipy.sparse as sp
import types
import collections


def numpy_2d_float(x):
    """Type predicate: a numpy array containing floating point values"""
    return isinstance(x, (np.ndarray, np.generic)) and len(
        x.shape) == 2 and x.dtype in (np.float32, np.float64)


"""Type predicate: something like a 2D array"""
array_like_2d = tc.any(tc.list_of(tc.list_of(tc.any(int,
                                                    float))), numpy_2d_float,
                       sp.lil_matrix, sp.csc_matrix, sp.csr_matrix)


@tc.typecheck
def _lower_neighbors(
    dist_mat: array_like_2d, max_scale: tc.any(int, float)
) -> tc.list_of(tc.list_of(np.int32)):
    """
    Converts a distance matrix to neighbor information.

    Takes a square, possibly lower triangular, and returns a list of lists of neighbor indices,
    for neighbors up to the specified scale.

    Parameters
    ----------
 def _kill_cursors(self, collection: empty_string, cursor_ids: list_of(int),
                   **kwargs):
     self._async_request(OP_KILL_CURSORS(cursor_ids, **kwargs))
 def _insert(self, collection: valid_collection, documents: list_of(dict)):
     collection = "{0:s}.{1:s}".format(self._database, collection)
     self._async_request(OP_INSERT(collection, documents))
Beispiel #13
0
 def foo_l(x: tc.list_of(int)) -> tc.list_of(float):
     return list(map(float, x))
Beispiel #14
0
    def _frag_message(message: bytes, bits_per_char: optional(int) = 8,
                      frag_bytes: optional(int) = 140, prefix_bytes: optional(int) = 0) -> list_of(bytes):

        if not message:
            return [ message ]

        bpc = bits_per_char
        pad_bits = (bpc - prefix_bytes * 8 % bpc) % bpc if (bpc < 8 and prefix_bytes > 0) else 0 # 7 bit left padding to septet

        mbpf = frag_bytes - prefix_bytes - (1 if pad_bits > 0 else 0) # maximum bytes per fragment we could squeeze

        # each fragment should contain full characters, with 7 bit encoding
        # we round each fragment up to 7 bytes to guarantee that

        bpp = bpc if bpc % 8 != 0 else bpc // 8
        if mbpf < bpp:
            raise Exception("fragment size {0:d} is too small".format(frag_bytes))
        bpf = mbpf // bpp * bpp

        # note that escape sequences are not honored and could be split

        frags, frag, message = [], message[:bpf], message[bpf:]
        while frag:

            # each fragment is padded independently

            if pad_bits > 0:
                pbb, pbm, nbb = pad_bits, (1 << pad_bits) - 1, 8 - pad_bits
                pb, fb = 0, []
                for b in frag:
                    fb.append(pb << nbb | b >> pbb)
                    pb = b & pbm
                fb.append(pb << nbb)
                frag = bytes(fb)

            frags.append(frag)
            frag, message = message[:bpf], message[bpf:]

        return frags