| 1 |
|
| 2 |
) |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
from .util import units |
| 10 |
from .dimension_type import DimensionType |
| 11 |
|
| 12 |
|
| 13 |
class ValidationError: |
| 14 |
""" |
| 15 |
Static class for defining validation error messages |
| 16 |
""" |
| 17 |
NoName = "no name set" |
| 18 |
NoType = "no type set" |
| 19 |
NoDate = "date is not set" |
| 20 |
NoDataType = "data type is not set" |
| 21 |
NoID = "no ID set" |
| 22 |
DimensionMismatch = ("data dimensionality does not match number of " |
| 23 |
"defined dimensions") |
| 24 |
InvalidDimensionIndex = ("index for dimension {} is not set to a valid " |
| 25 |
"value (index > 0)") |
| 26 |
IncorrectDimensionIndex = ("index for dimension {} is set to incorrect " |
| 27 |
"value {}") |
| 28 |
DimensionTypeMismatch = ("dimension_type attribute for dimension " |
| 29 |
"{} does not match Dimension object type") |
| 30 |
RangeDimTicksMismatch = ("number of ticks in RangeDimension ({}) " |
| 31 |
"differs from the number of data entries " |
| 32 |
"along the corresponding data dimension") |
| 33 |
SetDimLabelsMismatch = ("number of labels in SetDimension ({}) " |
| 34 |
"differs from the number of data entries " |
| 35 |
"along the corresponding data dimension") |
| 36 |
NoPosition = "position is not set" |
| 37 |
PositionDimensionMismatch = ("number of entries in position does not " |
| 38 |
"match number of dimensions in all " |
| 39 |
"referenced DataArrays") |
| 40 |
ExtentDimensionMismatch = ("number of entries in extent does not match " |
| 41 |
"number of dimensions in all referenced " |
| 42 |
"DataArrays") |
| 43 |
PositionExtentMismatch = ("number of entries in position and extent " |
| 44 |
"do not match") |
| 45 |
ReferenceUnitsMismatch = ("some of the referenced DataArrays' dimensions " |
| 46 |
"don't have units where the Tag has; " |
| 47 |
"make sure that all references have the same " |
| 48 |
"number of dimensions as the Tag has units " |
| 49 |
"and that each dimension has a unit set") |
| 50 |
ReferenceUnitsIncompatible = ("some of the referenced DataArrays' " |
| 51 |
"dimensions have units that are not " |
| 52 |
"convertible to the units set in the Tag " |
| 53 |
"(Note: composite units are not supported)") |
| 54 |
InvalidUnit = ("unit is invalid: not an atomic SI " |
| 55 |
"(Note: composite units are not supported)") |
| 56 |
NoPositions = "positions are not set" |
| 57 |
PositionsDimensionMismatch = ("number of entries (in 2nd dim) in " |
| 58 |
"positions does not match number of " |
| 59 |
"dimensions in all referenced DataArrays") |
| 60 |
ExtentsDimensionMismatch = ("number of entries (in 2nd dim) in extents " |
| 61 |
"does not match number of dimensions in all " |
| 62 |
"referenced DataArrays") |
| 63 |
PositionsExtentsMismatch = ("number of entries in positions and extents " |
| 64 |
"do not match") |
| 65 |
NoTicks = "ticks for dimension {} are not set" |
| 66 |
UnsortedTicks = "ticks for dimension {} are not sorted" |
| 67 |
InvalidDimensionUnit = ("unit for dimension {} is set but it is not an " |
| 68 |
"atomic SI unit " |
| 69 |
"(Note: composite units are not supported)") |
| 70 |
NoSamplingInterval = ("sampling interval for dimension {} " |
| 71 |
"is not set") |
| 72 |
InvalidSamplingInterval = ("sampling interval for dimension {} " |
| 73 |
"is not valid (interval > 0)") |
| 74 |
DataFrameMismatch = ("referenced data frame for dimension {} does " |
| 75 |
"not have the same row count as the data array") |
| 76 |
NoData = "data is not set" |
| 77 |
NoLinkType = "link_type is not set" |
| 78 |
|
| 79 |
|
| 80 |
class ValidationWarning: |
| 81 |
NoVersion = "version is not set" |
| 82 |
NoFormat = "format is not set" |
| 83 |
NoFileID = "file ID is not set" |
| 84 |
InvalidUnit = "unit is not SI or composite of SI units" |
| 85 |
NoExpansionOrigin = ("polynomial coefficients for calibration are set, " |
| 86 |
"but expansion origin is missing") |
| 87 |
NoPolynomialCoefficients = ("expansion origin for calibration is set, " |
| 88 |
"but polynomial coefficients are missing") |
| 89 |
OffsetNoUnit = ("offset for dimension {} is set, " |
| 90 |
"but no valid unit is set") |
| 91 |
NoUnit = "unit is not set" |
| 92 |
|
| 93 |
|
| 94 |
def check_file(nixfile): |
| 95 |
""" |
| 96 |
Validate a NIX file and all contained objects and return all errors and |
| 97 |
warnings for each individual object. |
| 98 |
|
| 99 |
:returns: A nested dictionary of errors and warnings. Each subdictionary |
| 100 |
is indexed by the object with values describing the error or warning. |
| 101 |
:rtype: Dictionary |
| 102 |
""" |
| 103 |
results = {"errors": dict(), "warnings": dict()} |
| 104 |
if not nixfile.created_at: |
| 105 |
results["errors"][nixfile] = [ValidationError.NoDate] |
| 106 |
|
| 107 |
file_warnings = list() |
| 108 |
if not nixfile.version: |
| 109 |
file_warnings.append(ValidationWarning.NoVersion) |
| 110 |
if not nixfile.format: |
| 111 |
file_warnings.append(ValidationWarning.NoFormat) |
| 112 |
if not nixfile.id and nixfile.version and nixfile.version >= (1, 2, 0): |
| 113 |
file_warnings.append(ValidationWarning.NoFileID) |
| 114 |
if file_warnings: |
| 115 |
results["warnings"][nixfile] = file_warnings |
| 116 |
|
| 117 |
def update_results(obj, errors, warnings): |
| 118 |
if errors: |
| 119 |
results["errors"][obj] = errors |
| 120 |
if warnings: |
| 121 |
results["warnings"][obj] = warnings |
| 122 |
|
| 123 |
|
| 124 |
def traverse_sources(sources): |
| 125 |
|
| 126 |
for source in sources: |
| 127 |
src_errors, src_warnings = check_source(source) |
| 128 |
update_results(source, src_errors, src_warnings) |
| 129 |
traverse_sources(source.sources) |
| 130 |
|
| 131 |
|
| 132 |
def traverse_sections(sections): |
| 133 |
|
| 134 |
for section in sections: |
| 135 |
sec_errors, sec_warnings = check_section(section) |
| 136 |
update_results(section, sec_errors, sec_warnings) |
| 137 |
traverse_sections(section.sections) |
| 138 |
|
| 139 |
|
| 140 |
for block in nixfile.blocks: |
| 141 |
blk_errors, blk_warnings = check_block(block) |
| 142 |
update_results(block, blk_errors, blk_warnings) |
| 143 |
|
| 144 |
|
| 145 |
for group in block.groups: |
| 146 |
grp_errors, grp_warnings = check_group(group) |
| 147 |
update_results(group, grp_errors, grp_warnings) |
| 148 |
|
| 149 |
|
| 150 |
for da in block.data_arrays: |
| 151 |
da_errors, da_warnings = check_data_array(da) |
| 152 |
update_results(da, da_errors, da_warnings) |
| 153 |
|
| 154 |
|
| 155 |
for tag in block.tags: |
| 156 |
tag_errors, tag_warnings = check_tag(tag) |
| 157 |
update_results(tag, tag_errors, tag_warnings) |
| 158 |
|
| 159 |
|
| 160 |
for mtag in block.multi_tags: |
| 161 |
mtag_errors, mtag_warnings = check_multi_tag(mtag) |
| 162 |
update_results(mtag, mtag_errors, mtag_warnings) |
| 163 |
|
| 164 |
traverse_sources(block.sources) |
| 165 |
|
| 166 |
traverse_sections(nixfile.sections) |
| 167 |
|
| 168 |
return results |
| 169 |
|
| 170 |
|
| 171 |
def check_block(block): |
| 172 |
""" |
| 173 |
Validate a Block and return all errors and warnings for the block and each |
| 174 |
individual contained object. |
| 175 |
|
| 176 |
:returns: A list of 'errors' and a list of 'warnings' |
| 177 |
""" |
| 178 |
errors = check_entity(block) |
| 179 |
return errors, list() |
| 180 |
|
| 181 |
|
| 182 |
def check_group(group): |
| 183 |
""" |
| 184 |
Validate a Group and return all errors and warnings. |
| 185 |
Does not check contained objects. |
| 186 |
|
| 187 |
:returns: A list of 'errors' and a list of 'warnings' |
| 188 |
""" |
| 189 |
errors = check_entity(group) |
| 190 |
return errors, list() |
| 191 |
|
| 192 |
|
| 193 |
def check_data_array(da): |
| 194 |
""" |
| 195 |
Validate a DataArray and its Dimensions and return all errors and warnings. |
| 196 |
Errors and warnings about Dimension objects are included in the DataArray |
| 197 |
errors and warnings lists. |
| 198 |
|
| 199 |
:returns: A list of 'errors' and a list of 'warnings' |
| 200 |
""" |
| 201 |
errors = check_entity(da) |
| 202 |
warnings = list() |
| 203 |
|
| 204 |
if not da.data_type: |
| 205 |
errors.append(ValidationError.NoDataType) |
| 206 |
|
| 207 |
if len(da.dimensions) != len(da.shape): |
| 208 |
errors.append(ValidationError.DimensionMismatch) |
| 209 |
|
| 210 |
if da.unit and not units.is_si(da.unit): |
| 211 |
warnings.append(ValidationWarning.InvalidUnit) |
| 212 |
|
| 213 |
if da.polynom_coefficients and not da.expansion_origin: |
| 214 |
warnings.append(ValidationWarning.NoExpansionOrigin) |
| 215 |
elif da.expansion_origin and not da.polynom_coefficients: |
| 216 |
warnings.append(ValidationWarning.NoPolynomialCoefficients) |
| 217 |
|
| 218 |
for idx, (dim, datalen) in enumerate(zip(da.dimensions, da.shape), 1): |
| 219 |
if not dim.index or dim.index <= 0: |
| 220 |
errors.append(ValidationError.InvalidDimensionIndex.format(idx)) |
| 221 |
elif dim.index != idx: |
| 222 |
errors.append( |
| 223 |
ValidationError.IncorrectDimensionIndex.format(idx, dim.index) |
| 224 |
) |
| 225 |
if dim.dimension_type == DimensionType.Range: |
| 226 |
if dim.ticks is not None and len(dim.ticks) != datalen: |
| 227 |
|
| 228 |
|
| 229 |
errors.append( |
| 230 |
ValidationError.RangeDimTicksMismatch.format(idx) |
| 231 |
) |
| 232 |
dim_errors, dim_warnings = check_range_dimension(dim, idx) |
| 233 |
elif dim.dimension_type == DimensionType.Sample: |
| 234 |
dim_errors, dim_warnings = check_sampled_dimension(dim, idx) |
| 235 |
elif dim.dimension_type == DimensionType.Set: |
| 236 |
if dim.labels and len(dim.labels) != datalen: |
| 237 |
|
| 238 |
errors.append(ValidationError.SetDimLabelsMismatch.format(idx)) |
| 239 |
dim_errors, dim_warnings = check_set_dimension() |
| 240 |
errors.extend(dim_errors) |
| 241 |
warnings.extend(dim_warnings) |
| 242 |
return errors, warnings |
| 243 |
|
| 244 |
|
| 245 |
def check_tag(tag): |
| 246 |
""" |
| 247 |
Validate a Tag and its Features and return all errors and warnings. |
| 248 |
Errors and warnings about Features are included in the Tag errors and |
| 249 |
warnings lists. |
| 250 |
|
| 251 |
For Features, only the basic Entity requirements, LinkType, and the |
| 252 |
existence of a referenced DataArray are checked. The linked DataArray is |
| 253 |
not checked for validity (see check_data_array() for those validation |
| 254 |
checks). |
| 255 |
|
| 256 |
:returns: A list of 'errors' and a list of 'warnings' |
| 257 |
""" |
| 258 |
errors = check_entity(tag) |
| 259 |
warnings = list() |
| 260 |
|
| 261 |
if not tag.position: |
| 262 |
errors.append(ValidationError.NoPosition) |
| 263 |
if tag.references: |
| 264 |
posdim = len(tag.position) |
| 265 |
if any(posdim != len(da.shape) for da in tag.references): |
| 266 |
errors.append(ValidationError.PositionDimensionMismatch) |
| 267 |
if tag.extent: |
| 268 |
extlen = len(tag.extent) |
| 269 |
if extlen != posdim: |
| 270 |
errors.append(ValidationError.PositionExtentMismatch) |
| 271 |
if any(extlen != len(da.shape) for da in tag.references): |
| 272 |
errors.append(ValidationError.ExtentDimensionMismatch) |
| 273 |
|
| 274 |
refs_units = [get_dim_units(da) for da in tag.references] |
| 275 |
if any(len(ru) != len(tag.units) for ru in refs_units): |
| 276 |
errors.append(ValidationError.ReferenceUnitsMismatch) |
| 277 |
|
| 278 |
if not tag_units_match_refs_units(tag.units, refs_units): |
| 279 |
errors.append(ValidationError.ReferenceUnitsIncompatible) |
| 280 |
|
| 281 |
if any(not units.is_si(u) for u in tag.units if u): |
| 282 |
errors.append(ValidationError.InvalidUnit) |
| 283 |
|
| 284 |
for idx, feat in enumerate(tag.features): |
| 285 |
feat_errors = check_feature(feat, idx) |
| 286 |
errors.extend(feat_errors) |
| 287 |
|
| 288 |
return errors, warnings |
| 289 |
|
| 290 |
|
| 291 |
def check_multi_tag(mtag): |
| 292 |
""" |
| 293 |
Validate a MultiTag and its Features and return all errors and warnings. |
| 294 |
Errors and warnings about Features are included in the MultiTag errors and |
| 295 |
warnings lists. |
| 296 |
|
| 297 |
For Features, only the basic Entity requirements, LinkType, and the |
| 298 |
existence of a referenced DataArray are checked. The linked DataArray is |
| 299 |
not checked for validity (see check_data_array() for those validation |
| 300 |
checks). |
| 301 |
|
| 302 |
:returns: A list of 'errors' and a list of 'warnings' |
| 303 |
""" |
| 304 |
errors = check_entity(mtag) |
| 305 |
warnings = list() |
| 306 |
|
| 307 |
if not mtag.positions: |
| 308 |
errors.append(ValidationError.NoPositions) |
| 309 |
if mtag.references: |
| 310 |
if len(mtag.positions.shape) == 1: |
| 311 |
posdim = 1 |
| 312 |
else: |
| 313 |
posdim = mtag.positions.shape[1] |
| 314 |
|
| 315 |
if any(posdim != len(da.shape) for da in mtag.references): |
| 316 |
errors.append(ValidationError.PositionsDimensionMismatch) |
| 317 |
if mtag.extents: |
| 318 |
if mtag.positions.shape != mtag.extents.shape: |
| 319 |
errors.append(ValidationError.PositionsExtentsMismatch) |
| 320 |
if len(mtag.extents.shape) == 1: |
| 321 |
extdim = 1 |
| 322 |
else: |
| 323 |
extdim = mtag.extents.shape[1] |
| 324 |
if any(extdim != len(da.shape) for da in mtag.references): |
| 325 |
errors.append(ValidationError.ExtentsDimensionMismatch) |
| 326 |
|
| 327 |
refs_units = [get_dim_units(da) for da in mtag.references] |
| 328 |
if any(len(ru) != len(mtag.units) for ru in refs_units): |
| 329 |
errors.append(ValidationError.ReferenceUnitsMismatch) |
| 330 |
|
| 331 |
if not tag_units_match_refs_units(mtag.units, refs_units): |
| 332 |
errors.append(ValidationError.ReferenceUnitsIncompatible) |
| 333 |
|
| 334 |
if any(not units.is_si(u) for u in mtag.units if u): |
| 335 |
errors.append(ValidationError.InvalidUnit) |
| 336 |
|
| 337 |
for idx, feat in enumerate(mtag.features): |
| 338 |
feat_errors = check_feature(feat, idx) |
| 339 |
errors.extend(feat_errors) |
| 340 |
|
| 341 |
return errors, warnings |
| 342 |
|
| 343 |
|
| 344 |
def check_feature(feat, idx): |
| 345 |
""" |
| 346 |
Validate a Feature and return all errors. |
| 347 |
|
| 348 |
:returns: A list of 'errors' |
| 349 |
""" |
| 350 |
errors = list() |
| 351 |
if not feat.id: |
| 352 |
errors.append("feature {}: {}".format(idx, ValidationError.NoID)) |
| 353 |
if feat.created_at is None: |
| 354 |
errors.append("feature {}: {}".format(idx, ValidationError.NoDate)) |
| 355 |
if not feat.data: |
| 356 |
errors.append("feature {}: {}".format(idx, ValidationError.NoData)) |
| 357 |
if not feat.link_type: |
| 358 |
errors.append("feature {}: {}".format(idx, ValidationError.NoLinkType)) |
| 359 |
return errors |
| 360 |
|
| 361 |
|
| 362 |
def check_section(section): |
| 363 |
""" |
| 364 |
Validate a Section and its Properties return all errors and warnings. |
| 365 |
|
| 366 |
Errors and warnings about Properties are included in the Section errors and |
| 367 |
warnings lists. |
| 368 |
|
| 369 |
:returns: A list of 'errors' and a list of 'warnings' |
| 370 |
""" |
| 371 |
errors = check_entity(section) |
| 372 |
warnings = list() |
| 373 |
for idx, prop in enumerate(section.props): |
| 374 |
prop_errors, prop_warnings = check_property(prop, idx) |
| 375 |
errors.extend(prop_errors) |
| 376 |
warnings.extend(prop_warnings) |
| 377 |
|
| 378 |
return errors, warnings |
| 379 |
|
| 380 |
|
| 381 |
def check_property(prop, idx): |
| 382 |
""" |
| 383 |
Validate a Property and return all errors. |
| 384 |
|
| 385 |
:returns: A list of 'errors' and 'warnings' |
| 386 |
""" |
| 387 |
errors = list() |
| 388 |
warnings = list() |
| 389 |
if not prop.id: |
| 390 |
errors.append("property {}: {}".format(idx, ValidationError.NoID)) |
| 391 |
if not prop.name: |
| 392 |
errors.append("property {}: {}".format(idx, ValidationError.NoName)) |
| 393 |
if not prop.unit: |
| 394 |
warnings.append("property {}: {}".format(idx, |
| 395 |
ValidationWarning.NoUnit)) |
| 396 |
return errors, warnings |
| 397 |
|
| 398 |
|
| 399 |
def check_source(source): |
| 400 |
""" |
| 401 |
Validate a Source and return all errors and warnings. |
| 402 |
|
| 403 |
:returns: A list of 'errors' and a list of 'warnings' |
| 404 |
""" |
| 405 |
errors = check_entity(source) |
| 406 |
return errors, list() |
| 407 |
|
| 408 |
|
| 409 |
def check_range_dimension(dim, idx): |
| 410 |
""" |
| 411 |
Validate a RangeDimension and return all errors and warnings. |
| 412 |
|
| 413 |
:returns: A list of 'errors' and a list of 'warnings' |
| 414 |
""" |
| 415 |
errors = list() |
| 416 |
warnings = list() |
| 417 |
|
| 418 |
if not dim.ticks: |
| 419 |
errors.append(ValidationError.NoTicks.format(idx)) |
| 420 |
elif not all(ti < tj for ti, tj in zip(dim.ticks[:-1], dim.ticks[1:])): |
| 421 |
errors.append(ValidationError.UnsortedTicks.format(idx)) |
| 422 |
|
| 423 |
if dim.unit and not units.is_atomic(dim.unit): |
| 424 |
errors.append(ValidationError.InvalidDimensionUnit.format(idx)) |
| 425 |
return errors, warnings |
| 426 |
|
| 427 |
|
| 428 |
def check_set_dimension(): |
| 429 |
""" |
| 430 |
Validate a SetDimension and return all errors and warnings. |
| 431 |
|
| 432 |
:returns: A list of 'errors' and a list of 'warnings' |
| 433 |
""" |
| 434 |
return list(), list() |
| 435 |
|
| 436 |
|
| 437 |
def check_sampled_dimension(dim, idx): |
| 438 |
""" |
| 439 |
Validate a SampledDimension and return all errors and warnings. |
| 440 |
|
| 441 |
:returns: A list of 'errors' and a list of 'warnings' |
| 442 |
""" |
| 443 |
errors = list() |
| 444 |
warnings = list() |
| 445 |
|
| 446 |
if not dim.sampling_interval: |
| 447 |
errors.append(ValidationError.NoSamplingInterval.format(idx)) |
| 448 |
elif dim.sampling_interval < 0: |
| 449 |
errors.append(ValidationError.InvalidSamplingInterval.format(idx)) |
| 450 |
|
| 451 |
if dim.unit: |
| 452 |
if not units.is_atomic(dim.unit): |
| 453 |
errors.append(ValidationError.InvalidDimensionUnit.format(idx)) |
| 454 |
else: |
| 455 |
if dim.offset: |
| 456 |
warnings.append(ValidationWarning.OffsetNoUnit.format(idx)) |
| 457 |
return errors, warnings |
| 458 |
|
| 459 |
|
| 460 |
def check_entity(entity): |
| 461 |
""" |
| 462 |
General NIX entity validator |
| 463 |
""" |
| 464 |
errors = [] |
| 465 |
if not entity.type: |
| 466 |
errors.append(ValidationError.NoType) |
| 467 |
if not entity.id: |
| 468 |
errors.append(ValidationError.NoID) |
| 469 |
if not entity.name: |
| 470 |
errors.append(ValidationError.NoName) |
| 471 |
if entity.created_at is None: |
| 472 |
errors.append(ValidationError.NoDate) |
| 473 |
return errors |
| 474 |
|
| 475 |
|
| 476 |
def get_dim_units(data_array): |
| 477 |
""" |
| 478 |
Helper function to collect the units of the dimensions of a data array |
| 479 |
""" |
| 480 |
unit_list = [] |
| 481 |
for dim in data_array.dimensions: |
| 482 |
if (dim.dimension_type == DimensionType.Range or |
| 483 |
dim.dimension_type == DimensionType.Sample): |
| 484 |
unit_list.append(dim.unit if dim.unit else "") |
| 485 |
elif dim.dimension_type == DimensionType.Set: |
| 486 |
unit_list.append("") |
| 487 |
return unit_list |
| 488 |
|
| 489 |
|
| 490 |
def tag_units_match_refs_units(tag_units, refs_units): |
| 491 |
for ref_units in refs_units: |
| 492 |
for tag_unit, ref_unit in zip(tag_units, ref_units): |
| 493 |
if tag_unit == "" and ref_unit == "": |
| 494 |
continue |
| 495 |
if not units.scalable(tag_unit, ref_unit): |
| 496 |
return False |
| 497 |
return True |