def quick_prn(a, edges=3, max_lines=25, width=100, decimals=2): """Format a structured array by setting the width so it hopefully wraps. """ width = min(len(str(a[0])), width) with np.printoptions(edgeitems=edges, threshold=max_lines, linewidth=width, precision=decimals, suppress=True, nanstr='-n-'): print("\nArray fields/values...:\n{}\n{}".format(a.dtype.names, a))
def deline(a, width=100, header="Array...", prefix=" ."): """Remove extraneous lines from array output. More useful for long arrays with ndim >= 3 Requires: -------- `a` : anything anything that can be put into array form `header` : an optional header `prefix` : text could be just spaces or something like shown """ def _pre(obj): for line in obj.splitlines(False): frmt = "{}{}".format(prefix, line) yield frmt # ---- if not isinstance(a, (list, tuple, np.ndarray)): return a a = np.asanyarray(a) if a.dtype.kind not in ('i', 'u', 'f', 'c'): return a header += " shape: {} ndim: {}".format(a.shape, a.ndim) f1 = (":arr[{}" + ", :{}"*len(a.shape[1:]) + "]") out = [header] c = 0 for i in a: a_s = f1.format(c, *i.shape) # ---- uses f1 format above out.append(a_s) out.extend(_pre(str(i))) c += 1 f = "\n".join([i for i in out if i != prefix]) with np.printoptions(edgeitems=edge, linewidth=width): print(f)
def test_ctx_mgr_restores(self): # test that print options are actually restrored opts = np.get_printoptions() with np.printoptions(precision=opts['precision'] - 1, linewidth=opts['linewidth'] - 4): pass assert_equal(np.get_printoptions(), opts)
def test_ctx_mgr_exceptions(self): # test that print options are restored even if an exeption is raised opts = np.get_printoptions() try: with np.printoptions(precision=2, linewidth=11): raise ValueError except ValueError: pass assert_equal(np.get_printoptions(), opts)
def __str__(self): fw = pt.Formatted_Write() fw.write('Optical depth information:') fw.write('Observing geometry (rt_path): {}', self.rt_path) if self.ec is not None: fw.write('Total atmospheric extinction coefficient (ec, cm-1) [layer' ', wave]:\n{}', self.ec, fmt={'float':'{: .3e}'.format}) if self.depth is None: fw.write('\nMaximum optical depth to calculate (maxdepth): {:.2f}', self.maxdepth) return fw.text ideepest = np.amax(self.ideep) if self.rt_path in pc.transmission_rt: fw.write('\nDistance along the ray path across each layer ' '(outside-in) at each impact parameter (raypath, km):') with np.printoptions(formatter={'float':'{:.1f}'.format},threshold=6): fw.write(' IP[{:3d}]: {}', 1, self.raypath[1]/pc.km) fw.write(' IP[{:3d}]: {}', 2, self.raypath[2]/pc.km) fw.write(' IP[{:3d}]: {}', 3, self.raypath[3]/pc.km) fw.write(' ...') fw.write(' IP[{:3d}]: {}', len(self.raypath)-1, self.raypath[len(self.raypath)-1]/pc.km) od_text = ('\nOptical depth at each impact parameter, down to ' 'max(ideep) (depth):') elif self.rt_path in pc.emission_rt: fw.write('\nDistance across each layer along a normal ray path ' '(raypath, km):\n {}', self.raypath/pc.km, fmt={'float':'{:.1f}'.format}, edge=4) od_text = ('\nOptical depth at each layer along a normal ray ' 'path into the planet, down to max(ideep) (depth):') fw.write('\nMaximum optical depth to calculate (maxdepth): {:.2f}', self.maxdepth) fw.write('Layer index where the optical depth reaches maxdepth (ideep):' '\n {}', self.ideep, fmt={'int': '{:3d}'.format}, edge=7) fw.write('Maximum ideep (deepest layer reaching maxdepth): {}', ideepest) if self.rt_path in pc.emission_rt: fw.write('\nPlanck emission down to max(ideep) (B, erg s-1 cm-2 ' 'sr-1 cm):\n{}', self.B[0:ideepest+1], fmt={'float':'{: .3e}'.format}) fw.write('{}\n{}', od_text, self.depth[0:ideepest+1], fmt={'float':'{: .3e}'.format}) return fw.text
def prn_(a, deci=2, width=100, title="Array", prefix=". . ", prnt=True): """Alternate format to prn_nd function. Inputs are largely the same. """ def _piece(sub, i, frmt, linewidth): """piece together 3D chunks by row""" s0 = sub.shape[0] block = np.hstack([sub[j] for j in range(s0)]) txt = "" if i is not None: fr = (":arr[{}" + ", :{}"*len(a.shape[1:]) + "]\n") txt = fr.format(i, *sub.shape) for line in block: ln = frmt.format(*line)[:linewidth] end = ["\n", "...\n"][len(ln) >= linewidth] txt += indent(ln + end, ". . ") return txt # ---- main section ---- out = "\n{}... ndim: {} shape: {}\n".format(title, a.ndim, a.shape) linewidth = width if a.ndim <= 1: return a if a.ndim == 2: a = a.reshape((1,) + a.shape) # ---- pull the 1st and 3rd dimension for 3D and 4D arrays frmt = make_row_format(dim=a.shape[-3], cols=a.shape[-1], a_kind=a.dtype.kind, deci=deci, a_max=a.max(), a_min=a.min(), width=width, prnt=False) if a.ndim == 3: s0, _, _ = a.shape out += _piece(a, None, frmt, linewidth) # ---- _piece ---- elif a.ndim == 4: s0, _, _, _ = a.shape for i in range(s0): out = out + "\n" + _piece(a[i], i, frmt, linewidth) # ---- _piece if prnt: with np.printoptions(precision=deci, linewidth=width): print(out) else: return out
def prn_(a, deci=2, wdth=100, title="Array", prefix=". . ", prn=True): """Alternate format to frmt_ function. Inputs are largely the same. """ def _piece(sub, i, frmt, linewidth): """piece together 3D chunks by row""" s0 = sub.shape[0] block = np.hstack([sub[j] for j in range(s0)]) txt = "" if i is not None: fr = (":arr[{}" + ", :{}"*len(a.shape[1:]) + "]\n") txt = fr.format(i, *sub.shape) for line in block: ln = frmt.format(*line)[:linewidth] end = ["\n", "...\n"][len(ln) >= linewidth] txt += indent(ln + end, ". . ") return txt # ---- main section ---- out = "\n{}... ndim: {} shape: {}\n".format(title, a.ndim, a.shape) linewidth = wdth if a.ndim <= 1: return a elif a.ndim == 2: a = a.reshape((1,) + a.shape) # ---- pull the 1st and 3rd dimension for 3D and 4D arrays frmt = make_row_format(dim=a.shape[-3], cols=a.shape[-1], a_kind=a.dtype.kind, deci=deci, a_max=a.max(), a_min=a.min(), wdth=wdth, prnt=False) if a.ndim == 3: s0, s1, s2 = a.shape out += _piece(a, None, frmt, linewidth) # ---- _piece ---- elif a.ndim == 4: s0, s1, s2, _ = a.shape for i in range(s0): out = out + "\n" + _piece(a[i], i, frmt, linewidth) # ---- _piece if prn: with np.printoptions(precision=deci, linewidth=wdth): print(out) else: return out
def _get_and_verify_data_sizes(data, sfreq, n_signals=None, n_times=None, times=None, warn_times=True): """Get and/or verify the data sizes and time scales.""" if not isinstance(data, (list, tuple)): raise ValueError('data has to be a list or tuple') n_signals_tot = 0 # Sometimes data can be (ndarray, SourceEstimate) groups so in the case # where ndarray comes first, don't use it for times times_inferred = False for this_data in data: this_n_signals, this_n_times = this_data.shape if n_times is not None: if this_n_times != n_times: raise ValueError('all input time series must have the same ' 'number of time points') else: n_times = this_n_times n_signals_tot += this_n_signals if hasattr(this_data, 'times'): assert isinstance(this_data, _BaseSourceEstimate) this_times = this_data.times if times is not None and not times_inferred: if warn_times and not np.allclose(times, this_times): with np.printoptions(threshold=4, linewidth=120): warn('time scales of input time series do not match:\n' f'{this_times}\n{times}') warn_times = False else: times = this_times elif times is None: times_inferred = True times = _arange_div(n_times, sfreq) if n_signals is not None: if n_signals != n_signals_tot: raise ValueError('the number of time series has to be the same in ' 'each epoch') n_signals = n_signals_tot return n_signals, n_times, times, warn_times
def testRepr(self): # test tensor repr with np.printoptions(threshold=100): arr = np.random.randint(1000, size=(11, 4, 13)) t = mt.tensor(arr, chunk_size=3) result = repr(t.execute()) expected = repr(arr) self.assertEqual(result, expected) for size in (5, 58, 60, 62, 64): pdf = pd.DataFrame(np.random.randint(1000, size=(size, 10))) # test DataFrame repr df = md.DataFrame(pdf, chunk_size=size // 2) result = repr(df.execute()) expected = repr(pdf) self.assertEqual( result, expected, 'failed repr for DataFrame when size = {}'.format(size)) # test DataFrame _repr_html_ result = df.execute()._repr_html_() expected = pdf._repr_html_() self.assertEqual( result, expected, 'failed repr html for DataFrame when size = {}'.format(size)) # test Series repr ps = pdf[0] s = md.Series(ps, chunk_size=size // 2) result = repr(s.execute()) expected = repr(ps) self.assertEqual( result, expected, 'failed repr for Series when size = {}'.format(size)) # test Index repr pind = pd.date_range('2020-1-1', periods=10) ind = md.Index(pind, chunk_size=5) self.assertIn('DatetimeIndex', repr(ind.execute()))
def waypoint_transition(self): """ 1. Command the next waypoint position 2. Transition to WAYPOINT state """ print("waypoint transition") if len(self.all_waypoints) < 1: return waypoint = self.all_waypoints.pop(0) self.target_position[0:3] = waypoint self.print_position(msg_level=3) with np.printoptions(precision=3, suppress=True): print_debug_message("Move to Waypoint: {}".format( self.target_position), msg_level=2) self.cmd_position(self.target_position[0], self.target_position[1], -self.target_position[2], 0.0) self.flight_state = States.WAYPOINT
def test_lu(): """ Сравниваем наше LU с LU из SciPy NB: вообще SciPy возвращет (P,L,U), где P - перестановочная матрица, но тут она единичная Q: какова сложность нашей реализации LU? """ with np.printoptions(precision=3, suppress=True): A = np.array([ [9, 1, 2], [0, 8, 1], [9, 1, 9], ], dtype='float64') P, L0, U0 = spla.lu(A) L1, U1 = lu(A) assert npla.norm(A - L1 @ U1) < 1e-6 assert npla.norm(L1 - L0) < 1e-6 assert npla.norm(U1 - U0) < 1e-6
def _to_str(self, representation=False): if is_build_mode() or len(self._executed_sessions) == 0: # in build mode, or not executed, just return representation if representation: return f'Tensor <op={type(self._op).__name__}, shape={self._shape}, key={self._key}>' else: return f'Tensor(op={type(self._op).__name__}, shape={self._shape})' else: print_options = np.get_printoptions() threshold = print_options['threshold'] corner_data = fetch_corner_data(self, session=self._executed_sessions[-1]) # if less than default threshold, just set it as default, # if not, set to corner_data.size - 1 make sure ... exists in repr threshold = threshold if self.size <= threshold else corner_data.size - 1 with np.printoptions(threshold=threshold): corner_str = repr(corner_data) if representation else str(corner_data) return corner_str
def affiche_chiffre_test(i): plt.imshow(X_test_data[i], cmap='Greys') chiffre_predit = np.argmax(Y_predict[i]) perc_max = round(100 * np.max(Y_predict[i])) # '{:.1%}'.format(1/3.0) print("\n --- Image numéro", i) with np.printoptions(precision=3, suppress=True): print("Sortie réseau", Y_predict[i]) print("Chiffre attendu :", Y_test_data[i]) plt.title('Attendu %d - Prédit %d (%d%%)' % (Y_test_data[i], chiffre_predit, perc_max), fontsize=25) plt.tight_layout() # plt.savefig('tf2-chiffre-test-result-%d.png' %i) plt.show() return
def quantifiers_in_order_of_monotonicity(l, measure=upward_monotonicity_extensions ): """ Prints all quantifiers on models of length l in order of monotonicity. :param l: Max model size :return: None """ models = generate_list_models(l) quantifiers = generate_list_models(len(models)).astype(int) mon_values = np.empty(shape=(len(quantifiers), 1)) for i in range(len(quantifiers)): mon_values[i] = measure_monotonicity(models, quantifiers[i], measure) order_indices = np.argsort(mon_values, axis=0) with np.printoptions(threshold=np.inf): pprint([(quantifier, mon_value) for quantifier, mon_value in zip( quantifiers[order_indices].tolist(), mon_values[order_indices].tolist())])
def main(): train_x_name = "train_x.csv" train_y_name = "train_y.csv" train_x = np.loadtxt(train_x_name, delimiter=',') train_y = np.loadtxt(train_y_name, delimiter=',') # load the test dateset test_x_name = "test_x.csv" test_x = np.loadtxt(test_x_name, delimiter=',') M = Model() M.fit_model(train_x, train_y) prediction = M.predict(test_x) with np.printoptions(precision=2): print(prediction) print(prediction.shape)
def simulate(self, exploration_rate=0.0, verbose=False): state = self.env.reset() state = np.reshape(state, (1, self.observation_space_size)) score = 0 while True: self.env.render() action = self.act(state, exploration_rate) if verbose: with np.printoptions(precision=5, sign=' ', floatmode='fixed', suppress=True): self.score_logger.log(f"State: {state[0]}, Output model: {self.qnet.predict(state)[0]}, Action: {action}, score: {score}") state, reward, done, info = self.env.step(action) score += reward state = np.reshape(state, (1, self.observation_space_size)) time.sleep(0.02) if done: self.score_logger.log(f"Episode finished, score: {score}") break self.env.close()
def step(self): prefix = '{:12s}'.format('PRINT({:s} (t={:.3f})'.format( self.name, self.bd.t)) value = self.inputs[0] if self.format is None: # no format string print() else: # format string provided if isinstance(value, (int, float)): print(prefix, self.format.format(value)) elif isinstance(value, np.ndarray): with np.printoptions( formatter={'all': lambda x: self.format.format(x)}): print(prefix, value) else: print(prefix, str(value))
def jacobi_max(A, eps=1e-5, max_iter=25, progress=0): ''' Finds eigenvalues of square matrix using Jacobi diagonalization. Maximum element is zeroed. Input params ----------------- A .......... square numpy array eps ........ absolute tolerance max_iter ... maximum number of iterations progess .... show animated iterations 0 .... show nothing >0 ... delay between iterations (seconds fractions) Output params ---------------------------- Vector (numpy array) of matrix A eigenvalues ''' A = A.astype(np.float64) m, n = np.shape(A) if (m == n) and (np.linalg.norm(A - A.T) < 1e-5): k = 0 err = 10000 while (err > eps) and (k < max_iter): i, j = np.unravel_index(np.argmax(np.abs(np.triu(A, 1))), A.shape) A = rotate(A, i, j) err = np.linalg.norm(np.tril(A, -1)) k += 1 if progress: clear_output(wait=True) with np.printoptions(precision=6, suppress=True): print(A) sleep(progress) if progress: print(f'\nPočet iterací: {k}') print('Vlastní čísla matice A:') print(f' {np.diagonal(A)}') return np.diagonal(A)
def check(test, ref, label=None, ac_kw=deepcopy(AC_KW), ignore_fails=False): """Check that `test` matches `ref` (closely enough). Parameters ---------- test ref ac_kw : mapping, optional Kwargs to `np.allclose`, as used by `pisa.utils.comparisons.recursiveEquality` ignore_fails : bool, optional If True and comparison fails, do not raise AssertionError Raises ------ AssertionError If `test` is not close enough to `ref` and ignore_fails is False """ same = True with np.printoptions(**PRINTOPTS): if isinstance(test, Mapping): if not label: label = "" else: label = label + ": " for key, val in test.items(): same &= compare_numeric( test=val, ref=ref[key], label=label + f"key: '{key}'", ac_kw=ac_kw, ignore_fails=ignore_fails, ) else: same &= compare_numeric(test=test, ref=ref, label=label, ac_kw=ac_kw, ignore_fails=ignore_fails) if not ignore_fails and not same: assert False return same
def _run_doctests(tests, full_name, verbose, doctest_warnings): """Run modified doctests for the set of `tests`. Returns: list of [(success_flag, output), ...] """ flags = NORMALIZE_WHITESPACE | ELLIPSIS | IGNORE_EXCEPTION_DETAIL runner = DTRunner(full_name, checker=Checker(), optionflags=flags, verbose=verbose) output = io.StringIO(newline='') success = True # Redirect stderr to the stdout or output tmp_stderr = sys.stdout if doctest_warnings else output from scipy._lib._util import _fixed_default_rng @contextmanager def temp_cwd(): cwd = os.getcwd() tmpdir = tempfile.mkdtemp() try: os.chdir(tmpdir) yield tmpdir finally: os.chdir(cwd) shutil.rmtree(tmpdir) # Run tests, trying to restore global state afterward cwd = os.getcwd() with np.errstate(), np.printoptions(), temp_cwd(), \ redirect_stderr(tmp_stderr), \ _fixed_default_rng(): # try to ensure random seed is NOT reproducible np.random.seed(None) for t in tests: t.filename = short_path(t.filename, cwd) fails, successes = runner.run(t, out=output.write) if fails > 0: success = False output.seek(0) return success, output.read()
def test_eval_control_radau_uncompressed_vectorized(self): grid_data = dm.transcriptions.grid_data.GridData( num_segments=2, transcription='radau-ps', transcription_order=[3, 5], compressed=False) time_options = dm.phase.options.TimeOptionsDictionary() time_options['units'] = 's' control_options = {'u1': dm.phase.options.ControlOptionsDictionary()} control_options['u1']['shape'] = (1, ) control_options['u1']['units'] = 'rad' p = om.Problem() interp_comp = p.model.add_subsystem( 'interp', VandermondeControlInterpComp(grid_data=grid_data, control_options=control_options, vec_size=5, standalone_mode=True, time_units='s')) p.setup(force_alloc_complex=True) interp_comp.options['segment_index'] = 1 p.set_val('interp.controls:u1', [0.0, 3.0, 1.5, 0.0, 4.0, 3.0, 4.0, 3.0]) p.set_val('interp.stau', [-1.0, -0.72048, -0.167181, 0.446314, 0.885792]) p.run_model() expected = np.array([[0.0, 4.0, 3.0, 4.0, 3.0]]).T assert_near_equal(p.get_val('interp.control_values:u1'), expected, tolerance=1.0E-6) with np.printoptions(linewidth=1024): cpd = p.check_partials(compact_print=False, method='cs') assert_check_partials(cpd, atol=_TOL, rtol=_TOL)
def print_data( verbose_data, header, data, input_size, expand, expand_thresh, ): """ Print `data` of dimensions `input_size` with `expand` and `expand_thresh`, prefixed by `header`. """ int8_format = '{0:4}' if np.any(data < 0) else '{0:3}' print(header, end='') if verbose_data: print(':') with np.printoptions(formatter={'int': int8_format.format}): if input_size[1] == input_size[2] == 1: for i in range(0, input_size[0], expand_thresh): last = min(i + expand_thresh, input_size[0]) if last - 1 > i: print(f'Channels #{i} to #{last-1}', end='') else: print(f'Channel #{i}', end='') if expand and expand > 1: print( f' (expansion: {(i // expand_thresh) + 1} of {expand})' ) else: print('') print(np.squeeze(data[i:last])) else: for i in range(input_size[0]): print(f'Channel #{i}', end='') if expand and expand > 1: print( f' (expansion: {(i // expand_thresh) + 1} of {expand})' ) else: print('') print(data[i]) print('')
def array(arr, title, verbose=1): assert arr.ndim <= 2 if arr.ndim == 1: arr = arr[None, :] if verbose > 1: with np.printoptions(precision=12, edgeitems=100, linewidth=200): line = '-' * (13 * arr.shape[1] + 1) s = '%s:\n' % title s += line + '\n' for i in range(arr.shape[0]): s += ' ' + ' '.join(['%12.6f' % x for x in arr[i, :]]) + ' \n' s += line + '\n' write(s)
def get_essential_matrix(fmatrix, k, verbose=False): """Compute the essential matrix :param fmatrix: the fundamental matrix corresponding to this essential matrix :param k: the camera intrinsics relevant to the two images (it is assumed that this will be the same) :param verbose: as in pipeline() """ # Essential matrix is calculated as E=K_1.T * F * K_2 # Since the same camera is used for the entire video sequence, K_1 = K_2 = k ematrix = np.matmul(np.matmul(np.transpose(k), fmatrix), k) if verbose: # print the essential matrix print("Essential matrix:") with np.printoptions(suppress=True): print(ematrix) # Return the essential matrix return ematrix
def main(): # 5x5 맵 생성 env = GridWorld(height=GRID_HEIGHT, width=GRID_WIDTH, start_state=None, terminal_states=[], transition_reward=0.0, outward_reward=-1.0, warm_hole_states=[(A_POSITION, A_PRIME_POSITION, 10.0), (B_POSITION, B_PRIME_POSITION, 5.0)]) state_values = calculate_grid_world_state_values(env) draw_grid_world_state_values_image(state_values, 'images/grid_world_state_values.png', GRID_HEIGHT, GRID_WIDTH) with np.printoptions(precision=2, suppress=True): print(state_values)
def test_network(FLAGS): x_test = Dataset.get_user("dataset/npy/", FLAGS.user) data = x_test.flatten("C") client = pyhe_client.HESealClient( FLAGS.hostname, FLAGS.port, FLAGS.batch_size, {FLAGS.tensor_name: (FLAGS.encrypt_data_str, data)}, ) results = np.round(client.get_results(), 2) y_pred_reshape = np.array(results).reshape(FLAGS.batch_size, 9) with np.printoptions(precision=3, suppress=True): print(y_pred_reshape) y_pred = y_pred_reshape.argmax(axis=1) print("y_pred", y_pred)
def remove_one_correlated(z, a, y): corr_vals = [] for i in range(z.shape[1]): col = z[:, i] corr = correlation(col, a) corr_vals.append(corr) # plot_gaussian(col, 'col_{:d}'.format(i)) print(corr_vals) with np.printoptions(precision=3, suppress=True): print(np.corrcoef(z.T)) for m in range(1, 11): print('Moment {:d}'.format(m)) print(moment(z, moment=m)) most_corr_i = np.argmax(np.abs(corr_vals)) less_corr_i = list(filter(lambda i: i != most_corr_i, range(z.shape[1]))) print(most_corr_i, less_corr_i) new_z = z[:, less_corr_i] print(z.shape, new_z.shape) return new_z
class PrintOptionsIV(EqualityCheckProblem): show_solution_on_correct = False _vars = ['a'] with np.printoptions(threshold=sys.maxsize): _expected = [np.arange(20000).reshape(100, 200).__str__()] _hints = [ 'There is some functionality to adapt print-outs for NumPy arrays: `np.set_printoptions`, `np.printoptions` and `np.get_printoptions`. Research them.', 'Consider `import sys; sys.maxsize`.' ] _solution = """ ```python with np.printoptions(threshold=sys.maxsize): print(a) ``` """ def check(self, *args): for arg, expected in zip(args, self._expected): assert_equal(arg.__str__(), expected, name="string representation")
def _log_affinity_matrix(name, affinity_matrix): if len(affinity_matrix) == 0: log.debug("Affinity matrix '{}' is empty".format(name)) rows = sorted(affinity_matrix.keys()) cols = sorted(affinity_matrix[rows[0]].keys()) if len(cols) == 0: log.debug("Affinity matrix '{}' has empty rows".format(name)) affinity_matrix = [[affinity_matrix[r][c] for c in cols] for r in rows] with np.printoptions(precision=2, suppress=True, threshold=sys.maxsize, linewidth=sys.maxsize): log.debug("Affinity matrix '{}' rows = \n{}".format( name, np.array(rows))) log.debug("Affinity matrix '{}' cols = \n{}".format( name, np.array(cols))) log.debug("Affinity matrix '{}' =\n{}".format( name, np.array(affinity_matrix)))
def main(): # 그리드 월드 환경 객체 생성 env = GridWorld( height=GRID_HEIGHT, width=GRID_WIDTH, start_state=None, # exploring start terminal_states=TERMINAL_STATES, transition_reward=-1.0, terminal_reward=-1.0, outward_reward=-1.0) MC = MonteCarloControl(env) MC.exploring_start_control() with np.printoptions(precision=2, suppress=True): for i in range(GRID_HEIGHT): for j in range(GRID_WIDTH): print(i, j, ": UP, DOWN, LEFT, RIGHT", MC.policy[(i, j)][1]) print()
def test_cl_func_vectorized(self): alpha_stall = 0.26179939 AR = 8 e = 0.68 a0 = 5.9 t_over_c = 0.12 aoa_blown = np.array([-0.07188392, 0.2270590416478, 0.2364583821384856, 0.2401759902150005, 0.2423804104464628, 0.243943990853836]) CL = cl_func_vectorized(aoa_blown, alpha_stall, AR, e, a0, t_over_c) expected = np.array([-0.3152743051485073, 0.9958538389888826, 1.0370761798060673, 1.0533747112777043, 1.063032129491213, 1.069874574395663]) with np.printoptions(precision=16): print(CL) assert_near_equal(CL, expected, tolerance=1.0E-8)
def eval_fn(self, data_loader): self.model.eval() fin_targets = [] fin_outputs = [] total_loss = 0 with torch.no_grad(): pbar = tqdm(enumerate(data_loader), total=len(data_loader)) for bi, d in pbar: ids = d["ids"] token_type_ids = d["token_type_ids"] mask = d["mask"] targets = d["targets"] ids = ids.to(self.device) token_type_ids = token_type_ids.to(self.device) mask = mask.to(self.device) targets = targets.to(self.device) outputs = self.model(ids=ids, mask=mask, token_type_ids=token_type_ids) loss = self.loss_fn(outputs, targets) L2_reg = torch.tensor(0., requires_grad=True) for name, param in self.model.named_parameters(): if 'weight' in name and 'attention' not in name: L2_reg = L2_reg + torch.norm(param, 2) loss += self.l2 * L2_reg / len(data_loader) total_loss += loss with np.printoptions(precision=3): v = round(loss.cpu().detach().numpy().item(), 3) pbar.set_description("Current eval Loss {}".format(v)) fin_targets.extend(targets.cpu().detach().numpy().tolist()) fin_outputs.extend( torch.sigmoid(outputs).cpu().detach().numpy().tolist()) return fin_outputs, total_loss, fin_targets
def test_mnist_cnn(FLAGS): (x_train, y_train, x_test, y_test) = load_mnist_data() batch_size = FLAGS.batch_size x_test_batch = x_test[:batch_size] y_test_batch = y_test[:FLAGS.batch_size] data = x_test_batch.swapaxes(1, 2).flatten('F') print('Client batch size from FLAG: ', batch_size) complex_packing = False if ('NGRAPH_COMPLEX_PACK' in os.environ): complex_packing = str2bool(os.environ['NGRAPH_COMPLEX_PACK']) hostname = 'localhost' port = 34000 print('complex_packing?', complex_packing) client = he_seal_client.HESealClient(hostname, port, batch_size, data, complex_packing) print('Sleeping until client is done') while not client.is_done(): time.sleep(1) results = client.get_results() results = np.round(results, 2) y_pred_reshape = np.array(results).reshape(10, batch_size) with np.printoptions(precision=3, suppress=True): print(y_pred_reshape.T) y_pred = y_pred_reshape.argmax(axis=0) print('y_pred', y_pred) y_true = y_test_batch.argmax(axis=1) correct = np.sum(np.equal(y_pred, y_true)) acc = correct / float(batch_size) print('pred size', len(y_pred)) print('correct', correct) print('Accuracy (batch size', batch_size, ') =', acc * 100., '%')
def calcObjFuncSens(xDV, funcs): """ Run the adjoint solver and get objective function sensitivities. """ Info("\n") Info( "+--------------------------------------------------------------------------+" ) Info( "| Evaluating Objective Function Sensitivities %03d |" % DASolver.nSolveAdjoints) Info( "+--------------------------------------------------------------------------+" ) a = time.time() # Setup an empty dictionary for the evaluated derivative values funcsSens = {} # Evaluate the geometric constraint derivatives DVCon.evalFunctionsSens(funcsSens) # Solve the adjoint DASolver.solveAdjoint() DASolver.calcTotalDeriv() # Evaluate the CFD derivatives DASolver.evalFunctionsSens(funcsSens, evalFuncs=evalFuncs) b = time.time() # Print the current solution to the screen with np.printoptions(precision=16, threshold=5, suppress=True): Info("Objective Function Sensitivity: ") Info(funcsSens) Info("Adjoint Runtime: %g s" % (b - a)) fail = funcsSens["fail"] return funcsSens, fail
def array_to_tex(a, fmt='{: 0.3f}', dim=True): """Display a LaTeX matrix in notebook Parameters ---------- a : np.ndarray matric to display fmt : str, optional format of matrix element, by default '{: 0.3f}' dim : bool, optional show matrix dim, by default True Raises ------ ValueError [description] """ from IPython.display import display, Math if isinstance(a, tuple): a = np.array(a) shape = a.shape if len(shape) > 2: raise ValueError('bmatrix can at most display two dimensions') with np.printoptions(formatter={'float': fmt.format}): lines = str(a).replace('[', '').replace(']', '').splitlines() rv = [r'\begin{bmatrix}'] rv += [' ' + ' & '.join(l.split()) + r'\\' for l in lines] rv += [r'\end{bmatrix}'] if dim: if len(shape) == 2: teX_math = "M_{" + str(shape[0]) + " \\times " + str( shape[1]) + "}=" elif len(shape) == 1: teX_math = "a_{" + str(shape[0]) + "}=" else: teX_math = "" teX_math += '\n'.join(rv) display(Math(r"{}".format(teX_math)))
def prn_nd(a, deci=2, width=100, title="Array", prefix=" .", prnt=True): """Format number arrays by row, and print Parameters: ----------- `a` : array An array of int or float dtypes, 1, 2, 3 and 4D arrays tested. `deci` - int Decimal places for floating point numbers `width` : int Default width for onscreen and printing, output beyond this length will be truncated with a warning. Reshape to overcome. `title` : string The default title, change to provide more information. Returns: -------- Prints the array with the 1st dimension flattened-like by row Notes: ----- - `w_frmt` : width formatter - `m_frmt` : max number formatter to get max. number of characters """ def _concat(rows, r_fmt, width, prefix): """print the subset to maximimum width""" end = ["", "...."][len(r_fmt.format(*rows[0])) > width] txt = prefix rw = [r_fmt.format(*v)[:width] + end for v in rows] txt += ("\n" + prefix).join(rw) # + "\n" return txt def d4_frmt(a_shp, a, txt, a_dim): """Dealing with 4, 5 ?D arrays""" d4, d, r, c = a_shp hdr = "\n" + "-"*25 fm = hdr + "\n-({}, + ({}, {}, {})" if a_dim == 5: fm = "\n--(.., {}, + ({}, {}, {})" t = "" for d3 in range(d4): t += fm.format(d3, d, r, c) + "\n" a_s = a[d3] rows = [a_s[..., i, :].flatten() for i in range(r)] t += _concat(rows, row_frmt, width, prefix) return t # # ---- begin constructing the array format ---- txt = "" a = np.asanyarray(a) # ---- run _check ---- if a.ndim < 3: if a.ndim == 2: a = a.reshape((1,) + a.shape) else: return "Array is not >= 2D" # a_shp, a_dim, a_kind, a_min, a_max = _check(a) # get base array info # fv = "" if np.ma.isMaskedArray(a): # ---- a = np.ma.round(a, decimals=deci) if a.dtype.kind in floats: default_fill = np.ma.default_fill_value(a) a.set_fill_value(default_fill) else: a.set_fill_value(np.iinfo(a.dtype).max) fv = ", masked array, fill value {}".format(a.get_fill_value()) #a = a.data # ---- correct dtype, get formats ---- if (a_kind in nums) and (a_dim >= 3): args = title, a_shp, a_dim, fv txt = "{}...\n-shape {}, ndim {}{}".format(*args) d, r, c = a_shp[-3:] row_frmt = _row_format(a, sep='', deci=deci) row_frmt = (row_frmt + " ") * d if a_dim == 3: rows = [a[..., i, :].flatten() for i in range(r)] txt += "\n" + _concat(rows, row_frmt, width, prefix) elif a_dim == 4: d4, d, r, c = a_shp t = d4_frmt(a_shp, a, txt, a_dim) txt += t elif a_dim == 5: d5, d4, d, r, c = a_shp hdr = "\n" + "-"*25 for i in range(d5): txt += hdr + '\n--({}, ..'.format(i) t = d4_frmt(a_shp[1:], a[i], txt, a_dim) txt += t else: txt = "Only integer and float arrays with ndim >= 2 supported" if prnt: with np.printoptions(precision=deci, linewidth=ln_wdth): print(txt) else: return txt
def test_ctx_mgr_as_smth(self): opts = {"precision": 2} with np.printoptions(**opts) as ctx: saved_opts = ctx.copy() assert_equal({k: saved_opts[k] for k in opts}, opts)
def test_ctx_mgr(self): # test that context manager actuall works with np.printoptions(precision=2): s = str(np.array([2.0]) / 3) assert_equal(s, '[0.67]')