From 6db197025ec9270c282415962b45199e5557fcb8 Mon Sep 17 00:00:00 2001 From: Sree Date: Sun, 14 Sep 2025 08:37:16 +1000 Subject: [PATCH 1/2] Update target.py The smoothing condition changed to a minimum of 2 Dnu for low numax stars. --- src/pysyd/target.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pysyd/target.py b/src/pysyd/target.py index 2a89599..45fec42 100644 --- a/src/pysyd/target.py +++ b/src/pysyd/target.py @@ -1595,8 +1595,8 @@ def numax_smooth(self, sm_par=None): sm_par = self.params['sm_par'] else: sm_par = 4.*(self.params['numax']/self.constants['numax_sun'])**0.2 - if sm_par < 1.: - sm_par = 1. + if sm_par < 2.: + sm_par = 2. sig = (sm_par*(self.params['dnu']/self.params['resolution']))/np.sqrt(8.0*np.log(2.0)) self.pssm = convolve_fft(np.copy(self.random_pow), Gaussian1DKernel(int(sig))) self.pssm_bgcorr = self.pssm-models.background(self.frequency, self.params['pars'], noise=self.params['noise']) From 828847801164f77edb342a19c135cef4e503ad95 Mon Sep 17 00:00:00 2001 From: Sreenivasan Date: Mon, 8 Dec 2025 15:44:40 +1100 Subject: [PATCH 2/2] Add files via upload --- src/pysyd/utils.py | 185 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 146 insertions(+), 39 deletions(-) diff --git a/src/pysyd/utils.py b/src/pysyd/utils.py index c346619..f3827a6 100644 --- a/src/pysyd/utils.py +++ b/src/pysyd/utils.py @@ -817,7 +817,8 @@ def _save_parameters(star): """ if star.params['save']: results = star.params['results']['parameters'] - df = pd.DataFrame(results) + print("results", results) + df = pd.DataFrame.from_dict(results,orient='index').transpose() star.df = df.copy() new_df = pd.DataFrame(columns=['parameter','value']) for c, col in enumerate(df.columns.values.tolist()): @@ -968,6 +969,35 @@ def _max_elements(x, y, npeaks, distance=None, exp_dnu=None): return list(peaks_x), list(peaks_y), weights +##def _return_max(x, y, exp_dnu=None,): +## """Return max +## +## Return the peak (and/or the index of the peak) in a given 2D array +## +## Parameters +## x, y : numpy.ndarray, numpy.ndarray +## the independent and dependent axis, respectively +## exp_dnu : Required[float] +## the expected dnu. Default value is `None`. +## +## Returns +## idx : int +## **New**: *always* returns the index first, followed by the corresponding peak from the x+y arrays +## xx[idx], yy[idx] : Union[int, float], Union[int, float] +## corresponding peak in the x and y arrays +## +## """ +## xx, yy = np.copy(x), np.copy(y) +## if list(yy) != []: +## if exp_dnu is not None: +## lst = list(np.absolute(xx-exp_dnu)) +## idx = lst.index(min(lst)) +## else: +## lst = list(yy) +## idx = lst.index(max(lst)) +## else: +## return None, np.nan, np.nan +## return idx, xx[idx], yy[idx] def _return_max(x, y, exp_dnu=None,): """Return max @@ -989,8 +1019,14 @@ def _return_max(x, y, exp_dnu=None,): xx, yy = np.copy(x), np.copy(y) if list(yy) != []: if exp_dnu is not None: - lst = list(np.absolute(xx-exp_dnu)) - idx = lst.index(min(lst)) + + new_idx = np.argmax(yy) + best_x = xx[new_idx] + if (best_x >= 0.75*exp_dnu) and (best_x <= 1.25*exp_dnu): + idx = new_idx + else: + lst = list(np.absolute(xx-exp_dnu)) + idx = lst.index(min(lst)) else: lst = list(yy) idx = lst.index(max(lst)) @@ -998,43 +1034,114 @@ def _return_max(x, y, exp_dnu=None,): return None, np.nan, np.nan return idx, xx[idx], yy[idx] - -def _bin_data(x, y, width, log=False, mode='mean'): - """Bin data +##def _bin_data(x, y, width, log=False, mode='mean'): +## """Bin data +## +## Bins 2D series of data +## +## Parameters +## x, y : numpy.ndarray, numpy.ndarray +## the x and y values of the data +## width : float +## bin width (typically in :math:`\\rm \\mu Hz`) +## log : bool, default=False +## creates equal bin sizes in logarithmic space when `True` +## +## Returns +## bin_x, bin_y, bin_yerr : numpy.ndarray, numpy.ndarray, numpy.ndarray +## binned arrays (and error is computed using the standard deviation) +## +## """ +## #fres= x[1]- x[0] +## #width = width/fres +## print("fres", fres) +## print("np", width/fres) +## #width = width/fres +## fres = np.median(np.diff(x)) +## print("bin size", fres) +## width = width*fres +## print("width", width) +## if log: +## +## mi = np.log10(min(x)) +## ma = np.log10(max(x)) +## no = int(np.ceil((max(x)-min(x))/width)) +## bins = np.logspace(mi, ma+width, no) +## print("no", no) +## else: +## print("yes") +## bins = np.arange(min(x), max(x)+width, width) +## +## digitized = np.digitize(x, bins) +## if mode == 'mean': +## bin_x = np.array([x[digitized == i].mean() for i in range(1, len(bins)) if len(x[digitized == i]) > 0]) +## bin_y = np.array([y[digitized == i].mean() for i in range(1, len(bins)) if len(x[digitized == i]) > 0]) +## elif mode == 'median': +## bin_x = np.array([np.median(x[digitized == i]) for i in range(1, len(bins)) if len(x[digitized == i]) > 0]) +## bin_y = np.array([np.median(y[digitized == i]) for i in range(1, len(bins)) if len(x[digitized == i]) > 0]) +## else: +## pass +## bin_yerr = np.array([y[digitized == i].std()/np.sqrt(len(y[digitized == i])) for i in range(1, len(bins)) if len(x[digitized == i]) > 0]) +## for i in range(len(bin_yerr)): +## if bin_yerr[i] == 0: +## bin_yerr[i] = 0.1*bin_y[i] +## print("from func", bin_x, bin_y, bin_yerr) +## return bin_x, bin_y, bin_yerr + + + +################## my version########################## + +def _bin_data(x, y, width, log, mode='mean'): + + if log is False: + + x = np.array(x) + y = np.array(y) + no = width + arr = np.arange(0, len(x), 1, dtype = int) + sect = len(arr)/no + #x_bin_start = 8 + #x_max = 12 + bin_x =[] + bin_y =[] + bin_yerr =[] + gh = np.array_split(arr, sect) + for i in gh: + num_bin = x[i] + frac_bin = y[i] + + bin_y.append(np.nanmean(frac_bin)) + bin_yerr.append(np.nanstd(frac_bin)/(np.sqrt(len(frac_bin)))) + bin_x.append((num_bin[0] + num_bin[-1])/2) + bin_x = np.array(bin_x) + bin_y = np.array(bin_y) + bin_yerr = np.array(bin_yerr) - Bins 2D series of data - - Parameters - x, y : numpy.ndarray, numpy.ndarray - the x and y values of the data - width : float - bin width (typically in :math:`\\rm \\mu Hz`) - log : bool, default=False - creates equal bin sizes in logarithmic space when `True` - - Returns - bin_x, bin_y, bin_yerr : numpy.ndarray, numpy.ndarray, numpy.ndarray - binned arrays (and error is computed using the standard deviation) - - """ - if log: - mi = np.log10(min(x)) - ma = np.log10(max(x)) - no = int(np.ceil((ma-mi)/width)) - bins = np.logspace(mi, mi+(no+1)*width, no) - else: - bins = np.arange(min(x), max(x)+width, width) - - digitized = np.digitize(x, bins) - if mode == 'mean': - bin_x = np.array([x[digitized == i].mean() for i in range(1, len(bins)) if len(x[digitized == i]) > 0]) - bin_y = np.array([y[digitized == i].mean() for i in range(1, len(bins)) if len(x[digitized == i]) > 0]) - elif mode == 'median': - bin_x = np.array([np.median(x[digitized == i]) for i in range(1, len(bins)) if len(x[digitized == i]) > 0]) - bin_y = np.array([np.median(y[digitized == i]) for i in range(1, len(bins)) if len(x[digitized == i]) > 0]) else: - pass - bin_yerr = np.array([y[digitized == i].std()/np.sqrt(len(y[digitized == i])) for i in range(1, len(bins)) if len(x[digitized == i]) > 0]) + + x = np.asarray(x) + y = np.asarray(y) + + # Create logarithmic bins + log_bins = np.logspace(np.log10(np.min(x)), np.log10(np.max(x)), num=len(x)//width + 1) + + # Initialize a list to hold binned values + binned_y = [] + binned_x = [] + binned_yerr = [] + + # Loop over the bins and collect points + for i in range(len(log_bins) - 1): + # Get indices of points in the current bin range + indices = np.where((x >= log_bins[i]) & (x < log_bins[i + 1]))[0] + binned_x.append((x[indices][-1] + x[indices][0])/2) + binned_y.append(np.nanmean(y[indices])) + binned_yerr.append(np.nanstd(y[indices])/np.sqrt(len(y[indices]))) + bin_x = np.array(binned_x) + bin_y = np.array(binned_y) + bin_yerr = np.array(binned_yerr) + return bin_x, bin_y, bin_yerr @@ -1268,4 +1375,4 @@ def get_output(fun=False): if len(sentence) > width: value = int(np.ceil((counts[0]-width)/2.)) sentence = sentence[value:-value] - print(sentence.center(width,' ')) \ No newline at end of file + print(sentence.center(width,' '))