Skip to content

Commit

Permalink
Cleanup/refactoring/etc complete for now. Tests cleaned up. Potential…
Browse files Browse the repository at this point in the history
… bug in widgets avoided by testing str(self.current_value) to get_value. Comparing self.current_value to float(get_value) is fragile for subclasses etc if ints are used instead of floats. pyqtgraph removed from requirements.txt. App now initiates with a size 10% smaller than the monitor and doesn't change size when toolbars are changed.
  • Loading branch information
sametz committed May 25, 2018
1 parent b7c6573 commit 13e167f
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 89 deletions.
8 changes: 8 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
from nmrmint.controller.controller import Controller

root = tk.Tk()

width, height = root.winfo_screenwidth(), root.winfo_screenheight()
padding_x = int(width * 0.1)
padding_y = int(height * 0.1)
xoffset, yoffset = padding_x / 2, padding_y / 2
geometry = "%dx%d%+d%+d" % (width-padding_x, height-padding_y, xoffset, yoffset)
root.geometry(geometry)

root.title('nmrmint')
app = Controller(root)

Expand Down
2 changes: 2 additions & 0 deletions nmrmint/GUI/toolbardefaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
Update 2018-05-10: Latest python has dicts ordered by default. Consider
revising these types of default dicts throughout NMR projects.
"""
# TODO: currently unused; consider deleting

from collections import OrderedDict

ABdict = OrderedDict([('Jab', 12.0),
Expand Down
2 changes: 1 addition & 1 deletion nmrmint/GUI/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def _entry_is_changed(self):
:return: True if changed, False if not.
"""
get_value = self._value_var.get() # for debugging
return self.current_value != float(get_value)
return str(self.current_value) != get_value

def _save_entry(self):
"""Saves widget's entry as self.stored_value , filling the entry with
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ pytest
numpy
matplotlib
scipy
pyqtgraph
54 changes: 3 additions & 51 deletions tests/GUI/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import pytest

from nmrmint.GUI.history import Subspectrum, History
from nmrmint.GUI.toolbars import FirstOrderBar, SecondOrderBar

from nmrmint.GUI.toolbars import FirstOrderBar

# Before writing these tests, the program was being manually debugged (with
# first-order simulations) .
Expand Down Expand Up @@ -272,9 +271,7 @@ def test_all_spec_data(ss1, ss2, vars_1, vars_2):
"""
# Given a history with two subspectra, each with references to toolbars
# and lineshape data
# toolbar = FirstOrderBar(callback=fake_callback)
history = History()
# history._toolbar = _toolbar
history._subspectra[history.current] = ss1
history.restore()
history._subspectra.append(ss2)
Expand All @@ -292,34 +289,6 @@ def test_all_spec_data(ss1, ss2, vars_1, vars_2):
assert all_spec_data == expected_data


# def test_current_toolbar():
# """Test that current_bar returns the bar for the current subspectrum."""
# # GIVEN a history with a toolbar assigned to its subspectrum
# history = History()
# bar = FirstOrderBar()
# history._subspectra[history.current].toolbar = bar
#
# # WHEN history is asked for its current toolbar
# # THEN the current subspectrum's bar is returned
# assert history.current_toolbar() is bar


# def test_change_toolbar(vars_2):
# """Test to see if the current subspectrum's toolbar can be replaced."""
# # GIVEN an initialized history with default toolbar
# history = History()
# bar_1 = FirstOrderBar(callback=fake_callback)
# history.current_subspectrum().toolbar = bar_1
#
# # WHEN a new toolbar is added
# bar_2 = FirstOrderBar(callback=fake_callback)
# bar_2.reset(vars_2)
# history.change_toolbar(bar_2)
#
# # THEN the current_toolbar is updated
# assert history.current_toolbar() is bar_2


