code
string
signature
string
docstring
string
loss_without_docstring
float64
loss_with_docstring
float64
factor
float64
self._get_fitness(self.swarm) i = 0 self.i = i while True: for particle in self.swarm: if ((self._gbest.fitness)<particle.fitness): self._gbest = particle.copy() if (particle.fitness > particle.pbest.fitness): ...
def _sample(self, maxIter=1000, c1=1.193, c2=1.193, lookback = 0.25, standard_dev = None)
Launches the PSO. Yields the complete swarm per iteration :param maxIter: maximum iterations :param c1: cognitive weight :param c2: social weight :param lookback: percentange of particles to use when determining convergence :param standard_dev: standard deviation of the last loo...
3.612111
3.358559
1.075494
gBests = [] for swarm in self._sample(maxIter, c1, c2, lookback, standard_dev): #swarms.append(swarm) gBests.append(self._gbest.copy()) return gBests
def _optimize(self, maxIter=1000, c1=1.193, c2=1.193, lookback=0.25, standard_dev=None)
:param maxIter: maximum number of swarm iterations :param c1: social weight :param c2: personal weight :param lookback: how many particles to assess when considering convergence :param standard_dev: the standard deviation of the last lookback # of particles used to determine convergence ...
4.742669
5.041147
0.940792
return Particle(numpy.array([[]]*paramCount), numpy.array([[]]*paramCount), -numpy.Inf)
def create(cls, paramCount)
Creates a new particle without position, velocity and -inf as fitness
14.585196
6.159806
2.367801
return Particle(copy(self.position), copy(self.velocity), self.fitness)
def copy(self)
Creates a copy of itself
9.149858
7.620312
1.20072
for imageModel in self._imageModel_list: imageModel.reset_point_source_cache(bool=bool)
def reset_point_source_cache(self, bool=True)
deletes all the cache in the point source class and saves it from then on :return:
4.956447
6.453341
0.768044
I_xy = copy.deepcopy(I_xy_input) background = np.minimum(0, np.min(I_xy)) I_xy -= background x_ = np.sum(I_xy * x) y_ = np.sum(I_xy * y) r = (np.max(x) - np.min(x)) / 3. mask = mask_util.mask_sphere(x, y, center_x=x_, center_y=y_, r=r) Q_xx = np.sum(I_xy * mask * (x - x_) ** 2) ...
def moments(I_xy_input, x, y)
compute quadrupole moments from a light distribution :param I_xy: light distribution :param x: x-coordinates of I_xy :param y: y-coordinates of I_xy :return: Q_xx, Q_xy, Q_yy
2.354317
2.364901
0.995524
Q_xx, Q_xy, Q_yy, bkg = moments(I_xy, x, y) norm = Q_xx + Q_yy + 2 * np.sqrt(Q_xx*Q_yy - Q_xy**2) e1 = (Q_xx - Q_yy) / norm e2 = 2 * Q_xy / norm return e1 / (1+bkg), e2 / (1+bkg)
def ellipticities(I_xy, x, y)
compute ellipticities of a light distribution :param I_xy: :param x: :param y: :return:
2.727539
3.12084
0.873976
x = np.zeros_like(theta_x) y = np.zeros_like(theta_y) alpha_x = theta_x alpha_y = theta_y i = -1 for i, idex in enumerate(self._sorted_redshift_index): delta_T = self._T_ij_list[i] x, y = self._ray_step(x, y, alpha_x, alpha_y, delta_T) ...
def ray_shooting(self, theta_x, theta_y, kwargs_lens, k=None)
ray-tracing (backwards light cone) :param theta_x: angle in x-direction on the image :param theta_y: angle in y-direction on the image :param kwargs_lens: :return: angles in the source plane
2.8411
2.945302
0.964621
z_lens_last = z_start first_deflector = True for i, idex in enumerate(self._sorted_redshift_index): z_lens = self._redshift_list[idex] if self._start_condition(include_z_start, z_lens, z_start) and z_lens <= z_stop: #if z_lens > z_start and z_lens <= ...
def ray_shooting_partial(self, x, y, alpha_x, alpha_y, z_start, z_stop, kwargs_lens, keep_range=False, include_z_start=False)
ray-tracing through parts of the coin, starting with (x,y) and angles (alpha_x, alpha_y) at redshift z_start and then backwards to redshfit z_stop :param x: co-moving position [Mpc] :param y: co-moving position [Mpc] :param alpha_x: ray angle at z_start [arcsec] :param alpha_y: ...
2.056026
2.041523
1.007104
z_lens_last = z_start first_deflector = True pos_x, pos_y, redshifts, Tz_list = [], [], [], [] pos_x.append(x) pos_y.append(y) redshifts.append(z_start) Tz_list.append(self._cosmo_bkg.T_xy(0, z_start)) current_z = z_lens_last for i, ide...
def ray_shooting_partial_steps(self, x, y, alpha_x, alpha_y, z_start, z_stop, kwargs_lens, include_z_start=False)
ray-tracing through parts of the coin, starting with (x,y) and angles (alpha_x, alpha_y) at redshift z_start and then backwards to redshfit z_stop. This function differs from 'ray_shooting_partial' in that it returns the angular position of the ray at each lens plane. :param x: co-movi...
2.39077
2.38418
1.002764
dt_grav = np.zeros_like(theta_x) dt_geo = np.zeros_like(theta_x) x = np.zeros_like(theta_x) y = np.zeros_like(theta_y) alpha_x = theta_x alpha_y = theta_y i = 0 for i, idex in enumerate(self._sorted_redshift_index): z_lens = self._reds...
def arrival_time(self, theta_x, theta_y, kwargs_lens, k=None)
light travel time relative to a straight path through the coordinate (0,0) Negative sign means earlier arrival time :param theta_x: angle in x-direction on the image :param theta_y: angle in y-direction on the image :param kwargs_lens: :return: travel time in unit of days
2.473031
2.522358
0.980444
beta_x, beta_y = self.ray_shooting(theta_x, theta_y, kwargs_lens) alpha_x = theta_x - beta_x alpha_y = theta_y - beta_y return alpha_x, alpha_y
def alpha(self, theta_x, theta_y, kwargs_lens, k=None)
reduced deflection angle :param theta_x: angle in x-direction :param theta_y: angle in y-direction :param kwargs_lens: lens model kwargs :return:
1.950276
2.360493
0.826216
alpha_ra, alpha_dec = self.alpha(theta_x, theta_y, kwargs_lens) alpha_ra_dx, alpha_dec_dx = self.alpha(theta_x + diff, theta_y, kwargs_lens) alpha_ra_dy, alpha_dec_dy = self.alpha(theta_x, theta_y + diff, kwargs_lens) dalpha_rara = (alpha_ra_dx - alpha_ra)/diff dalpha...
def hessian(self, theta_x, theta_y, kwargs_lens, k=None, diff=0.00000001)
computes the hessian components f_xx, f_yy, f_xy from f_x and f_y with numerical differentiation :param theta_x: x-position (preferentially arcsec) :type theta_x: numpy array :param theta_y: y-position (preferentially arcsec) :type theta_y: numpy array :param kwargs_lens: list o...
1.60259
1.50679
1.063579
factor = self._reduced2physical_factor[idex_lens] #factor = self._cosmo_bkg.D_xy(0, z_source) / self._cosmo_bkg.D_xy(z_lens, z_source) return alpha_reduced * factor
def _reduced2physical_deflection(self, alpha_reduced, idex_lens)
alpha_reduced = D_ds/Ds alpha_physical :param alpha_reduced: reduced deflection angle :param z_lens: lens redshift :param z_source: source redshift :return: physical deflection angle
4.837858
4.438681
1.089932
dt_days = (alpha_x**2 + alpha_y**2) / 2. * delta_T * const.Mpc / const.c / const.day_s * const.arcsec**2 return dt_days
def _geometrical_delay(self, alpha_x, alpha_y, delta_T)
geometrical delay (evaluated at z=0) of a light ray with an angle relative to the shortest path :param alpha_x: angle relative to a straight path :param alpha_y: angle relative to a straight path :param delta_T: transversal diameter distance between the start and end of the ray :return:...
6.028872
5.807637
1.038094
D_dt = self._cosmo_bkg.D_dt(z_lens, z_source) delay_days = const.delay_arcsec2days(potential, D_dt) return delay_days
def _lensing_potential2time_delay(self, potential, z_lens, z_source)
transforms the lensing potential (in units arcsec^2) to a gravitational time-delay as measured at z=0 :param potential: lensing potential :param z_lens: redshift of the deflector :param z_source: redshift of source for the definition of the lensing quantities :return: gravitational time...
7.240554
9.2052
0.786572
T_z = self._T_z_list[idex] #T_z = self._cosmo_bkg.T_xy(0, z_lens) theta_x = x / T_z theta_y = y / T_z return theta_x, theta_y
def _co_moving2angle(self, x, y, idex)
transforms co-moving distances Mpc into angles on the sky (radian) :param x: co-moving distance :param y: co-moving distance :param z_lens: redshift of plane :return: angles on the sky
5.264258
4.700934
1.119832
T_z = self._T_z_source theta_x = x / T_z theta_y = y / T_z return theta_x, theta_y
def _co_moving2angle_source(self, x, y)
special case of the co_moving2angle definition at the source redshift :param x: :param y: :return:
3.998833
4.297105
0.930588
x_ = x + alpha_x * delta_T y_ = y + alpha_y * delta_T return x_, y_
def _ray_step(self, x, y, alpha_x, alpha_y, delta_T)
ray propagation with small angle approximation :param x: co-moving x-position :param y: co-moving y-position :param alpha_x: deflection angle in x-direction at (x, y) :param alpha_y: deflection angle in y-direction at (x, y) :param delta_T: transversal angular diameter distance ...
2.24457
3.161638
0.709939
theta_x, theta_y = self._co_moving2angle(x, y, idex) alpha_x_red, alpha_y_red = self._lens_model.alpha(theta_x, theta_y, kwargs_lens, k=self._sorted_redshift_index[idex]) alpha_x_phys = self._reduced2physical_deflection(alpha_x_red, idex) alpha_y_phys = self._reduced2physical_de...
def _add_deflection(self, x, y, alpha_x, alpha_y, kwargs_lens, idex)
adds the pyhsical deflection angle of a single lens plane to the deflection field :param x: co-moving distance at the deflector plane :param y: co-moving distance at the deflector plane :param alpha_x: physical angle (radian) before the deflector plane :param alpha_y: physical angle (ra...
2.828882
2.493821
1.134357
x_shift = x - center_x y_shift = y - center_y R = np.sqrt(x_shift*x_shift + y_shift*y_shift) if isinstance(R, int) or isinstance(R, float): a = theta_E / max(0.000001, R) else: a=np.empty_like(R) r = R[R > 0] #in the SIS regime ...
def derivatives(self, x, y, theta_E, center_x=0, center_y=0)
returns df/dx and df/dy of the function
2.669643
2.682524
0.995198
x_shift = x - center_x y_shift = y - center_y R = (x_shift*x_shift + y_shift*y_shift)**(3./2) if isinstance(R, int) or isinstance(R, float): prefac = theta_E / max(0.000001, R) else: prefac = np.empty_like(R) r = R[R>0] #in the SIS re...
def hessian(self, x, y, theta_E, center_x=0, center_y=0)
returns Hessian matrix of function d^2f/dx^2, d^f/dy^2, d^2/dxdy
2.735056
2.738826
0.998624
x_shift = x - center_x y_shift = y - center_y A = np.pi * a_x * a_y dist = (x_shift/a_x)**2 + (y_shift/a_y)**2 torus = np.zeros_like(x) torus[dist <= 1] = 1 return amp/A * torus
def function(x, y, amp, a_x, a_y, center_x, center_y)
returns torus (ellipse with constant surface brightnes) profile
2.729439
2.41393
1.130704
return mp.hyp2f1(a, b, c, z)
def hyp_2F1(a, b, c, z)
http://docs.sympy.org/0.7.1/modules/mpmath/functions/hypergeometric.html
4.076663
2.71386
1.502164
dx, dy = self.alpha(x, y, kwargs, k=k) return x - dx, y - dy
def ray_shooting(self, x, y, kwargs, k=None)
maps image to source position (inverse deflection) :param x: x-position (preferentially arcsec) :type x: numpy array :param y: y-position (preferentially arcsec) :type y: numpy array :param kwargs: list of keyword arguments of lens model parameters matching the lens model classes...
4.764486
6.545517
0.727901
potential = self.potential(x_image, y_image, kwargs_lens, k=k) geometry = ((x_image - x_source)**2 + (y_image - y_source)**2) / 2. return geometry - potential
def fermat_potential(self, x_image, y_image, x_source, y_source, kwargs_lens, k=None)
fermat potential (negative sign means earlier arrival time) :param x_image: image position :param y_image: image position :param x_source: source position :param y_source: source position :param kwargs_lens: list of keyword arguments of lens model parameters matching the lens mo...
2.96584
3.713108
0.798749
x = np.array(x, dtype=float) y = np.array(y, dtype=float) bool_list = self._bool_list(k) x_, y_, kwargs_copy = self._update_foreground(x, y, kwargs) potential = np.zeros_like(x) for i, func in enumerate(self.func_list): if bool_list[i] is True: ...
def potential(self, x, y, kwargs, k=None)
lensing potential :param x: x-position (preferentially arcsec) :type x: numpy array :param y: y-position (preferentially arcsec) :type y: numpy array :param kwargs: list of keyword arguments of lens model parameters matching the lens model classes :param k: only evaluate ...
3.138692
3.060693
1.025484
x = np.array(x, dtype=float) y = np.array(y, dtype=float) bool_list = self._bool_list(k) x_, y_, kwargs_copy = self._update_foreground(x, y, kwargs) f_x, f_y = np.zeros_like(x_), np.zeros_like(x_) for i, func in enumerate(self.func_list): if bool_lis...
def alpha(self, x, y, kwargs, k=None)
deflection angles :param x: x-position (preferentially arcsec) :type x: numpy array :param y: y-position (preferentially arcsec) :type y: numpy array :param kwargs: list of keyword arguments of lens model parameters matching the lens model classes :param k: only evaluate ...
2.402149
2.370913
1.013174
x = np.array(x, dtype=float) y = np.array(y, dtype=float) if self._foreground_shear: # needs to be computed numerically due to non-linear effects f_xx, f_xy, f_yx, f_yy = self.hessian_differential(x, y, kwargs, k=k) else: bool_list = self._boo...
def hessian(self, x, y, kwargs, k=None)
hessian matrix :param x: x-position (preferentially arcsec) :type x: numpy array :param y: y-position (preferentially arcsec) :type y: numpy array :param kwargs: list of keyword arguments of lens model parameters matching the lens model classes :param k: only evaluate the...
2.474805
2.448004
1.010948
bool_list = self._bool_list(bool_list) mass_3d = 0 for i, func in enumerate(self.func_list): if bool_list[i] is True: kwargs_i = {k:v for k, v in kwargs[i].items() if not k in ['center_x', 'center_y']} mass_3d_i = func.mass_3d_lens(r, **kwargs...
def mass_3d(self, r, kwargs, bool_list=None)
computes the mass within a 3d sphere of radius r :param r: radius (in angular units) :param kwargs: list of keyword arguments of lens model parameters matching the lens model classes :param bool_list: list of bools that are part of the output :return: mass (in angular units, modulo epsi...
2.944845
2.898285
1.016065
bool_list = self._bool_list(bool_list) mass_2d = 0 for i, func in enumerate(self.func_list): if bool_list[i] is True: kwargs_i = {k: v for k, v in kwargs[i].items() if not k in ['center_x', 'center_y']} mass_2d_i = func.mass_2d_lens(r, **kwarg...
def mass_2d(self, r, kwargs, bool_list=None)
computes the mass enclosed a projected (2d) radius r :param r: radius (in angular units) :param kwargs: list of keyword arguments of lens model parameters matching the lens model classes :param bool_list: list of bools that are part of the output :return: projected mass (in angular unit...
2.904019
2.875254
1.010004
n = len(self.func_list) if k is None: bool_list = [True] * n elif isinstance(k, (int, np.integer)): bool_list = [False] * n bool_list[k] = True else: bool_list = [False] * n for i, k_i in enumerate(k): i...
def _bool_list(self, k=None)
returns a bool list of the length of the lens models if k = None: returns bool list with True's if k is int, returns bool list with False's but k'th is True :param k: None, int, or list of ints :return: bool list
3.133973
3.06535
1.022386
''' generates a open namespace from symbol namespace x { y { z {''' blocks = ['namespace {0} {{'.format(x) for x in symbol.module.name_parts] return ' '.join(blocks)
def open_ns(symbol)
generates a open namespace from symbol namespace x { y { z {
16.46858
6.637915
2.480987
'''generates a closing names statement from a symbol''' closing = ' '.join(['}' for x in symbol.module.name_parts]) name = '::'.join(symbol.module.name_parts) return '{0} // namespace {1}'.format(closing, name)
def close_ns(symbol)
generates a closing names statement from a symbol
9.571106
7.038645
1.359794
'''generates a namespace x::y::z statement from a symbol''' if symbol.type and symbol.type.is_primitive: return '' return '{0}::'.format('::'.join(symbol.module.name_parts))
def ns(symbol)
generates a namespace x::y::z statement from a symbol
10.259564
5.764815
1.779687
config = Path(config) if reload: argv = sys.argv.copy() argv.remove('--reload') monitor(config.dirname(), src, dst, argv) else: run(config, src, dst, force)
def app(config, src, dst, features, reload, force)
Takes several files or directories as src and generates the code in the given dst directory.
5.28922
5.512682
0.959464
script = Path(script).expand().abspath() output = Path(output).expand().abspath() input = input if isinstance(input, (list, tuple)) else [input] output.makedirs_p() _script_reload(script, input, output)
def reload(script, input, output)
reloads the generator script when the script files or the input files changes
3.845479
3.940369
0.975919
input = [Path(entry).expand().abspath() for entry in input] output = Path(output).expand().abspath() cmd = 'python3 {0} {1} {2}'.format(script, ' '.join(input), output) event_handler = RunScriptChangeHandler(cmd) event_handler.run() # run always once observer = Observer() path = script...
def _script_reload(script, input, output)
run the named generator and monitor the input and generator folder
2.689855
2.653737
1.01361
script_dir = str(Path(__file__).parent.abspath()) click.secho(script_dir, fg='blue') if editable: sh('pip3 install --editable {0} --upgrade'.format(script_dir)) else: sh('pip3 install {0} --upgrade'.format(script_dir))
def install(editable)
install the script onto the system using pip3
3.174756
2.942548
1.078914
"merges b into a recursively if a and b are dicts" for key in b: if isinstance(a.get(key), dict) and isinstance(b.get(key), dict): merge(a[key], b[key]) else: a[key] = b[key] return a
def merge(a, b)
merges b into a recursively if a and b are dicts
2.300357
2.002082
1.148982
source = name if name and name[0] is '/': source = name[1:] elif self.source is not None: source = '/'.join((self.source, name)) return self.env.get_template(source)
def get_template(self, name)
Retrieves a single template file from the template loader
3.641528
3.327896
1.094243
if Generator.strict: self.env.undefined = TestableUndefined else: self.env.undefined = Undefined template = self.get_template(name) return template.render(context)
def render(self, name, context)
Returns the rendered text from a single template file from the template loader using the given context data
5.848664
5.883265
0.994119
context.update(self.context) return self.env.from_string(template).render(context)
def apply(self, template, context={})
Return the rendered text of a template instance
5.104401
6.034329
0.845894
if not file_path or not template: click.secho('source or target missing for document') return if not context: context = self.context error = False try: self._write(file_path, template, context, preserve, force) except Templ...
def write(self, file_path, template, context={}, preserve=False, force=False)
Using a template file name it renders a template into a file given a context
3.321839
3.370625
0.985526
self.context.update({ 'system': system, }) document = FileSystem.load_yaml(path, required=True) for module, rules in document.items(): click.secho('process: {0}'.format(module), fg='green') self._process_rules(rules, system)
def process_rules(self, path: Path, system: System)
writes the templates read from the rules document
6.054622
4.930481
1.227998
self._source = None # reset the template source if not self._shall_proceed(rules): return self.context.update(rules.get('context', {})) self.path = rules.get('path', '') self.source = rules.get('source', None) self._process_rule(rules.get('system', No...
def _process_rules(self, rules: dict, system: System)
process a set of rules for a target
2.558715
2.471876
1.035131
if not rule or not self._shall_proceed(rule): return self.context.update(context) self.context.update(rule.get('context', {})) self.path = rule.get('path', None) self.source = rule.get('source', None) for entry in rule.get('documents', []): ...
def _process_rule(self, rule: dict, context: dict)
process a single rule
3.31749
3.112894
1.065725
logger.debug('parse document: {0}'.format(document)) stream = FileStream(str(document), encoding='utf-8') system = FileSystem._parse_stream(stream, system, document, profile) FileSystem.merge_annotations(system, document.stripext() + '.yaml') return system
def _parse_document(document: Path, system: System = None, profile=EProfile.FULL)
Parses a document and returns the resulting domain system :param path: document path to parse :param system: system to be used (optional)
7.148089
7.468143
0.957144
if not Path(document).exists(): return meta = FileSystem.load_yaml(document) click.secho('merge: {0}'.format(document.name), fg='blue') for identifier, data in meta.items(): symbol = system.lookup(identifier) if symbol: merge(s...
def merge_annotations(system, document)
Read a YAML document and for each root symbol identifier updates the tag information of that symbol
7.063753
5.305212
1.331474
inputs = input if isinstance(input, (list, tuple)) else [input] logger.debug('parse input={0}'.format(inputs)) identifier = 'system' if not identifier else identifier system = System() cache = None if use_cache: cache = shelve.open('qface.cache') ...
def parse(input, identifier: str = None, use_cache=False, clear_cache=True, pattern="*.qface", profile=EProfile.FULL)
Input can be either a file or directory or a list of files or directory. A directory will be parsed recursively. The function returns the resulting system. Stores the result of the run in the domain cache named after the identifier. :param path: directory to parse :param identifier: ide...
3.90022
3.810062
1.023663
watch = watch if isinstance(watch, (list, tuple)) else [watch] watch = [Path(entry).expand().abspath() for entry in watch] event_handler = RunScriptChangeHandler(args) observer = Observer() for entry in watch: if entry.isfile(): entry = entry.parent click.secho('watc...
def monitor(args, watch)
reloads the script given by argv when src files changes
3.636063
3.514169
1.034686
'''lookup a symbol by fully qualified name.''' # <module> if name in self._moduleMap: return self._moduleMap[name] # <module>.<Symbol> (module_name, type_name, fragment_name) = self.split_typename(name) if not module_name and type_name: click.secho...
def lookup(self, name: str)
lookup a symbol by fully qualified name.
4.727326
4.210304
1.122799
'''return the fully qualified name (`<module>.<name>`)''' if self.module == self: return self.module.name else: if "." not in self.name: return '{0}.{1}'.format(self.module.name, self.name) else: # We have a fully qualified refe...
def qualified_name(self)
return the fully qualified name (`<module>.<name>`)
4.644251
3.815457
1.21722
if tag not in self._tags: self._tags[tag] = dict()
def add_tag(self, tag)
add a tag to the tag list
4.645204
4.450225
1.043813
self.add_tag(tag) d = self._tags[tag] d[name] = value
def add_attribute(self, tag, name, value)
add an attribute (nam, value pair) to the named tag
4.287236
3.95124
1.085035
if tag in self._tags and name in self._tags[tag]: return self._tags[tag][name]
def attribute(self, tag, name)
return attribute by tag and attribute name
2.755986
2.498414
1.103094
'''checks if type is a valid type''' return (self.is_primitive and self.name) \ or (self.is_complex and self.name) \ or (self.is_list and self.nested) \ or (self.is_map and self.nested) \ or (self.is_model and self.nested)
def is_valid(self)
checks if type is a valid type
3.682385
3.317441
1.110008
self.__is_resolved = True if self.is_complex: type = self.nested if self.nested else self type.__reference = self.module.lookup(type.name)
def _resolve(self)
resolve the type symbol from name by doing a lookup
12.199239
9.780742
1.247271
'''lookup a symbol by name. If symbol is not local it will be looked up system wide''' if name in self._contentMap: symbol = self._contentMap[name] if fragment: return symbol._contentMap[fragment] return symbol return self.system.lookup...
def lookup(self, name: str, fragment: str = None)
lookup a symbol by name. If symbol is not local it will be looked up system wide
7.211608
3.583485
2.012457
'''return the fully qualified name (`<module>.<interface>#<operation>`)''' return '{0}.{1}#{2}'.format(self.module.name, self.interface.name, self.name)
def qualified_name(self)
return the fully qualified name (`<module>.<interface>#<operation>`)
6.76404
3.210894
2.106591
'''return the fully qualified name (`<module>.<struct>#<field>`)''' return '{0}.{1}#{2}'.format(self.module.name, self.struct.name, self.name)
def qualified_name(self)
return the fully qualified name (`<module>.<struct>#<field>`)
7.232825
3.265759
2.214745
'''return the fully qualified name (`<module>.<enum>#<member>`)''' return '{0}.{1}#{2}'.format(self.module.name, self.enum.name, self.name)
def qualified_name(self)
return the fully qualified name (`<module>.<enum>#<member>`)
7.627668
3.147005
2.423787
try: # all symbols have a toJson method, try it return json.dumps(symbol.toJson(), indent=' ') except AttributeError: pass return json.dumps(symbol, indent=' ')
def jsonify(symbol)
returns json format for symbol
5.80531
6.504834
0.892461
code = hashlib.new(hash_type) code.update(str(symbol).encode('utf-8')) return code.hexdigest()
def hash(symbol, hash_type='sha1')
create a hash code from symbol
2.331671
2.031215
1.147919
if isinstance(args, str): args = args.split() if not args: return click.echo('$ {0}'.format(' '.join(args))) try: return subprocess.check_call(args, **kwargs) except subprocess.CalledProcessError as exc: click.secho('run error {}'.format(exc)) except OSError ...
def sh(args, **kwargs)
runs the given cmd as shell command
2.798756
2.717623
1.029854
if not s: return doc = DocObject() tag = None s = s[3:-2] # remove '/**' and '*/' for line in s.splitlines(): line = line.lstrip(' *') # strip a ' ' and '*' from start if not line: tag = None # on empty line reset the tag information elif line[0] =...
def parse_doc(s)
parse a comment in the format of JavaDoc and returns an object, where each JavaDoc tag is a property of the object.
3.564255
3.391141
1.051049
url_username = request.parser_context.get('kwargs', {}).get('username', '') if request.user.username.lower() != url_username.lower(): if request.user.is_staff: return False # staff gets 403 raise Http404() return True
def has_permission(self, request, view)
Returns true if the current request is by the user themselves. Note: a 404 is returned for non-staff instead of a 403. This is to prevent users from being able to detect the existence of accounts.
4.364127
3.96242
1.101379
conn_module = type(connection).__module__ if "mysql" in conn_module: return "bigint AUTO_INCREMENT" elif "postgres" in conn_module: return "bigserial" return super(BigAutoField, self).db_type(connection)
def db_type(self, connection)
The type of the field to insert into the database.
3.438274
3.213626
1.069905
# Raise ValueError to match normal django semantics for wrong type of field. if not isinstance(course_key, CourseKey): raise ValueError( "course_key must be an instance of `opaque_keys.edx.keys.CourseKey`. Got {}".format(type(course_key)) ) try:...
def submit_completion(self, user, course_key, block_key, completion)
Update the completion value for the specified record. Parameters: * user (django.contrib.auth.models.User): The user for whom the completion is being submitted. * course_key (opaque_keys.edx.keys.CourseKey): The course in which the submitted block is found. ...
2.839891
2.721065
1.043669
block_completions = {} for block, completion in blocks: (block_completion, is_new) = self.submit_completion(user, course_key, block, completion) block_completions[block_completion] = is_new return block_completions
def submit_batch_completion(self, user, course_key, blocks)
Performs a batch insertion of completion objects. Parameters: * user (django.contrib.auth.models.User): The user for whom the completions are being submitted. * course_key (opaque_keys.edx.keys.CourseKey): The course in which the submitted blocks are found. ...
3.323028
2.84256
1.169027
if self.block_key.run is None: # pylint: disable=unexpected-keyword-arg, no-value-for-parameter return self.block_key.replace(course_key=self.course_key) return self.block_key
def full_block_key(self)
Returns the "correct" usage key value with the run filled in.
4.642973
3.659534
1.268733
user_course_completions = cls.user_course_completion_queryset(user, course_key) return cls.completion_by_block_key(user_course_completions)
def get_course_completions(cls, user, course_key)
Returns a dictionary mapping BlockKeys to completion values for all BlockCompletion records for the given user and course_key. Return value: dict[BlockKey] = float
4.695773
4.606086
1.019471
return cls.objects.filter(user=user, course_key=course_key)
def user_course_completion_queryset(cls, user, course_key)
Returns a Queryset of completions for a given user and course_key.
3.590921
2.535703
1.416144
return course_key = CourseKey.from_string(kwargs['course_id']) block_key = UsageKey.from_string(kwargs['usage_id']) block_cls = XBlock.load_class(block_key.block_type) if XBlockCompletionMode.get_mode(block_cls) != XBlockCompletionMode.COMPLETABLE: return if getattr(block_cls, 'has_c...
def scorable_block_completion(sender, **kwargs): # pylint: disable=unused-argument if not waffle.waffle().is_enabled(waffle.ENABLE_COMPLETION_TRACKING)
When a problem is scored, submit a new BlockCompletion for that block.
3.134613
3.00095
1.04454
if not waffle.waffle().is_enabled(waffle.ENABLE_COMPLETION_TRACKING): raise ValidationError( _("BlockCompletion.objects.submit_batch_completion should not be called when the feature is disabled.") ) for key in self.REQUIRED_KEYS: if key not i...
def _validate_and_parse(self, batch_object)
Performs validation on the batch object to make sure it is in the proper format. Parameters: * batch_object: The data provided to a POST. The expected format is the following: { "username": "username", "course_key": "course-key", "blocks":...
3.051676
2.587868
1.179224
try: return CourseKey.from_string(course_key) except InvalidKeyError: raise ValidationError(_("Invalid course key: {}").format(course_key))
def _validate_and_parse_course_key(self, course_key)
Returns a validated parsed CourseKey deserialized from the given course_key.
2.563798
2.228747
1.150331
try: block_key_obj = UsageKey.from_string(block_key) except InvalidKeyError: raise ValidationError(_("Invalid block key: {}").format(block_key)) if block_key_obj.run is None: expected_matching_course_key = course_key_obj.replace(run=None) els...
def _validate_and_parse_block_key(self, block_key, course_key_obj)
Returns a validated, parsed UsageKey deserialized from the given block_key.
2.22456
2.039688
1.090637
user, course_key, blocks = self._validate_and_parse(batch_object) BlockCompletion.objects.submit_batch_completion(user, course_key, blocks) except ValidationError as exc: return Response({ "detail": _(' ').join(text_type(msg) for msg in exc.messages), ...
def post(self, request, *args, **kwargs): # pylint: disable=unused-argument batch_object = request.data or {} try
Inserts a batch of completions. REST Endpoint Format: { "username": "username", "course_key": "course-key", "blocks": { "block_key1": 0.0, "block_key2": 1.0, "block_key3": 1.0, } } **Returns** A Respon...
2.481854
2.219666
1.118121
def get_completion(course_completions, all_blocks, block_id): block = all_blocks.get(block_id) child_ids = block.get('children', []) if not child_ids: return course_completions.get(block.serializer.instance, 0) completion = 0...
def get(self, request, username, course_key, subsection_id)
Returns completion for a (user, subsection, course).
3.164474
3.107499
1.018334
queryset = BlockCompletion.user_course_completion_queryset(self._user, self._course_key).filter( block_key__in=candidates ) completions = BlockCompletion.completion_by_block_key(queryset) candidates_with_runs = [candidate.replace(course_key=self._course_key) for cand...
def get_completions(self, candidates)
Given an iterable collection of block_keys in the course, returns a mapping of the block_keys to the present completion values of their associated blocks. If a completion is not found for a given block in the current course, 0.0 is returned. The service does not attempt to verify that ...
4.014941
3.240557
1.238966
if item.location.block_type != 'vertical': raise ValueError('The passed in xblock is not a vertical type!') if not self.completion_tracking_enabled(): return None # this is temporary local logic and will be removed when the whole course tree is included in comp...
def vertical_is_complete(self, item)
Calculates and returns whether a particular vertical is complete. The logic in this method is temporary, and will go away once the completion API is able to store a first-order notion of completeness for parent blocks (right now it just stores completion for leaves- problems, HTML, video...
5.89787
4.92837
1.196718
return ( XBlockCompletionMode.get_mode(block) == XBlockCompletionMode.COMPLETABLE and not getattr(block, 'has_custom_completion', False) and not getattr(block, 'has_score', False) )
def can_mark_block_complete_on_view(self, block)
Returns True if the xblock can be marked complete on view. This is true of any non-customized, non-scorable, completable block.
5.102518
3.582889
1.424135
blocks = {block for block in blocks if self.can_mark_block_complete_on_view(block)} completions = self.get_completions({block.location for block in blocks}) return {block for block in blocks if completions.get(block.location, 0) < 1.0}
def blocks_to_mark_complete_on_view(self, blocks)
Returns a set of blocks which should be marked complete on view and haven't been yet.
3.648817
3.120148
1.169437
if users is None: users = [] if user_ids is None: user_ids = [] more_users = User.objects.filter(id__in=user_ids) if len(more_users) < len(user_ids): found_ids = {u.id for u in more_users} not_found_ids = [pk for pk in user_ids if ...
def submit_group_completion(self, block_key, completion, users=None, user_ids=None)
Submit a completion for a group of users. Arguments: block_key (opaque_key.edx.keys.UsageKey): The block to submit completions for. completion (float): A value in the range [0.0, 1.0] users ([django.contrib.auth.models.User]): An optional iterable of Users that completed th...
2.315657
2.292967
1.009896
return BlockCompletion.objects.submit_completion( user=self._user, course_key=self._course_key, block_key=block_key, completion=completion )
def submit_completion(self, block_key, completion)
Submit a completion for the service user and course. Returns a (BlockCompletion, bool) where the boolean indicates whether the given BlockCompletion was newly created.
3.316936
2.83713
1.169117
last_completed_block = BlockCompletion.get_latest_block_completed(user, course_key) if last_completed_block is not None: return last_completed_block.block_key raise UnavailableCompletionData(course_key)
def get_key_to_last_completed_course_block(user, course_key)
Returns the last block a "user" completed in a course (stated as "course_key"). raises UnavailableCompletionData when the user has not completed blocks in the course. raises UnavailableCompletionData when the visual progress waffle flag is disabled.
4.419707
3.130496
1.411823
try: response.raise_for_status() except RequestException: try: msg = 'Response code: {}; response body:\n{}'.format(response.status_code, json.dumps(response.json(), indent=2)) raise CerberusClientException(msg) except ValueError: msg = 'Response ...
def throw_if_bad_response(response)
Throw an exception if the Cerberus response is not successful.
2.19041
1.994163
1.09841
''' Initialize a KeenClient instance using environment variables. ''' global _client, project_id, write_key, read_key, master_key, base_url if _client is None: # check environment for project ID and keys project_id = project_id or os.environ.get("KEEN_PROJECT_ID") write_key = write_...
def _initialize_client_from_environment()
Initialize a KeenClient instance using environment variables.
1.896259
1.82706
1.037875
_initialize_client_from_environment() _client.add_event(event_collection, body, timestamp=timestamp)
def add_event(event_collection, body, timestamp=None)
Adds an event. Depending on the persistence strategy of the client, this will either result in the event being uploaded to Keen immediately or will result in saving the event to some local cache. :param event_collection: the name of the collection to insert the event to :param body: dict, the ...
6.717342
11.936584
0.562752
_initialize_client_from_environment() return _client.generate_image_beacon(event_collection, body, timestamp=timestamp)
def generate_image_beacon(event_collection, body, timestamp=None)
Generates an image beacon URL. :param event_collection: the name of the collection to insert the event to :param body: dict, the body of the event to insert the event to :param timestamp: datetime, optional, the timestamp of the event
6.114973
11.915482
0.513196
_initialize_client_from_environment() return _client.count(event_collection=event_collection, timeframe=timeframe, timezone=timezone, interval=interval, filters=filters, group_by=group_by, order_by=order_by, max_age=max_age, limit=limit)
def count(event_collection, timeframe=None, timezone=None, interval=None, filters=None, group_by=None, order_by=None, max_age=None, limit=None)
Performs a count query Counts the number of events that meet the given criteria. :param event_collection: string, the name of the collection to query :param timeframe: string or dict, the timeframe in which the events happened example: "previous_7_days" :param timezone: int, the timezone you'd lik...
2.169784
2.786466
0.778687
_initialize_client_from_environment() return _client.sum(event_collection=event_collection, timeframe=timeframe, timezone=timezone, interval=interval, filters=filters, group_by=group_by, order_by=order_by, target_property=target_property, max_age=max_age, limit...
def sum(event_collection, target_property, timeframe=None, timezone=None, interval=None, filters=None, group_by=None, order_by=None, max_age=None, limit=None)
Performs a sum query Adds the values of a target property for events that meet the given criteria. :param event_collection: string, the name of the collection to query :param target_property: string, the name of the event property you would like use :param timeframe: string or dict, the timeframe in w...
2.267616
2.953087
0.76788
_initialize_client_from_environment() return _client.extraction(event_collection=event_collection, timeframe=timeframe, timezone=timezone, filters=filters, latest=latest, email=email, property_names=property_names)
def extraction(event_collection, timeframe=None, timezone=None, filters=None, latest=None, email=None, property_names=None)
Performs a data extraction Returns either a JSON object of events or a response indicating an email will be sent to you with data. :param event_collection: string, the name of the collection to query :param timeframe: string or dict, the timeframe in which the events happened example: "previous_7...
2.890873
4.491976
0.643564
_initialize_client_from_environment() return _client.multi_analysis(event_collection=event_collection, timeframe=timeframe, interval=interval, timezone=timezone, filters=filters, group_by=group_by, order_by=order_by, analyses=analyses, ...
def multi_analysis(event_collection, analyses, timeframe=None, interval=None, timezone=None, filters=None, group_by=None, order_by=None, max_age=None, limit=None)
Performs a multi-analysis query Returns a dictionary of analysis results. :param event_collection: string, the name of the collection to query :param analyses: dict, the types of analyses you'd like to run. example: {"total money made":{"analysis_type":"sum","target_property":"purchase.price", "a...
2.214658
2.987409
0.741331
_initialize_client_from_environment() return _client.create_access_key(name=name, is_active=is_active, permitted=permitted, options=options)
def create_access_key(name, is_active=True, permitted=[], options={})
Creates a new access key. A master key must be set first. :param name: the name of the access key to create :param is_active: Boolean value dictating whether this key is currently active (default True) :param permitted: list of strings describing which operation types this key will permit ...
3.662294
5.481589
0.668108
_initialize_client_from_environment() return _client.update_access_key_full(access_key_id, name, is_active, permitted, options)
def update_access_key_full(access_key_id, name, is_active, permitted, options)
Replaces the 'name', 'is_active', 'permitted', and 'options' values of a given key. A master key must be set first. :param access_key_id: the 'key' value of the access key for which the values will be replaced :param name: the new name desired for this access key :param is_active: whether the key shoul...
3.618376
4.860915
0.744382
try: OPENJP2.opj_version.restype = ctypes.c_char_p except: return "0.0.0" v = OPENJP2.opj_version() return v.decode('utf-8') if sys.hexversion >= 0x03000000 else v
def version()
Wrapper for opj_version library routine.
3.838221
3.207042
1.19681
global ERROR_MSG_LST if status != 1: if len(ERROR_MSG_LST) > 0: # clear out the existing error message so that we don't pick up # a bad one next time around. msg = '\n'.join(ERROR_MSG_LST) ERROR_MSG_LST = [] raise OpenJPEGLibraryError(msg)...
def check_error(status)
Set a generic function as the restype attribute of all OpenJPEG functions that return a BOOL_TYPE value. This way we do not have to check for error status in each wrapping function and an exception will always be appropriately raised.
5.185017
4.133392
1.254422
OPENJP2.opj_create_compress.restype = CODEC_TYPE OPENJP2.opj_create_compress.argtypes = [CODEC_FORMAT_TYPE] codec = OPENJP2.opj_create_compress(codec_format) return codec
def create_compress(codec_format)
Creates a J2K/JP2 compress structure. Wraps the openjp2 library function opj_create_compress. Parameters ---------- codec_format : int Specifies codec to select. Should be one of CODEC_J2K or CODEC_JP2. Returns ------- codec : Reference to CODEC_TYPE instance.
3.795026
3.130658
1.212213
OPENJP2.opj_decode.argtypes = [CODEC_TYPE, STREAM_TYPE_P, ctypes.POINTER(ImageType)] OPENJP2.opj_decode.restype = check_error OPENJP2.opj_decode(codec, stream, image)
def decode(codec, stream, image)
Reads an entire image. Wraps the openjp2 library function opj_decode. Parameters ---------- codec : CODEC_TYPE The JPEG2000 codec stream : STREAM_TYPE_P The stream to decode. image : ImageType Output image structure. Raises ------ RuntimeError If th...
5.42951
3.702725
1.466355