Skip to content

Commit

Permalink
imports moved to top of the file
Browse files Browse the repository at this point in the history
  • Loading branch information
kmilo9999 committed Feb 2, 2024
1 parent 1ad8502 commit 550193e
Showing 1 changed file with 9 additions and 26 deletions.
35 changes: 9 additions & 26 deletions src/icesat2_tracks/ICEsat2_SI_tools/generalized_FT.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import numpy as np
import matplotlib.pyplot as plt
from icesat2_tracks.ICEsat2_SI_tools import lanczos, spectral_estimates as spec
import xarray as xr
import matplotlib.pyplot as plt
import copy
import pandas as pd
import time
from scipy.signal import detrend
from numpy import linalg
import lmfit as LM
import icesat2_tracks.local_modules.JONSWAP_gamma as spectal_models


def rebin(data, dk):
Expand Down Expand Up @@ -119,8 +128,6 @@ def define_weight_shutter(weight, k, Ncut=3):


def make_xarray_from_dict(D, name, dims, coords):
import xarray as xr

D_return = dict()
for xi, I in D.items():
coords["x"] = xi
Expand Down Expand Up @@ -155,8 +162,6 @@ def define_weights(stancil, prior, x, y, dx, k, max_nfev, plot_flag=False):
weight = weight * define_weight_shutter(weight, k, Ncut=3)

if plot_flag:
import matplotlib.pyplot as plt

plt.plot(k, weight, zorder=12, c="darkgreen", linewidth=0.8, label=weight_name)

# peak normlize weights by std of data
Expand Down Expand Up @@ -226,9 +231,6 @@ def cal_spectrogram(
self.GG, params_dataframe
params_dataframe is a pd.DataFrame that containes all the parameters of the fitting process (and may contain uncertainties too once they are calculated)
"""
import xarray as xr
import copy
import pandas as pd

X = self.x if x is None else x # all x positions
DATA = self.data if data is None else data # all data points
Expand All @@ -244,8 +246,6 @@ def calc_gFT_apply(stancil, prior):
windows the data accoding to stencil and applies LS spectrogram
returns: stancil center, spectrum for this stencil, number of datapoints in stancil
"""
import matplotlib.pyplot as plt
import time

ta = time.perf_counter()
x_mask = (stancil[0] <= X) & (X <= stancil[-1])
Expand Down Expand Up @@ -274,8 +274,6 @@ def calc_gFT_apply(stancil, prior):
FT = generalized_Fourier(x, y, self.k)

if plot_flag:
import matplotlib.pyplot as plt

plt.figure(figsize=(3.34, 1.8), dpi=300)

# define weights. Weights are normalized to 1
Expand Down Expand Up @@ -628,14 +626,11 @@ def calc_var(self):

def parceval(self, add_attrs=True, weight_data=False):
"test Parceval theorem"
import copy

DATA = self.data
X = self.x

def get_stancil_var_apply(stancil):
from scipy.signal import detrend

"returns the variance of yy for stancil"
x_mask = (stancil[0] < X) & (X <= stancil[-1])
idata = DATA[x_mask]
Expand Down Expand Up @@ -749,7 +744,6 @@ def __init__(self, x, ydata, k):
"""
non_dimensionalize (bool, default=True) if True, then the data and R_data_uncertainty is non-dimensionalized by the std of the data
"""
import numpy as np

self.x, self.ydata, self.k = x, ydata, k
self.M = self.k.size # number of wavenumbers
Expand Down Expand Up @@ -784,8 +778,6 @@ def define_problem(self, P_weight, R_data_uncertainty):
self.R_1d = R_data_uncertainty

def solve(self):
from numpy import linalg

inv = linalg.inv
"""
solves the linear inverse problem, return hessian and p_hat
Expand Down Expand Up @@ -887,8 +879,6 @@ def get_stats(self, dk, Nx_full, print_flag=False):

class get_prior_spec:
def __init__(self, freq, data):
import lmfit as LM

self.LM = LM
self.data = data
self.freq = freq
Expand All @@ -910,7 +900,6 @@ def set_parameters(self, flim=None):
self.params LMfit.parameters class needed for optimization
"""
import numpy as np

params = self.LM.Parameters()

Expand Down Expand Up @@ -940,8 +929,6 @@ def model_func(self, f, params):
)

def non_dim_spec_model(self, f, f_max, amp, gamma=1, angle_rad=0):
import icesat2_tracks.local_modules.JONSWAP_gamma as spectal_models

f_true = f * np.cos(angle_rad)
model = spectal_models.JONSWAP_default_alt(f_true, f_max, 20, gamma=gamma)
model = amp * model / np.nanmean(model)
Expand Down Expand Up @@ -974,13 +961,9 @@ def optimize(self, fitting_args=None, method="dual_annealing", max_nfev=None):
return self.fitter

def plot_data(self):
import matplotlib.pyplot as plt

plt.plot(self.freq, self.data, "k")

def plot_model(self, pars):
import matplotlib.pyplot as plt

plt.plot(self.freq, self.model_func(self.freq, pars), "b--")

def runningmean(self, var, m, tailcopy=False):
Expand Down

0 comments on commit 550193e

Please sign in to comment.