Skip to content

Allow None in descriptor shape - #352

Merged
coretl merged 3 commits into
mainfrom
allow_none_in_shape
May 12, 2025
Merged

coretl merged 3 commits into
mainfrom
allow_none_in_shape

Conversation

@Tom-Willemsen

@Tom-Willemsen Tom-Willemsen commented Apr 14, 2025 •

Copy link
Copy Markdown
Member

Description

Closes #351

Allow None within a shape in event-model - a dimension with extent None is interpreted as a dimension of unknown or variable length.

Motivation and Context

One scientific motivation for this is event-mode data, common at neutron facilities, where a detector records the position and timestamp of each detection event individually. This means that event-mode detectors conceptually give an array of variable shape each time data is acquired from them, where the dimensionality corresponds to the number of detection events.

How Has This Been Tested?

Tested that the RunEngine now accepts emitting descriptors with a declared shape of (None, ).

At the moment not all downstream consumers are able to process these events - further work will be required to support this. For example the TiledWriter in bluesky does not currently handle this.

This change should be backwards-compatible with all existing data, as it is a loosening of the schema.

@Tom-Willemsen
Tom-Willemsen marked this pull request as ready for review April 15, 2025 16:29

@danielballan danielballan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Tom-Willemsen! One minor suggestion.

@evvaaaa

evvaaaa commented Apr 16, 2025

Copy link
Copy Markdown
Collaborator

Nice change, lgtm

@jacopoabramo

Copy link
Copy Markdown
Contributor

Just a little note since this change is also useful for me and this was discussed in the last dev meeting:

  • it would be nice (if possible) in the future to enhance the schema with another type of document which can attest the change of a descriptor during a run (for example ChangeEvent or something like that);
  • a use case I can attest for this is that during a run slow changing properties such as the region of interest may be changed while the run is being executed (for example while capturing live from a detector);
    • using the "configure" command would invalidate the descriptor document previously produced, as stated here

At run engine level I don't precisely know what this would imply (although I feel it would require big effort), but I suspect it's too big of a feature to implement on the fly and right now for what I need this PR is more than enough (hopefully). Just wanted to mention it for the future.

@DiamondJoseph

DiamondJoseph commented Apr 22, 2025 •

Copy link
Copy Markdown
Contributor

As down stream consumers I'd prefer Optional[List[int]] to List[Optional[int]]: is it one dimension in a list of other known dimensions that will be None or is it either you get the shape or you don't?

Can unknown shape be coerced into an ophyd-async Stream document?

e: currently my downstream Java NeXus converter has

        /** "The shape of the data. Empty list indicates scalar data." */
        @NonNull int[] shape;

which would need to either get boxed as Integer[] for nulls inside the list or lose NonNull if the list became null.

@danielballan

Copy link
Copy Markdown
Member

Good question, this is worth examining.

List[Optional[int]] conveys more information than Optional[List[int]]. For example, [None, None, None] at least tells the consumer that they can expect a three-dimensional array. We have some use cases where outer dimensions are known and inner vary, as in [3, 5, None] or [3, None, None]. It is useful to convey the fixed dimensions.

Also, List[Optional[int]] complies with the Array API definition of array.shape. To be precise, the Array API's intention is to use None to indicate unknown size, and here we are stretching that a bit to mean unknown or variable size, following in the footsteps of the ragged library.

More background is covered in bluesky/tiled#801. Tiled does not yet handle ragged shapes, but we have described how it could be added.

Consumers will need to be updated to either ignore ragged data; raise; pad to the largest size with zero, NaN, or another fill value; truncate to the smallest size; or handle it properly (if possible). HDF5 has no native support for ragged structures, so padding seems like a likely path there.

@Tom-Willemsen

Copy link
Copy Markdown
Member Author

Can unknown shape be coerced into an ophyd-async Stream document?

Is the question about this or have I misunderstood? If so, yes, a similar PR will be needed on ophyd-async to match event-model's definitions - which I was planning to make after/if this one gets through and we've ironed out Optional[list] vs list[Optional] here.

For my initial use-case, the data is 1-dimensional so None vs [None] makes little difference in that case. The only difference is that with [None], bluesky tells me it's 1D, whereas with None I have to "know" that from some external context. I also like the argument above about being able to describe arrays with partially known dimensionality - I can imagine data structures where that may be useful...

@Tom-Willemsen

Copy link
Copy Markdown
Member Author

One other thought - in a previous collaboration call we said:

shape=None or []    # scalar value

Would there be ambiguity then if we also overload the meaning of shape=None to be array of unknown/variable dimensions? How would we differentiate between a scalar, vs an array with an unknown number of dimensions (whose dimension lengths may happen to multiply to 1)?

@evvaaaa

evvaaaa commented Apr 29, 2025 •

Copy link
Copy Markdown
Collaborator

shape=None or [] #scalar value

I would vote for

  • None or NotRequired for a scalar.
  • [] for an array of unknown dimension.
  • [None, None, <etc...>] for an array of unknown shape, but known dimension.
  • [None, 3] and array with unknown length, containing arrays of three elements.

@danielballan

Copy link
Copy Markdown
Member

We have always required the shape key. For scalar, I would favor sticking with None, not loosening the specificiation to accept NotRequired as a synonym.

Does anyone have a use case for an array of unknown dimension? That is hard for consumers to handle, so I would resist supporting it unless we have a compelling need. For example, HDF5 can handle a dimension of unknown/variable length but not an unknown dimensionality. Likewise, Tiled can grow the ability to represent the former, but I have no idea how I would implement the latter.

Where use cases do arise, I think we can still say something more than "unknown dimensionality". For example, imagine a detector that might produce a single image or a stream of images: this could be represented like [None, 512, 512] where the first dimension may be 1 if we receive a single image. In the very worst case, we can write [None, None, <etc>] to express the maximum number of dimensions that might possible arise in the data.

@coretl

coretl commented May 8, 2025 •

Copy link
Copy Markdown
Contributor

Does anyone have a use case for an array of unknown dimension? That is hard for consumers to handle, so I would resist supporting it unless we have a compelling need. For example, HDF5 can handle a dimension of unknown/variable length but not an unknown dimensionality. Likewise, Tiled can grow the ability to represent the former, but I have no idea how I would implement the latter.

I can't think of a use case for it, so I would say we don't support it.

To paste the table we made in a previous collab call:

Measurement Descriptor HDF mimetype Tiled
Always scalar [] or None [N] [N]
Can give M scalars per Event [M], where M may be 1 [M*N] [N, M]
Variable number of scalars per Event [None] [?] [N, None]
Single M-length spectra per Event [1, M] [N, M] [N, 1, M]
Q M-length spectra per Event [Q, M] [N*Q, M] [N, Q, M]
Area Detector taking M 2D images [M, Y, X] [M*N, Y, X] [N, M, Y, X]

I don't think we have changed anything here, although I would suggest we drop None as a possible shape in the descriptor, and require an explicit [] for the "Always scalar" case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Express ragged arrays in Event Descriptor shape

6 participants