def __init__(self, matrix=None, source=''): if source: self.fromFile(source) return if matrix is None: return assert is_array(matrix) k = len(matrix) assert k assert (is_array(matrix[i]) for i in range(k)) classes = len(matrix[0]) assert classes assert (len(matrix[i]) == classes for i in range(k)) assert (is_scalar(matrix[i][j]) for i in range(k) for j in range(classes)) self.classes = classes self.k = k self.content = [[int(matrix[i][j]) for j in range(classes)] for i in range(k)] self.nbits = [] for j in range(k): m = 1 for x in self.content[j]: if abs(x) > m: m = abs(x) self.nbits.append(int(log2(m)) + 1)
def __init__(self, matrix=None, source=''): if source: self.fromFile(source) return if matrix is None: return assert is_array(matrix) n = len(matrix) assert n > 0 assert (is_array(matrix[i]) for i in range(n)) k = len(matrix[0]) assert k > 0 assert (len(matrix[i]) == k for i in range(n)) assert (is_scalar(matrix[i][j]) for i in range(n) for j in range(k)) self.n = n self.k = k self.columns = [[int(matrix[i][j]) for j in range(k)] for i in range(n)] self.nbits = [] for j in range(n): m = 1 for x in self.columns[j]: if abs(x) > m: m = abs(x) self.nbits.append(int(log2(m)) + 1)
def __init__(self, group=None, simplifier=None, left=None, right=None, source=''): if source: self.fromFile(source) return assert left assert right assert group assert simplifier assert is_array(left) assert is_array(right) assert len(left) == len( right), 'Ciphertext was not properly generated.' assert len(left) > 1, 'Ciphertext is empty.' assert (is_array(x) for x in left) assert (is_array(x) for x in right) assert (len(x) == 2 for x in left) assert (len(x) == 2 for x in right) self.n = len(left) self.group = group self.simplifier = simplifier self.left = left self.right = right
def detect(ts_data, ts_timestamp): ts_data = is_array(ts_data) ts_timestamp = is_array(ts_timestamp) ts_data_ = ts_data - np.mean(ts_data) lag, acf = interpolated_acf(ts_timestamp, ts_data_) # TODO: 当使用csv_test.csv时,出现detected_period为non的情况 detected_period = dominant_period(lag, acf, plot=False) interval = int(get_interval(ts_timestamp)) return int(detected_period // interval)
def __init__(self, group=None, h1=None, h2=None, source=''): if source: self.fromFile(source) return assert group assert h1 assert is_array(h1) assert is_array(h2) assert len(h1) == len(h2) self.group = group self.n = len(h1) self.h1 = h1 self.h2 = h2
def __init__(self, pk=None, s=None, t=None, source=''): if source: self.fromFile(source) return super(MasterKey, self).__init__(pk.group, pk.h1, pk.h2) assert s assert is_array(s) assert len(s) == len(pk.h1) assert t assert is_array(t) assert len(t) == len(pk.h1) self.s = s self.t = t
def __mul__(self, X): if isinstance(X, EncryptedVector): left = [[1, 1] for i in range(self.k)] right = [[1, 1] for i in range(self.k)] for j in range(self.n): l1 = batch_exp(X.left[j][0], self.columns[j], self.nbits[j]) l2 = batch_exp(X.left[j][1], self.columns[j], self.nbits[j]) r1 = batch_exp(X.right[j][0], self.columns[j], self.nbits[j]) r2 = batch_exp(X.right[j][1], self.columns[j], self.nbits[j]) for i in range(self.k): left[i][0] *= l1[i] left[i][1] *= l2[i] right[i][0] *= r1[i] right[i][1] *= r2[i] return EncryptedVector( group=X.group, simplifier=X.simplifier, left=left, right=right, ) elif is_array(X): import numpy as np assert self.n == len(X) return np.dot(np.transpose(self.columns), X) else: raise WrongInputError(X)
def __init__(self, model=None, skf=None, source=''): if source: self.fromFile(source) return assert model assert skf assert isinstance(model, MLModel) assert is_array(skf) classes = len(skf) assert classes self.classes = classes self.model = model self.skf = skf
def evaluate(self, X): if isinstance(X, EncryptedVector): return (self.proj * X) ** self.forms elif isinstance(X, MasterKey): Ps = self.proj * X.s Pt = self.proj * X.t element_wise_prod = np.multiply(Ps, Pt) return np.dot(np.transpose(self.forms.content), element_wise_prod).tolist() elif is_array(X): PXsquared = np.square(self.proj * X) return np.dot(np.transpose(self.forms.content), PXsquared) else: raise WrongInputError(X)
def __init__(self, array=None, source=''): if source: self.fromFile(source) return if is_array(array): assert len( array) > 0, 'Trying to generate an image from an empty vector.' self.n = len(array) self.content = [] for s in array: assert is_scalar(s), "Input doesn't contain valid scalars." self.content.append(int(s)) else: WrongInputError(array)
def set_ts_label_array(self, ts_label_array): if ts_label_array is None: self.ts_label_array = None else: ts_label_array = is_array(ts_label_array) self.ts_label_array = ts_label_array.copy()
def set_ts_timestamp_array(self, ts_timestamp_array): ts_timestamp_array = is_array(ts_timestamp_array) self.ts_timestamp_array = ts_timestamp_array.copy()
def set_ts_data_array(self, ts_data_array): ts_data_array = is_array(ts_data_array) self.ts_data_array = ts_data_array.copy()