{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## PyCircStat2 Utility Functions\n", "\n", "`pycircstat2` includes a handful of useful utility functions:\n", "\n", "- [`load_data`](#load-data)\n", "- [`time2float`](#converting-time-string-into-float)\n", "- [`data2rad`](#converting-data-onto-a-circular-scale-in-radian-and-vice-versa)\n", "- [`angrange`](#range)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Load Data\n", "\n", "All data sets from Fisher (1993) and Zar (2010; Ch26 and 27), some from Pewsey, et al. (2014) and Mardia (1972) are included in `pycircstat2`, which can be loaded by `load_data` in `pycircstat2.utils`. Meta data of the data set can be printed if `print_meta = True`, or return along with the data set if `return_meta = True`. " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " \"Title\": \"B.1 Arrival times at an intensive care unit\",\n", " \"Description\": \"Arrival times on a 24-hour clock of 254 patients at an intnsive care unit, over a period of about 12 months\",\n", " \"Type\": \"Vectors\",\n", " \"Source\": \"Cox & Lewis (1966, pp. 254-5)\",\n", " \"Examples\": \"2.1, 2.3, 2.4, 2.5, 2.6, 2.8, 2.10\",\n", " \"Columns\": {\n", " \"time\": {\n", " \"name\": \"time\",\n", " \"type\": \"vectors\"\n", " }\n", " }\n", "}\n" ] } ], "source": [ "from pycircstat2.utils import load_data\n", "\n", "data, meta = load_data(name='B1', source='fisher', print_meta=True, return_meta=True)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Title': 'B.1 Arrival times at an intensive care unit',\n", " 'Description': 'Arrival times on a 24-hour clock of 254 patients at an intnsive care unit, over a period of about 12 months',\n", " 'Type': 'Vectors',\n", " 'Source': 'Cox & Lewis (1966, pp. 254-5)',\n", " 'Examples': '2.1, 2.3, 2.4, 2.5, 2.6, 2.8, 2.10',\n", " 'Columns': {'time': {'name': 'time', 'type': 'vectors'}}}" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "meta" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
time
111:00
217:00
323:15
\n", "
" ], "text/plain": [ " time\n", "1 11:00\n", "2 17:00\n", "3 23:15" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data[:3]" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Converting time string into float\n", "\n", "Some of the typical circular data are encoded in string in time units (e.g. `12:45 h`), we need to convert them into float for further computation. " ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([11. , 17. , 23.25, 10. , 12. ])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from pycircstat2.utils import time2float\n", "data_float = time2float(data['time'].values)\n", "data_float[:5]" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Converting data onto a circular scale (in radian) and vice versa\n", "\n", "`data2rad(data, k)` convert data into radian and `rad2data(rad, k)` convert radian back to the original unit." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "from pycircstat2.utils import data2rad, rad2data" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2.87979327, 4.45058959, 6.08683577, 2.61799388, 3.14159265])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data_rad = data2rad(data=data_float, k=24) # k is the time units in the full cycle.\n", "data_rad[:5] # e.g. for hours, k=24; for years, k=365." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([11. , 17. , 23.25, 10. , 12. ])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data_hour = rad2data(rad=data_rad, k=24)\n", "data_hour[:5]" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.allclose(data_float, data_hour)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Range\n", "\n", "`pycircstat2` assumed all data fall onto a unit circle, meaning that all data points are within the range of [0, 2π). In the `Circular` class, all input data will be converted into this range by calling `angrange(rad)` in `pycircstat2.utils`." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0. , 3. , 6. , 0. , 0.71681469])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from pycircstat2.utils import angrange\n", "angrange(np.array([0., 3., 6., np.pi * 2, 7.]))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Last updated: 2024-11-21 16:44:20CET\n", "\n", "Python implementation: CPython\n", "Python version : 3.10.13\n", "IPython version : 8.29.0\n", "\n", "pycircstat2: 0.1.0\n", "\n", "numpy : 2.1.3\n", "pycircstat2: 0.1.0\n", "\n", "Watermark: 2.5.0\n", "\n" ] } ], "source": [ "%load_ext watermark\n", "%watermark --time --date --timezone --updated --python --iversions --watermark -p pycircstat2" ] } ], "metadata": { "kernelspec": { "display_name": ".circstat", "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.10.13" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }