{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\npynet dataset helpers overview\n==============================\n\nCredit: A Grigis\n\npynet is a Python package related to deep learning and its application in\nMRI mediacal data analysis. It is accessible to everybody, and is reusable\nin various contexts. The project is hosted on github:\nhttps://github.com/neurospin/pynet.\n\nFirst checks\n------------\n\nIn order to test if the 'pynet' package is installed on your machine, you can\ncheck the package version.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import pynet\nprint(pynet.__version__)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now you can run the the configuration info function to see if all the\ndependencies are installed properly.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import pynet.configure\nprint(pynet.configure.info())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Import a pynet dataset\n----------------------\n\nUse a fetcher to retrieve some data and use generic interface to import and\nsplit this dataset: train, test and validation.\nYou may need to change the 'datasetdir' parameter.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from pynet.datasets import DataManager, fetch_cifar\n\ndata = fetch_cifar(datasetdir=\"/tmp/cifar\")\nmanager = DataManager(\n    input_path=data.input_path,\n    labels=[\"label\"],\n    metadata_path=data.metadata_path,\n    number_of_folds=10,\n    batch_size=50,\n    stratify_label=\"category\",\n    test_size=0.1)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We have now a test, and multiple folds with train-validation datasets that\ncan be used to train our network using cross-validation.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nfrom pynet.plotting import plot_data\n\nprint(\"Nb folds: \", manager.number_of_folds)\ndataloader = manager.get_dataloader(\n    train=True,\n    validation=False,\n    test=False,\n    fold_index=0)\nprint(dataloader)\nfor trainloader in dataloader.train:\n    print(\"Inputs: \", trainloader.inputs.shape)\n    print(\"Outputs: \", trainloader.outputs)\n    print(\"Labels: \", trainloader.labels.shape)\n    plot_data(trainloader.inputs, nb_samples=5)\n    break\n\nimport os\nif \"CI_MODE\" not in os.environ:\n    import matplotlib.pyplot as plt\n    plt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.12"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}