Skip to content

Commit

Permalink
Merge pull request #77 from AllenNeuralDynamics/wip/bundle-adjustment
Browse files Browse the repository at this point in the history
Display transM info
  • Loading branch information
hannalee2 authored Aug 1, 2024
2 parents eb2e117 + c3e0cae commit 2ea0700
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
2 changes: 1 addition & 1 deletion parallax/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import os

__version__ = "0.37.19"
__version__ = "0.37.20"

# allow multiple OpenMP instances
os.environ["KMP_DUPLICATE_LIB_OK"] = "True"
1 change: 0 additions & 1 deletion parallax/bundle_adjustment.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ def _average_3D_points(self):
# Write the updated DataFrame back to the CSV file
self.df.to_csv(self.file_path, index=False)


def _remove_duplicates(self):
# Drop duplicate rows based on 'ts_local_coords', 'global_x', 'global_y', 'global_z' columns
logger.debug(f"Original rows: {self.df.shape[0]}")
Expand Down
38 changes: 27 additions & 11 deletions parallax/probe_calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
import numpy as np
import pandas as pd
from PyQt5.QtCore import QObject, pyqtSignal
from sklearn.linear_model import LinearRegression
from .coords_transformation import RotationTransformation
from .bundle_adjustmnet import BALProblem, BALOptimizer
from .bundle_adjustment import BALProblem, BALOptimizer

# Set logger name
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -485,7 +484,7 @@ def _is_enough_points(self):
self.transM_LR_prev = self.transM_LR
return False

def _update_info_ui(self, disp_avg_error=False, save_to_csv=False):
def _update_info_ui(self, disp_avg_error=False, save_to_csv=False, file_name=None):
sn = self.stage.sn
if sn is not None and sn in self.stages:
stage_data = self.stages[sn]
Expand All @@ -508,7 +507,6 @@ def _update_info_ui(self, disp_avg_error=False, save_to_csv=False):
)

if save_to_csv:
file_name = f"transM_{sn}.csv"
self._save_transM_to_csv(file_name)

def _save_df_to_csv(self, df, file_name):
Expand Down Expand Up @@ -567,6 +565,24 @@ def reshape_array(self):
global_points = np.array(self.global_points)
return local_points.reshape(-1, 1, 3), global_points.reshape(-1, 1, 3)

def _print_formatted_transM(self):
R = self.transM_LR[:3, :3]
# Extract the translation vector (top 3 elements of the last column)
T = self.transM_LR[:3, 3]
S = self.scale[:3]

print("stage sn: ", self.stage.sn)
print("Rotation matrix:")
print(f" [[{R[0][0]:.5f}, {R[0][1]:.5f}, {R[0][2]:.5f}],")
print(f" [{R[1][0]:.5f}, {R[1][1]:.5f}, {R[1][2]:.5f}],")
print(f" [{R[2][0]:.5f}, {R[2][1]:.5f}, {R[2][2]:.5f}]]")
print("Translation vector:")
print(f" [{T[0]:.1f}, {T[1]:.1f}, {T[2]:.1f}]")
print("Scale:")
print(f" [{S[0]:.5f}, {S[1]:.5f}, {S[2]:.5f}]")
print("==> Average L2 between stage and global: ", self.avg_err)


def update(self, stage, debug_info=None):
"""
Main method to update calibration with a new stage position and check if calibration is complete.
Expand Down Expand Up @@ -601,23 +617,23 @@ def update(self, stage, debug_info=None):
# TODO - Bundle Adjustment
print("\n\n=========================================================")
print("Before BA")
print(" Average L2 between stage and global: ", self.avg_err)
print(self.stage.sn, self.transM_LR, self.scale)
self._print_formatted_transM()
print("=========================================================")

self._update_info_ui(disp_avg_error=True, save_to_csv=True, \
file_name = f"transM_{self.stage.sn}.csv")

if self.model.bundle_adjustment:
ret = self.run_bundle_adjustment(self.file_name)
if ret:
print("\n=========================================================")
print("After BA")
print(" Average L2 between stage and global: ", self.avg_err)
print(self.stage.sn, self.transM_LR, self.scale)
self._print_formatted_transM()
print("=========================================================")
self._update_info_ui(disp_avg_error=True, save_to_csv=True, \
file_name = f"transM_BA_{self.stage.sn}.csv")
else:
return

self._update_info_ui(disp_avg_error=True, save_to_csv=True) # update transformation matrix and overall LR in UI

# Emit the signal to indicate that calibration is complete
self.calib_complete.emit(self.stage.sn, self.transM_LR, self.scale)
logger.debug(
Expand Down

0 comments on commit 2ea0700

Please sign in to comment.