Skip to content

Commit

Permalink
added colorpicker for gui
Browse files Browse the repository at this point in the history
  • Loading branch information
westphallm1 committed Apr 18, 2018
1 parent 09321ba commit 8c240fb
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 17 deletions.
40 changes: 30 additions & 10 deletions specdal/containers/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,21 @@ def data(self):
print("Unexpected exception occurred")
raise e

def _unflagged_data(self):
try:
spectra = [s for s in self.spectra if not s.name in self.flags]
return pd.concat(objs=[s.measurement for s in spectra],
axis=1, keys=[s.name for s in spectra])
except ValueError as err:
# typically from duplicate index due to overlapping wavelengths
if not all([s.stitched for s in self.spectra]):
warnings.warn('ValueError: Try after stitching the overlaps')
return None
except Exception as e:
print("Unexpected exception occurred")
raise e


def append(self, spectrum):
"""
insert spectrum to the collection
Expand Down Expand Up @@ -297,47 +312,52 @@ def to_csv(self, *args, **kwargs):
self.data.transpose().to_csv(*args, **kwargs)
##################################################
# aggregate
def mean(self, append=False):
def mean(self, append=False, ignore_flagged=True):
'''
'''
data = self._unflagged_data() if ignore_flagged else data
spectrum = Spectrum(name=self.name + '_mean',
measurement=self.data.mean(axis=1),
measurement=data.mean(axis=1),
measure_type=self.measure_type)
if append:
self.append(spectrum)
return spectrum
def median(self, append=False):
def median(self, append=False, ignore_flagged=True):
'''
'''
data = self._unflagged_data() if ignore_flagged else data
spectrum = Spectrum(name=self.name + '_median',
measurement=self.data.median(axis=1),
measurement=data.median(axis=1),
measure_type=self.measure_type)
if append:
self.append(spectrum)
return spectrum
def min(self, append=False):
def min(self, append=False, ignore_flagged=True):
'''
'''
data = self._unflagged_data() if ignore_flagged else data
spectrum = Spectrum(name=self.name + '_min',
measurement=self.data.min(axis=1),
measurement=data.min(axis=1),
measure_type=self.measure_type)
if append:
self.append(spectrum)
return spectrum
def max(self, append=False):
def max(self, append=False, ignore_flagged=True):
'''
'''
data = self._unflagged_data() if ignore_flagged else data
spectrum = Spectrum(name=self.name + '_max',
measurement=self.data.max(axis=1),
measurement=data.max(axis=1),
measure_type=self.measure_type)
if append:
self.append(spectrum)
return spectrum
def std(self, append=False):
def std(self, append=False, ignore_flagged=True):
'''
'''
data = self._unflagged_data() if ignore_flagged else data
spectrum = Spectrum(name=self.name + '_std',
measurement=self.data.std(axis=1),
measurement=data.std(axis=1),
measure_type=self.measure_type)
if append:
self.append(spectrum)
Expand Down
88 changes: 81 additions & 7 deletions specdal/gui/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from tkinter import ttk
from tkinter import filedialog
from tkinter import messagebox
from tkinter.colorchooser import askcolor
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -55,14 +56,19 @@ def __init__(self, parent, collection=None, with_toolbar=True):
# pack
self.pack(fill=tk.BOTH,expand=1)
self.last_draw = datetime.now()
self.color = '#000000'


def returnToSelectMode(self):
self.ax.set_navigate_mode(None)
#self.ax.set_navigate(False)

def setupNavBarExtras(self,navbar):
working_dir = os.path.dirname(os.path.abspath(__file__))
self.select_icon = tk.PhotoImage(file=os.path.join(working_dir,"select.png"))

self.select_button = tk.Button(navbar,width="24",height="24",
image=self.select_icon, command = lambda:self.ax.set_navigate_mode(None)).pack(side=tk.LEFT,anchor=tk.W)
image=self.select_icon, command = self.returnToSelectMode).pack(side=tk.LEFT,anchor=tk.W)



Expand All @@ -89,9 +95,27 @@ def rectangleMoveEvent(self,event):
def rectangleEndEvent(self,event):
if self._rect is not None:
self._rect.remove()
else:
#make a small, fake rectangle
class FakeEvent(object):
def __init__(self,x,y):
self.xdata, self.ydata = x, y

self._rect_start = FakeEvent(event.xdata-10,event.ydata+.01)
event = FakeEvent(event.xdata+10,event.ydata-.01)

if not self.collection is None:
x_data = self.collection.data.loc[self._rect_start.xdata:event.xdata]
try:
#if our data is sorted, we can easily isolate it
x_data = self.collection.data.loc[self._rect_start.xdata:event.xdata]
except:
#Pandas builtin throws an error, use another pandas builtin
data = self.collection.data
x0 = min(self._rect_start.xdata,event.xdata)
x1 = max(self._rect_start.xdata,event.xdata)
in_xrange = (data.index >= x0) & (data.index <= x1)
x_data = data.iloc[in_xrange]

ylim = sorted([self._rect_start.ydata,event.ydata])
is_in_box = ((x_data > ylim[0]) & (x_data < ylim[1])).any()

Expand Down Expand Up @@ -204,6 +228,18 @@ def invert_selection(self):
self.listbox.selection_set(i)
self.update_selected()

def change_color(self):
rgb,color = askcolor(self.color)
self.color = color or self.color
self.color_pick.config(bg=self.color)
#update our list of chosen colors
selected = self.listbox.curselection()
selected_keys = [self.collection.spectra[s].name for s in selected]

for key in selected_keys:
self.colors[key] = self.color
self.update()

def create_listbox(self):
self.scrollbar = ttk.Scrollbar(self)
self.listbox = tk.Listbox(self, yscrollcommand=self.scrollbar.set,
Expand All @@ -219,6 +255,16 @@ def create_listbox(self):
).pack(side=tk.TOP,anchor=tk.NW,fill=tk.X)
tk.Button(self.list_tools, text="Invert", command = lambda:self.invert_selection()
).pack(side=tk.TOP,anchor=tk.NW,fill=tk.X)

self.color_field=tk.Frame(self.list_tools)
tk.Label(self.color_field, text="Color:").pack(side=tk.LEFT)

self.color_pick = tk.Button(self.color_field, text="",
command=lambda:self.change_color(), bg='#000000')
self.color_pick.pack(side=tk.RIGHT,anchor=tk.NW,fill=tk.X,expand=True)

self.color_field.pack(side=tk.TOP,anchor=tk.NW,fill=tk.X)

self.list_tools.pack(side=tk.RIGHT,anchor=tk.NW)
self.scrollbar.pack(side=tk.RIGHT,anchor=tk.E, fill=tk.Y)
self.listbox.pack(side=tk.RIGHT,anchor=tk.E, fill=tk.Y)
Expand Down Expand Up @@ -270,6 +316,29 @@ def read_dir(self):
return
c = Collection(name="collection", directory=directory)
self.set_collection(c)

def reset_stats(self):
if self.mean_line:
self.mean_line.remove()
self.mean_line = None
self.mean = False
if self.median_line:
self.median_line.remove()
self.median_line = None
self.median = False
if self.max_line:
self.max_line.remove()
self.max_line = None
self.max = False
if self.min_line:
self.min_line.remove()
self.min_line = None
self.min = False
if self.std_line:
self.std_line.remove()
self.std_line = None
self.std = False

def toggle_mode(self):
if self.spectrum_mode:
self.spectrum_mode = False
Expand All @@ -283,12 +352,18 @@ def toggle_show_flagged(self):
self.show_flagged = True
self.update()
def unflag_all(self):
#new flags -> new statistics
self.reset_stats()

for spectrum in list(self.collection.flags):
self.collection.unflag(spectrum)
self.update()
self.update_list()

def toggle_flag(self):
#new flags -> new statistics
self.reset_stats()

selected = self.listbox.curselection()
keys = [self.listbox.get(s) for s in selected]

Expand Down Expand Up @@ -359,7 +434,7 @@ def update_artists(self,new_lim=False):
if self.show_flagged:
flag_style = 'r'
artists = Collection(name='selection', spectra=spectra).plot(ax=self.ax,
style=list(np.where(flags, flag_style, 'k')),
style=list(np.where(flags, flag_style, self.color)),
picker=1)
self.ax.set_title('selection')
# c = str(np.where(spectrum.name in self.collection.flags, 'r', 'k'))
Expand All @@ -379,6 +454,8 @@ def update_artists(self,new_lim=False):
keys = [s.name for s in self.collection.spectra]
artists = self.ax.lines
self.artist_dict = {key:artist for key,artist in zip(keys,artists)}
self.colors = {key:'black' for key in keys}
self.ax.legend().remove()
self.canvas.draw()

'''
Expand Down Expand Up @@ -444,7 +521,7 @@ def update(self):
else:
self.artist_dict[key].set_visible(False)
else:
self.artist_dict[key].set_color('black')
self.artist_dict[key].set_color(self.colors[key])

'''
self.collection.plot(ax=self.ax,
Expand All @@ -453,9 +530,6 @@ def update(self):
self.ax.set_title(self.collection.name)
'''

# reapply limits
# legend
self.ax.legend().remove()
if self.spectrum_mode:
#self.ax.legend()
pass
Expand Down

0 comments on commit 8c240fb

Please sign in to comment.