Skip to content

Commit 68c9648

Browse files
committed
move to new pycommons version
1 parent c0ff15b commit 68c9648

File tree

8 files changed

+139
-207
lines changed

8 files changed

+139
-207
lines changed

moptipy/algorithms/so/vector/cmaes_lib.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
import numpy as np
4343
from cmaes import CMA, SepCMA # type: ignore
4444
from numpy.random import Generator
45-
from pycommons.strings.string_conv import num_to_str
45+
from pycommons.strings.string_conv import bool_or_num_to_str
4646
from pycommons.types import type_error
4747

4848
from moptipy.api.algorithm import Algorithm
@@ -309,7 +309,8 @@ def solve(self, process: Process) -> None:
309309
if restarts is not None:
310310
restarts.append((process.get_consumed_fes(),
311311
process.get_consumed_time_millis(),
312-
cma.population_size, seed, is_small_pop))
312+
int(cma.population_size), seed,
313+
is_small_pop))
313314
fes = _run_cma(cma, f, should_terminate, solutions,
314315
cma.should_stop)
315316
if fes < 0: # this means that should_terminate became True
@@ -342,9 +343,8 @@ def solve(self, process: Process) -> None:
342343
log: Final[list[str]] = [
343344
f"fes{CSV_SEPARATOR}timeMillis{CSV_SEPARATOR}popSize"
344345
f"{CSV_SEPARATOR}seed{CSV_SEPARATOR}isSmall"]
345-
for row in restarts:
346-
log.append(CSV_SEPARATOR.join(map(
347-
num_to_str, (x for x in row))))
346+
log.extend(CSV_SEPARATOR.join(map(
347+
bool_or_num_to_str, row)) for row in restarts)
348348
del restarts
349349
process.add_log_section("CMA_RESTARTS", "\n".join(log))
350350
del log

moptipy/evaluation/end_results.py

