Skip to content

Commit 5193bca

Browse files
committed
[feature] add constructor/function overloads for DataFrame
1 parent 04baa8e commit 5193bca

File tree

2 files changed

+91
-8
lines changed

2 files changed

+91
-8
lines changed

Diff for: backend/hdf5/FeatureHDF5.cpp

+64-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <nix/util/util.hpp>
1212
#include <nix/DataArray.hpp>
1313
#include "DataArrayHDF5.hpp"
14+
#include "DataFrameHDF5.hpp"
1415

1516

1617
using namespace std;
@@ -71,10 +72,27 @@ FeatureHDF5::FeatureHDF5(const shared_ptr<IFile> &file, const shared_ptr<IBlock>
7172
: EntityHDF5(file, group, id, time), block(block)
7273
{
7374
linkType(link_type);
74-
targetType(TargetType::DataArray);
7575
// TODO: the line below currently throws an exception if the DataArray
7676
// is not in block - to consider if we prefer copying it to the block
77-
this->data(data.id());
77+
this->data(data);
78+
}
79+
80+
81+
FeatureHDF5::FeatureHDF5(const shared_ptr<IFile> &file, const shared_ptr<IBlock> &block, const H5Group &group,
82+
const string &id, DataFrame data, LinkType link_type)
83+
: FeatureHDF5(file, block, group, id, data, link_type, util::getTime())
84+
{
85+
}
86+
87+
88+
FeatureHDF5::FeatureHDF5(const shared_ptr<IFile> &file, const shared_ptr<IBlock> &block, const H5Group &group,
89+
const string &id, DataFrame data, LinkType link_type, time_t time)
90+
: EntityHDF5(file, group, id, time), block(block)
91+
{
92+
linkType(link_type);
93+
// TODO: the line below currently throws an exception if the DataArray
94+
// is not in block - to consider if we prefer copying it to the block
95+
this->data(data);
7896
}
7997

8098

@@ -91,8 +109,8 @@ void FeatureHDF5::targetType(TargetType ttype) {
91109
}
92110

93111

94-
void FeatureHDF5::data(const std::string &name_or_id) {
95-
std::shared_ptr<IDataArray> ida = block->getEntity<IDataArray>(name_or_id);
112+
void FeatureHDF5::data(const DataArray &data) {
113+
std::shared_ptr<IDataArray> ida = block->getEntity<IDataArray>(data.name());
96114
if (!ida) {
97115
throw std::runtime_error("FeatureHDF5::data: DataArray not found in block!");
98116
}
@@ -103,6 +121,48 @@ void FeatureHDF5::data(const std::string &name_or_id) {
103121
auto target = dynamic_pointer_cast<DataArrayHDF5>(ida);
104122

105123
group().createLink(target->group(), "data");
124+
targetType(TargetType::DataArray);
125+
126+
forceUpdatedAt();
127+
}
128+
129+
130+
void FeatureHDF5::data(const DataFrame &data) {
131+
std::shared_ptr<IDataFrame> idf = block->getEntity<IDataFrame>(data.name());
132+
if (!idf) {
133+
throw std::runtime_error("FeatureHDF5::data: DataFrame not found in block!");
134+
}
135+
if (group().hasGroup("data")) {
136+
group().removeGroup("data");
137+
}
138+
139+
auto target = dynamic_pointer_cast<DataFrameHDF5>(idf);
140+
141+
group().createLink(target->group(), "data");
142+
targetType(TargetType::DataFrame);
143+
forceUpdatedAt();
144+
}
145+
146+
147+
void FeatureHDF5::data(const std::string &name_or_id) {
148+
TargetType tt = TargetType::DataArray;
149+
if (group().hasGroup("data")) {
150+
group().removeGroup("data");
151+
}
152+
std::shared_ptr<IDataArray> ida = block->getEntity<IDataArray>(name_or_id);
153+
if (!ida) {
154+
std::shared_ptr<IDataFrame> idf = block->getEntity<IDataFrame>(name_or_id);
155+
if (!idf) {
156+
throw std::runtime_error("FeatureHDF5::data: entity is not found in block, neither DataArray nor DataFrame!");
157+
}
158+
tt = TargetType::DataFrame;
159+
auto target = dynamic_pointer_cast<DataFrameHDF5>(idf);
160+
group().createLink(target->group(), "data");
161+
} else {
162+
auto target = dynamic_pointer_cast<DataArrayHDF5>(ida);
163+
group().createLink(target->group(), "data");
164+
}
165+
targetType(tt);
106166
forceUpdatedAt();
107167
}
108168

Diff for: backend/hdf5/FeatureHDF5.hpp

+27-4
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class FeatureHDF5 : virtual public base::IFeature, public EntityHDF5 {
6060

6161
std::shared_ptr<base::IBlock> block;
6262
void targetType(TargetType type);
63-
63+
6464
public:
6565

6666
/**
@@ -69,17 +69,29 @@ class FeatureHDF5 : virtual public base::IFeature, public EntityHDF5 {
6969
FeatureHDF5(const std::shared_ptr<base::IFile> &file, const std::shared_ptr<base::IBlock> &block, const H5Group &group);
7070

7171
/**
72-
* Standard constructor for new Feature
72+
* Standard constructor for new Feature linking to a DataArray
7373
*/
7474
FeatureHDF5(const std::shared_ptr<base::IFile> &file, const std::shared_ptr<base::IBlock> &block, const H5Group &group,
7575
const std::string &id, DataArray data, LinkType link_type);
7676

7777
/**
78-
* Standard constructor for new Feature with time
78+
* Standard constructor for new Feature linking to a DataFrame
79+
*/
80+
FeatureHDF5(const std::shared_ptr<base::IFile> &file, const std::shared_ptr<base::IBlock> &block, const H5Group &group,
81+
const std::string &id, DataFrame data, LinkType link_type);
82+
83+
/**
84+
* Standard constructor for new Feature linking a DataArray with time
7985
*/
8086
FeatureHDF5(const std::shared_ptr<base::IFile> &file, const std::shared_ptr<base::IBlock> &block, const H5Group &group,
8187
const std::string &id, DataArray data, LinkType link_type, time_t time);
8288

89+
/**
90+
* Standard constructor for new Feature linking a DataFrame with time
91+
*/
92+
FeatureHDF5(const std::shared_ptr<base::IFile> &file, const std::shared_ptr<base::IBlock> &block, const H5Group &group,
93+
const std::string &id, DataFrame data, LinkType link_type, time_t time);
94+
8395

8496
void linkType(LinkType type);
8597

@@ -89,8 +101,19 @@ class FeatureHDF5 : virtual public base::IFeature, public EntityHDF5 {
89101

90102
TargetType targetType() const;
91103

104+
/**
105+
* links to the given data (DataArray or DataFrame) method tries to find the given name_or_id
106+
* first among the DataArrays and then the DataFrames. When passing a name there might be
107+
* an ambiguity that cannot be resolved. Rather, use the id or use the data(DataArray) or
108+
* data(DataFrame) overloads instead.
109+
*/
110+
DEPRECATED void data(const std::string &name_or_id);
111+
112+
113+
void data(const DataArray &data);
114+
92115

93-
void data(const std::string &name_or_id);
116+
void data(const DataFrame &data);
94117

95118

96119
std::shared_ptr<base::IDataArray> data() const;

0 commit comments

Comments
 (0)