Skip to content

Commit 2a2d15e

Browse files
committed
Merge branch 'main' of github.com:comet-toolkit/obsarray into small_fixes
2 parents a583091 + 195a69c commit 2a2d15e

File tree

8 files changed

+285
-23
lines changed

8 files changed

+285
-23
lines changed

docs/conf.py

Lines changed: 155 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,166 @@
1212
# All configuration values have a default; values that are commented out
1313
# serve to show the default.
1414

15+
import os
16+
import sys
17+
1518
# If extensions (or modules to document with autodoc) are in another
1619
# directory, add these directories to sys.path here. If the directory is
1720
# relative to the documentation root, use os.path.abspath to make it
1821
# absolute, like shown here.
1922
#
2023
import obsarray
21-
import os
22-
import sys
24+
25+
sys.path.insert(0, os.path.abspath('..'))
26+
27+
project_title = "obsarray".replace("_", " ").title()
28+
29+
# -- General configuration ---------------------------------------------
30+
31+
latex_toplevel_sectioning = 'section'
32+
33+
# If your documentation needs a minimal Sphinx version, state it here.
34+
#
35+
# needs_sphinx = '1.0'
36+
37+
# Attempt to make links automatially
38+
default_role = "code"
39+
40+
# Add any Sphinx extension module names here, as strings. They can be
41+
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
42+
# CFAB added napolean to support google-style docstrings
43+
extensions = [
44+
'sphinx.ext.autodoc',
45+
'sphinx.ext.viewcode',
46+
'sphinx.ext.napoleon',
47+
]
48+
49+
# Add any paths that contain templates here, relative to this directory.
50+
templates_path = ['_templates']
51+
52+
# The suffix(es) of source filenames.
53+
# You can specify multiple suffix as a list of string:
54+
#
55+
# source_suffix = ['.rst', '.md']
56+
source_suffix = '.rst'
57+
58+
# The master toctree document.
59+
master_doc = 'index'
60+
61+
# General information about the project.
62+
project = project_title
63+
copyright = "Sam Hunt"
64+
author = "Sam Hunt"
65+
66+
# The version info for the project you're documenting, acts as replacement
67+
# for |version| and |release|, also used in various other places throughout
68+
# the built documents.
69+
#
70+
# The short X.Y version.
71+
version = obsarray.__version__
72+
# The full version, including alpha/beta/rc tags.
73+
release = obsarray.__version__
74+
75+
# The language for content autogenerated by Sphinx. Refer to documentation
76+
# for a list of supported languages.
77+
#
78+
# This is also used if you do content translation via gettext catalogs.
79+
# Usually you set "language" from the command line for these cases.
80+
language = None
81+
82+
# List of patterns, relative to source directory, that match files and
83+
# directories to ignore when looking for source files.
84+
# This patterns also effect to html_static_path and html_extra_path
85+
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
86+
87+
# The name of the Pygments (syntax highlighting) style to use.
88+
pygments_style = 'sphinx'
89+
90+
# If true, `todo` and `todoList` produce output, else they produce nothing.
91+
todo_include_todos = False
92+
93+
94+
# -- Options for HTML output -------------------------------------------
95+
96+
# The theme to use for HTML and HTML Help pages. See the documentation for
97+
# a list of builtin themes.
98+
#
99+
html_theme = 'sphinx_rtd_theme'
100+
101+
# Theme options are theme-specific and customize the look and feel of a
102+
# theme further. For a list of options available for each theme, see the
103+
# documentation.
104+
#
105+
# html_theme_options = {}
106+
107+
# Add any paths that contain custom static files (such as style sheets) here,
108+
# relative to this directory. They are copied after the builtin static files,
109+
# so a file named "default.css" will overwrite the builtin "default.css".
110+
html_static_path = ['_static']
111+
112+
113+
# -- Options for HTMLHelp output ---------------------------------------
114+
115+
# Output file base name for HTML help builder.
116+
htmlhelp_basename = 'obsarraydoc'
117+
118+
119+
# -- Options for LaTeX output ------------------------------------------
120+
121+
latex_elements = {
122+
# The paper size ('letterpaper' or 'a4paper').
123+
#
124+
# 'papersize': 'letterpaper',
125+
126+
# The font size ('10pt', '11pt' or '12pt').
127+
#
128+
# 'pointsize': '10pt',
129+
130+
# Additional stuff for the LaTeX preamble.
131+
#
132+
# 'preamble': '',
133+
134+
# Latex figure (float) alignment
135+
#
136+
# 'figure_align': 'htbp',
137+
}
138+
139+
# Grouping the document tree into LaTeX files. List of tuples
140+
# (source start file, target name, title, author, documentclass
141+
# [howto, manual, or own class]).
142+
latex_documents = [
143+
(master_doc, 'user_manual.tex',
144+
'{} Documentation'.format(project_title),
145+
'Sam Hunt', 'manual'),
146+
]
147+
148+
149+
# -- Options for manual page output ------------------------------------
150+
151+
# One entry per manual page. List of tuples
152+
# (source start file, name, description, authors, manual section).
153+
man_pages = [
154+
(master_doc, 'obsarray',
155+
'obsarray Documentation',
156+
[author], 1)
157+
]
158+
159+
160+
# -- Options for Texinfo output ----------------------------------------
161+
162+
# Grouping the document tree into Texinfo files. List of tuples
163+
# (source start file, target name, title, author,
164+
# dir menu entry, description, category)
165+
texinfo_documents = [
166+
(master_doc, 'obsarray',
167+
'obsarray Documentation',
168+
author,
169+
'obsarray',
170+
'Propagating UNcertainties in PYthon',
171+
'Miscellaneous'),
172+
]
173+
174+
"""
23175
24176
sys.path.insert(0, os.path.abspath(".."))
25177
@@ -209,3 +361,4 @@ def setup(app):
209361
"Miscellaneous",
210362
),
211363
]
364+
"""

