<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The ILNumerics Blog &#187; HDF5</title>
	<atom:link href="https://ilnumerics.net/blog/tag/hdf5/feed/" rel="self" type="application/rss+xml" />
	<link>https://ilnumerics.net/blog</link>
	<description>The Productivity Machine  &#124;  A fresh attempt for scientific computing  &#124;  http://ilnumerics.net</description>
	<lastBuildDate>Thu, 05 Dec 2024 09:09:24 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.41</generator>
	<item>
		<title>HDF5 and Matlab Files &#8211; Fun with ILNumerics</title>
		<link>https://ilnumerics.net/blog/hdf5-and-matlab-files-fun-with-ilnumerics/</link>
		<comments>https://ilnumerics.net/blog/hdf5-and-matlab-files-fun-with-ilnumerics/#comments</comments>
		<pubDate>Thu, 19 Mar 2015 08:12:25 +0000</pubDate>
		<dc:creator><![CDATA[Balázs]]></dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[ILNumerics]]></category>
		<category><![CDATA[Matlab]]></category>
		<category><![CDATA[array visualizer]]></category>
		<category><![CDATA[conversion]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[h5dataset]]></category>
		<category><![CDATA[h5file]]></category>
		<category><![CDATA[HDF5]]></category>
		<category><![CDATA[loading]]></category>
		<category><![CDATA[mat]]></category>

		<guid isPermaLink="false">http://ilnumerics.net/blog/?p=790</guid>
		<description><![CDATA[<p>Why to use HDF5 and ILNumerics? HDF5 is a file format (Hierarchical Data Format) especially desgined to handle huge amount of numerical data. Just to mention an example,  NASA chose it to be the standard file format for storing data from the Earth Observing System (EOS). ILNumerics easily handles HDF5 files. They can be used &#8230; <a href="https://ilnumerics.net/blog/hdf5-and-matlab-files-fun-with-ilnumerics/" class="more-link">Continue reading <span class="screen-reader-text">HDF5 and Matlab Files &#8211; Fun with ILNumerics</span> <span class="meta-nav">&#8594;</span></a></p>
<p>The post <a rel="nofollow" href="https://ilnumerics.net/blog/hdf5-and-matlab-files-fun-with-ilnumerics/">HDF5 and Matlab Files &#8211; Fun with ILNumerics</a> appeared first on <a rel="nofollow" href="https://ilnumerics.net/blog">The ILNumerics Blog</a>.</p>
]]></description>
				<content:encoded><![CDATA[<h3>Why to use HDF5 and ILNumerics?</h3>
<p style="text-align: justify;">HDF5 is a file format (Hierarchical Data Format) especially desgined to handle huge amount of numerical data. Just to mention an example,  NASA chose it to be the standard file format for storing data from the Earth Observing System (EOS).</p>
<p style="text-align: justify;">ILNumerics easily handles HDF5 files. They can be used to exchange data with other software tools, for example Matlab mat files. In this post I will show a step by step guide &#8211; how to interface ILNumerics with Matlab.</p>
<p style="text-align: justify;"><span id="more-790"></span></p>
<h3>Save Matlab data to HDF5 format</h3>
<p style="text-align: justify;">Let&#8217;s start by creating some arbitrary data with Matlab and saving it in HDF5 format, Matlab supports it from 2006 September, version 7.3.</p>
<pre class="brush: matlabkey; title: ; notranslate">
matrix = randn(10, 10) * 5;
vector = [1 2 3 4 5]';
carr = 'It is a char array!';
noise = rand(1,100) - rand(1, 100);
sinus = 10 * sin(linspace(0, 2*pi, 100)) + noise;
complex = rand(1, 5) * (10 + 10i);
save('test_hdf5.mat', '-v7.3');
</pre>
<h3>Load Matlab data with ILNumerics</h3>
<p>Let&#8217;s now see what ILNumerics sees <img src="https://ilnumerics.net/blog/wp-includes/images/smilies/icon_smile.gif" alt=":-)" class="wp-smiley" /></p>
<figure id="attachment_791" style="width: 408px;" class="wp-caption alignnone"><a href="http://ilnumerics.net/blog/wp-content/uploads/2015/03/1.png"><img class="wp-image-791 size-full" src="http://ilnumerics.net/blog/wp-content/uploads/2015/03/1.png" alt="ILNumerics HDF5 File peek view" width="408" height="228" /></a><figcaption class="wp-caption-text">Children datasets of H5File</figcaption></figure>
<p style="text-align: justify;">Matlab simply stored all arrays as individual datasets in the root node of the HDF5 file. It is now easily possible to access the data! We could take a peek and inspect them right within the Visual Studio debugger visualizers. But let’s see how the data is loaded programmatically:</p>
<pre class="brush: csharp; title: ; notranslate">
// Reading from Matlab
using (H5File file = new H5File(&quot;test_hdf5.mat&quot;))
{
    using (ILScope.Enter())
    {
        ILArray&lt;double&gt; matrix = file.Get&lt;H5Dataset&gt;(&quot;matrix&quot;).Get&lt;double&gt;();
        ILArray&lt;double&gt; vector = file.Get&lt;H5Dataset&gt;(&quot;vector&quot;).Get&lt;double&gt;();
        ILArray&lt;char&gt; carr = file.Get&lt;H5Dataset&gt;(&quot;carr&quot;).Get&lt;char&gt;();
        ILArray&lt;double&gt; noise = file.Get&lt;H5Dataset&gt;(&quot;noise&quot;).Get&lt;double&gt;();
        ILArray&lt;double&gt; sinus = file.Get&lt;H5Dataset&gt;(&quot;sinus&quot;).Get&lt;double&gt;();
    }
}

</pre>
<p style="text-align: justify;">The datasets are loaded &#8211; and the whole process was very easy. It took me less than ten minutes to figure out the methods. It would have been even less if I had take a look on the <a href="http://ilnumerics.net/examples.php">examples</a> of ILNumerics <img src="https://ilnumerics.net/blog/wp-includes/images/smilies/icon_smile.gif" alt=":-)" class="wp-smiley" /></p>
<p style="text-align: justify;">Now, let&#8217;s startup the <a title="ILNumerics Array Visualizer" href="http://ilnumerics.net/Visual-Studio-Graphical-Debugger.html">Array Visualizer</a>! Just write &#8216;array&#8217; in the Quick Launch box of Visual Studio, or open a new visualizer window via VIEW -&gt; OTHER WINDOWS -&gt; Array Visualizer. In the visualizer expression field enter: &#8216;sinus&#8217;. The array is now visualized, how cool is that <img src="https://ilnumerics.net/blog/wp-includes/images/smilies/icon_smile.gif" alt=":-)" class="wp-smiley" /></p>
<h3><a href="http://ilnumerics.net/blog/wp-content/uploads/2015/03/33.png"><img class="alignnone wp-image-825 size-full" src="http://ilnumerics.net/blog/wp-content/uploads/2015/03/33.png" alt="ILNumerics Array Visualizer - Drawing local array on the fly" width="664" height="662" /></a></h3>
<h3>Visualizing Matlab mat files &#8211; on the fly</h3>
<p style="text-align: justify;">Now we have shown the content of the variable &#8216;sinus&#8217;. Just as well, we could visualize the content of the HDF5 file and have the visualizer follow any changes to it immediately:</p>
<p><a href="http://ilnumerics.net/blog/wp-content/uploads/2015/03/5.png"><img class="alignnone wp-image-826 size-full" src="http://ilnumerics.net/blog/wp-content/uploads/2015/03/5.png" alt="ILNumerics Array Visualizer - Reading HDF5 file" width="639" height="425" /></a></p>
<p style="text-align: justify;">If we multiply it with -1, the expression is immediately evaluated:</p>
<p><a href="http://ilnumerics.net/blog/wp-content/uploads/2015/03/6.png"><img class="alignnone wp-image-827 size-full" src="http://ilnumerics.net/blog/wp-content/uploads/2015/03/6.png" alt="ILNumerics Array Visualizer - Expression evaluation on the fly" width="639" height="425" /></a></p>
<p style="text-align: justify;">And what is even more interesting, if we change the HDF5 file interactively, in the immediate window, the ILNumerics Array Visualizer reflects that on the fly:</p>
<p><a href="http://ilnumerics.net/blog/wp-content/uploads/2015/03/7.png"><img class="alignnone wp-image-829 size-full" src="http://ilnumerics.net/blog/wp-content/uploads/2015/03/7.png" alt="Manipulating HDF5 file content in immediate window" width="607" height="131" /></a> <a href="http://ilnumerics.net/blog/wp-content/uploads/2015/03/8.png"><img class="alignnone wp-image-828 size-full" src="http://ilnumerics.net/blog/wp-content/uploads/2015/03/8.png" alt="ILNumerics Array Visualizer - Reading HDF5 content on the fly" width="639" height="425" /></a></p>
<h3>Loading ILArrays into Matlab</h3>
<p style="text-align: justify;">Now let us check, what happens if we want to go from ILNumerics to Matlab. Saving a noise cosine with the following code:</p>
<pre class="brush: csharp; title: ; notranslate">
// Writing some data to HDF5
// Note: It will not make it a proper mat file, only HDF5!
if (File.Exists(&quot;test_iln.mat&quot;))
    File.Delete(&quot;test_iln.mat&quot;);

using (H5File newFile = new H5File(&quot;test_iln.mat&quot;))
{
    using (ILScope.Enter())
    {
        ILArray&lt;double&gt; noisyCos = ILMath.cos(ILMath.linspace(0, 3 * Math.PI, 1000)) * 10 + ILMath.rand(1, 1000);
        H5Dataset set = new H5Dataset(&quot;noisyCos&quot;, noisyCos);
        newFile.Add(set);
    }
}
</pre>
<p style="text-align: justify;">Since the ILNumerics HDF5 is not a mat file format, the only way to load it (as for now) is with one of the &#8216;h5*&#8217; methods of Matlab. To check the info and load, the following code is used:</p>
<pre class="brush: matlabkey; title: ; notranslate">
h5info('test_iln.mat', '/')
noisyCos = h5read('test_iln.mat', '/noisyCos');
</pre>
<p>In this way, it is possible to load ILArrays into Matlab <img src="https://ilnumerics.net/blog/wp-includes/images/smilies/icon_smile.gif" alt=":-)" class="wp-smiley" /></p>
<h3>Altering Matlab mat files in ILNumerics</h3>
<p style="text-align: justify;">It is possible to alter loaded mat files with some limitations. What I found is that the length of the array can not be changed. Obviously, Matlab mat files are created as datasets without chunks, so the dataspace size cannot be changed after the file was created. However, one can alter the data (without changing the size) :</p>
<pre class="brush: csharp; title: ; notranslate">
// Altering data of mat file (v7.3 format)
using (H5File file = new H5File(&quot;test_hdf5.mat&quot;))
{
    using (ILScope.Enter())
    {
        ILArray&lt;double&gt; sinus = file.Get&lt;H5Dataset&gt;(&quot;sinus&quot;).Get&lt;double&gt;();
        sinus.a = sinus * -1;
        file.Get&lt;H5Dataset&gt;(&quot;sinus&quot;).Set(sinus, &quot;0;:&quot;);
    }
}
</pre>
<p>Let me draw your attention to the use of the Set function here. I first tried to use:</p>
<pre class="brush: csharp; title: ; notranslate">
file.Get&lt;H5Dataset&gt;(&quot;dsname&quot;).Set(A);
</pre>
<p><a href="http://ilnumerics.net/blog/wp-content/uploads/2015/03/4.png"><img class="alignnone wp-image-810 size-full" src="http://ilnumerics.net/blog/wp-content/uploads/2015/03/4.png" alt="HDF5 file altering Matlab data" width="804" height="546" /></a></p>
<p style="text-align: justify;">But this would attempt to overwrite the whole dataset &#8211; something which could potentially change the size, hence would require a chunked dataset. Therefore, I figured that I have to be more specific and need to provide the target data range also:</p>
<pre class="brush: csharp; title: ; notranslate">
file.Get&lt;H5Dataset&gt;(&quot;sinus&quot;).Set(sinus, &quot;0;:&quot;)
</pre>
<h3>More Limitations</h3>
<p>Out of curiosity I checked the complex array, to see if it is okay.</p>
<figure id="attachment_792" style="width: 656px;" class="wp-caption alignnone"><a href="http://ilnumerics.net/blog/wp-content/uploads/2015/03/2.png"><img class="wp-image-792 size-full" src="http://ilnumerics.net/blog/wp-content/uploads/2015/03/2.png" alt="Loading complex from Matlab" width="656" height="208" /></a><figcaption class="wp-caption-text">Loading a complex vector from mat file</figcaption></figure>
<p style="text-align: justify;">Unfortunately it throws a H5Exception, this is something I will look into and work on. Fixing this problem will require support for compound datatypes in our HDF5 API.</p>
<h3>Try out the example</h3>
<p>To play with it, head to the examples and download the code:<br />
<a title="Reading and Writing Matlab data with ILNumerics" href="http://ilnumerics.net/examples.php?exid=80c65dd5478b9726d6313eed9ef7d241" target="_blank">Reading and Writing Matlab data with ILNumerics</a></p>
<h3>Conclusion</h3>
<p style="text-align: justify;">To sum up, it is possible and easy to load data stored in Matlab mat files to ILNumerics with the HDF5 IO handling, and it is also possible to load ILArrays to Matlab as well.</p>
<p style="text-align: justify;">I am opened to suggestions and feedback, looking forward to hearing from you! Share your experiences with us, so we can learn from you!</p>
<p>The post <a rel="nofollow" href="https://ilnumerics.net/blog/hdf5-and-matlab-files-fun-with-ilnumerics/">HDF5 and Matlab Files &#8211; Fun with ILNumerics</a> appeared first on <a rel="nofollow" href="https://ilnumerics.net/blog">The ILNumerics Blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>https://ilnumerics.net/blog/hdf5-and-matlab-files-fun-with-ilnumerics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fun with HDF5, ILNumerics and Excel</title>
		<link>https://ilnumerics.net/blog/fun-with-hdf5-ilnumerics-and-excel/</link>
		<comments>https://ilnumerics.net/blog/fun-with-hdf5-ilnumerics-and-excel/#comments</comments>
		<pubDate>Sat, 29 Nov 2014 16:19:45 +0000</pubDate>
		<dc:creator><![CDATA[haymo]]></dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Features]]></category>
		<category><![CDATA[ILNumerics]]></category>
		<category><![CDATA[Usage]]></category>
		<category><![CDATA[Visualization]]></category>
		<category><![CDATA[Excel]]></category>
		<category><![CDATA[Extension]]></category>
		<category><![CDATA[HDF5]]></category>
		<category><![CDATA[Office]]></category>

		<guid isPermaLink="false">http://ilnumerics.net/blog/?p=663</guid>
		<description><![CDATA[<p>It is amazing how many complex business processes in major industries today are supported by a tool that shines by its simplicity: Microsoft Excel. &#8216;Recently&#8217; (with Visual Studio 2010) Microsoft managed to polish the development tools for all Office applications significantly. The whole Office product line is now ready to serve as a convenient, flexible &#8230; <a href="https://ilnumerics.net/blog/fun-with-hdf5-ilnumerics-and-excel/" class="more-link">Continue reading <span class="screen-reader-text">Fun with HDF5, ILNumerics and Excel</span> <span class="meta-nav">&#8594;</span></a></p>
<p>The post <a rel="nofollow" href="https://ilnumerics.net/blog/fun-with-hdf5-ilnumerics-and-excel/">Fun with HDF5, ILNumerics and Excel</a> appeared first on <a rel="nofollow" href="https://ilnumerics.net/blog">The ILNumerics Blog</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>It is amazing how many complex business processes in major industries today are supported by a tool that shines by its simplicity: Microsoft Excel. &#8216;Recently&#8217; (with Visual Studio 2010) Microsoft managed to polish the development tools for all Office applications significantly. The whole Office product line is now ready to serve as a convenient, flexible base framework for stunning custom business logic, custom computations and visualizations &#8211; with just a little help of tools like <a href="http://ilnumerics.net">ILNumerics</a>.</p>
<p><a href="http://ilnumerics.net/blog/wp-content/uploads/2014/11/ExcelWorkbook1.png"><img class="aligncenter size-full wp-image-664" src="http://ilnumerics.net/blog/wp-content/uploads/2014/11/ExcelWorkbook1.png" alt="ExcelWorkbook1" width="1118" height="594" /></a>In this blog post I am going to show how easy it is to extend the common functionality of Excel. We will enable an Excel Workbook to load arbitrary <a href="http://www.hdfgroup.org/">HDF5</a> data files, inspect the content of such files and show the data as interactive 2D or 3D plots. <span id="more-663"></span>HDF5 is an industry standard for the structured storage of technical data and is maintained by the <a href="http://www.hdfgroup.org/">HDF Group</a>. With version 4.0 <a href="http://ilnumerics.net">ILNumerics </a>supports the HDF5 file format with a very convenient, object oriented API. Visualizations have always been a popular feature since the early days of ILNumerics. And Excel can be another convenient GUI tool to marry both.</p>
<h2>Prerequisites</h2>
<p>Customizations like the one we are going to show are done in Visual Studio and utilize <a href="http://msdn.microsoft.com/en-us/library/jj620922.aspx">Visual Studio Tools for Office (VSTO)</a>. If you don&#8217;t own a copy of Visual Studio, you may find it useful that Microsoft gives away their <a href="http://www.visualstudio.com/en-us/products/free-developer-offers-vs.aspx">flagship for free</a> under certain conditions. Another prerequisite we will need: Office 2010 or 2013. I use Office 2010 on my computer. For some reason, Visual Studio 2013 did not allow me to create a new Workbook project for the 2010 version of Office and rather requested Office 2013. Therefore, I used Visual Studio 2012 instead. Just make sure to use the version of Office which is supported by your Visual Studio installation.</p>
<p>The last ingredient needed is ILNumerics. <a href="http://ilnumerics.net/download.html">Download the free trial</a> &#8211; it contains all features and tools you may possibly want and makes them available systemwide.</p>
<h2>Setup</h2>
<p>Let&#8217;s start with a fresh new Workbook project in Visual Studio:</p>
<p><a href="http://ilnumerics.net/blog/wp-content/uploads/2014/11/NewProject.png"><img class="aligncenter size-full wp-image-666" src="http://ilnumerics.net/blog/wp-content/uploads/2014/11/NewProject.png" alt="NewProject" width="955" height="660" /></a>We take all defaults here. This will create a new Visual Studio project with three worksheets, ready for your customizations. Double click on Sheet1.cs to open the designer view.  In the Visual Studio Toolbox find the ILNumerics section and drag a new instance of ILPanel onto your sheet:</p>
<p><a href="http://ilnumerics.net/blog/wp-content/uploads/2014/11/Workbook1_VisualStudioDesigner.png"><img class="aligncenter size-full wp-image-667" src="http://ilnumerics.net/blog/wp-content/uploads/2014/11/Workbook1_VisualStudioDesigner.png" alt="Workbook1_VisualStudioDesigner" width="1334" height="885" /></a>This will add the reference of ILNumerics to your project and places the ILPanel control on your sheet. You may want to resize and reposition the control. It will be available from now on as &#8216;iLNumerics_Drawing_ILPanel1&#8242; in your code-behind classes. Feel free to adjust the name &#8211; for this demonstration we will leave it as it is.</p>
<p>&nbsp;</p>
<h2>Loading HDF5 Files in Excel</h2>
<p>Excel does not support HDF5 file imports directly (yet?). Luckily, ILNumerics bridges the gap very efficiently and conveniently. First, we add a reference to the HDF5 assembly which comes with ILNumerics. In the last step the reference to ILNumerics was added automagically. For HDF5 we have to do this manually. Right click on the References tab in the solution explorer and chose: Add Reference. Now search for &#8220;HDF5&#8243; on your system, select the item found and hit &#8220;OK&#8221;:</p>
<p><a href="http://ilnumerics.net/blog/wp-content/uploads/2014/11/AddHDF5Reference.png"><img class="aligncenter size-full wp-image-668" src="http://ilnumerics.net/blog/wp-content/uploads/2014/11/AddHDF5Reference.png" alt="AddHDF5Reference" width="776" height="350" /></a>If no item was found, make sure you have installed the latest ILNumerics package using our installer and that you selected the Assemblies tab in the Reference Manager window.</p>
<p>Once we have the HDF5 assembly available, we can start coding. The idea is, that the user should be able to load an HDF5 file from disk, inspect the datasets contained and get the option to load and/or visualize their data. So, let&#8217;s add some more controls from the toolbox to the workbook sheet: OpenFileDialog and a button to trigger the opening. Drag the button (Common Controls in the Toolbox) and the OpenFileDialog (Dialogs tab in the Toolbox) to the designer surface:</p>
<p><a href="http://ilnumerics.net/blog/wp-content/uploads/2014/11/DragButtonOFDDesigner.png"><img class="aligncenter size-full wp-image-669" src="http://ilnumerics.net/blog/wp-content/uploads/2014/11/DragButtonOFDDesigner.png" alt="DragButtonOFDDesigner" width="906" height="560" /></a><br />
Now, rename the new button and double click it to open the auto-generated event handler method. This is where we are going to implement the code to open and inspect the HDF5 file:</p>
<pre class="brush: csharp; title: ; notranslate">
private void button1_Click(object sender, EventArgs e) {
    var fod = new OpenFileDialog();
    if (fod.ShowDialog() == DialogResult.OK) {

        var filename = fod.FileName;
        // access the HDF5 file for reading
        using (var file = new H5File(filename, accessMode: FileAccess.Read)) {
            int row = 4; // start listing at row 4
            // list all datasets in the file
            foreach (var ds in file.Find&lt;H5Dataset&gt;()) {
                Cells[row,   3].Value = ds.Path;
                Cells[row++, 4].Value = ds.Size.ToString();
            }
            while (row &lt; 100) {
                Cells[row, 3].Value = &quot;&quot;;
                Cells[row++, 4].Value = &quot;&quot;;
            }
            // display filename
            Cells[2, 4].Value = filename;
        }
    }
}
</pre>
<p>First, we ask for the filename to open. If the user provided a valid filename we open the file for reading. HDF5 files in ILNumerics are used in &#8216;using&#8217; blocks. No matter how you leave the block, ILNumerics ensures that the file is not left open. Read <a href="http://ilnumerics.net/hdf5-interface.html">here for more details</a>.<br />
Once we have the HDF5 file we start iterating over its datasets. C# foreach constructs make that really easy. Other languages have similar constructs. Inside the foreach loop we simply write out the path of the current dataset and its size to columns of the worksheet.<br />
The while loop afterwards is only needed to clear entries potentially left over from earlier loadings. This expects no more than 100 datasets in a file. In a production code, you will do better&#8230;<br />
Finally, the name of the HDF5 file is written into cell[2,4] for user information.</p>
<p>If we now run the workbook (hit F5) and click on our &#8220;Load HDF5 Dataset&#8221; button a file dialog opens up. Once we select an existing HDF5 file from our disk the file&#8217;s datasets are listed on the worksheet:<br />
<a href="http://ilnumerics.net/blog/wp-content/uploads/2014/11/FirstRun.png"><img class="aligncenter size-full wp-image-671" src="http://ilnumerics.net/blog/wp-content/uploads/2014/11/FirstRun.png" alt="FirstRun" width="464" height="478" /></a></p>
<h2>Loading HDF5 Dataset Content</h2>
<p>Next, register a new event handler for the double click event in the worksheet. The worksheet template offers a good place to do so: the Sheet1_Startup event handler is auto generated by Visual Studio. Add the following line in order to allow to react to double click events on the sheet:</p>
<pre class="brush: csharp; title: ; notranslate">
this.BeforeDoubleClick += Sheet1_BeforeDoubleClick;
</pre>
<p>The implementation of the Sheet1_BeforeDoubleClick method does all the work:</p>
<pre class="brush: csharp; title: ; notranslate">
void Sheet1_BeforeDoubleClick(Excel.Range Target, ref bool Cancel) {
    // only take cells we are interested in
    if (Target.Value == null || Cells[2, 4].Value == null) return;
    // grab the hdf5 filename from the cell
    var curFilename = Cells[2, 4].Value.ToString();
    // check if this points to an existing file
    if (File.Exists(curFilename)) {
        // grab the dataset name (if the user clicked on it)
        var dsName = ((object)Target.Value).ToString();
        // reasonable?
        if (Target.Count == 1 &amp;&amp; !String.IsNullOrEmpty(dsName)) {
            // open the file
            using (var file = new H5File(curFilename, accessMode: FileAccess.Read)) {
                // find the dataset in the file, we provide the full abs. path so we
                // are sure that there is only one such dataset
                var ds = file.First&lt;H5Dataset&gt;(dsName);
                if (ds != null) {
                    // add a new sheet with the name of the dataset
                    var sheet = (Excel.Worksheet)Globals.ThisWorkbook.Sheets.Add();
                    sheet.Name = checkName(dsName);
                    // ... and make it active
                    Globals.ThisWorkbook.Sheets[sheet.Name].Activate();
                    // load data using our extension method (see text)
                    sheet.Set(ds.Get&lt;double&gt;());
                } else {
                    // user has clicked on the size column -&gt; plot the data
                    var size = ParseSize(dsName);
                    if (size != null &amp;&amp; Target.Previous.Value != null) {
                        dsName = ((object)Target.Previous.Value).ToString();
                        // read data and render into panel
                        renderDS(file, dsName);
                    }
                }
            }
        }
    }
}
</pre>
<p>This is all straight forward: we do some very simple error checking here, just to make sure we only react to clicks on interesting columns. In your production code you will do much better error checking! However, here we decide if the user has clicked on a cell with a valid dataset name. If so, the file is opened (fetching the name from the HDF5 filename cell written to earlier), the dataset is located and its content loaded.<br />
The content is written to a new workbook sheet. Attention must be drawn to the naming of the sheet. If a sheet with a similar name exists already, Excel will throw an exception. One easy solution is to add the timestamp to the name, which is left as an exercise. Here, we only do very simple name checking to make sure, no invalid characters enter the name of the sheeet:</p>
<pre class="brush: csharp; title: ; notranslate">
string checkName(string name) {
    var ret = name.Replace('/','_');
    if (ret.Length &gt; 31) {
        ret = ret.Substring(-31);
    }
    return ret;
}
</pre>
<p>The method to actually load the data from an ILArray to the new sheet is found in the following extension method. This is one rather efficient attempt to load large data:</p>
<pre class="brush: csharp; title: ; notranslate">
public static void Set(this Worksheet worksheet, ILInArray&lt;double&gt; A, int fromColumn = 1, int fromRow = 1) {
    using (ILScope.Enter(A)) {

        var luCell = worksheet.Cells[fromRow, fromColumn];
        var rbCell = worksheet.Cells[fromRow + A.S[0] - 1, fromColumn + A.S[1] - 1];
        Range range = worksheet.Range[luCell, rbCell];
        range.Value = A.T.ToSystemMatrix();
    }
}

private static System.Array ToSystemMatrix&lt;T&gt;(this ILDenseArray&lt;T&gt; A) {
    using (ILScope.Enter(A)) {
        // some error checking (to be improved...)
        if (object.Equals(A, null)) throw new ArgumentException(&quot;A may not be null&quot;);
        if (!A.IsMatrix) throw new ArgumentException(&quot;Matrix expected&quot;);

        // create return array
        System.Array ret = Array.CreateInstance(typeof(T), A.S.ToIntArray().Reverse().ToArray());
        // fetch underlying system array
        T[] workArr = A.GetArrayForRead();
        // copy memory block
        Buffer.BlockCopy(workArr, 0, ret, 0, Marshal.SizeOf(typeof(T)) * A.S.NumberOfElements);
        return ret;
    }
}
</pre>
<p>Set() creates the range in the sheet to load the data into. The size is computed by the size of the incoming ILArray&lt;T&gt;. In order to load the data, we do not want to iterate over each<br />
individual cell for performance reasons. One option is to set the Value property of the range to a two dimensional System.Array. ToSystemArray() does exactly that conversion. However, you have to<br />
be careful not to get transposed data unexpectedly. The reason is that .NET multidimensional arrays are stored in row major order. ILNumerics on the other hand stores arrays in the same order as Matlab, FORTRAN and other technical tools do. Hence, we need to transpose our data before we assign them to the range. Read more details <a href="http://stackoverflow.com/a/19636778 /1215993">here</a>.</p>
<p>Now, when we run the application and load a HDF5 file, we can double click on the cell holding a dataset name and have Excel load the dataset contents into a new worksheet &#8211; fast. This can be easily adopted by defining ranges (hyperslabs) and only load partial datasets. Also you can adopt the method described here for writing worksheet contents to HDF5 datasets.</p>
<h2>Visualizing HDF5 Dataset Contents</h2>
<p>Now let&#8217;s add another nice feature to our workbook: instead of simply loading the data from a dataset to the worksheet, we add the option of creating interactive, fully configurable and fast visualizations and plots of the data. We&#8217;ll use the predefined plot classes of ILNumerics Visualization Engine here.</p>
<p>Back to the double click event handler created earlier, we left out the path which is executed once the user clicked on the size displayed next to each dataset. What happens here is also straightforward.</p>
<p>First we parse the size to see, if it gives something reasonable (again, you&#8217;ll add better error checking for a production release). If so, we give the HDF5 file together with the dataset name to the renderDS() method which does the rendering:</p>
<pre class="brush: csharp; title: ; notranslate">
private void renderDS(H5File file, string dsName) {
    var ds = file.First&lt;H5Dataset&gt;(dsName);
    if (ds != null) {
        using (ILScope.Enter()) {
            ILArray&lt;float&gt; A = ds.Get&lt;float&gt;();
            if (A.IsVector) {
                iLNumerics_Drawing_ILPanel1.Scene = new ILScene() {
                    new ILPlotCube(twoDMode: true) {
                        new ILLinePlot(A, markerStyle: MarkerStyle.Diamond, lineWidth: 2)
                    }
                };
            } else if (A.IsMatrix) {
                iLNumerics_Drawing_ILPanel1.Scene = new ILScene() {
                    new ILPlotCube(twoDMode: false) {
                        new ILSurface(A, colormap: Colormaps.Hot) {
                            UseLighting = true
                        }
                    }
                };
            }
            iLNumerics_Drawing_ILPanel1.Refresh();
        }
    }
}
</pre>
<p>This code does not need commenting. It fetches the dataset and loads its content into an ILArray&lt;float&gt;. A new scene replaces the existing one in the worksheet ILPanel. The new scene contains a <a href="http://ilnumerics.net/plot-cube-properties.html">plot cube</a> and a line plot or a <a href="http://ilnumerics.net/surface-plots.html">surface plot</a>. Which one is created depends on the shape of the data. Vector sized data create a lineplot, matrices are rendered as a surface plot. In order to have the new scene show up, we must trigger a refresh on the panel.<br />
<a href="http://ilnumerics.net/blog/wp-content/uploads/2014/11/WorkbooksLines.png"><img class="aligncenter size-full wp-image-673" src="http://ilnumerics.net/blog/wp-content/uploads/2014/11/WorkbooksLines.png" alt="WorkbooksLines" width="877" height="602" /></a>Now, run the workbook, load a HDF5 file having some vector and/or matrix sized datasets, select a dataset by double clicking on its <em>size</em> cell. The plot is created according to the data. Like all visualizations in ILNumerics we can interact with the data: rotation/ zoom /pan is done with the left/right mouse buttons. And of course, you are free to apply any of the very flexible configuration options in order to <a href="http://ilnumerics.net/visualization-engine.html">customize your plots</a> further. For line plots this includes: <a href="http://ilnumerics.net/line-plots.html#markers">markers</a>, <a href="http://ilnumerics.net/line-plots.html#customizing-line-properties">dash styles</a>, <a href="http://ilnumerics.net/labels.html">tex labeling</a>, advanced coloring, <a href="http://ilnumerics.net/axis-configuration.html#logarithmic-scales">logarithmic axes </a>and much <a href="http://ilnumerics.net/plotting-api.html">more</a> &#8230;</p>
<h2>Deploying ILNumerics Excel Workbooks</h2>
<p>Once the workbook is ready for distribution, we build a Release version from Visual Studio and from now on – do not need Visual Studio anymore! In order to make any machine ready for handling HDF5 data and create Excel worksheets with nice visualizations on it, one only needs to run the <a href="http://ilnumerics.net/download.html">ILNumerics installer</a> and transfer the workbook to that machine. It will run on any platform, regardsless if 32 or 64 bit!</p>
<p>You can find the full example for downloading on our <a href="http://ilnumerics.net/examples.php">examples page</a>.</p>
<p>The post <a rel="nofollow" href="https://ilnumerics.net/blog/fun-with-hdf5-ilnumerics-and-excel/">Fun with HDF5, ILNumerics and Excel</a> appeared first on <a rel="nofollow" href="https://ilnumerics.net/blog">The ILNumerics Blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>https://ilnumerics.net/blog/fun-with-hdf5-ilnumerics-and-excel/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Putting on a Good Show with HDF5, ILNumerics, and PowerShell</title>
		<link>https://ilnumerics.net/blog/putting-on-a-good-show-with-hdf5-ilnumerics-and-powershell/</link>
		<comments>https://ilnumerics.net/blog/putting-on-a-good-show-with-hdf5-ilnumerics-and-powershell/#comments</comments>
		<pubDate>Fri, 14 Sep 2012 18:09:33 +0000</pubDate>
		<dc:creator><![CDATA[haymo]]></dc:creator>
				<category><![CDATA[Usage]]></category>
		<category><![CDATA[Getting Started]]></category>
		<category><![CDATA[HDF]]></category>
		<category><![CDATA[HDF5]]></category>
		<category><![CDATA[ILNumerics]]></category>
		<category><![CDATA[Memory Management]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[PSH5X]]></category>
		<category><![CDATA[TypeAccelerator]]></category>

		<guid isPermaLink="false">http://ilnumerics.net/blog/?p=219</guid>
		<description><![CDATA[<p>It is certainly nice to have the option to do all kinds of numeric stuff right in your .NET application layer &#8211; without the need for interfacing any unmanaged module. But for some tasks, this still seems overkill. Lets say, you went to that conference and want to give your new friends some insight into &#8230; <a href="https://ilnumerics.net/blog/putting-on-a-good-show-with-hdf5-ilnumerics-and-powershell/" class="more-link">Continue reading <span class="screen-reader-text">Putting on a Good Show with HDF5, ILNumerics, and PowerShell</span> <span class="meta-nav">&#8594;</span></a></p>
<p>The post <a rel="nofollow" href="https://ilnumerics.net/blog/putting-on-a-good-show-with-hdf5-ilnumerics-and-powershell/">Putting on a Good Show with HDF5, ILNumerics, and PowerShell</a> appeared first on <a rel="nofollow" href="https://ilnumerics.net/blog">The ILNumerics Blog</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>It is certainly nice to have the option to do all kinds of numeric stuff right in your .NET application layer &#8211; without the need for interfacing any unmanaged module. But for some tasks, this still seems overkill.</p>
<p>Lets say, you went to that conference and want to give your new friends some insight into your brand new simulation results. The PC in the internet cafe enables you to fetch the data from your NAT storage at home. But will you be able to do <em>anything </em> with it on that <a href="http://1.bp.blogspot.com/-jURyoGkPgs0/T5Ey3J7WECI/AAAAAAAACEU/7JQGp17DyIs/s1600/PC+Express+iCafe+Solutions+Center+-+PC+Express+Shaw+Blvd.JPG" target="_blank">plain Windows PC</a>?</p>
<p>Or you want to localize a certain test data set but cannot remember its rather cryptic name. Or you might want to manage the latest measurement results from todays <a href="http://eosweb.larc.nasa.gov/">atmospheric observation satellite scans</a>. The data are huge but often require some sort of preprocessing. There should be some easy way to filter them by the meta data within the files, right?</p>
<p>Other than getting the data from some application layer, we now want to interface plain old file objects. Of course, you store your data in HDF5 format, right? You do so, because HDF5 is portable, very efficient, flexible and you are in <a href="http://www.hdfgroup.org/HDF5/users5.html" target="_blank">good company</a>.</p>
<p>Let&#8217;s see. We have a fresh Windows PC and we know every Windows installation nowadays comes with <a href="http://en.wikipedia.org/wiki/Windows_PowerShell">Powershell</a>. Powershell itself is based on the .NET framework and hence efficiently handles any .NET assembly. It should be easy to use ILNumerics with Powershell! All we still need is some way to access the HDF5 files. ILNumerics, natively is able to read and write Matlab mat files up to version 6. It currently lags on native HDF5 support.</p>
<p>Luckily, the <a title="http://www.hdfgroup.org" href="http://www.hdfgroup.org">HDF Group</a> provides a large collection of high quality <a title="HDF5 Projects" href="http://www.hdfgroup.org/projects/" target="_blank">tools for HDF support</a>. Among them you&#8217;ll find a .NET wrapper and &#8230; a brand new Powershell module: <a title="PSH5X - A WINDOWS POWERSHELL MODULE FOR HDF5" href="http://www.hdfgroup.org/projects/PSH5X/">PSH5X</a>! Together with Gerd Heber, the leading inventor of PSH5X, we did a feasibility study with the goal to investigate the options of utilizing HDF5 and ILNumerics together in Powershell. It can be <a href="http://ilnumerics.net/blog/wp-content/uploads/2012/09/HDF5-ILNumerics-PowerShell.a4.pdf" target="_blank">downloaded here</a>. We were quite impressed by the options this brings.</p>
<p>This blog post will describe the necessary steps to setup Powershell for ILNumerics and HDF5.</p>
<h1>Getting Started</h1>
<p>Basically, the installation process for any Powershell module consists of</p>
<ol>
<li>Getting the module files and its dependencies from somewhere,</li>
<li>Deploying the module files into a special folder on your machine, and</li>
<li>Importing the module in your session.</li>
</ol>
<p>The <a title="http://www.hdfgroup.org/projects/PSH5X/" href="http://www.hdfgroup.org/projects/PSH5X/">PSH5X</a> homepage gives all information on how to get ready using the HDF5 Powershell module. Just download the package and follow the three steps on the page. At the end, HDF5 signals you a successful installation by displaying its version numbers.</p>
<p>Since ILNumerics depends on several other modules, we provide a small bootstrapper script. Just open up your favorite Powershell IDE (PowerShell_ISE.exe comes with any recent Windows) and copy/paste the following line:</p>
<pre class="brush: csharp; title: ; notranslate">(new-object Net.WebClient).DownloadString('http://ilnumerics.net/media/InstallILNumericsPSM.ps1') | iex</pre>
<p>If you are curious, what this does &#8211; just ommit the trailing <code>| iex</code> and the script is not executed but displayed for your inspection.</p>
<p>The installer will ask for the installation folder (global under System32/ or local in your user profile), fetches the latest ILNumerics package for the current platform from the official <a title="nuget" href="http://nuget.org">nuget</a> repository and install it into the selected module folder. In addition it loads the <a title="Powershell Type Accelerators" href="http://pstx.codeplex.com/">TypeAccelerator Powershell module</a> and installs it into the same module directory. Note, the accelerators have been slightly modified in order to make them work with <a href="http://www.microsoft.com/en-us/download/details.aspx?id=34595">Powershell 3</a> and hence are fetched from our ILNumerics server. However, credits fully belong to <a href="http://poshoholic.com/">poshoholic</a> for his great work.</p>
<p>Note, the installation has to be done only once. Afterwards, on the next Powershell session, simply re-import needed modules by typing &#8211; lets say:</p>
<pre class="brush: csharp; title: ; notranslate">PS&gt; Import-Module ILNumerics </pre>
<h1>Go!</h1>
<p>If everything was setup correctly, we can now use the full spectrum of the involved modules:</p>
<pre class="brush: csharp; title: ; notranslate">PS&gt; [ilmath]::rand(4,5).ToString()
&lt;Double&gt; [5,4]
   0,72918    0,87547    0,43167    0,94942
   0,58024    0,75562    0,96125    0,83148
   0,22454    0,20583    0,82285    0,83144
   0,13300    0,40047    0,58829    0,87012
   0,50751    0,05496    0,02814    0,48764 </pre>
<p>Nice. But what about the MKL? Are the correct binaries really installed as well?</p>
<pre class="brush: csharp; title: ; notranslate">PS&gt; [ilf64] $A = [ilmath]::rand(1000,1000)
PS&gt; Measure-Command { [ilf64]$C = [ilmath]::rank($A) }
Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 920
Ticks             : 9202311
TotalDays         : 1,06508229166667E-05
TotalHours        : 0,00025561975
TotalMinutes      : 0,015337185
TotalSeconds      : 0,9202311
TotalMilliseconds : 920,2311

PS&gt; $C.ToString()
1000</pre>
<p>We have almost all options from C#:</p>
<pre class="brush: csharp; title: ; notranslate">PS&gt; [ilf64] $part = $A['10:15;993:end']
PS&gt; $part.ToString()
&lt;Double&gt; [11,7]
   0,08522    0,87217    0,59997    0,57363    0,22956    0,02006    0,02359
   0,33479    0,49003    0,65269    0,97772    0,28322    0,69505    0,70372
   0,30072    0,68705    0,47112    0,68627    0,65030    0,40454    0,63026
   0,15639    0,30391    0,22992    0,69310    0,65716    0,51797    0,68110
   0,72854    0,60188    0,50740    0,74499    0,13459    0,88481    0,12445
   0,80525    0,60180    0,69256    0,74825    0,64388    0,16792    0,45266 </pre>
<p>Lets sort the first row of $part, keeping track of original positions:</p>
<pre class="brush: csharp; title: ; notranslate">PS&gt; [ilf64] $indices = 0.0
PS&gt; [ilf64] $sorted = [ilmath]::sort($part['0,1;:'],$indices,0,$false)
PS&gt; $sorted.ToString()
&lt;Double&gt; [2,7]
   0,02006    0,02359    0,08522    0,22956    0,57363    0,59997    0,87217
   0,28322    0,33479    0,49003    0,65269    0,69505    0,70372    0,97772
PS&gt; $indices.ToString()
&lt;Double&gt; [2,7]
         5          6          0          4          3          2          1
         4          0          1          2          5          6          3 </pre>
<p>This is all interactive. Of course, we can write complete functions and even complex algorithms that way.<br />
One of the best things: Even in Powershell ILNumerics saves your memory and meets all expectations regarding execution speed. Powershell allows you to consequently use ILNumerics&#8217; <a title="ILNumerics General Rules " href="http://ilnumerics.net/Support_Documentation$GeneralRules.html">typing and scoping rules</a>.</p>
<p>In our feasibility study with Gerd Heber, we show how easy it gets to access an HDF5 file, to convert its data to ILNumerics arrays (implicitly), filter and manipulate a little and even create a full interactive 3D surface graph from it. We demonstrate how to use the type accelerators and to mimic the <code>using</code> statement for artificial scoping. <a href="http://ilnumerics.net/blog/wp-content/uploads/2012/09/HDF5-ILNumerics-PowerShell.a4.pdf" target="_blank">Take a look</a> and let us know, what you think!</p>
<p>The post <a rel="nofollow" href="https://ilnumerics.net/blog/putting-on-a-good-show-with-hdf5-ilnumerics-and-powershell/">Putting on a Good Show with HDF5, ILNumerics, and PowerShell</a> appeared first on <a rel="nofollow" href="https://ilnumerics.net/blog">The ILNumerics Blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>https://ilnumerics.net/blog/putting-on-a-good-show-with-hdf5-ilnumerics-and-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
