Python math 模块,isnan() 实例源码

我们从Python开源项目中,提取了以下49个代码示例,用于说明如何使用math.isnan()

项目:rca-evaluation    作者:sieve-microservices    | 项目源码 | 文件源码
def do_adfuller(path, srv, p_values):
    filename = os.path.join(path, srv["filename"])
    df = load_timeseries(filename, srv)
    columns = []
    for c in df.columns:
        if (not df[c].isnull().all()) and df[c].var() != 0:
            columns.append(c)
    df = df[columns]
    if len(columns) == 0: return []
    for i, col in enumerate(df.columns):
        serie = df[col].dropna()
        if is_monotonic(serie):
            serie = serie.diff()[1:]

        for reg in p_values:
            v = adfuller(serie, regression=reg)[1]
            if math.isnan(v): # uncertain
                p_values[reg].append(-0.1)
            else:
                p_values[reg].append(v)

    return p_values
项目:rca-evaluation    作者:sieve-microservices    | 项目源码 | 文件源码
def draw(path, srv):
     filename = os.path.join(path, srv["preprocessed_filename"])
     df = pd.read_csv(filename, sep="\t", index_col='time', parse_dates=True)
     bins = defaultdict(list)
     for i, col in enumerate(df.columns):
         serie = df[col].dropna()
         if pd.algos.is_monotonic_float64(serie.values, False)[0]:
             serie = serie.diff()[1:]
         p_value = adfuller(serie, autolag='AIC')[1]
         if math.isnan(p_value): continue
         nearest = 0.05 * round(p_value/0.05)
         bins[nearest].append(serie)
     for bin, members in bins.items():
         series = [serie.name for serie in members]
         if len(members) <= 10:
             columns = series
         else:
             columns = random.sample(series, 10)

         subset = df[columns]
         name = "%s_adf_confidence_%.2f.png" % (srv["name"], bin)
         print(name)
         axes = subset.plot(subplots=True)
         plt.savefig(os.path.join(path, name))
         plt.close("all")
项目:DropboxConnect    作者:raguay    | 项目源码 | 文件源码
def validate(self, val):
        if not isinstance(val, numbers.Real):
            raise ValidationError('expected real number, got %s' %
                                  generic_type_name(val))
        if not isinstance(val, float):
            # This checks for the case where a number is passed in with a
            # magnitude larger than supported by float64.
            try:
                val = float(val)
            except OverflowError:
                raise ValidationError('too large for float')
        if math.isnan(val) or math.isinf(val):
            raise ValidationError('%f values are not supported' % val)
        if self.minimum is not None and val < self.minimum:
            raise ValidationError('%f is not greater than %f' %
                                  (val, self.minimum))
        if self.maximum is not None and val > self.maximum:
            raise ValidationError('%f is not less than %f' %
                                  (val, self.maximum))
        return val
项目:kinect-2-libras    作者:inessadl    | 项目源码 | 文件源码
def __eq__(a, b):
        """a == b"""
        if isinstance(b, Rational):
            return (a._numerator == b.numerator and
                    a._denominator == b.denominator)
        if isinstance(b, numbers.Complex) and b.imag == 0:
            b = b.real
        if isinstance(b, float):
            if math.isnan(b) or math.isinf(b):
                # comparisons with an infinity or nan should behave in
                # the same way for any finite a, so treat a as zero.
                return 0.0 == b
            else:
                return a == a.from_float(b)
        else:
            # Since a doesn't know how to compare with b, let's give b
            # a chance to compare itself with a.
            return NotImplemented
项目:kinect-2-libras    作者:inessadl    | 项目源码 | 文件源码
def _richcmp(self, other, op):
        """Helper for comparison operators, for internal use only.

        Implement comparison between a Rational instance `self`, and
        either another Rational instance or a float `other`.  If
        `other` is not a Rational instance or a float, return
        NotImplemented. `op` should be one of the six standard
        comparison operators.

        """
        # convert other to a Rational instance where reasonable.
        if isinstance(other, Rational):
            return op(self._numerator * other.denominator,
                      self._denominator * other.numerator)
        # comparisons with complex should raise a TypeError, for consistency
        # with int<->complex, float<->complex, and complex<->complex comparisons.
        if isinstance(other, complex):
            raise TypeError("no ordering relation is defined for complex numbers")
        if isinstance(other, float):
            if math.isnan(other) or math.isinf(other):
                return op(0.0, other)
            else:
                return op(self, self.from_float(other))
        else:
            return NotImplemented
项目:Projects    作者:it2school    | 项目源码 | 文件源码
def validate(self, val):
        if not isinstance(val, numbers.Real):
            raise ValidationError('expected real number, got %s' %
                                  generic_type_name(val))
        if not isinstance(val, float):
            # This checks for the case where a number is passed in with a
            # magnitude larger than supported by float64.
            try:
                val = float(val)
            except OverflowError:
                raise ValidationError('too large for float')
        if math.isnan(val) or math.isinf(val):
            raise ValidationError('%f values are not supported' % val)
        if self.minimum is not None and val < self.minimum:
            raise ValidationError('%f is not greater than %f' %
                                  (val, self.minimum))
        if self.maximum is not None and val > self.maximum:
            raise ValidationError('%f is not less than %f' %
                                  (val, self.maximum))
        return val
