| 1 | #include "testutil.hpp" |
| 2 | #include <nix.hpp> |
| 3 | |
| 4 | |
| 5 | std::vector<nix::DataType> dtypes = { |
| 6 | nix::DataType::UInt8, |
| 7 | nix::DataType::UInt16, |
| 8 | nix::DataType::UInt32, |
| 9 | nix::DataType::UInt64, |
| 10 | nix::DataType::Int8, |
| 11 | nix::DataType::Int16, |
| 12 | nix::DataType::Int32, |
| 13 | nix::DataType::Int64, |
| 14 | nix::DataType::Float, |
| 15 | nix::DataType::Double, |
| 16 | nix::DataType::String, |
| 17 | nix::DataType::Bool |
| 18 | }; |
| 19 | |
| 20 | |
| 21 | int main(int argc, char* argv[]) { |
| 22 | if (argc != 2) { |
| 23 | std::cerr << "Please specify a nix file (and nothing else)" << std::endl; |
| 24 | return 1; |
| 25 | } |
| 26 | std::string fname = argv[1]; |
| 27 | nix::File nf = nix::File::open(fname, nix::FileMode::Overwrite); |
| 28 | |
| 29 | auto block = nf.createBlock("blockyblock", "ablocktype of thing"); |
| 30 | block.definition("I am a test block"); |
| 31 | block.forceCreatedAt(1500001000); |
| 32 | |
| 33 | block = nf.createBlock("I am another block", "void"); |
| 34 | block.definition("Void block of stuff"); |
| 35 | block.forceCreatedAt(1500002000); |
| 36 | |
| 37 | block = nf.createBlock("Block C", "a block of stuff"); |
| 38 | block.definition("The third block"); |
| 39 | block.forceCreatedAt(1500003000); |
| 40 | |
| 41 | int idx = 0; |
| 42 | char name [7]; |
| 43 | nix::Group group; |
| 44 | for (auto bl : nf.blocks()) { |
| 45 | sprintf(name, "grp%02d0", idx); |
| 46 | group = bl.createGroup(name, "grp"); |
| 47 | group.definition("group 0"); |
| 48 | group.forceCreatedAt(bl.createdAt()); |
| 49 | sprintf(name, "grp%02d1", idx); |
| 50 | group = bl.createGroup(name, "grp"); |
| 51 | group.definition("group 1"); |
| 52 | group.forceCreatedAt(bl.createdAt()); |
| 53 | idx++; |
| 54 | } |
| 55 | |
| 56 | block = nf.getBlock(0); |
| 57 | std::vector<double> datadbl = {1, 2, 10, 9, 1, 3}; |
| 58 | auto da = block.createDataArray("bunchodata", "recordings", nix::DataType::Double, nix::NDSize{2, 3}); |
| 59 | da.setData(nix::DataType::Double, datadbl.data(), nix::NDSize{2, 3}, nix::NDSize{0, 0}); |
| 60 | da.definition("A silly little data array"); |
| 61 | auto smpldim = da.appendSampledDimension(0.1); |
| 62 | smpldim.unit("ms"); |
| 63 | smpldim.label("time"); |
| 64 | auto setdim = da.appendSetDimension(); |
| 65 | setdim.labels({"a", "b"}); |
| 66 | group = block.getGroup(0); |
| 67 | group.addDataArray(da); |
| 68 | |
| 69 | // Data Frame |
| 70 | std::vector<nix::Column> cols = {{"str", "", nix::DataType::String} |
| 71 | ,{"Double", "A", nix::DataType::Double}, |
| 72 | {"int64", "ms", nix::DataType::Int64}, |
| 73 | {"bool", "", nix::DataType::Bool}}; |
| 74 | auto df = block.createDataFrame("table", "filing", cols); |
| 75 | std::vector<nix::Variant> vals = {nix::Variant("exp1"), |
| 76 | nix::Variant(42.1), nix::Variant(10), nix::Variant(false)}; |
| 77 | df.rows(1); |
| 78 | df.writeRow(0, vals); |
| 79 | group.addDataFrame(df); |
| 80 | df.rows(2); |
| 81 | vals = {nix::Variant("exp2"), |
| 82 | nix::Variant(30.2), nix::Variant(4), nix::Variant(true)}; |
| 83 | df.writeRow(1, vals); |
| 84 | |
| 85 | datadbl = {0.4, 0.41, 0.49, 0.1, 0.1, 0.1}; |
| 86 | auto featda = block.createDataArray("feat-da", "tag-feature", nix::DataType::Double, nix::NDSize{6}); |
| 87 | featda.setData(nix::DataType::Double, datadbl.data(), nix::NDSize{6}, nix::NDSize{0}); |
| 88 | auto tag = block.createTag("tagu", "tagging", {1, 0}); |
| 89 | tag.extent({1, 10}); |
| 90 | tag.units({"mV", "s"}); |
| 91 | tag.definition("tags ahoy"); |
| 92 | tag.addReference(da); |
| 93 | group.addTag(tag); |
| 94 | tag.createFeature(featda, nix::LinkType::Untagged); |
| 95 | |
| 96 | |
| 97 | |
| 98 | auto mtag = block.createMultiTag("mtagu", "multi tagging", block.createDataArray("tag-data", "multi-tagger", nix::DataType::Double, nix::NDSize{1, 3})); |
| 99 | datadbl = {0, 0.1, 10.1}; |
| 100 | // MultiTag positions array |
| 101 | da = block.getDataArray("tag-data"); |
| 102 | da.setData(nix::DataType::Double, datadbl.data(), nix::NDSize{1, 3}, nix::NDSize{0, 0}); |
| 103 | |
| 104 | smpldim = da.appendSampledDimension(0.01); |
| 105 | smpldim.unit("s"); |
| 106 | da.appendSetDimension(); |
| 107 | |
| 108 | // MultiTag extents array |
| 109 | datadbl = {0.5, 0.5, 0.5}; |
| 110 | da = block.createDataArray("tag-extents", "multi-tagger", nix::DataType::Double, nix::NDSize{1, 3}); |
| 111 | da.setData(nix::DataType::Double, datadbl.data(), nix::NDSize{1, 3}, nix::NDSize{0, 0}); |
| 112 | mtag.extents(da); |
| 113 | smpldim = da.appendSampledDimension(0.01); |
| 114 | smpldim.unit("s"); |
| 115 | da.appendSetDimension(); |
| 116 | |
| 117 | std::vector<int64_t> datai64 = {100, 200, 210, 3}; |
| 118 | da = nf.getBlock(1).createDataArray("FA001", "Primary data", nix::DataType::Int64, nix::NDSize{4}); |
| 119 | da.setData(nix::DataType::Int64, datai64.data(), nix::NDSize{4}, nix::NDSize{0}); |
| 120 | da.definition("Some random integers"); |
| 121 | |
| 122 | // Source tree |
| 123 | block = nf.getBlock(0); |
| 124 | auto src = block.createSource("root-source", "top-level-source"); |
| 125 | |
| 126 | // Point all (block's) data arrays to root-source |
| 127 | for (auto da : block.dataArrays()) |
| 128 | da.addSource(src); |
| 129 | |
| 130 | auto srcd1 = src.createSource("d1-source", "second-level-source"); |
| 131 | src.createSource("d1-source-2", "second-level-source"); |
| 132 | // point first da to d1-source |
| 133 | block.getDataArray(0).addSource(srcd1); |
| 134 | |
| 135 | // Metadata |
| 136 | // 3 root sections |
| 137 | for (auto name : {"mda", "mdb", "mdc"}) |
| 138 | nf.createSection(name, "root-section"); |
| 139 | |
| 140 | auto sec = nf.getSection("mdc"); |
| 141 | |
| 142 | // 6 sections under third root section |
| 143 | for (idx = 0; idx < 6; idx++) { |
| 144 | sprintf(name, "%03d-md", idx); |
| 145 | sec.createSection(name, "d1-section"); |
| 146 | } |
| 147 | |
| 148 | // Point existing objects to metadata sections |
| 149 | nf.getBlock(0).metadata(nf.getSection("mdb")); |
| 150 | nf.getBlock(2).metadata(nf.getSection("mdb")); |
| 151 | |
| 152 | nf.getBlock(1).getDataArray(0).metadata(nf.getSection("mda")); |
| 153 | nf.getBlock(0).getTag(0).metadata(nf.getSection("mdc").getSection(3)); |
| 154 | |
| 155 | // Add Tag and MultiTag to Block 2, Group 0 |
| 156 | block = nf.getBlock(2); |
| 157 | group = block.getGroup(0); |
| 158 | tag = block.createTag("POI", "TAG", {0, 0}); |
| 159 | tag.extent({1920, 1080}); |
| 160 | tag.units({"mm", "mm"}); |
| 161 | |
| 162 | auto png = block.createDataArray("some-sort-of-image?", "png", nix::DataType::Double, nix::NDSize{3840, 2160}); |
| 163 | tag.createFeature(png, nix::LinkType::Indexed); |
| 164 | |
| 165 | auto newmtpositions = block.createDataArray("nu-pos", "multi-tag-positions", nix::DataType::Double, nix::NDSize{10, 3}); |
| 166 | auto newmtag = block.createMultiTag("nu-mt", "multi-tag (new)", newmtpositions); |
| 167 | group.addTag(tag); |
| 168 | group.addMultiTag(newmtag); |
| 169 | |
| 170 | // Data with RangeDimension |
| 171 | std::vector<int32_t> datai32 = {0, 1, 23}; |
| 172 | block = nf.getBlock(2); |
| 173 | da = block.createDataArray("the ticker", "range-dim-array", nix::DataType::Int32, nix::NDSize{3}); |
| 174 | da.setData(nix::DataType::Int32, datai32.data(), nix::NDSize{3}, nix::NDSize{0}); |
| 175 | da.unit("uA"); |
| 176 | datadbl.clear(); |
| 177 | for (idx = 0; idx < 50; idx++) |
| 178 | datadbl.push_back(10+(idx*0.1)); |
| 179 | auto rdim = da.appendRangeDimension(datadbl); |
| 180 | rdim.label("a range dimension"); |
| 181 | rdim.unit("s"); |
| 182 | |
| 183 | // Alias RangeDimension |
| 184 | block = nf.getBlock(1); |
| 185 | da = block.createDataArray("alias da", "dimticks", nix::DataType::Int32, nix::NDSize{24}); |
| 186 | datadbl.clear(); |
| 187 | for (idx = 0; idx < 24; idx++) |
| 188 | datadbl.push_back(3+(idx*0.5)); |
| 189 | da.label("alias dimension label"); |
| 190 | da.unit("F"); |
| 191 | da.appendAliasRangeDimension(); |
| 192 | |
| 193 | // All types of metadata |
| 194 | std::vector<nix::Variant> values; |
| 195 | sec = nf.getSection("mdb"); |
| 196 | auto proptypesmd = sec.createSection("prop-test-parent", "test metadata section"); |
| 197 | auto numbermd = proptypesmd.createSection("numerical metadata", "test metadata section"); |
| 198 | numbermd.createProperty("integer", nix::Variant(int32_t(42))); |
| 199 | numbermd.createProperty("float", nix::Variant(float(4.2))); |
| 200 | for (int32_t v : {40, 41, 42, 43, 44, 45}) |
| 201 | values.push_back(nix::Variant(v)); |
| 202 | numbermd.createProperty("integers", values); |
| 203 | values = {nix::Variant(float(1.1)), nix::Variant(float(10.10))}; |
| 204 | numbermd.createProperty("floats", values); |
| 205 | |
| 206 | auto othermd = proptypesmd.createSection("other metadata", "test metadata section"); |
| 207 | othermd.createProperty("bool", nix::Variant(true)); |
| 208 | othermd.createProperty("false bool", nix::Variant(false)); |
| 209 | othermd.createProperty("bools", {nix::Variant(true), nix::Variant(false), nix::Variant(true)}); |
| 210 | othermd.createProperty("string", nix::Variant("I am a string. Rawr.")); |
| 211 | othermd.createProperty("strings", {nix::Variant("one"), nix::Variant("two"), nix::Variant("twenty")}); |
| 212 | othermd.createProperty("unicode", {nix::Variant("Μπύρα"), nix::Variant("Bräu"), nix::Variant("啤酒"), nix::Variant("🍺")}); |
| 213 | |
| 214 | // All types of data |
| 215 | block = nf.createBlock("datablock", "block of data"); |
| 216 | |
| 217 | for (auto dt : dtypes) { |
| 218 | block.createDataArray(nix::data_type_to_string(dt), "dtype-test-array", dt, nix::NDSize{0}); |
| 219 | } |
| 220 | |
| 221 | // Unicode data |
| 222 | auto unicodeda = block.createDataArray("unicodedata", "dtype-test-array", nix::DataType::String, nix::NDSize{4}); |
| 223 | std::vector<std::string> unicode_array = {"Καφές", "Café", "咖啡", "☕"}; |
| 224 | unicodeda.setData(nix::DataType::String, unicode_array.data(), nix::NDSize{4}, nix::NDSize{0}); |
| 225 | |
| 226 | return 0; |
| 227 | } |