Skip to content

Commit

Permalink
Merge pull request #206 from fslaborg/interactive
Browse files Browse the repository at this point in the history
  • Loading branch information
bvenn authored May 30, 2022
2 parents 085afe8 + 8a559f5 commit 9386ed5
Show file tree
Hide file tree
Showing 16 changed files with 386 additions and 110 deletions.
60 changes: 50 additions & 10 deletions src/FSharp.Stats.Interactive/test.ipynb

Large diffs are not rendered by default.

42 changes: 21 additions & 21 deletions src/FSharp.Stats/AlgTypes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2193,46 +2193,46 @@ namespace FSharp.Stats
elif displayRows >= nRows && displayCols < nCols then // this formats the matrix with only ommitted cols
Array.init (nRows+2) (fun rowIndex ->
match rowIndex with
| 0 ->
| 0 -> // column index header row
[|
"";"";
yield! [for i in [0 .. columnStartCount-1] do yield string i];
"...";
yield! [for i in [nCols - columnEndCount .. nCols - 1] do yield string i]
|]
| 1 -> [|for i in 0 .. (displayCols + 2) do yield ""|]
| 1 -> [|for i in 0 .. (displayCols + 2) do yield ""|] // empty row to distinguish column indices from data in FSI/StructuredDisplay
| _ -> // the rest of the rows contain data
Array.init (columnStartCount+columnEndCount+3) (fun colIndex ->
if (colIndex-2) < columnStartCount then
if (colIndex-2) < columnStartCount then // left
match colIndex with
| 0 -> string (rowIndex-2)
| 1 -> "->"
| _ -> m.[rowIndex-2,colIndex-2] |> formatValue
elif (colIndex-2) > columnStartCount then
elif (colIndex-2) > columnStartCount then // right
m[rowIndex-2,(nCols - 3 - columnEndCount + colIndex - columnStartCount)] |> formatValue
else
"..."
"..." // separator for signalling ommitted cols
)
)
elif displayRows < nRows && displayCols >= nCols then // this formats the matrix with only ommitted rows
Array.init (rowStartCount+rowEndCount+3) (fun rowIndex ->
match rowIndex with
| 0 -> [|"";"";yield! [for i = 0 to nCols-1 do yield string i]|]
| 1 -> [|for i in 0 .. nCols+1 do yield ""|]
| 0 -> [|"";"";yield! [for i = 0 to nCols-1 do yield string i]|] // column index header row
| 1 -> [|for i in 0 .. nCols+1 do yield ""|] // empty row to distinguish column indices from data in FSI/StructuredDisplay
| _ ->
Array.init (nCols+2) (fun colIndex ->
if (rowIndex-2) < rowStartCount then
if (rowIndex-2) < rowStartCount then // upper half
match colIndex with
| 0 -> string (rowIndex-2)
| 1 -> "->"
| _ -> m.[rowIndex-2,colIndex-2] |> formatValue
elif (rowIndex-2) > rowStartCount then
elif (rowIndex-2) > rowStartCount then // lower half
match colIndex with
| 0 -> string (nRows - 3 - rowEndCount + rowIndex - rowStartCount)
| 1 -> "->"
| _ -> m[(nRows - 3 - rowEndCount + rowIndex - rowStartCount),colIndex - 2] |> formatValue
else
match colIndex with
match colIndex with // separator for signalling ommitted rows
| 0 -> ":"
| 1 -> ""
| _ -> "..."
Expand All @@ -2241,33 +2241,33 @@ namespace FSharp.Stats
else // this formats the matrix with ommitted rows and cols
Array.init (rowStartCount+rowEndCount+3) (fun rowIndex ->
match rowIndex with
| 0 ->
| 0 -> // column index header row
[|
"";"";
yield! [for i in [0 .. columnStartCount-1] do yield string i];
"...";
yield! [for i in [nCols - columnEndCount .. nCols - 1] do yield string i]
|]
| 1 -> [|for i in 0 .. (displayCols + 2) do yield ""|]
| 1 -> [|for i in 0 .. (displayCols + 2) do yield ""|] // empty row to distinguish column indices from data in FSI/StructuredDisplay
| _ ->
Array.init (columnStartCount+columnEndCount+3) (fun colIndex ->
if (rowIndex-2) < rowStartCount then
if (colIndex-2) < columnStartCount then
if (rowIndex-2) < rowStartCount then // upper half
if (colIndex-2) < columnStartCount then // upper left
match colIndex with
| 0 -> string (rowIndex-2)
| 0 -> string (rowIndex-2)
| 1 -> "->"
| _ -> m.[rowIndex-1,colIndex-2] |> formatValue
elif (colIndex-2) > columnStartCount then
| _ -> m.[rowIndex-2,colIndex-2] |> formatValue
elif (colIndex-2) > columnStartCount then // upper right
m[rowIndex-2,(nCols - 3 - columnEndCount + colIndex - columnStartCount)] |> formatValue
else
"..."
elif (rowIndex-2) > rowStartCount then
if (colIndex-2) < columnStartCount then
elif (rowIndex-2) > rowStartCount then // lower half
if (colIndex-2) < columnStartCount then // lower left
match colIndex with
| 0 -> string (nRows - 3 - rowEndCount + rowIndex - rowStartCount)
| 1 -> "->"
| _ -> m[(nRows - 3 - rowEndCount + rowIndex - rowStartCount),colIndex] |> formatValue
elif (colIndex-2) > columnStartCount then
| _ -> m[(nRows - 3 - rowEndCount + rowIndex - rowStartCount),(colIndex-2)] |> formatValue
elif (colIndex-2) > columnStartCount then // lower right
m[(nRows - 3 - rowEndCount + rowIndex - rowStartCount),(nCols - 3 - columnEndCount + colIndex - columnStartCount)] |> formatValue
else
"..."
Expand Down
34 changes: 30 additions & 4 deletions src/FSharp.Stats/Playground.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,35 @@
#r "FSharp.Stats.dll"

open FSharp.Stats
let rnd = new System.Random(1)
let m2 = Matrix.Generic.init 50 50 (fun i j -> int $"{i}{j}")
let m = Matrix.init 500 500 (fun i j -> float i * float j + (rnd.NextDouble()))
let rnd = new System.Random(69)

printfn "%s" (m.Format(true))
let mDenseInt1 = Matrix.Generic.init 10 10 (fun r c -> $"{r}{c}" )
let mDenseInt2 = Matrix.Generic.init 10 100 (fun r c -> $"{r}{c}" )
let mDenseInt3 = Matrix.Generic.init 100 10 (fun r c -> $"{r}{c}" )
let mDenseInt4 = Matrix.Generic.init 100 100 (fun r c -> $"{r}{c}" )

mDenseInt1.Format(false)
mDenseInt1.Format(true)
mDenseInt2.Format(false)
mDenseInt2.Format(true)
mDenseInt3.Format(false)
mDenseInt3.Format(true)
mDenseInt4.Format(false)
mDenseInt4.Format(true)

let mDense1 = Matrix.init 10 10 (fun i j -> float i * float j * rnd.NextDouble())
let mDense2 = Matrix.init 10 100 (fun i j -> float i * float j * rnd.NextDouble())
let mDense3 = Matrix.init 100 10 (fun i j -> float i * float j * rnd.NextDouble())
let mDense4 = Matrix.init 100 100 (fun i j -> float i * float j * rnd.NextDouble())
let mDenseSpecial = matrix[[nan;100000000.;infinity;1.4];[1.337;-nan;4269420.42;-infinity]]

mDense1.Format(false)
mDense1.Format(true)
mDense2.Format(false)
mDense2.Format(true)
mDense3.Format(false)
mDense3.Format(true)
mDense4.Format(false)
mDense4.Format(true)
mDenseSpecial.Format(false)
mDenseSpecial.Format(true)
8 changes: 8 additions & 0 deletions tests/FSharp.Stats.Tests/FSharp.Stats.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
<EmbeddedResource Include="data/DenseMatrixFormat2WithInfo.txt" CopyToOutputDirectory="PreserveNewest" />
<EmbeddedResource Include="data/DenseMatrixFormat3WithInfo.txt" CopyToOutputDirectory="PreserveNewest" />
<EmbeddedResource Include="data/DenseMatrixFormat4WithInfo.txt" CopyToOutputDirectory="PreserveNewest" />
<EmbeddedResource Include="data/DenseMatrixIntFormat1NoInfo.txt" CopyToOutputDirectory="PreserveNewest" />
<EmbeddedResource Include="data/DenseMatrixIntFormat2NoInfo.txt" CopyToOutputDirectory="PreserveNewest" />
<EmbeddedResource Include="data/DenseMatrixIntFormat3NoInfo.txt" CopyToOutputDirectory="PreserveNewest" />
<EmbeddedResource Include="data/DenseMatrixIntFormat4NoInfo.txt" CopyToOutputDirectory="PreserveNewest" />
<EmbeddedResource Include="data/DenseMatrixIntFormat1WithInfo.txt" CopyToOutputDirectory="PreserveNewest" />
<EmbeddedResource Include="data/DenseMatrixIntFormat2WithInfo.txt" CopyToOutputDirectory="PreserveNewest" />
<EmbeddedResource Include="data/DenseMatrixIntFormat3WithInfo.txt" CopyToOutputDirectory="PreserveNewest" />
<EmbeddedResource Include="data/DenseMatrixIntFormat4WithInfo.txt" CopyToOutputDirectory="PreserveNewest" />
<EmbeddedResource Include="data/DenseMatrixSpecialNoInfo.txt" CopyToOutputDirectory="PreserveNewest" />
<EmbeddedResource Include="data/DenseMatrixSpecialWithInfo.txt" CopyToOutputDirectory="PreserveNewest" />
<EmbeddedResource Include="data/SparseMatrixFormat1NoInfo.txt" CopyToOutputDirectory="PreserveNewest" />
Expand Down
32 changes: 23 additions & 9 deletions tests/FSharp.Stats.Tests/Formatting.fs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ let formatTableTests =
let matrixFormattingtests =
testList "Formatting.MatrixFormatting" [

let mDenseInt1 = Matrix.Generic.init 10 10 (fun r c -> $"{r}{c}" )
let mDenseInt2 = Matrix.Generic.init 10 100 (fun r c -> $"{r}{c}" )
let mDenseInt3 = Matrix.Generic.init 100 10 (fun r c -> $"{r}{c}" )
let mDenseInt4 = Matrix.Generic.init 100 100 (fun r c -> $"{r}{c}" )

let mdense1_int_no_info = readEmbeddedRessource "DenseMatrixIntFormat1NoInfo.txt"
let mdense2_int_no_info = readEmbeddedRessource "DenseMatrixIntFormat2NoInfo.txt"
let mdense3_int_no_info = readEmbeddedRessource "DenseMatrixIntFormat3NoInfo.txt"
let mdense4_int_no_info = readEmbeddedRessource "DenseMatrixIntFormat4NoInfo.txt"
let mdense1_int_with_info = readEmbeddedRessource "DenseMatrixIntFormat1WithInfo.txt"
let mdense2_int_with_info = readEmbeddedRessource "DenseMatrixIntFormat2WithInfo.txt"
let mdense3_int_with_info = readEmbeddedRessource "DenseMatrixIntFormat3WithInfo.txt"
let mdense4_int_with_info = readEmbeddedRessource "DenseMatrixIntFormat4WithInfo.txt"

let rnd = new System.Random(69)

let mDense1 = Matrix.init 10 10 (fun i j -> float i * float j * rnd.NextDouble())
Expand All @@ -71,27 +85,27 @@ let matrixFormattingtests =
let msparse1_no_info = readEmbeddedRessource "SparseMatrixFormat1NoInfo.txt"
let msparse1_with_info = readEmbeddedRessource "SparseMatrixFormat1WithInfo.txt"

testCase "dense float matrix full display no info" (fun _ -> Expect.equal (mDense1.Format(false)) mdense1_no_info "Incorrect format for this value")
testCase "dense int matrix full display no info" (fun _ -> Expect.equal (mDenseInt1.Format(false)) mdense1_int_no_info "Incorrect format for this value")
testCase "dense int matrix full display with info" (fun _ -> Expect.equal (mDenseInt1.Format(true)) mdense1_int_with_info "Incorrect format for this value")
testCase "dense int matrix omitted cols no info" (fun _ -> Expect.equal (mDenseInt2.Format(false)) mdense2_int_no_info "Incorrect format for this value")
testCase "dense int matrix omitted cols with info" (fun _ -> Expect.equal (mDenseInt2.Format(true)) mdense2_int_with_info "Incorrect format for this value")
testCase "dense int matrix omitted rows no info" (fun _ -> Expect.equal (mDenseInt3.Format(false)) mdense3_int_no_info "Incorrect format for this value")
testCase "dense int matrix omitted rows with info" (fun _ -> Expect.equal (mDenseInt3.Format(true)) mdense3_int_with_info "Incorrect format for this value")
testCase "dense int matrix omitted rows and cols no info" (fun _ -> Expect.equal (mDenseInt4.Format(false)) mdense4_int_no_info "Incorrect format for this value")
testCase "dense int matrix omitted rows and cols with info" (fun _ -> Expect.equal (mDenseInt4.Format(true)) mdense4_int_with_info "Incorrect format for this value" )

testCase "dense float matrix full display no info" (fun _ -> Expect.equal (mDense1.Format(false)) mdense1_no_info "Incorrect format for this value")
testCase "dense float matrix full display with info" (fun _ -> Expect.equal (mDense1.Format(true)) mdense1_with_info "Incorrect format for this value")

testCase "dense float matrix omitted cols no info" (fun _ -> Expect.equal (mDense2.Format(false)) mdense2_no_info "Incorrect format for this value")

testCase "dense float matrix omitted cols with info" (fun _ -> Expect.equal (mDense2.Format(true)) mdense2_with_info "Incorrect format for this value")

testCase "dense float matrix omitted rows no info" (fun _ -> Expect.equal (mDense3.Format(false)) mdense3_no_info "Incorrect format for this value")

testCase "dense float matrix omitted rows with info" (fun _ -> Expect.equal (mDense3.Format(true)) mdense3_with_info "Incorrect format for this value")

testCase "dense float matrix omitted rows and cols no info" (fun _ -> Expect.equal (mDense4.Format(false)) mdense4_no_info "Incorrect format for this value")

testCase "dense float matrix omitted rows and cols with info" (fun _ -> Expect.equal (mDense4.Format(true)) mdense4_with_info "Incorrect format for this value" )

testCase "dense float matrix with edge cases (+/- nan, +/- infinity) no info" (fun _ -> Expect.equal (mDenseSpecial.Format(true)) mdenseSpecial_with_info "Incorrect format for this value" )

testCase "dense float matrix with edge cases (+/- nan, +/- infinity) with info" (fun _ -> Expect.equal (mDenseSpecial.Format(true)) mdenseSpecial_with_info "Incorrect format for this value" )

testCase "sparse float matrix full display no info" (fun _ -> Expect.equal (mSparse1.Format(false)) msparse1_no_info "Incorrect format for this value")

testCase "sparse float matrix full display with info" (fun _ -> Expect.equal (mSparse1.Format(true)) msparse1_with_info "Incorrect format for this value")
]
Loading

0 comments on commit 9386ed5

Please sign in to comment.