From fabaefad583b9bee27e70acfb69880c915ba26d8 Mon Sep 17 00:00:00 2001 From: amitjans <amitjans@ethz.ch> Date: Tue, 1 Jun 2021 16:47:43 +0200 Subject: [PATCH] remove DIST_FACTOR from settings --- calibrate.py | 49 ++++++++++++++++--------------------------------- settings.py | 3 --- tools.py | 5 ++--- 3 files changed, 18 insertions(+), 39 deletions(-) diff --git a/calibrate.py b/calibrate.py index be0534c..ab0e817 100644 --- a/calibrate.py +++ b/calibrate.py @@ -1,52 +1,35 @@ """Calibrate the trap by calculating the distance factor.""" -import fileinput -import re import numpy as np import pandas as pd import redis -import settings from tools import load_image, ion_positions, dark_ions, read_factor def main(): """ Main function """ conn = redis.Redis('localhost', decode_responses=True) - user = input("Load factor value from settings file? ([y]/n) ") - if user in ("y", "", "yes"): - conn.set(name='/image_analysis/distance_factor', value=settings.DIST_FACTOR) - print('The factor is {}'.format(settings.DIST_FACTOR)) + image = load_image() + pos, _ = ion_positions(image) + user_2 = input("Use (detected) dark ions for calibration? (y, [n]) ") + if user_2 in ("y", "yes"): + dist_factor = read_factor(conn) + pos = dark_ions(image, pos, dist_factor) + pos = pos[:, 1] - elif user in ("n", "no"): - image = load_image() - pos, _ = ion_positions(image) - user_2 = input("Use (detected) dark ions for calibration? (y, [n]) ") - if user_2 in ("y", "yes"): - dist_factor = read_factor(conn) - pos = dark_ions(image, pos, dist_factor) - pos = pos[:, 1] + if len(pos) > 1: + print('The detected ions are at positions: ', pos) - if len(pos) > 1: - print('The detected ions are at positions: ', pos) + pos = pos[pos.argsort()] # order + pos = (pos - pos[0])[1:] + mask = (pd.read_csv('data/mask.csv', index_col=0).to_numpy())[len(pos) - 1][1:len(pos)+1] - pos = pos[pos.argsort()] # order - pos = (pos - pos[0])[1:] - mask = (pd.read_csv('data/mask.csv', index_col=0).to_numpy())[len(pos) - 1][1:len(pos)+1] + new_dist_factor = round(np.mean(pos/mask), 1) - new_dist_factor = round(np.mean(pos/mask), 1) - - with fileinput.FileInput('settings.py', inplace=True) as file: - for line in file: - print(re.sub('DIST_FACTOR =.*$', - 'DIST_FACTOR = {}'.format(new_dist_factor), line), end='') - - conn.set(name='/image_analysis/distance_factor', value=new_dist_factor) - - print('The factor is {}'.format(new_dist_factor)) - else: - print('The trap needs to have more than 1 ion to calibrate.') + conn.set(name='/image_analysis/distance_factor', value=new_dist_factor) + print('The factor is {}'.format(new_dist_factor)) else: - print("Character not defined.") + print('The trap needs to have more than 1 ion to calibrate.') if __name__ == "__main__": diff --git a/settings.py b/settings.py index 0579433..4bd6c55 100644 --- a/settings.py +++ b/settings.py @@ -23,9 +23,6 @@ INT_THS = 0.65 # Number of iterations before restarting the position's memory NUM_ITERATIONS = 5 -# Relation factor between pixels and distances -DIST_FACTOR = 70.4 - # Shape of image SHAPE = (960, 1280, 3) diff --git a/tools.py b/tools.py index 806ac12..443b670 100644 --- a/tools.py +++ b/tools.py @@ -582,9 +582,8 @@ def dark_ions(image, coord, dist_factor): coord = coord[coord[:, 1] < image.shape[1]] # remove points outside the image if loss.min() > 20: - # warnings.warn("Loss is too high... " - # "Try calculating a new factor value using calibrate() function.") - pass + warnings.warn("Loss is too high... " + "Try calculating a new factor value using calibrate() function.") return coord -- GitLab