-
Notifications
You must be signed in to change notification settings - Fork 13
Decoding binary data #136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
srmukher
wants to merge
1
commit into
main
Choose a base branch
from
users/srmukher/Decode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Decoding binary data #136
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
apps/plotly_examples/python-scripts/seval/decode_binary_data.py
This file contains hidden or 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,99 @@ | ||
import os | ||
import base64 | ||
import numpy as np | ||
import json | ||
|
||
# Define the path to the data folder | ||
data_folder = '../../src/data' | ||
|
||
# Function to check if a string is base64-encoded | ||
def is_base64(s): | ||
try: | ||
if isinstance(s, str): | ||
s_bytes = s.encode('ascii') | ||
elif isinstance(s, bytes): | ||
s_bytes = s | ||
else: | ||
raise ValueError("Input must be a string or bytes") | ||
return base64.b64encode(base64.b64decode(s_bytes)) == s_bytes | ||
except Exception: | ||
return False | ||
|
||
# Helper function to decode base64-encoded data based on dtype | ||
def decode_base64(value, dtype): | ||
decoded_bytes = base64.b64decode(value) | ||
if dtype == 'f8': | ||
return np.frombuffer(decoded_bytes, dtype=np.float64).tolist() | ||
elif dtype == 'i8': | ||
return np.frombuffer(decoded_bytes, dtype=np.int64).tolist() | ||
elif dtype == 'u8': | ||
return np.frombuffer(decoded_bytes, dtype=np.uint64).tolist() | ||
elif dtype == 'i4': | ||
return np.frombuffer(decoded_bytes, dtype=np.int32).tolist() | ||
elif dtype == 'i2': | ||
return np.frombuffer(decoded_bytes, dtype=np.int16).tolist() | ||
elif dtype == 'i1': | ||
return np.frombuffer(decoded_bytes, dtype=np.int8).tolist() | ||
else: | ||
try: | ||
return decoded_bytes.decode('utf-8') | ||
except UnicodeDecodeError: | ||
return decoded_bytes | ||
|
||
# Function to decode base64-encoded 'bdata' in a dictionary | ||
def decode_bdata_in_dict(d): | ||
for key, value in d.items(): | ||
if isinstance(value, dict): | ||
decode_bdata_in_dict(value) | ||
elif key == 'bdata' and is_base64(value): | ||
dtype = d.get('dtype', 'utf-8') | ||
d[key] = decode_base64(value, dtype) | ||
elif isinstance(value, list): | ||
for i in range(len(value)): | ||
if isinstance(value[i], dict): | ||
decode_bdata_in_dict(value[i]) | ||
|
||
# Iterate through all files in the data folder | ||
for filename in os.listdir(data_folder): | ||
file_path = os.path.join(data_folder, filename) | ||
|
||
# Check if it's a file | ||
if os.path.isfile(file_path): | ||
with open(file_path, 'r') as file: | ||
# Read the JSON data | ||
original_data = json.load(file) | ||
|
||
# Make a copy of the original data to decode | ||
data = json.loads(json.dumps(original_data)) | ||
|
||
# Decode base64-encoded 'bdata' in the JSON data | ||
decode_bdata_in_dict(data) | ||
|
||
# Check if the data has changed | ||
if data != original_data: | ||
isNan = False | ||
# Overwrite the 'y' value with the decoded 'bdata' | ||
for item in data.get('data', []): | ||
if 'y' in item and isinstance(item['y'], dict) and 'bdata' in item['y']: | ||
if any(np.isnan(x) for x in item['y']['bdata'] if isinstance(x, float)): | ||
isNan = True | ||
break | ||
else: | ||
item['y'] = item['y']['bdata'] | ||
if 'x' in item and isinstance(item['x'], dict) and 'bdata' in item['x']: | ||
if any(np.isnan(x) for x in item['x']['bdata'] if isinstance(x, float)): | ||
isNan = True | ||
break | ||
else: | ||
item['x'] = item['x']['bdata'] | ||
if 'z' in item and isinstance(item['z'], dict) and 'bdata' in item['z']: | ||
if any(np.isnan(x) for x in item['z']['bdata'] if isinstance(x, float)): | ||
isNan = True | ||
break | ||
else: | ||
item['z'] = item['z']['bdata'] | ||
if not isNan: | ||
print(f"Decoded values from {filename}:") | ||
# Write the decoded data back to the file | ||
with open(file_path, 'w') as outfile: | ||
json.dump(data, outfile, indent=4) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fix needs to go in product not in test app.