How to use _coerce method in pandera

Best Python code snippet using pandera_python

expression_typecheck.py

Source:expression_typecheck.py Github

copy

Full Screen

...35 @abc.abstractmethod36 def str_t(self) -> str:37 ...38 def requires_conversion(self, t: HailType) -> bool:39 assert self.can_coerce(t), t40 return self._requires_conversion(t)41 @abc.abstractmethod42 def _requires_conversion(self, t: HailType) -> bool:43 """Assumes that :meth:`can_coerce` is ``True``"""44 ...45 @abc.abstractmethod46 def can_coerce(self, t: HailType) -> bool:47 ...48 def coerce(self, x) -> Expression:49 x = to_expr(x)50 if not self.can_coerce(x.dtype):51 raise ExpressionException(f"cannot coerce type '{x.dtype}' to type '{self.str_t}'")52 if self._requires_conversion(x.dtype):53 return self._coerce(x)54 else:55 return x56 def _coerce(self, x: Expression) -> Expression:57 # many coercers don't write any coercion rules, so don't override58 raise AssertionError()59 def expects(self):60 return f"expression of type {self.str_t}"61 def check(self, x: Any, caller: str, param: str) -> Any:62 try:63 return self.coerce(to_expr(x))64 except ExpressionException as e:65 raise TypecheckFailure from e66 def format(self, arg):67 if isinstance(arg, Expression):68 return str(arg)69 else:70 return super(ExprCoercer, self).format(arg)71class AnyCoercer(ExprCoercer):72 @property73 def str_t(self):74 return 'any'75 def _requires_conversion(self, t: HailType) -> bool:76 return False77 def can_coerce(self, t: HailType) -> bool:78 return True79class BoolCoercer(ExprCoercer):80 @property81 def str_t(self):82 return 'bool'83 def _requires_conversion(self, t: HailType) -> bool:84 return False85 def can_coerce(self, t: HailType) -> bool:86 return t == tbool87class Int32Coercer(ExprCoercer):88 @property89 def str_t(self):90 return 'int32'91 def _requires_conversion(self, t: HailType) -> bool:92 return t != tint3293 def can_coerce(self, t: HailType) -> bool:94 return t in (tbool, tint32)95 def _coerce(self, x):96 return x._method("toInt32", tint32)97class Int64Coercer(ExprCoercer):98 @property99 def str_t(self):100 return 'int64'101 def _requires_conversion(self, t: HailType) -> bool:102 return t != tint64103 def can_coerce(self, t: HailType) -> bool:104 return t in (tbool, tint32, tint64)105 def _coerce(self, x):106 return x._method("toInt64", tint64)107class Float32Coercer(ExprCoercer):108 @property109 def str_t(self):110 return 'float32'111 def _requires_conversion(self, t: HailType) -> bool:112 return t != tfloat32113 def can_coerce(self, t: HailType) -> bool:114 return t in (tbool, tint32, tint64, tfloat32)115 def _coerce(self, x):116 return x._method("toFloat32", tfloat32)117class Float64Coercer(ExprCoercer):118 @property119 def str_t(self):120 return 'float64'121 def _requires_conversion(self, t: HailType) -> bool:122 return t != tfloat64123 def can_coerce(self, t: HailType) -> bool:124 return t in (tbool, tint32, tint64, tfloat32, tfloat64)125 def _coerce(self, x):126 return x._method("toFloat64", tfloat64)127class StringCoercer(ExprCoercer):128 @property129 def str_t(self):130 return 'str'131 def _requires_conversion(self, t: HailType) -> bool:132 return False133 def can_coerce(self, t: HailType) -> bool:134 return t == tstr135class CallCoercer(ExprCoercer):136 @property137 def str_t(self):138 return 'call'139 def _requires_conversion(self, t: HailType) -> bool:140 return False141 def can_coerce(self, t: HailType) -> bool:142 return t == tcall143class LocusCoercer(ExprCoercer):144 def __init__(self, rg: Optional['hl.ReferenceGenome'] = None):145 super(LocusCoercer, self).__init__()146 self.rg = rg147 @property148 def str_t(self):149 return f'locus<{"any" if not self.rg else self.rg}>'150 def _requires_conversion(self, t: HailType) -> bool:151 return False152 def can_coerce(self, t: HailType) -> bool:153 if self.rg:154 return t == tlocus(self.rg)155 else:156 return isinstance(t, tlocus)157class IntervalCoercer(ExprCoercer):158 def __init__(self, point_type: ExprCoercer = AnyCoercer()):159 super(IntervalCoercer, self).__init__()160 self.point_type = point_type161 @property162 def str_t(self):163 return f'interval<{self.point_type.str_t}>'164 def _requires_conversion(self, t: HailType) -> bool:165 assert isinstance(t, tinterval)166 return self.point_type._requires_conversion(t.point_type)167 def can_coerce(self, t: HailType) -> bool:168 return isinstance(t, tinterval) and self.point_type.can_coerce(t.point_type)169 def _coerce(self, x):170 assert isinstance(x, hl.expr.IntervalExpression)171 return hl.interval(self.point_type.coerce(x.start),172 self.point_type.coerce(x.end),173 includes_start=x.includes_start,174 includes_end=x.includes_end)175class ArrayCoercer(ExprCoercer):176 def __init__(self, ec: ExprCoercer = AnyCoercer()):177 super(ArrayCoercer, self).__init__()178 self.ec = ec179 @property180 def str_t(self):181 return f'array<{self.ec.str_t}>'182 def _requires_conversion(self, t: HailType) -> bool:183 assert isinstance(t, tarray)184 return self.ec._requires_conversion(t.element_type)185 def can_coerce(self, t: HailType) -> bool:186 return isinstance(t, tarray) and self.ec.can_coerce(t.element_type)187 def _coerce(self, x: Expression):188 assert isinstance(x, hl.expr.ArrayExpression)189 return hl.map(lambda x_: self.ec.coerce(x_), x)190class NDArrayCoercer(ExprCoercer):191 def __init__(self, ec: ExprCoercer = AnyCoercer()):192 super(NDArrayCoercer, self).__init__()193 self.ec = ec194 @property195 def str_t(self):196 return f'ndarray<{self.ec.str_t}>'197 def _requires_conversion(self, t: HailType) -> bool:198 assert isinstance(t, tndarray)199 return self.ec._requires_conversion(t.element_type)200 def can_coerce(self, t: HailType) -> bool:201 return isinstance(t, tndarray) and self.ec.can_coerce(t.element_type)202 def _coerce(self, x: Expression):203 assert isinstance(x, hl.expr.expressions.NDArrayExpression)204 return hl.map(lambda x_: self.ec.coerce(x_), x)205class SetCoercer(ExprCoercer):206 def __init__(self, ec: ExprCoercer = AnyCoercer()):207 super(SetCoercer, self).__init__()208 self.ec = ec209 @property210 def str_t(self):211 return f'set<{self.ec.str_t}>'212 def _requires_conversion(self, t: HailType) -> bool:213 assert isinstance(t, tset)214 return self.ec._requires_conversion(t.element_type)215 def can_coerce(self, t: HailType) -> bool:216 return isinstance(t, tset) and self.ec.can_coerce(t.element_type)217 def _coerce(self, x: Expression):218 assert isinstance(x, hl.expr.SetExpression)219 return hl.map(lambda x_: self.ec.coerce(x_), x)220class DictCoercer(ExprCoercer):221 def __init__(self, kc: ExprCoercer = AnyCoercer(), vc: ExprCoercer = AnyCoercer()):222 super(DictCoercer, self).__init__()223 self.kc = kc224 self.vc = vc225 @property226 def str_t(self):227 return f'dict<{self.kc.str_t, self.vc.str_t}>'228 def _requires_conversion(self, t: HailType) -> bool:229 assert isinstance(t, tdict)230 return self.kc._requires_conversion(t.key_type) or self.vc._requires_conversion(t.value_type)231 def can_coerce(self, t: HailType) -> bool:232 return isinstance(t, tdict) and self.kc.can_coerce(t.key_type) and self.vc.can_coerce(t.value_type)233 def _coerce(self, x: Expression):234 assert isinstance(x, hl.expr.DictExpression)235 if not self.kc._requires_conversion(x.dtype.key_type):236 # fast path237 return x.map_values(self.vc.coerce)238 else:239 return hl.dict(hl.map(lambda e: (self.kc.coerce(e[0]), self.vc.coerce(e[1])),240 hl.array(x)))241class TupleCoercer(ExprCoercer):242 def __init__(self, elements: Optional[List[ExprCoercer]] = None):243 super(TupleCoercer, self).__init__()244 self.elements = elements245 @property246 def str_t(self):247 if self.elements is None:248 return 'tuple'249 else:250 return f'tuple({", ".join(c.str_t for c in self.elements)})'251 def _requires_conversion(self, t: HailType) -> bool:252 assert isinstance(t, ttuple)253 if self.elements is None:254 return False255 else:256 assert len(self.elements) == len(t.types)257 return any(c._requires_conversion(t_) for c, t_ in zip(self.elements, t.types))258 def can_coerce(self, t: HailType):259 if self.elements is None:260 return isinstance(t, ttuple)261 else:262 return (isinstance(t, ttuple)263 and len(t.types) == len(self.elements)264 and all(c.can_coerce(t_) for c, t_ in zip(self.elements, t.types)))265 def _coerce(self, x: Expression):266 assert isinstance(x, hl.expr.TupleExpression)267 return hl.tuple(c.coerce(e) for c, e in zip(self.elements, x))268class StructCoercer(ExprCoercer):269 def __init__(self, fields: Optional[Dict[str, ExprCoercer]] = None):270 super(StructCoercer, self).__init__()271 self.fields = fields272 @property273 def str_t(self) -> str:274 if self.fields is None:275 return 'struct'276 else:277 field_strs = ', '.join(f'{escape_parsable(name)}: {c.str_t}' for name, c in self.fields.items())278 return f'struct{{{field_strs}}})'279 def _requires_conversion(self, t: HailType) -> bool:280 assert isinstance(t, tstruct)281 if self.fields is None:282 return False283 else:284 return any(c._requires_conversion(t[name]) for name, c in self.fields.items())285 def can_coerce(self, t: HailType):286 if self.fields is None:287 return isinstance(t, tstruct)288 else:289 return (isinstance(t, tstruct)290 and len(t) == len(self.fields)291 and all(expected[0] == actual[0] and expected[1].can_coerce(actual[1])292 for expected, actual in zip(self.fields.items(), t.items())))293 def _coerce(self, x: Expression):294 assert isinstance(x, hl.expr.StructExpression)295 assert list(x.keys()) == list(self.fields.keys())296 return hl.struct(**{name: c.coerce(x[name]) for name, c in self.fields.items()})297class UnionCoercer(ExprCoercer):298 def __init__(self, cases: Optional[Dict[str, ExprCoercer]] = None):299 super(UnionCoercer, self).__init__()300 self.cases = cases301 @property302 def str_t(self) -> str:303 if self.cases is None:304 return 'union'305 else:306 case_strs = ', '.join(f'{escape_parsable(name)}: {c.str_t}' for name, c in self.cases.items())307 return f'union{{{case_strs}}})'308 def _requires_conversion(self, t: HailType) -> bool:309 assert isinstance(t, tunion)310 if self.cases is None:311 return False312 else:313 return any(c._requires_conversion(t[name]) for name, c in self.cases.items())314 def can_coerce(self, t: HailType):315 if self.cases is None:316 return isinstance(t, tunion)317 else:318 return (isinstance(t, tunion)319 and len(t) == len(self.cases)320 and all(expected[0] == actual[0] and expected[1].can_coerce(actual[1])321 for expected, actual in zip(self.cases.items(), t.items())))322 def _coerce(self, x: Expression):323 assert isinstance(x, hl.expr.StructExpression)324 assert list(x.keys()) == list(self.cases.keys())325 raise NotImplementedError()326class OneOfExprCoercer(ExprCoercer):327 def __init__(self, *options: ExprCoercer):328 super(OneOfExprCoercer, self).__init__()329 options_ = []330 for o in options:331 if isinstance(o, OneOfExprCoercer):332 options_.extend(o.options)333 else:334 options_.append(o)335 self.options = options_336 @property337 def str_t(self) -> str:338 return ' or '.join(o.str_t for o in self.options)339 def _requires_conversion(self, t: HailType) -> bool:340 return all(o._requires_conversion(t) for o in filter(lambda c: c.can_coerce(t), self.options))341 def can_coerce(self, t: HailType) -> bool:342 return any(o.can_coerce(t) for o in self.options)343 def _coerce(self, x: Expression) -> Expression:344 first_coercer = next(filter(lambda o: o.can_coerce(x.dtype), self.options))345 return first_coercer._coerce(x)346expr_any = AnyCoercer()347expr_oneof = OneOfExprCoercer348expr_int32 = Int32Coercer()349expr_int64 = Int64Coercer()350expr_float32 = Float32Coercer()351expr_float64 = Float64Coercer()352expr_call = CallCoercer()353expr_str = StringCoercer()354expr_bool = BoolCoercer()355expr_locus = LocusCoercer356expr_interval = IntervalCoercer357expr_array = ArrayCoercer358expr_ndarray = NDArrayCoercer359expr_set = SetCoercer...

