Exemple #1
0
def prune(rnkdbs: Sequence[Type[RankingDatabase]],
          modules: Sequence[Regulon],
          motif_annotations_fname: str,
          rank_threshold: int = 1500,
          auc_threshold: float = 0.05,
          nes_threshold=3.0,
          motif_similarity_fdr: float = 0.001,
          orthologuous_identity_threshold: float = 0.0,
          avgrcc_sample_frac: float = None,
          weighted_recovery=False,
          client_or_address='custom_multiprocessing',
          num_workers=None,
          module_chunksize=100) -> Sequence[Regulon]:
    """
    Calculate all regulons for a given sequence of ranking databases and a sequence of co-expression modules.
    The number of regulons derived from the supplied modules is usually much lower. In addition, the targets of the
    retained modules is reduced to only these ones for which a cis-regulatory footprint is present.

    :param rnkdbs: The sequence of databases.
    :param modules: The sequence of modules.
    :param motif_annotations_fname: The name of the file that contains the motif annotations to use.
    :param rank_threshold: The total number of ranked genes to take into account when creating a recovery curve.
    :param auc_threshold: The fraction of the ranked genome to take into account for the calculation of the
        Area Under the recovery Curve.
    :param nes_threshold: The Normalized Enrichment Score (NES) threshold to select enriched features.
    :param motif_similarity_fdr: The maximum False Discovery Rate to find factor annotations for enriched motifs.
    :param orthologuous_identity_threshold: The minimum orthologuous identity to find factor annotations
        for enriched motifs.
    :param avgrcc_sample_frac: The fraction of the features to use for the calculation of the average curve, If None
        then all features are used.
    :param weighted_recovery: Use weights of a gene signature when calculating recovery curves?
    :param num_workers: If not using a cluster, the number of workers to use for the calculation.
        None of all available CPUs need to be used.
    :param module_chunksize: The size of the chunk to use when using the dask framework.
    :param client_or_address: The client of IP address of the scheduler when working with dask. For local multi-core
        systems 'custom_multiprocessing' or 'dask_multiprocessing' can be supplied.
    :return: A sequence of regulons.
    """
    # Always use module2features_auc1st_impl not only because of speed impact but also because of reduced memory footprint.
    module2features_func = partial(module2features_auc1st_impl,
                                   rank_threshold=rank_threshold,
                                   auc_threshold=auc_threshold,
                                   nes_threshold=nes_threshold,
                                   avgrcc_sample_frac=avgrcc_sample_frac,
                                   filter_for_annotation=True)
    transformation_func = partial(modules2regulons,
                                  module2features_func=module2features_func,
                                  weighted_recovery=weighted_recovery)
    from toolz.curried import reduce
    aggregation_func = delayed(
        reduce(concat)
    ) if client_or_address != 'custom_multiprocessing' else reduce(concat)
    return _distributed_calc(rnkdbs, modules, motif_annotations_fname,
                             transformation_func, aggregation_func,
                             motif_similarity_fdr,
                             orthologuous_identity_threshold,
                             client_or_address, num_workers, module_chunksize)
Exemple #2
0
def apply_params(url, params):
    route_params = keyfilter(params_filter, params)
    return (
        reduce(lambda acc, kv: acc.replace(kv[0], kv[1]), route_params.items(),
               url),
        keyfilter(not_params_filter, params),
    )
Exemple #3
0
    def prototype(self, inputs: tf.Tensor):
        input_to_layers = [[[(inputs, tf.ones(
            (tf.shape(inputs)[0], )))]]] + self.tree
        proto_output = reduce(lambda x, f: self.forward(x, f),
                              input_to_layers[:-1])

        return [x[0] for x in list(chain(*proto_output))]
