-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
251 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import glob | ||
import os | ||
from enum import Enum | ||
from pathlib import Path | ||
|
||
import typer | ||
from rich.panel import Panel | ||
from rich.progress import Progress | ||
import numpy as np | ||
import shapely | ||
import signal | ||
|
||
class GracefulKiller: | ||
kill_now = False | ||
def __init__(self): | ||
signal.signal(signal.SIGINT, self.exit_gracefully) | ||
signal.signal(signal.SIGTERM, self.exit_gracefully) | ||
|
||
def exit_gracefully(self, *args): | ||
print("Recieved SIGTERM") | ||
self.kill_now = True | ||
|
||
|
||
|
||
app = typer.Typer() | ||
|
||
|
||
def get_image_fn(dummy): | ||
import serialem as sem | ||
sem.ConnectToSEM(48888,"192.168.122.149") | ||
image = np.asarray(sem.bufferImage("P")).copy() | ||
return image | ||
|
||
|
||
@app.command() | ||
def get_image(): | ||
import napari | ||
from napari.layers import Image | ||
from magicgui import magicgui | ||
from multiprocessing import Pool | ||
|
||
@magicgui(call_button="Get image") | ||
def my_widget() -> Image: | ||
pool = Pool(processes = 1) | ||
image = pool.map(get_image_fn,[0])[0] | ||
return Image(image) | ||
|
||
viewer = napari.Viewer() | ||
viewer.window.add_dock_widget(my_widget) | ||
napari.run() | ||
print("Done") | ||
|
||
@app.command() | ||
def setup_areas( | ||
session_name: str = typer.Option(None, help="Name of the session"), | ||
directory: str = typer.Option(None, help="Directory to save session in"), | ||
): | ||
session_o = load_session(session_name, directory) | ||
|
||
num_items = serialem.ReportNumTableItems() | ||
maps = [] | ||
map_coordinates = [] | ||
map_navids = [] | ||
for i in range(1,int(num_items)+1): | ||
nav_item_info = serialem.ReportOtherItem(i) | ||
nav_id = int(nav_item_info[0]) | ||
nav_note = serialem.GetVariable("navNote") | ||
|
||
if nav_note == "decolace_acquisition_map": | ||
serialem.LoadOtherMap(i,"A") | ||
image = np.asarray(serialem.bufferImage("A")).copy() | ||
maps.append(image) | ||
map_navids.append(nav_id) | ||
map_coordinates.append(nav_item_info[1:3]) | ||
import napari | ||
from napari.layers import Shapes | ||
from magicgui import magicgui | ||
viewer = napari.view_image(np.array(maps)) | ||
|
||
@magicgui(shapes={'label': 'Setup areas'}) | ||
def my_widget(shapes: Shapes): | ||
points = [] | ||
areas = shapes.data | ||
for area in areas: | ||
map_id = area[0,0] | ||
if np.sum(area[:,0] - map_id) != 0: | ||
raise("Error: Map ID is not the same for all points in the polygon") | ||
name = f"area{len(session_o.active_grid.state['acquisition_areas'])+2}" | ||
polygon = shapely.geometry.Polygon(area[:,1:3]) | ||
aa = AcquisitionAreaSingle(name,Path(session_o.active_grid.directory,name).as_posix(),beam_radius=session_o.state["beam_radius"],tilt=session_o.active_grid.state["tilt"]) | ||
aa.initialize_from_napari(map_navids[int(map_id)], [polygon.centroid.y, polygon.centroid.x], area[:,1:3]) | ||
aa.calculate_acquisition_positions_from_napari() | ||
aa.write_to_disk() | ||
session_o.active_grid.state["acquisition_areas"].append([aa.name,aa.directory]) | ||
session_o.active_grid.write_to_disk() | ||
session_o.active_grid.save_navigator() | ||
|
||
|
||
|
||
viewer.window.add_dock_widget(my_widget) | ||
napari.run() | ||
print("Done") | ||
typer.Exit() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import typer | ||
import logging | ||
from rich.logging import RichHandler | ||
from rich import print | ||
from pathlib import Path | ||
|
||
|
||
app = typer.Typer() | ||
|
||
@app.command() | ||
def split_particles_into_optical_groups( | ||
ctx: typer.Context, | ||
tmpackage_star_file: Path, | ||
parameters_star_file: Path, | ||
split_by_session: bool = True, | ||
beam_image_shift_threshold: float = 1.0 | ||
): | ||
import starfile | ||
import pandas as pd | ||
import sqlite3 | ||
from sklearn.cluster import KMeans | ||
|
||
particle_info = starfile.read(tmpackage_star_file) | ||
|
||
aa_fd = pd.DataFrame([aa.model_dump() for aa in ctx.obj.acquisition_areas]) | ||
|
||
|
||
aa_fd["image_folder"] = aa_fd["cistem_project"].map(lambda x: str(Path(x).parent / "Assets" / "Images" )) | ||
aa_fd["session"] = aa_fd["frames_folder"].map(lambda x: str(Path(x).parent.parent.parent.name)) | ||
particle_info["image_folder"] = particle_info["cisTEMOriginalImageFilename"].str.strip("'").map(lambda x: str(Path(x).parent)) | ||
|
||
particle_info["cisTEMOriginalImageFilename"] = particle_info["cisTEMOriginalImageFilename"].str.strip("'") | ||
particle_info = particle_info.join(aa_fd.set_index("image_folder"), on="image_folder", rsuffix="_aa") | ||
for i, cistem_project in enumerate(particle_info["cistem_project"].unique()): | ||
db = sqlite3.connect(cistem_project) | ||
microgaph_info = pd.read_sql_query("SELECT IMAGE_ASSETS.FILENAME, IMAGE_SHIFT_X, IMAGE_SHIFT_Y FROM IMAGE_ASSETS INNER JOIN MOVIE_ASSETS ON IMAGE_ASSETS.PARENT_MOVIE_ID=MOVIE_ASSETS.MOVIE_ASSET_ID INNER JOIN MOVIE_ASSETS_METADATA ON MOVIE_ASSETS.MOVIE_ASSET_ID=MOVIE_ASSETS_METADATA.MOVIE_ASSET_ID", db) | ||
if i == 0: | ||
particle_info = particle_info.merge(microgaph_info, right_on="FILENAME", left_on="cisTEMOriginalImageFilename",suffixes=(None,None),how="left") | ||
particle_info.set_index("cisTEMOriginalImageFilename", inplace=True) | ||
else: | ||
particle_info.update(microgaph_info.set_index("FILENAME")) | ||
|
||
particle_info["image_shift_label"] = particle_info.apply(lambda x: f"{x['session']}_{x['IMAGE_SHIFT_X'] // 1}_{x['IMAGE_SHIFT_Y'] // 1}", axis=1) | ||
particle_info.reset_index(inplace=True) | ||
print(particle_info["image_shift_label"].value_counts()) | ||
|
||
|
||
|