def __init__(self, distance=None, moltype=None, fast_calc=None, slow_calc=None): super(fast_slow_dist, self).__init__( input_types=ALIGNED_TYPE, output_types=(PAIRWISE_DISTANCE_TYPE, SERIALISABLE_TYPE), data_types=("ArrayAlignment", "Alignment"), ) self._formatted_params() self._moltype = moltype if moltype is None else get_moltype(moltype) self._sm = None if (fast_calc or slow_calc) and distance: raise ValueError("cannot combine distance and fast/slow") if distance: fast_calc = distance slow_calc = distance d = set(["hamming", "paralinear", "logdet"]) & set( [slow_calc, fast_calc]) if d and not self._moltype: raise ValueError(f"you must provide a moltype for {d}") try: fast_calc = get_distance_calculator(fast_calc, moltype=self._moltype) except (ValueError, AttributeError): fast_calc = None try: slow_calc = get_model(slow_calc) except ValueError: slow_calc = None if not (fast_calc or slow_calc): raise ValueError(f"invalid values for {slow_calc} or {fast_calc}") self.fast_calc = fast_calc if fast_calc and self._moltype and fast_calc.moltype != self._moltype: raise ValueError( f"{self._moltype} incompatible moltype with fast calculator {fast_calc.moltype}" ) elif fast_calc: self._moltype = fast_calc.moltype if slow_calc and self._moltype and slow_calc.moltype != self._moltype: raise ValueError("incompatible moltype with slow calculator") elif slow_calc: self._moltype = slow_calc.moltype self._sm = slow_calc
def test_get_calculator(self): """exercising getting specified calculator""" for key in _calculators: get_distance_calculator(key) get_distance_calculator(key.upper()) with self.assertRaises(ValueError): get_distance_calculator("blahblah")
def __init__(self, distance=None, moltype=None, fast_calc=None, slow_calc=None): """ Parameters ---------- moltype : str cogent3 moltype distance : str Name of a distance method available as both fast and slow calculator. fast_calc Name of a fast distance calculator. See cogent3.available_distances(). slow_calc Name of a slow distance calculator. See cogent3.available_models(). Notes ----- If you provide fast_calc or slow_calc, you must specify the moltype. """ super(fast_slow_dist, self).__init__( input_types=self._input_types, output_types=self._output_types, data_types=self._data_types, ) self._formatted_params() self._moltype = moltype if moltype is None else get_moltype(moltype) self._sm = None if (fast_calc or slow_calc) and distance: raise ValueError("cannot combine distance and fast/slow") if distance: fast_calc = distance slow_calc = distance d = {"hamming", "percent", "paralinear", "logdet" } & {slow_calc, fast_calc} if d and not self._moltype: raise ValueError(f"you must provide a moltype for {d}") try: fast_calc = get_distance_calculator(fast_calc, moltype=self._moltype) except (ValueError, AttributeError): fast_calc = None try: slow_calc = get_model(slow_calc) except ValueError: slow_calc = None if not (fast_calc or slow_calc): raise ValueError(f"invalid values for {slow_calc} or {fast_calc}") self.fast_calc = fast_calc if fast_calc and self._moltype and fast_calc.moltype != self._moltype: raise ValueError( f"{self._moltype} incompatible moltype with fast calculator {fast_calc.moltype}" ) elif fast_calc: self._moltype = fast_calc.moltype if slow_calc and self._moltype and slow_calc.moltype != self._moltype: raise ValueError("incompatible moltype with slow calculator") elif slow_calc: self._moltype = slow_calc.moltype self._sm = slow_calc self.func = self.calc_distance