diff --git a/calibrate.py b/calibrate.py index be0534c35c095327ec5de545fa1fc18533443542..ab0e81761f4a9ab75e2f43cd43c2d5b90660233c 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 05794338fcead5cb2b8d6f8501b97db7de0a5065..4bd6c55595ef36bbc3ddcb1219f2ddb52ebf6db9 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 806ac12108946fe10ce116fcf46e6f2ed58bb43e..443b670b75f0e4dcd877547df1ffb80c3c4964d0 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