项目:PowerMeter-Reader    作者:lucab85    | 项目源码 | 文件源码
def _modbusRead(self, key):
        if self.PM_cacheEnabled is False:
            # W/O cache
            val = self.mb.readRegistersAndDecode(self.modbusmap[key][0],self.modbusmap[key][1],self.modbusmap[key][2])
        else:
            # with cache
            val = self.mb.cachedRead(self.modbusmap[key][0], self.modbusmap[key][1], self.modbusmap[key][2])
        log.debug('"%s" Modbus: (%s,%s,%s) = %s' % (key, self.modbusmap[key][0],
                                                    self.modbusmap[key][1], self.modbusmap[key][2], val))
        if self.modbusmap[key][2].startswith("float"):
            try:
                if math.isnan(val):
                    log.debug("NaN regs %s => 0" % self.modbusmap[key][0])
                    val = 0
            except TypeError:
                val = 0
        return val
项目:girder_worker    作者:girder    | 项目源码 | 文件源码
def test_nan(self):
        output = convert(
            'table',
            {
                'format': 'csv',
                'url': 'file://' + os.path.join('data', 'RadiomicsData.csv')
            },
            {'format': 'rows.json'}
        )
        data = json.loads(output['data'])
        self.assertEqual(len(data['fields']), 454)
        self.assertEqual(data['fields'][:3], [
            'GLCM_autocorr', 'GLCM_clusProm', 'GLCM_clusShade'
        ])
        self.assertEqual(len(data['rows']), 99)
        for row in data['rows']:
            for field in row:
                if isinstance(row[field], float):
                    self.assertFalse(math.isnan(row[field]))
                    self.assertFalse(math.isinf(row[field]))
项目:girder_worker    作者:girder    | 项目源码 | 文件源码
def csv_to_rows(input):
    reader = get_csv_reader(input)
    rows = [d for d in reader]
    fields = reader.fieldnames

    output = {'fields': fields, 'rows': rows}

    # Attempt numeric conversion
    for row in output['rows']:
        for col in row:
            try:
                row[col] = int(row[col])
            except Exception:
                try:
                    orig = row[col]
                    row[col] = float(row[col])

                    # Disallow NaN, Inf, -Inf since this does not
                    # pass through JSON converters cleanly
                    if math.isnan(row[col]) or math.isinf(row[col]):
                        row[col] = orig
                except Exception:
                    pass

    return output
项目:code-uai16    作者:thanhan    | 项目源码 | 文件源码
def update_qz(self, left = None, right = None):

        if left == None:
            left = 0
            right = self.lc.n

        for index in range(left, right):
                #if index % 100 == 0:
                #    print index
                #    sys.stdout.flush()

                qz_1 = np.log(self.theta)
                qz_0 = np.log(1 - self.theta)
                for (label, worker) in zip(*self.lc.crowd_labels[index]):
                    if label >=0 and worker in self.quv:
                      qz_1 += expectation_z(self.quv[worker][0], self.quv[worker][1], label)
                      qz_0 += expectation_z(self.quv[worker][2], self.quv[worker][3], label)

                qz_1 = np.exp(qz_1)
                qz_0 = np.exp(qz_0)

                temp = qz_1 * 1.0 / (qz_0 + qz_1)
                if not math.isnan(temp):
                    self.total_changes += np.abs(self.qz[index] - temp)
                    self.qz[index] = temp
项目:pypilot    作者:pypilot    | 项目源码 | 文件源码
def add(self, t, data, mindt):
        # update previous timestamps based on downtime
        if self.points and math.isnan(self.points[0][1]):
            dt = time.time() - t - self.timeoff
            self.timeoff = False
            for i in range(len(self.points)):
                point = self.points[i]
                self.points[i] = point[0]-dt, point[1]


        if not self.timeoff or self.timeoff < time.time() - t or self.timeoff > time.time() - t + 1:
            self.timeoff = time.time() - t

        elif self.points and t-self.points[0][0]<mindt:
            return False

        self.points.insert(0, (t, data))
        return True
项目:pypilot    作者:pypilot    | 项目源码 | 文件源码
def tracevertexes(self, time, plot, gldrawtype):
        # remove datapoints after the first one that is off the screen
        for i in range(len(self.points)):
            if self.points[i][0] < time - plot.disptime:
                self.points = self.points[:i+1]
                break

        glBegin(gldrawtype)
        for point in self.points:
            if math.isnan(point[1]):
                glEnd()
                glBegin(gldrawtype)
            else:
                y = point[1] - self.offset
                if self.directional:
                    if y >= 180:
                        y -= 360
                    elif y < - 180:
                        y += 360
                glVertex2d(point[0]-time, y)
        glEnd()
