Python numpy.round_() Examples
The following are 29
code examples of numpy.round_().
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: test_extras.py From recruit with Apache License 2.0 | 6 votes |
def test_2d(self): # Tests mr_ on 2D arrays. a_1 = np.random.rand(5, 5) a_2 = np.random.rand(5, 5) m_1 = np.round_(np.random.rand(5, 5), 0) m_2 = np.round_(np.random.rand(5, 5), 0) b_1 = masked_array(a_1, mask=m_1) b_2 = masked_array(a_2, mask=m_2) # append columns d = mr_['1', b_1, b_2] assert_(d.shape == (5, 10)) assert_array_equal(d[:, :5], b_1) assert_array_equal(d[:, 5:], b_2) assert_array_equal(d.mask, np.r_['1', m_1, m_2]) d = mr_[b_1, b_2] assert_(d.shape == (10, 5)) assert_array_equal(d[:5,:], b_1) assert_array_equal(d[5:,:], b_2) assert_array_equal(d.mask, np.r_[m_1, m_2])
Example #2
Source File: test_extras.py From keras-lambda with MIT License | 6 votes |
def test_2d(self): # Tests mr_ on 2D arrays. a_1 = np.random.rand(5, 5) a_2 = np.random.rand(5, 5) m_1 = np.round_(np.random.rand(5, 5), 0) m_2 = np.round_(np.random.rand(5, 5), 0) b_1 = masked_array(a_1, mask=m_1) b_2 = masked_array(a_2, mask=m_2) # append columns d = mr_['1', b_1, b_2] self.assertTrue(d.shape == (5, 10)) assert_array_equal(d[:, :5], b_1) assert_array_equal(d[:, 5:], b_2) assert_array_equal(d.mask, np.r_['1', m_1, m_2]) d = mr_[b_1, b_2] self.assertTrue(d.shape == (10, 5)) assert_array_equal(d[:5,:], b_1) assert_array_equal(d[5:,:], b_2) assert_array_equal(d.mask, np.r_[m_1, m_2])
Example #3
Source File: test_extras.py From twitter-stock-recommendation with MIT License | 6 votes |
def test_2d(self): # Tests mr_ on 2D arrays. a_1 = np.random.rand(5, 5) a_2 = np.random.rand(5, 5) m_1 = np.round_(np.random.rand(5, 5), 0) m_2 = np.round_(np.random.rand(5, 5), 0) b_1 = masked_array(a_1, mask=m_1) b_2 = masked_array(a_2, mask=m_2) # append columns d = mr_['1', b_1, b_2] assert_(d.shape == (5, 10)) assert_array_equal(d[:, :5], b_1) assert_array_equal(d[:, 5:], b_2) assert_array_equal(d.mask, np.r_['1', m_1, m_2]) d = mr_[b_1, b_2] assert_(d.shape == (10, 5)) assert_array_equal(d[:5,:], b_1) assert_array_equal(d[5:,:], b_2) assert_array_equal(d.mask, np.r_[m_1, m_2])
Example #4
Source File: test_extras.py From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License | 6 votes |
def test_2d(self): # Tests mr_ on 2D arrays. a_1 = np.random.rand(5, 5) a_2 = np.random.rand(5, 5) m_1 = np.round_(np.random.rand(5, 5), 0) m_2 = np.round_(np.random.rand(5, 5), 0) b_1 = masked_array(a_1, mask=m_1) b_2 = masked_array(a_2, mask=m_2) # append columns d = mr_['1', b_1, b_2] assert_(d.shape == (5, 10)) assert_array_equal(d[:, :5], b_1) assert_array_equal(d[:, 5:], b_2) assert_array_equal(d.mask, np.r_['1', m_1, m_2]) d = mr_[b_1, b_2] assert_(d.shape == (10, 5)) assert_array_equal(d[:5,:], b_1) assert_array_equal(d[5:,:], b_2) assert_array_equal(d.mask, np.r_[m_1, m_2])
Example #5
Source File: test_extras.py From coffeegrindsize with MIT License | 6 votes |
def test_2d(self): # Tests mr_ on 2D arrays. a_1 = np.random.rand(5, 5) a_2 = np.random.rand(5, 5) m_1 = np.round_(np.random.rand(5, 5), 0) m_2 = np.round_(np.random.rand(5, 5), 0) b_1 = masked_array(a_1, mask=m_1) b_2 = masked_array(a_2, mask=m_2) # append columns d = mr_['1', b_1, b_2] assert_(d.shape == (5, 10)) assert_array_equal(d[:, :5], b_1) assert_array_equal(d[:, 5:], b_2) assert_array_equal(d.mask, np.r_['1', m_1, m_2]) d = mr_[b_1, b_2] assert_(d.shape == (10, 5)) assert_array_equal(d[:5,:], b_1) assert_array_equal(d[5:,:], b_2) assert_array_equal(d.mask, np.r_[m_1, m_2])
Example #6
Source File: tests.py From lamipy with MIT License | 6 votes |
def test_assemble_matrixT_returns_expected_output_with_known_input(self): """Sends input that has known T matrix, expects the same output""" ang_list = [0, 90, -45, +45, 90, 0] for ang in ang_list: T = clt.assemble_matrixT(ang) T = numpy.round_(T, 6) if ang == 0: expected_T = numpy.array([[ 1, 0, 0], [ 0, 1, 0], [ 0, 0, 1]]) elif ang == 90: expected_T = numpy.array([[ 0, 1, 0], [ 1, 0, 0], [ 0, 0, -1]]) elif ang == -45: expected_T = numpy.array([[ 0.5, 0.5, -0.5], [ 0.5, 0.5, -0.5], [ 1, -1, 0]]) elif ang == +45: expected_T = numpy.array([[ 0.5, 0.5, 0.5], [ 0.5, 0.5, -0.5], [ -1, 1, 0]]) expected_Ti_list = [int(Ti) for Ti in numpy.nditer(expected_T)] returned_Ti_list = [int(Ti) for Ti in numpy.nditer(T)] self.assertTrue(expected_Ti_list == returned_Ti_list)
Example #7
Source File: test_extras.py From elasticintel with GNU General Public License v3.0 | 6 votes |
def test_2d(self): # Tests mr_ on 2D arrays. a_1 = np.random.rand(5, 5) a_2 = np.random.rand(5, 5) m_1 = np.round_(np.random.rand(5, 5), 0) m_2 = np.round_(np.random.rand(5, 5), 0) b_1 = masked_array(a_1, mask=m_1) b_2 = masked_array(a_2, mask=m_2) # append columns d = mr_['1', b_1, b_2] self.assertTrue(d.shape == (5, 10)) assert_array_equal(d[:, :5], b_1) assert_array_equal(d[:, 5:], b_2) assert_array_equal(d.mask, np.r_['1', m_1, m_2]) d = mr_[b_1, b_2] self.assertTrue(d.shape == (10, 5)) assert_array_equal(d[:5,:], b_1) assert_array_equal(d[5:,:], b_2) assert_array_equal(d.mask, np.r_[m_1, m_2])
Example #8
Source File: test_extras.py From ImageFusion with MIT License | 6 votes |
def test_2d(self): # Tests mr_ on 2D arrays. a_1 = rand(5, 5) a_2 = rand(5, 5) m_1 = np.round_(rand(5, 5), 0) m_2 = np.round_(rand(5, 5), 0) b_1 = masked_array(a_1, mask=m_1) b_2 = masked_array(a_2, mask=m_2) # append columns d = mr_['1', b_1, b_2] self.assertTrue(d.shape == (5, 10)) assert_array_equal(d[:, :5], b_1) assert_array_equal(d[:, 5:], b_2) assert_array_equal(d.mask, np.r_['1', m_1, m_2]) d = mr_[b_1, b_2] self.assertTrue(d.shape == (10, 5)) assert_array_equal(d[:5,:], b_1) assert_array_equal(d[5:,:], b_2) assert_array_equal(d.mask, np.r_[m_1, m_2])
Example #9
Source File: Simple.py From zEpid with MIT License | 6 votes |
def summary(self, decimal=3): """Generate the summary information after the corrected risk ratio distribution is generated. fit() must be run before this Parameters ------------- decimal : int, optional Decimal places to display in output. Default is 3 """ print('----------------------------------------------------------------------') print('Median corrected Risk Ratio: ', np.round(np.median(self.corrected_RR),decimal)) print('Mean corrected Risk Ratio: ', np.round(np.mean(self.corrected_RR),decimal)) print('25th & 75th Percentiles: ', np.round_(np.percentile(self.corrected_RR,q=[25, 75]), decimals=decimal)) print('2.5th & 97.5th Percentiles: ', np.round_(np.percentile(self.corrected_RR,q=[2.5, 97.5]), decimals=decimal)) print('----------------------------------------------------------------------')
Example #10
Source File: utils.py From Multi-View-Silhouette-and-Depth-Decomposition-for-High-Resolution-3D-Object-Representation with MIT License | 6 votes |
def fast_smoothing(odms, high, low, threshold): for i,odm in (enumerate(odms)): copy = np.array(odm) on = np.where(odm != high) for x,y in zip(*on): window = odm[x-3:x+4,y-3:y+4] #window considered = np.where( abs(window - odm[x,y])< threshold) copy[x,y] = np.average(window[considered]) odms[i] = np.round_(copy) return odms # reconvers full odm from occupancy and depth map
Example #11
Source File: test_extras.py From mxnet-lambda with Apache License 2.0 | 6 votes |
def test_2d(self): # Tests mr_ on 2D arrays. a_1 = np.random.rand(5, 5) a_2 = np.random.rand(5, 5) m_1 = np.round_(np.random.rand(5, 5), 0) m_2 = np.round_(np.random.rand(5, 5), 0) b_1 = masked_array(a_1, mask=m_1) b_2 = masked_array(a_2, mask=m_2) # append columns d = mr_['1', b_1, b_2] self.assertTrue(d.shape == (5, 10)) assert_array_equal(d[:, :5], b_1) assert_array_equal(d[:, 5:], b_2) assert_array_equal(d.mask, np.r_['1', m_1, m_2]) d = mr_[b_1, b_2] self.assertTrue(d.shape == (10, 5)) assert_array_equal(d[:5,:], b_1) assert_array_equal(d[5:,:], b_2) assert_array_equal(d.mask, np.r_[m_1, m_2])
Example #12
Source File: test_extras.py From pySINDy with MIT License | 6 votes |
def test_2d(self): # Tests mr_ on 2D arrays. a_1 = np.random.rand(5, 5) a_2 = np.random.rand(5, 5) m_1 = np.round_(np.random.rand(5, 5), 0) m_2 = np.round_(np.random.rand(5, 5), 0) b_1 = masked_array(a_1, mask=m_1) b_2 = masked_array(a_2, mask=m_2) # append columns d = mr_['1', b_1, b_2] assert_(d.shape == (5, 10)) assert_array_equal(d[:, :5], b_1) assert_array_equal(d[:, 5:], b_2) assert_array_equal(d.mask, np.r_['1', m_1, m_2]) d = mr_[b_1, b_2] assert_(d.shape == (10, 5)) assert_array_equal(d[:5,:], b_1) assert_array_equal(d[5:,:], b_2) assert_array_equal(d.mask, np.r_[m_1, m_2])
Example #13
Source File: test_extras.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 6 votes |
def test_2d(self): # Tests mr_ on 2D arrays. a_1 = np.random.rand(5, 5) a_2 = np.random.rand(5, 5) m_1 = np.round_(np.random.rand(5, 5), 0) m_2 = np.round_(np.random.rand(5, 5), 0) b_1 = masked_array(a_1, mask=m_1) b_2 = masked_array(a_2, mask=m_2) # append columns d = mr_['1', b_1, b_2] assert_(d.shape == (5, 10)) assert_array_equal(d[:, :5], b_1) assert_array_equal(d[:, 5:], b_2) assert_array_equal(d.mask, np.r_['1', m_1, m_2]) d = mr_[b_1, b_2] assert_(d.shape == (10, 5)) assert_array_equal(d[:5,:], b_1) assert_array_equal(d[5:,:], b_2) assert_array_equal(d.mask, np.r_[m_1, m_2])
Example #14
Source File: test_extras.py From lambda-packs with MIT License | 6 votes |
def test_2d(self): # Tests mr_ on 2D arrays. a_1 = np.random.rand(5, 5) a_2 = np.random.rand(5, 5) m_1 = np.round_(np.random.rand(5, 5), 0) m_2 = np.round_(np.random.rand(5, 5), 0) b_1 = masked_array(a_1, mask=m_1) b_2 = masked_array(a_2, mask=m_2) # append columns d = mr_['1', b_1, b_2] self.assertTrue(d.shape == (5, 10)) assert_array_equal(d[:, :5], b_1) assert_array_equal(d[:, 5:], b_2) assert_array_equal(d.mask, np.r_['1', m_1, m_2]) d = mr_[b_1, b_2] self.assertTrue(d.shape == (10, 5)) assert_array_equal(d[:5,:], b_1) assert_array_equal(d[5:,:], b_2) assert_array_equal(d.mask, np.r_[m_1, m_2])
Example #15
Source File: test_extras.py From GraphicDesignPatternByPython with MIT License | 6 votes |
def test_2d(self): # Tests mr_ on 2D arrays. a_1 = np.random.rand(5, 5) a_2 = np.random.rand(5, 5) m_1 = np.round_(np.random.rand(5, 5), 0) m_2 = np.round_(np.random.rand(5, 5), 0) b_1 = masked_array(a_1, mask=m_1) b_2 = masked_array(a_2, mask=m_2) # append columns d = mr_['1', b_1, b_2] assert_(d.shape == (5, 10)) assert_array_equal(d[:, :5], b_1) assert_array_equal(d[:, 5:], b_2) assert_array_equal(d.mask, np.r_['1', m_1, m_2]) d = mr_[b_1, b_2] assert_(d.shape == (10, 5)) assert_array_equal(d[:5,:], b_1) assert_array_equal(d[5:,:], b_2) assert_array_equal(d.mask, np.r_[m_1, m_2])
Example #16
Source File: test_extras.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def test_2d(self): # Tests mr_ on 2D arrays. a_1 = np.random.rand(5, 5) a_2 = np.random.rand(5, 5) m_1 = np.round_(np.random.rand(5, 5), 0) m_2 = np.round_(np.random.rand(5, 5), 0) b_1 = masked_array(a_1, mask=m_1) b_2 = masked_array(a_2, mask=m_2) # append columns d = mr_['1', b_1, b_2] self.assertTrue(d.shape == (5, 10)) assert_array_equal(d[:, :5], b_1) assert_array_equal(d[:, 5:], b_2) assert_array_equal(d.mask, np.r_['1', m_1, m_2]) d = mr_[b_1, b_2] self.assertTrue(d.shape == (10, 5)) assert_array_equal(d[:5,:], b_1) assert_array_equal(d[5:,:], b_2) assert_array_equal(d.mask, np.r_[m_1, m_2])
Example #17
Source File: test_extras.py From Mastering-Elasticsearch-7.0 with MIT License | 6 votes |
def test_2d(self): # Tests mr_ on 2D arrays. a_1 = np.random.rand(5, 5) a_2 = np.random.rand(5, 5) m_1 = np.round_(np.random.rand(5, 5), 0) m_2 = np.round_(np.random.rand(5, 5), 0) b_1 = masked_array(a_1, mask=m_1) b_2 = masked_array(a_2, mask=m_2) # append columns d = mr_['1', b_1, b_2] assert_(d.shape == (5, 10)) assert_array_equal(d[:, :5], b_1) assert_array_equal(d[:, 5:], b_2) assert_array_equal(d.mask, np.r_['1', m_1, m_2]) d = mr_[b_1, b_2] assert_(d.shape == (10, 5)) assert_array_equal(d[:5,:], b_1) assert_array_equal(d[5:,:], b_2) assert_array_equal(d.mask, np.r_[m_1, m_2])
Example #18
Source File: test_extras.py From Computable with MIT License | 6 votes |
def test_2d(self): "Tests mr_ on 2D arrays." a_1 = rand(5, 5) a_2 = rand(5, 5) m_1 = np.round_(rand(5, 5), 0) m_2 = np.round_(rand(5, 5), 0) b_1 = masked_array(a_1, mask=m_1) b_2 = masked_array(a_2, mask=m_2) d = mr_['1', b_1, b_2] # append columns self.assertTrue(d.shape == (5, 10)) assert_array_equal(d[:, :5], b_1) assert_array_equal(d[:, 5:], b_2) assert_array_equal(d.mask, np.r_['1', m_1, m_2]) d = mr_[b_1, b_2] self.assertTrue(d.shape == (10, 5)) assert_array_equal(d[:5,:], b_1) assert_array_equal(d[5:,:], b_2) assert_array_equal(d.mask, np.r_[m_1, m_2])
Example #19
Source File: test_extras.py From vnpy_crypto with MIT License | 6 votes |
def test_2d(self): # Tests mr_ on 2D arrays. a_1 = np.random.rand(5, 5) a_2 = np.random.rand(5, 5) m_1 = np.round_(np.random.rand(5, 5), 0) m_2 = np.round_(np.random.rand(5, 5), 0) b_1 = masked_array(a_1, mask=m_1) b_2 = masked_array(a_2, mask=m_2) # append columns d = mr_['1', b_1, b_2] assert_(d.shape == (5, 10)) assert_array_equal(d[:, :5], b_1) assert_array_equal(d[:, 5:], b_2) assert_array_equal(d.mask, np.r_['1', m_1, m_2]) d = mr_[b_1, b_2] assert_(d.shape == (10, 5)) assert_array_equal(d[:5,:], b_1) assert_array_equal(d[5:,:], b_2) assert_array_equal(d.mask, np.r_[m_1, m_2])
Example #20
Source File: matrixmod2.py From pyGSTi with Apache License 2.0 | 5 votes |
def fix_top(A): """ Takes a symmetric binary matrix with ones along the diagonal and returns the permutation matrix P such that the [1:t,1:t] submatrix of P A P is invertible """ if A.shape == (1, 1): return _np.eye(1, dtype='int') t = len(A) found_B = False for ind in range(t): aa, P = permute_top(A, ind) B = _np.round_(aa[1:, 1:]) if detmod2(B) == 0: continue else: found_B = True break # Todo : put a more meaningful fail message here # assert(found_B), "Algorithm failed!" return P
Example #21
Source File: test_apex_executor.py From rlgraph with Apache License 2.0 | 5 votes |
def test_learning_2x2_grid_world(self): """ Tests if apex can learn a simple environment using a single worker, thus replicating dqn. """ env_spec = dict( type="grid-world", world="2x2", save_mode=False ) agent_config = config_from_path("configs/apex_agent_for_2x2_gridworld.json") executor = ApexExecutor( environment_spec=env_spec, agent_config=agent_config, ) # Define executor, test assembly. print("Successfully created executor.") # Executes actual workload. result = executor.execute_workload(workload=dict( num_timesteps=5000, report_interval=100, report_interval_min_seconds=1) ) full_worker_stats = executor.result_by_worker() print("All finished episode rewards") print(full_worker_stats["episode_rewards"]) print("STATES:\n{}".format(executor.local_agent.last_q_table["states"])) print("\n\nQ(s,a)-VALUES:\n{}".format(np.round_(executor.local_agent.last_q_table["q_values"], decimals=2))) # Check q-table for correct values. expected_q_values_per_state = { (1.0, 0, 0, 0): (-1, -5, 0, -1), (0, 1.0, 0, 0): (-1, 1, 0, 0) } for state, q_values in zip( executor.local_agent.last_q_table["states"], executor.local_agent.last_q_table["q_values"] ): state, q_values = tuple(state), tuple(q_values) assert state in expected_q_values_per_state, \ "ERROR: state '{}' not expected in q-table as it's a terminal state!".format(state) recursive_assert_almost_equal(q_values, expected_q_values_per_state[state], decimals=0)
Example #22
Source File: test_quantity_non_ufuncs.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_round_(self): self.check(np.round_)
Example #23
Source File: __init__.py From tiny-faces-pytorch with MIT License | 5 votes |
def get_dataloader(datapath, args, num_templates=25, template_file="templates.json", img_transforms=None, train=True, split="train"): template_file = osp.join("datasets", template_file) if osp.exists(template_file): templates = json.load(open(template_file)) else: # Cluster the bounding boxes to get the templates dataset = WIDERFace(osp.expanduser(args.traindata), []) clustering = compute_kmedoids(dataset.get_all_bboxes(), 1, indices=num_templates, option='pyclustering', max_clusters=num_templates) print("Canonical bounding boxes computed") templates = clustering[num_templates]['medoids'].tolist() # record templates json.dump(templates, open(template_file, "w")) templates = np.round_(np.array(templates), decimals=8) data_loader = data.DataLoader(WIDERFace(osp.expanduser(datapath), templates, train=train, split=split, img_transforms=img_transforms, dataset_root=osp.expanduser(args.dataset_root), debug=args.debug), batch_size=args.batch_size, shuffle=train, num_workers=args.workers, pin_memory=True) return data_loader, templates
Example #24
Source File: calc.py From PyCINRAD with GNU General Public License v3.0 | 5 votes |
def _process_grid(self, x_step: Number_T, y_step: Number_T) -> Tuple[np.ndarray]: x_lower = np.round_(self.lon_ravel.min(), 2) x_upper = np.round_(self.lon_ravel.max(), 2) y_lower = np.round_(self.lat_ravel.min(), 2) y_upper = np.round_(self.lat_ravel.max(), 2) x_grid = np.arange(x_lower, x_upper + x_step, x_step) y_grid = np.arange(y_lower, y_upper + x_step, x_step) return np.meshgrid(x_grid, y_grid)
Example #25
Source File: calc.py From PyCINRAD with GNU General Public License v3.0 | 5 votes |
def get_section( self, start_polar: Optional[Tuple[float, float]] = None, end_polar: Optional[Tuple[float, float]] = None, start_cart: Optional[Tuple[float, float]] = None, end_cart: Optional[Tuple[float, float]] = None, spacing: int = 500, ) -> Dataset: r""" Get cross-section data from input points Args: start_polar (tuple): polar coordinates of start point i.e.(distance, azimuth) end_polar (tuple): polar coordinates of end point i.e.(distance, azimuth) start_cart (tuple): geographic coordinates of start point i.e.(longitude, latitude) end_cart (tuple): geographic coordinates of end point i.e.(longitude, latitude) Returns: xarray.Dataset: Cross-section data """ if start_polar and end_polar: stlat = self.rl[0].stp["lat"] stlon = self.rl[0].stp["lon"] stp = np.round_( get_coordinate( start_polar[0], start_polar[1] * deg2rad, 0, stlon, stlat ), 2, ) enp = np.round_( get_coordinate(end_polar[0], end_polar[1] * deg2rad, 0, stlon, stlat), 2 ) elif start_cart and end_cart: stp = start_cart enp = end_cart else: raise RadarCalculationError("Invalid input") return self._get_section(stp, enp, spacing)
Example #26
Source File: utils.py From Multi-View-Silhouette-and-Depth-Decomposition-for-High-Resolution-3D-Object-Representation with MIT License | 5 votes |
def recover_depths(preds, ups, high, dis): preds = np.round_(preds*dis).reshape((-1,high,high)) ups = np.array(ups).reshape((-1,high,high)) for pred, up, i in zip(preds, ups, range(preds.shape[0])): pred = np.array(pred) pred = up + pred # add to upsampled low resolution odm off = np.where(pred > high) # set values which predict to high to be unoccupited -> 0 pred[off] = high-1 preds[i] = pred return preds # compute complete occupancy map, basically thresholds the probability outputs
Example #27
Source File: spectrum.py From spectrum_utils with Apache License 2.0 | 4 votes |
def _round(mz: np.ndarray, intensity: np.ndarray, decimals: int, combine: str)\ -> Tuple[np.ndarray, np.ndarray, np.ndarray]: """ JIT helper function for `MsmsSpectrum.round`. Parameters ---------- mz : np.ndarray The mass-to-charge ratios of the spectrum fragment peaks. intensity : np.ndarray The intensities of the corresponding spectrum fragment peaks. decimals : int Number of decimal places to round the mass-to-charge ratios. combine : {'sum', 'max'} Method used to combine intensities from merged fragment peaks. Returns ------- Tuple[np.ndarray, np.ndarray, np.ndarray] A tuple consisting of the rounded mass-to-charge ratios and the corresponding intensities and peak annotation indexes. """ mz_round = np.round_(mz, decimals, np.empty_like(mz, np.float32)) mz_unique = np.unique(mz_round) # If peaks got merged by rounding the mass-to-charge ratios we need to # combine their intensities and annotations as well. if len(mz_unique) < len(mz_round): intensity_unique = np.zeros_like(mz_unique, np.float32) annotations_unique_idx = np.zeros_like(mz_unique, np.int64) combine_is_sum = combine == 'sum' i_orig = 0 offset = 0 for i_unique in range(len(mz_unique)): # Check whether subsequent mz values got merged. while (abs(mz_unique[i_unique] - mz_round[i_orig + offset]) <= 1e-06): offset += 1 # Select the annotation of the most intense peak. annotations_unique_idx[i_unique] = i_orig + np.argmax( intensity[i_orig: i_orig + offset]) # Combine the corresponding intensities. intensity_unique[i_unique] = ( intensity[i_orig: i_orig + offset].sum() if combine_is_sum else intensity[annotations_unique_idx[i_unique]]) i_orig += offset offset = 0 return mz_unique, intensity_unique, annotations_unique_idx else: return mz_unique, intensity, np.arange(len(mz))
Example #28
Source File: level3.py From PyCINRAD with GNU General Public License v3.0 | 4 votes |
def __init__(self, file: Any): from metpy.io.nexrad import Level3File f = Level3File(file) # Because metpy interface doesn't provide station codes, # it's necessary to reopen it and read the code. with open(file, "rb") as buf: buf.seek(12) code = np.frombuffer(buf.read(2), ">i2")[0] cds = str(code).zfill(3) self.code = "Z9" + cds self._update_radar_info() product_code = f.prod_desc.prod_code self.dtype = self._det_product_type(product_code) self.radial_flag = self._is_radial(product_code) data_block = f.sym_block[0][0] data = np.array(data_block["data"], dtype=int) if self.dtype == "VEL": mapped_data = np.ma.masked_invalid(velocity_tbl[data]) rf = np.ma.masked_not_equal(mapped_data, 30) data = np.ma.masked_equal(mapped_data, 30) self.data = (data, rf) else: data[data == 0] = np.ma.masked self.data = np.ma.masked_invalid(f.map_data(data)) if self.dtype == "ET": # convert kft to km self.data *= 0.30478 elif self.dtype == "OHP": # convert in to mm self.data *= 25.4 station_info = _get_radar_info(self.code) self.radar_type = station_info[3] self.max_range = int(f.max_range) # Hard coding to adjust max range for different types of radar if f.max_range >= 230: if self.radar_type in ["SC", "CC"]: self.max_range = 150 elif self.radar_type in ["CA", "CB"]: self.max_range = 200 elif self.radar_type == "CD": self.max_range = 125 if self.radial_flag: self.az = np.linspace(0, 360, data.shape[0]) * deg2rad self.reso = self.max_range / data.shape[1] self.rng = np.arange(self.reso, self.max_range + self.reso, self.reso) else: xdim, ydim = data.shape x = np.linspace(self.max_range * -1, self.max_range, xdim) / 111 + f.lon y = np.linspace(self.max_range, self.max_range * -1, ydim) / 111 + f.lat self.lon, self.lat = np.meshgrid(x, y) self.reso = self.max_range / data.shape[0] * 2 self.stationlat = f.lat self.stationlon = f.lon self.el = np.round_(f.metadata.get("el_angle", 0), 1) self.scantime = f.metadata["vol_time"]
Example #29
Source File: geometry_converter_hex.py From ctapipe with BSD 3-Clause "New" or "Revised" License | 4 votes |
def get_orthogonal_grid_edges(pix_x, pix_y, scale_aspect=True): """calculate the bin edges of the slanted, orthogonal pixel grid to resample the pixel signals with np.histogramdd right after. Parameters ---------- pix_x, pix_y : 1D numpy arrays the list of x and y coordinates of the slanted, orthogonal pixel grid units should be in meters, and stripped off scale_aspect : boolean (default: True) if True, rescales the x-coordinates to create square pixels (instead of rectangular ones) Returns -------- x_edges, y_edges : 1D numpy arrays the bin edges for the slanted, orthogonal pixel grid x_scale : float factor by which the x-coordinates have been scaled """ # finding the size of the square patches d_x = 99 d_y = 99 x_base = pix_x[0] y_base = pix_y[0] for x, y in zip(pix_x, pix_y): if abs(y - y_base) < abs(x - x_base): d_x = min(d_x, abs(x - x_base)) if abs(y - y_base) > abs(x - x_base): d_y = min(d_y, abs(y - y_base)) # for x, y in zip(pix_x, pix_y): # if abs(y - y_base) > abs(x - x_base): # d_y = min(d_y, abs(y - y_base)) x_scale = 1 if scale_aspect: x_scale = d_y / d_x pix_x *= x_scale d_x = d_y # with the maximal extension of the axes and the size of the pixels, # determine the number of bins in each direction n_bins_x = int(np.round_(np.abs(np.max(pix_x) - np.min(pix_x)) / d_x) + 2) n_bins_y = int(np.round_(np.abs(np.max(pix_y) - np.min(pix_y)) / d_y) + 2) x_edges = np.linspace(pix_x.min(), pix_x.max(), n_bins_x) y_edges = np.linspace(pix_y.min(), pix_y.max(), n_bins_y) return (x_edges, y_edges, x_scale)