Skip to content

[Deprecation] Correct cases for memory units in core.units and fix unit_type detection of FloatWithUnit.from_str#3838

Merged
shyuep merged 27 commits into
materialsproject:masterfrom
DanielYang59:fix-core-unit-memory-case
Jun 17, 2024
Merged

[Deprecation] Correct cases for memory units in core.units and fix unit_type detection of FloatWithUnit.from_str#3838
shyuep merged 27 commits into
materialsproject:masterfrom
DanielYang59:fix-core-unit-memory-case

Conversation

@DanielYang59

@DanielYang59 DanielYang59 commented May 18, 2024

Copy link
Copy Markdown
Contributor

Summary

Correct cases for memory units in core.units

I assume this is another breaking fix, so I marked the incorrect units as deprecated until 2025-01-01. Any suggestion would be appreciated.

Currently the case of byte is incorrect in core.units, where B refers to "byte" and b refers to "bit".
Also note 1 byte = 8 bit so we cannot use them interchangeably.

"memory": {
"byte": 1,
"Kb": 1024,
"Mb": 1024**2,
"Gb": 1024**3,
"Tb": 1024**4,
},

Case sensitivity for memory units

I assume there is no good reason to remove case sensitivity for memory alone (case sensitivity of other units is maintained), especially when the case of memory has its meaning:

# Accept kb, mb, gb ... as well.
BASE_UNITS["memory"].update({k.lower(): v for k, v in BASE_UNITS["memory"].items()})

Fix FloatWithUnit.from_str to correctly detect unit_type from string with space between value and unit

Previously for a string with space (which adheres to the SI Unit rules) like Memory.from_str("1 MB"), the string would be split into 1 and MB, and the space in MB prevents the unit_type being detected properly and ended up being None.

from pymatgen.core.units import Memory

mem_0 = Memory.from_str("1MB")

print(mem_0.unit_type)  # >>> "memory"

mem_1 = Memory.from_str("1 MB")

print(mem_1.unit_type)  # >>> None

@DanielYang59 DanielYang59 marked this pull request as ready for review May 18, 2024 03:50
@DanielYang59 DanielYang59 marked this pull request as draft May 18, 2024 03:53
@DanielYang59 DanielYang59 changed the title Correct cases for memory units Correct cases for memory units in core.units May 18, 2024
@DanielYang59 DanielYang59 marked this pull request as ready for review May 18, 2024 04:04
Comment thread tests/core/test_units.py Outdated
@DanielYang59 DanielYang59 marked this pull request as draft May 18, 2024 05:31
@DanielYang59 DanielYang59 changed the title Correct cases for memory units in core.units [Deprecation] Correct cases for memory units in core.units May 18, 2024
@DanielYang59 DanielYang59 marked this pull request as ready for review May 18, 2024 06:03
@DanielYang59

Copy link
Copy Markdown
Contributor Author

@shyuep Can I have your advice on this please? This should be very similar to the change to Element, and I scheduled the same grace period until 2025-01-01

@DanielYang59 DanielYang59 changed the title [Deprecation] Correct cases for memory units in core.units [Deprecation] Correct cases for memory units in core.units and fix unit_type detection of FloatWithUnit.from_str May 18, 2024
@DanielYang59

DanielYang59 commented Jun 12, 2024

Copy link
Copy Markdown
Contributor Author

@shyuep Can you please review this? It should be very similar to #3817.

The rationale for this change is that something like KB (kilobyte) and kb(kilobit) refer to different units for memory (or storage in general), and as such it should not be case insensitive.

@DanielYang59 DanielYang59 marked this pull request as draft June 12, 2024 01:36
@DanielYang59 DanielYang59 marked this pull request as ready for review June 12, 2024 01:37
@shyuep shyuep enabled auto-merge (squash) June 14, 2024 16:54
@shyuep

shyuep commented Jun 14, 2024

Copy link
Copy Markdown
Member

This is fine. I am enabling auto-merge. There was a failing test that needs to be fixed.

@DanielYang59

DanielYang59 commented Jun 15, 2024

Copy link
Copy Markdown
Contributor Author

Thanks. I notice that too e572d34#r143073539, perhaps we also want to rename the id argument to avoid shadowing the builtin in e572d34#r143131235?

@shyuep

shyuep commented Jun 15, 2024

Copy link
Copy Markdown
Member

Thanks. I renamed id to id_

@DanielYang59

Copy link
Copy Markdown
Contributor Author

Thanks!

@shyuep shyuep merged commit 9719845 into materialsproject:master Jun 17, 2024
@DanielYang59 DanielYang59 deleted the fix-core-unit-memory-case branch June 18, 2024 11:24
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

Successfully merging this pull request may close these issues.

Incorrect letter cases for memory in core.units

3 participants