Skip to content

Commit ce43cd5

Browse files
lukasheinrichmatthewfeickert
authored andcommitted
docs: Apply pydocstyle to Workspace (#707)
* Add docstrings to workspace.py that pass pydocstyle * Add workspace.py to pydocstyle tests in CI
1 parent 97f5ea9 commit ce43cd5

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ jobs:
103103
- name: Check docstrings
104104
run: |
105105
# Group 1 is related to docstrings
106-
pydocstyle --select D1 src/pyhf/pdf.py src/pyhf/probability.py src/pyhf/interpolators src/pyhf/infer src/pyhf/optimize
106+
pydocstyle --select D1 src/pyhf/pdf.py src/pyhf/workspace.py src/pyhf/probability.py src/pyhf/interpolators src/pyhf/infer src/pyhf/optimize
107107
- name: Test and build docs
108108
run: |
109109
python -m doctest README.md

src/pyhf/workspace.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
"""
2+
pyhf workspaces hold the three data items:
3+
4+
* the statistical model p(data|parameters)
5+
* the observed data (optional)
6+
* fit configurations ("measurements")
7+
"""
18
import logging
29
import jsonpatch
310
from . import exceptions
@@ -11,10 +18,11 @@
1118

1219
class Workspace(_ChannelSummaryMixin, dict):
1320
"""
14-
A JSON-serializable object that is built from an object that follows the `workspace.json` schema.
21+
A JSON-serializable object that is built from an object that follows the :obj:`workspace.json` `schema <https://scikit-hep.org/pyhf/likelihood.html#workspace>`__.
1522
"""
1623

1724
def __init__(self, spec, **config_kwargs):
25+
"""Workspaces hold the model, data and measurements."""
1826
super(Workspace, self).__init__(spec, channels=spec['channels'])
1927
self.schema = config_kwargs.pop('schema', 'workspace.json')
2028
self.version = config_kwargs.pop('version', None)
@@ -31,20 +39,25 @@ def __init__(self, spec, **config_kwargs):
3139
self.observations[obs['name']] = obs['data']
3240

3341
def __eq__(self, other):
42+
"""Equality is defined as equal dict representations."""
3443
if not isinstance(other, Workspace):
3544
return False
3645
return dict(self) == dict(other)
3746

3847
def __ne__(self, other):
48+
"""Negation of equality."""
3949
return not self == other
4050

4151
def __repr__(self):
52+
"""Representation of the Workspace."""
4253
return object.__repr__(self)
4354

4455
# NB: this is a wrapper function to validate the returned measurement object against the spec
4556
def get_measurement(self, **config_kwargs):
4657
"""
47-
Get (or create) a measurement object using the following logic:
58+
Get (or create) a measurement object.
59+
60+
The following logic is used:
4861
4962
1. if the poi name is given, create a measurement object for that poi
5063
2. if the measurement name is given, find the measurement for the given name
@@ -61,15 +74,14 @@ def get_measurement(self, **config_kwargs):
6174
6275
Returns:
6376
:obj:`dict`: A measurement object adhering to the schema defs.json#/definitions/measurement
77+
6478
"""
6579
m = self._get_measurement(**config_kwargs)
6680
utils.validate(m, 'measurement.json', self.version)
6781
return m
6882

6983
def _get_measurement(self, **config_kwargs):
70-
"""
71-
See `Workspace::get_measurement`.
72-
"""
84+
"""See `Workspace::get_measurement`."""
7385
poi_name = config_kwargs.get('poi_name')
7486
if poi_name:
7587
return {
@@ -118,6 +130,7 @@ def model(self, **config_kwargs):
118130
119131
Returns:
120132
~pyhf.pdf.Model: A model object adhering to the schema model.json
133+
121134
"""
122135
measurement = self.get_measurement(**config_kwargs)
123136
log.debug(
@@ -150,8 +163,8 @@ def data(self, model, with_aux=True):
150163
151164
Returns:
152165
:obj:`list`: data
153-
"""
154166
167+
"""
155168
try:
156169
observed_data = sum(
157170
(self.observations[c] for c in model.config.channels), []
@@ -178,8 +191,7 @@ def _prune_and_rename(
178191
rename_measurements={},
179192
):
180193
"""
181-
Return a new, pruned, renamed workspace specification. This will not
182-
modify the original workspace.
194+
Return a new, pruned, renamed workspace specification. This will not modify the original workspace.
183195
184196
Pruning removes pieces of the workspace whose name or type matches the
185197
user-provided lists. The pruned, renamed workspace must also be a valid
@@ -208,6 +220,7 @@ def _prune_and_rename(
208220
209221
Returns:
210222
~pyhf.workspace.Workspace: A new workspace object with the specified components removed or renamed
223+
211224
"""
212225
newspec = {
213226
'channels': [
@@ -276,8 +289,7 @@ def prune(
276289
self, modifiers=[], modifier_types=[], samples=[], channels=[], measurements=[]
277290
):
278291
"""
279-
Return a new, pruned workspace specification. This will not modify the
280-
original workspace.
292+
Return a new, pruned workspace specification. This will not modify the original workspace.
281293
282294
The pruned workspace must also be a valid workspace.
283295
@@ -290,6 +302,7 @@ def prune(
290302
291303
Returns:
292304
~pyhf.workspace.Workspace: A new workspace object with the specified components removed
305+
293306
"""
294307
return self._prune_and_rename(
295308
prune_modifiers=modifiers,
@@ -302,8 +315,8 @@ def prune(
302315
def rename(self, modifiers={}, samples={}, channels={}, measurements={}):
303316
"""
304317
Return a new workspace specification with certain elements renamed.
305-
This will not modify the original workspace.
306318
319+
This will not modify the original workspace.
307320
The renamed workspace must also be a valid workspace.
308321
309322
Args:
@@ -314,6 +327,7 @@ def rename(self, modifiers={}, samples={}, channels={}, measurements={}):
314327
315328
Returns:
316329
~pyhf.workspace.Workspace: A new workspace object with the specified components renamed
330+
317331
"""
318332
return self._prune_and_rename(
319333
rename_modifiers=modifiers,
@@ -325,8 +339,7 @@ def rename(self, modifiers={}, samples={}, channels={}, measurements={}):
325339
@classmethod
326340
def combine(cls, left, right):
327341
"""
328-
Return a new workspace specification that is the combination of the two
329-
workspaces.
342+
Return a new workspace specification that is the combination of the two workspaces.
330343
331344
The new workspace must also be a valid workspace. A combination of
332345
workspaces is done by combining the set of:
@@ -352,6 +365,7 @@ def combine(cls, left, right):
352365
353366
Returns:
354367
~pyhf.workspace.Workspace: A new combined workspace object
368+
355369
"""
356370
common_channels = set(left.channels).intersection(right.channels)
357371
if common_channels:

0 commit comments

Comments
 (0)