Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ee7b55b
dummy monitor doesn't crash anymore + optional napari monitor should …
ShantanuKodgirwar Oct 9, 2026
9bafacb
minor correction with super() call
ShantanuKodgirwar Oct 9, 2026
7523987
class for the image refresh logic
ShantanuKodgirwar Oct 9, 2026
068f6e7
minor fix
ShantanuKodgirwar Oct 9, 2026
866c35b
holds reference to the live object and probe along with other parameters
ShantanuKodgirwar Oct 9, 2026
9ebd106
abstract monitor calls the existing methods and the subclasses method…
ShantanuKodgirwar Oct 9, 2026
452a048
some duplicated code is simplified + other changes that uses monitor …
ShantanuKodgirwar Oct 9, 2026
dd0c0e0
adds parameter history plot to trace parameters that have changed
ShantanuKodgirwar Oct 9, 2026
accbce2
removing beam width and overlap from tracked values
ShantanuKodgirwar Oct 9, 2026
cb69ee0
reconstruction.saveResults now takes the optional argument snapshot=m…
ShantanuKodgirwar Oct 9, 2026
cd28147
adds angle correction for monitoring
ShantanuKodgirwar Oct 9, 2026
ee55869
minor updates
ShantanuKodgirwar Oct 9, 2026
5ea348b
logs some messages only once while running
ShantanuKodgirwar Oct 9, 2026
8ba3182
using tqdm trange to make sure the progress bar appears only once: af…
ShantanuKodgirwar Oct 9, 2026
afb7faf
ignore some files
ShantanuKodgirwar Oct 9, 2026
e021dbd
black formatter
ShantanuKodgirwar Oct 9, 2026
0562a6b
fpm sampling test updated
ShantanuKodgirwar Oct 9, 2026
1bbfcd3
adds test for monitor frame
ShantanuKodgirwar Oct 9, 2026
1cb9196
tests the new parameter history plotting
ShantanuKodgirwar Oct 9, 2026
c51234e
minor change to the sim to show that we can now store snapshot with t…
ShantanuKodgirwar Oct 9, 2026
e534ba3
some summary message after run
ShantanuKodgirwar Oct 9, 2026
7503ff5
stores message afterwards
ShantanuKodgirwar Oct 9, 2026
c20968d
example monitor GUI to explore different reconstruction options
ShantanuKodgirwar Oct 9, 2026
6fc748c
ePIE had a commented out reconstruction
ShantanuKodgirwar Oct 9, 2026
c6dfa32
FPM didn't close filenames
ShantanuKodgirwar Oct 9, 2026
0736299
removing legacy dead code + docstring is updated
ShantanuKodgirwar Oct 9, 2026
39f9f15
minor reformatting
ShantanuKodgirwar Oct 9, 2026
b4cf966
updates docstrings
ShantanuKodgirwar Oct 9, 2026
81c0765
some unused import cleanup and other things
ShantanuKodgirwar Oct 9, 2026
d18e35d
adding some type hints
ShantanuKodgirwar Oct 9, 2026
81cccc2
updates to some tests
ShantanuKodgirwar Oct 9, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
*.vscode
*.venv
logs_tensorboard
*.hdf5
*.lock
.python-version
.DS_Store
.idea
Expand All @@ -18,4 +16,9 @@ site/
.cache/mkdocs-jupyter/

# misc
notes/
notes/

# files
*.hdf5
*.png
*.lock
254 changes: 118 additions & 136 deletions PtyLab/Engines/BaseEngine.py

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions PtyLab/Engines/OPR.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import logging
import sys

import tqdm
from tqdm.auto import trange # widget bar in Jupyter, text bar in a terminal

from PtyLab.Engines.BaseEngine import BaseEngine
from PtyLab.ExperimentalData.ExperimentalData import ExperimentalData
Expand All @@ -26,7 +26,6 @@


class OPR(BaseEngine):

def __init__(
self,
reconstruction: Reconstruction,
Expand Down Expand Up @@ -79,9 +78,7 @@ def reconstruct(self):
)

