Marco Fumana @ INAF-IASF Milano
The astropy package contains key functionality and common tools needed by astronomers who work with Python. It is the core of the Astropy Project, which aims to enable the community to develop a robust ecosystem of Affiliated Packages covering a broad range of needs for astronomical research, data processing, and data analysis.
In this course we will provide an introduction to Astropy and its main packages:
reproject package out the Astropy core project for images resampling
My idea is just to touch on a few topics here and there. I would like to show you the existence of tools/libraries that might be useful for you.
I don't want to convince you that Python is the best choice to explore data interactively. But for sure, Python is one of the simplest and most intuitive languages to automate repetitive operations, and create smart programs useful for your science
The course is based on examples. I have done my best to create real astronomical examples, but simplifications and approximations in the examples are necessary, given the purpose of this course.
I'm not an astronomer, please focus on example methodologies and ignore possible errors with regard to Astronomy.
Part 1
Part 2 Astropy by examples
Part 3 (bonus?)
First of all, we will see:
Keys: Python, IPython, object oriented, jupyter lab
Tab
completitionmagic commands
: %
prefix (e.g. %matplotlib qt
, ...)!
prefix$ ipython3
JupyterLab
is the next-generation web-based user interface for Project Jupyter
$ jupyter lab
Python is dynamically typed
/* C syntax: a is declared and then assigned */
int a=7;
a=7
type(a)
int
One can assigne to a variable a value of one type and then later re-assigne a value of a different type
#Type is defined by the value assigned
a=7*2.7
type(a)
float
Python is garbage-collected
) (no malloc
/alloc
required)
#Create a list without defining the size
a=[1,2,3]
type(a)
list
# Increase the size of the object
a+=[4,5]
a
[1, 2, 3, 4, 5]
Python uses whitespace indentation, rather than curly brackets or keywords, to delimit blocks.
Spaces are the preferred indentation method (see PEP8). Python 3 disallows mixing the use of tabs and spaces for indentation.
int k=0;
while (k<10){
k+=1;
printf("%d ", k);
if (k>3){
break;
}
}
k = 0
while k<10:
k+=1
print(k, end=" ")
if k>3:
break
1 2 3 4
def sgn(x):
if x > 0.0:
return 1.0
elif x < 0.0:
return -1.0
else:
raise Exception("No sign defined")
Python supports multiple programming paradigms, including procedural, object-oriented, and functional programming
We can see an object like a data structure with properties (attributes) and functions which operate on it (methods).
This isn't a definition of what object in Computer Science is, but in my opinion this is a good starting point.
A class is a blueprint or template to build a specific type of object. Every object is built from a class Class provides initial values for state (member variables or attributes) and implementations of behavior (member functions or methods)
An instance is a specific object built from a specific class
#Instance of complex number
c=7.0+3j
type(c)
complex
#Another instance of complex number
d=2.0-1j
type(d)
complex
d.real, d.imag
(2.0, -1.0)
dir
function¶dir()
is a builtin function, which returns list of the attributes and methods of any object
%pprint
dir(c)
Pretty printing has been turned OFF
['__abs__', '__add__', '__bool__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__int__', '__le__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__pos__', '__pow__', '__radd__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmod__', '__rmul__', '__rpow__', '__rsub__', '__rtruediv__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', 'conjugate', 'imag', 'real']
dir()
['In', 'Out', '_', '_1', '_10', '_11', '_12', '_13', '_16', '_17', '_18', '_2', '_21', '_22', '_23', '_24', '_27', '_28', '_29', '_3', '_30', '_31', '_32', '_33', '_36', '_37', '_38', '_4', '_41', '_42', '_43', '_44', '_45', '_46', '_49', '_50', '_51', '_52', '_53', '_54', '_55', '_56', '_57', '_58', '_61', '_62', '_63', '_64', '_65', '_7', '_8', '_9', '__', '___', '__builtin__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '_dh', '_i', '_i1', '_i10', '_i11', '_i12', '_i13', '_i14', '_i15', '_i16', '_i17', '_i18', '_i19', '_i2', '_i20', '_i21', '_i22', '_i23', '_i24', '_i25', '_i26', '_i27', '_i28', '_i29', '_i3', '_i30', '_i31', '_i32', '_i33', '_i34', '_i35', '_i36', '_i37', '_i38', '_i39', '_i4', '_i40', '_i41', '_i42', '_i43', '_i44', '_i45', '_i46', '_i47', '_i48', '_i49', '_i5', '_i50', '_i51', '_i52', '_i53', '_i54', '_i55', '_i56', '_i57', '_i58', '_i59', '_i6', '_i60', '_i61', '_i62', '_i63', '_i64', '_i65', '_i66', '_i7', '_i8', '_i9', '_ih', '_ii', '_iii', '_oh', 'a', 'b', 'c', 'd', 'decrease_first', 'exit', 'get_ipython', 'k', 'quit', 'sgn']
Every item in a Python program is an object of a specific class
a=7
a.bit_length()
3
A variable is symbolic name that is a reference or a pointer to an object
a = [1,2,3,4]
I'm creating the list object [1,2,3,4]
in memory and a
is pointing to this object somewhere in memory
The b
object is pointing to the same object
b = a
b
is a reference to a
. NOT a copy.
If b
changes... a
changes!
b[1]=0.0
b
[1, 0.0, 3, 4]
Basically, a
and b
are the same object
a is b
True
Objects to functions are passed by reference as well
def decrease_first(l):
l[0]-=10
print("Value of l (inside function)", l)
a=[1,2,3]
decrease_first(a)
print("Value of a", a)
Value of l (inside function) [-9, 2, 3] Value of a [-9, 2, 3]
Python has a set of built in functions and types. To avoid name conflics do not use built-in functions, or types as variable names
int
is a built in function
int(3.7)
a variable named int
int=4
the int
function is no more available
int(3.7) # Raise an error!
del int # restore the proper behavior