question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New accessor for a biomechanics open source library

See original GitHub issue

Hello and first of all, thank you for xarray.

With a colleague, we created pyomeca which is a Python library specialized in biomechanical data processing. Most of the data is multidimensional and so we have reimplemented by hand some functionality to access labels, etc. Except Xarray does it much better.

I am rewriting the entire library using xarray. motion reimplements the pyomeca functions with an accessor for xarray.

I have some questions about the architecture you recommend for a xarray-based library.

In pyomeca, I have three classes (RotoTrans, Markers3d and Analogs3d) with each having very specific functions.

I have two possible solutions to implement them with xarray: (1) write an accessor for each class or (2) inherit from DataArray, e.g:

import xarray

Rototrans class (xarray.DataArray):
    pass

According to your documentation, the accesor is the recommended method. However, for most functions, I need a three-dimensional table with specific dimensions ([axis, channels, time_frame]) and the accessor does not allow to perform these checks when creating the DataArray. At the same time, the second solution forces me to inherit a basic function that allows me to access the functions common to all classes

import xarray

class BaseArray(xarray.DataArray):
    def fct():
        pass
    
class Rototrans(Base):
    pass

Do you have any design recommendations?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
shoyercommented, Nov 29, 2019

What about simply wrapping xarray objects internally in your classes, along with utilities for converting to/from xarray objects? When users want to do domain specific stuff they can use your objects, and when they want to do something generic with xarray they can convert by calling a method like to_xarray.

1reaction
crusaderkycommented, Nov 29, 2019

+1 to shoyer - encapsulation is by far the easiest approach.

Why can’t you initialise whatever you need to upon first access?

How do you do that?

@xarray.register_dataset_accessor('foo')
class FooAccessor:
    def __init__(self, xarray_obj):
        self.obj = xarray_obj
        # <insert whatever health checks on xarray_obj>
        self.x = something(xarray_obj)

    @property
    def bar(self):
        # snip

    def baz(self):
        # snip

The accessor __init__ method will be invoked by xarray the first time you invoke array.foo.

Note, however, that it is not recommended to put expensive calculations in it, because the object will possibly be destroyed and reinitialised every time the array is transformed - or in other words, anything that is not a read-only method/property or a cell update.

I emphasized possibly because some transforms may not destroy and recreate your accessor instance, thus potentially causing it to be in a state that is incoherent with the attached xarray object. Every time you invoke a method, you should verify that whatever assumptions you relied upon to generate the state are still valid.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pyomeca
Pyomeca is a python library allowing you to carry out a complete biomechanical analysis; in a simple, logical and concise way.
Read more >
How to Use the Motion Library - The Biomechanist
The MoLib is a free library of different types of sport and clinically relevant movements for students and teachers.
Read more >
Software recreates complex movements for medical ... - Phys.org
An open-source movement simulator that has already helped solve problems in medicine, paleontology, and animal locomotion has been expanded ...
Read more >
pyomeca: An Open-Source Framework for Biomechanical ...
pyomeca: An Open-Source Framework for Biomechanical Analysis.
Read more >
OpenSim: Open-Source Software to Create and Analyze ...
do not provide full access to source code, which makes it difficult for biomechanics researchers to extend their capabilities. Over the past decade,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found