content
stringlengths
35
762k
sha1
stringlengths
40
40
id
int64
0
3.66M
def is_valid_action(state, x, y, direction): """ Checks if moving the piece at given x, y coordinates in the given direction is valid, given the current state. :param state: the current state :param x: the x coordinate of the piece :param y: the y coordinate of the piece :param direction: the direction to travel with this action :return: True if the action is valid, False otherwise """ new_x = x + X_MOVEMENT_DIFFS[direction] new_y = y + Y_MOVEMENT_DIFFS[direction] return is_within_bounds(new_x, new_y) and is_free_square(state, new_x, new_y)
9be9d6a16d6ec3f766ee7f91c08d3ced7d5ff6b8
3,655,103
def range_(minimum, maximum): """ A validator that raises a :exc:`ValueError` if the initializer is called with a value that does not belong in the [minimum, maximum] range. The check is performed using ``minimum <= value and value <= maximum`` """ return _RangeValidator(minimum, maximum)
27dc9c9c814371eb03b25f99be874e39d48c1a52
3,655,104
def sigmoid_prime(z): """Helper function for backpropagation""" return sigmoid(z) * (1 - sigmoid(z))
13541050982152668cdcec728f3a913298f2aad8
3,655,105
def register_widget_util(ui_name, some_type, gen_widgets, apply_with_params): """ ui_name: the name of this utility in the UI some_type: this utility will appear in the sidebar whenever your view function returns a value of type ``some_type`` gen_widgets(val): a function that takes the report value (of the specified type), and returns a list of widgets. These widget values will be passed like: ``apply_with_params(val, *widget_values)``. apply_with_params: a function that takes the report value (of the specified type) as its first parameter, followed by a list of arguments that are given by widgets. The function must return the result of a call to ``file_response`` """ def gen_html(val): widgets = gen_widgets(val) widget_data = widgets_template_data(widgets) return render_template('utility_button.html', name=ui_name, widgets=widget_data) def apply_util(val, data): widgets = gen_widgets(val) validate_widget_form_data(widgets, data) inputs = parse_widget_form_data(widgets, data) return apply_with_params(val, *inputs) register_util_for_type(some_type, gen_html, apply_util)
25273753e0e31472cc44ef3527200e9dfa797de2
3,655,106
from datetime import datetime def _CreateSamplePostsubmitReport(manifest=None, builder='linux-code-coverage', modifier_id=0): """Returns a sample PostsubmitReport for testing purpose. Note: only use this method if the exact values don't matter. """ manifest = manifest or _CreateSampleManifest() return PostsubmitReport.Create( server_host='chromium.googlesource.com', project='chromium/src', ref='refs/heads/main', revision='aaaaa', bucket='coverage', builder=builder, commit_timestamp=datetime(2018, 1, 1), manifest=manifest, summary_metrics=_CreateSampleCoverageSummaryMetric(), build_id=123456789, modifier_id=modifier_id, visible=True)
5c7bccda2648f4d8d26725e983a567d5b011dbb6
3,655,107
import typing def _fetch_measurement_stats_arrays( ssc_s: typing.List[_NIScopeSSC], scalar_measurements: typing.List[niscope.ScalarMeasurement], ): """ private function for fetching statics for selected functions. Obtains a waveform measurement and returns the measurement value. This method may return multiple statistical results depending on the number of channels, the acquisition type, and the number of records you specify. You specify a particular measurement type, such as rise time, frequency, or voltage peak-to-peak. The waveform on which the digitizer calculates the waveform measurement is from an acquisition that you previously initiated. The statistics for the specified measurement method are returned, where the statistics are updated once every acquisition when the specified measurement is fetched by any of the Fetch Measurement methods. If a Fetch Measurement method has not been called, this method fetches the data on which to perform the measurement. The statistics are cleared by calling clear_waveform_measurement_stats. Many of the measurements use the low, mid, and high reference levels. You configure the low, mid, and high references with meas_chan_low_ref_level, meas_chan_mid_ref_level, and meas_chan_high_ref_level to set each channel differently. Args: ssc_s (typing.List[_NIScopeSSC]): List of sessions for various channels in groups. scalar_measurements (typing.List[niscope.ScalarMeasurement]): The list of scalar measurement to be performed on each fetched waveform. Returns: list of measurement_stats (list of MeasurementStats): Returns a list of class instances with the following measurement statistics about the specified measurement: - **result** (float): the resulting measurement - **mean** (float): the mean scalar value, which is obtained by averaging each fetch_measurement_stats call - **stdev** (float): the standard deviations of the most recent **numInStats** measurements - **min_val** (float): the smallest scalar value acquired (the minimum of the **numInStats** measurements) - **max_val** (float): the largest scalar value acquired (the maximum of the **numInStats** measurements) - **num_in_stats** (int): the number of times fetch_measurement_stats has been called - **channel** (str): channel name this result was acquired from - **record** (int): record number of this result """ stats: typing.List[niscope.MeasurementStats] = [] for ssc, scalar_meas_function in zip(ssc_s, scalar_measurements): stats.append( ssc.session.channels[ssc.channels].fetch_measurement_stats(scalar_meas_function) ) # function with unknown type return stats
4acad87bc9b0cd682725ea5edcade10d996653a1
3,655,109
def generate_repository_dependencies_folder_label_from_key( repository_name, repository_owner, changeset_revision, key ): """Return a repository dependency label based on the repository dependency key.""" if key_is_current_repositorys_key( repository_name, repository_owner, changeset_revision, key ): label = 'Repository dependencies' else: label = "Repository <b>%s</b> revision <b>%s</b> owned by <b>%s</b>" % ( repository_name, changeset_revision, repository_owner ) return label
5654b29354b07f9742ef1cdf20c313ecbcfec02f
3,655,111
def weighted_characteristic_path_length(matrix): """Calculate the characteristic path length for weighted graphs.""" n_nodes = len(matrix) min_distances = weighted_shortest_path(matrix) sum_vector = np.empty(n_nodes) for i in range(n_nodes): # calculate the inner sum sum_vector[i] = (1/(n_nodes-1)) * np.sum([min_distances[i, j] for j in range(n_nodes) if j != i]) return (1/n_nodes) * np.sum(sum_vector)
ff171ad9bf7a6968ebf9d41dd5c508bb8b39b16a
3,655,112
def mean_IoU(threshold=0.5, center_crop=0, get_batch_mean=True): """ - y_true is a 3D array. Each channel represents the ground truth BINARY channel - y_pred is a 3D array. Each channel represents the predicted BINARY channel """ def _f(y_true, y_pred): y_true = fix_input(y_true) y_pred = fix_input(y_pred) y_true = get_binary_img( y_true, threshold=threshold, center_crop=center_crop ) y_pred = get_binary_img( y_pred, threshold=threshold, center_crop=center_crop ) inter = get_intersection(y_true, y_pred) union = get_alls(y_true, y_pred) - inter batch_metric = eps_divide(inter, union) if get_batch_mean: return K.mean(batch_metric, axis=-1) return batch_metric _f.__name__ = 'attila_metrics_{}'.format('mean_IoU') return _f
0c9ee55b694e11615bd6ab023ce1f43354b986b1
3,655,114
import csv def ConvertCSVStringToList(csv_string): """Helper to convert a csv string to a list.""" reader = csv.reader([csv_string]) return list(reader)[0]
fa244d2a1c8c50b2b097883f964f1b5bb7ccf393
3,655,115
def get_section_range_pairs(orig_section, new_pdf): """Return MatchingSection for a section.""" other_section = new_pdf.find_corresponding_section(orig_section) if not other_section: print("Skipping section {} - no match in the other doc!".format( orig_section.title)) return None return MatchingSection( title=orig_section.title, orig_range=orig_section.pdf_diff_options, new_range=other_section.pdf_diff_options)
ff1ef7bedcc0a1264a8cd191267d35a75c302eac
3,655,116
import sqlite3 from typing import Any import logging def atomic_transaction(conn: sqlite3.Connection, sql: str, *args: Any) -> sqlite3.Cursor: """Perform an **atomic** transaction. The transaction is committed if there are no exceptions else the transaction is rolled back. Args: conn: database connection sql: formatted string *args: arguments to use for parameter substitution Returns: sqlite cursor """ try: c = transaction(conn, sql, *args) except Exception as e: logging.exception("Could not execute transaction, rolling back") conn.rollback() raise e conn.commit() return c
9748a6e315521278c4dc60df891081dcd77c98b9
3,655,117
def convert_to_tensor(narray, device): """Convert numpy to tensor.""" return tf.convert_to_tensor(narray, tf.float32)
699f4cbdad83bc72525d237549420e67d0d464f8
3,655,118
def get_instance_ip() -> str: """ For a given identifier for a deployment (env var of IDENTIFIER), find the cluster that was deployed, find the tasks within the cluster (there should only be one), find the network interfaces on that task, and return the public IP of the instance :returns: str The public ip of the remote instance """ ecs_c = boto3.client("ecs") task_arns = ecs_c.list_tasks( cluster=f"remote-cluster-{IDENTIFIER}", desiredStatus="RUNNING" )["taskArns"] if task_arns: tasks = ecs_c.describe_tasks( cluster=f"remote-cluster-{IDENTIFIER}", tasks=task_arns )["tasks"] # Should only ever be one task and network interface on deployment task_details = { d["name"]: d["value"] for d in tasks[0]["attachments"][0]["details"] } interface_id = task_details["networkInterfaceId"] ec2_c = boto3.client("ec2") network_interfaces = ec2_c.describe_network_interfaces( NetworkInterfaceIds=[interface_id] )["NetworkInterfaces"] return network_interfaces[0]["Association"]["PublicIp"] else: return None
6b52f6c385e2d96e500458397465f67550a0deeb
3,655,120
def is_hign_level_admin(): """超级管理员""" return is_admin() and request.user.level == 1
7faa7872f556307b67afd1e5604c104aa6aa242d
3,655,121
def object_metadata(save_path): """Retrieves information about the objects in a checkpoint. Example usage: ```python object_graph = tf.contrib.checkpoint.object_metadata( tf.train.latest_checkpoint(checkpoint_directory)) ckpt_variable_names = set() for node in object_graph.nodes: for attribute in node.attributes: ckpt_variable_names.add(attribute.full_name) ``` Args: save_path: The path to the checkpoint, as returned by `save` or `tf.train.latest_checkpoint`. Returns: A parsed `tf.contrib.checkpoint.TrackableObjectGraph` protocol buffer. Raises: ValueError: If an object graph was not found in the checkpoint. """ reader = pywrap_tensorflow.NewCheckpointReader(save_path) try: object_graph_string = reader.get_tensor(base.OBJECT_GRAPH_PROTO_KEY) except errors_impl.NotFoundError: raise ValueError( ('The specified checkpoint "%s" does not appear to be object-based (it ' 'is missing the key "%s"). Likely it was created with a name-based ' "saver and does not contain an object dependency graph.") % (save_path, base.OBJECT_GRAPH_PROTO_KEY)) object_graph_proto = (trackable_object_graph_pb2.TrackableObjectGraph()) object_graph_proto.ParseFromString(object_graph_string) return object_graph_proto
8a01cc2a60298a466921c81144bfbd9c4e43aa97
3,655,122
async def login(_request: Request, _user: User) -> response.HTTPResponse: """ Login redirect """ return redirect(app.url_for("pages.portfolios"))
375452df081f619db9c887359b6bb0217aa8e802
3,655,123
def delete_source(source_uuid: SourceId, database: Database): """Delete a source.""" data_model = latest_datamodel(database) reports = latest_reports(database) data = SourceData(data_model, reports, source_uuid) delta_description = ( f"{{user}} deleted the source '{data.source_name}' from metric " f"'{data.metric_name}' of subject '{data.subject_name}' in report '{data.report_name}'." ) uuids = [data.report_uuid, data.subject_uuid, data.metric_uuid, source_uuid] del data.metric["sources"][source_uuid] return insert_new_report(database, delta_description, (data.report, uuids))
f160a8304df54a20026155f1afb763c0077d05a9
3,655,124
def find_object_with_matching_attr(iterable, attr_name, value): """ Finds the first item in an iterable that has an attribute with the given name and value. Returns None otherwise. Returns: Matching item or None """ for item in iterable: try: if getattr(item, attr_name) == value: return item except AttributeError: pass return None
e37b7620bf484ce887e6a75f31592951ed93ac74
3,655,125
def send_message(token, message: str) -> str: """ A function that notifies LINENotify of the character string given as an argument :param message: A string to be notified :param token: LineNotify Access Token :return response: server response (thats like 200 etc...) """ notify = Notifer(token) return notify.send_message(message)
995abdc61398d9e977323e18f3a3e008fbaa1f3b
3,655,126
def _fix(node): """Fix the naive construction of the adjont. See `fixes.py` for details. This function also returns the result of reaching definitions analysis so that `split` mode can use this to carry over the state from primal to adjoint. Args: node: A module with the primal and adjoint function definitions as returned by `reverse_ad`. Returns: node: A module with the primal and adjoint function with additional variable definitions and such added so that pushes onto the stack and gradient accumulations are all valid. defined: The variables defined at the end of the primal. reaching: The variable definitions that reach the end of the primal. """ # Do reaching definitions analysis on primal and adjoint pri_cfg = cfg.CFG.build_cfg(node.body[0]) defined = cfg.Defined() defined.visit(pri_cfg.entry) reaching = cfg.ReachingDefinitions() reaching.visit(pri_cfg.entry) cfg.forward(node.body[1], cfg.Defined()) cfg.forward(node.body[1], cfg.ReachingDefinitions()) # Remove pushes of variables that were never defined fixes.CleanStack().visit(node) fixes.FixStack().visit(node.body[0]) # Change accumulation into definition if possible fixes.CleanGrad().visit(node.body[1]) # Define gradients that might or might not be defined fixes.FixGrad().visit(node.body[1]) return node, defined.exit, reaching.exit
27c6836366afc033e12fea254d9cf13a902d1ee7
3,655,127
def greyscale(state): """ Preprocess state (210, 160, 3) image into a (80, 80, 1) image in grey scale """ state = np.reshape(state, [210, 160, 3]).astype(np.float32) # grey scale state = state[:, :, 0] * 0.299 + state[:, :, 1] * 0.587 + state[:, :, 2] * 0.114 # karpathy state = state[35:195] # crop state = state[::2,::2] # downsample by factor of 2 state = state[:, :, np.newaxis] return state.astype(np.uint8)
446651be7573eb1352a84e48780b908b0383e0ca
3,655,128
def functional_common_information(dist, rvs=None, crvs=None, rv_mode=None): """ Compute the functional common information, F, of `dist`. It is the entropy of the smallest random variable W such that all the variables in `rvs` are rendered independent conditioned on W, and W is a function of `rvs`. Parameters ---------- dist : Distribution The distribution from which the functional common information is computed. rvs : list, None A list of lists. Each inner list specifies the indexes of the random variables used to calculate the total correlation. If None, then the total correlation is calculated over all random variables, which is equivalent to passing `rvs=dist.rvs`. crvs : list, None A single list of indexes specifying the random variables to condition on. If None, then no variables are conditioned on. rv_mode : str, None Specifies how to interpret `rvs` and `crvs`. Valid options are: {'indices', 'names'}. If equal to 'indices', then the elements of `crvs` and `rvs` are interpreted as random variable indices. If equal to 'names', the the elements are interpreted as random variable names. If `None`, then the value of `dist._rv_mode` is consulted, which defaults to 'indices'. Returns ------- F : float The functional common information. """ rvs, crvs, rv_mode = normalize_rvs(dist, rvs, crvs, rv_mode) dtc = dual_total_correlation(dist, rvs, crvs, rv_mode) ent = entropy(dist, rvs, crvs, rv_mode) if np.isclose(dtc, ent): return dtc d = functional_markov_chain(dist, rvs, crvs, rv_mode) return entropy(d, [dist.outcome_length()])
cbeef4f042fc5a28d4d909419c1991c0501c0522
3,655,129
def kubernetes_client() -> BatchV1Api: """ returns a kubernetes client """ config.load_config() return BatchV1Api()
6323d5074f1af02f52eb99b62f7109793908c549
3,655,130
def create_simple(): """Create an instance of the `Simple` class.""" return Simple()
98180d64e264c7842596c8f74ce28574459d2648
3,655,132
def contains_rep_info(line): """ Checks does that line contains link to the github repo (pretty simple 'algorithm' at the moment) :param line: string from aa readme file :return: true if it has link to the github repository :type line:string :rtype: boolean """ return True if line.find("https://github.com/") != -1 else False
335e10a654510a4eda7d28d8df71030f31f98ff1
3,655,133
def GetAtomPairFingerprintAsBitVect(mol): """ Returns the Atom-pair fingerprint for a molecule as a SparseBitVect. Note that this doesn't match the standard definition of atom pairs, which uses counts of the pairs, not just their presence. **Arguments**: - mol: a molecule **Returns**: a SparseBitVect >>> from rdkit import Chem >>> m = Chem.MolFromSmiles('CCC') >>> v = [ pyScorePair(m.GetAtomWithIdx(0),m.GetAtomWithIdx(1),1), ... pyScorePair(m.GetAtomWithIdx(0),m.GetAtomWithIdx(2),2), ... ] >>> v.sort() >>> fp = GetAtomPairFingerprintAsBitVect(m) >>> list(fp.GetOnBits())==v True """ res = DataStructs.SparseBitVect(fpLen) fp = rdMolDescriptors.GetAtomPairFingerprint(mol) for val in fp.GetNonzeroElements(): res.SetBit(val) return res
9a63aa57f25d9a856d5628ec53bab0378e8088d1
3,655,134
import sqlite3 def get_registrations_by_player_id(db_cursor: sqlite3.Cursor, player_id: int) -> list[registration.Registration]: """ Get a list of registrations by player id. :param db_cursor: database object to interact with database :param player_id: player id :return: a list of registrations """ db_cursor.execute("""SELECT * FROM registrations WHERE user_id = ?""", [player_id]) registration_infos = db_cursor.fetchall() registrations = [] for registration_info in registration_infos: registrations.append(registration.Registration.from_sqlite_table(registration_info)) return registrations
3e87d0f7379ac7657f85225be95dd6c1d7697300
3,655,135
from re import L def run_sim(alpha,db,m,DELTA,game,game_constants,i): """run a single simulation and save interaction data for each clone""" rates = (DEATH_RATE,DEATH_RATE/db) rand = np.random.RandomState() data = [get_areas_and_fitnesses(tissue,DELTA,game,game_constants) for tissue in lib.run_simulation(simulation,L,TIMESTEP,TIMEND,rand,progress_on=False, init_time=INIT_TIME,til_fix='exclude_final',save_areas=True,return_events=False,save_cell_histories=False, N_limit=MAX_POP_SIZE,DELTA=DELTA,game=game,game_constants=game_constants, mutant_num=1,domain_size_multiplier=m,rates=rates,threshold_area_fraction=alpha,generator=True)] return data
2201a836ff8c3da4c289908e561558c52206256b
3,655,137
def IMDB(*args, **kwargs): """ Defines IMDB datasets. The labels includes: - 0 : Negative - 1 : Positive Create sentiment analysis dataset: IMDB Separately returns the training and test dataset Arguments: root: Directory where the datasets are saved. Default: ".data" ngrams: a contiguous sequence of n items from s string text. Default: 1 vocab: Vocabulary used for dataset. If None, it will generate a new vocabulary based on the train data set. removed_tokens: removed tokens from output dataset (Default: []) tokenizer: the tokenizer used to preprocess raw text data. The default one is basic_english tokenizer in fastText. spacy tokenizer is supported as well. A custom tokenizer is callable function with input of a string and output of a token list. data_select: a string or tuple for the returned datasets (Default: ('train', 'test')) By default, all the three datasets (train, test, valid) are generated. Users could also choose any one or two of them, for example ('train', 'test') or just a string 'train'. If 'train' is not in the tuple or string, a vocab object should be provided which will be used to process valid and/or test data. Examples: >>> from torchtext.experimental.datasets import IMDB >>> from torchtext.data.utils import get_tokenizer >>> train, test = IMDB(ngrams=3) >>> tokenizer = get_tokenizer("spacy") >>> train, test = IMDB(tokenizer=tokenizer) >>> train, = IMDB(tokenizer=tokenizer, data_select='train') """ return _setup_datasets(*(("IMDB",) + args), **kwargs)
4bb55c88fc108fce350dc3c29a2ac4497ab205b1
3,655,138
from typing import List def load_multiples(image_file_list: List, method: str='mean', stretch: bool=True, **kwargs) -> ImageLike: """Combine multiple image files into one superimposed image. Parameters ---------- image_file_list : list A list of the files to be superimposed. method : {'mean', 'max', 'sum'} A string specifying how the image values should be combined. stretch : bool Whether to normalize the images being combined by stretching their high/low values to the same values across images. kwargs : Further keyword arguments are passed to the load function. Examples -------- Load multiple images:: >>> from pylinac.core.image import load_multiples >>> paths = ['starshot1.tif', 'starshot2.tif'] >>> superimposed_img = load_multiples(paths) """ # load images img_list = [load(path, **kwargs) for path in image_file_list] first_img = img_list[0] # check that all images are the same size and stretch if need be for img in img_list: if img.shape != first_img.shape: raise ValueError("Images were not the same shape") if stretch: img.array = stretcharray(img.array, fill_dtype=first_img.array.dtype) # stack and combine arrays new_array = np.dstack(tuple(img.array for img in img_list)) if method == 'mean': combined_arr = np.mean(new_array, axis=2) elif method == 'max': combined_arr = np.max(new_array, axis=2) elif method == 'sum': combined_arr = np.sum(new_array, axis=2) # replace array of first object and return first_img.array = combined_arr first_img.check_inversion_by_histogram() return first_img
f61f51c89f3318d17f2223640e34573282280e4a
3,655,139
def select_seeds( img: np.ndarray, clust_result: np.ndarray, FN: int = 500, TN: int = 700, n_clust_object: int = 2 ): """ Sample seeds from the fluid and retina regions acording to the procedure described in Rashno et al. 2017 Args: img (np.ndarray): Image from where to sample the seeds. clust_result (np.ndarray): Image from with the clustering labels. FN (int, optional): Number of fluid points to sample. Defaults to 500. TN (int, optional): Number of ratina points to sample. Defaults to 700. n_clust_object (int, optional): number of clusters assigned to fluid. Returns: fluid_seeds, retina_seeds """ n_clust = len(np.unique(clust_result)) - 1 clusters_centers = [] for i in range(1, n_clust+1): clusters_centers.append(np.mean(img[clust_result == i])) clusters_centers = np.array(clusters_centers) indices = np.flip(np.argsort(clusters_centers)) + 1 # Fluid Seeds fluid_condition = (clust_result == indices[0]) for i in range(n_clust_object): fluid_condition = fluid_condition | (clust_result == indices[i]) potential_seeds = np.array(np.where(fluid_condition)).T sample_indx = np.random.randint(0, potential_seeds.shape[0], FN) fluid_seeds = potential_seeds[sample_indx] # Retina Seeds: # Get sampling probabilities and number of samples per cluster pi = 1/(2**np.arange(1, n_clust-n_clust_object+1)) Npi = np.ones((n_clust-n_clust_object))*70 pre_asigned = (n_clust-n_clust_object)*70 Npi = Npi + np.round((pi/np.sum(pi))*(700-pre_asigned)) Npi = Npi.astype('int') # Npi = (np.ones((n_clust-n_clust_object))*(700 / n_clust)).astype('int') # Sample seeds retina_seeds = [] for i in range(n_clust_object, len(indices)): bkg_condition = (clust_result == indices[i]) potential_seeds = np.array(np.where(bkg_condition)).T sample_indx = \ np.random.randint(0, potential_seeds.shape[0], Npi[i-n_clust_object]) retina_seeds.append(potential_seeds[sample_indx]) retina_seeds = np.concatenate(retina_seeds) return fluid_seeds, retina_seeds, clusters_centers, indices
afdfe7654b53b5269f42c6c74d07265623839d75
3,655,140
def common(list1, list2): """ This function is passed two lists and returns a new list containing those elements that appear in both of the lists passed in. """ common_list = [] temp_list = list1.copy() temp_list.extend(list2) temp_list = list(set(temp_list)) temp_list.sort() for i in temp_list: if (i in list1) and (i in list2): common_list.append(i) return common_list
021605a2aad6c939155a9a35b8845992870100f0
3,655,141
def create_combobox(root, values, **kwargs): """Creates and Grids A Combobox""" box = ttk.Combobox(root, values=values, **kwargs) box.set(values[0]) return box
7406dca6ab99d9130a09a6cb25220c1e40148cc0
3,655,142
def chi_x2(samples,df): """ Compute the central chi-squared statistics for set of chi-squared distributed samples. Parameters: - - - - - samples : chi-square random variables df : degrees of freedom """ return chi2.pdf(samples,df)
5700803396e78c7a5658c05ff1b7bd7ae3bd6722
3,655,144
def integrate( pc2i, eos, initial_frac=DEFAULT_INITIAL_FRAC, rtol=DEFAULT_RTOL, ): """integrate the TOV equations with central pressure "pc2i" and equation of state described by energy density "eps/c2" and pressure "p/c2" expects eos = (logenthalpy, pressurec2, energy_densityc2, baryon_density, cs2c2) """ ### define initial condition logh, vec = initial_condition(pc2i, eos, frac=initial_frac) m, r, eta, omega, mb = engine( logh, vec, eos, dvecdlogh, rtol=rtol, ) # compute tidal deformability l = eta2lambda(r, m, eta) # compute moment of inertia i = omega2i(r, omega) # convert to "standard" units m /= Msun ### reported in units of solar masses, not grams mb /= Msun r *= 1e-5 ### convert from cm to km i /= 1e45 ### normalize this to a common value but still in CGS return m, r, l, i, mb
5934da344fc6927d397dccc7c2a730436e1106d5
3,655,145
import oci.exceptions def add_ingress_port_to_security_lists(**kwargs): """Checks if the given ingress port already is a security list, if not it gets added. Args: **kwargs: Optional parameters Keyword Args: security_lists (list): A list of security_lists. port (int): The port to check description (str): A description for the ingress rule compartment_id (str): The OCID of the compartment config (object): An OCI config object or None. config_profile (str): The name of an OCI config profile interactive (bool): Indicates whether to execute in interactive mode raise_exceptions (bool): If true exceptions are raised Returns: True on success """ security_lists = kwargs.get("security_lists") port = kwargs.get("port") description = kwargs.get("description") compartment_id = kwargs.get("compartment_id") config = kwargs.get("config") config_profile = kwargs.get("config_profile") interactive = kwargs.get("interactive", core.get_interactive_default()) raise_exceptions = kwargs.get("raise_exceptions", not interactive) if security_lists is None: raise ValueError("No security_lists given.") try: # Get the active config and compartment config = configuration.get_current_config( config=config, config_profile=config_profile, interactive=interactive) compartment_id = configuration.get_current_compartment_id( compartment_id=compartment_id, config=config) for sec_list in security_lists: for rule in sec_list.ingress_security_rules: if rule.tcp_options is not None and \ port >= rule.tcp_options.destination_port_range.min and \ port <= rule.tcp_options.destination_port_range.max and \ rule.protocol == "6" and \ rule.source == "0.0.0.0/0": return True if len(security_lists) == 0: raise Exception("No security list available for this network.") sec_list = security_lists[0] try: network_client = core.get_oci_virtual_network_client( config=config) sec_list.ingress_security_rules.append( oci.core.models.IngressSecurityRule( protocol="6", source="0.0.0.0/0", is_stateless=False, source_type="CIDR_BLOCK", tcp_options=oci.core.models.TcpOptions( destination_port_range=oci.core.models.PortRange( max=port, min=port), source_port_range=None), udp_options=None, description=description ) ) details = oci.core.models.UpdateSecurityListDetails( defined_tags=sec_list.defined_tags, display_name=sec_list.display_name, egress_security_rules=sec_list.egress_security_rules, freeform_tags=sec_list.freeform_tags, ingress_security_rules=sec_list.ingress_security_rules ) network_client.update_security_list( security_list_id=sec_list.id, update_security_list_details=details) return True except oci.exceptions.ServiceError as e: if raise_exceptions: raise print(f'ERROR: {e.message}. (Code: {e.code}; Status: {e.status})') except Exception as e: if raise_exceptions: raise print(f'Could not list the availability domains for this ' f'compartment.\nERROR: {str(e)}')
0fd1cdc05ea3c035c5424f2e179d2655e4b84bd4
3,655,146
def list_statistics_keys(): """ListStatistics definition""" return ["list", "counts"]
39521910b4dbde3fc6c9836460c73945561be731
3,655,149
def forecast_handler(req, req_body, res, res_body, zip): """Handles forecast requests""" return True
a2e35eaad472cfd52dead476d18d18ee2bcd3f6f
3,655,150
def refToMastoidsNP(data, M1, M2): """ """ mastoidsMean = np.mean([M1, M2], axis=0) mastoidsMean = mastoidsMean.reshape(mastoidsMean.shape[0], 1) newData = data - mastoidsMean return newData
15f50718fb1ea0d7b0fc2961a3a9b8d1baa98636
3,655,152
from typing import Dict from typing import Callable from typing import Any def override_kwargs( kwargs: Dict[str, str], func: Callable[..., Any], filter: Callable[..., Any] = lambda _: True, ) -> Dict[str, str]: """Override the kwargs of a function given a function to apply and an optional filter. Parameters ---------- kwargs : Tuple The function kwargs input. func : Callable A function to apply on the kwargs. filter : Callable An optional filter to apply the function only on some kwargs. (Default value = lambda _: True). Returns ------- Dict The changed kwargs as a Dict. """ return { key: func(value) if filter(value) else value for key, value in kwargs.items() }
31c689a1e2df1e5168f784011fbac6cf4a86bf13
3,655,153
def prepare_for_revival(bucket, obj_prefix): """ Makes a manifest for reviving any deleted objects in the bucket. A deleted object is one that has a delete marker as its latest version. :param bucket: The bucket that contains the stanzas. :param obj_prefix: The prefix of the uploaded stanzas. :return: The manifest as a list of lines in CSV format. """ try: response = s3.meta.client.list_object_versions( Bucket=bucket.name, Prefix=f'{obj_prefix}stanza') manifest_lines = [ f"{bucket.name},{parse.quote(marker['Key'])},{marker['VersionId']}" for marker in response['DeleteMarkers'] if marker['IsLatest'] ] except ClientError: logger.exception("Couldn't get object versions from %s.", bucket.name) raise return manifest_lines
879182e354d94f1c24cddd233e7c004939c4d0c0
3,655,154
def make_subparser(sub, command_name, help, command_func=None, details=None, **kwargs): """ Create the "sub-parser" for our command-line parser. This facilitates having multiple "commands" for a single script, for example "norm_yaml", "make_rest", etc. """ if command_func is None: command_func_name = "command_{0}".format(command_name) command_func = globals()[command_func_name] # Capitalize the first letter for the long description. desc = help[0].upper() + help[1:] if details is not None: desc += "\n\n{0}".format(details) desc = _wrap(desc) parser = sub.add_parser(command_name, formatter_class=FORMATTER_CLASS, help=help, description=desc, **kwargs) parser.set_defaults(run_command=command_func) return parser
acd2467c78f2ff477a5f0412cf48cbef19882f2c
3,655,156
def application(): """ Flask application fixture. """ def _view(): return 'OK', 200 application = Flask('test-application') application.testing = True application.add_url_rule('/', 'page', view_func=_view) return application
bd9168a66cb8db8c7a2b816f1521d5633fbdcbf8
3,655,158
def get_qe_specific_fp_run_inputs( configure, code_pw, code_wannier90, code_pw2wannier90, get_repeated_pw_input, get_metadata_singlecore ): """ Creates the InSb inputs for the QE fp_run workflow. For the higher-level workflows (fp_tb, optimize_*), these are passed in the 'fp_run' namespace. """ def inner(): return { 'scf': get_repeated_pw_input(), 'bands': { 'pw': get_repeated_pw_input() }, 'to_wannier': { 'nscf': get_repeated_pw_input(), 'wannier': { 'code': code_wannier90, 'metadata': get_metadata_singlecore() }, 'pw2wannier': { 'code': code_pw2wannier90, 'metadata': get_metadata_singlecore() } } } return inner
b0f8fd6536a237ade55139ef0ec6daaad8c0fb08
3,655,159
def _get_cohort_representation(cohort, course): """ Returns a JSON representation of a cohort. """ group_id, partition_id = cohorts.get_group_info_for_cohort(cohort) assignment_type = cohorts.get_assignment_type(cohort) return { 'name': cohort.name, 'id': cohort.id, 'user_count': cohort.users.filter(courseenrollment__course_id=course.location.course_key, courseenrollment__is_active=1).count(), 'assignment_type': assignment_type, 'user_partition_id': partition_id, 'group_id': group_id, }
aaa2c7b9c53e3a49ebc97e738077a0f6873d0559
3,655,160
import json def config_string(cfg_dict): """ Pretty-print cfg_dict with one-line queries """ upper_level = ["queries", "show_attributes", "priority", "gtf", "bed", "prefix", "outdir", "threads", "output_by_query"] query_level = ["feature", "feature_anchor", "distance", "strand", "relative_location", "filter_attribute", "attribute_values", "internals", "name"] upper_lines = [] for upper_key in upper_level: if upper_key == "queries": query_lines = "\"queries\":[\n" #Convert sets to lists for query in cfg_dict["queries"]: for key in query: if type(query[key]) == set: query[key] = list(query[key]) query_strings = [json.dumps(query, sort_keys=True) for query in cfg_dict["queries"]] query_lines += " " + ",\n ".join(query_strings) + "\n ]" upper_lines.append(query_lines) elif upper_key == "show_attributes" and upper_key in cfg_dict: upper_lines.append("\"{0}\": {1}".format(upper_key, json.dumps(cfg_dict[upper_key]))) else: if upper_key in cfg_dict: upper_lines.append("\"{0}\": \"{1}\"".format(upper_key, cfg_dict[upper_key])) config_string = "{\n" + ",\n".join(upper_lines) + "\n}\n" return(config_string)
c6533512b6f87fea1726573c0588bbd3ddd54e41
3,655,161
def area_km2_per_grid(infra_dataset, df_store): """Total area in km2 per assettype per grid, given in geographic coordinates Arguments: *infra_dataset* : a shapely object with WGS-84 coordinates *df_store* : (empty) geopandas dataframe containing coordinates per grid for each grid Returns: area in km2 per assettype per grid in dataframe (with column = {asset}_km2 and row = the grid) """ asset_list = [] for asset in infra_dataset.asset.unique(): if not "{}_count".format(asset) in df_store.columns: df_store.insert(1, "{}_count".format(asset), "") #add assettype as column after first column for count calculations if not "{}_km2".format(asset) in df_store.columns: df_store.insert(1, "{}_km2".format(asset), "") #add assettype as column after first column for area calculations asset_list.append(asset) for grid_row in df_store.itertuples(): grid_cell = grid_row.geometry #select grid try: asset_clip = gpd.clip(infra_dataset, grid_cell) #clip infra data using GeoPandas clip #count per asset type count = asset_clip.asset.value_counts() #count number of assets per asset type for asset_type in asset_list: if asset_type in count.index: df_store.loc[grid_row.Index, "{}_count".format(asset_type)] = count.get(key = asset_type) else: df_store.loc[grid_row.Index, "{}_count".format(asset_type)] = 0 #calculate area for each asset in clipped infrastructure grid asset_clip.insert(1, "area_km2", "") #add assettype as column after first column for length calculations for polygon_object in asset_clip['index']: asset_clip.loc[polygon_object, "area_km2"] = polygon_area((asset_clip.loc[asset_clip['index']==polygon_object].geometry.item())) #calculate area per object and put in dataframe area_per_type = asset_clip.groupby(['asset'])['area_km2'].sum() #get total length per asset_type in grid for asset_type in asset_list: if asset_type in area_per_type.index: df_store.loc[grid_row.Index, "{}_km2".format(asset_type)] = area_per_type.get(key = asset_type) else: df_store.loc[grid_row.Index, "{}_km2".format(asset_type)] = 0 except: print("Grid number {} raises a ValueError, area has not been clipped".format(grid_row.index)) for asset_type in asset_list: df_store.loc[grid_row.Index, "{}_count".format(asset_type)] = np.nan df_store.loc[grid_row.Index, "{}_km2".format(asset_type)] = np.nan return df_store
3d4b516429235f6b20a56801b0ef98e3fd80306d
3,655,162
from click.testing import CliRunner def cli_runner(script_info): """Create a CLI runner for testing a CLI command. Scope: module .. code-block:: python def test_cmd(cli_runner): result = cli_runner(mycmd) assert result.exit_code == 0 """ def cli_invoke(command, input=None, *args): return CliRunner().invoke(command, args, input=input, obj=script_info) return cli_invoke
3593354dd190bcc36f2099a92bad247c9f7c7cf1
3,655,163
def sgf_to_gamestate(sgf_string): """ Creates a GameState object from the first game in the given collection """ # Don't Repeat Yourself; parsing handled by sgf_iter_states for (gs, move, player) in sgf_iter_states(sgf_string, True): pass # gs has been updated in-place to the final state by the time # sgf_iter_states returns return gs
1c1a6274769abb654d51dc02d0b3182e7a9fd1f6
3,655,164
def get_titlebar_text(): """Return (style, text) tuples for startup.""" return [ ("class:title", "Hello World!"), ("class:title", " (Press <Exit> to quit.)"), ]
947b94f2e85d7a172f5c0ba84db0ec78045a0f6c
3,655,165
import json def image_fnames_captions(captions_file, images_dir, partition): """ Loads annotations file and return lists with each image's path and caption Arguments: partition: string either 'train' or 'val' Returns: all_captions: list of strings list with each image caption all_img_paths: list of paths as strings list with each image's path to file """ with open(captions_file, 'r') as f: annotations = json.load(f) all_captions = [] all_img_paths = [] for annot in annotations['annotations']: caption = '<start> ' + annot['caption'] + ' <end>' image_id = annot['image_id'] full_coco_image_path = images_dir / ('COCO_{}2014_'.format(partition) + \ '{:012d}.jpg'.format(image_id)) all_img_paths.append(full_coco_image_path) all_captions.append(caption) return all_captions, all_img_paths
f592decefaded079fca92091ad795d67150b4ca8
3,655,166
def build_menu( buttons: list, columns: int = 3, header_button=None, footer_button=None, resize_keyboard: bool = True ): """Хелпер для удобного построения меню.""" menu = [buttons[i:i + columns] for i in range(0, len(buttons), columns)] if header_button: menu.insert(0, [header_button]) if footer_button: menu.append([footer_button]) return ReplyKeyboardMarkup(menu, resize_keyboard=resize_keyboard)
d375a7af5f5e45e4b08520561c70d8c2664af4ef
3,655,167
def pandas_dataframe_to_unit_arrays(df, column_units=None): """Attach units to data in pandas dataframes and return united arrays. Parameters ---------- df : `pandas.DataFrame` Data in pandas dataframe. column_units : dict Dictionary of units to attach to columns of the dataframe. Overrides the units attribute if it is attached to the dataframe. Returns ------- Dictionary containing united arrays with keys corresponding to the dataframe column names. """ if not column_units: try: column_units = df.units except AttributeError: raise ValueError('No units attribute attached to pandas ' 'dataframe and col_units not given.') # Iterate through columns attaching units if we have them, if not, don't touch it res = {} for column in df: if column in column_units and column_units[column]: res[column] = df[column].values * units(column_units[column]) else: res[column] = df[column].values return res
41aff3bd785139f4d99d677e09def2764448acf2
3,655,168
from typing import Any def is_empty(value: Any) -> bool: """ empty means given value is one of none, zero length string, empty list, empty dict """ if value is None: return True elif isinstance(value, str): return len(value) == 0 elif isinstance(value, list): return len(value) == 0 elif isinstance(value, dict): return len(value) == 0 else: return False
fd4c68dd5f0369e0836ab775d73424360bad9219
3,655,169
import caffe_parser import numpy as np def read_caffe_mean(caffe_mean_file): """ Reads caffe formatted mean file :param caffe_mean_file: path to caffe mean file, presumably with 'binaryproto' suffix :return: mean image, converted from BGR to RGB format """ mean_blob = caffe_parser.caffe_pb2.BlobProto() with open(caffe_mean_file, 'rb') as f: mean_blob.ParseFromString(f.read()) img_mean_np = np.array(mean_blob.data) img_mean_np = img_mean_np.reshape(mean_blob.channels, mean_blob.height, mean_blob.width) # swap channels from Caffe BGR to RGB img_mean_np[[0, 2], :, :] = img_mean_np[[2, 0], :, :] return img_mean_np
6835bf429f3caca6308db450bb18e9254ff2b9a0
3,655,171
def estimate_pauli_sum(pauli_terms, basis_transform_dict, program, variance_bound, quantum_resource, commutation_check=True, symmetrize=True, rand_samples=16): """ Estimate the mean of a sum of pauli terms to set variance The sample variance is calculated by .. math:: \begin{align} \mathrm{Var}[\hat{\langle H \rangle}] = \sum_{i, j}h_{i}h_{j} \mathrm{Cov}(\hat{\langle P_{i} \rangle}, \hat{\langle P_{j} \rangle}) \end{align} The expectation value of each Pauli operator (term and coefficient) is also returned. It can be accessed through the named-tuple field `pauli_expectations'. :param pauli_terms: list of pauli terms to measure simultaneously or a PauliSum object :param basis_transform_dict: basis transform dictionary where the key is the qubit index and the value is the basis to rotate into. Valid basis is [I, X, Y, Z]. :param program: program generating a state to sample from. The program is deep copied to ensure no mutation of gates or program is perceived by the user. :param variance_bound: Bound on the variance of the estimator for the PauliSum. Remember this is the SQUARE of the standard error! :param quantum_resource: quantum abstract machine object :param Bool commutation_check: Optional flag toggling a safety check ensuring all terms in `pauli_terms` commute with each other :param Bool symmetrize: Optional flag toggling symmetrization of readout :param Int rand_samples: number of random realizations for readout symmetrization :return: estimated expected value, expected value of each Pauli term in the sum, covariance matrix, variance of the estimator, and the number of shots taken. The objected returned is a named tuple with field names as follows: expected_value, pauli_expectations, covariance, variance, n_shots. `expected_value' == coef_vec.dot(pauli_expectations) :rtype: EstimationResult """ if not isinstance(pauli_terms, (list, PauliSum)): raise TypeError("pauli_terms needs to be a list or a PauliSum") if isinstance(pauli_terms, PauliSum): pauli_terms = pauli_terms.terms # check if each term commutes with everything if commutation_check: if len(commuting_sets(sum(pauli_terms))) != 1: raise CommutationError("Not all terms commute in the expected way") program = program.copy() pauli_for_rotations = PauliTerm.from_list( [(value, key) for key, value in basis_transform_dict.items()]) program += get_rotation_program(pauli_for_rotations) qubits = sorted(list(basis_transform_dict.keys())) if symmetrize: theta = program.declare("ro_symmetrize", "REAL", len(qubits)) for (idx, q) in enumerate(qubits): program += [RZ(np.pi/2, q), RY(theta[idx], q), RZ(-np.pi/2, q)] ro = program.declare("ro", "BIT", memory_size=len(qubits)) for num, qubit in enumerate(qubits): program.inst(MEASURE(qubit, ro[num])) coeff_vec = np.array( list(map(lambda x: x.coefficient, pauli_terms))).reshape((-1, 1)) # upper bound on samples given by IV of arXiv:1801.03524 num_sample_ubound = 10 * int(np.ceil(np.sum(np.abs(coeff_vec))**2 / variance_bound)) if num_sample_ubound <= 2: raise ValueError("Something happened with our calculation of the max sample") if symmetrize: if min(STANDARD_NUMSHOTS, num_sample_ubound)//rand_samples == 0: raise ValueError(f"The number of shots must be larger than {rand_samples}.") program = program.wrap_in_numshots_loop(min(STANDARD_NUMSHOTS, num_sample_ubound)//rand_samples) else: program = program.wrap_in_numshots_loop(min(STANDARD_NUMSHOTS, num_sample_ubound)) binary = quantum_resource.compiler.native_quil_to_executable(basic_compile(program)) results = None sample_variance = np.infty number_of_samples = 0 tresults = np.zeros((0, len(qubits))) while (sample_variance > variance_bound and number_of_samples < num_sample_ubound): if symmetrize: # for some number of times sample random bit string for r in range(rand_samples): rand_flips = np.random.randint(low=0, high=2, size=len(qubits)) temp_results = quantum_resource.run(binary, memory_map={'ro_symmetrize': np.pi * rand_flips}) tresults = np.vstack((tresults, rand_flips ^ temp_results)) else: tresults = quantum_resource.run(binary) number_of_samples += len(tresults) parity_results = get_parity(pauli_terms, tresults) # Note: easy improvement would be to update mean and variance on the fly # instead of storing all these results. if results is None: results = parity_results else: results = np.hstack((results, parity_results)) # calculate the expected values.... covariance_mat = np.cov(results, ddof=1) sample_variance = coeff_vec.T.dot(covariance_mat).dot(coeff_vec) / (results.shape[1] - 1) return EstimationResult(expected_value=coeff_vec.T.dot(np.mean(results, axis=1)), pauli_expectations=np.multiply(coeff_vec.flatten(), np.mean(results, axis=1).flatten()), covariance=covariance_mat, variance=sample_variance, n_shots=results.shape[1])
8dc63069229cf83164196c1ca2e29d20c5be2756
3,655,172
def GetQuasiSequenceOrderp(ProteinSequence, maxlag=30, weight=0.1, distancematrix={}): """ ############################################################################### Computing quasi-sequence-order descriptors for a given protein. [1]:Kuo-Chen Chou. Prediction of Protein Subcellar Locations by Incorporating Quasi-Sequence-Order Effect. Biochemical and Biophysical Research Communications 2000, 278, 477-483. Usage: result = GetQuasiSequenceOrderp(protein,maxlag,weight,distancematrix) Input: protein is a pure protein sequence maxlag is the maximum lag and the length of the protein should be larger than maxlag. default is 30. weight is a weight factor. please see reference 1 for its choice. default is 0.1. distancematrix is a dict form containing 400 distance values Output: result is a dict form containing all quasi-sequence-order descriptors ############################################################################### """ result = dict() result.update(GetQuasiSequenceOrder1(ProteinSequence, maxlag, weight, distancematrix)) result.update(GetQuasiSequenceOrder2(ProteinSequence, maxlag, weight, distancematrix)) return result
f59c60826e2dc40db6827ac263423cf69c338d89
3,655,173
def check(lst: list, search_element: int) -> bool: """Check if the list contains the search_element.""" return any([True for i in lst if i == search_element])
15f35ceff44e9fde28f577663e79a2216ffce148
3,655,174
def halfcube(random_start=0,random_end=32,halfwidth0=1,pow=-1): """ Produce a halfcube with given dimension and decaying power :param random_start: decay starting parameter :param random_end: decay ending parameter :param halfwidth0: base halfwidth :param pow: decaying power :return: A (random_end-random_start,) array """ ran=np.arange(random_start,random_end,dtype=float) ran[0]=1.0 return ran**pow*halfwidth0
eb3acfe76abf2ba2ddec73973a875ad7509cd265
3,655,175
def valid_passphrase(module, **kwargs): """Tests whether the given passphrase is valid for the specified device. Return: <boolean> <error>""" for req in ["device", "passphrase"]: if req not in kwargs or kwargs[req] is None: errmsg = "valid_passphrase: {0} is a required parameter".format(req) return False, {"msg": errmsg} is_keyfile = kwargs.get("is_keyfile", False) slot = kwargs.get("slot", None) args = ["cryptsetup", "open", "--test-passphrase", kwargs["device"]] if slot is not None: args.extend(["--key-slot", str(slot)]) _unused, err = run_cryptsetup( module, args, passphrase=kwargs["passphrase"], is_keyfile=is_keyfile ) if err: errmsg = "valid_passphrase: We need a valid passphrase for {0}".format( kwargs["device"] ) return False, {"msg": errmsg, "err": err} return True, None
c6355a4c75b8973372b5d01817fa59569746ed6c
3,655,176
def contract_address(deploy_hash_base16: str, fn_store_id: int) -> bytes: """ Should match what the EE does (new_function_address) //32 bytes for deploy hash + 4 bytes ID blake2b256( [0;32] ++ [0;4] ) deploy_hash ++ fn_store_id """ def hash(data: bytes) -> bytes: h = blake2b(digest_size=32) h.update(data) return h.digest() deploy_hash_bytes = bytes.fromhex(deploy_hash_base16) counter_bytes = fn_store_id.to_bytes(4, "little") data = deploy_hash_bytes + counter_bytes return hash(data)
0623209f88a59c1a2cbe8460603f920ff66575f1
3,655,177
import json def dump_js_escaped_json(obj, cls=EdxJSONEncoder): """ JSON dumps and escapes objects that are safe to be embedded in JavaScript. Use this for anything but strings (e.g. dicts, tuples, lists, bools, and numbers). For strings, use js_escaped_string. The output of this method is also usable as plain-old JSON. Usage: Used as follows in a Mako template inside a <SCRIPT> tag:: var json_obj = ${obj | n, dump_js_escaped_json} If you must use the cls argument, then use as follows:: var json_obj = ${dump_js_escaped_json(obj, cls) | n} Use the "n" Mako filter above. It is possible that the default filter may include html escaping in the future, and this ensures proper escaping. Ensure ascii in json.dumps (ensure_ascii=True) allows safe skipping of Mako's default filter decode.utf8. Arguments: obj: The object soon to become a JavaScript escaped JSON string. The object can be anything but strings (e.g. dicts, tuples, lists, bools, and numbers). cls (class): The JSON encoder class (defaults to EdxJSONEncoder). Returns: (string) Escaped encoded JSON. """ obj = list(obj) if isinstance(obj, type({}.values())) else obj # lint-amnesty, pylint: disable=isinstance-second-argument-not-valid-type, dict-values-not-iterating, line-too-long json_string = json.dumps(obj, ensure_ascii=True, cls=cls) json_string = _escape_json_for_js(json_string) return json_string
eba36fbf101c0779fe5756fa6fbfe8f0d2c5686c
3,655,178
from typing import NamedTuple def RawTuple(num_fields, name_prefix='field'): """ Creates a tuple of `num_field` untyped scalars. """ assert isinstance(num_fields, int) assert num_fields >= 0 return NamedTuple(name_prefix, *([np.void] * num_fields))
3287a827099098e1550141e9e99321f75a9317f6
3,655,179
def pose2mat(R, p): """ convert pose to transformation matrix """ p0 = p.ravel() H = np.block([ [R, p0[:, np.newaxis]], [np.zeros(3), 1] ]) return H
626cbfcf5c188d4379f60b0e2d7b399aece67e8c
3,655,180
def _fill_missing_values(df=None): """replace missing values with NaN""" # fills in rows where lake refroze in same season df['WINTER'].replace(to_replace='"', method='ffill', inplace=True) # use nan as the missing value for headr in ['DAYS', 'OPENED', 'CLOSED']: df[headr].replace(to_replace=['-', '--', '---'], value=_np.nan, inplace=True) return df.sort_values(by=['WINTER'])
b86bfdac06d6e22c47d7b905d9cb7b5feba40fdb
3,655,181
def csi_prelu(data, alpha, axis, out_dtype, q_params, layer_name=""): """Quantized activation relu. Parameters ---------- data : relay.Expr The quantized input data. alpha : relay.Expr The quantized alpha. out_dtype : str Specifies the output data type for mixed precision dense can be uint8. Returns ------- result : relay.Expr The computed result. """ return _make.CSIPRelu(data, alpha, axis, out_dtype, q_params, layer_name)
8e8e3c09ae7c3cb3b89f1229cfb83836682e5bc8
3,655,182
def json(filename): """Returns the parsed contents of the given JSON fixture file.""" content = contents(filename) return json_.loads(content)
fc66afdfe1a04ac8ef65272e55283de98c145f8c
3,655,183
def _parse_assayData(assayData, assay): """Parse Rpy2 assayData (Environment object) assayData: Rpy2 Environment object. assay: An assay name indicating the data to be loaded. Return a parsed expression dataframe (Pandas). """ pandas2ri.activate() mat = assayData[assay] # rpy2 expression matrix object data = pandas2ri.ri2py(mat) features = pandas2ri.ri2py(r.rownames(mat)) samples = pandas2ri.ri2py(r.colnames(mat)) return pd.DataFrame(data, index=features, columns=samples)
aea2e5fe25eaf563fdd7ed981d7486fdf39098b4
3,655,184
def method_list(): """ list of available electronic structure methods """ return theory.METHOD_LST
d1ad84bc709db1973f83e5a743bf4aed21f98652
3,655,185
def readReadQualities(fastqfile): """ Reads a .fastqfile and calculates a defined readscore input: fastq file output: fastq dictionary key = readid; value = qualstr @type fastqfile: string @param fastqfile: path to fastq file @rtype: dictionary @return: dictionary containing read ids and read qualities. """ fastq_file = HTSeq.FastqReader(fastqfile , "phred") readdictionary = {} for read in fastq_file: readdictionary[read.name.split()[0]] = ComputeRQScore(read.qualstr) print("\tReading Fastq file done!") return readdictionary
8af9c0fc0c2d8f3a4d2f93ef81489098cb572643
3,655,186
from typing import Optional from typing import Any from typing import Dict async def default_field_resolver( parent: Optional[Any], args: Dict[str, Any], ctx: Optional[Any], info: "ResolveInfo", ) -> Any: """ Default callable to use as resolver for field which doesn't implement a custom one. :param parent: default root value or field parent value :param args: computed arguments related to the resolved field :param ctx: context passed to the query execution :param info: information related to the execution and the resolved field :type parent: Optional[Any] :type args: Dict[str, Any] :type ctx: Optional[Any] :type info: ResolveInfo :return: the computed field value :rtype: Any """ # pylint: disable=unused-argument try: return getattr(parent, info.field_name) except AttributeError: pass try: return parent[info.field_name] except (KeyError, TypeError): pass return None
d0458cc10a968c359c9165ac59be60ece0270c41
3,655,187
from typing import List from typing import Dict def revision_list_to_str(diffs: List[Dict]) -> str: """Convert list of diff ids to a comma separated list, prefixed with "D".""" return ', '.join([diff_to_str(d['id']) for d in diffs])
fbaa4473daf1e4b52d089c801f2db46aa7485972
3,655,188
from typing import Optional from pathlib import Path import time def get_path_of_latest_file() -> Optional[Path]: """Gets the path of the latest produced file that contains weight information""" path = Path(storage_folder) latest_file = None time_stamp_latest = -1 for entry in path.iterdir(): if entry.is_file(): if latest_file == None: latest_file = entry time_stamp_latest = time.mktime(get_time_tuple_from_filename(entry.name)) else: time_stamp_latest = time.mktime(get_time_tuple_from_filename(latest_file.name)) time_stamp_current = time.mktime(get_time_tuple_from_filename(entry.name)) if time_stamp_current > time_stamp_latest: latest_file = entry # print_d(f"Latest file: {latest_file}") return latest_file
a72308c4b3852429d6959552cc90779a4ee03dc5
3,655,189
def index(): """ A function than returns the home page when called upon """ #get all available news sources news_sources = get_sources() #get all news articles available everything = get_everything() print(everything) # title = 'Home - Find all the current news at your convinience' return render_template('index.html', news_sources = news_sources, everything = everything)
4cf1eacaa550ce1ffd0ed954b60a7c7adf1702a6
3,655,190
def blur(img): """ :param img: SimpleImage, the input image :return: the processed image which is blurred the function calculate the every position and its neighbors' pixel color and then average then set it as the new pixel's RGB """ sum_red = 0 sum_blue = 0 sum_green = 0 neighbors = 0 new_img = SimpleImage.blank(img.width, img.height) for x in range(img.width): for y in range(img.height): new_pixel = new_img.get_pixel(x, y) for i in range(-1, 2): for j in range(-1, 2): if x + i >= 0 and x+i <= img.width -1 and y + j >= 0 and y + j <= img.height -1: sum_red += img.get_pixel(x + i, y + j).red sum_blue += img.get_pixel(x + i, y + j).blue sum_green += img.get_pixel(x + i, y + j).green neighbors += 1 new_pixel.red = sum_red // neighbors new_pixel.blue = sum_blue // neighbors new_pixel.green = sum_green // neighbors neighbors = 0 sum_red = 0 sum_blue = 0 sum_green = 0 return new_img
a4b9e98e97b7d4a27b76b8151fa91493b58fc4a6
3,655,192
def delete_project_api_document_annotations_url(document_id: int, annotation_id: int) -> str: """ Delete the annotation of a document. :param document_id: ID of the document as integer :param annotation_id: ID of the annotation as integer :return: URL to delete annotation of a document """ return f'{KONFUZIO_HOST}/api/projects/{KONFUZIO_PROJECT_ID}/docs/{document_id}/' f'annotations/{annotation_id}/'
470a9bb602c4a9327839f811f818e469747e6758
3,655,193
def GetHomeFunctorViaPose(): """ Deprecated. Returns a function that will move the robot to the home position when called. """ js_home = GetPlanToHomeService() req = ServoToPoseRequest() pose_home = GetHomePoseKDL() req.target = pm.toMsg(pose_home) open_gripper = GetOpenGripperService() move = GetPlanToPoseService() servo_mode = GetServoModeService() def home(): rospy.loginfo("HOME: set servo mode") servo_mode("servo") rospy.loginfo("HOME: open gripper to drop anything") open_gripper() rospy.loginfo("HOME: move to config home") max_tries = 10 tries = 0 res1 = None while tries < max_tries and (res1 is None or "failure" in res1.ack.lower()): res1 = js_home(ServoToPoseRequest()) tries += 1 if res1 is None or "failure" in res1.ack.lower(): rospy.logerr(res1.ack) raise RuntimeError("HOME(): error moving to home1: " + str(res1.ack)) rospy.loginfo("HOME: move to pose over objects") res2 = None tries = 0 while tries < max_tries and (res2 is None or "failure" in res2.ack.lower()): res2 = move(req) tries += 1 if res2 is None or "failure" in res2.ack.lower(): rospy.logerr("move failed:" + str(res2.ack)) raise RuntimeError("HOME(): error moving to pose over workspace" + str(res2.ack)) rospy.loginfo("HOME: done") return home
7160f326c1aa0249da16dbfbf6fd740774284a4a
3,655,195
import requests def getAveragePlatPrice(item_name): """ Get the current average price of the item on the Warframe marketplace. Args: item_name (str): The name of the item. Returns: float: the average platinum market price of the item. """ avg_price = -1 item_name = clean(item_name) item_info = requests.get(API + item_name.replace(" ", "_") + "/statistics").json() try: avg_price = item_info["payload"]["statistics_closed"]["48hours"][0]['avg_price'] except KeyError: print(item_name + " is not listed on warframe.market.") return avg_price
221abd20125df49f40cfe246869a321943c5afbc
3,655,196
def mode_strength(n, kr, sphere_type='rigid'): """Mode strength b_n(kr) for an incident plane wave on sphere. Parameters ---------- n : int Degree. kr : array_like kr vector, product of wavenumber k and radius r_0. sphere_type : 'rigid' or 'open' Returns ------- b_n : array_like Mode strength b_n(kr). References ---------- Rafaely, B. (2015). Fundamentals of Spherical Array Processing. Springer. eq. (4.4) and (4.5). """ if sphere_type == 'open': b_n = 4*np.pi*1j**n * scyspecial.spherical_jn(n, kr) elif sphere_type == 'rigid': b_n = 4*np.pi*1j**n * (scyspecial.spherical_jn(n, kr) - (scyspecial.spherical_jn(n, kr, True) / spherical_hn2(n, kr, True)) * spherical_hn2(n, kr)) else: raise ValueError('sphere_type Not implemented.') return b_n
888981e34d444934e1c4b3d25c3042deabbe5005
3,655,197
from pathlib import Path def data_dir(test_dir: Path) -> Path: """ Create a directory for storing the mock data set. """ _data_dir = test_dir / 'data' _data_dir.mkdir(exist_ok=True) return _data_dir
3b204816252a2c87698197a416a4e2de218f639d
3,655,198
import multiprocessing def get_runtime_brief(): """ A digest version of get_runtime to be used more frequently """ return {"cpu_count": multiprocessing.cpu_count()}
9dbb54c476d303bae401d52ce76197e094ee5d71
3,655,199
def dict_compare(d1, d2): """ compares all differences between 2x dicts. returns sub-dicts: "added", "removed", "modified", "same" """ d1_keys = set(d1.keys()) d2_keys = set(d2.keys()) intersect_keys = d1_keys.intersection(d2_keys) added = d1_keys - d2_keys removed = d2_keys - d1_keys modified = {o: (d1[o], d2[o]) for o in intersect_keys if d1[o] != d2[o]} same = set(o for o in intersect_keys if d1[o] == d2[o]) return added, removed, modified, same
284368eade7de1e1abfd629ea903f6dff113e279
3,655,200
from datetime import datetime def toLocalTime(seconds, microseconds=0): """toLocalTime(seconds, microseconds=0) -> datetime Converts the given number of seconds since the GPS Epoch (midnight on January 6th, 1980) to this computer's local time. Returns a Python datetime object. Examples: >>> toLocalTime(0) datetime.datetime(1980, 1, 6, 0, 0) >>> toLocalTime(25 * 86400) datetime.datetime(1980, 1, 31, 0, 0) """ delta = datetime.timedelta(seconds=seconds, microseconds=microseconds) return GPS_Epoch + delta
9dd9352003b19b5e785766c7fb6e11716284c3ed
3,655,201
def get_part_01_answer(): """ Static method that will return the answer to Day01.01 :return: The product result :rtype: float """ return prod(summation_equals(puzzle_inputs, 2020, 2))
4fcc108bef3d0e5117caff4f02ff7797021a0efd
3,655,202
def eig_of_series(matrices): """Returns the eigenvalues and eigenvectors for a series of matrices. Parameters ---------- matrices : array_like, shape(n,m,m) A series of square matrices. Returns ------- eigenvalues : ndarray, shape(n,m) The eigenvalues of the matrices. eigenvectors : ndarray, shape(n,m,m) The eigenvectors of the matrices. """ s = matrices.shape eigenvalues = np.zeros((s[0], s[1]), dtype=np.complex) eigenvectors = np.zeros(s, dtype=np.complex) for i, A in enumerate(matrices): eVal, eVec = np.linalg.eig(matrices[i]) eigenvalues[i] = eVal eigenvectors[i] = eVec return eigenvalues, eigenvectors
fd34bdb1dc1458d0d495259a07572e0b0a2e685a
3,655,203
from re import T def injectable( cls: T = None, *, qualifier: str = None, primary: bool = False, namespace: str = None, group: str = None, singleton: bool = False, ) -> T: """ Class decorator to mark it as an injectable dependency. This decorator accepts customization parameters but can be invoked without the parenthesis when no parameter will be specified. .. note:: All files using this decorator will be executed when :meth:`load_injection_container <injectable.load_injection_container>` is invoked. :param cls: (cannot be explicitly passed) the decorated class. This will be automatically passed to the decorator by Python magic. :param qualifier: (optional) string qualifier for the injectable to be registered with. Defaults to None. :param primary: (optional) marks the injectable as primary for resolution in ambiguous cases. Defaults to False. :param namespace: (optional) namespace in which the injectable will be registered. Defaults to :const:`injectable.constants.DEFAULT_NAMESPACE`. :param group: (optional) group to be assigned to the injectable. Defaults to None. :param singleton: (optional) when True the injectable will be a singleton, i.e. only one instance of it will be created and shared globally. Defaults to False. Usage:: >>> from injectable import injectable >>> >>> @injectable ... class Foo: ... ... """ def decorator(klass: T, direct_call: bool = False) -> T: steps_back = 3 if direct_call else 2 caller_filepath = get_caller_filepath(steps_back) if caller_filepath == InjectionContainer.LOADING_FILEPATH: InjectionContainer._register_injectable( klass, caller_filepath, qualifier, primary, namespace, group, singleton ) return klass return decorator(cls, True) if cls is not None else decorator
06f1b6d5b3747d92e91cf751e62d90222e06d9a8
3,655,204
def build_graph(defined_routes): """ build the graph form route definitions """ G = {} for row in defined_routes: t_fk_oid = int(row["t_fk_oid"]) t_pk_oid = int(row["t_pk_oid"]) if not t_fk_oid in G: G[t_fk_oid] = {} if not t_pk_oid in G: G[t_pk_oid] = {} G[t_fk_oid][t_pk_oid] = row["routing_cost"] G[t_pk_oid][t_fk_oid] = row["routing_cost"] return G
16962ee1f4e336a9a1edc7cc05712113461f9a1a
3,655,205
def grando_transform_gauss_batch(batch_of_images, mean, variance): """Input: batch of images; type: ndarray: size: (batch, 784) Output: batch of images with gaussian nois; we use clip function to be assured that numbers in matrixs belong to interval (0,1); type: ndarray; size: (batch, 784); """ x = batch_of_images + np.random.normal(mean, variance, batch_of_images.shape) return x
4def857b315c425337d3edb6273af16f052cb1ba
3,655,206
def LF_DG_DISTANCE_SHORT(c): """ This LF is designed to make sure that the disease mention and the gene mention aren't right next to each other. """ return -1 if len(list(get_between_tokens(c))) <= 2 else 0
23b5450b844f91d6cae0993f2b7cda5c80460be1
3,655,208
def populate_user_flags(conf, args): """Populate a dictionary of configuration flag parameters, "conf", from values supplied on the command line in the structure, "args".""" if args.cflags: conf['cflags'] = args.cflags.split(sep=' ') if args.ldflags: conf['ldflags'] = args.ldflags.split(sep=' ') return conf
3f3fe64e2e352e0685a048747c9c8351575e40fb
3,655,209
def combine_raytrace(input_list): """ Produce a combined output from a list of raytrace outputs. """ profiler.start('combine_raytrace') output = dict() output['config'] = input_list[0]['config'] output['total'] = dict() output['total']['meta'] = dict() output['total']['image'] = dict() output['found'] = dict() output['found']['meta'] = dict() output['found']['history'] = dict() output['lost'] = dict() output['lost']['meta'] = dict() output['lost']['history'] = dict() num_iter = len(input_list) key_opt_list = list(input_list[0]['total']['meta'].keys()) key_opt_last = key_opt_list[-1] # Combine the meta data. for key_opt in key_opt_list: output['total']['meta'][key_opt] = dict() key_meta_list = list(input_list[0]['total']['meta'][key_opt].keys()) for key_meta in key_meta_list: output['total']['meta'][key_opt][key_meta] = 0 for ii_iter in range(num_iter): output['total']['meta'][key_opt][key_meta] += input_list[ii_iter]['total']['meta'][key_opt][key_meta] # Combine the images. for key_opt in key_opt_list: if key_opt in input_list[0]['total']['image']: if input_list[0]['total']['image'][key_opt] is not None: output['total']['image'][key_opt] = np.zeros(input_list[0]['total']['image'][key_opt].shape) for ii_iter in range(num_iter): output['total']['image'][key_opt] += input_list[ii_iter]['total']['image'][key_opt] else: output['total']['image'][key_opt] = None # Combine all the histories. if len(input_list[0]['found']['history']) > 0: final_num_found = 0 final_num_lost = 0 for ii_run in range(num_iter): final_num_found += len(input_list[ii_run]['found']['history'][key_opt_last]['mask']) final_num_lost += len(input_list[ii_run]['lost']['history'][key_opt_last]['mask']) rays_found_temp = RayArray() rays_found_temp.zeros(final_num_found) rays_lost_temp = RayArray() rays_lost_temp.zeros(final_num_lost) for key_opt in key_opt_list: output['found']['history'][key_opt] = rays_found_temp.copy() output['lost']['history'][key_opt] = rays_lost_temp.copy() index_found = 0 index_lost = 0 for ii_run in range(num_iter): num_found = len(input_list[ii_run]['found']['history'][key_opt_last]['mask']) num_lost = len(input_list[ii_run]['lost']['history'][key_opt_last]['mask']) for key_opt in key_opt_list: for key_ray in output['found']['history'][key_opt]: output['found']['history'][key_opt][key_ray][index_found:index_found + num_found] = ( input_list[ii_run]['found']['history'][key_opt][key_ray][:]) output['lost']['history'][key_opt][key_ray][index_lost:index_lost + num_lost] = ( input_list[ii_run]['lost']['history'][key_opt][key_ray][:]) index_found += num_found index_lost += num_lost profiler.stop('combine_raytrace') return output
2ed47dc60585e793bdc40fa39e04057c26db347d
3,655,210
def is_dict(): """Expects any dictionary""" return TypeMatcher(dict)
81bed05f5c8dae6ba3b8e77caa4d2d7777fb7ea9
3,655,211
import re def get_list_from_comma_separated_string(comma_separated_list): """ get a python list of resource names from comma separated list :param str comma_separated_list: :return: """ # remove all extra whitespace after commas and before/after string but NOT in between resource names removed_whitespace_str = re.sub(r"(,\s+)", ",", comma_separated_list).strip() resource_names = removed_whitespace_str.split(",") return resource_names
73df5fe431aceec0fec42d6019269a247b5587a5
3,655,213
def cci(series, window=14): """ compute commodity channel index """ price = typical_price(series) typical_mean = rolling_mean(price, window) res = (price - typical_mean) / (.015 * np.std(typical_mean)) return pd.Series(index=series.index, data=res)
b3eeabd4369fc1a041c3a714edc193deced804dc
3,655,214
def load_definition_from_string(qualified_module, cache=True): """Load a definition based on a fully qualified string. Returns: None or the loaded object Example: .. code-block:: python definition = load_definition_from_string('watson.http.messages.Request') request = definition() """ if qualified_module in definition_lookup and cache: return definition_lookup[qualified_module] parts = qualified_module.split('.') try: module = import_module('.'.join(parts[:-1])) obj = getattr(module, parts[-1:][0]) definition_lookup[qualified_module] = obj return obj except ImportError: raise
c435b4945878c5b5c2f5c7e252259da2be2345d0
3,655,215
import requests import logging def get_session(auth_mechanism, username, password, host): """Takes a username, password and authentication mechanism, logs into ICAT and returns a session ID""" # The ICAT Rest API does not accept json in the body of the HTTP request. # Instead it takes the form parameter 'json' with a string value - which is # the json-encoded data - eurrgh! The json-encoded data is sensitive to # order so we cannot pass a Python dictionary to the requests.post call as # Python dictionaries do not preserve order - eurrgh! So we construct a # string with the json data in the correct order - an OrderedDict may work # here - untested. (Also, dictionaries preserve order in Python 3.something) form_data = {'json': '{"plugin": "' + auth_mechanism + '", "credentials":[{"username":"' + username + '"}, {"password":"' + password + '"}]}'} session_url = host + "/icat/session" response = requests.post(session_url, data=form_data) if response.ok: return response.json()['sessionId'] else: logging.critical("Failed to get a session ID. Exiting.") log_response(response) raise RuntimeError()
2395751bc64300ae32c052e5a9aba04e50f7941f
3,655,216
from typing import List def _create_all_aux_operators(num_modals: List[int]) -> List[VibrationalOp]: """Generates the common auxiliary operators out of the given WatsonHamiltonian. Args: num_modals: the number of modals per mode. Returns: A list of VibrationalOps. For each mode the number of occupied modals will be evaluated. """ aux_second_quantized_ops_list = [] for mode in range(len(num_modals)): aux_second_quantized_ops_list.append(_create_occ_modals_per_mode(num_modals, mode)) return aux_second_quantized_ops_list
58a30221757bfcaa5c8b8790fd5556b060ecc8d1
3,655,217
from typing import List from typing import Concatenate def add_conv(X: tf.Tensor, filters: List[int], kernel_sizes: List[int], output_n_filters: int) -> tf.Tensor: """ Builds a single convolutional layer. :param X: input layer. :param filters: number of output filters in the convolution. :param kernel_sizes: list of lengths of the 1D convolution window. :param output_n_filters: number of 1D output filters. :return: output layer. """ # normalize the input X = BatchNormalization()(X) # add convolutions convs = [] for n_filters, kernel_size in zip(filters, kernel_sizes): conv = Conv1D(filters=n_filters, kernel_size=kernel_size, padding="same", activation="relu") convs.append(conv(X)) # concatenate all convolutions conc = Concatenate(axis=-1)(convs) conc = BatchNormalization()(conc) # dimensionality reduction conv = Conv1D(filters=output_n_filters, kernel_size=1, padding="same", activation="relu") return conv(conc)
69e907fc87780ce754c69500108dc00866fef716
3,655,218
def show_toast(view, message, timeout=DEFAULT_TIMEOUT, style=DEFAULT_STYLE): # type: (sublime.View, str, int, Dict[str, str]) -> Callable[[], None] """Show a toast popup at the bottom of the view. A timeout of -1 makes a "sticky" toast. """ messages_by_line = escape_text(message).splitlines() content = style_message("<br />".join(messages_by_line), style) # Order can matter here. If we calc width *after* visible_region we get # different results! width, _ = view.viewport_extent() visible_region = view.visible_region() last_row, _ = view.rowcol(visible_region.end()) line_start = view.text_point(last_row - 4 - len(messages_by_line), 0) vid = view.id() key = IDS() def on_hide(vid, key): if HIDE_POPUP_TIMERS.get(vid) == key: HIDE_POPUP_TIMERS.pop(vid, None) def __hide_popup(vid, key, sink): if HIDE_POPUP_TIMERS.get(vid) == key: HIDE_POPUP_TIMERS.pop(vid, None) sink() inner_hide_popup = show_popup( view, content, max_width=width * 2 / 3, location=line_start, on_hide=partial(on_hide, vid, key) ) HIDE_POPUP_TIMERS[vid] = key hide_popup = partial(__hide_popup, vid, key, inner_hide_popup) if timeout > 0: sublime.set_timeout(hide_popup, timeout) return hide_popup
910809b3efca6c1256af3540acbe42449080bebc
3,655,219