{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datasets.Femnist import Femnist\n",
    "from graphs import SmallWorld\n",
    "from collections import defaultdict\n",
    "import os\n",
    "import json\n",
    "import numpy as np\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = FEMNIST\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b = SmallWorld(6, 2, 2, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b.adj_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(12):\n",
    "    print(b.neighbors(i))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "clients = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "num_samples = []\n",
    "data = defaultdict(lambda : None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "datadir = \"./leaf/data/femnist/data/train\"\n",
    "files = os.listdir(datadir)\n",
    "total_users=0\n",
    "users = set()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "files = os.listdir(datadir)[0:1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for f in files:\n",
    "    file_path = os.path.join(datadir, f)\n",
    "    print(file_path)\n",
    "    with open(file_path, 'r') as inf:\n",
    "        client_data = json.load(inf)\n",
    "    current_users = len(client_data['users'])\n",
    "    print(\"Current_Users: \", current_users)\n",
    "    total_users += current_users\n",
    "    users.update(client_data['users'])\n",
    "\n",
    "print(\"total_users: \", total_users)\n",
    "print(\"total_users: \", len(users))\n",
    "print(client_data['user_data'].keys())\n",
    "print(np.array(client_data['user_data']['f3408_47']['x']).shape)\n",
    "print(np.array(client_data['user_data']['f3408_47']['y']).shape)\n",
    "print(np.array(client_data['user_data']['f3327_11']['x']).shape)\n",
    "print(np.array(client_data['user_data']['f3327_11']['y']).shape)\n",
    "print(np.unique(np.array(client_data['user_data']['f3327_11']['y'])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "file = 'run.py'\n",
    "with open(file, 'r') as inf:\n",
    "            print(inf.readline().strip())\n",
    "            print(inf.readlines())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(l):\n",
    "    l[2] = 'c'\n",
    "\n",
    "a = ['a', 'a', 'a']\n",
    "print(a)\n",
    "f(a)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "l = ['a', 'b', 'c']\n",
    "print(l[:-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from localconfig import LocalConfig\n",
    "\n",
    "def read_ini(file_path):\n",
    "    config = LocalConfig(file_path)\n",
    "    for section in config:\n",
    "        print(\"Section: \", section)\n",
    "        for key, value in config.items(section):\n",
    "            print((key, value))\n",
    "    print(dict(config.items('DATASET')))\n",
    "    return config\n",
    " \n",
    "config = read_ini(\"config.ini\")\n",
    "for section in config:\n",
    "    print(section)\n",
    "#d = dict(config.sections())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def func(a = 1, b = 2, c = 3):\n",
    "    print(a + b + c)\n",
    "\n",
    "l = [3, 5, 7]\n",
    "\n",
    "func(*l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from torch import multiprocessing as mp\n",
    "\n",
    "mp.spawn(fn = func, nprocs = 2, args = [], kwargs = {'a': 4, 'b': 5, 'c': 6})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "l = '[0.4, 0.2, 0.3, 0.1]'\n",
    "type(eval(l))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from decentralizepy.datasets.Femnist import Femnist\n",
    "f1 = Femnist(0, 1, 'leaf/data/femnist/data/train')\n",
    "ts = f1.get_trainset(1)\n",
    "for data, target in ts:\n",
    "    print(data)\n",
    "    break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from decentralizepy.datasets.Femnist import Femnist\n",
    "from decentralizepy.graphs.SmallWorld import SmallWorld\n",
    "from decentralizepy.mappings.Linear import Linear\n",
    "\n",
    "f = Femnist(2, 'leaf/data/femnist/data/train', sizes=[0.6, 0.4])\n",
    "g = SmallWorld(4, 1, 0.5)\n",
    "l = Linear(2, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from decentralizepy.node.Node import Node\n",
    "from torch import multiprocessing as mp\n",
    "import logging\n",
    "n1 = Node(0, l, g, f, \"./results\", logging.DEBUG)\n",
    "n2 = Node(1, l, g, f, \"./results\", logging.DEBUG)\n",
    "# mp.spawn(fn = Node, nprocs = 2, args=[l,g,f])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from testing import f"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from torch import multiprocessing as mp\n",
    "import torch\n",
    "m1 = torch.nn.Linear(1,1)\n",
    "o1 = torch.optim.SGD(m1.parameters(), 0.6)\n",
    "print(m1)\n",
    "\n",
    "\n",
    "mp.spawn(fn = f, nprocs = 2, args=[m1, o1])\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "o1.param_groups"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with torch.no_grad():\n",
    "    o1.param_groups[0][\"params\"][0].copy_(torch.zeros(1,))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "o1.param_groups"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "m1.state_dict()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "loss = getattr(torch.nn.functional, 'nll_loss')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "loss"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Section:  GRAPH\n",
      "('package', 'decentralizepy.graphs.SmallWorld')\n",
      "('graph_class', 'SmallWorld')\n",
      "Section:  DATASET\n",
      "('dataset_package', 'decentralizepy.datasets.Femnist')\n",
      "('dataset_class', 'Femnist')\n",
      "('model_class', 'CNN')\n",
      "('n_procs', 36)\n",
      "('train_dir', 'leaf/data/femnist/per_user_data/train')\n",
      "('test_dir', 'leaf/data/femnist/data/test')\n",
      "('sizes', '')\n",
      "Section:  OPTIMIZER_PARAMS\n",
      "('optimizer_package', 'torch.optim')\n",
      "('optimizer_class', 'Adam')\n",
      "('lr', 0.01)\n",
      "Section:  TRAIN_PARAMS\n",
      "('training_package', 'decentralizepy.training.Training')\n",
      "('training_class', 'Training')\n",
      "('epochs_per_round', 1)\n",
      "('batch_size', 1024)\n",
      "('shuffle', True)\n",
      "('loss_package', 'torch.nn')\n",
      "('loss_class', 'CrossEntropyLoss')\n",
      "Section:  COMMUNICATION\n",
      "('comm_package', 'decentralizepy.communication.TCP')\n",
      "('comm_class', 'TCP')\n",
      "('addresses_filepath', 'ip_addr.json')\n",
      "Section:  SHARING\n",
      "('sharing_package', 'decentralizepy.sharing.Sharing')\n",
      "('sharing_class', 'Sharing')\n",
      "{'dataset_package': 'decentralizepy.datasets.Femnist', 'dataset_class': 'Femnist', 'model_class': 'CNN', 'n_procs': 36, 'train_dir': 'leaf/data/femnist/per_user_data/train', 'test_dir': 'leaf/data/femnist/data/test', 'sizes': ''}\n"
     ]
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "from decentralizepy.node.Node import Node\n",
    "from decentralizepy.graphs.SmallWorld import SmallWorld\n",
    "from decentralizepy.graphs.Graph import Graph\n",
    "from decentralizepy.mappings.Linear import Linear\n",
    "from torch import multiprocessing as mp\n",
    "import torch\n",
    "import logging\n",
    "\n",
    "from localconfig import LocalConfig\n",
    "\n",
    "def read_ini(file_path):\n",
    "    config = LocalConfig(file_path)\n",
    "    for section in config:\n",
    "        print(\"Section: \", section)\n",
    "        for key, value in config.items(section):\n",
    "            print((key, value))\n",
    "    print(dict(config.items('DATASET')))\n",
    "    return config\n",
    " \n",
    "config = read_ini(\"config.ini\")\n",
    "my_config = dict()\n",
    "for section in config:\n",
    "    my_config[section] = dict(config.items(section))\n",
    "\n",
    "#f = Femnist(2, 'leaf/data/femnist/data/train', sizes=[0.6, 0.4])\n",
    "g = Graph()\n",
    "g.read_graph_from_file(\"36_nodes.edges\", \"edges\")\n",
    "l = Linear(1, 36)\n",
    "\n",
    "#Node(0, 0, l, g, my_config, 20, \"results\", logging.DEBUG)\n",
    "\n",
    "mp.spawn(fn = Node, nprocs = g.n_procs, args=[0,l,g,my_config,20,\"results\",logging.INFO])\n",
    "\n",
    "# mp.spawn(fn = Node, args = [l, g, config, 10, \"results\", logging.DEBUG], nprocs=2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from decentralizepy.mappings.Linear import Linear\n",
    "from testing import f\n",
    "from torch import multiprocessing as mp\n",
    "\n",
    "l = Linear(1, 2)\n",
    "mp.spawn(fn = f, nprocs = 2, args = [0, 2, \"ip_addr.json\", l])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from decentralizepy.datasets.Femnist import Femnist\n",
    "\n",
    "f = Femnist()\n",
    "\n",
    "f.file_per_user('leaf/data/femnist/data/train','leaf/data/femnist/per_user_data/train')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = set()\n",
    "a.update([2, 3, 4, 5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{2, 3, 4, 5}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 3 4 5\n"
     ]
    }
   ],
   "source": [
    "print(*a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from decentralizepy.graphs.SmallWorld import SmallWorld\n",
    "\n",
    "s = SmallWorld(36, 2, .5)\n",
    "s.write_graph_to_file('36_nodes.edges')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['/home/risharma/miniconda3/envs/decpy/lib/python3.9/site-packages/ipykernel_launcher.py',\n",
       " '--ip=127.0.0.1',\n",
       " '--stdin=9008',\n",
       " '--control=9006',\n",
       " '--hb=9005',\n",
       " '--Session.signature_scheme=\"hmac-sha256\"',\n",
       " '--Session.key=b\"eac5d2f8-c460-45f1-a268-1e4b46a6efd6\"',\n",
       " '--shell=9007',\n",
       " '--transport=\"tcp\"',\n",
       " '--iopub=9009',\n",
       " '--f=/tmp/tmp-21212479paJaUBJBN84.json']"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import sys\n",
    "sys.argv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "996934296aa9d79be6c3d800a38d8fdb7dfa8fe7bb07df178f1397cde2cb8742"
  },
  "kernelspec": {
   "display_name": "Python 3.9.7 64-bit ('tff': conda)",
   "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.9.7"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}