Full Screen

Full Screen

multicomplex.py

Source:multicomplex.py Github

copy

Full Screen

...82 z1, z2 = other.z1, other.z283 return np.vstack((np.hstack((z1, -z2)),84 np.hstack((z2, z1))))85 @staticmethod86 def _coerce(other):87 if not isinstance(other, Bicomplex):88 return Bicomplex(other, np.zeros(np.shape(other)))89 return other90 @staticmethod91 def mat2bicomp(arr):92 shape = np.array(arr.shape)93 shape[:2] = shape[:2] // 294 z1 = arr[:shape[0]]95 z2 = arr[shape[0]:]96 slices = tuple([slice(None, None, 1)] + [slice(n) for n in shape[1:]])97 return Bicomplex(z1[slices], z2[slices])98 @staticmethod99 def __array_wrap__(result):100 if isinstance(result, Bicomplex):101 return result102 shape = result.shape103 result = np.atleast_1d(result)104 z1 = np.array([cls.z1 for cls in result.ravel()])105 z2 = np.array([cls.z2 for cls in result.ravel()])106 return Bicomplex(z1.reshape(shape), z2.reshape(shape))107 def __repr__(self):108 name = self.__class__.__name__109 return """{0!s}(z1={1!s}, z2={2!s})""".format(name, str(self.z1),110 str(self.z2))111 def __lt__(self, other):112 other = self._coerce(other)113 return self.z1.real < other.z1.real114 def __le__(self, other):115 other = self._coerce(other)116 return self.z1.real <= other.z1.real117 def __gt__(self, other):118 other = self._coerce(other)119 return self.z1.real > other.z1.real120 def __ge__(self, other):121 other = self._coerce(other)122 return self.z1.real >= other.z1.real123 def __eq__(self, other):124 other = self._coerce(other)125 return (self.z1 == other.z1) * (self.z2 == other.z2)126 def __getitem__(self, index):127 return Bicomplex(self.z1[index], self.z2[index])128 def __setitem__(self, index, value):129 value = self._coerce(value)130 if index in ['z1', 'z2']:131 setattr(self, index, value)132 else:133 self.z1[index] = value.z1134 self.z2[index] = value.z2135 def __abs__(self):136 z1, z2 = self.z1, self.z2137 mask = self >= 0138 return Bicomplex(np.where(mask, z1, -z1), np.where(mask, z2, -z2))139 def __neg__(self):140 return Bicomplex(-self.z1, -self.z2)141 def __add__(self, other):142 other = self._coerce(other)143 return Bicomplex(self.z1 + other.z1, self.z2 + other.z2)144 def __sub__(self, other):145 other = self._coerce(other)146 return Bicomplex(self.z1 - other.z1, self.z2 - other.z2)147 def __rsub__(self, other):148 return -self.__sub__(other)149 def __div__(self, other):150 """elementwise division"""151 return self * other ** -1 # np.exp(-np.log(other))152 __truediv__ = __div__153 def __rdiv__(self, other):154 """elementwise division"""155 return other * self ** -1156 def __mul__(self, other):157 """elementwise multiplication"""158 other = self._coerce(other)159 return Bicomplex(self.z1 * other.z1 - self.z2 * other.z2,160 self.z1 * other.z2 + self.z2 * other.z1)161 def _pow_singular(self, other):162 z1, z2 = self.z1, self.z2163 z01 = 0.5 * (z1 - 1j * z2) ** other164 z02 = 0.5 * (z1 + 1j * z2) ** other165 return Bicomplex(z01 + z02, (z01 - z02) * 1j)166 def __pow__(self, other):167 # TODO: Check correctness168 out = (self.log() * other).exp()169 non_invertible = np.abs(self.mod_c()) < 1e-15170 if non_invertible.any():171 out[non_invertible] = self[non_invertible]._pow_singular(other)172 return out173 def __rpow__(self, other):174 return (np.log(other) * self).exp()175 __radd__ = __add__176 __rmul__ = __mul__177 def __len__(self):178 return len(self.z1)179 def conjugate(self):180 return Bicomplex(self.z1, -self.z2)181 def flat(self, index):182 return Bicomplex(self.z1.flat[index], self.z2.flat[index])183 def dot(self, other):184 other = self._coerce(other)185 if self.size == 1 or other.size == 1:186 return self * other187 return self.mat2bicomp(self.asarray(self).dot(self.asarray(other).T))188 def logaddexp(self, other):189 other = self._coerce(other)190 return self + np.log1p(np.exp(other - self))191 def logaddexp2(self, other):192 other = self._coerce(other)193 return self + np.log2(1 + np.exp2(other - self))194 def sin(self):195 z1 = np.cosh(self.z2) * np.sin(self.z1)196 z2 = np.sinh(self.z2) * np.cos(self.z1)197 return Bicomplex(z1, z2)198 def cos(self):199 z1 = np.cosh(self.z2) * np.cos(self.z1)200 z2 = -np.sinh(self.z2) * np.sin(self.z1)201 return Bicomplex(z1, z2)202 def tan(self):203 return self.sin() / self.cos()204 def cot(self):205 return self.cos() / self.sin()206 def sec(self):...

