{"id":2700,"date":"2020-08-02T22:02:55","date_gmt":"2020-08-03T06:02:55","guid":{"rendered":"http:\/\/antoine.wojdyla.fr\/blog\/?p=2700"},"modified":"2021-11-16T13:58:05","modified_gmt":"2021-11-16T21:58:05","slug":"its-the-units-stupid","status":"publish","type":"post","link":"http:\/\/antoine.wojdyla.fr\/blog\/2020\/08\/02\/its-the-units-stupid\/","title":{"rendered":"Units, please"},"content":{"rendered":"<p>Whenever I see <strong>scientific code without units<\/strong> <em>I scream inside my heart<\/em>.<\/p>Everytime you write <strong>simulations of <em>physical phenomena<\/em><\/strong> (often using numpy or matlab), make sure to always have variables where the units are clear, e.g. :<\/p>\n<blockquote><p>lambda_m = 633e-9 #wavelentgh in meters<br \/>\nc_mps = 3e8 #speed of light in meters per seconds<br \/>\nfreq_Hz = c_mps\/lambda_m<\/p><\/blockquote><p>Failure to keep good track of the units has led to disasters. Yet complete lack of clarity\u00a0 happens more often than not \u2013 just look at the code of a random scientist on github to witness the extent of the damage.<\/p>\n<h3><a href=\"http:\/\/antoine.wojdyla.fr\/blog\/wp-content\/uploads\/swimming-hard.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-2716 aligncenter\" src=\"http:\/\/antoine.wojdyla.fr\/blog\/wp-content\/uploads\/swimming-hard.jpg\" alt=\"\" width=\"294\" height=\"393\" srcset=\"http:\/\/antoine.wojdyla.fr\/blog\/wp-content\/uploads\/swimming-hard.jpg 500w, http:\/\/antoine.wojdyla.fr\/blog\/wp-content\/uploads\/swimming-hard-224x300.jpg 224w\" sizes=\"auto, (max-width: 294px) 100vw, 294px\" \/><\/a><\/h3><p>The reason why I am adamant about this is because a lot of time is wasted trying to debug code where it there&#8217;s a silly unit mismatch, and because we are doing physics.<\/p>\n<h3>Math versus physics<\/h3><p>Why is coding without units such a terrible practice? It all boils down to the fact that computing is mostly about math and logic, and therefore not geared towards physical quantities. There is beauty in mathematical abstraction, but sometimes it doesn&#8217;t mean anything.<\/p>Take a mathematical statement that should be true:<\/p>\n<blockquote><p>1+1=2<\/p><\/blockquote><p>Now ask yourself: what does it mean? If I add one orange to one apple:<\/p>\n<blockquote><p>1 orange + 1 apple = ?<\/p><\/blockquote><p>It might sound silly but it&#8217;s actually pretty deep. You cannot add quantities which are not congruent. Yes, you may say that by adding one fruit with another fruit you get two fruits, but you&#8217;re cheating then.<\/p>This is somehow <strong>why object-oriented programming was invented<\/strong>: with the notion of &#8220;classes&#8221;, you can add entities which are compatible, through the game of function overloading and other niceties. In an ideal world, physical quantities in simulations should all have their own class, where the units would be defined.<\/p><!--more--><\/p>\n<h3>Coding without units is like a boat without a rudder<\/h3><p>I have rescued the code of many undergrads and grads by just adding units and let them realized that their calculations did not make sense.<\/p>\n<blockquote>\n<div role=\"group\" aria-label=\"Message shortcuts\" data-qa=\"message-actions\">&#8220;if you don&#8217;t know where you&#8217;re going you may end up somewhere else&#8221; \u2013 Yogi Berra<\/div>\n<\/blockquote>\n<h3 role=\"group\" aria-label=\"Message shortcuts\" data-qa=\"message-actions\">Some icebergs<\/h3>\n<div role=\"group\" aria-label=\"Message shortcuts\" data-qa=\"message-actions\">Of course, sometimes it&#8217;s unclear what the unit of a variable is, especially when they are intermediary variables.<\/div>\n<div role=\"group\" aria-label=\"Message shortcuts\" data-qa=\"message-actions\">The biggest offender is the Fourier Transform, where conversion between direct and reciprocal space if often botched (by laziness or misunderstanding.)<\/div>\n<div role=\"group\" aria-label=\"Message shortcuts\" data-qa=\"message-actions\"><\/div>\n<div role=\"group\" aria-label=\"Message shortcuts\" data-qa=\"message-actions\">If you process data, sometimes you miss a conversion factor (distances, angles)<\/div>\n<div role=\"group\" aria-label=\"Message shortcuts\" data-qa=\"message-actions\"><\/div>\n<div role=\"group\" aria-label=\"Message shortcuts\" data-qa=\"message-actions\">But these situation should not deter you: it&#8217;s a good occasion to ask yourself what are the dimensions in your system that you need to record in your metadata.<\/div>\n<div role=\"group\" aria-label=\"Message shortcuts\" data-qa=\"message-actions\"><\/div>\n<div role=\"group\" aria-label=\"Message shortcuts\" data-qa=\"message-actions\">I also found that trying to get &#8220;absolute&#8221; measurements when possible (e.g. converting counts on a camera to photons) does bring a lot of insights regarding noise and potential limitations of simulations.<\/div>\n<div role=\"group\" aria-label=\"Message shortcuts\" data-qa=\"message-actions\"><\/div>\n<div role=\"group\" aria-label=\"Message shortcuts\" data-qa=\"message-actions\">One case where things are quite though is when you&#8217;re dealing with a mix of physical quantities and black box machine learning, where the units of hyper-parameters is not clear, and the weights in the network are basically &#8220;coupling&#8221; constants, trying to create relations between unrelated physical quantities (reinventing the laws of physics through chained linear relations.) But hopefully this is only a fraction of your work.<\/div>\n<h3 role=\"group\" aria-label=\"Message shortcuts\" data-qa=\"message-actions\">Plots without axes are meaningless<\/h3>\n<div role=\"group\" aria-label=\"Message shortcuts\" data-qa=\"message-actions\">The corollary of quantities without units is that you end up with graphs without units (if you made it until there knowing what you plot in the end&#8230; congratulations!) And a graph without units is pretty much meaningless, unless you make all the measurement relative.<\/div>\n<div role=\"group\" aria-label=\"Message shortcuts\" data-qa=\"message-actions\"><\/div>\n<div role=\"group\" aria-label=\"Message shortcuts\" data-qa=\"message-actions\">Please use units, wear a mask, stay safe.<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Whenever I see scientific code without units I scream inside my heart.Everytime you write simulations of physical phenomena (often using numpy or matlab), make sure to always have variables where the units are clear, e.g. : lambda_m = 633e-9 #wavelentgh in meters c_mps = 3e8 #speed of light in meters per seconds freq_Hz = c_mps\/lambda_mFailure [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,13,14,16,9],"tags":[],"class_list":["post-2700","post","type-post","status-publish","format-standard","hentry","category-english","category-epistemology","category-projects","category-resources","category-science"],"_links":{"self":[{"href":"http:\/\/antoine.wojdyla.fr\/blog\/wp-json\/wp\/v2\/posts\/2700","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/antoine.wojdyla.fr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/antoine.wojdyla.fr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/antoine.wojdyla.fr\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/antoine.wojdyla.fr\/blog\/wp-json\/wp\/v2\/comments?post=2700"}],"version-history":[{"count":6,"href":"http:\/\/antoine.wojdyla.fr\/blog\/wp-json\/wp\/v2\/posts\/2700\/revisions"}],"predecessor-version":[{"id":3030,"href":"http:\/\/antoine.wojdyla.fr\/blog\/wp-json\/wp\/v2\/posts\/2700\/revisions\/3030"}],"wp:attachment":[{"href":"http:\/\/antoine.wojdyla.fr\/blog\/wp-json\/wp\/v2\/media?parent=2700"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/antoine.wojdyla.fr\/blog\/wp-json\/wp\/v2\/categories?post=2700"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/antoine.wojdyla.fr\/blog\/wp-json\/wp\/v2\/tags?post=2700"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}