Exemple #4
0
def main():
    """Main method."""
    # create a player named tuple
    Player = namedtuple('Player', ['first', 'last', 'number', 'team', 'city'])

    # create some player named tuples
    m_j = Player(first='Michael', last='Jordan', number='23', team='Bulls', city='Chicago')
    k_b = Player(first='Kobe', last='Bryant', number='24', team='Lakers', city='Los Angeles')
    l_b = Player(first='LeBron', last='James', number='23', team='Cavaliers', city='Cleveland')
    k_p = Player(first='Kristaps', last='Porzingis', number='6', team='Knicks', city='New York')
    k_d = Player(first='Kevin', last='Durant', number='35', team='Warriors', city='Oakland')

    # store the players in tuple
    players = (m_j, k_b, l_b, k_p, k_d)

    # filter
    two_three = filter(lambda x: x.number == '23', players)
    print(tuple(two_three)) # => (Player(first='Michael', last='Jordan', number='23', team='Bulls',
                            # city='Chicago'), Player(first='LeBron', last='James', number='23',
                            # team='Cavaliers', city='Cleveland'))

    # map
    result = map(lambda x: ''.join([x.first, ' ', x.last]), players)
    print(tuple(result)) # => ('Michael Jordan', 'Kobe Bryant', 'LeBron James', 'Kristaps Porzingis', 'Kevin Durant')

    # reduce
    result = reduce(lambda x, y: x + y, [1, 2, 3], 0)
    print(result) # => 6

    # compose
    nums = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
    c_greater = curry(greater)
    greater_zero = c_greater(y_val=0)
    result = compose(filter(greater_zero), filter(iseven))(nums)
    print(tuple(result)) # => (2, 4)
Exemple #5
0
    def call(self, inputs: tf.Tensor):
        input_to_layers = [[[(inputs, tf.ones(
            (tf.shape(inputs)[0], )))]]] + self.tree

        leaf_output = reduce(lambda x, f: self.forward(x, f), input_to_layers)

        return self.head(leaf_output)
Exemple #6
0
    def call(self, inputs: tf.Tensor):

        weighted_inputs = [x[0] * tf.reshape(x[1], (-1, 1)) for x in inputs]

        output = reduce(tf.add, weighted_inputs)

        return output
 def __get_all_metrics_for_each_class(self):
     def __get_all_metrics_for_class(confusion_table):
         return pmap({
             str(confusion_table.get_class_name()): pmap({
                 "Accuracy": confusion_table.accuracy,
                 "Precision": confusion_table.precision,
                 "Recall": confusion_table.recall,
                 "Specificity": confusion_table.specificity,
                 "F1score": confusion_table.f1score,
                 "Fall Out": confusion_table.fall_out,
                 "Miss Rate": confusion_table.miss_rate,
                 "False Discovery Rate": confusion_table.FDR,
                 "False Omission Rate": confusion_table.FOR,
                 "Negative Predictive Value": confusion_table.NPV,
                 "Positive Likelihood Ratio": confusion_table.PLR,
                 "Negative Likelihood Ratio": confusion_table.NLR,
                 "Diagnostic Odds Ratio": confusion_table.DOR,
             })
         })
     return pipe(
         self.__confusion_tables,
         itervalues,
         map(__get_all_metrics_for_class),
         reduce(lambda x, y: x + y),
     )
def __get_rows(data, max_length_per_column):
    return pipe(
        data,
        iterkeys,
        map(lambda key: __get_row(data, key, max_length_per_column)),
        reduce(lambda x, y: x + y)
    )
Exemple #9
0
def _wraps_magic(f, line, cell="""""", **kwargs):
    def _preprocess_line(line):
        """I don't understand how I would use this yet."""
        return line.strip().split(' ', 1)

    if not cell:
        line, cell = _preprocess_line(line)

    args = magic_arguments.parse_argstring(_wraps_magic, line.strip())

    retval = f(cell)

    if args.name:
        if '.' in args.name or '[' in args.name:
            path = args.name.split('.')
            var = get_ipython().user_ns[path[0]]
            setattr(
                reduce(lambda x, y: getattr(x, y) if hasattr(x, y) else x[y],
                       path[1:-1], var), path[-1], retval)
        else:
            get_ipython().user_ns[args.name] = retval

    if args.display:
        disp = kwargs['display'] if 'display' in kwargs else args.display
        if isinstance(disp, str):
            return display.display(getattr(display, disp)(retval))
        elif isinstance(disp, Callable):
            return disp(retval)
