13
|
1 # Copyright INRA (Institut National de la Recherche Agronomique)
|
|
2 # http://www.inra.fr
|
|
3 # http://urgi.versailles.inra.fr
|
|
4 #
|
|
5 # This software is governed by the CeCILL license under French law and
|
|
6 # abiding by the rules of distribution of free software. You can use,
|
|
7 # modify and/ or redistribute the software under the terms of the CeCILL
|
|
8 # license as circulated by CEA, CNRS and INRIA at the following URL
|
|
9 # "http://www.cecill.info".
|
|
10 #
|
|
11 # As a counterpart to the access to the source code and rights to copy,
|
|
12 # modify and redistribute granted by the license, users are provided only
|
|
13 # with a limited warranty and the software's author, the holder of the
|
|
14 # economic rights, and the successive licensors have only limited
|
|
15 # liability.
|
|
16 #
|
|
17 # In this respect, the user's attention is drawn to the risks associated
|
|
18 # with loading, using, modifying and/or developing or reproducing the
|
|
19 # software by the user in light of its specific status of free software,
|
|
20 # that may mean that it is complicated to manipulate, and that also
|
|
21 # therefore means that it is reserved for developers and experienced
|
|
22 # professionals having in-depth computer knowledge. Users are therefore
|
|
23 # encouraged to load and test the software's suitability as regards their
|
|
24 # requirements in conditions enabling the security of their systems and/or
|
|
25 # data to be ensured and, more generally, to use and operate it in the
|
|
26 # same conditions as regards security.
|
|
27 #
|
|
28 # The fact that you are presently reading this means that you have had
|
|
29 # knowledge of the CeCILL license and that you accept its terms.
|
|
30
|
|
31 from commons.core.LoggerFactory import LoggerFactory
|
|
32
|
|
33 LOG_DEPTH = "repet.commons.core.coord"
|
|
34
|
|
35 ## Splits a list of objects implementing getLength() based on a list of length thresholds
|
|
36 #
|
|
37 class SplitOnLength(object):
|
|
38
|
|
39 def __init__(self, lObjects, lThresholds, verbosity = 0):
|
|
40 self._log = LoggerFactory.createLogger("%s.%s" % (LOG_DEPTH, self.__class__.__name__), verbosity = verbosity)
|
|
41 self._lObjects = lObjects
|
|
42 self._lThresholds = lThresholds
|
|
43
|
|
44 ## Splits the list of objects over the list of thresholds.
|
|
45 #
|
|
46 # @return a list of lists (groups) of objects
|
|
47 #
|
|
48 def split(self):
|
|
49 lSplit = [self._lObjects]
|
|
50 doObjectsImplementGetLength = False not in set([hasattr(o, "getLength") for o in self._lObjects])
|
|
51
|
|
52 if not self._lObjects:
|
|
53 self._log.warning("Empty input objects list, no split.")
|
|
54 elif not doObjectsImplementGetLength:
|
|
55 self._log.warning("At least one object in the list does not implement getLength(), no split.")
|
|
56 elif not self._lThresholds:
|
|
57 self._log.warning("Empty input thresholds list, no split.")
|
|
58 elif not self._lThresholds == sorted(self._lThresholds):
|
|
59 self._log.warning("Input thresholds list isn't sorted, no split. (%s)" % self._lThresholds)
|
|
60 else:
|
|
61 lSplit = [[] for i in xrange(len(self._lThresholds) + 1)]
|
|
62
|
|
63 for obj in self._lObjects:
|
|
64 if obj.getLength() <= self._lThresholds[0]:
|
|
65 lSplit[0].append(obj)
|
|
66 elif self._lThresholds[-1] < obj.getLength():
|
|
67 lSplit[-1].append(obj)
|
|
68 else:
|
|
69 for i in range(0, len(self._lThresholds) - 1):
|
|
70 if self._lThresholds[i] < obj.getLength() <= self._lThresholds[i + 1]:
|
|
71 lSplit[i + 1].append(obj)
|
|
72 break
|
|
73 return lSplit
|