Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add data read #85

Merged
merged 211 commits into from
Jan 13, 2025
Merged

Add data read #85

merged 211 commits into from
Jan 13, 2025

Conversation

oruebel
Copy link
Contributor

@oruebel oruebel commented Aug 31, 2024

This PR is to try and implement the proposed approach for data read from #83 to see if this approach is viable. This PR is experimental right now and should not be merged.

2. Proposed Implementation for reading data arrays

BaseReadData

  • Create a new ReadDatasetWrapper and ReadAttributeWrapper classes for reading data. This is modified from the proposal, which suggested a single BaseReadData class for reading any array (datasets, attributes) from a file.
  • Support conversion to boost multi-dimensional array for convenience
  • Note I did not update BaseRecordingData to inherit from ReadDataWrapper because the two are not compatible right now. BaseReadData uses the io and the path to access the data, whereas BaseRecordingData leaves that choice to the I/O backend and stores the references to the dataset. We may or may not want to change this.

BaseIO

  • Note I did not add abstract methods for lazy reading objects from a file to BaseIO (or more accurately I remove them) because: 1) I wanted to use the shared_ptr to the I/O rather than a raw pointer, which I can't get from BaseIO, and 2) with the ReadDatasetWrapper this is more approbriately done in the Container class directly.
  • Add pure virtual method to allow us to get the storage object type (Group, Dataset, Attribute) for a given path
  • Add pure virtual methods to read data values from a Dataset or Attribute that the BaseReadData can call for read

HDF5IO

  • Note In contrast to the proposal, I did not implement specific version of the BaseReadData for HDF5 but left read logic to HDF5IO itself so that the ReadDatasetWrapper can remain generic. To make this more manageable, I defined ReadAttributeWrapper separately
  • Implement the methods for reading data values from a Dataset or Attribute that the HDF5ReadDataSet and HDF5ReadAttribute wrappers can call can call for read
  • Implement the getObjectType method for getting the storage object type (Group, Dataset, Attribute) for a given path

Container

  • Store the io object on the Container so that we can call io->readDataset and io->readAttribute in the read methods

NWB types: TimeSeries, ElectricalSeries etc.

  • Remove storage of properties from the Container classes and replace them with access methods that return BaseReadData objects instead. This allows for reading in both read and write mode and avoids keeping data in memory that we have already written to disk. For example, in TimeSeries, these variables would need to change to properties:
    /**
    * @brief Base unit of measurement for working with the data. Actual stored
    * values are not necessarily stored in these units. To access the data in
    * these units, multiply ‘data’ by ‘conversion’ and add ‘offset’.
    */
    std::string unit;
    /**
    * @brief The description of the TimeSeries.
    */
    std::string description;
    /**
    * @brief Human-readable comments about the TimeSeries.
    */
    std::string comments;
    /**
    * @brief Size used in dataset creation. Can be expanded when writing if
    * needed.
    */
    SizeArray dsetSize;
    /**
    * @brief Chunking size used in dataset creation.
    */
    SizeArray chunkSize;
    /**
    * @brief Scalar to multiply each element in data to convert it to the
    * specified ‘unit’.
    */
    float conversion;
    /**
    * @brief Smallest meaningful difference between values in data, stored in the
    * specified by unit.
    */
    float resolution;
    /**
    * @brief Scalar to add to the data after scaling by ‘conversion’ to finalize
    * its coercion to the specified ‘unit’.
    */
    float offset;
    /**
    * @brief The starting time of the TimeSeries.
    */
    float startingTime = 0.0;

3. Proposed implementation for reading whole Containers (e.g., to read an ElectricalSeries)

  • Add access methods on the respective Container that owns the respective objects, e.g., NWBFile owning ElectricalSeries objects to retrieve the object
  • Add abstract factory method (that is templated on the return type) to Container to create an instance of the specific Container type using only the io and path for the Container as input. The specific Container classes, such as TimeSeries will then need to implement a corresponding constructor that uses io and path as input.

Step 1: Define the Template Factory Method in Container

class Container {
public:
   
    template <typename T>
    static std::unique_ptr<T> create(const BaseIO& io, const std::string& path) {
        static_assert(std::is_base_of<Container, T>::value, "T must be a derived class of Container");
        return std::unique_ptr<T>(new T(path, io));
    }
};

Step 2: Implement the constructors on the specific Container classes (e.g., TimeSeries)

  • Add the necessary constructor
class TimeSeries : public Container {
public:
    TimeSeries(const std::string& path, const BaseIO& io) {
        // Implementation of TimeSeries constructor
    }
};

4. Proposed implementation for reading untyped groups (e.g., /acquisition)

I'm not sure we'll need do this, since a group by itself does not define data. To access the contents we could define access methods on the parent Container class (e.g., NWBFile) that owns the untyped group to access its contents.

TODO

Next steps

Items moved to new issues

@oruebel oruebel requested a review from stephprince September 1, 2024 08:45
@oruebel
Copy link
Contributor Author

oruebel commented Sep 1, 2024

@stephprince when you get a chance ,could you please do a first code review of this PR to make sure this is heading in the right direction. I now have a first outline of one possible solution for how we might implement read. There is still a lot more work to be done before this PR is ready, but it would be useful if you could take a look before I go any further with this approach.

I would start by looking at:

  1. tests/examples/test_ecephys_data_read.cpp which shows an example of how read works for the user
  2. BaseIO then defines the main new classes used for reading and HDF5IO then implements the actual reading
  3. Container and ElectricalSeries also have some relevant changes to allow us to construct Container objects for read and how we can get specific datasets/attributes

@oruebel
Copy link
Contributor Author

oruebel commented Sep 1, 2024

@stephprince I just added a documentation page as well, which hopefully helps explain the current proposed design for read so we can review and discuss.

@oruebel oruebel mentioned this pull request Jan 13, 2025
@oruebel oruebel marked this pull request as ready for review January 13, 2025 19:19
@oruebel
Copy link
Contributor Author

oruebel commented Jan 13, 2025

@stephprince Looking through the remaining ToDo items, I think we can reasonably create separate issues and PRs for those. I think this is now reasonably well tested that we can merge this and create new issues for the missing items.

Copy link
Collaborator

@stephprince stephprince left a comment

Choose a reason for hiding this comment

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

That sounds good to me! 🎉

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.

Propose refactor of I/O class organization
3 participants