-
Notifications
You must be signed in to change notification settings - Fork 39
/
removeFamilyGUI.py
188 lines (156 loc) · 6.35 KB
/
removeFamilyGUI.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# REDPy - Repeating Earthquake Detector in Python
# Copyright (C) 2016-2020 Alicia Hotovec-Ellis (ahotovec-ellis@usgs.gov)
# Licensed under GNU GPLv3 (see LICENSE.txt)
import matplotlib
matplotlib.use('TkAgg')
# ^ Not sure why this needs to be here, but apparently Tk hates me otherwise...
import tkinter as tk
import redpy.config
import redpy.table
import argparse
from PIL import Image
import os
import glob
"""
Run this script to manually remove families/clusters (e.g., correlated noise that made it
past the 'junk' detector) using a GUI interface. Reclusters and remakes images when done.
Note: using large NCOLS may make the window too wide for your monitor, and the GUI does
not currently support side scrolling...
usage: removeFamilyGUI.py [-h] [-v] [-c CONFIGFILE]
optional arguments:
-h, --help show this help message and exit
-v, --verbose increase written print statements
-c CONFIGFILE, --configfile CONFIGFILE
use configuration file named CONFIGFILE instead of
default settings.cfg
-n NCOLS, --ncols NCOLS
adjust number of columns in layout (default 3)
-m MINCLUST, --minclust MINCLUST
only look at clusters with numbers at or above MINCLUST
"""
# Define some functions specific to this GUI
def remove(*args):
"""
Run the removal script using checked boxes
"""
print('\nYou have selected the following families to remove:')
removethese = []
for n in range(len(var)):
if var[n].get() > 0:
removethese.append(n+m)
print(removethese)
root.destroy() # Close the window
redpy.table.removeFamilies(rtable, ctable, dtable, ftable, removethese, opt)
if len(removethese) > 0:
print("Creating plots...")
redpy.plotting.createPlots(rtable, ftable, ttable, ctable, otable, opt)
def close(*args):
"""
Close the window and the table
"""
root.destroy()
def onFrameConfigure(canvas):
"""
Reset the scroll region to encompass the inner frame
"""
canvas.configure(scrollregion=canvas.bbox("all"))
def mouse_wheel(event):
"""
Mousewheel scrolling is a bit squiffy
(only scrolls down, but better than nothing?)
"""
canvas.yview_scroll(-1*(event.delta/120), "units")
parser = argparse.ArgumentParser(description=
"Run this script to manually remove families/clusters using a GUI")
parser.add_argument("-v", "--verbose", action="count", default=0,
help="increase written print statements")
parser.add_argument("-c", "--configfile",
help="use configuration file named CONFIGFILE instead of default settings.cfg")
parser.add_argument("-n", "--ncols", default=3, type=int,
help="adjust number of columns in layout (default 3)")
parser.add_argument("-m", "--minclust", default=0, type=int,
help="only look at clusters with numbers at or above MINCLUST")
args = parser.parse_args()
if args.configfile:
opt = redpy.config.Options(args.configfile)
if args.verbose: print("Using config file: {0}".format(args.configfile))
else:
opt = redpy.config.Options("settings.cfg")
if args.verbose: print("Using config file: settings.cfg")
if args.verbose: print("Opening hdf5 table: {0}".format(opt.filename))
h5file, rtable, otable, ttable, ctable, jtable, dtable, ftable = redpy.table.openTable(opt)
# Check for MPL version mismatch
redpy.table.checkMPL(rtable, ftable, ttable, otable, dtable, opt)
oldnClust = ftable.attrs.nClust
if args.minclust:
m = args.minclust
else:
m = 0
# Create GUI window
root = tk.Tk()
root.title("REDPy - Check Families to Permanently Remove")
canvas = tk.Canvas(root, borderwidth=0, width=560*args.ncols, height=1500, background="#ffffff")
frame = tk.Frame(canvas, background="#ffffff")
vsb = tk.Scrollbar(root, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=vsb.set)
vsb.pack(side="right", fill="y")
canvas.pack(side="left", fill="both", expand=True)
canvas.create_window((4,4), window=frame, anchor="nw")
frame.bind("<Configure>", lambda event, canvas=canvas: onFrameConfigure(canvas))
# Build grid of families
fams = range(len(ftable))
r = 1
c = 1
imgobj = []
invimgobj = []
check = []
var = []
for n in fams:
if n >= args.minclust:
im = Image.open('{}{}/clusters/{}.png'.format(opt.outputPath,
opt.groupName,n)).convert('RGB')
im.save('{}{}/clusters/{}.gif'.format(opt.outputPath,opt.groupName,n))
# Create 'inverted' selection image
source = im.split()
blk = source[1].point(lambda i: i*0)
source[1].paste(blk)
source[2].paste(blk)
invim = Image.merge('RGB', source)
invim.save('{}{}/clusters/{}_inv.gif'.format(opt.outputPath,opt.groupName,n))
imgobj.append(tk.PhotoImage(file='{}{}/clusters/{}.gif'.format(
opt.outputPath,opt.groupName,n)))
invimgobj.append(tk.PhotoImage(file='{}{}/clusters/{}_inv.gif'.format(
opt.outputPath,opt.groupName,n)))
var.append(tk.IntVar())
check.append(tk.Checkbutton(frame, image=imgobj[n-m],
variable = var[n-m], selectimage=invimgobj[n-m]).grid(
column=c, row=r, sticky='N'))
c = c+1
if c == args.ncols+1:
c = 1
r = r+1
if r > 255:
print("Ran out of rows. Use -n or -m flags to view more...")
# Add buttons
tk.Button(frame, text="Remove Checked", background="#ffffff", command=remove).grid(
column=1, row=r+1, columnspan=args.ncols, sticky='N')
tk.Button(frame, text="Cancel", background="#ffffff", command=close).grid(
column=1, row=r+2, columnspan=args.ncols, sticky='S')
# Bind MouseWheel, Return, Escape keys to be more useful
root.bind_all("<MouseWheel>", mouse_wheel)
root.bind('<Return>', remove)
root.bind('<Escape>', close)
# Add some padding
for child in frame.winfo_children(): child.grid_configure(padx=15, pady=15)
# Go!
root.mainloop()
# Clean up
print("\nCleaning up .gif files...")
dlist = glob.glob('{}{}/clusters/*.gif'.format(opt.outputPath,opt.groupName))
for tmp in dlist:
os.remove(tmp)
if args.verbose: print("Cleaning up .html files...")
redpy.plotting.cleanHTML(oldnClust, ftable.attrs.nClust, opt)
print("Closing table...")
h5file.close()
print("Done")