projectlosangeles commited on
Commit
c59fecb
·
verified ·
1 Parent(s): c460525

Upload 2 files

Browse files
Files changed (1) hide show
  1. TPLOTS.py +168 -15
TPLOTS.py CHANGED
@@ -4,12 +4,12 @@ r'''############################################################################
4
  ################################################################################
5
  #
6
  #
7
- # Tegridy Plots Python Module (TPLOTS)
8
- # Version 1.0
9
  #
10
- # Project Los Angeles
11
  #
12
- # Tegridy Code 2024
13
  #
14
  # https://github.com/asigalov61/tegridy-tools
15
  #
@@ -33,20 +33,20 @@ r'''############################################################################
33
  ################################################################################
34
  ################################################################################
35
  #
36
- # Critical dependencies
37
  #
38
- # !pip install numpy
39
- # !pip install scipy
40
- # !pip install matplotlib
41
- # !pip install networkx
42
- # !pip3 install scikit-learn
43
  #
44
  ################################################################################
45
  #
46
- # Future critical dependencies
47
  #
48
- # !pip install umap-learn
49
- # !pip install alphashape
50
  #
51
  ################################################################################
52
  '''
@@ -1254,6 +1254,113 @@ def plot_parsons_code(parsons_code,
1254
 
1255
  plt.close()
1256
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1257
  ################################################################################
1258
  # [WIP] Future dev functions
1259
  ################################################################################
@@ -1363,7 +1470,53 @@ plt.show()
1363
  '''
1364
 
1365
  ################################################################################
1366
- #
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1367
  # This is the end of TPLOTS Python modules
1368
- #
1369
  ################################################################################
 
4
  ################################################################################
5
  #
6
  #
7
+ # Tegridy Plots Python Module (TPLOTS)
8
+ # Version 1.0
9
  #
10
+ # Project Los Angeles
11
  #
12
+ # Tegridy Code 2025
13
  #
14
  # https://github.com/asigalov61/tegridy-tools
15
  #
 
33
  ################################################################################
34
  ################################################################################
35
  #
36
+ # Critical dependencies
37
  #
38
+ # !pip install numpy==1.24.4
39
+ # !pip install scipy
40
+ # !pip install matplotlib
41
+ # !pip install networkx
42
+ # !pip3 install scikit-learn
43
  #
44
  ################################################################################
45
  #
46
+ # Future critical dependencies
47
  #
48
+ # !pip install umap-learn
49
+ # !pip install alphashape
50
  #
51
  ################################################################################
