Skip to content

Commit 3a579e4

Browse files
authored
Merge pull request #32 from FireDynamics/dev_sven
some more refactoring
2 parents ab20116 + e430349 commit 3a579e4

File tree

11 files changed

+410
-388
lines changed

11 files changed

+410
-388
lines changed

ledsa/__main__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@
5050
ledsa.find_search_areas(ledsa.config['find_search_areas']['reference_img'])
5151
ledsa.plot_search_areas(ledsa.config['find_search_areas']['reference_img'])
5252
if args.s2:
53-
ledsa.analyse_positions()
54-
ledsa.plot_lines()
53+
ledsa.match_leds_to_led_arrays()
5554

5655
if args.s3:
5756
ledsa = LEDSA(build_experiment_infos=True)

ledsa/analysis/plot_functions.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ def plot_model(fig, channel, img_id, led_id, window_radius):
147147
# load model
148148
model_params = load_model(img_id, led_id, channel, window_radius)
149149

150-
led_model = ledsa.core.model.led_fit(mesh[0], mesh[1], model_params[0], model_params[1], model_params[2], model_params[3],
151-
model_params[4], model_params[5], model_params[6], model_params[7])
150+
led_model = ledsa.core.model.led_model(mesh[0], mesh[1], model_params[0], model_params[1], model_params[2], model_params[3],
151+
model_params[4], model_params[5], model_params[6], model_params[7])
152152

153153
current_fig = plt.gcf()
154154

@@ -220,8 +220,8 @@ def fit_led(img_id, led_id, channel):
220220
ledsa.load_search_areas()
221221
ledsa.config['analyse_photo']['channel'] = str(channel)
222222
filename = led.get_img_name(img_id)
223-
fit_res = led.process_file(filename, ledsa.search_areas, ledsa.line_indices, ledsa.config['analyse_photo'], True,
224-
led_id)
223+
fit_res = led.generate_analysis_data(filename, ledsa.search_areas, ledsa.line_indices, ledsa.config['analyse_photo'], True,
224+
led_id)
225225
return fit_res
226226

227227

ledsa/core/_led_helper_functions.py

Lines changed: 1 addition & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import os
22
from datetime import timedelta, datetime
3-
import re
43

5-
import numpy as np
64
from PIL import Image
75
from PIL.ExifTags import TAGS
86

9-
# os path separator
7+
108
sep = os.path.sep
119

1210

@@ -58,129 +56,6 @@ def find_img_number_list(first, last, increment, number_string_length=4):
5856
return num_list
5957

6058