项目:hostapd-mana    作者:adde88    | 项目源码 | 文件源码
def __eq__(a, b):
        """a == b"""
        if isinstance(b, Rational):
            return (a._numerator == b.numerator and
                    a._denominator == b.denominator)
        if isinstance(b, numbers.Complex) and b.imag == 0:
            b = b.real
        if isinstance(b, float):
            if math.isnan(b) or math.isinf(b):
                # comparisons with an infinity or nan should behave in
                # the same way for any finite a, so treat a as zero.
                return 0.0 == b
            else:
                return a == a.from_float(b)
        else:
            # Since a doesn't know how to compare with b, let's give b
            # a chance to compare itself with a.
            return NotImplemented
项目:hostapd-mana    作者:adde88    | 项目源码 | 文件源码
def _richcmp(self, other, op):
        """Helper for comparison operators, for internal use only.

        Implement comparison between a Rational instance `self`, and
        either another Rational instance or a float `other`.  If
        `other` is not a Rational instance or a float, return
        NotImplemented. `op` should be one of the six standard
        comparison operators.

        """
        # convert other to a Rational instance where reasonable.
        if isinstance(other, Rational):
            return op(self._numerator * other.denominator,
                      self._denominator * other.numerator)
        # comparisons with complex should raise a TypeError, for consistency
        # with int<->complex, float<->complex, and complex<->complex comparisons.
        if isinstance(other, complex):
            raise TypeError("no ordering relation is defined for complex numbers")
        if isinstance(other, float):
            if math.isnan(other) or math.isinf(other):
                return op(0.0, other)
            else:
                return op(self, self.from_float(other))
        else:
            return NotImplemented
项目:geopyspark    作者:locationtech-labs    | 项目源码 | 文件源码
def test_floating_voint_ranges(self):
        arr = np.array([[[0.0, 0.0, 0.0, 0.0],
                         [1.0, 1.0, 1.0, 1.0],
                         [1.5, 1.5, 1.5, 1.5],
                         [2.0, 2.0, 2.0, 2.0]]], dtype=float)

        tile = Tile(arr, 'FLOAT', float('nan'))
        rdd = BaseTestClass.pysc.parallelize([(self.projected_extent, tile)])
        raster_rdd = RasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd)

        value_map = {2.0: 5.0}

        result = raster_rdd.reclassify(value_map, float,
                                       ClassificationStrategy.LESS_THAN).to_numpy_rdd().first()[1].cells

        expected = np.array([[[5.0, 5.0, 5.0, 5.0],
                              [5.0, 5.0, 5.0, 5.0],
                              [5.0, 5.0, 5.0, 5.0]]], dtype=float)

        self.assertTrue((result[0, 2, ] == expected).all())
        for x in result[0, 3, ]:
            self.assertTrue(math.isnan(x))
项目:python    作者:micronicstraining    | 项目源码 | 文件源码
def main(args):
    num, den = args
    try:
        num, den = float(num), float(den)
    except ValueError as e:
        logging.error('Invalid input')
        return INVALID_INPUT

    if den == 0:
        # this is a run-time error but not a type error
        # can be considered a warning or an error based on use case
        # written here as mere warning.
        logging.warn('Invalid denominator input!')
        return DIV_BY_ZERO_EXIT

    if math.isnan(num) or math.isnan(den):
        return INVALID_INPUT_NAN

    if math.isinf(num) or math.isinf(den):
        return INVALID_INPUT_INF

    print('Answer: ' + str(num / den))
    return 0
项目:MOSFiT    作者:guillochon    | 项目源码 | 文件源码
def process(self, **kwargs):
        """Process module."""
        self._times = kwargs[self.key('dense_times')]
        self._alpha = kwargs[self.key('alpha')]
        self._beta = kwargs[self.key('beta')]
        self._t_peak = kwargs[self.key('tpeak')]
        self._lum_scale = kwargs[self.key('lumscale')]
        self._rest_t_explosion = kwargs[self.key('resttexplosion')]

        ts = [
            np.inf
            if self._rest_t_explosion > x else (x - self._rest_t_explosion)
            for x in self._times
        ]

        luminosities = [
            self._lum_scale * (1.0 - np.exp(-t / self._t_peak)) **
            self._alpha * (t / self._t_peak) ** (-self._beta) for t in ts
        ]
        luminosities = [0.0 if isnan(x) else x for x in luminosities]

        return {self.dense_key('luminosities'): luminosities}
项目:MOSFiT    作者:guillochon    | 项目源码 | 文件源码
def process(self, **kwargs):
        """Process module."""
        self._times = kwargs[self.key('dense_times')]
        self._mnickel = kwargs[self.key('fnickel')] * kwargs[
            self.key('mejecta')]
        self._rest_t_explosion = kwargs[self.key('resttexplosion')]

        # From 1994ApJS...92..527N
        ts = [
            np.inf
            if self._rest_t_explosion > x else (x - self._rest_t_explosion)
            for x in self._times
        ]
        luminosities = [
            self._mnickel * (self.NI56_LUM * np.exp(-t / self.NI56_LIFE) +
                             self.CO56_LUM * np.exp(-t / self.CO56_LIFE))
            for t in ts
        ]
        luminosities = [0.0 if isnan(x) else x for x in luminosities]

        return {self.dense_key('luminosities'): luminosities}