def test_change_toolbar():
"""Test that history.toolbar can be changed via method call.
Expand All @@ -337,23 +306,6 @@ def test_change_toolbar():
assert history._toolbar is toolbar


# def test_change_toolbar_to_second_order_bar():
# """Test to see if change_toolbar works with SecondOrderBar."""
# # GIVEN an initialized history with default first-order toolbar
# history = History()
# bar_1 = FirstOrderBar(callback=fake_callback)
# history.current_subspectrum().toolbar = bar_1
#
# # WHEN a SecondOrderBar is added
# bar_2 = SecondOrderBar(callback=fake_callback)
# history.change_toolbar(bar_2)
#
# # THEN the current_toolbar is updated
# _, _vars = history.subspectrum_data()
# assert _ == 'nspin'
# assert _vars['w'][0][0] == 0.5


def test_delete(ss1, ss2):
"""Test that, when a non-last subspectrum is deleted, it is removed from
history._subspectra."""
Expand All @@ -363,7 +315,6 @@ def test_delete(ss1, ss2):
history = History()
history._toolbar = toolbar
history._subspectra[history.current] = ss1
# history.current = 1
history._subspectra.append(ss2)
assert history.current_subspectrum() is ss1

Expand Down Expand Up @@ -392,7 +343,7 @@ def test_delete_updates_total(ss1, ss2, x1, x2, y1, y2, y_total):
history.current = 1
assert np.array_equal(history.current_subspectrum().y, y2)

# IF told to delete the current spectrum
# WHEN told to delete the current spectrum
history.delete()

# THEN history.total_y has had the deleted spectrum's y subtracted from it
Expand Down Expand Up @@ -485,6 +436,7 @@ def test_save_alerts_if_no_toolbar(capsys):
"""
# GIVEN a History instance with no toolbar recorded
history = History()
# noinspection PyUnusedLocal
out, err = capsys.readouterr() # capture any print statements before save

# WHEN told to save toolbar state
Expand Down
7 changes: 2 additions & 5 deletions tests/GUI/test_toolbars.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

@pytest.fixture()
def dummy_frame():
"""A dummy tk.Frame to pack things into."""
dummy_frame = tk.Frame()
return dummy_frame

Expand Down Expand Up @@ -47,11 +48,7 @@ def test_can_instantiate(self,
"""Confirm a SecondOrderBar can be instantiated with a default frame
as parent.
"""
# GIVEN a default 2-spin SecondOrderBar
# testbar = SecondOrderBar(dummy_frame,
# callback=dummy_controller,
# n=2)

# GIVEN a default 2-spin SecondOrderBar (testbar)
# THEN it is instantiated with the expected .vars
np.testing.assert_equal(testbar.vars, default_nspin_vars)

Expand Down
31 changes: 0 additions & 31 deletions tests/GUI/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@
import numpy as np
import pytest

# from nmrmint.GUI.toolbars import FirstOrderBar
from nmrmint.GUI.widgets import _BaseEntryFrame, ArrayBox


# from nmrmint.initialize import getWINDNMRdefault


@pytest.fixture()
def dummy_toolbar():
frame = tk.Frame()
Expand Down Expand Up @@ -54,39 +50,12 @@ def array_entry_1d(dummy_toolbar):
return widget


# @pytest.fixture()
# def dummy_frame():
# dummy_frame = tk.Frame()
# return dummy_frame


def dummy_controller(*args):
"""For mocking out Toolbar _callback calls."""
print('Controller was passed: ', *args)
pass


# @pytest.fixture()
# def default_nspin_vars():
# """A copy of the default vars that SecondOrderBar should be instantiated
# with.
# """
# v, j = getWINDNMRdefault(2)
# _w_array = np.array([[0.5]])
# _v_ppm = v / 300.0 # Using default spectrometer frequency of 300 MHz
# return {'v': _v_ppm,
# 'j': j,
# 'w': _w_array}
#
#
# @pytest.fixture()
# def testbar(dummy_frame):
# """A default 2-spin SecondOrderBar to be tested."""
# return SecondOrderBar(dummy_frame,
# callback=dummy_controller,
# n=2)


class TestBaseEntryFrame:

def test_widget_instantiates(self, base_entry):
Expand Down
2 changes: 2 additions & 0 deletions tests/human/toolbar_focus.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def dummy_callback(*args, **kwargs):
print(kwargs)


# noinspection PyProtectedMember
def focus_next_entry():
global current_entry
next_entry = current_entry.master._find_next_entry(current_entry)
Expand All @@ -40,6 +41,7 @@ def focus_next_entry():
# Note: immediately packing testbar broke things
testbar = FirstOrderBar(root, callback=dummy_callback) # .pack(side=tk.TOP)
print(type(testbar))
# noinspection PyProtectedMember
first_widget = testbar._fields['# of nuclei']
first_entry = first_widget.entry
current_entry = first_entry
Expand Down
1 change: 1 addition & 0 deletions tests/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
can create data. Should instead save test data in some form for later
retrieval.
"""
# TODO: currently unused; consider deleting

import tkinter as tk

Expand Down

0 comments on commit 13e167f

Please sign in to comment.