Exemple #10
0
def non_yaml_lines(lines):
    return pipe(
        [-1] + all_indices(lines) + [len(lines)],
        partition(2),
        vmap(lambda s, e: lines[s + 1:e]),
        reduce(lambda a, b: a + b)
    )
Exemple #11
0
def check_slope(tree_field, slope):
    num_trees = pipe(
        walk(tree_field, slope),
        map(is_tree),
        reduce(add),
    )
    return num_trees
Exemple #12
0
def process_nearby_ticket(valid_ranges, ticket):
    def is_invalid(value):
        return not any(map(lambda r: value in r, valid_ranges))

    return pipe(
        [0] + list(ticket),
        reduce(lambda acc, value: acc + value if is_invalid(value) else acc),
    )
Exemple #13
0
    def leaf(self, inputs: tf.Tensor):

        input_to_layers = [[[(inputs, tf.ones(
            (tf.shape(inputs)[0], )))]]] + self.tree
        proto_output = reduce(lambda x, f: self.forward(x, f), input_to_layers)

        leaf_preductions = [x[0] for x in proto_output]

        return list(map(tf.nn.softmax, leaf_preductions))
Exemple #14
0
def cross(dists, f=None):
    if f is None:
        f = lambda *args: args
    outcomes = Counter()
    for outcome_probs in it.product(*dists):
        o, p = zip(*outcome_probs)
        outcomes[f(*o)] += reduce(lambda x, y: x * y, p)

    return Categorical(outcomes.keys(), outcomes.values())
Exemple #15
0
def calculate_coverage(rules: Collection[Rule], x_train):
    feature_ranges = np.ptp(x_train, axis=0)
    total_area = 0
    max_area = reduce(lambda a, b: a * b)(feature_ranges)
    for rule in rules:
        bounded_rule = bound_rule(rule, x_train)
        this_rule_statement_ranges = []
        for feature, statements in bounded_rule.get_statements_by_feature().items():
            sorted_thresholds = pipe(
                statements, map(lambda s: s.threshold), sorted, list
            )

            this_rule_statement_ranges.append(
                np.abs(sorted_thresholds[-1] - sorted_thresholds[0])
            )

        total_area = total_area + reduce(lambda a, b: a * b)(this_rule_statement_ranges)

    return 1 if total_area / max_area > 1 else total_area / max_area
 def make_data(val):
     clients = compose(count, unique, pluck('customer'), lambda: val)
     animals = compose(valmap(count), groupby('species'), lambda: val)
     return {
         'total_val':
         reduce(lambda total, x: total + x.get('total_vat'), val, 0.00),
         'animals':
         _get_dict_to_csv(animals()),
         'clients':
         clients()
     }
Exemple #17
0
 def select_files(cls, files):
     prefixes = pipe(reduce(cls.collect_prefix_dates, files, list()),
                     unique, sorted, map(lambda d: d.format("YYYY_MMM")), list)
     if len(prefixes) == 0:
         return files
     else:
         select_prefix = last(prefixes)
         skip_prefixes = prefixes[:-1]
         selected_files = pipe(files,
                               filter(lambda file: not any([prefix in file for prefix in skip_prefixes])), list)
         return selected_files
Exemple #18
0
def test_flowly_kv_transform__reduce(executor):
    actual = executor(
        kv_transform(reduce(lambda a, b: a + b)),
        [(i % 2, i) for i in [1, 2, 3, 4, 5, 6, 7]],
        npartitions=3,
    )

    assert sorted(actual) == sorted([
        (1, sum([1, 3, 5, 7])),
        (0, sum([2, 4, 6])),
    ])
 def __get_basic_metrics_for_each_class(self):
     def __get_basic_metrics_for_class(confusion_table):
         return pmap({
             str(confusion_table.get_class_name()): pmap({
                 "Accuracy": confusion_table.accuracy,
                 "Precision": confusion_table.precision,
                 "Recall": confusion_table.recall,
                 "Specificity": confusion_table.specificity,
                 "F1score": confusion_table.f1score
             })
         })
     return pipe(
         self.__confusion_tables,
         itervalues,
         map(__get_basic_metrics_for_class),
         reduce(lambda x, y: x + y),
     )
