Skip to content

Commit

Permalink
Merge pull request #2106 from nellh/2105-xml2js-fix
Browse files Browse the repository at this point in the history
fix: Use @libs/xml over xml2js for tiff parsing
  • Loading branch information
rwblair authored Aug 26, 2024
2 parents d232223 + 786cc93 commit 67c7b72
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
1 change: 1 addition & 0 deletions .github/workflows/deno_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
deno-version: v1.x
- run: ./build.ts
working-directory: ./bids-validator
- run: deno run -A ./bids-validator/dist/validator/bids-validator.js --version
- uses: actions/upload-artifact@v4
with:
name: main
Expand Down
2 changes: 1 addition & 1 deletion bids-validator/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@hed/validator": "npm:[email protected]",
"@ignore": "npm:[email protected]",
"@mango/nifti": "npm:[email protected]",
"@xml2js": "npm:[email protected]"
"@libs/xml": "jsr:@libs/[email protected]"
},
"tasks": {
"test": "deno test -A src/tests/"
Expand Down
18 changes: 9 additions & 9 deletions bids-validator/src/files/tiff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Module for extracting Tiff metadata
*/
import { Ome, Tiff } from '@bids/schema/context'
import * as xml2js from '@xml2js'
import * as XML from '@libs/xml'
import { BIDSFile } from '../types/filetree.ts'

function getImageDescription(
Expand Down Expand Up @@ -57,19 +57,19 @@ export async function parseTIFF(
}

const imageDescription = getImageDescription(dataview, littleEndian, version === 42 ? 12 : 20)
const omexml = await xml2js.parseStringPromise(imageDescription || '') as { [key: string]: any }
const Pixels = omexml?.OME?.Image[0]?.Pixels[0]["$"]
const omexml = await XML.parse(imageDescription || '') as { [key: string]: any }
const Pixels = omexml?.OME?.Image?.Pixels
if (!Pixels) return { tiff: { version } }

return {
tiff: { version },
ome: {
PhysicalSizeX: parseFloat(Pixels['PhysicalSizeX']),
PhysicalSizeY: parseFloat(Pixels['PhysicalSizeY']),
PhysicalSizeZ: parseFloat(Pixels['PhysicalSizeZ']),
PhysicalSizeXUnit: Pixels['PhysicalSizeXUnit'],
PhysicalSizeYUnit: Pixels['PhysicalSizeYUnit'],
PhysicalSizeZUnit: Pixels['PhysicalSizeZUnit'],
PhysicalSizeX: parseFloat(Pixels['@PhysicalSizeX']),
PhysicalSizeY: parseFloat(Pixels['@PhysicalSizeY']),
PhysicalSizeZ: parseFloat(Pixels['@PhysicalSizeZ']),
PhysicalSizeXUnit: Pixels['@PhysicalSizeXUnit'],
PhysicalSizeYUnit: Pixels['@PhysicalSizeYUnit'],
PhysicalSizeZUnit: Pixels['@PhysicalSizeZUnit'],
},
}
}
12 changes: 7 additions & 5 deletions bids-validator/src/validators/bids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,13 @@ export async function validate(

const modalitiesRule = schema.rules.modalities as Record<string, { datatypes: string[] }>
const blacklistedDatatypes = new Map<string, string>()
// Map blacklisted datatypes back to the modality that generated them
for (const modality of options.blacklistModalities) {
const datatypes = modalitiesRule[modality.toLowerCase()]?.datatypes as string[]
for (const datatype of datatypes) {
blacklistedDatatypes.set(datatype, modality)
if (options.blacklistModalities) {
// Map blacklisted datatypes back to the modality that generated them
for (const modality of options.blacklistModalities) {
const datatypes = modalitiesRule[modality.toLowerCase()]?.datatypes as string[]
for (const datatype of datatypes) {
blacklistedDatatypes.set(datatype, modality)
}
}
}

Expand Down
17 changes: 17 additions & 0 deletions web/vite.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,27 @@ import { nodePolyfills } from "npm:[email protected]"
import "npm:react@^18.2.0"
import "npm:react-dom@^18.2.0"

/**
* Vite plugin to hack a bug injected by the default assetImportMetaUrlPlugin
*/
function workaroundAssetImportMetaUrlPluginBug() {
return {
name: "vite-workaround-import-glob",
transform(src, id) {
if (id.includes('validator/main.js')) {
return src.replace(", import.meta.url", "")
} else {
return null
}
}
}
}

// https://vitejs.dev/config/
export default defineConfig({
base: '',
plugins: [
workaroundAssetImportMetaUrlPluginBug(),
httpsImports.default(),
react(),
nodePolyfills({
Expand Down

0 comments on commit 67c7b72

Please sign in to comment.