Skip to content

Commit fb2694c

Browse files
kdsudacgrybmadsci
authored andcommitted
Creating Dimension class that is required for multi-dim Measurements (#710)
* Creating Dimension class that is required for multi-dim Measurements * removing stray line * minor formatting/comment changes suggested during review
1 parent 35142b8 commit fb2694c

File tree

4 files changed

+69
-6
lines changed

4 files changed

+69
-6
lines changed

examples/all_the_things.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ def set_measurements(test):
8787
htf.Measurement('unset_dims').with_dimensions(units.HERTZ),
8888
htf.Measurement('dimensions').with_dimensions(units.HERTZ),
8989
htf.Measurement('lots_of_dims').with_dimensions(
90-
units.HERTZ, units.SECOND, units.RADIAN))
90+
units.HERTZ, units.SECOND,
91+
htf.Dimension(description='my_angle', unit=units.RADIAN)))
9192
def dimensions(test):
9293
for dim in range(5):
9394
test.measurements.dimensions[dim] = 1 << dim
@@ -112,7 +113,7 @@ def attachments(test):
112113
os.path.join(os.path.dirname(__file__), 'example_attachment.txt'))
113114

114115
test_attachment = test.get_attachment('test_attachment')
115-
assert test_attachment == 'This is test attachment data.'
116+
assert test_attachment.data == 'This is test attachment data.'
116117

117118

118119
@htf.TestPhase(run_if=lambda: False)
@@ -124,7 +125,7 @@ def analysis(test):
124125
level_all = test.get_measurement('level_all')
125126
assert level_all.value == 9
126127
test_attachment = test.get_attachment('test_attachment')
127-
assert test_attachment == 'This is test attachment data.'
128+
assert test_attachment.data == 'This is test attachment data.'
128129
lots_of_dims = test.get_measurement('lots_of_dims')
129130
assert lots_of_dims.value == [
130131
(1, 21, 101, 123),

openhtf/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
from openhtf import core
4040
from openhtf import plugs
4141
from openhtf import util
42-
from openhtf.core.measurements import Measurement, measures
42+
from openhtf.core.measurements import Dimension, Measurement, measures
4343
from openhtf.core.monitors import monitors
4444
from openhtf.core import phase_executor
4545
from openhtf.core import station_api

openhtf/core/measurements.py

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,19 @@ def _maybe_make_unit_desc(self, unit_desc):
185185
unit_desc))
186186
return unit_desc
187187

188+
def _maybe_make_dimension(self, dimension):
189+
"""Return a `measurements.Dimension` instance."""
190+
# For backwards compatibility the argument can be either a Dimension, a
191+
# string or a `units.UnitDescriptor`.
192+
if isinstance(dimension, Dimension):
193+
return dimension
194+
if isinstance(dimension, units.UnitDescriptor):
195+
return Dimension.from_unit_descriptor(dimension)
196+
if isinstance(dimension, str):
197+
return Dimension.from_string(string)
198+
199+
raise TypeError('Cannot convert %s to a dimension', dimension)
200+
188201
def with_units(self, unit_desc):
189202
"""Declare the units for this Measurement, returns self for chaining."""
190203
self.units = self._maybe_make_unit_desc(unit_desc)
@@ -193,7 +206,7 @@ def with_units(self, unit_desc):
193206
def with_dimensions(self, *dimensions):
194207
"""Declare dimensions for this Measurement, returns self for chaining."""
195208
self.dimensions = tuple(
196-
self._maybe_make_unit_desc(dim) for dim in dimensions)
209+
self._maybe_make_dimension(dim) for dim in dimensions)
197210
return self
198211

199212
def with_validator(self, validator):
@@ -304,6 +317,55 @@ def set(self, value):
304317
self.is_value_set = True
305318

306319

320+
class Dimension(object):
321+
"""Dimension for multi-dim Measurements.
322+
323+
Dimensions optionally include a unit and a description. This is intended
324+
as a drop-in replacement for UnitDescriptor for backwards compatibility.
325+
"""
326+
327+
def __init__(self, description='', unit=units.NO_DIMENSION):
328+
self.description = description
329+
self.unit = unit
330+
331+
@classmethod
332+
def from_unit_descriptor(cls, unit_desc):
333+
return cls(unit=unit_desc)
334+
335+
@classmethod
336+
def from_string(cls, string):
337+
"""Convert a string into a Dimension"""
338+
# Note: There is some ambiguity as to whether the string passed is intended
339+
# to become a unit looked up by name or suffix, or a Dimension descriptor.
340+
if string in units.UNITS_BY_ALL:
341+
return cls(description=string, unit=units.Unit(string))
342+
else:
343+
return cls(description=string)
344+
345+
@property
346+
def code(self):
347+
"""Provides backwards compatibility to `units.UnitDescriptor` api."""
348+
return self.unit.code
349+
350+
@property
351+
def suffix(self):
352+
"""Provides backwards compatibility to `units.UnitDescriptor` api."""
353+
return self.unit.suffix
354+
355+
@property
356+
def name(self):
357+
"""Provides backwards compatibility to `units.UnitDescriptor` api."""
358+
return self.description or self.unit.name
359+
360+
def _asdict(self):
361+
return {
362+
'code': self.code,
363+
'description': self.description,
364+
'name': self.name,
365+
'suffix': self.suffix,
366+
}
367+
368+
307369
class DimensionedMeasuredValue(mutablerecords.Record(
308370
'DimensionedMeasuredValue', ['name', 'num_dimensions'],
309371
{'notify_value_set': None, 'value_dict': collections.OrderedDict})):

test/core/measurements_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def test_chaining_in_measurement_declarations(self, user_mock):
4545
self.assertMeasurementPass(record, 'specified_as_args')
4646

4747
@htf_test.yields_phases
48-
def test_measurements_with_dimenstions(self):
48+
def test_measurements_with_dimensions(self):
4949
record = yield all_the_things.dimensions
5050
self.assertNotMeasured(record, 'unset_dims')
5151
self.assertMeasured(record, 'dimensions',

0 commit comments

Comments
 (0)