Adding captions and attributes to figures and tables from code blocks in org-mode
Posted February 26, 2016 at 12:43 PM | categories: orgmode | tags:
I have wanted for a long time to be able to add captions and attributes to figures and tables generated from code blocks. I brought this up on the mailing list (https://lists.gnu.org/archive/html/emacs-orgmode/2015-11/msg00544.html ) and finally, I have figured out a way to do it that seems ok. It is based on the :wrap feature of org-babel.
The idea is to use a function that will wrap the results in additional text. We use this function that will add a caption and attributes.
(defun src-decorate (&optional caption attributes) "A wrap function for src blocks." (concat "ORG\n" (when attributes (concat (mapconcat 'identity attributes "\n") "\n")) (when caption (format "#+caption: %s" caption))))
src-decorate
Now, we try it out on a figure:
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2 * np.pi) y = np.sin(x) plt.plot(x, y) plt.savefig('images/sin.png') print('[[./images/sin.png' + ']]')
Figure 1: A sin wave.
Success. We can even refer to Fig. fig-sin. Next, we try it on a table.
import numpy as np x = np.linspace(0, 2 * np.pi, 5) y = np.sin(x) return [['x', 'y'], None] + list(zip(x, y))
| x | y |
|---|---|
| 0.0 | 0.0 |
| 1.5707963267948966 | 1.0 |
| 3.141592653589793 | 1.2246467991473532e-16 |
| 4.71238898038469 | -1.0 |
| 6.283185307179586 | -2.4492935982947064e-16 |
Sweet. We can refer to Tab. tab-sin too. I have wanted this for a long time. Perseverance wins again! and org-mode gets better. Hopefully this works with org 8.3 too!
Copyright (C) 2016 by John Kitchin. See the License for information about copying.
Org-mode version = 8.2.10
Pycse is now using Python3
Posted February 25, 2016 at 07:09 PM | categories: python | tags:
Updated February 25, 2016 at 07:17 PM
I spent the last couple of days going through the notes for pycse and updating it for Python 3! If you aren't familiar with pycse, it is an acronym for Python3 Calculations in Science and Engineering, and it is about 400 pages of examples spanning scientific and engineering computations, and a python module that extends numpy/scipy with some functions for adding events to differential equation solvers, and regression with confidence intervals.
It is mostly done, and was pretty easy. The Python module for pycse (http://github.com/jkitchin/pycse ) should also be Python 3 compliant. Yep, on my Mac I have switched over to the Anaconda Python 3 from Continuum IO (thanks for that!).
import sys print(sys.version)
3.5.1 |Anaconda 2.5.0 (x86_64)| (default, Dec 7 2015, 11:24:55) [GCC 4.2.1 (Apple Inc. build 5577)]
Now we can succinctly do matrix multiplication like this:
import numpy as np a = np.array([1, 2, 3]) a = np.array([1, 2, 3]) print(a @ a) # the matrix multiplication operator # instead of print(np.dot(a, a))
14 14
Here is a summary of what the changes to the Python2 version of Pycse entailed.
- Change all print statements to print functions. There were so many…
- Eliminate use of tabs in some code blocks, Python3 is not flexible on that.
I wrote this function to fix both of these for me (I use Emacs as my editor), which worked nicely:
(defun fp () "convert print to print() allowing for line end comments. does not do multiline. and untabify" (interactive) (beginning-of-line) (re-search-forward "print " (line-end-position) t) (delete-char -1) (insert "(") ;; rm spaces (delete-forward-char (save-excursion (skip-chars-forward " "))) (when (re-search-forward "#" (line-end-position) 'move) (goto-char (match-beginning 0))) (skip-chars-backward " ") (insert ")") ;; untabify (let ((src (org-element-context))) (untabify (org-element-property :begin src) (org-element-property :end src))))
- reduce no longer exists, you have to use functools.reduce. Probably will not affect me much…
- Strings are sometimes bytes, and need to be encoded/decoded depending on what you are doing.
Otherwise, most things seemed to work fine. In a few places I had articles on Windows specific code, which I couldn't test on the Mac I work on now. Only one package was apparently not ready for Python3, and that was scimath from Enthought, which had units capability. Quantities worked fine though.
For some reason I could not do interactive key-presses in matplotlib. I am not sure if that is a Python3, or my Mac setup for now.
When I first wrote the Pycse book (in org-mode naturally), I must have had a default session setup for org-mode, because there was no :session information in a few of the src-block headers. I had to add these back in a few places.
Overall the transition was pretty seamless. It looks like I will be transitioning to Python3 in teaching this fall! Big thanks to the Anaconda team for packaging Python 3.5 and making it so easy to install!
Copyright (C) 2016 by John Kitchin. See the License for information about copying.
Org-mode version = 8.2.10
Generating an alphabetized list of collaborators from the past five years
Posted February 20, 2016 at 05:03 PM | categories: scopus, python | tags:
Updated February 20, 2016 at 05:24 PM
Almost every proposal I write requires some list of my coauthors from the past several years. Some want the list alphabetized, and some want affiliations too. It has always bothered me to make this list, mostly because it is tedious, and it seems like something that should not be hard to generate. It turns out it is not too hard. I have been developing a Python interface ((https://github.com/jkitchin/scopus )) to Scopus that more or less enables me to script this.
Scopus is not free. You need either a license, or institutional access to use it. Here is the strategy to generate my list of coauthors. First, we need to get the articles for the past 5 years that are mine, and for each paper we get the coauthors. I use my Scopus author id in the query, and then sort the names alphabetically into a table. Then, I use that table as input to a second code block that does an author query in Scopus to get the current affiliations. Here is the code.
from scopus.scopus_api import ScopusAbstract from scopus.scopus_search import ScopusSearch s = ScopusSearch('AU-ID(7004212771) AND PUBYEAR > 2010') coauthors = {} for eid in s.EIDS: ab = ScopusAbstract(eid) for au in ab.authors: if au.auid not in coauthors and au.auid != '7004212771': coauthors[au.auid] = au.indexed_name return sorted([[auid, name] for auid,name in coauthors.items()], key=lambda x:x[1])
| 52463103500 | Akhade S.A. |
| 6506329719 | Albenze E. |
| 36472906200 | Alesi W.R. |
| 56963752500 | Anna S.L. |
| 56522803500 | Boes J.R. |
| 26433085700 | Calle-Vallejo F. |
| 54973276000 | Chao R. |
| 7201800897 | Collins T.J. |
| 54883867200 | Curnan M.T. |
| 7003584159 | Damodaran K. |
| 55328415000 | Demeter E.L. |
| 37005464900 | Dsilva C. |
| 18037364800 | Egbebi A. |
| 35603120700 | Eslick J.C. |
| 56673468200 | Fan Q. |
| 24404182600 | Frenkel A.I. |
| 35514271900 | Gellman A.J. |
| 12803603300 | Gerdes K. |
| 54585146800 | Gumuslu G. |
| 55569145100 | Hallenbeck A.P. |
| 24316829300 | Hansen H.A. |
| 56009239000 | Hilburg S.L. |
| 55676869000 | Hopkinson D. |
| 56674328100 | Illes S.M. |
| 23479647900 | Inoglu N.G. |
| 6603398169 | Jaramillo T.F. |
| 8054222900 | Joshi Y.V. |
| 47962378000 | Keturakis C. |
| 57056061900 | Kondratyuk P. |
| 55391991800 | Kondratyuk P. |
| 7006205398 | Koper M.T.M. |
| 23004637900 | Kusuma V.A. |
| 35787409400 | Landon J. |
| 55005205100 | Lee A.S. |
| 6701399651 | Luebke D.R. |
| 35491189200 | Man I.C. |
| 27467500000 | Mantripragada H. |
| 55373026900 | Mao J.X. |
| 55210428500 | Marks A. |
| 27667815700 | Martinez J.I. |
| 56071079300 | Mehta P. |
| 56673592900 | Michael J.D. |
| 55772901000 | Miller D.C. |
| 7501599910 | Miller J.B. |
| 26032231600 | Miller S.D. |
| 35576929100 | Morreale B. |
| 55308251800 | Munprom R. |
| 14036290400 | Myers C.R. |
| 7007042214 | Norskov J.K. |
| 24081524800 | Nulwala H.B. |
| 56347288000 | Petrova R. |
| 7006208748 | Pushkarev V.V. |
| 56591664500 | Raman S. |
| 7004217247 | Resnik K.P. |
| 47962694800 | Richard Alesi Jr. W. |
| 9742604300 | Rossmeisl J. |
| 7201763336 | Rubin E.S. |
| 6602471339 | Sabolsky E.M. |
| 7004541416 | Salvador P.A. |
| 22981503200 | Shi W. |
| 55885836600 | Siefert N.S. |
| 25224517700 | Su H.-Y. |
| 57016792200 | Thirumalai H. |
| 8724572500 | Thompson R.L. |
| 8238710700 | Vasic R. |
| 37081979100 | Versteeg P. |
| 7006804734 | Wachs I.E. |
| 6701692232 | Washburn N.R. |
| 56542538800 | Watkins J.D. |
| 55569461200 | Xu Z. |
| 56424861600 | Yin C. |
| 56969809500 | Zhou X. |
It is worth inspecting this list for duplicates. I see at least two duplicates. That is a limitation of almost every indexing service I have seen. Names are hard to disambiguate. I will live with it. Now, we will use another query to get affiliations, and the names. Since we use a sorted list from above, these names are in alphabetical order. We exclude co-authors from Carnegie Mellon University since these are often my students, or colleagues, and they are obvious conflicts of interest for proposal reviewing anyway. I split the current affiliation on a comma, since it appears the institution comes first, followed by the department. We only need an institution here.
from scopus.scopus_author import ScopusAuthor coauthors = [ScopusAuthor(auid) for auid, name in data] print(', '.join(['{0} ({1})'.format(au.name, au.current_affiliation.split(',')[0]) for au in coauthors if au.current_affiliation.split(',')[0] != 'Carnegie Mellon University']))
Sneha A. Akhade (Pennsylvania State University), Erik J. Albenze (National Energy Technology Laboratory), Federico Calle-Vallejo (Leiden Institute of Chemistry), Robin Chao (National Energy Technology Laboratory), Krishnan V. Damodaran (University of Pittsburgh), Carmeline J. Dsilva (Princeton University), Adefemi A. Egbebi (URS), John C. Eslick (National Energy Technology Laboratory), Anatoly I. Frenkel (Yeshiva University), Kirk R. Gerdes (National Energy Technology Laboratory), Heine Anton Hansen (Danmarks Tekniske Universitet), David P. Hopkinson (National Energy Technology Laboratory), Thomas Francisco Jaramillo (Fermi National Accelerator Laboratory), Yogesh V. Joshi (Exxon Mobil Research and Engineering), Christopher J. Keturakis (Lehigh University), Marc T M Koper (Leiden Institute of Chemistry), Victor A. Kusuma (National Energy Technology Laboratory), James Landon (University of Kentucky), David R. Luebke (Liquid Ion Solutions), Isabelacostinela Man (Universitatea din Bucuresti), James X. Mao (University of Pittsburgh), José Ignacio Martínez (CSIC - Instituto de Ciencia de Materiales de Madrid (ICMM)), David C M Miller (National Energy Technology Laboratory), Bryan D. Morreale (National Energy Technology Laboratory), Christina R. Myers (National Energy Technology Laboratory), Jens Kehlet Nørskov (Stanford Linear Accelerator Center), Rumyana V. Petrova (International Iberian Nanotechnology Laboratory), Vladimir V. Pushkarev (Dow Corning Corporation), Sumathy Raman (Exxon Mobil Research and Engineering), Kevin P. Resnik (URS), Walter Richard Alesi (National Energy Technology Laboratory), Jan Rossmeisl (Kobenhavns Universitet), Edward M. Sabolsky (West Virginia University), Wei Shi (University of Pittsburgh), Nicholas S. Siefert (National Energy Technology Laboratory), Haiyan Su (Dalian Institute of Chemical Physics Chinese Academy of Sciences), Robert Lee Thompson (University of Pittsburgh Medical Center), Relja Vasić (SUNY College of Nanoscale Science and Engineering), Israel E. Wachs (Lehigh University), John D. Watkins (National Energy Technology Laboratory), Chunrong Yin (United States Department of Energy), Xu Zhou (Liquid Ion Solutions)
This is pretty sweet. I could pretty easily create a query that had all the PIs on a proposal, and alphabetize everyone's coauthors, or print them to a CSV file for import to Excel, or whatever format is required for conflict of interest reporting. The list is not perfect, but it is easy to manually fix it here.
That little bit of code is wrapped in a command-line utility in the scopus Python package. You use it like this. Just run it every time you need an updated list of coauthors! It isn't super flexible for now, e.g. excluding multiple affiliations, including multiple authors, etc… isn't fully supported.
./scopus_coauthors 7004212771 2010 --exclude-affiliation="Carnegie Mellon University"
Sneha A. Akhade (Pennsylvania State University), Erik J. Albenze (National Energy Technology Laboratory), Federico Calle-Vallejo (Leiden Institute of Chemistry), Robin Chao (National Energy Technology Laboratory), Krishnan V. Damodaran (University of Pittsburgh), Carmeline J. Dsilva (Princeton University), Adefemi A. Egbebi (URS), John C. Eslick (National Energy Technology Laboratory), Anatoly I. Frenkel (Yeshiva University), Kirk R. Gerdes (National Energy Technology Laboratory), Heine Anton Hansen (Danmarks Tekniske Universitet), David P. Hopkinson (National Energy Technology Laboratory), Thomas Francisco Jaramillo (Fermi National Accelerator Laboratory), Yogesh V. Joshi (Exxon Mobil Research and Engineering), Christopher J. Keturakis (Lehigh University), Marc T M Koper (Leiden Institute of Chemistry), Victor A. Kusuma (National Energy Technology Laboratory), James Landon (University of Kentucky), David R. Luebke (Liquid Ion Solutions), Isabelacostinela Man (Universitatea din Bucuresti), James X. Mao (University of Pittsburgh), José Ignacio Martínez (CSIC - Instituto de Ciencia de Materiales de Madrid (ICMM)), David C M Miller (National Energy Technology Laboratory), Bryan D. Morreale (National Energy Technology Laboratory), Christina R. Myers (National Energy Technology Laboratory), Jens Kehlet Nørskov (Stanford Linear Accelerator Center), Rumyana V. Petrova (International Iberian Nanotechnology Laboratory), Vladimir V. Pushkarev (Dow Corning Corporation), Sumathy Raman (Exxon Mobil Research and Engineering), Kevin P. Resnik (URS), Walter Richard Alesi (National Energy Technology Laboratory), Jan Rossmeisl (Kobenhavns Universitet), Edward M. Sabolsky (West Virginia University), Wei Shi (University of Pittsburgh), Nicholas S. Siefert (National Energy Technology Laboratory), Haiyan Su (Dalian Institute of Chemical Physics Chinese Academy of Sciences), Robert Lee Thompson (University of Pittsburgh Medical Center), Relja Vasić (SUNY College of Nanoscale Science and Engineering), Israel E. Wachs (Lehigh University), John D. Watkins (National Energy Technology Laboratory), Chunrong Yin (United States Department of Energy), Xu Zhou (Liquid Ion Solutions)
Copyright (C) 2016 by John Kitchin. See the License for information about copying.
Org-mode version = 8.2.10
Zhongnan Xu selected for the 2015-16 Dighe Fellowship in Chemical Engineering
Posted February 17, 2016 at 01:43 PM | categories: news | tags:
Congratulations Zhongnan!
Copyright (C) 2016 by John Kitchin. See the License for information about copying.
Org-mode version = 8.2.10
Zhongnan Xu successfully defended his PhD
Posted February 17, 2016 at 01:16 PM | categories: news | tags:
Updated February 17, 2016 at 01:42 PM
Zhongnan successfully defended his PhD dissertation yesterday!
Title: Towards Accurate Predictions and Mechanistic Understanding of the Catalytic Activity of Transition Metal Oxides.
Committee Members: Professor John Kitchin (chair), Professor Andrew Gellman, Professor Erik Ydstie, Professor Paul Salvador
Congratulations!
Copyright (C) 2016 by John Kitchin. See the License for information about copying.
Org-mode version = 8.2.10
« Previous Page -- Next Page »