linkml / linkml-arrays

Support for N-dimensional arrays in LinkML.
https://linkml.github.io/linkml-arrays
BSD 3-Clause "New" or "Revised" License
4 stars 1 forks source link

interpret `max_cardinality` and `min_cardinality` as axis size constraints? #5

Open sneakers-the-rat opened 5 months ago

sneakers-the-rat commented 5 months ago

Something that would be nice to have is the ability to constrain the size of an array. I think the example of a "video" type is a good motivating case - if we are accepting an RGB Video, we want to be able to express "x and y axes of any size, but channels == 3"

I used min_cardinality and max_cardinality in nwb-linkml for this, such that for an RGBImage one expresses the array component as (with minor adjustments to match linkml-array syntax):

RGBImage__Array:
  name: RGBImage__Array
  is_a: linkml:NDArray
  attributes:
    x:
      implements:
        - linkml:axis
      name: x
      range: numeric
      required: true
    y:
      implements:
        - linkml:axis
      name: y
      range: numeric
      required: true
    r, g, b:
      implements:
        - linkml:axis
      name: r, g, b
      range: numeric
      required: true
      minimum_cardinality: 3
      maximum_cardinality: 3

creates a pydantic model like this (i also collapse the array class into the parent class that has other attributes there, but you get the idea)

class RGBImage(Image):
    """
    A color image.
    """
    linkml_meta: ClassVar[LinkML_Meta] = Field(LinkML_Meta(tree_root=True), frozen=True)
    name: str = Field(...)
    array: Optional[NDArray[Shape["* x, * y, 3 r_g_b"], Number]] = Field(None)
    resolution: Optional[float] = Field(None, description="""Pixel resolution of the image, in pixels per centimeter.""")
    description: Optional[str] = Field(None, description="""Description of the image.""")

what do y'all think about having that be the means of specifying array sizes? It seems to match the semantics of that slot as its used elsewhere, and so we basically get it for free right?

If there was one suggestion i'd make to a change to the metamodel, it's sort of awkward to have to specify both the max and min to say that cardinality should == a value, so it might be nice to change that to work like:

# cardinality is exactly three
cardinality: 3

# minimum only
cardinality:
  min: 3

# range
cardinality:
  min: 3
  max: 5

but that's just a "nice to have" not a critical thing

rly commented 5 months ago

I used min_cardinality and max_cardinality

That makes sense and looks good to me.

I also think your suggested change to the metamodel would be nice. @cmungall what do you think?

cmungall commented 5 months ago

Few things to unpack here…

Shouldn’t the constraints be on the NDArray rather than DataArray axes?

It seems we want to align as closely as possible to nptyping https://github.com/ramonhagenaars/nptyping/blob/master/USERDOCS.md#Examples

More later…

On Sat, Feb 3, 2024 at 12:51 AM Ryan Ly @.***> wrote:

I used min_cardinality and max_cardinality

That makes sense and looks good to me.

I also think your suggested change to the metamodel would be nice. @cmungall https://github.com/cmungall what do you think?

— Reply to this email directly, view it on GitHub https://github.com/linkml/linkml-arrays/issues/5#issuecomment-1925221553, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAMMOIBDH75OKHCV465J7DYRX273AVCNFSM6AAAAABCXUNUVOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMRVGIZDCNJVGM . You are receiving this because you were mentioned.Message ID: @.***>

sneakers-the-rat commented 5 months ago

Yes maybe i need to clear up how y'all are thinking about the DataArray axes, but wherever we would put them, in this case just suggesting these be the terms used (i figure y'all had probably already thought about this, just wanted to start the ball rolling on getting it in the model somehow)