-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtranslated.py
133 lines (113 loc) · 5.21 KB
/
translated.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import numpy as np
import pandas as pd
from scipy.signal import butter, filtfilt
from scipy.interpolate import interp1d
from scipy.signal import find_peaks
from numpy.linalg import norm, det
# Placeholder for import_horse function
def import_horse(file_path):
# This function should be implemented to import data from the given file path.
# Assuming the file is in a format that can be read as a DataFrame, like CSV.
# Replace this with actual implementation.
df = pd.read_csv(file_path)
return df.to_numpy().T # Transposed to match MATLAB data structure
# Function to find the angle
def findangle(x1, y1, l1, x2, y2, l2, x3, y3, l3, frame, L, spotcheck, color, name, directionUsed=None):
angle3 = []
angle = np.array([x1, y1, x2, y2, x3, y3]).T
valid = (l1 > L) & (l2 > L) & (l3 > L)
angle = angle[valid, :]
goodframes = frame[valid]
P0 = angle[:, 2:4]
P1 = angle[:, 0:2]
P2 = angle[:, 4:6]
for i in range(len(goodframes)):
n1 = (P2[i, :] - P0[i, :]) / norm(P2[i, :] - P0[i, :])
n2 = (P1[i, :] - P0[i, :]) / norm(P1[i, :] - P0[i, :])
if directionUsed is not None:
angle3.append(np.arctan2(det([n2, n1]), np.dot(n2, n1)))
else:
angle3.append(np.arctan2(norm(det([n2, n1])), np.dot(n1, n2)))
angle3 = pd.Series(angle3).rolling(window=5).median().to_numpy()
return angle3, goodframes
# Function to find the floor
def floorfind(HTy, HMy, HHy):
datas = [HTy, HMy, HHy]
floory, floorx = [], []
for data in datas:
floory.append(np.min(data[:len(data)//3]))
floory.append(np.min(data[len(data)//3:2*len(data)//3]))
floory.append(np.min(data[2*len(data)//3:]))
floorx.append(np.argmin(data[:len(data)//3]))
floorx.append(np.argmin(data[len(data)//3:2*len(data)//3]) + len(data)//3)
floorx.append(np.argmin(data[2*len(data)//3:]) + 2*len(data)//3)
floory = np.array(floory).reshape(-1)
floorx = np.array(floorx).reshape(-1)
P = np.polyfit(floorx, floory, 1)
return P
# Function to extract Y data features
def pulloutYstuff(data, frames, spotcheck, color, name):
pks, locs, w, p = find_peaks(data, height=4, distance=25)
p = p['peak_heights']
TFp = np.abs(p - np.mean(p)) < 2 * np.std(p) # Remove outliers based on prominence
TFw = np.abs(w - np.mean(w)) < 2 * np.std(w) # Remove outliers based on width
TF = TFp & TFw
mp = np.mean(p[TF])
stdp = np.std(p[TF])
mw = np.mean(w[TF])
stdw = np.std(w[TF])
return mw, mp, stdw, stdp
# Function for finding stride-to-off (FSTO)
def fsto(data, frames, directionfactor, spotcheck, graphfactor, color, name):
dxdf = np.gradient(data) / np.gradient(frames)
dxdf, rframes = remove_outliers(np.column_stack((dxdf, frames)), 25)
pks, locs, _, _ = find_peaks(np.abs(dxdf), height=1, distance=25)
# Further implementation needed based on MATLAB code
# Placeholder return values
dutyfactor, stridelength, numcycles, strides = 0, 0, 0, np.array([])
dutyfactorStd, stridelengthStd, byEye = 0, 0, '-'
return dutyfactor, stridelength, numcycles, strides, dutyfactorStd, stridelengthStd, byEye
def avgforstride(data, f2, strides, stridesXstart, spotcheck, color, name):
"""
Averages stride data.
"""
# Placeholder implementation. Adjust as per specific requirements.
avg_stride = sum(data) / len(data)
return avg_stride
def remove_outliers(data, window):
"""
Removes outliers from data based on a moving average method.
"""
# Placeholder implementation. Adjust as per specific requirements.
filtered_data = [d for d in data if abs(d - sum(data) / len(data)) < window]
return filtered_data
def basiccmooth(datax, datay, dataf, d1):
"""
Smoothes coordinate data.
"""
# Placeholder implementation. Adjust as per specific requirements.
smoothed_datax = [sum(datax[max(i - d1, 0):min(i + d1 + 1, len(datax))]) / (2 * d1 + 1) for i in range(len(datax))]
smoothed_datay = [sum(datay[max(i - d1, 0):min(i + d1 + 1, len(datay))]) / (2 * d1 + 1) for i in range(len(datay))]
return smoothed_datax, smoothed_datay
# Reading the existing Python script
script_path = '/mnt/data/horse_analysis_script.py'
with open(script_path, 'r') as file:
existing_script = file.read()
# Adding the new function implementations to the script
new_functions = "\n\n".join([avgforstride.__doc__, remove_outliers.__doc__, basiccmooth.__doc__])
updated_script = existing_script + "\n\n# New Function Implementations\n" + new_functions
# Saving the updated script
updated_script_path = '/mnt/data/updated_horse_analysis_script.py'
with open(updated_script_path, 'w') as file:
file.write(updated_script)
script_content = inspect.getsource(import_horse) + "\n" + \
inspect.getsource(findangle) + "\n" + \
inspect.getsource(floorfind) + "\n" + \
inspect.getsource(pulloutYstuff) + "\n" + \
inspect.getsource(fsto) + "\n" + \
inspect.getsource(avgforstride) + "\n" + \
inspect.getsource(basiccmooth) + "\n" + \
inspect.getsource(main)
with open('/mnt/data/horse_analysis_script.py', 'w') as file:
file.write(script_content)
'/mnt/data/horse_analysis_script.py'