first commit

This commit is contained in:
Mike Dijkhof 2021-06-30 15:19:14 +02:00
commit 6aa852cce4
4 changed files with 306 additions and 0 deletions

36
DayCutter.py Normal file
View File

@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import os
import pandas as pd
WeekPath = 'D:\AcA Mike Dijkhof\cwa files\Pt203_csv' # path to directory with .csv file of entire week
ScriptPath = 'D:\AcA Mike Dijkhof\Scripts' # path to directory of formules.py
os.chdir(ScriptPath)
import formules
os.chdir(WeekPath)
#%%
for subdir, dirs, files in os.walk(WeekPath):
print(subdir)
os.chdir(subdir)
for i in files:
print(i)
if i.startswith('._'): #skip ._ files that may be present in the folder
continue
df = pd.read_csv(i, header=0, names=['Datetime', 'Acc X','Acc Y', 'Acc Z'], dtype={"Datetime": str, "Acc X": 'float32', "Acc Y": 'float32', "Acc Z": 'float32'}, infer_datetime_format=True)
df['Datetime'] = pd.to_datetime(df['Datetime'])
df['Date'] = [d.date() for d in df['Datetime']]
df = df.reindex(columns=['Datetime','Date','Time','Acc X','Acc Y', 'Acc Z'])
formules.CreateDays(df, i, subdir)
os.chdir(subdir)

61
Totalplotter.py Normal file
View File

@ -0,0 +1,61 @@
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 26 13:27:00 2021
@author: D_Mik
"""
import os
import pandas as pd
import formules
import numpy as np
import matplotlib.pyplot as plt
# provide directory for formula parameters
rootdir = 'D:\AcA Mike Dijkhof\cwa files\Pt304_csv\Pt304_week_formula'
d = {}
X = list()
# import function parameters from previous scripts
for subdir, dirs, files in os.walk(rootdir):
for file in files:
print(file)
os.chdir(subdir)
name = file.replace('Formula_','')
name = name.replace('.csv','')
df = pd.DataFrame(pd.read_csv(file))
df = df.set_index('Name')
scale = df['ENMOmax'].max()
key = name
d[key] = df.loc['Mean']
X.append(scale)
# plot curves
plt.figure(dpi=720)
for key in d:
formula = d[key]
Xscale = np.arange(0, max(X), 5)
Y = formules.func(Xscale, formula.loc['a'], formula.loc['b'], formula.loc['c'] )
plt.ylim(0,1440)
plt.xlim(0,(max(X)+10))
plt.plot(Xscale, Y, label=key)
plt.legend()
plt.title('All 6 weeks plotter for 304') #Change to pt-number
plt.show()
plt.savefig(fname='Total_weeks_304.png')

135
formules.py Normal file
View File

@ -0,0 +1,135 @@
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 16 09:34:35 2021
@author: -
"""
import osa
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from datetime import timedelta
from scipy.optimize import curve_fit
# Formulas
def ReadCSV(filename):
df = pd.read_csv(filename, names=['Datetime', 'Acc X','Acc Y', 'Acc Z'], infer_datetime_format=True)
df['Datetime'] = pd.to_datetime(df['Datetime'])
df['Date'] = [d.date() for d in df['Datetime']]
df = df.reindex(columns=['Datetime','Date','Time','Acc X','Acc Y', 'Acc Z'])
return df
def CreateDays(x, filename, path):
savename = filename.replace('.csv','')
savepath = path + savename
os.makedirs(savepath)
os.chdir(savepath)
startdate = x['Date'].iloc[0]
week = range(1,8)
for i in week:
weekdayindex = i-1
day = startdate + timedelta(days=weekdayindex)
daydate = x['Date'] == startdate + timedelta(days=weekdayindex)
dataday = x[daydate]
totalweek = {day:dataday}
savefile = totalweek[day]
varname = filename.replace('.csv','-') + str(day) + '.csv'
savefile.to_csv(varname)
print(varname +' saved')
return(totalweek)
def SVMEpoch(DF,ResampRate, ResampData):
newDF = pd.DataFrame(DF)
newDF['X2'] = np.power(newDF['Acc X'], 2)
newDF['Y2'] = np.power(newDF['Acc Y'], 2)
newDF['Z2'] = np.power(newDF['Acc Z'], 2)
newDF['SVM'] = np.sqrt(newDF[['X2', 'Y2', 'Z2']].sum(axis=1))
newDF['Datetime'] = pd.to_datetime(newDF['Datetime'])
EpochSVM = newDF.resample(ResampRate, on = ResampData).mean()
return(newDF, EpochSVM)
def func(x, a, b, c):
return a * np.exp(-b*x) + c
def SlopeWeeker(Keylist, Dict):
try:
SlopeWeek = pd.DataFrame(columns=['a','b', 'c', 'Name'])
SlopeWeek = SlopeWeek.set_index('Name')
for key in Keylist:
newDF, EpochSVM = SVMEpoch(Dict[key], '60S', 'Datetime')
ENMO = EpochSVM['SVM']-1
ENMO = ENMO*1000
for value in ENMO:
if value < 0:
value = 0
BinSize = 5
ENMOmax = int(ENMO.max())
if ENMOmax % BinSize == 0:
ENMOmax = ENMOmax+1 #to make sure that interference with binsize is impossible
MaxBin = int(ENMOmax/BinSize)+1
ENMO = ENMO.astype(int)
Counter = pd.DataFrame(np.zeros((1,MaxBin)))
for x in Counter:
Count = (x+1)*BinSize
Start = Count - BinSize
Number = ENMO.between(Start, Count).sum()
Counter[x] = Number
Counter = Counter.to_numpy()
Counter = Counter.astype(float)
Counter = Counter.flatten()
Xscale = np.arange(0,ENMOmax, BinSize)
Xscale = Xscale.astype(float)
popt, _ = curve_fit(func, Xscale, Counter, p0=None) # fit curve through points
a, b, c = popt
Trendline = func(Xscale, a, b, c)
SlopeWeek.loc[key, 'a'] = a
SlopeWeek.loc[key, 'b'] = b
SlopeWeek.loc[key, 'c'] = c
SlopeWeek.loc[key, 'ENMOmax'] = ENMOmax
PtName = key.replace('35694_00000', '')
PtName = PtName.replace('resampled-','')
PtName = PtName.replace('.csv','')
plt.figure()
plt.ylim(0,1440)
plt.xlim(0,(ENMOmax+10))
plt.title('Intensity plot ' + PtName)
plt.xlabel('Movement intensity [bins of ' + str(BinSize) + ' mg]')
plt.ylabel('Amount of time spend at intensity [min]')
plt.grid()
plt.scatter(Xscale, y=Counter)
plt.plot(Xscale, Trendline, 'r--')
plt.show()
PtName = (PtName + '.png')
plt.savefig(fname=PtName)
except:
print(PtName + ' could not be used')
return SlopeWeek