项目:Supply-demand-forecasting    作者:LevinJ    | 项目源码 | 文件源码
def calFinalResult(self, testSetNum):
        self.loadResultFiles(testSetNum)
        res = []
        for key, value in self.actualDict.iteritems():
            actual = value
            if actual == 0:
                print "record {} is 0, not included in final calculation".format(key)
                continue
            prediction = self.predictonDict[key]
            temp = (actual - prediction)/float(actual)
            if math.isnan(temp):
                print temp
            res.append(abs(temp))
        res = np.array(res)
        pd.DataFrame(res).to_csv('result.csv')
        print "final result: {}".format(res.mean())
        return np.mean(res)
项目:riko    作者:nerevu    | 项目源码 | 文件源码
def def_itemgetter(attr, default=0, _type=None):
    # like operator.itemgetter but fills in missing keys with a default value
    def keyfunc(item):
        value = item.get(attr, default)
        casted = cast(value, _type) if _type else value

        try:
            is_nan = isnan(casted)
        except TypeError:
            is_nan = False

        return default if is_nan else casted

    return keyfunc


# TODO: move this to meza.process.group
项目:pyro    作者:uber    | 项目源码 | 文件源码
def test_iter_discrete_traces_nan(enum_discrete, trace_graph):
    pyro.clear_param_store()

    def model():
        p = Variable(torch.Tensor([0.0, 0.5, 1.0]))
        pyro.sample("z", dist.Bernoulli(p))

    def guide():
        p = pyro.param("p", Variable(torch.Tensor([0.0, 0.5, 1.0]), requires_grad=True))
        pyro.sample("z", dist.Bernoulli(p))

    Elbo = TraceGraph_ELBO if trace_graph else Trace_ELBO
    elbo = Elbo(enum_discrete=enum_discrete)
    with xfail_if_not_implemented():
        loss = elbo.loss(model, guide)
        assert isinstance(loss, float) and not math.isnan(loss), loss
        loss = elbo.loss_and_grads(model, guide)
        assert isinstance(loss, float) and not math.isnan(loss), loss


# A simple Gaussian mixture model, with no vectorization.
项目:healthcare-deid    作者:GoogleCloudPlatform    | 项目源码 | 文件源码
def testCalculateStats(self):
    stats = results_pb2.Stats()
    stats.true_positives = 12
    stats.false_positives = 8
    stats.false_negatives = 3
    run_pipeline_lib.calculate_stats(stats)
    self.assertAlmostEqual(.6, stats.precision)
    self.assertAlmostEqual(.8, stats.recall)
    self.assertAlmostEqual(.6857142857142856, stats.f_score)

    stats = results_pb2.Stats()
    run_pipeline_lib.calculate_stats(stats)
    self.assertTrue(math.isnan(stats.precision))
    self.assertTrue(math.isnan(stats.recall))
    self.assertTrue(math.isnan(stats.f_score))
    self.assertEqual(
        'Precision has denominator of zero. Recall has denominator of zero. '
        'f-score is NaN',
        stats.error_message)
项目:healthcare-deid    作者:GoogleCloudPlatform    | 项目源码 | 文件源码
def testMacroStats(self):
    macro_stats = run_pipeline_lib._MacroStats()
    macro_stats.count = 50
    macro_stats.precision_sum = 40
    macro_stats.recall_sum = 45

    stats = macro_stats.calculate_stats()
    self.assertAlmostEqual(.8, stats.precision)
    self.assertAlmostEqual(.9, stats.recall)
    self.assertAlmostEqual(.8470588235294118, stats.f_score)

    macro_stats = run_pipeline_lib._MacroStats()
    stats = macro_stats.calculate_stats()
    self.assertTrue(math.isnan(stats.precision))
    self.assertTrue(math.isnan(stats.recall))
    self.assertTrue(math.isnan(stats.f_score))
项目:healthcare-deid    作者:GoogleCloudPlatform    | 项目源码 | 文件源码
def calculate_stats(stats):
  """Calculate derived stats and put them into the given results_pb2.Stats."""
  stats.error_message = ''
  if stats.true_positives + stats.false_positives:
    stats.precision = (float(stats.true_positives) /
                       (stats.true_positives + stats.false_positives))
  else:
    stats.precision = float('NaN')
    stats.error_message += 'Precision has denominator of zero. '

  if stats.true_positives + stats.false_negatives:
    stats.recall = (float(stats.true_positives) /
                    (stats.true_positives + stats.false_negatives))
  else:
    stats.recall = float('NaN')
    stats.error_message += 'Recall has denominator of zero. '

  stats.f_score = hmean(stats.precision, stats.recall)
  if math.isnan(stats.f_score):
    stats.error_message += 'f-score is NaN'

  return stats
