Skip to content

Commit

Permalink
now using batched iteration over split string to save memory in frequ…
Browse files Browse the repository at this point in the history
…ency processing
  • Loading branch information
thomasWeise committed Oct 14, 2024
1 parent f1a8f29 commit 136c79c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
9 changes: 5 additions & 4 deletions moptipy/algorithms/so/ffa/ffa_h.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ def h_to_str(
>>> hl = np.array([0, 0, 1, 7, 4, 0, 0, 9, 0])
>>> h_to_str(hl, 0)
'2;1;;7;;4;7;9'
'2;;;7;;4;7;9'
>>> h_to_str(hl, -1)
'3;1;;7;;4;8;9'
'3;;;7;;4;8;9'
>>> hd = Counter((1, 1, 1, 1, 1, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3,
... 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2))
>>> h_to_str(hd, 0)
Expand All @@ -102,7 +102,7 @@ def h_to_str(
>>> hx[90] = 1
>>> hx[45] = 2314
>>> h_to_str(hx, 0)
'10;4;12;234;45;2314;89;111;;1'
'10;4;12;234;45;2314;89;111;;'
"""
if not isinstance(h, np.ndarray | dict):
raise type_error(h, "h", (np.ndarray, Counter))
Expand Down Expand Up @@ -134,7 +134,8 @@ def h_to_str(
write(str(use_index))
old_index = use_index # step the index
write(csep) # write separator to frequency counter
write(str(value)) # write the frequency
if value > 1:
write(str(value)) # write the frequency
res: Final[str] = out.getvalue() # get the final string

if str.__len__(res) <= 0:
Expand Down
14 changes: 8 additions & 6 deletions moptipy/evaluation/frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
import argparse
from collections import Counter
from gc import collect
from itertools import batched
from math import isfinite
from typing import Any, Callable, Final, Iterable

from pycommons.io.console import logger
from pycommons.io.csv import CSV_SEPARATOR, SCOPE_SEPARATOR
from pycommons.io.path import Path
from pycommons.strings.string_conv import str_to_num
from pycommons.strings.tools import split_str
from pycommons.types import type_error

from moptipy.algorithms.so.ffa.ffa_h import H_LOG_SECTION
Expand Down Expand Up @@ -460,16 +462,16 @@ def lines(self, lines: list[str]) -> bool:
self.__state_h = 2
counter = self.__counter
for line in lines:
split = line.split(CSV_SEPARATOR)
prev_f: int | float = -1
for i in range(0, len(split), 2):
cis = split[i]
if str.__len__(cis) <= 0:
for cur_idx_s, cur_h_s in batched(map(
str.strip, split_str(line, CSV_SEPARATOR)), 2):
if str.__len__(cur_idx_s) <= 0:
cur_f = prev_f + 1
else:
cur_f = str_to_num(cis)
cur_f = str_to_num(cur_idx_s)
prev_f = cur_f
counter[cur_f] += int(split[i + 1])
counter[cur_f] += (
int(cur_h_s) if str.__len__(cur_h_s) > 0 else 1)
else:
return super().lines(lines)

Expand Down
2 changes: 1 addition & 1 deletion moptipy/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from typing import Final

#: the version string of `moptipy`
__version__: Final[str] = "0.9.124"
__version__: Final[str] = "0.9.125"
2 changes: 1 addition & 1 deletion tests/algorithms/so/ffa/test_eafea_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,5 @@ def test_h_log() -> None:

lines = tf.read_all_str().splitlines()
assert lines[-1] == "END_H"
assert lines[-2] == "4;8;;9;;1"
assert lines[-2] == "4;8;;9;;"
assert lines[-3] == "BEGIN_H"
2 changes: 1 addition & 1 deletion tests/algorithms/so/ffa/test_fea1plus1.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,5 @@ def test_h_log() -> None:

lines = tf.read_all_str().splitlines()
assert lines[-1] == "END_H"
assert lines[-2] == "3;2;;3;;4;;4;;2;;2;;1"
assert lines[-2] == "3;2;;3;;4;;4;;2;;2;;"
assert lines[-3] == "BEGIN_H"

0 comments on commit 136c79c

Please sign in to comment.