|
36 | 36 | "cell_type": "code", |
37 | 37 | "execution_count": 1, |
38 | 38 | "id": "ba14333118837a04", |
39 | | - "metadata": {}, |
| 39 | + "metadata": { |
| 40 | + "ExecuteTime": { |
| 41 | + "end_time": "2025-07-23T14:16:41.434784Z", |
| 42 | + "start_time": "2025-07-23T14:16:39.106959Z" |
| 43 | + } |
| 44 | + }, |
40 | 45 | "outputs": [], |
41 | 46 | "source": [ |
42 | 47 | "import circStudio\n", |
|
6690 | 6695 | }, |
6691 | 6696 | { |
6692 | 6697 | "cell_type": "code", |
6693 | | - "execution_count": null, |
| 6698 | + "execution_count": 4, |
| 6699 | + "id": "a02a7dde-08fe-4d1b-8401-c264de032916", |
| 6700 | + "metadata": {}, |
| 6701 | + "outputs": [], |
| 6702 | + "source": [ |
| 6703 | + "import pandas as pd\n", |
| 6704 | + "import numpy as np" |
| 6705 | + ] |
| 6706 | + }, |
| 6707 | + { |
| 6708 | + "cell_type": "code", |
| 6709 | + "execution_count": 96, |
6694 | 6710 | "id": "32e995ba-6cb9-411e-b4c5-ec531e865c1e", |
6695 | 6711 | "metadata": {}, |
6696 | 6712 | "outputs": [], |
| 6713 | + "source": [ |
| 6714 | + "def sleep_onset_latency():\n", |
| 6715 | + " main_sleep_df = pd.DataFrame({\n", |
| 6716 | + " 'date': ['1918-01-24', '1918-01-25', '1918-01-27'],\n", |
| 6717 | + " 'start_time': ['1918-01-24 23:43:00', '1918-01-25 22:17:00', '1918-01-27 01:43:00'],\n", |
| 6718 | + " 'stop_time': ['1918-01-25 06:25:00', '1918-01-26 07:20:00', '1918-01-27 07:11:00']\n", |
| 6719 | + " })\n", |
| 6720 | + " for col in main_sleep_df.columns:\n", |
| 6721 | + " main_sleep_df[col] = pd.to_datetime(main_sleep_df[col])\n", |
| 6722 | + "\n", |
| 6723 | + " diary_nights_df = pd.DataFrame({\n", |
| 6724 | + " 'START': ['1918-01-24 23:00:00', '1918-01-25 22:00:00', '1918-01-27 00:00:00'],\n", |
| 6725 | + " 'END': ['1918-01-25 07:00:00', '1918-01-26 07:30:00', '1918-01-27 07:30:00']\n", |
| 6726 | + " })\n", |
| 6727 | + " for col in diary_nights_df.columns:\n", |
| 6728 | + " diary_nights_df[col] = pd.to_datetime(diary_nights_df[col])\n", |
| 6729 | + "\n", |
| 6730 | + " # Create an empty dictionary to store sleep_onset_latency (sol) values\n", |
| 6731 | + " sol = {}\n", |
| 6732 | + " \n", |
| 6733 | + " # Iterate over the rows of the sleep diary corresponding to nighttime\n", |
| 6734 | + " for _, row in diary_nights_df.iterrows():\n", |
| 6735 | + " # Extract the date from the current row\n", |
| 6736 | + " date = row['START'].date()\n", |
| 6737 | + " matches = main_sleep_df[main_sleep_df['start_time'].dt.date == date]\n", |
| 6738 | + " if not matches.empty:\n", |
| 6739 | + " sleep_onset = matches.iloc[0]['start_time']\n", |
| 6740 | + " latency = sleep_onset - row['START']\n", |
| 6741 | + " sol[date] = latency\n", |
| 6742 | + " sol = pd.Series(sol)\n", |
| 6743 | + " return pd.Series(sol), np.mean(sol)" |
| 6744 | + ] |
| 6745 | + }, |
| 6746 | + { |
| 6747 | + "cell_type": "code", |
| 6748 | + "execution_count": 97, |
| 6749 | + "id": "5711a686-722d-4e11-9726-498beaed6e6a", |
| 6750 | + "metadata": {}, |
| 6751 | + "outputs": [ |
| 6752 | + { |
| 6753 | + "data": { |
| 6754 | + "text/plain": [ |
| 6755 | + "(1918-01-24 0 days 00:43:00\n", |
| 6756 | + " 1918-01-25 0 days 00:17:00\n", |
| 6757 | + " 1918-01-27 0 days 01:43:00\n", |
| 6758 | + " dtype: timedelta64[ns],\n", |
| 6759 | + " Timedelta('0 days 00:54:20'))" |
| 6760 | + ] |
| 6761 | + }, |
| 6762 | + "execution_count": 97, |
| 6763 | + "metadata": {}, |
| 6764 | + "output_type": "execute_result" |
| 6765 | + } |
| 6766 | + ], |
| 6767 | + "source": [ |
| 6768 | + "sleep_onset_latency()" |
| 6769 | + ] |
| 6770 | + }, |
| 6771 | + { |
| 6772 | + "cell_type": "code", |
| 6773 | + "execution_count": null, |
| 6774 | + "id": "ea5f75de-52d0-43c5-9252-43dac1698d96", |
| 6775 | + "metadata": {}, |
| 6776 | + "outputs": [], |
6697 | 6777 | "source": [] |
6698 | 6778 | } |
6699 | 6779 | ], |
|
0 commit comments