Skip to content

Commit 7a089cb

Browse files
committed
test and formatting
1 parent 4aa5882 commit 7a089cb

File tree

4 files changed

+64
-5
lines changed

4 files changed

+64
-5
lines changed

somalier.nimble

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ bin = @["somalier"]
1818

1919
task test, "run the tests":
2020
exec "nim c -d:useSysAssert -d:useGcAssert --lineDir:on --debuginfo -r tests/test_groups"
21+
exec "nim c -d:useSysAssert -d:useGcAssert --lineDir:on --debuginfo -r tests/test_format_float_clean"
2122
exec "bash tests/functional-tests.sh"

src/somalierpkg/pedrel.nim

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,35 @@ import strutils
22
import strformat
33
import argparse
44
import pedfile
5-
import sequtils
6-
import algorithm
5+
6+
proc formatFloatClean*(f: float32): string =
7+
## Format a float32 as string, removing trailing zeros after decimal point
8+
## but keeping at least one zero if the number is whole (e.g., "1.0" not "1.")
9+
# Use precision=6 to avoid floating-point precision artifacts
10+
let formatted = formatFloat(f, ffDecimal, precision=6)
11+
12+
# Find the decimal point
13+
let decimalPos = formatted.find('.')
14+
if decimalPos == -1:
15+
return formatted # No decimal point, return as is
16+
17+
# Remove trailing zeros from the decimal part
18+
var cleaned = formatted
19+
while cleaned.len > decimalPos + 1 and cleaned[^1] == '0':
20+
cleaned = cleaned[0..^2] # Remove last character
21+
22+
# If we removed all decimal digits, add one back to keep "1.0" format
23+
if cleaned[^1] == '.':
24+
cleaned &= "0"
25+
26+
return cleaned
727

828
proc pedrel_main*() =
929
var argv = commandLineParams()
1030
if argv[0] == "pedrel":
1131
argv = argv[1..argv.high]
32+
if len(argv) == 0:
33+
argv = @["-h"]
1234

1335
var p = newParser("somalier pedrel"):
1436
help("report pairwise relationships from pedigree file")
@@ -47,13 +69,14 @@ proc pedrel_main*() =
4769
let sampleB = samples[j]
4870
let rel = sampleA.relatedness(sampleB)
4971

50-
if rel > min_rel:
72+
if rel >= min_rel:
5173
# Ensure consistent ordering (alphabetical by sample ID)
5274
let (sample1, sample2) = if sampleA.id < sampleB.id:
5375
(sampleA.id, sampleB.id)
5476
else:
5577
(sampleB.id, sampleA.id)
56-
output_file.write_line(&"{sample1}\t{sample2}\t{rel:.6f}")
78+
let relf = formatFloatClean(rel)
79+
output_file.write_line(&"{sample1}\t{sample2}\t{relf}")
5780

5881
if opts.output != "stdout":
5982
output_file.close()

src/somalierpkg/version.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
let somalierVersion* = "0.3.0"
1+
let somalierVersion* = "0.3.1"

tests/test_format_float_clean.nim

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import unittest
2+
3+
# Import the function from pedrel module
4+
import ../src/somalierpkg/pedrel
5+
6+
suite "formatFloatClean function":
7+
test "handles decimal numbers correctly":
8+
check formatFloatClean(1.234'f32) == "1.234"
9+
check formatFloatClean(1.2'f32) == "1.2"
10+
check formatFloatClean(1.1'f32) == "1.1"
11+
check formatFloatClean(1.0'f32) == "1.0"
12+
check formatFloatClean(0.5'f32) == "0.5"
13+
14+
test "handles whole numbers correctly":
15+
check formatFloatClean(5.0'f32) == "5.0"
16+
check formatFloatClean(0.0'f32) == "0.0"
17+
check formatFloatClean(100.0'f32) == "100.0"
18+
19+
test "handles small decimal values":
20+
check formatFloatClean(0.1'f32) == "0.1"
21+
check formatFloatClean(0.01'f32) == "0.01"
22+
check formatFloatClean(0.001'f32) == "0.001"
23+
24+
test "handles negative numbers":
25+
check formatFloatClean(-1.234'f32) == "-1.234"
26+
check formatFloatClean(-1.2'f32) == "-1.2"
27+
check formatFloatClean(-1.0'f32) == "-1.0"
28+
29+
test "handles edge cases with precision":
30+
check formatFloatClean(1.000001'f32) == "1.000001"
31+
check formatFloatClean(1.0001'f32) == "1.0001"
32+
check formatFloatClean(1.000000'f32) == "1.0"
33+
34+
test "handles very small differences":
35+
check formatFloatClean(1.0000001'f32) == "1.0"

0 commit comments

Comments
 (0)