{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "05f4293f-3bd2-4e58-8784-842b76b008f2",
   "metadata": {},
   "source": [
    "# Count/Concentration Assignment \n",
    "\n",
    "In this section, we will explain how to add counts or concentrations to species in order to simulate them. We start by defining some base species and giving them some characteristics and combining them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "02e7f510-74b9-4ee7-9f7f-24fa865764dc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Species\n",
      "Tree.big.blue.old,30\n",
      "Tree.big.blue.young,30\n",
      "Tree.big.red.old,30\n",
      "Tree.big.red.young,30\n",
      "Tree.small.blue.old,100\n",
      "Tree.small.blue.young,30\n",
      "Tree.small.red.old,30\n",
      "Tree.small.red.young,30\n",
      "\n",
      "Mappings\n",
      "Tree :\n",
      "Tree.big.blue.old\n",
      "Tree.big.blue.young\n",
      "Tree.big.red.old\n",
      "Tree.big.red.young\n",
      "Tree.small.blue.old\n",
      "Tree.small.blue.young\n",
      "Tree.small.red.old\n",
      "Tree.small.red.young\n",
      "\n",
      "Parameters\n",
      "volume,1\n",
      "\n",
      "Reactions\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Compiling model\n"
     ]
    }
   ],
   "source": [
    "from mobspy import *\n",
    "\n",
    "Age, Color, Size = BaseSpecies()\n",
    "\n",
    "Age.young, Age.old, \n",
    "Color.blue, Color.red, \n",
    "Size.small, Size.big\n",
    "\n",
    "Tree = Age*Color*Size\n",
    "\n",
    "model = set_counts({All[Tree]:30, 'Tree.blue.old':100})\n",
    "S = Simulation(Tree)\n",
    "print(S.compile())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40f16f4c-a849-4686-aa10-35478bf9b206",
   "metadata": {},
   "source": [
    "To assign counts to the Tree species we use the __call__ operator:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b1f36e7f-1d7a-401e-ad7e-d4027303e88c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'characteristics': 'std$', 'quantity': 100}]\n"
     ]
    }
   ],
   "source": [
    "Tree(100)\n",
    "print(Tree._species_counts)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9a48144-ede0-40e0-82d8-f54f3a6b9c40",
   "metadata": {},
   "source": [
    "Mobspy will assign this count to the default state of Tree. The default state is constructed by taking the first characteristics that was added to each of it's inheritors. So in this case the state that is assigned the count of 100 is the following:\n",
    "\n",
    "Tree.young.blue.small = 100\n",
    "\n",
    "To assign counts to different states one must perform a query:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "35b65938-1178-4b77-b019-1382c710fe09",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'characteristics': 'std$', 'quantity': 100}, {'characteristics': {'red', 'big'}, 'quantity': 150}]\n"
     ]
    }
   ],
   "source": [
    "Tree.red.big(150)\n",
    "print(Tree._species_counts)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cfd2e347-3677-4808-b55f-e67724aa110e",
   "metadata": {},
   "source": [
    "In this case all the coordinates in the meta-species vector space that have not been specified will be replaced by the default. The only missing coordinate missing refers to the Age meta-species for which the default value is young.\n",
    "Thus the count Tree.red.big(150) will be assigned to:\n",
    "\n",
    "Tree.young.red.big = 150"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b05068b-d75a-4968-8a89-f3130d8120d7",
   "metadata": {},
   "source": [
    "MobsPy also allows for the usage of a operator called All. This operator allows one to assing counts to all the characteristics of a meta-species at the same time."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5415163b-60e3-465d-8ed2-fe9dc3e6e72c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'characteristics': 'std$', 'quantity': 100}, {'characteristics': {'red', 'big'}, 'quantity': 150}, {'characteristics': {'all$'}, 'quantity': 10}]\n"
     ]
    }
   ],
   "source": [
    "All[Tree](10)\n",
    "print(Tree._species_counts)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "344c9bc1-747f-4219-9d26-3003af337324",
   "metadata": {},
   "source": [
    "The All operator assignment is not prioritazed when compared to specific assignments, so here the states Tree.young.red.big and Tree.young.red.big will retain the old counts which have been assigned to them individualy.\n",
    "\n",
    "Finally, one can also perform queries using the All assignment:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "643199fe-a82a-4ce5-91e3-0154fcfdce47",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'characteristics': 'std$', 'quantity': 100}, {'characteristics': {'red', 'big'}, 'quantity': 150}, {'characteristics': {'all$'}, 'quantity': 10}, {'characteristics': {'big', 'all$'}, 'quantity': 100}]\n"
     ]
    }
   ],
   "source": [
    "All[Tree.big](100)\n",
    "print(Tree._species_counts)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "679f13f1-55b7-4a22-bdea-200406804790",
   "metadata": {},
   "source": [
    "Now the compilation: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bd743b55-5c9f-4ea2-adc9-6db579135853",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Species\n",
      "Tree.big.blue.old,100\n",
      "Tree.big.blue.young,100\n",
      "Tree.big.red.old,100\n",
      "Tree.big.red.young,150\n",
      "Tree.small.blue.old,10\n",
      "Tree.small.blue.young,100\n",
      "Tree.small.red.old,10\n",
      "Tree.small.red.young,10\n",
      "\n",
      "Mappings\n",
      "Tree :\n",
      "Tree.big.blue.old\n",
      "Tree.big.blue.young\n",
      "Tree.big.red.old\n",
      "Tree.big.red.young\n",
      "Tree.small.blue.old\n",
      "Tree.small.blue.young\n",
      "Tree.small.red.old\n",
      "Tree.small.red.young\n",
      "\n",
      "Parameters\n",
      "volume,1\n",
      "\n",
      "Reactions\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Compiling model\n"
     ]
    }
   ],
   "source": [
    "S = Simulation(Tree)\n",
    "print(S.compile())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d038443-f8d2-4016-a6db-3bf10e01cc71",
   "metadata": {},
   "source": [
    "# Set count function\n",
    "\n",
    "Another way of adding counts to species is by using the set_count function. The set count function receives a argument a dictionary. The keys for this dictionary are either meta-species or strings and the items are values. The set_count function is compatible with the All operator. Also, the set count function returns a model with all the meta-species used in the keys, allowing for easy simulation after count assignment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e294a75b-2670-4f5a-93d0-7a9555fd27b4",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'Simulation' object is not callable",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-8-511b38f65f86>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmodel\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mset_counts\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m{\u001b[0m\u001b[0mAll\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mTree\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;36m30\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'Tree.blue.old'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;36m100\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      2\u001b[0m \u001b[0mS\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mSimulation\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmodel\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mS\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcompile\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/opt/anaconda3/lib/python3.8/site-packages/mobspy/modules/set_counts_module.py\u001b[0m in \u001b[0;36mset_counts\u001b[0;34m(count_dic)\u001b[0m\n\u001b[1;32m     49\u001b[0m     \u001b[0;32mfor\u001b[0m \u001b[0mkey\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mcount_dic\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     50\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 51\u001b[0;31m             \u001b[0mall_found_species\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfind_species\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     52\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     53\u001b[0m     \u001b[0mmodel\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mset\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/opt/anaconda3/lib/python3.8/site-packages/mobspy/modules/set_counts_module.py\u001b[0m in \u001b[0;36mfind_species\u001b[0;34m()\u001b[0m\n\u001b[1;32m     34\u001b[0m             \u001b[0;32mfor\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mitem\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mglobal_names\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mitems\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     35\u001b[0m                 \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 36\u001b[0;31m                     \u001b[0;32mif\u001b[0m \u001b[0mitem\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mis_species\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mitem\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0mtype\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     37\u001b[0m                         \u001b[0mfound_species\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0madd\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mitem\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     38\u001b[0m                 \u001b[0;32mexcept\u001b[0m \u001b[0mAttributeError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mTypeError\u001b[0m: 'Simulation' object is not callable"
     ]
    }
   ],
   "source": [
    "model = set_counts({All[Tree]:30, 'Tree.blue.old':100})\n",
    "S = Simulation(model)\n",
    "print(S.compile())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1d1c9049-402c-44ed-beb9-02c067b51d3d",
   "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.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
