Upload 2 files
Browse files
TCUPY.py
CHANGED
@@ -1084,6 +1084,114 @@ def pairwise_cosine_similarity(X: cp.ndarray, eps: float = 1e-10) -> cp.ndarray:
|
|
1084 |
|
1085 |
###################################################################################
|
1086 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1087 |
print('Module is loaded!')
|
1088 |
print('Enjoy! :)')
|
1089 |
print('=' * 70)
|
|
|
1084 |
|
1085 |
###################################################################################
|
1086 |
|
1087 |
+
def cosine_similarities(src_array, trg_array):
|
1088 |
+
|
1089 |
+
"""
|
1090 |
+
Computes cosine similarities between 1D src array and 2D trg array
|
1091 |
+
"""
|
1092 |
+
|
1093 |
+
src_norm = cp.linalg.norm(src_array)
|
1094 |
+
|
1095 |
+
trg_norms = cp.linalg.norm(trg_array, axis=1)
|
1096 |
+
|
1097 |
+
dot_products = cp.dot(trg_array, src_array)
|
1098 |
+
|
1099 |
+
cosine_sims = dot_products / (src_norm * trg_norms + 1e-10)
|
1100 |
+
|
1101 |
+
return cosine_sims
|
1102 |
+
|
1103 |
+
###################################################################################
|
1104 |
+
|
1105 |
+
def embeddings_topk_cosine_neighbors(embeddings,
|
1106 |
+
k=1,
|
1107 |
+
row_batch=4096,
|
1108 |
+
col_batch=4096
|
1109 |
+
):
|
1110 |
+
|
1111 |
+
"""
|
1112 |
+
For each embedding, find the indices and similarities of its top-k neighbors,
|
1113 |
+
excluding itself, sorted by descending similarity.
|
1114 |
+
|
1115 |
+
Args:
|
1116 |
+
embeddings (cp.ndarray): shape (N, D), float32 on GPU.
|
1117 |
+
k (int): how many neighbors to return (must be < N).
|
1118 |
+
row_batch (int): number of rows to process at once.
|
1119 |
+
col_batch (int): number of columns to process at once.
|
1120 |
+
|
1121 |
+
Returns:
|
1122 |
+
top_idx (cp.ndarray): shape (N, k), int32 indices of nearest neighbors.
|
1123 |
+
top_sim (cp.ndarray): shape (N, k), float32 cosine similarities.
|
1124 |
+
Each row is sorted descending.
|
1125 |
+
"""
|
1126 |
+
|
1127 |
+
# normalize embeddings to unit length
|
1128 |
+
norms = cp.linalg.norm(embeddings, axis=1, keepdims=True)
|
1129 |
+
embeddings /= norms
|
1130 |
+
|
1131 |
+
N, D = embeddings.shape
|
1132 |
+
if not (1 <= k < N):
|
1133 |
+
raise ValueError(f"k must satisfy 1 ≤ k < N; got N={N}, k={k}")
|
1134 |
+
|
1135 |
+
# placeholders for top-k
|
1136 |
+
top_sim = cp.full((N, k), -cp.inf, dtype=cp.float32)
|
1137 |
+
top_idx = cp.full((N, k), -1, dtype=cp.int32)
|
1138 |
+
|
1139 |
+
for i in tqdm.tqdm(range(0, N, row_batch)):
|
1140 |
+
i_end = min(i + row_batch, N)
|
1141 |
+
rows = embeddings[i:i_end] # (rb, D)
|
1142 |
+
rb = i_end - i
|
1143 |
+
|
1144 |
+
# per-block buffers
|
1145 |
+
best_s = top_sim[i:i_end] # (rb, k)
|
1146 |
+
best_i = top_idx[i:i_end] # (rb, k)
|
1147 |
+
row_ids = cp.arange(i, i_end) # (rb,)
|
1148 |
+
|
1149 |
+
for j in range(0, N, col_batch):
|
1150 |
+
j_end = min(j + col_batch, N)
|
1151 |
+
cols = embeddings[j:j_end] # (cb, D)
|
1152 |
+
sims = rows.dot(cols.T) # (rb, cb)
|
1153 |
+
|
1154 |
+
# mask out self-similarities
|
1155 |
+
# find rows whose global index ∈ [j, j_end)
|
1156 |
+
mask = (row_ids >= j) & (row_ids < j_end)
|
1157 |
+
if mask.any():
|
1158 |
+
local_rows = cp.where(mask)[0]
|
1159 |
+
local_cols = row_ids[mask] - j
|
1160 |
+
sims[local_rows, local_cols] = -cp.inf
|
1161 |
+
|
1162 |
+
# get top-k within this block
|
1163 |
+
# argpartition to grab k largest in each row
|
1164 |
+
part = cp.argpartition(sims, -k, axis=1)[:, -k:] # (rb, k)
|
1165 |
+
blk_s = sims[cp.arange(rb)[:, None], part] # (rb, k)
|
1166 |
+
blk_i = part + j # (rb, k)
|
1167 |
+
|
1168 |
+
# merge with running best
|
1169 |
+
cat_s = cp.concatenate([best_s, blk_s], axis=1) # (rb, 2k)
|
1170 |
+
cat_i = cp.concatenate([best_i, blk_i], axis=1)
|
1171 |
+
|
1172 |
+
# select new top-k from the 2k candidates
|
1173 |
+
part2 = cp.argpartition(cat_s, -k, axis=1)[:, -k:]
|
1174 |
+
best_s = cat_s[cp.arange(rb)[:, None], part2]
|
1175 |
+
best_i = cat_i[cp.arange(rb)[:, None], part2]
|
1176 |
+
|
1177 |
+
# write back
|
1178 |
+
top_sim[i:i_end] = best_s
|
1179 |
+
top_idx[i:i_end] = best_i
|
1180 |
+
|
1181 |
+
# final sort per row so sims descend
|
1182 |
+
if k > 1:
|
1183 |
+
order = cp.argsort(-top_sim, axis=1)
|
1184 |
+
top_sim = cp.take_along_axis(top_sim, order, axis=1)
|
1185 |
+
top_idx = cp.take_along_axis(top_idx, order, axis=1)
|
1186 |
+
|
1187 |
+
# if k==1, squeeze dims
|
1188 |
+
if k == 1:
|
1189 |
+
return top_idx.ravel(), top_sim.ravel()
|
1190 |
+
|
1191 |
+
return top_idx, top_sim
|
1192 |
+
|
1193 |
+
###################################################################################
|
1194 |
+
|
1195 |
print('Module is loaded!')
|
1196 |
print('Enjoy! :)')
|
1197 |
print('=' * 70)
|
TMIDIX.py
CHANGED
@@ -51,7 +51,7 @@ r'''############################################################################
|
|
51 |
|
52 |
###################################################################################
|
53 |
|
54 |
-
__version__ = "25.7
|
55 |
|
56 |
print('=' * 70)
|
57 |
print('TMIDIX Python module')
|
@@ -7128,7 +7128,8 @@ def escore_notes_to_binary_matrix(escore_notes,
|
|
7128 |
channel=0,
|
7129 |
patch=0,
|
7130 |
flip_matrix=False,
|
7131 |
-
reverse_matrix=False
|
|
|
7132 |
):
|
7133 |
|
7134 |
escore = [e for e in escore_notes if e[3] == channel and e[6] == patch]
|
@@ -7152,14 +7153,17 @@ def escore_notes_to_binary_matrix(escore_notes,
|
|
7152 |
duration = max(1, duration)
|
7153 |
chan = max(0, min(15, chan))
|
7154 |
pitch = max(0, min(127, pitch))
|
7155 |
-
velocity = max(
|
7156 |
pat = max(0, min(128, pat))
|
7157 |
|
7158 |
if channel == chan and patch == pat:
|
7159 |
|
7160 |
for t in range(time, min(time + duration, time_range)):
|
7161 |
-
|
7162 |
-
|
|
|
|
|
|
|
7163 |
|
7164 |
if flip_matrix:
|
7165 |
|
@@ -7183,7 +7187,8 @@ def escore_notes_to_binary_matrix(escore_notes,
|
|
7183 |
def binary_matrix_to_original_escore_notes(binary_matrix,
|
7184 |
channel=0,
|
7185 |
patch=0,
|
7186 |
-
velocity=-1
|
|
|
7187 |
):
|
7188 |
|
7189 |
result = []
|
@@ -7222,8 +7227,11 @@ def binary_matrix_to_original_escore_notes(binary_matrix,
|
|
7222 |
|
7223 |
for r in result:
|
7224 |
|
7225 |
-
if velocity == -1:
|
7226 |
-
|
|
|
|
|
|
|
7227 |
|
7228 |
original_escore_notes.append(['note', r[0], r[1], channel, r[2], vel, patch])
|
7229 |
|
@@ -13604,6 +13612,89 @@ PERCUSSION_GROUPS = {
|
|
13604 |
|
13605 |
###################################################################################
|
13606 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13607 |
print('Module loaded!')
|
13608 |
print('=' * 70)
|
13609 |
print('Enjoy! :)')
|
|
|
51 |
|
52 |
###################################################################################
|
53 |
|
54 |
+
__version__ = "25.8.7"
|
55 |
|
56 |
print('=' * 70)
|
57 |
print('TMIDIX Python module')
|
|
|
7128 |
channel=0,
|
7129 |
patch=0,
|
7130 |
flip_matrix=False,
|
7131 |
+
reverse_matrix=False,
|
7132 |
+
encode_velocities=False
|
7133 |
):
|
7134 |
|
7135 |
escore = [e for e in escore_notes if e[3] == channel and e[6] == patch]
|
|
|
7153 |
duration = max(1, duration)
|
7154 |
chan = max(0, min(15, chan))
|
7155 |
pitch = max(0, min(127, pitch))
|
7156 |
+
velocity = max(1, min(127, velocity))
|
7157 |
pat = max(0, min(128, pat))
|
7158 |
|
7159 |
if channel == chan and patch == pat:
|
7160 |
|
7161 |
for t in range(time, min(time + duration, time_range)):
|
7162 |
+
if encode_velocities:
|
7163 |
+
escore_matrix[t][pitch] = velocity
|
7164 |
+
|
7165 |
+
else:
|
7166 |
+
escore_matrix[t][pitch] = 1
|
7167 |
|
7168 |
if flip_matrix:
|
7169 |
|
|
|
7187 |
def binary_matrix_to_original_escore_notes(binary_matrix,
|
7188 |
channel=0,
|
7189 |
patch=0,
|
7190 |
+
velocity=-1,
|
7191 |
+
decode_velocities=False
|
7192 |
):
|
7193 |
|
7194 |
result = []
|
|
|
7227 |
|
7228 |
for r in result:
|
7229 |
|
7230 |
+
if velocity == -1 and not decode_velocities:
|
7231 |
+
vel = max(40, r[2])
|
7232 |
+
|
7233 |
+
if decode_velocities:
|
7234 |
+
vel = r[3]
|
7235 |
|
7236 |
original_escore_notes.append(['note', r[0], r[1], channel, r[2], vel, patch])
|
7237 |
|
|
|
13612 |
|
13613 |
###################################################################################
|
13614 |
|
13615 |
+
def escore_notes_to_expanded_binary_matrix(escore_notes,
|
13616 |
+
channel=0,
|
13617 |
+
patch=0,
|
13618 |
+
flip_matrix=False,
|
13619 |
+
reverse_matrix=False,
|
13620 |
+
encode_velocities=True
|
13621 |
+
):
|
13622 |
+
|
13623 |
+
escore = [e for e in escore_notes if e[3] == channel and e[6] == patch]
|
13624 |
+
|
13625 |
+
if escore:
|
13626 |
+
last_time = escore[-1][1]
|
13627 |
+
last_notes = [e for e in escore if e[1] == last_time]
|
13628 |
+
max_last_dur = max([e[2] for e in last_notes])
|
13629 |
+
|
13630 |
+
time_range = last_time+max_last_dur
|
13631 |
+
|
13632 |
+
escore_matrix = []
|
13633 |
+
|
13634 |
+
escore_matrix = [[(0, 0)] * 128 for _ in range(time_range)]
|
13635 |
+
|
13636 |
+
for note in escore:
|
13637 |
+
|
13638 |
+
etype, time, duration, chan, pitch, velocity, pat = note
|
13639 |
+
|
13640 |
+
time = max(0, time)
|
13641 |
+
duration = max(1, duration)
|
13642 |
+
chan = max(0, min(15, chan))
|
13643 |
+
pitch = max(0, min(127, pitch))
|
13644 |
+
velocity = max(1, min(127, velocity))
|
13645 |
+
pat = max(0, min(128, pat))
|
13646 |
+
|
13647 |
+
if channel == chan and patch == pat:
|
13648 |
+
|
13649 |
+
count = 0
|
13650 |
+
|
13651 |
+
for t in range(time, min(time + duration, time_range)):
|
13652 |
+
if encode_velocities:
|
13653 |
+
escore_matrix[t][pitch] = velocity, count
|
13654 |
+
|
13655 |
+
else:
|
13656 |
+
escore_matrix[t][pitch] = 1, count
|
13657 |
+
count += 1
|
13658 |
+
|
13659 |
+
if flip_matrix:
|
13660 |
+
|
13661 |
+
temp_matrix = []
|
13662 |
+
|
13663 |
+
for m in escore_matrix:
|
13664 |
+
temp_matrix.append(m[::-1])
|
13665 |
+
|
13666 |
+
escore_matrix = temp_matrix
|
13667 |
+
|
13668 |
+
if reverse_matrix:
|
13669 |
+
escore_matrix = escore_matrix[::-1]
|
13670 |
+
|
13671 |
+
return escore_matrix
|
13672 |
+
|
13673 |
+
else:
|
13674 |
+
return None
|
13675 |
+
|
13676 |
+
###################################################################################
|
13677 |
+
|
13678 |
+
def transpose_list(lst):
|
13679 |
+
return [list(row) for row in zip(*lst)]
|
13680 |
+
|
13681 |
+
###################################################################################
|
13682 |
+
|
13683 |
+
def chunk_list(lst, size):
|
13684 |
+
return [lst[i:i + size] for i in range(0, len(lst), size)]
|
13685 |
+
|
13686 |
+
###################################################################################
|
13687 |
+
|
13688 |
+
def flip_list_rows(lst):
|
13689 |
+
return [row[::-1] for row in lst]
|
13690 |
+
|
13691 |
+
###################################################################################
|
13692 |
+
|
13693 |
+
def flip_list_columns(lst):
|
13694 |
+
return lst[::-1]
|
13695 |
+
|
13696 |
+
###################################################################################
|
13697 |
+
|
13698 |
print('Module loaded!')
|
13699 |
print('=' * 70)
|
13700 |
print('Enjoy! :)')
|