yatel.qbj package¶
Submodules¶
yatel.qbj.core module¶
Main logic behind QBJ.
- class yatel.qbj.core.QBJEngine(nw)[source]¶
Bases: object
Responsible of storing context for QBJ queries, and executes the functions required on it.
Parameters: Network to be used with the query.
- class yatel.qbj.core.QBJResolver(function, context)[source]¶
Bases: object
Resolver of QBJ calls.
Parameters: function : dict
Keys of function:
- name function to be called.
- args positional arguments for function name.
- kwargs named arguments for function name.
For further detail on functions arguments see yatel.qbj.functions
context : yatel.db.YatelNetwork
Network to execute functions on.
yatel.qbj.functions module¶
QBJ functions domain.
- yatel.qbj.functions.amax(nw, env=None, **kwargs)[source]¶
Return the maximum in a network.
Parameters: Network to which apply the operation.
env : yatel.dom.Enviroment or dict like
Environment for filtering.
- yatel.qbj.functions.amin(nw, env=None, **kwargs)[source]¶
Return the minimum in a network.
Parameters: Network to which apply the operation.
env : yatel.dom.Enviroment or dict like
Environment for filtering.
- yatel.qbj.functions.average(nw, env=None, **kwargs)[source]¶
Compute the weighted average on a network.
Parameters: Network to which apply the operation.
env : yatel.dom.Enviroment or dict like
Environment for filtering.
- yatel.qbj.functions.capitalize(nw, string)[source]¶
Return a copy of string with its first character capitalized and the rest lowercased.
- yatel.qbj.functions.count(nw, iterable, to_count)[source]¶
Returns the number of occurrences of to_count in iterable.
- yatel.qbj.functions.describe(nw)[source]¶
Returns a yatel.dom.Descriptor object with all the information about the network.
The descriptor object is a dictionary like with keys:
- edges_attributes : dict
- Dictionary contains always 2 keys : max_nodes How many nodes connect the edge with maximun number of connections. And weight the time of weight attribute
- fact_attributes : dict
- Contains an arbitrary number of keys, with keys as attributes name, and value as attribute type.
- haplotype_atributes : dict
- Contains an arbitrary number of keys, with keys as attributes name, and value as attribute type.
- mode : str
- Actual mode of the network
- size : dict
- Has the number of elements in the network discrimined by type haplotypes, facts and edges.
Examples
>>> nw = db.YatelNetwork(...) >>> nw.describe() ... { ... u'edge_attributes': { ... u'max_nodes': 2, ... u'weight': <type 'float'> ... }, ... u'fact_attributes': { ... u'align': <type 'int'>, ... u'category': <type 'str'>, ... u'coso': <type 'str'>, ... u'hap_id': <type 'int'>, ... } ... u'haplotype_attributes': { ... u'color': <type 'str'>, ... u'description': <type 'str'>, ... u'hap_id': <type 'int'>, ... } ... u'mode': 'r', ... u'size': {u'edges': 10, u'facts': 20, u'haplotypes': 5} ... }
- yatel.qbj.functions.edges(nw)[source]¶
Iterates over all yatel.dom.Edge instances stored in the database.
REQUIRE MODE: r
Returns: iterator
Iterator of yatel.dom.Edge instances.
- yatel.qbj.functions.edges_by_environment(nw, env=None, **kwargs)[source]¶
Iterates over all yatel.dom.Edge instances of a given environment please see yatel.db.YatelNetwork.haplotypes_enviroment for more documentation about environment.
REQUIRE MODE: r
Parameters: env : dict
Keys are yatel.dom.Fact attributes name, and value is a possible value of the given attribute.
kwargs : dict
Keys are yatel.dom.Fact attributes name, and value is a possible value of the given attribte.
Returns: iterator
Iterator of yatel.dom.Edge.
- yatel.qbj.functions.edges_by_haplotype(nw, hap)[source]¶
Iterates over all the edges of a given yatel.dom.Haplotype.
REQUIRE MODE: r
Parameters: hap : yatel.dom.Haplotype
Haplotype to search with.
Returns: iterator
Iterator of yatel.dom.Edge.
- yatel.qbj.functions.endswith(nw, string, suffix, start=None, end=None)[source]¶
Return True if string ends with the specified suffix, otherwise return False. suffix can also be a tuple of suffixes to look for. With optional start, test beginning at that position. With optional end, stop comparing at that position.
- yatel.qbj.functions.env2weightarray(nw, env=None, **kwargs)[source]¶
This function always return a numpy.ndarray with this conditions:
- If nw is instance of numpy.ndarray the same array is returned.
- If nw is instance of yatel.db.YatelNetwork and an environment is given return all the edges in this environment.
- If nw is instance of yatel.db.YatelNetwork and no environment is given then return all edges.
- In the last case the function tries to convert nw to numpy.ndarray instance.
- yatel.qbj.functions.environments(nw, facts_attrs=None)[source]¶
Iterates over all combinations of environments of the given attrs.
REQUIRE MODE: r
Parameters: fact_attrs : iterable
Collection of existing fact attribute names.
Returns: iterator
Iterator of dictionaries with all valid combinations of values of a given fact_attrs names
Examples
>>> for env in nw.enviroments(["native", "place"]): ··· print env {u'place': None, u'native': True} {u'place': u'Hogwarts', u'native': False} {u'place': None, u'native': False} {u'place': u'Mordor', u'native': True} {u'place': None, u'native': None} ...
- yatel.qbj.functions.facts(nw)[source]¶
Iterates over all yatel.dom.Fact instances stored in the database.
- yatel.qbj.functions.facts_by_environment(nw, env=None, **kwargs)[source]¶
Iterates over all yatel.dom.Fact instances of a given environment please see yatel.db.YatelNetwork.haplotypes_environment for more documentation about environment.
REQUIRE MODE: r
Parameters: env : dict
Keys are yatel.dom.Fact attributes name, and value is a possible value of the given attribute.
kwargs : dict of keywords arguments
Keys are yatel.dom.Fact attributes name, and value is a possible value of the given attribute.
Returns: iterator
Iteratorterator of yatel.dom.Fact.
- yatel.qbj.functions.facts_by_haplotype(nw, hap)[source]¶
Return a iterator of all facts of a given yatel.dom.Haplotype.
Parameters: hap : yatel.dom.Haplotype
Haplotype to search with.
Returns: iterator
Iterator of yatel.dom.Fact.
- yatel.qbj.functions.find(nw, string, subs, start=None, end=None)[source]¶
Return the lowest index in string where the substring sub is found such that sub is wholly contained in string[start:end]. Return -1 on failure. Defaults for start and end and interpretation of negative values is the same as for slices.
- yatel.qbj.functions.floor(nw, dividend, divider)[source]¶
Return mod from division operation between dividend and divider.
- yatel.qbj.functions.haplotype_by_id(nw, hap_id)[source]¶
Return a dom.Haplotype instace stored in the dabase with the given hap_id.
REQUIRE MODE: r
Parameters: hap_id : id of the haplotypes type table.
Returns: yatel.dom.Haplotype instance.
- yatel.qbj.functions.haplotypes(nw)[source]¶
Iterates over all yatel.dom.Haplotype instances stored in the database.
REQUIRE MODE: r
Returns: iterator
iterator of yatel.dom.Haplotypes instances.
- yatel.qbj.functions.haplotypes_by_environment(nw, env=None, **kwargs)[source]¶
Return an iterator of yatel.dom.Haplotype related to a yatel.dom.Fact with attribute and value specified in env and kwargs.
REQUIRE MODE: r
Parameters: env : dict
Keys are yatel.dom.Fact attributes name, and value is a possible value of the given attribute.
kwargs : a dict of keywords arguments
Keys are yatel.dom.Fact attributes name, and value is a possible value of the given attribute.
Returns: iterator
Iterator of yatel.dom.Haplotype.
Examples
>>> from yatel import db, dom >>> nw = db.YatelNetwork("sqlite", mode=db.MODE_WRITE, database="nw.db") >>> nw.add_elements([dom.Haplotype("hap1"), ··· dom.Haplotype("hap2"), ··· dom.Fact("hap1", a=1, c="foo"), ··· dom.Fact("hap2", a=1, b=2), ··· dom.Edge(1, ("hap1", "hap2"))]) >>> nw.confirm_changes() >>> tuple(nw.haplotypes_enviroment(a=1)) (<Haplotype 'hap1' at 0x2463250>, <Haplotype 'hap2' at 0x2463390>) >>> tuple(nw.haplotypes_enviroment({"c": "foo"})) (<Haplotype 'hap1' at 0x2463250>, ) >>> tuple(nw.haplotypes_enviroment({"a": 1}, b=2)) (<Haplotype 'hap2' at 0x2463390>, )
- yatel.qbj.functions.help(nw, fname=None)[source]¶
Returns a list of all functions if fname is not specified or None, otherwise documentation for fname.
- yatel.qbj.functions.index(nw, iterable, value, start=None, end=None)[source]¶
Return the lowest index in iterable where value is found. Returns -1 if not found.
Parameters: network source of data.
iterable : iterator
iterable object.
value :
Value to look for.
start : int or None
Starting point.
end : int or None
Finishing point.
- yatel.qbj.functions.isalnum(nw, string)[source]¶
Return true if all characters in string are alphanumeric and there is at least one character, false otherwise.
- yatel.qbj.functions.isalpha(nw, string)[source]¶
Return true if all characters in string are alphabetic and there is at least one character, false otherwise.
- yatel.qbj.functions.isdigit(nw, string)[source]¶
Return true if all characters in string are digits and there is at least one character, false otherwise.
- yatel.qbj.functions.islower(nw, string)[source]¶
Return true if all cased characters in string are lowercase and there is at least one cased character, false otherwise.
- yatel.qbj.functions.isspace(nw, string)[source]¶
Return true if there are only whitespace characters in string and there is at least one character, false otherwise.
- yatel.qbj.functions.istitle(nw, string)[source]¶
Return true if string is a titlecased string and there is at least one character.
- yatel.qbj.functions.isupper(nw, string)[source]¶
Return true if all cased characters in string are uppercase and there is at least one cased character, false otherwise.
- yatel.qbj.functions.join(nw, joiner, to_join)[source]¶
Concatenate a list or tuple of words with intervening occurrences of joiner.
- yatel.qbj.functions.kmeans(nw, envs, k_or_guess, whiten=False, coords=None, *args, **kwargs)[source]¶
- yatel.qbj.functions.kurtosis(nw, env=None, **kwargs)[source]¶
Computes the kurtosis (Fisher’s definition) of a network.
Parameters: Network to which apply the operation.
env : yatel.dom.Enviroment or dict like
Environment for filtering.
- yatel.qbj.functions.lower(nw, string)[source]¶
Return a copy of string, with upper case letters converted to lower case.
- yatel.qbj.functions.lstrip(nw, string, chars=None)[source]¶
Return a copy of string with leading characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the beginning of the string.
- yatel.qbj.functions.max(nw, env=None, **kwargs)[source]¶
Return the maximum in a network.
Parameters: Network to which apply the operation.
env : yatel.dom.Enviroment or dict like
Environment for filtering.
- yatel.qbj.functions.median(nw, env=None, **kwargs)[source]¶
Compute the median on a network.
Parameters: Network to which apply the operation.
env : yatel.dom.Enviroment or dict like
Environment for filtering.
- yatel.qbj.functions.min(nw, env=None, **kwargs)[source]¶
Return the minimum in a network.
Parameters: Network to which apply the operation.
env : yatel.dom.Enviroment or dict like
Environment for filtering.
- yatel.qbj.functions.mode(nw, env=None, **kwargs)[source]¶
Calculates mode on a network.
Parameters: Network to which apply the operation.
env : yatel.dom.Enviroment or dict like
Environment for filtering.
- yatel.qbj.functions.percentile(nw, q, env=None, **kwargs)[source]¶
Compute the q-th percentile of the network.
Parameters: Network to which apply the operation.
env : yatel.dom.Enviroment or dict like
Environment for filtering.
- yatel.qbj.functions.range(nw, env=None, **kwargs)[source]¶
Computes the distance between the maximum and minimum.
Parameters: Network to which apply the operation.
env : yatel.dom.Enviroment or dict like
Environment for filtering.
- yatel.qbj.functions.replace(nw, string, old, new, count=None)[source]¶
Return a copy of string with all occurrences of old replaced by new. If count is given, the first count occurrences are replaced.
- yatel.qbj.functions.rfind(nw, string, subs, start=None, end=None)[source]¶
Return the highest index in string where the substring sub is found.
- yatel.qbj.functions.rsplit(nw, string, s=None, maxsplit=None)[source]¶
Return a list of the words of string, scanning s from the end. With s as separator and maximum number of split by maxsplit.
Parameters: string : str
String to split.
s : str ot None
Used as separator if given, if None uses whitespace characters as separators.
maxsplit : int or None
Maximum number of split on string and the remainder of the string is returned as the first element of the list, if None no limit.
- yatel.qbj.functions.rstrip(nw, string, chars=None)[source]¶
Return a copy of string with trailing characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the end of the string.
- yatel.qbj.functions.slice(nw, iterable, f, t=None)[source]¶
Returns iterable from F-th element to T-th element.
Parameters: network source of data.
iterable : iterator
iterable object.
f : int
Starting point.
t : int or None
Finishing point.
- yatel.qbj.functions.sort(nw, iterable, key=None, dkey=None, reverse=False)[source]¶
Sorts iterable using one of it’s keys as reference.
Parameters: network source of data.
iterable : iterator
iterable object.
key : str or None
A key of objects in iterable to sort.
dkey : str or None
Defalut key to sort.
- yatel.qbj.functions.split(nw, string, s=None, maxsplit=None)[source]¶
Return a list of the words of string. With s as separator and maximum number of split by maxsplit.
Parameters: string : str
String to split.
s : str ot None
Used as separator if given, if None uses whitespace characters as separators.
maxsplit : int or None
Maximum number of split on string and the remainder of the string is returned as the final element of the list, if None no limit.
- yatel.qbj.functions.startswith(nw, string, prefix, start=None, end=None)[source]¶
Return True if string starts with the prefix, otherwise return False. prefix can also be a tuple of prefixes to look for. With optional start, test string beginning at that position. With optional end, stop comparing string at that position.
- yatel.qbj.functions.std(nw, env=None, **kwargs)[source]¶
This function always return a numpy.ndarray with this conditions:
- If nw is instance of numpy.ndarray the same array is returned.
- If nw is instance of yatel.db.YatelNetwork and an environment is given return all the edges in this environment.
- If nw is instance of yatel.db.YatelNetwork and no environment is given then return all edges.
- In the last case the function tries to convert nw to numpy.ndarray instance.
- yatel.qbj.functions.strip(nw, string, chars=None)[source]¶
Return a copy of string with leading and trailing characters removed. if chars is None whitespaces are removed otherwise the characters in the string will be stripped from the both ends.
- yatel.qbj.functions.sum(nw, env=None, **kwargs)[source]¶
Sum of the elements on the network.
Parameters: Network to which apply the operation.
env : yatel.dom.Enviroment or dict like
Environment for filtering.
- yatel.qbj.functions.swapcase(nw, string)[source]¶
Return a copy of string, with lower case letters converted to upper case and vice versa.
- yatel.qbj.functions.time(nw, *args, **kwargs)[source]¶
Return time object of current local date and time..
- yatel.qbj.functions.title(nw, string)[source]¶
Returns a copy of string in which first characters of all the words are capitalized.
- yatel.qbj.functions.upper(nw, string)[source]¶
Return a copy of string, with lower case letters converted to upper case.
- yatel.qbj.functions.utctime(nw, *args, **kwargs)[source]¶
Return time object of current UTC date and time.
- yatel.qbj.functions.utctoday(nw, *args, **kwargs)[source]¶
Return date object of current UTC date and time.
- yatel.qbj.functions.var(nw, env=None, **kwargs)[source]¶
Compute the variance of the network.
Parameters: Network to which apply the operation.
env : yatel.dom.Enviroment or dict like
Environment for filtering.
yatel.qbj.schema module¶
Defines the schema to validate all incomming QBJ.
- yatel.qbj.schema.DEFINITIONS = {'TYPE_ARRAY_DEF': {'items': {'$ref': '#/definitions/TYPE_DEF'}, 'type': 'array'}, 'ARGUMENT_FUNCTION_DEF': {'additionalProperties': False, 'required': ['type', 'function'], 'type': 'object', 'properties': {'function': {'$ref': '#/definitions/FUNCTION_DEF'}, 'type': {'$ref': '#/definitions/TYPE_DEF'}}}, 'FUNCTION_DEF': {'additionalProperties': False, 'required': ['name'], 'type': 'object', 'properties': {'args': {'items': {'oneOf': [{'$ref': '#/definitions/ARGUMENT_STATIC_DEF'}, {'$ref': '#/definitions/ARGUMENT_FUNCTION_DEF'}]}, 'type': 'array'}, 'name': {'enum': ['ping', 'help', 'haplotypes', 'haplotype_by_id', 'haplotypes_by_environment', 'edges', 'edges_by_haplotype', 'edges_by_environment', 'facts', 'facts_by_haplotype', 'facts_by_environment', 'describe', 'environments', 'amax', 'amin', 'average', 'env2weightarray', 'kurtosis', 'max', 'median', 'min', 'mode', 'percentile', 'range', 'std', 'sum', 'var', 'variation', 'kmeans', 'slice', 'size', 'sort', 'index', 'now', 'utcnow', 'today', 'utctoday', 'time', 'utctime', 'minus', 'times', 'div', 'floor', 'pow', 'xroot', 'count', 'split', 'rsplit', 'strip', 'lstrip', 'rstrip', 'join', 'upper', 'lower', 'title', 'capitalize', 'isalnum', 'isalpha', 'isdigit', 'startswith', 'endswith', 'istitle', 'isupper', 'isspace', 'islower', 'swapcase', 'replace', 'find', 'rfind'], 'type': 'string'}, 'kwargs': {'patternProperties': {'.*': {'oneOf': [{'$ref': '#/definitions/ARGUMENT_STATIC_DEF'}, {'$ref': '#/definitions/ARGUMENT_FUNCTION_DEF'}]}}, 'type': 'object'}}}, 'TYPE_SINGLE_DEF': {'enum': ['set', 'int', 'float', 'datetime', 'Environment', 'unicode', 'generator', 'long', 'complex', 'dict', 'bool', 'type', 'tuple', 'Decimal', 'Descriptor', 'Edge', 'frozenset', 'date', 'Fact', 'Haplotype', 'list', 'time', 'NoneType'], 'type': 'string'}, 'ARGUMENT_STATIC_DEF': {'additionalProperties': False, 'required': ['type', 'value'], 'type': 'object', 'properties': {'type': {'$ref': '#/definitions/TYPE_DEF'}, 'value': {}}}, 'TYPE_OBJECT_DEF': {'patternProperties': {'.*': {'$ref': '#/definitions/TYPE_DEF'}}, 'type': 'object'}, 'TYPE_DEF': {'oneOf': [{'$ref': '#/definitions/TYPE_SINGLE_DEF'}, {'$ref': '#/definitions/TYPE_ARRAY_DEF'}, {'$ref': '#/definitions/TYPE_OBJECT_DEF'}, {'enum': ['literal', 'iterable'], 'type': 'string'}]}}¶
Extra definitions for the schema validation.
- yatel.qbj.schema.QBJ_SCHEMA = {'description': 'Defines the schema to validate all incomming QBJ.\n\n- http://www.tutorialspoint.com/json/json_schema.htm\n\n', 'title': 'Yatel QBJ Schema', 'required': ['id', 'function'], 'additionalProperties': False, 'definitions': {'TYPE_ARRAY_DEF': {'items': {'$ref': '#/definitions/TYPE_DEF'}, 'type': 'array'}, 'ARGUMENT_FUNCTION_DEF': {'additionalProperties': False, 'required': ['type', 'function'], 'type': 'object', 'properties': {'function': {'$ref': '#/definitions/FUNCTION_DEF'}, 'type': {'$ref': '#/definitions/TYPE_DEF'}}}, 'FUNCTION_DEF': {'additionalProperties': False, 'required': ['name'], 'type': 'object', 'properties': {'args': {'items': {'oneOf': [{'$ref': '#/definitions/ARGUMENT_STATIC_DEF'}, {'$ref': '#/definitions/ARGUMENT_FUNCTION_DEF'}]}, 'type': 'array'}, 'name': {'enum': ['ping', 'help', 'haplotypes', 'haplotype_by_id', 'haplotypes_by_environment', 'edges', 'edges_by_haplotype', 'edges_by_environment', 'facts', 'facts_by_haplotype', 'facts_by_environment', 'describe', 'environments', 'amax', 'amin', 'average', 'env2weightarray', 'kurtosis', 'max', 'median', 'min', 'mode', 'percentile', 'range', 'std', 'sum', 'var', 'variation', 'kmeans', 'slice', 'size', 'sort', 'index', 'now', 'utcnow', 'today', 'utctoday', 'time', 'utctime', 'minus', 'times', 'div', 'floor', 'pow', 'xroot', 'count', 'split', 'rsplit', 'strip', 'lstrip', 'rstrip', 'join', 'upper', 'lower', 'title', 'capitalize', 'isalnum', 'isalpha', 'isdigit', 'startswith', 'endswith', 'istitle', 'isupper', 'isspace', 'islower', 'swapcase', 'replace', 'find', 'rfind'], 'type': 'string'}, 'kwargs': {'patternProperties': {'.*': {'oneOf': [{'$ref': '#/definitions/ARGUMENT_STATIC_DEF'}, {'$ref': '#/definitions/ARGUMENT_FUNCTION_DEF'}]}}, 'type': 'object'}}}, 'TYPE_SINGLE_DEF': {'enum': ['set', 'int', 'float', 'datetime', 'Environment', 'unicode', 'generator', 'long', 'complex', 'dict', 'bool', 'type', 'tuple', 'Decimal', 'Descriptor', 'Edge', 'frozenset', 'date', 'Fact', 'Haplotype', 'list', 'time', 'NoneType'], 'type': 'string'}, 'ARGUMENT_STATIC_DEF': {'additionalProperties': False, 'required': ['type', 'value'], 'type': 'object', 'properties': {'type': {'$ref': '#/definitions/TYPE_DEF'}, 'value': {}}}, 'TYPE_OBJECT_DEF': {'patternProperties': {'.*': {'$ref': '#/definitions/TYPE_DEF'}}, 'type': 'object'}, 'TYPE_DEF': {'oneOf': [{'$ref': '#/definitions/TYPE_SINGLE_DEF'}, {'$ref': '#/definitions/TYPE_ARRAY_DEF'}, {'$ref': '#/definitions/TYPE_OBJECT_DEF'}, {'enum': ['literal', 'iterable'], 'type': 'string'}]}}, 'type': 'object', 'properties': {'function': {'$ref': '#/definitions/FUNCTION_DEF'}, 'id': {'type': ['string', 'number', 'boolean', 'null']}}}¶
Schema to validate QBJ queries.
yatel.qbj.shell module¶
Interactive shell for QBJ.
- class yatel.qbj.shell.QBJShell(nw, debug)[source]¶
Bases: cmd.Cmd
Write your QBJQuery and end it with ‘;’
Special Commands:
- help: Print this help.
- exec <PATH>: execute a query from a qbj file.
- fhelp: Print a list of all available QBJ-functions
- fhelp <FNAME>: print help about FNAME function.
- indent <INT>: change indentation of response (Default: None).
Module contents¶
This package contains all necesary function and classes to execute queries using JSON