signature stringlengths 8 3.44k | body stringlengths 0 1.41M | docstring stringlengths 1 122k | id stringlengths 5 17 |
|---|---|---|---|
def qrlist(self, name_start, name_end, limit): | limit = get_positive_integer("<STR_LIT>", limit)<EOL>return self.execute_command('<STR_LIT>', name_start, name_end, limit)<EOL> | Return a list of the top ``limit`` keys between ``name_start`` and
``name_end`` in descending order
.. note:: The range is (``name_start``, ``name_end``]. ``name_start``
isn't in the range, but ``name_end`` is.
:param string name_start: The lower bound(not included) of keys to be
returned, empty string ``''`` means +inf
:param string name_end: The upper bound(included) of keys to be
returned, empty string ``''`` means -inf
:param int limit: number of elements will be returned.
:return: a list of keys
:rtype: list
>>> ssdb.qrlist('queue_2', 'queue_1', 10)
['queue_1']
>>> ssdb.qrlist('queue_z', 'queue_', 10)
['queue_2', 'queue_1']
>>> ssdb.qrlist('z', '', 10)
['queue_2', 'queue_1'] | f13250:c0:m77 |
def qfront(self, name): | return self.execute_command('<STR_LIT>', name)<EOL> | Returns the first element of a queue.
:param string name: the queue name
:return: ``None`` if queue empty, otherwise the item returned
:rtype: string | f13250:c0:m78 |
def qback(self, name): | return self.execute_command('<STR_LIT>', name)<EOL> | Returns the last element of a queue.
:param string name: the queue name
:return: ``None`` if queue empty, otherwise the item returned
:rtype: string | f13250:c0:m79 |
def qrange(self, name, offset, limit): | offset = get_integer('<STR_LIT>', offset) <EOL>limit = get_positive_integer('<STR_LIT>', limit) <EOL>return self.execute_command('<STR_LIT>', name, offset, limit)<EOL> | Return a ``limit`` slice of the list ``name`` at position ``offset``
``offset`` can be negative numbers just like Python slicing notation
Similiar with **Redis.LRANGE**
:param string name: the queue name
:param int offset: the returned list will start at this offset
:param int limit: number of elements will be returned
:return: a list of elements
:rtype: list | f13250:c0:m80 |
def qslice(self, name, start, end): | start = get_integer('<STR_LIT:start>', start)<EOL>end = get_integer('<STR_LIT:end>', end)<EOL>return self.execute_command('<STR_LIT>', name, start, end)<EOL> | Return a slice of the list ``name`` between
position ``start`` and ``end``
``start`` and ``end`` can be negative numbers just like
Python slicing notation
Like **Redis.LRANGE**
:param string name: the queue name
:param int start: the returned list will start at this offset
:param int end: the returned list will end at this offset
:return: a list of elements
:rtype: list | f13250:c0:m81 |
def qtrim_front(self, name, size=<NUM_LIT:1>): | size = get_positive_integer("<STR_LIT:size>", size)<EOL>return self.execute_command('<STR_LIT>', name, size)<EOL> | Sets the list element at ``index`` to ``value``. An error is returned for out of
range indexes.
:param string name: the queue name
:param int size: the max length of removed elements
:return: the length of removed elements
:rtype: int | f13250:c0:m82 |
def qtrim_back(self, name, size=<NUM_LIT:1>): | size = get_positive_integer("<STR_LIT:size>", size)<EOL>return self.execute_command('<STR_LIT>', name, size)<EOL> | Sets the list element at ``index`` to ``value``. An error is returned for out of
range indexes.
:param string name: the queue name
:param int size: the max length of removed elements
:return: the length of removed elements
:rtype: int | f13250:c0:m83 |
def hash_exists(self, name): | try:<EOL><INDENT>return hsize(name) > <NUM_LIT:0><EOL><DEDENT>except:<EOL><INDENT>return False<EOL><DEDENT> | Return a boolean indicating whether hash ``name`` exists
:param string name: the hash name
:return: ``True`` if the hash exists, ``False`` if not
:rtype: string
>>> ssdb.hash_exists('hash_1')
True
>>> ssdb.hash_exists('hash_2')
True
>>> ssdb.hash_exists('hash_not_exist')
False | f13250:c0:m85 |
def zset_exists(self, name): | try:<EOL><INDENT>return zsize(name) > <NUM_LIT:0><EOL><DEDENT>except:<EOL><INDENT>return False<EOL><DEDENT> | Return a boolean indicating whether zset ``name`` exists
:param string name: the zset name
:return: ``True`` if the zset exists, ``False`` if not
:rtype: string
>>> ssdb.zset_exists('zset_1')
True
>>> ssdb.zset_exists('zset_2')
True
>>> ssdb.zset_exists('zset_not_exist')
False | f13250:c0:m86 |
def queue_exists(self, name): | try:<EOL><INDENT>return hsize(name) > <NUM_LIT:0><EOL><DEDENT>except:<EOL><INDENT>return False<EOL><DEDENT> | Return a boolean indicating whether queue ``name`` exists
:param string name: the queue name
:return: ``True`` if the queue exists, ``False`` if not
:rtype: string
>>> ssdb.queue_exists('queue_1')
True
>>> ssdb.queue_exists('queue_2')
True
>>> ssdb.queue_exists('queue_not_exist')
False | f13250:c0:m87 |
def setx(self, name, value, ttl): | if isinstance(ttl, datetime.timedelta):<EOL><INDENT>ttl = ttl.seconds + ttl.days * <NUM_LIT> * <NUM_LIT><EOL><DEDENT>ttl = get_positive_integer('<STR_LIT>', ttl)<EOL>return self.execute_command('<STR_LIT>', name, value, ttl)<EOL> | Set the value of key ``name`` to ``value`` that expires in ``ttl``
seconds. ``ttl`` can be represented by an integer or a Python
timedelta object.
Like **Redis.SETEX**
:param string name: the key name
:param string value: a string or an object can be converted to string
:param int ttl: positive int seconds or timedelta object
:return: ``True`` on success, ``False`` if not
:rtype: bool
>>> import time
>>> ssdb.set("test_ttl", 'ttl', 4)
True
>>> ssdb.get("test_ttl")
'ttl'
>>> time.sleep(4)
>>> ssdb.get("test_ttl")
>>> | f13250:c1:m1 |
def copy(self): | <EOL>return self.__class__(self)<EOL> | Returns a copy of this object. | f13253:c0:m18 |
def __repr__(self): | <EOL>return '<STR_LIT>' % '<STR_LIT:U+002CU+0020>'.join('<STR_LIT>' % (k, v) for k, v in self.iteritems())<EOL> | Replaces the normal dict.__repr__ with a version that returns the keys
in their sorted order. | f13253:c0:m19 |
def get_template_sources(self, template_name, template_dirs=None): | if not template_dirs:<EOL><INDENT>template_dirs = self.get_dirs()<EOL><DEDENT>for template_dir in template_dirs:<EOL><INDENT>try:<EOL><INDENT>name = safe_join(template_dir, template_name)<EOL><DEDENT>except SuspiciousFileOperation:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>if Origin:<EOL><INDENT>yield Origin(<EOL>name=name,<EOL>template_name=template_name,<EOL>loader=self,<EOL>)<EOL><DEDENT>else:<EOL><INDENT>yield name<EOL><DEDENT><DEDENT><DEDENT> | Returns the absolute paths to "template_name", when appended to each
directory in "template_dirs". Any paths that don't lie inside one of the
template dirs are excluded from the result set, for security reasons. | f13265:c0:m0 |
def get_template(template_name, using=None): | engines = _engine_list(using)<EOL>for engine in engines:<EOL><INDENT>try:<EOL><INDENT>return engine.get_template(template_name)<EOL><DEDENT>except TemplateDoesNotExist as e:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>raise TemplateDoesNotExist(template_name)<EOL> | Loads and returns a template for the given name.
Raises TemplateDoesNotExist if no such template exists. | f13266:m1 |
def select_template(template_name_list, using=None): | if isinstance(template_name_list, six.string_types):<EOL><INDENT>raise TypeError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>' % template_name_list<EOL>)<EOL><DEDENT>engines = _engine_list(using)<EOL>for template_name in template_name_list:<EOL><INDENT>for engine in engines:<EOL><INDENT>try:<EOL><INDENT>return engine.get_template(template_name)<EOL><DEDENT>except TemplateDoesNotExist as e:<EOL><INDENT>pass<EOL><DEDENT><DEDENT><DEDENT>if template_name_list:<EOL><INDENT>raise TemplateDoesNotExist('<STR_LIT:U+002CU+0020>'.join(template_name_list))<EOL><DEDENT>else:<EOL><INDENT>raise TemplateDoesNotExist("<STR_LIT>")<EOL><DEDENT> | Loads and returns a template for one of the given names.
Tries names in order and returns the first template found.
Raises TemplateDoesNotExist if no such template exists. | f13266:m2 |
def render_to_string(template_name, context=None, request=None, using=None): | if isinstance(template_name, (list, tuple)):<EOL><INDENT>template = select_template(template_name, using=using)<EOL><DEDENT>else:<EOL><INDENT>template = get_template(template_name, using=using)<EOL><DEDENT>return template.render(context, request)<EOL> | Loads a template and renders it with a context. Returns a string.
template_name may be a string or a list of strings. | f13266:m3 |
def get_flux(self, energies): | results = [component.shape(energies) for component in self.components.values()]<EOL>return numpy.sum(results, <NUM_LIT:0>)<EOL> | Get the total flux of this particle source at the given energies (summed over the components) | f13277:c0:m1 |
def _repr__base(self, rich_output=False): | <EOL>repr_dict = collections.OrderedDict()<EOL>key = '<STR_LIT>' % self.name<EOL>repr_dict[key] = collections.OrderedDict()<EOL>repr_dict[key]['<STR_LIT>'] = collections.OrderedDict()<EOL>for component_name, component in self.components.iteritems():<EOL><INDENT>repr_dict[key]['<STR_LIT>'][component_name] = component.to_dict(minimal=True)<EOL><DEDENT>return dict_to_list(repr_dict, rich_output)<EOL> | Representation of the object
:param rich_output: if True, generates HTML, otherwise text
:return: the representation | f13277:c0:m2 |
@property<EOL><INDENT>def free_parameters(self):<DEDENT> | free_parameters = collections.OrderedDict()<EOL>for component in self._components.values():<EOL><INDENT>for par in component.shape.parameters.values():<EOL><INDENT>if par.free:<EOL><INDENT>free_parameters[par.path] = par<EOL><DEDENT><DEDENT><DEDENT>return free_parameters<EOL> | Returns a dictionary of free parameters for this source.
We use the parameter path as the key because it's
guaranteed to be unique, unlike the parameter name.
:return: | f13277:c0:m3 |
@property<EOL><INDENT>def parameters(self):<DEDENT> | all_parameters = collections.OrderedDict()<EOL>for component in self._components.values():<EOL><INDENT>for par in component.shape.parameters.values():<EOL><INDENT>all_parameters[par.path] = par<EOL><DEDENT><DEDENT>return all_parameters<EOL> | Returns a dictionary of all parameters for this source.
We use the parameter path as the key because it's
guaranteed to be unique, unlike the parameter name.
:return: | f13277:c0:m4 |
def has_free_parameters(self): | for component in self._components.values():<EOL><INDENT>for par in component.shape.parameters.values():<EOL><INDENT>if par.free:<EOL><INDENT>return True<EOL><DEDENT><DEDENT><DEDENT>for par in self.position.parameters.values():<EOL><INDENT>if par.free:<EOL><INDENT>return True<EOL><DEDENT><DEDENT>return False<EOL> | Returns True or False whether there is any parameter in this source
:return: | f13278:c0:m2 |
@property<EOL><INDENT>def free_parameters(self):<DEDENT> | free_parameters = collections.OrderedDict()<EOL>for component in self._components.values():<EOL><INDENT>for par in component.shape.parameters.values():<EOL><INDENT>if par.free:<EOL><INDENT>free_parameters[par.path] = par<EOL><DEDENT><DEDENT><DEDENT>for par in self.position.parameters.values():<EOL><INDENT>if par.free:<EOL><INDENT>free_parameters[par.path] = par<EOL><DEDENT><DEDENT>return free_parameters<EOL> | Returns a dictionary of free parameters for this source.
We use the parameter path as the key because it's
guaranteed to be unique, unlike the parameter name.
:return: | f13278:c0:m3 |
@property<EOL><INDENT>def parameters(self):<DEDENT> | all_parameters = collections.OrderedDict()<EOL>for component in self._components.values():<EOL><INDENT>for par in component.shape.parameters.values():<EOL><INDENT>all_parameters[par.path] = par<EOL><DEDENT><DEDENT>for par in self.position.parameters.values():<EOL><INDENT>all_parameters[par.path] = par<EOL><DEDENT>return all_parameters<EOL> | Returns a dictionary of all parameters for this source.
We use the parameter path as the key because it's
guaranteed to be unique, unlike the parameter name.
:return: | f13278:c0:m4 |
def _repr__base(self, rich_output=False): | <EOL>repr_dict = collections.OrderedDict()<EOL>key = '<STR_LIT>' % self.name<EOL>repr_dict[key] = collections.OrderedDict()<EOL>repr_dict[key]['<STR_LIT>'] = self._sky_position.to_dict(minimal=True)<EOL>repr_dict[key]['<STR_LIT>'] = collections.OrderedDict()<EOL>for component_name, component in self.components.iteritems():<EOL><INDENT>repr_dict[key]['<STR_LIT>'][component_name] = component.to_dict(minimal=True)<EOL><DEDENT>return dict_to_list(repr_dict, rich_output)<EOL> | Representation of the object
:param rich_output: if True, generates HTML, otherwise text
:return: the representation | f13278:c0:m5 |
@property<EOL><INDENT>def free_parameters(self):<DEDENT> | raise NotImplementedError("<STR_LIT>")<EOL> | Returns a dictionary of free parameters for this source
:return: | f13279:c1:m2 |
@property<EOL><INDENT>def components(self):<DEDENT> | return self._components<EOL> | Return the dictionary of components
:return: dictionary of components | f13279:c1:m3 |
@property<EOL><INDENT>def source_type(self):<DEDENT> | return self._src_type<EOL> | Return the type of the source ('point source' or 'extended source')
:return: type of the source | f13279:c1:m4 |
@property<EOL><INDENT>def spatial_shape(self):<DEDENT> | return self._spatial_shape<EOL> | A generic name for the spatial shape.
:return: the spatial shape instance | f13281:c0:m1 |
def get_spatially_integrated_flux( self, energies): | if not isinstance(energies, np.ndarray):<EOL><INDENT>energies = np.array(energies, ndmin=<NUM_LIT:1>)<EOL><DEDENT>results = [self.spatial_shape.get_total_spatial_integral(energies) * component.shape(energies) for component in self.components.values()]<EOL>if isinstance(energies, u.Quantity):<EOL><INDENT>differential_flux = sum(results)<EOL><DEDENT>else:<EOL><INDENT>differential_flux = np.sum(results, <NUM_LIT:0>)<EOL><DEDENT>return differential_flux<EOL> | Returns total flux of source at the given energy
:param energies: energies (array or float)
:return: differential flux at given energy | f13281:c0:m2 |
def __call__(self, lon, lat, energies): | assert type(lat) == type(lon) and type(lon) == type(energies), "<STR_LIT>"<EOL>if not isinstance(lat, np.ndarray):<EOL><INDENT>lat = np.array(lat, ndmin=<NUM_LIT:1>)<EOL>lon = np.array(lon, ndmin=<NUM_LIT:1>)<EOL>energies = np.array(energies, ndmin=<NUM_LIT:1>)<EOL><DEDENT>results = [component.shape(energies) for component in self.components.values()]<EOL>if isinstance(energies, u.Quantity):<EOL><INDENT>differential_flux = sum(results)<EOL><DEDENT>else:<EOL><INDENT>differential_flux = np.sum(results, <NUM_LIT:0>)<EOL><DEDENT>if self._spatial_shape.n_dim == <NUM_LIT:2>:<EOL><INDENT>brightness = self._spatial_shape(lon, lat)<EOL>n_points = lat.shape[<NUM_LIT:0>]<EOL>n_energies = differential_flux.shape[<NUM_LIT:0>]<EOL>cube = np.repeat(differential_flux, n_points).reshape(n_energies, n_points).T<EOL>result = (cube.T * brightness).T<EOL><DEDENT>else:<EOL><INDENT>result = self._spatial_shape(lon, lat, energies) * differential_flux<EOL><DEDENT>return np.squeeze(result)<EOL> | Returns brightness of source at the given position and energy
:param lon: longitude (array or float)
:param lat: latitude (array or float)
:param energies: energies (array or float)
:return: differential flux at given position and energy | f13281:c0:m3 |
def has_free_parameters(self): | for component in self._components.values():<EOL><INDENT>for par in component.shape.parameters.values():<EOL><INDENT>if par.free:<EOL><INDENT>return True<EOL><DEDENT><DEDENT><DEDENT>for par in self.spatial_shape.parameters.values():<EOL><INDENT>if par.free:<EOL><INDENT>return True<EOL><DEDENT><DEDENT>return False<EOL> | Returns True or False whether there is any parameter in this source
:return: | f13281:c0:m4 |
@property<EOL><INDENT>def free_parameters(self):<DEDENT> | free_parameters = collections.OrderedDict()<EOL>for component in self._components.values():<EOL><INDENT>for par in component.shape.parameters.values():<EOL><INDENT>if par.free:<EOL><INDENT>free_parameters[par.path] = par<EOL><DEDENT><DEDENT><DEDENT>for par in self.spatial_shape.parameters.values():<EOL><INDENT>if par.free:<EOL><INDENT>free_parameters[par.path] = par<EOL><DEDENT><DEDENT>return free_parameters<EOL> | Returns a dictionary of free parameters for this source
We use the parameter path as the key because it's
guaranteed to be unique, unlike the parameter name.
:return: | f13281:c0:m5 |
@property<EOL><INDENT>def parameters(self):<DEDENT> | all_parameters = collections.OrderedDict()<EOL>for component in self._components.values():<EOL><INDENT>for par in component.shape.parameters.values():<EOL><INDENT>all_parameters[par.path] = par<EOL><DEDENT><DEDENT>for par in self.spatial_shape.parameters.values():<EOL><INDENT>all_parameters[par.path] = par<EOL><DEDENT>return all_parameters<EOL> | Returns a dictionary of all parameters for this source.
We use the parameter path as the key because it's
guaranteed to be unique, unlike the parameter name.
:return: | f13281:c0:m6 |
def _repr__base(self, rich_output=False): | <EOL>repr_dict = collections.OrderedDict()<EOL>key = '<STR_LIT>' % self.name<EOL>repr_dict[key] = collections.OrderedDict()<EOL>repr_dict[key]['<STR_LIT>'] = self._spatial_shape.to_dict(minimal=True)<EOL>repr_dict[key]['<STR_LIT>'] = collections.OrderedDict()<EOL>for component_name, component in self.components.iteritems():<EOL><INDENT>repr_dict[key]['<STR_LIT>'][component_name] = component.to_dict(minimal=True)<EOL><DEDENT>return dict_to_list(repr_dict, rich_output)<EOL> | Representation of the object
:param rich_output: if True, generates HTML, otherwise text
:return: the representation | f13281:c0:m7 |
def get_boundaries(self): | return self._spatial_shape.get_boundaries()<EOL> | Returns the boundaries for this extended source
:return: a tuple of tuples ((min. lon, max. lon), (min lat, max lat)) | f13281:c0:m8 |
def _add_source(self, source): | try:<EOL><INDENT>self._add_child(source)<EOL><DEDENT>except AttributeError:<EOL><INDENT>if isinstance(source, Source):<EOL><INDENT>raise DuplicatedNode("<STR_LIT>"<EOL>"<STR_LIT>" % source.name)<EOL><DEDENT>else: <EOL><INDENT>raise<EOL><DEDENT><DEDENT>if source.source_type == POINT_SOURCE:<EOL><INDENT>self._point_sources[source.name] = source<EOL><DEDENT>elif source.source_type == EXTENDED_SOURCE:<EOL><INDENT>self._extended_sources[source.name] = source<EOL><DEDENT>elif source.source_type == PARTICLE_SOURCE:<EOL><INDENT>self._particle_sources[source.name] = source<EOL><DEDENT>else: <EOL><INDENT>raise InvalidInput("<STR_LIT>")<EOL><DEDENT> | Remember to call _update_parameters after this!
:param source:
:return: | f13292:c4:m1 |
def _remove_source(self, source_name): | assert source_name in self.sources, "<STR_LIT>" % source_name<EOL>source = self.sources.pop(source_name)<EOL>if source.source_type == POINT_SOURCE:<EOL><INDENT>self._point_sources.pop(source.name)<EOL><DEDENT>elif source.source_type == EXTENDED_SOURCE:<EOL><INDENT>self._extended_sources.pop(source.name)<EOL><DEDENT>elif source.source_type == PARTICLE_SOURCE:<EOL><INDENT>self._particle_sources.pop(source.name)<EOL><DEDENT>self._remove_child(source_name)<EOL> | Remember to call _update_parameters after this
:param source_name:
:return: | f13292:c4:m2 |
@property<EOL><INDENT>def parameters(self):<DEDENT> | self._update_parameters()<EOL>return self._parameters<EOL> | Return a dictionary with all parameters
:return: dictionary of parameters | f13292:c4:m5 |
@property<EOL><INDENT>def free_parameters(self):<DEDENT> | <EOL>self._update_parameters()<EOL>free_parameters_dictionary = collections.OrderedDict()<EOL>for parameter_name, parameter in self._parameters.iteritems():<EOL><INDENT>if parameter.free:<EOL><INDENT>free_parameters_dictionary[parameter_name] = parameter<EOL><DEDENT><DEDENT>return free_parameters_dictionary<EOL> | Get a dictionary with all the free parameters in this model
:return: dictionary of free parameters | f13292:c4:m6 |
@property<EOL><INDENT>def linked_parameters(self):<DEDENT> | <EOL>self._update_parameters()<EOL>linked_parameter_dictionary = collections.OrderedDict()<EOL>for parameter_name, parameter in self._parameters.iteritems():<EOL><INDENT>if parameter.has_auxiliary_variable():<EOL><INDENT>linked_parameter_dictionary[parameter_name] = parameter<EOL><DEDENT><DEDENT>return linked_parameter_dictionary<EOL> | Get a dictionary with all parameters in this model in a linked status. A parameter is in a linked status
if it is linked to another parameter (i.e. it is forced to have the same value of the other parameter), or
if it is linked with another parameter or an independent variable through a law.
:return: dictionary of linked parameters | f13292:c4:m7 |
def set_free_parameters(self, values): | assert len(values) == len(self.free_parameters)<EOL>for parameter, this_value in zip(self.free_parameters.values(), values):<EOL><INDENT>parameter.value = this_value<EOL><DEDENT> | Set the free parameters in the model to the provided values.
NOTE: of course, order matters
:param values: a list of new values
:return: None | f13292:c4:m8 |
def __getitem__(self, path): | return self._get_child_from_path(path)<EOL> | Get a parameter from a path like "source_1.component.powerlaw.logK". This might be useful in certain
context, although in an interactive analysis there is no reason to use this.
:param path: the address of the parameter
:return: the parameter | f13292:c4:m9 |
def __contains__(self, path): | try:<EOL><INDENT>_ = self._get_child_from_path(path)<EOL><DEDENT>except (AttributeError, KeyError):<EOL><INDENT>return False<EOL><DEDENT>else:<EOL><INDENT>return True<EOL><DEDENT> | This allows the model to be used with the "in" operator, like;
> if 'myparameter' in model:
> print("Myparameter is contained in the model")
:param path: the parameter to look for
:return: | f13292:c4:m10 |
def __iter__(self): | for parameter in self.parameters:<EOL><INDENT>yield self.parameters[parameter]<EOL><DEDENT> | This allows the model to be iterated on, like in:
for parameter in model:
...
NOTE: this will iterate over *all* parameters in the model, also those that are not free (and thus are not
normally displayed). If you need to operate only on free parameters, just check if they are free within
the loop or use the .free_parameters dictionary directly
:return: iterator | f13292:c4:m11 |
@property<EOL><INDENT>def point_sources(self):<DEDENT> | return self._point_sources<EOL> | Returns the dictionary of all defined point sources
:return: collections.OrderedDict() | f13292:c4:m12 |
@property<EOL><INDENT>def extended_sources(self):<DEDENT> | return self._extended_sources<EOL> | Returns the dictionary of all defined extended sources
:return: collections.OrderedDict() | f13292:c4:m13 |
@property<EOL><INDENT>def particle_sources(self):<DEDENT> | return self._particle_sources<EOL> | Returns the dictionary of all defined particle sources
:return: collections.OrderedDict() | f13292:c4:m14 |
@property<EOL><INDENT>def sources(self):<DEDENT> | sources = collections.OrderedDict()<EOL>for d in (self.point_sources, self.extended_sources, self.particle_sources):<EOL><INDENT>sources.update(d)<EOL><DEDENT>return sources<EOL> | Returns a dictionary containing all defined sources (of any kind)
:return: collections.OrderedDict() | f13292:c4:m15 |
def add_source(self, new_source): | self._add_source(new_source)<EOL>self._update_parameters()<EOL> | Add the provided source to the model
:param new_source: the new source to be added (an instance of PointSource, ExtendedSource or ParticleSource)
:return: (none) | f13292:c4:m16 |
def remove_source(self, source_name): | self._remove_source(source_name)<EOL>self._update_parameters()<EOL> | Returns a new model with the provided source removed from the current model
:param source_name: the name of the source to be removed
:return: a new Model instance without the source | f13292:c4:m17 |
def add_independent_variable(self, variable): | assert isinstance(variable, IndependentVariable), "<STR_LIT>"<EOL>if self._has_child(variable.name):<EOL><INDENT>self._remove_child(variable.name)<EOL><DEDENT>self._add_child(variable)<EOL>self._independent_variables[variable.name] = variable<EOL> | Add a global independent variable to this model, such as time.
:param variable: an IndependentVariable instance
:return: none | f13292:c4:m18 |
def remove_independent_variable(self, variable_name): | self._remove_child(variable_name)<EOL>self._independent_variables.pop(variable_name)<EOL> | Remove an independent variable which was added with add_independent_variable
:param variable_name: name of variable to remove
:return: | f13292:c4:m19 |
def add_external_parameter(self, parameter): | assert isinstance(parameter, Parameter), "<STR_LIT>"<EOL>if self._has_child(parameter.name):<EOL><INDENT>if isinstance(self._get_child(parameter.name), Parameter):<EOL><INDENT>warnings.warn("<STR_LIT>" % parameter.name,<EOL>RuntimeWarning)<EOL>self._remove_child(parameter.name)<EOL><DEDENT><DEDENT>self._add_child(parameter)<EOL> | Add a parameter that comes from something other than a function, to the model.
:param parameter: a Parameter instance
:return: none | f13292:c4:m20 |
def remove_external_parameter(self, parameter_name): | self._remove_child(parameter_name)<EOL> | Remove an external parameter which was added with add_external_parameter
:param variable_name: name of parameter to remove
:return: | f13292:c4:m21 |
def link(self, parameter_1, parameter_2, link_function=None): | if not isinstance(parameter_1,list):<EOL><INDENT>parameter_1_list = [parameter_1]<EOL><DEDENT>else:<EOL><INDENT>parameter_1_list = list(parameter_1)<EOL><DEDENT>for param_1 in parameter_1_list:<EOL><INDENT>assert param_1.path in self, "<STR_LIT>" % param_1.path<EOL><DEDENT>assert parameter_2.path in self, "<STR_LIT>" % parameter_2.path<EOL>if link_function is None:<EOL><INDENT>link_function = get_function('<STR_LIT>')<EOL>link_function.a.value = <NUM_LIT:1><EOL>link_function.a.fix = True<EOL>link_function.b.value = <NUM_LIT:0><EOL>link_function.b.fix = True<EOL><DEDENT>for param_1 in parameter_1_list: <EOL><INDENT>param_1.add_auxiliary_variable(parameter_2, link_function)<EOL>link_function.set_units(parameter_2.unit, param_1.unit)<EOL><DEDENT> | Link the value of the provided parameters through the provided function (identity is the default, i.e.,
parameter_1 = parameter_2).
:param parameter_1: the first parameter;can be either a single parameter or a list of prarameters
:param parameter_2: the second parameter
:param link_function: a function instance. If not provided, the identity function will be used by default.
Otherwise, this link will be set: parameter_1 = link_function(parameter_2)
:return: (none) | f13292:c4:m22 |
def unlink(self, parameter): | if not isinstance(parameter,list):<EOL><INDENT>parameter_list = [parameter]<EOL><DEDENT>else:<EOL><INDENT>parameter_list = list(parameter)<EOL><DEDENT>for param in parameter_list: <EOL><INDENT>if param.has_auxiliary_variable():<EOL><INDENT>param.remove_auxiliary_variable()<EOL><DEDENT>else:<EOL><INDENT>with warnings.catch_warnings():<EOL><INDENT>warnings.simplefilter("<STR_LIT>", RuntimeWarning)<EOL>warnings.warn("<STR_LIT>" % param.path, RuntimeWarning)<EOL><DEDENT><DEDENT><DEDENT> | Sets free one or more parameters which have been linked previously
:param parameter: the parameter to be set free, can also be a list of parameters
:return: (none) | f13292:c4:m23 |
def display(self, complete=False): | <EOL>self._complete_display = bool(complete)<EOL>super(Model, self).display()<EOL>self._complete_display = False<EOL> | Display information about the point source.
:param complete : if True, displays also information on fixed parameters
:return: (none) | f13292:c4:m24 |
def save(self, output_file, overwrite=False): | if os.path.exists(output_file) and overwrite is False:<EOL><INDENT>raise ModelFileExists("<STR_LIT>"<EOL>"<STR_LIT>" % (output_file, output_file))<EOL><DEDENT>else:<EOL><INDENT>data = self.to_dict_with_types()<EOL>try:<EOL><INDENT>representation = my_yaml.dump(data, default_flow_style=False)<EOL>with open(output_file, "<STR_LIT>") as f:<EOL><INDENT>f.write(representation.replace("<STR_LIT:\n>", "<STR_LIT>"))<EOL><DEDENT><DEDENT>except IOError:<EOL><INDENT>raise CannotWriteModel(os.path.dirname(os.path.abspath(output_file)),<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % output_file)<EOL><DEDENT><DEDENT> | Save the model to disk | f13292:c4:m27 |
def get_number_of_point_sources(self): | return len(self._point_sources)<EOL> | Return the number of point sources
:return: number of point sources | f13292:c4:m28 |
def get_point_source_position(self, id): | pts = self._point_sources.values()[id]<EOL>return pts.position.get_ra(), pts.position.get_dec()<EOL> | Get the point source position (R.A., Dec)
:param id: id of the source
:return: a tuple with R.A. and Dec. | f13292:c4:m29 |
def get_point_source_fluxes(self, id, energies, tag=None): | return self._point_sources.values()[id](energies, tag=tag)<EOL> | Get the fluxes from the id-th point source
:param id: id of the source
:param energies: energies at which you need the flux
:param tag: a tuple (integration variable, a, b) specifying the integration to perform. If this
parameter is specified then the returned value will be the average flux for the source computed as the integral
between a and b over the integration variable divided by (b-a). The integration variable must be an independent
variable contained in the model. If b is None, then instead of integrating the integration variable will be
set to a and the model evaluated in a.
:return: fluxes | f13292:c4:m30 |
def get_number_of_extended_sources(self): | return len(self._extended_sources)<EOL> | Return the number of extended sources
:return: number of extended sources | f13292:c4:m32 |
def get_extended_source_fluxes(self, id, j2000_ra, j2000_dec, energies): | return self._extended_sources.values()[id](j2000_ra, j2000_dec, energies)<EOL> | Get the flux of the id-th extended sources at the given position at the given energies
:param id: id of the source
:param j2000_ra: R.A. where the flux is desired
:param j2000_dec: Dec. where the flux is desired
:param energies: energies at which the flux is desired
:return: flux array | f13292:c4:m33 |
def get_extended_source_name(self, id): | return self._extended_sources.values()[id].name<EOL> | Return the name of the n-th extended source
:param id: id of the source (integer)
:return: the name of the id-th source | f13292:c4:m34 |
def get_number_of_particle_sources(self): | return len(self._particle_sources)<EOL> | Return the number of particle sources
:return: number of particle sources | f13292:c4:m37 |
def get_particle_source_fluxes(self, id, energies): | return self._particle_sources.values()[id](energies)<EOL> | Get the fluxes from the id-th point source
:param id: id of the source
:param energies: energies at which you need the flux
:return: fluxes | f13292:c4:m38 |
def get_total_flux(self, energies): | fluxes = []<EOL>for src in self._point_sources:<EOL><INDENT>fluxes.append(self._point_sources[src](energies))<EOL><DEDENT>return np.sum(fluxes, axis=<NUM_LIT:0>)<EOL> | Returns the total differential flux at the provided energies from all *point* sources
:return: | f13292:c4:m40 |
def __repr__(self): | return self._repr__base(rich_output=False)<EOL> | Textual representation for console
:return: representation | f13293:c4:m7 |
def _repr_html_(self): | return self._repr__base(rich_output=True)<EOL> | HTML representation for the IPython notebook
:return: HTML representation | f13293:c4:m8 |
def display(self): | <EOL>display(self)<EOL> | Display information about the point source.
:return: (none) | f13293:c4:m9 |
@property<EOL><INDENT>def name(self):<DEDENT> | return self._name<EOL> | Returns the name of the node
:return: a string containing the name | f13293:c5:m1 |
def _get_child_from_path(self, path): | keys = path.split("<STR_LIT:.>")<EOL>this_child = self<EOL>for key in keys:<EOL><INDENT>try:<EOL><INDENT>this_child = this_child._get_child(key)<EOL><DEDENT>except KeyError:<EOL><INDENT>raise KeyError("<STR_LIT>" % path)<EOL><DEDENT><DEDENT>return this_child<EOL> | Return a children below this level, starting from a path of the kind "this_level.something.something.name"
:param path: the key
:return: the child | f13293:c5:m9 |
def __repr__(self): | return self._repr__base(rich_output=False)<EOL> | Textual representation for console
:return: representation | f13293:c5:m13 |
def _repr_html_(self): | return self._repr__base(rich_output=True)<EOL> | HTML representation for the IPython notebook
:return: HTML representation | f13293:c5:m14 |
def display(self): | <EOL>display(self)<EOL> | Display information about the point source.
:return: (none) | f13293:c5:m15 |
def _find_instances(self, cls): | instances = collections.OrderedDict()<EOL>for child_name, child in self._children.iteritems():<EOL><INDENT>if isinstance(child, cls):<EOL><INDENT>key_name = "<STR_LIT:.>".join(child._get_path())<EOL>instances[key_name] = child<EOL>if child._children:<EOL><INDENT>instances.update(child._find_instances(cls))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>instances.update(child._find_instances(cls))<EOL><DEDENT><DEDENT>return instances<EOL> | Find all the instances of cls below this node.
:return: a dictionary of instances of cls | f13293:c5:m16 |
@contextlib.contextmanager<EOL>def use_astromodels_memoization(switch, cache_size=_CACHE_SIZE): | global _WITH_MEMOIZATION<EOL>global _CACHE_SIZE<EOL>old_status = bool(_WITH_MEMOIZATION)<EOL>old_cache_size = int(_CACHE_SIZE)<EOL>_WITH_MEMOIZATION = bool(switch)<EOL>_CACHE_SIZE = int(cache_size)<EOL>yield<EOL>_WITH_MEMOIZATION = old_status<EOL>_CACHE_SIZE = old_cache_size<EOL> | Activate/deactivate memoization temporarily
:param switch: True (memoization on) or False (memoization off)
:param cache_size: number of previous evaluation of functions to keep in memory. Default: 100
:return: | f13295:m0 |
def memoize(method): | cache = method.cache = collections.OrderedDict()<EOL>_get = cache.get<EOL>_popitem = cache.popitem<EOL>@functools.wraps(method)<EOL>def memoizer(instance, x, *args, **kwargs):<EOL><INDENT>if not _WITH_MEMOIZATION or isinstance(x, u.Quantity):<EOL><INDENT>return method(instance, x, *args, **kwargs)<EOL><DEDENT>unique_id = tuple(float(yy.value) for yy in instance.parameters.values()) + (x.size, x.min(), x.max())<EOL>key = hash(unique_id)<EOL>result = _get(key)<EOL>if result is not None:<EOL><INDENT>return result<EOL><DEDENT>else:<EOL><INDENT>result = method(instance, x, *args, **kwargs)<EOL>cache[key] = result<EOL>if len(cache) > _CACHE_SIZE:<EOL><INDENT>[_popitem(False) for i in range(max(_CACHE_SIZE // <NUM_LIT:2>, <NUM_LIT:1>))]<EOL><DEDENT>return result<EOL><DEDENT><DEDENT>memoizer.input_object = method<EOL>return memoizer<EOL> | A decorator for functions of sources which memoize the results of the last _CACHE_SIZE calls,
:param method: method to be memoized
:return: the decorated method | f13295:m1 |
def get_transformation(transformation_name): | if not transformation_name in _known_transformations:<EOL><INDENT>raise ValueError("<STR_LIT>" % transformation_name)<EOL><DEDENT>else:<EOL><INDENT>return _known_transformations[transformation_name]()<EOL><DEDENT> | Returns an instance of a transformation by name
:param transformation_name:
:return: instance of transformation with provided name | f13296:m0 |
def load_model(filename): | parser = ModelParser(filename)<EOL>return parser.get_model()<EOL> | Load a model from a file.
:param filename: the name of the file containing the model
:return: an instance of a Model | f13298:m0 |
def clone_model(model_instance): | data = model_instance.to_dict_with_types()<EOL>parser = ModelParser(model_dict=data)<EOL>return parser.get_model()<EOL> | Returns a copy of the given model with all objects cloned. This is equivalent to saving the model to
a file and reload it, but it doesn't require writing or reading to/from disk. The original model is not touched.
:param model: model to be cloned
:return: a cloned copy of the given model | f13298:m1 |
def _check_unit(new_unit, old_unit): | try:<EOL><INDENT>new_unit.physical_type<EOL><DEDENT>except AttributeError:<EOL><INDENT>raise UnitMismatch("<STR_LIT>"<EOL>% (new_unit, old_unit.physical_type))<EOL><DEDENT>if new_unit.physical_type != old_unit.physical_type:<EOL><INDENT>raise UnitMismatch("<STR_LIT>"<EOL>% (new_unit.physical_type, old_unit.physical_type))<EOL><DEDENT> | Check that the new unit is compatible with the old unit for the quantity described by variable_name
:param new_unit: instance of astropy.units.Unit
:param old_unit: instance of astropy.units.Unit
:return: nothin | f13299:m0 |
@property<EOL><INDENT>def shape(self):<DEDENT> | return self._spectral_shape<EOL> | A generic name for the spectral shape.
:return: the spectral shape instance | f13300:c0:m2 |
def __init__(self, ra=None, dec=None, l=None, b=None, equinox='<STR_LIT>'): | self._equinox = equinox<EOL>Node.__init__(self, '<STR_LIT>')<EOL>if ra is not None and dec is not None:<EOL><INDENT>ra = self._get_parameter_from_input(ra, <NUM_LIT:0>, <NUM_LIT>, '<STR_LIT>','<STR_LIT>')<EOL>dec = self._get_parameter_from_input(dec, -<NUM_LIT>, <NUM_LIT>, '<STR_LIT>','<STR_LIT>')<EOL>self._coord_type = '<STR_LIT>'<EOL>self._add_child(ra)<EOL>self._add_child(dec)<EOL><DEDENT>elif l is not None and b is not None:<EOL><INDENT>l = self._get_parameter_from_input(l, <NUM_LIT:0>, <NUM_LIT>, '<STR_LIT:l>','<STR_LIT>')<EOL>b = self._get_parameter_from_input(b, -<NUM_LIT>, <NUM_LIT>, '<STR_LIT:b>','<STR_LIT>')<EOL>self._coord_type = '<STR_LIT>'<EOL>self._add_child(l)<EOL>self._add_child(b)<EOL><DEDENT>else:<EOL><INDENT>raise WrongCoordinatePair("<STR_LIT>")<EOL><DEDENT> | :param ra: Right Ascension in degrees
:param dec: Declination in degrees
:param l: Galactic latitude in degrees
:param b: Galactic longitude in degrees
:param equinox: string
:return: | f13301:c3:m0 |
def get_ra(self): | try:<EOL><INDENT>return self.ra.value<EOL><DEDENT>except AttributeError:<EOL><INDENT>return self.sky_coord.transform_to('<STR_LIT>').ra.value<EOL><DEDENT> | Get R.A. corresponding to the current position (ICRS, J2000)
:return: Right Ascension | f13301:c3:m2 |
def get_dec(self): | try:<EOL><INDENT>return self.dec.value<EOL><DEDENT>except AttributeError:<EOL><INDENT>return self.sky_coord.transform_to('<STR_LIT>').dec.value<EOL><DEDENT> | Get Dec. corresponding to the current position (ICRS, J2000)
:return: Declination | f13301:c3:m3 |
def get_l(self): | try:<EOL><INDENT>return self.l.value<EOL><DEDENT>except AttributeError:<EOL><INDENT>return self.sky_coord.transform_to('<STR_LIT>').l.value<EOL><DEDENT> | Get Galactic Longitude (l) corresponding to the current position
:return: Galactic Longitude | f13301:c3:m4 |
def get_b(self): | try:<EOL><INDENT>return self.b.value<EOL><DEDENT>except AttributeError:<EOL><INDENT>return self.sky_coord.transform_to('<STR_LIT>').b.value<EOL><DEDENT> | Get Galactic latitude (b) corresponding to the current position
:return: Latitude | f13301:c3:m5 |
@property<EOL><INDENT>def sky_coord(self):<DEDENT> | return self._get_sky_coord()<EOL> | Return an instance of astropy.coordinates.SkyCoord which can be used to make all transformations supported
by it
:return: astropy.coordinates.SkyCoord | f13301:c3:m7 |
@property<EOL><INDENT>def parameters(self):<DEDENT> | if self._coord_type == '<STR_LIT>':<EOL><INDENT>return collections.OrderedDict((('<STR_LIT:l>', self.l), ('<STR_LIT:b>', self.b)))<EOL><DEDENT>else:<EOL><INDENT>return collections.OrderedDict((('<STR_LIT>', self.ra), ('<STR_LIT>', self.dec)))<EOL><DEDENT> | Get the dictionary of parameters (either ra,dec or l,b)
:return: dictionary of parameters | f13301:c3:m8 |
@property<EOL><INDENT>def equinox(self):<DEDENT> | return self._equinox<EOL> | Returns the equinox for the coordinates.
:return: | f13301:c3:m9 |
def fix(self): | if self._coord_type == '<STR_LIT>':<EOL><INDENT>self.ra.fix = True<EOL>self.dec.fix = True<EOL><DEDENT>else:<EOL><INDENT>self.l.fix = True<EOL>self.b.fix = True<EOL><DEDENT> | Fix the parameters with the coordinates (either ra,dec or l,b depending on how the class
has been instanced) | f13301:c3:m11 |
def free(self): | if self._coord_type == '<STR_LIT>':<EOL><INDENT>self.ra.fix = False<EOL>self.dec.fix = False<EOL><DEDENT>else:<EOL><INDENT>self.l.fix = False<EOL>self.b.fix = False<EOL><DEDENT> | Free the parameters with the coordinates (either ra,dec or l,b depending on how the class
has been instanced) | f13301:c3:m12 |
def __init__(self, degree, angle): | super(LinearPolarization, self).__init__(polarization_type='<STR_LIT>')<EOL>degree = self._get_parameter_from_input(degree, <NUM_LIT:0>, <NUM_LIT:100>, '<STR_LIT>', '<STR_LIT>', '<STR_LIT>')<EOL>angle = self._get_parameter_from_input(angle, <NUM_LIT:0>, <NUM_LIT>, '<STR_LIT>', '<STR_LIT>', '<STR_LIT>')<EOL>self._add_child(degree)<EOL>self._add_child(angle)<EOL> | Linear parameterization of polarization
:param degree: The polarization degree
:param angle: The polarization angle | f13302:c1:m0 |
def __init__(self, I, Q, U, V): | super(StokesPolarization, self).__init__(polarization_type='<STR_LIT>')<EOL>I = self._get_parameter_from_input(I, <NUM_LIT:0>, <NUM_LIT:1>, '<STR_LIT:I>', '<STR_LIT>')<EOL>Q = self._get_parameter_from_input(Q, <NUM_LIT:0>, <NUM_LIT:1>, '<STR_LIT>', '<STR_LIT>')<EOL>U = self._get_parameter_from_input(U, <NUM_LIT:0>, <NUM_LIT:1>, '<STR_LIT>', '<STR_LIT>')<EOL>V = self._get_parameter_from_input(V, <NUM_LIT:0>, <NUM_LIT:1>, '<STR_LIT>', '<STR_LIT>')<EOL>self._add_child(I)<EOL>self._add_child(Q)<EOL>self._add_child(U)<EOL>self._add_child(V)<EOL> | Stokes parameterization of polarization
:param I:
:param Q:
:param U:
:param V: | f13302:c2:m0 |
def _behaves_like_a_number(obj): | try:<EOL><INDENT>obj + <NUM_LIT:1><EOL>obj * <NUM_LIT:2><EOL>obj / <NUM_LIT:2><EOL>obj - <NUM_LIT:1><EOL><DEDENT>except TypeError:<EOL><INDENT>return False<EOL><DEDENT>else:<EOL><INDENT>return True<EOL><DEDENT> | :param obj:
:return: True if obj supports addition, subtraction, multiplication and division. False otherwise. | f13304:m0 |
def accept_quantity(input_type=float, allow_none=False): | def accept_quantity_wrapper(method):<EOL><INDENT>def handle_quantity(instance, value, *args, **kwargs):<EOL><INDENT>try:<EOL><INDENT>new_value = input_type(value)<EOL>return method(instance, new_value, *args, **kwargs)<EOL><DEDENT>except TypeError:<EOL><INDENT>if isinstance(value, u.Quantity):<EOL><INDENT>new_value = value.to(instance.unit).value<EOL>return method(instance, new_value, *args, **kwargs)<EOL><DEDENT>elif value is None:<EOL><INDENT>if allow_none:<EOL><INDENT>return method(instance, None, *args, **kwargs)<EOL><DEDENT>else: <EOL><INDENT>raise TypeError("<STR_LIT>"<EOL>"<STR_LIT>" % (method.__name__, instance.name))<EOL><DEDENT><DEDENT>else: <EOL><INDENT>raise TypeError("<STR_LIT>"<EOL>"<STR_LIT>" % (input_type.__name__, method.__name__, instance.name))<EOL><DEDENT><DEDENT><DEDENT>return handle_quantity<EOL><DEDENT>return accept_quantity_wrapper<EOL> | A class-method decorator which allow a given method (typically the set_value method) to receive both a
astropy.Quantity or a simple float, but to be coded like it's always receiving a pure float in the right units.
This is to give a way to avoid the huge bottleneck that are astropy.units
:param input_type: the expected type for the input (float, int)
:param allow_none : whether to allow or not the passage of None as argument (default: False)
:return: a decorator for the particular type | f13304:m1 |
@property<EOL><INDENT>def static_name(self):<DEDENT> | return self._static_name<EOL> | Returns a name which will never change, even if the name of the parameter does (for example in composite
functions)
:return : the static name
:type : str | f13304:c7:m2 |
@property<EOL><INDENT>def description(self):<DEDENT> | return self._desc<EOL> | Return a description of this parameter
:return: a string cointaining a description of the meaning of this parameter | f13304:c7:m3 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.