<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Weblog of Gopalakrishna Palem</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/gpalem/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/gpalem/feed.xml</link>
    <description>On Everything from Turing Machine to Turn-key optimization, Atom to Artificial Intelligence ... by Corporate Technology Strategist, Gopalakrishna Palem</description>
    <lastBuildDate>Wed, 17 Apr 2019 03:21:20 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Editing Meteor projects with Visual Studio</title>
      <link>https://docs.microsoft.com/archive/blogs/gpalem/editing-meteor-projects-with-visual-studio</link>
      <pubDate>Sat, 03 Jan 2015 23:04:30 GMT</pubDate>
      <dc:creator><![CDATA[Gopalakrishna Palem]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/gpalem/2015/01/03/editing-meteor-projects-with-visual-studio/</guid>
      <description><![CDATA[Meteor does not yet have full support for Windows. However, that should not deter one from using...]]></description>
      <content:encoded><![CDATA[<p>Meteor does not yet have full support for Windows. However, that should not deter one from using Visual Studio's great editing features for Meteor project development.</p>
<p>One particular trait that I personally like in VS is, tracking the code changes in the editor (with small green and yellow bars appearing on the side for changed and saved code portions). If you are like me, then you are probably using Linux VMs (on Windows) to build and test Meteor Projects. Here is one simple way to smooth the workflow.</p>
<ol>
<li>Get <strong>Node.js Turnkey Linux VM</strong>:&nbsp;<a href="https://www.turnkeylinux.org/nodejs">https://www.turnkeylinux.org/nodejs</a></li>
<li>Launch your Linux VM with <strong>Virtual Box</strong></li>
<li>Setup your Meteor project (with <strong>meteor add</strong> command or git clone command)</li>
<li>Test if you are able to connect to your Linux VM through ftp. If not, you may want to install <strong>vsftpd</strong>. Refer:&nbsp;<a href="https://www.wikihow.com/Set-up-an-FTP-Server-in-Ubuntu-Linux">https://www.wikihow.com/Set-up-an-FTP-Server-in-Ubuntu-Linux</a></li>
<li>Launch Visual Studio&nbsp;</li>
<li>Select the Open | <strong>Website</strong> option from File Menu.</li>
<li>Enter your FTP details. You can leave the Directory option empty, which will let you browse the whole Linux user home directory from VS. Else you can specify the meteor project directory</li>
<li>Once the solution is opened successfully, right click on the ftp project in the solution explorer and choose <strong>Browse With ...</strong> option.</li>
<li>In the browse with dialog, enter the url of the meteory project. Typically you just need to <strong>add port</strong> (:3000) to the end of the url.</li>
<li>Once done, you can press <strong>Ctrl + Shift + W</strong> to launch your meteor project in default browser. Else use the context menu option<strong> View in Browser.</strong></li>
</ol>
<div>Once the meteor project is launched in browser, meteor will take care of refreshing the page automatically whenever any component used in the project changes. So, you can freely edit any html page or javascript you want in the project and save the changes and see them automagically applied to your browser page.</div>]]></content:encoded>
    </item>
    <item>
      <title>Failure Rate estimation from historic data and Big data Analytics</title>
      <link>https://docs.microsoft.com/archive/blogs/gpalem/failure-rate-estimation-from-historic-data-and-big-data-analytics</link>
      <pubDate>Sat, 14 Sep 2013 00:26:00 GMT</pubDate>
      <dc:creator><![CDATA[Gopalakrishna Palem]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/gpalem/2013/09/14/failure-rate-estimation-from-historic-data-and-big-data-analytics/</guid>
      <description><![CDATA[My earlier post described Predictive Maintenance and showcased a demo video on M2M Telematics...]]></description>
      <content:encoded><![CDATA[<p>My earlier <a href="https://blogs.msdn.com/b/gpalem/archive/2013/09/14/condition-based-predictive-maintenance-with-m2m-big-data-analytics.aspx">post</a> described <em>Predictive Maintenance</em> and showcased a demo video on M2M Telematics application for computing <em>failure rate</em> and <em>MTTF</em> in real time from simulated sensor data attached to Machines/Vehicles.</p>
<p>At the end of that case study video I promised to demonstrate computing failure rate when you have no access to sensor data, purely from historic maintenance failure records.</p>
<p>Here is the video that demonstrates predicting future failures from historic failure data.</p>
<center><iframe src="https://www.youtube.com/embed/nfQ4U4tBnuY" frameborder="0" width="420" height="315"></iframe></center>
<p>With this analysis, you can answer questions such as:</p>
<ul>
<li>How many failure are likely to occur by the end of next year?</li>
<li>If I were to choose between two types of design, which one is more likely to meet my reliability requirements?</li>
<li>What is best inspection cycle that I should employ? etc...</li>
</ul>
<p>For more details on predictive analytics and its application in relation with M2M big data Telematics and Natural language processing for health care applications, retail and automotive, refer to my publications, available at: <a href="https://gopalakrishna.palem.in/Publications.html">https://gopalakrishna.palem.in/Publications.html</a>.</p>]]></content:encoded>
    </item>
    <item>
      <title>Condition based Predictive Maintenance with M2M Big data Analytics</title>
      <link>https://docs.microsoft.com/archive/blogs/gpalem/condition-based-predictive-maintenance-with-m2m-big-data-analytics</link>
      <pubDate>Fri, 13 Sep 2013 23:49:06 GMT</pubDate>
      <dc:creator><![CDATA[Gopalakrishna Palem]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/gpalem/2013/09/13/condition-based-predictive-maintenance-with-m2m-big-data-analytics/</guid>
      <description><![CDATA[Manufacturers and operators employ array of maintenance strategies, all of which can be broadly...]]></description>
      <content:encoded><![CDATA[<p>Manufacturers and operators employ array of maintenance strategies, all of which can be broadly categorized as below:</p>
<ul>
<li>Corrective Maintenance</li>
<li>Preventive Maintenance</li>
<li>Predictive Maintenance</li>
</ul>
<center><iframe src="https://www.youtube.com/embed/GUg4FjhTRS0" frameborder="0" width="420" height="315"></iframe></center>
<p>Corrective maintenance is the classic <em>Run-to-Failure</em> reactive maintenance that has no special maintenance plan in place. The machine is <em>assumed</em> to be fit unless proven otherwise.</p>
<ul>
<li>Cons:
<ul>
<li>High risk of collateral damage and secondary failure</li>
<li>High production downtime</li>
<li>Overtime labor and high cost of spare parts</li>
</ul>
</li>
<li>Pros:
<ul>
<li>Machines are not over-maintained</li>
<li>No overhead of condition monitoring or planning costs</li>
</ul>
</li>
</ul>
<p>Preventive maintenance (PM) is the popular <em>periodic maintenance</em> strategy that is actively employed by all manufacturers and operators in the industry today. An optimal breakdown window is pre-calculated (at the time of component design or installation, based on a wide range of models describing the degradation process of equipment, cost structure and admissible maintenance etc.), and a preventive maintenance schedule is laid out. Maintenance is carried-out on those periodic intervals, <em>assuming</em> that the machine is going to break otherwise.</p>
<ul>
<li>Cons:
<ul>
<li>Calendar-based maintenance: Machines are repaired when there are no faults</li>
<li>There will still be unscheduled breakdowns</li>
</ul>
</li>
<li>Pros:
<ul>
<li>Fewer catastrophic failures and lesser collateral damage</li>
<li>Greater control over spare-parts and inventory</li>
<li>Maintenance is performed in controlled manner, with a rough estimate of costs well-known ahead of time</li>
</ul>
</li>
</ul>
<p>Predictive Maintenance (PdM) is an alternative to the above two that employs predictive analytics over real-time data collected (streamed) from parts of the machine to a centralized processor that detects variations in the functional parameters and detects anomalies that can potentially lead to breakdowns. The real-time nature of the analytics helps identify the functional breakdowns long before they happen but soon after their potential cause arises.</p>
<ul>
<li>Pros:
<ul>
<li>Unexpected breakdown is reduced or even completely eliminated</li>
<li>Parts are ordered when needed and maintenance performed when convenient</li>
<li>Equipment life and there by its utilization is maximized</li>
</ul>
</li>
<li>Cons:
<ul>
<li>Investment costs for implementing the condition-based monitoring (CBM) system</li>
<li>Additional skills might be required to effectively use the CBM system effectively</li>
</ul>
</li>
</ul>
<p>Predictive maintenance, also known as Condition Based Maintenance (CBM) differs from preventive maintenance by basing maintenance need on the <em>actual</em> condition of the machine rather than on some preset schedule or <em>assumptions</em>.</p>
<p>For example, a typical preventive maintenance strategy demands automobile operators to change the engine oil after every 3,000 to 5,000 Miles traveled. No concern is given to the actual condition of vehicle or performance capability of the oil. If on the other hand, the operator has some way of knowing or somehow measuring the actual condition of the vehicle and the oil lubrication properties, he/she gains the potential to extend the vehicle usage and postpone oil change until the vehicle has traveled 10,000 Miles, or perhaps pre-pone the oil change in case of any abnormality.</p>
<p>Predictive analytics with M2M telematics provides such deep insights into the machine operations and full functionality status &ndash; giving rise to optimal maintenance schedules with improved machine availability.</p>
<p>The enormous data streamed in realtime from sensors attached to the machine/vechile is processed with big data architectures to enable anamoly detection and failure prediction in real time.</p>
<p>The demo video above showcases the Predictive Maintenance in operation. It demonstrates computing the <em>Failure rate</em> and <em>MTTF</em> from realtime operating conditions of machine.</p>
<p>More details can be found in my <a href="https://gopalakrishna.palem.in/Publications.html">M2M Telematics &amp; Predictive Analytics</a> paper.</p>]]></content:encoded>
    </item>
    <item>
      <title>Surviving Blender coming from Maya</title>
      <link>https://docs.microsoft.com/archive/blogs/gpalem/surviving-blender-coming-from-maya</link>
      <pubDate>Fri, 23 Aug 2013 05:01:00 GMT</pubDate>
      <dc:creator><![CDATA[Gopalakrishna Palem]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/gpalem/2013/08/23/surviving-blender-coming-from-maya/</guid>
      <description><![CDATA[For those who worked with Maya, Blender UI presents a nightmare. Even simple tasks of navigating the...]]></description>
      <content:encoded><![CDATA[<p>For those who worked with Maya, Blender UI presents a nightmare. Even simple tasks of navigating the views or adjusting the camera poses a big challenge, with the Blender's native misuse of 'right mouse button'.</p>
<p>However, you can survive past those initial hurdles with few adjustments in the preferences. In the below I list few common challenges that Maya users face with Blender and how to circumvent them, if not eliminate them completely.</p>
<ul>
<li><strong>Right Mouse Button Selection:</strong>This one is the source of all evil. This is what happens if you do not follow the well-established usage conventions and software UI behavioral patterns.
<p>Luckily the <em>adamantium</em> got depleted at Blender camp and now, as of version 2.68, they ship <em>Maya Preset</em> bundled by default that works flawlessly in mimicking the native Maya selection behavior. Select it from the File|User Preferences menu option, then the Input tab and <em>Presets</em> dropdown box.</p>
</li>
<li><strong>Scale, Rotate, Move:</strong> You can use your E, R, T usual shortcuts for these, using the <em>Maya Preset</em>configuration.<br /><br /></li>
<li><strong>Viewport Shading mode:</strong> Again, the <em>Maya preset&nbsp;</em>takes care of this one nicely. Press 4, 5 and 6 keys as you would have in Maya and now your viewport makes the meshes appropriately.<br /><br /></li>
<li><strong>Perspective / Ortho views:</strong>Well, for this one, the Blender native shortcuts are, NumPad-0 through Numpad-7. Not an ergonomic choice, given that NumPad keys are half way across the globe in the eastern side of keyboard (which is not your keyboard hand's natural resting position), and also most laptops do not have NumPad keys (know your customers: open-source 3D modeling software customers more likely to be trying Blender on laptops for a quick peek rather than a dedicated professional setup in their initial stages).
<p>But luckily, you can now use Ctrl+0 through Ctrl+7 shortcuts for the same effect. No need for NumPad keys. Agreed, Ctrl+0 is not same as NumPad-0 (two keys vs one key) but atleast you have a way out. (There are some scripts also in the Blender Add-ons that might be able to help you with this one.)</p>
</li>
<li><strong>Camera Navigation:</strong> Perhaps, the most irritating <em>feature</em> (or, shall I say <em>bug</em>) that you encounter in Blender (coming from Maya) is, once you are in Camera perspective (Ctrl+0 short-cut, same as 'look through camera' feature of Maya) if you try to adjust its positioning by transformation or rotation, the view goes out of camera perspective. There is just no way you could adjust your camera while looking through it. You would have to use two views, one to look through the camera, and the other to adjust the camera like a normal 3D-object. Yuck.
<p>Luckily, at least in latest build 2.68 of Blender, you have a way out of this. Actually not one, but two ways. First is, press <em>Shift+F</em> within camera perspective (with the camera border selected), you enter a <em>Fly</em> mode. Then you can position the camera by using mouse-wheel and buttons.</p>
<p>But I have a more better option. With the camera selected, go to its <em>View</em> properties, and select <em>Lock Camera to View</em> check box. Once ticked, then you can just use your regular Pan, Zoom etc. keys to adjust the scene and your Camera indeed gets adjusted. Once you are done, you can uncheck it and get along with your model as usual.</p>
</li>
</ul>
<p>By the way, it is not for nothing that companies like Apple or Microsoft spends millions of dollors in conducting detailed UI behavioral profiling studies, time and again proving: "Being creative is pefectly possible without voilating conventions". Though who or what defines convention is upto each individual to decide for oneself.</p>]]></content:encoded>
    </item>
    <item>
      <title>Extract pages from PDF or Merge multiple PDF files into Single PDF with Latex</title>
      <link>https://docs.microsoft.com/archive/blogs/gpalem/extract-pages-from-pdf-or-merge-multiple-pdf-files-into-single-pdf-with-latex</link>
      <pubDate>Wed, 17 Jul 2013 21:58:00 GMT</pubDate>
      <dc:creator><![CDATA[Gopalakrishna Palem]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/gpalem/2013/07/17/extract-pages-from-pdf-or-merge-multiple-pdf-files-into-single-pdf-with-latex/</guid>
      <description><![CDATA[In the recent times I often found myself looking for tools to perform few simple operations on PDF...]]></description>
      <content:encoded><![CDATA[<p>In the recent times I often found myself looking for tools to perform few simple operations on PDF files, such as extracting single or selective multiple pages from my scanned PDF files, and/or merge multiple scanned PDF documents into a single PDF and so on.</p>
<p>However, the solution did not come straight, but it just occurred to me while I was working on something else entirely different.</p>
<p>Here I was, preparing my <a title="The Practice of Predictive Analytics in Healthcare by Gopalakrishna Palem" href="https://www.researchgate.net/publication/236336250_The_Practice_of_Predictive_Analytics_in_Healthcare/file/72e7e51e77e8730b8d.pdf">Healthcare Analytics white paper</a> working on its Title page from scratch, and the realization dawned upon me - if you are a Latex user like me, then you have all the necessary tools at your disposal to perform the tricks on PDF files - once you become aware of this small package called&nbsp;<em>pdfpages</em>, that is. You can quickly, in just a single line or two, do whatever you want with your scanned PDF documents all by your own Latex code.</p>
<p>The <em>includepdf</em> command is going to be your solution. For example, the below code lets you extract 3rd page from your scanned PDF file:</p>
<pre style="font-size: 10pt; font-family: Courier New;">\documentclass{article} 
\usepackage{pdfpages}

\begin{document}
\includepdf[pages=3]{scanned_Doc.pdf}
\end{document}
</pre>
<p>Now, you might encounter online tools to merge few PDF documents into a single document, but they may not let you selectively pick pages from each of those documents and then do the merge. This kind of merge is pretty straight forward with <em>includepdf</em> as shown below.</p>
<p>Here is how you can combine pages from your cover letter PDF document with selective pages from your scanned certificate documents:</p>
<pre style="font-size: 10pt; font-family: Courier New;">\documentclass{article} 
\usepackage{pdfpages}

\begin{document}
\includepdf[pages=1]{CoverLetter.pdf}
\includepdf[pages={3,4}]{cert.pdf}
\end{document}
</pre>
<p>The package also lets you insert multiple extracted pages in a single page with <em>nup</em> option, as below (where first 1 to 4 pages are arranged in 3 columns x 2 rows on a single page):</p>
<pre style="font-size: 10pt; font-family: Courier New;">\includepdf[nup=3x2, pages=1-4]{cert.pdf}
</pre>
<p>You should be able to offset the extracted PDF into correct location on your page using the regular <em>setlength</em> commands. For a full page PDF insertion, you can try something like below:</p>
<pre style="font-size: 10pt; font-family: Courier New;">\documentclass{article} 
\usepackage{pdfpages}
\pagestyle{plain} <span style="color: #339966;">% no page numbers</span>
\begin{document}
\setlength\voffset{-0.1in} <span style="color: #339966;">% adjust the vertical offset</span>
\setlength\hoffset{-0.4in} <span style="color: #339966;">% adjust the horizontal offset</span>
\includepdf[pages={2,2,2}]{Scanned_Doc.pdf} <span style="color: #339966;">% insert 2nd page from Scanned_Doc three times</span>
\end{document}
</pre>
<p>For further details on how to <strong>add overlays or annotations</strong> to the PDF pages, visit my article: <a href="https://gopalakrishna.palem.in/PdfPages.html#Overwrite">PDF Page Manipulation</a>.</p>]]></content:encoded>
    </item>
    <item>
      <title>Correcting Sector build errors</title>
      <link>https://docs.microsoft.com/archive/blogs/gpalem/correcting-sector-build-errors</link>
      <pubDate>Tue, 14 May 2013 05:49:02 GMT</pubDate>
      <dc:creator><![CDATA[Gopalakrishna Palem]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/gpalem/2013/05/14/correcting-sector-build-errors/</guid>
      <description><![CDATA[If you are building Sector on Ubuntu or Mint, you might encounter three kinds of errors: One about...]]></description>
      <content:encoded><![CDATA[<p>If you are building Sector on Ubuntu or Mint, you might encounter three kinds of errors: One about <em>::close()</em> in tcpTransport.cpp at line 157, next about <em>usleep</em>, and the other about <em>SSL</em>.</p>
<p>SSL related errors look like below:</p>
<pre style="font-size: 10pt; font-family: Courier New;">/sector/common/ssltransport.cpp:229: undefined reference to `SSL_new'
/sector/common/ssltransport.cpp:230: undefined reference to `SSL_set_fd'
/sector/common/ssltransport.cpp:232: undefined reference to `SSL_connect'
/sector/common/ssltransport.cpp:234: undefined reference to `SSL_free'
/sector/common/ssltransport.cpp:239: undefined reference to `SSL_get_verify_result'
</pre>
<p>The way to solve them is:</p>
<ul>
<li>For the <em>::close()</em> error in the tcpTransport.cpp, prefix <em>UDT</em> as explicit namespace and it will be good. It should read, UDT::close(m_iSocket)</li>
<li>For the <em>usleep</em> errors, these happen in multiple locations in the source, and you want to add <em>#include &lt;unistd.h&gt;</em> in respective files to resolve it (there will be many, so you have to keep issuing <em>make</em> and correct them one by one)</li>
<li>For the SSL related errors, first ensure you have SSL development files installed (e.g., libssl-dev for Debian or openssl-devel for Fedora). You can check with <em>ldd $(which openssl)</em></li>
<li>The main problem that happens in Sector make files for SSL is, out of order library linkage. The <em>ssl, crypto</em> libraries should come <strong>after</strong> <em>libclient.a</em> and <em>libsecurity.a</em> on the g++ command line, but the make files have it incorrect. Need to correct this.</li>
<li>You can correct the make files (one by one, once again) by opening the respective make file that is causing the error from the corresponding directory and adding <em>-lssl -lcrypto</em> to the end for <em>LDFLAGS&nbsp;</em>variable. It should look something like:
<pre style="font-size: 10pt; font-family: Courier New;">LDFLAGS += ../lib/libclient.a ../lib/libsecurity.a ../lib/librpc.a ../lib/libcommon.a ../lib/libudt.a -lssl -lcrypto</pre>
</li>
</ul>]]></content:encoded>
    </item>
    <item>
      <title>Convert igraph R objects to Gexf GePhi format</title>
      <link>https://docs.microsoft.com/archive/blogs/gpalem/convert-igraph-r-objects-to-gexf-gephi-format</link>
      <pubDate>Fri, 29 Mar 2013 03:55:00 GMT</pubDate>
      <dc:creator><![CDATA[Gopalakrishna Palem]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/gpalem/2013/03/29/convert-igraph-r-objects-to-gexf-gephi-format/</guid>
      <description><![CDATA[GePhi has interesting visualization capabilities built around graphs - and igraph is one of the...]]></description>
      <content:encoded><![CDATA[<div style="font-family: Helvetica; font-size: 9pt; font-weight: normal; padding: 0px 0 5px 0; line-height: 140%; color: #111111;">
<p>GePhi has interesting visualization capabilities built around graphs - and igraph is one of the widely used graph processing libraries for R. But there is no straightway to combine these two at present in R. The gexf format used by GePhi is not currently supported by the igraph package.</p>
<p>GePhi has its own package, the <em>rgexf</em> package for R, that provides some support for creating GePhi styled graphs out of custom matrix/dataframe data. It would have been great if it accepted an igraph object straight out-of-the box and generated a gexf object from it. But the current version does not support it. However, rolling out your own exporter is not that difficult. Here is something to get you started.</p>
<pre style="background-color: #f4f4fa; font-size: 10pt; font-family: Courier New;">  # load the igraph and rgexf packages
  library(igraph)
  library(rgexf)

  # create a sample igraph object
  cg1 &lt;- erdos.renyi.game(10, 0.8)

  # construct the nodes and edges data for gexf conversion
  nodes &lt;- data.frame(cbind(V(cg1), as.character(V(cg1))))
  edges &lt;- t(Vectorize(get.edge, vectorize.args='id')(cg1, 1:ecount(cg1)))

  # do the conversion
  write.gexf(nodes, edges)    </pre>
<p>This piece of code will generate xml formatted in gexf ready for GePhi. You can further add node attributes and edge weights in similar fashion.</p>
<p>There are couple of points to be taken care though, such as xml &amp; problem. For some reason, <em>write.gexf</em> fails to properly handle &amp; symbols in the attribute vectors, so you would have to convert the &amp; symbol to &amp; before sending it to <em>write.gexf</em>.</p>
<p>Also note that write.gexf writes to console output and does not save to file. You would have to use print method for that. This is somewhat confusing naming convension !!</p>
<p>A reusable method with complete source code for exporting the igraph nodes and edges along with the associated attribute data can be found <a href="https://gopalakrishna.palem.in/iGraphExport.html#GexfExport">here</a>.</p>
</div>]]></content:encoded>
    </item>
    <item>
      <title>Make Vectorize your friend in R</title>
      <link>https://docs.microsoft.com/archive/blogs/gpalem/make-vectorize-your-friend-in-r</link>
      <pubDate>Thu, 28 Mar 2013 19:13:43 GMT</pubDate>
      <dc:creator><![CDATA[Gopalakrishna Palem]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/gpalem/2013/03/28/make-vectorize-your-friend-in-r/</guid>
      <description><![CDATA[Old habits die hard. For someone coming from linear programming models such as C++ or Java, it takes...]]></description>
      <content:encoded><![CDATA[<p>Old habits die hard. For someone coming from linear programming models such as C++ or Java, it takes quite a bit of effort before a <em>fully-compliant</em> R functions can be developed. The major pit-fall is, defining a function that is not <em>vector-compliant</em>.</p>
<p>For example, consider the below function that accepts two numbers and returns some computed value based on them.</p>
<pre style="font-size: 10pt; font-family: Courier New;">    getNc2 = function(vc, ec)
    {
      # compute nC2 and return the ratio
      max_ec = vc * (vc-1) / 2
      return (trunc(ec * 100 / max_ec))
    }    </pre>
<p>This is a valid function in R, and perhaps similar to any C++ or Java method, except it works on both single number input <em>and</em> vector inputs. For example, note the below single number input calls:</p>
<pre style="font-size: 10pt; font-family: Courier New;">&gt; getNc2(5, 4)
[1] 40

&gt; getNc2(6, 5)
[1] 33    </pre>
<p>Now, those two calls can be combined into a single vector call as below and our above definition works perfectly fine without complaints:</p>
<pre style="font-size: 10pt; font-family: Courier New;">    &gt; getNc2(c(5,6), c(4,5))
    [1] 40 33    </pre>
<p>The method accepted two vectors (instead of just two individual numbers) and returned vectorized results. Something not quite natural in C++ or Java, unless special care is taken to define the argument types as templated parameters and so on.</p>
<p>Now, what about the limits? Our method is computing a ratio which essentially involves a division. If we are not careful, we can endup with dividing-by-zero. For example, consider the below calls:</p>
<pre style="font-size: 10pt; font-family: Courier New;">&gt; getNc2(0, 5)
[1] -Inf

&gt; getNc2(0, 0)
[1] NaN    </pre>
<p>Suppose, we want to handle these cases where the argument is zero and return 0 instead of <em>-Inf</em> or <em>NaN</em>. Then we will be adding an if condition as below:</p>
<pre style="font-size: 10pt; font-family: Courier New;">    getNc2 = function(vc, ec)
    {
      # if no vertices return 0
      if(vc &lt;= 1) return (0)
      # compute nC2 and return the ratio
      max_ec = vc * (vc-1) / 2
      return (trunc(ec * 100 / max_ec))
    }    </pre>
<p>Now, the earlier calls will return 0 correctly as shown below:</p>
<pre style="font-size: 10pt; font-family: Courier New;">&gt; getNc2(0, 0)
[1] 0
&gt; getNc2(0, 5)
[1] 0    </pre>
<p>However, the if condition introduces a new problem. <em>if</em> in R is not a vector-compatible opration. It works only for scalar inputs and as such our method hence fails on vector inputs. For example:</p>
<pre style="font-size: 10pt; font-family: Courier New;">&gt; getNc2(c(5,0), c(4,5))
[1]   40 -Inf
Warning message:
In if (vc &lt;= 1) return(0) :
  the condition has length &gt; 1 and only the first element will be used    </pre>
<p>Now, there are couple of ways you can address this problem. One would be to use <em>apply</em> constructs - but they can be overkill oftentimes. Other way is, using <em>ifelse</em> instead of <em>if</em> etc. vector-capable operations explicitly. That would work, provided you have the option to change the source. What about cases where you are using a method from some package and you cannot change the method ?</p>
<p>Take the method <em>get.edge</em> from <em>igraph</em> package, for example. It accepts an edge id and returns the vertices for that edge. However, if you supply a vector of edge ids, it will not work - it just returns the vertices for only the first edge, as can be seen from the below output:</p>
<pre style="font-size: 10pt; font-family: Courier New;">&gt; cg &lt;- erdos.renyi.game(8, 0.6)
&gt; get.edge(cg, 13)
[1] 4 6
&gt; get.edge(cg, c(13,15,16))
[1] 4 6    </pre>
<p>How do you make these kind of methods work for vector inputs as well as scalar inputs? R has a nice base method under its sleeve that exactly addresses this problem. Named <em>Vectorize</em>, it helps in these situations - coverting scalar methods to vector methods.</p>
<p><em>Vectorize</em> creates a function wrapper that vectorizes the action of its argument function. For example, we can vectorize our <em>getNc2</em> method as shown below:</p>
<pre style="font-size: 10pt; font-family: Courier New;">&gt; Vectorize(getNc2)(c(5,0), c(4,5))
[1] 40  0    </pre>
<p>Instead of directly calling <em>getNc2</em>, we are rather creating a wrapper around it by calling <em>Vectorize</em> method with <em>getNc2</em> as its argument, and supplying the original vector inputs to the resultant function. In other words, the above is same as:</p>
<pre style="font-size: 10pt; font-family: Courier New;">&gt; vecNc2 &lt;- Vectorize(getNc2)
&gt; vecNc2(c(5,0), c(4,5))
[1] 40  0    </pre>
<p>Remember, the vectorized <em>vecNc2</em> method works perfectly fine on scalar inputs too. For example:</p>
<pre style="font-size: 10pt; font-family: Courier New;">&gt; vecNc2(5, 4)
[1] 40    </pre>
<p>By default <em>Vectorize</em> tries to vectorize all the arguments of the supplied method, which may not work sometimes. For example, the earlier example of <em>get.edge</em> in <em>igraph</em>:</p>
<pre style="font-size: 10pt; font-family: Courier New;">&gt; vecGE &lt;- Vectorize(get.edge)
&gt; vecGE(cg, c(13,15,16))
Error in function (graph, id)  : Not a graph object    </pre>
<p>Here the <em>get.edge</em> method is accepting two inputs, one the graph object itself and the second the edge id - and we want to vectorize only the second argument, namely the <em>id</em> argument and not the graph object. For this, we would have to use the <em>vectorize.args</em> facility of <em>Vectorize</em> method as shown below:</p>
<pre style="font-size: 10pt; font-family: Courier New;">&gt; vecGE &lt;- Vectorize(get.edge, vectorize.args="id")
&gt; vecGE(cg, c(13,15,16))
     [,1] [,2] [,3]
[1,]    4    1    2
[2,]    6    7    7    </pre>
<p>This is a simplified output of the <em>Vectorize</em> method. We can use the <em>SIMPLIFY</em> facility of <em>Vectorize</em> method to get the original results unaltered. For example:</p>
<pre style="font-size: 10pt; font-family: Courier New;">&gt; vecGE &lt;- Vectorize(get.edge, vectorize.args="id", SIMPLIFY=FALSE)
&gt; vecGE(cg, c(13,15,16))
[[1]]
[1] 4 6

[[2]]
[1] 1 7

[[3]]
[1] 2 7    </pre>
<p>Note, this is same as the <em>lapply</em> output.</p>
<pre style="font-size: 10pt; font-family: Courier New;">&gt; lapply(c(13,15,16), function(id) get.edge(cg, id))
[[1]]
[1] 4 6

[[2]]
[1] 1 7

[[3]]
[1] 2 7    </pre>
<p>So go ahead and use <em>Vectorize</em> whenever you want to avoid <em>lapply</em> constructs for methods that accept only scalar inputs.</p>]]></content:encoded>
    </item>
    <item>
      <title>Building ZeroMQ package for R (rzmq) from source</title>
      <link>https://docs.microsoft.com/archive/blogs/gpalem/building-zeromq-package-for-r-rzmq-from-source</link>
      <pubDate>Wed, 06 Feb 2013 05:57:00 GMT</pubDate>
      <dc:creator><![CDATA[Gopalakrishna Palem]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/gpalem/2013/02/06/building-zeromq-package-for-r-rzmq-from-source/</guid>
      <description><![CDATA[rzmq (ZeroMQ for R) is available only in source form on CRAN for Windows platform. So its upto you...]]></description>
      <content:encoded><![CDATA[<p>rzmq (ZeroMQ for R) is available only in source form on CRAN for Windows platform. So its upto you to build and install it as a binary package. Here are few steps to help achieve that.</p>
<p>First, you need to get the dependencies (ZeroMQ library and MikTex) ready. ZeroMQ, as usual, can be downloaded from <a href="https://www.zeromq.org/area:download">here</a>, and MikTex from <a href="https://miktex.org/">here</a>. In case you opt for MikTex portable, you might need to add the MikTex bin path to the system path (i.e. <em>pdflatex.exe</em>program from the MikTex bin directory should be accessible at the command prompt). In case you are on a 64-bit machine, then you might need to install both 32-bit and 64-bit versions of the ZeroMQ libraries to ensure your package build succeeds. By default, the R build command tries to build for all platforms.</p>
<p>Also, you would need to download the zmq.hpp file (from <a href="https://github.com/zeromq/cppzmq">here</a>) and put it in the ZeroMQ include directory. Otherwise, you will get compiler error.</p>
<p>Very importantly you would need the RTools to build R packages from source. Get the RTools from <a href="https://cran.r-project.org/bin/windows/Rtools/">here</a>. Ensure you select the path adjustments recommended by the installer. It is VERY important that the RTools bin directory comes <em>before</em> the rest of your path. Otherwise there is a good chance that you will never get it to work.</p>
<p>After setting the dependencies, get the latest rzmq source from git <a href="https://github.com/armstrtw/rzmq">here</a> (the one on CRAN is very old - do not use it, use the git version directly) and extract it to some directory on your disk. Lets say you extract it to <em>C:\</em>, then you have <em>C:\rzmq\src\interface.cpp</em> file that need to be compiled.</p>
<ul>
<li>First, open your Visual Studio command prompt and go to your extraction folder where <em>rzmq</em> folder is present. In our example it is <em>C:\</em></li>
<li>Now if you issue the command <strong><em>R CMD check rzmq&nbsp;</em></strong>you should see some output, and perhaps some build error.
<ul>
<li>If you get "Warning: 'rzmq' is neither a file nor directory, skipping" error, then you are not in the right directory. Go to correct directory that has <em>rzmq</em> folder inside it and issue the command again</li>
<li>R commands are case-sensitive, so ensure you type <em>CMD check</em> exactly as is</li>
<li>If you get unknown command or invalid command error, ensure that your R installation is accessible from command line. You might want to add your R bin path to your system path.</li>
</ul>
</li>
<li>Build error might happen due to ZeroMQ libraries not being found. This can be fixed by modifying the <em>Makevars</em> file in the <em>rzmq/src</em> folder. The <em>Makevars</em> file lets you supply the additional include paths, library paths and library files required for building the C++ programs as R package. You need to set the <em>PKG_LIBS</em> and <em>PKG_CPPFLAGS</em>variables correctly. Typically it should read,
<ul>
<li>PKG_LIBS = -L"C:\Program Files\ZeroMQ 2.2.0\lib" -llibzmq-v100-mt</li>
<li>PKG_CPPFLAGS=-I"C:\Program Files\ZeroMQ 2.2.0\include"</li>
</ul>
You should specify your ZeroMq library path and header-file locations as above. The <em>libzmq-v100-mt</em> is the .lib file that is present in the C:\Program Files\ZeroMQ 2.2.0\lib. Note the <em>-l</em> before the <em>libzmq-v100-mt</em>.</li>
<li>Now you need to copy all dll files from ZeroMQ bin directory to to the <em>rzmq/src</em>folder.
<ul>
<li>For example, copy C:\Program Files\ZeroMQ 2.2.0\bin\*.dll c:\rzmq\src\</li>
</ul>
</li>
<li>Issue the command <strong><em>R CMD check rzmq </em></strong>again. It should complete fine.
<ul>
<li>You might have to add the <em>pdfLatex.exe</em> path to your system path</li>
<li>Your MikTex distribution might be missing couple of packages and you might be prompted for installing them.</li>
</ul>
</li>
<li>If everything is completed successfully, you should have a <em>rzmq.RCheck</em> folder alongside your <em>rzmq</em> folder with <em>rzmq-manual.pdf</em> built inside it correctly.</li>
<li>Now use the command <em><strong>R CMD INSTALL rzmq </strong></em>to install the built R package to your R libraries repository.
<ul>
<li>You can use the --library option with the R CMD INSTALL to specify an alternate R library location, if required</li>
</ul>
</li>
</ul>
<p>You should now be ready to use rzmq from your R.</p>
<p><span style="font-size: 12px;">On 64-bit machines, instead of the above indicated, you want to use the below <em>--no-multiarch</em> version commands:</span></p>
<p>&nbsp; &nbsp;&nbsp;<em>R --arch x64 CMD check --no-multiarch rzmq</em></p>
<p>&nbsp; &nbsp;&nbsp;<em><strong>R --arch x64 CMD INSTALL --no-multiarch rzmq </strong></em></p>]]></content:encoded>
    </item>
    <item>
      <title>Dealing with Qt out-of-order focusoutevent bug</title>
      <link>https://docs.microsoft.com/archive/blogs/gpalem/dealing-with-qt-out-of-order-focusoutevent-bug</link>
      <pubDate>Sat, 03 Mar 2012 23:39:00 GMT</pubDate>
      <dc:creator><![CDATA[Gopalakrishna Palem]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/gpalem/2012/03/03/dealing-with-qt-out-of-order-focusoutevent-bug/</guid>
      <description><![CDATA[Some call it as feature, others treat it as bug - be what it may, you still have to address this if...]]></description>
      <content:encoded><![CDATA[<p>Some call it as feature, others treat it as bug - be what it may, you still have to address this if your application depends on the order of events in Qt.</p>
<p>Consider the case where you have a QLineEdit object with editingFinished() handler, along with a QPushButton with its relevant clicked() handler (or a toolbar /menu action with its triggered() handler). Now try typing something in the lineEdit followed immediately by a click on the button or menu/toolbar (while the cursor is still in the text box). You will see the button clicked() (or toolbar action triggered()) handler getting invoked <b>before</b> editingFinished() handler.</p>
<p>This happens because of the incorrect order in which focus out and button clicked events are raised by Qt. For some reasons, when focus shifts from textbox to button, the clicked() event for button gets triggered <i>before</i> the focusoutevent() for the lineEdit.</p>
<p>Now, if you do not see this behavior, then you do not need to worry. On the otherhand, if you do see this behavior, but argue that its a <i>feature</i> - good luck to you - but for my CarMusTy, this out of sequence event order means a problem, since there are custom property managers user has to deal with and receiving editingFinished() <b>after</b> the button event handler means having inconsistent state in the generated pdf. (In case you are wondering what CarMusTy is, its a Typesetting environment for editing and publishing high quality Carnatic Music Books. Compromising on quality is certainly not an option for this application.)</p>
<p>Now, how do we deal with such situation - is there a way you can <i>force</i> the event order in the Qt. Something that can help you tell Qt to raise the editingFinished() of lineEdit <i>before</i> the clicked() handler of button?</p>
<p>Luckily, there is one. Here is the way CarMusTy addressed this problem. Try this - it might help you too.</p>
<p>The event subscription mechanism of Qt allows you to specify how the handler should be invoked, through Qt::DirectConnection and Qt::QueuedConnection options. The Qt::DirectConnection lets the handler be called right-on-the-spot when the signal is emitted vs. the Qt::QueuedConnection lets the handler be pushed into the event queue and called next time when the event loop is executed. Combine this with the QCoreApplication::processEvents() method and you can execute all pending queued events before they are naturally invoked</p>
<p>As a first step, ensure that your button clicked() event handler is connected explicitly through QObject::connect() method rather than through named connection. This way you can specify Qt::QueuedConnection to the button clicked() event handler while connecting it to your slot. (If you let the named connections automatically connect your button clicked event handler, then you will not be able to specify Qt::QueuedConnection. Named connections by default use the Qt::AutoConnection, which resorts to direct connection if on the same thread.) This Qt::QueuedConnection specification helps delay the button event handler till the event loop execution and might resolve some order issue.</p>
<p>Note that when the button clicked handler is called, the editingFinished() will be waiting in the queue to be process once your button clicked handler returns. To ensure that the editingFinished() handler is completed <i>before</i>, you can insert a QCoreApplication::processEvents() call in your button clicked event handler. This forces the event queue to be flushed and thus the editingFinished() be called at that point.</p>
<p>For example, OnBuildPreview() is the button click handler and SongRagaChanged() is the lineEdit valuechanged handler. We want SongRagaChanged() be called <b>before</b> OnBuildPreview() be called. But we cannot enforce Qt to this, so we make QCoreApplication::processEvents() inside the buttong handler (OnBuildPreview), to force the line edit handler SongRagaChanged() get complete first.</p>
<div style="font-size: 10pt; font-family: Courier New;">
<pre>void PDFDocument::OnBuildPreview() // Handler for the button clicked
{
    // We need this to flush the events (so that any changes to song data get comitted first through SongRagaChanged())
    QCoreApplication::processEvents();

    if(m_pCMInputArea-&gt;OnCompile())            
        reloadPreview(m_pCMInputArea-&gt;GeneratedPDFPath());
}

void CMInputArea::SongRagaChanged(const QString &amp;val) // Handler for text value changed
{
    CallSongItemMethod(SetRaga, val);
    m_pVerseRagaProp-&gt;AddEditListEntry(val);
}
</pre>
</div>
<p>- Gopalakrishna Palem, Creator of <a href="https://cfugue.sourceforge.net">CFugue</a> and <a href="https://carmusty.sourceforge.net">CarMusTy</a></p>]]></content:encoded>
    </item>
  </channel>
</rss>