# actual reconstruction ePIE_engine
self.pbar = tqdm.trange(
self.numIterations, desc="OPR", file=sys.stdout, leave=True
)
self.pbar = trange(self.numIterations, desc="OPR", file=sys.stdout, leave=True)
for loop in self.pbar:
self.it = loop
# set position order
Expand Down Expand Up @@ -205,8 +202,13 @@ def _orthogonalizeIncoherentModes_batched(self):
# stays bounded regardless of frame count; the batched call is already
# wide enough at a few dozen frames to hide launch overhead.
elements_per_frame = nModes * n * n
chunk = int(max(1, self._orthogonalization_chunk_bytes //
(elements_per_frame * stack.dtype.itemsize)))
chunk = int(
max(
1,
self._orthogonalization_chunk_bytes
// (elements_per_frame * stack.dtype.itemsize),
)
)

flat = stack[0, 0, :, 0, :, :, :].reshape(nModes, n * n, nFrames)
for start in range(0, nFrames, chunk):
Expand Down
6 changes: 2 additions & 4 deletions PtyLab/Engines/aPIE.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from typing import Any

import numpy as np
import tqdm
from matplotlib import pyplot as plt
from scipy.interpolate import interp2d
from tqdm.auto import trange # widget bar in Jupyter, text bar in a terminal

from PtyLab.utils.visualisation import hsvplot

Expand Down Expand Up @@ -83,9 +83,7 @@ def doReconstruction(self):
self.thetaSearchRadiusMax, self.thetaSearchRadiusMin, self.numIterations
)

