Skip to content

Commit

Permalink
20240322_hotfix
Browse files Browse the repository at this point in the history
  • Loading branch information
jojoelfe committed Mar 26, 2024
1 parent c6e68fc commit 77492fc
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 15 deletions.
25 changes: 19 additions & 6 deletions src/decolace/acquisition/acquisition_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,16 @@ def load_from_disk(self):
most_recent = sorted(potential_files)[-1]
self.state = np.load(most_recent, allow_pickle=True).item()


def save_map(self, map):
import mrcfile

mrcfile.new(os.path.join(self.directory, self.name + "_map.mrc"), map)

def load_map(self):
import mrcfile

return mrcfile.read(os.path.join(self.directory, self.name + "_map.mrc"))

def initialize_from_napari(
self, map_navigator_id: int, center_coordinate, corner_coordinates, affine= None
):
Expand Down Expand Up @@ -402,24 +411,28 @@ def acquire(
fraction_of_gradient = (np.cos((index % self.state.defocus_steps)/self.state.defocus_steps * 2* np.pi) + 1) /2
self.state.desired_defocus = self.state.low_defocus + fraction_of_gradient * (self.state.high_defocus-self.state.low_defocus)
offset = self.state.desired_defocus - measured_defocus

true_offset = offset

if abs(offset) > self.state.ctf_max_step and established_lock:
offset = self.state.ctf_max_step * np.sign(offset)
if num_max_correction == 0:
offset_before_max_correction = abs(offset)
offset_before_max_correction = abs(true_offset)
num_max_correction += 1

else:
if abs(offset) > self.state.ctf_max_step_initially:
offset = self.state.ctf_max_step_initially * np.sign(offset)
if num_max_correction == 0:
offset_before_max_correction = abs(offset)
offset_before_max_correction = abs(true_offset)
num_max_correction += 1
else:
num_max_correction = 0
if num_max_correction > 3 and abs(offset) > offset_before_max_correction:
report["potential_overfocus"] = True
if num_max_correction > 2 and abs(true_offset) > offset_before_max_correction:
correction_attempt = -1.6 * true_offset
print(f"Overfocus, trying to correct by {correction_attempt} instead of {offset}")
offset = correction_attempt

#report["potential_overfocus"] = True

if abs(offset) < 0.1 and not established_lock and last_bs_correction < 0.06:
established_lock = True
Expand Down
26 changes: 25 additions & 1 deletion src/decolace/acquisition/cli_acquisition.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,19 @@ def print_aa_state(
for aa in session_o.active_grid.acquisition_areas:
print(aa.state)

@app.command()
def set_aa_state_abort(
name: str = typer.Option(None, help="Name of the session"),
directory: str = typer.Option(None , help="Directory to save session in"),
id: int = typer.Option(...),
):
session_o = load_session(name, directory)
for i, aa in enumerate(session_o.active_grid.acquisition_areas):
if i == id:
aa.state.aborted = True
aa.write_to_disk()
print(aa.state.aborted)

@app.command()
def set_session_state(
name: str = typer.Option(None, help="Name of the session"),
Expand Down Expand Up @@ -454,7 +467,15 @@ def nice_view_button() -> napari.types.LayerDataTuple:
pool = Pool(processes=1)
image = pool.map(get_nice_view,[session_o])[0]
existing_data = viewer.layers


@app.command()
def monitor(
session_name: str = typer.Option(None, help="Name of the session"),
directory: str = typer.Option(None, help="Directory to save session in"),
):
serialem = connect_sem()
session_o = load_session(session_name, directory)


@app.command()
def setup_areas(
Expand Down Expand Up @@ -508,6 +529,7 @@ def my_widget(shapes: Shapes, use_square_beam: bool = False, start_from_bottom:
aa.initialize_from_napari(map_navids[int(map_id)], [polygon.centroid.y, polygon.centroid.x], area[:,1:3])
aa.calculate_acquisition_positions_from_napari(beam_radius=session_o.state.beam_radius, use_square_beam=use_square_beam, start_from_bottom=start_from_bottom)
aa.write_to_disk()
aa.save_map(maps[int(map_id)])
session_o.active_grid.state.acquisition_areas.append([aa.name,aa.directory])
session_o.active_grid.acquisition_areas.append(aa)
session_o.active_grid.write_to_disk()
Expand Down Expand Up @@ -585,11 +607,13 @@ def acquire(
def progress_callback(grid, report, acquisition_area, type=None):
global numbad
if type == "start_new_area":
numbad = 0
progress.console.log(
f"Starting acquisition of area {acquisition_area.name} in grid {grid.name}"
)

elif type == "resume_area":
numbad = 0
progress.console.log(
f"Resuming acquisition of area {acquisition_area.name} in grid {grid.name}"
)
Expand Down
4 changes: 3 additions & 1 deletion src/decolace/acquisition/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def write_to_disk(self, save_areas=False):
def load_from_disk(self):
potential_files = glob.glob(os.path.join(self.directory, self.name + "_*.npy"))
if len(potential_files) < 1:
raise (FileNotFoundError("Couldn't find saved files"))
raise (FileNotFoundError("Couldn't find saved files grid"))
most_recent = sorted(potential_files)[-1]
self.state = np.load(most_recent, allow_pickle=True).item()
#self.state = GridState(**self.state)
Expand Down Expand Up @@ -145,6 +145,8 @@ def start_acquisition(self, initial_defocus, progress_callback=None):
aa.state.positions_acquired
):
continue
if aa.state.aborted:
continue
serialem.SetImageShift(0.0, 0.0)
if np.sum(aa.state.positions_acquired) == 0:
progress_callback(
Expand Down
18 changes: 11 additions & 7 deletions src/decolace/acquisition/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class SessionState(BaseModel):
active_grid: Optional[int] = None
beam_radius: Optional[float] = None
fringe_free_focus_vacuum: Optional[float] = None
min_defocus_for_ffsearch: Optional[float] = 30
max_defocus_for_ffsearch: Optional[float] = 80
min_defocus_for_ffsearch: Optional[float] = -10
max_defocus_for_ffsearch: Optional[float] = 10
fringe_free_focus_cross_grating: Optional[float] = None
dose_rate_e_per_pix_s: Optional[float] = None
unbinned_pixel_size_A: Optional[float] = None
Expand Down Expand Up @@ -69,8 +69,8 @@ def load_from_disk(self):
new_grid = grid(grid_info[0], grid_info[1])
new_grid.load_from_disk()
self.grids.append(new_grid)
except FileNotFoundError:
print(f"Can't load grid {grid_info[0]}")
except FileNotFoundError as e:
print(f"Can't load grid {grid_info[0]} {e}")


def add_grid(self, name, tilt):
Expand Down Expand Up @@ -160,9 +160,13 @@ def prepare_beam_cross_final(self):
print(f"Error could not center beam")
return

#serialem.MoveStage(0,0, -50.0)
wanted_defocus = -1.0

#serialem.MoveStage(0,0, 10.0)
wanted_defocus = -1.0
# Get Focus using record mode wihtout adjusting focus
#measured_defocus = serialem.G(-1,-1)
#defocus_error = wanted_defocus - measured_defocus
#serialem.MoveStage(0,0,defocus_error)

serialem.Record()
serialem.FFT("A")
powerspectrum = np.asarray(serialem.bufferImage("AF"))
Expand Down

0 comments on commit 77492fc

Please sign in to comment.