简体   繁体   English

我的代码以某种方式乘以 0.5 倍,我不知道为什么

[英]My code somehow multiplies by a factor of 0.5 and I don't know why

I wrote a unit converter for one of my plotting scripts.我为我的一个绘图脚本编写了一个单位转换器。 This converter works by determining the unit of the provided data, converting it to SI-units and then converting it into a target unit.该转换器的工作原理是确定所提供数据的单位,将其转换为 SI 单位,然后将其转换为目标单位。

It works without issues for all unit conversions, except for one: The target unit 'mmol/g'.它对所有单位转换都没有问题,除了一个:目标单位“毫摩尔/克”。

Here's the code for the converter:这是转换器的代码:

import scipy.constants as units
import numpy as np

example_data = np.arange(10,20)

def unit_converter(unit,target_unit,numerical_data):

    unit_converter_dict = {'kPa':units.kilo,
                           'atm':units.atm,
                           'bar':units.bar,
                           'mmHg':units.mmHg,
                           'psi':units.psi,
                           'cc/g':units.centi**3/units.gram,
                           'cm/g':1/units.gram,
                           'cm/kg':1,
                           'mmol/g':1/(22.41396954)}

    converted_data = numerical_data*(unit_converter_dict[unit]/unit_converter_dict[target_unit])
    return converted_data

print(unit_converter('cc/g','mmol/g',example_data))

For some reason the converted data is exactly half of what it should be and I can't figure out why.出于某种原因,转换后的数据正好是它应该的一半,我不知道为什么。 If you don't know why mmol/g is supposed to be 22.41..., that's because one mole of an ideal gas at 1.0325 bar pressure and 0 °C takes up a volume of 22.41... liters.如果您不知道为什么 mmol/g 应该是 22.41...,那是因为在 1.0325 bar 压力和 0°C 下,一摩尔理想气体的体积为 22.41...升。

So when I start with cc/g and convert that to cm/kg, I should only need to multiply by 22.41 mmol/g to get the correct conversion.因此,当我从 cc/g 开始并将其转换为 cm/kg 时,我只需乘以 22.41 mmol/g 即可获得正确的转换。 But for some reason, all my values are only half as big as they should be after the conversion.但由于某种原因,我所有的值都只有转换后的一半。

I get the correct result if I use 2*22.41... btw.如果我使用 2*22.41... 顺便说一句,我会得到正确的结果。

Does anyone know why this happens?有谁知道为什么会这样? Where does the factor 2 (or conversely, 1/2) come from?因子 2(或相反,1/2)从何而来?

I have already established that this issue is caused by this part of the code and not by some other operation further downstream.我已经确定这个问题是由这部分代码引起的,而不是由下游的其他操作引起的。

It's not off by 1/2, though it's close.它没有关闭 1/2,尽管它很接近。 Also, since you're in mmol, I think you want 1000/22.41396954.另外,因为你的单位是 mmol,我你想要 1000/22.41396954。 Also, since you're doubly dividing, I think the units need to be flipped when you convert.另外,由于您要进行双重除法,因此我认为转换时需要翻转单位。

import scipy.constants as units
import numpy as np

example_data = np.arange(10,20)

def unit_converter(unit,target_unit,numerical_data):

    unit_converter_dict = {'kPa':units.kilo,
                            'atm':units.atm,
                            'bar':units.bar,
                            'mmHg':units.mmHg,
                            'psi':units.psi,
                            'cc/g':(units.centi**3 /units.gram),
                            'cm/g':1/units.gram,
                            'cm/kg':1,
                            'mmol/g': (22.41396954/1000)}

    converted_data = numerical_data*(unit_converter_dict[unit] / unit_converter_dict[target_unit])
    return converted_data

print(unit_converter('cc/g','mmol/g',example_data))

# [0.44615033 0.49076537 0.5353804  0.57999543 0.62461047 0.6692255 0.71384053 0.75845557 0.8030706  0.84768563]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM