nanopyx.methods.drift_alignment.estimator_table
1import os 2import numpy as np 3from importlib import metadata 4from datetime import datetime 5 6 7class DriftEstimatorTable(object): 8 """ 9 Class used to store DriftAlignment parameters as a dictionary. 10 Parameters can be changes individually by setting the corresponding params key value to desired parameter 11 """ 12 13 def __init__(self): 14 self.params = {} 15 self.params["lib_version"] = metadata.version("nanopyx") 16 self.params["date"] = datetime.today() 17 self.params["apply"] = False 18 self.params["do_batch"] = False 19 self.params["ref_option"] = 1 # 0 if it is to use first frame, 1 if uses the previous frame 20 self.params["time_averaging"] = 1 21 self.params["max_expected_drift"] = 0 22 self.params["normalize"] = True 23 self.params["shift_calc_method"] = "Max Fitting" 24 self.params["use_roi"] = False 25 self.params["roi"] = None 26 self.params["show_ccm"] = True # used for napari 27 self.params["show_drift_plot"] = True # used for napari 28 self.params["show_drift_table"] = True # used for napari 29 self.params["comments"] = None 30 31 self.drift_table = None 32 33 def set_params(self, **kwargs): 34 """ 35 Method used to set the parameters of drift alignment using keyword arguments. 36 :param kwargs: same as self.params.keys() 37 """ 38 for key, value in kwargs.items(): 39 self.params[key] = value 40 41 def set_comments(self, comment_string: str): 42 """ 43 Method used to set comments for drift alignment operation 44 :param comment_string: str, comment text to be added 45 """ 46 self.params["comments"] = comment_string 47 48 def export_npy(self, path: str = None): 49 """ 50 Method used to export drift table as a npy file. 51 :param path: Path to export drift table as npy 52 """ 53 tmp = [] 54 for key in self.params.keys(): 55 tmp.append((key, self.params[key])) 56 tmp.append(self.drift_table) 57 if path is None: 58 path = input("Please provide a filepath to export drift table as npy") + "_drift_table.npy" 59 else: 60 path = os.path.join(path, "_drift_table.npy") 61 62 np.save(path, np.array(tmp, dtype=object)) 63 64 def import_npy(self, path: str = None): 65 """ 66 Method used to import drift table as a npy file. 67 :param path: str, Path to drift table saved as a npy file 68 """ 69 if path is None: 70 path = input("Please provide a filepath to import drift table") 71 72 tmp = np.load(path, allow_pickle=True) 73 74 for i in range(tmp.shape[0] - 1): 75 key, value = tmp[i] 76 self.params[key] = value 77 self.drift_table = tmp[tmp.shape[0] - 1] 78 79 def export_csv(self, path: str = None): 80 """ 81 Method used to export drift table as a csv file. 82 :param path: str, Path to export drift table as csv 83 """ 84 if path is None: 85 path = input("Please provide a filepath to export drift table as csv") + "_drift_table.csv" 86 else: 87 path = os.path.join(path, "_drift_table.csv") 88 89 txt = "" 90 for key in self.params.keys(): 91 txt += key + ";" + str(self.params[key]) + "\n" 92 txt += "Drift Table\n" 93 txt += "XY;X;Y\n" 94 for i in range(self.drift_table.shape[0]): 95 txt += ( 96 str(self.drift_table[i][0]) 97 + ";" 98 + str(self.drift_table[i][1]) 99 + ";" 100 + str(self.drift_table[i][2]) 101 + "\n" 102 ) 103 104 open(path, "w").writelines(txt) 105 106 def import_csv(self, path: str = None): 107 """ 108 Method used to import drift table from a csv file 109 :param path: str, path to import drift table as csv 110 """ 111 if path is None: 112 path = input("Please provide a filepath to import drift table") 113 114 tmp = open(path, "r").readlines() 115 116 count = 0 117 for line in tmp: 118 if line == "Drift Table\n": 119 break 120 else: 121 count += 1 122 param_split = line.split(";") 123 key = param_split[0] 124 value = param_split[1].split("\n")[0] 125 if value == "True": 126 value = True 127 elif value == "False": 128 value = False 129 elif value == "None": 130 value = None 131 self.params[key] = value 132 133 if self.params["roi"] is not None: 134 roi_str_list = self.params["roi"][1:-1].split(", ") 135 self.params["roi"] = tuple([int(coord) for coord in roi_str_list]) 136 137 drift_table = [] 138 139 for row in tmp[count + 2 :]: 140 row_split = row.split(";") 141 drift_xy = float(row_split[0]) 142 drift_x = float(row_split[1]) 143 drift_y = float(row_split[2]) 144 drift_table.append([drift_xy, drift_x, drift_y]) 145 146 self.drift_table = np.array(drift_table)
class
DriftEstimatorTable:
8class DriftEstimatorTable(object): 9 """ 10 Class used to store DriftAlignment parameters as a dictionary. 11 Parameters can be changes individually by setting the corresponding params key value to desired parameter 12 """ 13 14 def __init__(self): 15 self.params = {} 16 self.params["lib_version"] = metadata.version("nanopyx") 17 self.params["date"] = datetime.today() 18 self.params["apply"] = False 19 self.params["do_batch"] = False 20 self.params["ref_option"] = 1 # 0 if it is to use first frame, 1 if uses the previous frame 21 self.params["time_averaging"] = 1 22 self.params["max_expected_drift"] = 0 23 self.params["normalize"] = True 24 self.params["shift_calc_method"] = "Max Fitting" 25 self.params["use_roi"] = False 26 self.params["roi"] = None 27 self.params["show_ccm"] = True # used for napari 28 self.params["show_drift_plot"] = True # used for napari 29 self.params["show_drift_table"] = True # used for napari 30 self.params["comments"] = None 31 32 self.drift_table = None 33 34 def set_params(self, **kwargs): 35 """ 36 Method used to set the parameters of drift alignment using keyword arguments. 37 :param kwargs: same as self.params.keys() 38 """ 39 for key, value in kwargs.items(): 40 self.params[key] = value 41 42 def set_comments(self, comment_string: str): 43 """ 44 Method used to set comments for drift alignment operation 45 :param comment_string: str, comment text to be added 46 """ 47 self.params["comments"] = comment_string 48 49 def export_npy(self, path: str = None): 50 """ 51 Method used to export drift table as a npy file. 52 :param path: Path to export drift table as npy 53 """ 54 tmp = [] 55 for key in self.params.keys(): 56 tmp.append((key, self.params[key])) 57 tmp.append(self.drift_table) 58 if path is None: 59 path = input("Please provide a filepath to export drift table as npy") + "_drift_table.npy" 60 else: 61 path = os.path.join(path, "_drift_table.npy") 62 63 np.save(path, np.array(tmp, dtype=object)) 64 65 def import_npy(self, path: str = None): 66 """ 67 Method used to import drift table as a npy file. 68 :param path: str, Path to drift table saved as a npy file 69 """ 70 if path is None: 71 path = input("Please provide a filepath to import drift table") 72 73 tmp = np.load(path, allow_pickle=True) 74 75 for i in range(tmp.shape[0] - 1): 76 key, value = tmp[i] 77 self.params[key] = value 78 self.drift_table = tmp[tmp.shape[0] - 1] 79 80 def export_csv(self, path: str = None): 81 """ 82 Method used to export drift table as a csv file. 83 :param path: str, Path to export drift table as csv 84 """ 85 if path is None: 86 path = input("Please provide a filepath to export drift table as csv") + "_drift_table.csv" 87 else: 88 path = os.path.join(path, "_drift_table.csv") 89 90 txt = "" 91 for key in self.params.keys(): 92 txt += key + ";" + str(self.params[key]) + "\n" 93 txt += "Drift Table\n" 94 txt += "XY;X;Y\n" 95 for i in range(self.drift_table.shape[0]): 96 txt += ( 97 str(self.drift_table[i][0]) 98 + ";" 99 + str(self.drift_table[i][1]) 100 + ";" 101 + str(self.drift_table[i][2]) 102 + "\n" 103 ) 104 105 open(path, "w").writelines(txt) 106 107 def import_csv(self, path: str = None): 108 """ 109 Method used to import drift table from a csv file 110 :param path: str, path to import drift table as csv 111 """ 112 if path is None: 113 path = input("Please provide a filepath to import drift table") 114 115 tmp = open(path, "r").readlines() 116 117 count = 0 118 for line in tmp: 119 if line == "Drift Table\n": 120 break 121 else: 122 count += 1 123 param_split = line.split(";") 124 key = param_split[0] 125 value = param_split[1].split("\n")[0] 126 if value == "True": 127 value = True 128 elif value == "False": 129 value = False 130 elif value == "None": 131 value = None 132 self.params[key] = value 133 134 if self.params["roi"] is not None: 135 roi_str_list = self.params["roi"][1:-1].split(", ") 136 self.params["roi"] = tuple([int(coord) for coord in roi_str_list]) 137 138 drift_table = [] 139 140 for row in tmp[count + 2 :]: 141 row_split = row.split(";") 142 drift_xy = float(row_split[0]) 143 drift_x = float(row_split[1]) 144 drift_y = float(row_split[2]) 145 drift_table.append([drift_xy, drift_x, drift_y]) 146 147 self.drift_table = np.array(drift_table)
Class used to store DriftAlignment parameters as a dictionary. Parameters can be changes individually by setting the corresponding params key value to desired parameter
def
set_params(self, **kwargs):
34 def set_params(self, **kwargs): 35 """ 36 Method used to set the parameters of drift alignment using keyword arguments. 37 :param kwargs: same as self.params.keys() 38 """ 39 for key, value in kwargs.items(): 40 self.params[key] = value
Method used to set the parameters of drift alignment using keyword arguments.
Parameters
- kwargs: same as self.params.keys()
def
set_comments(self, comment_string: str):
42 def set_comments(self, comment_string: str): 43 """ 44 Method used to set comments for drift alignment operation 45 :param comment_string: str, comment text to be added 46 """ 47 self.params["comments"] = comment_string
Method used to set comments for drift alignment operation
Parameters
- comment_string: str, comment text to be added
def
export_npy(self, path: str = None):
49 def export_npy(self, path: str = None): 50 """ 51 Method used to export drift table as a npy file. 52 :param path: Path to export drift table as npy 53 """ 54 tmp = [] 55 for key in self.params.keys(): 56 tmp.append((key, self.params[key])) 57 tmp.append(self.drift_table) 58 if path is None: 59 path = input("Please provide a filepath to export drift table as npy") + "_drift_table.npy" 60 else: 61 path = os.path.join(path, "_drift_table.npy") 62 63 np.save(path, np.array(tmp, dtype=object))
Method used to export drift table as a npy file.
Parameters
- path: Path to export drift table as npy
def
import_npy(self, path: str = None):
65 def import_npy(self, path: str = None): 66 """ 67 Method used to import drift table as a npy file. 68 :param path: str, Path to drift table saved as a npy file 69 """ 70 if path is None: 71 path = input("Please provide a filepath to import drift table") 72 73 tmp = np.load(path, allow_pickle=True) 74 75 for i in range(tmp.shape[0] - 1): 76 key, value = tmp[i] 77 self.params[key] = value 78 self.drift_table = tmp[tmp.shape[0] - 1]
Method used to import drift table as a npy file.
Parameters
- path: str, Path to drift table saved as a npy file
def
export_csv(self, path: str = None):
80 def export_csv(self, path: str = None): 81 """ 82 Method used to export drift table as a csv file. 83 :param path: str, Path to export drift table as csv 84 """ 85 if path is None: 86 path = input("Please provide a filepath to export drift table as csv") + "_drift_table.csv" 87 else: 88 path = os.path.join(path, "_drift_table.csv") 89 90 txt = "" 91 for key in self.params.keys(): 92 txt += key + ";" + str(self.params[key]) + "\n" 93 txt += "Drift Table\n" 94 txt += "XY;X;Y\n" 95 for i in range(self.drift_table.shape[0]): 96 txt += ( 97 str(self.drift_table[i][0]) 98 + ";" 99 + str(self.drift_table[i][1]) 100 + ";" 101 + str(self.drift_table[i][2]) 102 + "\n" 103 ) 104 105 open(path, "w").writelines(txt)
Method used to export drift table as a csv file.
Parameters
- path: str, Path to export drift table as csv
def
import_csv(self, path: str = None):
107 def import_csv(self, path: str = None): 108 """ 109 Method used to import drift table from a csv file 110 :param path: str, path to import drift table as csv 111 """ 112 if path is None: 113 path = input("Please provide a filepath to import drift table") 114 115 tmp = open(path, "r").readlines() 116 117 count = 0 118 for line in tmp: 119 if line == "Drift Table\n": 120 break 121 else: 122 count += 1 123 param_split = line.split(";") 124 key = param_split[0] 125 value = param_split[1].split("\n")[0] 126 if value == "True": 127 value = True 128 elif value == "False": 129 value = False 130 elif value == "None": 131 value = None 132 self.params[key] = value 133 134 if self.params["roi"] is not None: 135 roi_str_list = self.params["roi"][1:-1].split(", ") 136 self.params["roi"] = tuple([int(coord) for coord in roi_str_list]) 137 138 drift_table = [] 139 140 for row in tmp[count + 2 :]: 141 row_split = row.split(";") 142 drift_xy = float(row_split[0]) 143 drift_x = float(row_split[1]) 144 drift_y = float(row_split[2]) 145 drift_table.append([drift_xy, drift_x, drift_y]) 146 147 self.drift_table = np.array(drift_table)
Method used to import drift table from a csv file
Parameters
- path: str, path to import drift table as csv