-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremovenoisebeforeik.py
More file actions
79 lines (62 loc) · 2.83 KB
/
Copy pathremovenoisebeforeik.py
File metadata and controls
79 lines (62 loc) · 2.83 KB
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
import matplotlib.pyplot as plt
import numpy as np
import sys
import os
def removeNoise(original_trc,start,stop,trial):
if start == '0' and stop == '0':
# Load data, skipping only the first two header rows
marker_data = np.loadtxt(original_trc, skiprows=5)
print(f"original trc is {original_trc}")
print(f"marker data is {marker_data.shape}")
# Select the 54th column
x = marker_data[:, 53] # Python's 0-based indexing, so 53 corresponds to the 54th column
y = marker_data[:, 54]
z = marker_data[:, 55]
# Create a plot for the 54th column
plt.plot(x, marker='o', linestyle='-', color='b')
plt.plot(y, marker='o', linestyle='-', color='b')
plt.plot(z, marker='o', linestyle='-', color='b')
# Initialize start and stop variables
start = None
stop = None
# Define a function to handle mouse click events and set start and stop variables
def on_click(event):
nonlocal start, stop
if event.button == 1: # Left mouse button
if start is None:
start = int(event.xdata)
print(f"Start set to {start}")
elif stop is None:
stop = int(event.xdata)
print(f"Stop set to {stop}")
plt.close()
# Connect the mouse click event to the handler function
plt.gcf().canvas.mpl_connect('button_press_event', on_click)
plt.xlabel('X Values')
plt.ylabel('Y Values')
plt.title('Click start and end points to remove noise')
plt.grid(True)
plt.show()
# Print the values of start and stop
print(f"Start: {start}")
print(f"Stop: {stop}")
start = int(start)
stop = int(stop)
# Get marker names and original header from the original TRC file
with open(original_trc, "r") as trc:
lines = trc.readlines() # Read all lines at once
header_lines = lines[:5] # Assuming the header is contained in the first 5 lines
data = lines[start:stop] # Extract data lines based on start and stop variables
# Create the directory if it doesn't exist
# Get the directory of the source file
source_dir = os.path.dirname(original_trc)
# Destination directory
output_dir = os.path.join(source_dir, "processed_trcs", trial)
os.makedirs(output_dir, exist_ok=True)
# Construct the cut filename
cutfilename = os.path.join(output_dir, os.path.basename(original_trc[:-4] + "_cut.trc"))
# Write header with original header to the temporary TRC file
with open(cutfilename, "w") as temp_file:
temp_file.writelines(header_lines)
temp_file.writelines(data)
return cutfilename, start, stop