def __get_row(data, class_key, max_length_per_column):
    def format_value(value, max_len):
        return __VERTICAL_SEPARATOR + __WHITESPACE + str(value) + \
               __WHITESPACE * ((max_len - len(str(value))) + 1)

    row_header = __VERTICAL_SEPARATOR + __WHITESPACE + \
                 str(class_key) + \
                 __WHITESPACE * \
                 (max_length_per_column[0] - len(str(class_key)) + 1)
    row_values = pipe(
        zip(data[class_key].itervalues(), max_length_per_column[1:]),
        map(lambda (value, max_len): format_value(value, max_len)),
        list,
        reduce(lambda x, y: x + y),
    )
    return row_header + row_values + __VERTICAL_SEPARATOR + \
           __LINE_BREAK + __get_row_separator(max_length_per_column)
 def get_token_string(given_values):
     """Return a token string, if the given values are a list of dictionaries"""
     # check if it is a list of token-related information
     if (isinstance(given_values, list) and 
         len(given_values) > 0 and
         isinstance(given_values[0], dict)):
         return tz.pipe(
             given_values,
             tz.map(lambda x: x['token']),
             tz.map(wrap_in_highlight_link), # wrap in link to highlight words
             tz.reduce(lambda x,y: u"{}, {}".format(x, y)))
     # return empty string for empty lists
     elif isinstance(given_values, list) and len(given_values) == 0:
         return ''
     # check if it is a date range in need of formating
     elif isinstance(given_values, list) and len(given_values) == 2:
         return format_date_range(given_values)
     else:
         return given_values
def __get_header(headers_list, max_length_per_column):
    def get_empty_header(max_length):
        return StringValueObject(
            __VERTICAL_SEPARATOR +
            __WHITESPACE * (max_length + 2)
        )

    def get_column_header(header_name, max_col_length):
        return StringValueObject(
            __VERTICAL_SEPARATOR + __WHITESPACE + header_name +
            (__WHITESPACE * (max_col_length - len(str(header_name)) + 1))
        )

    empty_header = get_empty_header(max_length_per_column[0])
    headers = pipe(
        zip(headers_list, max_length_per_column[1:]),
        map(lambda (header_name, length):
            get_column_header(header_name, length)),
        list,
        reduce(lambda x, y: x + y)
    )
    return empty_header + headers + __VERTICAL_SEPARATOR + __LINE_BREAK
Exemple #23
0
def lagger(
    dataset:pd.DataFrame, n_lags:int,
    price_columns : Union[str,List[str]]) -> pd.DataFrame:
    """
    Create columns of time lags

    Inputs
    ------
    dataset : dataframe to lag 
    n_lags : number of time lags
    price_columns :
    y_col : target column name(s)
    Returns
    ------
    result : lagged dataframe
    """
    from toolz.curried import reduce
    df = reduce(
        lambda df, lag: df.assign(**{col + '_' +str(lag): dataset[[col]].shift(lag).values for col in price_columns}),
        range(1, n_lags + 1),
        dataset[price_columns])

    result = df.assign(**{col: dataset[col] for col in dataset.drop(price_columns, axis=1).columns}).fillna(0)
    return result[sorted(result.columns)]
Exemple #24
0
def process_nearby_ticket(valid_ranges, ticket):
    def is_invalid(value):
        return not any(map(lambda r: value in r, valid_ranges))

    return pipe(
        [0] + list(ticket),
        reduce(lambda acc, value: acc + value if is_invalid(value) else acc),
    )


chunks = chunker(lines)
rules = pipe(
    next(chunks),
    map(lambda rule: process_rule(rule)),
    reduce(lambda acc, d: merge(acc, d)),
)
all_valid_ranges = list(get_all_valid_ranges(rules))
my_ticket = process_my_ticket(next(chunks)[1])
nearby_tickets = pipe(
    next(chunks)[1:],
    map(parse_ticket),
)
valid_tickets = pipe(
    nearby_tickets,
    filter(lambda ticket: process_nearby_ticket(all_valid_ranges, ticket) == 0),
)

