<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Pixel -> Stage calibration for overviews
Step1:  set paths
Step2:  do a dummy overview scan
please enter the coordinates of the field you wish to calibrate as (left, right, top, bottom) and the optical configuration you are using for overviews.
Running the cell will do a dummy overview and leave the resulting image open in NIS.
! The z-position will be recorded to the calibration file, so please focus on a slide !
Step3:  corresponding points
Now, please collect 3 pairs of pixel (x,y)-coordinates and stage (um) coordinates.
To do so, hover over the image, the pixel coordinates will be displayed on the bottom of the image window.
If you right-click, the corresponding stage coordinates will be shown under Move this point to center.
if you use binning, please multiply the pixel coordinates with the binning factor
Step4:  save the calibration
execute the following to save the calibration as a JSON-file.
Step5:  test the transformation
below, you can test the transformation by entering new pixel coordinates and checking if the output corresponds to the stage coordinates shown in NIS.
<ASSISTANT_TASK:>
Python Code:
import os
import logging
import json
from nis_util import do_large_image_scan, set_optical_configuration, get_position
logging.basicConfig(format='%(asctime)s - %(levelname)s in %(funcName)s: %(message)s', level=logging.DEBUG)
logger = logging.getLogger(__name__)
Explanation: Pixel -> Stage calibration for overviews
End of explanation
path_to_nis = 'C:\\Program Files\\NIS-Elements\\nis_ar.exe'
save_base_path = 'C:\\Users\\Nikon\\Documents\\David\\overview_calibrations'
calibration_name = 'right_color_251019.json'
if not os.path.exists(save_base_path):
    os.makedirs(save_base_path)
    
if os.path.exists(os.path.join(save_base_path, calibration_name)):
    logger.warning('output file already exists, will be overwritten')
Explanation: set paths
End of explanation
calibration_fov = (-26500,
  -50000,
  -21053,
  18177)
calibration_oc = 'DIA4x'
left, right, top, bottom = calibration_fov
set_optical_configuration(path_to_nis, calibration_oc)
do_large_image_scan(path_to_nis, '', left, right, top, bottom, close=False)
pos = get_position(path_to_nis)
Explanation: do a dummy overview scan
please enter the coordinates of the field you wish to calibrate as (left, right, top, bottom) and the optical configuration you are using for overviews.
Running the cell will do a dummy overview and leave the resulting image open in NIS.
! The z-position will be recorded to the calibration file, so please focus on a slide !
End of explanation
coords1px = (2071*3, 2273*3)
coords1st = (-29777, -17306)
coords2px = (11437*3, 6683*3)
coords2st = (-49516, -7747)
coords3px = (2873*3, 10802*3)
coords3st = (-30954, 1180)
Explanation: corresponding points
Now, please collect 3 pairs of pixel (x,y)-coordinates and stage (um) coordinates.
To do so, hover over the image, the pixel coordinates will be displayed on the bottom of the image window.
If you right-click, the corresponding stage coordinates will be shown under Move this point to center.
if you use binning, please multiply the pixel coordinates with the binning factor
End of explanation
res = {}
res['bbox'] = calibration_fov
res['coords_px'] = [coords1px, coords2px, coords3px]
res['coords_st'] = [coords1st, coords2st, coords3st]
res['zpos'] = pos[2]
with open(os.path.join(save_base_path, calibration_name), 'w') as fd:
    json.dump(res, fd, indent=1)
    print('saved calibration: \n\n{}'.format(json.dumps(res, indent=1)))
Explanation: save the calibration
execute the following to save the calibration as a JSON-file.
End of explanation
import numpy as np
from skimage.transform import AffineTransform
test_coords = (10116*3, 15814*3)
coords_px = np.array([coords1px, coords2px, coords3px], dtype=np.float)
coords_st = np.array([coords1st, coords2st, coords3st], dtype=np.float)
at = AffineTransform()
at.estimate(coords_px, coords_st)
at(np.array(test_coords)).ravel()
from resources import left_color_calib
import numpy as np
from skimage.transform import AffineTransform
test_coords = (11159*3, 19913*3)
field_def_file = left_color_calib
with open(field_def_file, 'r') as fd:
    field_calib = json.load(fd)
coords_px = np.array(field_calib['coords_px'], dtype=np.float)
coords_st = np.array(field_calib['coords_st'], dtype=np.float)
at = AffineTransform()
at.estimate(coords_px, coords_st)
at.params, at(np.array(test_coords)).ravel()
Explanation: test the transformation
below, you can test the transformation by entering new pixel coordinates and checking if the output corresponds to the stage coordinates shown in NIS.
End of explanation
<END_TASK>