{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1633b166",
   "metadata": {},
   "source": [
    "\n",
    "# Create a DataFrame\n",
    "\n",
    "The `create_scd` function allows you to create a structured DataFrame for single-case data analysis. It is designed to handle phase-based measurements and can include additional variables.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "90ebc6df",
   "metadata": {},
   "source": [
    "\n",
    "## What Does `create_scd` Do?\n",
    "\n",
    "The `create_scd` function creates a single-case experimental design (SCED) DataFrame. It requires at least `values`, which represent the recorded data. Phases can be defined either using `phase_starts` (which specifies where each phase begins) or `phase_design` (which specifies how long each phase lasts).\n",
    "\n",
    "### Required Arguments:\n",
    "- `values`: The primary data for your experiment (must be provided).\n",
    "\n",
    "### Optional Arguments:\n",
    "- `phase_starts`: Define where each phase begins (e.g., `{\"A\": 1, \"B\": 5}`). \n",
    "- `phase_design`: Define how many measurements belong to each phase (e.g., `{\"A\": 4, \"B\": 6}`). \n",
    "- `mt`: A list of measurement time points (optional). \n",
    "- Extra variables (e.g., `teacher`, `hour`) can be included.\n",
    "\n",
    "If no phase information is given, an error will be raised.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c165e18f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "📖 scia 1.101.0.dev6 - For Documentation, visit: https://ahsankhodami.github.io/scia/intro.html\n"
     ]
    }
   ],
   "source": [
    "\n",
    "import scia as sc\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bdddf323",
   "metadata": {},
   "source": [
    "\n",
    "### Example 1: Defining Phases with `phase_starts`\n",
    "\n",
    "To create a simple dataset where phases are specified by their starting points:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8fe12fb2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   values  mt phase\n",
      "0       2   1     A\n",
      "1       2   2     A\n",
      "2       4   3     A\n",
      "3       5   4     A\n",
      "4       8   5     B\n",
      "5       7   6     B\n",
      "6       6   7     B\n",
      "7       9   8     B\n",
      "8       8   9     B\n",
      "9       7  10     B\n"
     ]
    }
   ],
   "source": [
    "\n",
    "df = sc.create_scd(\n",
    "    values=[2,2,4,5,8,7,6,9,8,7],\n",
    "    phase_starts={\"A\": 1, \"B\": 5}\n",
    ")\n",
    "print(df)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f35f4c60",
   "metadata": {},
   "source": [
    "\n",
    "### Example 2: Using `phase_design`\n",
    "\n",
    "Here, phases are defined by specifying how many measurements belong to each phase:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e287dddd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   values  mt phase\n",
      "0       2   1     A\n",
      "1       2   2     A\n",
      "2       4   3     A\n",
      "3       5   6     A\n",
      "4       8   8     B\n",
      "5       7   9     B\n",
      "6       6  11     B\n",
      "7       9  12     B\n",
      "8       8  16     B\n",
      "9       7  18     B\n"
     ]
    }
   ],
   "source": [
    "\n",
    "df = sc.create_scd(\n",
    "    values=[2,2,4,5,8,7,6,9,8,7],\n",
    "    mt=[1,2,3,6,8,9,11,12,16,18],\n",
    "    phase_design={\"A\": 4, \"B\": 6}\n",
    ")\n",
    "print(df)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b16306d7",
   "metadata": {},
   "source": [
    "\n",
    "### Example 3: Including Additional Variables\n",
    "\n",
    "Additional variables such as `teacher` and `hour` can be included:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "70b36266",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   teacher  hour  values  mt phase\n",
      "0        0     2       2   1     A\n",
      "1        0     3       2   2     A\n",
      "2        1     4       3   3     A\n",
      "3        1     3       5   4     A\n",
      "4        0     3       8   5     B\n",
      "5        1     1       7   6     B\n",
      "6        1     6       6   7     B\n",
      "7        1     5       9   8     B\n",
      "8        0     2       7   9     B\n",
      "9        1     2       7  10     B\n"
     ]
    }
   ],
   "source": [
    "\n",
    "df = sc.create_scd(\n",
    "    values={\"A\": [2,2,3,5], \"B\": [8,7,6,9,7,7]},\n",
    "    teacher=[0,0,1,1,0,1,1,1,0,1], \n",
    "    hour=[2,3,4,3,3,1,6,5,2,2]\n",
    ")\n",
    "print(df)\n"
   ]
  }
 ],
 "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.12.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
