_id
stringlengths
2
7
title
stringlengths
1
88
partition
stringclasses
3 values
text
stringlengths
31
13.1k
language
stringclasses
1 value
meta_information
dict
q13400
generate_id
train
def generate_id(): """ Generate a 64bit base 16 ID for use as a Span or Trace ID """ global _current_pid pid = os.getpid() if _current_pid != pid:
python
{ "resource": "" }
q13401
package_version
train
def package_version(): """ Determine the version of this package. :return: String representing known version """ version = "" try: version =
python
{ "resource": "" }
q13402
strip_secrets
train
def strip_secrets(qp, matcher, kwlist): """ This function will scrub the secrets from a query param string based on the passed in matcher and kwlist. blah=1&secret=password&valid=true will result in blah=1&secret=<redacted>&valid=true You can even pass in path query combinations: /signup?blah=1&secret=password&valid=true will result in /signup?blah=1&secret=<redacted>&valid=true :param qp: a string representing the query params in URL form (unencoded) :param matcher: the matcher to use :param kwlist: the list of keywords to match :return: a scrubbed query param string """ path = None try: if qp is None: return '' if type(kwlist) is not list: logger.debug("strip_secrets: bad keyword list") return qp # If there are no key=values, then just return if not '=' in qp: return qp if '?' in qp: path, query = qp.split('?') else: query = qp params = parse.parse_qsl(query, keep_blank_values=True) redacted = ['<redacted>'] if matcher == 'equals-ignore-case': for keyword in kwlist: for index, kv in enumerate(params): if kv[0].lower() == keyword.lower(): params[index] = (kv[0], redacted) elif matcher == 'equals': for keyword in kwlist: for index, kv in enumerate(params): if kv[0] == keyword: params[index] = (kv[0], redacted) elif matcher == 'contains-ignore-case': for keyword in kwlist: for index, kv in enumerate(params): if keyword.lower() in kv[0].lower():
python
{ "resource": "" }
q13403
get_py_source
train
def get_py_source(file): """ Retrieves and returns the source code for any Python files requested by the UI via the host agent @param file [String] The fully qualified path to a file """ try: response = None pysource = "" if regexp_py.search(file) is None: response = {"error": "Only Python source files are allowed.
python
{ "resource": "" }
q13404
make_middleware
train
def make_middleware(app=None, *args, **kw): """ Given an app, return that app wrapped in iWSGIMiddleware """
python
{ "resource": "" }
q13405
eum_snippet
train
def eum_snippet(trace_id=None, eum_api_key=None, meta={}): """ Return an EUM snippet for use in views, templates and layouts that reports client side metrics to Instana that will automagically be linked to the current trace. @param trace_id [optional] the trace ID to insert into the EUM string @param eum_api_key [optional] the EUM API key from your Instana dashboard @param meta [optional] optional additional KVs you want reported with the EUM metrics @return string """ try: eum_file = open(os.path.dirname(__file__) + '/eum.js') eum_src = Template(eum_file.read()) # Prepare the standard required IDs ids = {} ids['meta_kvs'] = '' parent_span = tracer.active_span if trace_id or parent_span: ids['trace_id'] = trace_id or parent_span.trace_id else: # No trace_id passed in and tracer doesn't show an active span so # return nothing, nada & zip.
python
{ "resource": "" }
q13406
InstanaTracer.start_span
train
def start_span(self, operation_name=None, child_of=None, references=None, tags=None, start_time=None, ignore_active_span=False): "Taken from BasicTracer so we can override generate_id calls to ours" start_time = time.time() if start_time is None else start_time # See if we have a parent_ctx in `references` parent_ctx = None if child_of is not None: parent_ctx = ( child_of if isinstance(child_of, ot.SpanContext) else child_of.context) elif references is not None and len(references) > 0: # TODO only the first reference is currently used parent_ctx = references[0].referenced_context # retrieve the active SpanContext if not ignore_active_span and parent_ctx is None: scope = self.scope_manager.active if scope is not None: parent_ctx = scope.span.context # Assemble the child ctx gid = generate_id() ctx = SpanContext(span_id=gid) if parent_ctx is not None: if parent_ctx._baggage is not None:
python
{ "resource": "" }
q13407
InstanaTracer.__add_stack
train
def __add_stack(self, span, limit=None): """ Adds a backtrace to this span """ span.stack = [] frame_count = 0 tb = traceback.extract_stack() tb.reverse() for frame in tb: if limit is not None and frame_count >= limit: break # Exclude Instana frames unless we're in dev mode if "INSTANA_DEV" not in os.environ: if re_tracer_frame.search(frame[0]) is not None: continue
python
{ "resource": "" }
q13408
hook
train
def hook(module): """ Hook method to install the Instana middleware into Flask """ if "INSTANA_DEV" in os.environ: print("==============================================================") print("Instana: Running flask hook")
python
{ "resource": "" }
q13409
error_class_for_http_status
train
def error_class_for_http_status(status): """Return the appropriate `ResponseError` subclass for the given HTTP status code.""" try: return error_classes[status] except KeyError: def new_status_error(xml_response): if (status > 400 and status < 500):
python
{ "resource": "" }
q13410
ResponseError.response_doc
train
def response_doc(self): """The XML document received from the service.""" try: return self.__dict__['response_doc'] except KeyError:
python
{ "resource": "" }
q13411
ValidationError.transaction_error_code
train
def transaction_error_code(self): """The machine-readable error code for a transaction error.""" error = self.response_doc.find('transaction_error') if error is not None:
python
{ "resource": "" }
q13412
ValidationError.errors
train
def errors(self): """A dictionary of error objects, keyed on the name of the request field that was invalid. Each error value has `field`, `symbol`, and `message` attributes describing the particular invalidity of that field. """ try: return self.__dict__['errors'] except KeyError: pass suberrors = dict() for err in self.response_doc.findall('error'): field = err.attrib['field'] symbol = err.attrib['symbol'] message = err.text sub_err = self.Suberror(field, symbol, message)
python
{ "resource": "" }
q13413
objects_for_push_notification
train
def objects_for_push_notification(notification): """Decode a push notification with the given body XML. Returns a dictionary containing the constituent objects of the push notification. The kind of push notification is given
python
{ "resource": "" }
q13414
Account.invoice
train
def invoice(self, **kwargs): """Create an invoice for any outstanding adjustments this account has.""" url = urljoin(self._url, '/invoices') if kwargs: response = self.http_request(url, 'POST', Invoice(**kwargs), {'Content-Type': 'application/xml; charset=utf-8'})
python
{ "resource": "" }
q13415
Account.subscribe
train
def subscribe(self, subscription): """Create the given `Subscription` for this existing account."""
python
{ "resource": "" }
q13416
Account.update_billing_info
train
def update_billing_info(self, billing_info): """Change this account's billing information to the given `BillingInfo`.""" url = urljoin(self._url, '/billing_info') response = billing_info.http_request(url, 'PUT', billing_info, {'Content-Type': 'application/xml; charset=utf-8'}) if response.status == 200: pass elif response.status == 201: billing_info._url = response.getheader('Location') else:
python
{ "resource": "" }
q13417
Account.create_shipping_address
train
def create_shipping_address(self, shipping_address): """Creates a shipping address on an existing account. If you are creating an account, you can embed the shipping addresses with the
python
{ "resource": "" }
q13418
GiftCard.preview
train
def preview(self): """Preview the purchase of this gift card""" if hasattr(self, '_url'): url = self._url + '/preview' return self.post(url) else:
python
{ "resource": "" }
q13419
GiftCard.redeem
train
def redeem(self, account_code): """Redeem this gift card on the specified account code""" redemption_path = '%s/redeem' % (self.redemption_code) if hasattr(self, '_url'): url = urljoin(self._url, '/redeem') else:
python
{ "resource": "" }
q13420
Invoice.pdf
train
def pdf(cls, uuid): """Return a PDF of the invoice identified by the UUID This is a raw string, which can be written to a file with: ` with open('invoice.pdf', 'w') as invoice_file: invoice_file.write(recurly.Invoice.pdf(uuid)) `
python
{ "resource": "" }
q13421
Subscription.pause
train
def pause(self, remaining_pause_cycles): """Pause a subscription""" url = urljoin(self._url, '/pause') elem = ElementTreeBuilder.Element(self.nodename) elem.append(Resource.element_for_value('remaining_pause_cycles',
python
{ "resource": "" }
q13422
Subscription.create_usage
train
def create_usage(self, sub_add_on, usage): """Record the usage on the given subscription add on and update the usage object with returned xml"""
python
{ "resource": "" }
q13423
Transaction.get_refund_transaction
train
def get_refund_transaction(self): """Retrieve the refund transaction for this transaction, immediately after refunding. After calling `refund()` to refund a transaction, call this method to retrieve the new transaction representing the refund. """ try:
python
{ "resource": "" }
q13424
Transaction.refund
train
def refund(self, **kwargs): """Refund this transaction. Calling this method returns the refunded transaction (that is, ``self``) if the refund was successful, or raises a `ResponseError` if an error occurred requesting the refund. After a successful call to `refund()`, to retrieve the new transaction representing the refund, use the `get_refund_transaction()` method. """ # Find the URL and method to refund the transaction. try: selfnode = self._elem except AttributeError: raise AttributeError('refund') url, method = None, None
python
{ "resource": "" }
q13425
Plan.get_add_on
train
def get_add_on(self, add_on_code): """Return the `AddOn` for this plan with the given add-on code.""" url = urljoin(self._url, '/add_ons/%s' % (add_on_code,))
python
{ "resource": "" }
q13426
Plan.create_add_on
train
def create_add_on(self, add_on): """Make the given `AddOn` available to
python
{ "resource": "" }
q13427
_load_track_estimates
train
def _load_track_estimates(track, estimates_dir, output_dir): """load estimates from disk instead of processing""" user_results = {} track_estimate_dir = os.path.join( estimates_dir, track.subset, track.name ) for target in glob.glob( track_estimate_dir + '/*.wav' ): target_name = op.splitext( os.path.basename(target) )[0] try: target_audio, _ = sf.read(
python
{ "resource": "" }
q13428
eval_dir
train
def eval_dir( reference_dir, estimates_dir, output_dir=None, mode='v4', win=1.0, hop=1.0, ): """Compute bss_eval metrics for two given directories assuming file names are identical for both, reference source and estimates. Parameters ---------- reference_dir : str path to reference sources directory. estimates_dir : str path to estimates directory. output_dir : str path to output directory used to save evaluation results. Defaults to `None`, meaning no evaluation files will be saved. mode : str bsseval version number. Defaults to 'v4'. win : int window size in Returns ------- scores : EvalStore scores object that holds the framewise and global evaluation scores. """ reference = [] estimates = [] data = EvalStore(win=win, hop=hop) global_rate = None reference_glob = os.path.join(reference_dir, '*.wav') # Load in each reference file in the supplied dir for reference_file in glob.glob(reference_glob): ref_audio, rate = sf.read( reference_file, always_2d=True ) # Make sure fs is the same for all files assert (global_rate is None or rate == global_rate) global_rate = rate reference.append(ref_audio) if not reference: raise ValueError('`reference_dir` contains no wav files')
python
{ "resource": "" }
q13429
eval_mus_dir
train
def eval_mus_dir( dataset, estimates_dir, output_dir=None, *args, **kwargs ): """Run musdb.run for the purpose of evaluation of musdb estimate dir Parameters ---------- dataset : DB(object) Musdb Database object. estimates_dir : str Path to estimates folder. output_dir : str Output folder where evaluation json files are stored. *args Variable length argument list for `musdb.run()`. **kwargs Arbitrary keyword arguments for `musdb.run()`. """ # create a new musdb instance for estimates with the same file structure est = musdb.DB(root_dir=estimates_dir, is_wav=True) # load all estimates track_names est_tracks = est.load_mus_tracks() # get a list of track names tracknames = [t.name for t in est_tracks]
python
{ "resource": "" }
q13430
eval_mus_track
train
def eval_mus_track( track, user_estimates, output_dir=None, mode='v4', win=1.0, hop=1.0 ): """Compute all bss_eval metrics for the musdb track and estimated signals, given by a `user_estimates` dict. Parameters ---------- track : Track musdb track object loaded using musdb estimated_sources : Dict dictionary, containing the user estimates as np.arrays. output_dir : str path to output directory used to save evaluation results. Defaults to `None`, meaning no evaluation files will be saved. mode : str bsseval version number. Defaults to 'v4'. win : int window size in Returns ------- scores : EvalStore scores object that holds the framewise and global evaluation scores. """ audio_estimates = [] audio_reference = [] # make sure to always build the list in the same order # therefore track.targets is an OrderedDict eval_targets = [] # save the list of target names to be evaluated for key, target in list(track.targets.items()): try: # try to fetch the audio from the user_results of a given key user_estimates[key] except KeyError: # ignore wrong key and continue continue # append this target name to the list of target to evaluate eval_targets.append(key) data = EvalStore(win=win, hop=hop) # check if vocals and accompaniment is among the targets has_acc = all(x in eval_targets for x in ['vocals', 'accompaniment']) if has_acc: # remove accompaniment from list of targets, because # the voc/acc scenario will be evaluated separately eval_targets.remove('accompaniment') if len(eval_targets) >= 2: # compute evaluation of remaining targets for target in eval_targets: audio_estimates.append(user_estimates[target]) audio_reference.append(track.targets[target].audio) SDR, ISR, SIR, SAR = evaluate( audio_reference, audio_estimates, win=int(win*track.rate), hop=int(hop*track.rate), mode=mode ) # iterate over all evaluation results except for vocals for i, target in enumerate(eval_targets): if target == 'vocals' and has_acc: continue values = { "SDR": SDR[i].tolist(), "SIR": SIR[i].tolist(),
python
{ "resource": "" }
q13431
evaluate
train
def evaluate( references, estimates, win=1*44100, hop=1*44100, mode='v4', padding=True ): """BSS_EVAL images evaluation using metrics module Parameters ---------- references : np.ndarray, shape=(nsrc, nsampl, nchan) array containing true reference sources estimates : np.ndarray, shape=(nsrc, nsampl, nchan) array containing estimated sources window : int, defaults to 44100 window size in samples hop : int hop size in samples, defaults to 44100 (no overlap) mode : str BSSEval version, default to `v4` Returns ------- SDR : np.ndarray, shape=(nsrc,) vector of Signal to Distortion Ratios (SDR) ISR : np.ndarray, shape=(nsrc,) vector of Source to Spatial Distortion Image (ISR) SIR : np.ndarray, shape=(nsrc,)
python
{ "resource": "" }
q13432
EvalStore._q
train
def _q(self, number, precision='.00001'): """quantiztion of BSSEval values""" if np.isinf(number):
python
{ "resource": "" }
q13433
bsseval
train
def bsseval(inargs=None): """ Generic cli app for bsseval results. Expects two folder with """ parser = argparse.ArgumentParser() parser.add_argument( 'reference_dir', type=str ) parser.add_argument( 'estimates_dir', type=str ) parser.add_argument('-o', help='output_dir') parser.add_argument( '--win', type=float, help='Window size in seconds', default=1.0 ) parser.add_argument( '--hop', type=float, help='Hop size in seconds', default=1.0 ) parser.add_argument( '-m', type=str, help='bss_eval version [`v3`, `v4`]', default='v4' ) parser.add_argument( '--version', '-v', action='version',
python
{ "resource": "" }
q13434
museval
train
def museval(inargs=None): """ Commandline interface for museval evaluation tools """ parser = argparse.ArgumentParser() parser.add_argument( 'estimates_dir', type=str ) parser.add_argument('-o', help='output_dir') parser.add_argument('--cpu', type=int, help='number of cpus', default=4) parser.add_argument( '-p', help='enable multiprocessing', action='store_true', ) parser.add_argument( '--musdb', help='path to musdb', type=str ) parser.add_argument( '--iswav', help='Read musdb wav instead of stems', action='store_true', ) parser.add_argument( '--version', '-v', action='version',
python
{ "resource": "" }
q13435
Page.next_page
train
def next_page(self): """Return the next `Page` after this one in the result sequence it's from. If the current page is the last page in the sequence, calling this method raises a `ValueError`. """ try:
python
{ "resource": "" }
q13436
Page.first_page
train
def first_page(self): """Return the first `Page` in the result sequence this `Page` instance is from. If the current page is already the first page in the sequence, calling this method raises a `ValueError`. """ try:
python
{ "resource": "" }
q13437
Page.page_for_url
train
def page_for_url(cls, url): """Return a new `Page` containing the items at the given endpoint URL.""" resp, elem = Resource.element_for_url(url)
python
{ "resource": "" }
q13438
Page.page_for_value
train
def page_for_value(cls, resp, value): """Return a new `Page` representing the given resource `value` retrieved using the HTTP response `resp`. This method records pagination ``Link`` headers present in `resp`, so that the returned `Page` can return their resources from its `next_page()` and `first_page()` methods. """ page
python
{ "resource": "" }
q13439
Resource.serializable_attributes
train
def serializable_attributes(self): """ Attributes to be serialized in a ``POST`` or ``PUT`` request. Returns all attributes unless a blacklist is specified """ if hasattr(self, 'blacklist_attributes'): return [attr
python
{ "resource": "" }
q13440
Resource.headers_as_dict
train
def headers_as_dict(cls, resp): """Turns an array of response headers into a dictionary""" if six.PY2: pairs = [header.split(':', 1) for header in resp.msg.headers]
python
{ "resource": "" }
q13441
Resource.as_log_output
train
def as_log_output(self): """Returns an XML string containing a serialization of this instance suitable for logging. Attributes named in the instance's `sensitive_attributes` are redacted. """ elem = self.to_element()
python
{ "resource": "" }
q13442
Resource.get
train
def get(cls, uuid): """Return a `Resource` instance of this class identified by the given code or UUID. Only `Resource` classes with specified `member_path` attributes can be directly requested with this method. """ if not uuid:
python
{ "resource": "" }
q13443
Resource.headers_for_url
train
def headers_for_url(cls, url): """Return the headers only for the given URL as a dict""" response = cls.http_request(url, method='HEAD') if response.status != 200:
python
{ "resource": "" }
q13444
Resource.value_for_element
train
def value_for_element(cls, elem): """Deserialize the given XML `Element` into its representative value. Depending on the content of the element, the returned value may be: * a string, integer, or boolean value * a `datetime.datetime` instance * a list of `Resource` instances * a single `Resource` instance * a `Money` instance * ``None`` """ log = logging.getLogger('recurly.resource') if elem is None: log.debug("Converting %r element into None value", elem) return if elem.attrib.get('nil') is not None: log.debug("Converting %r element with nil attribute into None value", elem.tag) return if elem.tag.endswith('_in_cents') and 'currency' not in cls.attributes and not cls.inherits_currency: log.debug("Converting %r element in class with no matching 'currency' into a Money value", elem.tag) return Money.from_element(elem) attr_type = elem.attrib.get('type') log.debug("Converting %r element with type %r", elem.tag, attr_type) if attr_type == 'integer': return int(elem.text.strip()) if attr_type == 'float': return float(elem.text.strip()) if attr_type == 'boolean': return elem.text.strip() == 'true' if attr_type == 'datetime': return iso8601.parse_date(elem.text.strip())
python
{ "resource": "" }
q13445
Resource.element_for_value
train
def element_for_value(cls, attrname, value): """Serialize the given value into an XML `Element` with the given tag name, returning it. The value argument may be: * a `Resource` instance * a `Money` instance * a `datetime.datetime` instance * a string, integer, or boolean value * ``None`` * a list or tuple of these values """ if isinstance(value, Resource): if attrname in cls._classes_for_nodename: # override the child's node name with this attribute name return value.to_element(attrname) return value.to_element() el = ElementTreeBuilder.Element(attrname) if value is None: el.attrib['nil'] = 'nil' elif isinstance(value, bool): el.attrib['type'] = 'boolean' el.text = 'true' if value else 'false' elif isinstance(value, int): el.attrib['type'] = 'integer' el.text = str(value) elif isinstance(value, datetime):
python
{ "resource": "" }
q13446
Resource.update_from_element
train
def update_from_element(self, elem): """Reset this `Resource` instance to represent the values in the given XML element.""" self._elem = elem for attrname in self.attributes:
python
{ "resource": "" }
q13447
Resource.all
train
def all(cls, **kwargs): """Return a `Page` of instances of this `Resource` class from its general collection endpoint. Only `Resource` classes with specified `collection_path` endpoints can be requested with this method. Any provided keyword arguments are passed to the API endpoint as query parameters.
python
{ "resource": "" }
q13448
Resource.count
train
def count(cls, **kwargs): """Return a count of server side resources given filtering arguments in kwargs. """ url = recurly.base_uri() + cls.collection_path if kwargs:
python
{ "resource": "" }
q13449
Resource.put
train
def put(self, url): """Sends this `Resource` instance to the service with a ``PUT`` request to the given URL.""" response = self.http_request(url, 'PUT', self, {'Content-Type': 'application/xml; charset=utf-8'}) if response.status != 200: self.raise_http_error(response)
python
{ "resource": "" }
q13450
Resource.post
train
def post(self, url, body=None): """Sends this `Resource` instance to the service with a ``POST`` request to the given URL. Takes an optional body""" response = self.http_request(url, 'POST', body or self, {'Content-Type': 'application/xml; charset=utf-8'}) if response.status not in (200, 201, 204): self.raise_http_error(response)
python
{ "resource": "" }
q13451
Resource.delete
train
def delete(self): """Submits a deletion request for this `Resource` instance as a ``DELETE`` request
python
{ "resource": "" }
q13452
Resource.raise_http_error
train
def raise_http_error(cls, response): """Raise a `ResponseError` of the appropriate subclass in reaction to the given `http_client.HTTPResponse`.""" response_xml = response.read()
python
{ "resource": "" }
q13453
Resource.to_element
train
def to_element(self, root_name=None): """Serialize this `Resource` instance to an XML element.""" if not root_name: root_name = self.nodename elem = ElementTreeBuilder.Element(root_name) for attrname in self.serializable_attributes(): # Only use values that have been loaded into the internal # __dict__. For retrieved objects we look into the XML response at # access time, so the internal __dict__ contains only the elements # that have been set on the client side. try:
python
{ "resource": "" }
q13454
bss_eval_sources
train
def bss_eval_sources(reference_sources, estimated_sources, compute_permutation=True): """ BSS Eval v3 bss_eval_sources Wrapper to ``bss_eval`` with the right parameters. The call to this function is not recommended. See the description for the ``bsseval_sources`` parameter of ``bss_eval``. """ (sdr, isr, sir, sar, perm) = \ bss_eval(
python
{ "resource": "" }
q13455
bss_eval_sources_framewise
train
def bss_eval_sources_framewise(reference_sources, estimated_sources, window=30 * 44100, hop=15 * 44100, compute_permutation=False): """ BSS Eval v3 bss_eval_sources_framewise Wrapper to ``bss_eval`` with the right parameters. The call to this function is not recommended. See the description for the ``bsseval_sources`` parameter of ``bss_eval``. """ (sdr, isr, sir, sar,
python
{ "resource": "" }
q13456
bss_eval_images
train
def bss_eval_images(reference_sources, estimated_sources, compute_permutation=True): """ BSS Eval v3 bss_eval_images Wrapper to ``bss_eval`` with the right parameters. """ return bss_eval( reference_sources, estimated_sources, window=np.inf, hop=np.inf,
python
{ "resource": "" }
q13457
bss_eval_images_framewise
train
def bss_eval_images_framewise(reference_sources, estimated_sources, window=30 * 44100, hop=15 * 44100, compute_permutation=False): """ BSS Eval v3 bss_eval_images_framewise Framewise computation of bss_eval_images. Wrapper to ``bss_eval`` with the right parameters. """ return bss_eval(
python
{ "resource": "" }
q13458
_zeropad
train
def _zeropad(sig, N, axis=0): """pads with N zeros at the end of the signal, along given axis""" # ensures concatenation dimension is the first sig = np.moveaxis(sig, axis, 0) # zero pad
python
{ "resource": "" }
q13459
_compute_projection_filters
train
def _compute_projection_filters(G, sf, estimated_source): """Least-squares projection of estimated source on the subspace spanned by delayed versions of reference sources, with delays between 0 and filters_len-1 """ # epsilon eps = np.finfo(np.float).eps # shapes (nsampl, nchan) = estimated_source.shape # handles the case where we are calling this with only one source # G should be nsrc X nsrc X nchan X nchan X filters_len X filters_len # and sf should be nsrc X nchan X filters_len if len(G.shape) == 4: G = G[None, None, ...] sf = sf[None, ...] nsrc = G.shape[0] filters_len = G.shape[-1] # zero pad estimates and put chan in first dimension estimated_source = _zeropad(estimated_source.T, filters_len - 1, axis=1) # compute its FFT n_fft = int(2**np.ceil(np.log2(nsampl + filters_len - 1.))) sef = scipy.fftpack.fft(estimated_source, n=n_fft) # compute the cross-correlations between sources and estimates D = np.zeros((nsrc, nchan, filters_len, nchan)) for (j, cj, c) in itertools.product(
python
{ "resource": "" }
q13460
_project
train
def _project(reference_sources, C): """Project images using pre-computed filters C reference_sources are nsrc X nsampl X nchan C is nsrc X nchan X filters_len X nchan """ # shapes: ensure that input is 3d (comprising the source index) if len(reference_sources.shape) == 2: reference_sources = reference_sources[None, ...]
python
{ "resource": "" }
q13461
_safe_db
train
def _safe_db(num, den): """Properly handle the potential +Inf db SIR instead of raising a RuntimeWarning. """ if
python
{ "resource": "" }
q13462
construct_api_url
train
def construct_api_url(input, representation, resolvers=None, get3d=False, tautomers=False, xml=True, **kwargs): """Return the URL for the desired API endpoint. :param string input: Chemical identifier to resolve :param string representation: Desired output representation :param list(str) resolvers: (Optional) Ordered list of resolvers to use :param bool get3d: (Optional) Whether to return 3D coordinates (where applicable) :param bool tautomers: (Optional) Whether to return all tautomers :param bool xml: (Optional) Whether to return full XML response :returns: CIR API URL :rtype: str """ # File formats require representation=file and the format in the querystring if representation in FILE_FORMATS: kwargs['format'] = representation
python
{ "resource": "" }
q13463
request
train
def request(input, representation, resolvers=None, get3d=False, tautomers=False, **kwargs): """Make a request to CIR and return the XML response. :param string input: Chemical identifier to resolve :param string representation: Desired output representation :param list(string) resolvers: (Optional) Ordered list of resolvers to use :param bool get3d: (Optional) Whether to return 3D coordinates (where applicable) :param bool tautomers: (Optional) Whether to return all tautomers :returns: XML response from CIR
python
{ "resource": "" }
q13464
query
train
def query(input, representation, resolvers=None, get3d=False, tautomers=False, **kwargs): """Get all results for resolving input to the specified output representation. :param string input: Chemical identifier to resolve :param string representation: Desired output representation :param list(string) resolvers: (Optional) Ordered list of resolvers to use :param bool get3d: (Optional) Whether to return 3D coordinates (where applicable) :param bool tautomers: (Optional) Whether to return all tautomers :returns: List of resolved results :rtype: list(Result) :raises HTTPError: if CIR returns an error code :raises ParseError: if CIR response is uninterpretable """ tree = request(input, representation, resolvers, get3d, tautomers, **kwargs) results = [] for data in tree.findall('.//data'):
python
{ "resource": "" }
q13465
resolve
train
def resolve(input, representation, resolvers=None, get3d=False, **kwargs): """Resolve input to the specified output representation. :param string input: Chemical identifier to resolve :param string representation: Desired output representation :param list(string) resolvers: (Optional) Ordered list of resolvers to use :param bool get3d: (Optional) Whether to return 3D coordinates (where applicable) :returns: Output representation or None :rtype: string or None
python
{ "resource": "" }
q13466
resolve_image
train
def resolve_image(input, resolvers=None, fmt='png', width=300, height=300, frame=False, crop=None, bgcolor=None, atomcolor=None, hcolor=None, bondcolor=None, framecolor=None, symbolfontsize=11, linewidth=2, hsymbol='special', csymbol='special', stereolabels=False, stereowedges=True, header=None, footer=None, **kwargs): """Resolve input to a 2D image depiction. :param string input: Chemical identifier to resolve :param list(string) resolvers: (Optional) Ordered list of resolvers to use :param string fmt: (Optional) gif or png image format (default png) :param int width: (Optional) Image width in pixels (default 300) :param int height: (Optional) Image height in pixels (default 300) :param bool frame: (Optional) Whether to show border frame (default False) :param int crop: (Optional) Crop image with specified padding :param int symbolfontsize: (Optional) Atom label font size (default 11) :param int linewidth: (Optional) Bond line width (default 2) :param string bgcolor: (Optional) Background color :param string atomcolor: (Optional) Atom label color :param string hcolor: (Optional) Hydrogen atom label color :param string bondcolor: (Optional) Bond color :param string framecolor: (Optional) Border frame color :param bool hsymbol: (Optional) Hydrogens: all, special or none (default special) :param bool csymbol: (Optional) Carbons: all, special or none (default special) :param bool stereolabels: (Optional) Whether to show stereochemistry labels (default False) :param bool stereowedges: (Optional) Whether to show wedge/dash bonds (default True) :param string header: (Optional) Header text above structure :param string footer: (Optional) Footer text below structure """ # Aggregate all arguments into kwargs
python
{ "resource": "" }
q13467
download
train
def download(input, filename, representation, overwrite=False, resolvers=None, get3d=False, **kwargs): """Convenience function to save a CIR response as a file. This is just a simple wrapper around the resolve function. :param string input: Chemical identifier to resolve :param string filename: File path to save to :param string representation: Desired output representation :param bool overwrite: (Optional) Whether to allow overwriting of an existing file :param list(string) resolvers: (Optional) Ordered list of resolvers to use :param bool get3d: (Optional) Whether to return 3D coordinates (where applicable) :raises HTTPError: if CIR returns an error code :raises ParseError: if CIR response is uninterpretable :raises IOError: if overwrite is False and file already exists """ result = resolve(input, representation, resolvers, get3d, **kwargs)
python
{ "resource": "" }
q13468
Molecule.image_url
train
def image_url(self): """URL of a GIF image.""" return construct_api_url(self.input, 'image',
python
{ "resource": "" }
q13469
Molecule.twirl_url
train
def twirl_url(self): """Url of a TwirlyMol 3D viewer.""" return construct_api_url(self.input,
python
{ "resource": "" }
q13470
Molecule.download
train
def download(self, filename, representation, overwrite=False): """Download the resolved structure as a file. :param string filename: File path to save to :param string representation: Desired output representation :param bool overwrite: (Optional) Whether
python
{ "resource": "" }
q13471
DataSource.progress
train
def progress(self, loaded, total, msg=''): """ Notify on a progress change """ self.fire('progress', {
python
{ "resource": "" }
q13472
DataSource.raw
train
def raw(self, tag, raw, metadata): """ Create a raw response object """ raw = base64.b64encode(raw) return { 'type': 'raw',
python
{ "resource": "" }
q13473
assert_looks_like
train
def assert_looks_like(first, second, msg=None): """ Compare two strings if all contiguous whitespace is coalesced. """ first = _re.sub("\s+", " ", first.strip()) second = _re.sub("\s+", " ", second.strip())
python
{ "resource": "" }
q13474
load_feature
train
def load_feature(fname, language): """ Load and parse a feature file. """ fname =
python
{ "resource": "" }
q13475
run_steps
train
def run_steps(spec, language="en"): """ Can be called by the user from within a step definition to execute other steps. """ # The way this works is a little exotic, but I couldn't think of a better way to work around # the fact that this has to be a global function and therefore cannot know about which step # runner to use (other than making step runner global) # Find the step runner that is currently
python
{ "resource": "" }
q13476
StepsRunner.run_steps_from_string
train
def run_steps_from_string(self, spec, language_name='en'): """ Called from within step definitions to run other steps. """ caller = inspect.currentframe().f_back line = caller.f_lineno - 1 fname = caller.f_code.co_filename
python
{ "resource": "" }
q13477
simulate_async_event
train
def simulate_async_event(): """Simulate an asynchronous event.""" scc.state = 'executing' def async_event(result): """All other asynchronous events or function calls returned from later steps will wait until this callback fires.""" scc.state = result
python
{ "resource": "" }
q13478
hook_decorator
train
def hook_decorator(cb_type): """ Decorator to wrap hook definitions in. Registers hook. """ def decorator_wrapper(*tags_or_func): if len(tags_or_func) == 1 and callable(tags_or_func[0]): # No tags were passed to this decorator
python
{ "resource": "" }
q13479
StepImplLoader.load_steps_impl
train
def load_steps_impl(self, registry, path, module_names=None): """ Load the step implementations at the given path, with the given module names. If module_names is None then the module 'steps' is searched by default. """ if not module_names: module_names = ['steps'] path = os.path.abspath(path) for module_name in module_names: mod = self.modules.get((path, module_name)) if mod is None: #log.debug("Looking for step def module '%s' in %s" % (module_name, path)) cwd = os.getcwd() if cwd not in sys.path: sys.path.append(cwd) try: actual_module_name = os.path.basename(module_name) complete_path = os.path.join(path, os.path.dirname(module_name)) info = imp.find_module(actual_module_name, [complete_path]) except ImportError: #log.debug("Did not find step defs module '%s' in %s" % (module_name, path)) return try:
python
{ "resource": "" }
q13480
StepImplRegistry.find_step_impl
train
def find_step_impl(self, step): """ Find the implementation of the step for the given match string. Returns the StepImpl object corresponding to the implementation, and the arguments to the step implementation. If no implementation is found, raises UndefinedStepImpl. If more than one implementation is found, raises AmbiguousStepImpl. Each of the arguments returned will have been transformed by the first matching transform implementation. """ result = None for si in self.steps[step.step_type]:
python
{ "resource": "" }
q13481
run_command
train
def run_command(cmd): """ Open a child process, and return its exit status and stdout. """ child = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE, stdin=subprocess.PIPE, stdout=subprocess.PIPE) out
python
{ "resource": "" }
q13482
make_filter_list
train
def make_filter_list(filters): """Transform filters into list of table rows.""" filter_list = [] filter_ids = [] for f in filters: filter_ids.append(f.index) fullname = URL_P.sub(r'`<\1>`_', f.fullname) filter_list.append((str(f.index + 1), f.name, "{0:.2f}".format(f.msun_vega),
python
{ "resource": "" }
q13483
extract_version
train
def extract_version(): """Extract the version from the package.""" with open('pdftools/__init__.py', 'r') as f: content = f.read()
python
{ "resource": "" }
q13484
add_pypiper_args
train
def add_pypiper_args(parser, groups=("pypiper", ), args=None, required=None, all_args=False): """ Use this to add standardized pypiper arguments to your python pipeline. There are two ways to use `add_pypiper_args`: by specifying argument groups, or by specifying individual arguments. Specifying argument groups will add multiple arguments to your parser; these convenient argument groupings make it easy to add arguments to certain types of pipeline. For example, to make a looper-compatible pipeline, use `groups = ["pypiper", "looper"]`. :param argparse.ArgumentParser parser: ArgumentParser object from a pipeline :param str | Iterable[str] groups: Adds arguments belong to specified group of args. Options: pypiper, config, looper, resources, common, ngs, all. :param str | Iterable[str] args: You may specify a list of specific arguments one by one.
python
{ "resource": "" }
q13485
build_command
train
def build_command(chunks): """ Create a command from various parts. The parts provided may include a base, flags, option-bound arguments, and positional arguments. Each element must be either a string or a two-tuple. Raw strings are interpreted as either the command base, a pre-joined pair (or multiple pairs) of option and argument, a series of positional arguments, or a combination of those elements. The only modification they undergo is trimming of any space characters from each end. :param Iterable[str | (str, str | NoneType)] chunks: the collection of the command components to interpret, modify, and join to create a single meaningful command :return str: the single meaningful command built from the given components :raise ValueError: if no command parts are provided """ if not chunks: raise ValueError( "No command parts: {} ({})".format(chunks, type(chunks))) if isinstance(chunks, str): return chunks parsed_pieces = [] for cmd_part in chunks: if cmd_part is None: continue
python
{ "resource": "" }
q13486
build_sample_paths
train
def build_sample_paths(sample): """ Ensure existence of folders for a Sample. :param looper.models.Sample sample: Sample (or instance supporting get() that stores folders paths in a 'paths' key, in which the value is a mapping from path name to actual folder path) """ for path_name, path in sample.paths.items():
python
{ "resource": "" }
q13487
checkpoint_filename
train
def checkpoint_filename(checkpoint, pipeline_name=None): """ Translate a checkpoint to a filename. This not only adds the checkpoint file extension but also standardizes the way in which checkpoint names are mapped to filenames. :param str | pypiper.Stage checkpoint: name of a pipeline phase/stage :param str pipeline_name: name of pipeline to prepend to the checkpoint filename; this differentiates checkpoint files, e.g. within the same sample output folder but associated with different pipelines, in case of the (somewhat probable) scenario of a stage name collision between pipelines the processed the same sample and wrote to the same output folder :return str | NoneType: standardized checkpoint name for file, plus
python
{ "resource": "" }
q13488
checkpoint_filepath
train
def checkpoint_filepath(checkpoint, pm): """ Create filepath for indicated checkpoint. :param str | pypiper.Stage checkpoint: Pipeline phase/stage or one's name :param pypiper.PipelineManager | pypiper.Pipeline pm: manager of a pipeline instance, relevant for output folder path. :return str: standardized checkpoint name for file, plus extension :raise ValueError: if the checkpoint is given as absolute path that does not point within pipeline output folder """ # Handle case in which checkpoint is given not just as a string, but # as a checkpoint-like filename. Don't worry about absolute path status # of a potential filename input, or whether it's in the pipeline's # output folder. That's handled upstream. While this isn't a protected # function, there's no real reason to call this from outside the package. if isinstance(checkpoint, str): if os.path.isabs(checkpoint): if is_in_file_tree(checkpoint, pm.outfolder): return checkpoint else: raise ValueError( "Absolute checkpoint path '{}' is not in pipeline output " "folder '{}'".format(checkpoint, pm.outfolder)) _, ext = os.path.splitext(checkpoint)
python
{ "resource": "" }
q13489
check_shell_redirection
train
def check_shell_redirection(cmd): """ Determine whether a command appears to contain shell redirection symbol outside of curly brackets :param str cmd: Command to investigate. :return bool: Whether the command appears to contain shell redirection. """ curly_brackets = True while curly_brackets: SRE_match_obj = re.search(r'\{(.*?)}',cmd)
python
{ "resource": "" }
q13490
get_proc_name
train
def get_proc_name(cmd): """ Get the representative process name from complex command :param str | list[str] cmd: a command to be processed :return str: the basename representative command """
python
{ "resource": "" }
q13491
get_first_value
train
def get_first_value(param, param_pools, on_missing=None, error=True): """ Get the value for a particular parameter from the first pool in the provided priority list of parameter pools. :param str param: Name of parameter for which to determine/fetch value. :param Sequence[Mapping[str, object]] param_pools: Ordered (priority) collection of mapping from parameter name to value; this should be ordered according to descending priority. :param object | function(str) -> object on_missing: default value or action to take if the requested parameter is missing from all of the pools. If a callable, it should return a value when passed the requested parameter as the one and only argument. :param bool error: Whether to raise an error if the requested parameter is not mapped to a value AND there's no value or strategy provided with 'on_missing' with which to handle the case of a request for an unmapped parameter. :return object: Value to which the requested parameter first mapped in the (descending) priority collection of parameter 'pools,' or a value explicitly defined or derived with 'on_missing.' :raise KeyError: If the requested parameter is unmapped in all of the provided pools, and the argument to the 'error' parameter evaluates to True. """ # Search for the requested parameter.
python
{ "resource": "" }
q13492
is_in_file_tree
train
def is_in_file_tree(fpath, folder): """ Determine whether a file is in a folder. :param str fpath: filepath to investigate :param folder: path to folder to query :return bool: whether the path indicated is in the folder
python
{ "resource": "" }
q13493
is_gzipped_fastq
train
def is_gzipped_fastq(file_name): """ Determine whether indicated file appears to be a gzipped FASTQ. :param str file_name: Name/path of file to check as gzipped FASTQ. :return bool: Whether indicated file appears to be in
python
{ "resource": "" }
q13494
make_lock_name
train
def make_lock_name(original_path, path_base_folder): """ Create name for lock file from an absolute path. The original path must be absolute, and it should point to a location within the location indicated by the base folder path provided. This is particularly useful for deleting a sample's output folder path from within the path of a target file to generate a lock file corresponding to the original target. :param str original_path: Full original filepath. :param str path_base_folder: Portion of original path to delete :return str: Name or perhaps relative (to the base folder path indicated) path to lock file """ def make_name(p): return
python
{ "resource": "" }
q13495
is_multi_target
train
def is_multi_target(target): """ Determine if pipeline manager's run target is multiple. :param None or str or Sequence of str target: 0, 1, or multiple targets :return bool: Whether there are multiple targets :raise TypeError: if the argument is neither None nor string nor Sequence """ if target is None or isinstance(target, str): return
python
{ "resource": "" }
q13496
parse_cores
train
def parse_cores(cores, pm, default): """ Framework to finalize number of cores for an operation. Some calls to a function may directly provide a desired number of cores, others may not. Similarly, some pipeline managers may define a cores count while others will not. This utility provides a single via which the count of cores to use for an operation may be determined. If a cores count is given explicitly, use that. Then try pipeline manager for cores. Finally, fall back to a default. Force default to be defined (this function is intended to be partially applied, then reused within a module, class, etc. to standardize the way in which this value is determined within a scope.) :param int | str cores: direct specification of cores
python
{ "resource": "" }
q13497
parse_stage_name
train
def parse_stage_name(stage): """ Determine the name of a stage. The stage may be provided already as a name, as a Stage object, or as a callable with __name__ (e.g., function). :param str | pypiper.Stage | function stage: Object representing a stage, from which to obtain name. :return str: Name of putative pipeline Stage. """ if
python
{ "resource": "" }
q13498
pipeline_filepath
train
def pipeline_filepath(pm, filename=None, suffix=None): """ Derive path to file for managed pipeline. :param pypiper.PipelineManager | pypiper.Pipeline pm: Manager of a particular pipeline instance. :param str filename: Name of file for which to create full path based on pipeline's output folder. :param str suffix: Suffix for the file; this can be added to the filename if provided or added to the pipeline name if there's no filename. :raises TypeError: If neither filename nor suffix is provided, raise a TypeError, as in that case there's no substance from which to create a filepath. :return str: Path to file within managed pipeline's output folder, with filename as given or determined by the pipeline name, and suffix
python
{ "resource": "" }
q13499
NGSTk.bam_to_fastq_bedtools
train
def bam_to_fastq_bedtools(self, bam_file, out_fastq_pre, paired_end): """ Converts bam to fastq; A version using bedtools """ self.make_sure_path_exists(os.path.dirname(out_fastq_pre)) fq1 = out_fastq_pre + "_R1.fastq" fq2 = None cmd = self.tools.bedtools +
python
{ "resource": "" }