Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: read blank lines and blank cells in CSV files #309

Merged
merged 4 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions models/directconst/data/h.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
c,C1,C2
B1,1,2
B2,3,ZERO
B3,5,6
h,
A,1
B,2
,
C,3
D,4
12 changes: 1 addition & 11 deletions models/directconst/directconst.dat
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,8 @@ g[C2,C1]
0 21
g[C2,C2]
0 22
h[B1,C1]
0 1
h[B1,C2]
0 2
h[B2,C1]
h
0 3
h[B2,C2]
0 0
h[B3,C1]
0 5
h[B3,C2]
0 6
INITIAL TIME
0 0
SAVEPER
Expand Down
15 changes: 7 additions & 8 deletions models/directconst/directconst.mdl
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,17 @@ g[From DimC, To DimC] =
'B2'
) ~~~:SUPPLEMENTARY|

h[DimB, DimC] =
h =
GET DIRECT CONSTANTS(
'data/h.csv',
',',
'B2'
'B5'
)
~
~ This csv file has a cell that does not contain a number so that we can check that
SDE does not throw an error in this case. Note that Vensim will raise an error if
a cell in the csv file does not contain a number, so to run this test in Vensim,
temporarily change cell C3 to 0 in `h.csv`.
~:SUPPLEMENTARY|
~
~ This csv file has a blank line. The formula will have the correct value
only if SDEverywhere retains blank lines.
~ :SUPPLEMENTARY
|

********************************************************
.Control
Expand Down
4 changes: 4 additions & 0 deletions models/directconst_invalidcell/data/c.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
c,C1,C2
B1,1,2
B2,3,ZERO
B3,5,6
21 changes: 21 additions & 0 deletions models/directconst_invalidcell/directconst_invalidcell.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
c[B1,C1]
0 1
c[B1,C2]
0 2
c[B2,C1]
0 3
c[B2,C2]
0 0
c[B3,C1]
0 5
c[B3,C2]
0 6
FINAL TIME
0 1
INITIAL TIME
0 0
SAVEPER
0 1
1 1
TIME STEP
0 1
51 changes: 51 additions & 0 deletions models/directconst_invalidcell/directconst_invalidcell.mdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{UTF-8}
DimB: B1, B2, B3 ~~|
DimC: C1, C2 ~~|

c[DimB, DimC] =
GET DIRECT CONSTANTS(
'data/c.csv',
',',
'B2'
)
~
~ This csv file has a cell that does not contain a number so that we can check that
SDE does not throw an error in this case. Note that Vensim will raise an error if
a cell in the csv file does not contain a number, so to run this test in Vensim,
temporarily change cell C3 to 0 in `c.csv`.
~:SUPPLEMENTARY|

********************************************************
.Control
********************************************************~
Simulation Control Parameters
|

INITIAL TIME = 0 ~~|
FINAL TIME = 1 ~~|
TIME STEP = 1 ~~|
SAVEPER = TIME STEP ~~|

\\\---/// Sketch information - do not modify anything except names
V300 Do not put anything below this section - it will be ignored
*View 1
$0-0-0,0,|0||0-0-0|0-0-0|0-0-0|0-0-0|0-0-0|0,0,100,0
///---\\\
:L<%^E!@
9:directconst
15:0,0,0,0,0,0
19:100,0
27:2,
34:0,
5:FINAL TIME
35:Date
36:YYYY-MM-DD
37:2000
38:1
39:1
40:2
41:0
42:1
24:0
25:0
26:0
4 changes: 4 additions & 0 deletions packages/cli/src/sde-compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ export let compare = async (vensimfile, sdefile, opts) => {
}
if (noDATDifference) {
pr(`Data were the same for ${vensimfile} and ${sdefile}`)
return true
} else {
pr(`Data differences detected for ${vensimfile} and ${sdefile}`)
return false
}
}
let isZero = value => {
Expand Down
7 changes: 6 additions & 1 deletion packages/cli/src/sde-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ export let test = async (model, opts) => {
let vensimPathname = path.join(modelDirname, `${modelName}.dat`)
let p = path.parse(logPathname)
let sdePathname = path.format({ dir: p.dir, name: p.name, ext: '.dat' })
await compare(vensimPathname, sdePathname, opts)
let noDiffs = await compare(vensimPathname, sdePathname, opts)
if (!noDiffs) {
// Exit with a non-zero error code if differences were detected
console.error()
process.exit(1)
}
} catch (e) {
// Exit with a non-zero error code if any step failed
console.error(`ERROR: ${e.message}\n`)
Expand Down
4 changes: 2 additions & 2 deletions packages/compile/src/_shared/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ export let readCsv = (pathname, delimiter = ',') => {
delimiter,
columns: false,
trim: true,
skip_empty_lines: true,
skip_records_with_empty_values: true
skip_empty_lines: false,
skip_records_with_empty_values: false
}
try {
let data = B.read(pathname)
Expand Down