code stringlengths 52 7.75k | docs stringlengths 1 5.85k |
|---|---|
def started(generator_function):
@wraps(generator_function)
def wrapper(*args, **kwargs):
g = generator_function(*args, **kwargs)
next(g)
return g
return wrapper | starts a generator when created |
def add_log_error(self, x, flag_also_show=False, E=None):
if len(x) == 0:
x = "(empty error)"
tb.print_stack()
x_ = x
if E is not None:
a99.get_python_logger().exception(x_)
else:
a99.get_python_logger().info("ERROR: {}".f... | Sets text of labelError. |
def add_log(self, x, flag_also_show=False):
self._add_log_no_logger(x, flag_also_show)
a99.get_python_logger().info(x) | Logs to 4 different outputs: conditionally to 3, and certainly to get_python_logger() |
def config(name='CACHE_URL', default='locmem://'):
config = {}
s = env(name, default)
if s:
config = parse_cache_url(s)
return config | Returns configured CACHES dictionary from CACHE_URL |
def create_country(cls, country, **kwargs):
kwargs['_return_http_data_only'] = True
if kwargs.get('async'):
return cls._create_country_with_http_info(country, **kwargs)
else:
(data) = cls._create_country_with_http_info(country, **kwargs)
return data | Create Country
Create a new Country
This method makes a synchronous HTTP request by default. To make an
asynchronous HTTP request, please pass async=True
>>> thread = api.create_country(country, async=True)
>>> result = thread.get()
:param async bool
:param Coun... |
def delete_country_by_id(cls, country_id, **kwargs):
kwargs['_return_http_data_only'] = True
if kwargs.get('async'):
return cls._delete_country_by_id_with_http_info(country_id, **kwargs)
else:
(data) = cls._delete_country_by_id_with_http_info(country_id, **kwargs... | Delete Country
Delete an instance of Country by its ID.
This method makes a synchronous HTTP request by default. To make an
asynchronous HTTP request, please pass async=True
>>> thread = api.delete_country_by_id(country_id, async=True)
>>> result = thread.get()
:param a... |
def get_country_by_id(cls, country_id, **kwargs):
kwargs['_return_http_data_only'] = True
if kwargs.get('async'):
return cls._get_country_by_id_with_http_info(country_id, **kwargs)
else:
(data) = cls._get_country_by_id_with_http_info(country_id, **kwargs)
... | Find Country
Return single instance of Country by its ID.
This method makes a synchronous HTTP request by default. To make an
asynchronous HTTP request, please pass async=True
>>> thread = api.get_country_by_id(country_id, async=True)
>>> result = thread.get()
:param as... |
def list_all_countries(cls, **kwargs):
kwargs['_return_http_data_only'] = True
if kwargs.get('async'):
return cls._list_all_countries_with_http_info(**kwargs)
else:
(data) = cls._list_all_countries_with_http_info(**kwargs)
return data | List Countries
Return a list of Countries
This method makes a synchronous HTTP request by default. To make an
asynchronous HTTP request, please pass async=True
>>> thread = api.list_all_countries(async=True)
>>> result = thread.get()
:param async bool
:param int... |
def replace_country_by_id(cls, country_id, country, **kwargs):
kwargs['_return_http_data_only'] = True
if kwargs.get('async'):
return cls._replace_country_by_id_with_http_info(country_id, country, **kwargs)
else:
(data) = cls._replace_country_by_id_with_http_info... | Replace Country
Replace all attributes of Country
This method makes a synchronous HTTP request by default. To make an
asynchronous HTTP request, please pass async=True
>>> thread = api.replace_country_by_id(country_id, country, async=True)
>>> result = thread.get()
:par... |
def update_country_by_id(cls, country_id, country, **kwargs):
kwargs['_return_http_data_only'] = True
if kwargs.get('async'):
return cls._update_country_by_id_with_http_info(country_id, country, **kwargs)
else:
(data) = cls._update_country_by_id_with_http_info(co... | Update Country
Update attributes of Country
This method makes a synchronous HTTP request by default. To make an
asynchronous HTTP request, please pass async=True
>>> thread = api.update_country_by_id(country_id, country, async=True)
>>> result = thread.get()
:param asyn... |
def check_user(user, password):
return ((user == attowiki.user or attowiki.user is None) and
(password == attowiki.password or attowiki.password is None)) | check the auth for user and password. |
def view_meta_index():
rst_files = [filename[2:-4] for filename in sorted(glob.glob("./*.rst"))]
rst_files.reverse()
return template('index',
type="view",
filelist=rst_files,
name="__index__",
extended_name=None,
... | List all the available .rst files in the directory.
view_meta_index is called by the 'meta' url : /__index__ |
def view_cancel_edit(name=None):
if name is None:
return redirect('/')
else:
files = glob.glob("{0}.rst".format(name))
if len(files) > 0:
reset_to_last_commit()
return redirect('/' + name)
else:
return abort(404) | Cancel the edition of an existing page.
Then render the last modification status
.. note:: this is a bottle view
if no page name is given, do nothing (it may leave some .tmp. files in
the directory).
Keyword Arguments:
:name: (str) -- name of the page (OPTIONAL)
Returns:
bot... |
def view_edit(name=None):
response.set_header('Cache-control', 'no-cache')
response.set_header('Pragma', 'no-cache')
if name is None:
# new page
return template('edit',
type="edit",
name=name,
extended_name=None,
... | Edit or creates a new page.
.. note:: this is a bottle view
if no page name is given, creates a new page.
Keyword Arguments:
:name: (str) -- name of the page (OPTIONAL)
Returns:
bottle response object |
def view_pdf(name=None):
if name is None:
return view_meta_index()
files = glob.glob("{0}.rst".format(name))
if len(files) > 0:
file_handle = open(files[0], 'r')
dest_filename = name + '.pdf'
doctree = publish_doctree(file_handle.read())
try:
produce... | Render a pdf file based on the given page.
.. note:: this is a bottle view
Keyword Arguments:
:name: (str) -- name of the rest file (without the .rst extension)
MANDATORY |
def view_history(name, gitref):
response.set_header('Cache-control', 'no-cache')
response.set_header('Pragma', 'no-cache')
content = read_committed_file(gitref, name + '.rst')
if content:
html_body = publish_parts(content,
writer=AttowikiWriter(),
... | Serve a page name from git repo (an old version of a page).
.. note:: this is a bottle view
* this is a GET only method : you can not change a committed page
Keyword Arguments:
:name: (str) -- name of the rest file (without the .rst extension)
:gitref: (str) -- hexsha of the git commit to... |
def view_history_source(name, gitref=None):
response.set_header('Cache-control', 'no-cache')
response.set_header('Pragma', 'no-cache')
response.set_header('Content-Type', 'text/html; charset=utf-8')
if gitref is None:
files = glob.glob("{0}.rst".format(name))
if len(files) > 0:
... | Serve a page name from git repo (an old version of a page).
then return the reST source code
This function does not use any template it returns only plain text
.. note:: this is a bottle view
* this is a GET only method : you can not change a committed page
Keyword Arguments:
:nam... |
def view_history_diff(name, gitref):
response.set_header('Cache-control', 'no-cache')
response.set_header('Pragma', 'no-cache')
response.set_header('Content-Type', 'text/html; charset=utf-8')
old_content = read_committed_file(gitref, name + '.rst')
if old_content:
old_content = old_cont... | Serve a page name from git repo (an old version of a page).
then return the diff between current source and the old commited source
This function does not use any template it returns only plain text
.. note:: this is a bottle view
* this is a GET only method : you can not change a committed pa... |
def view_quick_save_page(name=None):
response.set_header('Cache-control', 'no-cache')
response.set_header('Pragma', 'no-cache')
if request.method == 'PUT':
if name is None:
# new file
if len(request.forms.filename) > 0:
name = request.forms.filename
... | Quick save a page.
.. note:: this is a bottle view
* this view must be called with the PUT method
write the new page content to the file, and not not commit or redirect
Keyword Arguments:
:name: (str) -- name of the rest file (without the .rst extension)
Returns:
bottle respons... |
def describeTable(TableName):
print('-----------------------------------------')
print(TableName+' summary:')
try:
print('-----------------------------------------')
print('Comment: \n'+LOCAL_TABLE_CACHE[TableName]['header']['comment'])
except:
pass
print('Number of rows: '... | INPUT PARAMETERS:
TableName: name of the table to describe
OUTPUT PARAMETERS:
none
---
DESCRIPTION:
Print information about table, including
parameter names, formats and wavenumber range.
---
EXAMPLE OF USAGE:
describeTable('sampletab')
--- |
def getColumns(TableName,ParameterNames):
Columns = []
for par_name in ParameterNames:
Columns.append(LOCAL_TABLE_CACHE[TableName]['data'][par_name])
return Columns | INPUT PARAMETERS:
TableName: source table name (required)
ParameterNames: list of column names to get (required)
OUTPUT PARAMETERS:
ListColumnData: tuple of lists of values from specified column
---
DESCRIPTION:
Returns columns with a names in ParameterN... |
def select(TableName,DestinationTableName=QUERY_BUFFER,ParameterNames=None,Conditions=None,Output=True,File=None):
# TODO: Variables defined in ParameterNames ('LET') MUST BE VISIBLE IN Conditions !!
# check if table exists
if TableName not in LOCAL_TABLE_CACHE.keys():
raise Exception('%s: no s... | INPUT PARAMETERS:
TableName: name of source table (required)
DestinationTableName: name of resulting table (optional)
ParameterNames: list of parameters or expressions (optional)
Conditions: list of logincal expressions (optional)
... |
def sort(TableName,DestinationTableName=None,ParameterNames=None,Accending=True,Output=False,File=None):
number_of_rows = LOCAL_TABLE_CACHE[TableName]['header']['number_of_rows']
index = range(0,number_of_rows)
#print 'num = '+str(number_of_rows)
if not DestinationTableName:
DestinationTable... | INPUT PARAMETERS:
TableName: name of source table (required)
DestinationTableName: name of resulting table (optional)
ParameterNames: list of parameters or expressions to sort by (optional)
Accending: sort in ascending (True) or descendin... |
def fetch_by_ids(TableName,iso_id_list,numin,numax,ParameterGroups=[],Parameters=[]):
if type(iso_id_list) not in set([list,tuple]):
iso_id_list = [iso_id_list]
queryHITRAN(TableName,iso_id_list,numin,numax,
pargroups=ParameterGroups,params=Parameters)
iso_names = [ISO_ID[i][ISO_... | INPUT PARAMETERS:
TableName: local table name to fetch in (required)
iso_id_list: list of isotopologue id's (required)
numin: lower wavenumber bound (required)
numax: upper wavenumber bound (required)
OUTPUT PARAMETERS:
none
---
DESCRIPT... |
def fetch(TableName,M,I,numin,numax,ParameterGroups=[],Parameters=[]):
queryHITRAN(TableName,[ISO[(M,I)][ISO_INDEX['id']]],numin,numax,
pargroups=ParameterGroups,params=Parameters)
iso_name = ISO[(M,I)][ISO_INDEX['iso_name']]
Comment = 'Contains lines for '+iso_name
Comment += ('\n ... | INPUT PARAMETERS:
TableName: local table name to fetch in (required)
M: HITRAN molecule number (required)
I: HITRAN isotopologue number (required)
numin: lower wavenumber bound (required)
numax: upper wavenumber bound (requir... |
def partitionSum(M,I,T,step=None):
# partitionSum
if not step:
if type(T) not in set([list,tuple]):
return BD_TIPS_2011_PYTHON(M,I,T)[1]
else:
return [BD_TIPS_2011_PYTHON(M,I,temp)[1] for temp in T]
else:
#n = (T[1]-T[0])/step
#TT = linspace(T[0],T[1],n)
... | INPUT PARAMETERS:
M: HITRAN molecule number (required)
I: HITRAN isotopologue number (required)
T: temperature conditions (required)
step: step to calculate temperatures (optional)
OUTPUT PARAMETERS:
TT: list of temperatures (present only... |
def hum1_wei(x,y,n=24):
t = y-1.0j*x
cerf=1/sqrt(pi)*t/(0.5+t**2)
mask = abs(x)+y<15.0
if any(mask):
w24 = weideman(x[mask],y[mask],n)
place(cerf,mask,w24)
return cerf.real,cerf.imag | z = x+1j*y
cerf = 1j*z/sqrt(pi)/(z**2-0.5) |
def PROFILE_SDRAUTIAN(sg0,GamD,Gam0,Gam2,Shift0,Shift2,anuVC,sg):
return pcqsdhc(sg0,GamD,Gam0,Gam2,Shift0,Shift2,anuVC,cZero,sg) | # Speed dependent Rautian profile based on HTP.
# Input parameters:
# sg0 : Unperturbed line position in cm-1 (Input).
# GamD : Doppler HWHM in cm-1 (Input)
# Gam0 : Speed-averaged line-width in cm-1 (Input).
# Gam2 : Speed dependence of the line-width in cm-1... |
def PROFILE_RAUTIAN(sg0,GamD,Gam0,Shift0,anuVC,eta,sg):
return pcqsdhc(sg0,GamD,Gam0,cZero,Shift0,cZero,anuVC,cZero,sg) | # Rautian profile based on HTP.
# Input parameters:
# sg0 : Unperturbed line position in cm-1 (Input).
# GamD : Doppler HWHM in cm-1 (Input)
# Gam0 : Speed-averaged line-width in cm-1 (Input).
# anuVC : Velocity-changing frequency in cm-1 (Input).
# Sh... |
def PROFILE_SDVOIGT(sg0,GamD,Gam0,Gam2,Shift0,Shift2,sg):
return pcqsdhc(sg0,GamD,Gam0,Gam2,Shift0,Shift2,cZero,cZero,sg) | # Speed dependent Voigt profile based on HTP.
# Input parameters:
# sg0 : Unperturbed line position in cm-1 (Input).
# GamD : Doppler HWHM in cm-1 (Input)
# Gam0 : Speed-averaged line-width in cm-1 (Input).
# Gam2 : Speed dependence of the line-width in cm-1 (... |
def PROFILE_VOIGT(sg0,GamD,Gam0,sg):
return PROFILE_HTP(sg0,GamD,Gam0,cZero,cZero,cZero,cZero,cZero,sg) | # Voigt profile based on HTP.
# Input parameters:
# sg0: Unperturbed line position in cm-1 (Input).
# GamD: Doppler HWHM in cm-1 (Input)
# Gam0: Speed-averaged line-width in cm-1 (Input).
# sg: Current WaveNumber of the Computation in cm-1 (Input). |
def PROFILE_DOPPLER(sg0,GamD,sg):
return cSqrtLn2divSqrtPi*exp(-cLn2*((sg-sg0)/GamD)**2)/GamD | # Doppler profile.
# Input parameters:
# sg0: Unperturbed line position in cm-1 (Input).
# GamD: Doppler HWHM in cm-1 (Input)
# sg: Current WaveNumber of the Computation in cm-1 (Input). |
def transmittanceSpectrum(Omegas,AbsorptionCoefficient,Environment={'l':100.},
File=None, Format='%e %e', Wavenumber=None):
# compatibility with older versions
if Wavenumber: Omegas=Wavenumber
l = Environment['l']
Xsect = exp(-AbsorptionCoefficient*l)
if File: save_to_... | INPUT PARAMETERS:
Wavenumber/Omegas: wavenumber grid (required)
AbsorptionCoefficient: absorption coefficient on grid (required)
Environment: dictionary containing path length in cm.
Default={'l':100.}
File: name of the output file ... |
def radianceSpectrum(Omegas,AbsorptionCoefficient,Environment={'l':100.,'T':296.},
File=None, Format='%e %e', Wavenumber=None):
# compatibility with older versions
if Wavenumber: Omegas=Wavenumber
l = Environment['l']
T = Environment['T']
Alw = 1-exp(-AbsorptionCoefficient*... | INPUT PARAMETERS:
Wavenumber/Omegas: wavenumber grid (required)
AbsorptionCoefficient: absorption coefficient on grid (required)
Environment: dictionary containing path length in cm.
and temperature in Kelvin.
Default={'l':100.,'... |
def getStickXY(TableName):
cent,intens = getColumns(TableName,('nu','sw'))
n = len(cent)
cent_ = zeros(n*3)
intens_ = zeros(n*3)
for i in range(n):
intens_[3*i] = 0
intens_[3*i+1] = intens[i]
intens_[3*i+2] = 0
cent_[(3*i):(3*i+3)] = cent[i]
return cent_,inte... | Get X and Y for fine plotting of a stick spectrum.
Usage: X,Y = getStickXY(TableName). |
def read_hotw(filename):
import sys
f = open(filename,'r')
nu = []
coef = []
for line in f:
pars = line.split()
try:
nu.append(float(pars[0]))
coef.append(float(pars[1]))
except:
if False:
print(sys.exc_info())
... | Read cross-section file fetched from HITRAN-on-the-Web.
The format of the file line must be as follows:
nu, coef
Other lines are omitted. |
def SLIT_RECTANGULAR(x,g):
index_inner = abs(x) <= g/2
index_outer = ~index_inner
y = zeros(len(x))
y[index_inner] = 1/g
y[index_outer] = 0
return y | Instrumental (slit) function.
B(x) = 1/γ , if |x| ≤ γ/2 & B(x) = 0, if |x| > γ/2,
where γ is a slit width or the instrumental resolution. |
def SLIT_GAUSSIAN(x,g):
g /= 2
return sqrt(log(2))/(sqrt(pi)*g)*exp(-log(2)*(x/g)**2) | Instrumental (slit) function.
B(x) = sqrt(ln(2)/pi)/γ*exp(-ln(2)*(x/γ)**2),
where γ/2 is a gaussian half-width at half-maximum. |
def SLIT_DIFFRACTION(x,g):
y = zeros(len(x))
index_zero = x==0
index_nonzero = ~index_zero
dk_ = pi/g
x_ = dk_*x[index_nonzero]
w_ = sin(x_)
r_ = w_**2/x_**2
y[index_zero] = 1
y[index_nonzero] = r_/g
return y | Instrumental (slit) function. |
def SLIT_MICHELSON(x,g):
y = zeros(len(x))
index_zero = x==0
index_nonzero = ~index_zero
dk_ = 2*pi/g
x_ = dk_*x[index_nonzero]
y[index_zero] = 1
y[index_nonzero] = 2/g*sin(x_)/x_
return y | Instrumental (slit) function.
B(x) = 2/γ*sin(2pi*x/γ)/(2pi*x/γ) if x!=0 else 1,
where 1/γ is the maximum optical path difference. |
def convolveSpectrumSame(Omega,CrossSection,Resolution=0.1,AF_wing=10.,
SlitFunction=SLIT_RECTANGULAR):
step = Omega[1]-Omega[0]
x = arange(-AF_wing,AF_wing+step,step)
slit = SlitFunction(x,Resolution)
print('step=')
print(step)
print('x=')
print(x)
print('s... | Convolves cross section with a slit function with given parameters. |
def setup_db(self, couch, dbname):
# Avoid race condition of two creating db
my_db = None
self.log.debug('Setting up DB: %s' % dbname)
if dbname not in couch:
self.log.info("DB doesn't exist so creating DB: %s", dbname)
try:
my_db = cou... | Setup and configure DB |
def commit(self, force=False):
self.log.debug('Bulk commit requested')
size = sys.getsizeof(self.docs)
self.log.debug('Size of docs in KB: %d', size)
if size > self.commit_threshold or force:
self.log.info('Commiting %d KB to CouchDB' % size)
self.my_db.... | Commit data to couchdb
Compared to threshold (unless forced) then sends data to couch |
def save(self, doc):
self.log.debug('save()')
self.docs.append(doc)
self.commit() | Save a doc to cache |
def apply_to_last(stream, fn):
''' applies a given function to the last item in a generator/stream '''
assert iterable(stream), 'apply_to_last needs stream to be iterable'
assert callable(fn), 'apply_to_last needs fn to be callable'
stream = iter(stream)
previous = next(stream)
for current in s... | applies a given function to the last item in a generator/stream |
def start(address, channel, key, loop=None):
if loop is None:
loop = asyncio.get_event_loop()
socket = yield from websockets.connect(address+"/robot", loop=loop)
conn = Connection(socket, loop)
yield from conn.send(_create_handshake(channel, key))
return conn | Starts a new Interactive client.
Takes the remote address of the Tetris robot, as well as the
channel number and auth key to use. Additionally, it takes
a list of handler. This should be a dict of protobuf wire
IDs to handler functions (from the .proto package). |
def _create_handshake(channel, key):
hsk = Handshake()
hsk.channel = channel
hsk.streamKey = key
return hsk | Creates and returns a Handshake packet that authenticates
on the channel with the given stream key. |
def import_module(filename):
module_name = "xyz"
module_spec = importlib.util.spec_from_file_location(module_name, filename)
if module_spec is None:
raise RuntimeError("Python cannot import file '{}'".format(filename))
module = importlib.util.module_from_spec(module_spec)
m... | Returns module object
Source: https://www.blog.pythonlibrary.org/2016/05/27/python-201-an-intro-to-importlib/ |
def get_exe_info(dir_, flag_protected=False):
ret = []
# gets all scripts in script directory
ff = glob.glob(os.path.join(dir_, "*.py"))
# discards scripts whose file name starts with a "_"
ff = [f for f in ff if flag_protected or not os.path.basename(f).startswith("_")]
ff.sort()
... | Returns a list of ExeInfo objects, which represent Python scripts within dir_
Args:
dir_: string, path to directory
flag_protected: whether or not to include files starting with a '_'
Returns:
list of ExeInfo objects
The ExeInfo objects represent the ".py" files in direct... |
def collect_doc(module, base_class=None, prefix="", flag_exclude_prefix=False):
ret = []
for attrname in module.__all__:
if prefix and not attrname.startswith(prefix):
continue
attr = module.__getattribute__(attrname)
if base_class is not None and not issubc... | Collects class names and docstrings in module for classes starting with prefix
Arguments:
module -- Python module
prefix -- argument for str.startswith(); if not passed, does not filter
base_class -- filters only descendants of this class
flag_exclude_prefix -- whether or not ... |
def get_classes_in_module(module, superclass=object):
ret = []
for classname in dir(module):
attr = module.__getattribute__(classname)
try:
if issubclass(attr, superclass) and (attr != superclass):
ret.append(attr)
except TypeError:
... | Returns a list with all classes in module that descend from parent
Args:
module: builtins.module
superclass: a class
Returns: list |
def get_obj_doc0(obj, alt="(no doc)"):
ret = obj.__doc__.strip().split("\n")[0] if obj.__doc__ is not None else alt
return ret | Returns first line of cls.__doc__, or alternative text |
def get_subpackages_names(dir_):
def is_package(d):
d = os.path.join(dir_, d)
return os.path.isdir(d) and glob.glob(os.path.join(d, '__init__.py*'))
ret = list(filter(is_package, os.listdir(dir_)))
ret.sort()
return ret | Figures out the names of the subpackages of a package
Args:
dir_: (str) path to package directory
Source: http://stackoverflow.com/questions/832004/python-finding-all-packages-inside-a-package |
def load_params(filepath):
# Read the file
with open(filepath) as file:
content = file.read()
# Detect all environment variables referenced (using %EXAMPLE%, use windows style since it is easier to match)
q = [m.start() for m in re.finditer("%", content)]
env_vars = []
for i in ran... | Load your hyper parameters from a json file.
:param filepath: Path to the json file.
:return: A hyper parameters object. |
def get(self, key, default=_sentinel):
if default is _sentinel:
default = HyperParams({})
return self.__dict__[key] if key in self.__dict__ else default | Get the value specified in the dictionary or a default.
:param key: The key which should be retrieved.
:param default: The default that is returned if the key is not set.
:return: The value from the dict or the default. |
def shell_source(script):
pipe = subprocess.Popen(
". %s; env" % script, stdout=subprocess.PIPE, shell=True)
output = pipe.communicate()[0].decode()
env = {}
for line in output.splitlines():
try:
keyval = line.split("=", 1)
env[keyval[0]] = keyval[1]
... | Sometime you want to emulate the action of "source" in bash,
settings some environment variables. Here is a way to do it. |
def nll(data, model):
try:
log_lik_vals = model.logpmf(data)
except:
log_lik_vals = model.logpdf(data)
return -np.sum(log_lik_vals) | Negative log likelihood given data and a model
Parameters
----------
{0}
{1}
Returns
-------
float
Negative log likelihood
Examples
---------
>>> import macroeco.models as md
>>> import macroeco.compare as comp
>>> # Generate random data
>>> rand_samp = m... |
def AIC_compare(aic_list):
aic_values = np.array(aic_list)
minimum = np.min(aic_values)
delta = aic_values - minimum
values = np.exp(-delta / 2)
weights = values / np.sum(values)
return delta, weights | Calculates delta AIC and AIC weights from a list of AIC values
Parameters
-----------------
aic_list : iterable
AIC values from set of candidat models
Returns
-------------
tuple
First element contains the delta AIC values, second element contains
the relative AIC weigh... |
def sum_of_squares(obs, pred):
return np.sum((np.array(obs) - np.array(pred)) ** 2) | Sum of squares between observed and predicted data
Parameters
----------
obs : iterable
Observed data
pred : iterable
Predicted data
Returns
-------
float
Sum of squares
Notes
-----
The length of observed and predicted data must match. |
def preston_bin(data, max_num):
log_ub = np.ceil(np.log2(max_num))
# Make an exclusive lower bound in keeping with Preston
if log_ub == 0:
boundaries = np.array([0, 1])
elif log_ub == 1:
boundaries = np.arange(1, 4)
else:
boundaries = 2 ** np.arange(0, log_ub + 1)
... | Bins data on base 2 using Preston's method
Parameters
----------
data : array-like
Data to be binned
max_num : float
The maximum upper value of the data
Returns
-------
tuple
(binned_data, bin_edges)
Notes
-----
Uses Preston's method of binning, which ... |
def pueyo_bins(data):
log_ub = np.ceil(np.log2(np.max(data)))
bins = 2**np.arange(log_ub + 1)
binned_data = np.histogram(data, bins=bins)[0]
epdf = (1 / bins[:-1]) * binned_data / len(data)
return binned_data, epdf | Binning method based on Pueyo (2006)
Parameters
----------
data : array-like data
Data to be binned
Returns
-------
: tuple of arrays
binned data, empirical probability density
Notes
-----
Bins the data in into bins of length 2**i, i=0, 1, 2 ...
The empirical p... |
def get_base_url(url, include_path=False):
if not url:
return None
parts = _urlsplit(url)
base_url = _urlunsplit((
parts.scheme, parts.netloc, (parts.path if include_path else ''), None, None
))
return base_url if base_url.endswith('/') else base_url + '/' | :return: the url without the query or fragment segments |
def update_url_params(url, replace_all=False, **url_params):
# Ensure 'replace_all' can be sent as a url param
if not (replace_all is True or replace_all is False):
url_params['replace_all'] = replace_all
if not url or not url_params:
return url or None
scheme, netloc, url_path, ... | :return: url with its query updated from url_query (non-matching params are retained) |
def url_to_parts(url):
if not url:
return None
scheme, netloc, path, query, fragment = _urlsplit(url)
if not path or path == '/':
path = []
else:
path = path.strip('/').split('/')
if not query:
query = {}
else:
query = _parse_qs(query)
return... | Split url urlsplit style, but return path as a list and query as a dict |
def parts_to_url(parts=None, scheme=None, netloc=None, path=None, query=None, fragment=None):
if isinstance(parts, _urllib_parse.SplitResult):
scheme, netloc, path, query, fragment = parts
elif parts and isinstance(parts, dict):
scheme = parts.get('scheme', 'http')
netloc = parts.g... | Build url urlunsplit style, but optionally handle path as a list and/or query as a dict |
def search(self, path, values=None, unique=False, raise_absent=False, vfunc=lambda x: x):
path_and_value_list = iterutils.search(
self.data,
path=path,
required_values=values)
# print 'search found ', [x[0] for x in path_and_value_list]
... | Return single model object instance matching given criteria
:param path: tuple or dpath expression representing the hierarchy/chain of parent keys
:param values: single value or list of values to match. If exact is False then .contains method is used as filter
:param raise_absent: if True then rais... |
def obj(self, path=None, model=None, values=None, raise_absent=False):
return self.search(path=path, unique=True, raise_absent=raise_absent, values=values,
vfunc=lambda x: self.path_index[x[0]].instance(model=model) if x[0] in self.path_index else None) | Return single model object instance matching given criteria
:param path: tuple or dpath expression representing the hierarchy/chain of parent keys
:param values: single value or list of values to match. If exact is False then .contains method is used as filter
:param raise_absent: if True then rais... |
def objs(self, path=None, model=None, values=None, raise_absent=False):
return self.search(path=path, unique=False, raise_absent=raise_absent, values=values,
vfunc=lambda x: self.path_index[x[0]].instance(model=model, reraise=False) if x[0] in self.path_index else None) | Return list of model object instances matching given criteria
:param path: tuple or dpath expression representing the hierarchy/chain of parent keys
:param values: single value or list of values to match. If exact is False then .contains method is used as filter
:param raise_absent: if True then ra... |
def fobj(self, path=None, values=None, unique=True, raise_absent=False):
return self.path_index[self.search(path=path, unique=unique, values=values, raise_absent=raise_absent)[0]] | Return model instance/registration object matching given criteria
:param path: tuple or dpath expression representing the hierarchy/chain of parent keys
:param values: single value or list of values to match. If exact is False then .contains method is used as filter
:param raise_absent: if True ... |
def __visit_index_model_instance(self, models, p, k, v):
# print 'model visit {} on {}'.format(model, v)
cp = p + (k,)
for model in models:
try:
if model.validator(v):
if cp in self.path_index:
# if self.path_in... | Called during model research on merged data |
def compute_edge_reduction(self) -> float:
nb_init_edge = self.init_edge_number()
nb_poweredge = self.edge_number()
return (nb_init_edge - nb_poweredge) / (nb_init_edge) | Compute the edge reduction. Costly computation |
def init_edge_number(self) -> int:
return len(frozenset(frozenset(edge) for edge in self.initial_edges())) | Return the number of edges present in the non-compressed graph |
def initial_edges(self) -> iter:
nodes_in = lambda n: ([n] if self.is_node(n) else self.nodes_in(n))
for node, succs in self.edges.items():
twos = tuple(two for succ in succs for two in nodes_in(succ))
for one in nodes_in(node):
for two in twos:
... | Yield edges in the initial (uncompressed) graphs. Possible doublons. |
def connected_components(self) -> (dict, dict):
inclusions = utils.completed_graph(self.inclusions) # allow bottom-up movement
edges = utils.completed_graph(self.edges) if self.oriented else self.edges
cc = {} # maps cc root with nodes in the cc
subroots = defaultdict(set) # ... | Return for one root of each connected component all
the linked objects, and the mapping linking a connected component
root with the roots that it contains. |
def assert_powernode(self, name:str) -> None or ValueError:
if name not in self.inclusions:
raise ValueError("Powernode '{}' does not exists.".format(name))
if self.is_node(name):
raise ValueError("Given name '{}' is a node.".format(name)) | Do nothing if given name refers to a powernode in given graph.
Raise a ValueError in any other case. |
def powernode_data(self, name:str) -> Powernode:
self.assert_powernode(name)
contained_nodes = frozenset(self.nodes_in(name))
return Powernode(
size=len(contained_nodes),
contained=frozenset(self.all_in(name)),
contained_pnodes=frozenset(self.powernod... | Return a Powernode object describing the given powernode |
def node_number(self, *, count_pnode=True) -> int:
return (sum(1 for n in self.nodes())
+ (sum(1 for n in self.powernodes()) if count_pnode else 0)) | Return the number of node |
def edge_number(self) -> int:
edges = set()
for node, succs in self.edges.items():
for succ in succs:
edges.add(frozenset((node, succ)))
return len(edges) | Return the number of (power) edges |
def nodes(self) -> iter:
yield from (elem for elem, subs in self.inclusions.items() if subs == ()) | Yield all nodes in the graph (not the powernodes) |
def powernodes(self) -> iter:
yield from (elem for elem, subs in self.inclusions.items() if subs != ()) | Yield all powernodes in the graph (not the nodes) |
def nodes_in(self, name) -> iter:
yield from (node for node in self.all_in(name) if self.is_node(node)) | Yield all nodes contained in given (power) node |
def powernodes_in(self, name) -> iter:
yield from (node for node in self.all_in(name) if self.is_powernode(node)) | Yield all power nodes contained in given (power) node |
def all_in(self, name) -> iter:
for elem in self.inclusions[name]:
yield elem
yield from self.all_in(elem) | Yield all (power) nodes contained in given (power) node |
def powernodes_containing(self, name, directly=False) -> iter:
if directly:
yield from (node for node in self.all_in(name)
if name in self.inclusions[node])
else:
# This algorithm is very bad. Inverting the inclusion dict could
# be f... | Yield all power nodes containing (power) node of given *name*.
If *directly* is True, will only yield the direct parent of given name. |
def write_bubble(self, filename:str):
from bubbletools import converter
converter.tree_to_bubble(self, filename) | Write in given filename the lines of bubble describing this instance |
def from_bubble_file(bblfile:str, oriented:bool=False,
symmetric_edges:bool=True) -> 'BubbleTree':
return BubbleTree.from_bubble_data(utils.data_from_bubble(bblfile),
oriented=bool(oriented),
... | Extract data from given bubble file,
then call from_bubble_data method |
def from_bubble_lines(bbllines:iter, oriented:bool=False,
symmetric_edges:bool=True) -> 'BubbleTree':
return BubbleTree.from_bubble_data((utils.line_data(line)
for line in bbllines),
oriente... | Return a BubbleTree instance.
bbllines -- iterable of raw line, bubble-formatted
oriented -- True: returned BubbleTree is oriented |
def same_network(atree, btree) -> bool:
return same_hierarchy(atree, btree) and same_topology(atree, btree) | True if given trees share the same structure of powernodes,
independently of (power)node names,
and same edge topology between (power)nodes. |
def set_from_tree(root:str, graph:dict) -> frozenset:
Node = namedtuple('Node', 'id succs')
succs = graph[root]
if succs:
return (len(succs), sorted(tuple(set_from_tree(succ, graph) for succ in succs)))
else:
return 0, () | Return a recursive structure describing given tree |
def get_suitable_vis_classes(obj):
ret = []
for class_ in classes_vis():
if isinstance(obj, class_.input_classes):
ret.append(class_)
return ret | Retuns a list of Vis classes that can handle obj. |
def get_suitable_vis_list_classes(objs):
from f311 import explorer as ex
ret = []
for class_ in classes_vis():
if isinstance(class_, ex.VisList):
flag_can = True
for obj in objs:
if not isinstance(obj, class_.item_input_classes):
fla... | Retuns a list of VisList classes that can handle a list of objects. |
def classes_file(flag_leaf=False):
if __flag_first:
__setup()
if not flag_leaf:
return _classes_file
return [cls for cls in _classes_file if cls not in _classes_file_superclass] | All known File* classes
Args:
flag_leaf: returns only classes that do not have subclasses
("leaf" nodes as in a class tree graph) |
def _collect_classes(m):
from f311 import filetypes as ft
from f311 import explorer as ex
def _extend(classes, newclasses):
"""Filters out classes already present in list.
This shouldn't be necessary, but collaborators may accidentally import already loaded
classes into the da... | Adds entries to _classes_*
Args:
m: module object that must contain the following sub-modules: datatypes, vis |
def __setup():
global __collaborators, __flag_first
import f311
__flag_first = False
for pkgname in f311.COLLABORATORS_C:
try:
pkg = importlib.import_module(pkgname)
a99.get_python_logger().info("Imported collaborator package '{}'".format(pkgname))
try... | Will be executed in the first time someone calls classes_*() |
def _get_programs_dict():
global __programs_dict
if __programs_dict is not None:
return __programs_dict
d = __programs_dict = OrderedDict()
for pkgname in COLLABORATORS_S:
try:
package = importlib.import_module(pkgname)
except ImportError:
# I thin... | Builds and returns programs dictionary
This will have to import the packages in COLLABORATORS_S in order to get their absolute path.
Returns:
dictionary: {"packagename": [ExeInfo0, ...], ...}
"packagename" examples: "f311.explorer", "numpy" |
def get_programs_dict(pkgname_only=None, flag_protected=False):
___ret = _get_programs_dict()
__ret = ___ret if pkgname_only is None else OrderedDict(((pkgname_only, ___ret[pkgname_only]),))
if flag_protected:
_ret = __ret
else:
_ret = copy.deepcopy(__ret)
for value in _ret... | Scans COLLABORATORS_S packages for scripts, eventually filtering if arguments passed
Args:
pkgname_only: name of single package within COLLABORATORS_S
flag_protected: include scripts starting with "_"?
Returns:
dictionary: {"packagename0": {"exeinfo": [ExeInfo00, ...], "description": d... |
def get(self, term):
# type: (Any) -> Type[ChomskyTermNonterminal]
if self._items[term].used is False:
cont = self._items[term]
self._grammar.nonterminals.add(cont.nonterminal)
self._grammar.rules.add(cont.rule)
cont.used = True
return sel... | Get nonterminal rewritable to term.
If the rules is not in the grammar, nonterminal and rule rewritable to terminal are add into grammar.
:param term: Term for which get the nonterminal.
:return: ChomskyTermNonterminal class for terminal. |
def _build_url(*args, **kwargs) -> str:
resource_url = API_RESOURCES_URLS
for key in args:
resource_url = resource_url[key]
if kwargs:
resource_url = resource_url.format(**kwargs)
return urljoin(URL, resource_url) | Return a valid url. |
def _get(url:str, headers:dict) -> dict:
response = requests.get(url, headers=headers)
data = response.json()
if response.status_code != 200:
raise GoogleApiError({"status_code": response.status_code,
"error": data.get("error", "")})
return data | Make a GET call. |
def _post(url:str, params:dict, headers:dict) -> dict:
response = requests.post(url, params=params, headers=headers)
data = response.json()
if response.status_code != 200 or "error" in data:
raise GoogleApiError({"status_code": response.status_code,
"error": da... | Make a POST call. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.