allow to round values in CSVexporter - #5497
Conversation
25a9222 to
92ba4b8
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #5497 +/- ##
=========================================
Coverage 61.53% 61.53%
Complexity 14 14
=========================================
Files 2 2
Lines 78 78
Branches 4 4
=========================================
Hits 48 48
Misses 24 24
Partials 6 6 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Tick the box to add this pull request to the merge queue (same as
|
There was a problem hiding this comment.
Pull request overview
This PR begins implementing issue #131’s request to let users control numeric rounding in CSV exports by introducing a new decimalPlacesCount parameter on CSVExporter and adjusting tests/configuration accordingly.
Changes:
- Added a
decimalPlacesCountconstructor parameter toCSVExporterand used it to round exported values in one output branch. - Updated
TestCSVExporter’s numeric-matching regex for the “fixed decimals” export. - Updated a Protelis test YAML to pass
intervalanddecimalPlacesCounttoCSVExporter.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| alchemist-loading/src/main/kotlin/it/unibo/alchemist/boundary/exporters/CSVExporter.kt | Adds exporter-level decimal rounding support via a new parameter and a rounding helper. |
| alchemist-loading/src/test/kotlin/it/unibo/alchemist/test/TestCSVExporter.kt | Updates the regex used to validate decimal formatting in exported data. |
| alchemist-incarnation-protelis/src/test/resources/testbase.yml | Exercises the new exporter parameters in a test configuration. |
| when { | ||
| data.size <= 1 -> data.values.joinToString(" ") | ||
| data.size <= 1 -> | ||
| roundValues(data.values, decimalPlacesCount.toInt()).joinToString(" ") | ||
| // Labels and keys match | ||
| data.size == names.size && data.keys.containsAll(names) -> |
There was a problem hiding this comment.
@Nathekip, add a test with multiple columns, please, and if that it goes red, apply a fix!
Thank you, let me know
212bdd4 to
7b19587
Compare
131dcac to
dc16b4f
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.
Suppressed comments (3)
alchemist-loading/src/test/kotlin/it/unibo/alchemist/test/TestCSVExporter.kt:67
- This test change updates the output-matching regex, but it still doesn't exercise the newly added
decimalPlacesCountCSVExporter parameter (the YAML uses extractor-levelprecisioninstead). Consider adding/adjusting a test config so one exporter setsdecimalPlacesCountand asserts the resulting CSV formatting, otherwise regressions in the new rounding logic may go unnoticed.
"should have limited-length decimals" {
val limitedDecimalsFile = simulation.csvExporters()[1].dataFile("fixed-decimals_")
val precision2 = """(\d\d\.\d\d|0\.0*\d\d|\d\.0*\d|\d\.\d|\d\d)(e(-|\+)\d+)?"""
val lineRegex = Regex("""^$precision2(\s($precision2))+$""")
limitedDecimalsFile.useLines { lines ->
lines
.filterNot { it.startsWith("#") }
.forEach {
lineRegex shouldMatch it
}
alchemist-loading/src/main/kotlin/it/unibo/alchemist/boundary/exporters/CSVExporter.kt:156
decimalPlacesCount/decimalsNumberis not validated: passing a negative value will makeString.format("%.<n>f")throw at runtime (e.g.,UnknownFormatPrecisionException). Add a non-negative requirement once per call to avoid exporter crashes from misconfiguration.
private fun roundValues(values: Collection<String>, decimalsNumber: Int): Collection<String> = values.map { raw ->
val number = raw.toDoubleOrNull()
if (number == null || !number.isFinite() || number % 1.0 == 0.0) {
raw
} else {
String.format(Locale.US, "%.${decimalsNumber}f", number)
}
}
alchemist-loading/src/main/kotlin/it/unibo/alchemist/boundary/exporters/CSVExporter.kt:48
decimalPlacesCountdefaults to2, which changes CSVExporter output for existing YAML configs that don't specify this parameter (values will now be rounded by default). To avoid an implicit behavior change, consider making rounding opt-in (e.g.,decimalPlacesCount: Int? = null/ a separateroundValuesflag / a sentinel like-1meaning "disabled").
constructor(
private val fileNameRoot: String = "",
val interval: Double = DEFAULT_INTERVAL,
val decimalPlacesCount: Int = 2,
val exportPath: String =
66f7274 to
87f9890
Compare
|



This PR partially tackles the issue #131, specifically the ability for user to specify the amount of numbers they want after the decimal