-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1624 from braingram/eslavich-simpler-integer-type
Eslavich simpler integer type
- Loading branch information
Showing
5 changed files
with
49 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import numpy as np | ||
|
||
from asdf.extension import Converter | ||
|
||
|
||
class IntegerConverter(Converter): | ||
tags = [ | ||
"tag:stsci.edu:asdf/core/integer-1.0.0", | ||
"tag:stsci.edu:asdf/core/integer-1.1.0", | ||
] | ||
|
||
types = ["asdf.tags.core.integer.IntegerType"] | ||
|
||
def to_yaml_tree(self, obj, tag, ctx): | ||
abs_value = int(np.abs(obj._value)) | ||
|
||
# pack integer value into 32-bit words | ||
words = [] | ||
value = abs_value | ||
while value > 0: | ||
words.append(value & 0xFFFFFFFF) | ||
value >>= 32 | ||
|
||
array = np.array(words, dtype=np.uint32) | ||
|
||
tree = {} | ||
ctx.set_array_storage(array, obj._storage) | ||
tree["words"] = array | ||
tree["sign"] = obj._sign | ||
tree["string"] = str(int(obj._value)) | ||
|
||
return tree | ||
|
||
def from_yaml_tree(self, node, tag, ctx): | ||
from asdf.tags.core.integer import IntegerType | ||
|
||
value = 0 | ||
for x in node["words"][::-1]: | ||
value <<= 32 | ||
value |= int(x) | ||
|
||
if node["sign"] == "-": | ||
value = -value | ||
|
||
return IntegerType(value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters