Skip to content

Commit

Permalink
support PySide6 for the GetDist GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
cmbant committed Sep 29, 2022
1 parent d0123af commit 5da9ce8
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 87 deletions.
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ Dependencies
* Python 3.6+
* matplotlib 2.2+ (3.1+ recommended)
* scipy
* PySide2 - optional, only needed for GUI
* PySide6 or PySide2 - optional, only needed for GUI
* Working LaTeX installation (not essential, only for some plotting/table functions)

Python distributions like Anaconda have most of what you need (except for LaTeX).

To use the `GUI <https://getdist.readthedocs.io/en/latest/gui.html>`_ you need PySide2.
To use the `GUI <https://getdist.readthedocs.io/en/latest/gui.html>`_ you need PySide.
See the `GUI docs <https://getdist.readthedocs.io/en/latest/gui.html#installation>`_ for suggestions on how to install.

Algorithm details
Expand Down Expand Up @@ -192,7 +192,7 @@ to produce the setting file distparams.ini, edit it, then run with your custom s
GetDist GUI
===================

Run *getdist-gui* to run the graphical user interface. This requires PySide2, but will run on Windows, Linux and Mac.
Run *getdist-gui* to run the graphical user interface. This requires PySide, but will run on Windows, Linux and Mac.
It allows you to open a folder of chain files, then easily select, open, plot and compare, as well as viewing standard GetDist outputs and tables.
See the `GUI Readme <http://getdist.readthedocs.io/en/latest/gui.html>`_.

Expand Down
19 changes: 6 additions & 13 deletions docs/source/gui.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
GetDist GUI
===================

Run the *getdist-gui* script to run the graphical user interface. This requires `PySide2 <https://wiki.qt.io/Qt_for_Python>`_ to be installed, but will run on Windows, Linux and Mac.
Run the *getdist-gui* script to run the graphical user interface. This requires `PySide <https://wiki.qt.io/Qt_for_Python>`_ to be installed, but will run on Windows, Linux and Mac.

It allows you to open a folder of chain files, then easily select, open, plot and compare, as well as viewing standard GetDist outputs and tables.

Expand Down Expand Up @@ -45,22 +45,15 @@ The "Plot module config" option lets you use a different module to define the pl
Installation
##############

To run the GUI you need PySide2. This is not included in default dependencies
because it is only needed for the GUI and on some systems installation from pip may not work easily.
To run the GUI you need PySide. This is not included in default dependencies, but can easily be installed::

The most reliable way to get PySide2 working is to use `Anaconda <https://www.anaconda.com/distribution/>`_, making a consistent new environment from conda-forge (which includes PySide2) e.g. ::
pip install PySide6

conda create -n py39forge -c conda-forge python=3.9 scipy pandas matplotlib PyYAML PySide2

(note that PySide2 is currently not included in the default Anaconda packages). You can also install PySide2 from conda-forge in an existing environment.

You can also install PySide2 from pip using::
You can also use PySide2, e.g. using `Anaconda <https://www.anaconda.com/distribution/>`_ to make a consistent new environment from conda-forge (which includes PySide2) e.g. ::

pip install PySide2

However on some configurations this appears not to work very reliably.
conda create -n py39forge -c conda-forge python=3.9 scipy pandas matplotlib PyYAML PySide2

Once PySide2 is set up, (re)install getdist and you should then be able to use the getdist-gui script on your path.
Once PySide is set up, (re)install getdist and you should then be able to use the getdist-gui script on your path.
On a Mac the installation will also make a GetDist GUI Mac app, which you can find using Spotlight.

If you don't want to install dependencies locally, you can also use a pre-configured `virtual environment <https://cosmologist.info/CosmoBox/>`_.
2 changes: 1 addition & 1 deletion getdist/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__author__ = 'Antony Lewis'
__version__ = "1.3.4"
__version__ = "1.4"
__url__ = "https://getdist.readthedocs.io"

