public void UpdateStringAttributeOnDataset() { string fileName = GetFilename("updatestringattributeondataset.h5"); Hdf5File file = Hdf5File.Create(fileName); Hdf5Group group = file.Groups.Add("group"); List <Hdf5DimensionProperty> dimensionProps = new List <Hdf5DimensionProperty>(); Hdf5DimensionProperty prop = new Hdf5DimensionProperty { CurrentSize = 1 }; dimensionProps.Add(prop); Hdf5Dataset dataset = group.Datasets.Add("dataset1", Hdf5DataTypes.Int32, dimensionProps); Hdf5Attribute attribute = dataset.Attributes.Add("attribute1", "test"); attribute.Value = "test2"; dataset.Attributes.Update(attribute); file.Close(); file = new Hdf5File(fileName); group = file.Groups[0]; dataset = group.Datasets[0]; attribute = dataset.Attributes[0]; Assert.AreEqual("test2", attribute.Value); }
public void SetUInt64() { string filename = GetFilename("setuint64.h5"); Hdf5File file = Hdf5File.Create(filename); List <Hdf5DimensionProperty> properties = new List <Hdf5DimensionProperty>(); Hdf5DimensionProperty property = new Hdf5DimensionProperty { CurrentSize = 5 }; properties.Add(property); Hdf5Dataset dataset = file.Datasets.Add("dataset1", Hdf5DataTypes.UInt64, properties); Assert.IsNotNull(dataset); UInt64[] value = { UInt64.MinValue, 1, 2, 3, UInt64.MaxValue }; dataset.SetData(value); var array = dataset.GetData(); Assert.AreEqual(1, array.Rank); Assert.AreEqual(5, array.GetLength(0)); Assert.AreEqual(UInt64.MinValue, array.GetValue(0)); Assert.AreEqual(UInt64.MaxValue, array.GetValue(4)); file.Close(); }
public void SetSingle() { string filename = GetFilename("setsingle.h5"); Hdf5File file = Hdf5File.Create(filename); List <Hdf5DimensionProperty> properties = new List <Hdf5DimensionProperty>(); Hdf5DimensionProperty property = new Hdf5DimensionProperty { CurrentSize = 5 }; properties.Add(property); Hdf5Dataset dataset = file.Datasets.Add("dataset1", Hdf5DataTypes.Single, properties); Assert.IsNotNull(dataset); Single[] value = { Single.MinValue, -1.1f, 0.0f, -1.3f, Single.MaxValue }; dataset.SetData(value); var array = dataset.GetData(); Assert.AreEqual(1, array.Rank); Assert.AreEqual(5, array.GetLength(0)); Assert.AreEqual(Single.MinValue, array.GetValue(0)); Assert.AreEqual(Single.MaxValue, array.GetValue(4)); file.Close(); }
public void SetArrayDimensionMismatch1() { string filename = GetFilename("setarraydimensionmismatch1.h5"); Hdf5File file = Hdf5File.Create(filename); List <Hdf5DimensionProperty> properties = new List <Hdf5DimensionProperty>(); Hdf5DimensionProperty property = new Hdf5DimensionProperty { CurrentSize = 5 }; properties.Add(property); Hdf5Dataset dataset = file.Datasets.Add("dataset1", Hdf5DataTypes.Int32, properties); Assert.IsNotNull(dataset); try { //2 dimensions sent, only one expected Int32[,] value = { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 } }; dataset.SetData(value); Assert.Fail("Exception was expected"); } catch (Exception ex) { Assert.IsInstanceOf <Hdf5ArrayDimensionsMismatchException>(ex); } file.Close(); }
public void OpenDatasetInFile() { string filename = GetFilename("opendatasetinfile.h5"); Hdf5File file = Hdf5File.Create(filename); List <Hdf5DimensionProperty> properties = new List <Hdf5DimensionProperty>(); Hdf5DimensionProperty property = new Hdf5DimensionProperty { CurrentSize = 100 }; properties.Add(property); Hdf5Dataset dataset = file.Datasets.Add("dataset1", Hdf5DataTypes.Int8, properties); Assert.IsNotNull(dataset); Assert.AreEqual(1, file.Datasets.Count); file.Close(); file = new Hdf5File(filename); dataset = file.Datasets[0]; Assert.IsNotNull(dataset); Assert.AreEqual("dataset1", dataset.Name); Assert.AreEqual(Hdf5DataTypes.Int8, dataset.DataType.Type); file.Close(); }
public void SetDataMismatch() { string filename = GetFilename("setdatamismatch.h5"); Hdf5File file = Hdf5File.Create(filename); List <Hdf5DimensionProperty> properties = new List <Hdf5DimensionProperty>(); Hdf5DimensionProperty property = new Hdf5DimensionProperty { CurrentSize = 3 }; properties.Add(property); Hdf5Dataset dataset = file.Datasets.Add("dataset1", Hdf5DataTypes.Int8, properties); Assert.IsNotNull(dataset); try { Int32[] value = { 1, 2, 3 }; dataset.SetData(value); Assert.Fail("Exception was expected"); } catch (Exception ex) { Assert.IsInstanceOf <Hdf5TypeMismatchException>(ex); } file.Close(); }
public void SetDataNull() { string filename = GetFilename("setdatatnull.h5"); Hdf5File file = Hdf5File.Create(filename); List <Hdf5DimensionProperty> properties = new List <Hdf5DimensionProperty>(); Hdf5DimensionProperty property = new Hdf5DimensionProperty { CurrentSize = 100 }; properties.Add(property); Hdf5Dataset dataset = file.Datasets.Add("dataset1", Hdf5DataTypes.Int8, properties); Assert.IsNotNull(dataset); try { sbyte[] value = null; dataset.SetData(value); Assert.Fail("Exception was expected"); } catch (Exception ex) { Assert.IsInstanceOf <ArgumentNullException>(ex); } file.Close(); }
public void CreateAttributeTwiceOnDataset() { string fileName = GetFilename("createattributetwiceondataset.h5"); Hdf5File file = Hdf5File.Create(fileName); Hdf5Group group = file.Groups.Add("test"); List <Hdf5DimensionProperty> dimensionProps = new List <Hdf5DimensionProperty>(); Hdf5DimensionProperty prop = new Hdf5DimensionProperty { CurrentSize = 1 }; dimensionProps.Add(prop); Hdf5Dataset dataset = group.Datasets.Add("dataset1", Hdf5DataTypes.Int32, dimensionProps); dataset.Attributes.Add("attribute1", "test"); try { dataset.Attributes.Add("attribute1", "test2"); Assert.Fail("Should have thrown an exception"); } catch (Exception ex) { file.Close(); Assert.IsInstanceOf <Hdf5AttributeAlreadyExistException>(ex); } }
public void UpdateAttributeWithMismatchOnDataset() { string fileName = GetFilename("updateattributewithmistmachondataset.h5"); Hdf5File file = Hdf5File.Create(fileName); Hdf5Group group = file.Groups.Add("group"); List <Hdf5DimensionProperty> dimensionProps = new List <Hdf5DimensionProperty>(); Hdf5DimensionProperty prop = new Hdf5DimensionProperty { CurrentSize = 1 }; dimensionProps.Add(prop); Hdf5Dataset dataset = group.Datasets.Add("dataset1", Hdf5DataTypes.Int32, dimensionProps); Hdf5Attribute attribute = dataset.Attributes.Add("attribute1", "test"); try { attribute.Value = 5; dataset.Attributes.Update(attribute); Assert.Fail("Should have thrown an exception"); } catch (Exception ex) { file.Close(); Assert.IsInstanceOf <Hdf5TypeMismatchException>(ex); } }
public static Hdf5Dataset CreateDataset( Hdf5Identifier _fileId, Hdf5Path _parentPath, string _name, Hdf5DataTypes _datatype, int _numberOfDimensions, List <Hdf5DimensionProperty> _properties) { Hdf5Path path = _parentPath.Append(_name); UInt64[] dimensionSize = new UInt64[_numberOfDimensions]; UInt64[] maxSize = null; // new UInt64[_numberOfDimensions]; int i = 0; foreach (var property in _properties) { dimensionSize[i] = property.CurrentSize; //if (property.MaximumSize == UInt64.MaxValue) //{ // maxSize[i] = H5S.UNLIMITED; //} //else //{ // maxSize[i] = property.MaximumSize; //} i++; } Hdf5Identifier dataspaceId = H5S.create_simple(_numberOfDimensions, dimensionSize, maxSize).ToId(); //TODO handle string datasets Hdf5Identifier typeId = H5T.copy(TypeHelper.GetNativeType(_datatype).Value).ToId(); var status = H5T.set_order(typeId.Value, H5T.order_t.LE); Hdf5Identifier datasetId = H5D.create(_fileId.Value, path.FullPath, typeId.Value, dataspaceId.Value).ToId(); Hdf5Dataset dataset = null; if (datasetId.Value > 0) { dataset = new Hdf5Dataset(_fileId, datasetId, path.FullPath) { DataType = TypeHelper.GetDataTypeFromDataset(datasetId), Dataspace = DataspaceHelper.GetDataspace(datasetId) }; H5D.close(datasetId.Value); } H5T.close(typeId.Value); FileHelper.FlushToFile(_fileId); return(dataset); }
public void DeleteAttributeOnDataset() { string fileName = GetFilename("deleteattributeondataset.h5"); Hdf5File file = Hdf5File.Create(fileName); Hdf5Group group = file.Groups.Add("group1"); List <Hdf5DimensionProperty> dimensionProps = new List <Hdf5DimensionProperty>(); Hdf5DimensionProperty prop = new Hdf5DimensionProperty { CurrentSize = 1 }; dimensionProps.Add(prop); Hdf5Dataset dataset = group.Datasets.Add("dataset1", Hdf5DataTypes.Int32, dimensionProps); dataset.Attributes.Add("attribute1", "test"); dataset.Attributes.Add("attribute2", 5); Assert.AreEqual(2, dataset.Attributes.Count); file.Close(); file = new Hdf5File(fileName); group = file.Groups[0]; dataset = group.Datasets[0]; var attibutes = dataset.Attributes; Assert.AreEqual(2, attibutes.Count); var attribute1 = attibutes[0]; Assert.AreEqual("attribute1", attribute1.Name); Assert.AreEqual("test", attribute1.Value); var attribute2 = attibutes[1]; Assert.AreEqual("attribute2", attribute2.Name); Assert.AreEqual(5, attribute2.Value); dataset.Attributes.Delete(attribute1); Assert.AreEqual(1, attibutes.Count); attribute2 = attibutes[0]; Assert.AreEqual("attribute2", attribute2.Name); Assert.AreEqual(5, attribute2.Value); file.Close(); file = new Hdf5File(fileName); group = file.Groups[0]; dataset = group.Datasets[0]; attibutes = dataset.Attributes; Assert.AreEqual(1, attibutes.Count); attribute2 = attibutes[0]; Assert.AreEqual("attribute2", attribute2.Name); Assert.AreEqual(5, attribute2.Value); }
private static Array SingleDimension(Hdf5Identifier _datasetIdentifer, Hdf5Dataset _dataset) { if (_dataset.DataType.Type == Hdf5DataTypes.Int8) { return(Read1DArray <sbyte>(_datasetIdentifer, _dataset)); } if (_dataset.DataType.Type == Hdf5DataTypes.Int16) { return(Read1DArray <Int16>(_datasetIdentifer, _dataset)); } if (_dataset.DataType.Type == Hdf5DataTypes.Int32) { return(Read1DArray <Int32>(_datasetIdentifer, _dataset)); } if (_dataset.DataType.Type == Hdf5DataTypes.Int64) { return(Read1DArray <Int64>(_datasetIdentifer, _dataset)); } if (_dataset.DataType.Type == Hdf5DataTypes.UInt8) { return(Read1DArray <byte>(_datasetIdentifer, _dataset)); } if (_dataset.DataType.Type == Hdf5DataTypes.UInt16) { return(Read1DArray <UInt16>(_datasetIdentifer, _dataset)); } if (_dataset.DataType.Type == Hdf5DataTypes.UInt32) { return(Read1DArray <UInt32>(_datasetIdentifer, _dataset)); } if (_dataset.DataType.Type == Hdf5DataTypes.UInt64) { return(Read1DArray <UInt64>(_datasetIdentifer, _dataset)); } if (_dataset.DataType.Type == Hdf5DataTypes.Single) { return(Read1DArray <Single>(_datasetIdentifer, _dataset)); } if (_dataset.DataType.Type == Hdf5DataTypes.Double) { return(Read1DArray <Double>(_datasetIdentifer, _dataset)); } return(null); }
/// <summary> /// Assumes that dataset already open /// </summary> /// <param name="_fileId"></param> /// <param name="_datasetId"></param> /// <param name="_fullPath"></param> /// <returns></returns> public static Hdf5Dataset LoadDataset(Hdf5Identifier _fileId, Hdf5Identifier _datasetId, string _fullPath) { Hdf5DataType datatype = TypeHelper.GetDataTypeFromDataset(_datasetId); Hdf5Dataspace dataspace = DataspaceHelper.GetDataspace(_datasetId); Hdf5Dataset dataset = new Hdf5Dataset(_fileId, _datasetId, _fullPath) { Dataspace = dataspace, DataType = datatype }; return(dataset); }
/// <summary> /// Assumes dataset is already open /// </summary> /// <typeparam name="T"></typeparam> /// <param name="_datasetIdentifer"></param> /// <param name="_dataset"></param> /// <returns></returns> public static T[] Read1DArray <T>(Hdf5Identifier _datasetIdentifer, Hdf5Dataset _dataset) { T[] dataArray = new T[_dataset.Dataspace.DimensionProperties[0].CurrentSize]; GCHandle arrayHandle = GCHandle.Alloc(dataArray, GCHandleType.Pinned); var dataType = H5T.copy(_dataset.DataType.NativeType.Value).ToId(); int result = H5D.read( _datasetIdentifer.Value, dataType.Value, H5S.ALL, H5S.ALL, H5P.DEFAULT, arrayHandle.AddrOfPinnedObject()); arrayHandle.Free(); H5T.close(dataType.Value); return(dataArray); }
public void Set2DuInt64() { string filename = GetFilename("set2duint64.h5"); Hdf5File file = Hdf5File.Create(filename); List <Hdf5DimensionProperty> properties = new List <Hdf5DimensionProperty>(); Hdf5DimensionProperty property1 = new Hdf5DimensionProperty { CurrentSize = 2 }; properties.Add(property1); Hdf5DimensionProperty property2 = new Hdf5DimensionProperty { CurrentSize = 3 }; properties.Add(property2); Hdf5Dataset dataset = file.Datasets.Add("dataset1", Hdf5DataTypes.UInt64, properties); Assert.IsNotNull(dataset); UInt64[,] value = { { UInt64.MinValue, 0, UInt64.MaxValue }, { UInt64.MaxValue, 0, UInt64.MinValue } }; dataset.SetData(value); var array = dataset.GetData(); Assert.AreEqual(2, array.Rank); Assert.AreEqual(2, array.GetLength(0)); Assert.AreEqual(3, array.GetLength(1)); Assert.AreEqual(UInt64.MinValue, array.GetValue(0, 0)); Assert.AreEqual(0, array.GetValue(0, 1)); Assert.AreEqual(UInt64.MaxValue, array.GetValue(0, 2)); Assert.AreEqual(UInt64.MaxValue, array.GetValue(1, 0)); Assert.AreEqual(0, array.GetValue(1, 1)); Assert.AreEqual(UInt64.MinValue, array.GetValue(1, 2)); file.Close(); }
public void CreateAttributeOnDataset() { string fileName = GetFilename("createattributeondataset.h5"); Hdf5File file = Hdf5File.Create(fileName); Hdf5Group group = file.Groups.Add("group1"); List <Hdf5DimensionProperty> dimensionProps = new List <Hdf5DimensionProperty>(); Hdf5DimensionProperty prop = new Hdf5DimensionProperty { CurrentSize = 1 }; dimensionProps.Add(prop); Hdf5Dataset dataset = group.Datasets.Add("dataset1", Hdf5DataTypes.Int32, dimensionProps); dataset.Attributes.Add("attribute1", "test"); dataset.Attributes.Add("attribute2", 5); Assert.AreEqual(2, dataset.Attributes.Count); file.Close(); }
public static void Write2DArray <T>(Hdf5Dataset _dataset, T[,] _array) { if (_dataset.Dataspace.NumberOfDimensions != 2) { throw new Hdf5ArrayDimensionsMismatchException(); } if ((ulong)_array.GetLength(0) != _dataset.Dataspace.DimensionProperties[0].CurrentSize) { throw new Hdf5ArraySizeMismatchException(); } if ((ulong)_array.GetLength(1) != _dataset.Dataspace.DimensionProperties[1].CurrentSize) { throw new Hdf5ArraySizeMismatchException(); } var datasetId = H5O.open(_dataset.FileId.Value, _dataset.Path.FullPath).ToId(); GCHandle arrayHandle = GCHandle.Alloc(_array, GCHandleType.Pinned); var typeId = H5T.copy(_dataset.DataType.NativeType.Value).ToId(); int result = H5D.write( datasetId.Value, typeId.Value, H5S.ALL, H5S.ALL, H5P.DEFAULT, arrayHandle.AddrOfPinnedObject()); arrayHandle.Free(); H5T.close(typeId.Value); H5O.close(datasetId.Value); FileHelper.FlushToFile(_dataset.FileId); }
public void CreateDatasetInGroup() { string filename = GetFilename("createdatasetingroup.h5"); Hdf5File file = Hdf5File.Create(filename); Hdf5Group group = file.Groups.Add("group1"); List <Hdf5DimensionProperty> properties = new List <Hdf5DimensionProperty>(); Hdf5DimensionProperty property = new Hdf5DimensionProperty { CurrentSize = 100 }; properties.Add(property); Hdf5Dataset dataset = group.Datasets.Add("dataset1", Hdf5DataTypes.Int8, properties); Assert.IsNotNull(dataset); Assert.AreEqual(0, file.Datasets.Count); Assert.AreEqual(1, group.Datasets.Count); file.Close(); }
public void Set2DArraySizeMismatch2() { string filename = GetFilename("set2darraysizemismatch2.h5"); Hdf5File file = Hdf5File.Create(filename); List <Hdf5DimensionProperty> properties = new List <Hdf5DimensionProperty>(); Hdf5DimensionProperty property1 = new Hdf5DimensionProperty { CurrentSize = 3 }; properties.Add(property1); Hdf5DimensionProperty property2 = new Hdf5DimensionProperty { CurrentSize = 5 }; properties.Add(property2); Hdf5Dataset dataset = file.Datasets.Add("dataset1", Hdf5DataTypes.Int32, properties); Assert.IsNotNull(dataset); try { //Only 3x4 Int32[,] value = { { 1, 2, 3 }, { 4, 5, 6, }, { 7, 8, 9, }, { 10, 11, 12, } }; dataset.SetData(value); Assert.Fail("Exception was expected"); } catch (Exception ex) { Assert.IsInstanceOf <Hdf5ArraySizeMismatchException>(ex); } file.Close(); }
public static Hdf5Dataset CreateDatasetAddToDatasets( ReadonlyNamedItemList <Hdf5Dataset> _datasets, Hdf5Identifier _fileId, Hdf5Path _parentPath, string _name, Hdf5DataTypes _datatype, int _numberOfDimensions, List <Hdf5DimensionProperty> _properties) { Hdf5Dataset dataset = CreateDataset( _fileId, _parentPath, _name, _datatype, _numberOfDimensions, _properties); if (dataset != null) { _datasets.Add(dataset); } return(dataset); }
public static Array GetData(Hdf5Dataset _dataset) { var id = H5O.open(_dataset.FileId.Value, _dataset.Path.FullPath).ToId(); if (id.Value > 0) { Array a = null; if (_dataset.Dataspace.NumberOfDimensions == 1) { a = SingleDimension(id, _dataset); } else if (_dataset.Dataspace.NumberOfDimensions == 2) { a = TwoDimension(id, _dataset); } H5O.close(id.Value); return(a); } return(null); }
public static Hdf5Dataset CreateDataset( Hdf5Identifier _fileId, Hdf5Path _parentPath, string _name, Hdf5DataTypes _datatype, int _numberOfDimensions, List <Hdf5DimensionProperty> _properties, Hdf5CompressionProperty _compressionProperty = null) { Hdf5Path path = _parentPath.Append(_name); UInt64[] dimensionSize = new UInt64[_numberOfDimensions]; UInt64[] maxSize = null; // new UInt64[_numberOfDimensions]; if (_numberOfDimensions != _properties.Count || (_compressionProperty != null && _numberOfDimensions != _compressionProperty.ChunkDimensions.Length)) { throw new Hdf5ArrayDimensionsMismatchException(); } int i = 0; foreach (var property in _properties) { dimensionSize[i] = property.CurrentSize; if (_compressionProperty != null && _compressionProperty.ChunkDimensions[i] > property.CurrentSize) { throw new Hdf5ArraySizeMismatchException(); } i++; } Hdf5Identifier dataspaceId = H5S.create_simple(_numberOfDimensions, dimensionSize, maxSize).ToId(); //TODO handle string datasets Hdf5Identifier typeId = H5T.copy(TypeHelper.GetNativeType(_datatype).Value).ToId(); var status = H5T.set_order(typeId.Value, H5T.order_t.LE); var plist_id = _compressionProperty != null?H5P.create(H5P.DATASET_CREATE) : 0; if (plist_id != 0) { H5P.set_chunk(plist_id, _compressionProperty.ChunkDimensions.Length, _compressionProperty.ChunkDimensions); H5P.set_deflate(plist_id, _compressionProperty.CompressionLevel); } Hdf5Identifier datasetId = H5D.create(_fileId.Value, path.FullPath, typeId.Value, dataspaceId.Value, dcpl_id: plist_id).ToId(); Hdf5Dataset dataset = null; if (datasetId.Value > 0) { dataset = new Hdf5Dataset(_fileId, datasetId, path.FullPath) { DataType = TypeHelper.GetDataTypeFromDataset(datasetId), Dataspace = DataspaceHelper.GetDataspace(datasetId) }; H5D.close(datasetId.Value); } H5T.close(typeId.Value); if (plist_id != 0) { H5P.close(plist_id); } FileHelper.FlushToFile(_fileId); return(dataset); }