<commit_before>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### PyEmma Featurizer Support"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import openpathsampling as paths\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#! lazy\n",
    "import pyemma.coordinates as coor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "28-07-16 08:59:31 openpathsampling.netcdfplus.netcdfplus INFO     Open existing netCDF file 'engine_store_test.nc' for reading - reading from existing file\n"
     ]
    }
   ],
   "source": [
    "#! lazy\n",
    "ref_storage = paths.Storage('engine_store_test.nc', mode='r')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "28-07-16 08:59:31 openpathsampling.netcdfplus.netcdfplus INFO     Create new netCDF file 'delete.nc' for writing - deleting existing file\n",
      "28-07-16 08:59:31 openpathsampling.netcdfplus.netcdfplus INFO     Setup netCDF file and create variables\n",
      "28-07-16 08:59:31 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'trajectories'\n",
      "28-07-16 08:59:31 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'topologies'\n",
      "28-07-16 08:59:31 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'cvs'\n",
      "28-07-16 08:59:31 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'snapshots'\n",
      "28-07-16 08:59:31 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'samples'\n",
      "28-07-16 08:59:31 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'samplesets'\n",
      "28-07-16 08:59:31 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'pathmovechanges'\n",
      "28-07-16 08:59:31 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'steps'\n",
      "28-07-16 08:59:31 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'details'\n",
      "28-07-16 08:59:31 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'pathmovers'\n",
      "28-07-16 08:59:31 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'shootingpointselectors'\n",
      "28-07-16 08:59:31 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'engines'\n",
      "28-07-16 08:59:31 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'pathsimulators'\n",
      "28-07-16 08:59:31 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'transitions'\n",
      "28-07-16 08:59:31 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'networks'\n",
      "28-07-16 08:59:31 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'schemes'\n",
      "28-07-16 08:59:31 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'volumes'\n",
      "28-07-16 08:59:31 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'ensembles'\n",
      "28-07-16 08:59:31 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'tag'\n",
      "28-07-16 08:59:31 openpathsampling.netcdfplus.netcdfplus INFO     Finished setting up netCDF file\n",
      "28-07-16 08:59:31 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'snapshot0'\n",
      "28-07-16 08:59:31 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'snapshot0statics'\n",
      "28-07-16 08:59:31 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'snapshot0kinetics'\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "UUID('8b0aba33-4832-11e6-88ff-000000000014')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#! lazy\n",
    "storage = paths.Storage('delete.nc', 'w')\n",
    "storage.trajectories.save(ref_storage.trajectories[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Import a PyEmma Coordinates Module"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using of pyemma featurizers or general other complex code requires a little trick to be storable. Since storing of code only works if we are not dependend on the context (scope) we need to wrap the construction of our featurizer in a function, that gets all it needs from the global scope as a parameter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def pyemma_generator(f):\n",
    "    f.add_inverse_distances(f.pairs(f.select_Backbone()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "cv = paths.collectivevariable.CV_PyEMMA_Featurizer(\n",
    "    'pyemma', \n",
    "    pyemma_generator, \n",
    "    topology=ref_storage.snapshots[0].topology\n",
    ").with_diskcache()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now use this featurizer generating function to build a collective variable out of it. All we need for that is a name as usual, the generating function, the list of parameters - here only the topology and at best a test snapshot, a template."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "cv(ref_storage.trajectories[0]);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's save it to the storage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(store.cvs[CollectiveVariable], 2, UUID('d4f16b4c-5490-11e6-891f-0000000000a8'))\n"
     ]
    }
   ],
   "source": [
    "#! lazy\n",
    "print storage.save(cv)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and apply the featurizer to a trajectory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "cv(storage.trajectories[0]);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Sync to make sure the cache is written to the netCDF file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "cv(storage.snapshots.all());"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "py_cv = storage.cvs['pyemma']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "store = storage.stores['cv%d' % storage.idx(py_cv)]\n",
    "nc_var = store.variables['value']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15\n"
     ]
    }
   ],
   "source": [
    "assert(nc_var.shape[1] == 15)\n",
    "print nc_var.shape[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "numpy.float32\n"
     ]
    }
   ],
   "source": [
    "assert(nc_var.var_type == 'numpy.float32')\n",
    "print nc_var.var_type"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ u'{\"_cls\": \"CV_PyEMMA_Featurizer\", \"_dict\": {\"topology\": {\"_obj\": \"topologies\", \"_uuid\": \"8b0aba33-4832-11e6-88ff-000000000002\"}, \"name\": \"pyemma\", \"featurizer\": {\"_marshal\": \"YwEAAAABAAAAAwAAAEMAAABzIAAAAHwAAGoAAHwAAGoBAHwAAGoCAIMAAIMBAIMBAAFkAABTKAEAAABOKAMAAAB0FQAAAGFkZF9pbnZlcnNlX2Rpc3RhbmNlc3QFAAAAcGFpcnN0DwAAAHNlbGVjdF9CYWNrYm9uZSgBAAAAdAEAAABmKAAAAAAoAAAAAHMeAAAAPGlweXRob24taW5wdXQtNS00Nzc0ZDhlZGRkMDA+dBAAAABweWVtbWFfZ2VuZXJhdG9yAQAAAHMCAAAAAAE=\", \"_module_vars\": [], \"_global_vars\": []}, \"kwargs\": {}}}']\n"
     ]
    }
   ],
   "source": [
    "#! ignore\n",
    "print storage.variables['cvs_json'][:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "py_cv_idx = storage.idx(py_cv)\n",
    "print py_cv_idx\n",
    "py_emma_feat = storage.vars['cvs_json'][py_cv_idx]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "erg = py_emma_feat(storage.snapshots);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2.68972969  2.06547379]\n",
      " [ 2.61396813  2.00944018]\n",
      " [ 2.73086929  2.06900215]\n",
      " [ 2.79462218  2.09850192]]\n"
     ]
    }
   ],
   "source": [
    "#! lazy\n",
    "print erg[0::5,2:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "storage.close()\n",
    "ref_storage.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "28-07-16 08:59:32 openpathsampling.netcdfplus.netcdfplus INFO     Open existing netCDF file 'delete.nc' for reading - reading from existing file\n"
     ]
    }
   ],
   "source": [
    "storage = paths.Storage('delete.nc', 'r')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "cv = storage.cvs[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 7.49063683,  4.10780048,  2.68972969,  2.06547379,  1.62878013,\n",
       "         6.90130949,  4.08113956,  2.72602296,  2.0472281 ,  6.57030106,\n",
       "         4.110569  ,  2.63028264,  7.49063683,  4.10780001,  6.90130711],\n",
       "       [ 7.49063683,  4.10780048,  2.68972969,  2.06547379,  1.62878013,\n",
       "         6.90130949,  4.08113956,  2.72602296,  2.0472281 ,  6.57030106,\n",
       "         4.110569  ,  2.63028264,  7.49063683,  4.10780001,  6.90130711],\n",
       "       [ 7.55804491,  4.04861212,  2.66780806,  2.04628849,  1.60359669,\n",
       "         6.81927395,  4.0386076 ,  2.70586848,  2.00875568,  6.44432783,\n",
       "         4.0713129 ,  2.58127213,  7.47397041,  3.9945395 ,  6.79377174],\n",
       "       [ 7.55804491,  4.04861212,  2.66780806,  2.04628849,  1.60359669,\n",
       "         6.81927395,  4.0386076 ,  2.70586848,  2.00875568,  6.44432783,\n",
       "         4.0713129 ,  2.58127213,  7.47397041,  3.9945395 ,  6.79377174],\n",
       "       [ 7.39766407,  3.91530514,  2.61396813,  2.00944018,  1.57548916,\n",
       "         6.78701973,  3.98001122,  2.67948079,  1.98068011,  6.38372326,\n",
       "         4.0377903 ,  2.55990911,  7.45296955,  3.9365232 ,  6.78809404],\n",
       "       [ 7.39766407,  3.91530514,  2.61396813,  2.00944018,  1.57548916,\n",
       "         6.78701973,  3.98001122,  2.67948079,  1.98068011,  6.38372326,\n",
       "         4.0377903 ,  2.55990911,  7.45296955,  3.9365232 ,  6.78809404],\n",
       "       [ 7.47759247,  3.91838408,  2.61284018,  2.00203228,  1.57443357,\n",
       "         6.78079987,  3.93411732,  2.64912009,  1.97010303,  6.35894108,\n",
       "         3.99012661,  2.54731131,  7.38262796,  3.9397471 ,  6.77619791],\n",
       "       [ 7.47759247,  3.91838408,  2.61284018,  2.00203228,  1.57443357,\n",
       "         6.78079987,  3.93411732,  2.64912009,  1.97010303,  6.35894108,\n",
       "         3.99012661,  2.54731131,  7.38262796,  3.9397471 ,  6.77619791],\n",
       "       [ 7.51974535,  4.00573206,  2.68295574,  2.04546571,  1.61202633,\n",
       "         6.86980486,  4.02839613,  2.70077038,  2.01711702,  6.45294619,\n",
       "         4.05020761,  2.58352399,  7.5034771 ,  4.03020859,  6.77858067],\n",
       "       [ 7.51974535,  4.00573206,  2.68295574,  2.04546571,  1.61202633,\n",
       "         6.86980486,  4.02839613,  2.70077038,  2.01711702,  6.45294619,\n",
       "         4.05020761,  2.58352399,  7.5034771 ,  4.03020859,  6.77858067],\n",
       "       [ 7.54831553,  4.00806856,  2.73086929,  2.06900215,  1.64360189,\n",
       "         6.84785843,  4.11174297,  2.73077893,  2.05752754,  6.50812197,\n",
       "         4.08443451,  2.61917949,  7.48407364,  4.1141634 ,  6.8487587 ],\n",
       "       [ 7.54831553,  4.00806856,  2.73086929,  2.06900215,  1.64360189,\n",
       "         6.84785843,  4.11174297,  2.73077893,  2.05752754,  6.50812197,\n",
       "         4.08443451,  2.61917949,  7.48407364,  4.1141634 ,  6.8487587 ],\n",
       "       [ 7.56239557,  4.0621357 ,  2.7974503 ,  2.09469628,  1.64985907,\n",
       "         6.78523636,  4.17631054,  2.73758578,  2.04132748,  6.53292179,\n",
       "         4.04364395,  2.57594609,  7.47516012,  3.98889875,  6.79680204],\n",
       "       [ 7.56239557,  4.0621357 ,  2.7974503 ,  2.09469628,  1.64985907,\n",
       "         6.78523636,  4.17631054,  2.73758578,  2.04132748,  6.53292179,\n",
       "         4.04364395,  2.57594609,  7.47516012,  3.98889875,  6.79680204],\n",
       "       [ 7.57628536,  4.07179117,  2.79462218,  2.09850192,  1.65384018,\n",
       "         6.92648983,  4.09565115,  2.7097106 ,  2.01867342,  6.45300055,\n",
       "         4.01589394,  2.56717825,  7.38926029,  3.98038411,  6.82837391],\n",
       "       [ 7.57628536,  4.07179117,  2.79462218,  2.09850192,  1.65384018,\n",
       "         6.92648983,  4.09565115,  2.7097106 ,  2.01867342,  6.45300055,\n",
       "         4.01589394,  2.56717825,  7.38926029,  3.98038411,  6.82837391],\n",
       "       [ 7.47465229,  4.00317764,  2.74107361,  2.08592939,  1.65441775,\n",
       "         6.78297091,  3.95277596,  2.67022729,  1.9990406 ,  6.43115568,\n",
       "         4.0501647 ,  2.58902216,  7.44663048,  4.03580093,  6.84330034],\n",
       "       [ 7.47465229,  4.00317764,  2.74107361,  2.08592939,  1.65441775,\n",
       "         6.78297091,  3.95277596,  2.67022729,  1.9990406 ,  6.43115568,\n",
       "         4.0501647 ,  2.58902216,  7.44663048,  4.03580093,  6.84330034],\n",
       "       [ 7.51655197,  4.00566053,  2.75102949,  2.09090209,  1.65996206,\n",
       "         6.76695871,  3.94014072,  2.66077065,  1.98808801,  6.42760515,\n",
       "         4.03192806,  2.57460976,  7.43470144,  3.99010825,  6.79955721],\n",
       "       [ 7.51655197,  4.00566053,  2.75102949,  2.09090209,  1.65996206,\n",
       "         6.76695871,  3.94014072,  2.66077065,  1.98808801,  6.42760515,\n",
       "         4.03192806,  2.57460976,  7.43470144,  3.99010825,  6.79955721]], dtype=float32)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cv(storage.snapshots)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Make sure that we get the same result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "assert np.allclose(erg, cv(storage.snapshots))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "storage.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {<commit_msg>update nb<commit_after>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### PyEmma Featurizer Support"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import openpathsampling as paths\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#! lazy\n",
    "import pyemma.coordinates as coor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "28-07-16 09:19:12 openpathsampling.netcdfplus.netcdfplus INFO     Open existing netCDF file 'engine_store_test.nc' for reading - reading from existing file\n"
     ]
    }
   ],
   "source": [
    "#! lazy\n",
    "ref_storage = paths.Storage('engine_store_test.nc', mode='r')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "28-07-16 09:19:12 openpathsampling.netcdfplus.netcdfplus INFO     Create new netCDF file 'delete.nc' for writing - deleting existing file\n",
      "28-07-16 09:19:12 openpathsampling.netcdfplus.netcdfplus INFO     Setup netCDF file and create variables\n",
      "28-07-16 09:19:12 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'trajectories'\n",
      "28-07-16 09:19:12 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'topologies'\n",
      "28-07-16 09:19:12 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'cvs'\n",
      "28-07-16 09:19:12 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'snapshots'\n",
      "28-07-16 09:19:12 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'samples'\n",
      "28-07-16 09:19:12 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'samplesets'\n",
      "28-07-16 09:19:12 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'pathmovechanges'\n",
      "28-07-16 09:19:12 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'steps'\n",
      "28-07-16 09:19:12 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'details'\n",
      "28-07-16 09:19:12 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'pathmovers'\n",
      "28-07-16 09:19:12 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'shootingpointselectors'\n",
      "28-07-16 09:19:12 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'engines'\n",
      "28-07-16 09:19:12 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'pathsimulators'\n",
      "28-07-16 09:19:12 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'transitions'\n",
      "28-07-16 09:19:12 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'networks'\n",
      "28-07-16 09:19:12 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'schemes'\n",
      "28-07-16 09:19:12 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'volumes'\n",
      "28-07-16 09:19:12 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'ensembles'\n",
      "28-07-16 09:19:12 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'tag'\n",
      "28-07-16 09:19:12 openpathsampling.netcdfplus.netcdfplus INFO     Finished setting up netCDF file\n",
      "28-07-16 09:19:12 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'snapshot0'\n",
      "28-07-16 09:19:12 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'snapshot0statics'\n",
      "28-07-16 09:19:12 openpathsampling.netcdfplus.netcdfplus INFO     Initializing store 'snapshot0kinetics'\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "UUID('8b0aba33-4832-11e6-88ff-000000000014')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#! lazy\n",
    "storage = paths.Storage('delete.nc', 'w')\n",
    "storage.trajectories.save(ref_storage.trajectories[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Import a PyEmma Coordinates Module"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using of pyemma featurizers or general other complex code requires a little trick to be storable. Since storing of code only works if we are not dependend on the context (scope) we need to wrap the construction of our featurizer in a function, that gets all it needs from the global scope as a parameter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def pyemma_generator(f):\n",
    "    f.add_inverse_distances(f.pairs(f.select_Backbone()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "cv = paths.collectivevariable.CV_PyEMMA_Featurizer(\n",
    "    'pyemma', \n",
    "    pyemma_generator, \n",
    "    topology=ref_storage.snapshots[0].topology\n",
    ").with_diskcache()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now use this featurizer generating function to build a collective variable out of it. All we need for that is a name as usual, the generating function, the list of parameters - here only the topology and at best a test snapshot, a template."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "cv(ref_storage.trajectories[0]);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's save it to the storage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(store.cvs[CollectiveVariable], 2, UUID('948dfc51-5493-11e6-92a4-0000000000a8'))\n"
     ]
    }
   ],
   "source": [
    "#! lazy\n",
    "print storage.save(cv)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and apply the featurizer to a trajectory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "cv(storage.trajectories[0]);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Sync to make sure the cache is written to the netCDF file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "cv(storage.snapshots.all());"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "py_cv = storage.cvs['pyemma']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "store = storage.stores['cv%d' % storage.idx(py_cv)]\n",
    "nc_var = store.variables['value']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15\n"
     ]
    }
   ],
   "source": [
    "assert(nc_var.shape[1] == 15)\n",
    "print nc_var.shape[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "numpy.float32\n"
     ]
    }
   ],
   "source": [
    "assert(nc_var.var_type == 'numpy.float32')\n",
    "print nc_var.var_type"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ u'{\"_cls\": \"CV_PyEMMA_Featurizer\", \"_dict\": {\"topology\": {\"_obj\": \"topologies\", \"_uuid\": \"8b0aba33-4832-11e6-88ff-000000000002\"}, \"name\": \"pyemma\", \"featurizer\": {\"_marshal\": \"YwEAAAABAAAAAwAAAEMAAABzIAAAAHwAAGoAAHwAAGoBAHwAAGoCAIMAAIMBAIMBAAFkAABTKAEAAABOKAMAAAB0FQAAAGFkZF9pbnZlcnNlX2Rpc3RhbmNlc3QFAAAAcGFpcnN0DwAAAHNlbGVjdF9CYWNrYm9uZSgBAAAAdAEAAABmKAAAAAAoAAAAAHMeAAAAPGlweXRob24taW5wdXQtNS00Nzc0ZDhlZGRkMDA+dBAAAABweWVtbWFfZ2VuZXJhdG9yAQAAAHMCAAAAAAE=\", \"_module_vars\": [], \"_global_vars\": []}, \"kwargs\": {}}}']\n"
     ]
    }
   ],
   "source": [
    "#! ignore\n",
    "print storage.variables['cvs_json'][:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "py_cv_idx = storage.idx(py_cv)\n",
    "print py_cv_idx\n",
    "py_emma_feat = storage.vars['cvs_json'][py_cv_idx]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "erg = py_emma_feat(storage.snapshots);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2.68972969  2.06547379]\n",
      " [ 2.61396813  2.00944018]\n",
      " [ 2.73086929  2.06900215]\n",
      " [ 2.79462218  2.09850192]]\n"
     ]
    }
   ],
   "source": [
    "#! lazy\n",
    "print erg[0::5,2:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "storage.close()\n",
    "ref_storage.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "28-07-16 09:19:13 openpathsampling.netcdfplus.netcdfplus INFO     Open existing netCDF file 'delete.nc' for reading - reading from existing file\n"
     ]
    }
   ],
   "source": [
    "#! lazy\n",
    "storage = paths.Storage('delete.nc', 'r')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "cv = storage.cvs[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Make sure that we get the same result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "assert np.allclose(erg, cv(storage.snapshots))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "storage.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
{'fraction_non_alphanumeric': 0.17602545968882602, 'fraction_numerical': 0.13348656294200847, 'mean_word_length': 2.5197261978842564, 'pattern_counts': {'":': 435, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 6, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4803983', 'n_tokens_mistral': 13512, 'n_tokens_neox': 11306, 'n_words': 1863}