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

Create new metadata fields that normalize units #501

Open
mccalluc opened this issue May 24, 2022 · 1 comment
Open

Create new metadata fields that normalize units #501

mccalluc opened this issue May 24, 2022 · 1 comment

Comments

@mccalluc
Copy link
Contributor

mccalluc commented May 24, 2022

When it was just for display, it didn't matter, but now that there are histograms, sorting, etc., we need numeric fields that are all on the same scale. Related to @john-conroy's search page work.

Not clear how to choose the best unit when their are multiple... Always choose the first or the last? or for that matter could map it to multiple scales...

@mccalluc mccalluc self-assigned this May 24, 2022
@mccalluc
Copy link
Contributor Author

Proposal:

from pint.unit import Unit

metadata = {
     'xyz_value': 1000, 'xyz_unit': 'ng',
     'abc_value': 60, 'abc_unit': 's'
}

target_units = ['g', 'kg', 'second', 'year'] # add or remove from this list as desired.

def _units_map(metadata):
    fields = metadata.keys()
    value_stems = set(f.replace('_value', '') for f in fields if f.endswith('_value'))
    unit_stems = set(f.replace('_unit', '') for f in fields if f.endswith('_unit'))
    stems = value_stems & unit_stems
    for stem in stems:
        q = metadata[f'{stem}_value'] * Unit(metadata[f'{stem}_unit'])
        for target_unit in target_units:
            if not q.units.is_compatible_with(target_unit):
                continue 
            q_conv = q.to(target_unit)
            
            metadata[f'{stem}_in_{q_conv.units}'] = float(f"{q_conv.magnitude:.3g}")
    return metadata

_units_map(metadata)
{'abc_in_second': 60.0,
 'abc_in_year': 1.9e-06,
 'abc_unit': 's',
 'abc_value': 60,
 'xyz_in_gram': 1e-06,
 'xyz_in_kilogram': 1e-09,
 'xyz_unit': 'ng',
 'xyz_value': 1000}

@mccalluc mccalluc removed their assignment Mar 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant