psyc410_s2x:sci_prog
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
psyc410_s2x:sci_prog [2025/01/09 18:13] – [LAB REPORT Part 2] admin | psyc410_s2x:sci_prog [2025/01/09 20:18] (current) – removed admin | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | /* | ||
- | ~~ODT~~ | ||
- | ~~SLIDESHOW~~ | ||
- | */ | ||
- | |||
- | |||
- | <WRAP centeralign> | ||
- | <typo ff:' | ||
- | Lab 1 - Scientific Computing | ||
- | </ | ||
- | </ | ||
- | |||
- | ====== Information, | ||
- | |||
- | ===== Readings to be completed prior to this lab: ===== | ||
- | * none | ||
- | |||
- | ===== Readings to be completed prior to next week's lab: ===== | ||
- | * none | ||
- | |||
- | |||
- | ===== Goals for this lab: ==== | ||
- | * Setup your computer for the semester | ||
- | * Learn how to take [[https:// | ||
- | * Introduce command line control of your computer. | ||
- | * Meet the '' | ||
- | * Put it in your dock | ||
- | * Customize it | ||
- | * Learn basic commands and terminology for using the '' | ||
- | * Introduce the MATLAB programming environment. | ||
- | * Introduce basic MATLAB programs and programming syntax. | ||
- | |||
- | |||
- | ===== Software introduced in this lab ===== | ||
- | * [[psyc410: | ||
- | * [[psyc410: | ||
- | * [[https:// | ||
- | |||
- | |||
- | ===== Laboratory Report ===== | ||
- | <WRAP center round important 100%> | ||
- | <WRAP centeralign>< | ||
- | * Throughout this (and all) lab exercise pages you will find instructions for your lab reports within these boxes. | ||
- | </ | ||
- | |||
- | ===== Housekeeping ===== | ||
- | Let's make sure the software we'll use most often is easily available by putting shortcuts in our dock (the row of applications at the bottom of your screen). For now, don't worry about knowing what these programs are or what they do. | ||
- | |||
- | **1.** Confirm that you have the icons for '' | ||
- | {{: | ||
- | |||
- | **2.** Put a shortcut to the '' | ||
- | <typo sp: | ||
- | {{ : | ||
- | <typo sp: | ||
- | |||
- | ===== A Note on Scientific Computing ===== | ||
- | |||
- | < | ||
- | \\ | ||
- | \\ | ||
- | Cohen, M. X. (2014). Analyzing Neural Time Series Data: Theory and practice. Cambridge, Massachusetts: | ||
- | </ | ||
- | |||
- | Scientific computing is often a frustrating uphill slog punctuated by moments of epiphany and success. It's sort of like fighting a losing war, just hoping to win some battles along the way. This is particularly true for beginners, but can also be true for intermediate and advanced users because, regardless of skill level, we are all generally operating at the edge of our knowledge and pushing the boundaries of what we can achieve. I am sharing this perspective (admittedly, | ||
- | |||
- | <WRAP center round alert 80%> | ||
- | The lab wiki for this course will contain lots of information and many step-by-step tutorials, but you will invariably make mistakes and/or run into problems such as a crashing program or script. **I expect you to attempt to solve these issues yourself before coming to me for help.** | ||
- | </ | ||
- | |||
- | To that end, [[psyc410: | ||
- | |||
- | ====== Part 1: OS X Terminal ====== | ||
- | <WRAP center round info 90%> | ||
- | EVERY Apple computer running OS X is built on the Unix platform and comes with the Terminal app pre-installed. | ||
- | </ | ||
- | |||
- | |||
- | The Mac operating system is a Unix-based graphical user interface. You can therefore interact with your Mac computer using many (most) of the same commands used in Unix/Linux systems. Many of these commands achieve the same things that you can achieve using 'point and click' methods. | ||
- | |||
- | We " | ||
- | |||
- | |||
- | Most things we want the computer to do can be achieved several ways. For example, let's say we want to create a new directory on our desktop. | ||
- | |||
- | <WRAP center round info 80%> | ||
- | You're probably familiar with referring to " | ||
- | </ | ||
- | |||
- | On our Mac, we can create a new folder on our desktop by clicking on the desktop, selecting '' | ||
- | |||
- | But you can also create a new folder from the command-line. Let's give it a shot. | ||
- | |||
- | ===== Using Terminal ===== | ||
- | |||
- | **1.** Open '' | ||
- | {{ : | ||
- | |||
- | <WRAP center round tip 100%> | ||
- | You can easily copy my code by clicking the little clipboard that appears in the upper-right when you hover over a code box. \\ | ||
- | {{: | ||
- | </ | ||
- | |||
- | **2.** Type the following command and then press '' | ||
- | |||
- | <code zsh> | ||
- | cd ~/Desktop | ||
- | </ | ||
- | |||
- | **3.** Type the following command and then press '' | ||
- | |||
- | <code zsh> | ||
- | mkdir ./ | ||
- | </ | ||
- | |||
- | Voila! You should see a new folder on your Desktop named " | ||
- | * '' | ||
- | * '' | ||
- | * '' | ||
- | * '' | ||
- | |||
- | ===== Why use Terminal?? ===== | ||
- | |||
- | At this point you are undoubtedly thinking "It was way easier to just use the mouse/ | ||
- | * the command-line gives you direct control over many aspects of your computer that would be hard (or impossible) to access via the graphical interface. | ||
- | * we can write our commands into a text file (called a " | ||
- | * if we need to run the task 100 times (e.g., we want to create 100 new folders), we can just tell the computer to read the script 100 times. That sounds a lot easier than right-clicking and selecting '' | ||
- | * among other great things, this ensures that whatever task we've " | ||
- | |||
- | **The majority of what we do in this course will rely on controlling your computer using the command-line (either in '' | ||
- | |||
- | Let's get started learning a bit about the Terminal! | ||
- | ===== Setup your Terminal ===== | ||
- | |||
- | |||
- | ==== Trick it out ==== | ||
- | |||
- | If you're so inclined, you can change the way your '' | ||
- | |||
- | ===== File paths ===== | ||
- | File paths are simply the directions to a particular file or directory on your computer. You already have an intuitive understanding of these paths, though you might not have ever referred to them as such. Most likely, you think of your file system in terms of " | ||
- | |||
- | <WRAP center round info 80%> | ||
- | When working with the command line it is more common to refer to " | ||
- | </ | ||
- | |||
- | **1.** Open your Mac '' | ||
- | {{ : | ||
- | |||
- | **2.** Navigate to your '' | ||
- | |||
- | **3.** To see the filepath for this location select '' | ||
- | |||
- | {{ : | ||
- | |||
- | You should now see a list of folders displayed at the bottom of the window. This is the filepath! If we wanted to use the command line to refer to this location we would type ''/ | ||
- | |||
- | {{ : | ||
- | |||
- | **4.** Let's try getting to that same location using the command line. Open your '' | ||
- | {{ : | ||
- | |||
- | **5.** In the terminal window type the following command followed by '' | ||
- | |||
- | <code bash> | ||
- | pwd | ||
- | </ | ||
- | |||
- | '' | ||
- | |||
- | <WRAP center round tip 90%> | ||
- | The tilda character '' | ||
- | </ | ||
- | |||
- | |||
- | **6.** Before we leave this location, let's see what directories and files are in our current location. | ||
- | |||
- | <code bash> | ||
- | ls | ||
- | </ | ||
- | |||
- | '' | ||
- | |||
- | **7.** So we know where we are and what's in this directory, but now we need to get into the '' | ||
- | |||
- | <code bash> | ||
- | cd / | ||
- | </ | ||
- | |||
- | **8.** Finally, make sure that you've successfully navigated to the '' | ||
- | ==== Absolute and Relative Paths ==== | ||
- | |||
- | An **absolute path** is a full set of instructions. It tells the computer how to get someplace starting from the very beginning (i.e., the top of your directory hierarchy). Let's say your parents were coming to campus and planned to meet you in the Crawford Computer lab, but wanted to check out the sculpture in the science quad along the way. You would give them directions from their house to the science quad. | ||
- | |||
- | So far, so good. But what directions would you give them to get from the science quad to the computer lab? You could give them directions from their home to the computer lab. This is similar to using an absolute path. You ignore the current location and it's proximity to the desired location and simply give the entire path. | ||
- | |||
- | However, I doubt that any of you would actually do this. Instead, you would tell your parents //" | ||
- | \\ | ||
- | \\ | ||
- | When we are using the command line to navigate our directory hierarchy '' | ||
- | |||
- | <code bash> | ||
- | cd .. | ||
- | </ | ||
- | |||
- | Correct! It would move us up one level in our directory hierarchy. If we were in ''/ | ||
- | |||
- | And if we wanted to move up two levels we could use | ||
- | |||
- | <code bash> | ||
- | cd ../.. | ||
- | </ | ||
- | |||
- | If we were in ''/ | ||
- | |||
- | <WRAP center round tip 80%> | ||
- | The ''/'' | ||
- | |||
- | You can think of it like the comma in: Gambier, OH, United States of America | ||
- | </ | ||
- | |||
- | |||
- | So now let's think about this in terms of relative paths. | ||
- | |||
- | **9.** Let's first go back to our home directory using what we've learned. We can type | ||
- | |||
- | <code zsh> | ||
- | cd /Users/hnl | ||
- | </ | ||
- | |||
- | or simply type | ||
- | |||
- | <code zsh> | ||
- | cd | ||
- | </ | ||
- | |||
- | or simply type | ||
- | |||
- | <code zsh> | ||
- | cd ~ | ||
- | </ | ||
- | |||
- | |||
- | <WRAP center round tip 80%> | ||
- | From the computer' | ||
- | No matter where you are in the directory hierarchy, typing '' | ||
- | |||
- | |||
- | **10.** We know we can get to the Downloads folder from our current location by using the absolute path and typing '' | ||
- | |||
- | <WRAP center round info 80%> | ||
- | When using the terminal, anything that comes after a hashtag ''#'' | ||
- | |||
- | You should read (and write) these annotations as they describe what is being done by the code. You should always strive to annotate your code so that 1) it's easily understood by yourself or other humans and 2) so that at a later date you can understand what you were trying to do. I promise you, you will not remember! | ||
- | |||
- | **NOTE:** If you copy and past the entire block of code into your terminal, everything will execute properly but you might also get lots of " | ||
- | </ | ||
- | |||
- | |||
- | If we do not specify a path, the computer will assume we are starting from wherever we currently reside. | ||
- | Therefore, the following command will work because computer assumes we are starting from our current location, which happens to be ''/ | ||
- | <code zsh> | ||
- | # change to the Downloads directory | ||
- | cd Downloads | ||
- | </ | ||
- | |||
- | The following command would have worked exactly the same, but it is more explicit that we are starting from our current location. Whereas '' | ||
- | |||
- | <code zsh> | ||
- | # Change to the Downloads directory | ||
- | cd ./Downloads | ||
- | </ | ||
- | |||
- | <WRAP center round help 80%> | ||
- | After typing '' | ||
- | |||
- | <code zsh> | ||
- | cd ~ | ||
- | cd Downloads | ||
- | cd ./Downloads | ||
- | </ | ||
- | |||
- | Oops, it didn't like that. Why not? If it's not clear to you why not, try running each of the three lines of code one at a time, and after you run each line, run '' | ||
- | </ | ||
- | |||
- | |||
- | **11.** So now we are in ''/ | ||
- | |||
- | <code bash> | ||
- | # Absolute path | ||
- | cd / | ||
- | |||
- | # Relative path | ||
- | # The Documents directory is parallel to the Downloads directory in our directory hierarchy. So to get there, we | ||
- | # need to go up one level, then down into Documents. | ||
- | cd ../ | ||
- | </ | ||
- | |||
- | <WRAP center round info 80%> | ||
- | On the one hand, it tends to be better to use absolute paths when you're writing scripts or programs because it ensures you're going where you think you're going and makes your code more robust (it should work no matter your starting point). On the other hand, it's better to use relative paths when you're working interactively (as we've been doing) because it spares you from typing out the full path. | ||
- | </ | ||
- | ===== File & Directory Control ===== | ||
- | |||
- | You've already learned a couple of important terminal commands; '' | ||
- | \\ | ||
- | \\ | ||
- | Over the next series of steps we'll create a new folder and some new files and then play with moving, copying, renaming, and deleting them. | ||
- | |||
- | **1.** To create a new directory we use the '' | ||
- | |||
- | <code bash> | ||
- | mkdir / | ||
- | </ | ||
- | |||
- | You should now see a folder named foo on your Mac Desktop. Kinda cool, no? | ||
- | |||
- | <WRAP center round tip 80%> | ||
- | Terminal has an auto-complete feature that can save you some typing. If you had typed '' | ||
- | |||
- | However, if you typed '' | ||
- | </ | ||
- | |||
- | |||
- | **2.** Delete the folder you just created. To do so we'll use the '' | ||
- | |||
- | <code zsh> | ||
- | rmdir / | ||
- | </ | ||
- | |||
- | Voila. Keep in mind that '' | ||
- | |||
- | <WRAP center round alert 80%> | ||
- | Be. Very. **VERY**. Careful. When you use the command line to remove files and directories they are gone FOREVER. They do not go into a Trash bin from where they can be recovered. They are truly deleted. | ||
- | </ | ||
- | |||
- | < | ||
- | I'm using absolute paths in these examples, but if you're feeling adventurous you should use relative paths instead. | ||
- | </ | ||
- | |||
- | **3.** Now we'll recreate our " | ||
- | |||
- | <code zsh> | ||
- | mkdir / | ||
- | </ | ||
- | |||
- | Oops! That doesn' | ||
- | |||
- | <code zsh> | ||
- | mkdir -p / | ||
- | </ | ||
- | |||
- | Success! The '' | ||
- | |||
- | <WRAP center round info 80%> | ||
- | Almost all commands (aka " | ||
- | </ | ||
- | |||
- | |||
- | **4.** Now that I think about it, " | ||
- | |||
- | <code bash> | ||
- | mv / | ||
- | </ | ||
- | |||
- | **5.** Now we'll create some files inside of our directories using the '' | ||
- | |||
- | <WRAP center round tip 80%> | ||
- | Instead of typing out the commands, you can just copy and paste them into your terminal. However, make sure you understand what each line is doing! | ||
- | </ | ||
- | |||
- | <code bash> | ||
- | touch / | ||
- | touch / | ||
- | touch / | ||
- | </ | ||
- | |||
- | **6.** We've already seen '' | ||
- | |||
- | <code bash> | ||
- | mv / | ||
- | </ | ||
- | |||
- | **7.** Now all three files are in the babyfoo directory. D'oh! I actually need to have file3.doc in both directories. So let's use the '' | ||
- | |||
- | <code bash> | ||
- | cp / | ||
- | </ | ||
- | |||
- | **8.** Actually, it'd be best if there were copies of all three files in both directories. We could use the '' | ||
- | |||
- | <code bash> | ||
- | cp / | ||
- | </ | ||
- | |||
- | **9.** Okay okay okay, I now realize that I don't need the file3.doc file in the foo directory after all. We've already seen that '' | ||
- | |||
- | <code bash> | ||
- | rm / | ||
- | </ | ||
- | |||
- | ===== Scripting ===== | ||
- | |||
- | So far we've used the command line // | ||
- | |||
- | <WRAP center round info 80%> | ||
- | There are lots of different shells (e.g. '' | ||
- | |||
- | '' | ||
- | |||
- | I am hoping to update my code to '' | ||
- | </ | ||
- | |||
- | In the last section we created, renamed, moved, copied, and deleted some files and directories in a long series of interactive steps. The script below would accomplish the same thing without us needing to write each command interactively. Instead the computer would simply read our list of commands and carry out each one in order. | ||
- | |||
- | **1.** Delete the " | ||
- | |||
- | **2.** Download the following script to your computer by clicking on the tab that reads " | ||
- | |||
- | <code bash myFirstScript.sh> | ||
- | #!/bin/zsh | ||
- | |||
- | # create the foo directory | ||
- | mkdir / | ||
- | |||
- | # remove the directory we just created | ||
- | rmdir / | ||
- | |||
- | # Do not run this next line of code | ||
- | # mkdir / | ||
- | |||
- | # make the foo directory and subfoo sub-directory | ||
- | mkdir -p / | ||
- | |||
- | # rename subfoo to babyfoo | ||
- | mv / | ||
- | |||
- | # create two text files in the babyfoo directory | ||
- | touch / | ||
- | touch / | ||
- | |||
- | # create on Word doc in the foo directory | ||
- | touch / | ||
- | |||
- | # move the Word doc from the foo to the babyfoo directory | ||
- | mv / | ||
- | |||
- | # copy the Word doc in the babyfoo directory into the foo directory | ||
- | cp / | ||
- | |||
- | # delete the Word doc from the foo directory | ||
- | rm / | ||
- | </ | ||
- | |||
- | |||
- | **3.** Navigate to your '' | ||
- | |||
- | <code bash> | ||
- | source ./ | ||
- | </ | ||
- | |||
- | This should have carried out all of the steps we previously did interactively. Obviously, there' | ||
- | |||
- | <WRAP center round info 80%> | ||
- | You can think of '' | ||
- | </ | ||
- | |||
- | <WRAP center round info 80%> | ||
- | // | ||
- | |||
- | The line can get kinda blurry, but basically scripting tells the computer //what// to do, whereas programming tells the computer //how// to do it. Almost everything we do in this course will be on the scripting side of things. | ||
- | \\ | ||
- | \\ | ||
- | **Disclaimer: | ||
- | </ | ||
- | |||
- | ==== Editing Shell Scripts ==== | ||
- | |||
- | <WRAP center round todo 100%> | ||
- | You should use BBEdit whenever you write or edit shell scripts. <fc # | ||
- | |||
- | To make things easier, let's make sure that your computer | ||
- | * create a .sh file on your Desktop. | ||
- | * right-click on the file and select '' | ||
- | * In the dropdown menu under '' | ||
- | * Press the '' | ||
- | |||
- | |||
- | Now whenever you double click on an '' | ||
- | |||
- | </ | ||
- | |||
- | ===== LAB REPORT Part 1 - #1 ===== | ||
- | <WRAP center round important 100%> | ||
- | |||
- | <WRAP centeralign> | ||
- | <typo fs:x-large; fc:purple; fw:bold; text-shadow: | ||
- | LAB REPORT Part 1 - #1 | ||
- | </ | ||
- | </ | ||
- | |||
- | *Write a script that will carry out the following steps. | ||
- | *Annotate each line of your code with a description of what that line will accomplish (you can simply use each of the instructions below as your annotations). You can use the example script above as reference. | ||
- | |||
- | - Use an absolute path to navigate into your '' | ||
- | - Print (i.e., display on the command line) your current working directory | ||
- | - Use a relative path to navigate into your '' | ||
- | - Create a text file named '' | ||
- | - Use a relative path to navigate back to your home directory | ||
- | - From your home directory, list the contents of your '' | ||
- | - Use an absolute path to navigate back to your " | ||
- | - Delete the lab1.txt file (be careful to only delete the '' | ||
- | |||
- | * //Hint: Don't forget to put the code that tells the computer to use the zsh shell at the top of your script.// | ||
- | |||
- | * Save the file as '' | ||
- | |||
- | |||
- | </ | ||
- | |||
- | |||
- | ====== Part 2: MATLAB ====== | ||
- | |||
- | [[http:// | ||
- | |||
- | <WRAP center round tip 80%> | ||
- | Kenyon has a MATLAB license, which means you can install it on your personal computer for free! | ||
- | </ | ||
- | |||
- | ===== MATLAB Introduction ===== | ||
- | To open MATLAB click on the icon in your dock. {{psyc410: | ||
- | |||
- | <WRAP center round info 80%> | ||
- | You will need to authorize MATLAB using your Kenyon email and credentials. | ||
- | * The first pop-up window requires your full Kenyon email address. | ||
- | * The second pop-up requires you to put in your Kenyon username and password. | ||
- | </ | ||
- | |||
- | The window that opens will look like the one below (without the shaded boxes). Note that there are several sub-windows. | ||
- | * The < | ||
- | * The < | ||
- | * The < | ||
- | * All of the defined variables are shown in this window (it's empty because we haven' | ||
- | |||
- | Some of the path navigation commands you learned in the Terminal will also work in the MATLAB command window. Try using '' | ||
- | |||
- | <WRAP center round tip 80%> | ||
- | We can have MATLAB run shell commands by putting '' | ||
- | </ | ||
- | |||
- | <code matlab> | ||
- | % The code below will not work because the Terminal file management commands don't work in MATLAB | ||
- | touch / | ||
- | |||
- | % The code below will work because putting the ! at the start of the command tells MATLAB | ||
- | % to use the bash shell (i.e. Terminal) to carry out the command rather than MATLAB | ||
- | ! touch / | ||
- | </ | ||
- | |||
- | <WRAP center round tip 80%> | ||
- | We learned that putting ''#'' | ||
- | |||
- | # This text would be invisible to the shell, but not Matlab. | ||
- | |||
- | % This text would be invisible to Matlab, but not to the shell. | ||
- | </ | ||
- | |||
- | ===== Using MATLAB ===== | ||
- | |||
- | As with Unix/ | ||
- | |||
- | **1.** Go to [[http:// | ||
- | |||
- | **2.** Select the < | ||
- | |||
- | **3.** Complete the < | ||
- | * These two are the only modules you are // | ||
- | |||
- | **4.** When you've completed both modules take a screenshot of your progress. | ||
- | * click the hamburger icon (blue box with three blue stripes) in the upper left corner. | ||
- | * capture the entire screen or, at least, the portion that shows your progress and your name. (see example below) | ||
- | * If you've already closed the window, you can just log back into the course to access your progress. | ||
- | |||
- | {{ psyc410: | ||
- | |||
- | |||
- | <WRAP center round tip 90%> | ||
- | <WRAP centeralign>< | ||
- | You will need to take many screenshots throughout the semester. You should spend a few minutes learning the various [[https:// | ||
- | |||
- | '' | ||
- | '' | ||
- | |||
- | //Tip:// To ensure the highest quality images, it's generally better to select the relevant portion of your screen, rather than capturing the entire screen and cropping it down. | ||
- | </ | ||
- | |||
- | |||
- | **5.** After completing the two tutorial modules, please watch these two videos. | ||
- | |||
- | |||
- | |||
- | |||
- | |||
- | /* | ||
- | <WRAP center round tip 80%> | ||
- | I usually watch youtube videos sped up in order to save some time. If you want to do the same, then after you begin playback, select the little gear icon in the bottom right corner of the youtube player, select the '' | ||
- | \\ | ||
- | \\ | ||
- | // | ||
- | </ | ||
- | */ | ||
- | |||
- | {{ youtube> | ||
- | \\ | ||
- | \\ | ||
- | {{ youtube> | ||
- | \\ | ||
- | \\ | ||
- | |||
- | ===== LAB REPORT Part 2 ===== | ||
- | <WRAP center round important 100%> | ||
- | <WRAP centeralign> | ||
- | <typo fs:x-large; fc:purple; fw:bold; text-shadow: | ||
- | LAB REPORT Part 2 - #1 | ||
- | </ | ||
- | </ | ||
- | |||
- | **1.** Share the screenshot of your progress in the < | ||
- | |||
- | Write MATLAB code that would | ||
- | |||
- | **2.** create an array named " | ||
- | |||
- | **3.** create a 2x2 matrix named " | ||
- | |||
- | **4.** find the size of " | ||
- | |||
- | **5.** assign the element that is in the first row and second column of " | ||
- | |||
- | |||
- | </ | ||
psyc410_s2x/sci_prog.1736464387.txt.gz · Last modified: 2025/01/09 18:13 by admin