/ concept-collection / seqlab
Sign in
concept-collection / seqlab
seqlab / src / viewer / format.ts
40 lines · 1.4 KBCodeBlameHistory
78d04f1seqlab: write and view pulseq MRI sequences in the browserJeremy Magland 1// Formatting helpers shared by the viewer components.
3/** Gyromagnetic ratio of 1H, Hz/T — used to express gradients in mT/m. */
4export const GAMMA = 42.576e6
6/** Format a time in seconds with an adaptive unit. */
7export function formatTime(t: number, digits = 4): string {
8 const a = Math.abs(t)
9 if (a >= 1) return `${t.toFixed(digits)} s`
10 if (a >= 1e-3) return `${(t * 1e3).toPrecision(digits)} ms`
11 if (a === 0) return '0'
12 return `${(t * 1e6).toPrecision(digits)} µs`
15/** Tick label for a time axis where the tick spacing is `step` seconds. */
16export function formatTick(t: number, step: number): string {
17 if (step >= 0.1) return `${trimZeros(t.toFixed(2))} s`
18 if (step >= 1e-4) return `${trimZeros((t * 1e3).toFixed(3))} ms`
19 return `${trimZeros((t * 1e6).toFixed(2))} µs`
22function trimZeros(s: string): string {
23 return s.includes('.') ? s.replace(/\.?0+$/, '') : s
26/** Gradient amplitude, Hz/m -> compact display in both unit systems. */
27export function formatGrad(hzPerM: number): string {
28 const mTm = (hzPerM / GAMMA) * 1e3
29 return `${(hzPerM / 1e3).toFixed(1)} kHz/m (${mTm.toFixed(2)} mT/m)`
32/** Slew rate, Hz/m/s -> display in T/m/s. */
33export function formatSlew(hzPerMPerS: number): string {
34 return `${(hzPerMPerS / GAMMA).toFixed(1)} T/m/s`
37export function formatNumber(v: number, digits = 6): string {
38 if (Number.isInteger(v)) return String(v)
39 return String(Number(v.toPrecision(digits)))
moveopenescclose