Repository navigation
Use clear errors for invalid annotation writer inputs - #799
Conversation
A non-empty annotation without a label field now raises ValueError instead of a bare Exception, and type errors name the field and what it should be as a plain message, instead of a tuple of types. The check of numeric array fields never ran, because it compared the tuple of allowed types with np.ndarray. It now rejects arrays that are not one-dimensional or not numeric, which previously failed inside NumPy. Whole-number floats and object arrays of numbers are still accepted. Closes #797
|
I found one validation gap I think we should fix before merging. The new numeric-array check accepts any floating array, but the writer later casts values to bytes. I reproduced Could we check that values in The affected tests pass locally (89 passed, 3 network skips). Style and docs CI are green; the remaining jobs were still running when I checked. |
The whole-number check applied only to sample. Fractional, NaN or infinite values in label_store, subtype, chan and num were truncated when written (chan=1.5 was read back as 1, and NaN as 0), and an object array of text failed with a raw comparison error. The check now covers all five fields, still accepting whole-number floats and numeric object arrays.
|
Thanks, good catch. Fixed in cb40e46. The whole-number check that
New tests cover each field with fractional, NaN, infinite and non-numeric object values, through both Offline suite: 1,301 passed, 4 skipped. Ruff and the strict docs build pass. |
|
The validation gap I flagged is fixed in cb40e46. I reran the examples: fractional and NaN I also found a separate, preexisting sample-range bug while checking boundaries: a |
The writer converted samples to int64 without checking their range. An unsigned 2**63 wrapped around and was read back as 0, and values such as 1e20 overflowed and made the SKIP loop run for billions of iterations. Sample numbers must now be below 2**53, where float64 still represents every integer and the SKIP elements for a whole file stay under about 25 MB. Differences are computed on signed integers, so decreasing unsigned samples are rejected, and full-size SKIP elements are built by repetition instead of one at a time (the bytes are unchanged).
|
Thanks for checking the boundaries. I fixed the sample-range bug here as well, in d78b972:
New tests cover the four over-limit cases through both writers, decreasing unsigned samples, and a round trip of gaps on each side of the SKIP boundaries up to 2**40. Offline suite: 1,311 passed, 4 skipped. Ruff and the strict docs build pass. |
Closes #797.
Summary
write_annotationsandAnnotation.writepassed some validation failures through from the shared annotation writer as tuple-valued exceptions. They now report plain messages. Both functions use the same validation (Annotation.writecallswrite_annotations), and the legacywrannfunction and method get the same messages.ValueError: A non-empty annotation needs at least one label field: label_store, symbol, or description, instead of a bareExceptionwith a tuple message.ValueErrorsubclassesException, so existingexcept Exceptionhandlers still work.TypeErrornaming what it should be, such asThe sample field must be a one-dimensional NumPy array, instead of a tuple of type objects.record_nameorextensionraisesValueErrorinstead of a bareException.While checking this, I found that the shape and type check for the numeric array fields (
sample,label_store,subtype,chan,num) never ran: it compared the tuple of allowed types withnp.ndarray, which is always false. So a two-dimensionalsamplefailed insidenp.concatenate, and a string array failed with'<' not supported between instances of 'numpy.str_' and 'int'. The check now runs and rejects arrays that are not one-dimensional or not numeric. It still accepts whole-number floats and object arrays of numbers, which the writer converts (the existing tests for those pass unchanged).The whole-number check that
samplealready had now covers all five numeric fields. Before, fractional, NaN or infinite values inlabel_store,subtype,chanandnumwere silently truncated when written (for example,chan=1.5was read back as1, and NaN as0), and object arrays of text failed with a raw comparison error. These now raiseValueError: The '<field>' field must only contain whole numbers.Sample numbers must also be below 253. Larger values were converted to
int64unchecked: an unsigned2**63wrapped around and was read back as0, and values such as1e20made the SKIP loop run for billions of iterations. 253 is where float64 stops representing every integer, and it bounds a file's SKIP elements at about 25 MB. Sample differences are now computed on signed integers, so decreasing unsigned samples are rejected, and full-size SKIP elements are built by repetition (the bytes are unchanged).Tests
New tests run each case through both
write_annotationsandAnnotation.write, and check the exact message and that no file is written: a missing label field,sampleas a list, a 2D array and a string array, and wrong types forchan,symbolandfs. Further tests cover fractional, NaN, infinite and non-numeric object values in each numeric field, and check that whole-number float and object values are still written for each field. Others cover samples at or above 2**53, decreasing unsigned samples, and a round trip of gaps on each side of the SKIP boundaries.Checks
@tompollard 🟥 red 🦥 sloth · aspen