_id stringlengths 2 7 | title stringlengths 1 88 | partition stringclasses 3
values | text stringlengths 31 13.1k | language stringclasses 1
value | meta_information dict |
|---|---|---|---|---|---|
q272900 | XMeans.compute_bic | test | def compute_bic(self, D, means, labels, K, R):
"""Computes the Bayesian Information Criterion."""
D = vq.whiten(D)
Rn = D.shape[0]
M = D.shape[1]
if R == K:
return 1
# Maximum likelihood estimate (MLE)
mle_var = 0
for k in range(len(means)):
X = D[np.argwhere(labels == k)]
X = X.reshape((X.shape[0], X.shape[-1]))
for x in X:
mle_var += distance.euclidean(x, means[k])
#print x, means[k], mle_var
| python | {
"resource": ""
} |
q272901 | magnitude | test | def magnitude(X):
"""Magnitude of a complex matrix."""
r = np.real(X)
| python | {
"resource": ""
} |
q272902 | json_to_bounds | test | def json_to_bounds(segments_json):
"""Extracts the boundaries from a json file and puts them into
an np array."""
f = open(segments_json)
segments = json.load(f)["segments"]
bounds = []
for segment in segments:
| python | {
"resource": ""
} |
q272903 | json_bounds_to_bounds | test | def json_bounds_to_bounds(bounds_json):
"""Extracts the boundaries from a bounds json file and puts them into
an np array."""
f = open(bounds_json)
segments = json.load(f)["bounds"]
| python | {
"resource": ""
} |
q272904 | json_to_labels | test | def json_to_labels(segments_json):
"""Extracts the labels from a json file and puts them into
an np array."""
f = open(segments_json)
segments = json.load(f)["segments"]
labels = []
str_labels = []
for segment in segments:
if not segment["label"] in str_labels:
str_labels.append(segment["label"])
| python | {
"resource": ""
} |
q272905 | json_to_beats | test | def json_to_beats(beats_json_file):
"""Extracts the beats from the beats_json_file and puts them into
an np array."""
f = open(beats_json_file, "r")
beats_json = json.load(f)
| python | {
"resource": ""
} |
q272906 | compute_ffmc2d | test | def compute_ffmc2d(X):
"""Computes the 2D-Fourier Magnitude Coefficients."""
# 2d-fft
fft2 = scipy.fftpack.fft2(X)
# Magnitude
fft2m = magnitude(fft2)
# FFTshift and flatten
fftshift = | python | {
"resource": ""
} |
q272907 | compute_labels | test | def compute_labels(X, rank, R, bound_idxs, niter=300):
"""Computes the labels using the bounds."""
try:
F, G = cnmf(X, rank, niter=niter, hull=False)
except:
return [1]
label_frames = filter_activation_matrix(G.T, R)
label_frames = np.asarray(label_frames, dtype=int)
#labels = [label_frames[0]]
labels = []
bound_inters = zip(bound_idxs[:-1], bound_idxs[1:])
for bound_inter in bound_inters:
| python | {
"resource": ""
} |
q272908 | filter_activation_matrix | test | def filter_activation_matrix(G, R):
"""Filters the activation matrix G, and returns a flattened copy."""
#import pylab as plt
#plt.imshow(G, interpolation="nearest", aspect="auto")
#plt.show()
idx = np.argmax(G, axis=1)
max_idx = np.arange(G.shape[0])
max_idx = (max_idx, idx.flatten())
G[:, :] = | python | {
"resource": ""
} |
q272909 | get_boundaries_module | test | def get_boundaries_module(boundaries_id):
"""Obtains the boundaries module given a boundary algorithm identificator.
Parameters
----------
boundaries_id: str
Boundary algorithm identificator (e.g., foote, sf).
Returns
-------
module: object
Object containing the selected boundary module.
| python | {
"resource": ""
} |
q272910 | get_labels_module | test | def get_labels_module(labels_id):
"""Obtains the label module given a label algorithm identificator.
Parameters
----------
labels_id: str
Label algorithm identificator (e.g., fmc2d, cnmf).
Returns
-------
module: object
Object containing the selected label module.
None for not computing the | python | {
"resource": ""
} |
q272911 | run_hierarchical | test | def run_hierarchical(audio_file, bounds_module, labels_module, frame_times,
config, annotator_id=0):
"""Runs hierarchical algorithms with the specified identifiers on the
audio_file. See run_algorithm for more information.
"""
# Sanity check
if bounds_module is None:
raise NoHierBoundaryError("A boundary algorithm is needed when using "
"hierarchical segmentation.")
# Get features to make code nicer
features = config["features"].features
# Compute boundaries
S = bounds_module.Segmenter(audio_file, **config)
est_idxs, est_labels = S.processHierarchical()
# Compute labels if needed
if labels_module is not None and \
bounds_module.__name__ != labels_module.__name__:
# Compute labels for each level in the hierarchy
flat_config = deepcopy(config)
flat_config["hier"] = False
for i, level_idxs in enumerate(est_idxs):
S = labels_module.Segmenter(audio_file,
in_bound_idxs=level_idxs,
| python | {
"resource": ""
} |
q272912 | run_flat | test | def run_flat(file_struct, bounds_module, labels_module, frame_times, config,
annotator_id):
"""Runs the flat algorithms with the specified identifiers on the
audio_file. See run_algorithm for more information.
"""
# Get features to make code nicer
features = config["features"].features
# Segment using the specified boundaries and labels
# Case when boundaries and labels algorithms are the same
if bounds_module is not None and labels_module is not None and \
bounds_module.__name__ == labels_module.__name__:
S = bounds_module.Segmenter(file_struct, **config)
est_idxs, est_labels = S.processFlat()
# Different boundary and label algorithms
else:
# Identify segment boundaries
if bounds_module is not None:
S = bounds_module.Segmenter(file_struct, in_labels=[], **config)
est_idxs, est_labels = S.processFlat()
else:
try:
# Ground-truth boundaries
est_times, est_labels = io.read_references(
file_struct.audio_file, annotator_id=annotator_id)
est_idxs = io.align_times(est_times, frame_times)
if est_idxs[0] != 0:
| python | {
"resource": ""
} |
q272913 | run_algorithms | test | def run_algorithms(file_struct, boundaries_id, labels_id, config,
annotator_id=0):
"""Runs the algorithms with the specified identifiers on the audio_file.
Parameters
----------
file_struct: `msaf.io.FileStruct`
Object with the file paths.
boundaries_id: str
Identifier of the boundaries algorithm to use ("gt" for ground truth).
labels_id: str
Identifier of the labels algorithm to use (None for not labeling).
config: dict
Dictionary containing the custom parameters of the algorithms to use.
annotator_id: int
Annotator identificator in the ground truth.
Returns
-------
est_times: np.array or list
List of estimated times for the segment boundaries.
If `list`, it will be a list of np.arrays, sorted by segmentation
layer.
est_labels: np.array or list
List of all the labels associated segments.
If `list`, it will be a list of np.arrays, sorted by segmentation
layer.
"""
# Check that there are enough audio frames
if config["features"].features.shape[0] <= msaf.config.minimum_frames:
logging.warning("Audio file too short, or too many few beats "
| python | {
"resource": ""
} |
q272914 | process_track | test | def process_track(file_struct, boundaries_id, labels_id, config,
annotator_id=0):
"""Prepares the parameters, runs the algorithms, and saves results.
Parameters
----------
file_struct: `msaf.io.FileStruct`
FileStruct containing the paths of the input files (audio file,
features file, reference file, output estimation file).
boundaries_id: str
Identifier of the boundaries algorithm to use ("gt" for ground truth).
labels_id: str
Identifier of the labels algorithm to use (None for not labeling).
config: dict
Dictionary containing the custom parameters of the algorithms to use.
annotator_id: int
Annotator identificator in the ground truth.
Returns
-------
est_times: np.array
List of estimated times for the segment boundaries.
est_labels: np.array
List of all | python | {
"resource": ""
} |
q272915 | process | test | def process(in_path, annot_beats=False, feature="pcp", framesync=False,
boundaries_id=msaf.config.default_bound_id,
labels_id=msaf.config.default_label_id, hier=False,
sonify_bounds=False, plot=False, n_jobs=4, annotator_id=0,
config=None, out_bounds="out_bounds.wav", out_sr=22050):
"""Main process to segment a file or a collection of files.
Parameters
----------
in_path: str
Input path. If a directory, MSAF will function in collection mode.
If audio file, MSAF will be in single file mode.
annot_beats: bool
Whether to use annotated beats or not.
feature: str
String representing the feature to be used (e.g. pcp, mfcc, tonnetz)
framesync: str
Whether to use framesync features or not (default: False -> beatsync)
boundaries_id: str
Identifier of the boundaries algorithm (use "gt" for groundtruth)
labels_id: str
Identifier of the labels algorithm (use None to not compute labels)
hier : bool
Whether to compute a hierarchical or flat segmentation.
sonify_bounds: bool
Whether to write an output audio file with the annotated boundaries
or not (only available in Single File Mode).
plot: bool
Whether to plot the boundaries and labels against the ground truth.
n_jobs: int
Number of processes to run in parallel. Only available in collection
mode.
annotator_id: int
Annotator identificator in the ground truth.
config: dict
Dictionary containing custom configuration parameters for the
algorithms. If None, the default parameters are used.
out_bounds: str
Path to the output for the sonified boundaries (only in single file
mode, when sonify_bounds is True.
out_sr : int
Sampling rate for the sonified bounds.
Returns
-------
results : list
List containing tuples of (est_times, est_labels) of estimated
boundary times and estimated labels.
If labels_id is None, est_labels will be a list of -1.
"""
# Seed random to reproduce results
np.random.seed(123)
# Set up configuration based on algorithms parameters
if config is None:
config = io.get_configuration(feature, annot_beats, framesync,
boundaries_id, labels_id)
config["features"] = None
# Save multi-segment (hierarchical) configuration
config["hier"] = hier
if not os.path.exists(in_path):
raise NoAudioFileError("File or directory does not exists, %s" %
in_path)
| python | {
"resource": ""
} |
q272916 | AA.update_w | test | def update_w(self):
""" alternating least squares step, update W under the convexity
constraint """
def update_single_w(i):
""" compute single W[:,i] """
# optimize beta using qp solver from cvxopt
FB = base.matrix(np.float64(np.dot(-self.data.T, W_hat[:,i])))
be = solvers.qp(HB, FB, INQa, INQb, EQa, EQb)
| python | {
"resource": ""
} |
q272917 | main | test | def main():
'''
Main Entry point for translator and argument parser
'''
args = command_line()
translate = partial(translator, args.source, args.dest,
| python | {
"resource": ""
} |
q272918 | coroutine | test | def coroutine(func):
"""
Initializes coroutine essentially priming it to the yield statement.
Used as a decorator over functions that generate coroutines.
.. code-block:: python
# Basic coroutine producer/consumer pattern
from translate import coroutine
@coroutine
def coroutine_foo(bar):
try:
while True:
baz = (yield)
| python | {
"resource": ""
} |
q272919 | accumulator | test | def accumulator(init, update):
"""
Generic accumulator function.
.. code-block:: python
# Simplest Form
>>> a = 'this' + ' '
>>> b = 'that'
>>> c = functools.reduce(accumulator, a, b)
>>> c
'this that'
# The type of the initial value determines output type.
>>> a = 5
>>> b = Hello
>>> c = functools.reduce(accumulator, a, b)
>>> c
10 | python | {
"resource": ""
} |
q272920 | set_task | test | def set_task(translator, translit=False):
"""
Task Setter Coroutine
End point destination coroutine of a purely consumer type.
Delegates Text IO to the `write_stream` function.
:param translation_function: Translator
:type translation_function: Function
:param translit: Transliteration Switch
| python | {
"resource": ""
} |
q272921 | spool | test | def spool(iterable, maxlen=1250):
"""
Consumes text streams and spools them together for more io
efficient processes.
:param iterable: Sends text stream for further processing
:type iterable: Coroutine
:param maxlen: Maximum query string size
:type maxlen: Integer
"""
words = int()
text = str()
try:
while True:
while words < maxlen:
stream = yield
| python | {
"resource": ""
} |
q272922 | source | test | def source(target, inputstream=sys.stdin):
"""
Coroutine starting point. Produces text stream and forwards to consumers
:param target: Target coroutine consumer
:type target: Coroutine
:param inputstream: Input Source
:type inputstream: BufferedTextIO Object
"""
for line in inputstream:
while len(line) > 600: | python | {
"resource": ""
} |
q272923 | push_url | test | def push_url(interface):
'''
Decorates a function returning the url of translation API.
Creates and maintains HTTP connection state
Returns a dict response object from the server containing the translated
text and metadata of the request body
:param interface: Callable Request Interface
:type interface: Function
'''
@functools.wraps(interface)
def connection(*args, **kwargs):
"""
Extends and wraps a HTTP interface.
:return: Response Content
:rtype: Dictionary
"""
session = Session()
session.mount('http://', HTTPAdapter(max_retries=2))
session.mount('https://', HTTPAdapter(max_retries=2))
| python | {
"resource": ""
} |
q272924 | translator | test | def translator(source, target, phrase, version='0.0 test', charset='utf-8'):
"""
Returns the url encoded string that will be pushed to the translation
server for parsing.
List of acceptable language codes for source and target languages
can be found as a JSON file in the etc directory.
Some source languages are limited in scope of the possible target languages
that are available.
.. code-block:: python
>>> from translate import translator
>>> translator('en', 'zh-TW', 'Hello World!')
'你好世界!'
:param source: Language code for translation source
:type source: String
:param target: Language code that source will be translate into
:type target: String
:param phrase: Text body string that will be url encoded and translated
:type phrase: String
:return: Request Interface
| python | {
"resource": ""
} |
q272925 | translation_table | test | def translation_table(language, filepath='supported_translations.json'):
'''
Opens up file located under the etc directory containing language
codes and prints them out.
:param file: Path to location of json file
:type file: str
:return: language codes
:rtype: dict
'''
fullpath = abspath(join(dirname(__file__), 'etc', filepath))
if not isfile(fullpath):
| python | {
"resource": ""
} |
q272926 | print_table | test | def print_table(language):
'''
Generates a formatted table of language codes
'''
table = translation_table(language)
for code, name in sorted(table.items(), key=operator.itemgetter(0)):
| python | {
"resource": ""
} |
q272927 | remove_nodes | test | def remove_nodes(network, rm_nodes):
"""
Create DataFrames of nodes and edges that do not include specified nodes.
Parameters
----------
network : pandana.Network
rm_nodes : array_like
A list, array, Index, or Series of node IDs that should *not*
be saved as part of the Network.
Returns
-------
nodes, edges : pandas.DataFrame | python | {
"resource": ""
} |
q272928 | network_to_pandas_hdf5 | test | def network_to_pandas_hdf5(network, filename, rm_nodes=None):
"""
Save a Network's data to a Pandas HDFStore.
Parameters
----------
network : pandana.Network
filename : str
rm_nodes : array_like
A list, array, Index, or Series of node IDs that should *not*
| python | {
"resource": ""
} |
q272929 | network_from_pandas_hdf5 | test | def network_from_pandas_hdf5(cls, filename):
"""
Build a Network from data in a Pandas HDFStore.
Parameters
----------
cls : class
Class to instantiate, usually pandana.Network.
filename : str
Returns
-------
network : pandana.Network
"""
with pd.HDFStore(filename) as store:
nodes = store['nodes']
| python | {
"resource": ""
} |
q272930 | Network.set | test | def set(self, node_ids, variable=None, name="tmp"):
"""
Characterize urban space with a variable that is related to nodes in
the network.
Parameters
----------
node_ids : Pandas Series, int
A series of node_ids which are usually computed using
get_node_ids on this object.
variable : Pandas Series, numeric, optional
A series which represents some variable defined in urban space.
It could be the location of buildings, or the income of all
households - just about anything can be aggregated using the
network queries provided here and this provides the api to set
the variable at its disaggregate locations. Note that node_id
and variable should have the same index (although the index is
not actually used). If variable is not set, then it is assumed
that the variable is all "ones" at the location specified by
node_ids. This could be, for instance, the location of all
coffee shops which don't really have a variable to aggregate. The
variable is connected to the closest node in the Pandana network
which assumes no impedance between the location of the variable
and the location of the closest network node.
name : string, optional
Name the variable. This is optional in the sense | python | {
"resource": ""
} |
q272931 | Network.aggregate | test | def aggregate(self, distance, type="sum", decay="linear", imp_name=None,
name="tmp"):
"""
Aggregate information for every source node in the network - this is
really the main purpose of this library. This allows you to touch
the data specified by calling set and perform some aggregation on it
within the specified distance. For instance, summing the population
within 1000 meters.
Parameters
----------
distance : float
The maximum distance to aggregate data within. 'distance' can
represent any impedance unit that you have set as your edge
weight. This will usually be a distance unit in meters however
if you have customized the impedance this could be in other
units such as utility or time etc.
type : string
The type of aggregation, can be one of "ave", "sum", "std",
"count", and now "min", "25pct", "median", "75pct", and "max" will
compute the associated quantiles. (Quantiles are computed by
sorting so might be slower than the others.)
decay : string
The type of decay to apply, which makes things that are further
away count less in the aggregation - must be one of "linear",
"exponential" or "flat" (which means no decay). Linear is the
fastest computation to perform. When performing an "ave",
the decay is typically "flat"
imp_name : string, optional
The impedance name to use for the aggregation on this network.
Must be one of the impedance names passed in the constructor of
this object. If not specified, there must be only one impedance
passed in the constructor, which will be used.
name : string, optional
The variable to aggregate. This variable will have been created
and named by a call to set. If not specified, the default
variable name will be used so that the most recent call to set
without giving a name will be the variable used.
Returns
| python | {
"resource": ""
} |
q272932 | Network.get_node_ids | test | def get_node_ids(self, x_col, y_col, mapping_distance=None):
"""
Assign node_ids to data specified by x_col and y_col
Parameters
----------
x_col : Pandas series (float)
A Pandas Series where values specify the x (e.g. longitude)
location of dataset.
y_col : Pandas series (float)
A Pandas Series where values specify the y (e.g. latitude)
location of dataset. x_col and y_col should use the same index.
mapping_distance : float, optional
The maximum distance that will be considered a match between the
x, y data and the nearest node in the network. This will usually
be a distance unit in meters however if you have customized the
impedance this could be in other units such as utility or time
etc. If not specified, every x, y coordinate will be mapped to
the nearest node.
Returns
-------
node_ids : Pandas series (int)
Returns a Pandas Series of node_ids for each x, y in the
input data. The index is the same as the indexes of the
x, y input data, and the values are | python | {
"resource": ""
} |
q272933 | Network.plot | test | def plot(
self, data, bbox=None, plot_type='scatter',
fig_kwargs=None, bmap_kwargs=None, plot_kwargs=None,
cbar_kwargs=None):
"""
Plot an array of data on a map using matplotlib and Basemap,
automatically matching the data to the Pandana network node positions.
Keyword arguments are passed to the plotting routine.
Parameters
----------
data : pandas.Series
Numeric data with the same length and index as the nodes
in the network.
bbox : tuple, optional
(lat_min, lng_min, lat_max, lng_max)
plot_type : {'hexbin', 'scatter'}, optional
fig_kwargs : dict, optional
Keyword arguments that will be passed to
matplotlib.pyplot.subplots. Use this to specify things like
figure size or background color.
bmap_kwargs : dict, optional
Keyword arguments that will be passed to the Basemap constructor.
This can be used to specify a projection or coastline resolution.
| python | {
"resource": ""
} |
q272934 | Network.set_pois | test | def set_pois(self, category, maxdist, maxitems, x_col, y_col):
"""
Set the location of all the pois of this category. The pois are
connected to the closest node in the Pandana network which assumes
no impedance between the location of the variable and the location
of the closest network node.
Parameters
----------
category : string
The name of the category for this set of pois
maxdist - the maximum distance that will later be used in
find_all_nearest_pois
| python | {
"resource": ""
} |
q272935 | Network.nearest_pois | test | def nearest_pois(self, distance, category, num_pois=1, max_distance=None,
imp_name=None, include_poi_ids=False):
"""
Find the distance to the nearest pois from each source node. The
bigger values in this case mean less accessibility.
Parameters
----------
distance : float
The maximum distance to look for pois. This will usually be a
distance unit in meters however if you have customized the
impedance this could be in other units such as utility or time
etc.
category : string
The name of the category of poi to look for
num_pois : int
The number of pois to look for, this also sets the number of
columns in the DataFrame that gets returned
max_distance : float, optional
The value to set the distance to if there is NO poi within the
specified distance - if not specified, gets set to distance. This
will usually be a distance unit in meters however if you have
customized the impedance this could be in other units such as
utility or time etc.
imp_name : string, optional
The impedance name to use for the aggregation on this network.
Must be one of the impedance names passed in the constructor of
this object. If not specified, there must be only one impedance
passed in the constructor, which will be used.
include_poi_ids : bool, optional
If this flag is set to true, the call will add columns to the
return DataFrame - instead of just returning the distance for
the nth POI, it will also return the id of that POI. The names
of the columns with the poi ids will be poi1, poi2, etc - it
will take roughly twice as long to include these ids as to not
include them
Returns
-------
d : Pandas DataFrame
Like aggregate, this series has an index of all the node ids for
the network. Unlike aggregate, this method returns a dataframe
with the number of columns equal to the distances to the Nth
closest poi. For instance, if you ask for the 10 closest poi to
each node, column d[1] wil be the distance to the 1st closest poi
of that category while column d[2] will be the distance to the 2nd
closest | python | {
"resource": ""
} |
q272936 | Network.low_connectivity_nodes | test | def low_connectivity_nodes(self, impedance, count, imp_name=None):
"""
Identify nodes that are connected to fewer than some threshold
of other nodes within a given distance.
Parameters
----------
impedance : float
Distance within which to search for other connected nodes. This
will usually be a distance unit in meters however if you have
customized the impedance this could be in other units such as
utility or time etc.
count : int
Threshold for connectivity. If a node is connected to fewer
| python | {
"resource": ""
} |
q272937 | process_node | test | def process_node(e):
"""
Process a node element entry into a dict suitable for going into
a Pandas DataFrame.
Parameters
----------
e : dict
Returns
-------
node : dict
"""
uninteresting_tags = {
'source',
'source_ref',
'source:ref',
'history',
'attribution',
'created_by',
'tiger:tlid',
'tiger:upload_uuid',
}
node = {
| python | {
"resource": ""
} |
q272938 | make_osm_query | test | def make_osm_query(query):
"""
Make a request to OSM and return the parsed JSON.
Parameters
----------
query : str
A string in the Overpass QL | python | {
"resource": ""
} |
q272939 | build_node_query | test | def build_node_query(lat_min, lng_min, lat_max, lng_max, tags=None):
"""
Build the string for a node-based OSM query.
Parameters
----------
lat_min, lng_min, lat_max, lng_max : float
tags : str or list of str, optional
Node tags that will be used to filter the search.
See http://wiki.openstreetmap.org/wiki/Overpass_API/Language_Guide
for information about OSM Overpass queries
and http://wiki.openstreetmap.org/wiki/Map_Features
for a list of tags.
Returns
-------
query : str
"""
if tags is not None:
if isinstance(tags, str):
| python | {
"resource": ""
} |
q272940 | node_query | test | def node_query(lat_min, lng_min, lat_max, lng_max, tags=None):
"""
Search for OSM nodes within a bounding box that match given tags.
Parameters
----------
lat_min, lng_min, lat_max, lng_max : float
tags : str or list of str, optional
Node tags that will be used to filter the search.
See http://wiki.openstreetmap.org/wiki/Overpass_API/Language_Guide
for information about OSM Overpass queries
and http://wiki.openstreetmap.org/wiki/Map_Features
for a list of tags.
Returns
-------
nodes : pandas.DataFrame
Will have 'lat' and 'lon' columns, plus other columns for the
tags associated with the node (these | python | {
"resource": ""
} |
q272941 | isregex | test | def isregex(value):
"""
Returns ``True`` if the input argument object is a native
regular expression object, otherwise ``False``.
Arguments:
value (mixed): input value to test.
Returns:
bool
| python | {
"resource": ""
} |
q272942 | BaseMatcher.compare | test | def compare(self, value, expectation, regex_expr=False):
"""
Compares two values with regular expression matching support.
Arguments:
value (mixed): value to compare.
expectation (mixed): value to match.
regex_expr (bool, optional): enables | python | {
"resource": ""
} |
q272943 | fluent | test | def fluent(fn):
"""
Simple function decorator allowing easy method chaining.
Arguments:
fn (function): target function to decorate.
"""
@functools.wraps(fn) | python | {
"resource": ""
} |
q272944 | compare | test | def compare(expr, value, regex_expr=False):
"""
Compares an string or regular expression againast a given value.
Arguments:
expr (str|regex): string or regular expression value to compare.
value (str): value to compare against to.
regex_expr (bool, optional): enables string based regex matching.
Raises:
AssertionError: in case of assertion error.
Returns:
bool
"""
# Strict equality comparison
if expr == value:
return True
# Infer negate expression to match, if needed
negate = False
| python | {
"resource": ""
} |
q272945 | trigger_methods | test | def trigger_methods(instance, args):
""""
Triggers specific class methods using a simple reflection
mechanism based on the given input dictionary params.
Arguments:
instance (object): target instance to dynamically trigger methods.
args (iterable): input arguments to trigger objects to
Returns:
None
"""
# Start the magic
for name in sorted(args):
value = args[name]
target = instance
# If response attibutes
if name.startswith('response_') or name.startswith('reply_'):
name = name.replace('response_', '').replace('reply_', '')
# If instance has response attribute, use it
if hasattr(instance, '_response'):
target = instance._response | python | {
"resource": ""
} |
q272946 | MatcherEngine.match | test | def match(self, request):
"""
Match the given HTTP request instance against the registered
matcher functions in the current engine.
Arguments:
request (pook.Request): outgoing request to match.
Returns:
tuple(bool, list[Exception]): ``True`` if all matcher tests
passes, otherwise ``False``. Also returns an optional list
of error exceptions.
"""
errors = []
| python | {
"resource": ""
} |
q272947 | get | test | def get(name):
"""
Returns a matcher instance by class or alias name.
Arguments:
name (str): matcher class name or alias.
Returns:
matcher: found matcher instance, otherwise ``None``. | python | {
"resource": ""
} |
q272948 | init | test | def init(name, *args):
"""
Initializes a matcher instance passing variadic arguments to
its constructor. Acts as a delegator proxy.
Arguments:
name (str): matcher class name or alias to execute.
*args (mixed): variadic argument
Returns:
matcher: matcher instance.
| python | {
"resource": ""
} |
q272949 | Response.body | test | def body(self, body):
"""
Defines response body data.
Arguments:
body (str|bytes): response body to use.
Returns:
self: ``pook.Response`` current instance.
| python | {
"resource": ""
} |
q272950 | Response.json | test | def json(self, data):
"""
Defines the mock response JSON body.
Arguments:
data (dict|list|str): JSON body data.
Returns:
| python | {
"resource": ""
} |
q272951 | HTTPHeaderDict.set | test | def set(self, key, val):
"""
Sets a header field with the given value, removing
previous values.
Usage::
headers = HTTPHeaderDict(foo='bar')
headers.set('Foo', 'baz')
| python | {
"resource": ""
} |
q272952 | _append_funcs | test | def _append_funcs(target, items):
"""
Helper function to append functions into a given list.
Arguments:
target (list): receptor list to append functions.
items (iterable): iterable that yields | python | {
"resource": ""
} |
q272953 | _trigger_request | test | def _trigger_request(instance, request):
"""
Triggers request mock definition methods dynamically based on input
keyword arguments passed to `pook.Mock` constructor.
This is used to provide a more Pythonic interface vs chainable API
approach.
"""
if not isinstance(request, Request):
raise TypeError('request must be instance of | python | {
"resource": ""
} |
q272954 | Mock.url | test | def url(self, url):
"""
Defines the mock URL to match.
It can be a full URL with path and query params.
Protocol schema is optional, defaults to ``http://``.
Arguments:
url (str): mock URL to match. E.g: ``server.com/api``.
| python | {
"resource": ""
} |
q272955 | Mock.headers | test | def headers(self, headers=None, **kw):
"""
Defines a dictionary of arguments.
Header keys are case insensitive.
Arguments:
headers (dict): headers to match.
**headers (dict): headers to match as variadic keyword arguments.
Returns:
| python | {
"resource": ""
} |
q272956 | Mock.header_present | test | def header_present(self, *names):
"""
Defines a new header matcher expectation that must be present in the
outgoing request in order to be satisfied, no matter what value it
hosts.
Header keys are case insensitive.
Arguments:
*names (str): header or headers names to match.
Returns:
self: current Mock instance.
Example::
(pook.get('server.com/api') | python | {
"resource": ""
} |
q272957 | Mock.headers_present | test | def headers_present(self, headers):
"""
Defines a list of headers that must be present in the
outgoing request in order to satisfy the matcher, no matter what value
the headers hosts.
Header keys are case insensitive.
Arguments:
headers (list|tuple): header keys to match.
Returns:
self: current Mock instance.
Example::
(pook.get('server.com/api')
| python | {
"resource": ""
} |
q272958 | Mock.content | test | def content(self, value):
"""
Defines the ``Content-Type`` outgoing header value to match.
You can pass one of the following type aliases instead of the full
MIME type representation:
- ``json`` = ``application/json``
- ``xml`` = ``application/xml``
- ``html`` = ``text/html``
- ``text`` = ``text/plain``
| python | {
"resource": ""
} |
q272959 | Mock.params | test | def params(self, params):
"""
Defines a set of URL query params to match.
Arguments:
params (dict): set of params to match.
Returns:
self: current Mock instance.
"""
| python | {
"resource": ""
} |
q272960 | Mock.body | test | def body(self, body):
"""
Defines the body data to match.
``body`` argument can be a ``str``, ``binary`` or a regular expression.
Arguments:
body (str|binary|regex): body data to match.
Returns: | python | {
"resource": ""
} |
q272961 | Mock.json | test | def json(self, json):
"""
Defines the JSON body to match.
``json`` argument can be an JSON string, a JSON serializable
Python structure, such as a ``dict`` or ``list`` or it can be
a regular expression used to match the body.
Arguments:
| python | {
"resource": ""
} |
q272962 | Mock.xml | test | def xml(self, xml):
"""
Defines a XML body value to match.
Arguments:
xml (str|regex): body XML to match.
Returns:
self: current Mock instance.
| python | {
"resource": ""
} |
q272963 | Mock.file | test | def file(self, path):
"""
Reads the body to match from a disk file.
Arguments:
path (str): relative or | python | {
"resource": ""
} |
q272964 | Mock.persist | test | def persist(self, status=None):
"""
Enables persistent mode for the current mock.
| python | {
"resource": ""
} |
q272965 | Mock.error | test | def error(self, error):
"""
Defines a simulated exception error that will be raised.
Arguments:
| python | {
"resource": ""
} |
q272966 | Mock.reply | test | def reply(self, status=200, new_response=False, **kw):
"""
Defines the mock response.
Arguments:
status (int, optional): response status code. Defaults to ``200``.
**kw (dict): optional keyword arguments passed to ``pook.Response``
constructor.
Returns:
| python | {
"resource": ""
} |
q272967 | Mock.match | test | def match(self, request):
"""
Matches an outgoing HTTP request against the current mock matchers.
This method acts like a delegator to `pook.MatcherEngine`.
Arguments:
request (pook.Request): request instance to match.
Raises:
Exception: if the mock has an exception defined.
Returns:
tuple(bool, list[Exception]): ``True`` if the mock matches
the outgoing HTTP request, otherwise ``False``. Also returns
an optional list of error exceptions.
"""
# If mock already expired, fail it
if self._times <= 0:
raise PookExpiredMock('Mock expired')
# Trigger mock filters
for test in self.filters:
if not test(request, self):
return False, []
# Trigger mock mappers
for mapper in self.mappers:
request = mapper(request, self)
if not request:
raise ValueError('map function must return a request object')
| python | {
"resource": ""
} |
q272968 | activate_async | test | def activate_async(fn, _engine):
"""
Async version of activate decorator
Arguments:
fn (function): function that be wrapped by decorator.
_engine (Engine): pook engine instance
Returns:
function: decorator wrapper function.
"""
@coroutine
@functools.wraps(fn)
def wrapper(*args, **kw):
_engine.activate()
| python | {
"resource": ""
} |
q272969 | Engine.set_mock_engine | test | def set_mock_engine(self, engine):
"""
Sets a custom mock engine, replacing the built-in one.
This is particularly useful if you want to replace the built-in
HTTP traffic mock interceptor engine with your custom one.
For mock engine implementation details, see `pook.MockEngine`.
Arguments:
engine (pook.MockEngine): custom mock engine to use.
"""
if not engine:
raise TypeError('engine must be a valid object')
# Instantiate mock engine
mock_engine = engine(self)
# Validate minimum viable interface
| python | {
"resource": ""
} |
q272970 | Engine.enable_network | test | def enable_network(self, *hostnames):
"""
Enables real networking mode, optionally passing one or multiple
hostnames that would be used as filter.
If at least one hostname matches with the outgoing traffic, the
request will be executed via the real network.
Arguments:
*hostnames: optional list of host names to enable real network
against them. hostname value can be a regular expression.
"""
def hostname_filter(hostname, req):
| python | {
"resource": ""
} |
q272971 | Engine.mock | test | def mock(self, url=None, **kw):
"""
Creates and registers a new HTTP mock in the current engine.
Arguments:
url (str): request URL to mock.
activate (bool): force mock engine activation.
Defaults to ``False``.
**kw (mixed): variadic keyword arguments for ``Mock`` constructor.
Returns:
pook.Mock: new mock instance.
"""
# Activate mock engine, if explicitly requested
if kw.get('activate'):
kw.pop('activate') | python | {
"resource": ""
} |
q272972 | Engine.remove_mock | test | def remove_mock(self, mock):
"""
Removes a specific mock instance by object reference.
Arguments:
| python | {
"resource": ""
} |
q272973 | Engine.activate | test | def activate(self):
"""
Activates the registered interceptors in the mocking engine.
This means any HTTP traffic captures by those interceptors will
trigger the HTTP mock matching engine in order to determine if a given
HTTP transaction should be mocked out or not.
"""
| python | {
"resource": ""
} |
q272974 | Engine.disable | test | def disable(self):
"""
Disables interceptors and stops intercepting any outgoing HTTP traffic.
"""
if not self.active:
return None
# Disable | python | {
"resource": ""
} |
q272975 | Engine.should_use_network | test | def should_use_network(self, request):
"""
Verifies if real networking mode should be used for the given
request, passing it to the registered network filters.
Arguments:
request (pook.Request): outgoing HTTP request to test.
| python | {
"resource": ""
} |
q272976 | Engine.match | test | def match(self, request):
"""
Matches a given Request instance contract against the registered mocks.
If a mock passes all the matchers, its response will be returned.
Arguments:
request (pook.Request): Request contract to match.
Raises:
pook.PookNoMatches: if networking is disabled and no mock matches
with the given request contract.
Returns:
pook.Response: the mock response to be used by the interceptor.
"""
# Trigger engine-level request filters
for test in self.filters:
if not test(request, self):
return False
# Trigger engine-level request mappers
for mapper in self.mappers:
request = mapper(request, self)
if not request:
raise ValueError('map function must return a request object')
# Store list of | python | {
"resource": ""
} |
q272977 | Request.copy | test | def copy(self):
"""
Copies the current Request object instance for side-effects purposes.
Returns:
pook.Request: copy of the current Request instance.
"""
req = type(self)()
| python | {
"resource": ""
} |
q272978 | activate | test | def activate(fn=None):
"""
Enables the HTTP traffic interceptors.
This function can be used as decorator.
Arguments:
fn (function|coroutinefunction): Optional function argument
if used as decorator.
Returns:
function: decorator wrapper function, only if called as decorator,
otherwise ``None``.
Example::
# Standard use case
pook.activate()
pook.mock('server.com/foo').reply(404)
res = requests.get('server.com/foo')
assert res.status_code == 404
| python | {
"resource": ""
} |
q272979 | use | test | def use(network=False):
"""
Creates a new isolated mock engine to be used via context manager.
Example::
with pook.use() as engine:
pook.mock('server.com/foo').reply(404)
res = requests.get('server.com/foo')
assert res.status_code == 404
"""
global _engine
| python | {
"resource": ""
} |
q272980 | MockEngine.add_interceptor | test | def add_interceptor(self, *interceptors):
"""
Adds one or multiple HTTP traffic interceptors to the current
mocking engine.
Interceptors are typically HTTP client specific wrapper classes that
implements the pook interceptor interface.
Arguments:
| python | {
"resource": ""
} |
q272981 | MockEngine.remove_interceptor | test | def remove_interceptor(self, name):
"""
Removes a specific interceptor by name.
Arguments:
name (str): interceptor name to disable.
Returns:
bool: `True` if the interceptor was disabled, otherwise `False`.
| python | {
"resource": ""
} |
q272982 | get_setting | test | def get_setting(connection, key):
"""Get key from connection or default to settings."""
| python | {
"resource": ""
} |
q272983 | DecryptedCol.as_sql | test | def as_sql(self, compiler, connection):
"""Build SQL with decryption and casting."""
sql, params = super(DecryptedCol, self).as_sql(compiler, connection)
| python | {
"resource": ""
} |
q272984 | HashMixin.pre_save | test | def pre_save(self, model_instance, add):
"""Save the original_value."""
if self.original:
original_value = | python | {
"resource": ""
} |
q272985 | HashMixin.get_placeholder | test | def get_placeholder(self, value=None, compiler=None, connection=None):
"""
Tell postgres to encrypt this field with a hashing function.
The `value` string is checked to determine if we need to hash or keep
the current value.
`compiler` and `connection` is ignored here as we | python | {
"resource": ""
} |
q272986 | PGPMixin.get_col | test | def get_col(self, alias, output_field=None):
"""Get the decryption for col."""
if output_field is None:
output_field = self
if alias != self.model._meta.db_table or output_field != self:
return DecryptedCol(
| python | {
"resource": ""
} |
q272987 | PGPPublicKeyFieldMixin.get_placeholder | test | def get_placeholder(self, value=None, compiler=None, connection=None):
"""Tell postgres | python | {
"resource": ""
} |
q272988 | hunt_repeated_yaml_keys | test | def hunt_repeated_yaml_keys(data):
"""Parses yaml and returns a list of repeated variables and
the line on which they occur
"""
loader = yaml.Loader(data)
def compose_node(parent, index):
# the line number where the previous token has ended (plus empty lines)
line = loader.line
node = Composer.compose_node(loader, parent, index)
node.__line__ = line + 1
return node
def construct_mapping(node, deep=False):
mapping = dict()
errors = dict()
for key_node, value_node in node.value:
key = key_node.value
| python | {
"resource": ""
} |
q272989 | base_regression | test | def base_regression(Q, slope=None):
"""
this function calculates the regression coefficients for a
given vector containing the averages of tip and branch
quantities.
Parameters
----------
Q : numpy.array
vector with
slope : None, optional
Description
Returns
-------
TYPE
Description
"""
if slope is None:
slope = (Q[dtavgii] - Q[tavgii]*Q[davgii]/Q[sii]) \
/(Q[tsqii] - Q[tavgii]**2/Q[sii])
only_intercept=False
else:
only_intercept=True
intercept = (Q[davgii] - Q[tavgii]*slope)/Q[sii]
if only_intercept:
return {'slope':slope, 'intercept':intercept,
'chisq': 0.5*(Q[dsqii]/Q[sii] - Q[davgii]**2/Q[sii]**2)} | python | {
"resource": ""
} |
q272990 | TreeRegression.CovInv | test | def CovInv(self):
"""
Inverse of the covariance matrix
Returns
-------
H : (np.array)
inverse of the covariance matrix.
| python | {
"resource": ""
} |
q272991 | TreeRegression.recurse | test | def recurse(self, full_matrix=False):
"""
recursion to calculate inverse covariance matrix
Parameters
----------
full_matrix : bool, optional
if True, the entire inverse matrix is calculated. otherwise, only the weighing vector.
"""
for n in self.tree.get_nonterminals(order='postorder'):
n_leaves = len(n._ii)
if full_matrix: M = np.zeros((n_leaves, n_leaves), dtype=float)
r = np.zeros(n_leaves, dtype=float)
c_count = 0
for c in n:
ssq = self.branch_variance(c)
nc = len(c._ii)
if c.is_terminal():
if full_matrix:
| python | {
"resource": ""
} |
q272992 | TreeRegression._calculate_averages | test | def _calculate_averages(self):
"""
calculate the weighted sums of the tip and branch values and
their second moments.
"""
for n in self.tree.get_nonterminals(order='postorder'):
Q = np.zeros(6, dtype=float)
for c in n:
tv = self.tip_value(c)
bv = self.branch_value(c)
var = self.branch_variance(c)
Q += self.propagate_averages(c, tv, bv, var)
n.Q=Q
for n in self.tree.find_clades(order='preorder'):
O = np.zeros(6, dtype=float)
if n==self.tree.root:
n.Qtot = n.Q
continue
for c in n.up:
if c==n:
continue
tv = self.tip_value(c)
bv = self.branch_value(c)
| python | {
"resource": ""
} |
q272993 | TreeRegression.propagate_averages | test | def propagate_averages(self, n, tv, bv, var, outgroup=False):
"""
This function implements the propagation of the means,
variance, and covariances along a branch. It operates
both towards the root and tips.
Parameters
----------
n : (node)
the branch connecting this node to its parent is used
for propagation
tv : (float)
tip value. Only required if not is terminal
bl : (float)
branch value. The increment of the tree associated quantity'
var : (float)
the variance increment along the branch
Returns
-------
Q : (np.array)
a vector of length 6 containing the updated quantities
"""
| python | {
"resource": ""
} |
q272994 | TreeRegression.explained_variance | test | def explained_variance(self):
"""calculate standard explained variance
Returns
-------
float
r-value of the root-to-tip distance and time.
independent of regression model, but dependent on root choice
| python | {
"resource": ""
} |
q272995 | TreeRegression.regression | test | def regression(self, slope=None):
"""regress tip values against branch values
Parameters
----------
slope : None, optional
if given, the slope isn't optimized
| python | {
"resource": ""
} |
q272996 | TreeRegression.find_best_root | test | def find_best_root(self, force_positive=True, slope=None):
"""
determine the position on the tree that minimizes the bilinear
product of the inverse covariance and the data vectors.
Returns
-------
best_root : (dict)
dictionary with the node, the fraction `x` at which the branch
is to be split, and the regression parameters
"""
self._calculate_averages()
best_root = {"chisq": np.inf}
for n in self.tree.find_clades():
if n==self.tree.root:
continue
tv = self.tip_value(n)
bv = self.branch_value(n)
var = self.branch_variance(n)
x, chisq = self._optimal_root_along_branch(n, tv, bv, var, slope=slope)
if (chisq<best_root["chisq"]):
tmpQ = self.propagate_averages(n, tv, bv*x, var*x) \
+ self.propagate_averages(n, tv, bv*(1-x), var*(1-x), outgroup=True)
reg = base_regression(tmpQ, slope=slope)
if reg["slope"]>=0 or (force_positive==False):
best_root = {"node":n, "split":x}
best_root.update(reg)
if 'node' not in best_root:
print("TreeRegression.find_best_root: No valid root found!", force_positive)
return None
if 'hessian' in best_root:
# calculate differentials with respect to x
deriv = []
n = best_root["node"]
tv = self.tip_value(n)
bv = self.branch_value(n)
var = self.branch_variance(n)
for dx in [-0.001, 0.001]:
y = min(1.0, max(0.0, best_root["split"]+dx))
tmpQ = self.propagate_averages(n, tv, bv*y, var*y) \
+ self.propagate_averages(n, tv, bv*(1-y), var*(1-y), outgroup=True)
| python | {
"resource": ""
} |
q272997 | Coalescent.set_Tc | test | def set_Tc(self, Tc, T=None):
'''
initialize the merger model with a coalescent time
Args:
- Tc: a float or an iterable, if iterable another argument T of same shape is required
- T: an array like of same shape as Tc that specifies the time pivots corresponding to Tc
Returns:
- None
'''
if isinstance(Tc, Iterable):
if len(Tc)==len(T):
x = np.concatenate(([-ttconf.BIG_NUMBER], T, [ttconf.BIG_NUMBER]))
y = np.concatenate(([Tc[0]], Tc, [Tc[-1]]))
self.Tc = interp1d(x,y)
| python | {
"resource": ""
} |
q272998 | Coalescent.calc_branch_count | test | def calc_branch_count(self):
'''
calculates an interpolation object that maps time to the number of
concurrent branches in the tree. The result is stored in self.nbranches
'''
# make a list of (time, merger or loss event) by root first iteration
self.tree_events = np.array(sorted([(n.time_before_present, len(n.clades)-1)
for n in self.tree.find_clades() if not n.bad_branch],
key=lambda x:-x[0]))
# collapse multiple events at one time point into sum of changes
from collections import defaultdict
dn_branch = defaultdict(int)
for (t, dn) in self.tree_events:
dn_branch[t]+=dn
unique_mergers = np.array(sorted(dn_branch.items(), key = lambda x:-x[0]))
# calculate the branch count at each point summing | python | {
"resource": ""
} |
q272999 | Coalescent.cost | test | def cost(self, t_node, branch_length, multiplicity=2.0):
'''
returns the cost associated with a branch starting at t_node
t_node is time before present, the branch goes back in time
Args:
- t_node: time of the node
| python | {
"resource": ""
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.