id
int32
0
252k
repo
stringlengths
7
55
path
stringlengths
4
127
func_name
stringlengths
1
88
original_string
stringlengths
75
19.8k
language
stringclasses
1 value
code
stringlengths
51
19.8k
code_tokens
list
docstring
stringlengths
3
17.3k
docstring_tokens
list
sha
stringlengths
40
40
url
stringlengths
87
242
227,600
tobgu/pyrsistent
pyrsistent/_pdeque.py
PDeque.append
def append(self, elem): """ Return new deque with elem as the rightmost element. >>> pdeque([1, 2]).append(3) pdeque([1, 2, 3]) """ new_left_list, new_right_list, new_length = self._append(self._left_list, self._right_list, elem) return PDeque(new_left_list, new_right_list, new_length, self._maxlen)
python
def append(self, elem): new_left_list, new_right_list, new_length = self._append(self._left_list, self._right_list, elem) return PDeque(new_left_list, new_right_list, new_length, self._maxlen)
[ "def", "append", "(", "self", ",", "elem", ")", ":", "new_left_list", ",", "new_right_list", ",", "new_length", "=", "self", ".", "_append", "(", "self", ".", "_left_list", ",", "self", ".", "_right_list", ",", "elem", ")", "return", "PDeque", "(", "new_...
Return new deque with elem as the rightmost element. >>> pdeque([1, 2]).append(3) pdeque([1, 2, 3])
[ "Return", "new", "deque", "with", "elem", "as", "the", "rightmost", "element", "." ]
c84dab0daaa44973cbe83830d14888827b307632
https://github.com/tobgu/pyrsistent/blob/c84dab0daaa44973cbe83830d14888827b307632/pyrsistent/_pdeque.py#L183-L191
227,601
tobgu/pyrsistent
pyrsistent/_pdeque.py
PDeque.extend
def extend(self, iterable): """ Return new deque with all elements of iterable appended to the right. >>> pdeque([1, 2]).extend([3, 4]) pdeque([1, 2, 3, 4]) """ new_right_list, new_left_list, extend_count = self._extend(self._right_list, self._left_list, iterable) return PDeque(new_left_list, new_right_list, self._length + extend_count, self._maxlen)
python
def extend(self, iterable): new_right_list, new_left_list, extend_count = self._extend(self._right_list, self._left_list, iterable) return PDeque(new_left_list, new_right_list, self._length + extend_count, self._maxlen)
[ "def", "extend", "(", "self", ",", "iterable", ")", ":", "new_right_list", ",", "new_left_list", ",", "extend_count", "=", "self", ".", "_extend", "(", "self", ".", "_right_list", ",", "self", ".", "_left_list", ",", "iterable", ")", "return", "PDeque", "(...
Return new deque with all elements of iterable appended to the right. >>> pdeque([1, 2]).extend([3, 4]) pdeque([1, 2, 3, 4])
[ "Return", "new", "deque", "with", "all", "elements", "of", "iterable", "appended", "to", "the", "right", "." ]
c84dab0daaa44973cbe83830d14888827b307632
https://github.com/tobgu/pyrsistent/blob/c84dab0daaa44973cbe83830d14888827b307632/pyrsistent/_pdeque.py#L232-L240
227,602
tobgu/pyrsistent
pyrsistent/_pdeque.py
PDeque.count
def count(self, elem): """ Return the number of elements equal to elem present in the queue >>> pdeque([1, 2, 1]).count(1) 2 """ return self._left_list.count(elem) + self._right_list.count(elem)
python
def count(self, elem): return self._left_list.count(elem) + self._right_list.count(elem)
[ "def", "count", "(", "self", ",", "elem", ")", ":", "return", "self", ".", "_left_list", ".", "count", "(", "elem", ")", "+", "self", ".", "_right_list", ".", "count", "(", "elem", ")" ]
Return the number of elements equal to elem present in the queue >>> pdeque([1, 2, 1]).count(1) 2
[ "Return", "the", "number", "of", "elements", "equal", "to", "elem", "present", "in", "the", "queue" ]
c84dab0daaa44973cbe83830d14888827b307632
https://github.com/tobgu/pyrsistent/blob/c84dab0daaa44973cbe83830d14888827b307632/pyrsistent/_pdeque.py#L254-L261
227,603
tobgu/pyrsistent
pyrsistent/_pdeque.py
PDeque.remove
def remove(self, elem): """ Return new deque with first element from left equal to elem removed. If no such element is found a ValueError is raised. >>> pdeque([2, 1, 2]).remove(2) pdeque([1, 2]) """ try: return PDeque(self._left_list.remove(elem), self._right_list, self._length - 1) except ValueError: # Value not found in left list, try the right list try: # This is severely inefficient with a double reverse, should perhaps implement a remove_last()? return PDeque(self._left_list, self._right_list.reverse().remove(elem).reverse(), self._length - 1) except ValueError: raise ValueError('{0} not found in PDeque'.format(elem))
python
def remove(self, elem): try: return PDeque(self._left_list.remove(elem), self._right_list, self._length - 1) except ValueError: # Value not found in left list, try the right list try: # This is severely inefficient with a double reverse, should perhaps implement a remove_last()? return PDeque(self._left_list, self._right_list.reverse().remove(elem).reverse(), self._length - 1) except ValueError: raise ValueError('{0} not found in PDeque'.format(elem))
[ "def", "remove", "(", "self", ",", "elem", ")", ":", "try", ":", "return", "PDeque", "(", "self", ".", "_left_list", ".", "remove", "(", "elem", ")", ",", "self", ".", "_right_list", ",", "self", ".", "_length", "-", "1", ")", "except", "ValueError",...
Return new deque with first element from left equal to elem removed. If no such element is found a ValueError is raised. >>> pdeque([2, 1, 2]).remove(2) pdeque([1, 2])
[ "Return", "new", "deque", "with", "first", "element", "from", "left", "equal", "to", "elem", "removed", ".", "If", "no", "such", "element", "is", "found", "a", "ValueError", "is", "raised", "." ]
c84dab0daaa44973cbe83830d14888827b307632
https://github.com/tobgu/pyrsistent/blob/c84dab0daaa44973cbe83830d14888827b307632/pyrsistent/_pdeque.py#L263-L280
227,604
tobgu/pyrsistent
pyrsistent/_pdeque.py
PDeque.rotate
def rotate(self, steps): """ Return deque with elements rotated steps steps. >>> x = pdeque([1, 2, 3]) >>> x.rotate(1) pdeque([3, 1, 2]) >>> x.rotate(-2) pdeque([3, 1, 2]) """ popped_deque = self.pop(steps) if steps >= 0: return popped_deque.extendleft(islice(self.reverse(), steps)) return popped_deque.extend(islice(self, -steps))
python
def rotate(self, steps): popped_deque = self.pop(steps) if steps >= 0: return popped_deque.extendleft(islice(self.reverse(), steps)) return popped_deque.extend(islice(self, -steps))
[ "def", "rotate", "(", "self", ",", "steps", ")", ":", "popped_deque", "=", "self", ".", "pop", "(", "steps", ")", "if", "steps", ">=", "0", ":", "return", "popped_deque", ".", "extendleft", "(", "islice", "(", "self", ".", "reverse", "(", ")", ",", ...
Return deque with elements rotated steps steps. >>> x = pdeque([1, 2, 3]) >>> x.rotate(1) pdeque([3, 1, 2]) >>> x.rotate(-2) pdeque([3, 1, 2])
[ "Return", "deque", "with", "elements", "rotated", "steps", "steps", "." ]
c84dab0daaa44973cbe83830d14888827b307632
https://github.com/tobgu/pyrsistent/blob/c84dab0daaa44973cbe83830d14888827b307632/pyrsistent/_pdeque.py#L297-L311
227,605
tobgu/pyrsistent
pyrsistent/_pmap.py
PMap.set
def set(self, key, val): """ Return a new PMap with key and val inserted. >>> m1 = m(a=1, b=2) >>> m2 = m1.set('a', 3) >>> m3 = m1.set('c' ,4) >>> m1 pmap({'a': 1, 'b': 2}) >>> m2 pmap({'a': 3, 'b': 2}) >>> m3 pmap({'a': 1, 'c': 4, 'b': 2}) """ return self.evolver().set(key, val).persistent()
python
def set(self, key, val): return self.evolver().set(key, val).persistent()
[ "def", "set", "(", "self", ",", "key", ",", "val", ")", ":", "return", "self", ".", "evolver", "(", ")", ".", "set", "(", "key", ",", "val", ")", ".", "persistent", "(", ")" ]
Return a new PMap with key and val inserted. >>> m1 = m(a=1, b=2) >>> m2 = m1.set('a', 3) >>> m3 = m1.set('c' ,4) >>> m1 pmap({'a': 1, 'b': 2}) >>> m2 pmap({'a': 3, 'b': 2}) >>> m3 pmap({'a': 1, 'c': 4, 'b': 2})
[ "Return", "a", "new", "PMap", "with", "key", "and", "val", "inserted", "." ]
c84dab0daaa44973cbe83830d14888827b307632
https://github.com/tobgu/pyrsistent/blob/c84dab0daaa44973cbe83830d14888827b307632/pyrsistent/_pmap.py#L168-L182
227,606
tobgu/pyrsistent
pyrsistent/_pmap.py
PMap.update_with
def update_with(self, update_fn, *maps): """ Return a new PMap with the items in Mappings maps inserted. If the same key is present in multiple maps the values will be merged using merge_fn going from left to right. >>> from operator import add >>> m1 = m(a=1, b=2) >>> m1.update_with(add, m(a=2)) pmap({'a': 3, 'b': 2}) The reverse behaviour of the regular merge. Keep the leftmost element instead of the rightmost. >>> m1 = m(a=1) >>> m1.update_with(lambda l, r: l, m(a=2), {'a':3}) pmap({'a': 1}) """ evolver = self.evolver() for map in maps: for key, value in map.items(): evolver.set(key, update_fn(evolver[key], value) if key in evolver else value) return evolver.persistent()
python
def update_with(self, update_fn, *maps): evolver = self.evolver() for map in maps: for key, value in map.items(): evolver.set(key, update_fn(evolver[key], value) if key in evolver else value) return evolver.persistent()
[ "def", "update_with", "(", "self", ",", "update_fn", ",", "*", "maps", ")", ":", "evolver", "=", "self", ".", "evolver", "(", ")", "for", "map", "in", "maps", ":", "for", "key", ",", "value", "in", "map", ".", "items", "(", ")", ":", "evolver", "...
Return a new PMap with the items in Mappings maps inserted. If the same key is present in multiple maps the values will be merged using merge_fn going from left to right. >>> from operator import add >>> m1 = m(a=1, b=2) >>> m1.update_with(add, m(a=2)) pmap({'a': 3, 'b': 2}) The reverse behaviour of the regular merge. Keep the leftmost element instead of the rightmost. >>> m1 = m(a=1) >>> m1.update_with(lambda l, r: l, m(a=2), {'a':3}) pmap({'a': 1})
[ "Return", "a", "new", "PMap", "with", "the", "items", "in", "Mappings", "maps", "inserted", ".", "If", "the", "same", "key", "is", "present", "in", "multiple", "maps", "the", "values", "will", "be", "merged", "using", "merge_fn", "going", "from", "left", ...
c84dab0daaa44973cbe83830d14888827b307632
https://github.com/tobgu/pyrsistent/blob/c84dab0daaa44973cbe83830d14888827b307632/pyrsistent/_pmap.py#L222-L243
227,607
tobgu/pyrsistent
pyrsistent/_transformations.py
rex
def rex(expr): """ Regular expression matcher to use together with transform functions """ r = re.compile(expr) return lambda key: isinstance(key, six.string_types) and r.match(key)
python
def rex(expr): r = re.compile(expr) return lambda key: isinstance(key, six.string_types) and r.match(key)
[ "def", "rex", "(", "expr", ")", ":", "r", "=", "re", ".", "compile", "(", "expr", ")", "return", "lambda", "key", ":", "isinstance", "(", "key", ",", "six", ".", "string_types", ")", "and", "r", ".", "match", "(", "key", ")" ]
Regular expression matcher to use together with transform functions
[ "Regular", "expression", "matcher", "to", "use", "together", "with", "transform", "functions" ]
c84dab0daaa44973cbe83830d14888827b307632
https://github.com/tobgu/pyrsistent/blob/c84dab0daaa44973cbe83830d14888827b307632/pyrsistent/_transformations.py#L32-L35
227,608
tobgu/pyrsistent
pyrsistent/_pbag.py
PBag.update
def update(self, iterable): """ Update bag with all elements in iterable. >>> s = pbag([1]) >>> s.update([1, 2]) pbag([1, 1, 2]) """ if iterable: return PBag(reduce(_add_to_counters, iterable, self._counts)) return self
python
def update(self, iterable): if iterable: return PBag(reduce(_add_to_counters, iterable, self._counts)) return self
[ "def", "update", "(", "self", ",", "iterable", ")", ":", "if", "iterable", ":", "return", "PBag", "(", "reduce", "(", "_add_to_counters", ",", "iterable", ",", "self", ".", "_counts", ")", ")", "return", "self" ]
Update bag with all elements in iterable. >>> s = pbag([1]) >>> s.update([1, 2]) pbag([1, 1, 2])
[ "Update", "bag", "with", "all", "elements", "in", "iterable", "." ]
c84dab0daaa44973cbe83830d14888827b307632
https://github.com/tobgu/pyrsistent/blob/c84dab0daaa44973cbe83830d14888827b307632/pyrsistent/_pbag.py#L52-L63
227,609
tobgu/pyrsistent
pyrsistent/_pbag.py
PBag.remove
def remove(self, element): """ Remove an element from the bag. >>> s = pbag([1, 1, 2]) >>> s2 = s.remove(1) >>> s3 = s.remove(2) >>> s2 pbag([1, 2]) >>> s3 pbag([1, 1]) """ if element not in self._counts: raise KeyError(element) elif self._counts[element] == 1: newc = self._counts.remove(element) else: newc = self._counts.set(element, self._counts[element] - 1) return PBag(newc)
python
def remove(self, element): if element not in self._counts: raise KeyError(element) elif self._counts[element] == 1: newc = self._counts.remove(element) else: newc = self._counts.set(element, self._counts[element] - 1) return PBag(newc)
[ "def", "remove", "(", "self", ",", "element", ")", ":", "if", "element", "not", "in", "self", ".", "_counts", ":", "raise", "KeyError", "(", "element", ")", "elif", "self", ".", "_counts", "[", "element", "]", "==", "1", ":", "newc", "=", "self", "...
Remove an element from the bag. >>> s = pbag([1, 1, 2]) >>> s2 = s.remove(1) >>> s3 = s.remove(2) >>> s2 pbag([1, 2]) >>> s3 pbag([1, 1])
[ "Remove", "an", "element", "from", "the", "bag", "." ]
c84dab0daaa44973cbe83830d14888827b307632
https://github.com/tobgu/pyrsistent/blob/c84dab0daaa44973cbe83830d14888827b307632/pyrsistent/_pbag.py#L65-L83
227,610
tobgu/pyrsistent
pyrsistent/_helpers.py
freeze
def freeze(o): """ Recursively convert simple Python containers into pyrsistent versions of those containers. - list is converted to pvector, recursively - dict is converted to pmap, recursively on values (but not keys) - set is converted to pset, but not recursively - tuple is converted to tuple, recursively. Sets and dict keys are not recursively frozen because they do not contain mutable data by convention. The main exception to this rule is that dict keys and set elements are often instances of mutable objects that support hash-by-id, which this function can't convert anyway. >>> freeze(set([1, 2])) pset([1, 2]) >>> freeze([1, {'a': 3}]) pvector([1, pmap({'a': 3})]) >>> freeze((1, [])) (1, pvector([])) """ typ = type(o) if typ is dict: return pmap(dict((k, freeze(v)) for k, v in six.iteritems(o))) if typ is list: return pvector(map(freeze, o)) if typ is tuple: return tuple(map(freeze, o)) if typ is set: return pset(o) return o
python
def freeze(o): typ = type(o) if typ is dict: return pmap(dict((k, freeze(v)) for k, v in six.iteritems(o))) if typ is list: return pvector(map(freeze, o)) if typ is tuple: return tuple(map(freeze, o)) if typ is set: return pset(o) return o
[ "def", "freeze", "(", "o", ")", ":", "typ", "=", "type", "(", "o", ")", "if", "typ", "is", "dict", ":", "return", "pmap", "(", "dict", "(", "(", "k", ",", "freeze", "(", "v", ")", ")", "for", "k", ",", "v", "in", "six", ".", "iteritems", "(...
Recursively convert simple Python containers into pyrsistent versions of those containers. - list is converted to pvector, recursively - dict is converted to pmap, recursively on values (but not keys) - set is converted to pset, but not recursively - tuple is converted to tuple, recursively. Sets and dict keys are not recursively frozen because they do not contain mutable data by convention. The main exception to this rule is that dict keys and set elements are often instances of mutable objects that support hash-by-id, which this function can't convert anyway. >>> freeze(set([1, 2])) pset([1, 2]) >>> freeze([1, {'a': 3}]) pvector([1, pmap({'a': 3})]) >>> freeze((1, [])) (1, pvector([]))
[ "Recursively", "convert", "simple", "Python", "containers", "into", "pyrsistent", "versions", "of", "those", "containers", "." ]
c84dab0daaa44973cbe83830d14888827b307632
https://github.com/tobgu/pyrsistent/blob/c84dab0daaa44973cbe83830d14888827b307632/pyrsistent/_helpers.py#L8-L39
227,611
tobgu/pyrsistent
pyrsistent/_helpers.py
thaw
def thaw(o): """ Recursively convert pyrsistent containers into simple Python containers. - pvector is converted to list, recursively - pmap is converted to dict, recursively on values (but not keys) - pset is converted to set, but not recursively - tuple is converted to tuple, recursively. >>> from pyrsistent import s, m, v >>> thaw(s(1, 2)) set([1, 2]) >>> thaw(v(1, m(a=3))) [1, {'a': 3}] >>> thaw((1, v())) (1, []) """ if isinstance(o, PVector): return list(map(thaw, o)) if isinstance(o, PMap): return dict((k, thaw(v)) for k, v in o.iteritems()) if isinstance(o, PSet): return set(o) if type(o) is tuple: return tuple(map(thaw, o)) return o
python
def thaw(o): if isinstance(o, PVector): return list(map(thaw, o)) if isinstance(o, PMap): return dict((k, thaw(v)) for k, v in o.iteritems()) if isinstance(o, PSet): return set(o) if type(o) is tuple: return tuple(map(thaw, o)) return o
[ "def", "thaw", "(", "o", ")", ":", "if", "isinstance", "(", "o", ",", "PVector", ")", ":", "return", "list", "(", "map", "(", "thaw", ",", "o", ")", ")", "if", "isinstance", "(", "o", ",", "PMap", ")", ":", "return", "dict", "(", "(", "k", ",...
Recursively convert pyrsistent containers into simple Python containers. - pvector is converted to list, recursively - pmap is converted to dict, recursively on values (but not keys) - pset is converted to set, but not recursively - tuple is converted to tuple, recursively. >>> from pyrsistent import s, m, v >>> thaw(s(1, 2)) set([1, 2]) >>> thaw(v(1, m(a=3))) [1, {'a': 3}] >>> thaw((1, v())) (1, [])
[ "Recursively", "convert", "pyrsistent", "containers", "into", "simple", "Python", "containers", "." ]
c84dab0daaa44973cbe83830d14888827b307632
https://github.com/tobgu/pyrsistent/blob/c84dab0daaa44973cbe83830d14888827b307632/pyrsistent/_helpers.py#L42-L67
227,612
tobgu/pyrsistent
pyrsistent/_plist.py
plist
def plist(iterable=(), reverse=False): """ Creates a new persistent list containing all elements of iterable. Optional parameter reverse specifies if the elements should be inserted in reverse order or not. >>> plist([1, 2, 3]) plist([1, 2, 3]) >>> plist([1, 2, 3], reverse=True) plist([3, 2, 1]) """ if not reverse: iterable = list(iterable) iterable.reverse() return reduce(lambda pl, elem: pl.cons(elem), iterable, _EMPTY_PLIST)
python
def plist(iterable=(), reverse=False): if not reverse: iterable = list(iterable) iterable.reverse() return reduce(lambda pl, elem: pl.cons(elem), iterable, _EMPTY_PLIST)
[ "def", "plist", "(", "iterable", "=", "(", ")", ",", "reverse", "=", "False", ")", ":", "if", "not", "reverse", ":", "iterable", "=", "list", "(", "iterable", ")", "iterable", ".", "reverse", "(", ")", "return", "reduce", "(", "lambda", "pl", ",", ...
Creates a new persistent list containing all elements of iterable. Optional parameter reverse specifies if the elements should be inserted in reverse order or not. >>> plist([1, 2, 3]) plist([1, 2, 3]) >>> plist([1, 2, 3], reverse=True) plist([3, 2, 1])
[ "Creates", "a", "new", "persistent", "list", "containing", "all", "elements", "of", "iterable", ".", "Optional", "parameter", "reverse", "specifies", "if", "the", "elements", "should", "be", "inserted", "in", "reverse", "order", "or", "not", "." ]
c84dab0daaa44973cbe83830d14888827b307632
https://github.com/tobgu/pyrsistent/blob/c84dab0daaa44973cbe83830d14888827b307632/pyrsistent/_plist.py#L288-L303
227,613
tobgu/pyrsistent
pyrsistent/_pset.py
PSet.update
def update(self, iterable): """ Return a new PSet with elements in iterable added >>> s1 = s(1, 2) >>> s1.update([3, 4, 4]) pset([1, 2, 3, 4]) """ e = self.evolver() for element in iterable: e.add(element) return e.persistent()
python
def update(self, iterable): e = self.evolver() for element in iterable: e.add(element) return e.persistent()
[ "def", "update", "(", "self", ",", "iterable", ")", ":", "e", "=", "self", ".", "evolver", "(", ")", "for", "element", "in", "iterable", ":", "e", ".", "add", "(", "element", ")", "return", "e", ".", "persistent", "(", ")" ]
Return a new PSet with elements in iterable added >>> s1 = s(1, 2) >>> s1.update([3, 4, 4]) pset([1, 2, 3, 4])
[ "Return", "a", "new", "PSet", "with", "elements", "in", "iterable", "added" ]
c84dab0daaa44973cbe83830d14888827b307632
https://github.com/tobgu/pyrsistent/blob/c84dab0daaa44973cbe83830d14888827b307632/pyrsistent/_pset.py#L76-L88
227,614
tobgu/pyrsistent
pyrsistent/_pset.py
PSet.remove
def remove(self, element): """ Return a new PSet with element removed. Raises KeyError if element is not present. >>> s1 = s(1, 2) >>> s1.remove(2) pset([1]) """ if element in self._map: return self.evolver().remove(element).persistent() raise KeyError("Element '%s' not present in PSet" % element)
python
def remove(self, element): if element in self._map: return self.evolver().remove(element).persistent() raise KeyError("Element '%s' not present in PSet" % element)
[ "def", "remove", "(", "self", ",", "element", ")", ":", "if", "element", "in", "self", ".", "_map", ":", "return", "self", ".", "evolver", "(", ")", ".", "remove", "(", "element", ")", ".", "persistent", "(", ")", "raise", "KeyError", "(", "\"Element...
Return a new PSet with element removed. Raises KeyError if element is not present. >>> s1 = s(1, 2) >>> s1.remove(2) pset([1])
[ "Return", "a", "new", "PSet", "with", "element", "removed", ".", "Raises", "KeyError", "if", "element", "is", "not", "present", "." ]
c84dab0daaa44973cbe83830d14888827b307632
https://github.com/tobgu/pyrsistent/blob/c84dab0daaa44973cbe83830d14888827b307632/pyrsistent/_pset.py#L90-L101
227,615
tobgu/pyrsistent
pyrsistent/_pset.py
PSet.discard
def discard(self, element): """ Return a new PSet with element removed. Returns itself if element is not present. """ if element in self._map: return self.evolver().remove(element).persistent() return self
python
def discard(self, element): if element in self._map: return self.evolver().remove(element).persistent() return self
[ "def", "discard", "(", "self", ",", "element", ")", ":", "if", "element", "in", "self", ".", "_map", ":", "return", "self", ".", "evolver", "(", ")", ".", "remove", "(", "element", ")", ".", "persistent", "(", ")", "return", "self" ]
Return a new PSet with element removed. Returns itself if element is not present.
[ "Return", "a", "new", "PSet", "with", "element", "removed", ".", "Returns", "itself", "if", "element", "is", "not", "present", "." ]
c84dab0daaa44973cbe83830d14888827b307632
https://github.com/tobgu/pyrsistent/blob/c84dab0daaa44973cbe83830d14888827b307632/pyrsistent/_pset.py#L103-L110
227,616
tobgu/pyrsistent
pyrsistent/_checked_types.py
maybe_parse_user_type
def maybe_parse_user_type(t): """Try to coerce a user-supplied type directive into a list of types. This function should be used in all places where a user specifies a type, for consistency. The policy for what defines valid user input should be clear from the implementation. """ is_type = isinstance(t, type) is_preserved = isinstance(t, type) and issubclass(t, _preserved_iterable_types) is_string = isinstance(t, string_types) is_iterable = isinstance(t, Iterable) if is_preserved: return [t] elif is_string: return [t] elif is_type and not is_iterable: return [t] elif is_iterable: # Recur to validate contained types as well. ts = t return tuple(e for t in ts for e in maybe_parse_user_type(t)) else: # If this raises because `t` cannot be formatted, so be it. raise TypeError( 'Type specifications must be types or strings. Input: {}'.format(t) )
python
def maybe_parse_user_type(t): is_type = isinstance(t, type) is_preserved = isinstance(t, type) and issubclass(t, _preserved_iterable_types) is_string = isinstance(t, string_types) is_iterable = isinstance(t, Iterable) if is_preserved: return [t] elif is_string: return [t] elif is_type and not is_iterable: return [t] elif is_iterable: # Recur to validate contained types as well. ts = t return tuple(e for t in ts for e in maybe_parse_user_type(t)) else: # If this raises because `t` cannot be formatted, so be it. raise TypeError( 'Type specifications must be types or strings. Input: {}'.format(t) )
[ "def", "maybe_parse_user_type", "(", "t", ")", ":", "is_type", "=", "isinstance", "(", "t", ",", "type", ")", "is_preserved", "=", "isinstance", "(", "t", ",", "type", ")", "and", "issubclass", "(", "t", ",", "_preserved_iterable_types", ")", "is_string", ...
Try to coerce a user-supplied type directive into a list of types. This function should be used in all places where a user specifies a type, for consistency. The policy for what defines valid user input should be clear from the implementation.
[ "Try", "to", "coerce", "a", "user", "-", "supplied", "type", "directive", "into", "a", "list", "of", "types", "." ]
c84dab0daaa44973cbe83830d14888827b307632
https://github.com/tobgu/pyrsistent/blob/c84dab0daaa44973cbe83830d14888827b307632/pyrsistent/_checked_types.py#L62-L89
227,617
tobgu/pyrsistent
pyrsistent/_checked_types.py
_all_dicts
def _all_dicts(bases, seen=None): """ Yield each class in ``bases`` and each of their base classes. """ if seen is None: seen = set() for cls in bases: if cls in seen: continue seen.add(cls) yield cls.__dict__ for b in _all_dicts(cls.__bases__, seen): yield b
python
def _all_dicts(bases, seen=None): if seen is None: seen = set() for cls in bases: if cls in seen: continue seen.add(cls) yield cls.__dict__ for b in _all_dicts(cls.__bases__, seen): yield b
[ "def", "_all_dicts", "(", "bases", ",", "seen", "=", "None", ")", ":", "if", "seen", "is", "None", ":", "seen", "=", "set", "(", ")", "for", "cls", "in", "bases", ":", "if", "cls", "in", "seen", ":", "continue", "seen", ".", "add", "(", "cls", ...
Yield each class in ``bases`` and each of their base classes.
[ "Yield", "each", "class", "in", "bases", "and", "each", "of", "their", "base", "classes", "." ]
c84dab0daaa44973cbe83830d14888827b307632
https://github.com/tobgu/pyrsistent/blob/c84dab0daaa44973cbe83830d14888827b307632/pyrsistent/_checked_types.py#L132-L144
227,618
tobgu/pyrsistent
pyrsistent/_pvector.py
PythonPVector.tolist
def tolist(self): """ The fastest way to convert the vector into a python list. """ the_list = [] self._fill_list(self._root, self._shift, the_list) the_list.extend(self._tail) return the_list
python
def tolist(self): the_list = [] self._fill_list(self._root, self._shift, the_list) the_list.extend(self._tail) return the_list
[ "def", "tolist", "(", "self", ")", ":", "the_list", "=", "[", "]", "self", ".", "_fill_list", "(", "self", ".", "_root", ",", "self", ".", "_shift", ",", "the_list", ")", "the_list", ".", "extend", "(", "self", ".", "_tail", ")", "return", "the_list"...
The fastest way to convert the vector into a python list.
[ "The", "fastest", "way", "to", "convert", "the", "vector", "into", "a", "python", "list", "." ]
c84dab0daaa44973cbe83830d14888827b307632
https://github.com/tobgu/pyrsistent/blob/c84dab0daaa44973cbe83830d14888827b307632/pyrsistent/_pvector.py#L115-L122
227,619
tobgu/pyrsistent
pyrsistent/_pvector.py
PythonPVector._push_tail
def _push_tail(self, level, parent, tail_node): """ if parent is leaf, insert node, else does it map to an existing child? -> node_to_insert = push node one more level else alloc new path return node_to_insert placed in copy of parent """ ret = list(parent) if level == SHIFT: ret.append(tail_node) return ret sub_index = ((self._count - 1) >> level) & BIT_MASK # >>> if len(parent) > sub_index: ret[sub_index] = self._push_tail(level - SHIFT, parent[sub_index], tail_node) return ret ret.append(self._new_path(level - SHIFT, tail_node)) return ret
python
def _push_tail(self, level, parent, tail_node): ret = list(parent) if level == SHIFT: ret.append(tail_node) return ret sub_index = ((self._count - 1) >> level) & BIT_MASK # >>> if len(parent) > sub_index: ret[sub_index] = self._push_tail(level - SHIFT, parent[sub_index], tail_node) return ret ret.append(self._new_path(level - SHIFT, tail_node)) return ret
[ "def", "_push_tail", "(", "self", ",", "level", ",", "parent", ",", "tail_node", ")", ":", "ret", "=", "list", "(", "parent", ")", "if", "level", "==", "SHIFT", ":", "ret", ".", "append", "(", "tail_node", ")", "return", "ret", "sub_index", "=", "(",...
if parent is leaf, insert node, else does it map to an existing child? -> node_to_insert = push node one more level else alloc new path return node_to_insert placed in copy of parent
[ "if", "parent", "is", "leaf", "insert", "node", "else", "does", "it", "map", "to", "an", "existing", "child?", "-", ">", "node_to_insert", "=", "push", "node", "one", "more", "level", "else", "alloc", "new", "path" ]
c84dab0daaa44973cbe83830d14888827b307632
https://github.com/tobgu/pyrsistent/blob/c84dab0daaa44973cbe83830d14888827b307632/pyrsistent/_pvector.py#L375-L396
227,620
tobgu/pyrsistent
pyrsistent/_field_common.py
_make_seq_field_type
def _make_seq_field_type(checked_class, item_type): """Create a subclass of the given checked class with the given item type.""" type_ = _seq_field_types.get((checked_class, item_type)) if type_ is not None: return type_ class TheType(checked_class): __type__ = item_type def __reduce__(self): return (_restore_seq_field_pickle, (checked_class, item_type, list(self))) suffix = SEQ_FIELD_TYPE_SUFFIXES[checked_class] TheType.__name__ = _types_to_names(TheType._checked_types) + suffix _seq_field_types[checked_class, item_type] = TheType return TheType
python
def _make_seq_field_type(checked_class, item_type): type_ = _seq_field_types.get((checked_class, item_type)) if type_ is not None: return type_ class TheType(checked_class): __type__ = item_type def __reduce__(self): return (_restore_seq_field_pickle, (checked_class, item_type, list(self))) suffix = SEQ_FIELD_TYPE_SUFFIXES[checked_class] TheType.__name__ = _types_to_names(TheType._checked_types) + suffix _seq_field_types[checked_class, item_type] = TheType return TheType
[ "def", "_make_seq_field_type", "(", "checked_class", ",", "item_type", ")", ":", "type_", "=", "_seq_field_types", ".", "get", "(", "(", "checked_class", ",", "item_type", ")", ")", "if", "type_", "is", "not", "None", ":", "return", "type_", "class", "TheTyp...
Create a subclass of the given checked class with the given item type.
[ "Create", "a", "subclass", "of", "the", "given", "checked", "class", "with", "the", "given", "item", "type", "." ]
c84dab0daaa44973cbe83830d14888827b307632
https://github.com/tobgu/pyrsistent/blob/c84dab0daaa44973cbe83830d14888827b307632/pyrsistent/_field_common.py#L168-L184
227,621
tobgu/pyrsistent
pyrsistent/_field_common.py
_sequence_field
def _sequence_field(checked_class, item_type, optional, initial): """ Create checked field for either ``PSet`` or ``PVector``. :param checked_class: ``CheckedPSet`` or ``CheckedPVector``. :param item_type: The required type for the items in the set. :param optional: If true, ``None`` can be used as a value for this field. :param initial: Initial value to pass to factory. :return: A ``field`` containing a checked class. """ TheType = _make_seq_field_type(checked_class, item_type) if optional: def factory(argument): if argument is None: return None else: return TheType.create(argument) else: factory = TheType.create return field(type=optional_type(TheType) if optional else TheType, factory=factory, mandatory=True, initial=factory(initial))
python
def _sequence_field(checked_class, item_type, optional, initial): TheType = _make_seq_field_type(checked_class, item_type) if optional: def factory(argument): if argument is None: return None else: return TheType.create(argument) else: factory = TheType.create return field(type=optional_type(TheType) if optional else TheType, factory=factory, mandatory=True, initial=factory(initial))
[ "def", "_sequence_field", "(", "checked_class", ",", "item_type", ",", "optional", ",", "initial", ")", ":", "TheType", "=", "_make_seq_field_type", "(", "checked_class", ",", "item_type", ")", "if", "optional", ":", "def", "factory", "(", "argument", ")", ":"...
Create checked field for either ``PSet`` or ``PVector``. :param checked_class: ``CheckedPSet`` or ``CheckedPVector``. :param item_type: The required type for the items in the set. :param optional: If true, ``None`` can be used as a value for this field. :param initial: Initial value to pass to factory. :return: A ``field`` containing a checked class.
[ "Create", "checked", "field", "for", "either", "PSet", "or", "PVector", "." ]
c84dab0daaa44973cbe83830d14888827b307632
https://github.com/tobgu/pyrsistent/blob/c84dab0daaa44973cbe83830d14888827b307632/pyrsistent/_field_common.py#L186-L211
227,622
tobgu/pyrsistent
pyrsistent/_field_common.py
pset_field
def pset_field(item_type, optional=False, initial=()): """ Create checked ``PSet`` field. :param item_type: The required type for the items in the set. :param optional: If true, ``None`` can be used as a value for this field. :param initial: Initial value to pass to factory if no value is given for the field. :return: A ``field`` containing a ``CheckedPSet`` of the given type. """ return _sequence_field(CheckedPSet, item_type, optional, initial)
python
def pset_field(item_type, optional=False, initial=()): return _sequence_field(CheckedPSet, item_type, optional, initial)
[ "def", "pset_field", "(", "item_type", ",", "optional", "=", "False", ",", "initial", "=", "(", ")", ")", ":", "return", "_sequence_field", "(", "CheckedPSet", ",", "item_type", ",", "optional", ",", "initial", ")" ]
Create checked ``PSet`` field. :param item_type: The required type for the items in the set. :param optional: If true, ``None`` can be used as a value for this field. :param initial: Initial value to pass to factory if no value is given for the field. :return: A ``field`` containing a ``CheckedPSet`` of the given type.
[ "Create", "checked", "PSet", "field", "." ]
c84dab0daaa44973cbe83830d14888827b307632
https://github.com/tobgu/pyrsistent/blob/c84dab0daaa44973cbe83830d14888827b307632/pyrsistent/_field_common.py#L214-L227
227,623
tobgu/pyrsistent
pyrsistent/_field_common.py
pvector_field
def pvector_field(item_type, optional=False, initial=()): """ Create checked ``PVector`` field. :param item_type: The required type for the items in the vector. :param optional: If true, ``None`` can be used as a value for this field. :param initial: Initial value to pass to factory if no value is given for the field. :return: A ``field`` containing a ``CheckedPVector`` of the given type. """ return _sequence_field(CheckedPVector, item_type, optional, initial)
python
def pvector_field(item_type, optional=False, initial=()): return _sequence_field(CheckedPVector, item_type, optional, initial)
[ "def", "pvector_field", "(", "item_type", ",", "optional", "=", "False", ",", "initial", "=", "(", ")", ")", ":", "return", "_sequence_field", "(", "CheckedPVector", ",", "item_type", ",", "optional", ",", "initial", ")" ]
Create checked ``PVector`` field. :param item_type: The required type for the items in the vector. :param optional: If true, ``None`` can be used as a value for this field. :param initial: Initial value to pass to factory if no value is given for the field. :return: A ``field`` containing a ``CheckedPVector`` of the given type.
[ "Create", "checked", "PVector", "field", "." ]
c84dab0daaa44973cbe83830d14888827b307632
https://github.com/tobgu/pyrsistent/blob/c84dab0daaa44973cbe83830d14888827b307632/pyrsistent/_field_common.py#L230-L243
227,624
tobgu/pyrsistent
pyrsistent/_field_common.py
_restore_pmap_field_pickle
def _restore_pmap_field_pickle(key_type, value_type, data): """Unpickling function for auto-generated PMap field types.""" type_ = _pmap_field_types[key_type, value_type] return _restore_pickle(type_, data)
python
def _restore_pmap_field_pickle(key_type, value_type, data): type_ = _pmap_field_types[key_type, value_type] return _restore_pickle(type_, data)
[ "def", "_restore_pmap_field_pickle", "(", "key_type", ",", "value_type", ",", "data", ")", ":", "type_", "=", "_pmap_field_types", "[", "key_type", ",", "value_type", "]", "return", "_restore_pickle", "(", "type_", ",", "data", ")" ]
Unpickling function for auto-generated PMap field types.
[ "Unpickling", "function", "for", "auto", "-", "generated", "PMap", "field", "types", "." ]
c84dab0daaa44973cbe83830d14888827b307632
https://github.com/tobgu/pyrsistent/blob/c84dab0daaa44973cbe83830d14888827b307632/pyrsistent/_field_common.py#L252-L255
227,625
tobgu/pyrsistent
pyrsistent/_field_common.py
_make_pmap_field_type
def _make_pmap_field_type(key_type, value_type): """Create a subclass of CheckedPMap with the given key and value types.""" type_ = _pmap_field_types.get((key_type, value_type)) if type_ is not None: return type_ class TheMap(CheckedPMap): __key_type__ = key_type __value_type__ = value_type def __reduce__(self): return (_restore_pmap_field_pickle, (self.__key_type__, self.__value_type__, dict(self))) TheMap.__name__ = "{0}To{1}PMap".format( _types_to_names(TheMap._checked_key_types), _types_to_names(TheMap._checked_value_types)) _pmap_field_types[key_type, value_type] = TheMap return TheMap
python
def _make_pmap_field_type(key_type, value_type): type_ = _pmap_field_types.get((key_type, value_type)) if type_ is not None: return type_ class TheMap(CheckedPMap): __key_type__ = key_type __value_type__ = value_type def __reduce__(self): return (_restore_pmap_field_pickle, (self.__key_type__, self.__value_type__, dict(self))) TheMap.__name__ = "{0}To{1}PMap".format( _types_to_names(TheMap._checked_key_types), _types_to_names(TheMap._checked_value_types)) _pmap_field_types[key_type, value_type] = TheMap return TheMap
[ "def", "_make_pmap_field_type", "(", "key_type", ",", "value_type", ")", ":", "type_", "=", "_pmap_field_types", ".", "get", "(", "(", "key_type", ",", "value_type", ")", ")", "if", "type_", "is", "not", "None", ":", "return", "type_", "class", "TheMap", "...
Create a subclass of CheckedPMap with the given key and value types.
[ "Create", "a", "subclass", "of", "CheckedPMap", "with", "the", "given", "key", "and", "value", "types", "." ]
c84dab0daaa44973cbe83830d14888827b307632
https://github.com/tobgu/pyrsistent/blob/c84dab0daaa44973cbe83830d14888827b307632/pyrsistent/_field_common.py#L257-L275
227,626
tobgu/pyrsistent
pyrsistent/_field_common.py
pmap_field
def pmap_field(key_type, value_type, optional=False, invariant=PFIELD_NO_INVARIANT): """ Create a checked ``PMap`` field. :param key: The required type for the keys of the map. :param value: The required type for the values of the map. :param optional: If true, ``None`` can be used as a value for this field. :param invariant: Pass-through to ``field``. :return: A ``field`` containing a ``CheckedPMap``. """ TheMap = _make_pmap_field_type(key_type, value_type) if optional: def factory(argument): if argument is None: return None else: return TheMap.create(argument) else: factory = TheMap.create return field(mandatory=True, initial=TheMap(), type=optional_type(TheMap) if optional else TheMap, factory=factory, invariant=invariant)
python
def pmap_field(key_type, value_type, optional=False, invariant=PFIELD_NO_INVARIANT): TheMap = _make_pmap_field_type(key_type, value_type) if optional: def factory(argument): if argument is None: return None else: return TheMap.create(argument) else: factory = TheMap.create return field(mandatory=True, initial=TheMap(), type=optional_type(TheMap) if optional else TheMap, factory=factory, invariant=invariant)
[ "def", "pmap_field", "(", "key_type", ",", "value_type", ",", "optional", "=", "False", ",", "invariant", "=", "PFIELD_NO_INVARIANT", ")", ":", "TheMap", "=", "_make_pmap_field_type", "(", "key_type", ",", "value_type", ")", "if", "optional", ":", "def", "fact...
Create a checked ``PMap`` field. :param key: The required type for the keys of the map. :param value: The required type for the values of the map. :param optional: If true, ``None`` can be used as a value for this field. :param invariant: Pass-through to ``field``. :return: A ``field`` containing a ``CheckedPMap``.
[ "Create", "a", "checked", "PMap", "field", "." ]
c84dab0daaa44973cbe83830d14888827b307632
https://github.com/tobgu/pyrsistent/blob/c84dab0daaa44973cbe83830d14888827b307632/pyrsistent/_field_common.py#L278-L303
227,627
SavinaRoja/PyUserInput
pykeyboard/base.py
PyKeyboardMeta.tap_key
def tap_key(self, character='', n=1, interval=0): """Press and release a given character key n times.""" for i in range(n): self.press_key(character) self.release_key(character) time.sleep(interval)
python
def tap_key(self, character='', n=1, interval=0): for i in range(n): self.press_key(character) self.release_key(character) time.sleep(interval)
[ "def", "tap_key", "(", "self", ",", "character", "=", "''", ",", "n", "=", "1", ",", "interval", "=", "0", ")", ":", "for", "i", "in", "range", "(", "n", ")", ":", "self", ".", "press_key", "(", "character", ")", "self", ".", "release_key", "(", ...
Press and release a given character key n times.
[ "Press", "and", "release", "a", "given", "character", "key", "n", "times", "." ]
153c1d39b1a41b467b235fd182392d6dcbf07947
https://github.com/SavinaRoja/PyUserInput/blob/153c1d39b1a41b467b235fd182392d6dcbf07947/pykeyboard/base.py#L40-L45
227,628
SavinaRoja/PyUserInput
pykeyboard/base.py
PyKeyboardMeta.type_string
def type_string(self, char_string, interval=0): """ A convenience method for typing longer strings of characters. Generates as few Shift events as possible.""" shift = False for char in char_string: if self.is_char_shifted(char): if not shift: # Only press Shift as needed time.sleep(interval) self.press_key(self.shift_key) shift = True #In order to avoid tap_key pressing Shift, we need to pass the #unshifted form of the character if char in '<>?:"{}|~!@#$%^&*()_+': ch_index = '<>?:"{}|~!@#$%^&*()_+'.index(char) unshifted_char = ",./;'[]\\`1234567890-="[ch_index] else: unshifted_char = char.lower() time.sleep(interval) self.tap_key(unshifted_char) else: # Unshifted already if shift and char != ' ': # Only release Shift as needed self.release_key(self.shift_key) shift = False time.sleep(interval) self.tap_key(char) if shift: # Turn off Shift if it's still ON self.release_key(self.shift_key)
python
def type_string(self, char_string, interval=0): shift = False for char in char_string: if self.is_char_shifted(char): if not shift: # Only press Shift as needed time.sleep(interval) self.press_key(self.shift_key) shift = True #In order to avoid tap_key pressing Shift, we need to pass the #unshifted form of the character if char in '<>?:"{}|~!@#$%^&*()_+': ch_index = '<>?:"{}|~!@#$%^&*()_+'.index(char) unshifted_char = ",./;'[]\\`1234567890-="[ch_index] else: unshifted_char = char.lower() time.sleep(interval) self.tap_key(unshifted_char) else: # Unshifted already if shift and char != ' ': # Only release Shift as needed self.release_key(self.shift_key) shift = False time.sleep(interval) self.tap_key(char) if shift: # Turn off Shift if it's still ON self.release_key(self.shift_key)
[ "def", "type_string", "(", "self", ",", "char_string", ",", "interval", "=", "0", ")", ":", "shift", "=", "False", "for", "char", "in", "char_string", ":", "if", "self", ".", "is_char_shifted", "(", "char", ")", ":", "if", "not", "shift", ":", "# Only ...
A convenience method for typing longer strings of characters. Generates as few Shift events as possible.
[ "A", "convenience", "method", "for", "typing", "longer", "strings", "of", "characters", ".", "Generates", "as", "few", "Shift", "events", "as", "possible", "." ]
153c1d39b1a41b467b235fd182392d6dcbf07947
https://github.com/SavinaRoja/PyUserInput/blob/153c1d39b1a41b467b235fd182392d6dcbf07947/pykeyboard/base.py#L54-L82
227,629
SavinaRoja/PyUserInput
reference_materials/xlib-keysyms-to-python.py
keysym_definitions
def keysym_definitions(): """Yields all keysym definitions parsed as tuples. """ for keysym_line in keysym_lines(): # As described in the input text, the format of a line is: # 0x20 U0020 . # space /* optional comment */ keysym_number, codepoint, status, _, name_part = [ p.strip() for p in keysym_line.split(None, 4)] name = name_part.split()[0] yield (int(keysym_number, 16), codepoint[1:], status, name)
python
def keysym_definitions(): for keysym_line in keysym_lines(): # As described in the input text, the format of a line is: # 0x20 U0020 . # space /* optional comment */ keysym_number, codepoint, status, _, name_part = [ p.strip() for p in keysym_line.split(None, 4)] name = name_part.split()[0] yield (int(keysym_number, 16), codepoint[1:], status, name)
[ "def", "keysym_definitions", "(", ")", ":", "for", "keysym_line", "in", "keysym_lines", "(", ")", ":", "# As described in the input text, the format of a line is:", "# 0x20 U0020 . # space /* optional comment */", "keysym_number", ",", "codepoint", ",", "status", ",", "_", ...
Yields all keysym definitions parsed as tuples.
[ "Yields", "all", "keysym", "definitions", "parsed", "as", "tuples", "." ]
153c1d39b1a41b467b235fd182392d6dcbf07947
https://github.com/SavinaRoja/PyUserInput/blob/153c1d39b1a41b467b235fd182392d6dcbf07947/reference_materials/xlib-keysyms-to-python.py#L61-L71
227,630
SavinaRoja/PyUserInput
pykeyboard/windows.py
PyKeyboard.release_key
def release_key(self, character=''): """ Release a given character key. """ try: shifted = self.is_char_shifted(character) except AttributeError: win32api.keybd_event(character, 0, KEYEVENTF_KEYUP, 0) else: if shifted: win32api.keybd_event(self.shift_key, 0, KEYEVENTF_KEYUP, 0) char_vk = win32api.VkKeyScan(character) win32api.keybd_event(char_vk, 0, KEYEVENTF_KEYUP, 0)
python
def release_key(self, character=''): try: shifted = self.is_char_shifted(character) except AttributeError: win32api.keybd_event(character, 0, KEYEVENTF_KEYUP, 0) else: if shifted: win32api.keybd_event(self.shift_key, 0, KEYEVENTF_KEYUP, 0) char_vk = win32api.VkKeyScan(character) win32api.keybd_event(char_vk, 0, KEYEVENTF_KEYUP, 0)
[ "def", "release_key", "(", "self", ",", "character", "=", "''", ")", ":", "try", ":", "shifted", "=", "self", ".", "is_char_shifted", "(", "character", ")", "except", "AttributeError", ":", "win32api", ".", "keybd_event", "(", "character", ",", "0", ",", ...
Release a given character key.
[ "Release", "a", "given", "character", "key", "." ]
153c1d39b1a41b467b235fd182392d6dcbf07947
https://github.com/SavinaRoja/PyUserInput/blob/153c1d39b1a41b467b235fd182392d6dcbf07947/pykeyboard/windows.py#L58-L70
227,631
SavinaRoja/PyUserInput
pykeyboard/x11.py
PyKeyboard._handle_key
def _handle_key(self, character, event): """Handles either a key press or release, depending on ``event``. :param character: The key to handle. See :meth:`press_key` and :meth:`release_key` for information about this parameter. :param event: The *Xlib* event. This should be either :attr:`Xlib.X.KeyPress` or :attr:`Xlib.X.KeyRelease` """ try: # Detect uppercase or shifted character shifted = self.is_char_shifted(character) except AttributeError: # Handle the case of integer keycode argument with display_manager(self.display) as d: fake_input(d, event, character) else: with display_manager(self.display) as d: if shifted: fake_input(d, event, self.shift_key) keycode = self.lookup_character_keycode(character) fake_input(d, event, keycode)
python
def _handle_key(self, character, event): try: # Detect uppercase or shifted character shifted = self.is_char_shifted(character) except AttributeError: # Handle the case of integer keycode argument with display_manager(self.display) as d: fake_input(d, event, character) else: with display_manager(self.display) as d: if shifted: fake_input(d, event, self.shift_key) keycode = self.lookup_character_keycode(character) fake_input(d, event, keycode)
[ "def", "_handle_key", "(", "self", ",", "character", ",", "event", ")", ":", "try", ":", "# Detect uppercase or shifted character", "shifted", "=", "self", ".", "is_char_shifted", "(", "character", ")", "except", "AttributeError", ":", "# Handle the case of integer ke...
Handles either a key press or release, depending on ``event``. :param character: The key to handle. See :meth:`press_key` and :meth:`release_key` for information about this parameter. :param event: The *Xlib* event. This should be either :attr:`Xlib.X.KeyPress` or :attr:`Xlib.X.KeyRelease`
[ "Handles", "either", "a", "key", "press", "or", "release", "depending", "on", "event", "." ]
153c1d39b1a41b467b235fd182392d6dcbf07947
https://github.com/SavinaRoja/PyUserInput/blob/153c1d39b1a41b467b235fd182392d6dcbf07947/pykeyboard/x11.py#L45-L66
227,632
SavinaRoja/PyUserInput
pykeyboard/x11.py
PyKeyboardEvent.stop
def stop(self): """Stop listening for keyboard input events.""" self.state = False with display_manager(self.display) as d: d.record_disable_context(self.ctx) d.ungrab_keyboard(X.CurrentTime) with display_manager(self.display2): d.record_disable_context(self.ctx) d.ungrab_keyboard(X.CurrentTime)
python
def stop(self): self.state = False with display_manager(self.display) as d: d.record_disable_context(self.ctx) d.ungrab_keyboard(X.CurrentTime) with display_manager(self.display2): d.record_disable_context(self.ctx) d.ungrab_keyboard(X.CurrentTime)
[ "def", "stop", "(", "self", ")", ":", "self", ".", "state", "=", "False", "with", "display_manager", "(", "self", ".", "display", ")", "as", "d", ":", "d", ".", "record_disable_context", "(", "self", ".", "ctx", ")", "d", ".", "ungrab_keyboard", "(", ...
Stop listening for keyboard input events.
[ "Stop", "listening", "for", "keyboard", "input", "events", "." ]
153c1d39b1a41b467b235fd182392d6dcbf07947
https://github.com/SavinaRoja/PyUserInput/blob/153c1d39b1a41b467b235fd182392d6dcbf07947/pykeyboard/x11.py#L252-L260
227,633
SavinaRoja/PyUserInput
pykeyboard/x11.py
PyKeyboardEvent.lookup_char_from_keycode
def lookup_char_from_keycode(self, keycode): """ This will conduct a lookup of the character or string associated with a given keycode. """ #TODO: Logic should be strictly adapted from X11's src/KeyBind.c #Right now the logic is based off of #http://tronche.com/gui/x/xlib/input/keyboard-encoding.html #Which I suspect is not the whole story and may likely cause bugs keysym_index = 0 #TODO: Display's Keysyms per keycode count? Do I need this? #If the Num_Lock is on, and the keycode corresponds to the keypad if self.modifiers['Num_Lock'] and keycode in self.keypad_keycodes: if self.modifiers['Shift'] or self.modifiers['Shift_Lock']: keysym_index = 0 else: keysym_index = 1 elif not self.modifiers['Shift'] and self.modifiers['Caps_Lock']: #Use the first keysym if uppercase or uncased #Use the uppercase keysym if the first is lowercase (second) keysym_index = 0 keysym = self.display.keycode_to_keysym(keycode, keysym_index) #TODO: Support Unicode, Greek, and special latin characters if keysym & 0x7f == keysym and chr(keysym) in 'abcdefghijklmnopqrstuvwxyz': keysym_index = 1 elif self.modifiers['Shift'] and self.modifiers['Caps_Lock']: keysym_index = 1 keysym = self.display.keycode_to_keysym(keycode, keysym_index) #TODO: Support Unicode, Greek, and special latin characters if keysym & 0x7f == keysym and chr(keysym) in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ': keysym_index = 0 elif self.modifiers['Shift'] or self.modifiers['Shift_Lock']: keysym_index = 1 if self.modifiers['Mode_switch']: keysym_index += 2 #Finally! Get the keysym keysym = self.display.keycode_to_keysym(keycode, keysym_index) #If the character is ascii printable, return that character if keysym & 0x7f == keysym and self.ascii_printable(keysym): return chr(keysym) #If the character was not printable, look for its name try: char = self.keysym_to_string[keysym] except KeyError: print('Unable to determine character.') print('Keycode: {0} KeySym {1}'.format(keycode, keysym)) return None else: return char
python
def lookup_char_from_keycode(self, keycode): #TODO: Logic should be strictly adapted from X11's src/KeyBind.c #Right now the logic is based off of #http://tronche.com/gui/x/xlib/input/keyboard-encoding.html #Which I suspect is not the whole story and may likely cause bugs keysym_index = 0 #TODO: Display's Keysyms per keycode count? Do I need this? #If the Num_Lock is on, and the keycode corresponds to the keypad if self.modifiers['Num_Lock'] and keycode in self.keypad_keycodes: if self.modifiers['Shift'] or self.modifiers['Shift_Lock']: keysym_index = 0 else: keysym_index = 1 elif not self.modifiers['Shift'] and self.modifiers['Caps_Lock']: #Use the first keysym if uppercase or uncased #Use the uppercase keysym if the first is lowercase (second) keysym_index = 0 keysym = self.display.keycode_to_keysym(keycode, keysym_index) #TODO: Support Unicode, Greek, and special latin characters if keysym & 0x7f == keysym and chr(keysym) in 'abcdefghijklmnopqrstuvwxyz': keysym_index = 1 elif self.modifiers['Shift'] and self.modifiers['Caps_Lock']: keysym_index = 1 keysym = self.display.keycode_to_keysym(keycode, keysym_index) #TODO: Support Unicode, Greek, and special latin characters if keysym & 0x7f == keysym and chr(keysym) in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ': keysym_index = 0 elif self.modifiers['Shift'] or self.modifiers['Shift_Lock']: keysym_index = 1 if self.modifiers['Mode_switch']: keysym_index += 2 #Finally! Get the keysym keysym = self.display.keycode_to_keysym(keycode, keysym_index) #If the character is ascii printable, return that character if keysym & 0x7f == keysym and self.ascii_printable(keysym): return chr(keysym) #If the character was not printable, look for its name try: char = self.keysym_to_string[keysym] except KeyError: print('Unable to determine character.') print('Keycode: {0} KeySym {1}'.format(keycode, keysym)) return None else: return char
[ "def", "lookup_char_from_keycode", "(", "self", ",", "keycode", ")", ":", "#TODO: Logic should be strictly adapted from X11's src/KeyBind.c", "#Right now the logic is based off of", "#http://tronche.com/gui/x/xlib/input/keyboard-encoding.html", "#Which I suspect is not the whole story and may ...
This will conduct a lookup of the character or string associated with a given keycode.
[ "This", "will", "conduct", "a", "lookup", "of", "the", "character", "or", "string", "associated", "with", "a", "given", "keycode", "." ]
153c1d39b1a41b467b235fd182392d6dcbf07947
https://github.com/SavinaRoja/PyUserInput/blob/153c1d39b1a41b467b235fd182392d6dcbf07947/pykeyboard/x11.py#L291-L348
227,634
SavinaRoja/PyUserInput
pykeyboard/x11.py
PyKeyboardEvent.get_translation_dicts
def get_translation_dicts(self): """ Returns dictionaries for the translation of keysyms to strings and from strings to keysyms. """ keysym_to_string_dict = {} string_to_keysym_dict = {} #XK loads latin1 and miscellany on its own; load latin2-4 and greek Xlib.XK.load_keysym_group('latin2') Xlib.XK.load_keysym_group('latin3') Xlib.XK.load_keysym_group('latin4') Xlib.XK.load_keysym_group('greek') #Make a standard dict and the inverted dict for string, keysym in Xlib.XK.__dict__.items(): if string.startswith('XK_'): string_to_keysym_dict[string[3:]] = keysym keysym_to_string_dict[keysym] = string[3:] return keysym_to_string_dict, string_to_keysym_dict
python
def get_translation_dicts(self): keysym_to_string_dict = {} string_to_keysym_dict = {} #XK loads latin1 and miscellany on its own; load latin2-4 and greek Xlib.XK.load_keysym_group('latin2') Xlib.XK.load_keysym_group('latin3') Xlib.XK.load_keysym_group('latin4') Xlib.XK.load_keysym_group('greek') #Make a standard dict and the inverted dict for string, keysym in Xlib.XK.__dict__.items(): if string.startswith('XK_'): string_to_keysym_dict[string[3:]] = keysym keysym_to_string_dict[keysym] = string[3:] return keysym_to_string_dict, string_to_keysym_dict
[ "def", "get_translation_dicts", "(", "self", ")", ":", "keysym_to_string_dict", "=", "{", "}", "string_to_keysym_dict", "=", "{", "}", "#XK loads latin1 and miscellany on its own; load latin2-4 and greek", "Xlib", ".", "XK", ".", "load_keysym_group", "(", "'latin2'", ")",...
Returns dictionaries for the translation of keysyms to strings and from strings to keysyms.
[ "Returns", "dictionaries", "for", "the", "translation", "of", "keysyms", "to", "strings", "and", "from", "strings", "to", "keysyms", "." ]
153c1d39b1a41b467b235fd182392d6dcbf07947
https://github.com/SavinaRoja/PyUserInput/blob/153c1d39b1a41b467b235fd182392d6dcbf07947/pykeyboard/x11.py#L463-L481
227,635
SavinaRoja/PyUserInput
pykeyboard/x11.py
PyKeyboardEvent.ascii_printable
def ascii_printable(self, keysym): """ If the keysym corresponds to a non-printable ascii character this will return False. If it is printable, then True will be returned. ascii 11 (vertical tab) and ascii 12 are printable, chr(11) and chr(12) will return '\x0b' and '\x0c' respectively. """ if 0 <= keysym < 9: return False elif 13 < keysym < 32: return False elif keysym > 126: return False else: return True
python
def ascii_printable(self, keysym): if 0 <= keysym < 9: return False elif 13 < keysym < 32: return False elif keysym > 126: return False else: return True
[ "def", "ascii_printable", "(", "self", ",", "keysym", ")", ":", "if", "0", "<=", "keysym", "<", "9", ":", "return", "False", "elif", "13", "<", "keysym", "<", "32", ":", "return", "False", "elif", "keysym", ">", "126", ":", "return", "False", "else",...
If the keysym corresponds to a non-printable ascii character this will return False. If it is printable, then True will be returned. ascii 11 (vertical tab) and ascii 12 are printable, chr(11) and chr(12) will return '\x0b' and '\x0c' respectively.
[ "If", "the", "keysym", "corresponds", "to", "a", "non", "-", "printable", "ascii", "character", "this", "will", "return", "False", ".", "If", "it", "is", "printable", "then", "True", "will", "be", "returned", "." ]
153c1d39b1a41b467b235fd182392d6dcbf07947
https://github.com/SavinaRoja/PyUserInput/blob/153c1d39b1a41b467b235fd182392d6dcbf07947/pykeyboard/x11.py#L483-L498
227,636
SavinaRoja/PyUserInput
examples/clickonacci.py
Clickonacci.click
def click(self, x, y, button, press): '''Print Fibonacci numbers when the left click is pressed.''' if button == 1: if press: print(self.fibo.next()) else: # Exit if any other mouse button used self.stop()
python
def click(self, x, y, button, press): '''Print Fibonacci numbers when the left click is pressed.''' if button == 1: if press: print(self.fibo.next()) else: # Exit if any other mouse button used self.stop()
[ "def", "click", "(", "self", ",", "x", ",", "y", ",", "button", ",", "press", ")", ":", "if", "button", "==", "1", ":", "if", "press", ":", "print", "(", "self", ".", "fibo", ".", "next", "(", ")", ")", "else", ":", "# Exit if any other mouse butto...
Print Fibonacci numbers when the left click is pressed.
[ "Print", "Fibonacci", "numbers", "when", "the", "left", "click", "is", "pressed", "." ]
153c1d39b1a41b467b235fd182392d6dcbf07947
https://github.com/SavinaRoja/PyUserInput/blob/153c1d39b1a41b467b235fd182392d6dcbf07947/examples/clickonacci.py#L17-L23
227,637
SavinaRoja/PyUserInput
pymouse/base.py
PyMouseMeta.click
def click(self, x, y, button=1, n=1): """ Click a mouse button n times on a given x, y. Button is defined as 1 = left, 2 = right, 3 = middle. """ for i in range(n): self.press(x, y, button) self.release(x, y, button)
python
def click(self, x, y, button=1, n=1): for i in range(n): self.press(x, y, button) self.release(x, y, button)
[ "def", "click", "(", "self", ",", "x", ",", "y", ",", "button", "=", "1", ",", "n", "=", "1", ")", ":", "for", "i", "in", "range", "(", "n", ")", ":", "self", ".", "press", "(", "x", ",", "y", ",", "button", ")", "self", ".", "release", "...
Click a mouse button n times on a given x, y. Button is defined as 1 = left, 2 = right, 3 = middle.
[ "Click", "a", "mouse", "button", "n", "times", "on", "a", "given", "x", "y", ".", "Button", "is", "defined", "as", "1", "=", "left", "2", "=", "right", "3", "=", "middle", "." ]
153c1d39b1a41b467b235fd182392d6dcbf07947
https://github.com/SavinaRoja/PyUserInput/blob/153c1d39b1a41b467b235fd182392d6dcbf07947/pymouse/base.py#L49-L57
227,638
c0fec0de/anytree
anytree/node/nodemixin.py
NodeMixin.siblings
def siblings(self): """ Tuple of nodes with the same parent. >>> from anytree import Node >>> udo = Node("Udo") >>> marc = Node("Marc", parent=udo) >>> lian = Node("Lian", parent=marc) >>> loui = Node("Loui", parent=marc) >>> lazy = Node("Lazy", parent=marc) >>> udo.siblings () >>> marc.siblings () >>> lian.siblings (Node('/Udo/Marc/Loui'), Node('/Udo/Marc/Lazy')) >>> loui.siblings (Node('/Udo/Marc/Lian'), Node('/Udo/Marc/Lazy')) """ parent = self.parent if parent is None: return tuple() else: return tuple([node for node in parent.children if node != self])
python
def siblings(self): parent = self.parent if parent is None: return tuple() else: return tuple([node for node in parent.children if node != self])
[ "def", "siblings", "(", "self", ")", ":", "parent", "=", "self", ".", "parent", "if", "parent", "is", "None", ":", "return", "tuple", "(", ")", "else", ":", "return", "tuple", "(", "[", "node", "for", "node", "in", "parent", ".", "children", "if", ...
Tuple of nodes with the same parent. >>> from anytree import Node >>> udo = Node("Udo") >>> marc = Node("Marc", parent=udo) >>> lian = Node("Lian", parent=marc) >>> loui = Node("Loui", parent=marc) >>> lazy = Node("Lazy", parent=marc) >>> udo.siblings () >>> marc.siblings () >>> lian.siblings (Node('/Udo/Marc/Loui'), Node('/Udo/Marc/Lazy')) >>> loui.siblings (Node('/Udo/Marc/Lian'), Node('/Udo/Marc/Lazy'))
[ "Tuple", "of", "nodes", "with", "the", "same", "parent", "." ]
775477e206a75e697983e70dae6372b5a7e42dcf
https://github.com/c0fec0de/anytree/blob/775477e206a75e697983e70dae6372b5a7e42dcf/anytree/node/nodemixin.py#L384-L407
227,639
c0fec0de/anytree
anytree/node/nodemixin.py
NodeMixin.height
def height(self): """ Number of edges on the longest path to a leaf `Node`. >>> from anytree import Node >>> udo = Node("Udo") >>> marc = Node("Marc", parent=udo) >>> lian = Node("Lian", parent=marc) >>> udo.height 2 >>> marc.height 1 >>> lian.height 0 """ if self.__children_: return max([child.height for child in self.__children_]) + 1 else: return 0
python
def height(self): if self.__children_: return max([child.height for child in self.__children_]) + 1 else: return 0
[ "def", "height", "(", "self", ")", ":", "if", "self", ".", "__children_", ":", "return", "max", "(", "[", "child", ".", "height", "for", "child", "in", "self", ".", "__children_", "]", ")", "+", "1", "else", ":", "return", "0" ]
Number of edges on the longest path to a leaf `Node`. >>> from anytree import Node >>> udo = Node("Udo") >>> marc = Node("Marc", parent=udo) >>> lian = Node("Lian", parent=marc) >>> udo.height 2 >>> marc.height 1 >>> lian.height 0
[ "Number", "of", "edges", "on", "the", "longest", "path", "to", "a", "leaf", "Node", "." ]
775477e206a75e697983e70dae6372b5a7e42dcf
https://github.com/c0fec0de/anytree/blob/775477e206a75e697983e70dae6372b5a7e42dcf/anytree/node/nodemixin.py#L464-L482
227,640
c0fec0de/anytree
anytree/importer/jsonimporter.py
JsonImporter.import_
def import_(self, data): """Read JSON from `data`.""" return self.__import(json.loads(data, **self.kwargs))
python
def import_(self, data): return self.__import(json.loads(data, **self.kwargs))
[ "def", "import_", "(", "self", ",", "data", ")", ":", "return", "self", ".", "__import", "(", "json", ".", "loads", "(", "data", ",", "*", "*", "self", ".", "kwargs", ")", ")" ]
Read JSON from `data`.
[ "Read", "JSON", "from", "data", "." ]
775477e206a75e697983e70dae6372b5a7e42dcf
https://github.com/c0fec0de/anytree/blob/775477e206a75e697983e70dae6372b5a7e42dcf/anytree/importer/jsonimporter.py#L60-L62
227,641
c0fec0de/anytree
anytree/importer/jsonimporter.py
JsonImporter.read
def read(self, filehandle): """Read JSON from `filehandle`.""" return self.__import(json.load(filehandle, **self.kwargs))
python
def read(self, filehandle): return self.__import(json.load(filehandle, **self.kwargs))
[ "def", "read", "(", "self", ",", "filehandle", ")", ":", "return", "self", ".", "__import", "(", "json", ".", "load", "(", "filehandle", ",", "*", "*", "self", ".", "kwargs", ")", ")" ]
Read JSON from `filehandle`.
[ "Read", "JSON", "from", "filehandle", "." ]
775477e206a75e697983e70dae6372b5a7e42dcf
https://github.com/c0fec0de/anytree/blob/775477e206a75e697983e70dae6372b5a7e42dcf/anytree/importer/jsonimporter.py#L64-L66
227,642
c0fec0de/anytree
anytree/walker.py
Walker.walk
def walk(self, start, end): """ Walk from `start` node to `end` node. Returns: (upwards, common, downwards): `upwards` is a list of nodes to go upward to. `common` top node. `downwards` is a list of nodes to go downward to. Raises: WalkError: on no common root node. >>> from anytree import Node, RenderTree, AsciiStyle >>> f = Node("f") >>> b = Node("b", parent=f) >>> a = Node("a", parent=b) >>> d = Node("d", parent=b) >>> c = Node("c", parent=d) >>> e = Node("e", parent=d) >>> g = Node("g", parent=f) >>> i = Node("i", parent=g) >>> h = Node("h", parent=i) >>> print(RenderTree(f, style=AsciiStyle())) Node('/f') |-- Node('/f/b') | |-- Node('/f/b/a') | +-- Node('/f/b/d') | |-- Node('/f/b/d/c') | +-- Node('/f/b/d/e') +-- Node('/f/g') +-- Node('/f/g/i') +-- Node('/f/g/i/h') Create a walker: >>> w = Walker() This class is made for walking: >>> w.walk(f, f) ((), Node('/f'), ()) >>> w.walk(f, b) ((), Node('/f'), (Node('/f/b'),)) >>> w.walk(b, f) ((Node('/f/b'),), Node('/f'), ()) >>> w.walk(h, e) ((Node('/f/g/i/h'), Node('/f/g/i'), Node('/f/g')), Node('/f'), (Node('/f/b'), Node('/f/b/d'), Node('/f/b/d/e'))) >>> w.walk(d, e) ((), Node('/f/b/d'), (Node('/f/b/d/e'),)) For a proper walking the nodes need to be part of the same tree: >>> w.walk(Node("a"), Node("b")) Traceback (most recent call last): ... anytree.walker.WalkError: Node('/a') and Node('/b') are not part of the same tree. """ s = start.path e = end.path if start.root != end.root: msg = "%r and %r are not part of the same tree." % (start, end) raise WalkError(msg) # common c = Walker.__calc_common(s, e) assert c[0] is start.root len_c = len(c) # up if start is c[-1]: up = tuple() else: up = tuple(reversed(s[len_c:])) # down if end is c[-1]: down = tuple() else: down = e[len_c:] return up, c[-1], down
python
def walk(self, start, end): s = start.path e = end.path if start.root != end.root: msg = "%r and %r are not part of the same tree." % (start, end) raise WalkError(msg) # common c = Walker.__calc_common(s, e) assert c[0] is start.root len_c = len(c) # up if start is c[-1]: up = tuple() else: up = tuple(reversed(s[len_c:])) # down if end is c[-1]: down = tuple() else: down = e[len_c:] return up, c[-1], down
[ "def", "walk", "(", "self", ",", "start", ",", "end", ")", ":", "s", "=", "start", ".", "path", "e", "=", "end", ".", "path", "if", "start", ".", "root", "!=", "end", ".", "root", ":", "msg", "=", "\"%r and %r are not part of the same tree.\"", "%", ...
Walk from `start` node to `end` node. Returns: (upwards, common, downwards): `upwards` is a list of nodes to go upward to. `common` top node. `downwards` is a list of nodes to go downward to. Raises: WalkError: on no common root node. >>> from anytree import Node, RenderTree, AsciiStyle >>> f = Node("f") >>> b = Node("b", parent=f) >>> a = Node("a", parent=b) >>> d = Node("d", parent=b) >>> c = Node("c", parent=d) >>> e = Node("e", parent=d) >>> g = Node("g", parent=f) >>> i = Node("i", parent=g) >>> h = Node("h", parent=i) >>> print(RenderTree(f, style=AsciiStyle())) Node('/f') |-- Node('/f/b') | |-- Node('/f/b/a') | +-- Node('/f/b/d') | |-- Node('/f/b/d/c') | +-- Node('/f/b/d/e') +-- Node('/f/g') +-- Node('/f/g/i') +-- Node('/f/g/i/h') Create a walker: >>> w = Walker() This class is made for walking: >>> w.walk(f, f) ((), Node('/f'), ()) >>> w.walk(f, b) ((), Node('/f'), (Node('/f/b'),)) >>> w.walk(b, f) ((Node('/f/b'),), Node('/f'), ()) >>> w.walk(h, e) ((Node('/f/g/i/h'), Node('/f/g/i'), Node('/f/g')), Node('/f'), (Node('/f/b'), Node('/f/b/d'), Node('/f/b/d/e'))) >>> w.walk(d, e) ((), Node('/f/b/d'), (Node('/f/b/d/e'),)) For a proper walking the nodes need to be part of the same tree: >>> w.walk(Node("a"), Node("b")) Traceback (most recent call last): ... anytree.walker.WalkError: Node('/a') and Node('/b') are not part of the same tree.
[ "Walk", "from", "start", "node", "to", "end", "node", "." ]
775477e206a75e697983e70dae6372b5a7e42dcf
https://github.com/c0fec0de/anytree/blob/775477e206a75e697983e70dae6372b5a7e42dcf/anytree/walker.py#L10-L85
227,643
c0fec0de/anytree
anytree/exporter/jsonexporter.py
JsonExporter.export
def export(self, node): """Return JSON for tree starting at `node`.""" dictexporter = self.dictexporter or DictExporter() data = dictexporter.export(node) return json.dumps(data, **self.kwargs)
python
def export(self, node): dictexporter = self.dictexporter or DictExporter() data = dictexporter.export(node) return json.dumps(data, **self.kwargs)
[ "def", "export", "(", "self", ",", "node", ")", ":", "dictexporter", "=", "self", ".", "dictexporter", "or", "DictExporter", "(", ")", "data", "=", "dictexporter", ".", "export", "(", "node", ")", "return", "json", ".", "dumps", "(", "data", ",", "*", ...
Return JSON for tree starting at `node`.
[ "Return", "JSON", "for", "tree", "starting", "at", "node", "." ]
775477e206a75e697983e70dae6372b5a7e42dcf
https://github.com/c0fec0de/anytree/blob/775477e206a75e697983e70dae6372b5a7e42dcf/anytree/exporter/jsonexporter.py#L54-L58
227,644
c0fec0de/anytree
anytree/exporter/jsonexporter.py
JsonExporter.write
def write(self, node, filehandle): """Write JSON to `filehandle` starting at `node`.""" dictexporter = self.dictexporter or DictExporter() data = dictexporter.export(node) return json.dump(data, filehandle, **self.kwargs)
python
def write(self, node, filehandle): dictexporter = self.dictexporter or DictExporter() data = dictexporter.export(node) return json.dump(data, filehandle, **self.kwargs)
[ "def", "write", "(", "self", ",", "node", ",", "filehandle", ")", ":", "dictexporter", "=", "self", ".", "dictexporter", "or", "DictExporter", "(", ")", "data", "=", "dictexporter", ".", "export", "(", "node", ")", "return", "json", ".", "dump", "(", "...
Write JSON to `filehandle` starting at `node`.
[ "Write", "JSON", "to", "filehandle", "starting", "at", "node", "." ]
775477e206a75e697983e70dae6372b5a7e42dcf
https://github.com/c0fec0de/anytree/blob/775477e206a75e697983e70dae6372b5a7e42dcf/anytree/exporter/jsonexporter.py#L60-L64
227,645
c0fec0de/anytree
anytree/search.py
findall
def findall(node, filter_=None, stop=None, maxlevel=None, mincount=None, maxcount=None): """ Search nodes matching `filter_` but stop at `maxlevel` or `stop`. Return tuple with matching nodes. Args: node: top node, start searching. Keyword Args: filter_: function called with every `node` as argument, `node` is returned if `True`. stop: stop iteration at `node` if `stop` function returns `True` for `node`. maxlevel (int): maximum decending in the node hierarchy. mincount (int): minimum number of nodes. maxcount (int): maximum number of nodes. Example tree: >>> from anytree import Node, RenderTree, AsciiStyle >>> f = Node("f") >>> b = Node("b", parent=f) >>> a = Node("a", parent=b) >>> d = Node("d", parent=b) >>> c = Node("c", parent=d) >>> e = Node("e", parent=d) >>> g = Node("g", parent=f) >>> i = Node("i", parent=g) >>> h = Node("h", parent=i) >>> print(RenderTree(f, style=AsciiStyle()).by_attr()) f |-- b | |-- a | +-- d | |-- c | +-- e +-- g +-- i +-- h >>> findall(f, filter_=lambda node: node.name in ("a", "b")) (Node('/f/b'), Node('/f/b/a')) >>> findall(f, filter_=lambda node: d in node.path) (Node('/f/b/d'), Node('/f/b/d/c'), Node('/f/b/d/e')) The number of matches can be limited: >>> findall(f, filter_=lambda node: d in node.path, mincount=4) # doctest: +ELLIPSIS Traceback (most recent call last): ... anytree.search.CountError: Expecting at least 4 elements, but found 3. ... Node('/f/b/d/e')) >>> findall(f, filter_=lambda node: d in node.path, maxcount=2) # doctest: +ELLIPSIS Traceback (most recent call last): ... anytree.search.CountError: Expecting 2 elements at maximum, but found 3. ... Node('/f/b/d/e')) """ return _findall(node, filter_=filter_, stop=stop, maxlevel=maxlevel, mincount=mincount, maxcount=maxcount)
python
def findall(node, filter_=None, stop=None, maxlevel=None, mincount=None, maxcount=None): return _findall(node, filter_=filter_, stop=stop, maxlevel=maxlevel, mincount=mincount, maxcount=maxcount)
[ "def", "findall", "(", "node", ",", "filter_", "=", "None", ",", "stop", "=", "None", ",", "maxlevel", "=", "None", ",", "mincount", "=", "None", ",", "maxcount", "=", "None", ")", ":", "return", "_findall", "(", "node", ",", "filter_", "=", "filter_...
Search nodes matching `filter_` but stop at `maxlevel` or `stop`. Return tuple with matching nodes. Args: node: top node, start searching. Keyword Args: filter_: function called with every `node` as argument, `node` is returned if `True`. stop: stop iteration at `node` if `stop` function returns `True` for `node`. maxlevel (int): maximum decending in the node hierarchy. mincount (int): minimum number of nodes. maxcount (int): maximum number of nodes. Example tree: >>> from anytree import Node, RenderTree, AsciiStyle >>> f = Node("f") >>> b = Node("b", parent=f) >>> a = Node("a", parent=b) >>> d = Node("d", parent=b) >>> c = Node("c", parent=d) >>> e = Node("e", parent=d) >>> g = Node("g", parent=f) >>> i = Node("i", parent=g) >>> h = Node("h", parent=i) >>> print(RenderTree(f, style=AsciiStyle()).by_attr()) f |-- b | |-- a | +-- d | |-- c | +-- e +-- g +-- i +-- h >>> findall(f, filter_=lambda node: node.name in ("a", "b")) (Node('/f/b'), Node('/f/b/a')) >>> findall(f, filter_=lambda node: d in node.path) (Node('/f/b/d'), Node('/f/b/d/c'), Node('/f/b/d/e')) The number of matches can be limited: >>> findall(f, filter_=lambda node: d in node.path, mincount=4) # doctest: +ELLIPSIS Traceback (most recent call last): ... anytree.search.CountError: Expecting at least 4 elements, but found 3. ... Node('/f/b/d/e')) >>> findall(f, filter_=lambda node: d in node.path, maxcount=2) # doctest: +ELLIPSIS Traceback (most recent call last): ... anytree.search.CountError: Expecting 2 elements at maximum, but found 3. ... Node('/f/b/d/e'))
[ "Search", "nodes", "matching", "filter_", "but", "stop", "at", "maxlevel", "or", "stop", "." ]
775477e206a75e697983e70dae6372b5a7e42dcf
https://github.com/c0fec0de/anytree/blob/775477e206a75e697983e70dae6372b5a7e42dcf/anytree/search.py#L6-L62
227,646
c0fec0de/anytree
anytree/search.py
findall_by_attr
def findall_by_attr(node, value, name="name", maxlevel=None, mincount=None, maxcount=None): """ Search nodes with attribute `name` having `value` but stop at `maxlevel`. Return tuple with matching nodes. Args: node: top node, start searching. value: value which need to match Keyword Args: name (str): attribute name need to match maxlevel (int): maximum decending in the node hierarchy. mincount (int): minimum number of nodes. maxcount (int): maximum number of nodes. Example tree: >>> from anytree import Node, RenderTree, AsciiStyle >>> f = Node("f") >>> b = Node("b", parent=f) >>> a = Node("a", parent=b) >>> d = Node("d", parent=b) >>> c = Node("c", parent=d) >>> e = Node("e", parent=d) >>> g = Node("g", parent=f) >>> i = Node("i", parent=g) >>> h = Node("h", parent=i) >>> print(RenderTree(f, style=AsciiStyle()).by_attr()) f |-- b | |-- a | +-- d | |-- c | +-- e +-- g +-- i +-- h >>> findall_by_attr(f, "d") (Node('/f/b/d'),) """ return _findall(node, filter_=lambda n: _filter_by_name(n, name, value), maxlevel=maxlevel, mincount=mincount, maxcount=maxcount)
python
def findall_by_attr(node, value, name="name", maxlevel=None, mincount=None, maxcount=None): return _findall(node, filter_=lambda n: _filter_by_name(n, name, value), maxlevel=maxlevel, mincount=mincount, maxcount=maxcount)
[ "def", "findall_by_attr", "(", "node", ",", "value", ",", "name", "=", "\"name\"", ",", "maxlevel", "=", "None", ",", "mincount", "=", "None", ",", "maxcount", "=", "None", ")", ":", "return", "_findall", "(", "node", ",", "filter_", "=", "lambda", "n"...
Search nodes with attribute `name` having `value` but stop at `maxlevel`. Return tuple with matching nodes. Args: node: top node, start searching. value: value which need to match Keyword Args: name (str): attribute name need to match maxlevel (int): maximum decending in the node hierarchy. mincount (int): minimum number of nodes. maxcount (int): maximum number of nodes. Example tree: >>> from anytree import Node, RenderTree, AsciiStyle >>> f = Node("f") >>> b = Node("b", parent=f) >>> a = Node("a", parent=b) >>> d = Node("d", parent=b) >>> c = Node("c", parent=d) >>> e = Node("e", parent=d) >>> g = Node("g", parent=f) >>> i = Node("i", parent=g) >>> h = Node("h", parent=i) >>> print(RenderTree(f, style=AsciiStyle()).by_attr()) f |-- b | |-- a | +-- d | |-- c | +-- e +-- g +-- i +-- h >>> findall_by_attr(f, "d") (Node('/f/b/d'),)
[ "Search", "nodes", "with", "attribute", "name", "having", "value", "but", "stop", "at", "maxlevel", "." ]
775477e206a75e697983e70dae6372b5a7e42dcf
https://github.com/c0fec0de/anytree/blob/775477e206a75e697983e70dae6372b5a7e42dcf/anytree/search.py#L65-L108
227,647
c0fec0de/anytree
anytree/resolver.py
Resolver.get
def get(self, node, path): """ Return instance at `path`. An example module tree: >>> from anytree import Node >>> top = Node("top", parent=None) >>> sub0 = Node("sub0", parent=top) >>> sub0sub0 = Node("sub0sub0", parent=sub0) >>> sub0sub1 = Node("sub0sub1", parent=sub0) >>> sub1 = Node("sub1", parent=top) A resolver using the `name` attribute: >>> r = Resolver('name') Relative paths: >>> r.get(top, "sub0/sub0sub0") Node('/top/sub0/sub0sub0') >>> r.get(sub1, "..") Node('/top') >>> r.get(sub1, "../sub0/sub0sub1") Node('/top/sub0/sub0sub1') >>> r.get(sub1, ".") Node('/top/sub1') >>> r.get(sub1, "") Node('/top/sub1') >>> r.get(top, "sub2") Traceback (most recent call last): ... anytree.resolver.ChildResolverError: Node('/top') has no child sub2. Children are: 'sub0', 'sub1'. Absolute paths: >>> r.get(sub0sub0, "/top") Node('/top') >>> r.get(sub0sub0, "/top/sub0") Node('/top/sub0') >>> r.get(sub0sub0, "/") Traceback (most recent call last): ... anytree.resolver.ResolverError: root node missing. root is '/top'. >>> r.get(sub0sub0, "/bar") Traceback (most recent call last): ... anytree.resolver.ResolverError: unknown root node '/bar'. root is '/top'. """ node, parts = self.__start(node, path) for part in parts: if part == "..": node = node.parent elif part in ("", "."): pass else: node = self.__get(node, part) return node
python
def get(self, node, path): node, parts = self.__start(node, path) for part in parts: if part == "..": node = node.parent elif part in ("", "."): pass else: node = self.__get(node, part) return node
[ "def", "get", "(", "self", ",", "node", ",", "path", ")", ":", "node", ",", "parts", "=", "self", ".", "__start", "(", "node", ",", "path", ")", "for", "part", "in", "parts", ":", "if", "part", "==", "\"..\"", ":", "node", "=", "node", ".", "pa...
Return instance at `path`. An example module tree: >>> from anytree import Node >>> top = Node("top", parent=None) >>> sub0 = Node("sub0", parent=top) >>> sub0sub0 = Node("sub0sub0", parent=sub0) >>> sub0sub1 = Node("sub0sub1", parent=sub0) >>> sub1 = Node("sub1", parent=top) A resolver using the `name` attribute: >>> r = Resolver('name') Relative paths: >>> r.get(top, "sub0/sub0sub0") Node('/top/sub0/sub0sub0') >>> r.get(sub1, "..") Node('/top') >>> r.get(sub1, "../sub0/sub0sub1") Node('/top/sub0/sub0sub1') >>> r.get(sub1, ".") Node('/top/sub1') >>> r.get(sub1, "") Node('/top/sub1') >>> r.get(top, "sub2") Traceback (most recent call last): ... anytree.resolver.ChildResolverError: Node('/top') has no child sub2. Children are: 'sub0', 'sub1'. Absolute paths: >>> r.get(sub0sub0, "/top") Node('/top') >>> r.get(sub0sub0, "/top/sub0") Node('/top/sub0') >>> r.get(sub0sub0, "/") Traceback (most recent call last): ... anytree.resolver.ResolverError: root node missing. root is '/top'. >>> r.get(sub0sub0, "/bar") Traceback (most recent call last): ... anytree.resolver.ResolverError: unknown root node '/bar'. root is '/top'.
[ "Return", "instance", "at", "path", "." ]
775477e206a75e697983e70dae6372b5a7e42dcf
https://github.com/c0fec0de/anytree/blob/775477e206a75e697983e70dae6372b5a7e42dcf/anytree/resolver.py#L20-L77
227,648
c0fec0de/anytree
anytree/resolver.py
Resolver.glob
def glob(self, node, path): """ Return instances at `path` supporting wildcards. Behaves identical to :any:`get`, but accepts wildcards and returns a list of found nodes. * `*` matches any characters, except '/'. * `?` matches a single character, except '/'. An example module tree: >>> from anytree import Node >>> top = Node("top", parent=None) >>> sub0 = Node("sub0", parent=top) >>> sub0sub0 = Node("sub0", parent=sub0) >>> sub0sub1 = Node("sub1", parent=sub0) >>> sub1 = Node("sub1", parent=top) >>> sub1sub0 = Node("sub0", parent=sub1) A resolver using the `name` attribute: >>> r = Resolver('name') Relative paths: >>> r.glob(top, "sub0/sub?") [Node('/top/sub0/sub0'), Node('/top/sub0/sub1')] >>> r.glob(sub1, ".././*") [Node('/top/sub0'), Node('/top/sub1')] >>> r.glob(top, "*/*") [Node('/top/sub0/sub0'), Node('/top/sub0/sub1'), Node('/top/sub1/sub0')] >>> r.glob(top, "*/sub0") [Node('/top/sub0/sub0'), Node('/top/sub1/sub0')] >>> r.glob(top, "sub1/sub1") Traceback (most recent call last): ... anytree.resolver.ChildResolverError: Node('/top/sub1') has no child sub1. Children are: 'sub0'. Non-matching wildcards are no error: >>> r.glob(top, "bar*") [] >>> r.glob(top, "sub2") Traceback (most recent call last): ... anytree.resolver.ChildResolverError: Node('/top') has no child sub2. Children are: 'sub0', 'sub1'. Absolute paths: >>> r.glob(sub0sub0, "/top/*") [Node('/top/sub0'), Node('/top/sub1')] >>> r.glob(sub0sub0, "/") Traceback (most recent call last): ... anytree.resolver.ResolverError: root node missing. root is '/top'. >>> r.glob(sub0sub0, "/bar") Traceback (most recent call last): ... anytree.resolver.ResolverError: unknown root node '/bar'. root is '/top'. """ node, parts = self.__start(node, path) return self.__glob(node, parts)
python
def glob(self, node, path): node, parts = self.__start(node, path) return self.__glob(node, parts)
[ "def", "glob", "(", "self", ",", "node", ",", "path", ")", ":", "node", ",", "parts", "=", "self", ".", "__start", "(", "node", ",", "path", ")", "return", "self", ".", "__glob", "(", "node", ",", "parts", ")" ]
Return instances at `path` supporting wildcards. Behaves identical to :any:`get`, but accepts wildcards and returns a list of found nodes. * `*` matches any characters, except '/'. * `?` matches a single character, except '/'. An example module tree: >>> from anytree import Node >>> top = Node("top", parent=None) >>> sub0 = Node("sub0", parent=top) >>> sub0sub0 = Node("sub0", parent=sub0) >>> sub0sub1 = Node("sub1", parent=sub0) >>> sub1 = Node("sub1", parent=top) >>> sub1sub0 = Node("sub0", parent=sub1) A resolver using the `name` attribute: >>> r = Resolver('name') Relative paths: >>> r.glob(top, "sub0/sub?") [Node('/top/sub0/sub0'), Node('/top/sub0/sub1')] >>> r.glob(sub1, ".././*") [Node('/top/sub0'), Node('/top/sub1')] >>> r.glob(top, "*/*") [Node('/top/sub0/sub0'), Node('/top/sub0/sub1'), Node('/top/sub1/sub0')] >>> r.glob(top, "*/sub0") [Node('/top/sub0/sub0'), Node('/top/sub1/sub0')] >>> r.glob(top, "sub1/sub1") Traceback (most recent call last): ... anytree.resolver.ChildResolverError: Node('/top/sub1') has no child sub1. Children are: 'sub0'. Non-matching wildcards are no error: >>> r.glob(top, "bar*") [] >>> r.glob(top, "sub2") Traceback (most recent call last): ... anytree.resolver.ChildResolverError: Node('/top') has no child sub2. Children are: 'sub0', 'sub1'. Absolute paths: >>> r.glob(sub0sub0, "/top/*") [Node('/top/sub0'), Node('/top/sub1')] >>> r.glob(sub0sub0, "/") Traceback (most recent call last): ... anytree.resolver.ResolverError: root node missing. root is '/top'. >>> r.glob(sub0sub0, "/bar") Traceback (most recent call last): ... anytree.resolver.ResolverError: unknown root node '/bar'. root is '/top'.
[ "Return", "instances", "at", "path", "supporting", "wildcards", "." ]
775477e206a75e697983e70dae6372b5a7e42dcf
https://github.com/c0fec0de/anytree/blob/775477e206a75e697983e70dae6372b5a7e42dcf/anytree/resolver.py#L85-L147
227,649
c0fec0de/anytree
anytree/exporter/dotexporter.py
DotExporter.to_dotfile
def to_dotfile(self, filename): """ Write graph to `filename`. >>> from anytree import Node >>> root = Node("root") >>> s0 = Node("sub0", parent=root) >>> s0b = Node("sub0B", parent=s0) >>> s0a = Node("sub0A", parent=s0) >>> s1 = Node("sub1", parent=root) >>> s1a = Node("sub1A", parent=s1) >>> s1b = Node("sub1B", parent=s1) >>> s1c = Node("sub1C", parent=s1) >>> s1ca = Node("sub1Ca", parent=s1c) >>> from anytree.exporter import DotExporter >>> DotExporter(root).to_dotfile("tree.dot") The generated file should be handed over to the `dot` tool from the http://www.graphviz.org/ package:: $ dot tree.dot -T png -o tree.png """ with codecs.open(filename, "w", "utf-8") as file: for line in self: file.write("%s\n" % line)
python
def to_dotfile(self, filename): with codecs.open(filename, "w", "utf-8") as file: for line in self: file.write("%s\n" % line)
[ "def", "to_dotfile", "(", "self", ",", "filename", ")", ":", "with", "codecs", ".", "open", "(", "filename", ",", "\"w\"", ",", "\"utf-8\"", ")", "as", "file", ":", "for", "line", "in", "self", ":", "file", ".", "write", "(", "\"%s\\n\"", "%", "line"...
Write graph to `filename`. >>> from anytree import Node >>> root = Node("root") >>> s0 = Node("sub0", parent=root) >>> s0b = Node("sub0B", parent=s0) >>> s0a = Node("sub0A", parent=s0) >>> s1 = Node("sub1", parent=root) >>> s1a = Node("sub1A", parent=s1) >>> s1b = Node("sub1B", parent=s1) >>> s1c = Node("sub1C", parent=s1) >>> s1ca = Node("sub1Ca", parent=s1c) >>> from anytree.exporter import DotExporter >>> DotExporter(root).to_dotfile("tree.dot") The generated file should be handed over to the `dot` tool from the http://www.graphviz.org/ package:: $ dot tree.dot -T png -o tree.png
[ "Write", "graph", "to", "filename", "." ]
775477e206a75e697983e70dae6372b5a7e42dcf
https://github.com/c0fec0de/anytree/blob/775477e206a75e697983e70dae6372b5a7e42dcf/anytree/exporter/dotexporter.py#L190-L215
227,650
c0fec0de/anytree
anytree/exporter/dotexporter.py
DotExporter.to_picture
def to_picture(self, filename): """ Write graph to a temporary file and invoke `dot`. The output file type is automatically detected from the file suffix. *`graphviz` needs to be installed, before usage of this method.* """ fileformat = path.splitext(filename)[1][1:] with NamedTemporaryFile("wb", delete=False) as dotfile: dotfilename = dotfile.name for line in self: dotfile.write(("%s\n" % line).encode("utf-8")) dotfile.flush() cmd = ["dot", dotfilename, "-T", fileformat, "-o", filename] check_call(cmd) try: remove(dotfilename) except Exception: # pragma: no cover msg = 'Could not remove temporary file %s' % dotfilename logging.getLogger(__name__).warn(msg)
python
def to_picture(self, filename): fileformat = path.splitext(filename)[1][1:] with NamedTemporaryFile("wb", delete=False) as dotfile: dotfilename = dotfile.name for line in self: dotfile.write(("%s\n" % line).encode("utf-8")) dotfile.flush() cmd = ["dot", dotfilename, "-T", fileformat, "-o", filename] check_call(cmd) try: remove(dotfilename) except Exception: # pragma: no cover msg = 'Could not remove temporary file %s' % dotfilename logging.getLogger(__name__).warn(msg)
[ "def", "to_picture", "(", "self", ",", "filename", ")", ":", "fileformat", "=", "path", ".", "splitext", "(", "filename", ")", "[", "1", "]", "[", "1", ":", "]", "with", "NamedTemporaryFile", "(", "\"wb\"", ",", "delete", "=", "False", ")", "as", "do...
Write graph to a temporary file and invoke `dot`. The output file type is automatically detected from the file suffix. *`graphviz` needs to be installed, before usage of this method.*
[ "Write", "graph", "to", "a", "temporary", "file", "and", "invoke", "dot", "." ]
775477e206a75e697983e70dae6372b5a7e42dcf
https://github.com/c0fec0de/anytree/blob/775477e206a75e697983e70dae6372b5a7e42dcf/anytree/exporter/dotexporter.py#L217-L237
227,651
c0fec0de/anytree
anytree/util/__init__.py
commonancestors
def commonancestors(*nodes): """ Determine common ancestors of `nodes`. >>> from anytree import Node >>> udo = Node("Udo") >>> marc = Node("Marc", parent=udo) >>> lian = Node("Lian", parent=marc) >>> dan = Node("Dan", parent=udo) >>> jet = Node("Jet", parent=dan) >>> jan = Node("Jan", parent=dan) >>> joe = Node("Joe", parent=dan) >>> commonancestors(jet, joe) (Node('/Udo'), Node('/Udo/Dan')) >>> commonancestors(jet, marc) (Node('/Udo'),) >>> commonancestors(jet) (Node('/Udo'), Node('/Udo/Dan')) >>> commonancestors() () """ ancestors = [node.ancestors for node in nodes] common = [] for parentnodes in zip(*ancestors): parentnode = parentnodes[0] if all([parentnode is p for p in parentnodes[1:]]): common.append(parentnode) else: break return tuple(common)
python
def commonancestors(*nodes): ancestors = [node.ancestors for node in nodes] common = [] for parentnodes in zip(*ancestors): parentnode = parentnodes[0] if all([parentnode is p for p in parentnodes[1:]]): common.append(parentnode) else: break return tuple(common)
[ "def", "commonancestors", "(", "*", "nodes", ")", ":", "ancestors", "=", "[", "node", ".", "ancestors", "for", "node", "in", "nodes", "]", "common", "=", "[", "]", "for", "parentnodes", "in", "zip", "(", "*", "ancestors", ")", ":", "parentnode", "=", ...
Determine common ancestors of `nodes`. >>> from anytree import Node >>> udo = Node("Udo") >>> marc = Node("Marc", parent=udo) >>> lian = Node("Lian", parent=marc) >>> dan = Node("Dan", parent=udo) >>> jet = Node("Jet", parent=dan) >>> jan = Node("Jan", parent=dan) >>> joe = Node("Joe", parent=dan) >>> commonancestors(jet, joe) (Node('/Udo'), Node('/Udo/Dan')) >>> commonancestors(jet, marc) (Node('/Udo'),) >>> commonancestors(jet) (Node('/Udo'), Node('/Udo/Dan')) >>> commonancestors() ()
[ "Determine", "common", "ancestors", "of", "nodes", "." ]
775477e206a75e697983e70dae6372b5a7e42dcf
https://github.com/c0fec0de/anytree/blob/775477e206a75e697983e70dae6372b5a7e42dcf/anytree/util/__init__.py#L4-L34
227,652
c0fec0de/anytree
anytree/util/__init__.py
leftsibling
def leftsibling(node): """ Return Left Sibling of `node`. >>> from anytree import Node >>> dan = Node("Dan") >>> jet = Node("Jet", parent=dan) >>> jan = Node("Jan", parent=dan) >>> joe = Node("Joe", parent=dan) >>> leftsibling(dan) >>> leftsibling(jet) >>> leftsibling(jan) Node('/Dan/Jet') >>> leftsibling(joe) Node('/Dan/Jan') """ if node.parent: pchildren = node.parent.children idx = pchildren.index(node) if idx: return pchildren[idx - 1] else: return None else: return None
python
def leftsibling(node): if node.parent: pchildren = node.parent.children idx = pchildren.index(node) if idx: return pchildren[idx - 1] else: return None else: return None
[ "def", "leftsibling", "(", "node", ")", ":", "if", "node", ".", "parent", ":", "pchildren", "=", "node", ".", "parent", ".", "children", "idx", "=", "pchildren", ".", "index", "(", "node", ")", "if", "idx", ":", "return", "pchildren", "[", "idx", "-"...
Return Left Sibling of `node`. >>> from anytree import Node >>> dan = Node("Dan") >>> jet = Node("Jet", parent=dan) >>> jan = Node("Jan", parent=dan) >>> joe = Node("Joe", parent=dan) >>> leftsibling(dan) >>> leftsibling(jet) >>> leftsibling(jan) Node('/Dan/Jet') >>> leftsibling(joe) Node('/Dan/Jan')
[ "Return", "Left", "Sibling", "of", "node", "." ]
775477e206a75e697983e70dae6372b5a7e42dcf
https://github.com/c0fec0de/anytree/blob/775477e206a75e697983e70dae6372b5a7e42dcf/anytree/util/__init__.py#L37-L61
227,653
c0fec0de/anytree
anytree/util/__init__.py
rightsibling
def rightsibling(node): """ Return Right Sibling of `node`. >>> from anytree import Node >>> dan = Node("Dan") >>> jet = Node("Jet", parent=dan) >>> jan = Node("Jan", parent=dan) >>> joe = Node("Joe", parent=dan) >>> rightsibling(dan) >>> rightsibling(jet) Node('/Dan/Jan') >>> rightsibling(jan) Node('/Dan/Joe') >>> rightsibling(joe) """ if node.parent: pchildren = node.parent.children idx = pchildren.index(node) try: return pchildren[idx + 1] except IndexError: return None else: return None
python
def rightsibling(node): if node.parent: pchildren = node.parent.children idx = pchildren.index(node) try: return pchildren[idx + 1] except IndexError: return None else: return None
[ "def", "rightsibling", "(", "node", ")", ":", "if", "node", ".", "parent", ":", "pchildren", "=", "node", ".", "parent", ".", "children", "idx", "=", "pchildren", ".", "index", "(", "node", ")", "try", ":", "return", "pchildren", "[", "idx", "+", "1"...
Return Right Sibling of `node`. >>> from anytree import Node >>> dan = Node("Dan") >>> jet = Node("Jet", parent=dan) >>> jan = Node("Jan", parent=dan) >>> joe = Node("Joe", parent=dan) >>> rightsibling(dan) >>> rightsibling(jet) Node('/Dan/Jan') >>> rightsibling(jan) Node('/Dan/Joe') >>> rightsibling(joe)
[ "Return", "Right", "Sibling", "of", "node", "." ]
775477e206a75e697983e70dae6372b5a7e42dcf
https://github.com/c0fec0de/anytree/blob/775477e206a75e697983e70dae6372b5a7e42dcf/anytree/util/__init__.py#L64-L88
227,654
c0fec0de/anytree
anytree/exporter/dictexporter.py
DictExporter.export
def export(self, node): """Export tree starting at `node`.""" attriter = self.attriter or (lambda attr_values: attr_values) return self.__export(node, self.dictcls, attriter, self.childiter)
python
def export(self, node): attriter = self.attriter or (lambda attr_values: attr_values) return self.__export(node, self.dictcls, attriter, self.childiter)
[ "def", "export", "(", "self", ",", "node", ")", ":", "attriter", "=", "self", ".", "attriter", "or", "(", "lambda", "attr_values", ":", "attr_values", ")", "return", "self", ".", "__export", "(", "node", ",", "self", ".", "dictcls", ",", "attriter", ",...
Export tree starting at `node`.
[ "Export", "tree", "starting", "at", "node", "." ]
775477e206a75e697983e70dae6372b5a7e42dcf
https://github.com/c0fec0de/anytree/blob/775477e206a75e697983e70dae6372b5a7e42dcf/anytree/exporter/dictexporter.py#L70-L73
227,655
gwastro/pycbc
pycbc/filter/matchedfilter.py
compute_u_val_for_sky_loc_stat
def compute_u_val_for_sky_loc_stat(hplus, hcross, hphccorr, hpnorm=None, hcnorm=None, indices=None): """The max-over-sky location detection statistic maximizes over a phase, an amplitude and the ratio of F+ and Fx, encoded in a variable called u. Here we return the value of u for the given indices. """ if indices is not None: hplus = hplus[indices] hcross = hcross[indices] if hpnorm is not None: hplus = hplus * hpnorm if hcnorm is not None: hcross = hcross * hcnorm # Sanity checking in func. above should already have identified any points # which are bad, and should be used to construct indices for input here hplus_magsq = numpy.real(hplus) * numpy.real(hplus) + \ numpy.imag(hplus) * numpy.imag(hplus) hcross_magsq = numpy.real(hcross) * numpy.real(hcross) + \ numpy.imag(hcross) * numpy.imag(hcross) rho_pluscross = numpy.real(hplus) * numpy.real(hcross) + \ numpy.imag(hplus)*numpy.imag(hcross) a = hphccorr * hplus_magsq - rho_pluscross b = hplus_magsq - hcross_magsq c = rho_pluscross - hphccorr * hcross_magsq sq_root = b*b - 4*a*c sq_root = sq_root**0.5 sq_root = -sq_root # Catch the a->0 case bad_lgc = (a == 0) dbl_bad_lgc = numpy.logical_and(c == 0, b == 0) dbl_bad_lgc = numpy.logical_and(bad_lgc, dbl_bad_lgc) # Initialize u u = sq_root * 0. # In this case u is completely degenerate, so set it to 1 u[dbl_bad_lgc] = 1. # If a->0 avoid overflow by just setting to a large value u[bad_lgc & ~dbl_bad_lgc] = 1E17 # Otherwise normal statistic u[~bad_lgc] = (-b[~bad_lgc] + sq_root[~bad_lgc]) / (2*a[~bad_lgc]) snr_cplx = hplus * u + hcross coa_phase = numpy.angle(snr_cplx) return u, coa_phase
python
def compute_u_val_for_sky_loc_stat(hplus, hcross, hphccorr, hpnorm=None, hcnorm=None, indices=None): if indices is not None: hplus = hplus[indices] hcross = hcross[indices] if hpnorm is not None: hplus = hplus * hpnorm if hcnorm is not None: hcross = hcross * hcnorm # Sanity checking in func. above should already have identified any points # which are bad, and should be used to construct indices for input here hplus_magsq = numpy.real(hplus) * numpy.real(hplus) + \ numpy.imag(hplus) * numpy.imag(hplus) hcross_magsq = numpy.real(hcross) * numpy.real(hcross) + \ numpy.imag(hcross) * numpy.imag(hcross) rho_pluscross = numpy.real(hplus) * numpy.real(hcross) + \ numpy.imag(hplus)*numpy.imag(hcross) a = hphccorr * hplus_magsq - rho_pluscross b = hplus_magsq - hcross_magsq c = rho_pluscross - hphccorr * hcross_magsq sq_root = b*b - 4*a*c sq_root = sq_root**0.5 sq_root = -sq_root # Catch the a->0 case bad_lgc = (a == 0) dbl_bad_lgc = numpy.logical_and(c == 0, b == 0) dbl_bad_lgc = numpy.logical_and(bad_lgc, dbl_bad_lgc) # Initialize u u = sq_root * 0. # In this case u is completely degenerate, so set it to 1 u[dbl_bad_lgc] = 1. # If a->0 avoid overflow by just setting to a large value u[bad_lgc & ~dbl_bad_lgc] = 1E17 # Otherwise normal statistic u[~bad_lgc] = (-b[~bad_lgc] + sq_root[~bad_lgc]) / (2*a[~bad_lgc]) snr_cplx = hplus * u + hcross coa_phase = numpy.angle(snr_cplx) return u, coa_phase
[ "def", "compute_u_val_for_sky_loc_stat", "(", "hplus", ",", "hcross", ",", "hphccorr", ",", "hpnorm", "=", "None", ",", "hcnorm", "=", "None", ",", "indices", "=", "None", ")", ":", "if", "indices", "is", "not", "None", ":", "hplus", "=", "hplus", "[", ...
The max-over-sky location detection statistic maximizes over a phase, an amplitude and the ratio of F+ and Fx, encoded in a variable called u. Here we return the value of u for the given indices.
[ "The", "max", "-", "over", "-", "sky", "location", "detection", "statistic", "maximizes", "over", "a", "phase", "an", "amplitude", "and", "the", "ratio", "of", "F", "+", "and", "Fx", "encoded", "in", "a", "variable", "called", "u", ".", "Here", "we", "...
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/filter/matchedfilter.py#L610-L657
227,656
gwastro/pycbc
pycbc/filter/matchedfilter.py
make_frequency_series
def make_frequency_series(vec): """Return a frequency series of the input vector. If the input is a frequency series it is returned, else if the input vector is a real time series it is fourier transformed and returned as a frequency series. Parameters ---------- vector : TimeSeries or FrequencySeries Returns ------- Frequency Series: FrequencySeries A frequency domain version of the input vector. """ if isinstance(vec, FrequencySeries): return vec if isinstance(vec, TimeSeries): N = len(vec) n = N/2+1 delta_f = 1.0 / N / vec.delta_t vectilde = FrequencySeries(zeros(n, dtype=complex_same_precision_as(vec)), delta_f=delta_f, copy=False) fft(vec, vectilde) return vectilde else: raise TypeError("Can only convert a TimeSeries to a FrequencySeries")
python
def make_frequency_series(vec): if isinstance(vec, FrequencySeries): return vec if isinstance(vec, TimeSeries): N = len(vec) n = N/2+1 delta_f = 1.0 / N / vec.delta_t vectilde = FrequencySeries(zeros(n, dtype=complex_same_precision_as(vec)), delta_f=delta_f, copy=False) fft(vec, vectilde) return vectilde else: raise TypeError("Can only convert a TimeSeries to a FrequencySeries")
[ "def", "make_frequency_series", "(", "vec", ")", ":", "if", "isinstance", "(", "vec", ",", "FrequencySeries", ")", ":", "return", "vec", "if", "isinstance", "(", "vec", ",", "TimeSeries", ")", ":", "N", "=", "len", "(", "vec", ")", "n", "=", "N", "/"...
Return a frequency series of the input vector. If the input is a frequency series it is returned, else if the input vector is a real time series it is fourier transformed and returned as a frequency series. Parameters ---------- vector : TimeSeries or FrequencySeries Returns ------- Frequency Series: FrequencySeries A frequency domain version of the input vector.
[ "Return", "a", "frequency", "series", "of", "the", "input", "vector", "." ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/filter/matchedfilter.py#L986-L1013
227,657
gwastro/pycbc
pycbc/filter/matchedfilter.py
sigmasq_series
def sigmasq_series(htilde, psd=None, low_frequency_cutoff=None, high_frequency_cutoff=None): """Return a cumulative sigmasq frequency series. Return a frequency series containing the accumulated power in the input up to that frequency. Parameters ---------- htilde : TimeSeries or FrequencySeries The input vector psd : {None, FrequencySeries}, optional The psd used to weight the accumulated power. low_frequency_cutoff : {None, float}, optional The frequency to begin accumulating power. If None, start at the beginning of the vector. high_frequency_cutoff : {None, float}, optional The frequency to stop considering accumulated power. If None, continue until the end of the input vector. Returns ------- Frequency Series: FrequencySeries A frequency series containing the cumulative sigmasq. """ htilde = make_frequency_series(htilde) N = (len(htilde)-1) * 2 norm = 4.0 * htilde.delta_f kmin, kmax = get_cutoff_indices(low_frequency_cutoff, high_frequency_cutoff, htilde.delta_f, N) sigma_vec = FrequencySeries(zeros(len(htilde), dtype=real_same_precision_as(htilde)), delta_f = htilde.delta_f, copy=False) mag = htilde.squared_norm() if psd is not None: mag /= psd sigma_vec[kmin:kmax] = mag[kmin:kmax].cumsum() return sigma_vec*norm
python
def sigmasq_series(htilde, psd=None, low_frequency_cutoff=None, high_frequency_cutoff=None): htilde = make_frequency_series(htilde) N = (len(htilde)-1) * 2 norm = 4.0 * htilde.delta_f kmin, kmax = get_cutoff_indices(low_frequency_cutoff, high_frequency_cutoff, htilde.delta_f, N) sigma_vec = FrequencySeries(zeros(len(htilde), dtype=real_same_precision_as(htilde)), delta_f = htilde.delta_f, copy=False) mag = htilde.squared_norm() if psd is not None: mag /= psd sigma_vec[kmin:kmax] = mag[kmin:kmax].cumsum() return sigma_vec*norm
[ "def", "sigmasq_series", "(", "htilde", ",", "psd", "=", "None", ",", "low_frequency_cutoff", "=", "None", ",", "high_frequency_cutoff", "=", "None", ")", ":", "htilde", "=", "make_frequency_series", "(", "htilde", ")", "N", "=", "(", "len", "(", "htilde", ...
Return a cumulative sigmasq frequency series. Return a frequency series containing the accumulated power in the input up to that frequency. Parameters ---------- htilde : TimeSeries or FrequencySeries The input vector psd : {None, FrequencySeries}, optional The psd used to weight the accumulated power. low_frequency_cutoff : {None, float}, optional The frequency to begin accumulating power. If None, start at the beginning of the vector. high_frequency_cutoff : {None, float}, optional The frequency to stop considering accumulated power. If None, continue until the end of the input vector. Returns ------- Frequency Series: FrequencySeries A frequency series containing the cumulative sigmasq.
[ "Return", "a", "cumulative", "sigmasq", "frequency", "series", "." ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/filter/matchedfilter.py#L1015-L1056
227,658
gwastro/pycbc
pycbc/filter/matchedfilter.py
sigma
def sigma(htilde, psd = None, low_frequency_cutoff=None, high_frequency_cutoff=None): """ Return the sigma of the waveform. See sigmasq for more details. Parameters ---------- htilde : TimeSeries or FrequencySeries The input vector containing a waveform. psd : {None, FrequencySeries}, optional The psd used to weight the accumulated power. low_frequency_cutoff : {None, float}, optional The frequency to begin considering waveform power. high_frequency_cutoff : {None, float}, optional The frequency to stop considering waveform power. Returns ------- sigmasq: float """ return sqrt(sigmasq(htilde, psd, low_frequency_cutoff, high_frequency_cutoff))
python
def sigma(htilde, psd = None, low_frequency_cutoff=None, high_frequency_cutoff=None): return sqrt(sigmasq(htilde, psd, low_frequency_cutoff, high_frequency_cutoff))
[ "def", "sigma", "(", "htilde", ",", "psd", "=", "None", ",", "low_frequency_cutoff", "=", "None", ",", "high_frequency_cutoff", "=", "None", ")", ":", "return", "sqrt", "(", "sigmasq", "(", "htilde", ",", "psd", ",", "low_frequency_cutoff", ",", "high_freque...
Return the sigma of the waveform. See sigmasq for more details. Parameters ---------- htilde : TimeSeries or FrequencySeries The input vector containing a waveform. psd : {None, FrequencySeries}, optional The psd used to weight the accumulated power. low_frequency_cutoff : {None, float}, optional The frequency to begin considering waveform power. high_frequency_cutoff : {None, float}, optional The frequency to stop considering waveform power. Returns ------- sigmasq: float
[ "Return", "the", "sigma", "of", "the", "waveform", ".", "See", "sigmasq", "for", "more", "details", "." ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/filter/matchedfilter.py#L1101-L1120
227,659
gwastro/pycbc
pycbc/filter/matchedfilter.py
get_cutoff_indices
def get_cutoff_indices(flow, fhigh, df, N): """ Gets the indices of a frequency series at which to stop an overlap calculation. Parameters ---------- flow: float The frequency (in Hz) of the lower index. fhigh: float The frequency (in Hz) of the upper index. df: float The frequency step (in Hz) of the frequency series. N: int The number of points in the **time** series. Can be odd or even. Returns ------- kmin: int kmax: int """ if flow: kmin = int(flow / df) if kmin < 0: err_msg = "Start frequency cannot be negative. " err_msg += "Supplied value and kmin {} and {}".format(flow, kmin) raise ValueError(err_msg) else: kmin = 1 if fhigh: kmax = int(fhigh / df ) if kmax > int((N + 1)/2.): kmax = int((N + 1)/2.) else: # int() truncates towards 0, so this is # equivalent to the floor of the float kmax = int((N + 1)/2.) if kmax <= kmin: err_msg = "Kmax cannot be less than or equal to kmin. " err_msg += "Provided values of freqencies (min,max) were " err_msg += "{} and {} ".format(flow, fhigh) err_msg += "corresponding to (kmin, kmax) of " err_msg += "{} and {}.".format(kmin, kmax) raise ValueError(err_msg) return kmin,kmax
python
def get_cutoff_indices(flow, fhigh, df, N): if flow: kmin = int(flow / df) if kmin < 0: err_msg = "Start frequency cannot be negative. " err_msg += "Supplied value and kmin {} and {}".format(flow, kmin) raise ValueError(err_msg) else: kmin = 1 if fhigh: kmax = int(fhigh / df ) if kmax > int((N + 1)/2.): kmax = int((N + 1)/2.) else: # int() truncates towards 0, so this is # equivalent to the floor of the float kmax = int((N + 1)/2.) if kmax <= kmin: err_msg = "Kmax cannot be less than or equal to kmin. " err_msg += "Provided values of freqencies (min,max) were " err_msg += "{} and {} ".format(flow, fhigh) err_msg += "corresponding to (kmin, kmax) of " err_msg += "{} and {}.".format(kmin, kmax) raise ValueError(err_msg) return kmin,kmax
[ "def", "get_cutoff_indices", "(", "flow", ",", "fhigh", ",", "df", ",", "N", ")", ":", "if", "flow", ":", "kmin", "=", "int", "(", "flow", "/", "df", ")", "if", "kmin", "<", "0", ":", "err_msg", "=", "\"Start frequency cannot be negative. \"", "err_msg",...
Gets the indices of a frequency series at which to stop an overlap calculation. Parameters ---------- flow: float The frequency (in Hz) of the lower index. fhigh: float The frequency (in Hz) of the upper index. df: float The frequency step (in Hz) of the frequency series. N: int The number of points in the **time** series. Can be odd or even. Returns ------- kmin: int kmax: int
[ "Gets", "the", "indices", "of", "a", "frequency", "series", "at", "which", "to", "stop", "an", "overlap", "calculation", "." ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/filter/matchedfilter.py#L1122-L1169
227,660
gwastro/pycbc
pycbc/filter/matchedfilter.py
smear
def smear(idx, factor): """ This function will take as input an array of indexes and return every unique index within the specified factor of the inputs. E.g.: smear([5,7,100],2) = [3,4,5,6,7,8,9,98,99,100,101,102] Parameters ----------- idx : numpy.array of ints The indexes to be smeared. factor : idx The factor by which to smear out the input array. Returns -------- new_idx : numpy.array of ints The smeared array of indexes. """ s = [idx] for i in range(factor+1): a = i - factor/2 s += [idx + a] return numpy.unique(numpy.concatenate(s))
python
def smear(idx, factor): s = [idx] for i in range(factor+1): a = i - factor/2 s += [idx + a] return numpy.unique(numpy.concatenate(s))
[ "def", "smear", "(", "idx", ",", "factor", ")", ":", "s", "=", "[", "idx", "]", "for", "i", "in", "range", "(", "factor", "+", "1", ")", ":", "a", "=", "i", "-", "factor", "/", "2", "s", "+=", "[", "idx", "+", "a", "]", "return", "numpy", ...
This function will take as input an array of indexes and return every unique index within the specified factor of the inputs. E.g.: smear([5,7,100],2) = [3,4,5,6,7,8,9,98,99,100,101,102] Parameters ----------- idx : numpy.array of ints The indexes to be smeared. factor : idx The factor by which to smear out the input array. Returns -------- new_idx : numpy.array of ints The smeared array of indexes.
[ "This", "function", "will", "take", "as", "input", "an", "array", "of", "indexes", "and", "return", "every", "unique", "index", "within", "the", "specified", "factor", "of", "the", "inputs", "." ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/filter/matchedfilter.py#L1255-L1280
227,661
gwastro/pycbc
pycbc/filter/matchedfilter.py
matched_filter
def matched_filter(template, data, psd=None, low_frequency_cutoff=None, high_frequency_cutoff=None, sigmasq=None): """ Return the complex snr. Return the complex snr, along with its associated normalization of the template, matched filtered against the data. Parameters ---------- template : TimeSeries or FrequencySeries The template waveform data : TimeSeries or FrequencySeries The strain data to be filtered. psd : FrequencySeries The noise weighting of the filter. low_frequency_cutoff : {None, float}, optional The frequency to begin the filter calculation. If None, begin at the first frequency after DC. high_frequency_cutoff : {None, float}, optional The frequency to stop the filter calculation. If None, continue to the the nyquist frequency. sigmasq : {None, float}, optional The template normalization. If none, this value is calculated internally. Returns ------- snr : TimeSeries A time series containing the complex snr. """ snr, _, norm = matched_filter_core(template, data, psd=psd, low_frequency_cutoff=low_frequency_cutoff, high_frequency_cutoff=high_frequency_cutoff, h_norm=sigmasq) return snr * norm
python
def matched_filter(template, data, psd=None, low_frequency_cutoff=None, high_frequency_cutoff=None, sigmasq=None): snr, _, norm = matched_filter_core(template, data, psd=psd, low_frequency_cutoff=low_frequency_cutoff, high_frequency_cutoff=high_frequency_cutoff, h_norm=sigmasq) return snr * norm
[ "def", "matched_filter", "(", "template", ",", "data", ",", "psd", "=", "None", ",", "low_frequency_cutoff", "=", "None", ",", "high_frequency_cutoff", "=", "None", ",", "sigmasq", "=", "None", ")", ":", "snr", ",", "_", ",", "norm", "=", "matched_filter_c...
Return the complex snr. Return the complex snr, along with its associated normalization of the template, matched filtered against the data. Parameters ---------- template : TimeSeries or FrequencySeries The template waveform data : TimeSeries or FrequencySeries The strain data to be filtered. psd : FrequencySeries The noise weighting of the filter. low_frequency_cutoff : {None, float}, optional The frequency to begin the filter calculation. If None, begin at the first frequency after DC. high_frequency_cutoff : {None, float}, optional The frequency to stop the filter calculation. If None, continue to the the nyquist frequency. sigmasq : {None, float}, optional The template normalization. If none, this value is calculated internally. Returns ------- snr : TimeSeries A time series containing the complex snr.
[ "Return", "the", "complex", "snr", "." ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/filter/matchedfilter.py#L1282-L1315
227,662
gwastro/pycbc
pycbc/filter/matchedfilter.py
overlap
def overlap(vec1, vec2, psd=None, low_frequency_cutoff=None, high_frequency_cutoff=None, normalized=True): """ Return the overlap between the two TimeSeries or FrequencySeries. Parameters ---------- vec1 : TimeSeries or FrequencySeries The input vector containing a waveform. vec2 : TimeSeries or FrequencySeries The input vector containing a waveform. psd : Frequency Series A power spectral density to weight the overlap. low_frequency_cutoff : {None, float}, optional The frequency to begin the overlap. high_frequency_cutoff : {None, float}, optional The frequency to stop the overlap. normalized : {True, boolean}, optional Set if the overlap is normalized. If true, it will range from 0 to 1. Returns ------- overlap: float """ return overlap_cplx(vec1, vec2, psd=psd, \ low_frequency_cutoff=low_frequency_cutoff,\ high_frequency_cutoff=high_frequency_cutoff,\ normalized=normalized).real
python
def overlap(vec1, vec2, psd=None, low_frequency_cutoff=None, high_frequency_cutoff=None, normalized=True): return overlap_cplx(vec1, vec2, psd=psd, \ low_frequency_cutoff=low_frequency_cutoff,\ high_frequency_cutoff=high_frequency_cutoff,\ normalized=normalized).real
[ "def", "overlap", "(", "vec1", ",", "vec2", ",", "psd", "=", "None", ",", "low_frequency_cutoff", "=", "None", ",", "high_frequency_cutoff", "=", "None", ",", "normalized", "=", "True", ")", ":", "return", "overlap_cplx", "(", "vec1", ",", "vec2", ",", "...
Return the overlap between the two TimeSeries or FrequencySeries. Parameters ---------- vec1 : TimeSeries or FrequencySeries The input vector containing a waveform. vec2 : TimeSeries or FrequencySeries The input vector containing a waveform. psd : Frequency Series A power spectral density to weight the overlap. low_frequency_cutoff : {None, float}, optional The frequency to begin the overlap. high_frequency_cutoff : {None, float}, optional The frequency to stop the overlap. normalized : {True, boolean}, optional Set if the overlap is normalized. If true, it will range from 0 to 1. Returns ------- overlap: float
[ "Return", "the", "overlap", "between", "the", "two", "TimeSeries", "or", "FrequencySeries", "." ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/filter/matchedfilter.py#L1366-L1393
227,663
gwastro/pycbc
pycbc/filter/matchedfilter.py
overlap_cplx
def overlap_cplx(vec1, vec2, psd=None, low_frequency_cutoff=None, high_frequency_cutoff=None, normalized=True): """Return the complex overlap between the two TimeSeries or FrequencySeries. Parameters ---------- vec1 : TimeSeries or FrequencySeries The input vector containing a waveform. vec2 : TimeSeries or FrequencySeries The input vector containing a waveform. psd : Frequency Series A power spectral density to weight the overlap. low_frequency_cutoff : {None, float}, optional The frequency to begin the overlap. high_frequency_cutoff : {None, float}, optional The frequency to stop the overlap. normalized : {True, boolean}, optional Set if the overlap is normalized. If true, it will range from 0 to 1. Returns ------- overlap: complex """ htilde = make_frequency_series(vec1) stilde = make_frequency_series(vec2) kmin, kmax = get_cutoff_indices(low_frequency_cutoff, high_frequency_cutoff, stilde.delta_f, (len(stilde)-1) * 2) if psd: inner = (htilde[kmin:kmax]).weighted_inner(stilde[kmin:kmax], psd[kmin:kmax]) else: inner = (htilde[kmin:kmax]).inner(stilde[kmin:kmax]) if normalized: sig1 = sigma(vec1, psd=psd, low_frequency_cutoff=low_frequency_cutoff, high_frequency_cutoff=high_frequency_cutoff) sig2 = sigma(vec2, psd=psd, low_frequency_cutoff=low_frequency_cutoff, high_frequency_cutoff=high_frequency_cutoff) norm = 1 / sig1 / sig2 else: norm = 1 return 4 * htilde.delta_f * inner * norm
python
def overlap_cplx(vec1, vec2, psd=None, low_frequency_cutoff=None, high_frequency_cutoff=None, normalized=True): htilde = make_frequency_series(vec1) stilde = make_frequency_series(vec2) kmin, kmax = get_cutoff_indices(low_frequency_cutoff, high_frequency_cutoff, stilde.delta_f, (len(stilde)-1) * 2) if psd: inner = (htilde[kmin:kmax]).weighted_inner(stilde[kmin:kmax], psd[kmin:kmax]) else: inner = (htilde[kmin:kmax]).inner(stilde[kmin:kmax]) if normalized: sig1 = sigma(vec1, psd=psd, low_frequency_cutoff=low_frequency_cutoff, high_frequency_cutoff=high_frequency_cutoff) sig2 = sigma(vec2, psd=psd, low_frequency_cutoff=low_frequency_cutoff, high_frequency_cutoff=high_frequency_cutoff) norm = 1 / sig1 / sig2 else: norm = 1 return 4 * htilde.delta_f * inner * norm
[ "def", "overlap_cplx", "(", "vec1", ",", "vec2", ",", "psd", "=", "None", ",", "low_frequency_cutoff", "=", "None", ",", "high_frequency_cutoff", "=", "None", ",", "normalized", "=", "True", ")", ":", "htilde", "=", "make_frequency_series", "(", "vec1", ")",...
Return the complex overlap between the two TimeSeries or FrequencySeries. Parameters ---------- vec1 : TimeSeries or FrequencySeries The input vector containing a waveform. vec2 : TimeSeries or FrequencySeries The input vector containing a waveform. psd : Frequency Series A power spectral density to weight the overlap. low_frequency_cutoff : {None, float}, optional The frequency to begin the overlap. high_frequency_cutoff : {None, float}, optional The frequency to stop the overlap. normalized : {True, boolean}, optional Set if the overlap is normalized. If true, it will range from 0 to 1. Returns ------- overlap: complex
[ "Return", "the", "complex", "overlap", "between", "the", "two", "TimeSeries", "or", "FrequencySeries", "." ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/filter/matchedfilter.py#L1395-L1438
227,664
gwastro/pycbc
pycbc/filter/matchedfilter.py
quadratic_interpolate_peak
def quadratic_interpolate_peak(left, middle, right): """ Interpolate the peak and offset using a quadratic approximation Parameters ---------- left : numpy array Values at a relative bin value of [-1] middle : numpy array Values at a relative bin value of [0] right : numpy array Values at a relative bin value of [1] Returns ------- bin_offset : numpy array Array of bins offsets, each in the range [-1/2, 1/2] peak_values : numpy array Array of the estimated peak values at the interpolated offset """ bin_offset = 1.0/2.0 * (left - right) / (left - 2 * middle + right) peak_value = middle + 0.25 * (left - right) * bin_offset return bin_offset, peak_value
python
def quadratic_interpolate_peak(left, middle, right): bin_offset = 1.0/2.0 * (left - right) / (left - 2 * middle + right) peak_value = middle + 0.25 * (left - right) * bin_offset return bin_offset, peak_value
[ "def", "quadratic_interpolate_peak", "(", "left", ",", "middle", ",", "right", ")", ":", "bin_offset", "=", "1.0", "/", "2.0", "*", "(", "left", "-", "right", ")", "/", "(", "left", "-", "2", "*", "middle", "+", "right", ")", "peak_value", "=", "midd...
Interpolate the peak and offset using a quadratic approximation Parameters ---------- left : numpy array Values at a relative bin value of [-1] middle : numpy array Values at a relative bin value of [0] right : numpy array Values at a relative bin value of [1] Returns ------- bin_offset : numpy array Array of bins offsets, each in the range [-1/2, 1/2] peak_values : numpy array Array of the estimated peak values at the interpolated offset
[ "Interpolate", "the", "peak", "and", "offset", "using", "a", "quadratic", "approximation" ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/filter/matchedfilter.py#L1440-L1461
227,665
gwastro/pycbc
pycbc/filter/matchedfilter.py
followup_event_significance
def followup_event_significance(ifo, data_reader, bank, template_id, coinc_times, coinc_threshold=0.005, lookback=150, duration=0.095): """ Followup an event in another detector and determine its significance """ from pycbc.waveform import get_waveform_filter_length_in_time tmplt = bank.table[template_id] length_in_time = get_waveform_filter_length_in_time(tmplt['approximant'], tmplt) # calculate onsource time range from pycbc.detector import Detector onsource_start = -numpy.inf onsource_end = numpy.inf fdet = Detector(ifo) for cifo in coinc_times: time = coinc_times[cifo] dtravel = Detector(cifo).light_travel_time_to_detector(fdet) if time - dtravel > onsource_start: onsource_start = time - dtravel if time + dtravel < onsource_end: onsource_end = time + dtravel # Source must be within this time window to be considered a possible # coincidence onsource_start -= coinc_threshold onsource_end += coinc_threshold # Calculate how much time needed to calculate significance trim_pad = (data_reader.trim_padding * data_reader.strain.delta_t) bdur = int(lookback + 2.0 * trim_pad + length_in_time) if bdur > data_reader.strain.duration * .75: bdur = data_reader.strain.duration * .75 # Require all strain be valid within lookback time if data_reader.state is not None: state_start_time = data_reader.strain.end_time \ - data_reader.reduced_pad * data_reader.strain.delta_t - bdur if not data_reader.state.is_extent_valid(state_start_time, bdur): return None, None, None, None # We won't require that all DQ checks be valid for now, except at # onsource time. if data_reader.dq is not None: dq_start_time = onsource_start - duration / 2.0 dq_duration = onsource_end - onsource_start + duration if not data_reader.dq.is_extent_valid(dq_start_time, dq_duration): return None, None, None, None # Calculate SNR time series for this duration htilde = bank.get_template(template_id, min_buffer=bdur) stilde = data_reader.overwhitened_data(htilde.delta_f) sigma2 = htilde.sigmasq(stilde.psd) snr, _, norm = matched_filter_core(htilde, stilde, h_norm=sigma2) # Find peak in on-source and determine p-value onsrc = snr.time_slice(onsource_start, onsource_end) peak = onsrc.abs_arg_max() peak_time = peak * snr.delta_t + onsrc.start_time peak_value = abs(onsrc[peak]) bstart = float(snr.start_time) + length_in_time + trim_pad bkg = abs(snr.time_slice(bstart, onsource_start)).numpy() window = int((onsource_end - onsource_start) * snr.sample_rate) nsamples = int(len(bkg) / window) peaks = bkg[:nsamples*window].reshape(nsamples, window).max(axis=1) pvalue = (1 + (peaks >= peak_value).sum()) / float(1 + nsamples) # Return recentered source SNR for bayestar, along with p-value, and trig baysnr = snr.time_slice(peak_time - duration / 2.0, peak_time + duration / 2.0) logging.info('Adding %s to candidate, pvalue %s, %s samples', ifo, pvalue, nsamples) return baysnr * norm, peak_time, pvalue, sigma2
python
def followup_event_significance(ifo, data_reader, bank, template_id, coinc_times, coinc_threshold=0.005, lookback=150, duration=0.095): from pycbc.waveform import get_waveform_filter_length_in_time tmplt = bank.table[template_id] length_in_time = get_waveform_filter_length_in_time(tmplt['approximant'], tmplt) # calculate onsource time range from pycbc.detector import Detector onsource_start = -numpy.inf onsource_end = numpy.inf fdet = Detector(ifo) for cifo in coinc_times: time = coinc_times[cifo] dtravel = Detector(cifo).light_travel_time_to_detector(fdet) if time - dtravel > onsource_start: onsource_start = time - dtravel if time + dtravel < onsource_end: onsource_end = time + dtravel # Source must be within this time window to be considered a possible # coincidence onsource_start -= coinc_threshold onsource_end += coinc_threshold # Calculate how much time needed to calculate significance trim_pad = (data_reader.trim_padding * data_reader.strain.delta_t) bdur = int(lookback + 2.0 * trim_pad + length_in_time) if bdur > data_reader.strain.duration * .75: bdur = data_reader.strain.duration * .75 # Require all strain be valid within lookback time if data_reader.state is not None: state_start_time = data_reader.strain.end_time \ - data_reader.reduced_pad * data_reader.strain.delta_t - bdur if not data_reader.state.is_extent_valid(state_start_time, bdur): return None, None, None, None # We won't require that all DQ checks be valid for now, except at # onsource time. if data_reader.dq is not None: dq_start_time = onsource_start - duration / 2.0 dq_duration = onsource_end - onsource_start + duration if not data_reader.dq.is_extent_valid(dq_start_time, dq_duration): return None, None, None, None # Calculate SNR time series for this duration htilde = bank.get_template(template_id, min_buffer=bdur) stilde = data_reader.overwhitened_data(htilde.delta_f) sigma2 = htilde.sigmasq(stilde.psd) snr, _, norm = matched_filter_core(htilde, stilde, h_norm=sigma2) # Find peak in on-source and determine p-value onsrc = snr.time_slice(onsource_start, onsource_end) peak = onsrc.abs_arg_max() peak_time = peak * snr.delta_t + onsrc.start_time peak_value = abs(onsrc[peak]) bstart = float(snr.start_time) + length_in_time + trim_pad bkg = abs(snr.time_slice(bstart, onsource_start)).numpy() window = int((onsource_end - onsource_start) * snr.sample_rate) nsamples = int(len(bkg) / window) peaks = bkg[:nsamples*window].reshape(nsamples, window).max(axis=1) pvalue = (1 + (peaks >= peak_value).sum()) / float(1 + nsamples) # Return recentered source SNR for bayestar, along with p-value, and trig baysnr = snr.time_slice(peak_time - duration / 2.0, peak_time + duration / 2.0) logging.info('Adding %s to candidate, pvalue %s, %s samples', ifo, pvalue, nsamples) return baysnr * norm, peak_time, pvalue, sigma2
[ "def", "followup_event_significance", "(", "ifo", ",", "data_reader", ",", "bank", ",", "template_id", ",", "coinc_times", ",", "coinc_threshold", "=", "0.005", ",", "lookback", "=", "150", ",", "duration", "=", "0.095", ")", ":", "from", "pycbc", ".", "wave...
Followup an event in another detector and determine its significance
[ "Followup", "an", "event", "in", "another", "detector", "and", "determine", "its", "significance" ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/filter/matchedfilter.py#L1732-L1812
227,666
gwastro/pycbc
pycbc/filter/matchedfilter.py
compute_followup_snr_series
def compute_followup_snr_series(data_reader, htilde, trig_time, duration=0.095, check_state=True, coinc_window=0.05): """Given a StrainBuffer, a template frequency series and a trigger time, compute a portion of the SNR time series centered on the trigger for its rapid sky localization and followup. If the trigger time is too close to the boundary of the valid data segment the SNR series is calculated anyway and might be slightly contaminated by filter and wrap-around effects. For reasonable durations this will only affect a small fraction of the triggers and probably in a negligible way. Parameters ---------- data_reader : StrainBuffer The StrainBuffer object to read strain data from. htilde : FrequencySeries The frequency series containing the template waveform. trig_time : {float, lal.LIGOTimeGPS} The trigger time. duration : float (optional) Duration of the computed SNR series in seconds. If omitted, it defaults to twice the Earth light travel time plus 10 ms of timing uncertainty. check_state : boolean If True, and the detector was offline or flagged for bad data quality at any point during the inspiral, then return (None, None) instead. coinc_window : float (optional) Maximum possible time between coincident triggers at different detectors. This is needed to properly determine data padding. Returns ------- snr : TimeSeries The portion of SNR around the trigger. None if the detector is offline or has bad data quality, and check_state is True. """ if check_state: # was the detector observing for the full amount of involved data? state_start_time = trig_time - duration / 2 - htilde.length_in_time state_end_time = trig_time + duration / 2 state_duration = state_end_time - state_start_time if data_reader.state is not None: if not data_reader.state.is_extent_valid(state_start_time, state_duration): return None # was the data quality ok for the full amount of involved data? dq_start_time = state_start_time - data_reader.dq_padding dq_duration = state_duration + 2 * data_reader.dq_padding if data_reader.dq is not None: if not data_reader.dq.is_extent_valid(dq_start_time, dq_duration): return None stilde = data_reader.overwhitened_data(htilde.delta_f) snr, _, norm = matched_filter_core(htilde, stilde, h_norm=htilde.sigmasq(stilde.psd)) valid_end = int(len(snr) - data_reader.trim_padding) valid_start = int(valid_end - data_reader.blocksize * snr.sample_rate) half_dur_samples = int(snr.sample_rate * duration / 2) coinc_samples = int(snr.sample_rate * coinc_window) valid_start -= half_dur_samples + coinc_samples valid_end += half_dur_samples if valid_start < 0 or valid_end > len(snr)-1: raise ValueError(('Requested SNR duration ({0} s)' ' too long').format(duration)) # Onsource slice for Bayestar followup onsource_idx = float(trig_time - snr.start_time) * snr.sample_rate onsource_idx = int(round(onsource_idx)) onsource_slice = slice(onsource_idx - half_dur_samples, onsource_idx + half_dur_samples + 1) return snr[onsource_slice] * norm
python
def compute_followup_snr_series(data_reader, htilde, trig_time, duration=0.095, check_state=True, coinc_window=0.05): if check_state: # was the detector observing for the full amount of involved data? state_start_time = trig_time - duration / 2 - htilde.length_in_time state_end_time = trig_time + duration / 2 state_duration = state_end_time - state_start_time if data_reader.state is not None: if not data_reader.state.is_extent_valid(state_start_time, state_duration): return None # was the data quality ok for the full amount of involved data? dq_start_time = state_start_time - data_reader.dq_padding dq_duration = state_duration + 2 * data_reader.dq_padding if data_reader.dq is not None: if not data_reader.dq.is_extent_valid(dq_start_time, dq_duration): return None stilde = data_reader.overwhitened_data(htilde.delta_f) snr, _, norm = matched_filter_core(htilde, stilde, h_norm=htilde.sigmasq(stilde.psd)) valid_end = int(len(snr) - data_reader.trim_padding) valid_start = int(valid_end - data_reader.blocksize * snr.sample_rate) half_dur_samples = int(snr.sample_rate * duration / 2) coinc_samples = int(snr.sample_rate * coinc_window) valid_start -= half_dur_samples + coinc_samples valid_end += half_dur_samples if valid_start < 0 or valid_end > len(snr)-1: raise ValueError(('Requested SNR duration ({0} s)' ' too long').format(duration)) # Onsource slice for Bayestar followup onsource_idx = float(trig_time - snr.start_time) * snr.sample_rate onsource_idx = int(round(onsource_idx)) onsource_slice = slice(onsource_idx - half_dur_samples, onsource_idx + half_dur_samples + 1) return snr[onsource_slice] * norm
[ "def", "compute_followup_snr_series", "(", "data_reader", ",", "htilde", ",", "trig_time", ",", "duration", "=", "0.095", ",", "check_state", "=", "True", ",", "coinc_window", "=", "0.05", ")", ":", "if", "check_state", ":", "# was the detector observing for the ful...
Given a StrainBuffer, a template frequency series and a trigger time, compute a portion of the SNR time series centered on the trigger for its rapid sky localization and followup. If the trigger time is too close to the boundary of the valid data segment the SNR series is calculated anyway and might be slightly contaminated by filter and wrap-around effects. For reasonable durations this will only affect a small fraction of the triggers and probably in a negligible way. Parameters ---------- data_reader : StrainBuffer The StrainBuffer object to read strain data from. htilde : FrequencySeries The frequency series containing the template waveform. trig_time : {float, lal.LIGOTimeGPS} The trigger time. duration : float (optional) Duration of the computed SNR series in seconds. If omitted, it defaults to twice the Earth light travel time plus 10 ms of timing uncertainty. check_state : boolean If True, and the detector was offline or flagged for bad data quality at any point during the inspiral, then return (None, None) instead. coinc_window : float (optional) Maximum possible time between coincident triggers at different detectors. This is needed to properly determine data padding. Returns ------- snr : TimeSeries The portion of SNR around the trigger. None if the detector is offline or has bad data quality, and check_state is True.
[ "Given", "a", "StrainBuffer", "a", "template", "frequency", "series", "and", "a", "trigger", "time", "compute", "a", "portion", "of", "the", "SNR", "time", "series", "centered", "on", "the", "trigger", "for", "its", "rapid", "sky", "localization", "and", "fo...
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/filter/matchedfilter.py#L1814-L1892
227,667
gwastro/pycbc
pycbc/filter/matchedfilter.py
LiveBatchMatchedFilter.combine_results
def combine_results(self, results): """Combine results from different batches of filtering""" result = {} for key in results[0]: result[key] = numpy.concatenate([r[key] for r in results]) return result
python
def combine_results(self, results): result = {} for key in results[0]: result[key] = numpy.concatenate([r[key] for r in results]) return result
[ "def", "combine_results", "(", "self", ",", "results", ")", ":", "result", "=", "{", "}", "for", "key", "in", "results", "[", "0", "]", ":", "result", "[", "key", "]", "=", "numpy", ".", "concatenate", "(", "[", "r", "[", "key", "]", "for", "r", ...
Combine results from different batches of filtering
[ "Combine", "results", "from", "different", "batches", "of", "filtering" ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/filter/matchedfilter.py#L1575-L1580
227,668
gwastro/pycbc
pycbc/filter/matchedfilter.py
LiveBatchMatchedFilter.process_all
def process_all(self): """Process every batch group and return as single result""" results = [] veto_info = [] while 1: result, veto = self._process_batch() if result is False: return False if result is None: break results.append(result) veto_info += veto result = self.combine_results(results) if self.max_triggers_in_batch: sort = result['snr'].argsort()[::-1][:self.max_triggers_in_batch] for key in result: result[key] = result[key][sort] tmp = veto_info veto_info = [tmp[i] for i in sort] result = self._process_vetoes(result, veto_info) return result
python
def process_all(self): results = [] veto_info = [] while 1: result, veto = self._process_batch() if result is False: return False if result is None: break results.append(result) veto_info += veto result = self.combine_results(results) if self.max_triggers_in_batch: sort = result['snr'].argsort()[::-1][:self.max_triggers_in_batch] for key in result: result[key] = result[key][sort] tmp = veto_info veto_info = [tmp[i] for i in sort] result = self._process_vetoes(result, veto_info) return result
[ "def", "process_all", "(", "self", ")", ":", "results", "=", "[", "]", "veto_info", "=", "[", "]", "while", "1", ":", "result", ",", "veto", "=", "self", ".", "_process_batch", "(", ")", "if", "result", "is", "False", ":", "return", "False", "if", ...
Process every batch group and return as single result
[ "Process", "every", "batch", "group", "and", "return", "as", "single", "result" ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/filter/matchedfilter.py#L1587-L1609
227,669
gwastro/pycbc
pycbc/filter/matchedfilter.py
LiveBatchMatchedFilter._process_vetoes
def _process_vetoes(self, results, veto_info): """Calculate signal based vetoes""" chisq = numpy.array(numpy.zeros(len(veto_info)), numpy.float32, ndmin=1) dof = numpy.array(numpy.zeros(len(veto_info)), numpy.uint32, ndmin=1) sg_chisq = numpy.array(numpy.zeros(len(veto_info)), numpy.float32, ndmin=1) results['chisq'] = chisq results['chisq_dof'] = dof results['sg_chisq'] = sg_chisq keep = [] for i, (snrv, norm, l, htilde, stilde) in enumerate(veto_info): correlate(htilde, stilde, htilde.cout) c, d = self.power_chisq.values(htilde.cout, snrv, norm, stilde.psd, [l], htilde) chisq[i] = c[0] / d[0] dof[i] = d[0] sgv = self.sg_chisq.values(stilde, htilde, stilde.psd, snrv, norm, c, d, [l]) if sgv is not None: sg_chisq[i] = sgv[0] if self.newsnr_threshold: newsnr = ranking.newsnr(results['snr'][i], chisq[i]) if newsnr >= self.newsnr_threshold: keep.append(i) if self.newsnr_threshold: keep = numpy.array(keep, dtype=numpy.uint32) for key in results: results[key] = results[key][keep] return results
python
def _process_vetoes(self, results, veto_info): chisq = numpy.array(numpy.zeros(len(veto_info)), numpy.float32, ndmin=1) dof = numpy.array(numpy.zeros(len(veto_info)), numpy.uint32, ndmin=1) sg_chisq = numpy.array(numpy.zeros(len(veto_info)), numpy.float32, ndmin=1) results['chisq'] = chisq results['chisq_dof'] = dof results['sg_chisq'] = sg_chisq keep = [] for i, (snrv, norm, l, htilde, stilde) in enumerate(veto_info): correlate(htilde, stilde, htilde.cout) c, d = self.power_chisq.values(htilde.cout, snrv, norm, stilde.psd, [l], htilde) chisq[i] = c[0] / d[0] dof[i] = d[0] sgv = self.sg_chisq.values(stilde, htilde, stilde.psd, snrv, norm, c, d, [l]) if sgv is not None: sg_chisq[i] = sgv[0] if self.newsnr_threshold: newsnr = ranking.newsnr(results['snr'][i], chisq[i]) if newsnr >= self.newsnr_threshold: keep.append(i) if self.newsnr_threshold: keep = numpy.array(keep, dtype=numpy.uint32) for key in results: results[key] = results[key][keep] return results
[ "def", "_process_vetoes", "(", "self", ",", "results", ",", "veto_info", ")", ":", "chisq", "=", "numpy", ".", "array", "(", "numpy", ".", "zeros", "(", "len", "(", "veto_info", ")", ")", ",", "numpy", ".", "float32", ",", "ndmin", "=", "1", ")", "...
Calculate signal based vetoes
[ "Calculate", "signal", "based", "vetoes" ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/filter/matchedfilter.py#L1611-L1644
227,670
gwastro/pycbc
pycbc/psd/variation.py
calc_psd_variation
def calc_psd_variation(strain, psd_short_segment, psd_long_segment, short_psd_duration, short_psd_stride, psd_avg_method, low_freq, high_freq): """Calculates time series of PSD variability This function first splits the segment up into 512 second chunks. It then calculates the PSD over this 512 second period as well as in 4 second chunks throughout each 512 second period. Next the function estimates how different the 4 second PSD is to the 512 second PSD and produces a timeseries of this variability. Parameters ---------- strain : TimeSeries Input strain time series to estimate PSDs psd_short_segment : {float, 8} Duration of the short segments for PSD estimation in seconds. psd_long_segment : {float, 512} Duration of the long segments for PSD estimation in seconds. short_psd_duration : {float, 4} Duration of the segments for PSD estimation in seconds. short_psd_stride : {float, 2} Separation between PSD estimation segments in seconds. psd_avg_method : {string, 'median'} Method for averaging PSD estimation segments. low_freq : {float, 20} Minimum frequency to consider the comparison between PSDs. high_freq : {float, 480} Maximum frequency to consider the comparison between PSDs. Returns ------- psd_var : TimeSeries Time series of the variability in the PSD estimation """ # Calculate strain precision if strain.precision == 'single': fs_dtype = numpy.float32 elif strain.precision == 'double': fs_dtype = numpy.float64 # Convert start and end times immediately to floats start_time = numpy.float(strain.start_time) end_time = numpy.float(strain.end_time) # Find the times of the long segments times_long = numpy.arange(start_time, end_time, psd_long_segment) # Set up the empty time series for the PSD variation estimate psd_var = TimeSeries(zeros(int(numpy.ceil((end_time - start_time) / psd_short_segment))), delta_t=psd_short_segment, copy=False, epoch=start_time) ind = 0 for tlong in times_long: # Calculate PSD for long segment and separate the long segment in to # overlapping shorter segments if tlong + psd_long_segment <= end_time: psd_long = pycbc.psd.welch( strain.time_slice(tlong, tlong + psd_long_segment), seg_len=int(short_psd_duration * strain.sample_rate), seg_stride=int(short_psd_stride * strain.sample_rate), avg_method=psd_avg_method) times_short = numpy.arange(tlong, tlong + psd_long_segment, psd_short_segment) else: psd_long = pycbc.psd.welch( strain.time_slice(end_time - psd_long_segment, end_time), seg_len=int(short_psd_duration * strain.sample_rate), seg_stride=int(short_psd_stride * strain.sample_rate), avg_method=psd_avg_method) times_short = numpy.arange(tlong, end_time, psd_short_segment) # Calculate the PSD of the shorter segments psd_short = [] for tshort in times_short: if tshort + psd_short_segment <= end_time: pshort = pycbc.psd.welch( strain.time_slice(tshort, tshort + psd_short_segment), seg_len=int(short_psd_duration * strain.sample_rate), seg_stride=int(short_psd_stride * strain.sample_rate), avg_method=psd_avg_method) else: pshort = pycbc.psd.welch( strain.time_slice(tshort - psd_short_segment, end_time), seg_len=int(short_psd_duration * strain.sample_rate), seg_stride=int(short_psd_stride * strain.sample_rate), avg_method=psd_avg_method) psd_short.append(pshort) # Estimate the range of the PSD to compare kmin = int(low_freq / psd_long.delta_f) kmax = int(high_freq / psd_long.delta_f) # Comapre the PSD of the short segment to the long segment # The weight factor gives the rough response of a cbc template across # the defined frequency range given the expected PSD (i.e. long PSD) # Then integrate the weighted ratio of the actual PSD (i.e. short PSD) # with the expected PSD (i.e. long PSD) over the specified frequency # range freqs = FrequencySeries(psd_long.sample_frequencies, delta_f=psd_long.delta_f, epoch=psd_long.epoch, dtype=fs_dtype) weight = numpy.array( freqs[kmin:kmax]**(-7./3.) / psd_long[kmin:kmax]) weight /= weight.sum() diff = numpy.array([(weight * numpy.array(p_short[kmin:kmax] / psd_long[kmin:kmax])).sum() for p_short in psd_short]) # Store variation value for i, val in enumerate(diff): psd_var[ind+i] = val ind = ind+len(diff) return psd_var
python
def calc_psd_variation(strain, psd_short_segment, psd_long_segment, short_psd_duration, short_psd_stride, psd_avg_method, low_freq, high_freq): # Calculate strain precision if strain.precision == 'single': fs_dtype = numpy.float32 elif strain.precision == 'double': fs_dtype = numpy.float64 # Convert start and end times immediately to floats start_time = numpy.float(strain.start_time) end_time = numpy.float(strain.end_time) # Find the times of the long segments times_long = numpy.arange(start_time, end_time, psd_long_segment) # Set up the empty time series for the PSD variation estimate psd_var = TimeSeries(zeros(int(numpy.ceil((end_time - start_time) / psd_short_segment))), delta_t=psd_short_segment, copy=False, epoch=start_time) ind = 0 for tlong in times_long: # Calculate PSD for long segment and separate the long segment in to # overlapping shorter segments if tlong + psd_long_segment <= end_time: psd_long = pycbc.psd.welch( strain.time_slice(tlong, tlong + psd_long_segment), seg_len=int(short_psd_duration * strain.sample_rate), seg_stride=int(short_psd_stride * strain.sample_rate), avg_method=psd_avg_method) times_short = numpy.arange(tlong, tlong + psd_long_segment, psd_short_segment) else: psd_long = pycbc.psd.welch( strain.time_slice(end_time - psd_long_segment, end_time), seg_len=int(short_psd_duration * strain.sample_rate), seg_stride=int(short_psd_stride * strain.sample_rate), avg_method=psd_avg_method) times_short = numpy.arange(tlong, end_time, psd_short_segment) # Calculate the PSD of the shorter segments psd_short = [] for tshort in times_short: if tshort + psd_short_segment <= end_time: pshort = pycbc.psd.welch( strain.time_slice(tshort, tshort + psd_short_segment), seg_len=int(short_psd_duration * strain.sample_rate), seg_stride=int(short_psd_stride * strain.sample_rate), avg_method=psd_avg_method) else: pshort = pycbc.psd.welch( strain.time_slice(tshort - psd_short_segment, end_time), seg_len=int(short_psd_duration * strain.sample_rate), seg_stride=int(short_psd_stride * strain.sample_rate), avg_method=psd_avg_method) psd_short.append(pshort) # Estimate the range of the PSD to compare kmin = int(low_freq / psd_long.delta_f) kmax = int(high_freq / psd_long.delta_f) # Comapre the PSD of the short segment to the long segment # The weight factor gives the rough response of a cbc template across # the defined frequency range given the expected PSD (i.e. long PSD) # Then integrate the weighted ratio of the actual PSD (i.e. short PSD) # with the expected PSD (i.e. long PSD) over the specified frequency # range freqs = FrequencySeries(psd_long.sample_frequencies, delta_f=psd_long.delta_f, epoch=psd_long.epoch, dtype=fs_dtype) weight = numpy.array( freqs[kmin:kmax]**(-7./3.) / psd_long[kmin:kmax]) weight /= weight.sum() diff = numpy.array([(weight * numpy.array(p_short[kmin:kmax] / psd_long[kmin:kmax])).sum() for p_short in psd_short]) # Store variation value for i, val in enumerate(diff): psd_var[ind+i] = val ind = ind+len(diff) return psd_var
[ "def", "calc_psd_variation", "(", "strain", ",", "psd_short_segment", ",", "psd_long_segment", ",", "short_psd_duration", ",", "short_psd_stride", ",", "psd_avg_method", ",", "low_freq", ",", "high_freq", ")", ":", "# Calculate strain precision", "if", "strain", ".", ...
Calculates time series of PSD variability This function first splits the segment up into 512 second chunks. It then calculates the PSD over this 512 second period as well as in 4 second chunks throughout each 512 second period. Next the function estimates how different the 4 second PSD is to the 512 second PSD and produces a timeseries of this variability. Parameters ---------- strain : TimeSeries Input strain time series to estimate PSDs psd_short_segment : {float, 8} Duration of the short segments for PSD estimation in seconds. psd_long_segment : {float, 512} Duration of the long segments for PSD estimation in seconds. short_psd_duration : {float, 4} Duration of the segments for PSD estimation in seconds. short_psd_stride : {float, 2} Separation between PSD estimation segments in seconds. psd_avg_method : {string, 'median'} Method for averaging PSD estimation segments. low_freq : {float, 20} Minimum frequency to consider the comparison between PSDs. high_freq : {float, 480} Maximum frequency to consider the comparison between PSDs. Returns ------- psd_var : TimeSeries Time series of the variability in the PSD estimation
[ "Calculates", "time", "series", "of", "PSD", "variability" ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/psd/variation.py#L8-L134
227,671
gwastro/pycbc
pycbc/psd/variation.py
find_trigger_value
def find_trigger_value(psd_var, idx, start, sample_rate): """ Find the PSD variation value at a particular time Parameters ---------- psd_var : TimeSeries Time series of the varaibility in the PSD estimation idx : numpy.ndarray Time indices of the triggers start : float GPS start time sample_rate : float Sample rate defined in ini file Returns ------- vals : Array PSD variation value at a particular time """ # Find gps time of the trigger time = start + idx / sample_rate # Find where in the psd variation time series the trigger belongs ind = numpy.digitize(time, psd_var.sample_times) ind -= 1 vals = psd_var[ind] return vals
python
def find_trigger_value(psd_var, idx, start, sample_rate): # Find gps time of the trigger time = start + idx / sample_rate # Find where in the psd variation time series the trigger belongs ind = numpy.digitize(time, psd_var.sample_times) ind -= 1 vals = psd_var[ind] return vals
[ "def", "find_trigger_value", "(", "psd_var", ",", "idx", ",", "start", ",", "sample_rate", ")", ":", "# Find gps time of the trigger", "time", "=", "start", "+", "idx", "/", "sample_rate", "# Find where in the psd variation time series the trigger belongs", "ind", "=", ...
Find the PSD variation value at a particular time Parameters ---------- psd_var : TimeSeries Time series of the varaibility in the PSD estimation idx : numpy.ndarray Time indices of the triggers start : float GPS start time sample_rate : float Sample rate defined in ini file Returns ------- vals : Array PSD variation value at a particular time
[ "Find", "the", "PSD", "variation", "value", "at", "a", "particular", "time" ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/psd/variation.py#L136-L162
227,672
gwastro/pycbc
pycbc/workflow/minifollowups.py
setup_foreground_minifollowups
def setup_foreground_minifollowups(workflow, coinc_file, single_triggers, tmpltbank_file, insp_segs, insp_data_name, insp_anal_name, dax_output, out_dir, tags=None): """ Create plots that followup the Nth loudest coincident injection from a statmap produced HDF file. Parameters ---------- workflow: pycbc.workflow.Workflow The core workflow instance we are populating coinc_file: single_triggers: list of pycbc.workflow.File A list cointaining the file objects associated with the merged single detector trigger files for each ifo. tmpltbank_file: pycbc.workflow.File The file object pointing to the HDF format template bank insp_segs: SegFile The segment file containing the data read and analyzed by each inspiral job. insp_data_name: str The name of the segmentlist storing data read. insp_anal_name: str The name of the segmentlist storing data analyzed. out_dir: path The directory to store minifollowups result plots and files tags: {None, optional} Tags to add to the minifollowups executables Returns ------- layout: list A list of tuples which specify the displayed file layout for the minifollops plots. """ logging.info('Entering minifollowups module') if not workflow.cp.has_section('workflow-minifollowups'): logging.info('There is no [workflow-minifollowups] section in configuration file') logging.info('Leaving minifollowups') return tags = [] if tags is None else tags makedir(dax_output) # turn the config file into a File class config_path = os.path.abspath(dax_output + '/' + '_'.join(tags) + 'foreground_minifollowup.ini') workflow.cp.write(open(config_path, 'w')) config_file = wdax.File(os.path.basename(config_path)) config_file.PFN(urlparse.urljoin('file:', urllib.pathname2url(config_path)), site='local') exe = Executable(workflow.cp, 'foreground_minifollowup', ifos=workflow.ifos, out_dir=dax_output) node = exe.create_node() node.add_input_opt('--config-files', config_file) node.add_input_opt('--bank-file', tmpltbank_file) node.add_input_opt('--statmap-file', coinc_file) node.add_multiifo_input_list_opt('--single-detector-triggers', single_triggers) node.add_input_opt('--inspiral-segments', insp_segs) node.add_opt('--inspiral-data-read-name', insp_data_name) node.add_opt('--inspiral-data-analyzed-name', insp_anal_name) node.new_output_file_opt(workflow.analysis_time, '.dax', '--output-file', tags=tags) node.new_output_file_opt(workflow.analysis_time, '.dax.map', '--output-map', tags=tags) node.new_output_file_opt(workflow.analysis_time, '.tc.txt', '--transformation-catalog', tags=tags) name = node.output_files[0].name map_file = node.output_files[1] tc_file = node.output_files[2] node.add_opt('--workflow-name', name) node.add_opt('--output-dir', out_dir) workflow += node # execute this in a sub-workflow fil = node.output_files[0] # determine if a staging site has been specified try: staging_site = workflow.cp.get('workflow-foreground_minifollowups', 'staging-site') except: staging_site = None job = dax.DAX(fil) job.addArguments('--basename %s' % os.path.splitext(os.path.basename(name))[0]) Workflow.set_job_properties(job, map_file, tc_file, staging_site=staging_site) workflow._adag.addJob(job) dep = dax.Dependency(parent=node._dax_node, child=job) workflow._adag.addDependency(dep) logging.info('Leaving minifollowups module')
python
def setup_foreground_minifollowups(workflow, coinc_file, single_triggers, tmpltbank_file, insp_segs, insp_data_name, insp_anal_name, dax_output, out_dir, tags=None): logging.info('Entering minifollowups module') if not workflow.cp.has_section('workflow-minifollowups'): logging.info('There is no [workflow-minifollowups] section in configuration file') logging.info('Leaving minifollowups') return tags = [] if tags is None else tags makedir(dax_output) # turn the config file into a File class config_path = os.path.abspath(dax_output + '/' + '_'.join(tags) + 'foreground_minifollowup.ini') workflow.cp.write(open(config_path, 'w')) config_file = wdax.File(os.path.basename(config_path)) config_file.PFN(urlparse.urljoin('file:', urllib.pathname2url(config_path)), site='local') exe = Executable(workflow.cp, 'foreground_minifollowup', ifos=workflow.ifos, out_dir=dax_output) node = exe.create_node() node.add_input_opt('--config-files', config_file) node.add_input_opt('--bank-file', tmpltbank_file) node.add_input_opt('--statmap-file', coinc_file) node.add_multiifo_input_list_opt('--single-detector-triggers', single_triggers) node.add_input_opt('--inspiral-segments', insp_segs) node.add_opt('--inspiral-data-read-name', insp_data_name) node.add_opt('--inspiral-data-analyzed-name', insp_anal_name) node.new_output_file_opt(workflow.analysis_time, '.dax', '--output-file', tags=tags) node.new_output_file_opt(workflow.analysis_time, '.dax.map', '--output-map', tags=tags) node.new_output_file_opt(workflow.analysis_time, '.tc.txt', '--transformation-catalog', tags=tags) name = node.output_files[0].name map_file = node.output_files[1] tc_file = node.output_files[2] node.add_opt('--workflow-name', name) node.add_opt('--output-dir', out_dir) workflow += node # execute this in a sub-workflow fil = node.output_files[0] # determine if a staging site has been specified try: staging_site = workflow.cp.get('workflow-foreground_minifollowups', 'staging-site') except: staging_site = None job = dax.DAX(fil) job.addArguments('--basename %s' % os.path.splitext(os.path.basename(name))[0]) Workflow.set_job_properties(job, map_file, tc_file, staging_site=staging_site) workflow._adag.addJob(job) dep = dax.Dependency(parent=node._dax_node, child=job) workflow._adag.addDependency(dep) logging.info('Leaving minifollowups module')
[ "def", "setup_foreground_minifollowups", "(", "workflow", ",", "coinc_file", ",", "single_triggers", ",", "tmpltbank_file", ",", "insp_segs", ",", "insp_data_name", ",", "insp_anal_name", ",", "dax_output", ",", "out_dir", ",", "tags", "=", "None", ")", ":", "logg...
Create plots that followup the Nth loudest coincident injection from a statmap produced HDF file. Parameters ---------- workflow: pycbc.workflow.Workflow The core workflow instance we are populating coinc_file: single_triggers: list of pycbc.workflow.File A list cointaining the file objects associated with the merged single detector trigger files for each ifo. tmpltbank_file: pycbc.workflow.File The file object pointing to the HDF format template bank insp_segs: SegFile The segment file containing the data read and analyzed by each inspiral job. insp_data_name: str The name of the segmentlist storing data read. insp_anal_name: str The name of the segmentlist storing data analyzed. out_dir: path The directory to store minifollowups result plots and files tags: {None, optional} Tags to add to the minifollowups executables Returns ------- layout: list A list of tuples which specify the displayed file layout for the minifollops plots.
[ "Create", "plots", "that", "followup", "the", "Nth", "loudest", "coincident", "injection", "from", "a", "statmap", "produced", "HDF", "file", "." ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/workflow/minifollowups.py#L32-L123
227,673
gwastro/pycbc
pycbc/workflow/minifollowups.py
make_plot_waveform_plot
def make_plot_waveform_plot(workflow, params, out_dir, ifos, exclude=None, require=None, tags=None): """ Add plot_waveform jobs to the workflow. """ tags = [] if tags is None else tags makedir(out_dir) name = 'single_template_plot' secs = requirestr(workflow.cp.get_subsections(name), require) secs = excludestr(secs, exclude) files = FileList([]) for tag in secs: node = PlotExecutable(workflow.cp, 'plot_waveform', ifos=ifos, out_dir=out_dir, tags=[tag] + tags).create_node() node.add_opt('--mass1', "%.6f" % params['mass1']) node.add_opt('--mass2', "%.6f" % params['mass2']) node.add_opt('--spin1z',"%.6f" % params['spin1z']) node.add_opt('--spin2z',"%.6f" % params['spin2z']) if 'u_vals' in params: # Precessing options node.add_opt('--spin1x',"%.6f" % params['spin1x']) node.add_opt('--spin2x',"%.6f" % params['spin2x']) node.add_opt('--spin1y',"%.6f" % params['spin1y']) node.add_opt('--spin2y',"%.6f" % params['spin2y']) node.add_opt('--inclination',"%.6f" % params['inclination']) node.add_opt('--u-val', "%.6f" % params['u_vals']) node.new_output_file_opt(workflow.analysis_time, '.png', '--output-file') workflow += node files += node.output_files return files
python
def make_plot_waveform_plot(workflow, params, out_dir, ifos, exclude=None, require=None, tags=None): tags = [] if tags is None else tags makedir(out_dir) name = 'single_template_plot' secs = requirestr(workflow.cp.get_subsections(name), require) secs = excludestr(secs, exclude) files = FileList([]) for tag in secs: node = PlotExecutable(workflow.cp, 'plot_waveform', ifos=ifos, out_dir=out_dir, tags=[tag] + tags).create_node() node.add_opt('--mass1', "%.6f" % params['mass1']) node.add_opt('--mass2', "%.6f" % params['mass2']) node.add_opt('--spin1z',"%.6f" % params['spin1z']) node.add_opt('--spin2z',"%.6f" % params['spin2z']) if 'u_vals' in params: # Precessing options node.add_opt('--spin1x',"%.6f" % params['spin1x']) node.add_opt('--spin2x',"%.6f" % params['spin2x']) node.add_opt('--spin1y',"%.6f" % params['spin1y']) node.add_opt('--spin2y',"%.6f" % params['spin2y']) node.add_opt('--inclination',"%.6f" % params['inclination']) node.add_opt('--u-val', "%.6f" % params['u_vals']) node.new_output_file_opt(workflow.analysis_time, '.png', '--output-file') workflow += node files += node.output_files return files
[ "def", "make_plot_waveform_plot", "(", "workflow", ",", "params", ",", "out_dir", ",", "ifos", ",", "exclude", "=", "None", ",", "require", "=", "None", ",", "tags", "=", "None", ")", ":", "tags", "=", "[", "]", "if", "tags", "is", "None", "else", "t...
Add plot_waveform jobs to the workflow.
[ "Add", "plot_waveform", "jobs", "to", "the", "workflow", "." ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/workflow/minifollowups.py#L477-L506
227,674
gwastro/pycbc
pycbc/workflow/minifollowups.py
make_sngl_ifo
def make_sngl_ifo(workflow, sngl_file, bank_file, trigger_id, out_dir, ifo, tags=None, rank=None): """Setup a job to create sngl detector sngl ifo html summary snippet. """ tags = [] if tags is None else tags makedir(out_dir) name = 'page_snglinfo' files = FileList([]) node = PlotExecutable(workflow.cp, name, ifos=[ifo], out_dir=out_dir, tags=tags).create_node() node.add_input_opt('--single-trigger-file', sngl_file) node.add_input_opt('--bank-file', bank_file) node.add_opt('--trigger-id', str(trigger_id)) if rank is not None: node.add_opt('--n-loudest', str(rank)) node.add_opt('--instrument', ifo) node.new_output_file_opt(workflow.analysis_time, '.html', '--output-file') workflow += node files += node.output_files return files
python
def make_sngl_ifo(workflow, sngl_file, bank_file, trigger_id, out_dir, ifo, tags=None, rank=None): tags = [] if tags is None else tags makedir(out_dir) name = 'page_snglinfo' files = FileList([]) node = PlotExecutable(workflow.cp, name, ifos=[ifo], out_dir=out_dir, tags=tags).create_node() node.add_input_opt('--single-trigger-file', sngl_file) node.add_input_opt('--bank-file', bank_file) node.add_opt('--trigger-id', str(trigger_id)) if rank is not None: node.add_opt('--n-loudest', str(rank)) node.add_opt('--instrument', ifo) node.new_output_file_opt(workflow.analysis_time, '.html', '--output-file') workflow += node files += node.output_files return files
[ "def", "make_sngl_ifo", "(", "workflow", ",", "sngl_file", ",", "bank_file", ",", "trigger_id", ",", "out_dir", ",", "ifo", ",", "tags", "=", "None", ",", "rank", "=", "None", ")", ":", "tags", "=", "[", "]", "if", "tags", "is", "None", "else", "tags...
Setup a job to create sngl detector sngl ifo html summary snippet.
[ "Setup", "a", "job", "to", "create", "sngl", "detector", "sngl", "ifo", "html", "summary", "snippet", "." ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/workflow/minifollowups.py#L547-L566
227,675
gwastro/pycbc
pycbc/workflow/minifollowups.py
make_qscan_plot
def make_qscan_plot(workflow, ifo, trig_time, out_dir, injection_file=None, data_segments=None, time_window=100, tags=None): """ Generate a make_qscan node and add it to workflow. This function generates a single node of the singles_timefreq executable and adds it to the current workflow. Parent/child relationships are set by the input/output files automatically. Parameters ----------- workflow: pycbc.workflow.core.Workflow The workflow class that stores the jobs that will be run. ifo: str Which interferometer are we using? trig_time: int The time of the trigger being followed up. out_dir: str Location of directory to output to injection_file: pycbc.workflow.File (optional, default=None) If given, add the injections in the file to strain before making the plot. data_segments: ligo.segments.segmentlist (optional, default=None) The list of segments for which data exists and can be read in. If given the start/end times given to singles_timefreq will be adjusted if [trig_time - time_window, trig_time + time_window] does not completely lie within a valid data segment. A ValueError will be raised if the trig_time is not within a valid segment, or if it is not possible to find 2*time_window (plus the padding) of continuous data around the trigger. This **must** be coalesced. time_window: int (optional, default=None) The amount of data (not including padding) that will be read in by the singles_timefreq job. The default value of 100s should be fine for most cases. tags: list (optional, default=None) List of tags to add to the created nodes, which determine file naming. """ tags = [] if tags is None else tags makedir(out_dir) name = 'plot_qscan' curr_exe = PlotQScanExecutable(workflow.cp, name, ifos=[ifo], out_dir=out_dir, tags=tags) node = curr_exe.create_node() # Determine start/end times, using data segments if needed. # Begin by choosing "optimal" times start = trig_time - time_window end = trig_time + time_window # Then if data_segments is available, check against that, and move if # needed if data_segments is not None: # Assumes coalesced, so trig_time can only be within one segment for seg in data_segments: if trig_time in seg: data_seg = seg break elif trig_time == -1.0: node.add_opt('--gps-start-time', int(trig_time)) node.add_opt('--gps-end-time', int(trig_time)) node.add_opt('--center-time', trig_time) caption_string = "'No trigger in %s'" % ifo node.add_opt('--plot-caption', caption_string) node.new_output_file_opt(workflow.analysis_time, '.png', '--output-file') workflow += node return node.output_files else: err_msg = "Trig time {} ".format(trig_time) err_msg += "does not seem to lie within any data segments. " err_msg += "This shouldn't be possible, please ask for help!" raise ValueError(err_msg) # Check for pad-data if curr_exe.has_opt('pad-data'): pad_data = int(curr_exe.get_opt('pad-data')) else: pad_data = 0 # We only read data that's available. The code must handle the case # of not much data being available. if end > (data_seg[1] - pad_data): end = data_seg[1] - pad_data if start < (data_seg[0] + pad_data): start = data_seg[0] + pad_data node.add_opt('--gps-start-time', int(start)) node.add_opt('--gps-end-time', int(end)) node.add_opt('--center-time', trig_time) if injection_file is not None: node.add_input_opt('--injection-file', injection_file) node.new_output_file_opt(workflow.analysis_time, '.png', '--output-file') workflow += node return node.output_files
python
def make_qscan_plot(workflow, ifo, trig_time, out_dir, injection_file=None, data_segments=None, time_window=100, tags=None): tags = [] if tags is None else tags makedir(out_dir) name = 'plot_qscan' curr_exe = PlotQScanExecutable(workflow.cp, name, ifos=[ifo], out_dir=out_dir, tags=tags) node = curr_exe.create_node() # Determine start/end times, using data segments if needed. # Begin by choosing "optimal" times start = trig_time - time_window end = trig_time + time_window # Then if data_segments is available, check against that, and move if # needed if data_segments is not None: # Assumes coalesced, so trig_time can only be within one segment for seg in data_segments: if trig_time in seg: data_seg = seg break elif trig_time == -1.0: node.add_opt('--gps-start-time', int(trig_time)) node.add_opt('--gps-end-time', int(trig_time)) node.add_opt('--center-time', trig_time) caption_string = "'No trigger in %s'" % ifo node.add_opt('--plot-caption', caption_string) node.new_output_file_opt(workflow.analysis_time, '.png', '--output-file') workflow += node return node.output_files else: err_msg = "Trig time {} ".format(trig_time) err_msg += "does not seem to lie within any data segments. " err_msg += "This shouldn't be possible, please ask for help!" raise ValueError(err_msg) # Check for pad-data if curr_exe.has_opt('pad-data'): pad_data = int(curr_exe.get_opt('pad-data')) else: pad_data = 0 # We only read data that's available. The code must handle the case # of not much data being available. if end > (data_seg[1] - pad_data): end = data_seg[1] - pad_data if start < (data_seg[0] + pad_data): start = data_seg[0] + pad_data node.add_opt('--gps-start-time', int(start)) node.add_opt('--gps-end-time', int(end)) node.add_opt('--center-time', trig_time) if injection_file is not None: node.add_input_opt('--injection-file', injection_file) node.new_output_file_opt(workflow.analysis_time, '.png', '--output-file') workflow += node return node.output_files
[ "def", "make_qscan_plot", "(", "workflow", ",", "ifo", ",", "trig_time", ",", "out_dir", ",", "injection_file", "=", "None", ",", "data_segments", "=", "None", ",", "time_window", "=", "100", ",", "tags", "=", "None", ")", ":", "tags", "=", "[", "]", "...
Generate a make_qscan node and add it to workflow. This function generates a single node of the singles_timefreq executable and adds it to the current workflow. Parent/child relationships are set by the input/output files automatically. Parameters ----------- workflow: pycbc.workflow.core.Workflow The workflow class that stores the jobs that will be run. ifo: str Which interferometer are we using? trig_time: int The time of the trigger being followed up. out_dir: str Location of directory to output to injection_file: pycbc.workflow.File (optional, default=None) If given, add the injections in the file to strain before making the plot. data_segments: ligo.segments.segmentlist (optional, default=None) The list of segments for which data exists and can be read in. If given the start/end times given to singles_timefreq will be adjusted if [trig_time - time_window, trig_time + time_window] does not completely lie within a valid data segment. A ValueError will be raised if the trig_time is not within a valid segment, or if it is not possible to find 2*time_window (plus the padding) of continuous data around the trigger. This **must** be coalesced. time_window: int (optional, default=None) The amount of data (not including padding) that will be read in by the singles_timefreq job. The default value of 100s should be fine for most cases. tags: list (optional, default=None) List of tags to add to the created nodes, which determine file naming.
[ "Generate", "a", "make_qscan", "node", "and", "add", "it", "to", "workflow", "." ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/workflow/minifollowups.py#L591-L682
227,676
gwastro/pycbc
pycbc/workflow/minifollowups.py
make_singles_timefreq
def make_singles_timefreq(workflow, single, bank_file, trig_time, out_dir, veto_file=None, time_window=10, data_segments=None, tags=None): """ Generate a singles_timefreq node and add it to workflow. This function generates a single node of the singles_timefreq executable and adds it to the current workflow. Parent/child relationships are set by the input/output files automatically. Parameters ----------- workflow: pycbc.workflow.core.Workflow The workflow class that stores the jobs that will be run. single: pycbc.workflow.core.File instance The File object storing the single-detector triggers to followup. bank_file: pycbc.workflow.core.File instance The File object storing the template bank. trig_time: int The time of the trigger being followed up. out_dir: str Location of directory to output to veto_file: pycbc.workflow.core.File (optional, default=None) If given use this file to veto triggers to determine the loudest event. FIXME: Veto files *should* be provided a definer argument and not just assume that all segments should be read. time_window: int (optional, default=None) The amount of data (not including padding) that will be read in by the singles_timefreq job. The default value of 10s should be fine for most cases. data_segments: ligo.segments.segmentlist (optional, default=None) The list of segments for which data exists and can be read in. If given the start/end times given to singles_timefreq will be adjusted if [trig_time - time_window, trig_time + time_window] does not completely lie within a valid data segment. A ValueError will be raised if the trig_time is not within a valid segment, or if it is not possible to find 2*time_window (plus the padding) of continuous data around the trigger. This **must** be coalesced. tags: list (optional, default=None) List of tags to add to the created nodes, which determine file naming. """ tags = [] if tags is None else tags makedir(out_dir) name = 'plot_singles_timefreq' curr_exe = SingleTimeFreqExecutable(workflow.cp, name, ifos=[single.ifo], out_dir=out_dir, tags=tags) node = curr_exe.create_node() node.add_input_opt('--trig-file', single) node.add_input_opt('--bank-file', bank_file) # Determine start/end times, using data segments if needed. # Begin by choosing "optimal" times start = trig_time - time_window end = trig_time + time_window # Then if data_segments is available, check against that, and move if # needed if data_segments is not None: # Assumes coalesced, so trig_time can only be within one segment for seg in data_segments: if trig_time in seg: data_seg = seg break elif trig_time == -1.0: node.add_opt('--gps-start-time', int(trig_time)) node.add_opt('--gps-end-time', int(trig_time)) node.add_opt('--center-time', trig_time) if veto_file: node.add_input_opt('--veto-file', veto_file) node.add_opt('--detector', single.ifo) node.new_output_file_opt(workflow.analysis_time, '.png', '--output-file') workflow += node return node.output_files else: err_msg = "Trig time {} ".format(trig_time) err_msg += "does not seem to lie within any data segments. " err_msg += "This shouldn't be possible, please ask for help!" raise ValueError(err_msg) # Check for pad-data if curr_exe.has_opt('pad-data'): pad_data = int(curr_exe.get_opt('pad-data')) else: pad_data = 0 if abs(data_seg) < (2 * time_window + 2 * pad_data): tl = 2 * time_window + 2 * pad_data err_msg = "I was asked to use {} seconds of data ".format(tl) err_msg += "to run a plot_singles_timefreq job. However, I have " err_msg += "only {} seconds available.".format(abs(data_seg)) raise ValueError(err_msg) if data_seg[0] > (start - pad_data): start = data_seg[0] + pad_data end = start + 2 * time_window if data_seg[1] < (end + pad_data): end = data_seg[1] - pad_data start = end - 2 * time_window # Sanity check, shouldn't get here! if data_seg[0] > (start - pad_data): err_msg = "I shouldn't be here! Go ask Ian what he broke." raise ValueError(err_msg) node.add_opt('--gps-start-time', int(start)) node.add_opt('--gps-end-time', int(end)) node.add_opt('--center-time', trig_time) if veto_file: node.add_input_opt('--veto-file', veto_file) node.add_opt('--detector', single.ifo) node.new_output_file_opt(workflow.analysis_time, '.png', '--output-file') workflow += node return node.output_files
python
def make_singles_timefreq(workflow, single, bank_file, trig_time, out_dir, veto_file=None, time_window=10, data_segments=None, tags=None): tags = [] if tags is None else tags makedir(out_dir) name = 'plot_singles_timefreq' curr_exe = SingleTimeFreqExecutable(workflow.cp, name, ifos=[single.ifo], out_dir=out_dir, tags=tags) node = curr_exe.create_node() node.add_input_opt('--trig-file', single) node.add_input_opt('--bank-file', bank_file) # Determine start/end times, using data segments if needed. # Begin by choosing "optimal" times start = trig_time - time_window end = trig_time + time_window # Then if data_segments is available, check against that, and move if # needed if data_segments is not None: # Assumes coalesced, so trig_time can only be within one segment for seg in data_segments: if trig_time in seg: data_seg = seg break elif trig_time == -1.0: node.add_opt('--gps-start-time', int(trig_time)) node.add_opt('--gps-end-time', int(trig_time)) node.add_opt('--center-time', trig_time) if veto_file: node.add_input_opt('--veto-file', veto_file) node.add_opt('--detector', single.ifo) node.new_output_file_opt(workflow.analysis_time, '.png', '--output-file') workflow += node return node.output_files else: err_msg = "Trig time {} ".format(trig_time) err_msg += "does not seem to lie within any data segments. " err_msg += "This shouldn't be possible, please ask for help!" raise ValueError(err_msg) # Check for pad-data if curr_exe.has_opt('pad-data'): pad_data = int(curr_exe.get_opt('pad-data')) else: pad_data = 0 if abs(data_seg) < (2 * time_window + 2 * pad_data): tl = 2 * time_window + 2 * pad_data err_msg = "I was asked to use {} seconds of data ".format(tl) err_msg += "to run a plot_singles_timefreq job. However, I have " err_msg += "only {} seconds available.".format(abs(data_seg)) raise ValueError(err_msg) if data_seg[0] > (start - pad_data): start = data_seg[0] + pad_data end = start + 2 * time_window if data_seg[1] < (end + pad_data): end = data_seg[1] - pad_data start = end - 2 * time_window # Sanity check, shouldn't get here! if data_seg[0] > (start - pad_data): err_msg = "I shouldn't be here! Go ask Ian what he broke." raise ValueError(err_msg) node.add_opt('--gps-start-time', int(start)) node.add_opt('--gps-end-time', int(end)) node.add_opt('--center-time', trig_time) if veto_file: node.add_input_opt('--veto-file', veto_file) node.add_opt('--detector', single.ifo) node.new_output_file_opt(workflow.analysis_time, '.png', '--output-file') workflow += node return node.output_files
[ "def", "make_singles_timefreq", "(", "workflow", ",", "single", ",", "bank_file", ",", "trig_time", ",", "out_dir", ",", "veto_file", "=", "None", ",", "time_window", "=", "10", ",", "data_segments", "=", "None", ",", "tags", "=", "None", ")", ":", "tags",...
Generate a singles_timefreq node and add it to workflow. This function generates a single node of the singles_timefreq executable and adds it to the current workflow. Parent/child relationships are set by the input/output files automatically. Parameters ----------- workflow: pycbc.workflow.core.Workflow The workflow class that stores the jobs that will be run. single: pycbc.workflow.core.File instance The File object storing the single-detector triggers to followup. bank_file: pycbc.workflow.core.File instance The File object storing the template bank. trig_time: int The time of the trigger being followed up. out_dir: str Location of directory to output to veto_file: pycbc.workflow.core.File (optional, default=None) If given use this file to veto triggers to determine the loudest event. FIXME: Veto files *should* be provided a definer argument and not just assume that all segments should be read. time_window: int (optional, default=None) The amount of data (not including padding) that will be read in by the singles_timefreq job. The default value of 10s should be fine for most cases. data_segments: ligo.segments.segmentlist (optional, default=None) The list of segments for which data exists and can be read in. If given the start/end times given to singles_timefreq will be adjusted if [trig_time - time_window, trig_time + time_window] does not completely lie within a valid data segment. A ValueError will be raised if the trig_time is not within a valid segment, or if it is not possible to find 2*time_window (plus the padding) of continuous data around the trigger. This **must** be coalesced. tags: list (optional, default=None) List of tags to add to the created nodes, which determine file naming.
[ "Generate", "a", "singles_timefreq", "node", "and", "add", "it", "to", "workflow", "." ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/workflow/minifollowups.py#L684-L795
227,677
gwastro/pycbc
pycbc/distributions/joint.py
JointDistribution.apply_boundary_conditions
def apply_boundary_conditions(self, **params): """Applies each distributions' boundary conditions to the given list of parameters, returning a new list with the conditions applied. Parameters ---------- **params : Keyword arguments should give the parameters to apply the conditions to. Returns ------- dict A dictionary of the parameters after each distribution's `apply_boundary_conditions` function has been applied. """ for dist in self.distributions: params.update(dist.apply_boundary_conditions(**params)) return params
python
def apply_boundary_conditions(self, **params): for dist in self.distributions: params.update(dist.apply_boundary_conditions(**params)) return params
[ "def", "apply_boundary_conditions", "(", "self", ",", "*", "*", "params", ")", ":", "for", "dist", "in", "self", ".", "distributions", ":", "params", ".", "update", "(", "dist", ".", "apply_boundary_conditions", "(", "*", "*", "params", ")", ")", "return",...
Applies each distributions' boundary conditions to the given list of parameters, returning a new list with the conditions applied. Parameters ---------- **params : Keyword arguments should give the parameters to apply the conditions to. Returns ------- dict A dictionary of the parameters after each distribution's `apply_boundary_conditions` function has been applied.
[ "Applies", "each", "distributions", "boundary", "conditions", "to", "the", "given", "list", "of", "parameters", "returning", "a", "new", "list", "with", "the", "conditions", "applied", "." ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/distributions/joint.py#L134-L152
227,678
gwastro/pycbc
pycbc/distributions/joint.py
JointDistribution.rvs
def rvs(self, size=1): """ Rejection samples the parameter space. """ # create output FieldArray out = record.FieldArray(size, dtype=[(arg, float) for arg in self.variable_args]) # loop until enough samples accepted n = 0 while n < size: # draw samples samples = {} for dist in self.distributions: draw = dist.rvs(1) for param in dist.params: samples[param] = draw[param][0] vals = numpy.array([samples[arg] for arg in self.variable_args]) # determine if all parameter values are in prior space # if they are then add to output if self(**dict(zip(self.variable_args, vals))) > -numpy.inf: out[n] = vals n += 1 return out
python
def rvs(self, size=1): # create output FieldArray out = record.FieldArray(size, dtype=[(arg, float) for arg in self.variable_args]) # loop until enough samples accepted n = 0 while n < size: # draw samples samples = {} for dist in self.distributions: draw = dist.rvs(1) for param in dist.params: samples[param] = draw[param][0] vals = numpy.array([samples[arg] for arg in self.variable_args]) # determine if all parameter values are in prior space # if they are then add to output if self(**dict(zip(self.variable_args, vals))) > -numpy.inf: out[n] = vals n += 1 return out
[ "def", "rvs", "(", "self", ",", "size", "=", "1", ")", ":", "# create output FieldArray", "out", "=", "record", ".", "FieldArray", "(", "size", ",", "dtype", "=", "[", "(", "arg", ",", "float", ")", "for", "arg", "in", "self", ".", "variable_args", "...
Rejection samples the parameter space.
[ "Rejection", "samples", "the", "parameter", "space", "." ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/distributions/joint.py#L163-L189
227,679
gwastro/pycbc
pycbc/waveform/waveform.py
_check_lal_pars
def _check_lal_pars(p): """ Create a laldict object from the dictionary of waveform parameters Parameters ---------- p: dictionary The dictionary of lalsimulation paramaters Returns ------- laldict: LalDict The lal type dictionary to pass to the lalsimulation waveform functions. """ lal_pars = lal.CreateDict() #nonGRparams can be straightforwardly added if needed, however they have to # be invoked one by one if p['phase_order']!=-1: lalsimulation.SimInspiralWaveformParamsInsertPNPhaseOrder(lal_pars,int(p['phase_order'])) if p['amplitude_order']!=-1: lalsimulation.SimInspiralWaveformParamsInsertPNAmplitudeOrder(lal_pars,int(p['amplitude_order'])) if p['spin_order']!=-1: lalsimulation.SimInspiralWaveformParamsInsertPNSpinOrder(lal_pars,int(p['spin_order'])) if p['tidal_order']!=-1: lalsimulation.SimInspiralWaveformParamsInsertPNTidalOrder(lal_pars, p['tidal_order']) if p['eccentricity_order']!=-1: lalsimulation.SimInspiralWaveformParamsInsertPNEccentricityOrder(lal_pars, p['eccentricity_order']) if p['lambda1'] is not None: lalsimulation.SimInspiralWaveformParamsInsertTidalLambda1(lal_pars, p['lambda1']) if p['lambda2'] is not None: lalsimulation.SimInspiralWaveformParamsInsertTidalLambda2(lal_pars, p['lambda2']) if p['lambda_octu1'] is not None: lalsimulation.SimInspiralWaveformParamsInsertTidalOctupolarLambda1(lal_pars, p['lambda_octu1']) if p['lambda_octu2'] is not None: lalsimulation.SimInspiralWaveformParamsInsertTidalOctupolarLambda2(lal_pars, p['lambda_octu2']) if p['quadfmode1'] is not None: lalsimulation.SimInspiralWaveformParamsInsertTidalQuadrupolarFMode1(lal_pars, p['quadfmode1']) if p['quadfmode2'] is not None: lalsimulation.SimInspiralWaveformParamsInsertTidalQuadrupolarFMode2(lal_pars, p['quadfmode2']) if p['octufmode1'] is not None: lalsimulation.SimInspiralWaveformParamsInsertTidalOctupolarFMode1(lal_pars, p['octufmode1']) if p['octufmode2'] is not None: lalsimulation.SimInspiralWaveformParamsInsertTidalOctupolarFMode2(lal_pars, p['octufmode2']) if p['dquad_mon1'] is not None: lalsimulation.SimInspiralWaveformParamsInsertdQuadMon1(lal_pars, p['dquad_mon1']) if p['dquad_mon2'] is not None: lalsimulation.SimInspiralWaveformParamsInsertdQuadMon2(lal_pars, p['dquad_mon2']) if p['numrel_data']: lalsimulation.SimInspiralWaveformParamsInsertNumRelData(lal_pars, str(p['numrel_data'])) if p['modes_choice']: lalsimulation.SimInspiralWaveformParamsInsertModesChoice(lal_pars, p['modes_choice']) if p['frame_axis']: lalsimulation.SimInspiralWaveformParamsInsertFrameAxis(lal_pars, p['frame_axis']) if p['side_bands']: lalsimulation.SimInspiralWaveformParamsInsertSideband(lal_pars, p['side_bands']) if p['mode_array'] is not None: ma = lalsimulation.SimInspiralCreateModeArray() for l,m in p['mode_array']: lalsimulation.SimInspiralModeArrayActivateMode(ma, l, m) lalsimulation.SimInspiralWaveformParamsInsertModeArray(lal_pars, ma) return lal_pars
python
def _check_lal_pars(p): lal_pars = lal.CreateDict() #nonGRparams can be straightforwardly added if needed, however they have to # be invoked one by one if p['phase_order']!=-1: lalsimulation.SimInspiralWaveformParamsInsertPNPhaseOrder(lal_pars,int(p['phase_order'])) if p['amplitude_order']!=-1: lalsimulation.SimInspiralWaveformParamsInsertPNAmplitudeOrder(lal_pars,int(p['amplitude_order'])) if p['spin_order']!=-1: lalsimulation.SimInspiralWaveformParamsInsertPNSpinOrder(lal_pars,int(p['spin_order'])) if p['tidal_order']!=-1: lalsimulation.SimInspiralWaveformParamsInsertPNTidalOrder(lal_pars, p['tidal_order']) if p['eccentricity_order']!=-1: lalsimulation.SimInspiralWaveformParamsInsertPNEccentricityOrder(lal_pars, p['eccentricity_order']) if p['lambda1'] is not None: lalsimulation.SimInspiralWaveformParamsInsertTidalLambda1(lal_pars, p['lambda1']) if p['lambda2'] is not None: lalsimulation.SimInspiralWaveformParamsInsertTidalLambda2(lal_pars, p['lambda2']) if p['lambda_octu1'] is not None: lalsimulation.SimInspiralWaveformParamsInsertTidalOctupolarLambda1(lal_pars, p['lambda_octu1']) if p['lambda_octu2'] is not None: lalsimulation.SimInspiralWaveformParamsInsertTidalOctupolarLambda2(lal_pars, p['lambda_octu2']) if p['quadfmode1'] is not None: lalsimulation.SimInspiralWaveformParamsInsertTidalQuadrupolarFMode1(lal_pars, p['quadfmode1']) if p['quadfmode2'] is not None: lalsimulation.SimInspiralWaveformParamsInsertTidalQuadrupolarFMode2(lal_pars, p['quadfmode2']) if p['octufmode1'] is not None: lalsimulation.SimInspiralWaveformParamsInsertTidalOctupolarFMode1(lal_pars, p['octufmode1']) if p['octufmode2'] is not None: lalsimulation.SimInspiralWaveformParamsInsertTidalOctupolarFMode2(lal_pars, p['octufmode2']) if p['dquad_mon1'] is not None: lalsimulation.SimInspiralWaveformParamsInsertdQuadMon1(lal_pars, p['dquad_mon1']) if p['dquad_mon2'] is not None: lalsimulation.SimInspiralWaveformParamsInsertdQuadMon2(lal_pars, p['dquad_mon2']) if p['numrel_data']: lalsimulation.SimInspiralWaveformParamsInsertNumRelData(lal_pars, str(p['numrel_data'])) if p['modes_choice']: lalsimulation.SimInspiralWaveformParamsInsertModesChoice(lal_pars, p['modes_choice']) if p['frame_axis']: lalsimulation.SimInspiralWaveformParamsInsertFrameAxis(lal_pars, p['frame_axis']) if p['side_bands']: lalsimulation.SimInspiralWaveformParamsInsertSideband(lal_pars, p['side_bands']) if p['mode_array'] is not None: ma = lalsimulation.SimInspiralCreateModeArray() for l,m in p['mode_array']: lalsimulation.SimInspiralModeArrayActivateMode(ma, l, m) lalsimulation.SimInspiralWaveformParamsInsertModeArray(lal_pars, ma) return lal_pars
[ "def", "_check_lal_pars", "(", "p", ")", ":", "lal_pars", "=", "lal", ".", "CreateDict", "(", ")", "#nonGRparams can be straightforwardly added if needed, however they have to", "# be invoked one by one", "if", "p", "[", "'phase_order'", "]", "!=", "-", "1", ":", "lal...
Create a laldict object from the dictionary of waveform parameters Parameters ---------- p: dictionary The dictionary of lalsimulation paramaters Returns ------- laldict: LalDict The lal type dictionary to pass to the lalsimulation waveform functions.
[ "Create", "a", "laldict", "object", "from", "the", "dictionary", "of", "waveform", "parameters" ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/waveform/waveform.py#L73-L133
227,680
gwastro/pycbc
pycbc/waveform/waveform.py
_spintaylor_aligned_prec_swapper
def _spintaylor_aligned_prec_swapper(**p): """ SpinTaylorF2 is only single spin, it also struggles with anti-aligned spin waveforms. This construct chooses between the aligned-twospin TaylorF2 model and the precessing singlespin SpinTaylorF2 models. If aligned spins are given, use TaylorF2, if nonaligned spins are given use SpinTaylorF2. In the case of nonaligned doublespin systems the code will fail at the waveform generator level. """ orig_approximant = p['approximant'] if p['spin2x'] == 0 and p['spin2y'] == 0 and p['spin1x'] == 0 and \ p['spin1y'] == 0: p['approximant'] = 'TaylorF2' else: p['approximant'] = 'SpinTaylorF2' hp, hc = _lalsim_fd_waveform(**p) p['approximant'] = orig_approximant return hp, hc
python
def _spintaylor_aligned_prec_swapper(**p): orig_approximant = p['approximant'] if p['spin2x'] == 0 and p['spin2y'] == 0 and p['spin1x'] == 0 and \ p['spin1y'] == 0: p['approximant'] = 'TaylorF2' else: p['approximant'] = 'SpinTaylorF2' hp, hc = _lalsim_fd_waveform(**p) p['approximant'] = orig_approximant return hp, hc
[ "def", "_spintaylor_aligned_prec_swapper", "(", "*", "*", "p", ")", ":", "orig_approximant", "=", "p", "[", "'approximant'", "]", "if", "p", "[", "'spin2x'", "]", "==", "0", "and", "p", "[", "'spin2y'", "]", "==", "0", "and", "p", "[", "'spin1x'", "]",...
SpinTaylorF2 is only single spin, it also struggles with anti-aligned spin waveforms. This construct chooses between the aligned-twospin TaylorF2 model and the precessing singlespin SpinTaylorF2 models. If aligned spins are given, use TaylorF2, if nonaligned spins are given use SpinTaylorF2. In the case of nonaligned doublespin systems the code will fail at the waveform generator level.
[ "SpinTaylorF2", "is", "only", "single", "spin", "it", "also", "struggles", "with", "anti", "-", "aligned", "spin", "waveforms", ".", "This", "construct", "chooses", "between", "the", "aligned", "-", "twospin", "TaylorF2", "model", "and", "the", "precessing", "...
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/waveform/waveform.py#L179-L196
227,681
gwastro/pycbc
pycbc/waveform/waveform.py
get_obj_attrs
def get_obj_attrs(obj): """ Return a dictionary built from the attributes of the given object. """ pr = {} if obj is not None: if isinstance(obj, numpy.core.records.record): for name in obj.dtype.names: pr[name] = getattr(obj, name) elif hasattr(obj, '__dict__') and obj.__dict__: pr = obj.__dict__ elif hasattr(obj, '__slots__'): for slot in obj.__slots__: if hasattr(obj, slot): pr[slot] = getattr(obj, slot) elif isinstance(obj, dict): pr = obj.copy() else: for name in dir(obj): try: value = getattr(obj, name) if not name.startswith('__') and not inspect.ismethod(value): pr[name] = value except: continue return pr
python
def get_obj_attrs(obj): pr = {} if obj is not None: if isinstance(obj, numpy.core.records.record): for name in obj.dtype.names: pr[name] = getattr(obj, name) elif hasattr(obj, '__dict__') and obj.__dict__: pr = obj.__dict__ elif hasattr(obj, '__slots__'): for slot in obj.__slots__: if hasattr(obj, slot): pr[slot] = getattr(obj, slot) elif isinstance(obj, dict): pr = obj.copy() else: for name in dir(obj): try: value = getattr(obj, name) if not name.startswith('__') and not inspect.ismethod(value): pr[name] = value except: continue return pr
[ "def", "get_obj_attrs", "(", "obj", ")", ":", "pr", "=", "{", "}", "if", "obj", "is", "not", "None", ":", "if", "isinstance", "(", "obj", ",", "numpy", ".", "core", ".", "records", ".", "record", ")", ":", "for", "name", "in", "obj", ".", "dtype"...
Return a dictionary built from the attributes of the given object.
[ "Return", "a", "dictionary", "built", "from", "the", "attributes", "of", "the", "given", "object", "." ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/waveform/waveform.py#L320-L345
227,682
gwastro/pycbc
pycbc/waveform/waveform.py
get_fd_waveform_sequence
def get_fd_waveform_sequence(template=None, **kwds): """Return values of the waveform evaluated at the sequence of frequency points. Parameters ---------- template: object An object that has attached properties. This can be used to substitute for keyword arguments. A common example would be a row in an xml table. {params} Returns ------- hplustilde: Array The plus phase of the waveform in frequency domain evaluated at the frequency points. hcrosstilde: Array The cross phase of the waveform in frequency domain evaluated at the frequency points. """ kwds['delta_f'] = -1 kwds['f_lower'] = -1 p = props(template, required_args=fd_required_args, **kwds) lal_pars = _check_lal_pars(p) hp, hc = lalsimulation.SimInspiralChooseFDWaveformSequence(float(p['coa_phase']), float(pnutils.solar_mass_to_kg(p['mass1'])), float(pnutils.solar_mass_to_kg(p['mass2'])), float(p['spin1x']), float(p['spin1y']), float(p['spin1z']), float(p['spin2x']), float(p['spin2y']), float(p['spin2z']), float(p['f_ref']), pnutils.megaparsecs_to_meters(float(p['distance'])), float(p['inclination']), lal_pars, _lalsim_enum[p['approximant']], p['sample_points'].lal()) return Array(hp.data.data), Array(hc.data.data)
python
def get_fd_waveform_sequence(template=None, **kwds): kwds['delta_f'] = -1 kwds['f_lower'] = -1 p = props(template, required_args=fd_required_args, **kwds) lal_pars = _check_lal_pars(p) hp, hc = lalsimulation.SimInspiralChooseFDWaveformSequence(float(p['coa_phase']), float(pnutils.solar_mass_to_kg(p['mass1'])), float(pnutils.solar_mass_to_kg(p['mass2'])), float(p['spin1x']), float(p['spin1y']), float(p['spin1z']), float(p['spin2x']), float(p['spin2y']), float(p['spin2z']), float(p['f_ref']), pnutils.megaparsecs_to_meters(float(p['distance'])), float(p['inclination']), lal_pars, _lalsim_enum[p['approximant']], p['sample_points'].lal()) return Array(hp.data.data), Array(hc.data.data)
[ "def", "get_fd_waveform_sequence", "(", "template", "=", "None", ",", "*", "*", "kwds", ")", ":", "kwds", "[", "'delta_f'", "]", "=", "-", "1", "kwds", "[", "'f_lower'", "]", "=", "-", "1", "p", "=", "props", "(", "template", ",", "required_args", "=...
Return values of the waveform evaluated at the sequence of frequency points. Parameters ---------- template: object An object that has attached properties. This can be used to substitute for keyword arguments. A common example would be a row in an xml table. {params} Returns ------- hplustilde: Array The plus phase of the waveform in frequency domain evaluated at the frequency points. hcrosstilde: Array The cross phase of the waveform in frequency domain evaluated at the frequency points.
[ "Return", "values", "of", "the", "waveform", "evaluated", "at", "the", "sequence", "of", "frequency", "points", "." ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/waveform/waveform.py#L391-L427
227,683
gwastro/pycbc
pycbc/waveform/waveform.py
get_td_waveform
def get_td_waveform(template=None, **kwargs): """Return the plus and cross polarizations of a time domain waveform. Parameters ---------- template: object An object that has attached properties. This can be used to subsitute for keyword arguments. A common example would be a row in an xml table. {params} Returns ------- hplus: TimeSeries The plus polarization of the waveform. hcross: TimeSeries The cross polarization of the waveform. """ input_params = props(template, required_args=td_required_args, **kwargs) wav_gen = td_wav[type(_scheme.mgr.state)] if input_params['approximant'] not in wav_gen: raise ValueError("Approximant %s not available" % (input_params['approximant'])) return wav_gen[input_params['approximant']](**input_params)
python
def get_td_waveform(template=None, **kwargs): input_params = props(template, required_args=td_required_args, **kwargs) wav_gen = td_wav[type(_scheme.mgr.state)] if input_params['approximant'] not in wav_gen: raise ValueError("Approximant %s not available" % (input_params['approximant'])) return wav_gen[input_params['approximant']](**input_params)
[ "def", "get_td_waveform", "(", "template", "=", "None", ",", "*", "*", "kwargs", ")", ":", "input_params", "=", "props", "(", "template", ",", "required_args", "=", "td_required_args", ",", "*", "*", "kwargs", ")", "wav_gen", "=", "td_wav", "[", "type", ...
Return the plus and cross polarizations of a time domain waveform. Parameters ---------- template: object An object that has attached properties. This can be used to subsitute for keyword arguments. A common example would be a row in an xml table. {params} Returns ------- hplus: TimeSeries The plus polarization of the waveform. hcross: TimeSeries The cross polarization of the waveform.
[ "Return", "the", "plus", "and", "cross", "polarizations", "of", "a", "time", "domain", "waveform", "." ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/waveform/waveform.py#L433-L455
227,684
gwastro/pycbc
pycbc/waveform/waveform.py
get_fd_waveform
def get_fd_waveform(template=None, **kwargs): """Return a frequency domain gravitational waveform. Parameters ---------- template: object An object that has attached properties. This can be used to substitute for keyword arguments. A common example would be a row in an xml table. {params} Returns ------- hplustilde: FrequencySeries The plus phase of the waveform in frequency domain. hcrosstilde: FrequencySeries The cross phase of the waveform in frequency domain. """ input_params = props(template, required_args=fd_required_args, **kwargs) wav_gen = fd_wav[type(_scheme.mgr.state)] if input_params['approximant'] not in wav_gen: raise ValueError("Approximant %s not available" % (input_params['approximant'])) try: ffunc = input_params.pop('f_final_func') if ffunc != '': # convert the frequency function to a value input_params['f_final'] = pnutils.named_frequency_cutoffs[ffunc]( input_params) # if the f_final is < f_lower, raise a NoWaveformError if 'f_final' in input_params and \ (input_params['f_lower']+input_params['delta_f'] >= input_params['f_final']): raise NoWaveformError("cannot generate waveform: f_lower >= f_final") except KeyError: pass return wav_gen[input_params['approximant']](**input_params)
python
def get_fd_waveform(template=None, **kwargs): input_params = props(template, required_args=fd_required_args, **kwargs) wav_gen = fd_wav[type(_scheme.mgr.state)] if input_params['approximant'] not in wav_gen: raise ValueError("Approximant %s not available" % (input_params['approximant'])) try: ffunc = input_params.pop('f_final_func') if ffunc != '': # convert the frequency function to a value input_params['f_final'] = pnutils.named_frequency_cutoffs[ffunc]( input_params) # if the f_final is < f_lower, raise a NoWaveformError if 'f_final' in input_params and \ (input_params['f_lower']+input_params['delta_f'] >= input_params['f_final']): raise NoWaveformError("cannot generate waveform: f_lower >= f_final") except KeyError: pass return wav_gen[input_params['approximant']](**input_params)
[ "def", "get_fd_waveform", "(", "template", "=", "None", ",", "*", "*", "kwargs", ")", ":", "input_params", "=", "props", "(", "template", ",", "required_args", "=", "fd_required_args", ",", "*", "*", "kwargs", ")", "wav_gen", "=", "fd_wav", "[", "type", ...
Return a frequency domain gravitational waveform. Parameters ---------- template: object An object that has attached properties. This can be used to substitute for keyword arguments. A common example would be a row in an xml table. {params} Returns ------- hplustilde: FrequencySeries The plus phase of the waveform in frequency domain. hcrosstilde: FrequencySeries The cross phase of the waveform in frequency domain.
[ "Return", "a", "frequency", "domain", "gravitational", "waveform", "." ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/waveform/waveform.py#L461-L498
227,685
gwastro/pycbc
pycbc/waveform/waveform.py
get_interpolated_fd_waveform
def get_interpolated_fd_waveform(dtype=numpy.complex64, return_hc=True, **params): """ Return a fourier domain waveform approximant, using interpolation """ def rulog2(val): return 2.0 ** numpy.ceil(numpy.log2(float(val))) orig_approx = params['approximant'] params['approximant'] = params['approximant'].replace('_INTERP', '') df = params['delta_f'] if 'duration' not in params: duration = get_waveform_filter_length_in_time(**params) elif params['duration'] > 0: duration = params['duration'] else: err_msg = "Waveform duration must be greater than 0." raise ValueError(err_msg) #FIXME We should try to get this length directly somehow # I think this number should be conservative ringdown_padding = 0.5 df_min = 1.0 / rulog2(duration + ringdown_padding) # FIXME: I don't understand this, but waveforms with df_min < 0.5 will chop # off the inspiral when using ringdown_padding - 0.5. # Also, if ringdown_padding is set to a very small # value we can see cases where the ringdown is chopped. if df_min > 0.5: df_min = 0.5 params['delta_f'] = df_min hp, hc = get_fd_waveform(**params) hp = hp.astype(dtype) if return_hc: hc = hc.astype(dtype) else: hc = None f_end = get_waveform_end_frequency(**params) if f_end is None: f_end = (len(hp) - 1) * hp.delta_f if 'f_final' in params and params['f_final'] > 0: f_end_params = params['f_final'] if f_end is not None: f_end = min(f_end_params, f_end) n_min = int(rulog2(f_end / df_min)) + 1 if n_min < len(hp): hp = hp[:n_min] if hc is not None: hc = hc[:n_min] offset = int(ringdown_padding * (len(hp)-1)*2 * hp.delta_f) hp = interpolate_complex_frequency(hp, df, zeros_offset=offset, side='left') if hc is not None: hc = interpolate_complex_frequency(hc, df, zeros_offset=offset, side='left') params['approximant'] = orig_approx return hp, hc
python
def get_interpolated_fd_waveform(dtype=numpy.complex64, return_hc=True, **params): def rulog2(val): return 2.0 ** numpy.ceil(numpy.log2(float(val))) orig_approx = params['approximant'] params['approximant'] = params['approximant'].replace('_INTERP', '') df = params['delta_f'] if 'duration' not in params: duration = get_waveform_filter_length_in_time(**params) elif params['duration'] > 0: duration = params['duration'] else: err_msg = "Waveform duration must be greater than 0." raise ValueError(err_msg) #FIXME We should try to get this length directly somehow # I think this number should be conservative ringdown_padding = 0.5 df_min = 1.0 / rulog2(duration + ringdown_padding) # FIXME: I don't understand this, but waveforms with df_min < 0.5 will chop # off the inspiral when using ringdown_padding - 0.5. # Also, if ringdown_padding is set to a very small # value we can see cases where the ringdown is chopped. if df_min > 0.5: df_min = 0.5 params['delta_f'] = df_min hp, hc = get_fd_waveform(**params) hp = hp.astype(dtype) if return_hc: hc = hc.astype(dtype) else: hc = None f_end = get_waveform_end_frequency(**params) if f_end is None: f_end = (len(hp) - 1) * hp.delta_f if 'f_final' in params and params['f_final'] > 0: f_end_params = params['f_final'] if f_end is not None: f_end = min(f_end_params, f_end) n_min = int(rulog2(f_end / df_min)) + 1 if n_min < len(hp): hp = hp[:n_min] if hc is not None: hc = hc[:n_min] offset = int(ringdown_padding * (len(hp)-1)*2 * hp.delta_f) hp = interpolate_complex_frequency(hp, df, zeros_offset=offset, side='left') if hc is not None: hc = interpolate_complex_frequency(hc, df, zeros_offset=offset, side='left') params['approximant'] = orig_approx return hp, hc
[ "def", "get_interpolated_fd_waveform", "(", "dtype", "=", "numpy", ".", "complex64", ",", "return_hc", "=", "True", ",", "*", "*", "params", ")", ":", "def", "rulog2", "(", "val", ")", ":", "return", "2.0", "**", "numpy", ".", "ceil", "(", "numpy", "."...
Return a fourier domain waveform approximant, using interpolation
[ "Return", "a", "fourier", "domain", "waveform", "approximant", "using", "interpolation" ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/waveform/waveform.py#L632-L692
227,686
gwastro/pycbc
pycbc/waveform/waveform.py
get_sgburst_waveform
def get_sgburst_waveform(template=None, **kwargs): """Return the plus and cross polarizations of a time domain sine-Gaussian burst waveform. Parameters ---------- template: object An object that has attached properties. This can be used to subsitute for keyword arguments. A common example would be a row in an xml table. approximant : string A string that indicates the chosen approximant. See `td_approximants` for available options. q : float The quality factor of a sine-Gaussian burst frequency : float The centre-frequency of a sine-Gaussian burst delta_t : float The time step used to generate the waveform hrss : float The strain rss amplitude: float The strain amplitude Returns ------- hplus: TimeSeries The plus polarization of the waveform. hcross: TimeSeries The cross polarization of the waveform. """ input_params = props_sgburst(template,**kwargs) for arg in sgburst_required_args: if arg not in input_params: raise ValueError("Please provide " + str(arg)) return _lalsim_sgburst_waveform(**input_params)
python
def get_sgburst_waveform(template=None, **kwargs): input_params = props_sgburst(template,**kwargs) for arg in sgburst_required_args: if arg not in input_params: raise ValueError("Please provide " + str(arg)) return _lalsim_sgburst_waveform(**input_params)
[ "def", "get_sgburst_waveform", "(", "template", "=", "None", ",", "*", "*", "kwargs", ")", ":", "input_params", "=", "props_sgburst", "(", "template", ",", "*", "*", "kwargs", ")", "for", "arg", "in", "sgburst_required_args", ":", "if", "arg", "not", "in",...
Return the plus and cross polarizations of a time domain sine-Gaussian burst waveform. Parameters ---------- template: object An object that has attached properties. This can be used to subsitute for keyword arguments. A common example would be a row in an xml table. approximant : string A string that indicates the chosen approximant. See `td_approximants` for available options. q : float The quality factor of a sine-Gaussian burst frequency : float The centre-frequency of a sine-Gaussian burst delta_t : float The time step used to generate the waveform hrss : float The strain rss amplitude: float The strain amplitude Returns ------- hplus: TimeSeries The plus polarization of the waveform. hcross: TimeSeries The cross polarization of the waveform.
[ "Return", "the", "plus", "and", "cross", "polarizations", "of", "a", "time", "domain", "sine", "-", "Gaussian", "burst", "waveform", "." ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/waveform/waveform.py#L694-L730
227,687
gwastro/pycbc
pycbc/waveform/waveform.py
get_imr_length
def get_imr_length(approx, **kwds): """Call through to pnutils to obtain IMR waveform durations """ m1 = float(kwds['mass1']) m2 = float(kwds['mass2']) s1z = float(kwds['spin1z']) s2z = float(kwds['spin2z']) f_low = float(kwds['f_lower']) # 10% margin of error is incorporated in the pnutils function return pnutils.get_imr_duration(m1, m2, s1z, s2z, f_low, approximant=approx)
python
def get_imr_length(approx, **kwds): m1 = float(kwds['mass1']) m2 = float(kwds['mass2']) s1z = float(kwds['spin1z']) s2z = float(kwds['spin2z']) f_low = float(kwds['f_lower']) # 10% margin of error is incorporated in the pnutils function return pnutils.get_imr_duration(m1, m2, s1z, s2z, f_low, approximant=approx)
[ "def", "get_imr_length", "(", "approx", ",", "*", "*", "kwds", ")", ":", "m1", "=", "float", "(", "kwds", "[", "'mass1'", "]", ")", "m2", "=", "float", "(", "kwds", "[", "'mass2'", "]", ")", "s1z", "=", "float", "(", "kwds", "[", "'spin1z'", "]",...
Call through to pnutils to obtain IMR waveform durations
[ "Call", "through", "to", "pnutils", "to", "obtain", "IMR", "waveform", "durations" ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/waveform/waveform.py#L757-L766
227,688
gwastro/pycbc
pycbc/waveform/waveform.py
get_waveform_filter
def get_waveform_filter(out, template=None, **kwargs): """Return a frequency domain waveform filter for the specified approximant """ n = len(out) input_params = props(template, **kwargs) if input_params['approximant'] in filter_approximants(_scheme.mgr.state): wav_gen = filter_wav[type(_scheme.mgr.state)] htilde = wav_gen[input_params['approximant']](out=out, **input_params) htilde.resize(n) htilde.chirp_length = get_waveform_filter_length_in_time(**input_params) htilde.length_in_time = htilde.chirp_length return htilde if input_params['approximant'] in fd_approximants(_scheme.mgr.state): wav_gen = fd_wav[type(_scheme.mgr.state)] duration = get_waveform_filter_length_in_time(**input_params) hp, _ = wav_gen[input_params['approximant']](duration=duration, return_hc=False, **input_params) hp.resize(n) out[0:len(hp)] = hp[:] hp = FrequencySeries(out, delta_f=hp.delta_f, copy=False) hp.length_in_time = hp.chirp_length = duration return hp elif input_params['approximant'] in td_approximants(_scheme.mgr.state): wav_gen = td_wav[type(_scheme.mgr.state)] hp, _ = wav_gen[input_params['approximant']](**input_params) # taper the time series hp if required if 'taper' in input_params.keys() and \ input_params['taper'] is not None: hp = wfutils.taper_timeseries(hp, input_params['taper'], return_lal=False) return td_waveform_to_fd_waveform(hp, out=out) else: raise ValueError("Approximant %s not available" % (input_params['approximant']))
python
def get_waveform_filter(out, template=None, **kwargs): n = len(out) input_params = props(template, **kwargs) if input_params['approximant'] in filter_approximants(_scheme.mgr.state): wav_gen = filter_wav[type(_scheme.mgr.state)] htilde = wav_gen[input_params['approximant']](out=out, **input_params) htilde.resize(n) htilde.chirp_length = get_waveform_filter_length_in_time(**input_params) htilde.length_in_time = htilde.chirp_length return htilde if input_params['approximant'] in fd_approximants(_scheme.mgr.state): wav_gen = fd_wav[type(_scheme.mgr.state)] duration = get_waveform_filter_length_in_time(**input_params) hp, _ = wav_gen[input_params['approximant']](duration=duration, return_hc=False, **input_params) hp.resize(n) out[0:len(hp)] = hp[:] hp = FrequencySeries(out, delta_f=hp.delta_f, copy=False) hp.length_in_time = hp.chirp_length = duration return hp elif input_params['approximant'] in td_approximants(_scheme.mgr.state): wav_gen = td_wav[type(_scheme.mgr.state)] hp, _ = wav_gen[input_params['approximant']](**input_params) # taper the time series hp if required if 'taper' in input_params.keys() and \ input_params['taper'] is not None: hp = wfutils.taper_timeseries(hp, input_params['taper'], return_lal=False) return td_waveform_to_fd_waveform(hp, out=out) else: raise ValueError("Approximant %s not available" % (input_params['approximant']))
[ "def", "get_waveform_filter", "(", "out", ",", "template", "=", "None", ",", "*", "*", "kwargs", ")", ":", "n", "=", "len", "(", "out", ")", "input_params", "=", "props", "(", "template", ",", "*", "*", "kwargs", ")", "if", "input_params", "[", "'app...
Return a frequency domain waveform filter for the specified approximant
[ "Return", "a", "frequency", "domain", "waveform", "filter", "for", "the", "specified", "approximant" ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/waveform/waveform.py#L851-L892
227,689
gwastro/pycbc
pycbc/waveform/waveform.py
get_two_pol_waveform_filter
def get_two_pol_waveform_filter(outplus, outcross, template, **kwargs): """Return a frequency domain waveform filter for the specified approximant. Unlike get_waveform_filter this function returns both h_plus and h_cross components of the waveform, which are needed for searches where h_plus and h_cross are not related by a simple phase shift. """ n = len(outplus) # If we don't have an inclination column alpha3 might be used if not hasattr(template, 'inclination') and 'inclination' not in kwargs: if hasattr(template, 'alpha3'): kwargs['inclination'] = template.alpha3 input_params = props(template, **kwargs) if input_params['approximant'] in fd_approximants(_scheme.mgr.state): wav_gen = fd_wav[type(_scheme.mgr.state)] hp, hc = wav_gen[input_params['approximant']](**input_params) hp.resize(n) hc.resize(n) outplus[0:len(hp)] = hp[:] hp = FrequencySeries(outplus, delta_f=hp.delta_f, copy=False) outcross[0:len(hc)] = hc[:] hc = FrequencySeries(outcross, delta_f=hc.delta_f, copy=False) hp.chirp_length = get_waveform_filter_length_in_time(**input_params) hp.length_in_time = hp.chirp_length hc.chirp_length = hp.chirp_length hc.length_in_time = hp.length_in_time return hp, hc elif input_params['approximant'] in td_approximants(_scheme.mgr.state): # N: number of time samples required N = (n-1)*2 delta_f = 1.0 / (N * input_params['delta_t']) wav_gen = td_wav[type(_scheme.mgr.state)] hp, hc = wav_gen[input_params['approximant']](**input_params) # taper the time series hp if required if 'taper' in input_params.keys() and \ input_params['taper'] is not None: hp = wfutils.taper_timeseries(hp, input_params['taper'], return_lal=False) hc = wfutils.taper_timeseries(hc, input_params['taper'], return_lal=False) # total duration of the waveform tmplt_length = len(hp) * hp.delta_t # for IMR templates the zero of time is at max amplitude (merger) # thus the start time is minus the duration of the template from # lower frequency cutoff to merger, i.e. minus the 'chirp time' tChirp = - float( hp.start_time ) # conversion from LIGOTimeGPS hp.resize(N) hc.resize(N) k_zero = int(hp.start_time / hp.delta_t) hp.roll(k_zero) hc.roll(k_zero) hp_tilde = FrequencySeries(outplus, delta_f=delta_f, copy=False) hc_tilde = FrequencySeries(outcross, delta_f=delta_f, copy=False) fft(hp.astype(real_same_precision_as(hp_tilde)), hp_tilde) fft(hc.astype(real_same_precision_as(hc_tilde)), hc_tilde) hp_tilde.length_in_time = tmplt_length hp_tilde.chirp_length = tChirp hc_tilde.length_in_time = tmplt_length hc_tilde.chirp_length = tChirp return hp_tilde, hc_tilde else: raise ValueError("Approximant %s not available" % (input_params['approximant']))
python
def get_two_pol_waveform_filter(outplus, outcross, template, **kwargs): n = len(outplus) # If we don't have an inclination column alpha3 might be used if not hasattr(template, 'inclination') and 'inclination' not in kwargs: if hasattr(template, 'alpha3'): kwargs['inclination'] = template.alpha3 input_params = props(template, **kwargs) if input_params['approximant'] in fd_approximants(_scheme.mgr.state): wav_gen = fd_wav[type(_scheme.mgr.state)] hp, hc = wav_gen[input_params['approximant']](**input_params) hp.resize(n) hc.resize(n) outplus[0:len(hp)] = hp[:] hp = FrequencySeries(outplus, delta_f=hp.delta_f, copy=False) outcross[0:len(hc)] = hc[:] hc = FrequencySeries(outcross, delta_f=hc.delta_f, copy=False) hp.chirp_length = get_waveform_filter_length_in_time(**input_params) hp.length_in_time = hp.chirp_length hc.chirp_length = hp.chirp_length hc.length_in_time = hp.length_in_time return hp, hc elif input_params['approximant'] in td_approximants(_scheme.mgr.state): # N: number of time samples required N = (n-1)*2 delta_f = 1.0 / (N * input_params['delta_t']) wav_gen = td_wav[type(_scheme.mgr.state)] hp, hc = wav_gen[input_params['approximant']](**input_params) # taper the time series hp if required if 'taper' in input_params.keys() and \ input_params['taper'] is not None: hp = wfutils.taper_timeseries(hp, input_params['taper'], return_lal=False) hc = wfutils.taper_timeseries(hc, input_params['taper'], return_lal=False) # total duration of the waveform tmplt_length = len(hp) * hp.delta_t # for IMR templates the zero of time is at max amplitude (merger) # thus the start time is minus the duration of the template from # lower frequency cutoff to merger, i.e. minus the 'chirp time' tChirp = - float( hp.start_time ) # conversion from LIGOTimeGPS hp.resize(N) hc.resize(N) k_zero = int(hp.start_time / hp.delta_t) hp.roll(k_zero) hc.roll(k_zero) hp_tilde = FrequencySeries(outplus, delta_f=delta_f, copy=False) hc_tilde = FrequencySeries(outcross, delta_f=delta_f, copy=False) fft(hp.astype(real_same_precision_as(hp_tilde)), hp_tilde) fft(hc.astype(real_same_precision_as(hc_tilde)), hc_tilde) hp_tilde.length_in_time = tmplt_length hp_tilde.chirp_length = tChirp hc_tilde.length_in_time = tmplt_length hc_tilde.chirp_length = tChirp return hp_tilde, hc_tilde else: raise ValueError("Approximant %s not available" % (input_params['approximant']))
[ "def", "get_two_pol_waveform_filter", "(", "outplus", ",", "outcross", ",", "template", ",", "*", "*", "kwargs", ")", ":", "n", "=", "len", "(", "outplus", ")", "# If we don't have an inclination column alpha3 might be used", "if", "not", "hasattr", "(", "template",...
Return a frequency domain waveform filter for the specified approximant. Unlike get_waveform_filter this function returns both h_plus and h_cross components of the waveform, which are needed for searches where h_plus and h_cross are not related by a simple phase shift.
[ "Return", "a", "frequency", "domain", "waveform", "filter", "for", "the", "specified", "approximant", ".", "Unlike", "get_waveform_filter", "this", "function", "returns", "both", "h_plus", "and", "h_cross", "components", "of", "the", "waveform", "which", "are", "n...
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/waveform/waveform.py#L951-L1015
227,690
gwastro/pycbc
pycbc/waveform/waveform.py
get_template_amplitude_norm
def get_template_amplitude_norm(template=None, **kwargs): """ Return additional constant template normalization. This only affects the effective distance calculation. Returns None for all templates with a physically meaningful amplitude. """ input_params = props(template,**kwargs) approximant = kwargs['approximant'] if approximant in _template_amplitude_norms: return _template_amplitude_norms[approximant](**input_params) else: return None
python
def get_template_amplitude_norm(template=None, **kwargs): input_params = props(template,**kwargs) approximant = kwargs['approximant'] if approximant in _template_amplitude_norms: return _template_amplitude_norms[approximant](**input_params) else: return None
[ "def", "get_template_amplitude_norm", "(", "template", "=", "None", ",", "*", "*", "kwargs", ")", ":", "input_params", "=", "props", "(", "template", ",", "*", "*", "kwargs", ")", "approximant", "=", "kwargs", "[", "'approximant'", "]", "if", "approximant", ...
Return additional constant template normalization. This only affects the effective distance calculation. Returns None for all templates with a physically meaningful amplitude.
[ "Return", "additional", "constant", "template", "normalization", ".", "This", "only", "affects", "the", "effective", "distance", "calculation", ".", "Returns", "None", "for", "all", "templates", "with", "a", "physically", "meaningful", "amplitude", "." ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/waveform/waveform.py#L1023-L1034
227,691
gwastro/pycbc
pycbc/waveform/waveform.py
get_waveform_filter_precondition
def get_waveform_filter_precondition(approximant, length, delta_f): """Return the data preconditioning factor for this approximant. """ if approximant in _filter_preconditions: return _filter_preconditions[approximant](length, delta_f) else: return None
python
def get_waveform_filter_precondition(approximant, length, delta_f): if approximant in _filter_preconditions: return _filter_preconditions[approximant](length, delta_f) else: return None
[ "def", "get_waveform_filter_precondition", "(", "approximant", ",", "length", ",", "delta_f", ")", ":", "if", "approximant", "in", "_filter_preconditions", ":", "return", "_filter_preconditions", "[", "approximant", "]", "(", "length", ",", "delta_f", ")", "else", ...
Return the data preconditioning factor for this approximant.
[ "Return", "the", "data", "preconditioning", "factor", "for", "this", "approximant", "." ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/waveform/waveform.py#L1036-L1042
227,692
gwastro/pycbc
pycbc/waveform/waveform.py
get_waveform_filter_norm
def get_waveform_filter_norm(approximant, psd, length, delta_f, f_lower): """ Return the normalization vector for the approximant """ if approximant in _filter_norms: return _filter_norms[approximant](psd, length, delta_f, f_lower) else: return None
python
def get_waveform_filter_norm(approximant, psd, length, delta_f, f_lower): if approximant in _filter_norms: return _filter_norms[approximant](psd, length, delta_f, f_lower) else: return None
[ "def", "get_waveform_filter_norm", "(", "approximant", ",", "psd", ",", "length", ",", "delta_f", ",", "f_lower", ")", ":", "if", "approximant", "in", "_filter_norms", ":", "return", "_filter_norms", "[", "approximant", "]", "(", "psd", ",", "length", ",", "...
Return the normalization vector for the approximant
[ "Return", "the", "normalization", "vector", "for", "the", "approximant" ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/waveform/waveform.py#L1044-L1050
227,693
gwastro/pycbc
pycbc/waveform/waveform.py
get_waveform_end_frequency
def get_waveform_end_frequency(template=None, **kwargs): """Return the stop frequency of a template """ input_params = props(template,**kwargs) approximant = kwargs['approximant'] if approximant in _filter_ends: return _filter_ends[approximant](**input_params) else: return None
python
def get_waveform_end_frequency(template=None, **kwargs): input_params = props(template,**kwargs) approximant = kwargs['approximant'] if approximant in _filter_ends: return _filter_ends[approximant](**input_params) else: return None
[ "def", "get_waveform_end_frequency", "(", "template", "=", "None", ",", "*", "*", "kwargs", ")", ":", "input_params", "=", "props", "(", "template", ",", "*", "*", "kwargs", ")", "approximant", "=", "kwargs", "[", "'approximant'", "]", "if", "approximant", ...
Return the stop frequency of a template
[ "Return", "the", "stop", "frequency", "of", "a", "template" ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/waveform/waveform.py#L1052-L1061
227,694
gwastro/pycbc
pycbc/waveform/waveform.py
get_waveform_filter_length_in_time
def get_waveform_filter_length_in_time(approximant, template=None, **kwargs): """For filter templates, return the length in time of the template. """ kwargs = props(template, **kwargs) if approximant in _filter_time_lengths: return _filter_time_lengths[approximant](**kwargs) else: return None
python
def get_waveform_filter_length_in_time(approximant, template=None, **kwargs): kwargs = props(template, **kwargs) if approximant in _filter_time_lengths: return _filter_time_lengths[approximant](**kwargs) else: return None
[ "def", "get_waveform_filter_length_in_time", "(", "approximant", ",", "template", "=", "None", ",", "*", "*", "kwargs", ")", ":", "kwargs", "=", "props", "(", "template", ",", "*", "*", "kwargs", ")", "if", "approximant", "in", "_filter_time_lengths", ":", "...
For filter templates, return the length in time of the template.
[ "For", "filter", "templates", "return", "the", "length", "in", "time", "of", "the", "template", "." ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/waveform/waveform.py#L1063-L1071
227,695
gwastro/pycbc
pycbc/inference/models/marginalized_gaussian_noise.py
MarginalizedGaussianNoise._extra_stats
def _extra_stats(self): """Adds ``loglr``, ``optimal_snrsq`` and matched filter snrsq in each detector to the default stats.""" return ['loglr'] + \ ['{}_optimal_snrsq'.format(det) for det in self._data] + \ ['{}_matchedfilter_snrsq'.format(det) for det in self._data]
python
def _extra_stats(self): return ['loglr'] + \ ['{}_optimal_snrsq'.format(det) for det in self._data] + \ ['{}_matchedfilter_snrsq'.format(det) for det in self._data]
[ "def", "_extra_stats", "(", "self", ")", ":", "return", "[", "'loglr'", "]", "+", "[", "'{}_optimal_snrsq'", ".", "format", "(", "det", ")", "for", "det", "in", "self", ".", "_data", "]", "+", "[", "'{}_matchedfilter_snrsq'", ".", "format", "(", "det", ...
Adds ``loglr``, ``optimal_snrsq`` and matched filter snrsq in each detector to the default stats.
[ "Adds", "loglr", "optimal_snrsq", "and", "matched", "filter", "snrsq", "in", "each", "detector", "to", "the", "default", "stats", "." ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/inference/models/marginalized_gaussian_noise.py#L402-L407
227,696
gwastro/pycbc
pycbc/inference/models/marginalized_gaussian_noise.py
MarginalizedGaussianNoise._margtime_mfsnr
def _margtime_mfsnr(template, data): """Returns a time series for the matched filter SNR assuming that the template and data have both been normalised and whitened. """ snr = matched_filter_core(template, data, h_norm=1, psd=None) hd_i = snr[0].numpy().real return hd_i
python
def _margtime_mfsnr(template, data): snr = matched_filter_core(template, data, h_norm=1, psd=None) hd_i = snr[0].numpy().real return hd_i
[ "def", "_margtime_mfsnr", "(", "template", ",", "data", ")", ":", "snr", "=", "matched_filter_core", "(", "template", ",", "data", ",", "h_norm", "=", "1", ",", "psd", "=", "None", ")", "hd_i", "=", "snr", "[", "0", "]", ".", "numpy", "(", ")", "."...
Returns a time series for the matched filter SNR assuming that the template and data have both been normalised and whitened.
[ "Returns", "a", "time", "series", "for", "the", "matched", "filter", "SNR", "assuming", "that", "the", "template", "and", "data", "have", "both", "been", "normalised", "and", "whitened", "." ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/inference/models/marginalized_gaussian_noise.py#L512-L518
227,697
gwastro/pycbc
pycbc/inference/models/marginalized_gaussian_noise.py
MarginalizedGaussianNoise._margtimedist_loglr
def _margtimedist_loglr(self, mf_snr, opt_snr): """Returns the log likelihood ratio marginalized over time and distance. """ logl = special.logsumexp(mf_snr, b=self._deltat) logl_marg = logl/self._dist_array opt_snr_marg = opt_snr/self._dist_array**2 return special.logsumexp(logl_marg - 0.5*opt_snr_marg, b=self._deltad*self.dist_prior)
python
def _margtimedist_loglr(self, mf_snr, opt_snr): logl = special.logsumexp(mf_snr, b=self._deltat) logl_marg = logl/self._dist_array opt_snr_marg = opt_snr/self._dist_array**2 return special.logsumexp(logl_marg - 0.5*opt_snr_marg, b=self._deltad*self.dist_prior)
[ "def", "_margtimedist_loglr", "(", "self", ",", "mf_snr", ",", "opt_snr", ")", ":", "logl", "=", "special", ".", "logsumexp", "(", "mf_snr", ",", "b", "=", "self", ".", "_deltat", ")", "logl_marg", "=", "logl", "/", "self", ".", "_dist_array", "opt_snr_m...
Returns the log likelihood ratio marginalized over time and distance.
[ "Returns", "the", "log", "likelihood", "ratio", "marginalized", "over", "time", "and", "distance", "." ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/inference/models/marginalized_gaussian_noise.py#L539-L547
227,698
gwastro/pycbc
pycbc/inference/models/marginalized_gaussian_noise.py
MarginalizedGaussianNoise._margtimephase_loglr
def _margtimephase_loglr(self, mf_snr, opt_snr): """Returns the log likelihood ratio marginalized over time and phase. """ return special.logsumexp(numpy.log(special.i0(mf_snr)), b=self._deltat) - 0.5*opt_snr
python
def _margtimephase_loglr(self, mf_snr, opt_snr): return special.logsumexp(numpy.log(special.i0(mf_snr)), b=self._deltat) - 0.5*opt_snr
[ "def", "_margtimephase_loglr", "(", "self", ",", "mf_snr", ",", "opt_snr", ")", ":", "return", "special", ".", "logsumexp", "(", "numpy", ".", "log", "(", "special", ".", "i0", "(", "mf_snr", ")", ")", ",", "b", "=", "self", ".", "_deltat", ")", "-",...
Returns the log likelihood ratio marginalized over time and phase.
[ "Returns", "the", "log", "likelihood", "ratio", "marginalized", "over", "time", "and", "phase", "." ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/inference/models/marginalized_gaussian_noise.py#L549-L553
227,699
gwastro/pycbc
pycbc/inference/models/marginalized_gaussian_noise.py
MarginalizedGaussianNoise._margdistphase_loglr
def _margdistphase_loglr(self, mf_snr, opt_snr): """Returns the log likelihood ratio marginalized over distance and phase. """ logl = numpy.log(special.i0(mf_snr)) logl_marg = logl/self._dist_array opt_snr_marg = opt_snr/self._dist_array**2 return special.logsumexp(logl_marg - 0.5*opt_snr_marg, b=self._deltad*self.dist_prior)
python
def _margdistphase_loglr(self, mf_snr, opt_snr): logl = numpy.log(special.i0(mf_snr)) logl_marg = logl/self._dist_array opt_snr_marg = opt_snr/self._dist_array**2 return special.logsumexp(logl_marg - 0.5*opt_snr_marg, b=self._deltad*self.dist_prior)
[ "def", "_margdistphase_loglr", "(", "self", ",", "mf_snr", ",", "opt_snr", ")", ":", "logl", "=", "numpy", ".", "log", "(", "special", ".", "i0", "(", "mf_snr", ")", ")", "logl_marg", "=", "logl", "/", "self", ".", "_dist_array", "opt_snr_marg", "=", "...
Returns the log likelihood ratio marginalized over distance and phase.
[ "Returns", "the", "log", "likelihood", "ratio", "marginalized", "over", "distance", "and", "phase", "." ]
7a64cdd104d263f1b6ea0b01e6841837d05a4cb3
https://github.com/gwastro/pycbc/blob/7a64cdd104d263f1b6ea0b01e6841837d05a4cb3/pycbc/inference/models/marginalized_gaussian_noise.py#L555-L563