content
stringlengths
35
762k
sha1
stringlengths
40
40
id
int64
0
3.66M
import codecs async def putStorBytes(app, key, data, filter_ops=None, bucket=None): """ Store byte string as S3 object with given key """ client = _getStorageClient(app) if not bucket: bucket = app['bucket_name'] if key[0] == '/': key = key[1:] # no leading slash shuffle = -1 # auto-shuffle clevel = 5 cname = None # compressor name if filter_ops: if "compressor" in filter_ops: cname = filter_ops["compressor"] if "use_shuffle" in filter_ops and not filter_ops['use_shuffle']: shuffle = 0 # client indicates to turn off shuffling if "level" in filter_ops: clevel = filter_ops["level"] msg = f"putStorBytes({bucket}/{key}), {len(data)} bytes shuffle: {shuffle}" msg += f" compressor: {cname} level: {clevel}" log.info(msg) if cname: try: blosc = codecs.Blosc(cname=cname, clevel=clevel, shuffle=shuffle) cdata = blosc.encode(data) # TBD: add cname in blosc constructor msg = f"compressed from {len(data)} bytes to {len(cdata)} bytes " msg += f"using filter: {blosc.cname} with level: {blosc.clevel}" log.info(msg) data = cdata except Exception as e: log.error(f"got exception using blosc encoding: {e}") raise HTTPInternalServerError() rsp = await client.put_object(key, data, bucket=bucket) return rsp
f58ff0c9073e2ce7dce19b2c586abc14af792590
22,600
import glob import os def fetch_block(folder, ind, full_output=False): """ A more generic function to fetch block number "ind" from a trajectory in a folder This function is useful both if you want to load both "old style" trajectories (block1.dat), and "new style" trajectories ("blocks_1-50.h5") It will be used in files "show" Parameters ---------- folder: str, folder with a trajectory ind: str or int, number of a block to fetch full_output: bool (default=False) If set to true, outputs a dict with positions, eP, eK, time etc. if False, outputs just the conformation (relevant only for new-style URIs, so default is False) Returns ------- data, Nx3 numpy array if full_output==True, then dict with data and metadata; XYZ is under key "pos" """ blocksh5 = glob.glob(os.path.join(folder, "blocks*.h5")) blocksdat = glob.glob(os.path.join(folder, "block*.dat")) ind = int(ind) if (len(blocksh5) > 0) and (len(blocksdat) > 0): raise ValueError("both .h5 and .dat files found in folder - exiting") if (len(blocksh5) == 0) and (len(blocksdat) == 0): raise ValueError("no blocks found") if len(blocksh5) > 0: fnames = [os.path.split(i)[-1] for i in blocksh5] inds = [i.split("_")[-1].split(".")[0].split("-") for i in fnames] exists = [(int(i[0]) <= ind) and (int(i[1]) >= ind) for i in inds] if True not in exists: raise ValueError(f"block {ind} not found in files") if exists.count(True) > 1: raise ValueError("Cannot find the file uniquely: names are wrong") pos = exists.index(True) block = load_URI(blocksh5[pos] + f"::{ind}") if not full_output: block = block["pos"] if len(blocksdat) > 0: block = load(os.path.join(folder, f"block{ind}.dat")) return block
0bbbfa1c2d86b6c71b8700c8dba43656173c797b
22,601
def unique_boxes(boxes, scale=1.0): """Return indices of unique boxes.""" assert boxes.shape[1] == 4, 'Func doesnot support tubes yet' v = np.array([1, 1e3, 1e6, 1e9]) hashes = np.round(boxes * scale).dot(v) _, index = np.unique(hashes, return_index=True) return np.sort(index)
951f0b6f0d51212ad63e787a32c78d14f7e11bd1
22,602
def dataloader(loader, mode): """Sets batchsize and repeat for the train, valid, and test iterators. Args: loader: tfds.load instance, a train, valid, or test iterator. mode: string, set to 'train' for use during training; set to anything else for use during validation/test Returns: An iterator for features and labels tensors. """ loader = loader.map(process_images) repeat = 1 if mode == 'train': repeat = None loader = loader.shuffle(1000 * FLAGS.batch_size) return loader.batch( FLAGS.batch_size).repeat(repeat).prefetch(tf.data.experimental.AUTOTUNE)
b15b736919c21df142e2d4815f33b24dc0f01e5f
22,603
def sub_inplace(X, varX, Y, varY): """In-place subtraction with error propagation""" # Z = X - Y # varZ = varX + varY X -= Y varX += varY return X, varX
646578886c37003eb860134b93db95e6b4d73ed7
22,604
def inv_logtransform(plog): """ Transform the power spectrum for the log field to the power spectrum of delta. Inputs ------ plog - power spectrum of log field computed at points on a Fourier grid Outputs ------- p - power spectrum of the delta field """ xi_log = np.fft.ifftn(plog) xi = np.exp(xi_log) - 1 p = np.fft.fftn(xi).real.astype('float') return p
aaf414796e5dfd5ede71dd8e18017f46b7761a39
22,605
import builtins def ipv6_b85decode(encoded, _base85_ords=RFC1924_ORDS): """Decodes an RFC1924 Base-85 encoded string to its 128-bit unsigned integral representation. Used to base85-decode IPv6 addresses or 128-bit chunks. Whitespace is ignored. Raises an ``OverflowError`` if stray characters are found. :param encoded: RFC1924 Base85-encoded string. :param _base85_ords: (Internal) Look up table. :returns: A 128-bit unsigned integer. """ if not builtins.is_bytes(encoded): raise TypeError("Encoded sequence must be bytes: got %r" % type(encoded).__name__) # Ignore whitespace. encoded = EMPTY_BYTE.join(encoded.split()) if len(encoded) != 20: raise ValueError("Not 20 encoded bytes: %r" % encoded) #uint128 = 0 #for char in encoded: # uint128 = uint128 * 85 + _base85_ords[byte_ord(char)] # Above loop unrolled to process 4 5-tuple chunks instead: try: #v, w, x, y, z = encoded[0:5] # v = encoded[0]..z = encoded[4] uint128 = ((((_base85_ords[encoded[0]] * 85 + _base85_ords[encoded[1]]) * 85 + _base85_ords[encoded[2]]) * 85 + _base85_ords[encoded[3]]) * 85 + _base85_ords[encoded[4]]) #v, w, x, y, z = encoded[5:10] # v = encoded[5]..z = encoded[9] uint128 = (((((uint128 * 85 + _base85_ords[encoded[5]]) * 85 + _base85_ords[encoded[6]]) * 85 + _base85_ords[encoded[7]]) * 85 + _base85_ords[encoded[8]]) * 85 + _base85_ords[encoded[9]]) #v, w, x, y, z = encoded[10:15] # v = encoded[10]..z = encoded[14] uint128 = (((((uint128 * 85 + _base85_ords[encoded[10]]) * 85 + _base85_ords[encoded[11]]) * 85 + _base85_ords[encoded[12]]) * 85 + _base85_ords[encoded[13]]) * 85 + _base85_ords[encoded[14]]) #v, w, x, y, z = encoded[15:20] # v = encoded[15]..z = encoded[19] uint128 = (((((uint128 * 85 + _base85_ords[encoded[15]]) * 85 + _base85_ords[encoded[16]]) * 85 + _base85_ords[encoded[17]]) * 85 + _base85_ords[encoded[18]]) * 85 + _base85_ords[encoded[19]]) except KeyError: raise OverflowError("Cannot decode `%r -- may contain stray " "ASCII bytes" % encoded) if uint128 > UINT128_MAX: raise OverflowError("Cannot decode `%r` -- may contain stray " "ASCII bytes" % encoded) return uint128 # I've left this approach in here to warn you to NOT use it. # This results in a massive amount of calls to byte_ord inside # tight loops.
324ec9835c7228bf406a8b33450c530b7191c4a0
22,606
def relabel_sig(sig:BaseSignature, arg_map:TDict[str, str]=None, new_vararg:str=None, kwarg_map:TDict[str, str]=None, new_varkwarg:str=None, output_map:TDict[str, str]=None) -> BaseSigMap: """ Given maps along which to rename signature elements, generate a new signature and an associated signature mapping from the original signature to the new signature. """ arg_map = {} if arg_map is None else arg_map kwarg_map = {} if kwarg_map is None else kwarg_map if output_map is not None and (not sig.has_fixed_outputs): raise ValueError() output_map = {} if output_map is None else output_map defaults_key_map = {**arg_map, **kwarg_map} ord_args = [(arg_map[name], tp) for name, tp in sig.ord_poskw] vararg = None if sig.vararg is None else (new_vararg, sig.vararg[1]) kwargs = {kwarg_map[name]: tp for name, tp in sig.kw.items()} varkwarg = None if sig.varkwarg is None else (new_varkwarg, sig.varkwarg[1]) if sig.has_fixed_outputs: ord_outputs = [(output_map[name], tp) for name, tp in sig.ord_outputs] fixed_outputs = True else: ord_outputs = None fixed_outputs = False defaults = {defaults_key_map[k]: v for k, v in sig.defaults.items()} renamed_sig = Signature( ord_poskw=ord_args, kw=kwargs, ord_outputs=ord_outputs, vararg=vararg, varkwarg=varkwarg, defaults=defaults, fixed_outputs=fixed_outputs ) sig_map = SigMap(source=sig, target=renamed_sig, kwarg_map=kwarg_map) return sig_map
a6b7ab1f8e8d3104938a6b1cecc609fb8a3aa1a0
22,607
import functools def inferred_batch_shape_tensor(batch_object, bijector_x_event_ndims=None, **parameter_kwargs): """Infers an object's batch shape from its parameters. Each parameter contributes a batch shape of `base_shape(parameter)[:-event_ndims(parameter)]`, where a parameter's `base_shape` is its batch shape if it defines one (e.g., if it is a Distribution, LinearOperator, etc.), and its Tensor shape otherwise, and `event_ndims` is as annotated by `batch_object.parameter_properties()[parameter_name].event_ndims`. Parameters with structured batch shape (in particular, non-autobatched JointDistributions) are not currently supported. Args: batch_object: Python object, typically a `tfd.Distribution` or `tfb.Bijector`. This must implement the method `batched_object.parameter_properties()` and expose a dict `batched_object.parameters` of the parameters passed to its constructor. bijector_x_event_ndims: If `batch_object` is a bijector, this is the (structure of) integer(s) value of `x_event_ndims` in the current context (for example, as passed to `experimental_batch_shape`). Otherwise, this argument should be `None`. Default value: `None`. **parameter_kwargs: Optional keyword arguments overriding parameter values in `batch_object.parameters`. Typically this is used to avoid multiple Tensor conversions of the same value. Returns: batch_shape_tensor: `Tensor` broadcast batch shape of all parameters. """ batch_shapes = map_fn_over_parameters_with_event_ndims( batch_object, get_batch_shape_tensor_part, bijector_x_event_ndims=bijector_x_event_ndims, require_static=False, **parameter_kwargs) return functools.reduce(ps.broadcast_shape, tf.nest.flatten(batch_shapes), [])
d3c3a40f36c66ef28f6aeaddbdaa7dff5729f1ed
22,608
from datetime import datetime def fsevent_log(self, event_id_status_message): """Amend filesystem event history with a logging message """ event_id = event_id_status_message[0] status = event_id_status_message[1] message = event_id_status_message[2] dbc = db_collection() history_entry = { 'state': fsevents.UPDATED, 'message': message, 'timestamp': datetime.datetime.now() } dbc.update_one({'id': event_id}, {'$push': { 'history': history_entry }}, upsert=True) return (event_id, status)
64cd88762e2775172bcf4c894c5187c373db3ee8
22,609
def fetch_synthetic_2d(lags=0): """ Build synthetic 2d data. Parameters ---------- lags : int, optional If greater than 0 it's added time dependence. The default is 0. Returns ------- data : numpy.ndarray, shape=(3000, 2) Synthetic data. """ seed = 98 np.random.seed(seed) mu_1, mu_2 = 1, 30 sigma_1, sigma_2 = 3, 1 num_samples = 3000 changes = {'incip': [ {'add': 50, 'where': (1000, 1300)}, {'add': 0, 'where': (1300, 1600)}, {'add': -50, 'where': (1600, 1650)} ], 'sudden': [ {'add': -50, 'where': (2000, 2200)} ] } labels = np.ones(num_samples, dtype=np.uint8) labels[1070:1250] = 2 labels[1250:1602] = 3 labels[1602:1640] = 2 labels[2000:2200] = 4 x_1, x_2 = build_2d_gauss_data(mu_1, mu_2, sigma_1, sigma_2, samples=num_samples, changes=changes, alpha=0.15, w=10, lags=lags) return np.c_[np.arange(1, 3001), x_1, x_2, labels]
006f1dc900d88c45d16eb518403bb576167bcae1
22,610
from sklearn.preprocessing import MultiLabelBinarizer from sklearn.model_selection import train_test_split def prepare_data() -> tuple: """Do the whole data preparation - incl. data conversion and test/train split. Returns: tuple: (X_train, y_train, X_test, y_test) """ (data, labels) = get_data() mlb = MultiLabelBinarizer() mlb.fit(labels) y = mlb.transform(labels) # read and decode images images = [tf.io.read_file(path).numpy() for path in data.image] print("images read") # create an empty array to allocate storage --> much faster than converting a list into a numpy-array using np.array(...) X = np.zeros(shape=(len(images), 60, 80, 3)) for i in range(len(images)): # pass encoded images into X X[i] = (decode_img(images[i], 60, 80)) print("images decoded") # split data into training and test set X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=42) print("Data prep finished") return (X_train, y_train, X_test, y_test)
a2a31f09858393d5fbcd5be80fd61b2e69df3cdf
22,611
def current_url_name(request): """ Adds the name for the matched url pattern for the current request to the request context. """ try: match = resolve(request.path) url_name = match.url_name except Http404: url_name = None return { 'current_url_name': url_name }
9ec6ea26a503e0969400a85a0df7491f6aefc064
22,612
import click def get_crypto_key(): """Shows your crypto key""" key = load_key() if key: click.secho("your actual crypto key:", fg = "blue") return click.echo(load_key())
7078f5fd0fe099ef49a3d702111aae9ee5e23387
22,613
def get_entropies(data: pd.DataFrame): """ Compute entropies for words in wide-format df """ counts = pd.DataFrame(apply_count_values( data.to_numpy(copy=True)), columns=data.columns) probs = (counts / counts.sum(axis=0)).replace(0, np.nan) nplogp = -probs * np.log2(probs) return nplogp.sum()
49a0a52194472a4285e60a05dab8a83dfaa7eec0
22,614
import os def create_dataset(dataset_path, do_train, image_size=224, interpolation='BILINEAR', crop_min=0.05, repeat_num=1, batch_size=32, num_workers=12, autoaugment=False, mixup=0.0, num_classes=1001): """create_dataset""" if hasattr(Inter, interpolation): interpolation = getattr(Inter, interpolation) else: interpolation = Inter.BILINEAR print('cannot find interpolation_type: {}, use {} instead'.format(interpolation, 'BILINEAR')) device_num = int(os.getenv("RANK_SIZE", '1')) rank_id = int(os.getenv('RANK_ID', '0')) if do_train: ds = de.ImageFolderDataset(dataset_path, num_parallel_workers=num_workers, shuffle=True, num_shards=device_num, shard_id=rank_id) else: batch_per_step = batch_size * device_num print("eval batch per step: {}".format(batch_per_step)) if batch_per_step < 50000: if 50000 % batch_per_step == 0: num_padded = 0 else: num_padded = batch_per_step - (50000 % batch_per_step) else: num_padded = batch_per_step - 50000 print("eval dataset num_padded: {}".format(num_padded)) if num_padded != 0: # padded_with_decode white_io = BytesIO() Image.new('RGB', (image_size, image_size), (255, 255, 255)).save(white_io, 'JPEG') padded_sample = { 'image': np.array(bytearray(white_io.getvalue()), dtype='uint8'), 'label': np.array(-1, np.int32) } sample = [padded_sample for x in range(num_padded)] ds_pad = de.PaddedDataset(sample) ds_imagefolder = de.ImageFolderDataset(dataset_path, num_parallel_workers=num_workers) ds = ds_pad + ds_imagefolder distribute_sampler = de.DistributedSampler(num_shards=device_num, shard_id=rank_id, \ shuffle=False, num_samples=None) ds.use_sampler(distribute_sampler) else: ds = de.ImageFolderDataset(dataset_path, num_parallel_workers=num_workers, \ shuffle=False, num_shards=device_num, shard_id=rank_id) print("eval dataset size: {}".format(ds.get_dataset_size())) mean = [0.485*255, 0.456*255, 0.406*255] std = [0.229*255, 0.224*255, 0.225*255] # define map operations if do_train: trans = [ C.RandomCropDecodeResize(image_size, scale=(crop_min, 1.0), \ ratio=(0.75, 1.333), interpolation=interpolation), C.RandomHorizontalFlip(prob=0.5), ] if autoaugment: trans += [ P.ToPIL(), ImageNetPolicy(), ToNumpy(), ] trans += [ C.Normalize(mean=mean, std=std), C.HWC2CHW(), ] else: resize = int(int(image_size / 0.875 / 16 + 0.5) * 16) print('eval, resize:{}'.format(resize)) trans = [ C.Decode(), C.Resize(resize, interpolation=interpolation), C.CenterCrop(image_size), C.Normalize(mean=mean, std=std), C.HWC2CHW() ] type_cast_op = C2.TypeCast(ms.int32) ds = ds.repeat(repeat_num) ds = ds.map(input_columns="image", num_parallel_workers=num_workers, operations=trans, python_multiprocessing=True) ds = ds.map(input_columns="label", num_parallel_workers=num_workers, operations=type_cast_op) if do_train and mixup > 0: one_hot_encode = C2.OneHot(num_classes) ds = ds.map(operations=one_hot_encode, input_columns=["label"]) ds = ds.batch(batch_size, drop_remainder=True) if do_train and mixup > 0: trans_mixup = C.MixUpBatch(alpha=mixup) ds = ds.map(input_columns=["image", "label"], num_parallel_workers=num_workers, operations=trans_mixup) return ds
130457530b30cb917091c3613a5d247140a8bfd9
22,615
def _numeric_adjust_widgets(caption: str, min: float, max: float) -> HBox: """Return a HBox with a label, a text box and a linked slider.""" label = Label(value=caption) text = BoundedFloatText(min=min, max=max, layout=wealth.plot.text_layout) slider = FloatSlider(readout=False, min=min, max=max) widgets.jslink((text, "value"), (slider, "value")) box = HBox([label, text, slider]) return box
b45aaa069f425a888bf54281c6324509eac783c6
22,616
import os def evaluate(benchmark, solution): """Evaluate the solution against the benchmark.""" # Get test data root = os.path.dirname(__file__) path = os.path.join(root, "../../task/regression/data/{}_test.csv".format(benchmark)) df_test = pd.read_csv(path, header=None) X = df_test.values[:, :-1].T # X values are the same for noisy/noiseless y_test = df_test.values[:, -1] var_y_test = np.var(y_test) # Get noiseless test data if "_n" in benchmark and "_d" in benchmark: noise_str = benchmark.split('_')[1] benchmark_noiseless = benchmark.replace(noise_str, "n0.00") path = os.path.join(root, "../../task/regression/data/{}_test.csv".format(benchmark_noiseless)) df_test_noiseless = pd.read_csv(path, header=None) y_test_noiseless = df_test_noiseless.values[:, -1] var_y_test_noiseless = np.var(y_test_noiseless) else: y_test_noiseless = y_test var_y_test_noiseless = var_y_test # Parse solution as sympy expression inputs = ["x{}".format(i+1) for i in range(X.shape[0])] solution = solution.replace("^", "**") f_hat = lambdify(inputs, solution, "numpy") # Compare against test data y_hat = f_hat(*X) nmse_test = np.mean((y_test - y_hat)**2) / var_y_test nmse_test_noiseless = np.mean((y_test_noiseless - y_hat)**2) / var_y_test_noiseless success = nmse_test_noiseless < THRESHOLD return nmse_test, nmse_test_noiseless, success
038f2e24c985619b319a24263d236ec77ded4b02
22,617
def where_handle(tokens): """Process where statements.""" internal_assert(len(tokens) == 2, "invalid where statement tokens", tokens) final_stmt, init_stmts = tokens return "".join(init_stmts) + final_stmt + "\n"
8377a1b62ffbca31a6b6fa42a125e3b16387a665
22,618
def nltk_ngram_pos_tagger(input_dict): """ A tagger that chooses a token's tag based on its word string and on the preceding n word's tags. In particular, a tuple (tags[i-n:i-1], words[i]) is looked up in a table, and the corresponding tag is returned. N-gram taggers are typically trained on a tagged corpus. Train a new NgramTagger using the given training data or the supplied model. In particular, construct a new tagger whose table maps from each context (tag[i-n:i-1], word[i]) to the most frequent tag for that context. But exclude any contexts that are already tagged perfectly by the backoff tagger. :param training_corpus: A tagged corpus included with NLTK, such as treebank, brown, cess_esp, floresta, or an Annotated Document Corpus in the standard TextFlows' adc format :param backoff_tagger: A backoff tagger, to be used by the new tagger if it encounters an unknown context. :param cutoff: If the most likely tag for a context occurs fewer than *cutoff* times, then exclude it from the context-to-tag table for the new tagger. :param n: N-gram is a contiguous sequence of n items from a given sequence of text or speech. :returns pos_tagger: A python dictionary containing the POS tagger object and its arguments. """ chunk = input_dict['training_corpus']['chunk'] corpus = input_dict['training_corpus']['corpus'] training_corpus=corpus_reader(corpus, chunk) backoff_tagger=input_dict['backoff_tagger']['object'] if input_dict['backoff_tagger'] else DefaultTagger('-None-') n=int(input_dict['n']) #default 2 cutoff=int(input_dict['cutoff']) #default 0 return {'pos_tagger': { 'function':'tag_sents', 'object': NgramTagger(n, train=training_corpus, model=None, backoff=backoff_tagger, cutoff=0) } }
ab6d1f48590042b1efe0a9c7e741e7a5ccbcdb34
22,619
import subprocess import sys def pkg(root, version, output, identifier=CONFIG['pkgid'], install_location='/', sign=CONFIG['sign_cert_cn'], ownership='recommended' ): """ Create a package. Most of the input parameters should be recognizable for most admins. `output` is the path so make sure and attach the pkg extension. Return: The exit code from pkgbuild. If non-zero an error has occurred """ cmd = ['/usr/bin/pkgbuild', '--root', root, '--install-location', install_location, '--identifier', identifier, '--version', version, '--ownership', ownership] # When sign_cert_cn are passed we should sign the package if sign: cmd.append('--sign') cmd.append(sign) # Always append the output path so signing will work cmd.append(output) print(cmd) proc = subprocess.Popen(cmd, shell=False, bufsize=-1, stdin=subprocess.PIPE, stdout=sys.stdout, stderr=subprocess.PIPE) (output, dummy_error) = proc.communicate() return proc.returncode
dc51b990d2e585f8c239ec459bcb9024764f5c27
22,620
def remove_app(INSTALLED_APPS, app): """ remove app from installed_apps """ if app in INSTALLED_APPS: apps = list(INSTALLED_APPS) apps.remove(app) return tuple(apps) return INSTALLED_APPS
7386b6f38b73abf25e94d9c8368aaac6255d2cee
22,621
from typing import OrderedDict def get_feature(cluster, sample, i): """Turn a cluster into a biopython SeqFeature.""" qualifiers = OrderedDict(ID="%s_%d" % (sample, i)) for attr in cluster.exportable: qualifiers[attr] = getattr(cluster, attr.lower()) feature = SeqFeature(FeatureLocation(cluster.start, cluster.end), type=cluster.type, strand=1, qualifiers=qualifiers) if cluster.feature_args: seqfeature_args = cluster.feature_args base_args = {'location': FeatureLocation(cluster.start, cluster.end), 'strand': 1} subfeatures = [] for feature_args in seqfeature_args: feature_args = feature_args.to_feature_args() args = base_args.copy() args.update(feature_args) subfeatures.append(SeqFeature(**args)) feature.sub_features = subfeatures return cluster.tid, feature
4ec3cb0757db9b884d3bce3d4406f1824a6c62dd
22,622
def loss(Y, spectra, beta, Yval, val_spec): """ Description ----------- Calculate the loss for a specfic set of beta values Parameters ---------- Y: labels (0 or 1) spectra: flux values beta: beta values Yval: validation set labels (0 or 1) val_spec: validation flux values Returns ------- J_sum: total loss calculated from all spectra beta_gradients: gradients for each beat value J_sum_val: validation loss calaculated from all validation spectra """ J_total = [] i = 0 while i < len(Y): #do logistic regression sum_p = param_sum(spectra[i],beta) lr = log_reg(sum_p) #deal with log(0) cases if (lr == 1 and Y[i] == 0) or (lr == 0 and Y[i] == 1): J_iter = 1e+30 else: J_iter = (-Y[i]*np.log(lr)) - ((1-Y[i])*np.log(1-lr)) J_total.append(J_iter) i += 1 J_sum = (1/len(Y))*np.sum(J_total) J_total_val = [] #validation i = 0 while i < len(Yval): sum_p_val = param_sum(val_spec[i],beta) lr_val = log_reg(sum_p_val) if (lr_val == 1 and Yval[i] == 0) or (lr_val == 0 and Yval[i] == 1): J_iter_val = 1e+30 else: J_iter_val = (-Yval[i]*np.log(lr_val)) - ((1-Yval[i])*np.log(1-lr_val)) J_total_val.append(J_iter_val) i += 1 J_sum_val = (1/len(Yval))*np.sum(J_total_val) #shuffle the data for SGD Y, spectra = unison_shuffled_copies(Y, spectra) #select subset of data batch = 100 Y_batch = Y[0:batch] spectra_batch = spectra[0:batch] beta_gradients = np.zeros(len(beta)) i = 0 #calculate gradients while i < len(Y_batch): sum_p = param_sum(spectra_batch[i],beta) for j in range(len(beta)): if j == 0: beta_gradients[j] += gradient1(Y_batch[i], sum_p) else: beta_gradients[j] += gradient2(Y_batch[i], spectra_batch[i][j-1], sum_p) i += 1 return J_sum, beta_gradients, J_sum_val
5be2675d14062834bc0f8c9cd83551a60b528668
22,623
def video_detail_except(): """取得channelPlayListItem/videoDetail兩表video_id的差集 Returns: [list]: [目前尚未儲存詳細資料的影片ID] """ playlist_id = get_db_ChannelPlayListItem_video_id() video_detail_id = get_db_VideoDetail_video_id() if video_detail_id and playlist_id: filter_video = list(set(playlist_id).difference(set(video_detail_id))) return filter_video return False
efe9f151c6689cf6a71584cf988d0af8bb1c0c2a
22,624
def nodes(xmrs): """Return the list of Nodes for *xmrs*.""" nodes = [] _props = xmrs.properties varsplit = sort_vid_split for p in xmrs.eps(): sortinfo = None iv = p.intrinsic_variable if iv is not None: sort, _ = varsplit(iv) sortinfo = _props(iv) sortinfo[CVARSORT] = sort nodes.append( Node(p.nodeid, p.pred, sortinfo, p.lnk, p.surface, p.base, p.carg) ) return nodes
0e081648e6b30ec6cc230218c346384624d2ade6
22,625
import matplotlib.pyplot as plt import matplotlib as mpl def get_figure(a=None, e=None, scale=10): """ Creates the desired figure setup: 1) Maximize Figure 2) Create 3D Plotting Environment 3) Rotate Z axis label so it is upright 4) Let labels 5) Increase label font size 6) Rotate camera to ideal (predetermined) angle """ mpl.rcParams["savefig.directory"] = "." fig = plt.figure() fig.set_size_inches(1920/1080*scale, scale) ax = fig.add_subplot(111, projection='3d') ax.zaxis.set_rotate_label(False) # disable automatic rotation, otherwise you can't manually override ax.xaxis._axinfo['label']['space_factor'] = 6.8 ax.yaxis._axinfo['label']['space_factor'] = 4.8 ax.zaxis._axinfo['label']['space_factor'] = 4.8 ax.view_init(azim=a, elev=e) return fig, ax
595f91b9e62fd85db68ec0d06d117d36d76e1ab2
22,626
import torch def get_data_loader(transformed_data, is_training_data=True): """ Creates and returns a data loader from transformed_data """ return torch.utils.data.DataLoader(transformed_data, batch_size=50, shuffle=True) if is_training_data else torch.utils.data.DataLoader(transformed_data, batch_size=50)
7f7dfdc83abc0ab261fde7be2216b4e130761f7e
22,627
import six def resolve_authconfig(authconfig, registry=None): """ Returns the authentication data from the given auth configuration for a specific registry. As with the Docker client, legacy entries in the config with full URLs are stripped down to hostnames before checking for a match. Returns None if no match was found. """ if 'credsStore' in authconfig: log.debug( 'Using credentials store "{0}"'.format(authconfig['credsStore']) ) return _resolve_authconfig_credstore( authconfig, registry, authconfig['credsStore'] ) # Default to the public index server registry = resolve_index_name(registry) if registry else INDEX_NAME log.debug("Looking for auth entry for {0}".format(repr(registry))) if registry in authconfig: log.debug("Found {0}".format(repr(registry))) return authconfig[registry] for key, config in six.iteritems(authconfig): if resolve_index_name(key) == registry: log.debug("Found {0}".format(repr(key))) return config log.debug("No entry found") return None
a21ee92f3477c4270f708c5577b3d1adcf1b03a9
22,628
def structured_rand_arr(size, sample_func=np.random.random, ltfac=None, utfac=None, fill_diag=None): """Make a structured random 2-d array of shape (size,size). If no optional arguments are given, a symmetric array is returned. Parameters ---------- size : int Determines the shape of the output array: (size,size). sample_func : function, optional. Must be a function which when called with a 2-tuple of ints, returns a 2-d array of that shape. By default, np.random.random is used, but any other sampling function can be used as long as it matches this API. utfac : float, optional Multiplicative factor for the upper triangular part of the matrix. ltfac : float, optional Multiplicative factor for the lower triangular part of the matrix. fill_diag : float, optional If given, use this value to fill in the diagonal. Otherwise the diagonal will contain random elements. Examples -------- >>> np.random.seed(0) # for doctesting >>> np.set_printoptions(precision=4) # for doctesting >>> structured_rand_arr(4) array([[ 0.5488, 0.7152, 0.6028, 0.5449], [ 0.7152, 0.6459, 0.4376, 0.8918], [ 0.6028, 0.4376, 0.7917, 0.5289], [ 0.5449, 0.8918, 0.5289, 0.0871]]) >>> structured_rand_arr(4,ltfac=-10,utfac=10,fill_diag=0.5) array([[ 0.5 , 8.3262, 7.7816, 8.7001], [-8.3262, 0.5 , 4.6148, 7.8053], [-7.7816, -4.6148, 0.5 , 9.4467], [-8.7001, -7.8053, -9.4467, 0.5 ]]) """ # Make a random array from the given sampling function rmat = sample_func((size,size)) # And the empty one we'll then fill in to return out = np.empty_like(rmat) # Extract indices for upper-triangle, lower-triangle and diagonal uidx = triu_indices(size,1) lidx = tril_indices(size,-1) didx = diag_indices(size) # Extract each part from the original and copy it to the output, possibly # applying multiplicative factors. We check the factors instead of # defaulting to 1.0 to avoid unnecessary floating point multiplications # which could be noticeable for very large sizes. if utfac: out[uidx] = utfac * rmat[uidx] else: out[uidx] = rmat[uidx] if ltfac: out[lidx] = ltfac * rmat.T[lidx] else: out[lidx] = rmat.T[lidx] # If fill_diag was provided, use it; otherwise take the values in the # diagonal from the original random array. if fill_diag is not None: out[didx] = fill_diag else: out[didx] = rmat[didx] return out
4463f62bef1feff23019cc35439545b52461ee40
22,629
def append_empty_args(func): """To use to transform an ingress function that only returns kwargs to one that returns the normal form of ingress functions: ((), kwargs)""" @wraps(func) def _func(*args, **kwargs): return (), func(*args, **kwargs) return _func
ec2bf4c30eddb418ade57e50ce8a4a233a8f0f9d
22,630
def gateway(job, app, tool, user, user_email): """ Function to specify the destination for a job. At present this is exactly the same as using dynamic_dtd with tool_destinations.yml but can be extended to more complex mapping such as limiting resources based on user group or selecting destinations based on queue size. Arguments to this function can include app, job, job_id, job_wrapper, tool, tool_id, user, user_email (see https://docs.galaxyproject.org/en/latest/admin/jobs.html) """ if user_email in user_destinations.keys(): if hasattr(tool, 'id') and isinstance(tool.id, str) and tool.id.startswith('toolshed'): # map shed tools only return user_destinations[user_email] if user: user_roles = [role.name for role in user.all_roles() if not role.deleted] # If any of these are prefixed with 'training-' if any([role.startswith('training-') for role in user_roles]): # Then they are a training user, we will send their jobs to pulsar, # Or give them extra resources if hasattr(tool, 'id') and isinstance(tool.id, str) and tool.id.startswith('toolshed') and tool.id.split('/')[-2] in pulsar_list: return app.job_config.get_destination('pulsar_destination') else: return app.job_config.get_destination('slurm_dest') destination = map_tool_to_destination(job, app, tool, user_email, path=TOOL_DESTINATION_PATH) return destination
b51cd288b638469d054191be0c1423d0c637ce9a
22,631
import argparse def process_command_line(): """ Return a 1-tuple: (args list). `argv` is a list of arguments, or `None` for ``sys.argv[1:]``. """ parser = argparse.ArgumentParser(description='usage') # add description # positional arguments parser.add_argument('d1s', metavar='domain1-source', type=str, help='domain 1 source') parser.add_argument('d1t', metavar='domain1-target', type=str, help='domain 1 target') parser.add_argument('d2s', metavar='domain2-source', type=str, help='domain 2 source') parser.add_argument('d2t', metavar='domain2-target', type=str, help='domain 2 target') parser.add_argument('v', metavar='vocab', type=str, help='shared bpe vocab') # optional arguments parser.add_argument('-x', '--averaging-over', dest='x', type=int, default=10, help='How many PAD approximations to compute and average over') parser.add_argument('-b', '--batch-size', dest='b', type=int, default=320, help='batch_size') args = parser.parse_args() return args
7ab4e61850bf1f4fd84198ffd93d577864a9b873
22,632
def pprint(matrix: list) -> str: """ Preety print matrix string Parameters ---------- matrix : list Square matrix. Returns ------- str Preety string form of matrix. """ matrix_string = str(matrix) matrix_string = matrix_string.replace('],', '],\n') return matrix_string
5c0ffa2b0a9c237b65b5ad7c4e17c2456195c088
22,633
def sigmoid(x : np.ndarray, a : float, b : float, c : float) -> np.ndarray : """ A parameterized sigmoid curve Args: x (np.ndarray or float): x values to evaluate the sigmoid a (float): vertical stretch parameter b (float): horizontal shift parameter c (float): horizontal stretch parameter Returns: evalutated sigmoid curve at x values for the given parameterization """ return a / (b + np.exp(-1.0 * c * x))
dacb80ca958bf9f0a007fe6d50970f444fd9b4e7
22,634
def merge_testcase_data(leaf, statsname, x_axis): """ statsname might be a function. It will be given the folder path of the test case and should return one line. """ res = get_leaf_tests_stats(leaf, statsname) return merge_testcase_data_set_x(res, x_axis)
5b8829377d9249630a9261e7ee27533cce72542c
22,635
import os def get_account_ids(status=None, table_name=None): """return an array of account_ids from the Accounts table. Optionally, filter by status""" dynamodb = boto3.resource('dynamodb') if table_name: account_table = dynamodb.Table(table_name) else: account_table = dynamodb.Table(os.environ['ACCOUNT_TABLE']) account_list = [] response = account_table.scan( AttributesToGet=['account_id', 'account_status'] ) while 'LastEvaluatedKey' in response: # Means that dynamoDB didn't return the full set, so ask for more. account_list = account_list + response['Items'] response = account_table.scan( AttributesToGet=['account_id', 'account_status'], ExclusiveStartKey=response['LastEvaluatedKey'] ) account_list = account_list + response['Items'] output = [] for a in account_list: if status is None: # Then we get everything output.append(a['account_id']) elif a['account_status'] == status: # this is what we asked for output.append(a['account_id']) # Otherwise, don't bother. return(output)
ea893a9001856ce6424eb3c6c340fd1a2c7ab99c
22,636
def dashed_word(answer): """ :param answer: str, from random_word :return: str, the number of '-' as per the length of answer """ ans = "" for i in answer: ans += '-' return ans
358be047bfad956afef27c0665b02a2a233fefbf
22,637
def merge_overpass_jsons(jsons): """Merge a list of overpass JSONs into a single JSON. Parameters ---------- jsons : :obj:`list` List of dictionaries representing Overpass JSONs. Returns ------- :obj:`dict` Dictionary containing all elements from input JSONS. """ elements = [] for osm_json in jsons: elements.extend(osm_json['elements']) return {'elements': elements}
c68fde0ddbdf22a34377e1e865be36aaabaa47be
22,638
from admin.get_session_info import run as _get_session_info from admin.login import run as _login from admin.logout import run as _logout from admin.recover_otp import run as _recover_otp from admin.register import run as _register from admin.request_login import run as _request_login from admin.handler import MissingFunctionError def identity_functions(function, args): """This function routes calls to sub-functions, thereby allowing a single identity function to stay hot for longer Args: function (str): for selection of function to call args: arguments to be passed to the selected function Returns: function: If valid function selected, function with args passed else None """ if function == "get_session_info": return _get_session_info(args) elif function == "login": return _login(args) elif function == "logout": return _logout(args) elif function == "recover_otp": return _recover_otp(args) elif function == "register": return _register(args) elif function == "request_login": return _request_login(args) else: raise MissingFunctionError()
1885705099ce67e806a6cc9a461f92c659bbf0bb
22,639
def load_template(tmpl): """ Loads the default template file. """ with open(tmpl, "r") as stream: return Template(stream.read())
a37a74cf37a05142bcddac870a81ee44531004f3
22,640
import hashlib def calculate_id_name(message): """ Calculates hash value based on message. Useful for unique id - six hex characters. 6 digits - 16777216 permutations. TODO: check for hash collisions. """ hash_object = hashlib.md5(b'%s'% message) return hash_object.hexdigest()[0:5]
37e6e53a75bd2397a37125c4205b6a1cf24057ee
22,641
def get_total_cases(): """全国の現在の感染者数""" return col_ref.document(n).get().to_dict()["total"]["total_cases"]
1e933fd86cde49edbb5d78591fb513cb9633c080
22,642
from typing import Optional def _find_db_team(team_id: OrgaTeamID) -> Optional[DbOrgaTeam]: """Return the team with that id, or `None` if not found.""" return db.session.query(DbOrgaTeam).get(team_id)
9c0709c8b601a1910ed1e6b08f16970c976b7310
22,643
from typing import Optional from contextlib import suppress def make_number( num: Optional[str], repr: str = None, speak: str = None, literal: bool = False, special: dict = None, ) -> Optional[Number]: """Returns a Number or Fraction dataclass for a number string If literal, spoken string will not convert to hundreds/thousands NOTE: Numerators are assumed to have a single digit. Additional are whole numbers """ # pylint: disable=too-many-branches if not num or is_unknown(num): return None # Check special with suppress(KeyError): item = (special or {}).get(num) or SPECIAL_NUMBERS[num] if isinstance(item, tuple): value, spoken = item else: value = item spoken = spoken_number(str(value), literal=literal) return Number(repr or num, value, spoken) # Check cardinal direction if num in CARDINALS: if not repr: repr = num num = str(CARDINALS[num]) # Remove spurious characters from the end num = num.rstrip("M.") num = num.replace("O", "0") num = num.replace("+", "") # Create Fraction if "/" in num: return make_fraction(num, repr, literal) # Handle Minus values with errors like 0M04 if "M" in num: val_str = num.replace("MM", "-").replace("M", "-") while val_str[0] != "-": val_str = val_str[1:] else: val_str = num # Check value prefixes speak_prefix = "" if val_str.startswith("ABV "): speak_prefix += "above " val_str = val_str[4:] if val_str.startswith("BLW "): speak_prefix += "below " val_str = val_str[4:] if val_str.startswith("FL"): speak_prefix += "flight level " val_str, literal = val_str[2:], True # Create Number if not val_str: return None if "." in num: value = float(val_str) # Overwrite float 0 due to "0.0" literal if not value: value = 0 else: value = int(val_str) spoken = speak_prefix + spoken_number(speak or str(value), literal) return Number(repr or num, value, spoken)
0fdbd9610355cfceb2ee5ab0fd04b694ca8da9af
22,644
import os def _readmapfile(fp, mapfile): """Load template elements from the given map file""" base = os.path.dirname(mapfile) conf = config.config() def include(rel, remap, sections): subresource = None if base: abs = os.path.normpath(os.path.join(base, rel)) if os.path.isfile(abs): subresource = util.posixfile(abs, b'rb') if not subresource: if pycompat.ossep not in rel: abs = rel subresource = resourceutil.open_resource( b'mercurial.templates', rel ) else: dir = templatedir() if dir: abs = os.path.normpath(os.path.join(dir, rel)) if os.path.isfile(abs): subresource = util.posixfile(abs, b'rb') if subresource: data = subresource.read() conf.parse( abs, data, sections=sections, remap=remap, include=include, ) data = fp.read() conf.parse(mapfile, data, remap={b'': b'templates'}, include=include) cache = {} tmap = {} aliases = [] val = conf.get(b'templates', b'__base__') if val and val[0] not in b"'\"": # treat as a pointer to a base class for this style path = os.path.normpath(os.path.join(base, val)) # fallback check in template paths if not os.path.exists(path): dir = templatedir() if dir is not None: p2 = os.path.normpath(os.path.join(dir, val)) if os.path.isfile(p2): path = p2 else: p3 = os.path.normpath(os.path.join(p2, b"map")) if os.path.isfile(p3): path = p3 fp = _open_mapfile(path) cache, tmap, aliases = _readmapfile(fp, path) for key, val in conf[b'templates'].items(): if not val: raise error.ParseError( _(b'missing value'), conf.source(b'templates', key) ) if val[0] in b"'\"": if val[0] != val[-1]: raise error.ParseError( _(b'unmatched quotes'), conf.source(b'templates', key) ) cache[key] = unquotestring(val) elif key != b'__base__': tmap[key] = os.path.join(base, val) aliases.extend(conf[b'templatealias'].items()) return cache, tmap, aliases
8faf0782230cce63b99295d6142c3ee713539d5e
22,645
import plotly.graph_objs as go def gif_jtfs_3d(Scx, jtfs=None, preset='spinned', savedir='', base_name='jtfs3d', images_ext='.png', cmap='turbo', cmap_norm=.5, axes_labels=('xi2', 'xi1_fr', 'xi1'), overwrite=False, save_images=False, width=800, height=800, surface_count=30, opacity=.2, zoom=1, angles=None, verbose=True, gif_kw=None): """Generate and save GIF of 3D JTFS slices. Parameters ---------- Scx : dict / tensor, 4D Output of `jtfs(x)` with `out_type='dict:array'` or `'dict:list'`, or output of `wavespin.toolkit.pack_coeffs_jtfs`. jtfs : TimeFrequencyScattering1D Required if `preset` is not `None`. preset : str['spinned', 'all'] / None If `Scx = jtfs(x)`, then - 'spinned': show only `psi_t * psi_f_up` and `psi_t * psi_f_dn` pairs - 'all': show all pairs `None` is for when `Scx` is already packed via `pack_coeffs_jtfs`. savedir, base_name, images_ext, overwrite : See `help(wavespin.visuals.gif_jtfs)`. cmap : str Colormap to use. cmap_norm : float Colormap norm to use, as fraction of maximum value of `packed` (i.e. `norm=(0, cmap_norm * packed.max())`). axes_labels : tuple[str] Names of last three dimensions of `packed`. E.g. `structure==2` (in `pack_coeffs_jtfs`) will output `(n2, n1_fr, n1, t)`, so `('xi2', 'xi1_fr', 'xi1')` (default). width : int 2D width of each image (GIF frame). height : int 2D height of each image (GIF frame). surface_count : int Greater improves 3D detail of each frame, but takes longer to render. opacity : float Lesser makes 3D surfaces more transparent, exposing more detail. zoom : float (default=1) / None Zoom factor on each 3D frame. If None, won't modify `angles`. If not None, will first divide by L2 norm of `angles`, then by `zoom`. angles : None / np.ndarray / list/tuple[np.ndarray] / str['rotate'] Controls display angle of the GIF. - None: default angle that faces the line extending from min to max of `xi1`, `xi2`, and `xi1_fr` (assuming default `axes_labels`). - Single 1D array: will reuse for each frame. - 'rotate': will use a preset that rotates the display about the default angle. Resulting array is passed to `go.Figure.update_layout()` as `'layout_kw': {'scene_camera': 'center': dict(x=e[0], y=e[1], z=e[2])}`, where `e = angles[0]` up to `e = angles[len(packed) - 1]`. verbose : bool (default True) Whether to print GIF generation progress. gif_kw : dict / None Passed as kwargs to `wavespin.visuals.make_gif`. Example ------- Also see `examples/visuals_tour.py`. :: N, J, Q = 2049, 7, 16 x = toolkit.echirp(N) jtfs = TimeFrequencyScattering1D(J, N, Q, J_fr=4, Q_fr=2, out_type='dict:list') Scx = jtfs(x) gif_jtfs_3d(Scx, jtfs, savedir='', preset='spinned') """ try: except ImportError as e: print("\n`plotly.graph_objs` is needed for `gif_jtfs_3d`.") raise e # handle args & check if already exists (if so, delete if `overwrite`) savedir, savepath_gif, images_ext, save_images, *_ = _handle_gif_args( savedir, base_name, images_ext, save_images, overwrite, show=False) if preset not in ('spinned', 'all', None): raise ValueError("`preset` must be 'spinned', 'all', or None (got %s)" % ( preset)) # handle input tensor if not isinstance(Scx, (dict, np.ndarray)): raise ValueError("`Scx` must be dict or numpy array (need `out_type` " "'dict:array' or 'dict:list'). Got %s" % type(Scx)) elif isinstance(Scx, dict): ckw = dict(Scx=Scx, meta=jtfs.meta(), reverse_n1=False, out_3D=jtfs.out_3D, sampling_psi_fr=jtfs.sampling_psi_fr) if preset == 'spinned': _packed = pack_coeffs_jtfs(structure=2, separate_lowpass=True, **ckw) _packed = _packed[0] # spinned only elif preset == 'all': _packed = pack_coeffs_jtfs(structure=2, separate_lowpass=False, **ckw) else: raise ValueError("dict `Scx` requires string `preset` (got %s)" % ( preset)) packed = _packed.transpose(-1, 0, 1, 2) # time first elif isinstance(Scx, np.ndarray): packed = Scx # handle labels supported = ('t', 'xi2', 'xi1_fr', 'xi1') for label in axes_labels: if label not in supported: raise ValueError(("unsupported `axes_labels` element: {} -- must " "be one of: {}").format( label, ', '.join(supported))) frame_label = [label for label in supported if label not in axes_labels][0] # 3D meshgrid def slc(i, g): label = axes_labels[i] start = {'xi1': .5, 'xi2': .5, 't': 0, 'xi1_fr': .5}[label] end = {'xi1': 0., 'xi2': 0., 't': 1, 'xi1_fr': -.5}[label] return slice(start, end, g*1j) a, b, c = packed.shape[1:] X, Y, Z = np.mgrid[slc(0, a), slc(1, b), slc(2, c)] # handle `angles`; camera focus if angles is None: eye = np.array([2.5, .3, 2]) eye /= np.linalg.norm(eye) eyes = [eye] * len(packed) elif (isinstance(angles, (list, tuple)) or (isinstance(angles, np.ndarray) and angles.ndim == 2)): eyes = angles elif isinstance(angles, str): assert angles == 'rotate', angles n_pts = len(packed) def gauss(n_pts, mn, mx, width=20): t = np.linspace(0, 1, n_pts) g = np.exp(-(t - .5)**2 * width) g *= (mx - mn) g += mn return g x = np.logspace(np.log10(2.5), np.log10(8.5), n_pts, endpoint=1) y = np.logspace(np.log10(0.3), np.log10(6.3), n_pts, endpoint=1) z = np.logspace(np.log10(2.0), np.log10(2.0), n_pts, endpoint=1) x, y, z = [gauss(n_pts, mn, mx) for (mn, mx) in [(2.5, 8.5), (0.3, 6.3), (2, 2)]] eyes = np.vstack([x, y, z]).T else: eyes = [angles] * len(packed) assert len(eyes) == len(packed), (len(eyes), len(packed)) # camera zoom if zoom is not None: for i in range(len(eyes)): eyes[i] /= (np.linalg.norm(eyes[i]) * .5 * zoom) # colormap norm mx = cmap_norm * packed.max() # gif configs volume_kw = dict( x=X.flatten(), y=Y.flatten(), z=Z.flatten(), opacity=opacity, surface_count=surface_count, colorscale=cmap, showscale=False, cmin=0, cmax=mx, ) layout_kw = dict( margin_pad=0, margin_l=0, margin_r=0, margin_t=0, title_pad_t=0, title_pad_b=0, margin_autoexpand=False, scene_aspectmode='cube', width=width, height=height, scene=dict( xaxis_title=axes_labels[0], yaxis_title=axes_labels[1], zaxis_title=axes_labels[2], ), scene_camera=dict( up=dict(x=0, y=1, z=0), center=dict(x=0, y=0, z=0), ), ) # generate gif frames #################################################### img_paths = [] for k, vol4 in enumerate(packed): fig = go.Figure(go.Volume(value=vol4.flatten(), **volume_kw)) eye = dict(x=eyes[k][0], y=eyes[k][1], z=eyes[k][2]) layout_kw['scene_camera']['eye'] = eye fig.update_layout( **layout_kw, title={'text': f"{frame_label}={k}", 'x': .5, 'y': .09, 'xanchor': 'center', 'yanchor': 'top'} ) savepath = os.path.join(savedir, f'{base_name}{k}{images_ext}') if os.path.isfile(savepath) and overwrite: os.unlink(savepath) fig.write_image(savepath) img_paths.append(savepath) if verbose: print("{}/{} frames done".format(k + 1, len(packed)), flush=True) # make gif ############################################################### try: if gif_kw is None: gif_kw = {} make_gif(loaddir=savedir, savepath=savepath_gif, ext=images_ext, delimiter=base_name, overwrite=overwrite, verbose=verbose, **gif_kw) finally: if not save_images: # guarantee cleanup for path in img_paths: if os.path.isfile(path): os.unlink(path)
0d34878378e463a55655be29bd6b3d665adc05c6
22,646
def get_arguments(): """ Parse command line arguments :return Namespace: parsed arguments """ parser = ArgumentParser(description='') subparsers = parser.add_subparsers(help='sub-command help', dest='subparser_name') parser.add_argument( '-v', '--verbose', type=int, default=1, help='Verbosity level: [under construction]' ) parser.add_argument( '-d', '--debug', action='store_true', required=False, help='Debug mode [under construction]' ) # adding tasks parser_add = subparsers.add_parser('add', help='Create a new task') parser_add.add_argument( 'name', type=str, help="Name of the task to add" ) parser_add.add_argument( '-p', '--priority', type=int, default=0, help="Priority of the task" ) parser_add.add_argument( '-t', '--time', type=validate_time, help="Start time of the task. Format: -t HH:MM" ) parser_add.add_argument( '-w', '--weight', type=float, default=0, help="tbd" ) parser_add.add_argument( '-r', '--repeat', type=validate_time_period, help="Repetition period. Format examples: " "'X years' " "'X months' " "'X days' " "workdays" ) parser_add.add_argument( '-d', '--date', default="today", type=validate_relative_date, help="Due date. Excepted formats:" "YYYY-MM-DD" "today [default]" "tomorrow" "'+X days'" "'+X months'" "'+X years'" "no" ) # listing tasks parser_list = subparsers.add_parser('list', help='List tasks') list_group = parser_list.add_mutually_exclusive_group() list_group.add_argument( '-t', '--top', action='store_true', help="List the highest priority task today" ) parser_list.add_argument( '-d', '--date', type=str, help="List open tasks at a given date. Format: YYYY-MM-DD" ) list_group.add_argument( '-o', '--open', action='store_true', help="List open tasks" ) # modifying tasks parser_mod = subparsers.add_parser('mod', help='Modify a task') parser_mod.add_argument( 'id', type=str, help="Name of the task to add" ) parser_mod.add_argument( '-n', '--name', type=str, default="", help="Name of the task" ) parser_mod.add_argument( '-p', '--priority', type=int, default=-1, help="Priority of the task" ) parser_mod.add_argument( '-t', '--time', type=validate_time, help="Start time of the task. Format HH:MM" ) parser_mod.add_argument( '-w', '--weight', type=float, default=-1, help="tbd" ) parser_mod.add_argument( '-r', '--repeat', type=validate_time_period, help="Repetition period. Format examples: " "'X days'" "'X months'" "'X years'" "workdays" ) parser_mod.add_argument( '-d', '--date', type=validate_relative_date, help="Postpone the task. Excepted formats:" "YYYY-MM-DD" "today" "tomorrow" "'+X days'" "'+X months'" "'+X years'" "no" ) # closing tasks parser_close = subparsers.add_parser('close', help='Mark a task as closed') parser_close.add_argument( 'id', type=str, nargs='?', help="ID of the task to close" ) # deleting tasks parser_delete = subparsers.add_parser('delete', help='Permanently delete a task') parser_delete.add_argument( 'id', type=str, help="ID of the task to delete" ) # reporting parser_report = subparsers.add_parser('report', help='Calculate a total weight of tasks. ' 'If no arguments specified - ' 'weight of all tasks today (both open and closed)') parser_report.add_argument( '-d', '--date', type=str, help="Total weight of tasks on a given date. Date format: YYYY-MM-DD" ) parser_report.add_argument( '-o', '--open', action='store_true', help="Total weight of open tasks today" ) parser_report.add_argument( '-p', '--plot', action='store_true', help="Build a plot of productivity by days" ) # notes parser_note = subparsers.add_parser('note', help='Add a note for a given day') parser_note.add_argument( 'text', type=str, help="The text of the note" ) parser_note.add_argument( '-d', '--date', type=validate_date, help="Associate the note with a date." "Default: current date" ) # RPG extension parser_rpg = subparsers.add_parser('rpg', help='tbd') rpg_group = parser_rpg.add_mutually_exclusive_group() rpg_group.add_argument( '-l', '--list-quests', action='store_true', help='tbd' ) rpg_group.add_argument( '-f', '--finish-quest', type=int, help='tbd' ) rpg_group.add_argument( '-a', '--list-awards', action='store_true', help='tbd' ) rpg_group.add_argument( '-c', '--claim-award', type=int, default=0, help='tbd' ) rpg_group.add_argument( '-p', '--character-parameters', action='store_true', help='tbd' ) args = parser.parse_args() return args
083f4e0c38ea3a4d1e7734136b08827aa26af32c
22,647
def _serialize_account(project): """Generate several useful fields related to a project's account""" account = project.account return {'goal': account.goal, 'community_contribution': account.community_contribution, 'total_donated': account.total_donated(), 'total_raised': account.total_raised(), 'total_cost': account.total_cost(), 'percent_raised': account.percent_raised(), 'percent_community': account.percent_community(), 'funded': account.funded(), 'remaining': account.remaining()}
dea20df5db1ae37f61d6c661f957432b7cb72158
22,648
import socket def _NodeThread(node_ip): """Check if this IP is a node""" log = GetLogger() SetThreadLogPrefix(node_ip) known_auth = [ ("admin", "admin"), ("admin", "solidfire") ] SFCONFIG_PORT = 442 sock = socket.socket() sock.settimeout(0.5) try: sock.connect((node_ip, SFCONFIG_PORT)) sock.close() log.debug("Port {} socket connect succeeded".format(SFCONFIG_PORT)) except (socket.timeout, socket.error, socket.herror, socket.gaierror): # If sfconfig is not running, this does not look like a node return None node_info = {} for user, passwd in known_auth: api = SolidFireNodeAPI(node_ip, user, passwd, maxRetryCount=0) try: result = api.Call("GetClusterConfig", {}, timeout=2) node_info["cluster"] = result["cluster"]["cluster"] node_info["name"] = result["cluster"]["name"] node_info["state"] = result["cluster"]["state"] if "version" in result["cluster"]: node_info["version"] = result["cluster"]["version"] else: result = api.Call("GetVersionInfo", {}, timeout=6) node_info["version"] = result["versionInfo"]["sfconfig"]["Version"] break except UnauthorizedError: continue except SolidFireError as ex: log.debug(str(ex)) return None if not node_info: return None if not node_info["cluster"]: node_info["cluster"] = "Available" return node_info
ffbd1058e105c4ddc7ddea11da1aa20862a8fb9f
22,649
def neighbor_smoothing_binary(data_3d, neighbors): """ takes a 3d binary (0/1) input, returns a "neighbor" smoothed 3d matrix Input: ------ data_3d: a 3d np.array (with 0s and 1s) -> 1s are "on", 0s are "off" neighbors: the value that indicates the number of neighbors around voxel to check Returns: -------- smoothed_neighbors: 3d np.array same shape as data_3d """ smoothed_neighbors = data_3d.copy() shape = data_3d.shape for i in 1 + np.arange(shape[0] - 2): for j in 1 + np.arange(shape[1] - 2): for k in 1 + np.arange(shape[2] - 2): # number of neighbors that need to be positivednm if np.sum(data_3d[(i - 1):(i + 2),(j - 1):(j + 2),(k - 1):(k + 2)] == 1) < neighbors and data_3d[i, j, k] == 1: smoothed_neighbors[i, j, k] = 0 return smoothed_neighbors
22f503e9843a1a0864e3a66c4ed05070712defa3
22,650
def view( name = None, description = None, parameters = (), devel = False ): """ Decorator function to be used to "annotate" the provided function as an view that is able to return a set of configurations for the proper display of associated information. Proper usage of the view definition/decoration is context based and should vary based on application. :type name: String :param name: The name of the view (in plain english) so that a better user experience is possible. :type description: String :param description: The description of the view (in plain english) so that a better user experience is possible. :type parameters: Tuple :param parameters: The sequence containing tuples that describe the various parameters to be send to the view. :type devel: bool :param devel: If the view should only be used/available under development like environments (eg: debugging purposes). :rtype: Function :return: The decorator function that is going to be used to generated the final function to be called. """ def decorator(function, *args, **kwargs): function._view = View( method = function.__name__, name = name or function.__name__, description = description, parameters = parameters, devel = devel ) return function return decorator
988750523fa0f2d471ec3d54e834411d565ff22f
22,651
import io import gzip def _decode_response(resp_bytes: bytes) -> str: """Even though we request identity, rvm.io sends us gzip.""" try: # Try UTF-8 first, in case they ever fix their bug return resp_bytes.decode('UTF-8') except UnicodeDecodeError: with io.BytesIO(resp_bytes) as bytesio: with gzip.GzipFile(fileobj=bytesio) as gzipfile: return gzipfile.read().decode('UTF-8')
3197cbdf3cc9d7b0337d6748cf5c1467759645ca
22,652
def registrymixin_models(): """Fixtures for RegistryMixin tests.""" # We have two sample models and two registered items to test that # the registry is unique to each model and is not a global registry # in the base RegistryMixin class. # Sample model 1 class RegistryTest1(BaseMixin, db.Model): """Registry test model 1.""" __tablename__ = 'registry_test1' # Sample model 2 class RegistryTest2(BaseMixin, db.Model): """Registry test model 2.""" __tablename__ = 'registry_test2' # Sample registered item (form or view) 1 class RegisteredItem1: """Registered item 1.""" def __init__(self, obj=None): """Init class.""" self.obj = obj # Sample registered item 2 @RegistryTest2.views('test') class RegisteredItem2: """Registered item 2.""" def __init__(self, obj=None): """Init class.""" self.obj = obj # Sample registered item 3 @RegistryTest1.features('is1') @RegistryTest2.features() def is1(obj): """Assert object is instance of RegistryTest1.""" return isinstance(obj, RegistryTest1) RegistryTest1.views.test = RegisteredItem1 return SimpleNamespace(**locals())
420ecaea78780524ac0a889af1d584d0bbced8f3
22,653
def is_test_input_output_file(file_name: str) -> bool: """ Return whether a file is used as input or output in a unit test. """ ret = is_under_test_dir(file_name) ret &= file_name.endswith(".txt") return ret
961c2dcda2cb848a1880b36ca06c01a8bf091704
22,654
import csv def generate_csv_from_queryset(queryset, csv_name = "query_csv"): """ Genera un file csv a partire da un oggetto di tipo QuerySet :param queryset: oggetto di tipo QuerySet :param csv_name: campo opzionale per indicare il nome di output del csv :return: oggetto response """ try: response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename=' + csv_name + '.csv' model_field_names = [] writer = csv.writer(response, delimiter=';') if isinstance(queryset.first(), dict): fields = queryset.first().keys() else: fields = [field.attname.split('_id')[0] if field.attname.endswith('_id') else field.attname for field in queryset.first()._meta.local_fields] for field in fields: model_field_names.append(field) writer.writerow(model_field_names) for query in queryset: csv_row = [] for field in fields: csv_row.append(query[field] if isinstance(query, dict) else model_to_dict(query)[field]) writer.writerow(csv_row) return response except Exception as e: return e
ba4fab63e40cf791d7ac148ca804f0f60173d395
22,655
def get(isamAppliance, id, check_mode=False, force=False, ignore_error=False): """ Retrieving the current runtime template files directory contents """ return isamAppliance.invoke_get("Retrieving the current runtime template files directory contents", "/mga/template_files/{0}".format(id), ignore_error=ignore_error)
252bf635445af134772e8e762508ec9eb32d974e
22,656
def update(contxt, vsmapp_id, attach_status=None, is_terminate=False): """update storage pool usage""" if contxt is None: contxt = context.get_admin_context() if not vsmapp_id: raise exception.StoragePoolUsageInvalid() is_terminate = utils.bool_from_str(is_terminate) kargs = { 'attach_status': attach_status, 'terminate_at': timeutils.utcnow() if is_terminate else None } try: return db.storage_pool_usage_update(contxt, vsmapp_id, kargs) except db_exc.DBError as e: LOG.exception(_("DB Error on updating new storage pool usage %s" % e)) raise exception.StoragePoolUsageFailure()
1015a7387cb264e9f8ce611d1b2de531aabb249e
22,657
import tqdm def adsgan(orig_data, params): """Generate synthetic data for ADSGAN framework. Args: orig_data: original data params: Network parameters mb_size: mini-batch size z_dim: random state dimension h_dim: hidden state dimension lamda: identifiability parameter iterations: training iterations Returns: synth_data: synthetically generated data """ # Reset the tensorflow graph tf.reset_default_graph() ## Parameters # Feature no x_dim = len(orig_data.columns) # Sample no no = len(orig_data) # Batch size mb_size = params['mb_size'] # Random variable dimension z_dim = params['z_dim'] # Hidden unit dimensions h_dim = params['h_dim'] # Identifiability parameter lamda = params['lamda'] # Training iterations iterations = params['iterations'] # WGAN-GP parameters lam = 10 lr = 1e-4 #%% Data Preprocessing orig_data = np.asarray(orig_data) def data_normalization(orig_data, epsilon = 1e-8): min_val = np.min(orig_data, axis=0) normalized_data = orig_data - min_val max_val = np.max(normalized_data, axis=0) normalized_data = normalized_data / (max_val + epsilon) normalization_params = {"min_val": min_val, "max_val": max_val} return normalized_data, normalization_params def data_renormalization(normalized_data, normalization_params, epsilon = 1e-8): renormalized_data = normalized_data * (normalization_params['max_val'] + epsilon) renormalized_data = renormalized_data + normalization_params['min_val'] return renormalized_data orig_data, normalization_params = data_normalization(orig_data) #%% Necessary Functions # Xavier Initialization Definition def xavier_init(size): in_dim = size[0] xavier_stddev = 1. / tf.sqrt(in_dim / 2.) return tf.random_normal(shape = size, stddev = xavier_stddev) # Sample from uniform distribution def sample_Z(m, n): return np.random.uniform(-1., 1., size = [m, n]) # Sample from the real data def sample_X(m, n): return np.random.permutation(m)[:n] #%% Placeholder # Feature X = tf.placeholder(tf.float32, shape = [None, x_dim]) # Random Variable Z = tf.placeholder(tf.float32, shape = [None, z_dim]) #%% Discriminator # Discriminator D_W1 = tf.Variable(xavier_init([x_dim, h_dim])) D_b1 = tf.Variable(tf.zeros(shape=[h_dim])) D_W2 = tf.Variable(xavier_init([h_dim,h_dim])) D_b2 = tf.Variable(tf.zeros(shape=[h_dim])) D_W3 = tf.Variable(xavier_init([h_dim,1])) D_b3 = tf.Variable(tf.zeros(shape=[1])) theta_D = [D_W1, D_W2, D_W3, D_b1, D_b2, D_b3] #%% Generator G_W1 = tf.Variable(xavier_init([z_dim + x_dim, h_dim])) G_b1 = tf.Variable(tf.zeros(shape=[h_dim])) G_W2 = tf.Variable(xavier_init([h_dim,h_dim])) G_b2 = tf.Variable(tf.zeros(shape=[h_dim])) G_W3 = tf.Variable(xavier_init([h_dim,h_dim])) G_b3 = tf.Variable(tf.zeros(shape=[h_dim])) G_W4 = tf.Variable(xavier_init([h_dim, x_dim])) G_b4 = tf.Variable(tf.zeros(shape=[x_dim])) theta_G = [G_W1, G_W2, G_W3, G_W4, G_b1, G_b2, G_b3, G_b4] #%% Generator and discriminator functions def generator(z, x): inputs = tf.concat([z, x], axis = 1) G_h1 = tf.nn.tanh(tf.matmul(inputs, G_W1) + G_b1) G_h2 = tf.nn.tanh(tf.matmul(G_h1, G_W2) + G_b2) G_h3 = tf.nn.tanh(tf.matmul(G_h2, G_W3) + G_b3) G_log_prob = tf.nn.sigmoid(tf.matmul(G_h3, G_W4) + G_b4) return G_log_prob def discriminator(x): D_h1 = tf.nn.relu(tf.matmul(x, D_W1) + D_b1) D_h2 = tf.nn.relu(tf.matmul(D_h1, D_W2) + D_b2) out = (tf.matmul(D_h2, D_W3) + D_b3) return out #%% Structure G_sample = generator(Z,X) D_real = discriminator(X) D_fake = discriminator(G_sample) # Replacement of Clipping algorithm to Penalty term # 1. Line 6 in Algorithm 1 eps = tf.random_uniform([mb_size, 1], minval = 0., maxval = 1.) X_inter = eps*X + (1. - eps) * G_sample # 2. Line 7 in Algorithm 1 grad = tf.gradients(discriminator(X_inter), [X_inter])[0] grad_norm = tf.sqrt(tf.reduce_sum((grad)**2 + 1e-8, axis = 1)) grad_pen = lam * tf.reduce_mean((grad_norm - 1)**2) # Loss function D_loss = tf.reduce_mean(D_fake) - tf.reduce_mean(D_real) + grad_pen G_loss1 = -tf.sqrt(tf.reduce_mean(tf.square(X - G_sample))) G_loss2 = -tf.reduce_mean(D_fake) G_loss = G_loss2 + lamda * G_loss1 # Solver D_solver = (tf.train.AdamOptimizer(learning_rate = lr, beta1 = 0.5).minimize(D_loss, var_list = theta_D)) G_solver = (tf.train.AdamOptimizer(learning_rate = lr, beta1 = 0.5).minimize(G_loss, var_list = theta_G)) #%% Iterations sess = tf.Session() sess.run(tf.global_variables_initializer()) # Iterations for it in tqdm(range(iterations)): # Discriminator training for _ in range(5): Z_mb = sample_Z(mb_size, z_dim) X_idx = sample_X(no,mb_size) X_mb = orig_data[X_idx,:] _, D_loss_curr = sess.run([D_solver, D_loss], feed_dict = {X: X_mb, Z: Z_mb}) # Generator Training Z_mb = sample_Z(mb_size, z_dim) X_idx = sample_X(no,mb_size) X_mb = orig_data[X_idx,:] _, G_loss1_curr, G_loss2_curr = sess.run([G_solver, G_loss1, G_loss2], feed_dict = {X: X_mb, Z: Z_mb}) #%% Output Generation synth_data = sess.run([G_sample], feed_dict = {Z: sample_Z(no, z_dim), X: orig_data}) synth_data = synth_data[0] # Renormalization synth_data = data_renormalization(synth_data, normalization_params) # Binary features for i in range(x_dim): if len(np.unique(orig_data[:, i])) == 2: synth_data[:, i] = np.round(synth_data[:, i]) return synth_data
f0e4f85f93d116c75c82a1aba524bed89beb4f14
22,658
import requests def unemployed(year,manu, key,state='*'): #ex manu= 54 is professional, scientific, and technical service industries, year= 2017 """Yearly data on self-employed manufacturing sectors for all counties. Returns all receipts in thousands of dollars for all counties for the specified state for certain industries. Parameters ---------- year: int Only full 4-integer values for years where the Community Survey is available, 2009-2019 manu: str string for a manufacturing sector code key: str API key requested from US census.gov website, string format state: str string argument for state code Returns ------- dataframe Pandas dataframe extracted with the inserted parameters from the census manufacturing survey Examples -------- >>> from us_census import us_census >>> unemployed(2002, '54', MYKEY, '02')""" assert isinstance(year, int), "Please only ask for available years and ensure the year entry is an integer" assert isinstance(manu, str), "Ensure the manufacturing sector is viable and a string." assert isinstance(state, str) r= requests.get(f'http://api.census.gov/data/{year}/nonemp?get=NRCPTOT,NAME&for=county:*&in=state:{state}&NAICS{year}={manu}&key={key}') if r.status_code== 200: try: df= pd.DataFrame(r.json()) return df except (NameError): print("This state, year, or manufacturing sector code was not found. Please try valid inputs for the American Manufacturing survey .")
1ee03a5a0ee1faf2101b8785aadda688b722a96e
22,659
def stack_bricks(top_brick, bottom_brick): """Stacks two Duplo bricks, returns the attachment frame of the top brick.""" arena = composer.Arena() # Bottom brick is fixed in place, top brick has a freejoint. arena.attach(bottom_brick) attachment_frame = arena.add_free_entity(top_brick) # Attachment frame is positioned such that the top brick is on top of the # bottom brick. attachment_frame.pos = (0, 0, 0.0192) return arena, attachment_frame
64b096bc77dfcd62c39cccdfad10241a1574a1ec
22,660
def trigger(name): """ @trigger decorator allow to register a function as a trigger with a given name Parameters ---------- name : str Name of the trigger """ _app = get_app_instance() return _app.trigger(name)
3af727c206565346b69ea5eb6735d9237cefeaf3
22,661
from typing import Dict from typing import List def get_components_to_remove(component_dict: Dict[str, ComponentImport]) -> List[str]: """Gets a list of components to remove from the dictionary using console input. Args: component_dict (Dict[str, ComponentImport]): The custom component dictionary. Returns: List[str]: The keys to remove from the dictionary. """ components = [] if component_dict: name = get_str("Enter a component name to remove(blank to skip): ") else: name = "" while name != "": if name.startswith("custom/"): name = name.removeprefix("custom/") if f"custom/{name}" not in component_dict: error(f"No component import with name: {name}") elif f"custom/{name}" in components: error(f"Already removing component import with name: {name}") else: components.append(f"custom/{name}") if len(component_dict) <= len(components): break name = get_str("Enter a component name to remove(blank to skip): ") return components
fd423b0de58ef2e8d5db1a63d334f034a729f2f4
22,662
import requests def list_data_objects(): """ This endpoint translates DOS List requests into requests against indexd and converts the responses into GA4GH messages. :return: """ req_body = app.current_request.json_body if req_body: page_token = req_body.get('page_token', None) page_size = req_body.get('page_size', None) else: page_token = "0" page_size = 100 if req_body and (page_token or page_size): gdc_req = dos_list_request_to_indexd(req_body) else: gdc_req = {} response = requests.get("{}/index/".format(INDEXD_URL), params=gdc_req) if response.status_code != 200: return Response( {'msg': 'The request was malformed {}'.format( response.json().get('message', ""))}) list_response = response.json() return gdc_to_dos_list_response(list_response)
0eae49f4e559a4d640ce304cfaa17ef3806b2937
22,663
from typing import Union def get_include_file_start_line(block: Block) -> Union[int, None]: """ >>> block = lib_test.get_test_block_ok() >>> # test start-line set to 10 >>> get_include_file_start_line(block) 10 >>> assert block.include_file_start_line == 10 >>> # test start-line not set >>> block = lib_test.get_test_block_start_line_not_set() >>> get_include_file_start_line(block) >>> assert block.include_file_start_line is None >>> # test start-line invalid >>> block = lib_test.get_test_block_start_line_invalid() >>> get_include_file_start_line(block) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE Traceback (most recent call last): ... ValueError: Error in File ".../README.template.rst", Line 47103: option "start-line" has no value >>> # test start-line not integer >>> block = lib_test.get_test_block_start_line_not_integer() >>> get_include_file_start_line(block) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE Traceback (most recent call last): ... TypeError: Error in File ".../README.template.rst", Line 47103: option "start-line" has to be integer """ include_file_start_line = block.include_file_start_line if lib_block_options.is_option_in_block('start-line', block): include_file_start_line = int(lib_block_options.get_option_value_from_block_or_raise_if_empty_or_invalid('start-line', block, value_must_be_int=True)) block.include_file_start_line = include_file_start_line return include_file_start_line
5e9ddeed83192f8a53acaca1a6ac4493a7dcbe36
22,664
def get_wcets(utils, periods): """ Returns WCET """ return [ui * ti for ui, ti in zip(utils, periods)] # return [math.ceil(ui * ti) for ui, ti in zip(utils, periods)]
f853459b2463fc75b91f5effe3357b9c4ec5c4f9
22,665
def aws_get_dynamodb_table_names(profile_name: str) -> list: """ get all DynamoDB tables :param profile_name: AWS IAM profile name :return: a list of DynamoDB table names """ dynamodb_client = aws_get_client("dynamodb", profile_name) table_names = [] more_to_evaluate = True last_evaluated_table_name = None while more_to_evaluate: if last_evaluated_table_name is None: response = dynamodb_client.list_tables() else: response = dynamodb_client.list_tables(ExclusiveStartTableName=last_evaluated_table_name) partial_table_names = response.get("TableNames") last_evaluated_table_name = response.get("LastEvaluatedTableName") if partial_table_names is not None and len(partial_table_names) > 0: table_names.extend(partial_table_names) if last_evaluated_table_name is None: more_to_evaluate = False table_names.sort() return table_names
fddc574e8f4f6a798e5fe0b339c898b09f18b527
22,666
from typing import Any def stack_images_vertical( image_0: NDArray[(Any, ...), Any], image_1: NDArray[(Any, ...), Any] ) -> NDArray[(Any, ...), Any]: """ Stack two images vertically. Args: image_0: The image to place on the top. image_1: The image to place on the bottom. Returns: An image with the original two images on top of eachother. Note: The images must have the same width. Example:: color_image = rc.camera.get_color_image() depth_image = rc.camera.get_depth_image() depth_image_colormap = rc_utils.colormap_depth_image(depth_image) # Create a new image with the color on the top and depth on the bottom new_image = rc_utils.stack_images_vertically(color_image, depth_image_colormap) """ assert ( image_0.shape[1] == image_1.shape[1] ), f"image_0 width ({image_0.shape[1]}) must be the same as image_1 width ({image_1.shape[1]})." return np.vstack((image_0, image_1))
bb83cc6246bef5df370b45ac0ce7ed5b58a974f7
22,667
def simulate_ids(num): """ 模拟生成一定数量的身份证号 """ ids = [] if num > 0: for i in range(1, num+1): id_raw = digit_1to6() + digit_7to10() + digit_11to14() + digit_15to17() id = id_raw + digit_18(id_raw) ids.append(id) else: return False return ids
2e74fb150f01b8d43eefffdab2d95790cad6f334
22,668
def geomean(x): """computes geometric mean """ return exp(sum(log(i) for i in x) / len(x))
7c5ad27938a6d6da7f304d7ac66fcc1a179162c2
22,669
from typing import Tuple def _get_mean_traces_for_iteration_line_plot( scenario_df: pd.DataFrame, ) -> Tuple[go.Scatter, go.Scatter, go.Scatter]: """Returns the traces for the mean of the success rate. Parameters ---------- scenario_df : pd.DataFrame DataFrame containing the columns "n_iterations", "mean_success_rate", "std_success_rate" Returns ------- Tuple[go.Scatter, go.Scatter, go.Scatter] tuple of traces. Contains: - main_trace: mean - upper_trace: upper bound using the standard deviation - lower_trace: lower bound using the standard deviation """ # standard colors used by Plotly colors = px.colors.qualitative.Plotly # mean of success rate mean_trace = go.Scatter( name="Mean", x=scenario_df["n_iterations"], y=scenario_df["mean_success_rate"], # line=dict(color="rgb(0,100,80)"), mode="lines+markers", marker=dict(size=15), line=dict(width=4), # legendgroup="group", visible=False, ) # upper std bound of success rate y = scenario_df["mean_success_rate"] + 2 * scenario_df["std_success_rate"] y = np.minimum(y, 1) upper_trace = go.Scatter( name="Upper bound", x=scenario_df["n_iterations"], y=y, mode="lines", # make the line invisible line=dict(color="rgba(255,255,255,0)"), showlegend=False, # legendgroup="group", visible=False, ) # lower std bound of success rate y = scenario_df["mean_success_rate"] - 2 * scenario_df["std_success_rate"] y = np.maximum(y, 0) lower_trace = go.Scatter( name="Lower bound", x=scenario_df["n_iterations"], y=y, mode="lines", # make the line invisible line=dict(color="rgba(255,255,255,0)"), fill="tonexty", fillcolor=f"rgba{(*_hex_to_rgb(colors[0]), 0.3)}", showlegend=False, # legendgroup="group", visible=False, ) return mean_trace, upper_trace, lower_trace
04542d1b1f76173e8fa2431a5a6bd3b8e8627026
22,670
def create_graph_of_words(words, database, filename, window_size = 4): """ Function that creates a Graph of Words that contains all nodes from each document for easy comparison, inside the neo4j database, using the appropriate cypher queries. """ # Files that have word length < window size, are skipped. # Window size ranges from 2 to 6. length = len(words) if (length < window_size): # Early exit, we return the skipped filename return filename # We are using a global set of edges to avoid creating duplicate edges between different graph of words. # Basically the co-occurences will be merged. global edges # We are using a global set of edges to avoid creating duplicate nodes between different graph of words. # A list is being used to respect the order of appearance. global nodes # We are getting the unique terms for the current graph of words. terms = [] for word in words: if word not in terms: terms.append(word) # Remove end-of-sentence token, so it doesn't get created. if 'e5c' in terms: terms.remove('e5c') # If the word doesn't exist as a node, then create it. for word in terms: if word not in nodes: database.execute(f'CREATE (w:Word {{key: "{word}"}})', 'w') # Append word to the global node graph, to avoid duplicate creation. nodes.append(word) # Create unique connections between existing nodes of the graph. for i, current in enumerate(words): # If there are leftover items smaller than the window size, reduce it. if i + window_size > length: window_size = window_size - 1 # If the current word is the end of sentence string, # we need to skip it, in order to go to the words of the next sentence, # without connecting words of different sentences, in the database. if current == 'e5c': continue # Connect the current element with the next elements of the window size. for j in range(1, window_size): next = words[i + j] # Reached the end of sentence string. # We can't connect words of different sentences, # therefore we need to pick a new current word, # by going back out to the outer loop. if next == 'e5c': break edge = (current, next) if edge in edges: # If the edge, exists just update its weight. edges[edge] = edges[edge] + 1 query = (f'MATCH (w1:Word {{key: "{current}"}})-[r:connects]-(w2:Word {{key: "{next}"}}) ' f'SET r.weight = {edges[edge]}') else: # Else, create it, with a starting weight of 1 meaning first co-occurence. edges[edge] = 1 query = (f'MATCH (w1:Word {{key: "{current}"}}) ' f'MATCH (w2:Word {{key: "{next}"}}) ' f'MERGE (w1)-[r:connects {{weight: {edges[edge]}}}]-(w2)') # This line of code, is meant to be executed, in both cases of the if...else statement. database.execute(query, 'w') # Create a parent node that represents the document itself. # This node is connected to all words of its own graph, # and will be used for similarity/comparison queries. database.execute(f'CREATE (d:Document {{filename: "{filename}"}})', 'w') # Create a word list with comma separated, quoted strings for use in the Cypher query below. #word_list = ', '.join(f'"{word}"' for word in terms) query = (f'MATCH (w:Word) WHERE w.key IN {terms} ' 'WITH collect(w) as words ' f'MATCH (d:Document {{filename: "{filename}"}}) ' 'UNWIND words as word ' 'CREATE (d)-[:includes]->(word)') database.execute(query, 'w') return
89560a26d2ab624a28b07bf2ff826a8eb34564e8
22,671
from pathlib import Path def get_dir(path): """ Функция возвращает директорию файла, если он является файлом, иначе возвращает объект Path из указанного пути """ if not isinstance(path, Path): path = Path(path) return path.parent if path.is_file() else path
b7ca7f60d88c06bc3181bd93039c13a13e2684a4
22,672
import hashlib def get_file_sha(fname): """ Calculates the SHA1 of a given file. `fname`: the file path return: the calculated SHA1 as hex """ result = '' if isfile(fname): sha1 = hashlib.sha1() with open(fname, 'rb') as f: while True: data = f.read(BUF_SIZE) if not data: break sha1.update(data) result = sha1.hexdigest() return result
c328f8c5b65a018016a02d39a54b753c33cd3217
22,673
def graphtool_to_gjgf(graph): """Convert a graph-tool graph object to gJGF. Parameters ---------- graph : graph object from graph-tool Returns ------- gjgf : dict Dictionary adhering to :doc:`gravis JSON Graph Format (gJGF) <../../format_specification>` Caution ------- 0 and 0.0 values are ignored because they represent missing values in graph-tool. This can cause problems when such values have the usual meaning of a quantity being zero. """ data, data_graph, data_nodes, data_edges = _internal.prepare_gjgf_dict() # 1) Graph properties graph_directed = graph.is_directed() graph_metadata_dict = {key: graph.graph_properties[key] # key syntax is necessary for key in graph.graph_properties.keys()} _internal.insert_graph_data(data_graph, graph_directed, graph_metadata_dict) # 2) Nodes and their properties for node_object in graph.vertices(): node_id = str(node_object) node_metadata_dict = {} for key, value_array in graph.vertex_properties.items(): val = value_array[node_object] if isinstance(val, (str, int, float)) and val not in ('', 0, 0.0): node_metadata_dict[key] = val _internal.insert_node_data(data_nodes, node_id, node_metadata_dict) # 3) Edges and their properties for edge_object in graph.edges(): edge_source_id = str(edge_object.source()) edge_target_id = str(edge_object.target()) edge_metadata_dict = {} for key, value_array in graph.edge_properties.items(): val = value_array[edge_object] if val not in ('', 0, 0.0): edge_metadata_dict[key] = val _internal.insert_edge_data(data_edges, edge_source_id, edge_target_id, edge_metadata_dict) return data
73ceca04270aaa1f754001d446dba9c796ef6822
22,674
def forbidden_error(error): """ 418 I'm a teapot """ return engine.get_template('errors/417.html').render({}), 418
43319b9aa95c970e75e392e584dd8af78199ceb0
22,675
def green(s: str) -> str: """green(s) color s with green. This function exists to encapsulate the coloring methods only in utils.py. """ return colorama.Fore.GREEN + s + colorama.Fore.RESET
56f12d56257d0728d7d71cba61256bede5c3a064
22,676
import pickle def cache_fun(fname_cache, fun): """Check whether cached data exists, otherwise call fun and return Parameters ---------- fname_cache: string name of cache to look for fun: function function to call in case cache doesn't exist probably a lambda function """ try: print("checking cache for", fname_cache) with open(fname_cache, 'rb') as fhandle: print("found cache") ret = pickle.load(fhandle) except (FileNotFoundError, EOFError): print("cache not found, running function") ret = fun() with open(fname_cache, 'wb') as fhandle: pickle.dump(ret, fhandle) return ret
0387f611ab12aeb8a2d7dfca664e08b0438b1905
22,677
def find_server_storage_UUIDs(serveruuid): """ @rtype : list @return: """ storageuuids = [] db = dbconnect() cursor = db.cursor() cursor.execute("SELECT UUID, ServerUUID FROM Storage WHERE ServerUUID = '%s'" % serveruuid) results = cursor.fetchall() for row in results: storageuuids.append(row[0]) db.close() return storageuuids
48abd3e3a9dc49cef5f9eb52d7327e79a61602a4
22,678
import torch def unpack_bidirectional_lstm_state(state, num_directions=2): """ Unpack the packed hidden state of a BiLSTM s.t. the first dimension equals to the number of layers multiplied by the number of directions. """ batch_size = state.size(1) new_hidden_dim = int(state.size(2) / num_directions) return torch.stack(torch.split(state, new_hidden_dim, dim=2), dim=1).view(-1, batch_size, new_hidden_dim)
fa58ed9bcf2e9e95aa62b3d18110abe6abce6b1b
22,679
def macd_diff(close, window_slow=26, window_fast=12, window_sign=9, fillna=False): """Moving Average Convergence Divergence (MACD Diff) Shows the relationship between MACD and MACD Signal. https://en.wikipedia.org/wiki/MACD Args: close(pandas.Series): dataset 'Close' column. window_fast(int): n period short-term. window_slow(int): n period long-term. window_sign(int): n period to signal. fillna(bool): if True, fill nan values. Returns: pandas.Series: New feature generated. """ return MACD( close=close, window_slow=window_slow, window_fast=window_fast, window_sign=window_sign, fillna=fillna, ).macd_diff()
ef5b222c688026a25121b35d4354967e4f5c93fc
22,680
def spike_histogram(series, merge_spikes=True, window_duration=60, n_bins=8): """ Args: * series (pd.Series): watts * merge_spikes (bool): Default = True * window_duration (float): Width of each window in seconds * n_bins (int): number of bins per window. Returns: spike_hist, bin_edges: spike_hist (pd.DataFrame): index is pd.DateTimeIndex of start of each time window columns are 2-tuples of the bin edges in watts (int) bin_edges (list of ints): """ fdiff = series.diff() if merge_spikes: fdiff = get_merged_spikes_pandas(fdiff) abs_fdiff = np.fabs(fdiff) freq = (window_duration, 'S') date_range, boundaries = _indicies_of_periods(fdiff.index, freq=freq) bin_edges = np.concatenate(([0], np.exp(np.arange(1,n_bins+1)))) bin_edges = np.round(bin_edges).astype(int) cols = zip(bin_edges[:-1], bin_edges[1:]) spike_hist = pd.DataFrame(index=date_range, columns=cols) for date_i, date in enumerate(date_range): start_i, end_i = boundaries[date_i] chunk = abs_fdiff[start_i:end_i] spike_hist.loc[date] = np.histogram(chunk, bins=bin_edges)[0] return spike_hist, bin_edges
fc401d8c2b4aabde646a3570397f8fdb8087bf6b
22,681
def data_context_connectivity_context_connectivity_serviceuuid_end_pointlocal_id_capacity_bandwidth_profile_peak_information_rate_post(uuid, local_id, tapi_common_capacity_value=None): # noqa: E501 """data_context_connectivity_context_connectivity_serviceuuid_end_pointlocal_id_capacity_bandwidth_profile_peak_information_rate_post creates tapi.common.CapacityValue # noqa: E501 :param uuid: Id of connectivity-service :type uuid: str :param local_id: Id of end-point :type local_id: str :param tapi_common_capacity_value: tapi.common.CapacityValue to be added to list :type tapi_common_capacity_value: dict | bytes :rtype: None """ if connexion.request.is_json: tapi_common_capacity_value = TapiCommonCapacityValue.from_dict(connexion.request.get_json()) # noqa: E501 return 'do some magic!'
92cefba50ff813e2fd1797d383ae2df7e02a99da
22,682
def any(*args, span=None): """Create a new experssion of the union of all conditions in the arguments Parameters ---------- args : list List of symbolic boolean expressions span : Optional[Span] The location of this operator in the source code. Returns ------- expr: Expr Expression """ if not args: raise ValueError("Any must take at least 1 argument") if len(args) == 1: return args[0] val = _ffi_api._OpOr(args[0], args[1], span) # type: ignore for i in range(2, len(args)): val = _ffi_api._OpOr(val, args[i], span) # type: ignore return val
697eb7e44d0fb9a0b9b947eea9fe4e8f68b78210
22,683
from typing import List from typing import Any def firstOrNone(list: List[Any]) -> Any: """ Return the first element of a list or None if it is not set """ return nthOrNone(list, 0)
9c51a2f72fe5f516258f2fd20210bd83a3cfbf2d
22,684
def ellipse_points( xy=[0,-5.], ex=254., ez=190., n=1000 ): """ :param ec: center of ellipse :param ex: xy radius of ellipse :param ez: z radius of ellipse :param n: number of points :return e: array of shape (n,2) of points on the ellipse """ t = np.linspace( 0, 2*np.pi, n ) e = np.zeros([len(t), 2]) e[:,0] = ex*np.cos(t) + xy[0] e[:,1] = ez*np.sin(t) + xy[1] return e
93b3aeccf6ab04ad8b0e96cdad80a52d9a6c46c4
22,685
def sum_dose_maps(dose_maps): """ sum a collection of dose maps to obtain the total dose """ ps.logger.debug('Summing %s dose_maps', len(dose_maps)) dose_maps = np.stack(dose_maps) return np.nansum(dose_maps, axis=0)
b05d54cc6e2276dca76eba68a643828ff14fa0fe
22,686
def ion_list(): """List of ions with pre-computed CLOUDY ionization fraction""" ions = np.array(['al2','c2','c3','c4','fe2','h1','mg2', 'n1','n2','n3','n4','n5','ne8','o1','o6', 'o7','o8','si2','si3','si4']) return ions
4fae0b3cf7956349a30807d4975b1d72cf10c9ec
22,687
def is_valid_filename(filename): """Determines if a filename is valid (with extension).""" valid_extensions = ['mp4', 'webm', 'ogg'] extension = get_extension(filename) return bool(extension and extension in valid_extensions)
6a493fac59fca900e5d3bf55075e897bf36528f7
22,688
import re def read(line_str, line_pos, pattern='[0-9a-zA-Z_:?!><=&]'): """ Read all tokens from a code line matching specific characters, starting at a specified position. Args: line_str (str): The code line. line_pos (int): The code line position to start reading. pattern (str): Regular expression for a single character. All matching characters will be read. Returns: literal (str): The literal that was read, including only characters that were defined in the pattern argument. line_pos (int): The updated line position. """ length = len(line_str) literal = '' while line_pos < length and re.match(pattern, line_str[line_pos]): literal += line_str[line_pos] line_pos += 1 return literal, line_pos
95ece37e927ff3f8ea9579a7d78251b10b1ed0e6
22,689
from typing import Dict import random def demographic(population: int, highest_lvl_ratio: int = ONE_MILLION, num_levels: int = NUM_LEVELS) -> Dict[int, int]: """ Calculate the number of levelled NPCs in a given population. Args: population: The population to consider these levelled NPCs in. highest_lvl_ratio: The fraction of the population that should be of the highest level. num_levels: The number of levels to consider. Returns: A dict mapping the levels (0-highest) to the number of NPCs at each level. """ # Generate the proportions of each level and scale to the desired population fractions = generate_per_level_fractions(highest_lvl_ratio, num_levels) rough_numbers = {(k + 1): (v * population) for k, v in enumerate(fractions)} # Take the rough numbers use the whole number part and probabilistically add the remainder final_numbers = dict() for level, rough_num in rough_numbers.items(): num, extra_prob = divmod(rough_num, 1) if random.random() < extra_prob: num += 1 final_numbers[level] = int(num) final_numbers[0] = population - sum(final_numbers.values()) return final_numbers
b45b26c7add41c85cf526789ba26f7c877db685a
22,690
def gencpppxd(env, exceptions=True, ts=None): """Generates all cpp_*.pxd Cython header files for an environment of modules. Parameters ---------- env : dict Environment dictonary mapping target module names to module description dictionaries. exceptions : bool or str, optional Cython exception annotation. Set to True to automatically detect exception types, False for when exceptions should not be included, and a str (such as '+' or '-1') to apply to everywhere. ts : TypeSystem, optional A type system instance. Returns ------- cpppxds : dict Maps environment target names to Cython cpp_*.pxd header files strings. """ ts = ts or TypeSystem() cpppxds = {} for name, mod in env.items(): if mod['srcpxd_filename'] is None: continue cpppxds[name] = modcpppxd(mod, exceptions, ts=ts) return cpppxds
627828bfc01c8282b0bf53f5e3cef234d0bdc816
22,691
import requests def configure_mongo_connection( key: str, host: str, port: int, dbname: str, username: str, password: str ): """ Configure the connection with the given `key` in fidesops with your PostgreSQL database credentials. Returns the response JSON if successful, or throws an error otherwise. See http://localhost:8000/docs#/Connections/put_connection_config_secrets_api_v1_connection__connection_key__secret_put """ connection_secrets_data = { "host": host, "port": port, "defaultauthdb": dbname, "username": username, "password": password, } response = requests.put( f"{FIDESOPS_URL}/api/v1/connection/{key}/secret", headers=oauth_header, json=connection_secrets_data, ) if response.ok: if (response.json())["test_status"] != "failed": logger.info( f"Configured fidesops mongo connection secrets via /api/v1/connection/{key}/secret" ) return response.json() raise RuntimeError( f"fidesops connection configuration failed! response.status_code={response.status_code}, response.json()={response.json()}" )
72746eb7bcebb747b4821f453e8f0f4543abc060
22,692
import random def fully_random(entries, count): """Choose completely at random from all entries""" return random.sample(entries, count)
a1f494f6b3cc635bc109378305bf547d48f29019
22,693
def _get_sets_grp(grpName="controllers_grp"): """Get set group Args: grpName (str, optional): group name Returns: PyNode: Set """ rig = _get_simple_rig_root() sets = rig.listConnections(type="objectSet") controllersGrp = None for oSet in sets: if grpName in oSet.name(): controllersGrp = oSet return controllersGrp
ec65ab91b69cb1c509412258b517f78f9e124f24
22,694
import re def clean_text(text, cvt_to_lowercase=True, norm_whitespaces=True): """ Cleans a text for language detection by transforming it to lowercase, removing unwanted characters and replacing whitespace characters for a simple space. :rtype : string :param text: Text to clean :param cvt_to_lowercase: Convert text to lowercase :param norm_whitespaces: Normalize whitespaces """ # converting text to lowercase (if required) cleaned_text = text.lower() if cvt_to_lowercase else text # removing unwanted characters cleaned_text = ''.join([ c for c in cleaned_text if c not in unwanted_chars ]) # normalizing whitespaces cleaned_text = re.sub(r'\s+', ' ', cleaned_text) if norm_whitespaces else cleaned_text # returning the cleaned text return cleaned_text
7586112429f529d21f5d7a992bf40d3604dfe52a
22,695
def print(*args, **kwargs) -> None: """Proxy for Console print.""" console = get_console() return console.print(*args, **kwargs)
49b96ae3df30bf09e742f8355f0867341396bc44
22,696
def student_dashboard(lti=lti, user_id=None): # def student_dashboard(user_id=None): """ Dashboard froms a student view. Used for students, parents and advisors :param lti: pylti :param user_id: users Canvas ID :return: template or error message """ # TODO REMOVE ME - not using records anymore record = Record.query.order_by(Record.id.desc()).first() # get current term current_term = EnrollmentTerm.query.filter(EnrollmentTerm.current_term).first() if current_term.cut_off_date: cut_off_date = current_term.cut_off_date else: cut_off_date = current_term.end_at # format as a string cut_off_date = cut_off_date.strftime("%Y-%m-%d") if user_id: # Todo - this probably isn't needed # check user is NOT authorized to access this file auth_users_id = [user["id"] for user in session["users"]] if not ( int(user_id) in auth_users_id or lti.is_role("admin") or lti.is_role("instructor") ): # TODO - OR role = 'admin' return "You are not authorized to view this users information" alignments, grades, user = get_user_dash_data(user_id) # calculation dictionaries calculation_dictionaries = get_calculation_dictionaries() if grades: return render_template( "users/dashboard.html", record=record, user=user, cut_off_date=cut_off_date, students=session["users"], grades=grades, calculation_dict=calculation_dictionaries, alignments=alignments, current_term=current_term ) return "You currently don't have any grades!"
361fdbe0c27738229d2a0e911ad0509e85ada990
22,697
import yaml def _yaml_to_dict(yaml_string): """ Converts a yaml string to dictionary Args: yaml_string: String containing YAML Returns: Dictionary containing the same object """ return yaml.safe_load(yaml_string)
c7de0c860028d17302cd4d07e20c3215503b977b
22,698
import urllib from bs4 import BeautifulSoup def room_urls_for_search_url(url): """ the urls of all rooms that are yieled in a search url """ with urllib.request.urlopen(url) as response: html = response.read() soup = BeautifulSoup(html, 'html.parser') room_urls = {erg_list_entry.find('a').find('strong').get_text(): room_url_for_room_id(erg_list_entry.find('a').get('href').split('.rgid=')[1].split('&')[0]) for erg_list_entry in soup.find_all('div', {'class': 'erg_list_entry'}) if erg_list_entry.find('div', {'class': 'erg_list_label'}).get_text() == 'Raum:'} return room_urls
8945b55e7379860defa0f4229690e53196acbe4d
22,699