-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnapari_utility_functions
More file actions
75 lines (63 loc) · 2.03 KB
/
napari_utility_functions
File metadata and controls
75 lines (63 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
### OPEN EVERYTHING IN NAPARI's ipython.
def view_img(url_image):
"""
This function takes as input the ome.zarr path.
ex. view_img("D:/hybiss_experiment/section.ome.zarr")
will display directly in the napari's image window
"""
viewer.open(url_image, plugin="napari-ome-zarr")
def load_spots(url_spots):
"""
This function takes as input the decoded spots path:
ex. df = load_spots("D:/hybiss_experiment/decoded/decoded_spots.csv")
It returns a pandas DataFrame, so assign to variable (df).
"""
import pandas as pd
df = pd.read_csv(url_spots)
df = df.dropna()
return df
def plot_ts(df, t_name, size, color):
"""
This function takes the df created with the function load_spots,
t_name is the gene name as written in the HybIss codebook (df.target)
size is the desired size of the points
color is the desired color of the points
It plots directly to the napari's image window
"""
t_coords = df[df.target == t_name][["y", "x"]]
viewer.add_points(t_coords, size=size, face_color=color, edge_color="white")
def export_pickled(outname):
"""
This function takes as input the name of a file in which to put
the pickled dictionary {"anatomical_acronym": coordinates}
It saves in a .pkl file in the working directory.
"""
import pickle as pkl
import os
# Delete any layer but the ANNOTATIONS.
data_dict = {}
for i in viewer.layers:
data_dict[i.name] = i.data
# for windows
with open(f"{outname}.pkl", "wb") as f:
pkl.dump(data_dict, f)
def read_pickled(filename):
"""
This function takes as input a .pkl file and
returns the dictionary {"anatomical_acronym": coordinates}
"""
import pickle as pkl
with open(filename, "rb") as f:
polygons = pkl.loads(f.read())
return polygons
def load_shapes(filename):
"""
This function takes as input the .pkl file,
reads it and creates the polygon layers in napari's image window.
"""
polygons = read_pickled(filename)
for i in polygons:
viewer.add_shapes(
polygons[i],
shape_type="polygon",
name=f"{i}")