forked from swcarpentry/perl-novice-inflammation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01-pdl.html
86 lines (86 loc) · 7.21 KB
/
01-pdl.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<title>Software Carpentry: Programming with Perl</title>
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
<link rel="stylesheet" href="css/bootstrap/bootstrap.css" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="css/bootstrap/bootstrap-responsive.css" />
<link rel="stylesheet" type="text/css" href="css/swc.css" />
<link rel="alternate" type="application/rss+xml" title="Software Carpentry Blog" href="http://software-carpentry.org/feed.xml"/>
<meta charset="UTF-8" />
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<div class="banner">
<a href="http://software-carpentry.org" title="Software Carpentry">
<img alt="Software Carpentry banner" src="img/software-carpentry-banner.png" />
</a>
</div>
<div class="row-fluid">
<div class="span10 offset1">
<h1 class="title">Programming with Perl</h1>
<h2 class="subtitle">Analyzing Patient Data</h2>
<div id="learning-objectives" class="objectives">
<h2>Learning Objectives</h2>
<ul>
<li>Explain what a library is, and what libraries are used for.</li>
<li>Load a Perl library and use the things it contains.</li>
<li>Read tabular data from a file into a program.</li>
<li>Assign values to variables.</li>
<li>Select individual values and subsections from data.</li>
<li>Perform operations on arrays of data.</li>
<li>Display simple graphs.</li>
</ul>
</div>
<p>Words are useful, but what's more useful are the sentences and stories we build with them. Similarly, while a lot of powerful tools are built into languages like Perl, even more live in the <strong>libraries</strong> they are used to build.</p>
<p>In order to load our inflammation data, we need to <strong>import</strong> a library called PDL (short for Perl Data Language). In general you should use this library if you want to do fancy things with numbers, especially if you have matrices. We can load PDL using:</p>
<pre class="sourceCode perl"><code class="sourceCode perl"><span class="fu">use</span> <span class="kw">strict</span>; <span class="co"># FIXME: see <https://github.com/zmughal/perl-novice-inflammation/issues/2></span>
<span class="fu">use</span> <span class="kw">warnings</span>;
<span class="fu">use</span> PDL;</code></pre>
<p>Importing a library is like getting a piece of lab equipment out of a storage locker and setting it up on the bench. Once it's done, we can ask the library to read our data file for us:</p>
<pre class="sourceCode perl"><code class="sourceCode perl">PDL->rcols( <span class="kw">'</span><span class="st">inflammation-01.csv</span><span class="kw">'</span>, [], colsep => <span class="kw">'</span><span class="st">,</span><span class="kw">'</span> );</code></pre>
<pre class="output"><code>[
[ 0 0 … 0 0]
[ ⋮ ⋮ ⋱ ⋮ ⋮]
[ 0 0 … 2 1]
[ 0 1 … 0 0]
]</code></pre>
<p>The expression <code>rcols( ... )</code> is a <strong>function call</strong> that asks Perl to run the function <code>rcols</code> that belongs to the <code>PDL</code> library. The <strong>arrow notation</strong> is used in Perl to refer to parts of things <code>thing->component</code>.</p>
<p>The call to to <code>rcols</code> has three <strong>parameters</strong>: the name of the file we want to read, a list of the columns to read, and the <strong>delimiter</strong> that separates values on a line. These both need to be character strings (or <strong>strings</strong> for short), so we put them in quotes.</p>
<p>When we are finished typing and press <code>Enter</code> (or <code>Return</code>), the interpreter runs our command. Since we haven't told it to do anything else with the function's output, the interpreter displays it. In this case, that output is the data we just loaded.</p>
<p>Our call to <code>numpy.loadtxt</code> read our file, but didn't save the data in memory. To do that, we need to <strong>assign</strong> the array to a <strong>variable</strong>. A variable is just a name for a value, such as <code>x</code>, <code>current_temperature</code>, or <code>subject_id</code>. Python's variables must begin with a letter. We can create a new variable simply by assigning a value to it using <code>=</code>. As an illustration, let's step back and instead of considering a table of data, consider the simplest "collection" of data, a single value. The line below assigns a value to a variable:</p>
<pre class="sourceCode perl"><code class="sourceCode perl"><span class="kw">my</span> <span class="dt">$weight_kg</span> = <span class="dv">55</span>;</code></pre>
<p>Once a variable has a value, we can print it:</p>
<pre class="sourceCode perl"><code class="sourceCode perl"><span class="fu">print</span> <span class="dt">$weight_kg</span>, <span class="kw">"</span><span class="ch">\n</span><span class="kw">"</span>; <span class="co"># FIXME: should this be say()?</span></code></pre>
<pre class="output"><code>55</code></pre>
<p>and do arithmetic with it:</p>
<pre class="sourceCode perl"><code class="sourceCode perl"><span class="fu">print</span> <span class="kw">'</span><span class="st">weight in pounds: </span><span class="kw">'</span>, <span class="fl">2.2</span> <span class="kw">*</span> <span class="dt">$weight_kg</span>, <span class="kw">"</span><span class="ch">\n</span><span class="kw">"</span>; <span class="co"># FIXME: say()?</span></code></pre>
<pre class="output"><code>weight in pounds: 121.0</code></pre>
<p>We can also change a variable's value by assigning it a new one:</p>
<pre class="sourceCode perl"><code class="sourceCode perl"><span class="dt">$weight_kg</span> = <span class="fl">57.5</span>;
<span class="fu">print</span> <span class="kw">'</span><span class="st">weight in kilograms is now:</span><span class="kw">'</span>, <span class="dt">$weight_kg</span>;</code></pre>
<pre class="output"><code>weight in kilograms is now: 57.5</code></pre>
<p>FIXME note that we don't need to put a <code>my</code> since we aren't declaring it for the first time.</p>
<p>As the example above shows, we can print several things at once by separating them with commas.</p>
<p>If we imagine the variable as a sticky note with a name written on it, assignment is like putting the sticky note on a particular value:</p>
</div>
</div>
<div class="footer">
<a class="label swc-blue-bg" href="http://software-carpentry.org">Software Carpentry</a>
<a class="label swc-blue-bg" href="https://github.com/swcarpentry/lesson-template">Source</a>
<a class="label swc-blue-bg" href="mailto:admin@software-carpentry.org">Contact</a>
<a class="label swc-blue-bg" href="LICENSE.html">License</a>
</div>
</div>
<!-- Javascript placed at the end of the document so the pages load faster -->
<script src="http://software-carpentry.org/v5/js/jquery-1.9.1.min.js"></script>
<script src="http://software-carpentry.org/v5/js/bootstrap/bootstrap.min.js"></script>
</body>
</html>