<commit_before><commit_msg>Prototype state_monitor (a troy) and status_manager (manage many troys)<commit_after>{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-02-03T17:21:10.707175Z",
     "start_time": "2019-02-03T17:21:10.276455Z"
    },
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Populating the interactive namespace from numpy and matplotlib\n"
     ]
    }
   ],
   "source": [
    "%pylab inline\n",
    "%gui qt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-02-03T17:21:17.989901Z",
     "start_time": "2019-02-03T17:21:17.724379Z"
    }
   },
   "outputs": [],
   "source": [
    "from spiketag.utils import EventEmitter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-02-03T17:21:22.116403Z",
     "start_time": "2019-02-03T17:21:22.102265Z"
    }
   },
   "outputs": [],
   "source": [
    "class state_monitor(EventEmitter):\n",
    "    def __init__(self, _id):\n",
    "        super(state_monitor, self).__init__()\n",
    "        self._id = _id\n",
    "        self.s = ['IDLE', 'BUSY', 'READY', 'DONE']\n",
    "        self._state = self.s[0]\n",
    "        \n",
    "        \n",
    "    @property\n",
    "    def state(self):\n",
    "        return self._state\n",
    "\n",
    "    @state.setter\n",
    "    def state(self, state):\n",
    "        if state in self.s:\n",
    "            self._state = state\n",
    "            self.emit('report', state=self._state)\n",
    "        else:\n",
    "            print('state has to be one of', self.s)\n",
    "            \n",
    "    \n",
    "    def set_busy(self):\n",
    "        self.state = self.s[1]\n",
    "        \n",
    "    def set_ready(self):\n",
    "        self.state = self.s[2]\n",
    "        \n",
    "    def set_done(self):\n",
    "        self.state = self.s[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-02-03T17:21:25.921971Z",
     "start_time": "2019-02-03T17:21:25.907263Z"
    }
   },
   "outputs": [],
   "source": [
    "class status_manager(EventEmitter):\n",
    "    def __init__(self):\n",
    "        super(status_manager, self).__init__()\n",
    "        self.reporters = {}\n",
    "    \n",
    "    def append(self, state_reporter):\n",
    "        self.reporters[state_reporter._id] = state_reporter\n",
    "        \n",
    "        @state_reporter.connect\n",
    "        def on_report(state):\n",
    "#             print(state+' from group '+ str(state_reporter._id))\n",
    "              self.emit('update', state=self.state_list)\n",
    "            \n",
    "    def __getitem__(self, i):\n",
    "        return self.reporters[i]\n",
    "    \n",
    "    def __setitem__(self, i, state_str):\n",
    "        self.reporters[i].state = state_str\n",
    "    \n",
    "    def __repr__(self):\n",
    "        _str = ''\n",
    "        for _, _reporter in self.reporters.items():\n",
    "            _str = _str + str(_reporter._id) + \":\" + str(_reporter.state) + '\\n'\n",
    "        return _str\n",
    "    \n",
    "    def reset(self):\n",
    "        for reports in self.reporters.values():\n",
    "            reports._state = 'IDLE'\n",
    "        self.emit('update', state=self.state_list)\n",
    "    \n",
    "    @property\n",
    "    def state_list(self):\n",
    "        return [troy.s.index(troy.state) for troy in self.reporters.values()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-02-03T17:22:29.457334Z",
     "start_time": "2019-02-03T17:22:29.428789Z"
    }
   },
   "outputs": [],
   "source": [
    "from spiketag.view import cluster_view"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "cluview = cluster_view()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = status_manager()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "troy = []\n",
    "for i in range(10):\n",
    "    troy.append(state_monitor(i))\n",
    "for i in range(10):\n",
    "    s.append(state_reporter = troy[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "@s.connect\n",
    "def on_update(state):\n",
    "    print(state)\n",
    "    cluview.set_data(10, nclu_list=10*[5], \n",
    "                     sorting_status=np.array(state), selected_group_id=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# disconnect if `update` function has something wrong\n",
    "# s._callbacks.pop('update')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "cluview.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cluview.set_data(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "id(troy[2]) == id(s[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 0, 1, 0, 0, 0, 0, 0, 0, 0]\n",
      "[0, 0, 1, 0, 0, 0, 0, 0, 2, 0]\n"
     ]
    }
   ],
   "source": [
    "troy[2].state = 'BUSY'\n",
    "s[8] = 'READY'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n"
     ]
    }
   ],
   "source": [
    "s.reset()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.state_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "cluview.set_data(10, nclu_list=10*[5], \n",
    "                 sorting_status=np.array([1]*10), selected_group_id=1)"
   ]
  }
 ],
 "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.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
{'fraction_non_alphanumeric': 0.2153825431034483, 'fraction_numerical': 0.0408135775862069, 'mean_word_length': 2.0158407798537774, 'pattern_counts': {'":': 143, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '3823338', 'n_tokens_mistral': 3174, 'n_tokens_neox': 2953, 'n_words': 468}