项目:RecQ    作者:Coder-Yu    | 项目源码 | 文件源码
def isConverged(self,iter):
        from math import isnan
        if isnan(self.loss):
            print 'Loss = NaN or Infinity: current settings does not fit the recommender! Change the settings and try again!'
            exit(-1)
        measure = self.performance()
        value = [item.strip()for item in measure]
        #with open(self.algorName+' iteration.txt')
        deltaLoss = (self.lastLoss-self.loss)
        print '%s %s iteration %d: loss = %.4f, delta_loss = %.5f learning_Rate = %.5f %s %s' %(self.algorName,self.foldInfo,iter,self.loss,deltaLoss,self.lRate,measure[0][:11],measure[1][:12])
        #check if converged
        cond = abs(deltaLoss) < 1e-3
        converged = cond
        if not converged:
            self.updateLearningRate(iter)
        self.lastLoss = self.loss
        shuffle(self.dao.trainingData)
        return converged
项目:CTAtools    作者:davidsanchez    | 项目源码 | 文件源码
def GetInfoFromTable(fitstable,indice):
    '''read salvatore table and return info corresponding to the source at the place indice
    Parameters
    ---------
    fitstable : pyfits object : table to be browsed
    indice : place of the source in the table
    '''
    data = fitstable[1].data[indice]
    sourcename = data[0]
    ra = data[1]
    dec = data[2]
    z = data[4]

    if math.isnan(z):
        z=0
    hemisphere = data[6]
    observation_type = data[8]
    if hemisphere =='S':
        hemisphere ='South'
    if hemisphere =='N':
        hemisphere ='North'

    return sourcename,ra,dec,z,hemisphere,observation_type
项目:cache-leak-detector    作者:falsecurity    | 项目源码 | 文件源码
def pearson(A, B):

    # Pearson
    coeff = 0.0
    try:
        coeff = scipy.stats.pearsonr(A, B)[0]
        if math.isnan(coeff):
            coeff = 0.0
    except:
        coeff = 0.0
    return (coeff)

##
# Calculate the hamming weight of all values
# within the given array.
#
#   @param A 1-D byte array
#   @return Hamming weight
#
项目:catalyst    作者:enigmampc    | 项目源码 | 文件源码
def maybe_create_close_position_transaction(self, asset, dt, data_portal):
        if not self.positions.get(asset):
            return None

        amount = self.positions.get(asset).amount
        price = data_portal.get_spot_value(
            asset, 'price', dt, self.data_frequency)

        # Get the last traded price if price is no longer available
        if isnan(price):
            price = self.positions.get(asset).last_sale_price

        txn = Transaction(
            asset=asset,
            amount=(-1 * amount),
            dt=dt,
            price=price,
            commission=0,
            order_id=None,
        )
        return txn
项目:catalyst    作者:enigmampc    | 项目源码 | 文件源码
def sync_last_sale_prices(self, dt, handle_non_market_minutes,
                              data_portal):
        if not handle_non_market_minutes:
            for asset, position in iteritems(self.positions):
                last_sale_price = data_portal.get_spot_value(
                    asset, 'price', dt, self.data_frequency
                )

                if not np.isnan(last_sale_price):
                    position.last_sale_price = last_sale_price
        else:
            for asset, position in iteritems(self.positions):
                last_sale_price = data_portal.get_adjusted_value(
                    asset,
                    'price',
                    data_portal.trading_calendar.previous_minute(dt),
                    dt,
                    self.data_frequency
                )

                if not np.isnan(last_sale_price):
                    position.last_sale_price = last_sale_price
项目:dd-trace-py    作者:DataDog    | 项目源码 | 文件源码
def set_metric(self, key, value):
        # This method sets a numeric tag value for the given key. It acts
        # like `set_meta()` and it simply add a tag without further processing.

        # FIXME[matt] we could push this check to serialization time as well.
        # only permit types that are commonly serializable (don't use
        # isinstance so that we convert unserializable types like numpy
        # numbers)
        if type(value) not in numeric_types:
            try:
                value = float(value)
            except (ValueError, TypeError):
                log.debug("ignoring not number metric %s:%s", key, value)
                return

        # don't allow nan or inf
        if math.isnan(value) or math.isinf(value):
            log.debug("ignoring not real metric %s:%s", key, value)
            return

        self.metrics[key] = value
项目:recommandation-film    作者:sambiak    | 项目源码 | 文件源码
def reccomandation(self, x):
        """"""
        y = np.array([[math.nan] * nombre_films()])
        for key in self.films:
            y[0][self.conv.renvoyer_index(key)] = self.films[key]
        max_i = 0
        n_max = 0
        t = np.dot(x, self._theta.T)
        print(t)
        for i, el in enumerate(y[0]):
            if np.isnan(el) and t[i, 0] > n_max:
                print("film : ", self.conv.renvoyer_nom_index(i), "note :", n_max)
                n_max = t[i, 0]
                max_i = i
        print(t)
        print(self._theta)
        return self.conv.renvoyer_nom_index(max_i)
