def test_convert_back_quantities(): pq_dict = {"value": 1, "unit": "m"} result = convert_back_quantities(pq_dict) assert result == pq.Quantity(1, "m") pq_dict = {"value": [1, 2, 3], "unit": "m"} result = convert_back_quantities(pq_dict) assert np.array_equal(result, pq.Quantity([1, 2, 3], "m")) pq_dict = {"value": [1, 2, 3]} result = convert_back_quantities(pq_dict) assert result == pq_dict result = convert_back_quantities(1) assert result == 1 result = convert_back_quantities(2.3) assert result == 2.3 pq_dict = {"unit": "m", "uncertainty": [3, 4], "value": [1.0, 2.0]} result = convert_back_quantities(pq_dict) pq_value = pq.UncertainQuantity([1, 2], "m", [3, 4]) assert isinstance(result, pq.UncertainQuantity) assert result.magnitude.tolist() == pq_value.magnitude.tolist() assert result.dimensionality.string == pq_value.dimensionality.string assert result.uncertainty.magnitude.tolist( ) == pq_value.uncertainty.magnitude.tolist() pq_dict = { "quantity": { "unit": "m", "value": 1 }, "uq_quantity": { "unit": "m", "uncertainty": [3, 4], "value": [1.0, 2.0] } } pq_values = { "quantity": pq.Quantity(1, "m"), "uq_quantity": pq.UncertainQuantity([1, 2], "m", [3, 4]) } result = convert_back_quantities(pq_values) assert result == pq_values pq_values = {"list": [1, 2, 3], "quantity": {"unit": "m", "value": 1}} result = convert_back_quantities(pq_values) assert result == {"list": [1, 2, 3], "quantity": pq.Quantity(1, "m")}
def test_convert_quantities(): pq_value = pq.Quantity(1, "m") result = convert_quantities(pq_value) assert result == {"value": 1, "unit": "m"} pq_value = pq.Quantity([1, 2, 3], "m") result = convert_quantities(pq_value) assert result == {"value": [1, 2, 3], "unit": "m"} result = convert_quantities(np.array([1, 2, 3])) assert result == [1, 2, 3] result = convert_quantities(1) assert result == 1 result = convert_quantities(2.3) assert result == 2.3 pq_value = pq.UncertainQuantity([1, 2], "m", [3, 4]) result = convert_quantities(pq_value) assert result == {"unit": "m", "uncertainty": [3, 4], "value": [1.0, 2.0]} pq_values = { "quantity": pq.Quantity(1, "m"), "uq_quantity": pq.UncertainQuantity([1, 2], "m", [3, 4]) } result = convert_quantities(pq_values) assert (result == { "quantity": { "unit": "m", "value": 1 }, "uq_quantity": { "unit": "m", "uncertainty": [3, 4], "value": [1.0, 2.0] } }) pq_values = {"list": [1, 2, 3], "quantity": pq.Quantity(1, "m")} pq_dict = {"list": [1, 2, 3], "quantity": {"unit": "m", "value": 1}} result = convert_quantities(pq_values) assert result == pq_dict
def __init__(self): self.cell = Organelle("cell") self.nucleus = Organelle('nucleus') self.cell.diameter = pq.Quantity(5, 'um') self.cell.volume = pq.Quantity(50, 'um**3') self.nucleus.diameter = pq.Quantity(2, 'um') self.nucleus.volume = pq.UncertainQuantity(2.9, 'um**3', 0.2) pq.nucleur_volume = pq.UnitQuantity('nuclear volume', self.nucleus.volume, symbol='NucVol') pq.cell_volume = pq.UnitQuantity('cellular volume', self.cell.volume, symbol='CellVol')
def convert_back_quantities(value): """Convert quantities back from dictionary.""" result = value if isinstance(value, dict): if "unit" in value and "value" in value and "uncertainty" in value: try: result = pq.UncertainQuantity(value["value"], value["unit"], value["uncertainty"]) except Exception: pass elif "unit" in value and "value" in value: try: result = pq.Quantity(value["value"], value["unit"]) except Exception: pass else: try: for key, value in result.items(): result[key] = convert_back_quantities(value) except AttributeError: pass return result
def convert_from_firebase(value): """ Converts quantities back from dictionary """ result = value if isinstance(value, dict): if "_force_dict" in value: del value["_force_dict"] if 'units' in value and "value" in value: value['unit'] = value['units'] del(value['units']) if "unit" in value and "value" in value: if "uncertainty" in value: try: result = pq.UncertainQuantity(value["value"], value["unit"], value["uncertainty"]) except Exception: pass else: try: result = pq.Quantity(value["value"], value["unit"]) except Exception: pass else: try: for key, value in result.items(): result[key] = convert_from_firebase(value) except AttributeError: pass if isinstance(result, str): if result == 'NaN': result = np.nan elif isinstance(result, list): result = [v if v != 'NaN' else np.nan for v in result] return result
def test_quantity_uncertain(): t = pq.UncertainQuantity(7., pq.second, 1.) q1 = Quantity(t) assert q1.dumps() == r'\SI{7.0 +- 1.0}{\second}'
options={ 'round-precision': 4, 'round-mode': 'figures' }) math = Math(data=['F=', q1]) subsection.append(math) section.append(subsection) subsection = Subsection('Scalars without units') world_population = 7400219037 N = Quantity(world_population, options={ 'round-precision': 2, 'round-mode': 'figures' }, format_cb="{0:23.17e}".format) subsection.append(Math(data=['N=', N])) section.append(subsection) subsection = Subsection('Scalars with uncertainties') width = pq.UncertainQuantity(7.0, pq.meter, .4) length = pq.UncertainQuantity(6.0, pq.meter, .3) area = Quantity(width * length, options='separate-uncertainty', format_cb=lambda x: "{0:.1f}".format(float(x))) subsection.append(Math(data=['A=', area])) section.append(subsection) doc.append(section) doc.generate_pdf('quantities_ex')
def __init__(self): self.Kd = pq.UncertainQuantity(1.2, pq.nM, 0.1) # From literature self.koff = pq.Quantity(5.5e-5, 1 / pq.s).rescale(1 / pq.min) self.kon = pq.Quantity(4.6e4, 1 / (pq.s * pq.M)) self.kon = self.kon.rescale(1 / (pq.min * pq.nM))