On this page:
query-table?
predicate?
predicate-list?
table-headers
table-rows
table-row-ref
table-row-cell
table-filter
table-search
table-filter-search
format-filter
table-column-values
table-sort

7.2 Query Functions

How to use a query to filter a list of files for subsequent bout analysis.

 (require "../query.rkt") package: base
Routines needed to filter and sort tables

procedure

(query-table? table)  boolean?

  table : any/c
test if given item is a query-table, identified as a list with 2 members, with first member is a header row (a non-empty list of strings), and the second member a list of rows, where each row has as many members as the header row. the list rows can be empty; elements in a row can be empty strings

Example:

(  ;; header row
   ( "header0"  "header1"  "header2")
   ;; list of rows
   ( ("row0-cell0" "row0-cell1" "row0-cell2")
     ("row1-cell0" "row1-cell1" "row1-cell2")))

procedure

(predicate? pred)  boolean?

  pred : any/c
test if given item is a predicate for querying a table, identified as a list with a minimum of 2 members, with the first member a string, and the second member a list of strings predicate should be of the form ’("<column-heading>" ("column-cell-option1" "column-cell-option1"...))

Example:
(predicate? '( "Group" ("Control")))
-> #t
 
(predicate? '( "Name" ("Rat02" "Rat04" "Rat06")))
-> #t

procedure

(predicate-list? pred-list)  boolean?

  pred-list : any/c
test if given item is a predicate-list for querying a table, identified as a list of predicates? predicate-list can be empty (if no query-filters are being applied)

Example:
(predicate-list? '(  ("Group" ("Control")) ("Name" ("Rat02" "Rat04" "Rat06")) ) )
-> #t

procedure

(table-headers table)  (non-empty-listof string?)

  table : query-table?
returns the column-headers of the given table, as a list of strings. Table is list of 2 lists; the first list is the list of column-header strings; the second list is the list of data rows containing the table data. Example:
(table-headers my-table)
-> ("Name" "Group" "Weight" "Intake")

procedure

(table-rows table)  list?

  table : query-table?
returns the data rows of the given table, as a list of lists. Table is list of 2 lists; the first list is the list of column-header strings; the second list is the list of data rows containing the table data.

(table-rows my-table)
->  ( ("Rat01" "Control" "320" "42")
      ("Rat02" "Control" "300" "41")
      ("Rat03" "Control" "310" "40")
      ("Rat04" "Drug" "250" "36")
      ("Rat05" "Drug" "255" "37")
      ("Rat06" "Drug" "253" "38") )
 

procedure

(table-row-ref table row-index)  list?

  table : query-table?
  row-index : nonnegative-integer?
returns the data row at the given index from the table

(table-row-ref my-table 3)
-> ("Rat03" "Control" "310" "40")

procedure

(table-row-cell table    
  #:row row    
  #:column-heading column-heading)  any/c
  table : query-table?
  row : list
  column-heading : string?
returns false if column heading is not in the table header

(table-row-cell my-table #:row (table-row-ref my-table 3) #:column-heading "Group")
-> "Control"

procedure

(table-filter table predicate-list)  query-table?

  table : query-table?
  predicate-list : predicate-list?
table is filtered to return only those rows for which all the predicates in predicate-list are #t.

a new table is returned, with the same headers list as the original table

predicate-list is list of predicates: (<column-heading> match-term1 [match-term2 ...]) <column-heading> should be in _column-headings hash table

each predicate starts with name of column, followed by variable number of terms as potential matches for that column.

Within a predicate, matches are OR’d.

Between predicates, matches are AND’d.

ie a predicate list
( (column-headingA (match1 match2)) (column-headingB (match3 match4)) )
will be parsed in order to match rows in the table with the following tests:
(and (or (equal? column-headingA  match1) (equal? column-headingA match2))
     (or (equal? column-headingB  match3) (equal? column-headingB match4)))

an example:

 ;; find rows  where column 1 is Control, and column 0 is Rat03
 (table-filter my-table '(("Group" ("Control")) ("Name"( "Rat03"))) )
->  ( ("Name" "Group" "Weight" "Intake")
      ( ("Rat03" "Control" "310" "40")))
 
  ;; find rows  where column 0 is Rat04 or Rat05
  (table-filter my-table '(("Name" ("Rat04" "Rat05"))))
->  ( ("Name" "Group" "Weight" "Intake")
      ( ("Rat04" "Drug" "250" "36")
        ("Rat05" "Drug" "255" "37")))
 

procedure

(table-search table search-string)  query-table?

  table : query-table?
  search-string : string?
table is filtered to contain only those rows for which the row contains the search-string (in any cell of that row). a new table is returned, with the same headers list as the original table

procedure

(table-filter-search table    
  predicate-list    
  search-string)  query-table?
  table : query-table?
  predicate-list : predicate-list?
  search-string : string?
table is filtered to contain only those rows for which all the predicates in predicate-list are #t, and for which the row contains the search-string (in any cell). a new table is returned, with the same headers list as the original table predicate-list can be ’() -> no filtering; search-string can be "" -> matches all strings

example: (table-filter-search test-table ’(("group" ("Lo Dose" "Hi Dose")) ("subject" ("TS04" "TS11"))) "Extinction" )

procedure

(format-filter predicate-list    
  search-string)  string?
  predicate-list : predicate-list?
  search-string : string?
pretty-print query (predicates and search string for display

procedure

(table-column-values table column-heading)  (listof string?)

  table : query-table?
  column-heading : string?
return list (set) of unique values in table column with given column-heading.

procedure

(table-sort table column-heading [direction])  query-table?

  table : query-table?
  column-heading : string?
  direction : (or/c 'ascending 'descending) = 'ascending
return a table with same heading but all rows sorted by the column with column-heading. Values are sorted with string<? or string>?, with direction either ’ascending or ’descending default is ’ascending