项目:recommandation-film    作者:sambiak    | 项目源码 | 文件源码
def sous_ensemble():
    """Renvoie un sous_tableau sans nan en regardant les films les plus regardes et prend
    les utlisateurs present
    dans ces films"""
    tableau = tableau_des_notes()
    reduit = [(tableau[:,i][~np.isnan(tableau[:,i])], i) for i in range(9125)]
    trie = sorted(reduit, reverse=True, key=lambda entree: len(entree[0]))
    utilisateurs_ayant_vu_le_premier_film = [i for i, u in enumerate(tableau[: ,trie[0][1]]) \
                                             if not math.isnan(u)]
    utilisateurs_ayant_vu_les_film = [u for u in utilisateurs_ayant_vu_le_premier_film \
                                      if a_vu_tout_les_films(u, trie, tableau)]
    index_11_premiers_films = [trie[i][1] for i in range(11)]
    tableau_concentre = [[note for i, note in enumerate(tableau[u]) if\
                          (i in index_11_premiers_films)] \
                         for u in utilisateurs_ayant_vu_les_film]
    return tableau_concentre
项目:recommandation-film    作者:sambiak    | 项目源码 | 文件源码
def reccomandation(self, x):
        """"""
        y = np.array([[math.nan] * nombre_films()])
        for key in self.films:
            y[0][self.conv.renvoyer_index(key)] = self.films[key]
        max_i = 0
        n_max = 0
        t = np.dot(x, self._theta.T)
        print(t)
        for i, el in enumerate(y[0]):
            if np.isnan(el) and t[i, 0] > n_max:
                print("film : ", self.conv.renvoyer_nom_index(i), "note :", n_max)
                n_max = t[i, 0]
                max_i = i
        print(t)
        print(self._theta)
        return self.conv.renvoyer_nom_index(max_i)
项目:papers    作者:jeffheaton    | 项目源码 | 文件源码
def generate_data_fn2(rows, cnt, x_low, x_high, fn):
    x_array = []
    y_array = []

    while (len(x_array) < rows):
        args = []
        for i in range(cnt):
            args.append(np.random.uniform(x_low, x_high))

        try:
            y = fn(*args)
            if not math.isnan(y):
                x_array.append(args)
                y_array.append(y)
        except (ValueError, ZeroDivisionError):
            pass

    return np.array(x_array, dtype=np.float32), np.array(y_array, dtype=np.float32)


# Generate data for the ratio experiment
项目:PeARS-evaluation    作者:minimalparts    | 项目源码 | 文件源码
def runScript(query_dist,dm_dict,pears_ids):
    best_pears=[]

    #############################################################
    #Calculate score for each pear in relation to the user query
    #############################################################

    if len(query_dist) > 0: 
        pears_scores={}
        for pear_name,v in pears_ids.items():
            scoreSIM = 0.0      #Initialise score for similarity
            score=cosine_similarity(np.array(v),query_dist)
            if not isnan(score):
                pears_scores[pear_name]=score
                print pear_name,score   
        best_pears=outputBestPears(pears_scores)
    return best_pears
项目:rec-attend-public    作者:renmengye    | 项目源码 | 文件源码
def getColorEntry(val, args):
    if not args.colorized:
        return ""
    if not isinstance(val, float) or math.isnan(val):
        return colors.ENDC
    if (val < .20):
        return colors.RED
    elif (val < .40):
        return colors.YELLOW
    elif (val < .60):
        return colors.BLUE
    elif (val < .80):
        return colors.CYAN
    else:
        return colors.GREEN

# Cityscapes files have a typical filename structure
# <city>_<sequenceNb>_<frameNb>_<type>[_<type2>].<ext>
# This class contains the individual elements as members
# For the sequence and frame number, the strings are returned, including leading zeros
项目:datatest    作者:shawnbrown    | 项目源码 | 文件源码
def __init__(self, lower, upper=None, msg=None, **kwds):
        lower, upper, msg = _normalize_deviation_args(lower, upper, msg)
        normalize_numbers = lambda x: x if x else 0
        def function(diff):
            if not isinstance(diff, xDeviation):
                return False
            value = normalize_numbers(diff.value)  # Closes over normalize_numbers().
            required = normalize_numbers(diff.required)
            if isnan(value) or isnan(required):
                return False
            if value != 0 and required == 0:
                return False
            percent = value / required if required else 0  # % error calc.
            return lower <= percent <= upper  # Closes over *lower* and *upper*.
        function.__name__ = self.__class__.__name__
        super(allow_percent_deviation, self).__init__(function, msg, **kwds)
#_prettify_deviation_signature(allow_percent_deviation.__init__)
项目:datatest    作者:shawnbrown    | 项目源码 | 文件源码
def percent_deviation(self):
        expected = self.expected
        if isinstance(expected, float):
            expected = Decimal.from_float(expected)
        else:
            expected = Decimal(expected if expected else 0)

        deviation = self.deviation
        if isinstance(deviation, float):
            deviation = Decimal.from_float(deviation)
        else:
            deviation = Decimal(deviation if deviation else 0)

        if isnan(expected) or isnan(deviation):
            return Decimal('NaN')
        return deviation / expected if expected else Decimal(0)  # % error calc.