# produce a mask for each field, indicating whether or not
# each index of all nearby tickets is valid for that field
fields = rules.keys()
Exemple #25
0
def parse_tiles(lines):
    tiles = dict()
    for chunk in chunker(lines):
        tile_id = chunk.pop(0)
        tile_id = int(re.match("Tile (\d+):", tile_id).groups()[0])  # noqa
        tiles[tile_id] = Tile(copy(chunk))
    return tiles


def tiles_match(t1, t2):
    """
    Checks if there exists any orientation of tile t1 and tile 2 such that
    they share a matching edge.
    """
    for e1, e2 in product(t1.edges(), t2.edges()):
        if e1 == e2:
            return True
    return False


tiles = parse_tiles(lines)
matches = defaultdict(bool)
for tid1, tid2 in combinations(tiles.keys(), 2):
    if tiles_match(tiles[tid1], tiles[tid2]):
        matches[tid1] += 1
        matches[tid2] += 1

corner_ids = pipe(matches, valfilter(lambda x: x == 2), dict.keys)
assert len(corner_ids) == 4
print(reduce(mul, corner_ids))
Exemple #26
0
    def leaf_probabilty(self, inputs: tf.Tensor):
        input_to_layers = [[[(inputs, tf.ones(
            (tf.shape(inputs)[0], )))]]] + self.tree
        proto_output = reduce(lambda x, f: self.forward(x, f), input_to_layers)

        return [x[1] for x in proto_output]
Exemple #27
0
def gen_key_avgs_from_dicts(obj: List) -> Dict[str, float]:
    sum_values_by_key = reduce(lambda x, y: dict((k, v + y.get(k, 0)) for k, v in x.items()), obj)
    return {k: float(v) / len(obj) for k, v in sum_values_by_key.items()}
Exemple #28
0
def stack(seq, axis=0):
    """
    Stack arrays along a new axis

    Given a sequence of dask Arrays form a new dask Array by stacking them
    along a new dimension (axis=0 by default)

    Example
    -------

    Create slices

    >>> import dask.array as da
    >>> import numpy as np

    >>> data = [from_array(np.ones((4, 4)), blockshape=(2, 2))
    ...          for i in range(3)]

    >>> x = da.stack(data, axis=0)
    >>> x.shape
    (3, 4, 4)

    >>> da.stack(data, axis=1).shape
    (4, 3, 4)

    >>> da.stack(data, axis=-1).shape
    (4, 4, 3)

    Result is a new dask Array

    See Also:
        concatenate
    """
    n = len(seq)
    ndim = len(seq[0].shape)
    if axis < 0:
        axis = ndim + axis + 1
    if axis > ndim:
        raise ValueError("Axis must not be greater than number of dimensions"
                         "\nData has %d dimensions, but got axis=%d" %
                         (ndim, axis))

    assert len(set(a.blockdims for a in seq)) == 1  # same blockshape
    shape = seq[0].shape[:axis] + (len(seq), ) + seq[0].shape[axis:]
    blockdims = (seq[0].blockdims[:axis] + ((1, ) * n, ) +
                 seq[0].blockdims[axis:])

    name = next(stacked_names)
    keys = list(product([name], *[range(len(bd)) for bd in blockdims]))

    names = [a.name for a in seq]
    inputs = [(names[key[axis + 1]], ) + key[1:axis + 1] + key[axis + 2:]
              for key in keys]
    values = [(getitem, inp, (slice(None, None, None), ) * axis + (None, ) +
               (slice(None, None, None), ) * (ndim - axis)) for inp in inputs]

    dsk = dict(zip(keys, values))
    dsk2 = merge(dsk, *[a.dask for a in seq])

    if all(a._dtype is not None for a in seq):
        dt = reduce(np.promote_types, [a._dtype for a in seq])
    else:
        dt = None

    return Array(dsk2, name, shape, blockdims=blockdims, dtype=dt)
Exemple #29
0
def test_toolz_reduce(executor):
    assert executor(reduce(op.add), range(100),
                    npartitions=5) == sum(range(100))
