justus-tobias commited on
Commit
2c57374
·
1 Parent(s): f218c29

improved type handling

Browse files
Files changed (3) hide show
  1. __pycache__/utils.cpython-312.pyc +0 -0
  2. app.py +12 -3
  3. utils.py +25 -12
__pycache__/utils.cpython-312.pyc ADDED
Binary file (23.2 kB). View file
 
app.py CHANGED
@@ -216,11 +216,10 @@ def get_visualizations(beattimes_table: str, cleanedaudio: gr.Audio):
216
 
217
  sr, audiodata = cleanedaudio
218
 
219
- segment_metrics = u.compute_segment_metrics(df, sr, audiodata)
220
 
 
221
 
222
 
223
- # Normalize audio data from int16 to float in range [-1, 1]
224
  audiodata = audiodata.astype(np.float32) / 32768.0
225
 
226
  # Create figure with secondary y-axes
@@ -438,11 +437,21 @@ def updateBeatsv2(audio:gr.Audio, uploadeddf:gr.File=None)-> go.Figure:
438
  encoding="utf-8-sig")
439
 
440
  # Drop rows where all columns are NaN (empty)
441
- beattimes_table = beattimes_table.dropna(how='all')
 
442
 
443
  # Reset the index after dropping rows
444
  beattimes_table = beattimes_table.reset_index(drop=True)
445
 
 
 
 
 
 
 
 
 
 
446
  # Check if the column "Label (S1=0/S2=1)" exists and rename it
447
  if "Label (S1=0/S2=1)" in beattimes_table.columns:
448
  beattimes_table = beattimes_table.rename(columns={"Label (S1=0/S2=1)": "Label (S1=1/S2=0)"})
 
216
 
217
  sr, audiodata = cleanedaudio
218
 
 
219
 
220
+ segment_metrics = u.compute_segment_metrics(df, sr, audiodata)
221
 
222
 
 
223
  audiodata = audiodata.astype(np.float32) / 32768.0
224
 
225
  # Create figure with secondary y-axes
 
437
  encoding="utf-8-sig")
438
 
439
  # Drop rows where all columns are NaN (empty)
440
+ beattimes_table = beattimes_table.dropna()
441
+
442
 
443
  # Reset the index after dropping rows
444
  beattimes_table = beattimes_table.reset_index(drop=True)
445
 
446
+ # Convert the 'Beattimes' column to float
447
+ # Handle both string and numeric values in the Beattimes column
448
+ if beattimes_table['Beattimes'].dtype == 'object':
449
+ # If strings, replace commas with dots
450
+ beattimes_table['Beattimes'] = beattimes_table['Beattimes'].str.replace(',', '.').astype(float)
451
+ else:
452
+ # If already numeric, just ensure float type
453
+ beattimes_table['Beattimes'] = beattimes_table['Beattimes'].astype(float)
454
+
455
  # Check if the column "Label (S1=0/S2=1)" exists and rename it
456
  if "Label (S1=0/S2=1)" in beattimes_table.columns:
457
  beattimes_table = beattimes_table.rename(columns={"Label (S1=0/S2=1)": "Label (S1=1/S2=0)"})
utils.py CHANGED
@@ -1,13 +1,14 @@
1
- import librosa
2
- import numpy as np
3
- import plotly.graph_objects as go
4
- from scipy.signal import savgol_filter, find_peaks
5
  from scipy.signal import butter, filtfilt, find_peaks
6
- from sklearn.cluster import KMeans
7
  from sklearn.preprocessing import StandardScaler
8
- import pywt
 
9
  import pandas as pd
10
- from scipy.interpolate import interp1d
 
 
 
 
11
 
12
 
13
 
@@ -183,6 +184,8 @@ def getBeats(audiodata: np.ndarray, sr: int, method='envelope') -> tuple[float,
183
  """
184
  # Denoise and normalize
185
  audiodata, sr = denoise_audio(audiodata, sr)
 
 
186
  cleaned_audio = audiodata / np.max(np.abs(audiodata))
187
 
188
  def get_envelope_peaks():
@@ -364,9 +367,11 @@ def plotBeattimes(beattimes: np.ndarray,
364
 
365
  # Process and plot primary beat times
366
  if isinstance(beattimes[0], str):
367
- beat_indices = np.round(np.array(beattimes, dtype=float) * sr).astype(int)
368
  else:
369
  beat_indices = np.round(beattimes * sr).astype(int)
 
 
370
  beat_indices = beat_indices[beat_indices < len(audiodata)]
371
  beat_amplitudes = audiodata[beat_indices]
372
 
@@ -399,7 +404,11 @@ def plotBeattimes(beattimes: np.ndarray,
399
 
400
  # Process and plot secondary beat times if provided
401
  if beattimes2 is not None:
402
- beat_indices2 = np.round(beattimes2 * sr).astype(int)
 
 
 
 
403
  beat_indices2 = beat_indices2[beat_indices2 < len(audiodata)]
404
  beat_amplitudes2 = audiodata[beat_indices2]
405
 
@@ -482,8 +491,9 @@ def iterate_beat_segments(beat_times, sr, audio):
482
  # Extract audio segment
483
  segment = audio[start_sample:end_sample]
484
 
485
- # Analyze segment with beat information
486
- segment_metrics.append(segment_analysis(segment, sr, beat_info))
 
487
 
488
  return segment_metrics
489
 
@@ -498,6 +508,7 @@ def segment_analysis(segment, sr, s1s2:list):
498
  Returns:
499
  - List of computed metrics
500
  """
 
501
  # Duration
502
  duration = len(segment) / sr
503
 
@@ -576,9 +587,11 @@ def compute_segment_metrics(beattimes: pd.DataFrame, sr: int, audio: np.ndarray)
576
 
577
  segment_metrics = iterate_beat_segments(beattimes, sr, audio)
578
 
 
 
579
  return segment_metrics
580
 
581
- def compute_hrv(s1_to_s2, s2_to_s1, sampling_rate=1000):
582
  """
583
  Compute Heart Rate Variability with debug statements
584
  """
 
 
 
 
 
1
  from scipy.signal import butter, filtfilt, find_peaks
2
+ from scipy.signal import savgol_filter, find_peaks
3
  from sklearn.preprocessing import StandardScaler
4
+ from sklearn.cluster import KMeans
5
+ import plotly.graph_objects as go
6
  import pandas as pd
7
+ import numpy as np
8
+ import librosa
9
+ import pywt
10
+
11
+
12
 
13
 
14
 
 
184
  """
185
  # Denoise and normalize
186
  audiodata, sr = denoise_audio(audiodata, sr)
187
+
188
+ # Normalize to prevent clipping while maintaining relative amplitudes
189
  cleaned_audio = audiodata / np.max(np.abs(audiodata))
190
 
191
  def get_envelope_peaks():
 
367
 
368
  # Process and plot primary beat times
369
  if isinstance(beattimes[0], str):
370
+ beat_indices = np.round(np.array([float(bt.replace(',', '.')) for bt in beattimes]) * sr).astype(int)
371
  else:
372
  beat_indices = np.round(beattimes * sr).astype(int)
373
+
374
+
375
  beat_indices = beat_indices[beat_indices < len(audiodata)]
376
  beat_amplitudes = audiodata[beat_indices]
377
 
 
404
 
405
  # Process and plot secondary beat times if provided
406
  if beattimes2 is not None:
407
+ if isinstance(beattimes2[0], str):
408
+ beat_indices2 = np.round(np.array([float(bt.replace(',', '.')) for bt in beattimes2]) * sr).astype(int)
409
+ else:
410
+ beat_indices2 = np.round(beattimes2 * sr).astype(int)
411
+
412
  beat_indices2 = beat_indices2[beat_indices2 < len(audiodata)]
413
  beat_amplitudes2 = audiodata[beat_indices2]
414
 
 
491
  # Extract audio segment
492
  segment = audio[start_sample:end_sample]
493
 
494
+ # Analyze segment with beat information if not empty
495
+ if len(segment) > 0:
496
+ segment_metrics.append(segment_analysis(segment, sr, beat_info))
497
 
498
  return segment_metrics
499
 
 
508
  Returns:
509
  - List of computed metrics
510
  """
511
+
512
  # Duration
513
  duration = len(segment) / sr
514
 
 
587
 
588
  segment_metrics = iterate_beat_segments(beattimes, sr, audio)
589
 
590
+ print("segment_metrics", segment_metrics)
591
+
592
  return segment_metrics
593
 
594
+ def compute_hrv(s1_to_s2, s2_to_s1, sampling_rate):
595
  """
596
  Compute Heart Rate Variability with debug statements
597
  """