Skip to content

Commit

Permalink
Merge pull request #131 from lifewatch/feature/remove_nmf
Browse files Browse the repository at this point in the history
removed nmf
  • Loading branch information
cparcerisas authored Sep 3, 2024
2 parents d9b0d95 + 70930ae commit ac65662
Show file tree
Hide file tree
Showing 6 changed files with 0 additions and 293 deletions.
36 changes: 0 additions & 36 deletions pypam/acoustic_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import xarray
from tqdm.auto import tqdm

from pypam import nmf
from pypam import plots
from pypam import signal as sig
from pypam import utils
Expand Down Expand Up @@ -980,41 +979,6 @@ def spd(self, binsize=None, bin_overlap=0, h=0.1, nfft=512, fft_overlap=0.5,
bin_overlap=bin_overlap, band=band)
return utils.compute_spd(psd_evolution, h=h, percentiles=percentiles, max_val=max_val, min_val=min_val)

def source_separation(self, window_time=1.0, n_sources=15, binsize=None, save_path=None, verbose=False, band=None):
"""
Perform non-negative Matrix Factorization to separate sources
Parameters
----------
window_time: float
window time to consider in seconds
n_sources : int
Number of sources
binsize : float
Time window considered, in seconds. If set to None, only one value is returned
save_path: str or Path
Where to save the output
verbose: bool
Set to True to make plots of the process
band : tuple or None
Band to filter the spectrogram in. A band is represented with a tuple - or a list - as
(low_freq, high_freq). If set to None, the broadband up to the Nyquist frequency will be analyzed
"""
if band is None:
band = [None, self.fs / 2]
separator = nmf.NMF(window_time=window_time, rank=n_sources, save_path=save_path)
ds = xarray.Dataset()
for i, time_bin, signal, start_sample, end_sample in self._bins(binsize, bin_overlap=0.0):
signal.set_band(band)
separation_ds = separator(signal, verbose=verbose)
separation_ds = separation_ds.assign_coords({'id': [i], 'datetime': ('id', [time_bin])})
if i == 0:
ds = separation_ds
else:
ds = xarray.concat((ds, separation_ds), 'id')
return ds

def plot_spectrum_median(self, scaling='density', db=True, log=True, save_path=None, **kwargs):
"""
Plot the power spectrogram density of all the file (units^2 / Hz) re 1 V 1 upa
Expand Down
24 changes: 0 additions & 24 deletions pypam/acoustic_survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,30 +352,6 @@ def hybrid_millidecade_bands(self, db=True, method='spectrum', band=None, percen
spectra_ds['millidecade_bands'] = milli_spectra
return spectra_ds

def source_separation(self, window_time=1.0, n_sources=15, save_path=None, verbose=False, band=None):
"""
Separate the signal in n_sources sources, using non-negative matrix factorization
Parameters
----------
window_time: float
Duration of the window in seconds
n_sources: int
Number of sources to separate the sound in
save_path: str or Path
Where to save the output
verbose: bool
Set to True to make plots of the process
band : tuple or list
Tuple or list with two elements: low-cut and high-cut of the band to analyze
"""
ds = xarray.Dataset(attrs=self._get_metadata_attrs())
for sound_file in self._files():
nmf_ds = sound_file.source_separation(window_time, n_sources, binsize=self.binsize, band=band,
save_path=save_path, verbose=verbose)
ds = utils.merge_ds(ds, nmf_ds, self.file_dependent_attrs)

return ds

def plot_rms_evolution(self, db=True, save_path=None):
"""
Plot the rms evolution
Expand Down
217 changes: 0 additions & 217 deletions pypam/nmf.py

This file was deleted.

4 changes: 0 additions & 4 deletions tests/test_acoustic_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ def test_millidecade_bands(self):
self.acu_file.hybrid_millidecade_bands(nfft, fft_overlap=0.5, binsize=None, bin_overlap=0, db=True,
method='density', band=None)

def test_nmf(self):
self.acu_file.source_separation(window_time=1.0, n_sources=15,
binsize=None, save_path=None, verbose=False, band=None)

def test_update_freq_cal(self):
ds_psd = self.acu_file.psd()
ds_psd_updated = self.acu_file.update_freq_cal(ds=ds_psd, data_var='band_density')
Expand Down
3 changes: 0 additions & 3 deletions tests/test_acoustic_survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ def test_path_not_exists(self):
def test_timestamp_array(self):
self.asa.timestamps_array()

def test_nmf(self):
ds = self.asa.source_separation(window_time=1.0, n_sources=15, save_path=None, verbose=verbose)

def test_features(self):
self.asa.evolution_multiple(method_list=fast_features, band_list=band_list)
ds = self.asa.evolution_multiple(method_list=acoustic_indices_features, min_freq=0, max_freq=4000,
Expand Down
9 changes: 0 additions & 9 deletions tests/test_signal.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import unittest
import pypam.signal as sig
import pypam.nmf as nmf
import numpy as np
from tests import skip_unless_with_plots, with_plots
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -35,14 +34,6 @@ def test_spectrum(self):
plt.plot(fbands, spectra)
plt.show()

def test_source_separation(self):
separator = nmf.NMF(window_time=0.1, rank=15)
s = sig.Signal(self.data, fs=fs)
s.set_band(None)
separation_ds = separator(s, verbose=with_plots())
reconstructed_sources = separator.reconstruct_sources(separation_ds)
separator.return_filtered_signal(s, reconstructed_sources['C_tf'])

def test_acoustic_indices(self):
s = sig.Signal(self.data, fs=fs)
aci = s.aci()
Expand Down

0 comments on commit ac65662

Please sign in to comment.