Exemple #30
0
def concatenate(seq, axis=0):
    """
    Concatenate arrays along an existing axis

    Given a sequence of dask Arrays form a new dask Array by stacking them
    along an existing dimension (axis=0 by default)

    Example
    -------

    Create slices

    >>> import dask.array as da
    >>> import numpy as np

    >>> data = [from_array(np.ones((4, 4)), blockshape=(2, 2))
    ...          for i in range(3)]

    >>> x = da.concatenate(data, axis=0)
    >>> x.shape
    (12, 4)

    >>> da.concatenate(data, axis=1).shape
    (4, 12)

    Result is a new dask Array

    See Also:
        stack
    """
    n = len(seq)
    ndim = len(seq[0].shape)
    if axis < 0:
        axis = ndim + axis
    if axis >= ndim:
        raise ValueError("Axis must be less than than number of dimensions"
                         "\nData has %d dimensions, but got axis=%d" %
                         (ndim, axis))

    bds = [a.blockdims for a in seq]

    if not all(
            len(set(bds[i][j] for i in range(n))) == 1
            for j in range(len(bds[0])) if j != axis):
        raise ValueError("Block shapes do not align")

    shape = (seq[0].shape[:axis] + (sum(a.shape[axis] for a in seq), ) +
             seq[0].shape[axis + 1:])
    blockdims = (seq[0].blockdims[:axis] + (sum([bd[axis] for bd in bds],
                                                ()), ) +
                 seq[0].blockdims[axis + 1:])

    name = next(concatenate_names)
    keys = list(product([name], *[range(len(bd)) for bd in blockdims]))

    cum_dims = [0] + list(
        accumulate(add, [len(a.blockdims[axis]) for a in seq]))
    names = [a.name for a in seq]
    values = [
        (names[bisect(cum_dims, key[axis + 1]) - 1], ) + key[1:axis + 1] +
        (key[axis + 1] - cum_dims[bisect(cum_dims, key[axis + 1]) - 1], ) +
        key[axis + 2:] for key in keys
    ]

    dsk = dict(zip(keys, values))
    dsk2 = merge(dsk, *[a.dask for a in seq])

    if all(a._dtype is not None for a in seq):
        dt = reduce(np.promote_types, [a._dtype for a in seq])
    else:
        dt = None

    return Array(dsk2, name, shape, blockdims=blockdims, dtype=dt)
Exemple #31
0
 def sample(n=None):
     return reduce(np.maximum, [d.sample(n) for d in dists])
Exemple #32
0
def concatenate(seq, axis=0):
    """
    Concatenate arrays along an existing axis

    Given a sequence of dask Arrays form a new dask Array by stacking them
    along an existing dimension (axis=0 by default)

    Example
    -------

    Create slices

    >>> import dask.array as da
    >>> import numpy as np

    >>> data = [from_array(np.ones((4, 4)), blockshape=(2, 2))
    ...          for i in range(3)]

    >>> x = da.concatenate(data, axis=0)
    >>> x.shape
    (12, 4)

    >>> da.concatenate(data, axis=1).shape
    (4, 12)

    Result is a new dask Array

    See Also:
        stack
    """
    n = len(seq)
    ndim = len(seq[0].shape)
    if axis < 0:
        axis = ndim + axis
    if axis >= ndim:
        raise ValueError("Axis must be less than than number of dimensions"
                "\nData has %d dimensions, but got axis=%d" % (ndim, axis))

    bds = [a.blockdims for a in seq]

    if not all(len(set(bds[i][j] for i in range(n))) == 1
            for j in range(len(bds[0])) if j != axis):
        raise ValueError("Block shapes do not align")

    shape = (seq[0].shape[:axis]
            + (sum(a.shape[axis] for a in seq),)
            + seq[0].shape[axis + 1:])
    blockdims = (  seq[0].blockdims[:axis]
                + (sum([bd[axis] for bd in bds], ()),)
                + seq[0].blockdims[axis + 1:])

    name = next(concatenate_names)
    keys = list(product([name], *[range(len(bd)) for bd in blockdims]))

    cum_dims = [0] + list(accumulate(add, [len(a.blockdims[axis]) for a in seq]))
    names = [a.name for a in seq]
    values = [(names[bisect(cum_dims, key[axis + 1]) - 1],)
                + key[1:axis + 1]
                + (key[axis + 1] - cum_dims[bisect(cum_dims, key[axis+1]) - 1],)
                + key[axis + 2:]
                for key in keys]

    dsk = dict(zip(keys, values))
    dsk2 = merge(dsk, *[a.dask for a in seq])

    if all(a._dtype is not None for a in seq):
        dt = reduce(np.promote_types, [a._dtype for a in seq])
    else:
        dt = None

    return Array(dsk2, name, shape, blockdims=blockdims, dtype=dt)
