From 42a1af11f7e3d93aaab70d280e043c0af145121c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 25 Dec 2023 20:00:52 +0000 Subject: [PATCH] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- docs/guide/tutorial.rst | 2 +- requirements_dev.txt | 10 +-- setup.py | 4 +- src/femto/pgmcompiler.py | 3 +- src/femto/spreadsheet.py | 85 +++++++++---------------- src/femto/trench.py | 2 +- src/femto/utils/spreadsheet_columns.txt | 4 +- src/femto/writer.py | 3 +- tests/device_test.py | 3 +- tests/helpers_test.py | 14 ++-- tests/spreadsheet_test.py | 23 ++----- tests/trench_test.py | 10 +-- tests/writer_test.py | 2 +- 13 files changed, 65 insertions(+), 100 deletions(-) diff --git a/docs/guide/tutorial.rst b/docs/guide/tutorial.rst index ae23677..b23b9e5 100644 --- a/docs/guide/tutorial.rst +++ b/docs/guide/tutorial.rst @@ -1,2 +1,2 @@ Tutorial -======== \ No newline at end of file +======== diff --git a/requirements_dev.txt b/requirements_dev.txt index c86a48a..224c90c 100644 --- a/requirements_dev.txt +++ b/requirements_dev.txt @@ -1,8 +1,8 @@ +black>=22.10.0 flake8>=3.9.2 -tox>=3.24.3 -pytest>=6.2.5 -pytest-cov>=2.12.1 mypy>=0.910 -black>=22.10.0 -pre-commit>=2.1.0 openpyxl>=3.0.10 +pre-commit>=2.1.0 +pytest>=6.2.5 +pytest-cov>=2.12.1 +tox>=3.24.3 diff --git a/setup.py b/setup.py index 7f1a176..1ce554a 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,6 @@ +from __future__ import annotations + from setuptools import setup -if __name__ == "__main__": +if __name__ == '__main__': setup() diff --git a/src/femto/pgmcompiler.py b/src/femto/pgmcompiler.py index d2ea6f1..0fd8ac2 100644 --- a/src/femto/pgmcompiler.py +++ b/src/femto/pgmcompiler.py @@ -28,7 +28,6 @@ @dataclasses.dataclass(repr=False) class PGMCompiler: - filename: str #: File name of the PGM file. laser: str = 'PHAROS' #: Name of the laser source. export_dir: str = '' #: Directory in which the G-Code files will be exported. @@ -745,7 +744,7 @@ def write(self, points: npt.NDArray[np.float32]) -> None: # Convert points if G-Code commands args = [self._format_args(x, y, z, f) for (x, y, z, f) in zip(x_gc, y_gc, z_gc, f_gc)] - for (arg, s) in itertools.zip_longest(args, s_gc): + for arg, s in itertools.zip_longest(args, s_gc): if s == 0 and self._shutter_on is True: self.instruction('\n') self.dwell(self.short_pause) diff --git a/src/femto/spreadsheet.py b/src/femto/spreadsheet.py index c4d0496..29fcb28 100644 --- a/src/femto/spreadsheet.py +++ b/src/femto/spreadsheet.py @@ -1,34 +1,31 @@ from __future__ import annotations import time - -from xlsxwriter import Workbook - +from dataclasses import dataclass from pathlib import Path - -from femto import __file__ as fpath -import numpy as np - from types import TracebackType -from femto.waveguide import Waveguide -from femto.marker import Marker -from femto.helpers import flatten -from femto.writer import WaveguideWriter, MarkerWriter - +from typing import Any from typing import cast from typing import Union -from typing import Any -import nptyping as nptyp - -from dataclasses import dataclass import femto.device +import nptyping as nptyp +import numpy as np +from femto import __file__ as fpath +from femto.helpers import flatten +from femto.marker import Marker +from femto.waveguide import Waveguide +from femto.writer import MarkerWriter +from femto.writer import WaveguideWriter +from xlsxwriter import Workbook -def generate_all_cols_data() -> nptyp.NDArray[ - Any, - nptyp.Structure["tagname: Str, fullname: Str, unit: Str, width: Int, format: Str"], -]: +def generate_all_cols_data() -> ( + nptyp.NDArray[ + Any, + nptyp.Structure[tagname:Str, fullname:Str, unit:Str, width:Int, format:Str], + ] +): """ Create the available columns array from a file. @@ -81,7 +78,6 @@ def get_path(key, d, path=None, prev_k=None): nothing = True if hasattr(d, 'items'): for k, v in d.items(): - if k == key: prev_k.append(k) path.append(' '.join(prev_k)) @@ -113,7 +109,7 @@ class Parameter: """Class that handles preamble parameters.""" n: str # Full name - v: str = "" # Value + v: str = '' # Value loc: tuple[int, int] = (0, 0) # Location (1-indexing) sz: tuple[int, int] = (0, 0) # Size (for merged cells) fmt: str = 'parval' # Format @@ -135,10 +131,10 @@ class Spreadsheet: """Class representing the spreadsheet with all entities to fabricate.""" device: femto.device.Device | None = None - columns_names: str = "" - book_name: str | Path = "my_fabrication.xlsx" - sheet_name: str = "Fabrication" - font_name: str = "DejaVu Sans Mono" + columns_names: str = '' + book_name: str | Path = 'my_fabrication.xlsx' + sheet_name: str = 'Fabrication' + font_name: str = 'DejaVu Sans Mono' font_size: int = 11 suppr_redd_cols: bool = True static_preamble: bool = False @@ -203,12 +199,10 @@ def __post_init__( self.columns_names = scn self.suppr_redd_cols = True print( - ( - 'Columns_names not given in spreadsheet initialization.' - f' Will proceed with standard columns names \'{scn}\' ' - 'and activate the suppr_redd_cols flag to deal with ' - 'reddundant columns.' - ) + 'Columns_names not given in spreadsheet initialization.' + f' Will proceed with standard columns names \'{scn}\' ' + 'and activate the suppr_redd_cols flag to deal with ' + 'reddundant columns.' ) if 'name' not in self.columns_names: @@ -223,7 +217,6 @@ def __post_init__( ac = generate_all_cols_data() if self.new_columns: - for elem in self.new_columns: tns = ac['tagname'] if elem[0] in tns: @@ -370,7 +363,7 @@ def _write_saints_list(self, column: int = 156) -> None: None. """ - with open(Path(fpath).parent / 'utils' / 'saints_data.txt', 'r') as f: + with open(Path(fpath).parent / 'utils' / 'saints_data.txt') as f: for i in range(367): s = f.readline().strip('\n') # print(f'writing day {i}\t{s}') @@ -412,10 +405,7 @@ def _dtype(self, tagname): return dt - def _get_structure_list( - self, str_list: list[Union[Waveguide, Marker]] | None = None - ) -> list[Union[Waveguide, Marker]]: - + def _get_structure_list(self, str_list: list[Waveguide | Marker] | None = None) -> list[Waveguide | Marker]: assert isinstance(self.device, femto.device.Device) if str_list is None: @@ -514,11 +504,9 @@ def _build_struct_list( table_lines = np.zeros(n_structures, dtype=dtype) for i, ent in enumerate(structures): - sline = [] for t in tagnames: - if t in 'yin yout': item = coords(ent)[type(ent)][t] else: @@ -536,7 +524,6 @@ def _build_struct_list( ignored_fields = [] for i, t in enumerate(tagnames): - if t.lower() == 'name': # the name of the structure is always present keep.append(i) @@ -546,7 +533,7 @@ def _build_struct_list( ignored_fields.append(t) continue - if np.all(table_lines[t] == table_lines[t][0]) and suppr_redd_cols and table_lines[t][0] != "": + if np.all(table_lines[t] == table_lines[t][0]) and suppr_redd_cols and table_lines[t][0] != '': # eliminate reddundancies if explicitly requested ignored_fields.append(t) @@ -570,10 +557,8 @@ def _build_struct_list( if ignored_fields and verbose: fields_left_out = ', '.join(ignored_fields) print( - ( - f'For all entities, the fields {fields_left_out} were not ' - 'defined, so they will not be shown as table columns.' - ) + f'For all entities, the fields {fields_left_out} were not ' + 'defined, so they will not be shown as table columns.' ) self.keep = keep @@ -594,7 +579,6 @@ def _prepare_columns(self, columns=None) -> None: self.columns_data = columns for i, col in enumerate(columns): - fmt = col['format'] w = col['width'] @@ -643,7 +627,6 @@ def _create_numerical_format(self, fmt_string) -> None: self.formats[fmt_string] = wb.add_format({'align': 'center', 'valign': 'vcenter', 'num_format': fmt_string}) def _fill_spreadsheet(self): - self.struct_data = self.struct_data[self.columns_data['tagname']] self.ws.set_row(7, 50) @@ -654,7 +637,6 @@ def _fill_spreadsheet(self): self._add_line((7, 5), titles, fmt='title') for i, sdata in enumerate(self.struct_data): - sdata = [ s if (isinstance(s, (np.int64, np.float64)) and s < 1e5) or (not isinstance(s, (np.int64, np.float64))) @@ -669,7 +651,6 @@ def _fill_spreadsheet(self): ) def _write_header(self) -> None: - ws = self.ws ws.set_column(1, 1, 15) @@ -686,7 +667,6 @@ def _write_header(self) -> None: ws.merge_range(2, 3, 2, nc_f + 4, self.description, self.formats['parval']) def _write_preamble(self) -> None: - ws = self.ws row = 8 @@ -703,7 +683,6 @@ def _write_preamble(self) -> None: for sg, parameters in self.preamble.dict.items(): for tname, p in parameters.items(): - if np.any(p.sz): ws.merge_range(*p.loc, *(p.loc + p.sz), '', self.formats['parname']) @@ -768,7 +747,6 @@ def _add_line( def main() -> None: - import numpy as np from itertools import product from femto.device import Device @@ -809,7 +787,6 @@ def main() -> None: all_fabb = Device(**PARS_GC) for i_guide, (p, v, ns) in enumerate(product(powers, speeds, scans)): - start_pt = [LS, 2 + i_guide * 0.08, PARS_WG.depth] wg = Waveguide(**PARS_WG, speed=v, scan=ns) # wg.power = p # Can NOT be added inside of the arguments of Waveguide diff --git a/src/femto/trench.py b/src/femto/trench.py index f3d964f..0674ab0 100644 --- a/src/femto/trench.py +++ b/src/femto/trench.py @@ -736,7 +736,7 @@ def main() -> None: # b = T._trench_list[0] # b = T.trenchbed[0] for tr in T._trench_list: - for (x, y) in tr.toolpath(): + for x, y in tr.toolpath(): plt.plot(x, y) plt.axis('equal') diff --git a/src/femto/utils/spreadsheet_columns.txt b/src/femto/utils/spreadsheet_columns.txt index 383578a..ac06cef 100644 --- a/src/femto/utils/spreadsheet_columns.txt +++ b/src/femto/utils/spreadsheet_columns.txt @@ -7,9 +7,9 @@ speed, Speed, mm/s, 6, 0.0 scan, Scans, , 5, 0 depth, Depth, mm, 8, 0.000 radius, R, mm, 5, 0.00 -int_dist, Cdist, um, 8, 0.0000 +int_dist, Cdist, um, 8, 0.0000 wl, λ, um, 8, 0.000 -int_length, Clength, mm, 8, 0.000 +int_length, Clength, mm, 8, 0.000 arm_length, Alength, mm, 8, 0.000 yin, Yin, mm, 8, 0.0000 yout, Yout, mm, 8, 0.0000 diff --git a/src/femto/writer.py b/src/femto/writer.py index 1ecfaa7..bedbb70 100644 --- a/src/femto/writer.py +++ b/src/femto/writer.py @@ -535,7 +535,7 @@ def export_array2d( ] gcode_instr = [] - for (line, dec) in itertools.zip_longest(instr, listcast(forced_deceleration)): + for line, dec in itertools.zip_longest(instr, listcast(forced_deceleration)): if bool(dec): gcode_instr.append(f'G9 G1 {line}\n') else: @@ -810,7 +810,6 @@ def _export_trench_column(self, column: UTrenchColumn, column_path: pathlib.Path # Bed script for i, bed_block in enumerate(column.trenchbed): - x_bed_block = np.array([]) y_bed_block = np.array([]) f_decel = np.array([]) diff --git a/tests/device_test.py b/tests/device_test.py index 2f50870..295a13a 100644 --- a/tests/device_test.py +++ b/tests/device_test.py @@ -53,7 +53,7 @@ def list_wg() -> list[Waveguide]: def list_mk() -> list[Marker]: PARAM_MK = dotdict(scan=1, speed=2, speed_pos=5, speed_closed=5, depth=0.000, lx=1, ly=1) markers = [] - for (x, y) in zip(range(4, 8), range(3, 7)): + for x, y in zip(range(4, 8), range(3, 7)): m = Marker(**PARAM_MK) m.cross([x, y]) markers.append(m) @@ -292,7 +292,6 @@ def test_plot3d_save_true(device, list_wg, list_mk, list_tcol) -> None: def test_device_pgm(device, list_wg, list_mk) -> None: - device.extend(list_wg) device.extend(list_mk) device.pgm() diff --git a/tests/helpers_test.py b/tests/helpers_test.py index 7d8a2a2..50ecb07 100644 --- a/tests/helpers_test.py +++ b/tests/helpers_test.py @@ -1,9 +1,10 @@ from __future__ import annotations +import pathlib + import numpy as np import pytest import yaml -import pathlib from femto.helpers import almost_equal from femto.helpers import dotdict from femto.helpers import flatten @@ -268,7 +269,6 @@ def test_dotdict(): def test_load_param_empty_default(): - PARAM_WG = dict( scan=6, speed=15, @@ -296,7 +296,7 @@ def test_load_param_empty_default(): PARAM_GC = dict(filename='UPP8.pgm', laser='PHAROS', aerotech_angle=0.0, rotation_angle=0.0) p_dicts = {'DEFAULT': {}, 'wg': PARAM_WG, 'mk': PARAM_MK, 'gc': PARAM_GC} - with open('test.yaml', "w") as f: + with open('test.yaml', 'w') as f: yaml.dump(p_dicts, f, sort_keys=False) pw, pm, pg = load_parameters('test.yaml') @@ -307,7 +307,6 @@ def test_load_param_empty_default(): def test_load_param_no_default(): - PARAM_WG = dict( scan=6, speed=15, @@ -340,7 +339,7 @@ def test_load_param_no_default(): ) p_dicts = {'wg': PARAM_WG, 'mk': PARAM_MK, 'gc': PARAM_GC} - with open('test.yaml', "w") as f: + with open('test.yaml', 'w') as f: yaml.dump(p_dicts, f, sort_keys=False) pw, pm, pg = load_parameters('test.yaml') @@ -351,7 +350,6 @@ def test_load_param_no_default(): def test_load_param_pathlib(): - PARAM_WG = dict( scan=6, speed=15, @@ -380,7 +378,7 @@ def test_load_param_pathlib(): p_dicts = {'wg': PARAM_WG, 'mk': PARAM_MK, 'gc': PARAM_GC} fp = pathlib.Path('test.yaml') - with open(fp, "w") as f: + with open(fp, 'w') as f: yaml.dump(p_dicts, f, sort_keys=False) pw, pm, pg = load_parameters(fp) @@ -393,7 +391,7 @@ def test_load_param_pathlib(): def test_load_param_empty(): p_dicts = {} fp = pathlib.Path('test.yaml') - with open(fp, "w") as f: + with open(fp, 'w') as f: yaml.dump(p_dicts, f, sort_keys=False) assert load_parameters(fp) == [] diff --git a/tests/spreadsheet_test.py b/tests/spreadsheet_test.py index 0310d6c..829fb0e 100644 --- a/tests/spreadsheet_test.py +++ b/tests/spreadsheet_test.py @@ -1,17 +1,18 @@ from __future__ import annotations -from femto import __file__ as fpath + +from contextlib import nullcontext as does_not_raise +from itertools import product +from pathlib import Path import numpy as np +import openpyxl import pytest +from femto import __file__ as fpath from femto.device import Device from femto.helpers import dotdict from femto.marker import Marker from femto.spreadsheet import Spreadsheet -from itertools import product from femto.waveguide import Waveguide -from pathlib import Path -import openpyxl -from contextlib import nullcontext as does_not_raise src_path = Path(fpath).parent dot_path = Path('.').cwd() @@ -59,7 +60,7 @@ def list_wg() -> list[Waveguide]: def list_mk() -> list[Marker]: PARAM_MK = dotdict(scan=1, speed=2, speed_pos=5, speed_closed=5, depth=0.000, lx=1, ly=1) markers = [] - for (x, y) in zip(range(4, 8), range(3, 7)): + for x, y in zip(range(4, 8), range(3, 7)): m = Marker(**PARAM_MK) m.cross([x, y]) markers.append(m) @@ -114,7 +115,6 @@ def device() -> Device: ) for i_guide, (p, v, ns) in enumerate(product(powers, speeds, scans)): - start_pt = [-2, 2 + i_guide * 0.08, wg_param.depth] wg = Waveguide(**wg_param, speed=v, scan=ns) wg.power = p # Can NOT be added inside of the arguments of Waveguide @@ -134,7 +134,6 @@ def empty_device() -> Device: def device_redd_cols(redd_cols, non_redd_cols, gc_param): - # redd_cols: the reddundant columns, so all guides have this same attribute dev = Device(**gc_param) @@ -143,7 +142,6 @@ def device_redd_cols(redd_cols, non_redd_cols, gc_param): i_global = 0 for i_guide in range(10): - non_rep_value = next(ints) start_pt = [-2, 2 + i_guide * 0.08, 0] @@ -194,7 +192,6 @@ def test_create_numerical_format(fmt_str, device, ss_param): def test_extra_preamble_info(all_cols, device, ss_param): - extra_preamble_info = { 'wl': '1030 nm', 'laboratory': 'CAPABLE', @@ -229,7 +226,6 @@ def test_extra_preamble_info(all_cols, device, ss_param): def test_add_custom_column(device, ss_param): - rep_rates = [20, 100, 1000] powers = np.linspace(100, 500, 5) scan_offsets = np.linspace(0.000100, 0.000500, 5) @@ -246,7 +242,6 @@ def test_add_custom_column(device, ss_param): ) for i_guide, (rr, p, so) in enumerate(product(rep_rates, powers, scan_offsets)): - start_pt = [-2, 2 + i_guide * 0.08, wg_param.depth] wg = Waveguide(**wg_param) @@ -328,7 +323,6 @@ def test_write_header(device, ss_param): ], ) def test_redd_cols(cols, ss_param, gc_param): - non_redd_cols, redd_cols = cols.split(', ') non_redd_cols = non_redd_cols.split() redd_cols = redd_cols.split() @@ -372,11 +366,9 @@ def test_write_structures(device, wg_param, gc_param, ss_param): ], ) def test_static_preamble(powers, speeds, scans, gc_param, wg_param, ss_param): - device = Device(**gc_param) for i_guide, (p, v, ns) in enumerate(product(powers, speeds, scans)): - start_pt = [-2, 2 + i_guide * 0.08, wg_param.depth] wg = Waveguide(**wg_param, speed=v, scan=ns) wg.power = p @@ -403,7 +395,6 @@ def test_static_preamble(powers, speeds, scans, gc_param, wg_param, ss_param): list(product(*2 * [[True, False]])), ) def test_device_init(device, ss_param, init_dev, bsl_dev): - ss_pars = ss_param bsl_pars = {} diff --git a/tests/trench_test.py b/tests/trench_test.py index fcb03e7..fde6f4d 100644 --- a/tests/trench_test.py +++ b/tests/trench_test.py @@ -263,7 +263,7 @@ def test_buffer_polygon(p, offset, p_expected) -> None: tc = Trench(block=Polygon()) buff_polygon = tc.buffer_polygon(p, offset) - for (p_calc, p_exp) in zip(buff_polygon, p_expected): + for p_calc, p_exp in zip(buff_polygon, p_expected): assert almost_equal(p_calc, p_exp) @@ -439,7 +439,7 @@ def test_trenchcol_dig() -> None: tc._dig(coords) - for (t, c) in zip(tc._trench_list, comp_box): + for t, c in zip(tc._trench_list, comp_box): assert normalize_polygon(c).equals_exact(t.block, tolerance=1e-8) assert almost_equal(normalize_polygon(c), t.block) @@ -461,7 +461,7 @@ def test_trenchcol_dig_remove() -> None: tc._dig(coords, remove=[1]) - for (t, c) in zip(tc._trench_list, comp_box): + for t, c in zip(tc._trench_list, comp_box): assert normalize_polygon(c).equals_exact(t.block, tolerance=1e-8) @@ -649,7 +649,7 @@ def test_u_dig() -> None: utc._dig(coords) - for (t, c) in zip(utc._trench_list, comp_box): + for t, c in zip(utc._trench_list, comp_box): assert normalize_polygon(c).equals_exact(t.block, tolerance=1e-8) assert almost_equal(normalize_polygon(c), t.block) assert utc.trenchbed is not [] @@ -675,7 +675,7 @@ def test_u_dig_no_pillars() -> None: utc._dig(coords) - for (t, c) in zip(utc._trench_list, comp_box): + for t, c in zip(utc._trench_list, comp_box): assert normalize_polygon(c).equals_exact(t.block, tolerance=1e-8) assert almost_equal(normalize_polygon(c), t.block) assert utc.trenchbed is not [] diff --git a/tests/writer_test.py b/tests/writer_test.py index 1a035ab..bfabe27 100644 --- a/tests/writer_test.py +++ b/tests/writer_test.py @@ -68,7 +68,7 @@ def list_ng() -> list[NasuWaveguide]: def list_mk() -> list[Marker]: PARAM_MK = dotdict(scan=1, speed=2, speed_pos=5, speed_closed=5, depth=0.000, lx=1, ly=1) markers = [] - for (x, y) in zip(range(4, 8), range(3, 7)): + for x, y in zip(range(4, 8), range(3, 7)): m = Marker(**PARAM_MK) m.cross([x, y]) markers.append(m)