项目:datatest    作者:shawnbrown    | 项目源码 | 文件源码
def _from_float(cls, f):
        if isinstance(f, int):                # handle integer inputs
            return cls(f)
        if not isinstance(f, float):
            raise TypeError("argument must be int or float.")
        if _math.isinf(f) or _math.isnan(f):
            return cls(repr(f))
        if _math.copysign(1.0, f) == 1.0:
            sign = 0
        else:
            sign = 1
        n, d = abs(f).as_integer_ratio()
        #k = d.bit_length() - 1
        k = _bit_length(d) - 1
        result = _dec_from_triple(sign, str(n*5**k), -k)
        if cls is Decimal:
            return result
        else:
            return cls(result)
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def assertFloatIdentical(self, x, y):
        """Fail unless floats x and y are identical, in the sense that:
        (1) both x and y are nans, or
        (2) both x and y are infinities, with the same sign, or
        (3) both x and y are zeros, with the same sign, or
        (4) x and y are both finite and nonzero, and x == y

        """
        msg = 'floats {!r} and {!r} are not identical'

        if math.isnan(x) or math.isnan(y):
            if math.isnan(x) and math.isnan(y):
                return
        elif x == y:
            if x != 0.0:
                return
            # both zero; check that signs match
            elif math.copysign(1.0, x) == math.copysign(1.0, y):
                return
            else:
                msg += ': zeros have different signs'
        self.fail(msg.format(x, y))
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def testCeil(self):
        self.assertRaises(TypeError, math.ceil)
        self.assertEqual(int, type(math.ceil(0.5)))
        self.ftest('ceil(0.5)', math.ceil(0.5), 1)
        self.ftest('ceil(1.0)', math.ceil(1.0), 1)
        self.ftest('ceil(1.5)', math.ceil(1.5), 2)
        self.ftest('ceil(-0.5)', math.ceil(-0.5), 0)
        self.ftest('ceil(-1.0)', math.ceil(-1.0), -1)
        self.ftest('ceil(-1.5)', math.ceil(-1.5), -1)
        #self.assertEqual(math.ceil(INF), INF)
        #self.assertEqual(math.ceil(NINF), NINF)
        #self.assertTrue(math.isnan(math.ceil(NAN)))

        class TestCeil:
            def __ceil__(self):
                return 42
        class TestNoCeil:
            pass
        self.ftest('ceil(TestCeil())', math.ceil(TestCeil()), 42)
        self.assertRaises(TypeError, math.ceil, TestNoCeil())

        t = TestNoCeil()
        t.__ceil__ = lambda *args: args
        self.assertRaises(TypeError, math.ceil, t)
        self.assertRaises(TypeError, math.ceil, t, 0)
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def testFmod(self):
        self.assertRaises(TypeError, math.fmod)
        self.ftest('fmod(10,1)', math.fmod(10,1), 0)
        self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0)
        self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1)
        self.ftest('fmod(-10,1)', math.fmod(-10,1), 0)
        self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
        self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
        self.assertTrue(math.isnan(math.fmod(NAN, 1.)))
        self.assertTrue(math.isnan(math.fmod(1., NAN)))
        self.assertTrue(math.isnan(math.fmod(NAN, NAN)))
        self.assertRaises(ValueError, math.fmod, 1., 0.)
        self.assertRaises(ValueError, math.fmod, INF, 1.)
        self.assertRaises(ValueError, math.fmod, NINF, 1.)
        self.assertRaises(ValueError, math.fmod, INF, 0.)
        self.assertEqual(math.fmod(3.0, INF), 3.0)
        self.assertEqual(math.fmod(-3.0, INF), -3.0)
        self.assertEqual(math.fmod(3.0, NINF), 3.0)
        self.assertEqual(math.fmod(-3.0, NINF), -3.0)
        self.assertEqual(math.fmod(0.0, 3.0), 0.0)
        self.assertEqual(math.fmod(0.0, NINF), 0.0)
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def testModf(self):
        self.assertRaises(TypeError, math.modf)

        def testmodf(name, result, expected):
            (v1, v2), (e1, e2) = result, expected
            if abs(v1-e1) > eps or abs(v2-e2):
                self.fail('%s returned %r, expected %r'%\
                          (name, result, expected))

        testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
        testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))

        self.assertEqual(math.modf(INF), (0.0, INF))
        self.assertEqual(math.modf(NINF), (-0.0, NINF))

        modf_nan = math.modf(NAN)
        self.assertTrue(math.isnan(modf_nan[0]))
        self.assertTrue(math.isnan(modf_nan[1]))
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def assertFloatsAreIdentical(self, x, y):
        """assert that floats x and y are identical, in the sense that:
        (1) both x and y are nans, or
        (2) both x and y are infinities, with the same sign, or
        (3) both x and y are zeros, with the same sign, or
        (4) x and y are both finite and nonzero, and x == y

        """
        msg = 'floats {!r} and {!r} are not identical'

        if isnan(x) or isnan(y):
            if isnan(x) and isnan(y):
                return
        elif x == y:
            if x != 0.0:
                return
            # both zero; check that signs match
            elif copysign(1.0, x) == copysign(1.0, y):
                return
            else:
                msg += ': zeros have different signs'
        self.fail(msg.format(x, y))