61-
def remove_led(im_set, ix, iy, window_radius):
62-
im_set[ix - window_radius:ix + window_radius, iy - window_radius:iy + window_radius] = 0
63-
64-
65-
def find_led_pos(image, ix, iy, window_radius):
66-
s_radius = window_radius // 2
67-
s = np.index_exp[ix - s_radius:ix + s_radius, iy - s_radius:iy + s_radius]
68-
res = np.unravel_index(np.argmax(image[s]), image[s].shape)
69-
max_x = ix - s_radius + res[0]
70-
max_y = iy - s_radius + res[1]
71-
return max_x, max_y
72-
73-
74-
def match_leds_to_arrays_with_min_dist(dists_led_arrays_search_areas, edge_indices, config, search_areas):
75-
ignore_indices = get_indices_of_ignored_leds(config)
76-
77-
xs = search_areas[:, 1]
78-
ys = search_areas[:, 2]
79-
80-
num_leds = search_areas.shape[0]
81-
82-
# construct 2D array for LED indices sorted by line
83-
led_arrays = []
84-
for edge_idx in edge_indices:
85-
led_arrays.append([])
86-
87-
for iled in range(num_leds):
88-
if iled in ignore_indices:
89-
continue
90-
91-
idx_nearest_array = np.argmin(dists_led_arrays_search_areas[iled, :])
92-
# TODO: ask Lukas for need of following code
93-
94-
# for il_repeat in range(len(edge_indices)):
95-
# i1 = edge_indices[idx_nearest_array][0]
96-
# i2 = edge_indices[idx_nearest_array][1]
97-
#
98-
# x_outer_led1 = xs[i1]
99-
# y_outer_led1 = ys[i1]
100-
# x_outer_led2 = xs[i2]
101-
# y_outer_led2 = ys[i2]
102-
#
103-
# x_led = xs[iled]
104-
# y_led = ys[iled]
105-
#
106-
# dist_led_outer_led1 = np.sqrt((x_outer_led1 - x_led) ** 2 + (y_outer_led1 - y_led) ** 2)
107-
# dist_led_outer_led2 = np.sqrt((x_outer_led2 - x_led) ** 2 + (y_outer_led2 - y_led) ** 2)
108-
# dist_outer_leds = np.sqrt((x_outer_led1 - x_outer_led2) ** 2 + (y_outer_led1 - y_outer_led2) ** 2) + 1e-8
109-
#
110-
# if dist_led_outer_led1 < dist_outer_leds and dist_led_outer_led2 < dist_outer_leds:
111-
# break
112-
#
113-
# dists_led_arrays_search_areas[iled, idx_nearest_array] *= 2
114-
115-
led_arrays[idx_nearest_array].append(iled)
116-
return led_arrays
117-
118-
119-
def calc_dists_between_led_arrays_and_search_areas(line_edge_indices, search_areas):
120-
distances_led_arrays_search_areas = np.zeros((search_areas.shape[0], len(line_edge_indices)))
121-
122-
xs = search_areas[:, 1]
123-
ys = search_areas[:, 2]
124-
125-
for line_edge_idx in range(len(line_edge_indices)):
126-
i1 = line_edge_indices[line_edge_idx][0]
127-
i2 = line_edge_indices[line_edge_idx][1]
128-
129-
p1x = xs[i1]
130-
p1y = ys[i1]
131-
p2x = xs[i2]
132-
p2y = ys[i2]
133-
134-
pd = np.sqrt((p1x - p2x) ** 2 + (p1y - p2y) ** 2)
135-
d = np.abs(((p2y - p1y) * xs - (p2x - p1x) * ys
136-
+ p2x * p1y - p2y * p1x) / pd)
137-
138-
distances_led_arrays_search_areas[:, line_edge_idx] = d
139-
return distances_led_arrays_search_areas
140-
141-
142-
def get_indices_of_outer_leds(config):
143-
if config['analyse_positions']['line_edge_indices'] == 'None':
144-
config.in_line_edge_indices()
145-
with open('config.ini', 'w') as configfile:
146-
config.write(configfile)
147-
line_edge_indices = config.get2dnparray('analyse_positions', 'line_edge_indices')
148-
# makes sure that line_edge_indices is a 2d list
149-
if len(line_edge_indices.shape) == 1:
150-
line_edge_indices = [line_edge_indices]
151-
return line_edge_indices
152-
153-
154-
def get_indices_of_ignored_leds(config):
155-
if config['analyse_positions']['ignore_indices'] != 'None':
156-
ignore_indices = np.array([int(i) for i in config['analyse_positions']['ignore_indices'].split(' ')])
157-
else:
158-
ignore_indices = np.array([])
159-
return ignore_indices
160-
161-
162-
# switch to Lib/logging at some point
163-
def log_warnings(img_filename, fit_res, iled, line_number, data, s, cx, cy, conf):
164-
res = ' '.join(np.array_str(fit_res.x).split()).replace('[ ', '[').replace(' ]', ']').replace(' ', ',')
165-
img_file_path = conf['img_directory'] + img_filename
166-
window_radius = int(conf['window_radius'])
167-
168-
x, y, _ = fit_res.x
169-
170-
im_x = x + cx - window_radius
171-
im_y = y + cy - window_radius
172-
173-
log = f'Irregularities while fitting:\n {img_file_path} {iled} {line_number} {res} {fit_res.success} ' \
174-
f'{fit_res.fun} {fit_res.nfev} {data[s].shape[0]} {data[s].shape[1]} {im_x} {im_y} {window_radius} {cx} ' \
175-
f'{cy} {conf["channel"]}'
176-
if not os.path.exists('.{}logfiles'.format(sep)):
177-
os.makedirs('.{}logfiles'.format(sep))
178-
logfile = open('.{}logfiles{}warnings.log'.format(sep, sep), 'a')
179-
logfile.write(log)
180-
logfile.write('\n')
181-
logfile.close()
182-
183-
18459
def build_img_data_string(build_type, config):
18560
img_data = ''
18661
img_idx = 1
@@ -198,18 +73,6 @@ def build_img_data_string(build_type, config):
19873
return img_data
19974

