def _stats(self, distribution_f0): distribution_f0 = Hvsr.correct_distribution(distribution_f0) if distribution_f0 == "lognormal": columns = ["Lognormal Median", "Lognormal Standard Deviation"] data = np.array([[ self.mean_f0_frq(distribution_f0), self.std_f0_frq(distribution_f0) ], [ 1 / self.mean_f0_frq(distribution_f0), self.std_f0_frq(distribution_f0) ]]) elif distribution_f0 == "normal": columns = ["Means", "Standard Deviation"] data = np.array([[ self.mean_f0_frq(distribution_f0), self.std_f0_frq(distribution_f0) ], [np.nan, np.nan]]) else: msg = f"`distribution_f0` of {distribution_f0} is not implemented." raise NotImplementedError(msg) df = DataFrame(data=data, columns=columns, index=[ "Fundamental Site Frequency, f0,AZ", "Fundamental Site Period, T0,AZ" ]) return df
def _mean_factory(distribution, values, **kwargs): distribution = Hvsr.correct_distribution(distribution) if distribution == "normal": mean = np.mean([np.mean(x, **kwargs) for x in values], **kwargs) return mean elif distribution == "lognormal": mean = np.mean([np.mean(np.log(x), **kwargs) for x in values], **kwargs) return np.exp(mean) else: msg = f"distribution type {distribution} not recognized." raise NotImplementedError(msg)
def _std_factory(distribution, values, **kwargs): distribution = Hvsr.correct_distribution(distribution) n = len(values) mean = HvsrRotated._mean_factory(distribution, values, **kwargs) num = 0 wi2 = 0 if distribution == "normal": def _diff(value, mean): return value - mean elif distribution == "lognormal": def _diff(value, mean): return np.log(value) - np.log(mean) for value in values: i = len(value) diff = _diff(value, mean) wi = 1/(n*i) num += np.sum(diff*diff*wi, **kwargs) wi2 += wi*wi*i return np.sqrt(num/(1-wi2))
def _hvsrpy_style_lines(self, distribution_f0, distribution_mc): """Lines for hvsrpy-style file.""" # Correct distribution distribution_f0 = Hvsr.correct_distribution(distribution_f0) distribution_mc = Hvsr.correct_distribution(distribution_mc) # `f0` from windows mean_f = self.mean_f0_frq(distribution_f0) sigm_f = self.std_f0_frq(distribution_f0) ci_68_lower_f = self.nstd_f0_frq(-1, distribution_f0) ci_68_upper_f = self.nstd_f0_frq(+1, distribution_f0) # mean curve mc = self.mean_curve(distribution_mc) mc_peak_frq = self.mc_peak_frq(distribution_mc) mc_peak_amp = self.mc_peak_amp(distribution_mc) _min = self.nstd_curve(-1, distribution_mc) _max = self.nstd_curve(+1, distribution_mc) rejection = "False" if self.meta.get( 'Performed Rejection') is None else "True" nseries = self.hvsrs[0].nseries n_accepted = sum( [sum(hvsr.valid_window_indices) for hvsr in self.hvsrs]) n_rejected = self.azimuth_count * nseries - n_accepted lines = [ f"# hvsrpy output version {__version__}", f"# File Name (),{self.meta.get('File Name')}", f"# Window Length (s),{self.meta.get('Window Length')}", f"# Total Number of Windows per Azimuth (),{nseries}", f"# Total Number of Azimuths (),{self.azimuth_count}", f"# Frequency Domain Window Rejection Performed (),{rejection}", f"# Lower frequency limit for peaks (Hz),{self.hvsrs[0].f_low}", f"# Upper frequency limit for peaks (Hz),{self.hvsrs[0].f_high}", f"# Number of Standard Deviations Used for Rejection () [n],{self.meta.get('n')}", f"# Number of Accepted Windows (),{n_accepted}", f"# Number of Rejected Windows (),{n_rejected}", f"# Distribution of f0 (),{distribution_f0}" ] def fclean(number, decimals=4): return np.round(number, decimals=decimals) if distribution_f0 == "lognormal": mean_t = 1 / mean_f sigm_t = sigm_f ci_68_lower_t = np.exp(np.log(mean_t) - sigm_t) ci_68_upper_t = np.exp(np.log(mean_t) + sigm_t) lines += [ f"# Median f0 (Hz) [LMf0AZ],{fclean(mean_f)}", f"# Lognormal standard deviation f0 () [SigmaLNf0AZ],{fclean(sigm_f)}", f"# 68 % Confidence Interval f0 (Hz),{fclean(ci_68_lower_f)},to,{fclean(ci_68_upper_f)}", f"# Median T0 (s) [LMT0AZ],{fclean(mean_t)}", f"# Lognormal standard deviation T0 () [SigmaLNT0AZ],{fclean(sigm_t)}", f"# 68 % Confidence Interval T0 (s),{fclean(ci_68_lower_t)},to,{fclean(ci_68_upper_t)}", ] else: lines += [ f"# Mean f0 (Hz) [f0AZ],{fclean(mean_f)}", f"# Standard deviation f0 (Hz) [Sigmaf0AZ],{fclean(sigm_f)}", f"# 68 % Confidence Interval f0 (Hz),{fclean(ci_68_lower_f)},to,{fclean(ci_68_upper_f)}", f"# Mean T0 (s) [LMT0AZ],NAN", f"# Standard deviation T0 () [SigmaT0AZ],NAN", f"# 68 % Confidence Interval T0 (s),NAN", ] c_type = "Median" if distribution_mc == "lognormal" else "Mean" lines += [ f"# {c_type} Curve Distribution (),{distribution_mc}", f"# {c_type} Curve Peak Frequency (Hz) [f0mcAZ],{fclean(mc_peak_frq)}", f"# {c_type} Curve Peak Amplitude (),{fclean(mc_peak_amp)}", f"# Frequency (Hz),{c_type} Curve,1 STD Below {c_type} Curve,1 STD Above {c_type} Curve", ] _lines = [] for line in lines: _lines.append(line + "\n") for f_i, mean_i, bel_i, abv_i in zip(fclean(self.frq), fclean(mc), fclean(_min), fclean(_max)): _lines.append(f"{f_i},{mean_i},{bel_i},{abv_i}\n") return _lines