merlin.functions¶
functions.py is a module of generalized, reusable functions
-
merlin.functions.
bounds_to_coordinates
(bounds, grid, cfg)[source]¶ Returns coordinates from a sequence of bounds. Performs minbox operation on bounds, thus irregular geometries may be supplied.
Parameters: - bounds – a sequence of bounds.
- grid (dict) – {‘name’: ‘chip’, ‘sx’: SCALE_FACTOR, ‘sy’: SCALE_FACTOR, ‘rx’: 1, ‘ry’: -1}
- cfg (dict) – {‘snap_fn’: some_func}
Returns: chip coordinates
Return type: tuple
Example
>>> xys = bounds_to_coordinates( bounds=((112, 443), (112, 500), (100, 443)), grid={'sx': 3000, 'sy': 3000}, cfg={'snap_fn': some_func}) >>> ((100, 500),)
-
merlin.functions.
chexists
(dictionary, keys, check_fn)[source]¶ applies check_fn against dictionary minus keys then ensures the items returned from check_fn exist in dictionary[keys]
Parameters: - dictionary (dict) – {key: [v1, v3, v2]}
- keys (sequence) – A sequence of keys in dictionary
- check_fn (function) – Function that accepts dict and returns sequence of items or Exception
Returns: A sequence of items that are returned from check_fn and exist in dictionary[keys] or Exception
-
merlin.functions.
coordinates
(ulx, uly, lrx, lry, grid, cfg)[source]¶ Returns all the coordinates that are needed to cover a supplied bounding box.
Parameters: - ulx (float) – upper left x
- uly (float) – upper left y
- lrx (float) – lower right x
- lry (float) – lower right y
- grid (dict) – {‘name’: ‘chip’, ‘sx’: 3000, ‘sy’: 3000, ‘rx’: 1, ‘ry’: -1}
- cfg (dict) – A Merlin configuration
Returns: tuple of tuples of chip coordinates ((x1,y1), (x2,y1) …)
Return type: tuple
This example assumes chip sizes of 500 pixels.
Example
>>> coordinates = coordinates(ulx=-1000, uly=1000, lrx=-500, lry=500, grid={'name': 'chip', 'sx': 500, 'sy': 500, 'rx': 1, 'ry': -1}, cfg={'snap_fn': some_func})
((-1000, 500), (-500, 500), (-1000, -500), (-500, -500))
-
merlin.functions.
cqlstr
(string)[source]¶ Makes a string safe to use in Cassandra CQL commands
Parameters: string – The string to use in CQL Returns: A safe string replacement Return type: str
-
merlin.functions.
deserialize
(string)[source]¶ Reconstitues datastructure from a string.
Parameters: string – A serialized data structure Returns: Data structure represented by string parameter
-
merlin.functions.
difference
(a, b)[source]¶ Subtracts items in b from items in a.
Parameters: - a – sequence a
- b – sequence b
Returns: items that exist in a but not b
Return type: set
-
merlin.functions.
extract
(sequence, elements)[source]¶ Given a sequence (possibly with nested sequences), extract the element identifed by the elements sequence.
Parameters: - sequence – A sequence of elements which may be other sequences
- elements – Sequence of nested element indicies (in sequence parameter) to extract
Returns: The target element
Example
>>> inputs = [1, (2, 3, (4, 5)), 6] >>> extract(inputs, [0]) >>> 1 >>> extract(inputs, [1]) >>> (2, 3, (4, 5)) >>> extract(inputs, [1, 0]) >>> 2 >>> extract(inputs, [1, 1]) >>> 3 >>> extract(inputs, [1, 2]) >>> (4, 5) >>> extract(inputs, [1, 2, 0]) >>> 4
…
-
merlin.functions.
flatten
(iterable)[source]¶ Reduce dimensionality of iterable containing iterables
Parameters: iterable – A multi-dimensional iterable Returns: A one dimensional iterable
-
merlin.functions.
flip_keys
(dods)[source]¶ Accepts a dictionary of dictionaries and flips the outer and inner keys. All inner dictionaries must have a consistent set of keys or key Exception is raised.
Parameters: dods – dict of dicts Returns: dict of dicts with inner and outer keys flipped Example
>>> dods = {"reds": {(0, 0): [110, 110, 234, 664], (0, 1): [23, 887, 110, 111]}, "greens": {(0, 0): [120, 112, 224, 624], (0, 1): [33, 387, 310, 511]}, "blues": {(0, 0): [128, 412, 244, 654], (0, 1): [73, 987, 119, 191]}, >>> flip_keys(dods) {(0, 0): {"reds": [110, 110, 234, 664], "greens": [120, 112, 224, 624], "blues": [128, 412, 244, 654], ... }, (0, 1), {"reds": [23, 887, 110, 111], "greens": [33, 387, 310, 511], "blues": [73, 987, 119, 191], ... }}
-
merlin.functions.
insert_into_every
(dods, key, value)[source]¶ Insert key:values into every subdictionary of dods.
Parameters: - dods – dictionary of dictionaries
- key – key to hold values in subdictionaires
- value – value to associate with key
Returns: dictionary of dictionaries with key:values inserted into each
Return type: dict
-
merlin.functions.
intersection
(items)[source]¶ Returns the intersecting set contained in items
Parameters: items – Two dimensional sequence of items Returns: Intersecting set of items Example
>>> items = [[1, 2, 3], [2, 3, 4], [3, 4, 5]] >>> intersection(items) {3}
-
merlin.functions.
isnumeric
(value)[source]¶ Does a string value represent a number (positive or negative?)
Parameters: value (str) – A string Returns: True or False Return type: bool
-
merlin.functions.
issubset
(a, b)[source]¶ Determines if a exists in b
Parameters: - a – sequence a
- b – sequence b
Returns: True or False
Return type: bool
-
merlin.functions.
md5
(string)[source]¶ Computes and returns an md5 digest of the supplied string
Parameters: string – string to digest Returns: digest value
-
merlin.functions.
minbox
[source]¶ Returns the minimal bounding box necessary to contain points
Parameters: points (tuple) – (x,y) points: ((0,0), (40, 55), (66, 22)) Returns: ulx, uly, lrx, lry Return type: dict
-
merlin.functions.
represent
(item)[source]¶ Represents callables and values consistently
Parameters: item – The item to represent Returns: Item representation
-
merlin.functions.
seqeq
(a, b)[source]¶ Determine if two unordered sequences are equal.
Parameters: - a – sequence a
- b – sequence b
Returns: True or False
Return type: bool
-
merlin.functions.
serialize
(arg)[source]¶ Converts datastructure to json, computes digest
Parameters: dictionary – A python dict Returns: (digest,json) Return type: tuple