Skip to content

Instantly share code, notes, and snippets.

@kain88-de
Last active December 22, 2015 09:24
Show Gist options
  • Save kain88-de/7dfcb53b9831411fd3d2 to your computer and use it in GitHub Desktop.
Save kain88-de/7dfcb53b9831411fd3d2 to your computer and use it in GitHub Desktop.
datreant-view
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import datreant\n",
"import numpy as np\n",
"from os.path import split, join"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This Notebook should serve as a testing ground for a new static treant view. First we create a dummy treant with a struture that resembles what I usually end up with during my research"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"obs = datreant.Treant('test-obs')\n",
"data = np.ones((40, 40))\n",
"\n",
"# First simulation runs\n",
"obs.data.add('sim-setup-1/var-value-1/energy', data)\n",
"obs.data.add('sim-setup-1/var-value-1/rmsd', data)\n",
"\n",
"obs.data.add('sim-setup-1/var-value-2/energy', data)\n",
"obs.data.add('sim-setup-1/var-value-2/rmsd', data)\n",
"\n",
"obs.data.add('sim-setup-1/var-value-3/energy', data)\n",
"obs.data.add('sim-setup-1/var-value-3/rmsd', data)\n",
"\n",
"obs.data.add('sim-setup-1/var-value-10/energy', data)\n",
"obs.data.add('sim-setup-1/var-value-10/rmsd', data)\n",
"\n",
"# second simulation runs\n",
"obs.data.add('sim-setup-2/var-value-1/energy', data)\n",
"obs.data.add('sim-setup-2/var-value-1/rmsd', data)\n",
"\n",
"obs.data.add('sim-setup-2/var-value-2/energy', data)\n",
"obs.data.add('sim-setup-2/var-value-2/rmsd', data)\n",
"\n",
"obs.data.add('sim-setup-2/var-value-3/energy', data)\n",
"obs.data.add('sim-setup-2/var-value-3/rmsd', data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's write a static treant view. The view is able to get specific leaves of the treant and additionally can also\n",
"be constrained to a subset of the leave with a filter"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"class TreantView(object):\n",
" def __init__(self, treant, head, tail=None, include=None, exclude=None,\n",
" sort_func=None):\n",
" self._treant = treant\n",
" self._head = head\n",
" self._tail = None\n",
" self._get_keys()\n",
" \n",
" if include is not None:\n",
" self.filter(include, exclude)\n",
" \n",
" self._tail = tail\n",
" if tail is not None:\n",
" self.use_tail(tail)\n",
" \n",
" if sort_func is not None:\n",
" self.sort(sort_func)\n",
" \n",
" def _get_keys(self):\n",
" \"\"\"get keys of treant leave\"\"\"\n",
" keys = self._treant.data.keys()\n",
" keys = [k.replace(self._head + '/', '') for k in keys if self._head in k]\n",
" if self._tail is not None:\n",
" keys = [k.replace('/' + self._tail, '/') for k in keys]\n",
" self._keys = keys\n",
" \n",
" def use_tail(self, tail):\n",
" \"\"\"use a common tail for all keys and blend that out in the selection process\"\"\"\n",
" self._tail = tail\n",
" self._keys = [k.replace('/' + tail, '') for k in self._keys]\n",
" \n",
" def sort(self, sort_func):\n",
" \"\"\"Use sort_func to convert elements to a data-type according to which\n",
" the keys will get sorted\n",
" \"\"\"\n",
" sort_idx = np.argsort([sort_func(k) for k in self._keys])\n",
" self._keys = list(np.array(self._keys)[sort_idx])\n",
" \n",
" def filter(self, include, exclude=None):\n",
" \"\"\"filter to subset of leave only\"\"\"\n",
" if exclude is None:\n",
" exclude = []\n",
" keys = [k for k in self._keys if \\\n",
" (np.all([w in k for w in include]) and\n",
" (np.all([w not in k for w in exclude])))]\n",
" self._keys = keys\n",
" \n",
" def __getitem__(self, item):\n",
" if type(item) == int:\n",
" return self._treant[self._keys[item]]\n",
" elif type(item) == str:\n",
" return self._treant[join_paths(self._head, item, self._tail)]\n",
" else:\n",
" raise NotImplementedError('can only handle \"int\" and \"str\" objects')\n",
" \n",
" @property\n",
" def keys(self):\n",
" return self._keys\n",
" \n",
" @property\n",
" def treant(self):\n",
" return self._treant\n",
" \n",
" @property\n",
" def head(self):\n",
" return self._head\n",
" \n",
"def join_paths(head, middle, tail=None):\n",
" if tail:\n",
" return join(head, middle, tail)\n",
" else:\n",
" return join(head, middle)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Let's have some fun with the view"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Lets begin by only accessing one leave."
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"['var-value-1/energy',\n",
" 'var-value-1/rmsd',\n",
" 'var-value-10/energy',\n",
" 'var-value-10/rmsd',\n",
" 'var-value-2/energy',\n",
" 'var-value-2/rmsd',\n",
" 'var-value-3/energy',\n",
" 'var-value-3/rmsd']"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"leave = TreantView(obs, 'sim-setup-1')\n",
"leave.keys"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(40, 40)"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"leave['var-value-1/rmsd'].shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now I only want to access the energy entries."
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"['var-value-1/energy',\n",
" 'var-value-10/energy',\n",
" 'var-value-2/energy',\n",
" 'var-value-3/energy']"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"leave.filter(include='energy')\n",
"leave.keys"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Hm that is already quite good. But now they all have the same ending. I don't really want to type 'energy' everytime\n",
"I access one of these fields now."
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['var-value-1', 'var-value-10', 'var-value-2', 'var-value-3']\n",
"(40, 40)\n"
]
}
],
"source": [
"leave.use_tail('energy')\n",
"print(leave.keys)\n",
"print(leave['var-value-1'].shape)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We are already pretty good right now. But the order is still a bit of. Wouldn't it be nice if we can change that as well?"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['var-value-1', 'var-value-2', 'var-value-3', 'var-value-10']\n"
]
}
],
"source": [
"leave.sort(lambda x: int(x[10:]))\n",
"print(leave.keys)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And now all together"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"tv = TreantView(obs, 'sim-setup-1', include=['energy'], tail='energy', sort_func=lambda x: int(x[10:]))"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"['var-value-1', 'var-value-2', 'var-value-3', 'var-value-10']"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tv.keys"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"# What happens if we add a new analysis?"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"obs.data.add('sim-setup-1/energy_hist', data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We just added a new entry in the datreant where we save an energy histogram calculated from all 'sim-setup-1' simulations. Let's try to create a TreantView like before again.\n",
"\n",
"**Note**\n",
"You have to create a new View. The views are static and NOT updated with the underlying treant object!!"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "ValueError",
"evalue": "invalid literal for int() with base 10: 't'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-44-bcb22c20ed50>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mtv\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mTreantView\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mobs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'sim-setup-1'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0minclude\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'energy'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtail\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'energy'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msort_func\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mlambda\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;32m<ipython-input-9-8e1a6d3ff422>\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, treant, head, tail, include, exclude, sort_func)\u001b[0m\n\u001b[0;32m 15\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 16\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0msort_func\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 17\u001b[1;33m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msort\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msort_func\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 18\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 19\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0m_get_keys\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m<ipython-input-9-8e1a6d3ff422>\u001b[0m in \u001b[0;36msort\u001b[1;34m(self, sort_func)\u001b[0m\n\u001b[0;32m 29\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 30\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0msort\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msort_func\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 31\u001b[1;33m \u001b[0msort_idx\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0margsort\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0msort_func\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mk\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mk\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_keys\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 32\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_keys\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlist\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0marray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_keys\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0msort_idx\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 33\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m<ipython-input-44-bcb22c20ed50>\u001b[0m in \u001b[0;36m<lambda>\u001b[1;34m(x)\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mtv\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mTreantView\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mobs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'sim-setup-1'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0minclude\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'energy'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtail\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'energy'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msort_func\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mlambda\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: 't'"
]
}
],
"source": [
"tv = TreantView(obs, 'sim-setup-1', include=['energy'], tail='energy', sort_func=lambda x: int(x[10:]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This resulted in an error. It looks like we can't use the sorting anymore right now. "
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"tv = TreantView(obs, 'sim-setup-1', include=['energy'])"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"['energy_hist',\n",
" 'var-value-1/energy',\n",
" 'var-value-10/energy',\n",
" 'var-value-2/energy',\n",
" 'var-value-3/energy']"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tv.keys"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Ah there is the problem. We now have also included the 'energy_hist' analysis. Let's exclude that one"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['var-value-1/energy', 'var-value-10/energy', 'var-value-2/energy', 'var-value-3/energy']\n"
]
}
],
"source": [
"tv.filter(include=['v'], exclude=['hist'])\n",
"print(tv.keys)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we have the same values as before. Ok lets create a quick view a gain that excludes the histogram"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"tv = TreantView(obs, 'sim-setup-1', include=['energy'], exclude=['hist'],\n",
" tail='energy', sort_func=lambda x: int(x[10:]))"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"['var-value-1', 'var-value-2', 'var-value-3', 'var-value-10']"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tv.keys"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment