Skip to content

Commit 7ff0016

Browse files
committed
Add a dimension labels example.
1 parent 5e7b7bf commit 7ff0016

2 files changed

Lines changed: 201 additions & 0 deletions

File tree

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
using System;
2+
using System.Linq;
3+
4+
namespace TileDB.CSharp.Examples
5+
{
6+
public static class ExampleDimensionLabels
7+
{
8+
private static readonly string ArrayPath = ExampleUtil.MakeExamplePath("dimension-labels");
9+
private static readonly Context Ctx = Context.GetDefault();
10+
11+
private static void CreateArray()
12+
{
13+
using var xIndex = Dimension.Create(Ctx, "x_index", 0, 5, 6);
14+
using var sample = Dimension.Create(Ctx, "sample", 0, 3, 4);
15+
16+
using var domain = new Domain(Ctx);
17+
domain.AddDimensions(xIndex, sample);
18+
19+
using var a = Attribute.Create<short>(Ctx, "a");
20+
21+
using var schema = new ArraySchema(Ctx, ArrayType.Dense);
22+
schema.SetCellOrder(LayoutType.RowMajor);
23+
schema.SetTileOrder(LayoutType.RowMajor);
24+
schema.SetDomain(domain);
25+
schema.AddAttribute(a);
26+
schema.AddDimensionLabel(0, "x", DataOrder.Increasing, DataType.Float64);
27+
schema.AddDimensionLabel(0, "y", DataOrder.Increasing, DataType.Float64);
28+
schema.AddDimensionLabel(1, "timestamp", DataOrder.Increasing, DataType.DateTimeSecond);
29+
30+
Array.Create(Ctx, ArrayPath, schema);
31+
}
32+
33+
private static void WriteArrayAndLabels()
34+
{
35+
short[] a = Enumerable.Range(1, 24).Select(x => (short)x).ToArray();
36+
double[] x = { -1.0, -0.6, -0.2, 0.2, 0.6, 1.0 };
37+
double[] y = { 0.0, 2.0, 4.0, 6.0, 8.0, 10.0 };
38+
long[] timestamp = { 31943, 32380, 33131, 33228 };
39+
40+
using Array array = new Array(Ctx, ArrayPath);
41+
array.Open(QueryType.Write);
42+
43+
using Query query = new Query(Ctx, array);
44+
query.SetLayout(LayoutType.RowMajor);
45+
query.SetDataBuffer("a", a);
46+
query.SetDataBuffer("x", x);
47+
query.SetDataBuffer("y", y);
48+
query.SetDataBuffer("timestamp", timestamp);
49+
50+
query.Submit();
51+
52+
if (query.Status() != QueryStatus.Completed)
53+
{
54+
throw new Exception("Write query did not complete.");
55+
}
56+
57+
array.Close();
58+
}
59+
60+
private static void ReadArrayAndLabels()
61+
{
62+
Console.WriteLine("Read from main array");
63+
64+
using var array = new Array(Ctx, ArrayPath);
65+
array.Open(QueryType.Read);
66+
67+
using var subarray = new Subarray(array);
68+
subarray.AddRange(0, 1, 2);
69+
subarray.AddRange(1, 0, 2);
70+
71+
short[] a = new short[6];
72+
double[] x = new double[2];
73+
double[] y = new double[2];
74+
long[] timestamp = new long[3];
75+
76+
using var query = new Query(Ctx, array);
77+
query.SetLayout(LayoutType.RowMajor);
78+
query.SetSubarray(subarray);
79+
query.SetDataBuffer("a", a);
80+
query.SetDataBuffer("x", x);
81+
query.SetDataBuffer("y", y);
82+
query.SetDataBuffer("timestamp", timestamp);
83+
84+
query.Submit();
85+
86+
if (query.Status() != QueryStatus.Completed)
87+
{
88+
throw new Exception("Read query did not complete.");
89+
}
90+
91+
for (int i = 0; i < 2; i++)
92+
{
93+
for (int j = 0; j < 3; j++)
94+
{
95+
int x_val = i + 1;
96+
int sample_val = j;
97+
Console.WriteLine($"Cell ({x_val}, {sample_val})");
98+
Console.WriteLine($" * a({x_val}, {sample_val}) = {a[3 * i + j]}");
99+
Console.WriteLine($" * x({x_val}) = {x[i]}");
100+
Console.WriteLine($" * y({x_val}) = {y[i]}");
101+
Console.WriteLine($" * timestamp({sample_val}) = {TimeSpan.FromSeconds(timestamp[j])}");
102+
}
103+
}
104+
105+
array.Close();
106+
}
107+
108+
private static void ReadTimestampData()
109+
{
110+
Console.WriteLine("Read from dimension label");
111+
112+
using var array = new Array(Ctx, ArrayPath);
113+
array.Open(QueryType.Read);
114+
115+
using var subarray = new Subarray(array);
116+
subarray.AddRange(1, 1, 3);
117+
118+
long[] timestamp = new long[3];
119+
120+
using var query = new Query(Ctx, array);
121+
query.SetLayout(LayoutType.RowMajor);
122+
query.SetSubarray(subarray);
123+
query.SetDataBuffer("timestamp", timestamp);
124+
125+
query.Submit();
126+
127+
if (query.Status() != QueryStatus.Completed)
128+
{
129+
throw new Exception("Read query did not complete.");
130+
}
131+
132+
for (int i = 0; i < 3; i++)
133+
{
134+
int sample_val = i + 1;
135+
Console.WriteLine($"Cell ({sample_val})");
136+
Console.WriteLine($" * timestamp({sample_val}) = {TimeSpan.FromSeconds(timestamp[i])}");
137+
}
138+
139+
array.Close();
140+
}
141+
142+
private static void ReadArrayByLabel()
143+
{
144+
Console.WriteLine("Read array from label ranges");
145+
146+
using var array = new Array(Ctx, ArrayPath);
147+
array.Open(QueryType.Read);
148+
149+
using var subarray = new Subarray(array);
150+
subarray.AddLabelRange("y", 3.0, 8.0);
151+
subarray.AddLabelRange<long>("timestamp", 31943, 32380);
152+
153+
short[] a = new short[6];
154+
double[] y = new double[3];
155+
long[] timestamp = new long[2];
156+
157+
using var query = new Query(Ctx, array);
158+
query.SetLayout(LayoutType.RowMajor);
159+
query.SetSubarray(subarray);
160+
query.SetDataBuffer("y", y);
161+
query.SetDataBuffer("timestamp", timestamp);
162+
query.SetDataBuffer("a", a);
163+
164+
query.Submit();
165+
166+
if (query.Status() != QueryStatus.Completed)
167+
{
168+
throw new Exception("Read query did not complete.");
169+
}
170+
171+
for (int i = 0; i < 3; i++)
172+
{
173+
for (int j = 0; j < 2; j++)
174+
{
175+
Console.WriteLine($"Cell ({y[i]}, {TimeSpan.FromSeconds(timestamp[j])})");
176+
Console.WriteLine($" * a = {a[2 * i + j]}");
177+
}
178+
}
179+
180+
array.Close();
181+
}
182+
183+
public static void Run()
184+
{
185+
Ctx.RemoveObject(ArrayPath);
186+
if (Ctx.GetObjectType(ArrayPath) != ObjectType.Array)
187+
{
188+
CreateArray();
189+
WriteArrayAndLabels();
190+
}
191+
192+
ReadArrayAndLabels();
193+
Console.WriteLine();
194+
ReadTimestampData();
195+
Console.WriteLine();
196+
ReadArrayByLabel();
197+
Console.WriteLine();
198+
}
199+
}
200+
}

examples/TileDB.CSharp.Example/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ static void Main(string[] args)
1515
ExampleIncompleteQueryVariableSize.Run();
1616
ExampleWritingDenseGlobal.Run();
1717
ExampleWritingSparseGlobal.Run();
18+
ExampleDimensionLabels.Run();
1819

1920
ExampleFile.RunLocal();
2021
// ExampleFile.RunCloud("tiledb_api_token", "tiledb_namespace", "new_cloud_array_name", "s3://bucket/prefix/");

0 commit comments

Comments
 (0)