Full Screen

Full Screen

test_webutil.py

Source:test_webutil.py Github

copy

Full Screen

...103 assert outdict["message"] == (104 "couldn't format response data: Object of type ndarray is not JSON serializable"105 )106 return107def test_coerce():108 # test coercion of different types109 assert webutil._coerce(bool, "bool_var", True) is True110 assert webutil._coerce(int, "int_var", 7) == 7111 assert (112 webutil._coerce(str, "unicode_var", "this is unicode") == "this is unicode"113 )114 assert webutil._coerce(float, "float_var", 1.0) == 1.0115 assert webutil._coerce(dict, "dict_var", {"key": "val"}) == {"key": "val"}116 assert webutil._coerce(list, "list_var", [0, 1, 2]) == [0, 1, 2]117 # test raising errors118 with pytest.raises(ServerError):119 webutil._coerce(bool, "bool_var", "foo")120 with pytest.raises(ServerError):121 webutil._coerce(int, "int_var", "foo")122 with pytest.raises(ServerError):123 webutil._coerce(str, "unicode_var", 7)124 with pytest.raises(ServerError):125 webutil._coerce(float, "float_var", "foo")126 with pytest.raises(ServerError):127 webutil._coerce(dict, "dict_var", "foo")128 with pytest.raises(ServerError):129 webutil._coerce(list, "list_var", "foo")130 with pytest.raises(ServerError):131 webutil._coerce("foo", "weird_argtype", "bar")132 return133def test_required_arg():134 args = {"arg1": 1, "arg2": "my_string"}135 arg1 = webutil.required_arg(args, int, "arg1")136 assert arg1 == 1137 arg2 = webutil.required_arg(args, str, "arg2")138 assert arg2 == "my_string"139 with pytest.raises(ServerError):140 arg3 = webutil.required_arg(args, int, "arg3")141 return142def test_optional_arg():143 args = {"arg1": 1, "arg2": "my_string"}144 arg1 = webutil.optional_arg(args, int, "arg1")145 assert arg1 == 1...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run pandera automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful