使用两个二进制文件构建加权直方图

问题描述

我需要同时迭代两个二进制文件,以便一个文件中产生的值正确(相同位置)对应于另一个文件中产生的值。我正在将值分类到直方图箱中,一个文件中的值对应于另一个文件中的值的权重。

我尝试了以下语法:

import numpy as np
import struct
import matplotlib.pyplot as plt

low = np.inf
high = -np.inf

struct_fmt = 'f'
struct_len = struct.calcsize(struct_fmt)
struct_unpack = struct.Struct(struct_fmt).unpack_from

file = "/projects/current/real-core-snaps/core4_256_velx_0009.bin"
file2 = "/projects/current/real-core-snaps/core4_256_dens_0009.bin"

def read_chunks(f,length):
    while True:
        data = f.read(length)
        if not data: break
        yield data

loop = 0

with open(file,"rb") as f:
    for chunk in read_chunks(f,struct_len):   
        x = struct_unpack(chunk)
        low = np.minimum(x,low)
        high = np.maximum(x,high)
        loop += 1

nbins = math.ceil(math.sqrt(loop)) 

bin_edges = np.linspace(low,high,nbins + 1)
total = np.zeros(nbins,np.int64)


f = open(file,"rb")
f2 = open(file2,"rb")

for chunk1,chunk2 in zip(read_chunks(f,struct_len),read_chunks(f2,struct_len)):
    subtotal,e = np.histogram(struct_unpack(chunk1),bins=bin_edges,weights=struct_unpack(chunk2))
    total = np.add(total,subtotal,out=total,casting="unsafe")

plt.hist(bin_edges[:-1],weights=total)
plt.savefig('hist-veldens.svg')

但是所产生的直方图是荒谬的(请参阅下文)。我在做什么错了?

enter image description here

数据文件位于https://drive.google.com/file/d/1fhia2CGzl_aRX9Q9Ng61W-4XJGQe1OCV/view?usp=sharinghttps://drive.google.com/file/d/1CrhQjyG2axSFgK9LGytELbxjy3Ndon1S/view?usp=sharing

解决方法

错误是 Future<void> _signInWithEmailAndPassword(BuildContext context,AsyncSnapshot<String> userSnapshot,AsyncSnapshot<String> passwordSnapshot) async { try { final auth = Provider.of<AuthService>(context,listen: false); await auth.signInWithEmailAndPassword( userSnapshot.data.trim(),passwordSnapshot.data.trim()); setState(() => errorString = 'Success'); } on FirebaseAuthException catch (e) { // I don't want to call setState() in each condition if (e.code == 'invalid-credential') { errorString = "Email address appears to be malformed/expired"; } else if (e.code == 'wrong-password') { errorString = "Password associated with this email is wrong"; } else if (e.code == 'user-not-found') { errorString = "Email has not been registered,please sign up :)"; } else if (e.code == 'user-disabled') { errorString = "User with this email has been disabled :("; } else if (e.code == 'too-many-requests') { errorString = "Too many requests,please try again later."; } else if (e.code == 'operation-not-allowed') { errorString = "Signing in with email and password is not enabled"; } else if (e.code == 'account-exists-with-different-credential') { errorString = "Email has already been registered. Reset your password."; } setState(() {}) } } 为数组total = np.zeros(nbins,np.int64)的每个元素分配了整数类型。假设total不在加权直方图中包含计数,而是浮点型,则总数也应为subtotal类型。