Skip to content

Commit

Permalink
gireformatted;
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaDuina committed Dec 19, 2024
1 parent 784f7a7 commit fe964f6
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 139 deletions.
2 changes: 1 addition & 1 deletion sqil_core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .utils import extract_h5_data, read_param_dict

__all__ = ['extract_h5_data', 'read_param_dict']
__all__ = ["extract_h5_data", "read_param_dict"]
6 changes: 3 additions & 3 deletions sqil_core/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .read import *
from .formatter import *
from .analysis import *
from .formatter import *
from .read import *

__all__ = []
__all__.extend(name for name in dir() if not name.startswith('_'))
__all__.extend(name for name in dir() if not name.startswith("_"))
2 changes: 0 additions & 2 deletions sqil_core/utils/const.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

EXP_UNIT_MAP = {
-15: "p",
-12: "f",
Expand Down Expand Up @@ -37,4 +36,3 @@
"vna_bw": {"name": "VNA bandwidth", "symbol": "BW_{VNA}", "unit": "Hz", "scale": 1},
"vna_avg": {"name": "VNA averages", "symbol": "avg_{VNA}", "unit": "", "scale": 1},
}

21 changes: 13 additions & 8 deletions sqil_core/utils/formatter.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from decimal import Decimal, ROUND_DOWN
from decimal import ROUND_DOWN, Decimal

import numpy as np

from .const import EXP_UNIT_MAP, PARAM_METADATA
from .read import read_json
from .const import PARAM_METADATA, EXP_UNIT_MAP


def _cut_to_significant_digits(number, n):
Expand All @@ -11,7 +12,9 @@ def _cut_to_significant_digits(number, n):
return 0 # Zero has no significant digits
d = Decimal(str(number))
shift = d.adjusted() # Get the exponent of the number
rounded = d.scaleb(-shift).quantize(Decimal('1e-{0}'.format(n-1)), rounding=ROUND_DOWN)
rounded = d.scaleb(-shift).quantize(
Decimal("1e-{0}".format(n - 1)), rounding=ROUND_DOWN
)
return float(rounded.scaleb(shift))