obsarray/templater/dataset_util.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def create_unc_variable(
153153
# set undefined dims as random
154154
defined_err_corr_dims = []
155155
for erd in err_corr:
156-
if isinstance(erd["dim"],str):
156+
if isinstance(erd["dim"], str):
157157
defined_err_corr_dims.append(erd["dim"])
158158
else:
159159
defined_err_corr_dims.extend(erd["dim"])
@@ -173,7 +173,10 @@ def create_unc_variable(
173173
units_str = DatasetUtil.return_err_corr_units_str(idx)
174174

175175
form = ecdef["form"]
176-
attributes[dim_str] = ecdef["dim"]
176+
if isinstance(ecdef["dim"], list) and len(ecdef["dim"]) == 1:
177+
attributes[dim_str] = ecdef["dim"]
178+
else:
179+
attributes[dim_str] = ecdef["dim"]
177180
attributes[form_str] = ecdef["form"]
178181
attributes[units_str] = ecdef["units"] if "units" in ecdef else []
179182

@@ -383,7 +386,9 @@ def unpack_flags(da):
383386

384387
ds = xarray.Dataset()
385388
for flag_meaning, flag_mask in zip(flag_meanings, flag_masks):
386-
ds[flag_meaning] = DatasetUtil.create_variable(list(da.shape), bool, dim_names=list(da.dims))
389+
ds[flag_meaning] = DatasetUtil.create_variable(
390+
list(da.shape), bool, dim_names=list(da.dims)
391+
)
387392
ds[flag_meaning] = (da & flag_mask).astype(bool)
388393

389394
return ds
@@ -447,7 +452,9 @@ def set_flag(da, flag_name, error_if_set=False):
447452
set_flags = DatasetUtil.unpack_flags(da)[flag_name]
448453

449454
if numpy.any(set_flags == True) and error_if_set:
450-
raise ValueError("Flag " + flag_name + " already set for variable " + da.name)
455+
raise ValueError(
456+
"Flag " + flag_name + " already set for variable " + da.name
457+
)
451458

452459
# Find flag mask
453460
flag_meanings, flag_masks = DatasetUtil._get_flag_encoding(da)
@@ -473,7 +480,9 @@ def unset_flag(da, flag_name, error_if_unset=False):
473480
set_flags = DatasetUtil.unpack_flags(da)[flag_name]
474481

475482
if numpy.any(set_flags == False) and error_if_unset:
476-
raise ValueError("Flag " + flag_name + " already set for variable " + da.name)
483+
raise ValueError(
484+
"Flag " + flag_name + " already set for variable " + da.name
485+
)
477486

478487
# Find flag mask
479488
flag_meanings, flag_masks = DatasetUtil._get_flag_encoding(da)
@@ -501,7 +510,7 @@ def get_set_flags(da):
501510

502511
set_flags = []
503512
for flag_meaning, flag_mask in zip(flag_meanings, flag_masks):
504-
if (da & flag_mask):
513+
if da & flag_mask:
505514
set_flags.append(flag_meaning)
506515

507516
return set_flags

obsarray/templater/template_util.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,19 @@
1111

1212

1313
def create_ds(
14-
template: Dict[str, Dict], size: Dict[str, int], metadata: Optional[Dict] = None,
15-
propagate_ds: Optional[xarray.Dataset] = None,
14+
template: Dict[str, Dict],
15+
size: Dict[str, int],
16+
metadata: Optional[Dict] = None,
17+
append_ds: Optional[xarray.Dataset] = None,
18+
propagate_ds: Optional[xarray.Dataset] = None,
1619
) -> xarray.Dataset:
1720
"""
1821
Returns template dataset
1922
2023
:param template: dictionary defining ds variable structure, as defined below.
2124
:param size: dictionary of dataset dimensions, entry per dataset dimension with value of size as int
2225
:param metadata: dictionary of dataset metadata
26+
:param append_ds: base dataset to append with template variables
2327
:param propagate_ds: template dataset is populated with data from propagate_ds for their variables with
2428
common names and dimensions. Useful for transferring common data between datasets at different processing levels
2529
(e.g. times, etc.).
@@ -38,7 +42,7 @@ def create_ds(
3842
"""
3943

4044
# Create dataset
41-
ds = xarray.Dataset()
45+
ds = append_ds if append_ds is not None else xarray.Dataset()
4246

4347
# Add variables
4448
ds = TemplateUtil.add_variables(ds, template, size)
@@ -85,7 +89,9 @@ def add_variables(
8589
8690
:returns: dataset with defined variables
8791
"""
92+
8893
for var_name in template.keys():
94+
8995
var = TemplateUtil._create_var(var_name, template[var_name], size)
9096

9197
ds[var_name] = var
@@ -123,7 +129,7 @@ def _create_var(
123129
)
124130

125131
# Create variable and add to dataset
126-
if isinstance(dtype,str):
132+
if dtype == str:
127133
if dtype == "flag":
128134
flag_meanings = attributes.pop("flag_meanings")
129135
variable = du.create_flags_variable(
@@ -218,21 +224,32 @@ def propagate_values(target_ds, source_ds, exclude=None):
218224

219225
# Find variable names common to target_ds and source_ds, excluding specified exclude variables
220226
common_variable_names = list(set(target_ds).intersection(source_ds))
221-
#common_variable_names = list(set(target_ds.variables).intersection(source_ds.variables))
222-
#print(common_variable_names)
227+
# common_variable_names = list(set(target_ds.variables).intersection(source_ds.variables))
228+
# print(common_variable_names)
223229

224230
if exclude is not None:
225-
common_variable_names = [name for name in common_variable_names if name not in exclude]
231+
common_variable_names = [
232+
name for name in common_variable_names if name not in exclude
233+
]
226234

227235
# Remove any common variables that have different dimensions in target_ds and source_ds
228-
common_variable_names = [name for name in common_variable_names if target_ds[name].dims == source_ds[name].dims]
236+
common_variable_names = [
237+
name
238+
for name in common_variable_names
239+
if target_ds[name].dims == source_ds[name].dims
240+
]
229241

230242
# Propagate data
231243
for common_variable_name in common_variable_names:
232-
if target_ds[common_variable_name].shape == source_ds[common_variable_name].shape:
233-
target_ds[common_variable_name].values = source_ds[common_variable_name].values
234-
235-
#to do - add method to propagate common unpopulated metadata
244+
if (
245+
target_ds[common_variable_name].shape
246+
== source_ds[common_variable_name].shape
247+
):
248+
target_ds[common_variable_name].values = source_ds[
249+
common_variable_name
250+
].values
251+
252+
# to do - add method to propagate common unpopulated metadata
236253

237254

238255
if __name__ == "__main__":

obsarray/templater/tests/test_dataset_util.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,36 @@ def test_create_variable_3D_int_attributes(self):
173173
self.assertEqual(-127, array_variable[2, 4, 2])
174174
self.assertEqual("std", array_variable.attrs["standard_name"])
175175

176-
def test_create_unc_variable(self):
176+
def test_create_unc_variable_1dimundef(self):
177+
err_corr = [
178+
{
179+
"dim": ["x"],
180+
"form": "rectangle_absolute",
181+
"params": [1, 2],
182+
"units": ["m", "m"],
183+
},
184+
]
185+
186+
unc_variable = DatasetUtil.create_unc_variable(
187+
[7],
188+
np.int8,
189+
["x"],
190+
pdf_shape="gaussian",
191+
err_corr=err_corr,
192+
)
193+
194+
expected_attrs = {
195+
"err_corr_1_dim": ["x"],
196+
"err_corr_1_form": "rectangle_absolute",
197+
"err_corr_1_units": ["m", "m"],
198+
"err_corr_1_params": [1, 2],
199+
}
200+
201+
actual_attrs = unc_variable.attrs
202+
203+
self.assertTrue(expected_attrs.items() <= actual_attrs.items())
204+
205+
def test_create_unc_variable_1undef(self):
177206
err_corr = [
178207
{
179208
"dim": "x",

0 commit comments

Comments
 (0)