Example #1
0
def _inject_dives_ui(dives, title, info, temp, avg_depth,
        legend, labels):

    """
    Inject ``kz.dives.ui`` data frame into R global space.

    The data frame has the following columns

    info
        Dive information string with dive duration, maximum depth and
        dive temperature.
    title
        Dive title.
    avg_depth
        Dive average depth label.
    label
        Dive label put on legend.

    See ``plot`` for parameters description.
    """
    labels = [] if labels is None else labels

    # dive title formatter
    tfmt = lambda d, l: '{}'.format(d.datetime)

    # dive info formatter
    _ifmt = '{:.1f}m \u00b7 {}min \u00b7 {:.1f}\u00b0C'.format
    ifmt = lambda d, l: _ifmt(d.depth, min2str(d.duration / 60.0), K2C(d.temp))

    # average depth formatter
    dfmt = lambda d, l: None if d.avg_depth is None \
                else '{:.1f}m'.format(d.avg_depth)

    # label formatter
    lfmt = lambda d, l: l if l else tfmt(d)

    cols = []
    fmts = []
    if title:
        cols.append('title')
        fmts.append(tfmt)
    if info:
        cols.append('info')
        fmts.append(ifmt)
    if avg_depth:
        cols.append('avg_depth')
        fmts.append(dfmt)
    if legend:
        cols.append('label')
        fmts.append(lfmt)
    vt = (ro.StrVector, ) * len(cols)
    data = (tuple(f(d, l) for f in fmts) for d, l in lzip(dives, labels))
    ui_df = kr.df(cols, vt, data)

    ro.globalenv['kz.dives.ui'] = ui_df
def missing_number (array, upper_bound):
    from itertools import izip_longest as lzip

    possible_numbers = range (0, upper_bound+1)

    missing_num = 0
    for elem, poss_elem in lzip (array, possible_numbers, fillvalue = 0):
        missing_num += poss_elem
        missing_num -= elem

    return missing_num
Example #3
0
def test_dsl(formula, seed, expected):
    from dice.parser import parse

    # logging.basicConfig(level=logging.DEBUG)

    values = parse(formula, seed=seed)
    if isinstance(expected, (list, tuple)):
        for value, expect in lzip(values, expected):
            assert value == expect
    else:
        found = next(values)
        assert found == expected
Example #4
0
def test_iters_numpy(shape, chunkshape1, blockshape1, chunkshape2, blockshape2, dtype):
    dtype = np.dtype(dtype)

    size = int(np.prod(shape))
    nparray = np.ones(size, dtype=dtype).reshape(shape)
    a = cat.asarray(nparray, chunkshape=chunkshape1, blockshape=blockshape1)  # creates a NPArray

    b = cat.empty(shape, dtype.itemsize, str(dtype), chunkshape=chunkshape2, blockshape=blockshape2)
    itershape = chunkshape2 if chunkshape2 is not None else b.shape

    for (block_r, info_r), (block_w, info_w) in lzip(a.iter_read(itershape), b.iter_write()):
        block_w[:] = bytearray(np.asarray(block_r))

    nparray2 = np.asarray(b.copy())
    np.testing.assert_equal(nparray, nparray2)
Example #5
0
def get_position(node):
    ret = [(-HALF_NODE_WIDTH, HALF_NODE_WIDTH)]
    if node.char:
        return ret
    left = get_position(node.left)
    right = get_position(node.right)
    width = reduce(lambda w, lr:
                    max(w, lr[0][1] -lr[1][0] + NODE_SPACE),
                    zip(left, right), 0)
    half = width / 2.0
    node.left.offset = -half
    node.right.offset = half
    for l, r in lzip(left, right):
        if l is None:
            ret.append((r[0] + half, r[1] + half))
        elif r is None:
            ret.append((l[0] - half, l[1] - half))
        else:
            ret.append((l[0] - half, r[1] + half))
    return ret
Example #6
0
def find_dive_gas_nodes(files, nodes=None):
    """
    Find gas nodes referenced by dives in UDDF files using optional node
    ranges as search parameter.

    The collection of gas nodes is returned.

    :Parameters:
     files
        Collection of UDDF files.
     nodes
        Numeric ranges of nodes, `None` if all nodes.

    .. seealso:: :py:func:`parse_range`
    """
    nodes = [] if nodes is None else nodes
    data = (ku.find(f, ku.XP_FIND_DIVE_GASES, nodes=q) \
        for q, f in lzip(nodes, files))
    nodes_by_id = ((n.get('id'), n) for n in ichain(data))
    return dict(nodes_by_id).values()
