Skip to content

Sympy#3

Closed
Margus2004 wants to merge 42 commits into
uellue:mainfrom
Margus2004:sympy
Closed

Sympy#3
Margus2004 wants to merge 42 commits into
uellue:mainfrom
Margus2004:sympy

Conversation

@Margus2004
Copy link
Copy Markdown

No description provided.

uellue and others added 30 commits September 2, 2025 17:14
Basics already work; currently adding a ton of tests (WIP)
Simulate point charge displacement vector field in detector coordinates with model
and validate by guessing CoM parameters with `libertem.udf.com.guess_corrections()`
See LiberTEM/LiberTEM#1775 for discussion!

* Undo sign change between column and pixel y coordinates since that was
  ineffective.
* Add tests to confirm correct behavior of the model
* Add additional tests for CoM consistency
* Make inverted sign the expected behavior
Some functions in TemGymCore rely on `PixelsXY` and `CoordsYX` being arrays,
so we use newly introduced single types here
* Extract mapping matrix from model
* Fast forward simulation with that matrix
* Tests WIP
This allows to better simulate a real scan shift, rotation and scale where
the scan changes relative to the object. Previously, the scan coordinaates
vs. simulated object coordinates were fixed and always identical.

Fix handling of semiconv.

Introduce many tests, incl descan error moving aperture outlines.
Enable float64 everywhere and manually specify inverses for good measure
WIP, flake8 skipped
* Tests for correction and back-projetion
* Adapt OverfocusUDF to work again

FIXME: How to update the parameters of an UDF without changing it? Important to keep plots alive!
* Update UDF to use new functions
* Adapt notebooks to new parameters
* Update tests
* Simulation now includes semiconv, will be important for descan error optimization
The solver converges extremely quickly
The idea is a series of calibration functions to obtain
a fully calibrated parameter set from a number of test measurements.

* Wrapper function to run optax.lbfgs, which works EXTREMELY well on the model.
* High level functions that take meaningful input parameters and starting parameters,
  and return updated parameters. TODO perhaps include the residual in the return value?
* Descan error is WIP, will be based on measurements at different camera lengths.
* Overfocus, rotation and flip TBD, in particular relative to descan error.
One will first have to compensate descan error, and then
adjust other parameters such as flip_y, rotations etc.

This change starts adding functions that allow setting these parameters
while keeping the effect of descan error compensation wrt scan and
detector coordinates the same.

The transformations should be very fast for interactive use. For that reason
they are implemented directly instead of using the solver method.

TODO complete and add tests.
The `adjust_*()` methods allow first compensating the descan error
based on incorrect model parameters, and then adjusting other model
parameters while keeping the descan error compensated, i.e. they change
the descan parameters in such a way that their effect relative to
scan and detector pixel coordinates remains the same.

This is helpful since e.g. calibrating camera length based on known
diffraction angles may need descan compensation if position-averaged
diffraction patterns from a 4D STEM dataset are to be used, and coordinate system
calibration for scan step, scan rotation etc is easier as well if
descan error is removed first.

These methods are implemented as direct transformations rather than using a
solver since they need to be fast enough for immediate response in interactive use.
Also adding a trace() function to reduce boilerplate since we are
just interested in the tracing result from parameters in most cases.

Refactoring all tests WIP
Works in principle, but can confuse downstream users.

I guess it could be better to try preserve the original type of the
input parameter to the optimization routine, but later(TM).
Descan error correction for single camera length works!
uellue and others added 12 commits October 28, 2025 16:31
Currently some issue with the backprojection, details TBD...
* Include relevant projections in GUI
* Introduce corrected picking UDF
* Use stock UDFs where possible instead of re-implementing; remove unnecessary buffers
* Use flip_factor from (-1., 1.) instead of boolean flip_y to aid numerical solving.
  To be tested further! Final user-facing solution TB
* Modify the coordinate refinement to work with reference on both specimen and detector
* Complicate the GUI further

TODO: Tests for new optimization methods
* Sympy is not compatible with jax, that is why injecting numpy backend
* The comparison in sypmy works differntly, "==" does not work as intended
…n for adjusting scan pixel pitch

*sympy based functions: scale(), rotate(), flip_y(), identity(), scale_rotate_flip()
Use sympy functions to calculate sin(), cos() etc.
*generalized function: find a general expression for DescanError while adjusting scan pixel pitch, then convert it to a function for insertinf values and construct new model
*comments: + exchange jnp by np in Parameters4DSTEM for xp value, as symbols are not jax-compatible
+ comment out some assertions with comparisons, as arbitrary symbols compared to a number are undetermined
* Cached class method to generate adjustment function with SymPy
* Distinguish docstring for end users from comments for developers
* A few simplifications
* Add simplify to scale_rotate_flip()
…adjust_scan_pixel_pitch()

* Simplify symbol declaration through symbol_maker()
* Assign symbols to parameters using symbol_maker() in _mk_adjust_scan_pixel_pitch()
* Some simplifications in _mk_adjust_scan_pixel_pitch()
@uellue
Copy link
Copy Markdown
Owner

uellue commented Mar 6, 2026

Hi @Margus2004, this seems to be a duplicate of #2 into the wrong target branch? The development is in branch dev2, this is against main.

@Margus2004 Margus2004 closed this Mar 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants