Coverage for python/lsst/sims/utils/CodeUtilities.py : 7%

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
1from builtins import zip
2import numpy as np
3import numbers
4import gc
7def sims_clean_up():
8 """
9 This method will clean up data caches created by the sims software stack.
10 Any time a cache is added to the sims software stack, it can be added to
11 the list sims_clean_up.targets. When sims_clean_up() is called, it will
12 loop through the contents of sims_clean_up.targets. It will call pop()
13 on all of the contents of each sims_clean_up.target, run close() on each
14 item it pops (if applicable), delete each item it pops, and then reset
15 each sims_clean_up.target to either a blank dict or list (depending on
16 what the target was). sims_clean_up() will then run the garbage
17 collector.
19 Note: if a target cache is not a dict or a list, it will attempt to call
20 close() on the cache and delete the cache.
21 """
23 if not hasattr(sims_clean_up, 'targets'):
24 return None
26 for target in sims_clean_up.targets:
27 if isinstance(target, dict):
28 while len(target) > 0:
29 obj = target.popitem()
30 if hasattr(obj[1], 'close'):
31 try:
32 obj[1].close()
33 except:
34 pass
35 del obj
36 elif isinstance(target, list):
37 while len(target) > 0:
38 obj = target.pop()
39 if hasattr(obj, 'close'):
40 try:
41 obj.close()
42 except:
43 pass
44 del obj
45 else:
46 if hasattr(target, 'close'):
47 target.close()
48 del target
50 gc.collect()
51 return None
53sims_clean_up.targets = []
56def _validate_inputs(input_list, input_names, method_name):
57 """
58 This method will validate the inputs of other methods.
60 input_list is a list of the inputs passed to a method.
62 input_name is a list of the variable names associated with
63 input_list
65 method_name is the name of the method whose input is being validated.
67 _validate_inputs will verify that all of the inputs in input_list are:
69 1) of the same type
70 2) either numpy arrays or instances of numbers.Number (floats or ints)
71 3) if they are numpy arrays, they all have the same length
73 If any of these criteria are violated, a RuntimeError will be raised
75 returns True if the inputs are numpy arrays; False if not
76 """
78 if isinstance(input_list[0], np.ndarray):
79 desired_type = np.ndarray
80 elif isinstance(input_list[0], numbers.Number):
81 desired_type = numbers.Number
82 else:
83 raise RuntimeError("The arg %s input to method %s " % (input_names[0], method_name) +
84 "should be either a number or a numpy array")
86 valid_type = True
87 bad_names = []
88 for ii, nn in zip(input_list, input_names):
89 if not isinstance(ii, desired_type):
90 valid_type = False
91 bad_names.append(nn)
93 if not valid_type:
94 msg = "The input arguments:\n"
95 for nn in bad_names:
96 msg += "%s,\n" % nn
97 msg += "passed to %s " % method_name
98 msg += "need to be either numbers or numpy arrays\n"
99 msg += "and the same type as the argument %s" % input_names[0]
100 msg += "\n\nTypes of arguments are:\n"
101 for name, arg in zip(input_names, input_list):
102 msg += '%s: %s\n' % (name, type(arg))
103 raise RuntimeError(msg)
105 if desired_type is np.ndarray:
106 same_length = True
107 for ii in input_list:
108 if len(ii) != len(input_list[0]):
109 same_length = False
110 if not same_length:
111 raise RuntimeError("The arrays input to %s " % method_name +
112 "all need to have the same length")
114 if desired_type is np.ndarray:
115 return True
117 return False