Beispiel #1
0
    def __init__(self, **kwargs):
        """
        Init function.

        :param kwargs:
        :raises PilotException: NotImplementedError for unknown fitting model, NotDefined if input data not defined.
        """

        # extract parameters
        self._model = kwargs.get('model', 'linear')
        self._x = kwargs.get('x', None)
        self._y = kwargs.get('y', None)

        if not self._x or not self._y:
            raise NotDefined('input data not defined')

        if len(self._x) != len(self._y):
            raise NotSameLength('input data (lists) have different lengths')

        # base calculations
        if self._model == 'linear':
            self._ss = sum_square_dev(self._x)
            self._ss2 = sum_dev(self._x, self._y)
            self.set_slope()
            self._xm = mean(self._x)
            self._ym = mean(self._y)
            self.set_intersect()
            self.set_chi2()
        else:
            logger.warning("\'%s\' model is not implemented", self._model)
            raise NotImplementedError()
Beispiel #2
0
def float_to_rounded_string(num, precision=3):
    """
    Convert float to a string with a desired number of digits (the precision).
    E.g. num=3.1415, precision=2 -> '3.14'.

    :param num: number to be converted (float).
    :param precision: number of desired digits (int)
    :raises NotDefined: for undefined precisions and float conversions to Decimal.
    :return: rounded string.
    """

    try:
        _precision = Decimal(10) ** -precision
    except Exception as e:
        raise NotDefined('failed to define precision=%s: %e' % (str(precision), e))

    try:
        s = Decimal(str(num)).quantize(_precision)
    except Exception as e:
        raise NotDefined('failed to convert %s to Decimal: %s' % (str(num), e))

    return str(s)
Beispiel #3
0
    def chi2(self):
        """
        Return the chi2 of the fit.

        :raises NotDefined: exception thrown if fit is not defined.
        :return: chi2 (float).
        """

        x2 = None

        if self._fit:
            x2 = self._fit.chi2()
        else:
            raise NotDefined('Fit has not been defined')

        return x2
Beispiel #4
0
    def intersect(self):
        """
        Return the intersect of a linear fit, y(x) = slope * x + intersect.

        :raises NotDefined: exception thrown if fit is not defined.
        :return: intersect (float).
        """

        intersect = None

        if self._fit:
            intersect = self._fit.intersect()
        else:
            raise NotDefined('Fit has not been defined')

        return intersect
Beispiel #5
0
    def slope(self):
        """
        Return the slope of a linear fit, y(x) = slope * x + intersect.

        :raises NotDefined: exception thrown if fit is not defined.
        :return: slope (float).
        """

        slope = None

        if self._fit:
            slope = self._fit.slope()
        else:
            raise NotDefined('Fit has not been defined')

        return slope
Beispiel #6
0
    def get_storage_id(self, ddmendpoint):
        """
        Return the storage_id of a ddmendpoint.

        :param ddmendpoint: ddmendpoint name.
        :returns storage_id: storage_id of the ddmendpoint.
        :raises NotDefined:
        """
        if ddmendpoint not in self.ddmendpoint2storage_id:
            self.resolve_ddmendpoint_storageid(ddmendpoint)

        if ddmendpoint in self.ddmendpoint2storage_id:
            storage_id = self.ddmendpoint2storage_id[ddmendpoint]
            logger.info("Found storage id for ddmendpoint(%s): %s" % (ddmendpoint, storage_id))
            return storage_id
        else:
            raise NotDefined("Cannot find the storage id for ddmendpoint: %s" % ddmendpoint)
Beispiel #7
0
    def get_ddmendpoint(self, storage_id):
        """
        Return the ddmendpoint name from a storage id.

        :param storage_id: storage_id as an int.
        :returns ddmendpoint: ddmendpoint name.
        :raises NotDefined:
        """
        storage_id = int(storage_id)
        if storage_id not in self.storage_id2ddmendpoint:
            self.resolve_ddmendpoint_storageid()

        if storage_id in self.storage_id2ddmendpoint:
            ddmendpoint = self.storage_id2ddmendpoint[storage_id]
            logger.info("Found ddmendpoint for storage id(%s): %s" % (storage_id, ddmendpoint))
            return ddmendpoint
        else:
            self.resolve_storage_data()
            raise NotDefined("Cannot find ddmendpoint for storage id: %s" % storage_id)