我有几个 csv 格式的文件,其中包含科学数据,用于检查以 3D 方式绘制它们。 文件内容按日期时间、频率(以 Hz 为单位)和强度(以 dB 为单位)进行组织,如下例所示:
2014-03-15 18:00:00, 19700000.0, -30.19
2014-03-15 18:00:00, 19700781.25, -31.19
2014-03-15 18:00:00, 19701562.5, -30.43
2014-03-15 18:00:00, 19702343.75, -30.37
2014-03-15 18:00:00, 19703125.0, -27.06
对于绘图上下文,3 个值分别代表 x、y、z。
我想创建一个名为 datasample 的自定义类型,其中包含 datetime,float,float 类型和然后将数组声明为
ILArray<datasample>
是否有从文件加载 ILArray 进行绘图的特定函数或建议方法?
ILNumerics 能否正确处理第一列的日期时间格式,或者我是否需要预处理文件以将其转换为其他格式?
感谢您的支持
在回答后更新了以下源代码 我已经加载了 360 万点,并且 3d 绘图渲染得很流畅。 我需要一些关于如何根据 Z 值对各个点进行着色的说明。 似乎使用具有数百万个输入点的表面有点......沉重:-)我可以使用任何性能提示吗?
这是屏幕截图:
using ILNumerics;
using ILNumerics.Drawing;
using ILNumerics.Drawing.Plotting;
using System.IO;
using System.Globalization;
private void ilPanel1_Load(object sender, EventArgs e)
{
fsin = new System.IO.StreamReader("testlong.iln");
while(fsin.EndOfStream == false)
{
datarow = fsin.ReadLine();
// Console.WriteLine(datarow);
rowvalues = datarow.Split(',');
inrows = inrows + 1;
tempX.Add(float.Parse(rowvalues[0], CultureInfo.InvariantCulture));
tempY.Add(float.Parse(rowvalues[1], CultureInfo.InvariantCulture));
tempZ.Add(float.Parse(rowvalues[2], CultureInfo.InvariantCulture));
}
fsin.Close();
// now that I know the input size (number of x,y,z tuples), I can
// create and initialize the ILArray :
ILArray<float> datasamples = ILMath.zeros<float>(3, (int)inrows );
label1.Text = String.Format("Data points read: {0:N0}", inrows);
// ... and now I can copy data from the input arrays:
datasamples["0;:"] = tempX.ToArray();
datasamples["1;:"] = tempY.ToArray();
datasamples["2;:"] = tempZ.ToArray();
// these are no longer used so...
tempX.Clear();
tempY.Clear();
tempZ.Clear();
var scene = new ILScene {
new ILPlotCube(twoDMode: false) {
new ILPoints {
Positions = datasamples,
Color = Color.Blue,
// Colors =
Size = 0.5F
}
}
};
// at the end of all modifications, call Configure()
ilPanel1.Scene = scene;
ilPanel1.Scene.Configure();
ilPanel1.Refresh();
}
最佳答案
使用 ILMath.csvread<T>()
读取 CSV 数据。该函数允许通过通用参数指定整体元素类型:
string s =
@"2014-03-15 18:00:00, 19700000.0, -30.19
2014-03-15 18:00:00, 19700781.25, -31.19
2014-03-15 18:00:00, 19701562.5, -30.43
2014-03-15 18:00:00, 19702343.75, -30.37
2014-03-15 18:00:00, 19703125.0, -27.06";
ILArray<double> A = ILMath.csvread<double>(s);
>A
<Double> [5,3]
(:,:) 1e+007 *
0,00000 1,97000 0,00000
0,00000 1,97008 0,00000
0,00000 1,97016 0,00000
0,00000 1,97023 0,00000
0,00000 1,97031 0,00000
目前没有单独的列格式规范。因此,为了包含日期本身 (DateTime),您必须创建自己的数据类型 - 正如您所指出的。
然而,这引入了其他问题:
1) csvread() 期望来自 csv 文件的同类元素。而ILArray<datasample>
很好(而且有用),csvread 不直接支持。
2)如果您的目标是绘制数据,则无论如何您都必须将日期时间转换为某个实数。 Drawing 命名空间仅需要 ILArray。
3) 此外,大多数 ILMath 函数专门用于某些真实 (System.Value) 元素格式的 ILArray。拥有 ILArray 将通过这些函数带来非常有限的支持。
编辑:这个怎么样? :)
private void ilPanel1_Load(object sender, EventArgs e) {
ILArray<float> A = ILMath.tosingle(ILMath.rand(4, 3000));
ilPanel1.Scene.Add(
new ILPlotCube(twoDMode:false) {
new ILPoints() {
Positions = A["0,1,2;:"],
Colors = new ILColormap(Colormaps.Jet).Map(A["2;:"]).T,
Color = null
}
});
}
关于csv - 如何在 ILArray 中加载大 CSV 文件进行 3d 绘图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22506286/