Last active
August 3, 2023 12:35
-
-
Save unfo/c16c9c32abc49ca99e85c05424c6a3d1 to your computer and use it in GitHub Desktop.
Analyzing entropy visually in jupyter notebook
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
# ChatGPT: i have a binary file called "output.bin". | |
# i want to read it with python as a stream of integers triplets | |
# that map as coordinates in a 3d space and then plot those data points with mathplotlib. | |
# all of this is happening in jupyter notebook. | |
# + some additional small tweaking | |
# Output: | |
import matplotlib.pyplot as plt | |
from mpl_toolkits.mplot3d import Axes3D | |
def plot_file_3d(filename): | |
# Step 1: Read the binary file in chunks and convert to a list of integers | |
chunk_size = 4 * 3 # Each triplet has 3 integers, each integer is 4 bytes | |
x_coords = [] | |
y_coords = [] | |
z_coords = [] | |
with open(filename, 'rb') as file: | |
while True: | |
chunk = file.read(chunk_size) | |
if not chunk: | |
break | |
if len(chunk) == chunk_size: # Discard the incomplete chunk at the end | |
# Convert the chunk to a list of integers | |
data = [int.from_bytes(chunk[i:i+4], byteorder='little') for i in range(0, len(chunk), 4)] | |
# Append the coordinates to their respective lists | |
x_coords.extend(data[::3]) | |
y_coords.extend(data[1::3]) | |
z_coords.extend(data[2::3]) | |
# Step 2: Plot the data points in 3D space | |
fig = plt.figure() | |
ax = fig.add_subplot(111, projection='3d') | |
ax.scatter(x_coords, y_coords, z_coords, s=1, c='black') | |
# Add labels and title | |
ax.set_xlabel('X') | |
ax.set_ylabel('Y') | |
ax.set_zlabel('Z') | |
plt.title('Data Points in 3D Space') | |
# Show the plot | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment