diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 1382f59f5573ee5883d3f5362e14d8c1efc54cfe..9f33d61a27b224c0bd0e572e81b09f6fdeaca587 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -18,17 +18,6 @@ before_script: - python -m pip install --upgrade pip - python -m pip install hatch -test: - stage: test - script: - - python -m hatch run tests:run - artifacts: - when: always - paths: - - pytest_report.xml - reports: - junit: pytest_report.xml - build: stage: build script: @@ -37,7 +26,7 @@ build: paths: - dist/*.whl -prepare_release_and_deploy2zenodo: +prepare_deploy: stage: build image: name: alpine:latest @@ -76,8 +65,21 @@ prepare_release_and_deploy2zenodo: paths: - artifacts/* +test: + stage: test + needs: [] + script: + - python -m hatch run tests:run + artifacts: + when: always + paths: + - pytest_report.xml + reports: + junit: pytest_report.xml + pages: stage: deploy + needs: [] script: - python -m hatch run doc:build artifacts: @@ -91,11 +93,6 @@ pages: release: stage: deploy - needs: - - job: "build" - artifacts: true - - job: "prepare_release_and_deploy2zenodo" - artifacts: true rules: - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && '$CI_COMMIT_TAG =~ CALVER_REGEX_DEV' when: on_success @@ -117,10 +114,7 @@ release: url: '$CI_PROJECT_URL/-/jobs/$CI_JOB_ID/artifacts/file/dist/python_spectrometer-$VERSION-py3-none-any.whl' deploy2zenodo: - stage: "deploy" - needs: - - job: "prepare_release_and_deploy2zenodo" - artifacts: true + stage: deploy rules: - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && '$CI_COMMIT_TAG =~ $CALVER_REGEX' when: on_success @@ -128,14 +122,11 @@ deploy2zenodo: when: manual variables: DEPLOY2ZENODO_API_URL: https://zenodo.org/api - DEPLOY2ZENODO_DEPOSITION_ID: "create NEW record" + DEPLOY2ZENODO_DEPOSITION_ID: 13785490 DEPLOY2ZENODO_SKIP_PUBLISH: "true" pypi: stage: deploy - needs: - - job: "build" - artifacts: true script: - pip install -U twine - twine upload -u "$TWINE_USERNAME" -p "$TWINE_PASSWORD" dist/* diff --git a/src/python_spectrometer/__init__.py b/src/python_spectrometer/__init__.py index 6ed29b45aa9da87e82f86459c0cbeffef6de5935..100664c1e6deb0b684a906b7a33ec1a1bcb57520 100644 --- a/src/python_spectrometer/__init__.py +++ b/src/python_spectrometer/__init__.py @@ -121,7 +121,7 @@ In this short demonstration, we reproduce the example from Averaging the PSD yields the noise power on the signal. ->>> np.mean(spect[0]['S_processed'][0][256:]) +>>> float(np.mean(spect[0]['S_processed'][0][256:])) 0.0009997881856675976 Computing the power spectrum instead yields an estimate for the RMS @@ -132,7 +132,7 @@ of the peak. The 'flattop' window seems to give a more accurate result. >>> spect.reprocess_data(0, window='flattop') >>> # Need to get from plot since internal data is unchanged >>> data = spect.ax[0].lines[0].get_ydata() ->>> data.max() +>>> float(data.max()) 2.009491183836163 Finally, we can also plot data in dB relative to a given dataset. @@ -142,7 +142,7 @@ Finally, we can also plot data in dB relative to a given dataset. >>> spect.plot_dB_scale = True >>> spect.set_reference_spectrum(0) >>> data = spect.ax[0].lines[1].get_ydata() ->>> data.max() # Factor two in amplitude is approx 3 dB +>>> float(data.max()) # Factor two in amplitude is approx 3 dB 3.0284739712568682 See the documentation of :class:`~core.Spectrometer` and its methods diff --git a/src/python_spectrometer/core.py b/src/python_spectrometer/core.py index f033e1b00ffe092022da65f83b657f1799c3a666..b0fe1d1df6a808b7d85d4fdf7f93d88e3e447aef 100644 --- a/src/python_spectrometer/core.py +++ b/src/python_spectrometer/core.py @@ -13,7 +13,6 @@ from typing import (Any, Callable, Dict, Generator, Iterator, List, from unittest import mock import dill -import matplotlib.pyplot as plt import numpy as np from matplotlib import colors from qutil import io @@ -319,9 +318,6 @@ class Spectrometer: else: return super().__repr__() - def __del__(self): - plt.close(self.fig) - def __getitem__(self, key: _keyT) -> Dict[str, Any]: return self._data[self._parse_keys(key)[0]] @@ -364,7 +360,7 @@ class Spectrometer: @savepath.setter def savepath(self, path): - self._savepath = self._resolve_path(path) + self._savepath = io.to_global_path(path) def _resolve_path(self, file: _pathT) -> Path: """Resolve file to a fully qualified path.""" @@ -984,7 +980,6 @@ class Spectrometer: -------- :meth:`recall_from_disk` """ - # shelve writes three files, .dat, .bak, and .dir. Only need to check for one if file is None: file = self._objfile file = io.check_path_length( diff --git a/tests/test_serialization.py b/tests/test_serialization.py index bdc82d3635415129e57dc2e949cc6f1eac862f85..a553f09636954df3df3dea02c2bf62ece2117d23 100644 --- a/tests/test_serialization.py +++ b/tests/test_serialization.py @@ -1,6 +1,7 @@ import os import pathlib -from tempfile import mkdtemp +import random +import string import pytest @@ -8,9 +9,13 @@ from python_spectrometer import Spectrometer, daq @pytest.fixture(params=[True, False]) -def spectrometer(request) -> Spectrometer: - speck = Spectrometer(daq.QoptColoredNoise(), savepath=mkdtemp(), +def spectrometer(monkeypatch, request) -> Spectrometer: + # patch input to answer overwrite queries with "yes" + monkeypatch.setattr('builtins.input', lambda: 'y') + + speck = Spectrometer(daq.QoptColoredNoise(), savepath=pathlib.Path(os.getcwd(), 'data'), plot_cumulative=True, relative_paths=request.param) + speck.savepath.mkdir(parents=True, exist_ok=True) cwd = os.getcwd() os.chdir(speck.savepath) @@ -26,12 +31,18 @@ def spectrometer(request) -> Spectrometer: @pytest.fixture def serialized(spectrometer: Spectrometer) -> pathlib.Path: - spectrometer.serialize_to_disk('blub') + stem = ''.join(random.choices(string.ascii_letters, k=10)) + spectrometer.serialize_to_disk(stem) - yield spectrometer.savepath / 'blub' + yield spectrometer.savepath / stem - for ext in ['.bak', '.dat', '.dir', '_files.txt']: - os.remove(spectrometer.savepath / f'blub{ext}') + exts = ['_files.txt'] + if (spectrometer.savepath / stem).is_file(): + os.remove(spectrometer.savepath / stem) + else: + exts.extend(['.bak', '.dat', '.dir']) + for ext in exts: + os.remove(spectrometer.savepath / f'{stem}{ext}') def test_saving(spectrometer: Spectrometer): @@ -42,7 +53,13 @@ def test_saving(spectrometer: Spectrometer): def test_serialization(spectrometer: Spectrometer): spectrometer.serialize_to_disk('blub') - for ext in ['.bak', '.dat', '.dir', '_files.txt']: + + exts = ['_files.txt'] + if (spectrometer.savepath / 'blub').is_file(): + assert os.path.exists(spectrometer.savepath / 'blub') + else: + exts.extend(['.bak', '.dat', '.dir']) + for ext in exts: assert os.path.exists(spectrometer.savepath / f'blub{ext}')