Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix code snippets in documentation for doctest #1440

Merged
merged 8 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Root level conftest to ignore scripts folder in doctests.
def pytest_ignore_collect(path):
if "scripts/" in str(path):
return True
if "docs/" in str(path):
return True
13 changes: 6 additions & 7 deletions pyaerocom/_lowlevel_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ class ConstrainedContainer(BrowseDict):

This class enables to create dict-like objects that have a fixed set of
keys and value types (once assigned). Optional values may be instantiated
as None, in which case the first time instantiation definecs its type.
as None, in which case the first time instantiation defines its type.

Note
----
Expand All @@ -346,12 +346,11 @@ class ConstrainedContainer(BrowseDict):

Example
-------
class MyContainer(ConstrainedContainer):
def __init__(self):
self.val1 = 1
self.val2 = 2
self.option = None

>>> class MyContainer(ConstrainedContainer):
... def __init__(self):
... self.val1 = 1
... self.val2 = 2
... self.option = None
>>> mc = MyContainer()
>>> mc['option'] = 42
"""
Expand Down
52 changes: 26 additions & 26 deletions pyaerocom/aeroval/config/emep/emep4no_base_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,32 @@ def get_CFG(year: int, model_dir: str, *, file_pattern: str = r"^RERUN2022_{freq
>>> import pathlib
>>>
>>> if __name__ == "__main__":
>>> import matplotlib.pyplot as plt
>>> import pyaerocom as pya
>>> from pyaerocom import const
>>> from pyaerocom.aeroval import EvalSetup, ExperimentProcessor
>>>
>>> # Customize cache dir to avoid disk quota issues.
>>> # cdir = pathlib.Path("./cache")
>>> # cdir.mkdir(exist_ok=True)
>>> # const.CACHEDIR = str(cdir)
>>>
>>> cfg = get_CFG(2022, "/lustre/storeB/project/fou/kl/emep/ModelRuns/EMEP4NO/EMEP4NO_rerun_2022/")
>>>
>>> # Change any experiment details.
>>> cfg.update(
>>> {
>>> #"proj_id": "<project name>",
>>> #"exp_id": "<experiment name>",
>>> #"json_basedir": "/lustre/storeB/users/thlun8736/python/aeroval/data",
>>> #"coldata_basedir": "/lustre/storeB/users/thlun8736/python/aeroval/coldata",
>>> }
>>> )
>>>
>>> # Run the experiment.
>>> stp = EvalSetup(**cfg)
>>> ana = ExperimentProcessor(stp)
>>> res = ana.run()
... import matplotlib.pyplot as plt
... import pyaerocom as pya
... from pyaerocom import const
... from pyaerocom.aeroval import EvalSetup, ExperimentProcessor
...
... # Customize cache dir to avoid disk quota issues.
... # cdir = pathlib.Path("./cache")
... # cdir.mkdir(exist_ok=True)
... # const.CACHEDIR = str(cdir)
...
... cfg = get_CFG(2022, "/lustre/storeB/project/fou/kl/emep/ModelRuns/EMEP4NO/EMEP4NO_rerun_2022/")
...
... # Change any experiment details.
... cfg.update(
... {
... #"proj_id": "<project name>",
... #"exp_id": "<experiment name>",
... #"json_basedir": "/lustre/storeB/users/thlun8736/python/aeroval/data",
... #"coldata_basedir": "/lustre/storeB/users/thlun8736/python/aeroval/coldata",
... }
... )
...
... # Run the experiment.
... stp = EvalSetup(**cfg)
... ana = ExperimentProcessor(stp)
... res = ana.run()
"""
cfg = get_EMEP_CFG(None, year, model_dir)

