Reading binary files in Cython
up vote
0
down vote
favorite
I am attempting to read a binary file in Cython. Previously this was working in Python, but I am looking to speed up the process. This code below was written as a familiarisation and logic check before writing the complete module. Once this section is complete the code will be expanded to read in multiple 400 Mb files and process.
A function was created that opens the file, reads in a number of data point and returns them to an array.
from libc.stdlib cimport malloc, free
from libc.stdio cimport fopen, fclose, FILE, fscanf, fread
def readin_binary(filename, int number_of_points):
"""
Test reading in a file and returning data
"""
header_bytes = <unsigned char*>malloc(number_of_points)
filename_byte_string = filename.encode("UTF-8")
cdef FILE *in_binary_file
in_binary_file = fopen(filename_byte_string, 'rb')
if in_binary_file is NULL:
print("file not found")
else:
print("Read file {}".format(filename))
fread(&header_bytes, 1, number_of_points, in_binary_file)
fclose(in_binary_file)
return header_bytes
print(hDVS.readin_binary(filename, 10))
The code compiles.
When the code is run the following error occurs:
Python has stopped working error
I've been playing with this for a few days now. I think there is a simple error but I can not see it. Any ideas?
cython binaryfiles
|
show 4 more comments
up vote
0
down vote
favorite
I am attempting to read a binary file in Cython. Previously this was working in Python, but I am looking to speed up the process. This code below was written as a familiarisation and logic check before writing the complete module. Once this section is complete the code will be expanded to read in multiple 400 Mb files and process.
A function was created that opens the file, reads in a number of data point and returns them to an array.
from libc.stdlib cimport malloc, free
from libc.stdio cimport fopen, fclose, FILE, fscanf, fread
def readin_binary(filename, int number_of_points):
"""
Test reading in a file and returning data
"""
header_bytes = <unsigned char*>malloc(number_of_points)
filename_byte_string = filename.encode("UTF-8")
cdef FILE *in_binary_file
in_binary_file = fopen(filename_byte_string, 'rb')
if in_binary_file is NULL:
print("file not found")
else:
print("Read file {}".format(filename))
fread(&header_bytes, 1, number_of_points, in_binary_file)
fclose(in_binary_file)
return header_bytes
print(hDVS.readin_binary(filename, 10))
The code compiles.
When the code is run the following error occurs:
Python has stopped working error
I've been playing with this for a few days now. I think there is a simple error but I can not see it. Any ideas?
cython binaryfiles
1
That is a crash.
– Robert Harvey♦
Nov 22 at 16:19
1
Have you determined yet if you're not already reading data as fast as the hard drive can provide it?
– Robert Harvey♦
Nov 22 at 16:20
Did you inspect the generated C Code? Does it happen for any file? If yes could you provide a minimal file and Python code needed to reproduce the shown error?
– MSeifert
Nov 22 at 16:22
Hi @MSeifert I have checked a few files and the result is the same. I had a look at the *.c file. I do not know where to start with this file - very complex. The attached zip file contains the test python module. In the test directory is test.py which calls the Cython function readin_binary().
– Christopher Bridge
Nov 22 at 16:46
2
Assuming it is fread from c ( how to know without cimports?), it should be fread(header_bytes,....), without &.
– ead
Nov 22 at 16:59
|
show 4 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am attempting to read a binary file in Cython. Previously this was working in Python, but I am looking to speed up the process. This code below was written as a familiarisation and logic check before writing the complete module. Once this section is complete the code will be expanded to read in multiple 400 Mb files and process.
A function was created that opens the file, reads in a number of data point and returns them to an array.
from libc.stdlib cimport malloc, free
from libc.stdio cimport fopen, fclose, FILE, fscanf, fread
def readin_binary(filename, int number_of_points):
"""
Test reading in a file and returning data
"""
header_bytes = <unsigned char*>malloc(number_of_points)
filename_byte_string = filename.encode("UTF-8")
cdef FILE *in_binary_file
in_binary_file = fopen(filename_byte_string, 'rb')
if in_binary_file is NULL:
print("file not found")
else:
print("Read file {}".format(filename))
fread(&header_bytes, 1, number_of_points, in_binary_file)
fclose(in_binary_file)
return header_bytes
print(hDVS.readin_binary(filename, 10))
The code compiles.
When the code is run the following error occurs:
Python has stopped working error
I've been playing with this for a few days now. I think there is a simple error but I can not see it. Any ideas?
cython binaryfiles
I am attempting to read a binary file in Cython. Previously this was working in Python, but I am looking to speed up the process. This code below was written as a familiarisation and logic check before writing the complete module. Once this section is complete the code will be expanded to read in multiple 400 Mb files and process.
A function was created that opens the file, reads in a number of data point and returns them to an array.
from libc.stdlib cimport malloc, free
from libc.stdio cimport fopen, fclose, FILE, fscanf, fread
def readin_binary(filename, int number_of_points):
"""
Test reading in a file and returning data
"""
header_bytes = <unsigned char*>malloc(number_of_points)
filename_byte_string = filename.encode("UTF-8")
cdef FILE *in_binary_file
in_binary_file = fopen(filename_byte_string, 'rb')
if in_binary_file is NULL:
print("file not found")
else:
print("Read file {}".format(filename))
fread(&header_bytes, 1, number_of_points, in_binary_file)
fclose(in_binary_file)
return header_bytes
print(hDVS.readin_binary(filename, 10))
The code compiles.
When the code is run the following error occurs:
Python has stopped working error
I've been playing with this for a few days now. I think there is a simple error but I can not see it. Any ideas?
cython binaryfiles
cython binaryfiles
edited Nov 22 at 21:08
asked Nov 22 at 16:18
Christopher Bridge
42
42
1
That is a crash.
– Robert Harvey♦
Nov 22 at 16:19
1
Have you determined yet if you're not already reading data as fast as the hard drive can provide it?
– Robert Harvey♦
Nov 22 at 16:20
Did you inspect the generated C Code? Does it happen for any file? If yes could you provide a minimal file and Python code needed to reproduce the shown error?
– MSeifert
Nov 22 at 16:22
Hi @MSeifert I have checked a few files and the result is the same. I had a look at the *.c file. I do not know where to start with this file - very complex. The attached zip file contains the test python module. In the test directory is test.py which calls the Cython function readin_binary().
– Christopher Bridge
Nov 22 at 16:46
2
Assuming it is fread from c ( how to know without cimports?), it should be fread(header_bytes,....), without &.
– ead
Nov 22 at 16:59
|
show 4 more comments
1
That is a crash.
– Robert Harvey♦
Nov 22 at 16:19
1
Have you determined yet if you're not already reading data as fast as the hard drive can provide it?
– Robert Harvey♦
Nov 22 at 16:20
Did you inspect the generated C Code? Does it happen for any file? If yes could you provide a minimal file and Python code needed to reproduce the shown error?
– MSeifert
Nov 22 at 16:22
Hi @MSeifert I have checked a few files and the result is the same. I had a look at the *.c file. I do not know where to start with this file - very complex. The attached zip file contains the test python module. In the test directory is test.py which calls the Cython function readin_binary().
– Christopher Bridge
Nov 22 at 16:46
2
Assuming it is fread from c ( how to know without cimports?), it should be fread(header_bytes,....), without &.
– ead
Nov 22 at 16:59
1
1
That is a crash.
– Robert Harvey♦
Nov 22 at 16:19
That is a crash.
– Robert Harvey♦
Nov 22 at 16:19
1
1
Have you determined yet if you're not already reading data as fast as the hard drive can provide it?
– Robert Harvey♦
Nov 22 at 16:20
Have you determined yet if you're not already reading data as fast as the hard drive can provide it?
– Robert Harvey♦
Nov 22 at 16:20
Did you inspect the generated C Code? Does it happen for any file? If yes could you provide a minimal file and Python code needed to reproduce the shown error?
– MSeifert
Nov 22 at 16:22
Did you inspect the generated C Code? Does it happen for any file? If yes could you provide a minimal file and Python code needed to reproduce the shown error?
– MSeifert
Nov 22 at 16:22
Hi @MSeifert I have checked a few files and the result is the same. I had a look at the *.c file. I do not know where to start with this file - very complex. The attached zip file contains the test python module. In the test directory is test.py which calls the Cython function readin_binary().
– Christopher Bridge
Nov 22 at 16:46
Hi @MSeifert I have checked a few files and the result is the same. I had a look at the *.c file. I do not know where to start with this file - very complex. The attached zip file contains the test python module. In the test directory is test.py which calls the Cython function readin_binary().
– Christopher Bridge
Nov 22 at 16:46
2
2
Assuming it is fread from c ( how to know without cimports?), it should be fread(header_bytes,....), without &.
– ead
Nov 22 at 16:59
Assuming it is fread from c ( how to know without cimports?), it should be fread(header_bytes,....), without &.
– ead
Nov 22 at 16:59
|
show 4 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434892%2freading-binary-files-in-cython%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
That is a crash.
– Robert Harvey♦
Nov 22 at 16:19
1
Have you determined yet if you're not already reading data as fast as the hard drive can provide it?
– Robert Harvey♦
Nov 22 at 16:20
Did you inspect the generated C Code? Does it happen for any file? If yes could you provide a minimal file and Python code needed to reproduce the shown error?
– MSeifert
Nov 22 at 16:22
Hi @MSeifert I have checked a few files and the result is the same. I had a look at the *.c file. I do not know where to start with this file - very complex. The attached zip file contains the test python module. In the test directory is test.py which calls the Cython function readin_binary().
– Christopher Bridge
Nov 22 at 16:46
2
Assuming it is fread from c ( how to know without cimports?), it should be fread(header_bytes,....), without &.
– ead
Nov 22 at 16:59