AdvancedPhotonSource / tike

Repository for ptychography software
http://tike.readthedocs.io
Other
29 stars 15 forks source link

NEW/BUG: Function to visualize AffineTransform #299

Closed carterbox closed 9 months ago

carterbox commented 9 months ago

Purpose

Add a function to visualize AffineTransform for the purpose of comparing the transform between datasets/reconstruction parameters

Fix a bug where the PositionOptions is deleted when PtychoParameters are resampled.

Approach

The plotting function draws a unit circle transformed by the AffineTransform. Here is an example of what is drawn.

ellipse

Pre-Merge Checklists

Submitter

Reviewer

a4894z commented 9 months ago

In the test_position.py unit test file, I added the following code to the test_fit_linear( ) function:

def test_fit_linear(self):
    """Fit a linear operator instead of a composed affine matrix."""

    T = tike.linalg.lstsq(
        a=np.pad(self.positions0, ((0, 0), (0, 1)), constant_values=1),
        b=self.positions1,
        weights=self.weights,
    )

    result     = tike.ptycho.AffineTransform.fromarray(T)
    result_gto = result.resample( 2.0 )
    result_lto = result.resample( 0.5 )

    f = plt.figure(dpi=600)
    tike.view.plot_affine_transform( ax = plt.gca(), t = result )
    plt.title('testing')
    plt.axis('tight')
    plt.savefig(os.path.join(result_dir, 'result.png'))
    plt.close(f)

    f = plt.figure(dpi=600)
    tike.view.plot_affine_transform( ax = plt.gca(), t = result_gto )
    plt.title('result_resample_gto')
    plt.axis('tight')
    plt.savefig(os.path.join(result_dir, 'result_gto.png'))
    plt.close(f)

    f = plt.figure(dpi=600)
    tike.view.plot_affine_transform( ax = plt.gca(), t = result_lto )
    plt.title('result_resample_lto')
    plt.axis('tight')
    plt.savefig(os.path.join(result_dir, 'result_lto.png'))
    plt.close(f)

    f = plt.figure(dpi=600)
    plt.title('weighted')
    plt.scatter(
        self.positions0[..., 0],
        self.positions0[..., 1],
        marker='o',
    )
    plt.scatter(
        self.positions1[..., 0],
        self.positions1[..., 1],
        marker='o',
        color='red',
        facecolor='None',
    )
    plt.scatter(
        result(self.positions0)[..., 0],
        result(self.positions0)[..., 1],
        marker='x',
    )
    plt.axis('equal')
    plt.legend(['initial', 'final', 'estimated'])
    plt.savefig(os.path.join(result_dir, 'fit-weighted-linear.svg'))
    plt.close(f)

    np.testing.assert_almost_equal(result.asarray3(), T, decimal=3)

The results of these are in the attached image result.png; the plot for result_gto, result_lto, and result are identical.

result

The t0 and t1 values ( row/column translations I'm guessing) in the resulting AffineTransform class are the only differences, as expected.

This looks like it's doing exactly as advertised...not sure what else to test.