[2fc8322] | 1 | """ |
---|
| 2 | This module implement a class that generates values given some inputs. The User apply a function to a sequence |
---|
| 3 | of inputs and outputs are generated. |
---|
| 4 | """ |
---|
| 5 | import numpy |
---|
| 6 | |
---|
| 7 | |
---|
| 8 | class DataTable(object): |
---|
| 9 | """ |
---|
| 10 | This class is store inputs as a dictionary of key (string) and values list of python objects. |
---|
| 11 | It generates outputs given some function and its current inputs. |
---|
| 12 | if an output object has an attribute "extract" function returning attributes names and values |
---|
| 13 | relevant to this output then DataTable will map them accordingly. |
---|
| 14 | """ |
---|
| 15 | def __init__(self, inputs): |
---|
| 16 | """ |
---|
| 17 | """ |
---|
| 18 | #array of inputs |
---|
| 19 | self.__inputs_data = None |
---|
| 20 | #array of outputs |
---|
| 21 | self.__outputs_data = None |
---|
| 22 | #contains combinations of inputs and outputs |
---|
| 23 | self__data = None |
---|
| 24 | #the selected map function |
---|
| 25 | self.__function = map |
---|
| 26 | #the number of row in the array |
---|
| 27 | self.length = 0 |
---|
| 28 | |
---|
| 29 | def set_inputs(self, inputs): |
---|
| 30 | """ |
---|
| 31 | Checks that inputs is a dictionary. |
---|
| 32 | Checks that all values are list of same length. |
---|
| 33 | Creates an array of dimension n x m and stores into self.__inputs_data |
---|
| 34 | :param inputs: a dictionary of string as keys and values (list) of same length |
---|
| 35 | :example: inputs= {"data":[1, 2], "temperature":[30, 40]} |
---|
| 36 | result = self.set_value(inputs) |
---|
| 37 | assert result == [ ["data" "temperature"] |
---|
| 38 | [1 30 ] |
---|
| 39 | [2 40 ]] |
---|
| 40 | """ |
---|
| 41 | |
---|
| 42 | def get_inputs(self): |
---|
| 43 | """ |
---|
| 44 | return array of inputs |
---|
| 45 | """ |
---|
| 46 | def get_outputs(self): |
---|
| 47 | """ |
---|
| 48 | return array of output value |
---|
| 49 | """ |
---|
| 50 | def get_value(self): |
---|
| 51 | """ |
---|
| 52 | Return current saved inputs and outputs |
---|
| 53 | """ |
---|
| 54 | return self.__data |
---|
| 55 | |
---|
| 56 | def _mapply(self, arguments): |
---|
| 57 | """ |
---|
| 58 | Receive a list of arguments where the fist item in the list is a function pointer |
---|
| 59 | and the rest of items are argument to the function |
---|
| 60 | :param arguments: arguments[0] is a function pointer |
---|
| 61 | arguments[1:] is possible parameters of that function |
---|
| 62 | """ |
---|
| 63 | return apply(arguments[0], arguments[1:]) |
---|
| 64 | |
---|
| 65 | def _compute(self, instance, func_name, *args): |
---|
| 66 | """ |
---|
| 67 | Receive an instance of a class , a function name and some possible arguments for the function |
---|
| 68 | Generate result from that function |
---|
| 69 | """ |
---|
| 70 | return getattr(instance, func)(*args) |
---|
| 71 | |
---|
| 72 | def compute(self,instances, func_names): |
---|
| 73 | """ |
---|
| 74 | compute a series of values , the set the value of self.__outputs_data and self.__data |
---|
| 75 | :param instances: list of instance of object |
---|
| 76 | :param func_names: name of the function to use for mapping |
---|
| 77 | |
---|
| 78 | """ |
---|
| 79 | |
---|
| 80 | def _extract_output(self, output): |
---|
| 81 | """ |
---|
| 82 | receive an output which is a python object request extract_output function |
---|
| 83 | in order to extract additional information about the output. If the output does |
---|
| 84 | not have extract_output has attribute, the output is return as it is else |
---|
| 85 | a dictionary of output attribute names is return as well as their corresponding |
---|
| 86 | values |
---|
| 87 | :example: class OutPut: |
---|
| 88 | a = 2 |
---|
| 89 | b = 3 |
---|
| 90 | out = OutPut() |
---|
| 91 | result = self.extract_output(out) |
---|
| 92 | assert result == {"a":2, "b":3} |
---|
| 93 | """ |
---|
| 94 | |
---|
| 95 | def select_map_function(self, mapper="default"): |
---|
| 96 | """ |
---|
| 97 | given a key word defined [mapper] a map function is used to map object |
---|
| 98 | available map functions are: built-in python map , processing map etc... |
---|
| 99 | """ |
---|
| 100 | |
---|
| 101 | class DataProcessor(object): |
---|
| 102 | """ |
---|
| 103 | Implement a singleton of DataTable |
---|
| 104 | """ |
---|
| 105 | __data_table = DataTable() |
---|
| 106 | |
---|
| 107 | def set_inputs(self, inputs): |
---|
| 108 | self.__data_table.set_inputs(inputs) |
---|
| 109 | |
---|
| 110 | def select_map_function(self, mapper="default"): |
---|
| 111 | self.__data_table.select_map_function(mapper) |
---|
| 112 | |
---|
| 113 | def compute(self,instance, func_name): |
---|
| 114 | return self.__data_table.compute(instance, func_name) |
---|
| 115 | |
---|
| 116 | def get_inputs(self): |
---|
| 117 | """ |
---|
| 118 | return array of inputs |
---|
| 119 | """ |
---|
| 120 | return self.__data_table.get_inputs() |
---|
| 121 | |
---|
| 122 | def get_outputs(self): |
---|
| 123 | """ |
---|
| 124 | return array of output value |
---|
| 125 | """ |
---|
| 126 | return self.__data_table.get_outputs() |
---|
| 127 | |
---|
| 128 | def get_value(self): |
---|
| 129 | """ |
---|
| 130 | Return current saved inputs and outputs |
---|
| 131 | """ |
---|
| 132 | return self.__data_table.get_value() |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | |
---|
| 136 | |
---|
| 137 | if __name__ == "__main__": |
---|
| 138 | a = numpy.array([['c','d']]) |
---|
| 139 | c = numpy.array([2,3]) |
---|
| 140 | d = numpy.array([4,5]) |
---|
| 141 | m = numpy.column_stack([c, d]) |
---|
| 142 | print numpy.concatenate((a, m)) |
---|
| 143 | # the way set_value will work |
---|
| 144 | my_dict = {"c":[2,3], "d":[4, 5]} |
---|
| 145 | if my_dict: |
---|
| 146 | length = len(my_dict.values()[0]) |
---|
| 147 | index = [] |
---|
| 148 | for item in my_dict.values(): |
---|
| 149 | assert len(item) == length |
---|
| 150 | data = numpy.concatenate((numpy.array([my_dict.keys()]), numpy.column_stack( my_dict.values()))) |
---|
| 151 | print "Data" |
---|
| 152 | print data |
---|
| 153 | print data[1:] |
---|
| 154 | print zip(data[1:1].T) |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | |
---|
| 158 | |
---|
| 159 | |
---|
| 160 | |
---|