<?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; conversion</title>
	<atom:link href="https://ilnumerics.net/blog/tag/conversion/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>Uncommon data conversion with ILArray</title>
		<link>https://ilnumerics.net/blog/uncommon-data-conversion-with-ilarray/</link>
		<comments>https://ilnumerics.net/blog/uncommon-data-conversion-with-ilarray/#comments</comments>
		<pubDate>Tue, 07 Oct 2014 08:42:14 +0000</pubDate>
		<dc:creator><![CDATA[haymo]]></dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[ILNumerics]]></category>
		<category><![CDATA[Usage]]></category>
		<category><![CDATA[conversion]]></category>
		<category><![CDATA[float]]></category>
		<category><![CDATA[ILArray]]></category>
		<category><![CDATA[pointer arithmetic]]></category>
		<category><![CDATA[unsafe]]></category>
		<category><![CDATA[ushort]]></category>

		<guid isPermaLink="false">http://ilnumerics.net/blog/?p=635</guid>
		<description><![CDATA[<p>ILNumerics Computing Engine supports the most common numeric data types out of the box: double, float, complex, fcomplex, byte, short, int, long, ulong If you need to convert from, let&#8217;s say ushort to float, you will not find any prepared conversion function in ILMath. Luckily, it is very easy to write your own: Here comes &#8230; <a href="https://ilnumerics.net/blog/uncommon-data-conversion-with-ilarray/" class="more-link">Continue reading <span class="screen-reader-text">Uncommon data conversion with ILArray</span> <span class="meta-nav">&#8594;</span></a></p>
<p>The post <a rel="nofollow" href="https://ilnumerics.net/blog/uncommon-data-conversion-with-ilarray/">Uncommon data conversion with ILArray</a> appeared first on <a rel="nofollow" href="https://ilnumerics.net/blog">The ILNumerics Blog</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>ILNumerics Computing Engine supports the most common numeric data types out of the box: double, float, complex, fcomplex, byte, short, int, long, ulong</p>
<p>If you need to convert from, let&#8217;s say ushort to float, you will not find any prepared conversion function in ILMath. Luckily, it is very easy to write your own:</p>
<p>Here comes a method which implements the conversion from ushort -&gt; float. A straight forward version first:</p>
<pre class="brush: csharp; title: ; notranslate">
        /// &lt;summary&gt;
        /// Convert ushort data to ILArray&amp;lt;float&gt;
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;A&quot;&gt;Input Array&lt;/param&gt;
        /// &lt;returns&gt;Array of the same size as A, single precision float elements&lt;/returns&gt;
        public static ILRetArray&lt;float&gt; UShort2Single(ILInArray&lt;ushort&gt; A) {
            using (ILScope.Enter(A)) {
                ILArray&lt;float&gt; ret = ILMath.zeros&lt;float&gt;(A.S);
                var retArr = ret.GetArrayForWrite();
                var AArr = A.GetArrayForRead();
                int c = 0;
                foreach (ushort a in A) {
                    retArr[c++] = a;
                }
                return ret;
            }
        }</pre>
<p><span id="more-635"></span>This method is used like that:</p>
<pre class="brush: csharp; title: ; notranslate">
            ushort[,] rawSensorData = new ushort[,] {{0,1,2},{3,4,5}};
            ILArray&lt;float&gt; converted = UShort2Single(rawSensorData);
            /*
             * &lt;Single&gt; [3,2]
             * [0]:          0          3
             * [1]:          1          4
             * [2]:          2          5
             */

            // continue working with 'converted' here...
</pre>
<p>The following method does the same but utilizes pointer arithmetic, hence it needs the /unsafe flag. Use this, if performance is critical and your data are sufficiently large:</p>
<pre class="brush: csharp; title: ; notranslate">
        /// &lt;summary&gt;
        /// Convert ushort data to ILArray&amp;lt;float&gt; (unsafe version)
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;A&quot;&gt;Input Array&lt;/param&gt;
        /// &lt;returns&gt;Array of the same size as A, single precision float elements&lt;/returns&gt;
        public unsafe static ILRetArray&lt;float&gt; UShort2SingleUnsafe(ILInArray&lt;ushort&gt; A) {
            using (ILScope.Enter(A)) {
                ILArray&lt;float&gt; ret = ILMath.zeros&lt;float&gt;(A.S);
                var retArr = ret.GetArrayForWrite();
                var AArr = A.GetArrayForRead();

                fixed (ushort* pAArr = AArr)
                fixed (float* pRetArr = retArr) {
                    ushort* pInWalk = pAArr;
                    ushort* pInEnd = pAArr + A.S.NumberOfElements;
                    float* pRetWalk = pRetArr;
                    while (pInWalk &lt; pInEnd) {
                        *(pRetWalk++) = /*implicit: (float)*/ (*(pInWalk++));
                    }
                }
                return ret;
            }
        }</pre>
<p>The post <a rel="nofollow" href="https://ilnumerics.net/blog/uncommon-data-conversion-with-ilarray/">Uncommon data conversion with ILArray</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/uncommon-data-conversion-with-ilarray/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
