Tracking Symptom Patterns and Ketogenic Therapy: Using Data and AI to Manage Mental Health

Tracking Symptom Patterns and Ketogenic Therapy: Using Data and AI to Manage Mental Health

In previous blog posts Finding Hope After Decades of Struggle, My Journey: From Mental Health Struggles to Hope Through the Power of Keto, How the Ketogenic Diet Helped Me Regain Focus and Energy, I shared my experiences with mental illness and how I’ve been using ketogenic therapy with promising results so far. Over the years, I noticed a pattern in the intensity of my symptoms, which often required me to increase and then taper off my medication—a process that typically took a couple of weeks.

To track these symptom relapses, I started logging the date, symptom intensity, and medication dosage in a Google Sheet, and I’ve been doing this consistently. I was curious to identify the precise periodicity of my relapses, so I prepared the data, filled in the missing daily entries, and generated a CSV file. Then, I asked ChatGPT to help me write a Python script to analyze the power spectrum of the data:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.fft import fft, fftfreq
from scipy.signal import periodogram, spectrogram

# Load the data
data = pd.read_csv('medical_journal.csv', parse_dates=['Data'])
data.set_index('Data', inplace=True)

# Fill missing values with 0 and resample to daily frequency
data = data.fillna(0).resample('D').asfreq().fillna(0)

# Extract the 'Symptoms' column
symptoms = data['Symptoms'].values

# Compute FFT
n = len(symptoms)
fft_values = fft(symptoms)
fft_freq = fftfreq(n, d=1)  # Assuming daily sampling

# Compute the power spectrum
power_spectrum = np.abs(fft_values)**2

def plot_power_spectrum(freq, power, max_freq=0.5):
    plt.figure(figsize=(12, 6))
    plt.plot(freq[freq > 0], power[freq > 0])
    plt.xlabel('Frequency (cycles/day)')
    plt.ylabel('Power')
    plt.title('Power Spectrum of Symptoms Data')
    plt.xlim(0, max_freq)
    plt.grid(True)
    plt.savefig('power_spectrum.png')
    plt.close()

# Plot the power spectrum
plot_power_spectrum(fft_freq, power_spectrum)

def find_significant_frequencies(freq, power, n_peaks=5):
    positive_freq = freq > 0
    sorted_indices = np.argsort(power[positive_freq])[::-1]
    top_indices = sorted_indices[:n_peaks]
    significant_freq = freq[positive_freq][top_indices]
    return significant_freq

significant_freq = find_significant_frequencies(fft_freq, power_spectrum)
print("Most significant frequencies (cycles/day):", significant_freq)
print("Corresponding periods (days):", 1/significant_freq)

def plot_periodogram(data, fs=1, max_freq=0.5):
    f, Pxx = periodogram(data, fs)
    plt.figure(figsize=(12, 6))
    plt.semilogy(f, Pxx)
    plt.xlabel('Frequency (cycles/day)')
    plt.ylabel('Power Spectral Density')
    plt.title('Periodogram of Symptoms Data')
    plt.xlim(0, max_freq)
    plt.grid(True)
    plt.savefig('periodogram.png')
    plt.close()

plot_periodogram(symptoms)

def plot_spectrogram(data, fs=1, nperseg=30):
    f, t, Sxx = spectrogram(data, fs, nperseg=nperseg)
    plt.figure(figsize=(12, 6))
    plt.pcolormesh(t, f, np.log10(Sxx), shading='gouraud')
    plt.ylabel('Frequency (cycles/day)')
    plt.xlabel('Time (days)')
    plt.title('Spectrogram of Symptoms Data')
    plt.colorbar(label='Log Power')
    plt.savefig('spectrogram.png')
    plt.close()

plot_spectrogram(symptoms)

# Save significant frequencies to a file
np.savetxt('significant_frequencies.txt', significant_freq, fmt='%f')


        

Running the script resulted in the following significant frequencies:

0.014660

0.034555

0.015707

0.001047

0.019895        

By taking the inverse of the main frequency, I calculated a period of 68.21 days, which aligns well with my empirical observations of my relapse cycle.

I’m now using this value as a reference to track the effectiveness of the ketogenic therapy I’m following. It’s still early, but my hope is that future relapses will become less frequent—or even stop—while I maintain a state of ketosis.

Additionally, ChatGPT has been helpful in generating a new keto recipe for me every day, ensuring I stay on track with the right macronutrient proportions to maintain ketosis.

Thank you for reading, and I hope you enjoy the rest of your day.

To view or add a comment, sign in

More articles by Alexandre Gomes

Explore content categories