项目:ios-xr-grpc-python    作者:cisco-grpc-connection-libs    | 项目源码 | 文件源码
def _FieldToJsonObject(self, field, value):
    """Converts field value according to Proto3 JSON Specification."""
    if field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE:
      return self._MessageToJsonObject(value)
    elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_ENUM:
      enum_value = field.enum_type.values_by_number.get(value, None)
      if enum_value is not None:
        return enum_value.name
      else:
        raise SerializeToJsonError('Enum field contains an integer value '
                                   'which can not mapped to an enum value.')
    elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_STRING:
      if field.type == descriptor.FieldDescriptor.TYPE_BYTES:
        # Use base64 Data encoding for bytes
        return base64.b64encode(value).decode('utf-8')
      else:
        return value
    elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_BOOL:
      return bool(value)
    elif field.cpp_type in _INT64_TYPES:
      return str(value)
    elif field.cpp_type in _FLOAT_TYPES:
      if math.isinf(value):
        if value < 0.0:
          return _NEG_INFINITY
        else:
          return _INFINITY
      if math.isnan(value):
        return _NAN
    return value
项目:cellranger    作者:10XGenomics    | 项目源码 | 文件源码
def format_value(value, format_type):
    if format_type == 'string':
        return str(value)

    value = value if not math.isnan(float(value)) else 0

    if format_type == 'percent':
        return '{:.1%}'.format(float(value))
    elif format_type == 'integer':
        return '{:,d}'.format(int(value))
    elif format_type[0] == '%':
        return format_type % float(value)
    raise Exception('Invalid format type: %s' % format_type)
项目:cellranger    作者:10XGenomics    | 项目源码 | 文件源码
def json_sanitize(data):
    # This really doesn't make me happy. How many cases we we have to test?
    if (type(data) == float) or (type(data) == numpy.float64):
        # Handle floats specially
        if math.isnan(data):
            return "NaN";
        if (data ==  float("+Inf")):
            return "inf"
        if (data == float("-Inf")):
            return "-inf"
        return data
    elif hasattr(data, 'iterkeys'):
        # Dictionary case
        new_data = {}
        for k in data.keys():
            new_data[k] = json_sanitize(data[k])
        return new_data
    elif hasattr(data, '__iter__'):
        # Anything else that looks like a list. N
        new_data = []
        for d in data:
            new_data.append(json_sanitize(d))
        return new_data
    elif hasattr(data, 'shape') and data.shape == ():
        # Numpy 0-d array
        return np.asscalar(data)
    else:
        return data
项目:kinect-2-libras    作者:inessadl    | 项目源码 | 文件源码
def from_float(cls, f):
        """Converts a float to a decimal number, exactly.

        Note that Decimal.from_float(0.1) is not the same as Decimal('0.1').
        Since 0.1 is not exactly representable in binary floating point, the
        value is stored as the nearest representable value which is
        0x1.999999999999ap-4.  The exact equivalent of the value in decimal
        is 0.1000000000000000055511151231257827021181583404541015625.

        >>> Decimal.from_float(0.1)
        Decimal('0.1000000000000000055511151231257827021181583404541015625')
        >>> Decimal.from_float(float('nan'))
        Decimal('NaN')
        >>> Decimal.from_float(float('inf'))
        Decimal('Infinity')
        >>> Decimal.from_float(-float('inf'))
        Decimal('-Infinity')
        >>> Decimal.from_float(-0.0)
        Decimal('-0')

        """
        if isinstance(f, (int, long)):        # handle integer inputs
            return cls(f)
        if _math.isinf(f) or _math.isnan(f):  # raises TypeError if not a float
            return cls(repr(f))
        if _math.copysign(1.0, f) == 1.0:
            sign = 0
        else:
            sign = 1
        n, d = abs(f).as_integer_ratio()
        k = d.bit_length() - 1
        result = _dec_from_triple(sign, str(n*5**k), -k)
        if cls is Decimal:
            return result
        else:
            return cls(result)
项目:kinect-2-libras    作者:inessadl    | 项目源码 | 文件源码
def from_float(cls, f):
        """Converts a finite float to a rational number, exactly.

        Beware that Fraction.from_float(0.3) != Fraction(3, 10).

        """
        if isinstance(f, numbers.Integral):
            return cls(f)
        elif not isinstance(f, float):
            raise TypeError("%s.from_float() only takes floats, not %r (%s)" %
                            (cls.__name__, f, type(f).__name__))
        if math.isnan(f) or math.isinf(f):
            raise TypeError("Cannot convert %r to %s." % (f, cls.__name__))
        return cls(*f.as_integer_ratio())