Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make unary matrix prettier #201

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 22 additions & 24 deletions py/visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ def make_numpy_matrix(obs, entities, relations):
for ob in obs:
val = ob.value
ent_index = entities.index(ob.items[0])
rel_index = relations.index(ob.relation)
m[ent_index][rel_index] = val
if ob.relation in relations:
rel_index = relations.index(ob.relation)
m[ent_index][rel_index] = val
return m


Expand All @@ -35,14 +36,13 @@ def normalize_matrix(m):
return (m - np.min(m)) / np.ptp(m)


def unary_matrix(cluster, obs, all_relations, clusters):
def unary_matrix(cluster, obs, clusters):
"""Plot a matrix visualization for the cluster of unary relations."""
fig = plt.figure()
ax = plt.gca()
fontsize = 12

ax.xaxis.tick_top()
ax.set_xticks(np.arange(len(all_relations)))
ax.set_xticklabels(all_relations, rotation=90)
relations = sorted(cluster.relations)
num_columns = len(relations)
longest_relation_length = max(len(r) for r in relations)

all_entities = []
domain = ""
Expand All @@ -53,27 +53,26 @@ def unary_matrix(cluster, obs, all_relations, clusters):
print("Error in unary_matrix: Found multiple domains in clusters")
sys.exit(1)

all_entities.extend(dc.entities)
all_entities.extend(sorted(dc.entities))

num_rows = len(all_entities)
longest_entity_length = max(len(e) for e in all_entities)

ax.set_yticks(np.arange(len(all_entities)))
ax.set_yticklabels(all_entities)
width = (num_columns + longest_entity_length) * fontsize / 72.0 + 0.2 # Fontsize is in points, 1 pt = 1/72in
height = (num_rows + longest_relation_length) * fontsize / 72.0 + 0.2
fig, ax = plt.subplots(figsize=(width,height))

ax.xaxis.tick_top()
ax.set_xticks(np.arange(num_columns), labels=relations,
rotation=90, fontsize=fontsize)
ax.set_yticks(np.arange(num_rows), labels=all_entities, fontsize=fontsize)

# Matrix of data
m = make_numpy_matrix(obs, all_entities, all_relations)
m = make_numpy_matrix(obs, all_entities, relations)
cmap = copy.copy(plt.get_cmap('Greys'))
cmap.set_bad(color='gray')
ax.imshow(normalize_matrix(m))

# Red lines between columns (relations)
d = 0
for c in clusters:
if cluster.cluster_id == c.cluster_id:
ax.axvline(d - 0.5, color='r', linewidth=2)
d += len(clusters[i].relations)
if cluster.cluster_id == c.cluster_id:
ax.axvline(d - 0.5, color='r', linewidth=2)
break

# Red lines between rows (entities)
n = 0
for dc in cluster.domain_clusters:
Expand All @@ -87,11 +86,10 @@ def unary_matrix(cluster, obs, all_relations, clusters):

def plot_unary_matrices(clusters, obs, output):
"""Write a matrix visualization for each cluster in clusters."""
all_relations = sum([c.relations for c in clusters], [])
with open(output, 'w') as f:
f.write("<html><body>\n\n")
for cluster in clusters:
f.write("<h1>IRM #" + cluster.cluster_id + "\n")
fig = unary_matrix(cluster, obs, all_relations, clusters)
fig = unary_matrix(cluster, obs, clusters)
f.write(figure_to_html(fig) + "\n\n")
f.write("</body></html>\n")