Coverage for HMpTy/htm/sets.py : 100%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/local/bin/python
2# encoding: utf-8
3"""
4*Given a list of coordinates and a crossmatch radius, split the list up into sets of associated locations*
6:Author:
7 David Young
8"""
9from builtins import zip
10from builtins import object
11import sys
12import os
13os.environ['TERM'] = 'vt100'
14from fundamentals import tools
15import numpy as np
17class sets(object):
18 """
19 *Given a list of coordinates and a crossmatch radius, split the list up into sets of associated locations*
21 **Key Arguments**
23 - ``log`` -- logger
24 - ``ra`` -- a list of the corrdinate right ascensions
25 - ``dec`` -- a list of the corrdinate declinations (same length as ``ra``)
26 - ``radius`` -- the radius to crossmatch the list of coordinates against itself (degrees)
27 - ``sourceList`` -- the list of source imformation to be divided into associated sets (same length as ``ra`` and ``dec``)
28 - ``convertToArray`` -- convert the coordinates into an array. Default *True*. Can bypass the conversion check if you are sure coordinates in numpy array
31 **Usage**
33 Given a list of transient metadata (any list, possibly a list of dictionaries) you can divide the list to assoicated sets of transients by running the following code:
35 ```python
36 from HMpTy.htm import sets
37 xmatcher = sets(
38 log=log,
39 ra=raList,
40 dec=decList,
41 radius=10 / (60. * 60.),
42 sourceList=transientList
43 )
44 allMatches = xmatcher.match
45 ```
47 ``raList`` and ``decList`` are the coordinates for the sources found in the ``transientList`` and are therefore the same length as the `transientList`` (it's up to the user to create these lists).
48 This code will group the sources into set of assocated transients which are within a radius of 10 arcsecs from one-another. ``allMatches`` is a list of lists, each contained list being an associate group of sources.
50 .. image:: https://i.imgur.com/hHExDqR.png
51 :width: 800px
52 :alt: divide a list of sources into associated sets
54 """
55 # Initialisation
57 def __init__(
58 self,
59 log,
60 ra,
61 dec,
62 radius,
63 sourceList,
64 convertToArray=True
65 ):
66 self.log = log
67 log.debug("instansiating a new 'sets' object")
68 self.ra = ra
69 self.dec = dec
70 self.radius = radius
71 self.sourceList = sourceList
72 self.convertToArray = convertToArray
73 # Initial Actions
75 return None
77 @property
78 def match(
79 self):
80 """*all of the assocaited sets of sources*
82 See the class for usage
83 """
85 return self._extract_all_sets_from_list()
87 def _extract_all_sets_from_list(
88 self):
89 """*Extract all of the sets from the list of coordinates*
91 **Return**
93 - ``allMatches`` -- a list of lists. All of the assocaited sets of sources
95 """
96 self.log.debug('starting the ``_extract_all_sets_from_list`` method')
98 from HMpTy import HTM
99 mesh = HTM(
100 depth=12,
101 log=self.log
102 )
104 matchIndices1, matchIndices2, seps = mesh.match(
105 ra1=self.ra,
106 dec1=self.dec,
107 ra2=self.ra,
108 dec2=self.dec,
109 radius=self.radius,
110 maxmatch=0, # 1 = match closest 1, 0 = match all,
111 convertToArray=self.convertToArray
112 )
114 anchorIndicies = []
115 childIndicies = []
116 allMatches = []
117 thisMatch = None
118 for m1, m2, s in zip(matchIndices1, matchIndices2, seps):
119 if m1 not in anchorIndicies and m1 not in childIndicies:
120 if thisMatch:
121 allMatches.append(thisMatch)
122 thisMatch = [self.sourceList[m1]]
123 anchorIndicies.append(m1)
124 if m2 not in anchorIndicies and m2 not in childIndicies:
125 childIndicies.append(m2)
126 thisMatch.append(self.sourceList[m2])
127 if thisMatch:
128 allMatches.append(thisMatch)
130 self.log.debug('completed the ``_extract_all_sets_from_list`` method')
131 return allMatches