def merge_physical_data(x1, x2, y1, y2): # Create the variable if they don't exist yet try: x1 except NameError: x1 = np.array([]) try: y1 except NameError: y1 = np.array([]) # Change datatype in given arrays in case there are ints or stuff x1 = np.asarray(x1, dtype=float) x2 = np.asarray(x2, dtype=float) y1 = np.asarray(y1, dtype=float) y2 = np.asarray(y2, dtype=float) # The merge and sort action for x merged_x, ind = snp.merge(x1, x2, indices=True) # The merge and sort action for y ind_1 = ind[0] ind_2 = ind[1] merged_y = np.zeros(len(merged_x)) count_1 = 0 count_2 = 0 for i in range(0, len(merged_x)): if i in ind_1: merged_y[i] = y1[count_1] count_1 += 1 elif i in ind_2: merged_y[i] = y2[count_2] count_2 += 1 return merged_x, merged_y
def quasi_clique_expansions(quasi_cliques, cliques, incomp): max_expansions = set() temp_expansions = set([tuple(q) for q in quasi_cliques]) expanded = set() valid = dict() while len(temp_expansions) > 0: current = temp_expansions.pop() expanded.add(current) new_expansions = set() #partition cliques into compatible and incompatible? #TODO NOT NECESSARY TO DO ALL!! ONLY CLIQUES THAT WERE NOT TRIED YET! #keep index path for each in temp ([qc[i], c[j], c[k], ....]) #keep compatibility set for each in temp #---------- #make pairwise comp graph for cs. possible combinations are max cliques. #for each qc calculate comp with each c and take subgraph unions = [tuple(snp.merge(np.array(current), c, duplicates=snp.DROP)) for c in cliques] [validate_quasi_clique(u, valid, incomp) for u in unions] new_expansions = [u for u in unions if valid[u] and len(u) > len(current)] #== len(current)+1 #append and remove all sub-quasi-cliques if len(new_expansions) == 0: max_expansions.add(current) new_expansions = [e for e in new_expansions if e not in expanded] temp_expansions.update(new_expansions) #temp_expansions = remove_subarrays(temp_expansions)#<--------------- print(len(max_expansions), len(expanded), len(valid)) max_expansions = [np.array(e) for e in max_expansions] #max_expansions = remove_subarrays(max_expansions)#<--------------- #print(len(max_expansions)) return max_expansions if len(max_expansions) > 1 else []
def get_dtype(self): return 'uint8' a = np.array([2, 255], dtype=self.get_dtype()) b = np.array([0, 4], dtype=self.get_dtype()) m = snp.merge(a, b) self.assertEqual(list(m), [0, 2, 4, 255]) self.assertEqual(m.dtype, self.get_dtype())
def test_reference_counting(self): """ Check that the reference counting is done correctly. """ # Create inputs a = np.arange(10, dtype=self.get_dtype()) * 3 b = np.arange(10, dtype=self.get_dtype()) * 2 + 5 # Check ref count for input. Numpy arrays have two references. self.assertEqual(sys.getrefcount(a), 2) self.assertEqual(sys.getrefcount(b), 2) # Create weak refs for inputs weak_a = weakref.ref(a) weak_b = weakref.ref(b) self.assertEqual(sys.getrefcount(a), 2) self.assertEqual(sys.getrefcount(b), 2) self.assertIsNotNone(weak_a()) self.assertIsNotNone(weak_b()) ## Intersect m = snp.merge(a, b) self.assertEqual(sys.getrefcount(a), 2) self.assertEqual(sys.getrefcount(b), 2) self.assertEqual(sys.getrefcount(m), 2) # Create weakref for m weak_m = weakref.ref(m) self.assertEqual(sys.getrefcount(a), 2) self.assertEqual(sys.getrefcount(b), 2) self.assertEqual(sys.getrefcount(m), 2) self.assertIsNotNone(weak_a()) self.assertIsNotNone(weak_b()) self.assertIsNotNone(weak_m()) # Delete a del a self.assertEqual(sys.getrefcount(b), 2) self.assertEqual(sys.getrefcount(m), 2) self.assertIsNone(weak_a()) self.assertIsNotNone(weak_b()) self.assertIsNotNone(weak_m()) # Delete b del b self.assertEqual(sys.getrefcount(m), 2) self.assertIsNone(weak_a()) self.assertIsNone(weak_b()) self.assertIsNotNone(weak_m()) # Delete m del m self.assertIsNone(weak_a()) self.assertIsNone(weak_b()) self.assertIsNone(weak_m())
def test_identical(self): """ Check that merging two identical arrays returns each element twice. """ a = np.array([1, 3, 7], dtype=self.get_dtype()) m = snp.merge(a, a) self.assertEqual(list(m), [1, 1, 3, 3, 7, 7]) self.assertEqual(m.dtype, self.get_dtype())
def test_duplicates_other(self): """ Check that duplications in the other array are passed to the result. """ a = np.array([1, 3, 7], dtype=self.get_dtype()) b = np.array([2, 3, 5, 6], dtype=self.get_dtype()) m = snp.merge(a, b) self.assertEqual(list(m), [1, 2, 3, 3, 5, 6, 7]) self.assertEqual(m.dtype, self.get_dtype())
def test_type_limites(self): """ Ensure that merging works with numbers specific to this data type. """ a = np.array([2, 4294967295], dtype=self.get_dtype()) b = np.array([0, 4], dtype=self.get_dtype()) m = snp.merge(a, b) self.assertEqual(list(m), [0, 2, 4, 4294967295]) self.assertEqual(m.dtype, self.get_dtype())
def test_type_limites(self): """ Ensure that merging works with numbers specific to this data type. """ a = np.array([-1.3e300, -1.2e300, -2.3e-200], dtype=self.get_dtype()) b = np.array([-1.1e300, 3.14e20], dtype=self.get_dtype()) m = snp.merge(a, b) self.assertListAlmostEqual( list(m), [-1.3e300, -1.2e300, -1.1e300, -2.3e-200, 3.14e20]) self.assertEqual(m.dtype, self.get_dtype())
def test_empty_both(self): """ Check that merging two empty arrays returns an empty array. """ a = np.array([], dtype=self.get_dtype()) b = np.array([], dtype=self.get_dtype()) m = snp.merge(a, b) self.assertEqual(list(m), []) self.assertEqual(len(m), 0) self.assertEqual(m.dtype, self.get_dtype())
def test_simple(self): """ Check that merging two non-empty arrays returns the union of the two arrays. """ a = np.array([1, 3, 7], dtype=self.get_dtype()) b = np.array([2, 5, 6], dtype=self.get_dtype()) m = snp.merge(a, b) self.assertEqual(list(m), [1, 2, 3, 5, 6, 7]) self.assertEqual(m.dtype, self.get_dtype())
def get_dtype(self): return 'uint64' """ Ensure that merging works with numbers specific to this data type. """ a = np.array([2, 18446744073709551615], dtype=self.get_dtype()) b = np.array([0, 4], dtype=self.get_dtype()) m = snp.merge(a, b) self.assertEqual(list(m), [0, 2, 4, 18446744073709551615]) self.assertEqual(m.dtype, self.get_dtype())
def test_type_limites(self): """ Ensure that merging works with numbers specific to this data type. """ a = np.array([2, 9223372036854775807], dtype=self.get_dtype()) b = np.array([-9223372036854775807, 4], dtype=self.get_dtype()) m = snp.merge(a, b) self.assertEqual(list(m), [-9223372036854775807, 2, 4, 9223372036854775807]) self.assertEqual(m.dtype, self.get_dtype())
def test_empty_single(self): """ Check that merging two arrays returns a copy of the first one if the other is empty. """ a = np.array([1, 3, 7], dtype=self.get_dtype()) b = np.array([], dtype=self.get_dtype()) m = snp.merge(a, b) self.assertEqual(list(m), [1, 3, 7]) self.assertEqual(list(a), [1, 3, 7]) self.assertEqual(m.dtype, self.get_dtype()) m[0] = 0 self.assertEqual(list(a), [1, 3, 7]) m = snp.merge(b, a) self.assertEqual(list(m), [1, 3, 7]) self.assertEqual(list(a), [1, 3, 7]) self.assertEqual(m.dtype, self.get_dtype()) m[0] = 0 self.assertEqual(list(a), [1, 3, 7])
def test_separated(self): """ Check that merging two non-empty arrays returns the union of the two arrays if all element in on array are greater than all elements in the other. This tests the copy parts of the implementation. """ a = np.array([1, 3, 7], dtype=self.get_dtype()) b = np.array([9, 10, 16], dtype=self.get_dtype()) m = snp.merge(a, b) self.assertEqual(list(m), [1, 3, 7, 9, 10, 16]) self.assertEqual(m.dtype, self.get_dtype())
def remove_dense_areas(patterns, min_dist=1): translations = np.concatenate([p.t for p in patterns]) unique, counts = np.unique(translations, return_counts=True) freqs = dict(zip(unique, counts)) #keep only most common vector in dense areas within pattern for p in patterns: p.t = filter_out_dense_infreq(p.t, min_dist, freqs) #delete occurrences in dense areas between patterns for i, p in enumerate(patterns): for q in patterns[:i]: if q.first_occ_overlaps(p): t_union = np.unique(snp.merge(p.t, q.t)) sparse = filter_out_dense_infreq(t_union, min_dist, freqs) if not np.array_equal(t_union, sparse): p.t = snp.intersect(p.t, sparse) q.t = snp.intersect(q.t, sparse) return filter_and_sort_patterns([p for p in patterns if len(p.t) > 1]) #filter out rudiments
def test_non_cc_both(self): """ Check that using a non-c-contiguous array as the both arguments returns the correct value. Repeat the test 1000 times because memory issues might be flaky. """ for i in range(1000): a = np.array([[1, 2, 3], [3, 4, 5], [7, 9, 10]]) nonzero_row, nonzero_col = np.nonzero(a) x = nonzero_col[0:3] y = nonzero_col[0:3] try: self.assertEqual(list(snp.merge(x, y)), [0, 0, 1, 1, 2, 2]) except ValueError: pass
def get_intervals_from_matrix(_mat, driver='ev'): _q = _mat.shape[0] diag_bl = np.diag([1], -_q + 1) diag_tr = np.diag([1], _q - 1) _mat += diag_bl _mat += diag_tr eig_zero = scipy.linalg.eigvalsh(_mat, driver=driver) _mat -= 2 * diag_bl _mat -= 2 * diag_tr eig_pi = scipy.linalg.eigvalsh(_mat, driver=driver) _mat += diag_bl _mat += diag_tr _intervals = sortednp.merge(eig_zero, eig_pi) _intervals.shape = (_q, 2) return _intervals
def valid_combo(c1, c2, incomp): return valid_clique(snp.merge(c1, c2, duplicates=snp.DROP), incomp)
def sorted_unique(array): return snp.merge(array, np.array([]), duplicates=snp.DROP)