diff options
Diffstat (limited to 'loading-data-from-files')
-rw-r--r-- | loading-data-from-files/quickref.tex | 8 | ||||
-rw-r--r-- | loading-data-from-files/script.rst | 146 | ||||
-rw-r--r-- | loading-data-from-files/slides.tex | 106 |
3 files changed, 260 insertions, 0 deletions
diff --git a/loading-data-from-files/quickref.tex b/loading-data-from-files/quickref.tex new file mode 100644 index 0000000..b26d168 --- /dev/null +++ b/loading-data-from-files/quickref.tex @@ -0,0 +1,8 @@ +Creating a linear array:\\ +{\ex \lstinline| x = linspace(0, 2*pi, 50)|} + +Plotting two variables:\\ +{\ex \lstinline| plot(x, sin(x))|} + +Plotting two lists of equal length x, y:\\ +{\ex \lstinline| plot(x, y)|} diff --git a/loading-data-from-files/script.rst b/loading-data-from-files/script.rst new file mode 100644 index 0000000..f67a8c3 --- /dev/null +++ b/loading-data-from-files/script.rst @@ -0,0 +1,146 @@ +======== + Script +======== + +Welcome to this tutorial on loading data from files. + +{{{ Screen shows welcome slide }}} + +We often require to plot points obtained from experimental +observations. In this tutorial we shall learn to read data from files +and save it into sequences that can later be used to plot. + +{{{ Show the outline for this tutorial }}} + +We shall use the ``loadtxt`` command to load data from files. We will +be looking at how to read a file with multiple columns of data and +load each column of data into a sequence. + +{{{ switch back to the terminal }}} + +As usual, let us start IPython, using +:: + + ipython -pylab + +Now, Let us begin with reading the file primes.txt, which contains +just a list of primes listed in a column, using the loadtxt command. +The file, in our case, is present in ``/home/fossee/primes.txt``. + +{{{ Navigate to the path in the OS, open the file and show it }}} + +.. #[punch: do we need a slide for showing the path?] + +.. We use the ``cat`` command to see the contents of this file. + +.. #[punch: should we show the cat command here? seems like a good place + to do it] :: + + cat /home/fossee/primes.txt + +.. #[Nishanth]: A problem for windows users. + Should we simply open the file and show them the data + so that we can be fine with GNU/Linux ;) and windows? + +Now let us read this list into the variable ``primes``. +:: + + primes = loadtxt('/home/fossee/primes.txt') + +``primes`` is now a sequence of primes, that was listed in the file, +``primes.txt``. + +We now type, ``print primes`` to see the sequence printed. + +We observe that all of the numbers end with a period. This is so, +because these numbers are actually read as ``floats``. We shall learn +about them, later. + +Now, let us use the ``loadtxt`` command to read a file that contains +two columns of data, ``pendulum.txt``. This file contains the length +of the pendulum in the first column and the corresponding time period +in the second. + +%%1%% Pause the video here, and use the ``cat`` command to view the +contents of this file and then resume the video. + +This is how we look at the contents of the file, ``pendulum.txt`` +:: + + cat /home/fossee/pendulum.txt + +.. #[Nishanth]: The first column is L values and second is T values + from a simle pelculum experiment. + Since you are using the variable names later in the + script. + Not necessary but can be included also. + +Let us, now, read the data into the variable ``pend``. Again, it is +assumed that the file is in ``/home/fossee/`` +:: + + pend = loadtxt('/home/fossee/pendulum.txt') + +Let us now print the variable ``pend`` and see what's in it. +:: + + print pend + +Notice that ``pend`` is not a simple sequence like ``primes``. It has +two sequences, containing both the columns of the data file. Let us +use an additional argument of the ``loadtxt`` command, to read it into +two separate, simple sequences. +:: + + L, T = loadtxt('/home/fossee/pendulum.txt', unpack=True) + +.. #[Nishanth]: It has a sequence of items in which each item contains + two values. first is l and second is t + +Let us now, print the variables L and T, to see what they contain. +:: + + print L + print T + +.. #[Nishanth]: Stress on ``unpack=True`` ?? + +Notice, that L and T now contain the first and second columns of data +from the data file, ``pendulum.txt``, and they are both simple +sequences. ``unpack=True`` has given us the two columns in to two +separate sequences instead of one complex sequence. + +{{{ show the slide with loadtxt --- other features }}} + +In this tutorial, we have learnt the basic use of the ``loadtxt`` +command, which is capable of doing a lot more than we have used it for +until now, for example + +%%2%% Pause the video here, and read the file +``pendulum_semicolon.txt`` which contains the same data as +``pendulum.txt``, but the columns are separated by semi-colons instead +of spaces. Use the IPython help to see how to do this. Once you have +finished, resume the video to look at the solution. + +{{{ switch back to the terminal }}} +:: + + L, T = loadtxt('/home/fossee/pendulum_semicolon.txt', unpack=True, delimiter=';') + + print L + + print T + +This brings us to the end of this tutorial. + +{{{ show the summary slide }}} + +You should now be able to do the following, comfortably. + + + Read data from files, containing a single column of data using the + ``loadtxt`` command. + + Read multiple columns of data, separated by spaces or other + delimiters. + +Thank you! + diff --git a/loading-data-from-files/slides.tex b/loading-data-from-files/slides.tex new file mode 100644 index 0000000..df1462c --- /dev/null +++ b/loading-data-from-files/slides.tex @@ -0,0 +1,106 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%Tutorial slides on Python. +% +% Author: FOSSEE +% Copyright (c) 2009, FOSSEE, IIT Bombay +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +\documentclass[14pt,compress]{beamer} +%\documentclass[draft]{beamer} +%\documentclass[compress,handout]{beamer} +%\usepackage{pgfpages} +%\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm] + +% Modified from: generic-ornate-15min-45min.de.tex +\mode<presentation> +{ + \usetheme{Warsaw} + \useoutertheme{infolines} + \setbeamercovered{transparent} +} + +\usepackage[english]{babel} +\usepackage[latin1]{inputenc} +%\usepackage{times} +\usepackage[T1]{fontenc} + +\usepackage{ae,aecompl} +\usepackage{mathpazo,courier,euler} +\usepackage[scaled=.95]{helvet} + +\definecolor{darkgreen}{rgb}{0,0.5,0} + +\usepackage{listings} +\lstset{language=Python, + basicstyle=\ttfamily\bfseries, + commentstyle=\color{red}\itshape, + stringstyle=\color{darkgreen}, + showstringspaces=false, + keywordstyle=\color{blue}\bfseries} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Macros +\setbeamercolor{emphbar}{bg=blue!20, fg=black} +\newcommand{\emphbar}[1] +{\begin{beamercolorbox}[rounded=true]{emphbar} + {#1} + \end{beamercolorbox} +} +\newcounter{time} +\setcounter{time}{0} +\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} + +\newcommand{\typ}[1]{\lstinline{#1}} + +\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } + +% Title page +\title{Your Title Here} + +\author[FOSSEE] {FOSSEE} + +\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} +\date{} + +% DOCUMENT STARTS +\begin{document} + +\begin{frame} + \maketitle +\end{frame} + +\begin{frame}[fragile] + \frametitle{Outline} + \begin{itemize} + \item + \end{itemize} +\end{frame} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% All other slides here. %% +%% The same slides will be used in a classroom setting. %% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +\begin{frame}[fragile] + \frametitle{Summary} + \begin{itemize} + \item + \end{itemize} +\end{frame} + +\begin{frame} + \frametitle{Thank you!} + \begin{block}{} + \begin{center} + This spoken tutorial has been produced by the + \textcolor{blue}{FOSSEE} team, which is funded by the + \end{center} + \begin{center} + \textcolor{blue}{National Mission on Education through \\ + Information \& Communication Technology \\ + MHRD, Govt. of India}. + \end{center} + \end{block} +\end{frame} + +\end{document} |