from getdist.inifile import IniFile
Expand Down
2 changes: 1 addition & 1 deletion getdist/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def getdist_gui():
run_gui()
else:
print('GetDist GUI.app not found; not running getdist-gui, getdist package not installed '
'or no valid PySide2 found when setup was run. Running script...')
'or no valid PySide2 or PySide6 found when setup was run. Running script...')
run_gui()
else:
run_gui()
43 changes: 26 additions & 17 deletions getdist/gui/SyntaxHighlight.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from PySide2.QtCore import QRegExp
from PySide2.QtGui import QColor, QTextCharFormat, QFont, QSyntaxHighlighter
try:
from PySide6.QtCore import QRegularExpression
from PySide6.QtGui import QColor, QTextCharFormat, QFont, QSyntaxHighlighter

except ImportError:

from PySide2.QtCore import QRegularExpression
from PySide2.QtGui import QColor, QTextCharFormat, QFont, QSyntaxHighlighter


def txformat(color, style=''):
Expand Down Expand Up @@ -70,8 +76,8 @@ def __init__(self, document):
# Multi-line strings (expression, flag, style)
# FIXME: The triple-quotes in these two lines will mess up the
# syntax highlighting from this point onward
self.tri_single = (QRegExp("'''"), 1, STYLES['string2'])
self.tri_double = (QRegExp('"""'), 2, STYLES['string2'])
self.tri_single = (QRegularExpression("'''"), 1, STYLES['string2'])
self.tri_double = (QRegularExpression('"""'), 2, STYLES['string2'])

rules = []

Expand Down Expand Up @@ -107,24 +113,25 @@ def __init__(self, document):
(r'\b[+-]?[0-9]+(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\b', 0, STYLES['numbers']),
]

# Build a QRegExp for each pattern
# Build a QRegularExpression for each pattern
# noinspection PyArgumentList
self.rules = [(QRegExp(pat), index, fmt)
self.rules = [(QRegularExpression(pat), index, fmt)
for (pat, index, fmt) in rules]

def highlightBlock(self, text):
"""Apply syntax highlighting to the given block of text.
"""
# Do other syntax formatting
for expression, nth, _format in self.rules:
index = expression.indexIn(text, 0)
match = expression.match(text)
index = match.capturedStart()

while index >= 0:
# We actually want the index of the nth match
index = expression.pos(nth)
length = len(expression.cap(nth))
length = match.capturedLength()
self.setFormat(index, length, _format)
index = expression.indexIn(text, index + length)
match = expression.match(text, index + length)
index = match.capturedStart()

self.setCurrentBlockState(0)

Expand All @@ -135,7 +142,7 @@ def highlightBlock(self, text):

def match_multiline(self, text, delimiter, in_state, style):
"""Do highlighting of multi-line strings. ``delimiter`` should be a
``QRegExp`` for triple-single-quotes or triple-double-quotes, and
``QRegularExpression`` for triple-single-quotes or triple-double-quotes, and
``in_state`` should be a unique integer to represent the corresponding
state changes when inside those strings. Returns True if we're still
inside a multi-line string when this function is finished.
Expand All @@ -146,17 +153,18 @@ def match_multiline(self, text, delimiter, in_state, style):
add = 0
# Otherwise, look for the delimiter on this line
else:
start = delimiter.indexIn(text)
# Move past this match
add = delimiter.matchedLength()
match = delimiter.match(text)
start = match.capturedStart()
add = match.capturedLength()

# As long as there's a delimiter match on this line...
while start >= 0:
# Look for the ending delimiter
end = delimiter.indexIn(text, start + add)
match = delimiter.match(text, start+add)
end = match.capturedStart()
# Ending delimiter on this line?
if end >= add:
length = end - start + add + delimiter.matchedLength()
length = end - start + add + match.capturedLength()
self.setCurrentBlockState(0)
# No; multi-line string
else:
Expand All @@ -165,7 +173,8 @@ def match_multiline(self, text, delimiter, in_state, style):
# Apply formatting
self.setFormat(start, length, style)
# Look for the next match
start = delimiter.indexIn(text, start + length)
match = delimiter.match(text, start + length)
start = match.capturedStart()

# Return True if still inside a multi-line string, False otherwise
if self.currentBlockState() == in_state:
Expand Down

0 comments on commit 5da9ce8

Please sign in to comment.