def gen_data_str(a, dat_changes, init_c, path, anom_type, seed = 0): """ Function to genrte data array structure to hold ground truths and pointers to data files """ # May wish to change pA if 'pA' in dat_changes: pA_ = np.max(dat_changes['pA']) else: pA_ = a['pA'] # Changing N will also affect numAnom if 'N' in dat_changes: N_ = np.max(dat_changes['N']) else: N_ = a['N'] # Get (max) num of Anomalies if pA_ < 1: numAnom = np.floor(pA_ * N_) else: numAnom = pA_ # Double anomalous entries if 2 anomalies per run if anom_type == 'grad_persist' or anom_type == 'step_n_back': numAnom *= 2 # Data info Data type dt = ([('file', 'a24'), ('gt',[('start', np.int_, (numAnom,)), ('loc', np.int_, (numAnom,)), ('len', np.int_, (numAnom,)), ('mag', np.int_, (numAnom,)), ('type', 'a10', (numAnom,) )] ), ]) # Big Results Structure Array D = np.zeros((init_c, len(dat_changes.values()[0]) ), dtype = dt) # For each Change to Dataset Parameters dat_var = dat_changes.keys()[0] dat_values = dat_changes.values()[0] for j, v in enumerate(dat_values): # update data set parameters a a[dat_var] = v for i in xrange(init_c): if seed == 1: a['seed'] = i filename = 'D_' + dat_var + '=' + str(v) + '_' + str(i) + '.npy' # Generate the data temp = gen_funcs[anomaly_type](**a.copy()) # so tidy! # Write to file np.save(path + '/' + filename, temp['data']) # fill in D str nprec.recursive_fill_fields(temp['gt'], D[i,j]['gt']) D[i,j]['file'] = filename return D
def test_simple_flexible(self): # Test recursive_fill_fields on flexible-array a = np.array([(1, 10.0), (2, 20.0)], dtype=[("A", int), ("B", float)]) b = np.zeros((3,), dtype=a.dtype) test = recursive_fill_fields(a, b) control = np.array([(1, 10.0), (2, 20.0), (0, 0.0)], dtype=[("A", int), ("B", float)]) assert_equal(test, control)
def test_simple_flexible(self): # Test recursive_fill_fields on flexible-array a = np.array([(1, 10.), (2, 20.)], dtype=[('A', int), ('B', float)]) b = np.zeros((3, ), dtype=a.dtype) test = recursive_fill_fields(a, b) control = np.array([(1, 10.), (2, 20.), (0, 0.)], dtype=[('A', int), ('B', float)]) assert_equal(test, control)
def test_masked_flexible(self): # Test recursive_fill_fields on masked flexible-array a = ma.array([(1, 10.0), (2, 20.0)], mask=[(0, 1), (1, 0)], dtype=[("A", int), ("B", float)]) b = ma.zeros((3,), dtype=a.dtype) test = recursive_fill_fields(a, b) control = ma.array( [(1, 10.0), (2, 20.0), (0, 0.0)], mask=[(0, 1), (1, 0), (0, 0)], dtype=[("A", int), ("B", float)] ) assert_equal(test, control)
def test_masked_flexible(self): # Test recursive_fill_fields on masked flexible-array a = ma.array([(1, 10.), (2, 20.)], mask=[(0, 1), (1, 0)], dtype=[('A', int), ('B', float)]) b = ma.zeros((3,), dtype=a.dtype) test = recursive_fill_fields(a, b) control = ma.array([(1, 10.), (2, 20.), (0, 0.)], mask=[(0, 1), (1, 0), (0, 0)], dtype=[('A', int), ('B', float)]) assert_equal(test, control)