Skip to content

Commit

Permalink
Extend ArcTable.Join ✨✅
Browse files Browse the repository at this point in the history
  • Loading branch information
Freymaurer committed Jan 17, 2024
1 parent a112468 commit b21e3bc
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 14 deletions.
14 changes: 9 additions & 5 deletions src/ISA/ISA/ArcTypes/ArcTable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -503,12 +503,17 @@ type ArcTable(name: string, headers: ResizeArray<CompositeHeader>, values: Syste
/// <summary>
/// This function can be used to join two arc tables.
/// </summary>
/// <param name="index">If not set default to append. -1 will also append.</param>
/// <param name="table">The table to join to this table.</param>
/// <param name="joinOptions">Can add only headers, header with unitized cell information, headers with values.</param>
/// <param name="forceReplace">if set to true will replace unique columns.</param>
member this.Join(table:ArcTable, ?joinOptions: TableJoinOptions, ?forceReplace: bool, ?SkipFillMissing) : unit =
member this.Join(table:ArcTable, ?index: int, ?joinOptions: TableJoinOptions, ?forceReplace: bool, ?skipFillMissing) : unit =
let joinOptions = defaultArg joinOptions TableJoinOptions.Headers
let forceReplace = defaultArg forceReplace false
let skipFillMissing = defaultArg skipFillMissing true
let mutable index = defaultArg index this.ColumnCount
index <- if index = -1 then this.ColumnCount else index //make -1 default to append to make function usage more fluent.
SanityChecks.validateColumnIndex index this.ColumnCount true
let onlyHeaders = joinOptions = TableJoinOptions.Headers
let columns =
let pre = table.Columns
Expand All @@ -528,20 +533,19 @@ type ArcTable(name: string, headers: ResizeArray<CompositeHeader>, values: Syste
| WithValues -> pre
SanityChecks.validateNoDuplicateUniqueColumns columns
columns |> Array.iter (fun x -> SanityChecks.validateColumn x)
let mutable index = this.ColumnCount
columns
|> Array.iter (fun col ->
let prevHeadersCount = this.Headers.Count
Unchecked.addColumn col.Header col.Cells index forceReplace onlyHeaders this.Headers this.Values
// Check if more headers, otherwise `ArcTableAux.insertColumn` replaced a column and we do not need to increase index.
if this.Headers.Count > prevHeadersCount then index <- index + 1
)
if not(SkipFillMissing = Some true) then Unchecked.fillMissingCells this.Headers this.Values
if not(skipFillMissing) then Unchecked.fillMissingCells this.Headers this.Values

static member join(table:ArcTable, ?joinOptions: TableJoinOptions, ?forceReplace: bool) =
static member join(table:ArcTable, ?index: int, ?joinOptions: TableJoinOptions, ?forceReplace: bool) =
fun (this: ArcTable) ->
let copy = this.Copy()
copy.Join(table,?joinOptions=joinOptions,?forceReplace=forceReplace)
copy.Join(table,?index=index,?joinOptions=joinOptions,?forceReplace=forceReplace)
copy

static member insertParameterValue (t : ArcTable) (p : ProcessParameterValue) : ArcTable =
Expand Down
50 changes: 41 additions & 9 deletions tests/ISA/ISA.Tests/ArcTable.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2081,11 +2081,43 @@ let private tests_UpdateRefWithSheet =
]

let private tests_Join = testList "Join" [
testList "index" [
testCase "ensure default is append" <| fun _ ->
let table1 = create_testTable()
let columnCount = table1.ColumnCount
let table2 = create_testTable()
table2.RemoveColumn 0 // rmv input
table2.RemoveColumn 0 // rmv output
let expectedJoinCount = columnCount + table2.ColumnCount
table1.Join(table2)
Expect.equal table1.ColumnCount expectedJoinCount "new column count"
Expect.equal (table1.Headers.[columnCount]) (table2.Headers.[0]) "column equal"
testCase "ensure -1 defaults to append" <| fun _ ->
let table1 = create_testTable()
let columnCount = table1.ColumnCount
let table2 = create_testTable()
table2.RemoveColumn 0 // rmv input
table2.RemoveColumn 0 // rmv output
let expectedJoinCount = columnCount + table2.ColumnCount
table1.Join(table2,-1)
Expect.equal table1.ColumnCount expectedJoinCount "new column count"
Expect.equal (table1.Headers.[columnCount]) (table2.Headers.[0]) "column equal"
testCase "insert at start" <| fun _ ->
let table1 = create_testTable()
let columnCount = table1.ColumnCount
let table2 = create_testTable()
table2.RemoveColumn 0 // rmv input
table2.RemoveColumn 0 // rmv output
let expectedJoinCount = columnCount + table2.ColumnCount
table1.Join(table2,0)
Expect.equal table1.ColumnCount expectedJoinCount "new column count"
Expect.equal (table1.Headers.[0]) (table2.Headers.[0]) "column equal"
]
testList "TableJoinOption.Headers" [
testCase "Add to empty" <| fun _ ->
let table = ArcTable.init("MyTable")
let joinTable = create_testTable()
table.Join(joinTable,TableJoinOptions.Headers)
table.Join(joinTable,-1,TableJoinOptions.Headers)
Expect.equal table.ColumnCount 5 "columnCount"
// test headers
Expect.equal table.Headers.[0] (CompositeHeader.Input IOType.Source) "Header input"
Expand All @@ -2098,12 +2130,12 @@ let private tests_Join = testList "Join" [
testCase "Add to duplicate" <| fun _ ->
let table = create_testTable()
let joinTable = create_testTable()
let func = fun () -> table.Join(joinTable,TableJoinOptions.Headers)
let func = fun () -> table.Join(joinTable,-1,TableJoinOptions.Headers)
Expect.throws func "This should fail as we try to add multiple inputs/outputs to one table"
testCase "Add to duplicate, forceReplace" <| fun _ ->
let table = create_testTable()
let joinTable = create_testTable()
table.Join(joinTable,TableJoinOptions.Headers, true)
table.Join(joinTable,-1,TableJoinOptions.Headers,true)
Expect.equal table.ColumnCount 8 "We expect 8 columns as there are 5 per table with 2 unique 5 + (5-2) = 8"
// headers
Expect.equal table.Headers.[0] (CompositeHeader.Input IOType.Source) "Header input"
Expand All @@ -2123,7 +2155,7 @@ let private tests_Join = testList "Join" [
ResizeArray([CompositeHeader.Input IOType.ImageFile]),
System.Collections.Generic.Dictionary()
)
table.Join(joinTable,TableJoinOptions.Headers, true)
table.Join(joinTable,-1,TableJoinOptions.Headers,true)
Expect.equal table.ColumnCount 5 "columnCount"
// test headers
Expect.equal table.Headers.[0] (CompositeHeader.Input IOType.ImageFile) "Here should be new image input"
Expand All @@ -2142,7 +2174,7 @@ let private tests_Join = testList "Join" [
column_component
|]
joinTable.AddColumns(columns)
table.Join(joinTable,TableJoinOptions.WithUnit)
table.Join(joinTable,-1,TableJoinOptions.WithUnit)
Expect.equal table.ColumnCount 1 "column count"
Expect.equal table.RowCount 0 "row count"
testCase "Add to empty, with unit" <| fun _ ->
Expand All @@ -2158,11 +2190,11 @@ let private tests_Join = testList "Join" [
)
|]
joinTable.AddColumns(columns)
table.Join(joinTable,TableJoinOptions.WithUnit)
table.Join(joinTable,-1,TableJoinOptions.WithUnit, skipFillMissing=false)
Expect.equal table.ColumnCount 2 "column count"
Expect.equal table.RowCount 5 "row count"
Expect.equal table.Values.[0,0] (CompositeCell.createTerm OntologyAnnotation.empty) "empty term cell"
Expect.equal table.Values.[1,0] (CompositeCell.createUnitized("",oa_temperature)) "temperature unit cell"
Expect.equal table.Values.[(0,0)] (CompositeCell.createTerm OntologyAnnotation.empty) "empty term cell"
Expect.equal table.Values.[(1,0)] (CompositeCell.createUnitized("",oa_temperature)) "temperature unit cell"
]
testList "TableJoinOption.WithValues" [
testCase "Add to empty" <| fun _ ->
Expand All @@ -2178,7 +2210,7 @@ let private tests_Join = testList "Join" [
)
|]
joinTable.AddColumns(columns)
table.Join(joinTable,TableJoinOptions.WithValues)
table.Join(joinTable,-1,TableJoinOptions.WithValues)
Expect.equal table.ColumnCount 2 "column count"
Expect.equal table.RowCount 5 "row count"
Expect.equal table.Values.[0,0] (CompositeCell.createTerm oa_SCIEXInstrumentModel) "sciex instrument model"
Expand Down

0 comments on commit b21e3bc

Please sign in to comment.