def get_type(value): from pytracer.core.stats.numpy import StatisticNumpy from pytracer.core.stats.sklearn import is_sklearn_value import numpy as np _type = None if isinstance(value, int): _type = TypeValue.INT elif isinstance(value, float): _type = TypeValue.FLOAT elif StatisticNumpy.hasinstance(value): _type = TypeValue.NUMPY elif isinstance(value, list): array = np.array(value, dtype=np.object) if StatisticNumpy.hasinstance(array): _type = TypeValue.LIST else: _type = TypeValue.OTHER elif isinstance(value, tuple): _type = TypeValue.TUPLE elif isinstance(value, FunctionType): _type = TypeValue.FUNCTION elif is_sklearn_value(value): _type = TypeValue.SKLEARN else: _type = TypeValue.OTHER if not isinstance(value, (str, np.ndarray, np.dtype, type)): if type(value) not in _type_cache: logger.warning(f"Unknown type: {type(value)} {value}") _type_cache.add(type(value)) return _type
def parse_data(self, data): _data = dict() for attr in self.__attributes: for value in data: if hasattr(value, attr): if attr in _data: _data[attr].append(getattr(value, attr)) else: _data[attr] = [getattr(value, attr)] if attr in _data: if (d := _data[attr]) == []: _data[attr] = StatisticNumpy(np.array(d), empty=True) else: _data[attr] = StatisticNumpy(np.array(d))
def parse_data(self, data): _data = dict() for attr in self.__attributes: for value in data: if hasattr(value, attr): attr_name = f"MultiTaskLasso.{attr}" if attr_name in _data: _data[attr_name].append(getattr(value, attr)) else: _data[attr_name] = [getattr(value, attr)] if attr_name in _data: if (d := _data[attr_name]) == []: _data[attr_name] = StatisticNumpy(np.array(d), empty=True) else: _data[attr_name] = StatisticNumpy(np.array(d))
def parse_data(self, data): _data = dict() for attr in self.__attributes: for value in data: if hasattr(value, attr): obj = getattr(value, attr, None) if obj is None: _data[attr] = [] continue if attr in _data: _data[attr].append(obj) else: _data[attr] = [obj] if attr in _data: if (d := _data[attr]) == []: empty = True else: empty = False _data[attr] = StatisticNumpy(np.array(d), empty=empty)
def get_stats(values): from pytracer.core.stats.numpy import StatisticNumpy from pytracer.core.stats.sklearn import get_sklearn_stat import numpy as np check_type(values) _type = get_type(values[0]) _stats = None if _type == TypeValue.INT: array = np.array(values, dtype=np.int64) _stats = StatisticNumpy(array) elif _type == TypeValue.FLOAT: array = np.array(values, dtype=np.float64) _stats = StatisticNumpy(array) elif _type == TypeValue.LIST: array = np.array(values) _stats = StatisticNumpy(array) elif _type == TypeValue.TUPLE: types = [get_type(t) for t in values[0]] _stats = [] zipv = zip(*values) [(t, v) for t, v in zip(types, zipv)] for ty, v in zip(types, zipv): if ty == TypeValue.OTHER: _stats.append(StatisticNumpy(v, empty=True)) else: _stats.append(StatisticNumpy(np.array(v))) elif _type == TypeValue.NUMPY: try: array = np.array(values) _stats = StatisticNumpy(array) except: logger.debug(f"Could not parse {values}") _stats = StatisticNumpy(values, empty=True) elif _type == TypeValue.SKLEARN: _stats = get_sklearn_stat(values, type(values[0])) else: _stats = StatisticNumpy(values, empty=True) # _stats = values[0] return _stats