Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BIDS-compatible automatic reformatting of units of measure #222

Merged
merged 56 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
a494a27
add check_trigger_amount_std method
vinferrer Mar 23, 2020
d8f58cb
add option to activate check_trigger_amount_std
vinferrer Mar 23, 2020
eee1223
add if condition to use either check_trigger_amount or check_trigger_…
vinferrer Mar 23, 2020
2148737
correct linting
vinferrer Mar 23, 2020
7e40f0d
merge check_trigger_amount functions in one
vinferrer Mar 24, 2020
e99dcbd
delete std_option and if statement
vinferrer Mar 24, 2020
98c7ba1
delete std_option and make 0 the threshold defalut
vinferrer Mar 24, 2020
640159a
change legend so it's more understandable
vinferrer Mar 24, 2020
9e9a67b
change thr default to None
vinferrer Mar 24, 2020
64efe10
delete unused function
vinferrer Mar 25, 2020
6920301
Update phys2bids/cli/run.py
vinferrer Mar 24, 2020
b604650
Update phys2bids/physio_obj.py
vinferrer Mar 24, 2020
dde563e
Update phys2bids/physio_obj.py
vinferrer Mar 24, 2020
a31c167
now we don't use the derivative, we only use the trigger channel
vinferrer Mar 25, 2020
161ca65
now -tr and -ntp is needed to run the check_trigger_amount function
vinferrer Mar 25, 2020
8314d0e
Update phys2bids/cli/run.py
vinferrer Mar 25, 2020
5d17060
Update phys2bids/cli/run.py
vinferrer Mar 25, 2020
d09e977
correct syntax errors
vinferrer Mar 25, 2020
714f38b
adding thr as attribute to the class and define it as float
vinferrer Mar 26, 2020
fdc123a
delete thr=phys_in.thr line
vinferrer Mar 26, 2020
112827b
add space to log message
vinferrer Mar 26, 2020
adda4ff
change a bit log message
vinferrer Mar 26, 2020
0a530dd
change or statement for and statement
vinferrer Mar 26, 2020
97ce038
change log
vinferrer Mar 26, 2020
6eb8b8f
Fixes integration tests
eurunuela Mar 26, 2020
9531927
Very tiny text changes
Mar 26, 2020
fc50a05
initialize self.num_timepoints_found = None
vinferrer Mar 26, 2020
170431e
update docstring
vinferrer Mar 26, 2020
f5b8ecf
Update phys2bids/physio_obj.py
vinferrer Mar 26, 2020
4cf435d
Add unit aliases dictionary
Mar 28, 2020
6637b93
Added prefix dictionary
Apr 22, 2020
2e9a6e1
Merge remote-tracking branch 'upstream/master' into enh/bids-units
Apr 23, 2020
6ddffe1
Merge branch 'enh/bids-units' of github.com:smoia/phys2bids into bids…
vinferrer May 21, 2020
2dcef64
functions for units bids formatting
vinferrer May 21, 2020
a606741
pass bidsify_units to bids_units.py
vinferrer May 21, 2020
8d44ab3
solve linting problem
vinferrer May 21, 2020
dc0b896
use bidify_units after creating phys_in
vinferrer May 22, 2020
5436c02
tiny change in submultipliers
vinferrer May 22, 2020
ceeefdb
Update phys2bids/bids_units.py
vinferrer May 24, 2020
a3800d3
Merge branch 'master' of https://github.com/physiopy/phys2bids into b…
vinferrer May 26, 2020
3ca50f1
adding test file
vinferrer May 26, 2020
7d64f07
changing one prefix
vinferrer May 26, 2020
86bc61e
add asserts
vinferrer May 26, 2020
42a4165
use endswith
vinferrer May 28, 2020
60f6ade
avoid iterating dict by using .get() build in funtion
vinferrer May 28, 2020
30891cf
use unit = unit[:-len(u_key)] instead of .replace
vinferrer May 28, 2020
12d90b9
change commas type to ''
vinferrer May 28, 2020
ca6685f
Update phys2bids/bids_units.py
vinferrer May 29, 2020
8d6ac5a
Update phys2bids/bids_units.py
vinferrer May 29, 2020
e2de339
uppercase asserts
vinferrer May 29, 2020
3c78d28
accept stefano's suggestion
vinferrer May 29, 2020
032efec
complete smoia suggestion
vinferrer May 29, 2020
57850f6
reorder unit dict
vinferrer May 29, 2020
b723bea
adding assert
vinferrer May 29, 2020
4520c06
Merge branch 'master' of https://github.com/physiopy/phys2bids into b…
vinferrer Jun 2, 2020
d068faf
add str.lower() optimization
vinferrer Jun 2, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions phys2bids/bids_units.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import logging

