Skip to content

Commit

Permalink
last fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tensionhead committed Jan 6, 2021
1 parent 5afb8f3 commit 4f32de8
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 79 deletions.
63 changes: 4 additions & 59 deletions pyboat/ensemble_measures.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from pyboat.core import find_COI_crossing, complex_average


def average_power_distribution(ridge_results, signal_ids = None, exclude_coi = False):

'''
Expand Down Expand Up @@ -69,13 +70,15 @@ def average_power_distribution(ridge_results, signal_ids = None, exclude_coi = F

return powers_series


def get_ensemble_dynamics(ridge_results):

'''
Aggregate all the ridge-readouts (period, amplitude and phase)
of a ridge-analyzed signal ensemble and return time-continuous median and
quartiles (as in a time-continuous box plot).
In the case of phase return the 1st order parameter (length of resultant vector)
In the case of phase return the 1st order parameter
(length of resultant vector)
as a phase coherence measure over time.
Signals/ridge-readouts of unequal length in time are Ok!
Expand Down Expand Up @@ -126,61 +129,3 @@ def get_ensemble_dynamics(ridge_results):
phases_R['R'] = R

return periods_mq1q3, amplitudes_mq1q3, powers_mq1q3, phases_R



if __name__ == '__main__':

'''
A short demonstration of the provided
ensemble measures
'''

from pyboat import WAnalyzer, ssg
from pyboat.plotting import ensemble_dynamics, power_distribution, Fourier_distribution

# set up analyzing instance
periods = np.linspace(5, 60, 100)
dt = 1
wAn = WAnalyzer(periods, dt, T_cut_off = None)

# create a bunch of chirp signals
Nsignals = 50 # times 2
Tstart = 30 # initial period
Tend = 50 # period at the end
Nt = 500 # number of samples per signal
signals = [
ssg.create_noisy_chirp(T1 = Tstart, T2 = T, Nt = Nt, eps = 1)
for T in np.linspace(Tstart, Tend, Nsignals) ]

# add the same amount of pure noise
noisy_ones = [
ssg.ar1_sim(alpha = 0.5, N = Nt)
for i in range(Nsignals)]

signals = signals + noisy_ones

# get the the individual ridge readouts
ridge_results = []

# store the individual time averaged Wavelet spectra
df_fouriers = pd.DataFrame(columns = np.arange(len(signals)), index = wAn.periods)

for i,signal in enumerate(signals):

wAn.compute_spectrum(signal, sinc_detrend = False, do_plot = False)
rd = wAn.get_maxRidge(smoothing_wsize = 11)
ridge_results.append(rd)

df_fouriers[i] = wAn.get_averaged_spectrum()

# the time-averaged power distribution
powers_series = average_power_distribution( ridge_results, signal_ids = None )
power_distribution(powers_series)

# keeping the pure noise signal out
res = get_ensemble_dynamics(ridge_results[:Nsignals])
ensemble_dynamics(*res)

# the Fourier power distribution
Fourier_distribution(df_fouriers)
7 changes: 5 additions & 2 deletions pyboat/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def averaged_Wspec(averaged_Wspec, periods, time_unit="a.u", fig=None):
ax.plot(periods, averaged_Wspec, lw=SIGNAL_LW, color=FOURIER_COLOR)
ax.fill_between(periods, 0, averaged_Wspec, color=FOURIER_COLOR, alpha=0.3)


return ax
# --- Wavelet spectrum ------


Expand Down Expand Up @@ -304,7 +304,6 @@ def plot_signal_modulus(axs, time_vector, signal, modulus, periods, p_max=None):
mod_ax.set_xlim((time_vector[0], time_vector[-1]))
mod_ax.grid(axis="y", color="0.6", lw=1.0, alpha=0.5) # vertical grid lines

min_power = modulus.min()
if p_max is None:
cb_ticks = [0, int(np.floor(modulus.max()))]
else:
Expand Down Expand Up @@ -543,6 +542,8 @@ def plot_readout(ridge_data, time_unit="a.u.", draw_coi=False, fig=None):
ax4.tick_params(axis="both", labelsize=tick_label_size)

fig.tight_layout()

return axs

# -------- Ensemble Measures Plots -------------------------------------

