-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
230 lines (193 loc) · 9.36 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
from pyvis.network import Network
import networkx as nx
import chromadb
import chromadb.types
from chromadb.utils import embedding_functions
import csv
import json
import itertools
import operator
from math import dist
from typing import Literal, Optional
from random import sample
LONGEST_EDGE_SPRING_LENGTH = 20
MAX_EDGE_WEIGHT = 10
EMBEDDING_FUNCTION = embedding_functions.SentenceTransformerEmbeddingFunction(model_name='embaas/sentence-transformers-gte-base')
def read_mathematical_definition_notes(filepath="data/MathsDefinitionNotes.txt"):
with open(filepath, newline="") as notefile:
note_reader = csv.reader(notefile, delimiter="\t")
# Skip over headings
for _ in range(6):
next(note_reader)
notes:list[dict[str, str]] = []
for row in note_reader:
id, note_type, deck, term, symbol, definition, extra_notes, tags = row
assert (note_type == "08 Mathematical Definition")
# Remove custom LaTeX tags
def replace_latex(s):
return s.replace("[$]","$").replace("[/$]","$").replace("[$$]","$$").replace("[/$$]","$$")
term = replace_latex(term)
symbol = replace_latex(symbol) if symbol else None
definition = replace_latex(definition)
notes.append({
"id": id,
"note_type": note_type,
"deck": deck,
"term": term,
"definition": definition,
"extra_notes": extra_notes,
"tags": tags,
} | ({"symbol": symbol} if symbol else {}))
return notes
def add_mathematical_definition_notes_to_db(db, notes):
db.add(
documents = [f"{note['term']} := {note['definition']}" for note in notes],
metadatas = notes,
ids = [note["id"] for note in notes]
)
def distance_metric(v1, v2):
return l2_distance(v1, v2)
def l2_distance(v1, v2):
return dist(v1, v2)
def create_edges(ids: list[str], embeddings: list[chromadb.types.Vector], mode: Literal["Linear Cutoff", "Nearest Neighbours"], db: Optional[chromadb.Collection] = None):
edges = []
if mode == "Linear Cutoff":
weights = [distance_metric(e1, e2) for (e1, e2) in itertools.combinations(embeddings, 2)]
max_weight = max(weights)
min_weight = min(weights)
for (id1, embedding1), (id2, embedding2) in itertools.combinations(zip(ids, embeddings), 2):
weight = (max_weight - distance_metric(embedding1, embedding2)) / (max_weight - min_weight)
if weight > 0.4:
edges.append((id1, id2, {
"weight": MAX_EDGE_WEIGHT*weight,
"length": LONGEST_EDGE_SPRING_LENGTH*(1-weight)
}))
elif mode == "Nearest Neighbours":
assert db
id_set = set(ids)
edge_set = set()
# distance_sample = [distance_metric(*sample(embeddings, 2)) for _ in range(1000)]
distance_sample = [distance_metric(e1, e2) for (e1, e2) in itertools.combinations(embeddings, 2)]
max_distance = max(distance_sample)
min_distance = min(distance_sample)
weight_cut_off = 0.42
weight_calc = lambda d: max(0, (max_distance - d) / (max_distance - min_distance))
for id, embedding in zip(ids, embeddings):
potential_neighbours = db.query(
query_embeddings=embedding,
n_results=30,
include=["embeddings"]
)
assert potential_neighbours["embeddings"] and potential_neighbours["embeddings"][0]
neighbour_zip = zip(potential_neighbours["ids"][0], potential_neighbours["embeddings"][0])
sorted_potential_neighbours = sorted(neighbour_zip, key = lambda neighbour: distance_metric(embedding, neighbour[1]))
potential_edges: list[tuple[float, str, chromadb.types.Vector]] = []
for neighbour_id, neighbour_embedding in sorted_potential_neighbours:
if neighbour_id not in id_set or neighbour_id == id:
continue
distance = distance_metric(embedding, neighbour_embedding)
if not potential_edges:
# always have one edge.
potential_edges.append((distance, neighbour_id, neighbour_embedding))
continue
if weight_calc(distance) < weight_cut_off:
continue
potential_edges.append((distance, neighbour_id, neighbour_embedding))
potential_edges.sort(key=operator.itemgetter(2), reverse=True)
capacity = 1.5
for i, (distance, neighbour_id, neighbour_embedding) in enumerate(potential_edges[0:9]):
if capacity < 0:
break
capacity -= (i*0.5 + 1)*(1 - weight_calc(distance))
if (id, neighbour_id) in edge_set:
continue
edges.append((id, neighbour_id, {
"weight": MAX_EDGE_WEIGHT * weight_calc(distance),
"length": LONGEST_EDGE_SPRING_LENGTH*(1-weight_calc(distance)+0.4)/(1-0.4),
"label": f"{round(weight_calc(distance),2)}"
}))
edge_set.add((id, neighbour_id))
else:
print("Unknown edge creation method")
assert False
return edges
def reduce_edges(network: nx.Graph):
for node in sorted(list(network.nodes()), key=network.degree, reverse=True):
if network.degree(node) < 7:
continue# TODO change to break
sorted_edges = sorted(list(network.edges(node, True)), key=lambda e: e[2]["weight"] if (e and e[2]) else 0, reverse=True)
sorted_edges = [(u,v) for u,v,_ in sorted_edges if network.degree(u) > 1 and network.degree(v) > 1]
network.remove_edges_from(sorted_edges[min(10,len(sorted_edges)):])
def get_all_terms(group: list[tuple[str, dict]]):
terms = []
for id in group:
document = flashcards_db.get(id)["documents"][0]
terms += get_all_words(document)
return terms
def get_all_words(s):
removed_spaces = [''.join(c for c in a if c.isalnum()) for a in s.split()]
return [a for a in removed_spaces if len(a) > 3]
def get_closest_term(terms, vector):
current_min_distance = 10000
current_min_term = ""
for term in [t.lower() for t in terms]:
distance = distance_metric(EMBEDDING_FUNCTION([term])[0], vector)
if distance < current_min_distance:
current_min_distance = distance
current_min_term = term
return current_min_term
def average_vector(ids):
sum = []
for id in ids:
embedding = flashcards_db.get(id, include=["embeddings"])["embeddings"][0]
if sum:
sum = [s + e for s, e in zip(sum, embedding)]
else:
sum = embedding
return sum
chroma_client = chromadb.PersistentClient(path="data/chromadb")
flashcards_db = chroma_client.get_or_create_collection(name="flashcards", embedding_function=EMBEDDING_FUNCTION)
# chroma_client.delete_collection(name="flashcards")
dictionary_db = chroma_client.get_or_create_collection(name="dictionary", embedding_function=EMBEDDING_FUNCTION)
# notes = read_mathematical_definition_notes()
# add_mathematical_definition_notes_to_db(flashcards_db, notes)
# print(flashcards_db.peek(1))
# results = flashcards_db.query(
# query_texts=["clopen"],
# n_results=5
# )
# print(f"Results {results}")
mindmap = nx.Graph()
flashcard_sample = flashcards_db.get(include=["embeddings","metadatas"], limit=500)
assert flashcard_sample["metadatas"]
assert flashcard_sample["embeddings"]
for id, note in zip(flashcard_sample["ids"], flashcard_sample["metadatas"]):
mindmap.add_node(id, title=note["term"], label=" ")
mindmap.add_edges_from(create_edges(flashcard_sample["ids"], flashcard_sample["embeddings"], "Nearest Neighbours", db=flashcards_db))
reduce_edges(mindmap)
partition = nx.community.louvain_communities(mindmap)
partition_names = [get_closest_term(get_all_terms(list(p)), average_vector(list(p))) if len(p) >= 4 else "" for p in partition]
new_partition = []
new_names = []
for unique_name in set(partition_names):
indicies = [i for i, _ in enumerate(partition) if partition_names[i] == unique_name]
new_partition.append(set().union(*[partition[i] for i in indicies]))
new_names.append(unique_name)
print(new_names)
print(new_partition)
export_dict = {
"nodes": [{"id": id, "title": node_data["title"], "group": [i for i, g in enumerate(new_partition) if id in g][0]} for id, node_data in mindmap.nodes(True)],
"edges": [{"source": u, "target": v, "label": edge_data["label"], "weight": edge_data["weight"]} for u, v, edge_data in mindmap.edges(None, True)],
"groups": [{"title": name.title(), "index": i} for i, name in enumerate(new_names)]
}
with open("data/mindmap.json", "w") as json_file:
json.dump(export_dict, json_file, indent="\t", sort_keys=True)
# nt = Network('700px', '100%')
# nt.from_nx(mindmap,show_edge_weights=True)
# nt.barnes_hut(
# central_gravity=5.0,
# spring_strength=0.06,
# damping=0.25
# )
# nt.show_buttons(filter_=['physics'])
# nt.save_graph("demo.html")