jrjohansson / scientific-python-lectures Goto Github PK
View Code? Open in Web Editor NEWLectures on scientific computing with python, as IPython notebooks.
Lectures on scientific computing with python, as IPython notebooks.
Something like:
import collections
from collections import OrderedDict
_Thing = collections.namedtuple('_Thing', ('attr1', 'attr2'))
class Thing(_Thing):
pass
values = [
True,
0b01,
0x42,
1e42,
42,
42.0,
"str",
[1,2,3],
(1,2,3,), # tuples are immutable (but their references are not)
{1,2,3},
{'one':1,'two':2},
OrderedDict([('one',1), ('two', 2)]),
Thing(attr1=1, attr2=2),
(x for x in range(4)),
[x for x in range(2)],
]
print("# type, obj, iter(obj), list(iter(obj))")
for obj in values:
output = (type(obj), obj, iter(obj), list(iter(obj))) if hasattr(obj, '__iter__') else (obj,)
print(obj)
# ...
# * https://docs.python.org/3/whatsnew/2.7.html#python-3-1-features
And then something about tablib, dataset, pandas etc for reading/writing actual safe CSV.
Hi,
I was quite surprised with y = bool(z.real)
and y = bool(z.imag)
at https://github.com/jrjohansson/scientific-python-lectures/blame/master/Lecture-1-Introduction-to-Python-Programming.ipynb#L994
While it would be ok for integers and it works as shown, it is useless as exact zero is quite uncommon as a result of a computation.
As well, complex to boolean conversion works quite well as it well fits into a scheme x != 0
.
>>> bool(1j)
True
>>> bool(1+0j)
True
>>> bool(0j)
False
It currently says:
Python 3 will eventually supercede Python 2, but it is not backward-compatible with Python 2. A lot of existing python code and packages has been written for Python 2, and it is still the most wide-spread version. For these lectures either version will be fine, but it is probably easier to stick with Python 2 for now, because it is more readily available via prebuilt packages and binary installers.
I suggest updating this to
Python 2 was officially discontinued in 2020. Python 3 has superseded Python 2 in most areas, although it is not backward-compatible with Python 2. A lot of existing python code and packages that has been written for Python 2 has now been ported to Python 3. For these lectures either version will be fine, but it is probably easier to continue with Python 3.
UNIX system commands that show up in scientific-python-lectures and their Windows equivalents
PREFACE:
The Windows "cmd" window is of limited utility. Powershell is a windows shell utility with a greater scripting language. To use Powershell from python as a magic, you can download and use powershellmagic
https://pypi.python.org/pypi/powershellmagic/0.1.0
Install as a Python package:
$ pip install powershellmagic
Use in IPython Notebooks
In [1]: %load_ext powershellmagic
UNIX
;
WINDOWS
cat scripts/hello-world.py
;
!type scripts/hello-world.py #for cmd window
or
%%powershell
get-content scripts/hello-world.py
!head stockholm_td_adj.dat #There is no cmd window equivalent, !type will print the entire file
;
%%powershell
gc stockholm_td_adj.dat -TotalCount 3
!file random-matrix.npy
;
!assoc random-matrix.npy #Not really equivalent to !file
!rm -f dprod.pyf
;
%% powershell
!rm -f dprod.pyf
When I open Lecture 2 notebook as explained in the README on my Firefox browser, I get unresponsive script and I can't run any of the cells.
Do you have a license in mind for these? If not, can I recommend some kind of liberal Creative Commons license like CC-BY? These look like the sort of thing we'd very much like to consider using in Software Carpentry.
I think there may be a typo in Example 4 (actually 'Example' is also a typo here, says 'Exmaple'). In the mpi-psum.py code block, I'm guessing the final line should be:
print "sum =", s, ", numpy sum =", a.sum()
where a.sum() replaced s.sum(). This doesn't cause errors, but does result in fairly silly code.
First, let me say that I have greatly benefited from these lectures. The ability to build a PDF with a LaTeX template is an added bonus. I'm using the latest Anaconda Python and I would like to use your Makefile to build PDF's. I noticed others posted a similar desire previously. I'm posting this as a new issue because I have a specific error message and was hoping you might have encountered this using the latest jupyter notebook. I also assume you will also want to migrate to this current tool chain, if you have not already.
I am using the following syntax in the latexfiles
section of the Makefile
jupyter nbconvert --to latex --template $(TEMPLATE) filename.ipynb
This fails with an error in pygments
File "C:\Apps\Anaconda3\lib\site-packages\pygments\util.py", line 309, in guess_decode
text = text.decode('utf-8')
jinja2.exceptions.UndefinedError: 'nbformat.notebooknode.NotebookNode object' has no attribute 'input'
I should also note that my small test notebooks download to PDF individually through the browser. I just can't get the template and Makefile to work.
Thank you.
I installed mencoder and ffmpge using Homebrew, but the error still exists.
TypeError: 'NoneType' object is not iterable
Then I run the code on Spyder.
ValueError: Cannot save animation: no writers are available. Please install mencoder or ffmpeg to save animations.
Hi,
I'm hoping to be able to use your notebooks as a primer for a medical physics programming workshop. However at this point in time if someone is starting out they should begin with Python 3.
Are there any plans to port to Python 3? I would be willing to help.
Cheers,
Simon
Seems that fortran code in this lecture is written in fortran 77 fixed format, but f2py supports f90 as well, then why not use this modern format with good readability?
Typo in:
The legend function takes an optional keywork argument loc that can be used to specify where in the figure the legend is to be drawn.
keywork
should be keyword
any reason for the capitalized function, Theta, in lecture 2? just curious if I'm missing anything specific to vectorizing ufuncs in numpy
Spotted it at nbviewer [1] - chose
should be choose
.
When discussing lists, I think it is imperative to mention that lists are modified in the original space.
Also, when assigning one list to another, mentioning that modifications to the second list is reflected in the first list would be helpful.
l=[]
l.append('a')
print(l)
a=l
a.append('b')
print(l)
Hi,
Fantastic resource!
One small thing: in Lecture-4-Matplotlib.ipynb, you show pylab and suggest to use --pylab inline
when starting the notebook. The use of pylab is not recommended anymore, see http://carreau.github.io/posts/10-No-PyLab-Thanks.ipynb.html.
Best,
Lex Nederbragt
There's sth wrong with my nbconvert on windows platform, it would be more convenient for us to read pdf on pad.
Hello!
The phrase "of the" occurs twice in the sentence:
"One of the of the key features of matplotlib that I would like to emphasize, and that I think makes matplotlib highly suitable for generating figures for scientific publications is that all aspects of the figure can be controlled programmatically."
In the "Introduction" section.
Great notes!
For those of us coming from Matlab trying to learn numpy, i.e. lecture 2.
Everything seems to be easy for vectors and 2D arrays. However, an example with 3D shows a big difference compared to Matlab, which maybe should be highlighted (as the normal 0-indexing).
Say we construct the following 3D array
m = np.arange(8).reshape(2,2,2)
Asking a Matlab person to get the first items (upper left) from the 2 dimensions would result in something like (knowing that indexing starts at 0...)
m[0,0,0] #first item in the first dimension
m[0,0,1] #first item in the second dimension
Expecting the numbers 0 and 4. But the result from the above is 0 and 1, respectively. Quite confusing for someone used to Matlab arrays.
Some clarification on accessing elements i higher dimensional arrays would be of great value.
Disclaimer.
I am sorry if this is not the right location for such a request.
Context.
The material is very useful and informative, and I especially like the way you generate a pdf document of all the content. As someone starting out with python, git and make, creating this type of pdf shows another great benefit of this type of work flow.
Issue.
At some point would it be possible to add some documentation to the make file. I sort of follow the code, but some additional documentation would help users of the code possibly be able to use a similar approach on their own projects.
Cheers,
Marty
First, your notes are very good!
Since some releases ago matplotlib has supported pgf for LaTeX output. This allows to graphs that are compiled into to pdf graphics by the LaTeX pgf package. If compilation time isn't an issue (caching is possible) it may be preferable to pdf output, as font sizes and fonts are handled by LaTeX.
Perhaps this should be reflected in the "Formatting text: LaTeX, fontsize, font family" in the matplotlib slides. Using pgf does in my experience allow for less fiddling when producing plots.
I'm running your HPC notebook on my machine (2011 MacBook Air) and notice that your first example of parallelization on IPython fails. Specifically, the non-load balanced mapping of dummy_task
:
result = dummy_task.map(delay_times)
leads to the following error, which is confusing since you are calling any()
at the point of failure. Note that the load-balanced example works without issue.
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-32-002199129e98> in <module>()
----> 1 result = dummy_task.map(delay_times)
2 visualize_tasks(result)
/Library/Python/2.7/site-packages/ipython-1.0.0_dev-py2.7.egg/IPython/parallel/client/remotefunction.pyc in map(self, *sequences)
275 self._mapping = True
276 try:
--> 277 ret = self(*sequences)
278 finally:
279 self._mapping = False
/Library/Python/2.7/site-packages/ipython-1.0.0_dev-py2.7.egg/IPython/parallel/client/remotefunction.pyc in __call__(self, *sequences)
/Library/Python/2.7/site-packages/ipython-1.0.0_dev-py2.7.egg/IPython/parallel/client/remotefunction.pyc in sync_view_results(f, self, *args, **kwargs)
93 view._in_sync_results = True
94 try:
---> 95 ret = f(self, *args, **kwargs)
96 finally:
97 view._in_sync_results = False
/Library/Python/2.7/site-packages/ipython-1.0.0_dev-py2.7.egg/IPython/parallel/client/remotefunction.pyc in __call__(self, *sequences)
233 part = self.mapObject.getPartition(seq, index, nparts, maxlen)
234 args.append(part)
--> 235 if not any(args):
236 continue
237
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
in sync results <function __call__ at 0x10fe415f0>
$ python2 setup.py build_ext --inplace
running build_ext
skipping 'cy_dcumsum.c' Cython extension (up-to-date)
building 'cy_dcumsum' extension
creating build/temp.macosx-10.12-x86_64-2.7
clang -fno-strict-aliasing -fno-common -dynamic -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/usr/local/include -I/usr/local/opt/openssl/include -I/usr/local/opt/sqlite/include -I/usr/local/Cellar/python/2.7.13_1/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c cy_dcumsum.c -o build/temp.macosx-10.12-x86_64-2.7/cy_dcumsum.o
cy_dcumsum.c:517:10: fatal error: 'numpy/arrayobject.h' file not found
#include "numpy/arrayobject.h"
^
1 error generated.
error: command 'clang' failed with exit status 1
#####################################################################
I googled this compilation issue online and found a workaround by inserting the "include_dirs = [np.get_include()]" inside the Extension clause of the setup.py file.
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
import numpy as np
setup(
cmdclass = {'build_ext': build_ext},
ext_modules = [Extension("cy_dcumsum", ["cy_dcumsum.pyx"], include_dirs = [np.get_include()])]
)
The standard character encoding in Lecture 1 is listed as ASCII. This is now UTF-8 in Python 3. Might want to make a note of it in the notebook. I'd be happy to submit a PR if you'd like.
Great tutorial, but in Lession 1 a few times you mention "Expection" instead of "Exception".
I expect it should be the latter... ;-)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.