Example #7
0
def find_dive_nodes(files, nodes=None, dives=None):
    """
    Find dive nodes in UDDF files using optional numeric ranges or total
    dive number as search parameters.

    The collection of dive nodes is returned.

    :Parameters:
     files
        Collection of UDDF files.
     nodes
        Numeric ranges of nodes, `None` if all nodes.
     dives
        Numeric range of total dive number, `None` if any dive.

    .. seealso:: :py:func:`parse_range`
    .. seealso:: :py:func:`find_dives`
    """
    nodes = [] if nodes is None else nodes
    data = (ku.find(f, ku.XP_FIND_DIVES, nodes=q, dives=dives) \
        for q, f in lzip(nodes, files))
    return ichain(data)
Example #8
0
def test_iters(shape, chunkshape1, blockshape1, chunkshape2, blockshape2,
               dtype):
    size = int(np.prod(shape))
    nparray = np.ones(size, dtype=dtype).reshape(shape)
    a = cat.from_buffer(bytes(nparray),
                        nparray.shape,
                        itemsize=nparray.itemsize,
                        chunkshape=chunkshape1,
                        blockshape=blockshape1)

    itemsize = np.dtype(dtype).itemsize
    b = cat.empty(shape,
                  chunkshape=chunkshape2,
                  blockshape=blockshape2,
                  itemsize=itemsize)
    itershape = chunkshape2 if chunkshape2 is not None else b.shape

    for (block_r, info_r), (block_w, info_w) in lzip(a.iter_read(itershape),
                                                     b.iter_write()):
        block_w[:] = block_r

    nparray2 = b.to_numpy(dtype)
    np.testing.assert_equal(nparray, nparray2)
Example #9
0
 def __add__(self, other):  # f + g
     other = self.convert(other)
     return IntPolynomial(x + y for x, y in lzip(self, other, fillvalue=0))
Example #10
0
 def __sub__(self, g):  # f - g
     g = self.valid(g)
     return IntPolynomial(x - y for x, y in lzip(self, g, fillvalue=0))
Example #11
0
 def __add__(self, g):  # f + g
     """
     Addition with another polynomial or an integer.
     """
     g = self.valid(g)
     return IntPolynomial(x + y for x, y in lzip(self, g, fillvalue=0))
Example #12
0
 def __sub__(self, other):  # f - g
     other = self.convert(other)
     return self.__class__(x - y for x, y in lzip(self, other, fillvalue=0))
Example #13
0
# Create a numpy array
nparray = np.arange(int(np.prod(shape)), dtype=dtype).reshape(shape)

# Create a caterva array from a numpy array
a = cat.asarray(nparray)

# Create an empty caterva array (on disk)
b = cat.empty(shape,
              itemsize,
              dtype=str(dtype),
              chunkshape=chunkshape,
              blockshape=blockshape,
              filename=filename)

# Fill an empty caterva array using a block iterator
for block, info in b.iter_write():
    block[:] = bytes(nparray[info.slice])

# Load file
c = cat.from_file(filename)

# Assert both caterva arrays
itershape = (5, 5)
for (block1, info1), (block2, info2) in lzip(a.iter_read(itershape),
                                             c.iter_read(itershape)):
    np.testing.assert_equal(np.asarray(block1), np.asarray(block2))

# Remove file on disk
os.remove(filename)
 def __add__(self, g):
     """Add a polynomial or an integer.
     """
     g = self.valid(g)
     return IntPolynomial(x + y for x, y in lzip(self, g, fillvalue=0))
Example #15
0
 def __sub__(self, other):  # f - g
     other = self.convert(other)
     return IntPolynomial(x - y for x, y in lzip(self, other, fillvalue=0))
Example #16
0
assert (b.has_metalayer("numpy") is True)
assert (b.get_metalayer("numpy") == {
    b"dtype": bytes(str(np.dtype(dtype)), "utf-8")
})
assert (b.has_metalayer("test") is True)
assert (b.get_metalayer("test") == {b"lorem": 1234})
assert (b.update_metalayer("test", {b"lorem": 4321}) >= 0)
assert (b.get_metalayer("test") == {b"lorem": 4321})

# Fill an empty caterva array using a block iterator
for block, info in b.iter_write():
    block[:] = bytes(nparray[info.slice])

# Assert that both caterva arrays are equal
itershape = (5, 5)
for (block1, info1), (block2, info2) in lzip(a.iter_read(blockshape),
                                             b.iter_read(blockshape)):
    np.testing.assert_equal(np.asarray(block1), np.asarray(block2))

assert (b.update_usermeta({
    b"author": b"cat4py example",
    b"description": b"lorem ipsum"
}) >= 0)
assert (b.get_usermeta() == {
    b"author": b"cat4py example",
    b"description": b"lorem ipsum"
})

assert (b.update_usermeta({b"author": b"cat4py example"}) >= 0)
assert (b.get_usermeta() == {b"author": b"cat4py example"})

# Remove file on disk
Example #17
0
 def __add__(self, other):  # f + g
     other = self.convert(other)
     return self.__class__(x + y for x, y in lzip(self, other, fillvalue=0))