├── static ├── alex.jpg ├── conda.png ├── numpy.png ├── siro.jpg ├── sympy.png ├── ipython.png ├── 3d_array.png ├── matplotlib.png ├── scipy_logo.png ├── jupyter-logo.png ├── pandas_logo.png ├── pycones2016.jpg ├── scipy_org_logo.gif ├── scikit-learn-logo.png ├── aeropython_name_mini.png ├── cheatsheet-scikit-learn.png └── style.css ├── extras ├── Excel │ ├── datos.xlsx │ ├── static │ │ ├── aeropython_name_mini.png │ │ └── style.css │ ├── LICENSE │ └── Lightning Talk - Destruir lo Bello.ipynb ├── Pandas │ ├── imgs │ │ ├── Thumbs.db │ │ ├── Estructuras.png │ │ ├── DF_Rows_Columns.jpg │ │ ├── stack-unstack1.png │ │ ├── merging_concat_basic.png │ │ └── pivoting_simple_error.png │ ├── Datos │ │ └── README.md │ └── Kiko Correoso - PyDataMad16 - Tutorial 01 - Estructuras de datos.ipynb └── Arduino │ ├── programa arduino.txt │ └── arduino.ipynb ├── data ├── ligo_frecuencias.txt ├── barrio_del_pilar-20151222.csv ├── barrio_del_pilar-20160322.csv └── ligo_tiempos.txt ├── .gitignore ├── README.md ├── Basic Python Packages for Science - Student.ipynb └── Basic Python Packages for Science.ipynb /static/alex.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AeroPython/Taller-Aeropython-PyConEs16/master/static/alex.jpg -------------------------------------------------------------------------------- /static/conda.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AeroPython/Taller-Aeropython-PyConEs16/master/static/conda.png -------------------------------------------------------------------------------- /static/numpy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AeroPython/Taller-Aeropython-PyConEs16/master/static/numpy.png -------------------------------------------------------------------------------- /static/siro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AeroPython/Taller-Aeropython-PyConEs16/master/static/siro.jpg -------------------------------------------------------------------------------- /static/sympy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AeroPython/Taller-Aeropython-PyConEs16/master/static/sympy.png -------------------------------------------------------------------------------- /static/ipython.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AeroPython/Taller-Aeropython-PyConEs16/master/static/ipython.png -------------------------------------------------------------------------------- /static/3d_array.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AeroPython/Taller-Aeropython-PyConEs16/master/static/3d_array.png -------------------------------------------------------------------------------- /static/matplotlib.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AeroPython/Taller-Aeropython-PyConEs16/master/static/matplotlib.png -------------------------------------------------------------------------------- /static/scipy_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AeroPython/Taller-Aeropython-PyConEs16/master/static/scipy_logo.png -------------------------------------------------------------------------------- /extras/Excel/datos.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AeroPython/Taller-Aeropython-PyConEs16/master/extras/Excel/datos.xlsx -------------------------------------------------------------------------------- /static/jupyter-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AeroPython/Taller-Aeropython-PyConEs16/master/static/jupyter-logo.png -------------------------------------------------------------------------------- /static/pandas_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AeroPython/Taller-Aeropython-PyConEs16/master/static/pandas_logo.png -------------------------------------------------------------------------------- /static/pycones2016.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AeroPython/Taller-Aeropython-PyConEs16/master/static/pycones2016.jpg -------------------------------------------------------------------------------- /static/scipy_org_logo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AeroPython/Taller-Aeropython-PyConEs16/master/static/scipy_org_logo.gif -------------------------------------------------------------------------------- /extras/Pandas/imgs/Thumbs.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AeroPython/Taller-Aeropython-PyConEs16/master/extras/Pandas/imgs/Thumbs.db -------------------------------------------------------------------------------- /static/scikit-learn-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AeroPython/Taller-Aeropython-PyConEs16/master/static/scikit-learn-logo.png -------------------------------------------------------------------------------- /static/aeropython_name_mini.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AeroPython/Taller-Aeropython-PyConEs16/master/static/aeropython_name_mini.png -------------------------------------------------------------------------------- /extras/Arduino/programa arduino.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AeroPython/Taller-Aeropython-PyConEs16/master/extras/Arduino/programa arduino.txt -------------------------------------------------------------------------------- /extras/Pandas/imgs/Estructuras.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AeroPython/Taller-Aeropython-PyConEs16/master/extras/Pandas/imgs/Estructuras.png -------------------------------------------------------------------------------- /static/cheatsheet-scikit-learn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AeroPython/Taller-Aeropython-PyConEs16/master/static/cheatsheet-scikit-learn.png -------------------------------------------------------------------------------- /extras/Pandas/imgs/DF_Rows_Columns.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AeroPython/Taller-Aeropython-PyConEs16/master/extras/Pandas/imgs/DF_Rows_Columns.jpg -------------------------------------------------------------------------------- /extras/Pandas/imgs/stack-unstack1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AeroPython/Taller-Aeropython-PyConEs16/master/extras/Pandas/imgs/stack-unstack1.png -------------------------------------------------------------------------------- /extras/Pandas/imgs/merging_concat_basic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AeroPython/Taller-Aeropython-PyConEs16/master/extras/Pandas/imgs/merging_concat_basic.png -------------------------------------------------------------------------------- /extras/Excel/static/aeropython_name_mini.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AeroPython/Taller-Aeropython-PyConEs16/master/extras/Excel/static/aeropython_name_mini.png -------------------------------------------------------------------------------- /extras/Pandas/imgs/pivoting_simple_error.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AeroPython/Taller-Aeropython-PyConEs16/master/extras/Pandas/imgs/pivoting_simple_error.png -------------------------------------------------------------------------------- /extras/Pandas/Datos/README.md: -------------------------------------------------------------------------------- 1 | Here you can find two data files: 2 | 3 | * **model.txt**: It is a simulation made with a numerical weather prediction model in a mountainous area in the Alps. 4 | 5 | * **mast.txt**: It is a fake file created from the **model.txt** using random lags and random noise values 6 | to create a 10 minute frequency time series. Most of the columns are invalid. 7 | -------------------------------------------------------------------------------- /data/ligo_frecuencias.txt: -------------------------------------------------------------------------------- 1 | 1.600000000000000000e+01 2 | 3.200000000000000000e+01 3 | 4.800000000000000000e+01 4 | 6.400000000000000000e+01 5 | 8.000000000000000000e+01 6 | 9.600000000000000000e+01 7 | 1.120000000000000000e+02 8 | 1.280000000000000000e+02 9 | 1.440000000000000000e+02 10 | 1.600000000000000000e+02 11 | 1.760000000000000000e+02 12 | 1.920000000000000000e+02 13 | 2.080000000000000000e+02 14 | 2.240000000000000000e+02 15 | 2.400000000000000000e+02 16 | 2.560000000000000000e+02 17 | 2.720000000000000000e+02 18 | 2.880000000000000000e+02 19 | 3.040000000000000000e+02 20 | 3.200000000000000000e+02 21 | 3.360000000000000000e+02 22 | 3.520000000000000000e+02 23 | 3.680000000000000000e+02 24 | 3.840000000000000000e+02 25 | 4.000000000000000000e+02 26 | 4.160000000000000000e+02 27 | 4.320000000000000000e+02 28 | 4.480000000000000000e+02 29 | 4.640000000000000000e+02 30 | 4.800000000000000000e+02 31 | 4.960000000000000000e+02 32 | -------------------------------------------------------------------------------- /extras/Excel/LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Siro 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | 27 | # PyInstaller 28 | # Usually these files are written by a python script from a template 29 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 30 | *.manifest 31 | *.spec 32 | 33 | # Installer logs 34 | pip-log.txt 35 | pip-delete-this-directory.txt 36 | 37 | # Unit test / coverage reports 38 | htmlcov/ 39 | .tox/ 40 | .coverage 41 | .coverage.* 42 | .cache 43 | nosetests.xml 44 | coverage.xml 45 | *,cover 46 | .hypothesis/ 47 | 48 | # Translations 49 | *.mo 50 | *.pot 51 | 52 | # Django stuff: 53 | *.log 54 | local_settings.py 55 | 56 | # Flask stuff: 57 | instance/ 58 | .webassets-cache 59 | 60 | # Scrapy stuff: 61 | .scrapy 62 | 63 | # Sphinx documentation 64 | docs/_build/ 65 | 66 | # PyBuilder 67 | target/ 68 | 69 | # IPython Notebook 70 | .ipynb_checkpoints 71 | 72 | # pyenv 73 | .python-version 74 | 75 | # celery beat schedule file 76 | celerybeat-schedule 77 | 78 | # dotenv 79 | .env 80 | 81 | # virtualenv 82 | venv/ 83 | ENV/ 84 | 85 | # Spyder project settings 86 | .spyderproject 87 | 88 | # Rope project settings 89 | .ropeproject 90 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | PyConEs 4 | 5 | ## VIERNES 7/OCT/2016 6 | 7 | ### SIRO MORENO, ALEJANDRO SÁEZ MOLLEJO 8 | #### NIVEL: INICIACIÓN - INTERMEDIO 9 | ## Información General 10 | 11 | Hola pythonistas! Bienvenidos a nuestro *TALLER AEROPYTHON*, orientado a aquellos que aún estén empezando con el lenguaje, que quieran aprender o que estén interesados en descubrir las posibilidades de Python para ciencia e ingeniería. 12 | 13 | Para ello, [Álex Sáez](https://www.linkedin.com/in/alejandrosaezm/) y [Siro Moreno](https://www.linkedin.com/in/siro-moreno-mart%C3%ADn-1bab18b7/en) repetirán el taller básico de introducción a Python científico que ya impartieron en la PyData Madrid y que podéis ver en vídeo: 14 | 15 | https://youtu.be/kZo_nmpkzr0 16 | 17 | Para poder seguir el taller de forma autónoma será necesario traer portátil con Anaconda instalado. Si te surgen dudas puedes seguir esta guía de instalación: 18 | 19 | [http://nbviewer.jupyter.org/github/AeroPython/Curso_AeroPython/blob/master/notebooks_completos/Clase0_Bienvenido.ipynb](http://nbviewer.jupyter.org/github/AeroPython/Curso_AeroPython/blob/master/notebooks_completos/Clase0_Bienvenido.ipynb) 20 | 21 | 22 | 23 | ## Descripción 24 | 25 | The Aeropython’s guide to the Python Galaxy! 26 | 27 | Este taller será un introducción a algunos de los paquetes más habituales del entorno científico. Comenzaremos explorando el Jupyter Notebook, que usaremos durante toda las sesión, introduciremos el funcionamiento básico de los arrays de NumPy, haremos nuestras primeras representaciones gráficas con matplotlib, veremos algunas de las funcionalidades que ofrece SciPy y mostraremos las posibilidades para hacer cálculo simbólico que proporciona SymPy. 28 | 29 | Usaremos [Anaconda Python 3.5.1 distribution](https://www.continuum.io/downloads) y los siguientes paquetes: 30 | 31 | * IPython 5 32 | * IPython-Notebook 4.1.1 33 | * IPyWidgets 4.1.1 34 | * matplotlib 1.5.1 35 | * SciPy 0.17.1 36 | * SymPy 1.0 37 | 38 | ¡Trata de instalarlos antes! 39 | 40 | Este taller es una adaptación del [Curso de Introducción a Python para Ingenieros de AeroPython](https://github.com/AeroPython/Curso_AeroPython) y sus variantes que ha sido impartido en torno a quince veces en cinco universidades diferentes. 41 | 42 | [Síguenos en Twitter](https://twitter.com/AeroPython) 43 | 44 | AeroPython -------------------------------------------------------------------------------- /static/style.css: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 127 | 143 | -------------------------------------------------------------------------------- /extras/Excel/static/style.css: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 127 | 143 | -------------------------------------------------------------------------------- /data/barrio_del_pilar-20151222.csv: -------------------------------------------------------------------------------- 1 | Estación: Barrio del Pilar;;;; 2 | Fecha;Hora;CO;NO2;O3 3 | ;;mg/m³;µg/m³;µg/m³ 4 | 22/12/2015;01:00;0.5;65;7 5 | 22/12/2015;02:00;0.4;50;7 6 | 22/12/2015;03:00;0.3;37;11 7 | 22/12/2015;04:00;0.3;37;10 8 | 22/12/2015;05:00;0.3;29;17 9 | 22/12/2015;06:00;0.3;36;13 10 | 22/12/2015;07:00;0.3;47;7 11 | 22/12/2015;08:00;0.4;57;6 12 | 22/12/2015;09:00;0.5;67;6 13 | 22/12/2015;10:00;0.5;69;8 14 | 22/12/2015;11:00;0.5;67;11 15 | 22/12/2015;12:00;0.5;64;15 16 | 22/12/2015;13:00;0.5;65;21 17 | 22/12/2015;14:00;0.5;68;24 18 | 22/12/2015;15:00;0.7;91;19 19 | 22/12/2015;16:00;0.8;105;13 20 | 22/12/2015;17:00;0.8;102;10 21 | 22/12/2015;18:00;0.7;100;8 22 | 22/12/2015;19:00;1.5;148;7 23 | 22/12/2015;20:00;2.7;223;7 24 | 22/12/2015;21:00;2;204;7 25 | 22/12/2015;22:00;3.3;251;7 26 | 22/12/2015;23:00;3;264;7 27 | 22/12/2015;24:00;2;204;7 28 | 23/12/2015;01:00;1.2;127;6 29 | 23/12/2015;02:00;0.4;61;7 30 | 23/12/2015;03:00;0.3;46;9 31 | 23/12/2015;04:00;0.3;47;7 32 | 23/12/2015;05:00;0.5;44;6 33 | 23/12/2015;06:00;0.5;45;6 34 | 23/12/2015;07:00;0.5;46;6 35 | 23/12/2015;08:00;0.4;53;6 36 | 23/12/2015;09:00;0.5;69;6 37 | 23/12/2015;10:00;0.9;90;7 38 | 23/12/2015;11:00;0.4;51;11 39 | 23/12/2015;12:00;0.4;52;16 40 | 23/12/2015;13:00;0.4;56;20 41 | 23/12/2015;14:00;0.4;59;27 42 | 23/12/2015;15:00;0.4;66;25 43 | 23/12/2015;16:00;0.6;78;20 44 | 23/12/2015;17:00;0.7;96;11 45 | 23/12/2015;18:00;0.7;97;7 46 | 23/12/2015;19:00;1;117;7 47 | 23/12/2015;20:00;1.7;158;7 48 | 23/12/2015;21:00;2.8;235;8 49 | 23/12/2015;22:00;3.2;268;7 50 | 23/12/2015;23:00;3.4;281;7 51 | 23/12/2015;24:00;2.1;189;7 52 | 24/12/2015;01:00;0.7;88;6 53 | 24/12/2015;02:00;0.4;66;6 54 | 24/12/2015;03:00;0.6;61;6 55 | 24/12/2015;04:00;0.6;59;6 56 | 24/12/2015;05:00;0.5;45;6 57 | 24/12/2015;06:00;0.6;50;6 58 | 24/12/2015;07:00;0.7;52;6 59 | 24/12/2015;08:00;0.7;51;6 60 | 24/12/2015;09:00;1;62;6 61 | 24/12/2015;10:00;0.9;64;6 62 | 24/12/2015;11:00;0.8;68;7 63 | 24/12/2015;12:00;0.9;73;8 64 | 24/12/2015;13:00;-;-;- 65 | 24/12/2015;14:00;0.7;69;14 66 | 24/12/2015;15:00;0.6;68;15 67 | 24/12/2015;16:00;0.7;75;11 68 | 24/12/2015;17:00;0.6;67;9 69 | 24/12/2015;18:00;0.6;70;7 70 | 24/12/2015;19:00;0.9;84;7 71 | 24/12/2015;20:00;1;92;7 72 | 24/12/2015;21:00;1.3;108;7 73 | 24/12/2015;22:00;1.5;135;7 74 | 24/12/2015;23:00;1.2;116;7 75 | 24/12/2015;24:00;1.3;104;7 76 | 25/12/2015;01:00;1;80;7 77 | 25/12/2015;02:00;0.6;63;6 78 | 25/12/2015;03:00;0.4;51;6 79 | 25/12/2015;04:00;0.4;45;6 80 | 25/12/2015;05:00;0.5;46;6 81 | 25/12/2015;06:00;0.5;40;6 82 | 25/12/2015;07:00;0.6;35;6 83 | 25/12/2015;08:00;0.4;39;6 84 | 25/12/2015;09:00;0.3;33;9 85 | 25/12/2015;10:00;0.3;28;12 86 | 25/12/2015;11:00;0.3;21;22 87 | 25/12/2015;12:00;0.3;25;28 88 | 25/12/2015;13:00;0.3;26;37 89 | 25/12/2015;14:00;0.3;32;42 90 | 25/12/2015;15:00;0.4;41;42 91 | 25/12/2015;16:00;0.5;58;30 92 | 25/12/2015;17:00;0.6;72;17 93 | 25/12/2015;18:00;0.5;71;12 94 | 25/12/2015;19:00;1;108;7 95 | 25/12/2015;20:00;1.7;165;7 96 | 25/12/2015;21:00;2;186;7 97 | 25/12/2015;22:00;2;180;7 98 | 25/12/2015;23:00;1.8;178;7 99 | 25/12/2015;24:00;0.5;71;8 100 | 26/12/2015;01:00;0.4;43;17 101 | 26/12/2015;02:00;0.3;36;22 102 | 26/12/2015;03:00;0.3;32;25 103 | 26/12/2015;04:00;0.3;28;25 104 | 26/12/2015;05:00;0.3;23;29 105 | 26/12/2015;06:00;0.3;27;25 106 | 26/12/2015;07:00;0.4;35;12 107 | 26/12/2015;08:00;0.3;41;10 108 | 26/12/2015;09:00;0.6;53;6 109 | 26/12/2015;10:00;0.9;61;6 110 | 26/12/2015;11:00;0.6;58;11 111 | 26/12/2015;12:00;0.5;50;15 112 | 26/12/2015;13:00;0.5;50;20 113 | 26/12/2015;14:00;0.6;49;23 114 | 26/12/2015;15:00;0.5;60;20 115 | 26/12/2015;16:00;0.6;66;18 116 | 26/12/2015;17:00;0.5;64;19 117 | 26/12/2015;18:00;0.5;74;11 118 | 26/12/2015;19:00;0.8;105;7 119 | 26/12/2015;20:00;1.5;154;7 120 | 26/12/2015;21:00;1.1;136;7 121 | 26/12/2015;22:00;0.6;80;7 122 | 26/12/2015;23:00;0.6;74;7 123 | 26/12/2015;24:00;0.5;55;9 124 | 27/12/2015;01:00;0.4;42;13 125 | 27/12/2015;02:00;0.3;41;14 126 | 27/12/2015;03:00;0.3;37;16 127 | 27/12/2015;04:00;0.3;33;19 128 | 27/12/2015;05:00;0.3;32;19 129 | 27/12/2015;06:00;0.2;31;22 130 | 27/12/2015;07:00;0.3;24;30 131 | 27/12/2015;08:00;0.3;35;21 132 | 27/12/2015;09:00;0.3;32;25 133 | 27/12/2015;10:00;0.4;37;27 134 | 27/12/2015;11:00;0.4;42;23 135 | 27/12/2015;12:00;0.3;33;39 136 | 27/12/2015;13:00;0.3;27;51 137 | 27/12/2015;14:00;0.3;25;54 138 | 27/12/2015;15:00;0.3;29;50 139 | 27/12/2015;16:00;0.3;32;46 140 | 27/12/2015;17:00;0.3;30;49 141 | 27/12/2015;18:00;0.3;34;44 142 | 27/12/2015;19:00;0.4;54;24 143 | 27/12/2015;20:00;0.4;54;23 144 | 27/12/2015;21:00;0.4;52;22 145 | 27/12/2015;22:00;0.4;55;21 146 | 27/12/2015;23:00;0.4;55;19 147 | 27/12/2015;24:00;0.3;32;36 148 | 28/12/2015;01:00;0.3;28;37 149 | 28/12/2015;02:00;0.3;36;27 150 | 28/12/2015;03:00;0.3;46;16 151 | 28/12/2015;04:00;0.3;31;28 152 | 28/12/2015;05:00;0.2;29;29 153 | 28/12/2015;06:00;0.2;26;34 154 | 28/12/2015;07:00;0.3;37;25 155 | 28/12/2015;08:00;0.4;62;8 156 | 28/12/2015;09:00;0.6;65;10 157 | 28/12/2015;10:00;0.4;66;11 158 | 28/12/2015;11:00;0.4;60;17 159 | 28/12/2015;12:00;0.5;40;37 160 | 28/12/2015;13:00;0.4;52;32 161 | 28/12/2015;14:00;0.3;42;43 162 | 28/12/2015;15:00;0.3;49;36 163 | 28/12/2015;16:00;0.4;53;27 164 | 28/12/2015;17:00;0.4;61;14 165 | 28/12/2015;18:00;0.6;77;9 166 | 28/12/2015;19:00;0.5;77;8 167 | 28/12/2015;20:00;0.5;76;10 168 | 28/12/2015;21:00;0.5;77;8 169 | 28/12/2015;22:00;0.4;61;17 170 | 28/12/2015;23:00;0.3;48;25 171 | 28/12/2015;24:00;0.3;30;39 172 | 29/12/2015;01:00;0.2;16;48 173 | 29/12/2015;02:00;0.2;11;53 174 | 29/12/2015;03:00;0.2;8;56 175 | 29/12/2015;04:00;0.2;7;55 176 | 29/12/2015;05:00;0.2;10;49 177 | 29/12/2015;06:00;0.2;14;49 178 | 29/12/2015;07:00;0.3;35;29 179 | 29/12/2015;08:00;0.4;51;15 180 | 29/12/2015;09:00;0.4;62;8 181 | 29/12/2015;10:00;0.5;63;9 182 | 29/12/2015;11:00;0.4;50;16 183 | 29/12/2015;12:00;-;-;14 184 | 29/12/2015;13:00;-;-;12 185 | 29/12/2015;14:00;0.5;60;- 186 | 29/12/2015;15:00;0.5;65;20 187 | 29/12/2015;16:00;0.5;62;22 188 | 29/12/2015;17:00;0.4;51;27 189 | 29/12/2015;18:00;0.5;61;12 190 | 29/12/2015;19:00;0.5;61;10 191 | 29/12/2015;20:00;0.4;54;20 192 | 29/12/2015;21:00;0.4;54;22 193 | 29/12/2015;22:00;0.3;40;31 194 | 29/12/2015;23:00;0.3;30;38 195 | 29/12/2015;24:00;0.3;24;43 196 | -------------------------------------------------------------------------------- /data/barrio_del_pilar-20160322.csv: -------------------------------------------------------------------------------- 1 | Estación: Barrio del Pilar;;;; 2 | Fecha;Hora;CO;NO2;O3 3 | ;;mg/m³;µg/m³;µg/m³ 4 | 22/03/2016;01:00;0.2;14;73 5 | 22/03/2016;02:00;0.2;10;77 6 | 22/03/2016;03:00;0.2;9;75 7 | 22/03/2016;04:00;0.2;3;81 8 | 22/03/2016;05:00;0.2;3;81 9 | 22/03/2016;06:00;0.2;6;79 10 | 22/03/2016;07:00;0.2;24;59 11 | 22/03/2016;08:00;0.3;48;37 12 | 22/03/2016;09:00;0.3;40;43 13 | 22/03/2016;10:00;0.3;41;44 14 | 22/03/2016;11:00;0.3;20;68 15 | 22/03/2016;12:00;0.3;17;74 16 | 22/03/2016;13:00;0.2;14;84 17 | 22/03/2016;14:00;0.3;16;88 18 | 22/03/2016;15:00;0.3;15;94 19 | 22/03/2016;16:00;0.4;29;81 20 | 22/03/2016;17:00;0.3;23;82 21 | 22/03/2016;18:00;0.3;26;81 22 | 22/03/2016;19:00;0.3;30;75 23 | 22/03/2016;20:00;0.4;57;39 24 | 22/03/2016;21:00;0.4;73;17 25 | 22/03/2016;22:00;0.4;51;42 26 | 22/03/2016;23:00;0.4;72;16 27 | 22/03/2016;24:00;0.4;61;28 28 | 23/03/2016;01:00;0.3;25;62 29 | 23/03/2016;02:00;0.3;21;64 30 | 23/03/2016;03:00;0.3;40;39 31 | 23/03/2016;04:00;0.4;52;19 32 | 23/03/2016;05:00;0.4;47;8 33 | 23/03/2016;06:00;0.4;42;8 34 | 23/03/2016;07:00;0.5;68;8 35 | 23/03/2016;08:00;0.6;71;9 36 | 23/03/2016;09:00;0.9;76;10 37 | 23/03/2016;10:00;0.7;63;29 38 | 23/03/2016;11:00;0.3;27;66 39 | 23/03/2016;12:00;0.3;13;88 40 | 23/03/2016;13:00;0.2;10;92 41 | 23/03/2016;14:00;0.3;10;98 42 | 23/03/2016;15:00;0.3;11;99 43 | 23/03/2016;16:00;0.3;12;99 44 | 23/03/2016;17:00;0.2;11;98 45 | 23/03/2016;18:00;0.2;8;101 46 | 23/03/2016;19:00;0.2;13;92 47 | 23/03/2016;20:00;0.2;23;79 48 | 23/03/2016;21:00;0.5;40;56 49 | 23/03/2016;22:00;0.6;49;43 50 | 23/03/2016;23:00;0.5;66;25 51 | 23/03/2016;24:00;0.4;47;44 52 | 24/03/2016;01:00;0.3;18;76 53 | 24/03/2016;02:00;0.3;25;64 54 | 24/03/2016;03:00;0.3;16;77 55 | 24/03/2016;04:00;0.3;16;59 56 | 24/03/2016;05:00;0.3;34;31 57 | 24/03/2016;06:00;0.3;27;33 58 | 24/03/2016;07:00;0.3;44;17 59 | 24/03/2016;08:00;0.4;45;9 60 | 24/03/2016;09:00;0.5;52;22 61 | 24/03/2016;10:00;0.4;37;53 62 | 24/03/2016;11:00;0.3;21;73 63 | 24/03/2016;12:00;0.3;20;76 64 | 24/03/2016;13:00;0.3;24;76 65 | 24/03/2016;14:00;0.4;38;71 66 | 24/03/2016;15:00;0.3;32;78 67 | 24/03/2016;16:00;0.3;21;89 68 | 24/03/2016;17:00;0.2;10;105 69 | 24/03/2016;18:00;0.3;15;102 70 | 24/03/2016;19:00;0.3;21;93 71 | 24/03/2016;20:00;0.3;45;63 72 | 24/03/2016;21:00;0.4;59;47 73 | 24/03/2016;22:00;0.4;59;44 74 | 24/03/2016;23:00;0.7;99;9 75 | 24/03/2016;24:00;0.6;88;9 76 | 25/03/2016;01:00;0.8;93;9 77 | 25/03/2016;02:00;0.9;89;9 78 | 25/03/2016;03:00;0.8;84;8 79 | 25/03/2016;04:00;0.5;64;10 80 | 25/03/2016;05:00;0.4;58;11 81 | 25/03/2016;06:00;0.5;53;9 82 | 25/03/2016;07:00;0.4;41;8 83 | 25/03/2016;08:00;0.5;43;9 84 | 25/03/2016;09:00;0.5;45;13 85 | 25/03/2016;10:00;0.6;51;25 86 | 25/03/2016;11:00;0.5;44;40 87 | 25/03/2016;12:00;0.4;36;59 88 | 25/03/2016;13:00;0.4;36;68 89 | 25/03/2016;14:00;0.3;26;84 90 | 25/03/2016;15:00;0.3;16;98 91 | 25/03/2016;16:00;0.3;17;97 92 | 25/03/2016;17:00;0.3;24;89 93 | 25/03/2016;18:00;0.3;17;99 94 | 25/03/2016;19:00;0.3;12;100 95 | 25/03/2016;20:00;0.3;42;61 96 | 25/03/2016;21:00;0.4;52;44 97 | 25/03/2016;22:00;0.5;54;39 98 | 25/03/2016;23:00;0.5;60;28 99 | 25/03/2016;24:00;0.6;73;13 100 | 26/03/2016;01:00;0.5;58;23 101 | 26/03/2016;02:00;0.4;58;16 102 | 26/03/2016;03:00;0.5;61;10 103 | 26/03/2016;04:00;0.5;59;9 104 | 26/03/2016;05:00;0.4;50;9 105 | 26/03/2016;06:00;0.3;31;10 106 | 26/03/2016;07:00;0.4;36;9 107 | 26/03/2016;08:00;0.6;45;9 108 | 26/03/2016;09:00;0.5;43;18 109 | 26/03/2016;10:00;0.5;37;24 110 | 26/03/2016;11:00;0.5;40;38 111 | 26/03/2016;12:00;0.4;26;59 112 | 26/03/2016;13:00;0.3;14;67 113 | 26/03/2016;14:00;0.3;12;64 114 | 26/03/2016;15:00;0.3;13;62 115 | 26/03/2016;16:00;0.2;10;63 116 | 26/03/2016;17:00;0.2;7;58 117 | 26/03/2016;18:00;0.2;8;53 118 | 26/03/2016;19:00;0.2;11;51 119 | 26/03/2016;20:00;0.3;16;47 120 | 26/03/2016;21:00;0.2;19;42 121 | 26/03/2016;22:00;0.3;22;38 122 | 26/03/2016;23:00;0.3;23;36 123 | 26/03/2016;24:00;0.3;16;43 124 | 27/03/2016;01:00;0.2;9;49 125 | 27/03/2016;02:00;0.2;6;48 126 | 27/03/2016;03:00;-;-;- 127 | 27/03/2016;04:00;0.2;4;64 128 | 27/03/2016;05:00;0.2;3;89 129 | 27/03/2016;06:00;0.2;4;90 130 | 27/03/2016;07:00;0.2;3;92 131 | 27/03/2016;08:00;0.2;6;89 132 | 27/03/2016;09:00;0.3;11;83 133 | 27/03/2016;10:00;0.3;9;87 134 | 27/03/2016;11:00;0.3;8;84 135 | 27/03/2016;12:00;0.3;10;82 136 | 27/03/2016;13:00;0.3;10;80 137 | 27/03/2016;14:00;0.3;12;80 138 | 27/03/2016;15:00;0.3;12;81 139 | 27/03/2016;16:00;0.3;8;84 140 | 27/03/2016;17:00;0.3;10;85 141 | 27/03/2016;18:00;0.2;10;85 142 | 27/03/2016;19:00;0.2;14;82 143 | 27/03/2016;20:00;0.3;18;72 144 | 27/03/2016;21:00;0.3;28;60 145 | 27/03/2016;22:00;0.3;30;55 146 | 27/03/2016;23:00;0.3;21;61 147 | 27/03/2016;24:00;0.3;16;63 148 | 28/03/2016;01:00;0.3;12;65 149 | 28/03/2016;02:00;0.2;9;67 150 | 28/03/2016;03:00;0.2;5;70 151 | 28/03/2016;04:00;0.2;5;69 152 | 28/03/2016;05:00;0.2;6;65 153 | 28/03/2016;06:00;0.2;7;63 154 | 28/03/2016;07:00;0.3;16;55 155 | 28/03/2016;08:00;0.3;30;45 156 | 28/03/2016;09:00;0.3;38;39 157 | 28/03/2016;10:00;0.3;37;41 158 | 28/03/2016;11:00;0.3;29;53 159 | 28/03/2016;12:00;0.3;27;53 160 | 28/03/2016;13:00;0.3;27;49 161 | 28/03/2016;14:00;0.3;23;54 162 | 28/03/2016;15:00;0.3;22;57 163 | 28/03/2016;16:00;0.3;19;61 164 | 28/03/2016;17:00;0.3;17;63 165 | 28/03/2016;18:00;0.3;22;59 166 | 28/03/2016;19:00;0.3;27;53 167 | 28/03/2016;20:00;0.3;29;50 168 | 28/03/2016;21:00;0.3;34;44 169 | 28/03/2016;22:00;0.3;33;45 170 | 28/03/2016;23:00;0.3;26;50 171 | 28/03/2016;24:00;0.3;19;56 172 | 29/03/2016;01:00;0.2;11;63 173 | 29/03/2016;02:00;0.2;8;63 174 | 29/03/2016;03:00;0.2;9;58 175 | 29/03/2016;04:00;0.2;6;63 176 | 29/03/2016;05:00;0.2;5;66 177 | 29/03/2016;06:00;0.2;7;62 178 | 29/03/2016;07:00;0.3;18;53 179 | 29/03/2016;08:00;0.4;38;37 180 | 29/03/2016;09:00;0.4;49;28 181 | 29/03/2016;10:00;0.4;45;35 182 | 29/03/2016;11:00;0.3;34;47 183 | 29/03/2016;12:00;0.3;24;62 184 | 29/03/2016;13:00;0.3;24;68 185 | 29/03/2016;14:00;0.3;28;68 186 | 29/03/2016;15:00;0.3;23;78 187 | 29/03/2016;16:00;0.3;21;82 188 | 29/03/2016;17:00;0.3;17;87 189 | 29/03/2016;18:00;0.3;23;80 190 | 29/03/2016;19:00;0.3;28;75 191 | 29/03/2016;20:00;0.3;29;71 192 | 29/03/2016;21:00;0.3;46;50 193 | 29/03/2016;22:00;0.4;66;27 194 | 29/03/2016;23:00;0.3;51;38 195 | 29/03/2016;24:00;0.3;42;46 196 | -------------------------------------------------------------------------------- /data/ligo_tiempos.txt: -------------------------------------------------------------------------------- 1 | 9.503906250000000000e+00 2 | 9.507812500000000000e+00 3 | 9.511718750000000000e+00 4 | 9.515625000000000000e+00 5 | 9.519531250000000000e+00 6 | 9.523437500000000000e+00 7 | 9.527343750000000000e+00 8 | 9.531250000000000000e+00 9 | 9.535156250000000000e+00 10 | 9.539062500000000000e+00 11 | 9.542968750000000000e+00 12 | 9.546875000000000000e+00 13 | 9.550781250000000000e+00 14 | 9.554687500000000000e+00 15 | 9.558593750000000000e+00 16 | 9.562500000000000000e+00 17 | 9.566406250000000000e+00 18 | 9.570312500000000000e+00 19 | 9.574218750000000000e+00 20 | 9.578125000000000000e+00 21 | 9.582031250000000000e+00 22 | 9.585937500000000000e+00 23 | 9.589843750000000000e+00 24 | 9.593750000000000000e+00 25 | 9.597656250000000000e+00 26 | 9.601562500000000000e+00 27 | 9.605468750000000000e+00 28 | 9.609375000000000000e+00 29 | 9.613281250000000000e+00 30 | 9.617187500000000000e+00 31 | 9.621093750000000000e+00 32 | 9.625000000000000000e+00 33 | 9.628906250000000000e+00 34 | 9.632812500000000000e+00 35 | 9.636718750000000000e+00 36 | 9.640625000000000000e+00 37 | 9.644531250000000000e+00 38 | 9.648437500000000000e+00 39 | 9.652343750000000000e+00 40 | 9.656250000000000000e+00 41 | 9.660156250000000000e+00 42 | 9.664062500000000000e+00 43 | 9.667968750000000000e+00 44 | 9.671875000000000000e+00 45 | 9.675781250000000000e+00 46 | 9.679687500000000000e+00 47 | 9.683593750000000000e+00 48 | 9.687500000000000000e+00 49 | 9.691406250000000000e+00 50 | 9.695312500000000000e+00 51 | 9.699218750000000000e+00 52 | 9.703125000000000000e+00 53 | 9.707031250000000000e+00 54 | 9.710937500000000000e+00 55 | 9.714843750000000000e+00 56 | 9.718750000000000000e+00 57 | 9.722656250000000000e+00 58 | 9.726562500000000000e+00 59 | 9.730468750000000000e+00 60 | 9.734375000000000000e+00 61 | 9.738281250000000000e+00 62 | 9.742187500000000000e+00 63 | 9.746093750000000000e+00 64 | 9.750000000000000000e+00 65 | 9.753906250000000000e+00 66 | 9.757812500000000000e+00 67 | 9.761718750000000000e+00 68 | 9.765625000000000000e+00 69 | 9.769531250000000000e+00 70 | 9.773437500000000000e+00 71 | 9.777343750000000000e+00 72 | 9.781250000000000000e+00 73 | 9.785156250000000000e+00 74 | 9.789062500000000000e+00 75 | 9.792968750000000000e+00 76 | 9.796875000000000000e+00 77 | 9.800781250000000000e+00 78 | 9.804687500000000000e+00 79 | 9.808593750000000000e+00 80 | 9.812500000000000000e+00 81 | 9.816406250000000000e+00 82 | 9.820312500000000000e+00 83 | 9.824218750000000000e+00 84 | 9.828125000000000000e+00 85 | 9.832031250000000000e+00 86 | 9.835937500000000000e+00 87 | 9.839843750000000000e+00 88 | 9.843750000000000000e+00 89 | 9.847656250000000000e+00 90 | 9.851562500000000000e+00 91 | 9.855468750000000000e+00 92 | 9.859375000000000000e+00 93 | 9.863281250000000000e+00 94 | 9.867187500000000000e+00 95 | 9.871093750000000000e+00 96 | 9.875000000000000000e+00 97 | 9.878906250000000000e+00 98 | 9.882812500000000000e+00 99 | 9.886718750000000000e+00 100 | 9.890625000000000000e+00 101 | 9.894531250000000000e+00 102 | 9.898437500000000000e+00 103 | 9.902343750000000000e+00 104 | 9.906250000000000000e+00 105 | 9.910156250000000000e+00 106 | 9.914062500000000000e+00 107 | 9.917968750000000000e+00 108 | 9.921875000000000000e+00 109 | 9.925781250000000000e+00 110 | 9.929687500000000000e+00 111 | 9.933593750000000000e+00 112 | 9.937500000000000000e+00 113 | 9.941406250000000000e+00 114 | 9.945312500000000000e+00 115 | 9.949218750000000000e+00 116 | 9.953125000000000000e+00 117 | 9.957031250000000000e+00 118 | 9.960937500000000000e+00 119 | 9.964843750000000000e+00 120 | 9.968750000000000000e+00 121 | 9.972656250000000000e+00 122 | 9.976562500000000000e+00 123 | 9.980468750000000000e+00 124 | 9.984375000000000000e+00 125 | 9.988281250000000000e+00 126 | 9.992187500000000000e+00 127 | 9.996093750000000000e+00 128 | 1.000000000000000000e+01 129 | 1.000390625000000000e+01 130 | 1.000781250000000000e+01 131 | 1.001171875000000000e+01 132 | 1.001562500000000000e+01 133 | 1.001953125000000000e+01 134 | 1.002343750000000000e+01 135 | 1.002734375000000000e+01 136 | 1.003125000000000000e+01 137 | 1.003515625000000000e+01 138 | 1.003906250000000000e+01 139 | 1.004296875000000000e+01 140 | 1.004687500000000000e+01 141 | 1.005078125000000000e+01 142 | 1.005468750000000000e+01 143 | 1.005859375000000000e+01 144 | 1.006250000000000000e+01 145 | 1.006640625000000000e+01 146 | 1.007031250000000000e+01 147 | 1.007421875000000000e+01 148 | 1.007812500000000000e+01 149 | 1.008203125000000000e+01 150 | 1.008593750000000000e+01 151 | 1.008984375000000000e+01 152 | 1.009375000000000000e+01 153 | 1.009765625000000000e+01 154 | 1.010156250000000000e+01 155 | 1.010546875000000000e+01 156 | 1.010937500000000000e+01 157 | 1.011328125000000000e+01 158 | 1.011718750000000000e+01 159 | 1.012109375000000000e+01 160 | 1.012500000000000000e+01 161 | 1.012890625000000000e+01 162 | 1.013281250000000000e+01 163 | 1.013671875000000000e+01 164 | 1.014062500000000000e+01 165 | 1.014453125000000000e+01 166 | 1.014843750000000000e+01 167 | 1.015234375000000000e+01 168 | 1.015625000000000000e+01 169 | 1.016015625000000000e+01 170 | 1.016406250000000000e+01 171 | 1.016796875000000000e+01 172 | 1.017187500000000000e+01 173 | 1.017578125000000000e+01 174 | 1.017968750000000000e+01 175 | 1.018359375000000000e+01 176 | 1.018750000000000000e+01 177 | 1.019140625000000000e+01 178 | 1.019531250000000000e+01 179 | 1.019921875000000000e+01 180 | 1.020312500000000000e+01 181 | 1.020703125000000000e+01 182 | 1.021093750000000000e+01 183 | 1.021484375000000000e+01 184 | 1.021875000000000000e+01 185 | 1.022265625000000000e+01 186 | 1.022656250000000000e+01 187 | 1.023046875000000000e+01 188 | 1.023437500000000000e+01 189 | 1.023828125000000000e+01 190 | 1.024218750000000000e+01 191 | 1.024609375000000000e+01 192 | 1.025000000000000000e+01 193 | 1.025390625000000000e+01 194 | 1.025781250000000000e+01 195 | 1.026171875000000000e+01 196 | 1.026562500000000000e+01 197 | 1.026953125000000000e+01 198 | 1.027343750000000000e+01 199 | 1.027734375000000000e+01 200 | 1.028125000000000000e+01 201 | 1.028515625000000000e+01 202 | 1.028906250000000000e+01 203 | 1.029296875000000000e+01 204 | 1.029687500000000000e+01 205 | 1.030078125000000000e+01 206 | 1.030468750000000000e+01 207 | 1.030859375000000000e+01 208 | 1.031250000000000000e+01 209 | 1.031640625000000000e+01 210 | 1.032031250000000000e+01 211 | 1.032421875000000000e+01 212 | 1.032812500000000000e+01 213 | 1.033203125000000000e+01 214 | 1.033593750000000000e+01 215 | 1.033984375000000000e+01 216 | 1.034375000000000000e+01 217 | 1.034765625000000000e+01 218 | 1.035156250000000000e+01 219 | 1.035546875000000000e+01 220 | 1.035937500000000000e+01 221 | 1.036328125000000000e+01 222 | 1.036718750000000000e+01 223 | 1.037109375000000000e+01 224 | 1.037500000000000000e+01 225 | 1.037890625000000000e+01 226 | 1.038281250000000000e+01 227 | 1.038671875000000000e+01 228 | 1.039062500000000000e+01 229 | 1.039453125000000000e+01 230 | 1.039843750000000000e+01 231 | 1.040234375000000000e+01 232 | 1.040625000000000000e+01 233 | 1.041015625000000000e+01 234 | 1.041406250000000000e+01 235 | 1.041796875000000000e+01 236 | 1.042187500000000000e+01 237 | 1.042578125000000000e+01 238 | 1.042968750000000000e+01 239 | 1.043359375000000000e+01 240 | 1.043750000000000000e+01 241 | 1.044140625000000000e+01 242 | 1.044531250000000000e+01 243 | 1.044921875000000000e+01 244 | 1.045312500000000000e+01 245 | 1.045703125000000000e+01 246 | 1.046093750000000000e+01 247 | 1.046484375000000000e+01 248 | 1.046875000000000000e+01 249 | 1.047265625000000000e+01 250 | 1.047656250000000000e+01 251 | 1.048046875000000000e+01 252 | 1.048437500000000000e+01 253 | 1.048828125000000000e+01 254 | 1.049218750000000000e+01 255 | 1.049609375000000000e+01 256 | 1.050000000000000000e+01 257 | -------------------------------------------------------------------------------- /extras/Arduino/arduino.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "collapsed": false 7 | }, 8 | "source": [ 9 | "## Interactuar con arduino desde un Notebook" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "AVISO DE CHAPUZA SUPREMA" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "Primero, abrimos la vía de comunicación" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": null, 29 | "metadata": { 30 | "collapsed": true 31 | }, 32 | "outputs": [], 33 | "source": [ 34 | "import serial\n", 35 | "import datetime" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": null, 41 | "metadata": { 42 | "collapsed": false 43 | }, 44 | "outputs": [], 45 | "source": [ 46 | "ser = serial.Serial('COM4', 9600)\n", 47 | "\n", 48 | "#Linux: ser = serial.Serial('/dev/ttyACM0', 9600)" 49 | ] 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "metadata": {}, 54 | "source": [ 55 | "Con la función readline() podemos leer, y con decode la dejamos más bonita" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": null, 61 | "metadata": { 62 | "collapsed": false, 63 | "scrolled": true 64 | }, 65 | "outputs": [], 66 | "source": [ 67 | "for i in range(100):\n", 68 | " print(ser.readline().decode(\"utf-8\") )" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": {}, 74 | "source": [ 75 | "Podemos también escribir!" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": null, 81 | "metadata": { 82 | "collapsed": false 83 | }, 84 | "outputs": [], 85 | "source": [ 86 | "ser.write(b'50')" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": {}, 92 | "source": [ 93 | "Para lo siguiente, tendremos que cortar la comunicación" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": null, 99 | "metadata": { 100 | "collapsed": true 101 | }, 102 | "outputs": [], 103 | "source": [ 104 | "ser.close()" 105 | ] 106 | }, 107 | { 108 | "cell_type": "markdown", 109 | "metadata": {}, 110 | "source": [ 111 | "Hagamos cosas de verdad" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": null, 117 | "metadata": { 118 | "collapsed": false 119 | }, 120 | "outputs": [], 121 | "source": [ 122 | "ser = serial.Serial('COM4', 9600) #Iniciamos comunicación\n", 123 | "\n", 124 | "print('preparado')\n", 125 | "for i in range(3):\n", 126 | " print(ser.readline().decode(\"utf-8\") ) #Espera hasta que empiece a comunicarse\n", 127 | "# -----------------------------------\n", 128 | "ser.write(b'50') #Enviamos una orden\n", 129 | "print('mensaje enviado\\r\\n')\n", 130 | "for i in range(2):\n", 131 | " print(ser.readline().decode(\"utf-8\") ) #Leemos un par de veces para limpiar el puerto por si acaso\n", 132 | "\n", 133 | "for i in range(100): #Leemos hasta que pase a 'Esperando', o hasta que termine\n", 134 | " if ser.readline() == b'esperando\\r\\n':\n", 135 | " print('esperando\\r\\n')\n", 136 | " print(ser.readline().decode(\"utf-8\") )\n", 137 | " print(ser.readline().decode(\"utf-8\") )\n", 138 | " break\n", 139 | " print(ser.readline().decode(\"utf-8\") )\n", 140 | "#------------------------------------\n", 141 | "print('pasando a segunda orden\\r\\n') \n", 142 | "ser.write(b'150')\n", 143 | "for i in range(3):\n", 144 | " print(ser.readline().decode(\"utf-8\") )\n", 145 | "for i in range(100):\n", 146 | " if ser.readline() == b'esperando\\r\\n':\n", 147 | " print('esperando\\r\\n')\n", 148 | " print(ser.readline().decode(\"utf-8\") )\n", 149 | " print(ser.readline().decode(\"utf-8\") )\n", 150 | " break\n", 151 | " print(ser.readline().decode(\"utf-8\") )\n", 152 | "ser.close()" 153 | ] 154 | }, 155 | { 156 | "cell_type": "markdown", 157 | "metadata": {}, 158 | "source": [ 159 | "Un poco más avanzado!" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": null, 165 | "metadata": { 166 | "collapsed": false, 167 | "scrolled": true 168 | }, 169 | "outputs": [], 170 | "source": [ 171 | "tiempos = []\n", 172 | "posiciones =[]\n", 173 | "starttime = datetime.datetime.now()\n", 174 | "\n", 175 | "ser = serial.Serial('COM4', 9600) #Iniciamos comunicación\n", 176 | "\n", 177 | "print('preparado')\n", 178 | "for i in range(3):\n", 179 | " lectura = ser.readline().decode(\"utf-8\")\n", 180 | " print(lectura) #Espera hasta que empiece a comunicarse\n", 181 | " if lectura[:8] == 'Medida: ':\n", 182 | " tiempo = datetime.datetime.now()\n", 183 | " deltat = tiempo - starttime\n", 184 | " deltatvalue = deltat.seconds + deltat.microseconds/1000000\n", 185 | " tiempos.append(deltatvalue)\n", 186 | " medida = float(lectura[8:])\n", 187 | " posiciones.append(medida)\n", 188 | "# -----------------------------------\n", 189 | "ser.write(b'50') #Enviamos una orden\n", 190 | "print('mensaje enviado\\r\\n')\n", 191 | "for i in range(2):\n", 192 | " print(ser.readline().decode(\"utf-8\") ) #Leemos un par de veces para limpiar el puerto por si acaso\n", 193 | "\n", 194 | "for i in range(100): #Leemos hasta que pase a 'Esperando', o hasta que termine\n", 195 | " lectura = ser.readline().decode(\"utf-8\")\n", 196 | " print(lectura) \n", 197 | " if lectura == 'esperando\\r\\n':\n", 198 | " print(ser.readline().decode(\"utf-8\") )\n", 199 | " print(ser.readline().decode(\"utf-8\") )\n", 200 | " break\n", 201 | " if lectura[:8] == 'moviendo':\n", 202 | " _pos = lectura.find('posicion') + 10\n", 203 | " _pos2 = lectura.find(' ',_pos )\n", 204 | " medida = float(lectura[_pos : _pos2])\n", 205 | " posiciones.append(medida)\n", 206 | " tiempo = datetime.datetime.now()\n", 207 | " deltat = tiempo - starttime\n", 208 | " deltatvalue = deltat.seconds + deltat.microseconds/1000000\n", 209 | " tiempos.append(deltatvalue)\n", 210 | " \n", 211 | "#------------------------------------\n", 212 | "print('pasando a segunda orden\\r\\n') \n", 213 | "ser.write(b'150')\n", 214 | "for i in range(4):\n", 215 | " lectura = ser.readline().decode(\"utf-8\")\n", 216 | " print(lectura) #Espera hasta que empiece a comunicarse\n", 217 | " if lectura[:8] == 'Medida: ':\n", 218 | " tiempo = datetime.datetime.now()\n", 219 | " deltat = tiempo - starttime\n", 220 | " deltatvalue = deltat.seconds + deltat.microseconds/1000000\n", 221 | " tiempos.append(deltatvalue)\n", 222 | " medida = float(lectura[8:])\n", 223 | " posiciones.append(medida)\n", 224 | "for i in range(100):\n", 225 | " lectura = ser.readline().decode(\"utf-8\")\n", 226 | " print(lectura) \n", 227 | " if lectura == 'esperando\\r\\n':\n", 228 | " print(ser.readline().decode(\"utf-8\") )\n", 229 | " print(ser.readline().decode(\"utf-8\") )\n", 230 | " break\n", 231 | " if lectura[:8] == 'moviendo':\n", 232 | " _pos = lectura.find('posicion') + 10\n", 233 | " _pos2 = lectura.find(' ',_pos )\n", 234 | " medida = float(lectura[_pos : _pos2])\n", 235 | " posiciones.append(medida)\n", 236 | " tiempo = datetime.datetime.now()\n", 237 | " deltat = tiempo - starttime\n", 238 | " deltatvalue = deltat.seconds + deltat.microseconds/1000000\n", 239 | " tiempos.append(deltatvalue)\n", 240 | "ser.close()" 241 | ] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "execution_count": null, 246 | "metadata": { 247 | "collapsed": false 248 | }, 249 | "outputs": [], 250 | "source": [ 251 | "tiempos" 252 | ] 253 | }, 254 | { 255 | "cell_type": "code", 256 | "execution_count": null, 257 | "metadata": { 258 | "collapsed": false 259 | }, 260 | "outputs": [], 261 | "source": [ 262 | "posiciones" 263 | ] 264 | }, 265 | { 266 | "cell_type": "code", 267 | "execution_count": null, 268 | "metadata": { 269 | "collapsed": true 270 | }, 271 | "outputs": [], 272 | "source": [ 273 | "%matplotlib inline\n", 274 | "import matplotlib.pyplot as plt" 275 | ] 276 | }, 277 | { 278 | "cell_type": "code", 279 | "execution_count": null, 280 | "metadata": { 281 | "collapsed": false 282 | }, 283 | "outputs": [], 284 | "source": [ 285 | "plt.figure(figsize=(10,6))\n", 286 | "plt.plot(tiempos, posiciones)" 287 | ] 288 | }, 289 | { 290 | "cell_type": "code", 291 | "execution_count": null, 292 | "metadata": { 293 | "collapsed": true 294 | }, 295 | "outputs": [], 296 | "source": [] 297 | } 298 | ], 299 | "metadata": { 300 | "kernelspec": { 301 | "display_name": "Python 3", 302 | "language": "python", 303 | "name": "python3" 304 | }, 305 | "language_info": { 306 | "codemirror_mode": { 307 | "name": "ipython", 308 | "version": 3 309 | }, 310 | "file_extension": ".py", 311 | "mimetype": "text/x-python", 312 | "name": "python", 313 | "nbconvert_exporter": "python", 314 | "pygments_lexer": "ipython3", 315 | "version": "3.5.2" 316 | } 317 | }, 318 | "nbformat": 4, 319 | "nbformat_minor": 0 320 | } 321 | -------------------------------------------------------------------------------- /extras/Pandas/Kiko Correoso - PyDataMad16 - Tutorial 01 - Estructuras de datos.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Nuevas estructuras de datos\n", 8 | "\n", 9 | "PANDAs = PANel DAta Structure\n", 10 | "\n", 11 | "`pandas` introduce tres nuevos tipos de estructura de datos:\n", 12 | "\n", 13 | "* `Series` : Es una estructura de datos de 1D, como si fuera un vector de datos con índices.\n", 14 | "\n", 15 | "* `DataFrame` : Es una estructura de datos de 2D, como si fuera un diccionario de `Series`.\n", 16 | "\n", 17 | "* `Panel` : Es una estructura de datos de nD (con $n \\ge 3$), como si fuera un diccionario de `DataFrame`s." 18 | ] 19 | }, 20 | { 21 | "cell_type": "markdown", 22 | "metadata": {}, 23 | "source": [ 24 | "![](imgs/Estructuras.png)\n", 25 | "\n", 26 | "(imagen extraída de [aquí](https://github.com/jonathanrocher/pandas_tutorial/blob/master/analyzing_and_manipulating_data_with_pandas_manual.pdf))" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": null, 32 | "metadata": { 33 | "collapsed": true 34 | }, 35 | "outputs": [], 36 | "source": [ 37 | "import pandas as pd\n", 38 | "import numpy as np\n", 39 | "import matplotlib.pyplot as plt\n", 40 | "\n", 41 | "np.random.seed(19760812)\n", 42 | "%matplotlib inline" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": { 49 | "collapsed": true 50 | }, 51 | "outputs": [], 52 | "source": [ 53 | "s = pd.Series()" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": null, 59 | "metadata": { 60 | "collapsed": true 61 | }, 62 | "outputs": [], 63 | "source": [ 64 | "df = pd.DataFrame()" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": null, 70 | "metadata": { 71 | "collapsed": true 72 | }, 73 | "outputs": [], 74 | "source": [ 75 | "p = pd.Panel()" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": null, 81 | "metadata": { 82 | "collapsed": false 83 | }, 84 | "outputs": [], 85 | "source": [ 86 | "print(s, df, p, sep = '\\n' * 2)" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": {}, 92 | "source": [ 93 | "# `Series`" 94 | ] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "Una `Series` es un array indexado:\n", 101 | "\n", 102 | "• Un NumPy array mapea un rango de números enteros a valores.\n", 103 | "\n", 104 | "\n", 105 | "• Una `Series` mapea un grupo arbitrario de etiquetas a valores.\n", 106 | "\n", 107 | "\n", 108 | "• Una `Series` se puede ver también como un diccionario especializado donde todos los valores poseen el mismo tipo y se encuentran almacenados de forma eficiente." 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": null, 114 | "metadata": { 115 | "collapsed": true 116 | }, 117 | "outputs": [], 118 | "source": [ 119 | "s = pd.Series({'a' : 1000, 'b' : 2000, 'c' : 3000, 'd' : 4000})" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": null, 125 | "metadata": { 126 | "collapsed": false 127 | }, 128 | "outputs": [], 129 | "source": [ 130 | "s['b']" 131 | ] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "metadata": {}, 136 | "source": [ 137 | "Para acceder a las etiquetas se usa el atributo `s.index` mientras que para acceder a los valores se usa el atributo `s.values` (NumPy array)." 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": null, 143 | "metadata": { 144 | "collapsed": false 145 | }, 146 | "outputs": [], 147 | "source": [ 148 | "help(pd.Series)" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": null, 154 | "metadata": { 155 | "collapsed": false 156 | }, 157 | "outputs": [], 158 | "source": [ 159 | "lista = [1,10,100,1000]\n", 160 | "dicc = {'a': 1, 'b': 10, 'c': 100, 'd': 1000}" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": null, 166 | "metadata": { 167 | "collapsed": true 168 | }, 169 | "outputs": [], 170 | "source": [ 171 | "# creación a partir de una lista\n", 172 | "s1 = pd.Series(data = lista, index = ['a','b','c','d'], name = 'Mi Serie')" 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": null, 178 | "metadata": { 179 | "collapsed": true 180 | }, 181 | "outputs": [], 182 | "source": [ 183 | "# creación a partir de un diccionario\n", 184 | "s2 = pd.Series(data = dicc, name = 'Mi Serie')" 185 | ] 186 | }, 187 | { 188 | "cell_type": "code", 189 | "execution_count": null, 190 | "metadata": { 191 | "collapsed": true 192 | }, 193 | "outputs": [], 194 | "source": [ 195 | "# cread s3 a partir de un numpy array\n", 196 | "\n", 197 | "\n" 198 | ] 199 | }, 200 | { 201 | "cell_type": "code", 202 | "execution_count": null, 203 | "metadata": { 204 | "collapsed": false 205 | }, 206 | "outputs": [], 207 | "source": [ 208 | "s1" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": null, 214 | "metadata": { 215 | "collapsed": false 216 | }, 217 | "outputs": [], 218 | "source": [ 219 | "s2" 220 | ] 221 | }, 222 | { 223 | "cell_type": "code", 224 | "execution_count": null, 225 | "metadata": { 226 | "collapsed": false 227 | }, 228 | "outputs": [], 229 | "source": [ 230 | "s1.index" 231 | ] 232 | }, 233 | { 234 | "cell_type": "code", 235 | "execution_count": null, 236 | "metadata": { 237 | "collapsed": false 238 | }, 239 | "outputs": [], 240 | "source": [ 241 | "s1.values" 242 | ] 243 | }, 244 | { 245 | "cell_type": "code", 246 | "execution_count": null, 247 | "metadata": { 248 | "collapsed": false 249 | }, 250 | "outputs": [], 251 | "source": [ 252 | "# Acceder a un elemento, como si fuera un diccionario\n", 253 | "s1['a']" 254 | ] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "execution_count": null, 259 | "metadata": { 260 | "collapsed": false 261 | }, 262 | "outputs": [], 263 | "source": [ 264 | "# Acceder a un elemento como si fuera un numpy array\n", 265 | "s1[0]" 266 | ] 267 | }, 268 | { 269 | "cell_type": "code", 270 | "execution_count": null, 271 | "metadata": { 272 | "collapsed": false 273 | }, 274 | "outputs": [], 275 | "source": [ 276 | "s1[0:3]" 277 | ] 278 | }, 279 | { 280 | "cell_type": "code", 281 | "execution_count": null, 282 | "metadata": { 283 | "collapsed": false 284 | }, 285 | "outputs": [], 286 | "source": [ 287 | "s1['a':'c']" 288 | ] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "execution_count": null, 293 | "metadata": { 294 | "collapsed": true 295 | }, 296 | "outputs": [], 297 | "source": [ 298 | "# Se pueden añadir nuevos elementos como si fuera un diccionario\n", 299 | "s1['e'] = 10000" 300 | ] 301 | }, 302 | { 303 | "cell_type": "code", 304 | "execution_count": null, 305 | "metadata": { 306 | "collapsed": false 307 | }, 308 | "outputs": [], 309 | "source": [ 310 | "s1" 311 | ] 312 | }, 313 | { 314 | "cell_type": "code", 315 | "execution_count": null, 316 | "metadata": { 317 | "collapsed": false 318 | }, 319 | "outputs": [], 320 | "source": [ 321 | "s2" 322 | ] 323 | }, 324 | { 325 | "cell_type": "code", 326 | "execution_count": null, 327 | "metadata": { 328 | "collapsed": false 329 | }, 330 | "outputs": [], 331 | "source": [ 332 | "# Podemos hacer operaciones como si fueran numpy arrays\n", 333 | "s1 / 10" 334 | ] 335 | }, 336 | { 337 | "cell_type": "code", 338 | "execution_count": null, 339 | "metadata": { 340 | "collapsed": false 341 | }, 342 | "outputs": [], 343 | "source": [ 344 | "# Alineamiento de índices, las operaciones se hacen vía índice\n", 345 | "s1 + s2" 346 | ] 347 | }, 348 | { 349 | "cell_type": "markdown", 350 | "metadata": {}, 351 | "source": [ 352 | "# `DataFrame`" 353 | ] 354 | }, 355 | { 356 | "cell_type": "markdown", 357 | "metadata": {}, 358 | "source": [ 359 | "Un `DataFrame` puede considerarse que es como un diccionario de `Series` que comparten índices comunes:\n", 360 | "\n", 361 | "• `Dataframes` tienen índices para filas (index) y columnas (columns).\n", 362 | "\n", 363 | "\n", 364 | "• Cada columna puede poseer un tipo de dato diferente.\n", 365 | "\n", 366 | "\n", 367 | "• Añadir nuevas columnas es 'barato'." 368 | ] 369 | }, 370 | { 371 | "cell_type": "code", 372 | "execution_count": null, 373 | "metadata": { 374 | "collapsed": false 375 | }, 376 | "outputs": [], 377 | "source": [ 378 | "df = pd.DataFrame(np.random.randn(10, 3), columns = ['col1', 'col2', 'col3'])" 379 | ] 380 | }, 381 | { 382 | "cell_type": "code", 383 | "execution_count": null, 384 | "metadata": { 385 | "collapsed": false 386 | }, 387 | "outputs": [], 388 | "source": [ 389 | "df" 390 | ] 391 | }, 392 | { 393 | "cell_type": "markdown", 394 | "metadata": {}, 395 | "source": [ 396 | "Pensad en un `DataFrame` como en una 'pestaña' (hoja) de una hoja de cálculos, o en una tabla en una Base de Datos SQL." 397 | ] 398 | }, 399 | { 400 | "cell_type": "code", 401 | "execution_count": null, 402 | "metadata": { 403 | "collapsed": false 404 | }, 405 | "outputs": [], 406 | "source": [ 407 | "help(pd.DataFrame)" 408 | ] 409 | }, 410 | { 411 | "cell_type": "code", 412 | "execution_count": null, 413 | "metadata": { 414 | "collapsed": true 415 | }, 416 | "outputs": [], 417 | "source": [ 418 | "# Cread un dataframe a partir de s1 y s2\n", 419 | "\n" 420 | ] 421 | }, 422 | { 423 | "cell_type": "code", 424 | "execution_count": null, 425 | "metadata": { 426 | "collapsed": true 427 | }, 428 | "outputs": [], 429 | "source": [ 430 | "# Cread un dataframe a partir de un diccionario de numpy arrays\n", 431 | "\n" 432 | ] 433 | }, 434 | { 435 | "cell_type": "code", 436 | "execution_count": null, 437 | "metadata": { 438 | "collapsed": false 439 | }, 440 | "outputs": [], 441 | "source": [ 442 | "# Se puede acceder a una columna (una Series) como si accediéramos a la clave de un diccionario\n", 443 | "df['col1']" 444 | ] 445 | }, 446 | { 447 | "cell_type": "code", 448 | "execution_count": null, 449 | "metadata": { 450 | "collapsed": false 451 | }, 452 | "outputs": [], 453 | "source": [ 454 | "# Podemos acceder a los índices como si fuera una Series\n", 455 | "df.index" 456 | ] 457 | }, 458 | { 459 | "cell_type": "code", 460 | "execution_count": null, 461 | "metadata": { 462 | "collapsed": false 463 | }, 464 | "outputs": [], 465 | "source": [ 466 | "# Además, podemos acceder a las columnas\n", 467 | "df.columns" 468 | ] 469 | }, 470 | { 471 | "cell_type": "markdown", 472 | "metadata": {}, 473 | "source": [ 474 | "# `Panel`" 475 | ] 476 | }, 477 | { 478 | "cell_type": "markdown", 479 | "metadata": {}, 480 | "source": [ 481 | "Se puede ver como un diccionario de `DataFrame`s. \n", 482 | "\n", 483 | "Para no sobrecargar el vistazo que vamos a hacer a `pandas` no hablaremos más sobre `Panel`es ya que es una estructura de datos que se usa menos en la práctica." 484 | ] 485 | }, 486 | { 487 | "cell_type": "code", 488 | "execution_count": null, 489 | "metadata": { 490 | "collapsed": false 491 | }, 492 | "outputs": [], 493 | "source": [ 494 | "help(pd.Panel)" 495 | ] 496 | } 497 | ], 498 | "metadata": { 499 | "kernelspec": { 500 | "display_name": "Python 3", 501 | "language": "python", 502 | "name": "python3" 503 | }, 504 | "language_info": { 505 | "codemirror_mode": { 506 | "name": "ipython", 507 | "version": 3 508 | }, 509 | "file_extension": ".py", 510 | "mimetype": "text/x-python", 511 | "name": "python", 512 | "nbconvert_exporter": "python", 513 | "pygments_lexer": "ipython3", 514 | "version": "3.5.1" 515 | } 516 | }, 517 | "nbformat": 4, 518 | "nbformat_minor": 0 519 | } 520 | -------------------------------------------------------------------------------- /extras/Excel/Lightning Talk - Destruir lo Bello.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "![Aropython_logo](./static/aeropython_name_mini.png)\n", 8 | "###### Siro Moreno Martín" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "# Cómo destruir y corromper todo lo es bueno y bello" 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | "## A veces, tenemos que usar Excel" 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "metadata": {}, 28 | "source": [ 29 | "Hay que reconocer que hay gente por ahí que hace maravillas, aunque me de cosilla pensarlo:\n", 30 | "\n", 31 | "https://www.youtube.com/watch?v=AmlqgQidXtk\n", 32 | "\n", 33 | "Pero el caso es que quién más, quién menos, todos alguna vez necesitamos tratar con archivos de Excel en algún momento de la vida. Y si tienes que trabajar con los datos y temes que si intentas programar una macro puedas invocar a Shub-Niggurath, siempre podemos contar con el poder de Anaconda!\n", 34 | "\n", 35 | "Para esta charla he utilizado openpyxl, pero hay otros paquetes que también permiten trabajar con ficheros de Excel, como xlsreader y xlsxwriter.\n", 36 | "\n", 37 | "Nota: para que los estilos del notebook se rendericen bien, haz File-> Trust Notebook. Si aún así no salen las letras de colores, ejecuta la última celda. Los colores importan because reasons." 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": null, 43 | "metadata": { 44 | "collapsed": true 45 | }, 46 | "outputs": [], 47 | "source": [ 48 | "#Vamos a importar numpy y pandas para jugar un poquillo\n", 49 | "import numpy as np\n", 50 | "import pandas as pd\n", 51 | "from openpyxl import Workbook\n", 52 | "from openpyxl import load_workbook" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": null, 58 | "metadata": { 59 | "collapsed": true 60 | }, 61 | "outputs": [], 62 | "source": [ 63 | "#También usaremos funciones para dibujar gráficos\n", 64 | "from openpyxl.chart import (\n", 65 | " ScatterChart,\n", 66 | " Reference,\n", 67 | " Series,\n", 68 | ")" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": {}, 74 | "source": [ 75 | "Empezaremos cargando a Python datos desde un documento de excel." 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": null, 81 | "metadata": { 82 | "collapsed": false 83 | }, 84 | "outputs": [], 85 | "source": [ 86 | "#Vamos a cargar el workbook existente\n", 87 | "wb_cargado = load_workbook(filename='datos.xlsx', read_only=True)#wb es un workbook iterable\n", 88 | "ws_cargado = wb_cargado['datos'] # El primer elemento de wb, ws, es una worksheet iterable" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": null, 94 | "metadata": { 95 | "collapsed": false, 96 | "scrolled": true 97 | }, 98 | "outputs": [], 99 | "source": [ 100 | "#Comprobamos que nuestros importantísimos datos científicos se han cargado\n", 101 | "for row in ws_cargado.rows: #Podemos iterar en las filas de la worksheet\n", 102 | " for cell in row: #Cada fila es iterable a su vez, cada elemento es una celda\n", 103 | " print(cell.value) #Las celdas ya no son iterables más. Gracias a los Dioses." 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": null, 109 | "metadata": { 110 | "collapsed": true 111 | }, 112 | "outputs": [], 113 | "source": [ 114 | "#Vamos a volcar los datos en algo así como más de Python, una lista de listas, que siempre mola.\n", 115 | "datos = []\n", 116 | "for row in ws_cargado.rows:\n", 117 | " fila = []\n", 118 | " for cell in row:\n", 119 | " fila.append(cell.value)\n", 120 | " datos.append(fila)" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": null, 126 | "metadata": { 127 | "collapsed": false 128 | }, 129 | "outputs": [], 130 | "source": [ 131 | "#Podemos pasar ahora estos datos a una tabla de pandas, por ejemplo. Pandas mola también.\n", 132 | "pd.DataFrame(datos)" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": null, 138 | "metadata": { 139 | "collapsed": false 140 | }, 141 | "outputs": [], 142 | "source": [ 143 | "#Si intentamos usar indexación como si fuera un array, no podremos.\n", 144 | "#Esto evita que los demonios de la dimensión J invadan la tierra.\n", 145 | "ws_cargado.rows[1:5]" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": null, 151 | "metadata": { 152 | "collapsed": false 153 | }, 154 | "outputs": [], 155 | "source": [ 156 | "#Tenemos que usar el método que ya viene para trazar un rectángulo alquímico de celdas:\n", 157 | "for row in ws_cargado.iter_rows('A1:B5'):\n", 158 | " for cell in row:\n", 159 | " print (cell.value)" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": null, 165 | "metadata": { 166 | "collapsed": false 167 | }, 168 | "outputs": [], 169 | "source": [ 170 | "#Inspeccionando los datos científicos, hemos descubierto que no son del todo correctos.\n", 171 | "#Debemos corregirlos a algo más apropiado a lo que es usar Excel\n", 172 | "nombres = []\n", 173 | "for row in ws_cargado.rows:\n", 174 | " nombres.append(row[1].value)\n", 175 | "\n", 176 | "new_data = []\n", 177 | "for word in nombres:\n", 178 | " word = word.replace('mariposas', 'avispas')\n", 179 | " word =word.replace('luz', 'oscuridad')\n", 180 | " word =word.replace('amor', 'sufrimiento')\n", 181 | " word =word.replace('paz','odio')\n", 182 | " word =word.replace('felicidad', 'desesperación')\n", 183 | " new_data.append(word)" 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": null, 189 | "metadata": { 190 | "collapsed": true 191 | }, 192 | "outputs": [], 193 | "source": [ 194 | "#Podemos también generar datos nuevos como siempre, que luego guardaremos.\n", 195 | "#Ahora, usaremos numpy para calcular vectores y mierdas así.\n", 196 | "#Las matematicas son algo así como científico, estos datos son ciencia también.\n", 197 | "theta = np.linspace(-0.5 * np.pi, 1.5 *np.pi, 100)\n", 198 | "theta_2 = np.linspace(1.5 * np.pi, 7.5*np.pi, 6)\n", 199 | "theta = np.concatenate((theta, theta_2))\n", 200 | "x = np.cos(theta)\n", 201 | "y = np.sin(theta)\n" 202 | ] 203 | }, 204 | { 205 | "cell_type": "markdown", 206 | "metadata": {}, 207 | "source": [ 208 | "Ahora que ya hemos cargado datos de un excel en Python, y que seguimos pudiendo usar otras librerías a pesar de tamaña atrocidad, crearemos un excel vacío nuevo para guardar toda nuestra mierda y mandársela a alguien que le importe." 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": null, 214 | "metadata": { 215 | "collapsed": true 216 | }, 217 | "outputs": [], 218 | "source": [ 219 | "#Crear workbook nuevo\n", 220 | "wb = Workbook()\n", 221 | "\n", 222 | "#Seleccionar la worksheet activa\n", 223 | "ws = wb.active" 224 | ] 225 | }, 226 | { 227 | "cell_type": "code", 228 | "execution_count": null, 229 | "metadata": { 230 | "collapsed": true 231 | }, 232 | "outputs": [], 233 | "source": [ 234 | "#Vamos a crear más páginas, simplemente porque podemos.\n", 235 | "ws1 = wb.create_sheet()\n", 236 | "ws1 = wb.create_sheet()" 237 | ] 238 | }, 239 | { 240 | "cell_type": "code", 241 | "execution_count": null, 242 | "metadata": { 243 | "collapsed": false 244 | }, 245 | "outputs": [], 246 | "source": [ 247 | "#Te interesa saber los nombres de las worksheet nuevas? Se puede!\n", 248 | "wb.get_sheet_names()" 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": null, 254 | "metadata": { 255 | "collapsed": false 256 | }, 257 | "outputs": [], 258 | "source": [ 259 | "#Otra manera de sacar los nombres de las cosas esas es ir iterando en el workbook:\n", 260 | "#Cada elemento es una worksheet.\n", 261 | "#A la gente que hizo esto les molaba la mierda esta de iterar cosas.\n", 262 | "for sheet in wb:\n", 263 | " print(sheet.title)" 264 | ] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": null, 269 | "metadata": { 270 | "collapsed": true 271 | }, 272 | "outputs": [], 273 | "source": [ 274 | "#Carguemos los datos corregidos en el workbook nuevo usando un bucle\n", 275 | "for row in range(len(new_data)):\n", 276 | " cell_name = 'A' + str(row + 1)\n", 277 | " ws[cell_name] = new_data[row]" 278 | ] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": null, 283 | "metadata": { 284 | "collapsed": false 285 | }, 286 | "outputs": [], 287 | "source": [ 288 | "#Con otro bucle vamos a cargar los datos generados por ciencia.\n", 289 | "colname = ['B','C']\n", 290 | "data = np.array([x, y])\n", 291 | "for col in range(2):\n", 292 | " for row in range(1, len(x)+1):\n", 293 | " letter = colname[col]\n", 294 | " name = letter + str(row)\n", 295 | " ws[name] = data[col, row - 1]\n" 296 | ] 297 | }, 298 | { 299 | "cell_type": "code", 300 | "execution_count": null, 301 | "metadata": { 302 | "collapsed": false 303 | }, 304 | "outputs": [], 305 | "source": [ 306 | "#Ahora vamos a añadir un gráfico al workbook. Es para lo que sirve excel, no?\n", 307 | "chart = ScatterChart()\n", 308 | "chart.title = \"Scatter Chart\"\n", 309 | "chart.style = 44\n", 310 | "chart.x_axis.title = 'Maldad'\n", 311 | "chart.y_axis.title = 'Obscenidad'\n", 312 | "\n", 313 | "#Añadimos una serie de datos para pintar la ciencia en la hoja\n", 314 | "xvalues = Reference(ws, min_col=2, min_row=1, max_row=len(x)+1)\n", 315 | "values = Reference(ws, min_col=3, min_row=1, max_row=len(x)+1)\n", 316 | "series = Series(values, xvalues, title_from_data=False)\n", 317 | "chart.series.append(series)\n", 318 | "\n" 319 | ] 320 | }, 321 | { 322 | "cell_type": "code", 323 | "execution_count": null, 324 | "metadata": { 325 | "collapsed": true 326 | }, 327 | "outputs": [], 328 | "source": [ 329 | "#El tamaño importa\n", 330 | "chart.height= 15\n", 331 | "chart.width = 18\n", 332 | "\n", 333 | "#Después de haberlo definido, lo añadimos al workbook cual pegote\n", 334 | "ws.add_chart(chart, \"C1\")" 335 | ] 336 | }, 337 | { 338 | "cell_type": "code", 339 | "execution_count": null, 340 | "metadata": { 341 | "collapsed": false 342 | }, 343 | "outputs": [], 344 | "source": [ 345 | "#Vamos a guardar nuestro coso horrible antes de que nos dé un colapso \n", 346 | "#Y el mundo se quede sin disfrutar de nuestra obra\n", 347 | "wb.save('resultado.xlsx')" 348 | ] 349 | }, 350 | { 351 | "cell_type": "markdown", 352 | "metadata": { 353 | "collapsed": false 354 | }, 355 | "source": [ 356 | "## Eso es todo artemaníacos! Ya podéis jugar con Excel y Python para crear aberraciones que pueblen la tierra!" 357 | ] 358 | }, 359 | { 360 | "cell_type": "markdown", 361 | "metadata": { 362 | "collapsed": true 363 | }, 364 | "source": [ 365 | "Espero que esta mini-charla te ayude a crear la **Ñapa Definitiva del Universo** que seguro que quieres fabricar, pero si aún así quieres saber más sobre este paquete, puedes consultar su documentación:\n", 366 | "\n", 367 | "https://openpyxl.readthedocs.org/en/default/index.html" 368 | ] 369 | }, 370 | { 371 | "cell_type": "code", 372 | "execution_count": 1, 373 | "metadata": { 374 | "collapsed": false 375 | }, 376 | "outputs": [ 377 | { 378 | "data": { 379 | "text/html": [ 380 | "\n", 381 | "\n", 382 | "\n", 383 | "\n", 506 | "\n" 522 | ], 523 | "text/plain": [ 524 | "" 525 | ] 526 | }, 527 | "execution_count": 1, 528 | "metadata": {}, 529 | "output_type": "execute_result" 530 | } 531 | ], 532 | "source": [ 533 | "# Notebook style. Para que salgan las letras de colorines.\n", 534 | "from IPython.core.display import HTML\n", 535 | "css_file = './static/style.css'\n", 536 | "HTML(open(css_file, \"r\").read())" 537 | ] 538 | } 539 | ], 540 | "metadata": { 541 | "kernelspec": { 542 | "display_name": "Python 3", 543 | "language": "python", 544 | "name": "python3" 545 | }, 546 | "language_info": { 547 | "codemirror_mode": { 548 | "name": "ipython", 549 | "version": 3 550 | }, 551 | "file_extension": ".py", 552 | "mimetype": "text/x-python", 553 | "name": "python", 554 | "nbconvert_exporter": "python", 555 | "pygments_lexer": "ipython3", 556 | "version": "3.5.1" 557 | } 558 | }, 559 | "nbformat": 4, 560 | "nbformat_minor": 0 561 | } 562 | -------------------------------------------------------------------------------- /Basic Python Packages for Science - Student.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "![Python Madrid Logo](./static/pycones2016.jpg)" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Basic Python Packages for Science\n", 15 | "## The Aeropython’s guide to the Python Galaxy! " 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | "![Aropython_logo](./static/aeropython_name_mini.png)\n", 23 | "###### Siro Moreno Martín\n", 24 | "###### Alejandro Sáez Mollejo" 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": {}, 30 | "source": [ 31 | "## Who are we? " 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "##### Siro Moreno " 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "\n", 46 | "* **Aerospace Engineer**\n", 47 | "* Working at: Instituto Nacional de Técnica Aeroespacial\n", 48 | "* Full-time Nerd\n", 49 | "* Have another workshop later because stupidity (please COME!!)" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": {}, 55 | "source": [ 56 | "##### Álex Sáez " 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": {}, 62 | "source": [ 63 | "\n", 64 | "\n", 65 | "* **Aerospace Engineer**\n", 66 | "* **Flight Test Methods Engineer** at AIRBUS Defence & Space on behalf of ALTRAN\n", 67 | "* Co-creator of AeroPython --> PyFME \n", 68 | "* Member of Python España (come and join us!)" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": {}, 74 | "source": [ 75 | "---" 76 | ] 77 | }, 78 | { 79 | "cell_type": "markdown", 80 | "metadata": {}, 81 | "source": [ 82 | "### 0. Introduction" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": {}, 88 | "source": [ 89 | "#### Python in the Scientific environment " 90 | ] 91 | }, 92 | { 93 | "cell_type": "markdown", 94 | "metadata": {}, 95 | "source": [ 96 | "##### Principal Python Packages for scientific purposes " 97 | ] 98 | }, 99 | { 100 | "cell_type": "markdown", 101 | "metadata": {}, 102 | "source": [ 103 | "##### Anaconda & conda" 104 | ] 105 | }, 106 | { 107 | "cell_type": "markdown", 108 | "metadata": {}, 109 | "source": [ 110 | "![conda](./static/conda.png)" 111 | ] 112 | }, 113 | { 114 | "cell_type": "markdown", 115 | "metadata": {}, 116 | "source": [ 117 | "http://conda.pydata.org/docs/intro.html\n", 118 | "\n", 119 | "Conda is a package manager application that quickly installs, runs, and updates packages and their dependencies. The conda command is the primary interface for managing installations of various packages. It can query and search the package index and current installation, create new environments, and install and update packages into existing conda environments. " 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": null, 125 | "metadata": { 126 | "collapsed": false 127 | }, 128 | "outputs": [], 129 | "source": [ 130 | "from IPython.display import HTML\n", 131 | "HTML('')" 132 | ] 133 | }, 134 | { 135 | "cell_type": "markdown", 136 | "metadata": {}, 137 | "source": [ 138 | "##### Main objectives of this workshop \n", 139 | "\n", 140 | "* Provide you with a **first insight into the principal Python tools & libraries used in Science**:\n", 141 | " - conda.\n", 142 | " - Jupyter Notebook and preview of JupyterLab\n", 143 | " - NumPy, matplotlib, SciPy\n", 144 | " - SymPy\n", 145 | " " 146 | ] 147 | }, 148 | { 149 | "cell_type": "markdown", 150 | "metadata": {}, 151 | "source": [ 152 | "##### Other common libraries that will not be covered here are:\n", 153 | "\n", 154 | "- Pandas\n", 155 | "- scikit-learn\n", 156 | "- Numba & Cython" 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "metadata": {}, 162 | "source": [ 163 | "### 1. Jupyter Notebook" 164 | ] 165 | }, 166 | { 167 | "cell_type": "markdown", 168 | "metadata": {}, 169 | "source": [ 170 | "![jupyter](./static/jupyter-logo.png)" 171 | ] 172 | }, 173 | { 174 | "cell_type": "markdown", 175 | "metadata": {}, 176 | "source": [ 177 | "The Jupyter Notebook is a web application that allows you to create and share documents that contain live code, equations, visualizations and explanatory text. Uses include: data cleaning and transformation, numerical simulation, statistical modeling, machine learning and much more.\n", 178 | "\n", 179 | "It has been widely recognised as a great way to distribute scientific papers, because of the capability to have an integrated format with text and executable code, highly reproducible. Top level investigators around the world are already using it, like the team behind the Gravitational Waves discovery (LIGO), whose analysis was translated to an interactive dowloadable Jupyter notebook. You can see it here: https://github.com/minrk/ligo-binder/blob/master/GW150914_tutorial.ipynb" 180 | ] 181 | }, 182 | { 183 | "cell_type": "markdown", 184 | "metadata": {}, 185 | "source": [ 186 | "### 2. Using arrays: NumPy " 187 | ] 188 | }, 189 | { 190 | "cell_type": "markdown", 191 | "metadata": {}, 192 | "source": [ 193 | "![numpy-logo](./static/numpy.png)" 194 | ] 195 | }, 196 | { 197 | "cell_type": "markdown", 198 | "metadata": {}, 199 | "source": [ 200 | "#### ndarray object " 201 | ] 202 | }, 203 | { 204 | "cell_type": "markdown", 205 | "metadata": {}, 206 | "source": [ 207 | "| index | 0 | 1 | 2 | 3 | ... | n-1 | n |\n", 208 | "| ---------- | :---: | :---: | :---: | :---: | :---: | :---: | :---: |\n", 209 | "| value | 2.1 | 3.6 | 7.8 | 1.5 | ... | 5.4 | 6.3 |" 210 | ] 211 | }, 212 | { 213 | "cell_type": "markdown", 214 | "metadata": {}, 215 | "source": [ 216 | "* N-dimensional data structure.\n", 217 | "* Homogeneously typed.\n", 218 | "* Efficient!\n", 219 | "\n", 220 | "A universal function (or ufunc for short) is a function that operates on ndarrays. It is a “**vectorized** function\"." 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": null, 226 | "metadata": { 227 | "collapsed": true 228 | }, 229 | "outputs": [], 230 | "source": [ 231 | "# Let's import numpy\n" 232 | ] 233 | }, 234 | { 235 | "cell_type": "markdown", 236 | "metadata": {}, 237 | "source": [ 238 | "Let's have a look at the efficiency of a ndarray against the efficiency of a list:" 239 | ] 240 | }, 241 | { 242 | "cell_type": "code", 243 | "execution_count": null, 244 | "metadata": { 245 | "collapsed": true 246 | }, 247 | "outputs": [], 248 | "source": [ 249 | "# List \n", 250 | "my_list = \n", 251 | "# Array \n", 252 | "my_array = " 253 | ] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": null, 258 | "metadata": { 259 | "collapsed": false 260 | }, 261 | "outputs": [], 262 | "source": [ 263 | "# Let's check how long does it take to add all the elements?" 264 | ] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": null, 269 | "metadata": { 270 | "collapsed": false 271 | }, 272 | "outputs": [], 273 | "source": [ 274 | "%timeit " 275 | ] 276 | }, 277 | { 278 | "cell_type": "markdown", 279 | "metadata": {}, 280 | "source": [ 281 | "Another example:" 282 | ] 283 | }, 284 | { 285 | "cell_type": "code", 286 | "execution_count": null, 287 | "metadata": { 288 | "collapsed": false 289 | }, 290 | "outputs": [], 291 | "source": [ 292 | "# Square each element\n" 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "execution_count": null, 298 | "metadata": { 299 | "collapsed": false 300 | }, 301 | "outputs": [], 302 | "source": [] 303 | }, 304 | { 305 | "cell_type": "markdown", 306 | "metadata": {}, 307 | "source": [ 308 | "#### Array creation" 309 | ] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "execution_count": null, 314 | "metadata": { 315 | "collapsed": false 316 | }, 317 | "outputs": [], 318 | "source": [ 319 | "# [1, 2, 3, 4]\n", 320 | "one_dim_array = \n", 321 | "one_dim_array" 322 | ] 323 | }, 324 | { 325 | "cell_type": "code", 326 | "execution_count": null, 327 | "metadata": { 328 | "collapsed": false 329 | }, 330 | "outputs": [], 331 | "source": [ 332 | "# [1, 2, 3],\n", 333 | "# [4, 5, 6],\n", 334 | "# [7, 8, 9]\n", 335 | "\n", 336 | "two_dim_array = \n", 337 | "two_dim_array" 338 | ] 339 | }, 340 | { 341 | "cell_type": "code", 342 | "execution_count": null, 343 | "metadata": { 344 | "collapsed": false 345 | }, 346 | "outputs": [], 347 | "source": [ 348 | "# size\n" 349 | ] 350 | }, 351 | { 352 | "cell_type": "code", 353 | "execution_count": null, 354 | "metadata": { 355 | "collapsed": false 356 | }, 357 | "outputs": [], 358 | "source": [ 359 | "# shape\n" 360 | ] 361 | }, 362 | { 363 | "cell_type": "code", 364 | "execution_count": null, 365 | "metadata": { 366 | "collapsed": false 367 | }, 368 | "outputs": [], 369 | "source": [ 370 | "# dtype\n" 371 | ] 372 | }, 373 | { 374 | "cell_type": "code", 375 | "execution_count": null, 376 | "metadata": { 377 | "collapsed": false 378 | }, 379 | "outputs": [], 380 | "source": [ 381 | "# change dtype\n" 382 | ] 383 | }, 384 | { 385 | "cell_type": "code", 386 | "execution_count": null, 387 | "metadata": { 388 | "collapsed": true 389 | }, 390 | "outputs": [], 391 | "source": [ 392 | "# Other interesting functions for array creation\n", 393 | "# zeros\n", 394 | "zeros_arr = " 395 | ] 396 | }, 397 | { 398 | "cell_type": "code", 399 | "execution_count": null, 400 | "metadata": { 401 | "collapsed": true 402 | }, 403 | "outputs": [], 404 | "source": [ 405 | "# ones\n", 406 | "ones_arr = " 407 | ] 408 | }, 409 | { 410 | "cell_type": "code", 411 | "execution_count": null, 412 | "metadata": { 413 | "collapsed": false 414 | }, 415 | "outputs": [], 416 | "source": [ 417 | "# identity\n", 418 | "eye_arr = " 419 | ] 420 | }, 421 | { 422 | "cell_type": "code", 423 | "execution_count": null, 424 | "metadata": { 425 | "collapsed": false 426 | }, 427 | "outputs": [], 428 | "source": [ 429 | "# range\n", 430 | "range_arr = \n", 431 | "range_arr" 432 | ] 433 | }, 434 | { 435 | "cell_type": "code", 436 | "execution_count": null, 437 | "metadata": { 438 | "collapsed": false 439 | }, 440 | "outputs": [], 441 | "source": [ 442 | "# reshape\n" 443 | ] 444 | }, 445 | { 446 | "cell_type": "code", 447 | "execution_count": null, 448 | "metadata": { 449 | "collapsed": false 450 | }, 451 | "outputs": [], 452 | "source": [ 453 | "# linspace\n" 454 | ] 455 | }, 456 | { 457 | "cell_type": "markdown", 458 | "metadata": {}, 459 | "source": [ 460 | "#### Basic slicing " 461 | ] 462 | }, 463 | { 464 | "cell_type": "code", 465 | "execution_count": null, 466 | "metadata": { 467 | "collapsed": false 468 | }, 469 | "outputs": [], 470 | "source": [ 471 | "one_dim_array" 472 | ] 473 | }, 474 | { 475 | "cell_type": "code", 476 | "execution_count": null, 477 | "metadata": { 478 | "collapsed": false 479 | }, 480 | "outputs": [], 481 | "source": [ 482 | "two_dim_array" 483 | ] 484 | }, 485 | { 486 | "cell_type": "markdown", 487 | "metadata": {}, 488 | "source": [ 489 | "`[start:stop:step]`" 490 | ] 491 | }, 492 | { 493 | "cell_type": "code", 494 | "execution_count": null, 495 | "metadata": { 496 | "collapsed": false 497 | }, 498 | "outputs": [], 499 | "source": [ 500 | "my_arr = np.arange(100)\n", 501 | "# odd numbers\n" 502 | ] 503 | }, 504 | { 505 | "cell_type": "code", 506 | "execution_count": null, 507 | "metadata": { 508 | "collapsed": false 509 | }, 510 | "outputs": [], 511 | "source": [ 512 | "chess_board = np.zeros([8, 8], dtype=int)\n", 513 | "\n", 514 | "\n", 515 | "\n", 516 | "chess_board" 517 | ] 518 | }, 519 | { 520 | "cell_type": "markdown", 521 | "metadata": {}, 522 | "source": [ 523 | "### 2. Drawing: Matplotlib" 524 | ] 525 | }, 526 | { 527 | "cell_type": "markdown", 528 | "metadata": {}, 529 | "source": [ 530 | "![Matplotlib-logo](./static/matplotlib.png)" 531 | ] 532 | }, 533 | { 534 | "cell_type": "code", 535 | "execution_count": null, 536 | "metadata": { 537 | "collapsed": true 538 | }, 539 | "outputs": [], 540 | "source": [ 541 | "# Import matplotlib and let's paint the chess board\n" 542 | ] 543 | }, 544 | { 545 | "cell_type": "code", 546 | "execution_count": null, 547 | "metadata": { 548 | "collapsed": false 549 | }, 550 | "outputs": [], 551 | "source": [] 552 | }, 553 | { 554 | "cell_type": "markdown", 555 | "metadata": {}, 556 | "source": [ 557 | "#### Operations & linalg " 558 | ] 559 | }, 560 | { 561 | "cell_type": "code", 562 | "execution_count": null, 563 | "metadata": { 564 | "collapsed": false 565 | }, 566 | "outputs": [], 567 | "source": [ 568 | "# Let's plot a cosine\n" 569 | ] 570 | }, 571 | { 572 | "cell_type": "code", 573 | "execution_count": null, 574 | "metadata": { 575 | "collapsed": false 576 | }, 577 | "outputs": [], 578 | "source": [ 579 | "plt.plot(x, y)" 580 | ] 581 | }, 582 | { 583 | "cell_type": "code", 584 | "execution_count": null, 585 | "metadata": { 586 | "collapsed": false 587 | }, 588 | "outputs": [], 589 | "source": [ 590 | "# Creating a 2d array\n", 591 | "two_dim_array = np.array([[10, 25, 33],\n", 592 | " [40, 25, 16],\n", 593 | " [77, 68, 91]])\n", 594 | "\n", 595 | "two_dim_array.T" 596 | ] 597 | }, 598 | { 599 | "cell_type": "code", 600 | "execution_count": null, 601 | "metadata": { 602 | "collapsed": false 603 | }, 604 | "outputs": [], 605 | "source": [ 606 | "# matrix multiplication\n" 607 | ] 608 | }, 609 | { 610 | "cell_type": "code", 611 | "execution_count": null, 612 | "metadata": { 613 | "collapsed": false 614 | }, 615 | "outputs": [], 616 | "source": [ 617 | "# matrix vector\n", 618 | "one_dim_array = np.array([2.5, 3.6, 3.8])\n", 619 | "\n" 620 | ] 621 | }, 622 | { 623 | "cell_type": "code", 624 | "execution_count": null, 625 | "metadata": { 626 | "collapsed": false 627 | }, 628 | "outputs": [], 629 | "source": [ 630 | "# inv\n", 631 | "np.linalg.inv(two_dim_array)" 632 | ] 633 | }, 634 | { 635 | "cell_type": "code", 636 | "execution_count": null, 637 | "metadata": { 638 | "collapsed": false 639 | }, 640 | "outputs": [], 641 | "source": [ 642 | "# eigenvectors & eigenvalues\n", 643 | "np.linalg.eig(two_dim_array)" 644 | ] 645 | }, 646 | { 647 | "cell_type": "markdown", 648 | "metadata": {}, 649 | "source": [ 650 | "#### Air quality data " 651 | ] 652 | }, 653 | { 654 | "cell_type": "code", 655 | "execution_count": null, 656 | "metadata": { 657 | "collapsed": false 658 | }, 659 | "outputs": [], 660 | "source": [ 661 | "from IPython.display import HTML\n", 662 | "HTML('')" 664 | ] 665 | }, 666 | { 667 | "cell_type": "markdown", 668 | "metadata": {}, 669 | "source": [ 670 | "##### Loading the data " 671 | ] 672 | }, 673 | { 674 | "cell_type": "code", 675 | "execution_count": 1, 676 | "metadata": { 677 | "collapsed": false 678 | }, 679 | "outputs": [ 680 | { 681 | "name": "stdout", 682 | "output_type": "stream", 683 | "text": [ 684 | "Estación: Barrio del Pilar;;;;\r\n", 685 | "Fecha;Hora;CO;NO2;O3\r\n", 686 | ";;mg/m³;µg/m³;µg/m³\r\n", 687 | "22/03/2016;01:00;0.2;14;73\r\n", 688 | "22/03/2016;02:00;0.2;10;77\r\n", 689 | "22/03/2016;03:00;0.2;9;75\r\n", 690 | "22/03/2016;04:00;0.2;3;81\r\n", 691 | "22/03/2016;05:00;0.2;3;81\r\n", 692 | "22/03/2016;06:00;0.2;6;79\r\n", 693 | "22/03/2016;07:00;0.2;24;59\r\n" 694 | ] 695 | } 696 | ], 697 | "source": [ 698 | "# Linux command \n", 699 | "!head ./data/barrio_del_pilar-20160322.csv\n", 700 | "\n", 701 | "# Windows\n", 702 | "# !gc log.txt | select -first 10 # head" 703 | ] 704 | }, 705 | { 706 | "cell_type": "code", 707 | "execution_count": null, 708 | "metadata": { 709 | "collapsed": false 710 | }, 711 | "outputs": [], 712 | "source": [ 713 | "# loading the data\n", 714 | "# ./data/barrio_del_pilar-20160322.csv\n", 715 | "data1 = np.genfromtxt()\n", 716 | "data1" 717 | ] 718 | }, 719 | { 720 | "cell_type": "markdown", 721 | "metadata": {}, 722 | "source": [ 723 | "##### Dealing with missing values " 724 | ] 725 | }, 726 | { 727 | "cell_type": "code", 728 | "execution_count": null, 729 | "metadata": { 730 | "collapsed": false 731 | }, 732 | "outputs": [], 733 | "source": [ 734 | "# mean\n" 735 | ] 736 | }, 737 | { 738 | "cell_type": "code", 739 | "execution_count": null, 740 | "metadata": { 741 | "collapsed": false, 742 | "scrolled": true 743 | }, 744 | "outputs": [], 745 | "source": [ 746 | "# an alternative\n" 747 | ] 748 | }, 749 | { 750 | "cell_type": "code", 751 | "execution_count": null, 752 | "metadata": { 753 | "collapsed": false 754 | }, 755 | "outputs": [], 756 | "source": [ 757 | "# another alternative: masking invalid data\n", 758 | "data1 = np.ma.masked_invalid(data1)\n" 759 | ] 760 | }, 761 | { 762 | "cell_type": "code", 763 | "execution_count": null, 764 | "metadata": { 765 | "collapsed": true 766 | }, 767 | "outputs": [], 768 | "source": [ 769 | "data2 = np.genfromtxt('./data/barrio_del_pilar-20151222.csv',\n", 770 | " skip_header=3,\n", 771 | " delimiter=';',\n", 772 | " usecols=(2,3,4))\n", 773 | "data2 = np.ma.masked_invalid(data2)" 774 | ] 775 | }, 776 | { 777 | "cell_type": "markdown", 778 | "metadata": {}, 779 | "source": [ 780 | "##### Plotting the data " 781 | ] 782 | }, 783 | { 784 | "cell_type": "markdown", 785 | "metadata": {}, 786 | "source": [ 787 | "** Maximum values ** from: http://www.mambiente.munimadrid.es/opencms/export/sites/default/calaire/Anexos/valores_limite_1.pdf" 788 | ] 789 | }, 790 | { 791 | "cell_type": "markdown", 792 | "metadata": {}, 793 | "source": [ 794 | "* NO2\n", 795 | " - Media anual: 40 µg/m3\n", 796 | " - **Media horaria: 200 µg/m3 **" 797 | ] 798 | }, 799 | { 800 | "cell_type": "code", 801 | "execution_count": null, 802 | "metadata": { 803 | "collapsed": false 804 | }, 805 | "outputs": [], 806 | "source": [ 807 | "# plot\n", 808 | "\n", 809 | "\n", 810 | "# legend\n", 811 | "\n", 812 | "\n", 813 | "# limits and level line\n" 814 | ] 815 | }, 816 | { 817 | "cell_type": "code", 818 | "execution_count": null, 819 | "metadata": { 820 | "collapsed": false 821 | }, 822 | "outputs": [], 823 | "source": [ 824 | "from IPython.display import HTML\n", 825 | "HTML('')" 826 | ] 827 | }, 828 | { 829 | "cell_type": "markdown", 830 | "metadata": {}, 831 | "source": [ 832 | "* CO \n", 833 | " - **Máxima diaria de las medias móviles octohorarias: 10 mg/m³**" 834 | ] 835 | }, 836 | { 837 | "cell_type": "code", 838 | "execution_count": null, 839 | "metadata": { 840 | "collapsed": true 841 | }, 842 | "outputs": [], 843 | "source": [ 844 | "# http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.convolve.html\n", 845 | "def moving_average(x, N=8):\n", 846 | " return np.convolve(x, np.ones(N)/N, mode='same')" 847 | ] 848 | }, 849 | { 850 | "cell_type": "code", 851 | "execution_count": null, 852 | "metadata": { 853 | "collapsed": false 854 | }, 855 | "outputs": [], 856 | "source": [ 857 | "plt.plot()\n", 858 | "plt.plot()\n", 859 | "\n", 860 | "plt.hlines(10, 0, 250, linestyles='--')\n", 861 | "plt.ylim(0, 11)\n", 862 | "\n", 863 | "plt.legend()" 864 | ] 865 | }, 866 | { 867 | "cell_type": "markdown", 868 | "metadata": {}, 869 | "source": [ 870 | "* O3\n", 871 | " - **Máxima diaria de las medias móviles octohorarias: 120 µg/m3**\n", 872 | " - Umbral de información. 180 µg/m3\n", 873 | " - Media horaria. Umbral de alerta. 240 µg/m3" 874 | ] 875 | }, 876 | { 877 | "cell_type": "code", 878 | "execution_count": null, 879 | "metadata": { 880 | "collapsed": false 881 | }, 882 | "outputs": [], 883 | "source": [ 884 | "plt.plot(moving_average(data1[:, 2]), label='2016')\n", 885 | "#plt.plot(data1[:, 2])\n", 886 | "\n", 887 | "plt.plot(moving_average(data2[:, 2]), label='2015')\n", 888 | "#plt.plot(data2[:, 2])\n", 889 | "\n", 890 | "plt.hlines(180, 0, 250, linestyles='--')\n", 891 | "plt.ylim(0, 190)\n", 892 | "\n", 893 | "plt.legend()" 894 | ] 895 | }, 896 | { 897 | "cell_type": "markdown", 898 | "metadata": {}, 899 | "source": [ 900 | "### 4. Scientific functions: SciPy" 901 | ] 902 | }, 903 | { 904 | "cell_type": "markdown", 905 | "metadata": {}, 906 | "source": [ 907 | "![matplotlib](./static/scipy_logo.png)" 908 | ] 909 | }, 910 | { 911 | "cell_type": "markdown", 912 | "metadata": {}, 913 | "source": [ 914 | "```\n", 915 | "scipy.linalg: ATLAS LAPACK and BLAS libraries\n", 916 | "\n", 917 | "scipy.stats: distributions, statistical functions...\n", 918 | "\n", 919 | "scipy.integrate: integration of functions and ODEs\n", 920 | "\n", 921 | "scipy.optimization: local and global optimization, fitting, root finding...\n", 922 | "\n", 923 | "scipy.interpolate: interpolation, splines...\n", 924 | "\n", 925 | "scipy.fftpack: Fourier trasnforms\n", 926 | "\n", 927 | "scipy.signal, scipy.special, scipy.io\n", 928 | "```" 929 | ] 930 | }, 931 | { 932 | "cell_type": "markdown", 933 | "metadata": {}, 934 | "source": [ 935 | "#### Temperature and precipitation data " 936 | ] 937 | }, 938 | { 939 | "cell_type": "markdown", 940 | "metadata": {}, 941 | "source": [ 942 | "Now, we will use some temperature data from Consejeria Agricultura Pesca y Desarrollo Rural Andalucía." 943 | ] 944 | }, 945 | { 946 | "cell_type": "code", 947 | "execution_count": null, 948 | "metadata": { 949 | "collapsed": false 950 | }, 951 | "outputs": [], 952 | "source": [ 953 | "HTML('')" 954 | ] 955 | }, 956 | { 957 | "cell_type": "markdown", 958 | "metadata": {}, 959 | "source": [ 960 | "The file contains data from 2004 to 2015 (included). Each row corresponds to a day of the year, so evey 365 lines contain data from a whole year*\n", 961 | "\n", 962 | "Note1: 29th February has been removed for leap-years.\n", 963 | "Note2: Missing values have been replaced with the immediately prior valid data.\n", 964 | "\n", 965 | "These kind of events are better handled with Pandas!" 966 | ] 967 | }, 968 | { 969 | "cell_type": "code", 970 | "execution_count": null, 971 | "metadata": { 972 | "collapsed": false 973 | }, 974 | "outputs": [], 975 | "source": [ 976 | "!head 'data/tabernas_meteo_data.csv'" 977 | ] 978 | }, 979 | { 980 | "cell_type": "code", 981 | "execution_count": null, 982 | "metadata": { 983 | "collapsed": true 984 | }, 985 | "outputs": [], 986 | "source": [ 987 | "data_file = 'data/tabernas_meteo_data.csv'" 988 | ] 989 | }, 990 | { 991 | "cell_type": "code", 992 | "execution_count": null, 993 | "metadata": { 994 | "collapsed": false 995 | }, 996 | "outputs": [], 997 | "source": [ 998 | "# Loading the data\n", 999 | "temp_data = np.genfromtxt()" 1000 | ] 1001 | }, 1002 | { 1003 | "cell_type": "code", 1004 | "execution_count": null, 1005 | "metadata": { 1006 | "collapsed": false 1007 | }, 1008 | "outputs": [], 1009 | "source": [ 1010 | "# Importing SciPy stats\n", 1011 | "import scipy.stats as st" 1012 | ] 1013 | }, 1014 | { 1015 | "cell_type": "code", 1016 | "execution_count": null, 1017 | "metadata": { 1018 | "collapsed": false 1019 | }, 1020 | "outputs": [], 1021 | "source": [ 1022 | "# Applying some functions: describe, mode, mean...\n" 1023 | ] 1024 | }, 1025 | { 1026 | "cell_type": "code", 1027 | "execution_count": null, 1028 | "metadata": { 1029 | "collapsed": false 1030 | }, 1031 | "outputs": [], 1032 | "source": [] 1033 | }, 1034 | { 1035 | "cell_type": "code", 1036 | "execution_count": null, 1037 | "metadata": { 1038 | "collapsed": false 1039 | }, 1040 | "outputs": [], 1041 | "source": [] 1042 | }, 1043 | { 1044 | "cell_type": "code", 1045 | "execution_count": null, 1046 | "metadata": { 1047 | "collapsed": false 1048 | }, 1049 | "outputs": [], 1050 | "source": [] 1051 | }, 1052 | { 1053 | "cell_type": "markdown", 1054 | "metadata": {}, 1055 | "source": [ 1056 | "We can also get information about **percentiles**!" 1057 | ] 1058 | }, 1059 | { 1060 | "cell_type": "code", 1061 | "execution_count": null, 1062 | "metadata": { 1063 | "collapsed": false 1064 | }, 1065 | "outputs": [], 1066 | "source": [ 1067 | "st.scoreatpercentile()" 1068 | ] 1069 | }, 1070 | { 1071 | "cell_type": "code", 1072 | "execution_count": null, 1073 | "metadata": { 1074 | "collapsed": false 1075 | }, 1076 | "outputs": [], 1077 | "source": [ 1078 | "st.percentileofscore()" 1079 | ] 1080 | }, 1081 | { 1082 | "cell_type": "markdown", 1083 | "metadata": {}, 1084 | "source": [ 1085 | "##### Rearranging the data " 1086 | ] 1087 | }, 1088 | { 1089 | "cell_type": "markdown", 1090 | "metadata": {}, 1091 | "source": [ 1092 | "![3d_array](./static/3d_array.png)" 1093 | ] 1094 | }, 1095 | { 1096 | "cell_type": "code", 1097 | "execution_count": null, 1098 | "metadata": { 1099 | "collapsed": false 1100 | }, 1101 | "outputs": [], 1102 | "source": [ 1103 | "n_years = 15\n", 1104 | "n_columns = temp_data.shape[1]\n", 1105 | "temp_data2 = np.zeros([365, n_columns, n_years])\n", 1106 | "\n", 1107 | "for year in range(n_years):\n", 1108 | " temp_data2[:, :, year] = temp_data[year*365:(year+1)*365, :]\n", 1109 | " temp_data2[:, :, year] = temp_data2[::-1, :, year]" 1110 | ] 1111 | }, 1112 | { 1113 | "cell_type": "code", 1114 | "execution_count": null, 1115 | "metadata": { 1116 | "collapsed": false 1117 | }, 1118 | "outputs": [], 1119 | "source": [ 1120 | "# Calculating mean of mean temp\n", 1121 | "mean_mean = \n", 1122 | "# max of max\n", 1123 | "max_max = \n", 1124 | "# min of min\n", 1125 | "min_min = " 1126 | ] 1127 | }, 1128 | { 1129 | "cell_type": "markdown", 1130 | "metadata": {}, 1131 | "source": [ 1132 | "##### Let's visualize data! " 1133 | ] 1134 | }, 1135 | { 1136 | "cell_type": "markdown", 1137 | "metadata": {}, 1138 | "source": [ 1139 | "Using matplotlib styles http://matplotlib.org/users/whats_new.html#styles" 1140 | ] 1141 | }, 1142 | { 1143 | "cell_type": "code", 1144 | "execution_count": null, 1145 | "metadata": { 1146 | "collapsed": false 1147 | }, 1148 | "outputs": [], 1149 | "source": [ 1150 | "%matplotlib inline\n", 1151 | "plt.style.available" 1152 | ] 1153 | }, 1154 | { 1155 | "cell_type": "code", 1156 | "execution_count": null, 1157 | "metadata": { 1158 | "collapsed": false 1159 | }, 1160 | "outputs": [], 1161 | "source": [ 1162 | "plt.style.use('ggplot')" 1163 | ] 1164 | }, 1165 | { 1166 | "cell_type": "code", 1167 | "execution_count": null, 1168 | "metadata": { 1169 | "collapsed": false 1170 | }, 1171 | "outputs": [], 1172 | "source": [ 1173 | "days = np.arange(1, 366)\n", 1174 | "\n", 1175 | "plt.fill_between()\n", 1176 | "plt.plot()\n", 1177 | "plt.xlim()" 1178 | ] 1179 | }, 1180 | { 1181 | "cell_type": "markdown", 1182 | "metadata": {}, 1183 | "source": [ 1184 | "Let's see if 2015 was a normal year..." 1185 | ] 1186 | }, 1187 | { 1188 | "cell_type": "code", 1189 | "execution_count": null, 1190 | "metadata": { 1191 | "collapsed": false 1192 | }, 1193 | "outputs": [], 1194 | "source": [ 1195 | "# Mean of 2015\n", 1196 | "\n", 1197 | "# Mean of means\n", 1198 | "\n", 1199 | "\n", 1200 | "plt.xlim(1, 365)" 1201 | ] 1202 | }, 1203 | { 1204 | "cell_type": "code", 1205 | "execution_count": null, 1206 | "metadata": { 1207 | "collapsed": false 1208 | }, 1209 | "outputs": [], 1210 | "source": [ 1211 | "# range \n", 1212 | "plt.fill_between(days, max_max, min_min, alpha=0.7)\n", 1213 | "# range 2015\n", 1214 | "plt.fill_between(days, temp_data2[:,0,-1], temp_data2[:,1,-1],\n", 1215 | " color='purple', alpha=0.5)\n", 1216 | "# mean\n", 1217 | "plt.plot(days, temp_data2[:,2,-1], lw=2)\n", 1218 | "plt.xlim(1, 365)" 1219 | ] 1220 | }, 1221 | { 1222 | "cell_type": "markdown", 1223 | "metadata": {}, 1224 | "source": [ 1225 | "For example, lets represent a function over a 2D domain!\n", 1226 | "\n", 1227 | "For this we will use the contour function, which requires some special inputs..." 1228 | ] 1229 | }, 1230 | { 1231 | "cell_type": "code", 1232 | "execution_count": null, 1233 | "metadata": { 1234 | "collapsed": true 1235 | }, 1236 | "outputs": [], 1237 | "source": [ 1238 | "#we will use numpy functions in order to work with numpy arrays\n", 1239 | "def funcion(x,y):\n", 1240 | " return np.cos(x) + np.sin(y)" 1241 | ] 1242 | }, 1243 | { 1244 | "cell_type": "code", 1245 | "execution_count": null, 1246 | "metadata": { 1247 | "collapsed": false 1248 | }, 1249 | "outputs": [], 1250 | "source": [ 1251 | "# 0D: works!\n", 1252 | "funcion(3,5)" 1253 | ] 1254 | }, 1255 | { 1256 | "cell_type": "code", 1257 | "execution_count": null, 1258 | "metadata": { 1259 | "collapsed": false 1260 | }, 1261 | "outputs": [], 1262 | "source": [ 1263 | "# 1D: works!\n", 1264 | "x = np.linspace(0, 5, 100)\n", 1265 | "plt.plot(x, funcion(x,1))" 1266 | ] 1267 | }, 1268 | { 1269 | "cell_type": "markdown", 1270 | "metadata": {}, 1271 | "source": [ 1272 | "### Matplotlib AGAIN, Now with 2D COLOURSSS!!1!1!11" 1273 | ] 1274 | }, 1275 | { 1276 | "cell_type": "markdown", 1277 | "metadata": {}, 1278 | "source": [ 1279 | "![Matplotlib-logo](./static/matplotlib.png)" 1280 | ] 1281 | }, 1282 | { 1283 | "cell_type": "markdown", 1284 | "metadata": {}, 1285 | "source": [ 1286 | "For example, lets represent a function over a 2D domain!\n", 1287 | "\n", 1288 | "For this we will use the contour function, which requires some special inputs..." 1289 | ] 1290 | }, 1291 | { 1292 | "cell_type": "code", 1293 | "execution_count": null, 1294 | "metadata": { 1295 | "collapsed": false 1296 | }, 1297 | "outputs": [], 1298 | "source": [ 1299 | "#we will use numpy functions in order to work with numpy arrays\n", 1300 | "def funcion(x,y):\n", 1301 | " '''This is the help of the function funcion.\n", 1302 | " This function receives two parameters and returns another.\n", 1303 | " Such sicence. Wow.'''\n", 1304 | " return ????" 1305 | ] 1306 | }, 1307 | { 1308 | "cell_type": "code", 1309 | "execution_count": null, 1310 | "metadata": { 1311 | "collapsed": false 1312 | }, 1313 | "outputs": [], 1314 | "source": [ 1315 | "# 0D: works?\n", 1316 | "funcion(,)" 1317 | ] 1318 | }, 1319 | { 1320 | "cell_type": "code", 1321 | "execution_count": null, 1322 | "metadata": { 1323 | "collapsed": false 1324 | }, 1325 | "outputs": [], 1326 | "source": [ 1327 | "# 1D: works?\n", 1328 | "x = np.???(0,5, 100)\n", 1329 | "z = funcion(,)\n", 1330 | "z" 1331 | ] 1332 | }, 1333 | { 1334 | "cell_type": "code", 1335 | "execution_count": null, 1336 | "metadata": { 1337 | "collapsed": true 1338 | }, 1339 | "outputs": [], 1340 | "source": [ 1341 | "plt.plot(x, z)" 1342 | ] 1343 | }, 1344 | { 1345 | "cell_type": "markdown", 1346 | "metadata": {}, 1347 | "source": [ 1348 | "In oder to plot the 2D function, we will need a grid.\n", 1349 | "\n", 1350 | "For 1D domain, we just needed one 1D array containning the X position and another 1D array containing the value.\n", 1351 | "\n", 1352 | "Now, we will create a grid, a distribution of points covering a surface. For the 2D domain, we will need:\n", 1353 | "- One 2D array containing the X coordinate of the points.\n", 1354 | "- One 2D array containing the Y coordinate of the points.\n", 1355 | "- One 2D array containing the function value at the points.\n", 1356 | "\n", 1357 | "The three matrices must have the exact same dimensions, because each cell of them represents a particular point." 1358 | ] 1359 | }, 1360 | { 1361 | "cell_type": "code", 1362 | "execution_count": null, 1363 | "metadata": { 1364 | "collapsed": true 1365 | }, 1366 | "outputs": [], 1367 | "source": [ 1368 | "#We can create the X and Y matrices by hand, or use a function designed to make ir easy:\n", 1369 | "\n", 1370 | "#we create two 1D arrays of the desired lengths:\n", 1371 | "x_1d = np.linspace(0, 5, 5)\n", 1372 | "y_1d = np.linspace(-2, 4, 7)" 1373 | ] 1374 | }, 1375 | { 1376 | "cell_type": "code", 1377 | "execution_count": null, 1378 | "metadata": { 1379 | "collapsed": false 1380 | }, 1381 | "outputs": [], 1382 | "source": [ 1383 | "x_1d" 1384 | ] 1385 | }, 1386 | { 1387 | "cell_type": "code", 1388 | "execution_count": null, 1389 | "metadata": { 1390 | "collapsed": true 1391 | }, 1392 | "outputs": [], 1393 | "source": [ 1394 | "y_1d" 1395 | ] 1396 | }, 1397 | { 1398 | "cell_type": "code", 1399 | "execution_count": null, 1400 | "metadata": { 1401 | "collapsed": true 1402 | }, 1403 | "outputs": [], 1404 | "source": [ 1405 | "#And we use the meshgrid function to create the X and Y matrices!\n", 1406 | "X, Y = np.????(x_1d, y_1d)" 1407 | ] 1408 | }, 1409 | { 1410 | "cell_type": "code", 1411 | "execution_count": null, 1412 | "metadata": { 1413 | "collapsed": false 1414 | }, 1415 | "outputs": [], 1416 | "source": [ 1417 | "X" 1418 | ] 1419 | }, 1420 | { 1421 | "cell_type": "code", 1422 | "execution_count": null, 1423 | "metadata": { 1424 | "collapsed": false 1425 | }, 1426 | "outputs": [], 1427 | "source": [ 1428 | "Y" 1429 | ] 1430 | }, 1431 | { 1432 | "cell_type": "markdown", 1433 | "metadata": {}, 1434 | "source": [ 1435 | "Note that with the meshgrid function we can only create rectangular grids" 1436 | ] 1437 | }, 1438 | { 1439 | "cell_type": "code", 1440 | "execution_count": null, 1441 | "metadata": { 1442 | "collapsed": true 1443 | }, 1444 | "outputs": [], 1445 | "source": [ 1446 | "#Using Numpy arrays, calculating the function value at the points is easy!\n", 1447 | "Z = " 1448 | ] 1449 | }, 1450 | { 1451 | "cell_type": "code", 1452 | "execution_count": null, 1453 | "metadata": { 1454 | "collapsed": false 1455 | }, 1456 | "outputs": [], 1457 | "source": [ 1458 | "#Let's plot it!\n", 1459 | "plt.????(X, Y, Z)\n", 1460 | "plt.colorbar()" 1461 | ] 1462 | }, 1463 | { 1464 | "cell_type": "markdown", 1465 | "metadata": {}, 1466 | "source": [ 1467 | "We can try a little more resolution..." 1468 | ] 1469 | }, 1470 | { 1471 | "cell_type": "code", 1472 | "execution_count": null, 1473 | "metadata": { 1474 | "collapsed": false 1475 | }, 1476 | "outputs": [], 1477 | "source": [ 1478 | "x_1d = np.linspace(0, 5, 100)\n", 1479 | "y_1d = np.linspace(-2, 4, 100)\n", 1480 | "X, Y = np.????(x_1d, y_1d)\n", 1481 | "Z = funcion(X,Y)\n", 1482 | "plt.contour(X, Y, Z)\n", 1483 | "plt.colorbar()" 1484 | ] 1485 | }, 1486 | { 1487 | "cell_type": "markdown", 1488 | "metadata": {}, 1489 | "source": [ 1490 | "The countourf function is simmilar, but it colours also between the lines. In both functions, we can manually adjust the number of lines/zones we want to differentiate on the plot." 1491 | ] 1492 | }, 1493 | { 1494 | "cell_type": "code", 1495 | "execution_count": null, 1496 | "metadata": { 1497 | "collapsed": false 1498 | }, 1499 | "outputs": [], 1500 | "source": [ 1501 | "plt.contourf(X, Y, Z, np.linspace(-2, 2, 6),cmap=plt.cm.Spectral) #With cmap, a color map is specified\n", 1502 | "plt.colorbar()" 1503 | ] 1504 | }, 1505 | { 1506 | "cell_type": "code", 1507 | "execution_count": null, 1508 | "metadata": { 1509 | "collapsed": false, 1510 | "scrolled": true 1511 | }, 1512 | "outputs": [], 1513 | "source": [ 1514 | "plt.contourf(X, Y, Z, np.linspace(-2, 2, 100),cmap=plt.cm.Spectral)\n", 1515 | "plt.colorbar()" 1516 | ] 1517 | }, 1518 | { 1519 | "cell_type": "code", 1520 | "execution_count": null, 1521 | "metadata": { 1522 | "collapsed": false 1523 | }, 1524 | "outputs": [], 1525 | "source": [ 1526 | "#We can even combine them!\n", 1527 | "plt.contourf(X, Y, Z, np.linspace(-2, 2, 100),cmap=plt.cm.Spectral)\n", 1528 | "plt.colorbar()\n", 1529 | "cs = plt.contour(X, Y, Z, np.linspace(-2, 2, 9), colors='k')\n", 1530 | "plt.clabel(cs)\n" 1531 | ] 1532 | }, 1533 | { 1534 | "cell_type": "markdown", 1535 | "metadata": {}, 1536 | "source": [ 1537 | "These functions can be enormously useful when you want to visualize something.\n", 1538 | "\n", 1539 | "And remember!\n", 1540 | "\n", 1541 | "## Always visualize data!\n", 1542 | "\n", 1543 | "Let's try it with **Real** data!" 1544 | ] 1545 | }, 1546 | { 1547 | "cell_type": "code", 1548 | "execution_count": null, 1549 | "metadata": { 1550 | "collapsed": true 1551 | }, 1552 | "outputs": [], 1553 | "source": [ 1554 | "time_vector = np.loadtxt('data/ligo_tiempos.txt')\n", 1555 | "frequency_vector = np.loadtxt('data/ligo_frecuencias.txt')\n", 1556 | "intensity_matrix = np.loadtxt('data/ligo_datos.txt')" 1557 | ] 1558 | }, 1559 | { 1560 | "cell_type": "markdown", 1561 | "metadata": {}, 1562 | "source": [ 1563 | "The time and frequency vectors contain the values at which the instrument was reading, and the intensity matrix, the postprocessed strength measured for each frequency at each time.\n", 1564 | "\n", 1565 | "We need again to create the 2D arrays of coordinates." 1566 | ] 1567 | }, 1568 | { 1569 | "cell_type": "code", 1570 | "execution_count": null, 1571 | "metadata": { 1572 | "collapsed": true 1573 | }, 1574 | "outputs": [], 1575 | "source": [ 1576 | "time_2D, freq_2D = np.????(time_vector, frequency_vector)" 1577 | ] 1578 | }, 1579 | { 1580 | "cell_type": "code", 1581 | "execution_count": null, 1582 | "metadata": { 1583 | "collapsed": false 1584 | }, 1585 | "outputs": [], 1586 | "source": [ 1587 | "plt.figure(figsize=(10,6)) #We can manually adjust the sice of the picture\n", 1588 | "plt.???Pintarrelleno???(time_2D, freq_2D, intensity_matrix, np.linspace(0, 0.02313, 200), cmap='bone')\n", 1589 | "plt.xlabel('time (s)')\n", 1590 | "plt.ylabel('Frequency (Hz)')\n", 1591 | "plt.colorbar()" 1592 | ] 1593 | }, 1594 | { 1595 | "cell_type": "markdown", 1596 | "metadata": {}, 1597 | "source": [ 1598 | "Wow! What is that? Let's zoom into it!" 1599 | ] 1600 | }, 1601 | { 1602 | "cell_type": "code", 1603 | "execution_count": null, 1604 | "metadata": { 1605 | "collapsed": false 1606 | }, 1607 | "outputs": [], 1608 | "source": [ 1609 | "\n", 1610 | "plt.figure(figsize=(10,6))\n", 1611 | "plt.???Pintarrelleno???(time_2D, freq_2D,intensity_matrix,np.linspace(0, 0.02313, 200),cmap = plt.cm.Spectral)\n", 1612 | "plt.colorbar()\n", 1613 | "plt.???Pintarrelleno???(time_2D, freq_2D,intensity_matrix,np.linspace(0, 0.02313, 9), colors='k')\n", 1614 | "plt.xlabel('time (s)')\n", 1615 | "plt.ylabel('Frequency (Hz)')\n", 1616 | "\n", 1617 | "plt.axis([9.9, 10.05, 0, 300]) #HERE HAPPENS THE ZOOM MAGIC" 1618 | ] 1619 | }, 1620 | { 1621 | "cell_type": "markdown", 1622 | "metadata": {}, 1623 | "source": [ 1624 | "# IPython Widgets" 1625 | ] 1626 | }, 1627 | { 1628 | "cell_type": "markdown", 1629 | "metadata": {}, 1630 | "source": [ 1631 | "The IPython Widgets are interactive tools to use in the notebook. They are fun and very useful to quickly understand how different parameters affect a certain function.\n", 1632 | "\n", 1633 | "This is based on a section of the PyConEs 14 talk by Kiko Correoso \"Hacking the notebook\": http://nbviewer.jupyter.org/github/kikocorreoso/PyConES14_talk-Hacking_the_Notebook/blob/master/notebooks/Using%20Interact.ipynb" 1634 | ] 1635 | }, 1636 | { 1637 | "cell_type": "code", 1638 | "execution_count": null, 1639 | "metadata": { 1640 | "collapsed": true 1641 | }, 1642 | "outputs": [], 1643 | "source": [ 1644 | "from ipywidgets import interact" 1645 | ] 1646 | }, 1647 | { 1648 | "cell_type": "code", 1649 | "execution_count": null, 1650 | "metadata": { 1651 | "collapsed": true 1652 | }, 1653 | "outputs": [], 1654 | "source": [ 1655 | "#Lets define a extremely simple function:\n", 1656 | "def ejemplo(x):\n", 1657 | " print(x)" 1658 | ] 1659 | }, 1660 | { 1661 | "cell_type": "code", 1662 | "execution_count": null, 1663 | "metadata": { 1664 | "collapsed": false 1665 | }, 1666 | "outputs": [], 1667 | "source": [ 1668 | "???(???, x =10) #Try changing the value of x to True, 'Hello' or ['hello', 'world']" 1669 | ] 1670 | }, 1671 | { 1672 | "cell_type": "code", 1673 | "execution_count": null, 1674 | "metadata": { 1675 | "collapsed": false 1676 | }, 1677 | "outputs": [], 1678 | "source": [ 1679 | "#We can control the slider values with more precission:\n", 1680 | "???(???, x = (9, 10, 0.1))" 1681 | ] 1682 | }, 1683 | { 1684 | "cell_type": "markdown", 1685 | "metadata": {}, 1686 | "source": [ 1687 | "If you want a dropdown menu that passes non-string values to the Python function, you can pass a dictionary. The keys in the dictionary are used for the names in the dropdown menu UI and the values are the arguments that are passed to the underlying Python function." 1688 | ] 1689 | }, 1690 | { 1691 | "cell_type": "code", 1692 | "execution_count": null, 1693 | "metadata": { 1694 | "collapsed": false 1695 | }, 1696 | "outputs": [], 1697 | "source": [ 1698 | "???(???, x={'one': 10, 'two': 20})" 1699 | ] 1700 | }, 1701 | { 1702 | "cell_type": "markdown", 1703 | "metadata": {}, 1704 | "source": [ 1705 | "Let's have some fun! We talked before about frequencys and waves. Have you ever learn about AM and FM modulation? It's the process used to send radio communications!" 1706 | ] 1707 | }, 1708 | { 1709 | "cell_type": "code", 1710 | "execution_count": null, 1711 | "metadata": { 1712 | "collapsed": false 1713 | }, 1714 | "outputs": [], 1715 | "source": [ 1716 | "x = np.linspace(-1, 7, 1000)\n", 1717 | "\n", 1718 | "fig = plt.figure()\n", 1719 | "\n", 1720 | "plt.subplot(211)#This allows us to display multiple sub-plots, and where to put them\n", 1721 | "plt.plot(x, np.sin(x))\n", 1722 | "plt.grid(False)\n", 1723 | "plt.title(\"Audio signal: modulator\")\n", 1724 | "\n", 1725 | "plt.subplot(212)\n", 1726 | "plt.plot(x, np.sin(50 * x))\n", 1727 | "plt.grid(False)\n", 1728 | "plt.title(\"Radio signal: carrier\")" 1729 | ] 1730 | }, 1731 | { 1732 | "cell_type": "code", 1733 | "execution_count": null, 1734 | "metadata": { 1735 | "collapsed": false 1736 | }, 1737 | "outputs": [], 1738 | "source": [ 1739 | "#Am modulation simply works like this:\n", 1740 | "am_wave = np.sin(50 * x) * (0.5 + 0.5 * np.sin(x))\n", 1741 | "plt.plot(x, am_wave)" 1742 | ] 1743 | }, 1744 | { 1745 | "cell_type": "markdown", 1746 | "metadata": {}, 1747 | "source": [ 1748 | "In order to interact with it, we will need to transform it into a function" 1749 | ] 1750 | }, 1751 | { 1752 | "cell_type": "code", 1753 | "execution_count": null, 1754 | "metadata": { 1755 | "collapsed": false 1756 | }, 1757 | "outputs": [], 1758 | "source": [ 1759 | "def am_mod (f_carr=50, f_mod=1, depth=0.5): #The default values will be the starting points of the sliders\n", 1760 | " x = np.???(-1, 7, 1000)\n", 1761 | " am_wave = np.sin(f_carr * x) * (1- depth/2 + depth/2 * np.sin(f_mod * x))\n", 1762 | " \n", 1763 | " plt.???(x, am_wave)\n", 1764 | " " 1765 | ] 1766 | }, 1767 | { 1768 | "cell_type": "code", 1769 | "execution_count": null, 1770 | "metadata": { 1771 | "collapsed": false 1772 | }, 1773 | "outputs": [], 1774 | "source": [ 1775 | "???(???,\n", 1776 | " f_carr = (1,100,2),\n", 1777 | " f_mod = (0.2, 2, 0.1),\n", 1778 | " depth = (0, 1, 0.1))" 1779 | ] 1780 | }, 1781 | { 1782 | "cell_type": "markdown", 1783 | "metadata": {}, 1784 | "source": [ 1785 | "#### Other options... \n", 1786 | "\n" 1787 | ] 1788 | }, 1789 | { 1790 | "cell_type": "markdown", 1791 | "metadata": {}, 1792 | "source": [ 1793 | "### 5. Other packages" 1794 | ] 1795 | }, 1796 | { 1797 | "cell_type": "markdown", 1798 | "metadata": {}, 1799 | "source": [ 1800 | "#### Symbolic calculations with SymPy " 1801 | ] 1802 | }, 1803 | { 1804 | "cell_type": "markdown", 1805 | "metadata": {}, 1806 | "source": [ 1807 | "![sympy](./static/sympy.png)" 1808 | ] 1809 | }, 1810 | { 1811 | "cell_type": "markdown", 1812 | "metadata": {}, 1813 | "source": [ 1814 | "SymPy is a Python package for symbolic math. We will not cover it in depth, but let's take a picure of the basics!" 1815 | ] 1816 | }, 1817 | { 1818 | "cell_type": "code", 1819 | "execution_count": null, 1820 | "metadata": { 1821 | "collapsed": false 1822 | }, 1823 | "outputs": [], 1824 | "source": [ 1825 | "# Importación\n", 1826 | "from sympy import init_session\n", 1827 | "\n", 1828 | "init_session(use_latex='matplotlib') #We must start calling this function" 1829 | ] 1830 | }, 1831 | { 1832 | "cell_type": "markdown", 1833 | "metadata": {}, 1834 | "source": [ 1835 | "The basic unit of this package is the symbol. A simbol object has name and graphic representation, which can be different:" 1836 | ] 1837 | }, 1838 | { 1839 | "cell_type": "code", 1840 | "execution_count": null, 1841 | "metadata": { 1842 | "collapsed": false 1843 | }, 1844 | "outputs": [], 1845 | "source": [ 1846 | "coef_traccion = symbols('c_T')\n", 1847 | "coef_traccion" 1848 | ] 1849 | }, 1850 | { 1851 | "cell_type": "code", 1852 | "execution_count": null, 1853 | "metadata": { 1854 | "collapsed": false 1855 | }, 1856 | "outputs": [], 1857 | "source": [ 1858 | "w = symbols('omega')\n", 1859 | "W = symbols('Omega')\n", 1860 | "w, W" 1861 | ] 1862 | }, 1863 | { 1864 | "cell_type": "markdown", 1865 | "metadata": {}, 1866 | "source": [ 1867 | "By default, SymPy takes symbols as complex numbers. That can lead to unexpected results in front of certain operations, like logarithms. We can explicitly signal that a symbol is real when we create it. We can also create several symbols at a time." 1868 | ] 1869 | }, 1870 | { 1871 | "cell_type": "code", 1872 | "execution_count": null, 1873 | "metadata": { 1874 | "collapsed": false 1875 | }, 1876 | "outputs": [], 1877 | "source": [ 1878 | "x, y, z, t = symbols('x y z t', real=True)\n", 1879 | "x.assumptions0" 1880 | ] 1881 | }, 1882 | { 1883 | "cell_type": "markdown", 1884 | "metadata": {}, 1885 | "source": [ 1886 | "Expressions can be created from symbols:" 1887 | ] 1888 | }, 1889 | { 1890 | "cell_type": "code", 1891 | "execution_count": null, 1892 | "metadata": { 1893 | "collapsed": false 1894 | }, 1895 | "outputs": [], 1896 | "source": [ 1897 | "expr = cos(x)**2 + sin(x)**2\n", 1898 | "expr" 1899 | ] 1900 | }, 1901 | { 1902 | "cell_type": "code", 1903 | "execution_count": null, 1904 | "metadata": { 1905 | "collapsed": false 1906 | }, 1907 | "outputs": [], 1908 | "source": [ 1909 | "simplify(expr)" 1910 | ] 1911 | }, 1912 | { 1913 | "cell_type": "code", 1914 | "execution_count": null, 1915 | "metadata": { 1916 | "collapsed": false 1917 | }, 1918 | "outputs": [], 1919 | "source": [ 1920 | "#We can substitute pieces of the expression:\n", 1921 | "expr.subs(x, y**2)" 1922 | ] 1923 | }, 1924 | { 1925 | "cell_type": "code", 1926 | "execution_count": null, 1927 | "metadata": { 1928 | "collapsed": false 1929 | }, 1930 | "outputs": [], 1931 | "source": [ 1932 | "#We can particularize on a certain value:\n", 1933 | "(sin(x) + 3 * x).subs(x, pi)" 1934 | ] 1935 | }, 1936 | { 1937 | "cell_type": "code", 1938 | "execution_count": null, 1939 | "metadata": { 1940 | "collapsed": false 1941 | }, 1942 | "outputs": [], 1943 | "source": [ 1944 | "#We can evaluate the numerical value with a certain precission:\n", 1945 | "(sin(x) + 3 * x).subs(x, pi).???(25)" 1946 | ] 1947 | }, 1948 | { 1949 | "cell_type": "markdown", 1950 | "metadata": {}, 1951 | "source": [ 1952 | "We can manipulate the expression in several ways. For example:" 1953 | ] 1954 | }, 1955 | { 1956 | "cell_type": "code", 1957 | "execution_count": null, 1958 | "metadata": { 1959 | "collapsed": false 1960 | }, 1961 | "outputs": [], 1962 | "source": [ 1963 | "expr1 = (x ** 3 + 3 * y + 2) ** 2\n", 1964 | "expr1" 1965 | ] 1966 | }, 1967 | { 1968 | "cell_type": "code", 1969 | "execution_count": null, 1970 | "metadata": { 1971 | "collapsed": false 1972 | }, 1973 | "outputs": [], 1974 | "source": [ 1975 | "expr1.expand()" 1976 | ] 1977 | }, 1978 | { 1979 | "cell_type": "markdown", 1980 | "metadata": {}, 1981 | "source": [ 1982 | "We can derivate and integrate:" 1983 | ] 1984 | }, 1985 | { 1986 | "cell_type": "code", 1987 | "execution_count": null, 1988 | "metadata": { 1989 | "collapsed": false 1990 | }, 1991 | "outputs": [], 1992 | "source": [ 1993 | "expr = cos(2*x)\n", 1994 | "expr.diff(x, x, x)" 1995 | ] 1996 | }, 1997 | { 1998 | "cell_type": "code", 1999 | "execution_count": null, 2000 | "metadata": { 2001 | "collapsed": false 2002 | }, 2003 | "outputs": [], 2004 | "source": [ 2005 | "expr_xy = y ** 3 * sin(x) ** 2 + x ** 2 * cos(y)\n", 2006 | "expr_xy" 2007 | ] 2008 | }, 2009 | { 2010 | "cell_type": "code", 2011 | "execution_count": null, 2012 | "metadata": { 2013 | "collapsed": false 2014 | }, 2015 | "outputs": [], 2016 | "source": [ 2017 | "diff(expr_xy, x, 2, y, 2)" 2018 | ] 2019 | }, 2020 | { 2021 | "cell_type": "code", 2022 | "execution_count": null, 2023 | "metadata": { 2024 | "collapsed": false 2025 | }, 2026 | "outputs": [], 2027 | "source": [ 2028 | "int2 = 1 / sin(x)\n", 2029 | "integrate(int2)" 2030 | ] 2031 | }, 2032 | { 2033 | "cell_type": "code", 2034 | "execution_count": null, 2035 | "metadata": { 2036 | "collapsed": false 2037 | }, 2038 | "outputs": [], 2039 | "source": [ 2040 | "x, a = symbols('x a', real=True)\n", 2041 | "\n", 2042 | "int3 = 1 / (x**2 + a**2)**2\n", 2043 | "integrate(int3, x)" 2044 | ] 2045 | }, 2046 | { 2047 | "cell_type": "markdown", 2048 | "metadata": {}, 2049 | "source": [ 2050 | "We also have ecuations and differential ecuations:" 2051 | ] 2052 | }, 2053 | { 2054 | "cell_type": "code", 2055 | "execution_count": null, 2056 | "metadata": { 2057 | "collapsed": false 2058 | }, 2059 | "outputs": [], 2060 | "source": [ 2061 | "a, x, t, C = symbols('a, x, t, C', real=True)\n", 2062 | "ecuacion = Eq(a * exp(x/t), C)\n", 2063 | "ecuacion" 2064 | ] 2065 | }, 2066 | { 2067 | "cell_type": "code", 2068 | "execution_count": null, 2069 | "metadata": { 2070 | "collapsed": false 2071 | }, 2072 | "outputs": [], 2073 | "source": [ 2074 | "???(ecuacion ,x)" 2075 | ] 2076 | }, 2077 | { 2078 | "cell_type": "code", 2079 | "execution_count": null, 2080 | "metadata": { 2081 | "collapsed": false 2082 | }, 2083 | "outputs": [], 2084 | "source": [ 2085 | "x = symbols('x')\n", 2086 | "f = Function('y')\n", 2087 | "ecuacion_dif = Eq(f(x).diff(x,2) + f(x).diff(x) + f(x), cos(x))\n", 2088 | "ecuacion_dif" 2089 | ] 2090 | }, 2091 | { 2092 | "cell_type": "code", 2093 | "execution_count": null, 2094 | "metadata": { 2095 | "collapsed": false 2096 | }, 2097 | "outputs": [], 2098 | "source": [ 2099 | "???(ecuacion_dif, f(x))" 2100 | ] 2101 | }, 2102 | { 2103 | "cell_type": "markdown", 2104 | "metadata": {}, 2105 | "source": [ 2106 | "#### Data Analysis with pandas " 2107 | ] 2108 | }, 2109 | { 2110 | "cell_type": "markdown", 2111 | "metadata": {}, 2112 | "source": [ 2113 | "![pandas](./static/pandas_logo.png)" 2114 | ] 2115 | }, 2116 | { 2117 | "cell_type": "markdown", 2118 | "metadata": {}, 2119 | "source": [ 2120 | "Pandas is a package that focus on data structures and data analysis tools" 2121 | ] 2122 | }, 2123 | { 2124 | "cell_type": "markdown", 2125 | "metadata": {}, 2126 | "source": [ 2127 | "#### Machine Learning with scikit-learn " 2128 | ] 2129 | }, 2130 | { 2131 | "cell_type": "markdown", 2132 | "metadata": {}, 2133 | "source": [ 2134 | "![scikit-learn](./static/scikit-learn-logo.png)" 2135 | ] 2136 | }, 2137 | { 2138 | "cell_type": "markdown", 2139 | "metadata": {}, 2140 | "source": [ 2141 | "Scikit-learn is a very complete Python package focusing on machin learning, and data mining and analysis. We will not cover it in depth because it will be the focus of many more talks at the PyData." 2142 | ] 2143 | }, 2144 | { 2145 | "cell_type": "markdown", 2146 | "metadata": {}, 2147 | "source": [ 2148 | "##### A world of possibilities... " 2149 | ] 2150 | }, 2151 | { 2152 | "cell_type": "markdown", 2153 | "metadata": {}, 2154 | "source": [ 2155 | "![scikit-learn](./static/cheatsheet-scikit-learn.png)" 2156 | ] 2157 | }, 2158 | { 2159 | "cell_type": "markdown", 2160 | "metadata": {}, 2161 | "source": [ 2162 | "# ¡Gracias por vuetra atención! " 2163 | ] 2164 | }, 2165 | { 2166 | "cell_type": "markdown", 2167 | "metadata": {}, 2168 | "source": [ 2169 | "![PyData_logo](./static/pycones2016.jpg)" 2170 | ] 2171 | }, 2172 | { 2173 | "cell_type": "markdown", 2174 | "metadata": {}, 2175 | "source": [ 2176 | "## ¿Alguna pregunta?" 2177 | ] 2178 | }, 2179 | { 2180 | "cell_type": "markdown", 2181 | "metadata": {}, 2182 | "source": [ 2183 | "\n", 2184 | "---\n" 2185 | ] 2186 | }, 2187 | { 2188 | "cell_type": "code", 2189 | "execution_count": null, 2190 | "metadata": { 2191 | "collapsed": false 2192 | }, 2193 | "outputs": [], 2194 | "source": [ 2195 | "# Notebook style\n", 2196 | "from IPython.core.display import HTML\n", 2197 | "css_file = './static/style.css'\n", 2198 | "HTML(open(css_file, \"r\").read())" 2199 | ] 2200 | } 2201 | ], 2202 | "metadata": { 2203 | "kernelspec": { 2204 | "display_name": "Python 3", 2205 | "language": "python", 2206 | "name": "python3" 2207 | }, 2208 | "language_info": { 2209 | "codemirror_mode": { 2210 | "name": "ipython", 2211 | "version": 3 2212 | }, 2213 | "file_extension": ".py", 2214 | "mimetype": "text/x-python", 2215 | "name": "python", 2216 | "nbconvert_exporter": "python", 2217 | "pygments_lexer": "ipython3", 2218 | "version": "3.5.2" 2219 | } 2220 | }, 2221 | "nbformat": 4, 2222 | "nbformat_minor": 0 2223 | } 2224 | -------------------------------------------------------------------------------- /Basic Python Packages for Science.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "![Python Madrid Logo](./static/pycones2016.jpg)" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Basic Python Packages for Science\n", 15 | "## The Aeropython’s guide to the Python Galaxy! " 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | "![Aropython_logo](./static/aeropython_name_mini.png)\n", 23 | "###### Siro Moreno Martín\n", 24 | "###### Alejandro Sáez Mollejo" 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": {}, 30 | "source": [ 31 | "## Who are we? " 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "##### Siro Moreno " 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "\n", 46 | "* **Aerospace Engineer**\n", 47 | "* Working at: Instituto Nacional de Técnica Aeroespacial\n", 48 | "* Full-time Nerd\n", 49 | "* Have another workshop later because stupidity (please COME!!)" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": {}, 55 | "source": [ 56 | "##### Álex Sáez " 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": {}, 62 | "source": [ 63 | "\n", 64 | "\n", 65 | "* **Aerospace Engineer**\n", 66 | "* **Flight Test Methods Engineer** at AIRBUS Defence & Space on behalf of ALTRAN\n", 67 | "* Co-creator of AeroPython --> PyFME \n", 68 | "* Member of Python España (come and join us!)" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": {}, 74 | "source": [ 75 | "---" 76 | ] 77 | }, 78 | { 79 | "cell_type": "markdown", 80 | "metadata": {}, 81 | "source": [ 82 | "### 0. Introduction" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": {}, 88 | "source": [ 89 | "#### Python in the Scientific environment " 90 | ] 91 | }, 92 | { 93 | "cell_type": "markdown", 94 | "metadata": {}, 95 | "source": [ 96 | "##### Principal Python Packages for scientific purposes " 97 | ] 98 | }, 99 | { 100 | "cell_type": "markdown", 101 | "metadata": {}, 102 | "source": [ 103 | "##### Anaconda & conda" 104 | ] 105 | }, 106 | { 107 | "cell_type": "markdown", 108 | "metadata": {}, 109 | "source": [ 110 | "![conda](./static/conda.png)" 111 | ] 112 | }, 113 | { 114 | "cell_type": "markdown", 115 | "metadata": {}, 116 | "source": [ 117 | "http://conda.pydata.org/docs/intro.html\n", 118 | "\n", 119 | "Conda is a package manager application that quickly installs, runs, and updates packages and their dependencies. The conda command is the primary interface for managing installations of various packages. It can query and search the package index and current installation, create new environments, and install and update packages into existing conda environments. " 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": null, 125 | "metadata": { 126 | "collapsed": false 127 | }, 128 | "outputs": [], 129 | "source": [ 130 | "from IPython.display import HTML\n", 131 | "HTML('')" 132 | ] 133 | }, 134 | { 135 | "cell_type": "markdown", 136 | "metadata": {}, 137 | "source": [ 138 | "##### Main objectives of this workshop \n", 139 | "\n", 140 | "* Provide you with a **first insight into the principal Python tools & libraries used in Science**:\n", 141 | " - conda.\n", 142 | " - Jupyter Notebook and preview of JupyterLab\n", 143 | " - NumPy, matplotlib, SciPy\n", 144 | " - SymPy\n", 145 | " " 146 | ] 147 | }, 148 | { 149 | "cell_type": "markdown", 150 | "metadata": {}, 151 | "source": [ 152 | "##### Other common libraries that will not be covered here are:\n", 153 | "\n", 154 | "- Pandas\n", 155 | "- scikit-learn\n", 156 | "- Numba & Cython" 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "metadata": {}, 162 | "source": [ 163 | "### 1. Jupyter Notebook" 164 | ] 165 | }, 166 | { 167 | "cell_type": "markdown", 168 | "metadata": {}, 169 | "source": [ 170 | "![jupyter](./static/jupyter-logo.png)" 171 | ] 172 | }, 173 | { 174 | "cell_type": "markdown", 175 | "metadata": {}, 176 | "source": [ 177 | "The Jupyter Notebook is a web application that allows you to create and share documents that contain live code, equations, visualizations and explanatory text. Uses include: data cleaning and transformation, numerical simulation, statistical modeling, machine learning and much more.\n", 178 | "\n", 179 | "It has been widely recognised as a great way to distribute scientific papers, because of the capability to have an integrated format with text and executable code, highly reproducible. Top level investigators around the world are already using it, like the team behind the Gravitational Waves discovery (LIGO), whose analysis was translated to an interactive dowloadable Jupyter notebook. You can see it here: https://github.com/minrk/ligo-binder/blob/master/GW150914_tutorial.ipynb" 180 | ] 181 | }, 182 | { 183 | "cell_type": "markdown", 184 | "metadata": {}, 185 | "source": [ 186 | "### 2. Using arrays: NumPy " 187 | ] 188 | }, 189 | { 190 | "cell_type": "markdown", 191 | "metadata": {}, 192 | "source": [ 193 | "![numpy-logo](./static/numpy.png)" 194 | ] 195 | }, 196 | { 197 | "cell_type": "markdown", 198 | "metadata": {}, 199 | "source": [ 200 | "#### ndarray object " 201 | ] 202 | }, 203 | { 204 | "cell_type": "markdown", 205 | "metadata": {}, 206 | "source": [ 207 | "| index | 0 | 1 | 2 | 3 | ... | n-1 | n |\n", 208 | "| ---------- | :---: | :---: | :---: | :---: | :---: | :---: | :---: |\n", 209 | "| value | 2.1 | 3.6 | 7.8 | 1.5 | ... | 5.4 | 6.3 |" 210 | ] 211 | }, 212 | { 213 | "cell_type": "markdown", 214 | "metadata": {}, 215 | "source": [ 216 | "* N-dimensional data structure.\n", 217 | "* Homogeneously typed.\n", 218 | "* Efficient!\n", 219 | "\n", 220 | "A universal function (or ufunc for short) is a function that operates on ndarrays. It is a “**vectorized** function\"." 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": null, 226 | "metadata": { 227 | "collapsed": true 228 | }, 229 | "outputs": [], 230 | "source": [ 231 | "import numpy as np" 232 | ] 233 | }, 234 | { 235 | "cell_type": "markdown", 236 | "metadata": {}, 237 | "source": [ 238 | "Let's have a look at the efficiency of a ndarray against the efficiency of a list:" 239 | ] 240 | }, 241 | { 242 | "cell_type": "code", 243 | "execution_count": null, 244 | "metadata": { 245 | "collapsed": true 246 | }, 247 | "outputs": [], 248 | "source": [ 249 | "# List \n", 250 | "my_list = list(range(0,100000))\n", 251 | "# Array \n", 252 | "my_array = np.arange(0, 100000)" 253 | ] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": null, 258 | "metadata": { 259 | "collapsed": false 260 | }, 261 | "outputs": [], 262 | "source": [ 263 | "%timeit sum(my_list)" 264 | ] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": null, 269 | "metadata": { 270 | "collapsed": false 271 | }, 272 | "outputs": [], 273 | "source": [ 274 | "%timeit np.sum(my_array)" 275 | ] 276 | }, 277 | { 278 | "cell_type": "markdown", 279 | "metadata": {}, 280 | "source": [ 281 | "Another example:" 282 | ] 283 | }, 284 | { 285 | "cell_type": "code", 286 | "execution_count": null, 287 | "metadata": { 288 | "collapsed": false 289 | }, 290 | "outputs": [], 291 | "source": [ 292 | "%timeit [ii**2 for ii in range(100000)]" 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "execution_count": null, 298 | "metadata": { 299 | "collapsed": false 300 | }, 301 | "outputs": [], 302 | "source": [ 303 | "%timeit np.arange(100000)**2" 304 | ] 305 | }, 306 | { 307 | "cell_type": "markdown", 308 | "metadata": {}, 309 | "source": [ 310 | "#### Array creation" 311 | ] 312 | }, 313 | { 314 | "cell_type": "code", 315 | "execution_count": null, 316 | "metadata": { 317 | "collapsed": false 318 | }, 319 | "outputs": [], 320 | "source": [ 321 | "# [1, 2, 3, 4]\n", 322 | "one_dim_array = np.array([1, 2, 3, 4])\n", 323 | "one_dim_array" 324 | ] 325 | }, 326 | { 327 | "cell_type": "code", 328 | "execution_count": null, 329 | "metadata": { 330 | "collapsed": false 331 | }, 332 | "outputs": [], 333 | "source": [ 334 | "# [1, 2, 3],\n", 335 | "# [4, 5, 6],\n", 336 | "# [7, 8, 9]\n", 337 | "\n", 338 | "two_dim_array = np.array([[1, 2, 3],\n", 339 | " [4, 5, 6],\n", 340 | " [7, 8, 9]])\n", 341 | "two_dim_array" 342 | ] 343 | }, 344 | { 345 | "cell_type": "code", 346 | "execution_count": null, 347 | "metadata": { 348 | "collapsed": false 349 | }, 350 | "outputs": [], 351 | "source": [ 352 | "# size\n", 353 | "two_dim_array.size" 354 | ] 355 | }, 356 | { 357 | "cell_type": "code", 358 | "execution_count": null, 359 | "metadata": { 360 | "collapsed": false 361 | }, 362 | "outputs": [], 363 | "source": [ 364 | "# shape\n", 365 | "two_dim_array.shape" 366 | ] 367 | }, 368 | { 369 | "cell_type": "code", 370 | "execution_count": null, 371 | "metadata": { 372 | "collapsed": false 373 | }, 374 | "outputs": [], 375 | "source": [ 376 | "# dtype\n", 377 | "two_dim_array.dtype" 378 | ] 379 | }, 380 | { 381 | "cell_type": "code", 382 | "execution_count": null, 383 | "metadata": { 384 | "collapsed": false 385 | }, 386 | "outputs": [], 387 | "source": [ 388 | "two_dim_array.astype(float)" 389 | ] 390 | }, 391 | { 392 | "cell_type": "code", 393 | "execution_count": null, 394 | "metadata": { 395 | "collapsed": false 396 | }, 397 | "outputs": [], 398 | "source": [ 399 | "# Other interesting functions for array creation\n", 400 | "# zeros\n", 401 | "zeros_arr = np.zeros([3, 3])\n", 402 | "# ones\n", 403 | "ones_arr = np.ones([10])\n", 404 | "# identity\n", 405 | "eye_arr = np.eye(5)" 406 | ] 407 | }, 408 | { 409 | "cell_type": "code", 410 | "execution_count": null, 411 | "metadata": { 412 | "collapsed": false 413 | }, 414 | "outputs": [], 415 | "source": [ 416 | "# range\n", 417 | "range_arr = np.arange(15)\n", 418 | "range_arr" 419 | ] 420 | }, 421 | { 422 | "cell_type": "code", 423 | "execution_count": null, 424 | "metadata": { 425 | "collapsed": false 426 | }, 427 | "outputs": [], 428 | "source": [ 429 | "# reshape\n", 430 | "range_arr.reshape([3, 5])" 431 | ] 432 | }, 433 | { 434 | "cell_type": "code", 435 | "execution_count": null, 436 | "metadata": { 437 | "collapsed": false 438 | }, 439 | "outputs": [], 440 | "source": [ 441 | "# linspace\n", 442 | "np.linspace(0, 10, 21)" 443 | ] 444 | }, 445 | { 446 | "cell_type": "markdown", 447 | "metadata": {}, 448 | "source": [ 449 | "#### Basic slicing " 450 | ] 451 | }, 452 | { 453 | "cell_type": "code", 454 | "execution_count": null, 455 | "metadata": { 456 | "collapsed": false 457 | }, 458 | "outputs": [], 459 | "source": [ 460 | "one_dim_array[0]" 461 | ] 462 | }, 463 | { 464 | "cell_type": "code", 465 | "execution_count": null, 466 | "metadata": { 467 | "collapsed": false 468 | }, 469 | "outputs": [], 470 | "source": [ 471 | "two_dim_array[-1, -1]" 472 | ] 473 | }, 474 | { 475 | "cell_type": "markdown", 476 | "metadata": {}, 477 | "source": [ 478 | "`[start:stop:step]`" 479 | ] 480 | }, 481 | { 482 | "cell_type": "code", 483 | "execution_count": null, 484 | "metadata": { 485 | "collapsed": false 486 | }, 487 | "outputs": [], 488 | "source": [ 489 | "my_arr = np.arange(100)\n", 490 | "my_arr[0::2]" 491 | ] 492 | }, 493 | { 494 | "cell_type": "code", 495 | "execution_count": null, 496 | "metadata": { 497 | "collapsed": false 498 | }, 499 | "outputs": [], 500 | "source": [ 501 | "chess_board = np.zeros([8, 8], dtype=int)\n", 502 | "\n", 503 | "chess_board[0::2, 1::2] = 1\n", 504 | "chess_board[1::2, 0::2] = 1\n", 505 | "\n", 506 | "chess_board" 507 | ] 508 | }, 509 | { 510 | "cell_type": "markdown", 511 | "metadata": {}, 512 | "source": [ 513 | "### 2. Drawing: Matplotlib" 514 | ] 515 | }, 516 | { 517 | "cell_type": "markdown", 518 | "metadata": {}, 519 | "source": [ 520 | "![Matplotlib-logo](./static/matplotlib.png)" 521 | ] 522 | }, 523 | { 524 | "cell_type": "code", 525 | "execution_count": null, 526 | "metadata": { 527 | "collapsed": true 528 | }, 529 | "outputs": [], 530 | "source": [ 531 | "%matplotlib inline\n", 532 | "import matplotlib.pyplot as plt" 533 | ] 534 | }, 535 | { 536 | "cell_type": "code", 537 | "execution_count": null, 538 | "metadata": { 539 | "collapsed": false 540 | }, 541 | "outputs": [], 542 | "source": [ 543 | "plt.matshow(chess_board, cmap=plt.cm.gray)" 544 | ] 545 | }, 546 | { 547 | "cell_type": "markdown", 548 | "metadata": {}, 549 | "source": [ 550 | "#### Operations & linalg " 551 | ] 552 | }, 553 | { 554 | "cell_type": "code", 555 | "execution_count": null, 556 | "metadata": { 557 | "collapsed": false 558 | }, 559 | "outputs": [], 560 | "source": [ 561 | "# numpy functions\n", 562 | "x = np.linspace(1, 10)\n", 563 | "y = np.sin(x)" 564 | ] 565 | }, 566 | { 567 | "cell_type": "code", 568 | "execution_count": null, 569 | "metadata": { 570 | "collapsed": false 571 | }, 572 | "outputs": [], 573 | "source": [ 574 | "plt.plot(x, y)" 575 | ] 576 | }, 577 | { 578 | "cell_type": "code", 579 | "execution_count": null, 580 | "metadata": { 581 | "collapsed": false 582 | }, 583 | "outputs": [], 584 | "source": [ 585 | "y_2 = (1 + np.log(x)) ** 2" 586 | ] 587 | }, 588 | { 589 | "cell_type": "code", 590 | "execution_count": null, 591 | "metadata": { 592 | "collapsed": false 593 | }, 594 | "outputs": [], 595 | "source": [ 596 | "plt.plot(x, y_2, 'r-*')" 597 | ] 598 | }, 599 | { 600 | "cell_type": "code", 601 | "execution_count": null, 602 | "metadata": { 603 | "collapsed": false 604 | }, 605 | "outputs": [], 606 | "source": [ 607 | "# Creating a 2d array\n", 608 | "two_dim_array = np.array([[10, 25, 33],\n", 609 | " [40, 25, 16],\n", 610 | " [77, 68, 91]])\n", 611 | "\n", 612 | "two_dim_array.T" 613 | ] 614 | }, 615 | { 616 | "cell_type": "code", 617 | "execution_count": null, 618 | "metadata": { 619 | "collapsed": false 620 | }, 621 | "outputs": [], 622 | "source": [ 623 | "# matrix multiplication\n", 624 | "two_dim_array @ two_dim_array" 625 | ] 626 | }, 627 | { 628 | "cell_type": "code", 629 | "execution_count": null, 630 | "metadata": { 631 | "collapsed": false 632 | }, 633 | "outputs": [], 634 | "source": [ 635 | "# matrix vector\n", 636 | "one_dim_array = np.array([2.5, 3.6, 3.8])\n", 637 | "\n", 638 | "two_dim_array @ one_dim_array" 639 | ] 640 | }, 641 | { 642 | "cell_type": "code", 643 | "execution_count": null, 644 | "metadata": { 645 | "collapsed": false 646 | }, 647 | "outputs": [], 648 | "source": [ 649 | "# inv\n", 650 | "np.linalg.inv(two_dim_array)" 651 | ] 652 | }, 653 | { 654 | "cell_type": "code", 655 | "execution_count": null, 656 | "metadata": { 657 | "collapsed": false 658 | }, 659 | "outputs": [], 660 | "source": [ 661 | "# eigenvectors & eigenvalues\n", 662 | "np.linalg.eig(two_dim_array)" 663 | ] 664 | }, 665 | { 666 | "cell_type": "markdown", 667 | "metadata": {}, 668 | "source": [ 669 | "#### Air quality data " 670 | ] 671 | }, 672 | { 673 | "cell_type": "code", 674 | "execution_count": null, 675 | "metadata": { 676 | "collapsed": false 677 | }, 678 | "outputs": [], 679 | "source": [ 680 | "from IPython.display import HTML\n", 681 | "HTML('')" 683 | ] 684 | }, 685 | { 686 | "cell_type": "markdown", 687 | "metadata": {}, 688 | "source": [ 689 | "##### Loading the data " 690 | ] 691 | }, 692 | { 693 | "cell_type": "code", 694 | "execution_count": null, 695 | "metadata": { 696 | "collapsed": false 697 | }, 698 | "outputs": [], 699 | "source": [ 700 | "# Linux command \n", 701 | "!head ./data/barrio_del_pilar-20160322.csv\n", 702 | "\n", 703 | "# Windows\n", 704 | "# !gc log.txt | select -first 10 # head" 705 | ] 706 | }, 707 | { 708 | "cell_type": "code", 709 | "execution_count": null, 710 | "metadata": { 711 | "collapsed": false 712 | }, 713 | "outputs": [], 714 | "source": [ 715 | "# loading the data\n", 716 | "# ./data/barrio_del_pilar-20160322.csv\n", 717 | "data1 = np.genfromtxt('./data/barrio_del_pilar-20160322.csv', skip_header=3, delimiter=';', usecols=(2,3,4))\n", 718 | "data1" 719 | ] 720 | }, 721 | { 722 | "cell_type": "markdown", 723 | "metadata": {}, 724 | "source": [ 725 | "##### Dealing with missing values " 726 | ] 727 | }, 728 | { 729 | "cell_type": "code", 730 | "execution_count": null, 731 | "metadata": { 732 | "collapsed": false 733 | }, 734 | "outputs": [], 735 | "source": [ 736 | "np.mean(data1, axis=0)" 737 | ] 738 | }, 739 | { 740 | "cell_type": "code", 741 | "execution_count": null, 742 | "metadata": { 743 | "collapsed": false, 744 | "scrolled": true 745 | }, 746 | "outputs": [], 747 | "source": [ 748 | "np.nanmean(data1, axis=0)" 749 | ] 750 | }, 751 | { 752 | "cell_type": "code", 753 | "execution_count": null, 754 | "metadata": { 755 | "collapsed": false 756 | }, 757 | "outputs": [], 758 | "source": [ 759 | "# masking invalid data\n", 760 | "data1 = np.ma.masked_invalid(data1)\n", 761 | "np.mean(data1, axis=0)" 762 | ] 763 | }, 764 | { 765 | "cell_type": "code", 766 | "execution_count": null, 767 | "metadata": { 768 | "collapsed": true 769 | }, 770 | "outputs": [], 771 | "source": [ 772 | "data2 = np.genfromtxt('./data/barrio_del_pilar-20151222.csv', skip_header=3, delimiter=';', usecols=(2,3,4))\n", 773 | "data2 = np.ma.masked_invalid(data2)" 774 | ] 775 | }, 776 | { 777 | "cell_type": "markdown", 778 | "metadata": {}, 779 | "source": [ 780 | "##### Plotting the data " 781 | ] 782 | }, 783 | { 784 | "cell_type": "markdown", 785 | "metadata": {}, 786 | "source": [ 787 | "** Maximum values ** from: http://www.mambiente.munimadrid.es/opencms/export/sites/default/calaire/Anexos/valores_limite_1.pdf" 788 | ] 789 | }, 790 | { 791 | "cell_type": "markdown", 792 | "metadata": {}, 793 | "source": [ 794 | "* NO2\n", 795 | " - Media anual: 40 µg/m3\n", 796 | " - **Media horaria: 200 µg/m3 **" 797 | ] 798 | }, 799 | { 800 | "cell_type": "code", 801 | "execution_count": null, 802 | "metadata": { 803 | "collapsed": false 804 | }, 805 | "outputs": [], 806 | "source": [ 807 | "plt.plot(data1[:, 1], label='2016')\n", 808 | "plt.plot(data2[:, 1], label='2015')\n", 809 | "\n", 810 | "plt.legend()\n", 811 | "\n", 812 | "plt.hlines(200, 0, 200, linestyles='--')\n", 813 | "plt.ylim(0, 220)" 814 | ] 815 | }, 816 | { 817 | "cell_type": "code", 818 | "execution_count": null, 819 | "metadata": { 820 | "collapsed": false 821 | }, 822 | "outputs": [], 823 | "source": [ 824 | "from IPython.display import HTML\n", 825 | "HTML('')" 826 | ] 827 | }, 828 | { 829 | "cell_type": "markdown", 830 | "metadata": {}, 831 | "source": [ 832 | "* CO \n", 833 | " - **Máxima diaria de las medias móviles octohorarias: 10 mg/m³**" 834 | ] 835 | }, 836 | { 837 | "cell_type": "code", 838 | "execution_count": null, 839 | "metadata": { 840 | "collapsed": true 841 | }, 842 | "outputs": [], 843 | "source": [ 844 | "# http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.convolve.html\n", 845 | "def moving_average(x, N=8):\n", 846 | " return np.convolve(x, np.ones(N)/N, mode='same')" 847 | ] 848 | }, 849 | { 850 | "cell_type": "code", 851 | "execution_count": null, 852 | "metadata": { 853 | "collapsed": false 854 | }, 855 | "outputs": [], 856 | "source": [ 857 | "plt.plot(moving_average(data1[:, 0]), label='2016')\n", 858 | "\n", 859 | "plt.plot(moving_average(data2[:, 0]), label='2015')\n", 860 | "\n", 861 | "plt.hlines(10, 0, 250, linestyles='--')\n", 862 | "plt.ylim(0, 11)\n", 863 | "\n", 864 | "plt.legend()" 865 | ] 866 | }, 867 | { 868 | "cell_type": "markdown", 869 | "metadata": {}, 870 | "source": [ 871 | "* O3\n", 872 | " - **Máxima diaria de las medias móviles octohorarias: 120 µg/m3**\n", 873 | " - Umbral de información. 180 µg/m3\n", 874 | " - Media horaria. Umbral de alerta. 240 µg/m3" 875 | ] 876 | }, 877 | { 878 | "cell_type": "code", 879 | "execution_count": null, 880 | "metadata": { 881 | "collapsed": false 882 | }, 883 | "outputs": [], 884 | "source": [ 885 | "plt.plot(moving_average(data1[:, 2]), label='2016')\n", 886 | "#plt.plot(data1[:, 2])\n", 887 | "\n", 888 | "plt.plot(moving_average(data2[:, 2]), label='2015')\n", 889 | "#plt.plot(data2[:, 2])\n", 890 | "\n", 891 | "plt.hlines(180, 0, 250, linestyles='--')\n", 892 | "plt.ylim(0, 190)\n", 893 | "\n", 894 | "plt.legend()" 895 | ] 896 | }, 897 | { 898 | "cell_type": "markdown", 899 | "metadata": {}, 900 | "source": [ 901 | "### 4. Scientific functions: SciPy" 902 | ] 903 | }, 904 | { 905 | "cell_type": "markdown", 906 | "metadata": {}, 907 | "source": [ 908 | "![matplotlib](./static/scipy_logo.png)" 909 | ] 910 | }, 911 | { 912 | "cell_type": "markdown", 913 | "metadata": {}, 914 | "source": [ 915 | "```\n", 916 | "scipy.linalg: ATLAS LAPACK and BLAS libraries\n", 917 | "\n", 918 | "scipy.stats: distributions, statistical functions...\n", 919 | "\n", 920 | "scipy.integrate: integration of functions and ODEs\n", 921 | "\n", 922 | "scipy.optimization: local and global optimization, fitting, root finding...\n", 923 | "\n", 924 | "scipy.interpolate: interpolation, splines...\n", 925 | "\n", 926 | "scipy.fftpack: Fourier trasnforms\n", 927 | "\n", 928 | "scipy.signal, scipy.special, scipy.io\n", 929 | "```" 930 | ] 931 | }, 932 | { 933 | "cell_type": "markdown", 934 | "metadata": {}, 935 | "source": [ 936 | "#### Temperature and precipitation data " 937 | ] 938 | }, 939 | { 940 | "cell_type": "markdown", 941 | "metadata": {}, 942 | "source": [ 943 | "Now, we will use some temperature data from Consejeria Agricultura Pesca y Desarrollo Rural Andalucía." 944 | ] 945 | }, 946 | { 947 | "cell_type": "code", 948 | "execution_count": null, 949 | "metadata": { 950 | "collapsed": false 951 | }, 952 | "outputs": [], 953 | "source": [ 954 | "HTML('')" 955 | ] 956 | }, 957 | { 958 | "cell_type": "markdown", 959 | "metadata": {}, 960 | "source": [ 961 | "The file contains data from 2004 to 2015 (included). Each row corresponds to a day of the year, so evey 365 lines contain data from a whole year*\n", 962 | "\n", 963 | "Note1: 29th February has been removed for leap-years.\n", 964 | "Note2: Missing values have been replaced with the immediately prior valid data.\n", 965 | "\n", 966 | "These kind of events are better handled with Pandas!" 967 | ] 968 | }, 969 | { 970 | "cell_type": "code", 971 | "execution_count": null, 972 | "metadata": { 973 | "collapsed": false 974 | }, 975 | "outputs": [], 976 | "source": [ 977 | "!head 'data/tabernas_meteo_data.csv'" 978 | ] 979 | }, 980 | { 981 | "cell_type": "code", 982 | "execution_count": null, 983 | "metadata": { 984 | "collapsed": true 985 | }, 986 | "outputs": [], 987 | "source": [ 988 | "data_file = 'data/tabernas_meteo_data.csv'" 989 | ] 990 | }, 991 | { 992 | "cell_type": "code", 993 | "execution_count": null, 994 | "metadata": { 995 | "collapsed": false 996 | }, 997 | "outputs": [], 998 | "source": [ 999 | "# Loading the data\n", 1000 | "temp_data = np.genfromtxt(data_file, skip_header=2, delimiter=';', usecols=(1,2,3,4))" 1001 | ] 1002 | }, 1003 | { 1004 | "cell_type": "code", 1005 | "execution_count": null, 1006 | "metadata": { 1007 | "collapsed": false 1008 | }, 1009 | "outputs": [], 1010 | "source": [ 1011 | "# Importing SciPy stats\n", 1012 | "import scipy.stats as st" 1013 | ] 1014 | }, 1015 | { 1016 | "cell_type": "code", 1017 | "execution_count": null, 1018 | "metadata": { 1019 | "collapsed": false 1020 | }, 1021 | "outputs": [], 1022 | "source": [ 1023 | "# Applying some functions: describe, mode, mean...\n", 1024 | "st.describe(temp_data)" 1025 | ] 1026 | }, 1027 | { 1028 | "cell_type": "code", 1029 | "execution_count": null, 1030 | "metadata": { 1031 | "collapsed": false 1032 | }, 1033 | "outputs": [], 1034 | "source": [ 1035 | "st.mode(temp_data.astype(int))" 1036 | ] 1037 | }, 1038 | { 1039 | "cell_type": "code", 1040 | "execution_count": null, 1041 | "metadata": { 1042 | "collapsed": false 1043 | }, 1044 | "outputs": [], 1045 | "source": [ 1046 | "np.mean(temp_data, axis=0)" 1047 | ] 1048 | }, 1049 | { 1050 | "cell_type": "code", 1051 | "execution_count": null, 1052 | "metadata": { 1053 | "collapsed": false 1054 | }, 1055 | "outputs": [], 1056 | "source": [ 1057 | "np.median(temp_data, axis=0)" 1058 | ] 1059 | }, 1060 | { 1061 | "cell_type": "markdown", 1062 | "metadata": {}, 1063 | "source": [ 1064 | "We can also get information about **percentiles**!" 1065 | ] 1066 | }, 1067 | { 1068 | "cell_type": "code", 1069 | "execution_count": null, 1070 | "metadata": { 1071 | "collapsed": false 1072 | }, 1073 | "outputs": [], 1074 | "source": [ 1075 | "st.scoreatpercentile(temp_data, per=25, axis=0)" 1076 | ] 1077 | }, 1078 | { 1079 | "cell_type": "code", 1080 | "execution_count": null, 1081 | "metadata": { 1082 | "collapsed": false 1083 | }, 1084 | "outputs": [], 1085 | "source": [ 1086 | "st.percentileofscore(temp_data[:,0], score=0)" 1087 | ] 1088 | }, 1089 | { 1090 | "cell_type": "code", 1091 | "execution_count": null, 1092 | "metadata": { 1093 | "collapsed": false 1094 | }, 1095 | "outputs": [], 1096 | "source": [ 1097 | "st.percentileofscore(temp_data[:,1], score=0)" 1098 | ] 1099 | }, 1100 | { 1101 | "cell_type": "code", 1102 | "execution_count": null, 1103 | "metadata": { 1104 | "collapsed": false 1105 | }, 1106 | "outputs": [], 1107 | "source": [ 1108 | "st.percentileofscore(temp_data[:,2], score=0)" 1109 | ] 1110 | }, 1111 | { 1112 | "cell_type": "markdown", 1113 | "metadata": {}, 1114 | "source": [ 1115 | "##### Rearranging the data " 1116 | ] 1117 | }, 1118 | { 1119 | "cell_type": "markdown", 1120 | "metadata": {}, 1121 | "source": [ 1122 | "![3d_array](./static/3d_array.png)" 1123 | ] 1124 | }, 1125 | { 1126 | "cell_type": "code", 1127 | "execution_count": null, 1128 | "metadata": { 1129 | "collapsed": false 1130 | }, 1131 | "outputs": [], 1132 | "source": [ 1133 | "temp_data2 = np.zeros([365, 4, 15])\n", 1134 | "\n", 1135 | "for year in range(15):\n", 1136 | " temp_data2[:, :, year] = temp_data[year*365:(year+1)*365, :]\n", 1137 | " temp_data2[:, :, year] = temp_data2[::-1, :, year]" 1138 | ] 1139 | }, 1140 | { 1141 | "cell_type": "code", 1142 | "execution_count": null, 1143 | "metadata": { 1144 | "collapsed": false 1145 | }, 1146 | "outputs": [], 1147 | "source": [ 1148 | "# Calculating mean of mean temp\n", 1149 | "mean_mean = np.mean(temp_data2[:, 2, :], axis=1)\n", 1150 | "# max of max\n", 1151 | "max_max = np.max(temp_data2[:, 0, :], axis=1)\n", 1152 | "# min of min\n", 1153 | "min_min = np.min(temp_data2[:, 1, :], axis=1)" 1154 | ] 1155 | }, 1156 | { 1157 | "cell_type": "markdown", 1158 | "metadata": {}, 1159 | "source": [ 1160 | "##### Let's visualize data! " 1161 | ] 1162 | }, 1163 | { 1164 | "cell_type": "markdown", 1165 | "metadata": {}, 1166 | "source": [ 1167 | "Using matplotlib styles http://matplotlib.org/users/whats_new.html#styles" 1168 | ] 1169 | }, 1170 | { 1171 | "cell_type": "code", 1172 | "execution_count": null, 1173 | "metadata": { 1174 | "collapsed": false 1175 | }, 1176 | "outputs": [], 1177 | "source": [ 1178 | "%matplotlib inline\n", 1179 | "plt.style.available" 1180 | ] 1181 | }, 1182 | { 1183 | "cell_type": "code", 1184 | "execution_count": null, 1185 | "metadata": { 1186 | "collapsed": false 1187 | }, 1188 | "outputs": [], 1189 | "source": [ 1190 | "plt.style.use('ggplot')" 1191 | ] 1192 | }, 1193 | { 1194 | "cell_type": "code", 1195 | "execution_count": null, 1196 | "metadata": { 1197 | "collapsed": false 1198 | }, 1199 | "outputs": [], 1200 | "source": [ 1201 | "days = np.arange(1, 366)\n", 1202 | "\n", 1203 | "plt.fill_between(days, max_max, min_min, alpha=0.7)\n", 1204 | "plt.plot(days, mean_mean)\n", 1205 | "plt.xlim(1, 365)" 1206 | ] 1207 | }, 1208 | { 1209 | "cell_type": "markdown", 1210 | "metadata": {}, 1211 | "source": [ 1212 | "Let's see if 2015 was a normal year..." 1213 | ] 1214 | }, 1215 | { 1216 | "cell_type": "code", 1217 | "execution_count": null, 1218 | "metadata": { 1219 | "collapsed": false 1220 | }, 1221 | "outputs": [], 1222 | "source": [ 1223 | "plt.plot(days, temp_data2[:,2,-1], lw=2)\n", 1224 | "plt.plot(days, mean_mean)\n", 1225 | "plt.xlim(1, 365)" 1226 | ] 1227 | }, 1228 | { 1229 | "cell_type": "code", 1230 | "execution_count": null, 1231 | "metadata": { 1232 | "collapsed": false 1233 | }, 1234 | "outputs": [], 1235 | "source": [ 1236 | "plt.fill_between(days, max_max, min_min, alpha=0.7)\n", 1237 | "plt.fill_between(days, temp_data2[:,0,-1], temp_data2[:,1,-1],\n", 1238 | " color='purple', alpha=0.5)\n", 1239 | "plt.plot(days, temp_data2[:,2,-1], lw=2)\n", 1240 | "plt.xlim(1, 365)" 1241 | ] 1242 | }, 1243 | { 1244 | "cell_type": "markdown", 1245 | "metadata": {}, 1246 | "source": [ 1247 | "For example, lets represent a function over a 2D domain!\n", 1248 | "\n", 1249 | "For this we will use the contour function, which requires some special inputs..." 1250 | ] 1251 | }, 1252 | { 1253 | "cell_type": "code", 1254 | "execution_count": null, 1255 | "metadata": { 1256 | "collapsed": true 1257 | }, 1258 | "outputs": [], 1259 | "source": [ 1260 | "#we will use numpy functions in order to work with numpy arrays\n", 1261 | "def funcion(x,y):\n", 1262 | " return np.cos(x) + np.sin(y)" 1263 | ] 1264 | }, 1265 | { 1266 | "cell_type": "code", 1267 | "execution_count": null, 1268 | "metadata": { 1269 | "collapsed": false 1270 | }, 1271 | "outputs": [], 1272 | "source": [ 1273 | "# 0D: works!\n", 1274 | "funcion(3,5)" 1275 | ] 1276 | }, 1277 | { 1278 | "cell_type": "code", 1279 | "execution_count": null, 1280 | "metadata": { 1281 | "collapsed": false 1282 | }, 1283 | "outputs": [], 1284 | "source": [ 1285 | "# 1D: works!\n", 1286 | "x = np.linspace(0, 5, 100)\n", 1287 | "plt.plot(x, funcion(x,1))" 1288 | ] 1289 | }, 1290 | { 1291 | "cell_type": "markdown", 1292 | "metadata": {}, 1293 | "source": [ 1294 | "### Matplotlib AGAIN, Now with 2D COLOURSSS!!1!1!11" 1295 | ] 1296 | }, 1297 | { 1298 | "cell_type": "markdown", 1299 | "metadata": {}, 1300 | "source": [ 1301 | "![Matplotlib-logo](./static/matplotlib.png)" 1302 | ] 1303 | }, 1304 | { 1305 | "cell_type": "markdown", 1306 | "metadata": {}, 1307 | "source": [ 1308 | "For example, lets represent a function over a 2D domain!\n", 1309 | "\n", 1310 | "For this we will use the contour function, which requires some special inputs..." 1311 | ] 1312 | }, 1313 | { 1314 | "cell_type": "code", 1315 | "execution_count": null, 1316 | "metadata": { 1317 | "collapsed": false 1318 | }, 1319 | "outputs": [], 1320 | "source": [ 1321 | "#we will use numpy functions in order to work with numpy arrays\n", 1322 | "def funcion(x,y):\n", 1323 | " '''This is the help of the function funcion.\n", 1324 | " This function receives two parameters and returns another.\n", 1325 | " Such sicence. Wow.'''\n", 1326 | " return ????" 1327 | ] 1328 | }, 1329 | { 1330 | "cell_type": "code", 1331 | "execution_count": null, 1332 | "metadata": { 1333 | "collapsed": false 1334 | }, 1335 | "outputs": [], 1336 | "source": [ 1337 | "# 0D: works?\n", 1338 | "funcion(,)" 1339 | ] 1340 | }, 1341 | { 1342 | "cell_type": "code", 1343 | "execution_count": null, 1344 | "metadata": { 1345 | "collapsed": false 1346 | }, 1347 | "outputs": [], 1348 | "source": [ 1349 | "# 1D: works?\n", 1350 | "x = np.???(0,5, 100)\n", 1351 | "z = funcion(,)\n", 1352 | "z" 1353 | ] 1354 | }, 1355 | { 1356 | "cell_type": "code", 1357 | "execution_count": null, 1358 | "metadata": { 1359 | "collapsed": true 1360 | }, 1361 | "outputs": [], 1362 | "source": [ 1363 | "plt.plot(x, z)" 1364 | ] 1365 | }, 1366 | { 1367 | "cell_type": "markdown", 1368 | "metadata": {}, 1369 | "source": [ 1370 | "In oder to plot the 2D function, we will need a grid.\n", 1371 | "\n", 1372 | "For 1D domain, we just needed one 1D array containning the X position and another 1D array containing the value.\n", 1373 | "\n", 1374 | "Now, we will create a grid, a distribution of points covering a surface. For the 2D domain, we will need:\n", 1375 | "- One 2D array containing the X coordinate of the points.\n", 1376 | "- One 2D array containing the Y coordinate of the points.\n", 1377 | "- One 2D array containing the function value at the points.\n", 1378 | "\n", 1379 | "The three matrices must have the exact same dimensions, because each cell of them represents a particular point." 1380 | ] 1381 | }, 1382 | { 1383 | "cell_type": "code", 1384 | "execution_count": null, 1385 | "metadata": { 1386 | "collapsed": true 1387 | }, 1388 | "outputs": [], 1389 | "source": [ 1390 | "#We can create the X and Y matrices by hand, or use a function designed to make ir easy:\n", 1391 | "\n", 1392 | "#we create two 1D arrays of the desired lengths:\n", 1393 | "x_1d = np.linspace(0, 5, 5)\n", 1394 | "y_1d = np.linspace(-2, 4, 7)" 1395 | ] 1396 | }, 1397 | { 1398 | "cell_type": "code", 1399 | "execution_count": null, 1400 | "metadata": { 1401 | "collapsed": false 1402 | }, 1403 | "outputs": [], 1404 | "source": [ 1405 | "x_1d" 1406 | ] 1407 | }, 1408 | { 1409 | "cell_type": "code", 1410 | "execution_count": null, 1411 | "metadata": { 1412 | "collapsed": true 1413 | }, 1414 | "outputs": [], 1415 | "source": [ 1416 | "y_1d" 1417 | ] 1418 | }, 1419 | { 1420 | "cell_type": "code", 1421 | "execution_count": null, 1422 | "metadata": { 1423 | "collapsed": true 1424 | }, 1425 | "outputs": [], 1426 | "source": [ 1427 | "#And we use the meshgrid function to create the X and Y matrices!\n", 1428 | "X, Y = np.meshgrid(x_1d, y_1d)" 1429 | ] 1430 | }, 1431 | { 1432 | "cell_type": "code", 1433 | "execution_count": null, 1434 | "metadata": { 1435 | "collapsed": false 1436 | }, 1437 | "outputs": [], 1438 | "source": [ 1439 | "X" 1440 | ] 1441 | }, 1442 | { 1443 | "cell_type": "code", 1444 | "execution_count": null, 1445 | "metadata": { 1446 | "collapsed": false 1447 | }, 1448 | "outputs": [], 1449 | "source": [ 1450 | "Y" 1451 | ] 1452 | }, 1453 | { 1454 | "cell_type": "markdown", 1455 | "metadata": {}, 1456 | "source": [ 1457 | "Note that with the meshgrid function we can only create rectangular grids" 1458 | ] 1459 | }, 1460 | { 1461 | "cell_type": "code", 1462 | "execution_count": null, 1463 | "metadata": { 1464 | "collapsed": true 1465 | }, 1466 | "outputs": [], 1467 | "source": [ 1468 | "#Using Numpy arrays, calculating the function value at the points is easy!\n", 1469 | "Z = funcion(X,Y)" 1470 | ] 1471 | }, 1472 | { 1473 | "cell_type": "code", 1474 | "execution_count": null, 1475 | "metadata": { 1476 | "collapsed": false 1477 | }, 1478 | "outputs": [], 1479 | "source": [ 1480 | "#Let's plot it!\n", 1481 | "plt.contour(X, Y, Z)\n", 1482 | "plt.colorbar()" 1483 | ] 1484 | }, 1485 | { 1486 | "cell_type": "markdown", 1487 | "metadata": {}, 1488 | "source": [ 1489 | "We can try a little more resolution..." 1490 | ] 1491 | }, 1492 | { 1493 | "cell_type": "code", 1494 | "execution_count": null, 1495 | "metadata": { 1496 | "collapsed": false 1497 | }, 1498 | "outputs": [], 1499 | "source": [ 1500 | "x_1d = np.linspace(0, 5, 100)\n", 1501 | "y_1d = np.linspace(-2, 4, 100)\n", 1502 | "X, Y = np.meshgrid(x_1d, y_1d)\n", 1503 | "Z = funcion(X,Y)\n", 1504 | "plt.contour(X, Y, Z)\n", 1505 | "plt.colorbar()" 1506 | ] 1507 | }, 1508 | { 1509 | "cell_type": "markdown", 1510 | "metadata": {}, 1511 | "source": [ 1512 | "The countourf function is simmilar, but it colours also between the lines. In both functions, we can manually adjust the number of lines/zones we want to differentiate on the plot." 1513 | ] 1514 | }, 1515 | { 1516 | "cell_type": "code", 1517 | "execution_count": null, 1518 | "metadata": { 1519 | "collapsed": false 1520 | }, 1521 | "outputs": [], 1522 | "source": [ 1523 | "plt.contourf(X, Y, Z, np.linspace(-2, 2, 6),cmap=plt.cm.Spectral) #With cmap, a color map is specified\n", 1524 | "plt.colorbar()" 1525 | ] 1526 | }, 1527 | { 1528 | "cell_type": "code", 1529 | "execution_count": null, 1530 | "metadata": { 1531 | "collapsed": false, 1532 | "scrolled": true 1533 | }, 1534 | "outputs": [], 1535 | "source": [ 1536 | "plt.contourf(X, Y, Z, np.linspace(-2, 2, 100),cmap=plt.cm.Spectral)\n", 1537 | "plt.colorbar()" 1538 | ] 1539 | }, 1540 | { 1541 | "cell_type": "code", 1542 | "execution_count": null, 1543 | "metadata": { 1544 | "collapsed": false 1545 | }, 1546 | "outputs": [], 1547 | "source": [ 1548 | "#We can even combine them!\n", 1549 | "plt.contourf(X, Y, Z, np.linspace(-2, 2, 100),cmap=plt.cm.Spectral)\n", 1550 | "plt.colorbar()\n", 1551 | "cs = plt.contour(X, Y, Z, np.linspace(-2, 2, 9), colors='k')\n", 1552 | "plt.clabel(cs)\n" 1553 | ] 1554 | }, 1555 | { 1556 | "cell_type": "markdown", 1557 | "metadata": {}, 1558 | "source": [ 1559 | "These functions can be enormously useful when you want to visualize something.\n", 1560 | "\n", 1561 | "And remember!\n", 1562 | "\n", 1563 | "## Always visualize data!\n", 1564 | "\n", 1565 | "Let's try it with **Real** data!" 1566 | ] 1567 | }, 1568 | { 1569 | "cell_type": "code", 1570 | "execution_count": null, 1571 | "metadata": { 1572 | "collapsed": true 1573 | }, 1574 | "outputs": [], 1575 | "source": [ 1576 | "time_vector = np.loadtxt('data/ligo_tiempos.txt')\n", 1577 | "frequency_vector = np.loadtxt('data/ligo_frecuencias.txt')\n", 1578 | "intensity_matrix = np.loadtxt('data/ligo_datos.txt')" 1579 | ] 1580 | }, 1581 | { 1582 | "cell_type": "markdown", 1583 | "metadata": {}, 1584 | "source": [ 1585 | "The time and frequency vectors contain the values at which the instrument was reading, and the intensity matrix, the postprocessed strength measured for each frequency at each time.\n", 1586 | "\n", 1587 | "We need again to create the 2D arrays of coordinates." 1588 | ] 1589 | }, 1590 | { 1591 | "cell_type": "code", 1592 | "execution_count": null, 1593 | "metadata": { 1594 | "collapsed": true 1595 | }, 1596 | "outputs": [], 1597 | "source": [ 1598 | "time_2D, freq_2D = np.meshgrid(time_vector, frequency_vector)" 1599 | ] 1600 | }, 1601 | { 1602 | "cell_type": "code", 1603 | "execution_count": null, 1604 | "metadata": { 1605 | "collapsed": false 1606 | }, 1607 | "outputs": [], 1608 | "source": [ 1609 | "plt.figure(figsize=(10,6)) #We can manually adjust the sice of the picture\n", 1610 | "plt.contourf(time_2D, freq_2D, intensity_matrix, np.linspace(0, 0.02313, 200), cmap='bone')\n", 1611 | "plt.xlabel('time (s)')\n", 1612 | "plt.ylabel('Frequency (Hz)')\n", 1613 | "plt.colorbar()" 1614 | ] 1615 | }, 1616 | { 1617 | "cell_type": "markdown", 1618 | "metadata": {}, 1619 | "source": [ 1620 | "Wow! What is that? Let's zoom into it!" 1621 | ] 1622 | }, 1623 | { 1624 | "cell_type": "code", 1625 | "execution_count": null, 1626 | "metadata": { 1627 | "collapsed": false 1628 | }, 1629 | "outputs": [], 1630 | "source": [ 1631 | "\n", 1632 | "plt.figure(figsize=(10,6))\n", 1633 | "plt.contourf(time_2D, freq_2D,intensity_matrix,np.linspace(0, 0.02313, 200),cmap = plt.cm.Spectral)\n", 1634 | "plt.colorbar()\n", 1635 | "plt.contour(time_2D, freq_2D,intensity_matrix,np.linspace(0, 0.02313, 9), colors='k')\n", 1636 | "plt.xlabel('time (s)')\n", 1637 | "plt.ylabel('Frequency (Hz)')\n", 1638 | "\n", 1639 | "plt.axis([9.9, 10.05, 0, 300])" 1640 | ] 1641 | }, 1642 | { 1643 | "cell_type": "markdown", 1644 | "metadata": {}, 1645 | "source": [ 1646 | "# IPython Widgets" 1647 | ] 1648 | }, 1649 | { 1650 | "cell_type": "markdown", 1651 | "metadata": {}, 1652 | "source": [ 1653 | "The IPython Widgets are interactive tools to use in the notebook. They are fun and very useful to quickly understand how different parameters affect a certain function.\n", 1654 | "\n", 1655 | "This is based on a section of the PyConEs 14 talk by Kiko Correoso \"Hacking the notebook\": http://nbviewer.jupyter.org/github/kikocorreoso/PyConES14_talk-Hacking_the_Notebook/blob/master/notebooks/Using%20Interact.ipynb" 1656 | ] 1657 | }, 1658 | { 1659 | "cell_type": "code", 1660 | "execution_count": null, 1661 | "metadata": { 1662 | "collapsed": true 1663 | }, 1664 | "outputs": [], 1665 | "source": [ 1666 | "from ipywidgets import interact" 1667 | ] 1668 | }, 1669 | { 1670 | "cell_type": "code", 1671 | "execution_count": null, 1672 | "metadata": { 1673 | "collapsed": true 1674 | }, 1675 | "outputs": [], 1676 | "source": [ 1677 | "#Lets define a extremely simple function:\n", 1678 | "def ejemplo(x):\n", 1679 | " print(x)" 1680 | ] 1681 | }, 1682 | { 1683 | "cell_type": "code", 1684 | "execution_count": null, 1685 | "metadata": { 1686 | "collapsed": false 1687 | }, 1688 | "outputs": [], 1689 | "source": [ 1690 | "interact(ejemplo, x =10) #Try changing the value of x to True, 'Hello' or ['hello', 'world']" 1691 | ] 1692 | }, 1693 | { 1694 | "cell_type": "code", 1695 | "execution_count": null, 1696 | "metadata": { 1697 | "collapsed": false 1698 | }, 1699 | "outputs": [], 1700 | "source": [ 1701 | "#We can control the slider values with more precission:\n", 1702 | "interact(ejemplo, x = (9, 10, 0.1))" 1703 | ] 1704 | }, 1705 | { 1706 | "cell_type": "markdown", 1707 | "metadata": {}, 1708 | "source": [ 1709 | "If you want a dropdown menu that passes non-string values to the Python function, you can pass a dictionary. The keys in the dictionary are used for the names in the dropdown menu UI and the values are the arguments that are passed to the underlying Python function." 1710 | ] 1711 | }, 1712 | { 1713 | "cell_type": "code", 1714 | "execution_count": null, 1715 | "metadata": { 1716 | "collapsed": false 1717 | }, 1718 | "outputs": [], 1719 | "source": [ 1720 | "interact(ejemplo, x={'one': 10, 'two': 20})" 1721 | ] 1722 | }, 1723 | { 1724 | "cell_type": "markdown", 1725 | "metadata": {}, 1726 | "source": [ 1727 | "Let's have some fun! We talked before about frequencys and waves. Have you ever learn about AM and FM modulation? It's the process used to send radio communications!" 1728 | ] 1729 | }, 1730 | { 1731 | "cell_type": "code", 1732 | "execution_count": null, 1733 | "metadata": { 1734 | "collapsed": false 1735 | }, 1736 | "outputs": [], 1737 | "source": [ 1738 | "x = np.linspace(-1, 7, 1000)\n", 1739 | "\n", 1740 | "fig = plt.figure()\n", 1741 | "\n", 1742 | "plt.subplot(211)#This allows us to display multiple sub-plots, and where to put them\n", 1743 | "plt.plot(x, np.sin(x))\n", 1744 | "plt.grid(False)\n", 1745 | "plt.title(\"Audio signal: modulator\")\n", 1746 | "\n", 1747 | "plt.subplot(212)\n", 1748 | "plt.plot(x, np.sin(50 * x))\n", 1749 | "plt.grid(False)\n", 1750 | "plt.title(\"Radio signal: carrier\")" 1751 | ] 1752 | }, 1753 | { 1754 | "cell_type": "code", 1755 | "execution_count": null, 1756 | "metadata": { 1757 | "collapsed": false 1758 | }, 1759 | "outputs": [], 1760 | "source": [ 1761 | "#Am modulation simply works like this:\n", 1762 | "am_wave = np.sin(50 * x) * (0.5 + 0.5 * np.sin(x))\n", 1763 | "plt.plot(x, am_wave)" 1764 | ] 1765 | }, 1766 | { 1767 | "cell_type": "markdown", 1768 | "metadata": {}, 1769 | "source": [ 1770 | "In order to interact with it, we will need to transform it into a function" 1771 | ] 1772 | }, 1773 | { 1774 | "cell_type": "code", 1775 | "execution_count": null, 1776 | "metadata": { 1777 | "collapsed": false 1778 | }, 1779 | "outputs": [], 1780 | "source": [ 1781 | "def am_mod (f_carr=50, f_mod=1, depth=0.5): #The default values will be the starting points of the sliders\n", 1782 | " x = np.linspace(-1, 7, 1000)\n", 1783 | " am_wave = np.sin(f_carr * x) * (1- depth/2 + depth/2 * np.sin(f_mod * x))\n", 1784 | " \n", 1785 | " plt.plot(x, am_wave)\n", 1786 | " " 1787 | ] 1788 | }, 1789 | { 1790 | "cell_type": "code", 1791 | "execution_count": null, 1792 | "metadata": { 1793 | "collapsed": false 1794 | }, 1795 | "outputs": [], 1796 | "source": [ 1797 | "interact(am_mod,\n", 1798 | " f_carr = (1,100,2),\n", 1799 | " f_mod = (0.2, 2, 0.1),\n", 1800 | " depth = (0, 1, 0.1))" 1801 | ] 1802 | }, 1803 | { 1804 | "cell_type": "markdown", 1805 | "metadata": {}, 1806 | "source": [ 1807 | "#### Other options... \n", 1808 | "\n" 1809 | ] 1810 | }, 1811 | { 1812 | "cell_type": "markdown", 1813 | "metadata": {}, 1814 | "source": [ 1815 | "### 5. Other packages" 1816 | ] 1817 | }, 1818 | { 1819 | "cell_type": "markdown", 1820 | "metadata": {}, 1821 | "source": [ 1822 | "#### Symbolic calculations with SymPy " 1823 | ] 1824 | }, 1825 | { 1826 | "cell_type": "markdown", 1827 | "metadata": {}, 1828 | "source": [ 1829 | "![sympy](./static/sympy.png)" 1830 | ] 1831 | }, 1832 | { 1833 | "cell_type": "markdown", 1834 | "metadata": {}, 1835 | "source": [ 1836 | "SymPy is a Python package for symbolic math. We will not cover it in depth, but let's take a picure of the basics!" 1837 | ] 1838 | }, 1839 | { 1840 | "cell_type": "code", 1841 | "execution_count": null, 1842 | "metadata": { 1843 | "collapsed": false 1844 | }, 1845 | "outputs": [], 1846 | "source": [ 1847 | "# Importación\n", 1848 | "from sympy import init_session\n", 1849 | "\n", 1850 | "init_session(use_latex='matplotlib') #We must start calling this function" 1851 | ] 1852 | }, 1853 | { 1854 | "cell_type": "markdown", 1855 | "metadata": {}, 1856 | "source": [ 1857 | "The basic unit of this package is the symbol. A simbol object has name and graphic representation, which can be different:" 1858 | ] 1859 | }, 1860 | { 1861 | "cell_type": "code", 1862 | "execution_count": null, 1863 | "metadata": { 1864 | "collapsed": false 1865 | }, 1866 | "outputs": [], 1867 | "source": [ 1868 | "coef_traccion = symbols('c_T')\n", 1869 | "coef_traccion" 1870 | ] 1871 | }, 1872 | { 1873 | "cell_type": "code", 1874 | "execution_count": null, 1875 | "metadata": { 1876 | "collapsed": false 1877 | }, 1878 | "outputs": [], 1879 | "source": [ 1880 | "w = symbols('omega')\n", 1881 | "W = symbols('Omega')\n", 1882 | "w, W" 1883 | ] 1884 | }, 1885 | { 1886 | "cell_type": "markdown", 1887 | "metadata": {}, 1888 | "source": [ 1889 | "By default, SymPy takes symbols as complex numbers. That can lead to unexpected results in front of certain operations, like logarithms. We can explicitly signal that a symbol is real when we create it. We can also create several symbols at a time." 1890 | ] 1891 | }, 1892 | { 1893 | "cell_type": "code", 1894 | "execution_count": null, 1895 | "metadata": { 1896 | "collapsed": false 1897 | }, 1898 | "outputs": [], 1899 | "source": [ 1900 | "x, y, z, t = symbols('x y z t', real=True)\n", 1901 | "x.assumptions0" 1902 | ] 1903 | }, 1904 | { 1905 | "cell_type": "markdown", 1906 | "metadata": {}, 1907 | "source": [ 1908 | "Expressions can be created from symbols:" 1909 | ] 1910 | }, 1911 | { 1912 | "cell_type": "code", 1913 | "execution_count": null, 1914 | "metadata": { 1915 | "collapsed": false 1916 | }, 1917 | "outputs": [], 1918 | "source": [ 1919 | "expr = cos(x)**2 + sin(x)**2\n", 1920 | "expr" 1921 | ] 1922 | }, 1923 | { 1924 | "cell_type": "code", 1925 | "execution_count": null, 1926 | "metadata": { 1927 | "collapsed": false 1928 | }, 1929 | "outputs": [], 1930 | "source": [ 1931 | "simplify(expr)" 1932 | ] 1933 | }, 1934 | { 1935 | "cell_type": "code", 1936 | "execution_count": null, 1937 | "metadata": { 1938 | "collapsed": false 1939 | }, 1940 | "outputs": [], 1941 | "source": [ 1942 | "#We can substitute pieces of the expression:\n", 1943 | "expr.subs(x, y**2)" 1944 | ] 1945 | }, 1946 | { 1947 | "cell_type": "code", 1948 | "execution_count": null, 1949 | "metadata": { 1950 | "collapsed": false 1951 | }, 1952 | "outputs": [], 1953 | "source": [ 1954 | "#We can particularize on a certain value:\n", 1955 | "(sin(x) + 3 * x).subs(x, pi)" 1956 | ] 1957 | }, 1958 | { 1959 | "cell_type": "code", 1960 | "execution_count": null, 1961 | "metadata": { 1962 | "collapsed": false 1963 | }, 1964 | "outputs": [], 1965 | "source": [ 1966 | "#We can evaluate the numerical value with a certain precission:\n", 1967 | "(sin(x) + 3 * x).subs(x, pi).evalf(25)" 1968 | ] 1969 | }, 1970 | { 1971 | "cell_type": "markdown", 1972 | "metadata": {}, 1973 | "source": [ 1974 | "We can manipulate the expression in several ways. For example:" 1975 | ] 1976 | }, 1977 | { 1978 | "cell_type": "code", 1979 | "execution_count": null, 1980 | "metadata": { 1981 | "collapsed": false 1982 | }, 1983 | "outputs": [], 1984 | "source": [ 1985 | "expr1 = (x ** 3 + 3 * y + 2) ** 2\n", 1986 | "expr1" 1987 | ] 1988 | }, 1989 | { 1990 | "cell_type": "code", 1991 | "execution_count": null, 1992 | "metadata": { 1993 | "collapsed": false 1994 | }, 1995 | "outputs": [], 1996 | "source": [ 1997 | "expr1.expand()" 1998 | ] 1999 | }, 2000 | { 2001 | "cell_type": "markdown", 2002 | "metadata": {}, 2003 | "source": [ 2004 | "We can derivate and integrate:" 2005 | ] 2006 | }, 2007 | { 2008 | "cell_type": "code", 2009 | "execution_count": null, 2010 | "metadata": { 2011 | "collapsed": false 2012 | }, 2013 | "outputs": [], 2014 | "source": [ 2015 | "expr = cos(2*x)\n", 2016 | "expr.diff(x, x, x)" 2017 | ] 2018 | }, 2019 | { 2020 | "cell_type": "code", 2021 | "execution_count": null, 2022 | "metadata": { 2023 | "collapsed": false 2024 | }, 2025 | "outputs": [], 2026 | "source": [ 2027 | "expr_xy = y ** 3 * sin(x) ** 2 + x ** 2 * cos(y)\n", 2028 | "expr_xy" 2029 | ] 2030 | }, 2031 | { 2032 | "cell_type": "code", 2033 | "execution_count": null, 2034 | "metadata": { 2035 | "collapsed": false 2036 | }, 2037 | "outputs": [], 2038 | "source": [ 2039 | "diff(expr_xy, x, 2, y, 2)" 2040 | ] 2041 | }, 2042 | { 2043 | "cell_type": "code", 2044 | "execution_count": null, 2045 | "metadata": { 2046 | "collapsed": false 2047 | }, 2048 | "outputs": [], 2049 | "source": [ 2050 | "int2 = 1 / sin(x)\n", 2051 | "integrate(int2)" 2052 | ] 2053 | }, 2054 | { 2055 | "cell_type": "code", 2056 | "execution_count": null, 2057 | "metadata": { 2058 | "collapsed": false 2059 | }, 2060 | "outputs": [], 2061 | "source": [ 2062 | "x, a = symbols('x a', real=True)\n", 2063 | "\n", 2064 | "int3 = 1 / (x**2 + a**2)**2\n", 2065 | "integrate(int3, x)" 2066 | ] 2067 | }, 2068 | { 2069 | "cell_type": "markdown", 2070 | "metadata": {}, 2071 | "source": [ 2072 | "We also have ecuations and differential ecuations:" 2073 | ] 2074 | }, 2075 | { 2076 | "cell_type": "code", 2077 | "execution_count": null, 2078 | "metadata": { 2079 | "collapsed": false 2080 | }, 2081 | "outputs": [], 2082 | "source": [ 2083 | "a, x, t, C = symbols('a, x, t, C', real=True)\n", 2084 | "ecuacion = Eq(a * exp(x/t), C)\n", 2085 | "ecuacion" 2086 | ] 2087 | }, 2088 | { 2089 | "cell_type": "code", 2090 | "execution_count": null, 2091 | "metadata": { 2092 | "collapsed": false 2093 | }, 2094 | "outputs": [], 2095 | "source": [ 2096 | "solve(ecuacion ,x)" 2097 | ] 2098 | }, 2099 | { 2100 | "cell_type": "code", 2101 | "execution_count": null, 2102 | "metadata": { 2103 | "collapsed": false 2104 | }, 2105 | "outputs": [], 2106 | "source": [ 2107 | "x = symbols('x')\n", 2108 | "f = Function('y')\n", 2109 | "ecuacion_dif = Eq(f(x).diff(x,2) + f(x).diff(x) + f(x), cos(x))\n", 2110 | "ecuacion_dif" 2111 | ] 2112 | }, 2113 | { 2114 | "cell_type": "code", 2115 | "execution_count": null, 2116 | "metadata": { 2117 | "collapsed": false 2118 | }, 2119 | "outputs": [], 2120 | "source": [ 2121 | "dsolve(ecuacion_dif, f(x))" 2122 | ] 2123 | }, 2124 | { 2125 | "cell_type": "markdown", 2126 | "metadata": {}, 2127 | "source": [ 2128 | "#### Data Analysis with pandas " 2129 | ] 2130 | }, 2131 | { 2132 | "cell_type": "markdown", 2133 | "metadata": {}, 2134 | "source": [ 2135 | "![pandas](./static/pandas_logo.png)" 2136 | ] 2137 | }, 2138 | { 2139 | "cell_type": "markdown", 2140 | "metadata": {}, 2141 | "source": [ 2142 | "Pandas is a package that focus on data structures and data analysis tools" 2143 | ] 2144 | }, 2145 | { 2146 | "cell_type": "markdown", 2147 | "metadata": {}, 2148 | "source": [ 2149 | "#### Machine Learning with scikit-learn " 2150 | ] 2151 | }, 2152 | { 2153 | "cell_type": "markdown", 2154 | "metadata": {}, 2155 | "source": [ 2156 | "![scikit-learn](./static/scikit-learn-logo.png)" 2157 | ] 2158 | }, 2159 | { 2160 | "cell_type": "markdown", 2161 | "metadata": {}, 2162 | "source": [ 2163 | "Scikit-learn is a very complete Python package focusing on machin learning, and data mining and analysis. We will not cover it in depth because it will be the focus of many more talks at the PyData." 2164 | ] 2165 | }, 2166 | { 2167 | "cell_type": "markdown", 2168 | "metadata": {}, 2169 | "source": [ 2170 | "##### A world of possibilities... " 2171 | ] 2172 | }, 2173 | { 2174 | "cell_type": "markdown", 2175 | "metadata": {}, 2176 | "source": [ 2177 | "![scikit-learn](./static/cheatsheet-scikit-learn.png)" 2178 | ] 2179 | }, 2180 | { 2181 | "cell_type": "markdown", 2182 | "metadata": {}, 2183 | "source": [ 2184 | "# ¡Gracias por vuetra atención! " 2185 | ] 2186 | }, 2187 | { 2188 | "cell_type": "markdown", 2189 | "metadata": {}, 2190 | "source": [ 2191 | "![PyData_logo](./static/pycones2016.jpg)" 2192 | ] 2193 | }, 2194 | { 2195 | "cell_type": "markdown", 2196 | "metadata": {}, 2197 | "source": [ 2198 | "## ¿Alguna pregunta?" 2199 | ] 2200 | }, 2201 | { 2202 | "cell_type": "markdown", 2203 | "metadata": {}, 2204 | "source": [ 2205 | "\n", 2206 | "---\n" 2207 | ] 2208 | }, 2209 | { 2210 | "cell_type": "code", 2211 | "execution_count": null, 2212 | "metadata": { 2213 | "collapsed": false 2214 | }, 2215 | "outputs": [], 2216 | "source": [ 2217 | "# Notebook style\n", 2218 | "from IPython.core.display import HTML\n", 2219 | "css_file = './static/style.css'\n", 2220 | "HTML(open(css_file, \"r\").read())" 2221 | ] 2222 | } 2223 | ], 2224 | "metadata": { 2225 | "kernelspec": { 2226 | "display_name": "Python 3", 2227 | "language": "python", 2228 | "name": "python3" 2229 | }, 2230 | "language_info": { 2231 | "codemirror_mode": { 2232 | "name": "ipython", 2233 | "version": 3 2234 | }, 2235 | "file_extension": ".py", 2236 | "mimetype": "text/x-python", 2237 | "name": "python", 2238 | "nbconvert_exporter": "python", 2239 | "pygments_lexer": "ipython3", 2240 | "version": "3.5.2" 2241 | } 2242 | }, 2243 | "nbformat": 4, 2244 | "nbformat_minor": 0 2245 | } 2246 | --------------------------------------------------------------------------------