#!/usr/bin/env python # coding: utf-8 # In[1]: import numpy as np # ## PyCircStat2 Utility Functions # # `pycircstat2` includes a handful of useful utility functions: # # - [`load_data`](#load-data) # - [`time2float`](#converting-time-string-into-float) # - [`data2rad`](#converting-data-onto-a-circular-scale-in-radian-and-vice-versa) # - [`angrange`](#range) # ### Load Data # # 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`. # In[2]: from pycircstat2.utils import load_data data, meta = load_data(name='B1', source='fisher', print_meta=True, return_meta=True) # In[3]: meta # In[4]: data[:3] # ## Converting time string into float # # 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. # In[5]: from pycircstat2.utils import time2float data_float = time2float(data['time'].values) data_float[:5] # ### Converting data onto a circular scale (in radian) and vice versa # # `data2rad(data, k)` convert data into radian and `rad2data(rad, k)` convert radian back to the original unit. # In[6]: from pycircstat2.utils import data2rad, rad2data # In[7]: data_rad = data2rad(data=data_float, k=24) # k is the time units in the full cycle. data_rad[:5] # e.g. for hours, k=24; for years, k=365. # In[8]: data_hour = rad2data(rad=data_rad, k=24) data_hour[:5] # In[9]: np.allclose(data_float, data_hour) # ### Range # # `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`. # In[10]: from pycircstat2.utils import angrange angrange(np.array([0., 3., 6., np.pi * 2, 7.])) # In[11]: get_ipython().run_line_magic('load_ext', 'watermark') get_ipython().run_line_magic('watermark', '--time --date --timezone --updated --python --iversions --watermark -p pycircstat2')