Lines changed: 23 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@
2424
SCOPE_SEPARATOR,
2525
csv_column,
2626
csv_column_or_none,
27-
csv_read,
2827
csv_scope,
2928
csv_str_or_none,
3029
csv_val_or_none,
31-
csv_write,
3230
pycommons_footer_bottom_comments,
3331
)
32+
from pycommons.io.csv import CsvReader as CsvReaderBase
33+
from pycommons.io.csv import CsvWriter as CsvWriterBase
3434
from pycommons.io.path import Path, file_path, line_writer
3535
from pycommons.strings.string_conv import (
3636
int_or_none_to_str,
@@ -553,14 +553,7 @@ def to_csv(results: Iterable[EndResult], file: str) -> Path:
553553
path.ensure_parent_dir_exists()
554554
with path.open_for_write() as wt:
555555
consumer: Final[Callable[[str], None]] = line_writer(wt)
556-
for r in csv_write(
557-
data=sorted(results),
558-
setup=CsvWriter().setup,
559-
column_titles=CsvWriter.get_column_titles,
560-
get_row=CsvWriter.get_row,
561-
header_comments=CsvWriter.get_header_comments,
562-
footer_comments=CsvWriter.get_footer_comments,
563-
footer_bottom_comments=CsvWriter.get_footer_bottom_comments):
556+
for r in CsvWriter.write(results):
564557
consumer(r)
565558
logger(f"Done writing end results to CSV file {path!r}.")
566559
return path
@@ -580,50 +573,24 @@ def from_csv(file: str,
580573
path: Final[Path] = file_path(file)
581574
logger(f"Now reading CSV file {path!r}.")
582575
with path.open_for_read() as rd:
583-
for r in csv_read(rows=rd,
584-
setup=CsvReader,
585-
parse_row=CsvReader.parse_row):
576+
for r in CsvReader.read(rd):
586577
if filterer(r):
587578
yield r
588579
logger(f"Done reading CSV file {path!r}.")
589580

590581

591-
class CsvWriter:
582+
class CsvWriter(CsvWriterBase):
592583
"""A class for CSV writing of :class:`EndResult`."""
593584

594-
def __init__(self, scope: str | None = None) -> None:
585+
def __init__(self, data: Iterable[EndResult],
586+
scope: str | None = None) -> None:
595587
"""
596588
Initialize the csv writer.
597589
590+
:param data: the data
598591
:param scope: the prefix to be pre-pended to all columns
599592
"""
600-
#: an optional scope
601-
self.scope: Final[str | None] = (
602-
str.strip(scope)) if scope is not None else None
603-
604-
#: has this writer been set up?
605-
self.__setup: bool = False
606-
#: do we need the encoding?
607-
self.__needs_encoding: bool = False
608-
#: do we need the max FEs?
609-
self.__needs_max_fes: bool = False
610-
#: do we need the max millis?
611-
self.__needs_max_ms: bool = False
612-
#: do we need the goal F?
613-
self.__needs_goal_f: bool = False
614-
615-
def setup(self, data: Iterable[EndResult]) -> "CsvWriter":
616-
"""
617-
Set up this csv writer based on existing data.
618-
619-
:param data: the data to setup with
620-
:returns: this writer
621-
"""
622-
if self.__setup:
623-
raise ValueError(
624-
"EndResults CsvWriter has already been set up.")
625-
self.__setup = True
626-
593+
super().__init__(data, scope)
627594
no_encoding: bool = True
628595
no_max_fes: bool = True
629596
no_max_ms: bool = True
@@ -632,30 +599,33 @@ def setup(self, data: Iterable[EndResult]) -> "CsvWriter":
632599
for er in data:
633600
if no_encoding and (er.encoding is not None):
634601
no_encoding = False
635-
self.__needs_encoding = True
636602
check -= 1
637603
if check <= 0:
638-
return self
604+
break
639605
if no_max_fes and (er.max_fes is not None):
640-
self.__needs_max_fes = True
641606
no_max_fes = False
642607
check -= 1
643608
if check <= 0:
644-
return self
609+
break
645610
if no_max_ms and (er.max_time_millis is not None):
646-
self.__needs_max_ms = True
647611
no_max_ms = False
648612
check -= 1
649613
if check <= 0:
650-
return self
614+
break
651615
if no_goal_f and (er.goal_f is not None) and (
652616
isfinite(er.goal_f)):
653-
self.__needs_goal_f = True
654617
no_goal_f = False
655618
check -= 1
656619
if check <= 0:
657-
return self
658-
return self
620+
break
621+
#: do we need the encoding?
622+
self.__needs_encoding: Final[bool] = not no_encoding
623+
#: do we need the max FEs?
624+
self.__needs_max_fes: Final[bool] = not no_max_fes
625+
#: do we need the max millis?
626+
self.__needs_max_ms: Final[bool] = not no_max_ms
627+
#: do we need the goal F?
628+
self.__needs_goal_f: Final[bool] = not no_goal_f
659629

660630
def get_column_titles(self) -> Iterable[str]:
661631
"""
@@ -768,7 +738,7 @@ def get_footer_bottom_comments(self) -> Iterable[str]:
768738
yield from pycommons_footer_bottom_comments(self)
769739

770740

771-
class CsvReader:
741+
class CsvReader(CsvReaderBase):
772742
"""A csv parser for end results."""
773743

774744
def __init__(self, columns: dict[str, int]) -> None:
@@ -777,9 +747,7 @@ def __init__(self, columns: dict[str, int]) -> None:
777747
778748
:param columns: the columns
779749
"""
780-
super().__init__()
781-
if not isinstance(columns, dict):
782-
raise type_error(columns, "columns", dict)
750+
super().__init__(columns)
783751
#: the index of the algorithm column, if any
784752
self.__idx_algorithm: Final[int] = csv_column(columns, KEY_ALGORITHM)
785753
#: the index of the instance column, if any

0 commit comments

Comments
 (0)