def to_joule(val=0.0, unit='J', dim=False): """Converts a value from any energy unit to Kelvin. Parameters: ----------- val -- (int) value scale -- (char) new scale Returns: -------- temp -- (Tensor) value in Joule or {'value' : temp, 'dim' : 'J'} -- (dict) dictionary of value and dimension Example: -------- >>> watthours = 300 >>> energy.to_joule(watthours, 'Wh') tensor(1080000., dtype=torch.float64) >>> electronvolt = [1, 2] >>> energy.to_joule(electronvolt, 'eV') tensor([1.6022e-19, 3.2044e-19], dtype=torch.float64) >>> energy.to_joule(electronvolt, 'eV', dim=True) {'val': tensor([1.6022e-19, 3.2044e-19], dtype=torch.float64), 'dim': 'J'} """ energy = T(val) if unit == 'J': energy = energy elif unit == 'KJ': energy = energy * 1000 elif unit == 'Wh': energy = energy * 3600 elif unit == 'KWh': energy = energy * 3.6 * constants.mega elif unit == 'eV': energy = energy * constants.eV.get('val') else: raise NotImplementedError( f'{unit} is not supported. See documentation for available units.') if dim == False: return energy else: return dict(val=energy, dim='J')
def test_to_bytes_from_kibibyte_scalar(self): bytes = to_bytes(1, 'KiB') assert torch.all(torch.eq(bytes, T(constants.kibi)))
def test_to_celsius_from_kelvin_list(self): celsius = to_celsius([0, 273.15], 'k') assert torch.all(torch.eq(celsius, T([-273.15, 0])))
def test_to_celsius_from_celsius_list(self): celsius = to_celsius([0, -273.15], 'c') assert torch.all(torch.eq(celsius, T([0, -273.15])))
def test_to_kelvin_default_values(self): kelvin = to_kelvin() assert torch.all(torch.eq(kelvin, T(0)))
def test_to_celsius_default_values(self): celsius = to_celsius() assert torch.all(torch.eq(celsius, T(0)))
def test_to_kelvin_from_fahrenheit_scalar(self): kelvin = to_kelvin(32, 'f') assert torch.all(torch.eq(kelvin, T(273.15)))
def test_to_kelvin_from_celsius_scalar(self): kelvin = to_kelvin(0, 'c') assert torch.all(torch.eq(kelvin, T(273.15)))
def test_to_fahrenheit_from_fahrenheit_scalar(self): fahrenheit = to_fahrenheit(32, 'f') assert torch.all(torch.eq(fahrenheit, T(32)))
def test_to_fahrenheit_with_dimension(self): fahrenheit = to_fahrenheit([273.15, 258.15], 'k', dim=True) assert isinstance(fahrenheit, dict) assert torch.all(torch.eq(fahrenheit['val'], T( [32, 5]))) and fahrenheit['dim'] == 'F'
def test_to_fahrenheit_default_values(self): fahrenheit = to_fahrenheit() assert torch.all(torch.eq(fahrenheit, T(0)))
def test_to_celsius_from_fahrenheit_list(self): celsius = to_celsius([32, 5], 'f') assert torch.all(torch.eq(celsius, T([0, -15])))
def to_bytes(val=0.0, unit='B', dim=False): """ Converts a value from any byte or bit format to bytes. Parameters: ----------- val : float Value(s) of units to be converted to Bytes. unit : char Specifies as a string the original unit from which the units will be converted to Bytes. Returns: -------- temp -- (Tensor) value in bytes Example: -------- >>> megabytes = 200 >>> digital.to_bytes(megabytes, 'MB') tensor(2.0000e+08, dtype=torch.float64) >>> kibibits = 1024 >>> digital.to_bytes(kibibits, 'Kib') tensor(131072., dtype=torch.float64) >>> digital.to_bytes(kibibits, 'Kib', dim=True) {'val': tensor(131072., dtype=torch.float64), 'dim': 'B'} """ # ds := digital storage ds = T(val) if 'b' not in unit: if unit == 'B': ds = ds elif unit == 'KB': ds = ds * constants.kilo elif unit == 'MB': ds = ds * constants.mega elif unit == 'GB': ds = ds * constants.giga elif unit == 'TB': ds = ds * constants.tera elif unit == 'PB': ds = ds * constants.peta elif unit == 'KiB': ds = ds * constants.kibi elif unit == 'MiB': ds = ds * constants.mebi elif unit == 'GiB': ds = ds * constants.gibi elif unit == 'TiB': ds = ds * constants.tebi elif unit == 'PiB': ds = ds * constants.pebi else: raise NotImplementedError( f'{unit} is not supported. See documentation for available units.' ) else: if unit == 'b': ds = ds / 8 elif unit == 'Kbit': ds = ds * 125 elif unit == 'Mbit': ds = ds * 125 * constants.kilo elif unit == 'Gbit': ds = ds * 125 * constants.mega elif unit == 'Tbit': ds = ds * 125 * constants.giga elif unit == 'Pbit': ds = ds * 125 * constants.tera elif unit == 'Kib': ds = ds * constants.kibi / 8 elif unit == 'Mib': ds = ds * constants.mebi / 8 elif unit == 'Gib': ds = ds * constants.gibi / 8 elif unit == 'Tib': ds = ds * constants.tebi / 8 elif unit == 'Pib': ds = ds * constants.pebi / 8 else: raise NotImplementedError( f'{unit} is not supported. See documentation for available units.' ) if dim == False: return ds else: return dict(val=ds, dim='B')
def to_bits(val=0.0, unit='b', dim=False): """Converts a value from any byte or bit format to bits. Parameters: ----------- val -- (int) value scale -- (char) new unit Returns: -------- temp -- (Tensor) value in Kelvin scale Example: -------- >>> bytes = 4 >>> digital.to_bits(bytes, 'B') tensor(32., dtype=torch.float64) >>> kilobits = 32 >>> digital.to_bits(kilobits, 'Kbit') tensor(32000., dtype=torch.float64) >>> digital.to_bits(kilobits, 'Kbit', dim=True) {'val': tensor(32000., dtype=torch.float64), 'dim': 'b'} """ # ds := digital storage if unit == 'b': ds = T(val) else: ds = to_bytes(val, unit) ds = ds * 8 if dim == False: return ds else: return dict(val=ds, dim='b') # def to_kilobits(val=0.0, unit='Kbit'): # """Converts a value from any byte or bit format to kilobits. # # Parameters: # ----------- # # val -- (int) value # scale -- (char) new unit # # Returns: # -------- # # temp -- (Tensor) value in Kelvin scale # # Example: # -------- # # >>> gigabytes = 500 # >>> digital.to_kilobits(gigabytes, 'GB') # tensor(4.0000e+09, dtype=torch.float64) # >>> mebibits = 32 # >>> digital.to_kilobits(mebibits, 'Mib') # tensor(33554.4320, dtype=torch.float64) # # """ # # # ds := digital storage # if unit == 'Kbit': # ds = T(val) # else: # ds = to_bytes(val, unit) # ds = ds * 8 * constants.milli # # return ds # # def to_megabits(val=0.0, unit='Mbit'): # """Converts a value from any byte or bit format to megabits. # # Parameters: # ----------- # # val -- (int) value # scale -- (char) new unit # # Returns: # -------- # # temp -- (Tensor) value in Kelvin scale # # Example: # -------- # # >>> petabytes = 4 # >>> digital.to_megabits(petabytes, 'PB') # tensor(3.2000e+10, dtype=torch.float64) # >>> bits = 1024 # >>> digital.to_megabits(bits, 'b') # tensor(0.0010, dtype=torch.float64) # # """ # # # ds := digital storage # if unit == 'Mbit': # ds = T(val) # else: # ds = to_bytes(val, unit) # ds = ds * 8 * constants.micro # # return ds # # def to_gigabits(val=0.0, unit='Gbit'): # """Converts a value from any byte or bit format to gigabits. # # Parameters: # ----------- # # val -- (int) value # scale -- (char) new unit # # Returns: # -------- # # temp -- (Tensor) value in Kelvin scale # # Example: # -------- # # >>> kilobytes = 400 # >>> digital.to_gigabits(kilobytes, 'KB') # tensor(0.0032, dtype=torch.float64) # >>> pebibit = 400 # >>> digital.to_gigabits(pebibit, 'Pib') # tensor(4.5036e+08, dtype=torch.float64) # # """ # # # ds := digital storage # if unit == 'Gbit': # ds = T(val) # else: # ds = to_bytes(val, unit) # ds = ds * 8 * constants.nano # # return ds # # def to_terabits(val=0.0, unit='Tbit'): # """Converts a value from any byte or bit format to terabits. # # Parameters: # ----------- # # val -- (int) value # scale -- (char) new unit # # Returns: # -------- # # temp -- (Tensor) value in Kelvin scale # # Example: # -------- # # >>> bytes = 6900 # >>> digital.to_terabits(bytes, 'B') # tensor(5.5200e-08, dtype=torch.float64) # >>> terabits = 45 # >>> digital.to_terabits(terabits, 'Tbit') # tensor(45., dtype=torch.float64) # # """ # # # ds := digital storage # if unit == 'Tbit': # ds = T(val) # else: # ds = to_bytes(val, unit) # ds = ds * 8 * constants.pico # # return ds # # def to_petabits(val=0.0, unit='Pbit'): # """Converts a value from any byte or bit format to petabits. # # Parameters: # ----------- # # val -- (int) value # scale -- (char) new unit # # Returns: # -------- # # temp -- (Tensor) value in Kelvin scale # # Example: # -------- # # >>> mebibytes = 465 # >>> digital.to_petabits(mebibytes, 'MiB') # tensor(3.9007e-06, dtype=torch.float64) # >>> tebibits = 34 # >>> digital.to_petabits(tebibits, 'Tib') # tensor(0.0374, dtype=torch.float64) # # # """ # # # ds := digital storage # if unit == 'Pbit': # ds = T(val) # else: # ds = to_bytes(val, unit) # ds = ds * 8 * constants.femto # # return ds # # def to_kibibits(val=0.0, unit='Kib'): # """Converts a value from any byte or bit format to kibibits. # # Parameters: # ----------- # # val -- (int) value # scale -- (char) new unit # # Returns: # -------- # # temp -- (Tensor) value in Kelvin scale # # Example: # -------- # # >>> gibibyte = 23 # >>> digital.to_kibibits(gibibyte, 'GiB') # tensor(1.9294e+08, dtype=torch.float64) # >>> gigabit = 56 # >>> digital.to_kibibits(gigabit, 'Gbit') # tensor(54687500., dtype=torch.float64) # # """ # # # ds := digital storage # if unit == 'Kib': # ds = T(val) # else: # ds = to_bytes(val, unit) # ds = ds / (constants.kibi / 8) # # return ds # # def to_mebibits(val=0.0, unit='Mib'): # """Converts a value from any byte or bit format to mebibits. # # Parameters: # ----------- # # val -- (int) value # scale -- (char) new unit # # Returns: # -------- # # temp -- (Tensor) value in Kelvin scale # # Example: # -------- # # >>> tebibyte = 5 # >>> digital.to_mebibits(tebibyte, 'TiB') # tensor(41943040., dtype=torch.float64) # >>> bit = 45 # >>> digital.to_mebibits(bit, 'b') # tensor(4.2915e-05, dtype=torch.float64) # # """ # # # ds := digital storage # if unit == 'Mib': # ds = T(val) # else: # ds = to_bytes(val, unit) # ds = ds / (constants.mebi / 8) # # return ds # # def to_gibibits(val=0.0, unit='Gib'): # """Converts a value from any byte or bit format to gibibits. # # Parameters: # ----------- # # val -- (int) value # scale -- (char) new unit # # Returns: # -------- # # temp -- (Tensor) value in Kelvin scale # # Example: # -------- # # >>> kilobyte = 45 # >>> digital.to_gibibits(kilobyte, 'KB') # tensor(0.0003, dtype=torch.float64) # >>> mebibit = 235 # >>> digital.to_gibibits(mebibit, 'Mib') # tensor(0.2295, dtype=torch.float64) # # """ # # # ds := digital storage # if unit == 'Gib': # ds = T(val) # else: # ds = to_bytes(val, unit) # ds = ds / (constants.gibi / 8) # # return ds # # def to_tebibits(val=0.0, unit='Tib'): # """Converts a value from any byte or bit format to tebibits. # # Parameters: # ----------- # # val -- (int) value # scale -- (char) new unit # # Returns: # -------- # # temp -- (Tensor) value in Kelvin scale # # Example: # -------- # # >>> byte = 5 # >>> digital.to_tebibits(byte, 'B') # tensor(3.6380e-11, dtype=torch.float64) # >>> bit = 23 # >>> digital.to_tebibits(bit, 'b') # tensor(2.0918e-11, dtype=torch.float64) # # """ # # # ds := digital storage # if unit == 'Tib': # ds = T(val) # else: # ds = to_bytes(val, unit) # ds = ds / (constants.tebi / 8) # # return ds # # def to_pebibits(val=0.0, unit='Pib'): # """Converts a value from any byte or bit format to pebibits. # # Parameters: # ----------- # # val -- (int) value # scale -- (char) new unit # # Returns: # -------- # # temp -- (Tensor) value in Kelvin scale # # Example: # -------- # # >>> petabyte = 15 # >>> digital.to_pebibits(petabyte, 'PB') # tensor(106.5814, dtype=torch.float64) # >>> megabit = 1 # >>> digital.to_pebibits(megabit, 'Mbit') # tensor(8.8818e-10, dtype=torch.float64) # # """ # # # ds := digital storage # if unit == 'Pib': # ds = T(val) # else: # ds = to_bytes(val, unit) # ds = ds / (constants.pebi / 8) # # return ds
def test_to_kelvin_from_kelvin_scalar(self): kelvin = to_kelvin(0, 'k') assert torch.all(torch.eq(kelvin, T(0)))
def test_to_fahrenheit_from_fahrenheit_list(self): fahrenheit = to_fahrenheit([32, 0], 'f') assert torch.all(torch.eq(fahrenheit, T([32, 0])))
def test_to_kelvin_from_kelvin_list(self): kelvin = to_kelvin([0, 273.15], 'k') assert torch.all(torch.eq(kelvin, T([0, 273.15])))
def test_to_fahrenheit_from_celsius_scalar(self): fahrenheit = to_fahrenheit(0, 'c') assert torch.all(torch.eq(fahrenheit, T(32)))
def test_to_kelvin_from_celsius_list(self): kelvin = to_kelvin([0, -273.15], 'c') assert torch.all(torch.eq(kelvin, T([273.15, 0])))
def test_to_fahrenheit_from_celsius_list(self): fahrenheit = to_fahrenheit([0, -15], 'c') assert torch.all(torch.eq(fahrenheit, T([32, 5])))
def test_to_kelvin_from_fahrenheit_list(self): kelvin = to_kelvin([32, 5], 'f') assert torch.all(torch.eq(kelvin, T([273.15, 258.15])))
def test_to_fahrenheit_from_kelvin_scalar(self): fahrenheit = to_fahrenheit(273.15, 'k') assert torch.all(torch.eq(fahrenheit, T(32)))
def test_to_celsius_with_dimension(self): celsius = to_celsius([32, 5], 'f', dim=True) assert isinstance(celsius, dict) assert torch.all(torch.eq(celsius['val'], T( [0, -15]))) and celsius['dim'] == 'C'
def test_to_fahrenheit_from_kelvin_list(self): fahrenheit = to_fahrenheit([273.15, 258.15], 'k') assert torch.all(torch.eq(fahrenheit, T([32, 5])))
def test_to_celsius_from_celsius_scalar(self): celsius = to_celsius(0, 'c') assert torch.all(torch.eq(celsius, T(0)))
def test_to_kelvin_tensor(self): kelvin_tensor = T([0, 0]) kelvin = to_kelvin(kelvin_tensor) assert torch.all(torch.eq(kelvin, T([0, 0])))
def test_to_celsius_from_kelvin_scalar(self): celsius = to_celsius(0, 'k') assert torch.all(torch.eq(celsius, T(-273.15)))
def test_to_kelvin_with_dimension(self): kelvin = to_kelvin([0, -273.15], 'c', dim=True) assert isinstance(kelvin, dict) assert torch.all(torch.eq(kelvin['val'], T( [273.15, 0]))) and kelvin['dim'] == 'K'
def test_to_celsius_from_fahrenheit_scalar(self): celsius = to_celsius(32, 'f') assert torch.all(torch.eq(celsius, T(0)))
def test_to_bytes_from_petabyte_list(self): bytes = to_bytes([0, 1], 'PB') assert torch.all(torch.eq(bytes, T([0, constants.peta])))