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
# =========
try:
    from LF_scanner.pypulseq.SAR.SAR_calc import calc_SAR
except Exception as _sar_import_error:
    # Optional dependency (matplotlib) may be missing; keep core sequence API usable.
    def calc_SAR(*args, **kwargs):
        raise ImportError(
            "calc_SAR is unavailable because optional SAR dependencies are missing"
        ) from _sar_import_error

from LF_scanner.pypulseq.Sequence.sequence import Sequence
from LF_scanner.pypulseq.calc_duration import calc_duration
from LF_scanner.pypulseq.check_timing import check_timing
from LF_scanner.pypulseq.make_adc import make_adc
from LF_scanner.pypulseq.make_delay import make_delay
from LF_scanner.pypulseq.make_sinc_pulse import make_sinc_pulse
from LF_scanner.pypulseq.make_trapezoid import make_trapezoid
from LF_scanner.pypulseq.opts import Opts
