Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def partial_class(cls, *args, **kwds):
'''the way to generate partial from a constructor'''
class NewCls(cls):
__init__ = curry(cls.__init__, *args, **kwds)
return NewCls
def compute_down(expr, data, map=map, **kwargs):
leaf = expr._leaves()[0]
(chunk, chunk_expr), (agg, agg_expr) = split(leaf, expr)
indices = list(range(len(data.data)))
parts = list(map(curry(compute_chunk, data.data, chunk, chunk_expr),
indices))
if isinstance(parts[0], np.ndarray):
intermediate = np.concatenate(parts)
elif isinstance(parts[0], pd.DataFrame):
intermediate = pd.concat(parts)
elif isinstance(parts[0], (Iterable, Iterator)):
intermediate = concat(parts)
return compute(agg_expr, {agg: intermediate})
@curry
def _apply_augmentation2D(aug, numpy_array):
assert len(numpy_array.shape) == 2, "This method only accepts 2D arrays"
return aug(image=numpy_array)["image"]
@curry
def apply(f, *args, **kwargs):
"""Apply a function to arguments.
Parameters
----------
f : callable
The function to call.
*args, **kwargs
**kwargs
Arguments to feed to the callable.
Returns
-------
a : any
The result of ``f(*args, **kwargs)``
@curry
def find_last_index(p, xs):
"""Returns the index of the last element of the list which matches the
predicate, or -1 if no element matches.
Acts as a transducer if a transformer is given in list position"""
for i in (i for i, x in enumerate(reversed(xs)) if p(x)):
return len(xs) - i
@curry
@log_learner_time(learner_name='onehot_categorizer')
def onehot_categorizer(df: pd.DataFrame,
columns_to_categorize: List[str],
hardcode_nans: bool = False,
drop_first_column: bool = False,
store_mapping: bool = False) -> LearnerReturnType:
"""
Onehot encoding on categorical columns.
Encoded columns are removed and substituted by columns named
`fklearn_feat__col==val`, where `col` is the name of the column
and `val` is one of the values the feature can assume.
Parameters
----------
df : pd.DataFrame
A Pandas' DataFrame that must contain `columns_to_categorize` columns.
@curry
def assoc(key, value, object):
"""Makes a shallow clone of an object, setting or overriding the specified
property with the given value. Note that this copies and flattens prototype
properties onto the new object as well. All non-primitive properties are
copied by reference"""
o = clone(object)
o[key] = value
return o
@curry
def curry_n(n, f):
"""Returns a curried equivalent of the provided function, with the specified
arity. The curried function has two unusual capabilities. First, its
arguments needn't be provided one at a time. If g is R.curryN(3, f), the
following are equivalent:
g(1)(2)(3)
g(1)(2, 3)
g(1, 2)(3)
g(1, 2, 3)
Secondly, the special placeholder value R.__ may be used to specify
"gaps", allowing partial application of any combination of arguments,
regardless of their positions. If g is as above and _ is R.__,
the following are equivalent:
g(1, 2, 3)
g(_, 2, 3)(1)
g(_, _, 3)(1)(2)
@curry
def equals(x, y):
"""Returns true if its arguments are equivalent, false otherwise. Handles
cyclical data structures.
Dispatches symmetrically to the equals methods of both arguments, if
present"""
return x == y
@curry
def intersection(list1, list2):
"""Combines two lists into a set (i.e. no duplicates) composed of those
elements common to both lists"""
return list(set(list1).intersection(set(list2)))