LGR = logging.getLogger(__name__)

unit_aliases = {
rmarkello marked this conversation as resolved.
Show resolved Hide resolved
# kelvin: thermodynamic temperature
'k': 'K', 'kelvin': 'K', 'kelvins': 'K',
# mole: amount of substance
'mol': 'mol', 'mole': 'mol',
# newton: force, weight
'newton': 'N', 'newtons': 'N', 'n': 'N',
# pascal: pressure, stress
'pascal': 'Pa', 'pascals': 'Pa', 'pa': 'Pa',
# volt: voltage (electrical potential), emf
'v': 'V', 'volt': 'V', 'volts': 'V',
# degree Celsius: temperature relative to 273.15 K
'°c': '°C', '°celsius': '°C', 'celsius': '°C',
# ampere: electric current
'a': 'A', 'ampere': 'A', 'amp': 'A', 'amps': 'A',
# second: time and hertzs
'1/hz': 's', '1/hertz': 's', 'hz': 'Hz',
'1/s': 'Hz', '1/second': 'Hz', '1/seconds': 'Hz',
'1/sec': 'Hz', '1/secs': 'Hz', 'hertz': 'Hz',
'second': 's', 'seconds': 's', 'sec': 's',
'secs': 's', 's': 's',
}

# Init dictionary of aliases for multipliers. Entries are still lowercase
prefix_aliases = {
# Multiples - skip "mega" and only up to "tera"
'da': 'da', 'deca': 'da', 'h': 'h', 'hecto': 'h',
'k': 'k', 'kilo': 'k', 'g': 'G', 'giga': 'G', 't': 'T',
'tera': 'T',
# Submultipliers
'd': 'd', 'deci': 'd', 'c': 'c', 'centi': 'c',
'milli': 'm', 'm': 'm', 'µ': 'µ', 'micro': 'µ',
'n': 'n', 'nano': 'n', 'p': 'p', 'pico': 'p',
'f': 'f', 'femto': 'f', 'a': 'a', 'atto': 'a',
'z': 'z', 'zepto': 'z', 'y': 'y', 'yocto': 'y',
}


def bidsify_units(orig_unit):
"""
Read the input unit of measure and use the dictionary of aliases
to bidsify its value.
It is possible to make simple conversions

Parameters
----------
unit: string
Unit of measure, might or might not be BIDS compliant.

Returns
-------
new_unit: str
BIDSified alias of input unit

Notes
-----
This function should implement a double check, one for unit and
the other for prefixes (e.g. "milli"). However, that is going to be tricky,
unless there is a weird way to multiply two dictionaries together.
"""
# call prefix and unit dicts
# for every unit alias in the dict
orig_unit = orig_unit.lower()
for u_key in unit_aliases.keys():
if orig_unit.endswith(u_key):
new_unit = unit_aliases[u_key]
unit = orig_unit[:-len(u_key)]
if unit != '':
# for every prefix alias
prefix = prefix_aliases.get(unit, '')
if prefix == '':
LGR.warning(f'The given unit prefix {unit} does not have aliases, '
f'passing it as is')
prefix = orig_unit[:len(unit)]
return prefix + new_unit
else:
return new_unit
LGR.warning(f'The given unit {orig_unit} does not have aliases, '
f'passing it as is')
return orig_unit
3 changes: 3 additions & 0 deletions phys2bids/phys2bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from phys2bids import utils, viz, _version
from phys2bids.cli.run import _get_parser
from phys2bids.physio_obj import BlueprintOutput
from phys2bids.bids_units import bidsify_units

LGR = logging.getLogger(__name__)

Expand Down Expand Up @@ -265,6 +266,8 @@ def phys2bids(filename, info=False, indir='.', outdir='.', heur_file=None,

LGR.info(f'Reading the file {infile}')
phys_in = populate_phys_input(infile, chtrig)
for index, unit in enumerate(phys_in.units):
phys_in.units[index] = bidsify_units(unit)
LGR.info('Reading infos')
phys_in.print_info(filename)
# #!# Here the function viz.plot_channel should be called
Expand Down
14 changes: 14 additions & 0 deletions phys2bids/tests/test_bids_units.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from phys2bids.bids_units import bidsify_units
from phys2bids.bids_units import unit_aliases


def test_bidsify_units():
vinferrer marked this conversation as resolved.
Show resolved Hide resolved
# test unit with standard prefix
assert bidsify_units('centik') == 'cK'
# test unit with not standard prefix
assert bidsify_units('matV') == 'matV'
# test unit that's not bids standard
assert bidsify_units('mmlie') == 'mmlie'
# test there is not problem with every unit in the dict
for unit_key in unit_aliases.keys():
assert bidsify_units(unit_key) == unit_aliases[unit_key]