Skip to content

Commit

Permalink
Add function to convert from old TSV format to new one
Browse files Browse the repository at this point in the history
Also fix minor typo.
  • Loading branch information
happy5214 committed Nov 20, 2023
1 parent 33280f2 commit e0bd377
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
24 changes: 23 additions & 1 deletion bids/tsvParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const isContentfulRow = (row) => row && !/^\s*$/.test(row)
* @param {string} contents The contents of a TSV file.
* @returns {Map<string, string[]>} The parsed contents of the TSV file.
*/
function parseTSV(contents) {
export function parseTSV(contents) {
const columns = new Map()
const rows = stripBOM(normalizeEOL(contents))
.split('\n')
Expand All @@ -36,5 +36,27 @@ function parseTSV(contents) {
}
return columns
}
/**
* Convert parsed TSV file data from the old BIDS format to the new BIDS format.
*
* @param {{headers: string[], rows: string[][]}} oldParsedTsv Parsed TSV data using the old format
* @returns {Map<string, string[]>} The parsed contents of the TSV file, using the new format.
*/
export function convertParsedTSVData(oldParsedTsv) {
const columns = new Map()

oldParsedTsv.headers.forEach((x) => {
columns.set(x, [])
})
for (let i = 1; i < oldParsedTsv.rows.length; i++) {
for (let j = 0; j < oldParsedTsv.headers.length; j++) {
columns.get(oldParsedTsv.headers[j])?.push(oldParsedTsv.rows[i][j])
}
}
for (const [key, value] of columns) {
columns.set(key, value)
}
return columns
}

export default parseTSV
13 changes: 9 additions & 4 deletions bids/types.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { sidecarValueHasHed } from './utils'
import { generateIssue, Issue } from '../common/issues/issues'
import parseTSV from './tsvParser'
import { parseTSV, convertParsedTSVData } from './tsvParser'
import { parseHedString } from '../parser/main'

/**
Expand Down Expand Up @@ -110,17 +110,22 @@ export class BidsTsvFile extends BidsFile {
* @todo This interface is provisional and subject to modification in version 4.0.0.
*
* @param {string} name The name of the TSV file.
* @param {Map<string, string[]>|string} tsvData This file's TSV data.
* @param {{headers: string[], rows: string[][]}|Map<string, string[]>|string} tsvData This file's TSV data.
* @param {object} file The file object representing this file.
* @param {string[]} potentialSidecars The list of potential JSON sidecars.
* @param {object} mergedDictionary The merged sidecar data.
*/
constructor(name, tsvData, file, potentialSidecars = [], mergedDictionary = {}) {
super(name, file)
let parsedTsvData
if (typeof tsvData === 'string') {
tsvData = parseTSV(tsvData)
parsedTsvData = parseTSV(tsvData)
} else if (tsvData === Object(tsvData)) {
parsedTsvData = convertParsedTSVData(tsvData)
} else {
parsedTsvData = tsvData
}
this.parsedTsv = tsvData
this.parsedTsv = parsedTsvData
this.potentialSidecars = potentialSidecars

this.mergedSidecar = new BidsSidecar(name, mergedDictionary, null)
Expand Down
2 changes: 1 addition & 1 deletion bids/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ class BidsHedValidator {
*/
_generateTsvSidecarRows(sidecarHedColumns) {
const sidecarHedRows = []
for (const [header, data] of sidecarHedColumns.parsedTsv.entries()) {
for (const [header, data] of sidecarHedColumns.entries()) {
data.forEach((value, index) => {
if (sidecarHedRows[index] === undefined) {
sidecarHedRows[index] = new Map()
Expand Down

0 comments on commit e0bd377

Please sign in to comment.