Expand Down
11 changes: 0 additions & 11 deletions pyaerocom/griddeddata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2171,17 +2171,6 @@ def interpolate(self, sample_points=None, scheme="nearest", collapse_scalar=True
-------
GriddedData
new data object containing interpolated data

Examples
--------

>>> from pyaerocom import GriddedData
>>> data = GriddedData()
>>> data._init_testdata_default()
>>> itp = data.interpolate([("longitude", (10)),
... ("latitude" , (35))])
>>> print(itp.shape)
(365, 1, 1)
"""
if isinstance(scheme, str):
scheme = str_to_iris(scheme)
Expand Down
6 changes: 6 additions & 0 deletions pyaerocom/io/file_conventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,15 @@ def from_file(self, file):
>>> from pyaerocom.io import FileConventionRead
>>> filename = 'aerocom3_CAM5.3-Oslo_AP3-CTRL2016-PD_od550aer_Column_2010_monthly.nc'
>>> print(FileConventionRead().from_file(filename))
<BLANKLINE>
pyaeorocom FileConventionRead
name: aerocom3
file_sep: _
year_pos: -2
var_pos: -4
ts_pos: -1
vert_pos: -3
data_id_pos: 1
"""

if basename(file).count("_") >= 4:
Expand Down Expand Up @@ -295,6 +298,9 @@ def get_info_from_file(self, file: str) -> dict:
('year', 2010)
('var_name', 'od550aer')
('ts_type', 'monthly')
('vert_code', 'Column')
('is_at_stations', False)
('data_id', 'CAM5.3-Oslo_AP3-CTRL2016-PD')
"""
if self.name == "aerocom3":
return self._info_from_aerocom3(file)
Expand Down
2 changes: 1 addition & 1 deletion pyaerocom/io/read_aeronet_invv3.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def read_file(self, filename, vars_to_retrieve=None, vars_as_series=False):
Example
-------
>>> import pyaerocom.io as pio
>>> obj = pio.read_aeronet_invv2.ReadAeronetInvV2()
>>> obj = pio.read_aeronet_invv3.ReadAeronetInvV3()
>>> files = obj.get_file_list()
>>> filedata = obj.read_file(files[0])
"""
Expand Down
12 changes: 6 additions & 6 deletions pyaerocom/io/readungridded.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,12 +773,12 @@ def read(

Example
-------
>>> import pyaerocom.io.readungridded as pio
>>> from pyaerocom import const
>>> obj = pio.ReadUngridded(data_id=const.AERONET_SUN_V3L15_AOD_ALL_POINTS_NAME)
>>> obj.read()
>>> print(obj)
>>> print(obj.metadata[0.]['latitude'])
>>> import pyaerocom.io.readungridded as pio # doctest: +SKIP
>>> from pyaerocom import const # doctest: +SKIP
>>> obj = pio.ReadUngridded(data_ids=const.AERONET_SUN_V3L15_AOD_ALL_POINTS_NAME) # doctest: +SKIP
>>> obj.read() # doctest: +SKIP
>>> print(obj) # doctest: +SKIP
>>> print(obj.metadata[0.]['latitude']) # doctest: +SKIP

"""
if data_ids is None:
Expand Down
10 changes: 4 additions & 6 deletions pyaerocom/io/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,12 @@ def browse_database(model_or_obs, verbose=False):
Example
-------
>>> import pyaerocom as pya
>>> pya.io.browse_database('AATSR*ORAC*v4*')
>>> pya.io.utils.browse_database('AATSR*ORAC*v4*')
<BLANKLINE>
Pyaerocom ReadGridded
---------------------
Model ID: AATSR_ORAC_v4.01
Data directory: /lustre/storeA/project/aerocom/aerocom-users-database/CCI-Aerosol/CCI_AEROSOL_Phase2/AATSR_ORAC_v4.01/renamed
Available variables: ['abs550aer', 'ang4487aer', 'clt', 'landseamask', 'od550aer', 'od550dust', 'od550gt1aer', 'od550lt1aer', 'pixelcount']
Available years: [2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012]
Available time resolutions ['daily']
Data ID: AATSR_ORAC_v4.02
...

"""
if not verbose:
Expand Down
10 changes: 5 additions & 5 deletions pyaerocom/mathutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def numbers_in_str(input_string):
Example
-------
>>> numbers_in_str('Bla42Blub100')
[42, 100]
['42', '100']
"""
numbers = []
IN_NUM = False
Expand Down Expand Up @@ -227,7 +227,7 @@ def exponent(num):
-------
>>> from pyaerocom.mathutils import exponent
>>> exponent(2340)
3
np.int64(3)
"""
return np.floor(np.log10(abs(np.asarray(num)))).astype(int)

Expand All @@ -251,11 +251,11 @@ def range_magnitude(low, high):
-------

>>> range_magnitude(0.1, 100)
3
np.int64(3)
>>> range_magnitude(100, 0.1)
-3
np.int64(-3)
>>> range_magnitude(1e-3, 1e6)
9
np.int64(9)

"""
return exponent(high) - exponent(low)
Expand Down
3 changes: 3 additions & 0 deletions pyaerocom/plot/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ class ColorTheme:
pyaerocom ColorTheme
name : dark
cmap_map : viridis
cmap_map_div : PuOr_r
cmap_map_div_shifted : True
color_coastline : #e6e6e6
color_map_text : r
"""

def __init__(
Expand Down
15 changes: 4 additions & 11 deletions pyaerocom/plot/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,13 @@ def calc_pseudolog_cmaplevels(vmin, vmax, add_zero=False):
Example
-------
>>> vmin, vmax = 0.02, 0.75
>>> vals = calc_pseudolog_cmaplevels(vmin, vmax, num_per_mag=10, add_zero=True)
>>> vals = calc_pseudolog_cmaplevels(vmin, vmax, add_zero=True)
>>> for val in vals: print("%.4f" %val)
0.0000
0.0100
0.0126
0.0158
0.0200
0.0251
0.0316
0.0398
0.0501
0.0631
0.0794
0.1000
...
0.7000
0.7500

"""
warnings.warn(
Expand Down
11 changes: 5 additions & 6 deletions pyaerocom/ungriddeddata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2034,10 +2034,9 @@ def filter_by_meta(self, negate=None, **filter_attributes):
Example
-------
>>> import pyaerocom as pya
>>> r = pya.io.ReadUngridded(['AeronetSunV2Lev2.daily',
'AeronetSunV3Lev2.daily'], 'od550aer')
>>> r = pya.io.ReadUngridded(['AeronetSunV3Lev2.daily'], 'od550aer')
>>> data = r.read()
>>> data_filtered = data.filter_by_meta(data_id='AeronetSunV2Lev2.daily',
>>> data_filtered = data.filter_by_meta(data_id='AeronetSunV3Lev2.daily',
... longitude=[-30, 30],
... latitude=[20, 70],
... altitude=[0, 1000])
Expand Down Expand Up @@ -3083,16 +3082,16 @@ def __and__(self, other):

Example
-------
>>> from pyaerocom.io import ReadAeronetSdaV2
>>> read = ReadAeronetSdaV2()
>>> from pyaerocom.io import ReadAeronetSdaV3
>>> read = ReadAeronetSdaV3()

>>> d0 = read.read(last_file=10)
>>> d1 = read.read(first_file=10, last_file=20)

>>> merged = d0 & d1

>>> print(d0.shape, d1.shape, merged.shape)
(7326, 11) (9894, 11) (17220, 11)
(9868, 12) (12336, 12) (22204, 12)
"""
return self.merge(other, new_obj=True)

Expand Down
10 changes: 6 additions & 4 deletions pyaerocom/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ def create_varinfo_table(

Example
-------
>>> from pyaerocom import create_varinfo_table
>>> from pyaerocom import create_varinfo_table # doctest: +ELLIPSIS
>>> models = ['INCA-BCext_CTRL2016-PD',
'GEOS5-freegcm_CTRL2016-PD']
... 'GEOS5-freegcm_CTRL2016-PD']
>>> vars = ['ang4487aer', 'od550aer', 'ec*']
>>> df = create_varinfo_table(models, vars)
>>> print(df)
>>> create_varinfo_table(models, vars)
At model: INCA-BCext_CTRL2016-PD (0 of 2)
At model: GEOS5-freegcm_CTRL2016-PD (1 of 2)
...
"""
if isinstance(model_ids, str):
model_ids = [model_ids]
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ commands_pre =
python --version
python -m pip freeze --all
commands =
python -m pytest -ra -q {posargs:--cov --no-cov-on-fail}
python -m pytest . -ra -q {posargs:--cov --no-cov-on-fail}
extras =
test
setenv =
Expand Down
Empty file.
Loading