Skip to content

Commit

Permalink
Progress toward python 3 support (#54)
Browse files Browse the repository at this point in the history
- close_all function in tracker
- Import Qt modules from pyqtgraph instead of PyQt4
  - Should allow them to be sourced from PyQt5 when working in python3, as pyqtgraph already handles this
- Lead pgmpl submodule imports with pgmpl.*
  - `import pgmpl.info` instead of `import info`
- Adjust dealias for python 3 compatibility
  - Stuff with iterators and dictionaries
- Remove leading zeros
  - https://stackoverflow.com/a/36386530/6605826
  • Loading branch information
eldond authored Aug 3, 2018
1 parent bd96af4 commit 242df74
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 35 deletions.
8 changes: 4 additions & 4 deletions pgmpl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
import os

# GUI imports
from PyQt4 import QtGui
from pyqtgraph import QtGui

# Plotting imports
import pyqtgraph as pg
from matplotlib import rcParams

# pgmpl imports
from info import * # Defines __version__, etc.
from util import printd
from translate import color_translator
from pgmpl.info import * # Defines __version__, etc.
from pgmpl.util import printd
from pgmpl.translate import color_translator

__all__ = ['figure', 'axes', 'pyplot', 'translate', 'text', 'util']

Expand Down
12 changes: 6 additions & 6 deletions pgmpl/axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@

# pgmpl
# noinspection PyUnresolvedReferences
import __init__ # __init__ does setup stuff like making sure a QApp exists
from translate import plotkw_translator, color_translator, setup_pen_kw, color_map_translator, dealias
from legend import Legend
from util import printd, tolist, is_numeric
from text import Text
from contour import QuadContourSet
import pgmpl.__init__ # __init__ does setup stuff like making sure a QApp exists
from pgmpl.translate import plotkw_translator, color_translator, setup_pen_kw, color_map_translator, dealias
from pgmpl.legend import Legend
from pgmpl.util import printd, tolist, is_numeric
from pgmpl.text import Text
from pgmpl.contour import QuadContourSet


class Axes(pg.PlotItem):
Expand Down
4 changes: 2 additions & 2 deletions pgmpl/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import pyqtgraph as pg

# pgmpl
import __init__
from util import printd, tolist
import pgmpl.__init__
from pgmpl.util import printd, tolist


class ColorbarBase(object):
Expand Down
6 changes: 3 additions & 3 deletions pgmpl/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

# pgmpl
# noinspection PyUnresolvedReferences
import __init__ # __init__ does setup stuff like making sure a QApp exists
from translate import plotkw_translator, color_translator, setup_pen_kw, color_map_translator, dealias
from util import printd, tolist, is_numeric
import pgmpl.__init__ # __init__ does setup stuff like making sure a QApp exists
from pgmpl.translate import plotkw_translator, color_translator, setup_pen_kw, color_map_translator, dealias
from pgmpl.util import printd, tolist, is_numeric


class ContourSet(object):
Expand Down
10 changes: 5 additions & 5 deletions pgmpl/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@

# pgmpl
# noinspection PyUnresolvedReferences
import __init__ # __init__ does setup stuff like making sure a QApp exists
from tracking import tracker
from axes import Axes
from util import printd, tolist
from colorbar import Colorbar
import pgmpl.__init__ # __init__ does setup stuff like making sure a QApp exists
from pgmpl.tracking import tracker
from pgmpl.axes import Axes
from pgmpl.util import printd, tolist
from pgmpl.colorbar import Colorbar


class Figure(pg.PlotWidget):
Expand Down
4 changes: 2 additions & 2 deletions pgmpl/legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

# pgmpl
# noinspection PyUnresolvedReferences
import __init__ # __init__ does setup stuff like making sure a QApp exists
from util import printd, tolist, is_numeric
import pgmpl.__init__ # __init__ does setup stuff like making sure a QApp exists
from pgmpl.util import printd, tolist, is_numeric


class Legend:
Expand Down
8 changes: 4 additions & 4 deletions pgmpl/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
import numpy as np

# pgmpl
from tracking import tracker
from figure import Figure
from axes import Axes
from util import printd
from pgmpl.tracking import tracker
from pgmpl.figure import Figure
from pgmpl.axes import Axes
from pgmpl.util import printd


def figure(*args, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion pgmpl/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

# Plotting imports
import pyqtgraph as pg
from translate import color_translator, dealias
from pgmpl.translate import color_translator, dealias


class Text(pg.TextItem):
Expand Down
6 changes: 5 additions & 1 deletion pgmpl/tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import warnings

# pgmpl
from util import printd
from pgmpl.util import printd


class WTracker:
Expand All @@ -34,5 +34,9 @@ def window_opened(self, win):
def status(self):
printd(' {} tracked windows = {}'.format(len(self.open_windows), self.open_windows))

def close_all(self):
while len(self.open_windows):
self.open_windows[0].close()


tracker = WTracker()
8 changes: 4 additions & 4 deletions pgmpl/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import numpy as np

# Plotting imports
from PyQt4 import QtCore
from pyqtgraph import QtCore
import pyqtgraph as pg
import matplotlib.cm
from matplotlib import rcParams
Expand All @@ -27,7 +27,7 @@
to_rgba = colorConverter.to_rgba

# pgmpl imports
from util import set_debug, printd, tolist
from pgmpl.util import set_debug, printd, tolist


def dealias(**kws):
Expand All @@ -49,12 +49,12 @@ def dealias(**kws):
'verticalalignment': ['va'],
'horizontalalignment': ['ha'],
}
for primary, aliases in alias_lists.iteritems():
for primary, aliases in list(alias_lists.items()): # https://stackoverflow.com/a/13998534/6605826
aliasv = {alias: kws.pop(alias, missing_value_mark) for alias in aliases}
not_missing = [v != missing_value_mark for v in aliasv.values()]
if primary not in kws.keys() and any(not_missing):
# The aliases only need be considered if the primary is missing.
aliasu = np.atleast_1d(aliasv.keys())[np.atleast_1d(not_missing)][0]
aliasu = np.atleast_1d(list(aliasv.keys()))[np.atleast_1d(not_missing)][0]
kws[primary] = aliasv[aliasu]
printd(" assigned kws['{}'] = kws.pop('{}')".format(primary, aliasu))
else:
Expand Down
4 changes: 2 additions & 2 deletions tests/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ def demo_plot():
axs[1, 1].errorbar(-x[40:45], y1[40:45], y1[40:45] * 0.1, x[40:45] * 0.05, color='g', xuplims=True, xlolims=True)

axs[2, 0].plot(x, y1, color='m', marker='o', label='y1 purple circles')
axs[2, 0].scatter([0, 2, 4, 6, 8, 10], [80, 40, 90, 10, 20, 05], c=['r', 'b', 'g', 'k', 'm', 'y'], linewidths=1)
axs[2, 0].scatter([0, 2, 4, 6, 8, 10], [80, 40, 90, 10, 20, 5], c=['r', 'b', 'g', 'k', 'm', 'y'], linewidths=1)
axs[2, 0].scatter(x, x*0+60, c=x, marker='v', s=4, edgecolors=' ')
axs[2, 0].scatter(x, x*0+50, c=x, marker='s', s=5, cmap='plasma', linewidths=0)
axs[2, 0].scatter([0, 2, 4, 6, 8, 10], [50, 90, 80, 00, 50, 90], c=[90, 0, 50, 20, 75, 66],
axs[2, 0].scatter([0, 2, 4, 6, 8, 10], [50, 90, 80, 0, 50, 90], c=[90, 0, 50, 20, 75, 66],
marker='^', linewidths=2, edgecolors='r')
axs[2, 0].scatter(x, x*0-10, c=x, cmap='jet', marker=None,
verts=[(0, 0), (0.5, 0.5), (0, 0.5), (-0.5, 0), (0, -0.5), (0.5, -0.5)])
Expand Down
7 changes: 7 additions & 0 deletions tests/test_tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# pgmpl
from pgmpl import __init__ # __init__ does setup stuff like making sure a QApp exists
from pgmpl.tracking import WTracker, tracker
from pgmpl.figure import Figure


class TestPgmplTracking(unittest.TestCase):
Expand All @@ -28,6 +29,7 @@ def printv(self, *args):

def test_tracker(self):
assert isinstance(tracker, WTracker)
tracker.close_all()
dummy = 'dummy_window'
open_windows0 = copy.deepcopy(tracker.open_windows)
tracker.window_opened(dummy)
Expand All @@ -53,6 +55,11 @@ def test_tracker_warnings(self):
self.printv(' test_tracker_warnings: called window_closed with a nonsense window to trigger a warning '
'and got {}/{} warnings.'.format(len(w), warnings_expected))

def test_close_all(self):
Figure()
tracker.close_all()
assert len(tracker.open_windows) == 0

def setUp(self):
test_id = self.id()
test_name = '.'.join(test_id.split('.')[-2:])
Expand Down
2 changes: 1 addition & 1 deletion tests/test_translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import os
import unittest
import numpy as np
from PyQt4 import QtCore, QtGui
from pyqtgraph import QtCore, QtGui
import copy
from matplotlib import rcParams

Expand Down

0 comments on commit 242df74

Please sign in to comment.