import numpy as np
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), 'pypulseq')))

# =========
# BANKER'S ROUNDING FIX
# =========
def round_half_up(n, decimals=0):
    """
    Avoid banker's rounding inconsistencies; from https://realpython.com/python-rounding/#rounding-half-up
    """
    multiplier = 10**decimals
    return np.floor(np.abs(n) * multiplier + 0.5) / multiplier


# =========
# NP.FLOAT EPSILON
# =========
eps = np.finfo(np.float64).eps

# =========
# PACKAGE-LEVEL IMPORTS
# =========
from LF_scanner.pypulseq.Sequence.sequence import Sequence
from LF_scanner.pypulseq.add_gradients import add_gradients
from LF_scanner.pypulseq.align import align
from LF_scanner.pypulseq.calc_duration import calc_duration
from LF_scanner.pypulseq.calc_ramp import calc_ramp
from LF_scanner.pypulseq.calc_rf_bandwidth import calc_rf_bandwidth
from LF_scanner.pypulseq.calc_rf_center import calc_rf_center
from LF_scanner.pypulseq.make_adc import make_adc
from LF_scanner.pypulseq.make_arbitrary_rf import make_arbitrary_rf
from LF_scanner.pypulseq.make_block_pulse import make_block_pulse
from LF_scanner.pypulseq.make_delay import make_delay
from LF_scanner.pypulseq.make_digital_output_pulse import make_digital_output_pulse
from LF_scanner.pypulseq.make_extended_trapezoid import make_extended_trapezoid
from LF_scanner.pypulseq.make_extended_trapezoid_area import make_extended_trapezoid_area
from LF_scanner.pypulseq.make_gauss_pulse import make_gauss_pulse
from LF_scanner.pypulseq.make_label import make_label
from LF_scanner.pypulseq.make_sinc_pulse import make_sinc_pulse
from LF_scanner.pypulseq.make_trapezoid import make_trapezoid
from LF_scanner.pypulseq.sigpy_pulse_opts import SigpyPulseOpts
from LF_scanner.pypulseq.make_trigger import make_trigger
from LF_scanner.pypulseq.opts import Opts
from LF_scanner.pypulseq.points_to_waveform import points_to_waveform
from LF_scanner.pypulseq.rotate import rotate
from LF_scanner.pypulseq.scale_grad import scale_grad
from LF_scanner.pypulseq.split_gradient import split_gradient
from LF_scanner.pypulseq.split_gradient_at import split_gradient_at
from LF_scanner.pypulseq.supported_labels_rf_use import get_supported_labels
from LF_scanner.pypulseq.traj_to_grad import traj_to_grad

try:
    from LF_scanner.pypulseq.SAR.SAR_calc import calc_SAR
except Exception:
    calc_SAR = None

try:
    from LF_scanner.pypulseq.make_adiabatic_pulse import make_adiabatic_pulse
except Exception:
    make_adiabatic_pulse = None

try:
    from LF_scanner.pypulseq.make_sigpy_pulse import *
except Exception:
    pass
