{
 "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": "90de3aa9-8706-46c6-a937-35ba7afe658e",
   "metadata": {},
   "outputs": [],
   "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"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40f16f4c-a849-4686-aa10-35478bf9b206",
   "metadata": {},
   "source": [
    "To assign initial amounts to the Tree species, we use the \\textbf{call} operator (parenthesis with a value applied to a meta-species). \n",
    "The value inside the operator can be either a float or an integer, and it represents the initial amount of a state of a meta-species in the system. Furthermore, if the simulation volume was not changed, then the amount is equivalent to the concentration. \n",
    "\n",
    "Below, we use the \\textbf{call} operator in the Tree meta-species, assigning the count of 100 to the default state of Tree. The default state is constructed by taking the first characteristics that were added to each of its 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",
    "The code follows:"
   ]
  },
  {
   "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": [
    "Now, to assign counts to different states other than the default one, one can perform a query using the dot operator. One can query as many characteristic spaces as possible, and the ones not queried are replaced by the default state for the respective predecessor meta-species. \n",
    "\n",
    "As an example, below assign the count of 150 to the Tree.red.big.young state of the Tree meta-species. The state red from the Color predecessor and big from the Size predecessor are both specified in the query. However, the young is not specified and comes as the default state of the Age meta-species. The code follows:"
   ]
  },
  {
   "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": "1b05068b-d75a-4968-8a89-f3130d8120d7",
   "metadata": {},
   "source": [
    "MobsPy also allows for the usage of an operator called All. This operator allows one to assign 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 prioritized when compared to specific assignments, so here, the states Tree.young.red.big and Tree.young.red.big will retain the old counts that have been assigned to them individually.\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': {'all$', 'big'}, '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": "stderr",
     "output_type": "stream",
     "text": [
      "Compiling model\n"
     ]
    },
    {
     "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"
     ]
    }
   ],
   "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": 7,
   "id": "e294a75b-2670-4f5a-93d0-7a9555fd27b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Compiling model\n"
     ]
    },
    {
     "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,100\n",
      "Tree.small.blue.young,100\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"
     ]
    }
   ],
   "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
}