74
plotter.py Normal file
View File

@ -0,0 +1,74 @@
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 16 13:03:03 2021
@author: -
"""
import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
os.chdir('D:\AcA Mike Dijkhof\Scripts') # set path to folder of formules.py
import formules
rootdir = 'D:\AcA Mike Dijkhof\cwa files\Pt204_csv\Pt204_csv35694_0000020406.resampled' # provide path to .csv files of the cut weekdays
d = {}
for subdir, dirs, files in os.walk(rootdir):
print(subdir)
for file in files:
print(file)
os.chdir(subdir)
d[file] = pd.read_csv(file, infer_datetime_format=True)
Keys = d.keys()
#%%
Check = formules.SlopeWeeker(Keys, d)
Worklist = Check
Length = pd.DataFrame(np.zeros(((1,int(len(Check)/7)))))
# Create PtName, this depends on subdir string so has to be changed for each patient
PtName = subdir.replace('35694_00000', '')
PtName = PtName.replace('D:\AcA Mike Dijkhof\cwa files\Pt204_csv\Pt204_','')
PtName = PtName.replace('resampled-','')
PtName = PtName.replace('.resampled','')
PtName = PtName.replace('.csv','')
for i in Length:
CheckWeek = Worklist[:7]
Worklist = Worklist.drop(CheckWeek.index, axis=0)
CheckWeek.loc['Mean','a'] = CheckWeek['a'].median()
CheckWeek.loc['Mean','b'] = CheckWeek['b'].median()
CheckWeek.loc['Mean','c'] = CheckWeek['c'].median()
CheckWeek.to_csv('Formula_Week6.csv')
Xscale = np.arange(0,CheckWeek['ENMOmax'].max(), 5)
plt.figure()
plt.ylim(0,1440)
plt.xlim(0,CheckWeek['ENMOmax'].max())
plt.title('All weekdays and average plotted ' + PtName)
plt.xlabel('Movement intensity [bins of 5 mg]')
plt.ylabel('Amount of time spend at intensity [min]')
plt.grid()
for i, r in CheckWeek.iterrows():
Y = formules.func(Xscale, CheckWeek.loc[i,'a'],CheckWeek.loc[i,'b'], CheckWeek.loc[i,'c'] )
if i != 'Mean':
plt.plot(Xscale, Y, 'grey')
else:
plt.plot(Xscale, Y, 'k--')
plt.show()
plt.savefig(fname=('Weekplot ' + PtName+ '.png'))