Python numpy.isnan() Examples
The following are 30
code examples of numpy.isnan().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
numpy
, or try the search function
.
Example #1
Source File: numerical.py From Kaggler with MIT License | 6 votes |
def _transform_col(self, x, i): """Encode one numerical feature column to quantiles. Args: x (pandas.Series): numerical feature column to encode i (int): column index of the numerical feature Returns: Encoded feature (pandas.Series). """ # Map values to the emperical CDF between .1% and 99.9% rv = np.ones_like(x) * -1 filt = ~np.isnan(x) rv[filt] = np.floor((self.ecdfs[i](x[filt]) * 0.998 + .001) * self.n_label) return rv
Example #2
Source File: test_utils.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def almost_equal_ignore_nan(a, b, rtol=None, atol=None): """Test that two NumPy arrays are almost equal (ignoring NaN in either array). Combines a relative and absolute measure of approximate eqality. If either the relative or absolute check passes, the arrays are considered equal. Including an absolute check resolves issues with the relative check where all array values are close to zero. Parameters ---------- a : np.ndarray b : np.ndarray rtol : None or float The relative threshold. Default threshold will be used if set to ``None``. atol : None or float The absolute threshold. Default threshold will be used if set to ``None``. """ a = np.copy(a) b = np.copy(b) nan_mask = np.logical_or(np.isnan(a), np.isnan(b)) a[nan_mask] = 0 b[nan_mask] = 0 return almost_equal(a, b, rtol, atol)
Example #3
Source File: test_utils.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def assert_almost_equal_ignore_nan(a, b, rtol=None, atol=None, names=('a', 'b')): """Test that two NumPy arrays are almost equal (ignoring NaN in either array). Combines a relative and absolute measure of approximate eqality. If either the relative or absolute check passes, the arrays are considered equal. Including an absolute check resolves issues with the relative check where all array values are close to zero. Parameters ---------- a : np.ndarray b : np.ndarray rtol : None or float The relative threshold. Default threshold will be used if set to ``None``. atol : None or float The absolute threshold. Default threshold will be used if set to ``None``. """ a = np.copy(a) b = np.copy(b) nan_mask = np.logical_or(np.isnan(a), np.isnan(b)) a[nan_mask] = 0 b[nan_mask] = 0 assert_almost_equal(a, b, rtol, atol, names)
Example #4
Source File: test_bayestar.py From dustmaps with GNU General Public License v2.0 | 6 votes |
def test_bounds(self): """ Test that out-of-bounds coordinates return NaN reddening, and that in-bounds coordinates do not return NaN reddening. """ for mode in (['random_sample', 'random_sample_per_pix', 'median', 'samples', 'mean']): # Draw random coordinates, both above and below dec = -30 degree line n_pix = 1000 ra = -180. + 360.*np.random.random(n_pix) dec = -75. + 90.*np.random.random(n_pix) # 45 degrees above/below c = coords.SkyCoord(ra, dec, frame='icrs', unit='deg') ebv_calc = self._bayestar(c, mode=mode) nan_below = np.isnan(ebv_calc[dec < -35.]) nan_above = np.isnan(ebv_calc[dec > -25.]) pct_nan_above = np.sum(nan_above) / float(nan_above.size) # print r'{:s}: {:.5f}% nan above dec=-25 deg.'.format(mode, 100.*pct_nan_above) self.assertTrue(np.all(nan_below)) self.assertTrue(pct_nan_above < 0.05)
Example #5
Source File: core.py From neuropythy with GNU Affero General Public License v3.0 | 6 votes |
def apply_cmap(zs, cmap, vmin=None, vmax=None, unit=None, logrescale=False): ''' apply_cmap(z, cmap) applies the given cmap to the values in z; if vmin and/or vmax are passed, they are used to scale z. Note that this function can automatically rescale data into log-space if the colormap is a neuropythy log-space colormap such as log_eccentricity. To enable this behaviour use the optional argument logrescale=True. ''' zs = pimms.mag(zs) if unit is None else pimms.mag(zs, unit) zs = np.asarray(zs, dtype='float') if pimms.is_str(cmap): cmap = matplotlib.cm.get_cmap(cmap) if logrescale: if vmin is None: vmin = np.log(np.nanmin(zs)) if vmax is None: vmax = np.log(np.nanmax(zs)) mn = np.exp(vmin) u = zdivide(nanlog(zs + mn) - vmin, vmax - vmin, null=np.nan) else: if vmin is None: vmin = np.nanmin(zs) if vmax is None: vmax = np.nanmax(zs) u = zdivide(zs - vmin, vmax - vmin, null=np.nan) u[np.isnan(u)] = -np.inf return cmap(u)
Example #6
Source File: train_utils.py From DOTA_models with Apache License 2.0 | 6 votes |
def train_step(sess, train_op, loss, global_step): """Runs a single training step.""" start_time = time.time() _, loss_val, global_step_val = sess.run([train_op, loss, global_step]) duration = time.time() - start_time # Logging if global_step_val % 10 == 0: examples_per_sec = FLAGS.batch_size / duration sec_per_batch = float(duration) format_str = ('step %d, loss = %.2f (%.1f examples/sec; %.3f ' 'sec/batch)') tf.logging.info(format_str % (global_step_val, loss_val, examples_per_sec, sec_per_batch)) if np.isnan(loss_val): raise OverflowError('Loss is nan') return global_step_val
Example #7
Source File: map_utils.py From DOTA_models with Apache License 2.0 | 6 votes |
def generate_egocentric_maps(scaled_maps, map_scales, map_crop_sizes, loc, x_axis, y_axis, theta): maps = [] for i, (map_, sc, map_crop_size) in enumerate(zip(scaled_maps, map_scales, map_crop_sizes)): maps_i = np.array(get_map_to_predict(loc*sc, x_axis, y_axis, map_, map_crop_size, interpolation=cv2.INTER_LINEAR)[0]) maps_i[np.isnan(maps_i)] = 0 maps.append(maps_i) return maps
Example #8
Source File: test_statistics.py From esmlab with Apache License 2.0 | 6 votes |
def test_weighted_mean(dim, level, wgts_name): res = esmlab.weighted_mean(dset, dim=dim, weights=wgts[wgts_name]) df = dset.to_dataframe() df_w = wgts.to_dataframe()[wgts_name] if not dim: res = res.to_array().data d = pd.concat([df, df_w], axis=1) expected = d.apply( lambda x: np.ma.average(np.ma.MaskedArray(x, mask=np.isnan(x)), weights=d.t_s_wgts) )[['da1', 'da2']] expected = expected.to_xarray().data np.testing.assert_allclose(res, expected) else: expected = df.groupby(level=level).apply( wavg, weights=wgts[wgts_name].data, col_names=['da1', 'da2'] ) res = res.to_dataframe() assert_frame_equal(res.sort_index(), expected.sort_index())
Example #9
Source File: helper.py From Deep_Learning_Weather_Forecasting with Apache License 2.0 | 6 votes |
def nan_helper(y): """Helper to handle indices and logical indices of NaNs. Input: - y, 1d numpy array with possible NaNs Output: - nans, logical indices of NaNs - index, a function, with signature indices= index(logical_indices), to convert logical indices of NaNs to 'equivalent' indices Example: >>> # linear interpolation of NaNs >>> nans, x= nan_helper(y) >>> y[nans]= np.interp(x(nans), x(~nans), y[~nans]) """ return np.isnan(y), lambda z: z.nonzero()[0]
Example #10
Source File: torch_utils.py From pytorch-trpo with MIT License | 6 votes |
def fit(self, observations, labels): def closure(): predicted = self.predict(observations) loss = self.loss_fn(predicted, labels) self.optimizer.zero_grad() loss.backward() return loss old_params = parameters_to_vector(self.model.parameters()) for lr in self.lr * .5**np.arange(10): self.optimizer = optim.LBFGS(self.model.parameters(), lr=lr) self.optimizer.step(closure) current_params = parameters_to_vector(self.model.parameters()) if any(np.isnan(current_params.data.cpu().numpy())): print("LBFGS optimization diverged. Rolling back update...") vector_to_parameters(old_params, self.model.parameters()) else: return
Example #11
Source File: test_std.py From differential-privacy-library with MIT License | 5 votes |
def test_nan(self): a = np.random.random((5, 5)) a[2, 2] = np.nan res = std(a, bounds=(0, 1)) self.assertTrue(np.isnan(res))
Example #12
Source File: test_nanstd.py From differential-privacy-library with MIT License | 5 votes |
def test_nan(self): a = np.random.random((5, 5)) a[2, 2] = np.nan res = nanstd(a, bounds=(0, 1)) self.assertFalse(np.isnan(res))
Example #13
Source File: test_mean.py From differential-privacy-library with MIT License | 5 votes |
def test_nan(self): a = np.random.random((5, 5)) a[2, 2] = np.nan res = mean(a, bounds=(0, 1)) self.assertTrue(np.isnan(res))
Example #14
Source File: test_sum.py From differential-privacy-library with MIT License | 5 votes |
def test_nan(self): a = np.random.random((5, 5)) a[2, 2] = np.nan res = sum(a, bounds=(0, 1)) self.assertTrue(np.isnan(res))
Example #15
Source File: test_nanvar.py From differential-privacy-library with MIT License | 5 votes |
def test_nan(self): a = np.random.random((5, 5)) a[2, 2] = np.nan res = nanvar(a, bounds=(0, 1)) self.assertFalse(np.isnan(res))
Example #16
Source File: test_var.py From differential-privacy-library with MIT License | 5 votes |
def test_nan(self): a = np.random.random((5, 5)) a[2, 2] = np.nan res = var(a, bounds=(0, 1)) self.assertTrue(np.isnan(res))
Example #17
Source File: condensenet.py From Pytorch-Project-Template with MIT License | 5 votes |
def validate(self): """ One epoch validation :return: """ tqdm_batch = tqdm(self.data_loader.valid_loader, total=self.data_loader.valid_iterations, desc="Valiation at -{}-".format(self.current_epoch)) # set the model in training mode self.model.eval() epoch_loss = AverageMeter() top1_acc = AverageMeter() top5_acc = AverageMeter() for x, y in tqdm_batch: if self.cuda: x, y = x.cuda(async=self.config.async_loading), y.cuda(async=self.config.async_loading) x, y = Variable(x), Variable(y) # model pred = self.model(x) # loss cur_loss = self.loss(pred, y) if np.isnan(float(cur_loss.item())): raise ValueError('Loss is nan during validation...') top1, top5 = cls_accuracy(pred.data, y.data, topk=(1, 5)) epoch_loss.update(cur_loss.item()) top1_acc.update(top1.item(), x.size(0)) top5_acc.update(top5.item(), x.size(0)) self.logger.info("Validation results at epoch-" + str(self.current_epoch) + " | " + "loss: " + str( epoch_loss.avg) + "- Top1 Acc: " + str(top1_acc.val) + "- Top5 Acc: " + str(top5_acc.val)) tqdm_batch.close() return top1_acc.avg
Example #18
Source File: test_nanmean.py From differential-privacy-library with MIT License | 5 votes |
def test_nan(self): a = np.random.random((5, 5)) a[2, 2] = np.nan res = nanmean(a, bounds=(0, 1)) self.assertFalse(np.isnan(res))
Example #19
Source File: ms_ssim_np.py From imgcomp-cvpr with GNU General Public License v3.0 | 5 votes |
def _calc_msssim_orig(img1, img2): v = MultiScaleSSIM(img1, img2, max_val=255) if np.isnan(v): print(img1[0, :15, :15, 0]) print(img2[0, :15, :15, 0]) return np.float32(v)
Example #20
Source File: val.py From imgcomp-cvpr with GNU General Public License v3.0 | 5 votes |
def update(self, fetch_dict_out): for tag, value in fetch_dict_out.items(): if tag in self.tags_to_agregate: assert not np.isnan(value), 'nan encountered in {}'.format(fetch_dict_out) self._tags_to_values[tag].append(value)
Example #21
Source File: standard_scaler.py From differential-privacy-library with MIT License | 5 votes |
def _incremental_mean_and_var(X, epsilon, bounds, last_mean, last_variance, last_sample_count): # Initialising new accountant, as budget is tracked in main class. Subject to review in line with GH issue #21 temp_acc = BudgetAccountant() # old = stats until now # new = the current increment # updated = the aggregated stats last_sum = last_mean * last_sample_count new_mean = nanmean(X, epsilon=epsilon, axis=0, bounds=bounds, accountant=temp_acc) new_sample_count = np.sum(~np.isnan(X), axis=0) new_sum = new_mean * new_sample_count updated_sample_count = last_sample_count + new_sample_count updated_mean = (last_sum + new_sum) / updated_sample_count if last_variance is None: updated_variance = None else: new_unnormalized_variance = nanvar(X, epsilon=epsilon, axis=0, bounds=bounds, accountant=temp_acc) * new_sample_count last_unnormalized_variance = last_variance * last_sample_count with np.errstate(divide='ignore', invalid='ignore'): last_over_new_count = last_sample_count / new_sample_count updated_unnormalized_variance = ( last_unnormalized_variance + new_unnormalized_variance + last_over_new_count / updated_sample_count * (last_sum / last_over_new_count - new_sum) ** 2) zeros = last_sample_count == 0 updated_unnormalized_variance[zeros] = new_unnormalized_variance[zeros] updated_variance = updated_unnormalized_variance / updated_sample_count return updated_mean, updated_variance, updated_sample_count # noinspection PyPep8Naming,PyAttributeOutsideInit
Example #22
Source File: numerical.py From Kaggler with MIT License | 5 votes |
def fit(self, X, y=None): """Get empirical CDFs of numerical features. Args: X (pandas.DataFrame): numerical features to encode Returns: A trained QuantileEncoder object. """ def _calculate_ecdf(x): return ECDF(x[~np.isnan(x)]) if self.sample >= X.shape[0]: self.ecdfs = X.apply(_calculate_ecdf, axis=0) elif self.sample > 1: self.ecdfs = X.sample(n=self.sample, random_state=self.random_state).apply( _calculate_ecdf, axis=0 ) else: self.ecdfs = X.sample(frac=self.sample, random_state=self.random_state).apply( _calculate_ecdf, axis=0 ) return self
Example #23
Source File: foundation.py From TOPFARM with GNU Affero General Public License v3.0 | 5 votes |
def execute(self): foundation_func = LinearNDInterpolator(self.depth[:,0:2],self.depth[:,2]) self.foundations = array([foundation_func(self.wt_positions[i,0],self.wt_positions[i,1]) for i in range(self.wt_positions.shape[0])]) #dist = DistFromBorders()(wt_positions=self.wt_positions, borders=self.borders).dist min_depth = self.depth[:,2].min() self.foundations[isnan(self.foundations)] = min_depth if self.scaling == 0.0: # Using the baseline for scaling self.scaling = sum(self.foundations) self.foundation_length = sum(self.foundations)/self.scaling # print 'foundations:', self.foundation_length
Example #24
Source File: tf_util.py From cs294-112_hws with MIT License | 5 votes |
def __call__(self, *inputvals): assert len(inputvals) == len(self.inputs) feed_dict = dict(zip(self.inputs, inputvals)) feed_dict.update(self.givens) results = get_session().run(self.outputs_update, feed_dict=feed_dict)[:-1] if self.check_nan: if any(np.isnan(r).any() for r in results): raise RuntimeError("Nan detected") return results
Example #25
Source File: test_TargetList.py From EXOSIMS with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_fillPhotometry(self): """ Filling in photometry should result in larger or equal sized target list """ with RedirectStreams(stdout=self.dev_null): sim = MissionSim.MissionSim(scriptfile,fillPhotometry=True) self.assertTrue(sim.TargetList.fillPhotometry) #self.assertGreaterEqual(sim.TargetList.nStars, self.targetlist.nStars) self.assertTrue(np.all(sim.TargetList.Imag != 0) and np.all(~np.isnan(sim.TargetList.Imag)))
Example #26
Source File: metrics_test.py From DOTA_models with Apache License 2.0 | 5 votes |
def test_compute_precision_recall_and_ap_no_groundtruth(self): num_gt = 0 scores = np.array([0.4, 0.3, 0.6, 0.2, 0.7, 0.1], dtype=float) labels = np.array([0, 0, 0, 0, 0, 0], dtype=bool) expected_precision = None expected_recall = None precision, recall = metrics.compute_precision_recall(scores, labels, num_gt) self.assertEquals(precision, expected_precision) self.assertEquals(recall, expected_recall) ap = metrics.compute_average_precision(precision, recall) self.assertTrue(np.isnan(ap))
Example #27
Source File: nav_env.py From DOTA_models with Apache License 2.0 | 5 votes |
def image_pre(images, modalities): # Assumes images are ...xHxWxC. # We always assume images are RGB followed by Depth. if 'depth' in modalities: d = images[...,-1][...,np.newaxis]*1. d[d < 0.01] = np.NaN; isnan = np.isnan(d); d = 100./d; d[isnan] = 0.; images = np.concatenate((images[...,:-1], d, isnan), axis=images.ndim-1) if 'rgb' in modalities: images[...,:3] = images[...,:3]*1. - 128 return images
Example #28
Source File: map_utils.py From DOTA_models with Apache License 2.0 | 5 votes |
def get_map_to_predict(src_locs, src_x_axiss, src_y_axiss, map, map_size, interpolation=cv2.INTER_LINEAR): fss = [] valids = [] center = (map_size-1.0)/2.0 dst_theta = np.pi/2.0 dst_loc = np.array([center, center]) dst_x_axis = np.array([np.cos(dst_theta), np.sin(dst_theta)]) dst_y_axis = np.array([np.cos(dst_theta+np.pi/2), np.sin(dst_theta+np.pi/2)]) def compute_points(center, x_axis, y_axis): points = np.zeros((3,2),dtype=np.float32) points[0,:] = center points[1,:] = center + x_axis points[2,:] = center + y_axis return points dst_points = compute_points(dst_loc, dst_x_axis, dst_y_axis) for i in range(src_locs.shape[0]): src_loc = src_locs[i,:] src_x_axis = src_x_axiss[i,:] src_y_axis = src_y_axiss[i,:] src_points = compute_points(src_loc, src_x_axis, src_y_axis) M = cv2.getAffineTransform(src_points, dst_points) fs = cv2.warpAffine(map, M, (map_size, map_size), None, flags=interpolation, borderValue=np.NaN) valid = np.invert(np.isnan(fs)) valids.append(valid) fss.append(fs) return fss, valids
Example #29
Source File: test_loss.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def test_poisson_nllloss(): pred = mx.nd.random.normal(shape=(3, 4)) min_pred = mx.nd.min(pred) #This is necessary to ensure only positive random values are generated for prediction, # to avoid ivalid log calculation pred[:] = pred + mx.nd.abs(min_pred) target = mx.nd.random.normal(shape=(3, 4)) min_target = mx.nd.min(target) #This is necessary to ensure only positive random values are generated for prediction, # to avoid ivalid log calculation target[:] += mx.nd.abs(min_target) Loss = gluon.loss.PoissonNLLLoss(from_logits=True) Loss_no_logits = gluon.loss.PoissonNLLLoss(from_logits=False) #Calculating by brute formula for default value of from_logits = True # 1) Testing for flag logits = True brute_loss = np.mean(np.exp(pred.asnumpy()) - target.asnumpy() * pred.asnumpy()) loss_withlogits = Loss(pred, target) assert_almost_equal(brute_loss, loss_withlogits.asscalar()) #2) Testing for flag logits = False loss_no_logits = Loss_no_logits(pred, target) np_loss_no_logits = np.mean(pred.asnumpy() - target.asnumpy() * np.log(pred.asnumpy() + 1e-08)) if np.isnan(loss_no_logits.asscalar()): assert_almost_equal(np.isnan(np_loss_no_logits), np.isnan(loss_no_logits.asscalar())) else: assert_almost_equal(np_loss_no_logits, loss_no_logits.asscalar()) #3) Testing for Sterling approximation np_pred = np.random.uniform(1, 5, (2, 3)) np_target = np.random.uniform(1, 5, (2, 3)) np_compute_full = np.mean((np_pred - np_target * np.log(np_pred + 1e-08)) + ((np_target * np.log(np_target)-\ np_target + 0.5 * np.log(2 * np_target * np.pi))*(np_target > 1))) Loss_compute_full = gluon.loss.PoissonNLLLoss(from_logits=False, compute_full=True) loss_compute_full = Loss_compute_full(mx.nd.array(np_pred), mx.nd.array(np_target)) assert_almost_equal(np_compute_full, loss_compute_full.asscalar())
Example #30
Source File: Collection.py From fullrmc with GNU Affero General Public License v3.0 | 5 votes |
def orient(xyzArray, arrayAxis, alignToAxis): """ Rotates xyzArray using the rotation matrix that rotates and aligns arrayAxis to alignToAXis. :Parameters: #. xyzArray (numpy.ndarray): The xyz (N,3) array to rotate. #. arrayAxis (list, tuple, numpy.ndarray): xyzArray axis. #. alignToAxis (list, tuple, numpy.ndarray): The axis to align to. """ # normalize alignToAxis alignToAxisNorm = np.linalg.norm(alignToAxis) assert alignToAxisNorm>0, LOGGER.error("alignToAxis returned 0 norm") alignToAxis = np.array(alignToAxis, dtype=FLOAT_TYPE)/alignToAxisNorm # normalize arrayAxis arrayAxisNorm = np.linalg.norm(arrayAxis) assert arrayAxisNorm>0, LOGGER.error("arrayAxis returned 0 norm") arrayAxis = np.array(arrayAxis, dtype=FLOAT_TYPE)/arrayAxisNorm # calculate rotationAngle dotProduct = np.dot(arrayAxis, alignToAxis) if np.abs(dotProduct-1) <= PRECISION : rotationAngle = 0 elif np.abs(dotProduct+1) <= PRECISION : rotationAngle = PI else: rotationAngle = np.arccos( dotProduct ) if np.isnan(rotationAngle) or np.abs(rotationAngle) <= PRECISION : return xyzArray # calculate rotation axis. if np.abs(rotationAngle-PI) <= PRECISION: rotationAxis = get_random_perpendicular_vector(arrayAxis) else: rotationAxis = np.cross(alignToAxis, arrayAxis) #rotationAxis /= np.linalg.norm(rotationAxis) # calculate rotation matrix rotationMatrix = get_rotation_matrix(rotationAxis, rotationAngle) # rotate and return return rotate(xyzArray , rotationMatrix)