Skip to content

Commit

Permalink
Fix error parsing SelectedEventFunctionParams from JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
bpuchala committed Dec 20, 2024
1 parent 67e64e2 commit 9323f4c
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to `libcasm-monte` will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed

- Fixed error parsing SelectedEventFunctionParams from JSON.


## [2.0a3] - 2024-12-11

### Added
Expand Down
90 changes: 90 additions & 0 deletions python/tests/sampling/test_SelectedEventFunctionParams.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import libcasm.monte.sampling as sampling


def test_SelectedEventFunctionParams():
params = sampling.SelectedEventFunctionParams()
assert isinstance(params, sampling.SelectedEventFunctionParams)

# test collect and do_not_collect
params.collect("event_type")
assert len(params.function_names) == 1
assert "event_type" in params.function_names

params.do_not_collect("event_type")
assert len(params.function_names) == 0


def test_SelectedEventFunctionParams_get_parameters():
params = sampling.SelectedEventFunctionParams()
assert isinstance(params, sampling.SelectedEventFunctionParams)

params.collect(
name="dE_activated",
initial_begin=0.01,
bin_width=1.0,
max_size=10,
spacing="linear",
)
data = params.get_parameters("dE_activated")
# print(xtal.pretty_json(data))
assert "initial_begin" in data
assert "bin_width" in data
assert "max_size" in data
assert "spacing" in data


def test_collect_hop_correlations():
params = sampling.SelectedEventFunctionParams()
assert isinstance(params, sampling.SelectedEventFunctionParams)

assert params.correlations_data_params is None

params.collect_hop_correlations(
jumps_per_position_sample=10,
)
assert isinstance(params.correlations_data_params, sampling.CorrelationsDataParams)
assert params.correlations_data_params.jumps_per_position_sample == 10

data = params.correlations_data_params.to_dict()
# print(xtal.pretty_json(data))

correlations_data_params_in = sampling.CorrelationsDataParams.from_dict(data)
assert isinstance(correlations_data_params_in, sampling.CorrelationsDataParams)

params.do_not_collect_hop_correlations()
assert params.correlations_data_params is None

params.collect_hop_correlations(**data)
assert isinstance(params.correlations_data_params, sampling.CorrelationsDataParams)
assert params.correlations_data_params.jumps_per_position_sample == 10


def test_SelectedEventFunctionParams_to_and_from_dict():
params = sampling.SelectedEventFunctionParams()

params.collect("event_type")
params.collect(
name="dE_activated",
initial_begin=0.01,
bin_width=1.0,
max_size=10,
spacing="linear",
)
params.collect_hop_correlations(
jumps_per_position_sample=10,
)
# print(params)

# test to_dict and from_dict
data = params.to_dict()
# print(xtal.pretty_json(data))
assert isinstance(data, dict)

params_in = sampling.SelectedEventFunctionParams.from_dict(data)
# print(params_in)
assert isinstance(params_in, sampling.SelectedEventFunctionParams)
assert len(params.function_names) == 2
assert "event_type" in params.function_names
assert "dE_activated" in params.function_names
assert isinstance(params.correlations_data_params, sampling.CorrelationsDataParams)
assert params.correlations_data_params.jumps_per_position_sample == 10
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ void parse(InputParser<SelectedEventFunctionParams> &parser) {
SelectedEventFunctionParams &params = *parser.value;

auto subparser =
parser.subparse<CorrelationsDataParams>("correlations_data_params");
if (subparser->valid()) {
parser.subparse_if<CorrelationsDataParams>("correlations_data_params");
if (subparser->valid() && subparser->value != nullptr) {
params.correlations_data_params = std::move(*subparser->value);
}

Expand Down

0 comments on commit 9323f4c

Please sign in to comment.