self.pbar = tqdm.trange(
self.numIterations, desc="aPIE", file=sys.stdout, leave=True
)
self.pbar = trange(self.numIterations, desc="aPIE", file=sys.stdout, leave=True)
for loop in self.pbar:
# save theta search history
self.reconstruction.thetaHistory = np.append(
Expand Down
8 changes: 3 additions & 5 deletions PtyLab/Engines/e3PIE.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np
import tqdm
from matplotlib import pyplot as plt
from tqdm.auto import trange # widget bar in Jupyter, text bar in a terminal

try:
import cupy as cp
Expand All @@ -25,7 +25,6 @@


class e3PIE(BaseEngine):

def __init__(
self,
reconstruction: Reconstruction,
Expand Down Expand Up @@ -76,7 +75,7 @@ def reconstruct(self):
# get module
xp = getArrayModule(self.reconstruction.object)

self.pbar = tqdm.trange(
self.pbar = trange(
self.numIterations, desc="e3PIE", file=sys.stdout, leave=True
)

Expand Down Expand Up @@ -123,7 +122,6 @@ def reconstruct(self):
]
# update object slice
for loopTemp in range(self.reconstruction.nslice - 1):

sliceLoop = self.reconstruction.nslice - 1 - loopTemp

# temp_delta = self.reconstruction.esw[..., sliceLoop, sy, sx]
Expand All @@ -137,7 +135,7 @@ def reconstruct(self):
)
)
# eswTemp update (here probe incident on last slice)
beth = 0.9 # todo, why need beth, not betaProbe, changable?
beth = 0.9 # todo, why need beth, not betaProbe, changable?
self.reconstruction.probe[:, :, :, sliceLoop, ...] = (
self.probeUpdate(
objectPatch[:, :, :, sliceLoop, ...],
Expand Down
22 changes: 11 additions & 11 deletions PtyLab/Engines/ePIE.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import sys

import numpy as np
import tqdm
from matplotlib import pyplot as plt
from tqdm.auto import trange # widget bar in Jupyter, text bar in a terminal

from PtyLab.Engines.BaseEngine import BaseEngine
from PtyLab.ExperimentalData.ExperimentalData import ExperimentalData
Expand Down Expand Up @@ -66,6 +66,7 @@ class ePIE(BaseEngine):
imaging," Ultramicroscopy 109, 1256-1262 (2009).
https://doi.org/10.1016/j.ultramic.2009.05.012
"""

def __init__(
self,
reconstruction: Reconstruction,
Expand Down Expand Up @@ -169,8 +170,9 @@ def reconstruct_stepwise(self, experimentalData: ExperimentalData = None):
probe estimate is updated sequentially across all scan positions.

After all scan positions in an iteration have been processed,
`getErrorMetrics()` evaluates the reconstruction error and
`applyConstraints()` applies the enabled reconstruction constraints.
`getErrorMetrics()` evaluates the reconstruction error,
`applyConstraints()` applies the enabled reconstruction constraints and
`showReconstruction()` updates the monitor.

The method yields after every scan-position update, allowing custom code
to be interleaved with the reconstruction.
Expand All @@ -190,9 +192,7 @@ def reconstruct_stepwise(self, experimentalData: ExperimentalData = None):
self._prepareReconstruction()

# actual reconstruction ePIE_engine
self.pbar = tqdm.trange(
self.numIterations, desc="ePIE", file=sys.stdout, leave=True
)
self.pbar = trange(self.numIterations, desc="ePIE", file=sys.stdout, leave=True)
for loop in self.pbar:
# set position order
self.setPositionOrder()
Expand Down Expand Up @@ -234,10 +234,10 @@ def reconstruct_stepwise(self, experimentalData: ExperimentalData = None):
object_patch = self.objectPatchUpdate(objectPatch, DELTA)

self.reconstruction.object[..., sy, sx] = object_patch
#self.reconstruction.object[..., sy, sx] = self.objectPatchUpdate(

# self.reconstruction.object[..., sy, sx] = self.objectPatchUpdate(
# objectPatch, DELTA
#)
# )

# probe update
self.reconstruction.probe = self.probeUpdate(objectPatch, DELTA)
Expand All @@ -255,8 +255,8 @@ def reconstruct_stepwise(self, experimentalData: ExperimentalData = None):
# apply Constraints
self.applyConstraints(loop)

# show reconstruction
# self.showReconstruction(loop)
# update the monitor, as every other engine does after the constraints
self.showReconstruction(loop)

if self.params.gpuFlag:
self.logger.info("switch to cpu")
Expand Down
21 changes: 10 additions & 11 deletions PtyLab/Engines/mPIE.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import sys
import warnings

import tqdm
from tqdm.auto import tqdm, trange # widget bar in Jupyter, text bar in a terminal

from PtyLab.Engines.BaseEngine import BaseEngine
from PtyLab.ExperimentalData.ExperimentalData import ExperimentalData
Expand Down Expand Up @@ -98,6 +98,7 @@ class mPIE(BaseEngine):
Baseline ePIE reconstruction without rPIE regularization or
momentum acceleration.
"""

def __init__(
self,
reconstruction: Reconstruction,
Expand Down Expand Up @@ -127,7 +128,7 @@ def __init__(
monitor (Monitor):
Monitor used for reconstruction visualization and progress
reporting.
"""
"""
super().__init__(reconstruction, experimentalData, params, monitor)
self.logger = logging.getLogger("mPIE")
self.logger.info("Successfully created mPIE engine")
Expand Down Expand Up @@ -200,7 +201,9 @@ def initializeReconstructionParams(self):

self.reconstruction.probeWindow = np.abs(self.reconstruction.probe)

def reconstruct(self, experimentalData=None, reconstruction=None, vis_after_each_iteration=None):
def reconstruct(
self, experimentalData=None, reconstruction=None, vis_after_each_iteration=None
):
r"""
Run the mPIE reconstruction to completion.

Expand Down Expand Up @@ -273,13 +276,11 @@ def reconstruct(self, experimentalData=None, reconstruction=None, vis_after_each
self.reconstruction.objectBuffer = self.reconstruction.object.copy()
self.reconstruction.probeBuffer = self.reconstruction.probe.copy()
# actual reconstruction MPIE_engine
self.pbar = tqdm.trange(
self.numIterations, desc="mPIE", file=sys.stdout, leave=True
)
self.pbar = trange(self.numIterations, desc="mPIE", file=sys.stdout, leave=True)
for loop in self.pbar:
# set position order
self.setPositionOrder()
self.pbar_pos = tqdm.tqdm(
self.pbar_pos = tqdm(
self.positionIndices, leave=False, desc="ptychogram", file=sys.stdout
)
for positionLoop, positionIndex in enumerate(self.pbar_pos):
Expand Down Expand Up @@ -329,9 +330,7 @@ def reconstruct(self, experimentalData=None, reconstruction=None, vis_after_each
# self.reconstruction.push_probe_update(self.reconstruction.probe, positionIndex, self.experimentalData.ptychogram.shape[0])

if self.params.positionCorrectionSwitch:
self.positionCorrection(
objectPatch, positionIndex, sy, sx
)
self.positionCorrection(objectPatch, positionIndex, sy, sx)
# self.pbar_pos.write(f'Corr: {shifter[0]*1e6:.2f} um x {shifter[1]*1e6:.2f} um')

# momentum updates
Expand Down Expand Up @@ -610,4 +609,4 @@ def stepM(self):

@stepM.setter
def stepM(self, value):
self.frictionM = value
self.frictionM = value
4 changes: 2 additions & 2 deletions PtyLab/Engines/mqNewton.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import logging
import sys

import tqdm
from tqdm.auto import trange # widget bar in Jupyter, text bar in a terminal

from PtyLab.Engines.BaseEngine import BaseEngine
from PtyLab.ExperimentalData.ExperimentalData import ExperimentalData
Expand Down Expand Up @@ -85,7 +85,7 @@ def reconstruct(self, experimentalData: ExperimentalData = None):
self._prepareReconstruction()
self.initializeAdaptiveMomentum()

self.pbar = tqdm.trange(
self.pbar = trange(
self.numIterations, desc="mqNewton", file=sys.stdout, leave=True
)
for loop in self.pbar:
Expand Down
4 changes: 2 additions & 2 deletions PtyLab/Engines/multiPIE.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import logging
import sys

import tqdm
from tqdm.auto import trange # widget bar in Jupyter, text bar in a terminal

from PtyLab.Engines.BaseEngine import BaseEngine
from PtyLab.ExperimentalData.ExperimentalData import ExperimentalData
Expand Down Expand Up @@ -68,7 +68,7 @@ def initializeReconstructionParams(self):
def reconstruct(self):
self._prepareReconstruction()

self.pbar = tqdm.trange(
self.pbar = trange(
self.numIterations, desc="multiPIE", file=sys.stdout, leave=True
)

Expand Down
4 changes: 2 additions & 2 deletions PtyLab/Engines/qNewton.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import logging
import sys

import tqdm
from tqdm.auto import trange # widget bar in Jupyter, text bar in a terminal

from PtyLab.Engines.BaseEngine import BaseEngine
from PtyLab.ExperimentalData.ExperimentalData import ExperimentalData
Expand Down Expand Up @@ -57,7 +57,7 @@ def reconstruct(self, experimentalData: ExperimentalData = None):
self.experimentalData = experimentalData
self._prepareReconstruction()

self.pbar = tqdm.trange(
self.pbar = trange(
self.numIterations, desc="qNewton", file=sys.stdout, leave=True
)
for loop in self.pbar:
Expand Down
4 changes: 2 additions & 2 deletions PtyLab/Engines/zPIE.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np
import tqdm
from matplotlib import pyplot as plt
from tqdm.auto import trange # widget bar in Jupyter, text bar in a terminal

try:
import cupy as cp
Expand Down Expand Up @@ -112,7 +112,7 @@ def reconstruct(self, experimentalData=None, reconstruction=None):
X, Y = xp.meshgrid(xp.arange(-n // 2, n // 2), xp.arange(-n // 2, n // 2))
w = xp.exp(-((xp.sqrt(X**2 + Y**2) / self.reconstruction.Np) ** 4))

self.pbar = tqdm.trange(
self.pbar = trange(
self.numIterations, desc="zPIE", file=sys.stdout, leave=True
) # in order to change description to the tqdm progress bar
for loop in self.pbar:
Expand Down
Loading
Loading