20075

201-
def append_fit_res_to_img_data(cx, cy, fit_res, iled, img_data, led_array_idx, mean_color_value, sum_color_value,
202-
window_radius):
203-
x, y, dx, dy, A, alpha, wx, wy = fit_res.x
204-
im_x = x + cx - window_radius
205-
im_y = y + cy - window_radius
206-
led_data = (f'{iled:4d},{led_array_idx:2d},{im_x:10.4e},{im_y:10.4e},{dx:10.4e},{dy:10.4e},{A:10.4e},'
207-
f'{alpha:10.4e},{wx:10.4e},{wy:10.4e},{fit_res.success:12d},{fit_res.fun:10.4e},'
208-
f'{fit_res.nfev:9d},{sum_color_value:10.4e},{mean_color_value:10.4e}')
209-
img_data += led_data + '\n'
210-
return img_data
211-
212-
21376
def save_analysis_infos(img_data):
21477
out_file = open('.{}analysis{}image_infos_analysis.csv'.format(sep, sep), 'w')
21578
out_file.write("#ID,Name,Time[s],Experiment_Time[s]\n")
@@ -224,18 +87,3 @@ def save_experiment_infos(img_data):
22487
out_file.close()
22588

22689

227-
def find_analysed_img_ids(config):
228-
processed_imgs = []
229-
directory_content = os.listdir('.{}analysis{}channel{}'.format(sep, sep, config['channel']))
230-
for file_name in directory_content:
231-
img = re.search(r"([0-9]+)_led_positions.csv", file_name)
232-
if img is not None:
233-
processed_imgs.append(int(img.group(1)))
234-
return processed_imgs
235-
236-
237-
def save_list_of_remaining_imgs_needed_to_be_processed(remaining_imgs):
238-
out_file = open('images_to_process.csv', 'w')
239-
for i in list(remaining_imgs):
240-
out_file.write('{}\n'.format(i))
241-
out_file.close()
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import numpy as np
2+
3+
4+
def generate_mask_of_led_areas(image):
5+
im_mean = np.mean(image)
6+
im_max = np.max(image)
7+
th = 0.25 * (im_max - im_mean)
8+
print("mean pixel value:", im_mean)
9+
print("max pixel value:", im_max)
10+
im_set = np.zeros_like(image)
11+
im_set[image > th] = 1
12+
return im_set
13+
14+
15+
def find_pos_of_max_col_val_per_area(image, led_mask, skip, window_radius):
16+
search_areas_list = []
17+
led_id = 0
18+
for ix in range(window_radius, image.shape[0] - window_radius, skip):
19+
for iy in range(window_radius, image.shape[1] - window_radius, skip):
20+
if led_mask[ix, iy] != 0:
21+
max_x, max_y = find_led_pos(image, ix, iy, window_radius)
22+
search_areas_list.append([led_id, max_x, max_y])
23+
led_id += 1
24+
remove_led_from_mask(led_mask, ix, iy, window_radius)
25+
26+
print('.', end='', flush=True)
27+
search_areas_array = np.array(search_areas_list)
28+
return search_areas_array
29+
30+
31+
def find_led_pos(image, ix, iy, window_radius):
32+
s_radius = window_radius // 2
33+
s = np.index_exp[ix - s_radius:ix + s_radius, iy - s_radius:iy + s_radius]
34+
res = np.unravel_index(np.argmax(image[s]), image[s].shape)
35+
max_x = ix - s_radius + res[0]
36+
max_y = iy - s_radius + res[1]
37+
return max_x, max_y
38+
39+
40+
def remove_led_from_mask(im_set, ix, iy, window_radius):
41+
im_set[ix - window_radius:ix + window_radius, iy - window_radius:iy + window_radius] = 0
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import numpy as np
2+
3+
4+
def get_indices_of_outer_leds(config):
5+
if config['analyse_positions']['line_edge_indices'] == 'None':
6+
config.in_line_edge_indices()
7+
with open('config.ini', 'w') as configfile:
8+
config.write(configfile)
9+
line_edge_indices = config.get2dnparray('analyse_positions', 'line_edge_indices')
10+
# makes sure that line_edge_indices is a 2d list
11+
if len(line_edge_indices.shape) == 1:
12+
line_edge_indices = [line_edge_indices]
13+
return line_edge_indices
14+
15+
16+
def calc_dists_between_led_arrays_and_search_areas(line_edge_indices, search_areas):
17+
distances_led_arrays_search_areas = np.zeros((search_areas.shape[0], len(line_edge_indices)))
18+
19+
xs = search_areas[:, 1]
20+
ys = search_areas[:, 2]
21+
22+
for line_edge_idx in range(len(line_edge_indices)):
23+
i1 = line_edge_indices[line_edge_idx][0]
24+
i2 = line_edge_indices[line_edge_idx][1]
25+
26+
p1x = xs[i1]
27+
p1y = ys[i1]
28+
p2x = xs[i2]
29+
p2y = ys[i2]
30+
31+
pd = np.sqrt((p1x - p2x) ** 2 + (p1y - p2y) ** 2)
32+
d = np.abs(((p2y - p1y) * xs - (p2x - p1x) * ys
33+
+ p2x * p1y - p2y * p1x) / pd)
34+
35+
distances_led_arrays_search_areas[:, line_edge_idx] = d
36+
return distances_led_arrays_search_areas
37+
38+
39+
def match_leds_to_arrays_with_min_dist(dists_led_arrays_search_areas, edge_indices, config, search_areas):
40+
ignore_indices = get_indices_of_ignored_leds(config)
41+
42+
xs = search_areas[:, 1]
43+
ys = search_areas[:, 2]
44+
45+
num_leds = search_areas.shape[0]
46+
47+
# construct 2D array for LED indices sorted by line
48+
led_arrays = []
49+
for edge_idx in edge_indices:
50+
led_arrays.append([])
51+
52+
for iled in range(num_leds):
53+
if iled in ignore_indices:
54+
continue
55+
56+
idx_nearest_array = np.argmin(dists_led_arrays_search_areas[iled, :])
57+
# TODO: ask Lukas for need of following code
58+
59+
# for il_repeat in range(len(edge_indices)):
60+
# i1 = edge_indices[idx_nearest_array][0]
61+
# i2 = edge_indices[idx_nearest_array][1]
62+
#
63+
# x_outer_led1 = xs[i1]
64+
# y_outer_led1 = ys[i1]
65+
# x_outer_led2 = xs[i2]
66+
# y_outer_led2 = ys[i2]
67+
#
68+
# x_led = xs[iled]
69+
# y_led = ys[iled]
70+
#
71+
# dist_led_outer_led1 = np.sqrt((x_outer_led1 - x_led) ** 2 + (y_outer_led1 - y_led) ** 2)
72+
# dist_led_outer_led2 = np.sqrt((x_outer_led2 - x_led) ** 2 + (y_outer_led2 - y_led) ** 2)
73+
# dist_outer_leds = np.sqrt((x_outer_led1 - x_outer_led2) ** 2 + (y_outer_led1 - y_outer_led2) ** 2) + 1e-8
74+
#
75+
# if dist_led_outer_led1 < dist_outer_leds and dist_led_outer_led2 < dist_outer_leds:
76+
# break
77+
#
78+
# dists_led_arrays_search_areas[iled, idx_nearest_array] *= 2
79+
80+
led_arrays[idx_nearest_array].append(iled)
81+
return led_arrays
82+
83+
84+
def get_indices_of_ignored_leds(config):
85+
if config['analyse_positions']['ignore_indices'] != 'None':
86+
ignore_indices = np.array([int(i) for i in config['analyse_positions']['ignore_indices'].split(' ')])
87+
else:
88+
ignore_indices = np.array([])
89+
return ignore_indices

0 commit comments

Comments
 (0)