Exemple #33
0
def stack(seq, axis=0):
    """
    Stack arrays along a new axis

    Given a sequence of dask Arrays form a new dask Array by stacking them
    along a new dimension (axis=0 by default)

    Example
    -------

    Create slices

    >>> import dask.array as da
    >>> import numpy as np

    >>> data = [from_array(np.ones((4, 4)), blockshape=(2, 2))
    ...          for i in range(3)]

    >>> x = da.stack(data, axis=0)
    >>> x.shape
    (3, 4, 4)

    >>> da.stack(data, axis=1).shape
    (4, 3, 4)

    >>> da.stack(data, axis=-1).shape
    (4, 4, 3)

    Result is a new dask Array

    See Also:
        concatenate
    """
    n = len(seq)
    ndim = len(seq[0].shape)
    if axis < 0:
        axis = ndim + axis + 1
    if axis > ndim:
        raise ValueError("Axis must not be greater than number of dimensions"
                "\nData has %d dimensions, but got axis=%d" % (ndim, axis))

    assert len(set(a.blockdims for a in seq)) == 1  # same blockshape
    shape = seq[0].shape[:axis] + (len(seq),) + seq[0].shape[axis:]
    blockdims = (  seq[0].blockdims[:axis]
                + ((1,) * n,)
                + seq[0].blockdims[axis:])

    name = next(stacked_names)
    keys = list(product([name], *[range(len(bd)) for bd in blockdims]))

    names = [a.name for a in seq]
    inputs = [(names[key[axis+1]],) + key[1:axis + 1] + key[axis + 2:]
                for key in keys]
    values = [(getitem, inp, (slice(None, None, None),) * axis
                           + (None,)
                           + (slice(None, None, None),) * (ndim - axis))
                for inp in inputs]

    dsk = dict(zip(keys, values))
    dsk2 = merge(dsk, *[a.dask for a in seq])

    if all(a._dtype is not None for a in seq):
        dt = reduce(np.promote_types, [a._dtype for a in seq])
    else:
        dt = None

    return Array(dsk2, name, shape, blockdims=blockdims, dtype=dt)
Exemple #34
0
def get_rules_statements(rules):
    return pipe(
        rules,
        map(lambda r: list(r.statements)),
        reduce(list.__add__),
    )
Exemple #35
0
def verify(*conditions) -> Callable[[Any], bool]:
    return compose(
        reduce(and_),
        juxt(*conditions)
    )
def _merge_redirects(redirects):
    def _merge_redirect(mapping, redirect):
        return t.assoc(mapping, redirect['from'], redirect['to'])

    return t.reduce(_merge_redirect, redirects, {})
Exemple #37
0
def test_reduce():
    assert reduce(add)((1, 2, 3)) == 6
Exemple #38
0
    rows = map(lambda line: line.strip(), lines)
    tree_field = map(lambda row: cycle(row), rows)
    return tree_field


def is_tree(s):
    return s == "#"


def walk(tree_field, slope=Slope(1, 1)):
    for i, row in enumerate(islice(tree_field, slope.down, None, slope.down)):
        consume(row, slope.across * (i + 1))
        yield next(row)


def check_slope(tree_field, slope):
    num_trees = pipe(
        walk(tree_field, slope),
        map(is_tree),
        reduce(add),
    )
    return num_trees


product = pipe(
    SLOPES,
    map(lambda slope: check_slope(get_tree_field(), slope)),
    reduce(mul),
)
print(product)