52
  '''
 
1254
 
1255
  plt.close()
1256
 
1257
+ ################################################################################
1258
+
1259
+ def plot_tokens_embeddings_constellation(tokens_embeddings,
1260
+ start_token,
1261
+ end_token,
1262
+ plot_size=(10, 10),
1263
+ labels_size=12,
1264
+ show_grid=False,
1265
+ save_plot=''):
1266
+
1267
+ """
1268
+ Plots token embeddings constellation using MST and graph layout
1269
+ without dimensionality reduction.
1270
+ """
1271
+
1272
+ distance_matrix = metrics.pairwise_distances(tokens_embeddings[start_token:end_token], metric='cosine')
1273
+
1274
+ token_labels = [str(i) for i in range(start_token, end_token)]
1275
+
1276
+ mst = minimum_spanning_tree(distance_matrix).toarray()
1277
+
1278
+ n = distance_matrix.shape[0]
1279
+ G = nx.Graph()
1280
+
1281
+ for i in range(n):
1282
+ for j in range(n):
1283
+ if mst[i, j] > 0:
1284
+ weight = 1 / (distance_matrix[i, j] + 1e-8)
1285
+ G.add_edge(i, j, weight=weight)
1286
+
1287
+ pos = nx.kamada_kawai_layout(G, weight='weight')
1288
+
1289
+ points = np.array([pos[i] for i in range(n)])
1290
+
1291
+ plt.figure(figsize=plot_size)
1292
+ plt.scatter(points[:, 0], points[:, 1], color='blue')
1293
+
1294
+ for i, label in enumerate(token_labels):
1295
+ plt.annotate(label, (points[i, 0], points[i, 1]),
1296
+ textcoords="offset points",
1297
+ xytext=(0, 10),
1298
+ ha='center',
1299
+ fontsize=labels_size)
1300
+
1301
+ for i in range(n):
1302
+ for j in range(n):
1303
+ if mst[i, j] > 0:
1304
+ plt.plot([points[i, 0], points[j, 0]],
1305
+ [points[i, 1], points[j, 1]],
1306
+ 'k--', alpha=0.5)
1307
+
1308
+ plt.title('Token Embeddings Constellation with MST', fontsize=labels_size)
1309
+ plt.grid(show_grid)
1310
+
1311
+ if save_plot:
1312
+ plt.savefig(save_plot, bbox_inches="tight")
1313
+ plt.close()
1314
+
1315
+ else:
1316
+ plt.show()
1317
+
1318
+ plt.close()
1319
+
1320
+ ################################################################################
1321
+
1322
+ def find_token_path(tokens_embeddings,
1323
+ start_token,
1324
+ end_token,
1325
+ verbose=False
1326
+ ):
1327
+
1328
+ """
1329
+ Finds the path of tokens between start_token and end_token using
1330
+ the Minimum Spanning Tree (MST) derived from the distance matrix.
1331
+ """
1332
+
1333
+ distance_matrix = metrics.pairwise_distances(tokens_embeddings, metric='cosine')
1334
+
1335
+ token_labels = [str(i) for i in range(len(distance_matrix))]
1336
+
1337
+ if verbose:
1338
+ print('Total number of tokens:', len(distance_matrix))
1339
+
1340
+ mst = minimum_spanning_tree(distance_matrix).toarray()
1341
+
1342
+ n = distance_matrix.shape[0]
1343
+ G = nx.Graph()
1344
+
1345
+ for i in range(n):
1346
+ for j in range(n):
1347
+ if mst[i, j] > 0:
1348
+ weight = 1 / (distance_matrix[i, j] + 1e-8)
1349
+ G.add_edge(i, j, weight=weight)
1350
+
1351
+ try:
1352
+ start_idx = token_labels.index(str(start_token))
1353
+ end_idx = token_labels.index(str(end_token))
1354
+
1355
+ except ValueError:
1356
+ raise ValueError("Start or end token not found in the provided token labels.")
1357
+
1358
+ path_indices = nx.shortest_path(G, source=start_idx, target=end_idx)
1359
+
1360
+ token_path = [int(token_labels[idx]) for idx in path_indices]
1361
+
1362
+ return token_path
1363
+
1364
  ################################################################################
1365
  # [WIP] Future dev functions
1366
  ################################################################################
 
1470
  '''
1471
 
1472
  ################################################################################
1473
+
1474
+ def plot_tree_horizontal(data):
1475
+
1476
+ """
1477
+ Given data as a list of levels (each level is a tuple or list of
1478
+ displacements for each branch), this function computes the cumulative
1479
+ value per branch (starting from 0) and plots each branch
1480
+ with the tree level mapped to the x-axis and the cumulative value mapped
1481
+ to the y-axis. This gives a left-to-right tree with branches spanning up
1482
+ (positive) and down (negative).
1483
+
1484
+ Parameters:
1485
+ data (list of tuple/list): Each element represents a tree level.
1486
+ It is assumed every level has the same length.
1487
+ """
1488
+
1489
+ # Convert data to a NumPy array with shape (n_levels, n_branches)
1490
+ data = np.array(data)
1491
+ n_levels, n_branches = data.shape
1492
+
1493
+ # Compute cumulative sums along each branch.
1494
+ # Each branch starts at 0 at level 0.
1495
+ cum = np.zeros((n_levels + 1, n_branches))
1496
+ for i in range(n_levels):
1497
+ cum[i + 1, :] = cum[i, :] + data[i, :]
1498
+
1499
+ plt.figure(figsize=(12, 8))
1500
+
1501
+ # Plot each branch as a line. For branch j:
1502
+ # - x coordinates are the tree levels (0 to n_levels)
1503
+ # - y coordinates are the corresponding cumulative values.
1504
+ for j in range(n_branches):
1505
+ x = np.arange(n_levels + 1)
1506
+ y = cum[:, j]
1507
+ plt.plot(x, y, marker='o', label=f'Branch {j}')
1508
+
1509
+ plt.title("Horizontal Tree Visualization: Branches Spanning Up and Down", fontsize=14)
1510
+ plt.xlabel("Tree Level (Left = Root)")
1511
+ plt.ylabel("Cumulative Value (Up = Positive, Down = Negative)")
1512
+
1513
+ # Add a horizontal line at y=0 to emphasize the center.
1514
+ plt.axhline(0, color="gray", linestyle="--")
1515
+
1516
+ #plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
1517
+ plt.tight_layout()
1518
+ plt.show()
1519
+
1520
+ ################################################################################
1521
  # This is the end of TPLOTS Python modules
 
1522
  ################################################################################