Expand Down Expand Up @@ -716,6 +717,7 @@ def ensemble_dynamics(
fig.subplots_adjust(wspace=0.3, left=0.11, top=0.98, right=0.97, bottom=0.14)

# fig.subplots_adjust(bottom = 0.1, left = 0.2, top = 0.95, hspace = 0.1)
return axs


def Fourier_distribution(
Expand Down Expand Up @@ -764,3 +766,4 @@ def Fourier_distribution(
ax.legend()

fig.tight_layout()
return ax
26 changes: 20 additions & 6 deletions pyboat/ui/data_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def initUI(self, pos_offset):
"Enter a fixed value or leave blank for automatic wavelet power scaling"
)

Tmin_lab = QLabel("Smallest period")
Tmin_lab = QLabel("Lowest period")
step_lab = QLabel("Number of periods")
Tmax_lab = QLabel("Highest period")
pow_max_lab = QLabel("Expected maximal power")
Expand Down Expand Up @@ -634,10 +634,10 @@ def set_wlet_pars(self):
# -- read all the QLineEdits --

text = self.Tmin_edit.text()
Tmin = text.replace(",", ".")
check, _, _ = vali.validate(Tmin, 0)
text = text.replace(",", ".")
check, _, _ = vali.validate(text, 0)
if self.debug:
print("Min periodValidator output:", check, "value:", Tmin)
print("Min periodValidator output:", check, "value:", text)
if check == 0:

msgBox = QMessageBox()
Expand All @@ -646,7 +646,21 @@ def set_wlet_pars(self):

return False

wlet_pars['Tmin'] = float(Tmin)
Tmin = float(text)

if Tmin < 2 * self.dt:

Tmin = 2 * self.dt
self.Tmin_edit.clear()
self.Tmin_edit.insert(str(Tmin))

msgBox = QMessageBox()
msgBox.setWindowTitle('Warning')
msg = f"Lowest period set to Nyquist limit: {Tmin} {self.time_unit}!"
msgBox.setText(msg)
msgBox.exec()

wlet_pars['Tmin'] = Tmin

step_num = self.nT_edit.text()
check, _, _ = posintV.validate(step_num, 0)
Expand Down Expand Up @@ -905,7 +919,7 @@ def doPlot(self):
ax2 = pl.draw_detrended(
ax1, time_vector=self.tvec, detrended=self.raw_signal - trend
)
ax2.legend(fontsize=pl.tick_label_size, loc='bottom left')
ax2.legend(fontsize=pl.tick_label_size, loc='lower left')
if envelope is not None and not self.cb_detrend.isChecked():
pl.draw_envelope(ax1, time_vector=self.tvec, envelope=envelope)

Expand Down
17 changes: 7 additions & 10 deletions pyboat/ui/start_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,17 +492,16 @@ def initUI(self):
config_grid.addWidget(pow_max_label, 3, 2)
config_grid.addWidget(self.pow_max_edit, 3, 3)

CancelButton = QPushButton("Cancel", self)
CancelButton.setToolTip("Discards changes")
CancelButton.clicked.connect(self.clicked_cancel)
CloseButton = QPushButton("Close", self)
CloseButton.clicked.connect(self.clicked_close)

OkButton = QPushButton("Set", self)
OkButton = QPushButton("Set!", self)
OkButton.setToolTip("Approves changes")
OkButton.clicked.connect(self.clicked_ok)
OkButton.clicked.connect(self.clicked_set)

button_box = QHBoxLayout()
button_box.addStretch(1)
button_box.addWidget(CancelButton)
button_box.addWidget(CloseButton)
button_box.addWidget(OkButton)
button_box.addStretch(1)
button_w = QWidget()
Expand Down Expand Up @@ -536,7 +535,7 @@ def initUI(self):

self.show()

def clicked_ok(self):
def clicked_set(self):

'''
Retrieves all input fields
Expand All @@ -561,10 +560,8 @@ def clicked_ok(self):
if self.debug:
for key in settings.allKeys():
print(f'Set: {key} -> {settings.value(key)}')

self.close()

def clicked_cancel(self):
def clicked_close(self):
self.close()

def load_settings(self):
Expand Down
4 changes: 2 additions & 2 deletions pyboat/ui/synth_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ def initUI(self):

# self.p_max.insert(str(20)) # leave blank

T_min_lab = QLabel("Smallest period")
T_min_lab = QLabel("Lowest period")
step_lab = QLabel("Number of periods")
T_max_lab = QLabel("Highest period")
p_max_lab = QLabel("Expected maximal power")
Expand Down Expand Up @@ -411,8 +411,8 @@ def initUI(self):
## Add Wavelet analyzer options to tab1.parameter_box layout

tab1.parameter_box.addRow(T_min_lab, self.T_min)
tab1.parameter_box.addRow(step_lab, self.step_num)
tab1.parameter_box.addRow(T_max_lab, self.T_max)
tab1.parameter_box.addRow(step_lab, self.step_num)
tab1.parameter_box.addRow(p_max_lab, self.p_max)
tab1.parameter_box.addRow(self.cb_use_detrended)
tab1.parameter_box.addRow(self.cb_use_envelope)
Expand Down

0 comments on commit 4f32de8

Please sign in to comment.