From 7f67dde01bdb23302887419f0b1c35228b8da3d1 Mon Sep 17 00:00:00 2001 From: Reto Trappitsch Date: Fri, 5 Apr 2024 10:24:56 +0200 Subject: [PATCH 1/5] edit head space setting to be smarter --- src/rimsschemedrawer/gui.py | 31 +++++++++++++++++++++++++------ src/rimsschemedrawer/plotter.py | 4 +--- src/rimsschemedrawer/utils.py | 12 ++++++++++-- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/rimsschemedrawer/gui.py b/src/rimsschemedrawer/gui.py index 2a84e40..bb30ecb 100644 --- a/src/rimsschemedrawer/gui.py +++ b/src/rimsschemedrawer/gui.py @@ -30,7 +30,7 @@ class SchemeDrawer(QtWidgets.QMainWindow): def __init__(self): # run in debug mode? - self.rundebug = False + self.rundebug = True # program info self.author = "Reto Trappitsch" @@ -392,7 +392,7 @@ def init_ui(self): ) self.edt_sett_headspace.setToolTip( "Set how much space is added on top of the " - "IP level: the head space. Adjust this " + "highest transition: the head space. Adjust this " "value whenever there is not enough space " "on top to fit all the text in. The value " "is given in cm-1." @@ -652,11 +652,11 @@ def check_fields(self): Check if the required fields are filled in. """ # throw an error if not at least one box is filled - if self.edt_level[0].text() == "": + if self.edt_level[0].text() == "" or self.edt_gslevel.text() == "": QtWidgets.QMessageBox.warning( self, "No entries", - "Need at least one level to make a plot!", + "Need at least ground state and one level to make a plot!", QtWidgets.QMessageBox.Ok, ) return False @@ -679,6 +679,9 @@ def plot(self): """ Call the plotting window """ + if not self.check_fields(): + return + # fill default values - if not already there self.fill_default_values() @@ -717,7 +720,16 @@ def load_config(self, **kwargs): # load the json file _load_dict = rimsschemedrawer.json_parser.json_reader(filename) - config_parser = rimsschemedrawer.json_parser.ConfigParser(_load_dict) + try: + config_parser = rimsschemedrawer.json_parser.ConfigParser(_load_dict) + except Exception as err: + QtWidgets.QMessageBox.warning( + self, + "Error", + f"An error occurred while reading the file.\n{'\n'.join(err.args)}", + QtWidgets.QMessageBox.Ok, + ) + return # set the settings for the levels if config_parser.sett_unit_nm: @@ -744,7 +756,11 @@ def load_config(self, **kwargs): self.chk_lowlying[it].setChecked(config_parser.is_low_lying[it]) self.chk_forbidden[it].setChecked(config_parser.step_forbidden[it]) except IndexError: - break + self.edt_level[it].setText("") + self.edt_term[it].setText("") + self.edt_transition_strengths[it].setText("") + self.chk_lowlying[it].setChecked(False) + self.chk_forbidden[it].setChecked(False) # IP level self.cmb_element.setCurrentText(config_parser.element) @@ -931,6 +947,9 @@ def test(self): "Documents/code/RIMSCode/RIMSSchemeDrawer/examples/example_titanium.json" ) self.load_config(fname=fname) + self.user_path = Path( + "/home/reto/Documents/code/RIMSCode/rims-code.github.io/temp/out" + ) class MplCanvas(FigureCanvasQTAgg): diff --git a/src/rimsschemedrawer/plotter.py b/src/rimsschemedrawer/plotter.py index 4487aec..8f96b2f 100644 --- a/src/rimsschemedrawer/plotter.py +++ b/src/rimsschemedrawer/plotter.py @@ -158,10 +158,8 @@ def _plotit(self): # ymax: if ipvalue > totwavenumber_photons + wavenumber_gs: ymax = ipvalue + sett_headspace - elif totwavenumber_photons + wavenumber_gs - ipvalue < sett_headspace: - ymax = ipvalue + sett_headspace else: - ymax = totwavenumber_photons + wavenumber_gs + ymax = totwavenumber_photons + wavenumber_gs + sett_headspace # ### CREATE FIGURE ### a2 = self._axes.twinx() diff --git a/src/rimsschemedrawer/utils.py b/src/rimsschemedrawer/utils.py index 13df4ef..134887a 100644 --- a/src/rimsschemedrawer/utils.py +++ b/src/rimsschemedrawer/utils.py @@ -14,7 +14,7 @@ "fs_axes": 12, "fs_axes_labels": 12, "fs_labels": 12, - "headspace": 3000, + "headspace": 1300, "arrow_width": 0.2, "arrow_head_width": 0.6, "prec_wavelength": 3, @@ -225,14 +225,22 @@ def nm_to_cm_2(nm: Union[float, np.ndarray]) -> Union[float, np.ndarray]: def term_to_string(tstr: str): - """ + """Convert term symbol to LaTeX enabled string. + Converts a term symbol string to a LaTeX enabled matplotlib string + If already a LaTeX string, just return it. :param tstr: Input string to convert :return: Output string LaTeX enabled for Matplotlib """ if tstr == "": return None + latex_characters = ["{", "}", "^", "_"] + for lch in latex_characters: + if lch in tstr: + tstr = tstr.replace(" ", "\\,") + return f"${tstr}$" + # some exceptionslike AI and IP if tstr == "IP": return "IP" From 5bd1982e393358b2bb7b5a784b4865e7d4a04074 Mon Sep 17 00:00:00 2001 From: Reto Trappitsch Date: Fri, 5 Apr 2024 10:25:15 +0200 Subject: [PATCH 2/5] deactivate debug mode --- src/rimsschemedrawer/gui.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rimsschemedrawer/gui.py b/src/rimsschemedrawer/gui.py index bb30ecb..74dfe8b 100644 --- a/src/rimsschemedrawer/gui.py +++ b/src/rimsschemedrawer/gui.py @@ -30,7 +30,7 @@ class SchemeDrawer(QtWidgets.QMainWindow): def __init__(self): # run in debug mode? - self.rundebug = True + self.rundebug = False # program info self.author = "Reto Trappitsch" From 1fdf363924619a8457379e72bb72b10b3f7d3f0e Mon Sep 17 00:00:00 2001 From: Reto Trappitsch Date: Fri, 5 Apr 2024 10:32:22 +0200 Subject: [PATCH 3/5] update pre-commit and workflows --- .github/workflows/package_testing.yml | 8 ++++---- .github/workflows/release.yml | 2 +- .pre-commit-config.yaml | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/package_testing.yml b/.github/workflows/package_testing.yml index 6de4552..31fe2a5 100644 --- a/.github/workflows/package_testing.yml +++ b/.github/workflows/package_testing.yml @@ -23,16 +23,16 @@ jobs: - name: Checkout repo uses: actions/checkout@v4 - name: Install the latest version of rye - uses: eifinger/setup-rye@v1 + uses: eifinger/setup-rye@v2 - name: Sync Rye run: | rye pin ${{ matrix.python-version }} rye sync - name: Run Tests for python interface run: rye run test -# - name: Run Lint on one python -# if: ${{ matrix.python-version == env.MAIN_PYTHON_VERSION }} -# run: rye lint + - name: Run Lint on one python + if: ${{ matrix.python-version == env.MAIN_PYTHON_VERSION }} + run: rye lint - name: Run Pytest with coverage if: ${{ matrix.python-version == env.MAIN_PYTHON_VERSION }} run: | diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 479dedf..0c021a2 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -17,7 +17,7 @@ jobs: - name: Checkout code uses: actions/checkout@v4 - name: Install the latest version of rye - uses: eifinger/setup-rye@v1 + uses: eifinger/setup-rye@v2 - name: Sync Rye run: | rye pin ${{ env.PYTHON_VERSION }} diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d28da8f..a3f62cf 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -6,7 +6,7 @@ repos: - id: trailing-whitespace - repo: https://github.com/astral-sh/ruff-pre-commit # Ruff version. - rev: v0.2.1 + rev: v0.3.5 hooks: # Run the linter. - id: ruff @@ -14,7 +14,7 @@ repos: # Run the formatter. - id: ruff-format - repo: https://github.com/asottile/pyupgrade - rev: v3.15.0 + rev: v3.15.2 hooks: - id: pyupgrade args: [ --py38-plus ] From 12118dd883b6c1546ea3ba005e057e7962aeaeaf Mon Sep 17 00:00:00 2001 From: Reto Trappitsch Date: Fri, 5 Apr 2024 11:20:21 +0200 Subject: [PATCH 4/5] workflow update --- .github/workflows/package_testing.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/package_testing.yml b/.github/workflows/package_testing.yml index 31fe2a5..6db1a21 100644 --- a/.github/workflows/package_testing.yml +++ b/.github/workflows/package_testing.yml @@ -1,6 +1,3 @@ -# This workflow will install Python dependencies, run tests and lint with a variety of Python versions -# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions - name: tests on: From 19c446fd874f1e72e864dc377f7906b41f5fae4b Mon Sep 17 00:00:00 2001 From: Reto Trappitsch Date: Fri, 5 Apr 2024 11:27:46 +0200 Subject: [PATCH 5/5] add tests for latex string --- src/rimsschemedrawer/gui.py | 6 +++--- tests/test_utils.py | 7 +++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/rimsschemedrawer/gui.py b/src/rimsschemedrawer/gui.py index 74dfe8b..c0eb0f3 100644 --- a/src/rimsschemedrawer/gui.py +++ b/src/rimsschemedrawer/gui.py @@ -873,9 +873,9 @@ def write_json(self) -> dict: dispforbidden = "noshow" savedict["settings"]["show_forbidden_transitions"] = dispforbidden savedict["settings"]["plot_title"] = self.edt_sett_plttitle.text() - savedict["settings"][ - "show_transition_strength" - ] = self.chk_sett_trans_strength.isChecked() + savedict["settings"]["show_transition_strength"] = ( + self.chk_sett_trans_strength.isChecked() + ) savedict["settings"]["line_breaks"] = self.chk_sett_linebreaks.isChecked() savedict["settings"]["show_cm-1_axis"] = self.chk_sett_showcmax.isChecked() savedict["settings"]["show_eV_axis"] = self.chk_sett_showevax.isChecked() diff --git a/tests/test_utils.py b/tests/test_utils.py index 65949c0..b3eef29 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -87,6 +87,13 @@ def test_term_to_string(vals): assert ut.term_to_string(vals[0]) == vals[1] +def test_term_to_string_latex(): + """Return a string surrounded by $ if LaTeX characters found.""" + str_in = "^{3}F_{2}" + str_exp = f"${str_in}$" + assert ut.term_to_string(str_in) == str_exp + + @pytest.mark.parametrize("val", ["IP", "AI", "Rydberg", "Ryd"]) def test_term_to_string_no_change(val): """Leave these symbols / strings unchanged."""