Expand Down Expand Up @@ -54,8 +57,12 @@ def format_number(
# Apply precision to the base
if precision < 3:
precision = 3
base_precise = _cut_to_significant_digits(base, precision+1) #np.round(base, precision - (int(exponent) % 3))
base_precise = np.round(base_precise, precision - len(str(base_precise).split('.')[0]))
base_precise = _cut_to_significant_digits(
base, precision + 1
) # np.round(base, precision - (int(exponent) % 3))
base_precise = np.round(
base_precise, precision - len(str(base_precise).split(".")[0])
)
if int(base_precise) == float(base_precise):
base_precise = int(base_precise)

Expand Down Expand Up @@ -86,9 +93,7 @@ def get_name_and_unit(param_id: str) -> str:
return f"{meta['name']} [{EXP_UNIT_MAP[exponent]}{meta['unit']}]"


def get_x_id_by_plot_dim(
exp_id: str, plot_dim: str, sweep_param: str | None
) -> str:
def get_x_id_by_plot_dim(exp_id: str, plot_dim: str, sweep_param: str | None) -> str:
if exp_id == "CW_onetone":
if plot_dim == "1":
return sweep_param or "ro_freq"
Expand Down
34 changes: 19 additions & 15 deletions sqil_core/utils/read.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import json
import os

import h5py
import numpy as np

from .const import PARAM_METADATA
import os


def extract_h5_data(
Expand Down Expand Up @@ -37,7 +37,7 @@ def extract_h5_data(
"""
# If the path is to a folder open /data.ddh5
if os.path.isdir(path):
path = os.path.join(path, 'data.ddh5')
path = os.path.join(path, "data.ddh5")

with h5py.File(path, "r") as h5file:
data = h5file["data"]
Expand Down Expand Up @@ -67,12 +67,14 @@ def _h5_to_dict(obj) -> dict:
data_dict[key] = extract_h5_data(item)
return data_dict


def read_json(path: str) -> dict:
"""Reads a json file and returns the data as a dictionary."""
with open(path) as f:
dictionary = json.load(f)
return dictionary


class ParamInfo:
"""Parameter information for items of param_dict
Expand All @@ -84,27 +86,28 @@ class ParamInfo:
unit (str): base unit of measurement (e.g. Hz)
scale (int): the scale that should be generally applied to raw data (e.g. 1e-9 to take raw Hz to GHz)
"""

def __init__(self, id, value):
self.id = id
self.value = value
if id in PARAM_METADATA:
meta = PARAM_METADATA[id]
else:
meta = {}
self.name = meta['name'] if 'name' in meta else id
self.symbol = meta['symbol'] if 'symbol' in meta else id
self.unit = meta['unit'] if 'unit' in meta else ''
self.scale = meta['scale'] if 'scale' in meta else 1
self.name = meta["name"] if "name" in meta else id
self.symbol = meta["symbol"] if "symbol" in meta else id
self.unit = meta["unit"] if "unit" in meta else ""
self.scale = meta["scale"] if "scale" in meta else 1

def to_dict(self):
"""Convert ParamInfo to a dictionary."""
return {
'id': self.id,
'value': self.value,
'name': self.name,
'symbol': self.symbol,
'unit': self.unit,
'scale': self.scale
"id": self.id,
"value": self.value,
"name": self.name,
"symbol": self.symbol,
"unit": self.unit,
"scale": self.scale,
}

def __str__(self):
Expand All @@ -118,11 +121,12 @@ def __eq__(self, other):
return self.value == other
return False


ParamDict = dict[str, ParamInfo | dict[str, ParamInfo]]


def _enrich_param_dict(param_dict: dict) -> ParamDict:
"""Add metadata to param_dict entries.
"""
"""Add metadata to param_dict entries."""
res = {}
for key, value in param_dict.items():
if isinstance(value, dict):
Expand All @@ -148,5 +152,5 @@ def read_param_dict(path: str) -> ParamDict:
"""
# If the path is to a folder open /param_dict.json
if os.path.isdir(path):
path = os.path.join(path, 'param_dict.json')
path = os.path.join(path, "param_dict.json")
return _enrich_param_dict(read_json(path))
144 changes: 93 additions & 51 deletions tests/utils/test_formatter.py
Original file line number Diff line number Diff line change
@@ -1,66 +1,108 @@
import pytest

from sqil_core.utils.formatter import *


class TestFormatNumber:
def test_correctly_formats_number_in_scientific_notation(self):
assert format_number(1, precision=3, unit='', latex=True) == '$1$'
assert format_number(12, precision=3, unit='', latex=True) == '$12$'
assert format_number(123, precision=3, unit='', latex=True) == '$123$'
assert format_number(1234, precision=3, unit='', latex=True) == '$1.23 x 10^{3}$'
assert format_number(12345, precision=3, unit='', latex=True) == '$12.3 x 10^{3}$'
assert format_number(123456, precision=3, unit='', latex=True) == '$123 x 10^{3}$'
assert format_number(1234567, precision=3, unit='', latex=True) == '$1.23 x 10^{6}$'
assert format_number(0.1, precision=3, unit='', latex=True) == '$100 x 10^{-3}$'
assert format_number(0.01, precision=3, unit='', latex=True) == '$10 x 10^{-3}$'
assert format_number(0.001, precision=3, unit='', latex=True) == '$1 x 10^{-3}$'
assert format_number(0.0001, precision=3, unit='', latex=True) == '$100 x 10^{-6}$'
assert format_number(1e6, precision=3, unit='', latex=True) == '$1 x 10^{6}$'
assert format_number(1e9, precision=3, unit='', latex=True) == '$1 x 10^{9}$'
assert format_number(1e-6, precision=3, unit='', latex=True) == '$1 x 10^{-6}$'
assert format_number(1e-9, precision=3, unit='', latex=True) == '$1 x 10^{-9}$'
assert format_number(1, precision=3, unit="", latex=True) == "$1$"
assert format_number(12, precision=3, unit="", latex=True) == "$12$"
assert format_number(123, precision=3, unit="", latex=True) == "$123$"
assert (
format_number(1234, precision=3, unit="", latex=True) == "$1.23 x 10^{3}$"
)
assert (
format_number(12345, precision=3, unit="", latex=True) == "$12.3 x 10^{3}$"
)
assert (
format_number(123456, precision=3, unit="", latex=True) == "$123 x 10^{3}$"
)
assert (
format_number(1234567, precision=3, unit="", latex=True)
== "$1.23 x 10^{6}$"
)
assert format_number(0.1, precision=3, unit="", latex=True) == "$100 x 10^{-3}$"
assert format_number(0.01, precision=3, unit="", latex=True) == "$10 x 10^{-3}$"
assert format_number(0.001, precision=3, unit="", latex=True) == "$1 x 10^{-3}$"
assert (
format_number(0.0001, precision=3, unit="", latex=True) == "$100 x 10^{-6}$"
)
assert format_number(1e6, precision=3, unit="", latex=True) == "$1 x 10^{6}$"
assert format_number(1e9, precision=3, unit="", latex=True) == "$1 x 10^{9}$"
assert format_number(1e-6, precision=3, unit="", latex=True) == "$1 x 10^{-6}$"
assert format_number(1e-9, precision=3, unit="", latex=True) == "$1 x 10^{-9}$"

def test_precision_controls_the_number_of_digits(self):
# Precision below 3 is not allowed
assert format_number(123456789, precision=0, unit='', latex=True) == '$123 x 10^{6}$'
assert format_number(123456789, precision=1, unit='', latex=True) == '$123 x 10^{6}$'
assert format_number(123456789, precision=2, unit='', latex=True) == '$123 x 10^{6}$'
assert format_number(123456789, precision=3, unit='', latex=True) == '$123 x 10^{6}$'
assert (
format_number(123456789, precision=0, unit="", latex=True)
== "$123 x 10^{6}$"
)
assert (
format_number(123456789, precision=1, unit="", latex=True)
== "$123 x 10^{6}$"
)
assert (
format_number(123456789, precision=2, unit="", latex=True)
== "$123 x 10^{6}$"
)
assert (
format_number(123456789, precision=3, unit="", latex=True)
== "$123 x 10^{6}$"
)
# Larger precisions
assert format_number(123456789, precision=4, unit='', latex=True) == '$123.4 x 10^{6}$'
assert format_number(123456789, precision=5, unit='', latex=True) == '$123.46 x 10^{6}$'
assert format_number(123456789, precision=6, unit='', latex=True) == '$123.457 x 10^{6}$'
assert format_number(123456789, precision=7, unit='', latex=True) == '$123.4568 x 10^{6}$'
assert format_number(123456789, precision=8, unit='', latex=True) == '$123.45679 x 10^{6}$'
assert format_number(123456789, precision=9, unit='', latex=True) == '$123.456789 x 10^{6}$'
assert (
format_number(123456789, precision=4, unit="", latex=True)
== "$123.4 x 10^{6}$"
)
assert (
format_number(123456789, precision=5, unit="", latex=True)
== "$123.46 x 10^{6}$"
)
assert (
format_number(123456789, precision=6, unit="", latex=True)
== "$123.457 x 10^{6}$"
)
assert (
format_number(123456789, precision=7, unit="", latex=True)
== "$123.4568 x 10^{6}$"
)
assert (
format_number(123456789, precision=8, unit="", latex=True)
== "$123.45679 x 10^{6}$"
)
assert (
format_number(123456789, precision=9, unit="", latex=True)
== "$123.456789 x 10^{6}$"
)

def test_correctly_adds_units(self):
assert format_number(1, unit='A', precision=3, latex=True) == '$1~A$'
assert format_number(12, unit='A', precision=3, latex=True) == '$12~A$'
assert format_number(123, unit='A', precision=3, latex=True) == '$123~A$'
assert format_number(1234, unit='A', precision=3, latex=True) == '$1.23~kA$'
assert format_number(12345, unit='A', precision=3, latex=True) == '$12.3~kA$'
assert format_number(123456, unit='A', precision=3, latex=True) == '$123~kA$'
assert format_number(1234567, unit='A', precision=3, latex=True) == '$1.23~MA$'
assert format_number(0.1, unit='A', precision=3, latex=True) == '$100~mA$'
assert format_number(0.01, unit='A', precision=3, latex=True) == '$10~mA$'
assert format_number(0.001, unit='A', precision=3, latex=True) == '$1~mA$'
assert format_number(0.0001, unit='A', precision=3, latex=True) == r'$100~\muA$'
assert format_number(1e6, unit='A', precision=3, latex=True) == '$1~MA$'
assert format_number(1e9, unit='A', precision=3, latex=True) == '$1~GA$'
assert format_number(1e-6, unit='A', precision=3, latex=True) == r'$1~\muA$'
assert format_number(1e-9, unit='A', precision=3, latex=True) == '$1~nA$'
assert format_number(1, unit="A", precision=3, latex=True) == "$1~A$"
assert format_number(12, unit="A", precision=3, latex=True) == "$12~A$"
assert format_number(123, unit="A", precision=3, latex=True) == "$123~A$"
assert format_number(1234, unit="A", precision=3, latex=True) == "$1.23~kA$"
assert format_number(12345, unit="A", precision=3, latex=True) == "$12.3~kA$"
assert format_number(123456, unit="A", precision=3, latex=True) == "$123~kA$"
assert format_number(1234567, unit="A", precision=3, latex=True) == "$1.23~MA$"
assert format_number(0.1, unit="A", precision=3, latex=True) == "$100~mA$"
assert format_number(0.01, unit="A", precision=3, latex=True) == "$10~mA$"
assert format_number(0.001, unit="A", precision=3, latex=True) == "$1~mA$"
assert format_number(0.0001, unit="A", precision=3, latex=True) == r"$100~\muA$"
assert format_number(1e6, unit="A", precision=3, latex=True) == "$1~MA$"
assert format_number(1e9, unit="A", precision=3, latex=True) == "$1~GA$"
assert format_number(1e-6, unit="A", precision=3, latex=True) == r"$1~\muA$"
assert format_number(1e-9, unit="A", precision=3, latex=True) == "$1~nA$"

def test_adds_latex_syntax(self):
assert format_number(1e-9, latex=True, unit='A', precision=3) == '$1~nA$'
assert format_number(1e-9, latex=False, unit='A', precision=3) == '1 nA'
assert format_number(1e-9, latex=True, unit='', precision=3) == '$1 x 10^{-9}$'
assert format_number(1e-9, latex=False, unit='', precision=3) == '1 x 10^{-9}'
assert format_number(1e9, latex=True, unit='', precision=3) == '$1 x 10^{9}$'
assert format_number(1e9, latex=False, unit='', precision=3) == '1 x 10^{9}'

assert format_number(1e-9, latex=True, unit="A", precision=3) == "$1~nA$"
assert format_number(1e-9, latex=False, unit="A", precision=3) == "1 nA"
assert format_number(1e-9, latex=True, unit="", precision=3) == "$1 x 10^{-9}$"
assert format_number(1e-9, latex=False, unit="", precision=3) == "1 x 10^{-9}"
assert format_number(1e9, latex=True, unit="", precision=3) == "$1 x 10^{9}$"
assert format_number(1e9, latex=False, unit="", precision=3) == "1 x 10^{9}"

def test_integers_remain_integers(self):
assert format_number(1) == '$1$'
assert format_number(2) == '$2$'
assert format_number(1, unit='a') == '$1~a$'
assert format_number(2, unit='a') == '$2~a$'
assert format_number(1) == "$1$"
assert format_number(2) == "$2$"
assert format_number(1, unit="a") == "$1~a$"
assert format_number(2, unit="a") == "$2~a$"
Loading

0 comments on commit fe964f6

Please sign in to comment.