From 77492fc6b83ac507953440db4fd1f6cfd8291ed8 Mon Sep 17 00:00:00 2001 From: Johannes Elferich Date: Tue, 26 Mar 2024 16:40:39 -0400 Subject: [PATCH] 20240322_hotfix --- src/decolace/acquisition/acquisition_area.py | 25 ++++++++++++++----- src/decolace/acquisition/cli_acquisition.py | 26 +++++++++++++++++++- src/decolace/acquisition/grid.py | 4 ++- src/decolace/acquisition/session.py | 18 ++++++++------ 4 files changed, 58 insertions(+), 15 deletions(-) diff --git a/src/decolace/acquisition/acquisition_area.py b/src/decolace/acquisition/acquisition_area.py index aca6ce3..a7b37e7 100644 --- a/src/decolace/acquisition/acquisition_area.py +++ b/src/decolace/acquisition/acquisition_area.py @@ -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 ): @@ -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 diff --git a/src/decolace/acquisition/cli_acquisition.py b/src/decolace/acquisition/cli_acquisition.py index 10f59dd..5b916bd 100644 --- a/src/decolace/acquisition/cli_acquisition.py +++ b/src/decolace/acquisition/cli_acquisition.py @@ -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"), @@ -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( @@ -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() @@ -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}" ) diff --git a/src/decolace/acquisition/grid.py b/src/decolace/acquisition/grid.py index 461741d..5608999 100644 --- a/src/decolace/acquisition/grid.py +++ b/src/decolace/acquisition/grid.py @@ -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) @@ -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( diff --git a/src/decolace/acquisition/session.py b/src/decolace/acquisition/session.py index 1e3fc0c..b73da78 100644 --- a/src/decolace/acquisition/session.py +++ b/src/decolace/acquisition/session.py @@ -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 @@ -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): @@ -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"))