Skip to content
Snippets Groups Projects
python-data-project-networks-sol.ipynb 206 KiB
Newer Older
   "source": [
    "bar(linspace(0, 10, len(ws_triads.values())), ws_triads.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cens = nx.triadic_census(G)\n",
    "ab_triads = {'003': 0,\n",
    "             '012': 0,\n",
    "             '021C': 0,\n",
    "             '021D': 0,\n",
    "             '021U': 0,\n",
    "             '030C': 0,\n",
    "             '030T': 0,\n",
    "             '102': 0,\n",
    "             '111D': 0,\n",
    "             '111U': 0,\n",
    "             '120C': 0,\n",
    "             '120D': 0,\n",
    "             '120U': 0,\n",
    "             '201': 0,\n",
    "             '210': 0,\n",
    "             '300': 0}\n",
    "n = 0\n",
    "for i in range(0, 10):\n",
    "    AB = nx.barabasi_albert_graph(len(G), 51).to_directed()\n",
    "    tr = nx.triadic_census(AB)\n",
    "    for t in tr.keys():\n",
    "        ab_triads[t] += tr[t]\n",
    "    n += 1\n",
    "    print(\"Completed %d\" % n)\n",
    "    \n",
    "for t in ab_triads:\n",
    "    m = ab_triads[t]/10\n",
    "    ab_triads[t] = m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bar(linspace(0, 10, len(ab_triads.values())), ab_triads.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bar(linspace(0, 10, len(cens.values())), cens.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cens2 = nx.triadic_census(D)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bar(linspace(0, 10, len(cens2.values())), cens2.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "nx.Graph.to_directed()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bar(linspace(0, 10, len(cens.values())), cens.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cens"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "nx.draw(nx.triad_graph('003'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "depts = {}\n",
    "for k in truth_dict.keys():\n",
    "    if truth_dict[k] in depts.keys():\n",
    "        curr = depts[truth_dict[k]]\n",
    "        curr.append(k)\n",
    "        depts[truth_dict[k]] = curr\n",
    "    else:\n",
    "        depts[truth_dict[k]] = [k]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "comms2 = list(nx.algorithms.community.asyn_fluid.asyn_fluidc(G.to_undirected(), 42))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "for k in depts.keys():\n",
    "    maps = set([])\n",
    "    for e in depts[k]:\n",
    "        for i in range(len(comms2)):\n",
    "            if e in comms2[i]:\n",
    "                maps.add(i)\n",
    "    if len(maps) == 1:\n",
    "        print(\"Dept. %s is preserved!\" % k)\n",
    " #   print(\"Dept. %s maps to communities: %s\" %(k, str(maps)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for k in depts.keys():\n",
    "    maps = set([])\n",
    "    for e in depts[k]:\n",
    "        for i in range(len(comms)):\n",
    "            if e in comms[i]:\n",
    "                maps.add(i)\n",
    "    if len(maps) == 1:\n",
    "        print(\"Dept. %s is preserved!\" % k)\n",
    "  #  print(\"Dept. %s maps to communities: %s\" %(k, str(maps)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "type(type)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}