Operating environment
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 14.04 LTS desktop amd64
TensorFlow v0.11
cuDNN v5.1 for Linux
CUDA v8.0
Python 2.7.6
IPython 5.1.0 -- An enhanced Interactive Python.
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
I want to display an image of the coefficient generated by C ++ code with Jupyter / matplotlib. To do that, I'm likely to read the C fwrite () result in Python.
I implemented it.
C (write)
writeTest.c
#include <stdio.h>
#include <stdlib.h>
int main(void){
FILE *wfp;
float pi = 3.141592;
wfp = fopen("sample.bin","wb");
fwrite(&pi, sizeof(float), 1, wfp);
fclose(wfp);
}
Python (read)
Reference http://stackoverflow.com/questions/2146031/what-is-the-equivalent-of-fread-from-matlab-in-python
readTest.py
import numpy as np
import array
with open("sample.bin") as rfp:
a = array.array("f") # f: typecode for float
a.fromfile(rfp, 1)
print (a)
The above type code is based on the following. https://docs.python.org/3.1/library/array.html
sample.bin generation
$ gcc writeTest.c && ./a.out
read sample.bin
$ python readTest.py
array('f', [3.141592025756836])
I read.
C (write)
writeTest.c
#include <stdio.h>
#include <stdlib.h>
int main(void){
FILE *wfp;
float pi = 3.141592;
float napier = 2.718;
float avogadro1 = 6.022;
wfp = fopen("sample.bin","wb");
fwrite(&pi, sizeof(float), 1, wfp);
fwrite(&napier, sizeof(float), 1, wfp);
fwrite(&avogadro1, sizeof(float), 1, wfp);
fclose(wfp);
}
Python (read)
readTest.py
import numpy as np
import array
with open("sample.bin","rb") as rfp:
a = array.array("f") # f: typecode for float
a.fromfile(rfp, 3)
print (a)
sample.bin generation
$ gcc writeTest.c && ./a.out
read sample.bin
$ python readTest.py
array('f', [3.141592025756836, 2.7179999351501465, 6.021999835968018])
(Addition 2016/12/19)
I was stumbling on reading the size_t type. v0.1 @ http://qiita.com/7of9/items/68673ac3239532064c28
sizeofPrint.c
#include <stdio.h>
#include <stdlib.h>
int main(void) {
printf("%ld\n", sizeof(size_t));
printf("%ld\n", sizeof(int));
}
Run
$ gcc sizeofPrint.c && ./a.out
8
4
In this environment, size_t
is 8.
C (write)
writeTest.c
#include <stdio.h>
#include <stdlib.h>
int main(void){
FILE *wfp;
size_t pi = 314;
float napier = 2.718;
float avogadro1 = 6.022;
wfp = fopen("sample.bin","wb");
fwrite(&pi, sizeof(size_t), 1, wfp);
fwrite(&napier, sizeof(float), 1, wfp);
fwrite(&avogadro1, sizeof(float), 1, wfp);
fclose(wfp);
}
Python (read)
To read the value of size_t (sizeof () = 8) on the Python side, it would be of type [unsigned long].
https://docs.python.org/3.1/library/array.html
According to L
readTest.py
import numpy as np
import array
with open("sample.bin","rb") as rfp:
szv = array.array("L") # L: typecodef for [unsigned long]
szv.fromfile(rfp, 1)
print (szv)
flv = array.array("f") # f: typecode for float
flv.fromfile(rfp, 2)
print (flv)
Run
$ python readTest.py
array('L', [314L])
array('f', [2.7179999351501465, 6.021999835968018])
(Addition 2016/12/20)
How to read a double complex type? I read it in double type for the time being.
C (write)
It is different from the actual value of Avogadro's number, but for the time being.
writeTest.c
#include <stdio.h>
#include <stdlib.h>
#include <complex.h>
int main(void){
FILE *wfp;
size_t pi = 314;
float napier = 2.718;
double complex avogadro = 6.022 + 10.23 * I;
wfp = fopen("sample.bin","wb");
fwrite(&pi, sizeof(size_t), 1, wfp);
fwrite(&napier, sizeof(float), 1, wfp);
fwrite(&avogadro, sizeof(double complex), 1, wfp);
fclose(wfp);
}
Python (read)
readTest.py
import numpy as np
import array
with open("sample.bin","rb") as rfp:
szv = array.array("L") # L: typecodef for [unsigned long]
szv.fromfile(rfp, 1)
print (szv)
napier = array.array("f") # f: typecode for float
napier.fromfile(rfp, 1)
print (napier)
avogadro = array.array("d") # d: typecode for double
avogadro.fromfile(rfp, 2) # real and imaginary part of the complex number
print (avogadro)
Run
$ gcc writeTest.c && ./a.out
$ python readTest.py
array('L', [314L])
array('f', [2.7179999351501465])
array('d', [6.022, 10.23])
For the time being, I was able to read the real and imaginary parts of the double complex. Is it a method like casting to a complex type based on the two values?
Recommended Posts