Skip to content

Commit 3753e01

Browse files
the actual code stuff
0 parents  commit 3753e01

7 files changed

+1311
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
extracted-data
2+
__pycache__

blockmodel_avg_mapper.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import numpy as np
2+
import pandas as pd
3+
4+
# Get the blockmodel_avgs
5+
blockmodel_avgs_df = pd.read_csv("blockmodel_avgs.csv").transpose()
6+
raw_blockmodel_avgs = blockmodel_avgs_df.to_dict()
7+
8+
# Decode it into a nice easy to use datastructures
9+
blockmodel_avgs = { raw_blockmodel_avg["block_name"]: np.array([raw_blockmodel_avg[i] for i in ['r', 'g', 'b', 'a']]) for raw_blockmodel_avg in raw_blockmodel_avgs.values() }
10+
blockmodel_names, avgs = list(blockmodel_avgs.keys()), np.array(list(blockmodel_avgs.values()))
11+
12+
# Get the average color of a block (returns None if there isn't any)
13+
def get_block_avg_color(blockname):
14+
return blockmodel_avgs.get(blockname)
15+
16+
# Get a block with the closest average to the specified color, its average, and the difference (there's no need to use a 16-tuple tree here so I didn't)
17+
def get_closest_colored_block(color):
18+
# Convert the color to a numpy array if it isn't already
19+
if not isinstance(color, np.ndarray):
20+
color = np.array(color)
21+
22+
distances = np.sqrt(np.sum((avgs - color) ** 2, axis=1))
23+
closests_index = np.argmin(distances)
24+
closest = blockmodel_names[closests_index]
25+
closest_avg = avgs[closests_index]
26+
distance = distances[closests_index]
27+
return (closest, closest_avg, distance)
28+
29+
# Get the blocks that have an exact color
30+
def get_blocks_of_color(color):
31+
# Convert the color to a numpy array if it isn't already
32+
if not isinstance(color, np.ndarray):
33+
color = np.array(color)
34+
35+
return [blockmodel_names[i] for i in np.nonzero((color == avgs).all(axis=1))[0]]

0 commit comments

Comments
 (0)