-
Notifications
You must be signed in to change notification settings - Fork 0
/
flip-particle-y.py
executable file
·149 lines (122 loc) · 4.46 KB
/
flip-particle-y.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python
from cryosparc.tools import CryoSPARC
import json
import numpy as np
import argparse
from pathlib import Path
def main(args):
with open(Path('~/instance-info.json').expanduser(), 'r') as f:
instance_info = json.load(f)
cs = CryoSPARC(**instance_info)
assert cs.test_connection()
project_uid = args.project
workspace_uid = args.workspace
job_uids = args.job
project = cs.find_project(project_uid)
flip_job = project.create_external_job(
workspace_uid,
"Flipped particles"
)
for i, jid in enumerate(job_uids):
out_name = args.particle_output_name[i]
job = project.find_job(jid)
particles = job.load_output(out_name)
if not args.no_plots:
import matplotlib.pyplot as plt
mic_uid = particles["location/micrograph_uid"][0]
first_mic_particles = particles.query({"location/micrograph_uid": mic_uid})
_, mic = project.download_mrc(first_mic_particles["location/micrograph_path"][0])
mic = mic[0,:,:]
height, width = mic.shape
fig, ax = plt.subplots(figsize = (10,10))
ax.axis(False)
ax.imshow(
mic,
cmap = "Greys_r",
origin = "lower",
vmin = np.percentile(mic, 5),
vmax = np.percentile(mic, 95)
)
ax.plot(
first_mic_particles["location/center_x_frac"] * width,
first_mic_particles["location/center_y_frac"] * height,
color = "#2c2c2c",
label= "Before flip",
linestyle = "",
marker = "o",
markersize = width / 1000,
markeredgecolor = "white"
)
ax.plot(
first_mic_particles["location/center_x_frac"] * width,
(1 - first_mic_particles["location/center_y_frac"]) * height,
color = "#FAB06E",
label = "After flip",
linestyle = "",
marker = "P",
markersize = width / 1000,
markeredgecolor = "white"
)
ax.legend(loc = "lower left")
particles["location/center_y_frac"] = 1 - particles["location/center_y_frac"]
particle_src_name = f"particles_{jid}"
flip_job.add_input(
type = "particle",
name = particle_src_name
)
flip_job.connect(
target_input = particle_src_name,
source_job_uid = jid,
source_output = out_name
)
flip_job.add_output(
type = "particle",
name = particle_src_name,
title = f"Flipped particles {jid}",
slots = ["location"],
passthrough = particle_src_name,
alloc = particles
)
flip_job.save_output(particle_src_name, particles)
if not args.no_plots:
flip_job.log_plot(fig, f"Before/after flip for {jid}")
flip_job.stop()
parser = argparse.ArgumentParser(
usage="flip-particle-y.py {PID} {WID} {JID} {...more JIDs}"
)
parser.add_argument(
"project",
help = "Project UID, e.g., P123"
)
parser.add_argument(
"workspace",
help = "Workspace UID, e.g., W3"
)
parser.add_argument(
"job",
help = "Job(s) with particles to flip. Give as space-separated list, e.g., J1 J2 J3 J4",
nargs="+",
metavar="job(s)"
)
parser.add_argument(
"--particle-output-name",
help = "Name of the particles output. Give as a single value (e.g., particles) or a list of values the same length as the number of jobs, with each job's particle name (e.g., particles imported_particles)",
nargs = "+",
default = ["particles"]
)
parser.add_argument(
"--no-plots",
help = "Do not produce plots",
action = "store_true"
)
if __name__ == "__main__":
args = parser.parse_args()
args.project = args.project if "P" in args.project else f"P{args.project}"
args.workspace = args.workspace if "W" in args.workspace else f"W{args.workspace}"
args.job = [jid if "J" in jid else f"J{jid}" for jid in args.job]
print(args)
if len(args.job) > 1 and len(args.particle_output_name) == 1:
args.particle_output_name = args.particle_output_name * len(args.job)
else:
assert len(args.job) == len(args.particle_output_name), "Give a single particle output name or a list the same length as the jobs"
main(args)