MCPcopy Create free account
hub / github.com/bgrimstad/splinter / DataTable

Class DataTable

python/splinter/datatable.py:13–79  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

11
12
13class DataTable:
14 def __init__(self, x_or_data, y=None):
15 self.__handle = None # Handle to instance in the library
16 self.__x_dim = None
17 self.__num_samples = 0 # Number of samples not yet transferred to back end
18 self.__samples = []
19
20 if is_string(x_or_data):
21 self.__handle = splinter._call(splinter._get_handle().splinter_datatable_load_init, get_c_string(x_or_data))
22 self.__x_dim = splinter._call(splinter._get_handle().splinter_datatable_get_num_variables, self.__handle)
23 else:
24 self.__handle = splinter._call(splinter._get_handle().splinter_datatable_init)
25
26 if y is None:
27 raise Exception("No y-values supplied.")
28
29 if len(x_or_data) != len(y):
30 raise Exception("x and y must be of the same length!")
31
32 # If x_or_data is not a string, we expect it to be lists of x values which has corresponding y values in 'y'.
33 for idx in range(len(x_or_data)):
34 self.add_sample(x_or_data[idx], y[idx])
35 # if x_or_data is not None:
36 # for data_row in x_or_data:
37 # self.add_sample(list(data_row[:-1]), data_row[-1])
38
39 # "Public" methods (for use by end user of the library)
40 def add_sample(self, x, y):
41 if not isinstance(x, list):
42 x = [x]
43
44 if self.__x_dim is None:
45 self.__x_dim = len(x)
46
47 if self.__x_dim != len(x):
48 raise Exception("Dimension of the new sample disagrees with the dimension of previous samples!\nPrevious: " + str(self.__x_dim) + ", new: " + str(len(x)))
49
50 self.__samples += list(x)
51 self.__samples += [y]
52 self.__num_samples += 1
53
54 def get_num_variables(self):
55 return self.__x_dim
56
57 def get_num_samples(self):
58 self.__transfer()
59 return splinter._call(splinter._get_handle().splinter_datatable_get_num_samples, self.__handle)
60
61 # Methods below are internal use only
62
63 # Transfer samples to the library
64 def __transfer(self):
65 #print("Transferring " + str(self.__numSamples) + " samples to backend:")
66 #for i in range(self.__numSamples):
67 # print(str(self.__samples[i*(self.__xDim+1)]) + "," + str(self.__samples[i*(self.__xDim+1)+1]) + " = " + str(self.__samples[i*(self.__xDim+1)+2]))
68
69 if self.__num_samples > 0:
70 splinter._call(splinter._get_handle().splinter_datatable_add_samples_row_major, self.__handle, (c_double * len(self.__samples))(*self.__samples), self.__num_samples, self.__x_dim)

Callers 1

__init__Method · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected