diff --git a/.gitignore b/.gitignore index 2026309..8e1b87c 100644 --- a/.gitignore +++ b/.gitignore @@ -63,4 +63,5 @@ test_regsurf *.gcov gmon.out npytest +*.npy # CVS default ignores end diff --git a/t.cc b/t.cc index 81212c0..322883d 100644 --- a/t.cc +++ b/t.cc @@ -159,7 +159,7 @@ cout < tt; +tt.numpy_read("test.npy"); +cout< +void Tensor::numpy_write(const char *name, const char *descr) const +{ +if(!is_flat()) laerror("numpy_write only for flat tensors"); +int fd=open(name,O_CREAT|O_LARGEFILE|O_RDWR,0777); +if(fd<0) laerror("cannot open in numpy_write"); +char magic[]="\x93NUMPY\x01\x00"; +if(8!=write(fd,magic,8)) laerror("cannot write 1 in numpy_write"); + +//construct header +char header[2048]; +int16_t header_len=0; +sprintf(header,"{'descr': '%s', 'fortran_order': False, 'shape': (",descr); +for(int i=myrank-1; i>=0; --i) + { + header_len=strlen(header); + sprintf(header+header_len,"%d",shape[i].range); + if(i>0) + { + header_len=strlen(header); + sprintf(header+header_len,", "); + } + } + +header_len=strlen(header); +sprintf(header+header_len,"), }"); + +//pad header by spaces to 8 byte boundary and terminate by \n +header_len=strlen(header); +int x= 8 - (header_len+2+8)%8; +for(int i=0; i +void Tensor::numpy_read(const char *name) +{ +int fd=open(name,O_LARGEFILE|O_RDONLY); +if(fd<0) laerror("cannot open in numpy_read"); +char magic[]="\x93NUMPY\x01\x00"; +char readmagic[8]; +if(8!=read(fd,readmagic,8)) laerror("cannot read 1 in numpy_read"); +if(memcmp(magic,readmagic,8)) laerror("bad magic or version != 1.0 in numpy_read"); + +int16_t header_len; +if(2!=read(fd,&header_len,2)) laerror("cannot read 2 in numpy_read"); + +char header[header_len]; +if(header_len!=read(fd,header,header_len)) laerror("cannot read 3 in numpy_read"); + +char *p=strstr(header,"fortran_order"); +if(!p) laerror("cannot find fortran_order in numpy_read"); +bool fortranorder=false; +p=strstr(p,": "); +if(strncmp(p+2,"False",5)) fortranorder=true; +//std::cout <<"Fortran order "< dimslist; +p=strstr(p,"'shape': ("); +if(!p) laerror("cannot find shape in numpy_read"); +p+=strlen("'shape': ("); +int d; +while(1==sscanf(p,"%d",&d)) + { + if(fortranorder) dimslist.push_back(d); else dimslist.push_front(d); + while(isdigit(*p)||isspace(*p)) p++; + if(*p==')') break; + ++p; //skip , + } + +NRVec dims(dimslist); +if(dims.size()==0) laerror("zero-dimensional array in numpy_read"); + +shape.resize(dims.size()); +for(int i=0; i; diff --git a/tensor.h b/tensor.h index 2a2fc9c..65823fe 100644 --- a/tensor.h +++ b/tensor.h @@ -279,6 +279,10 @@ public: //subtensor - todo: more general versions Tensor subtensor1(int i) const; //for one value of rightmost index + //numpy interface + void numpy_write(const char *name, const char *descr=" &x); explicit Tensor(const NRMat &x, bool flat=false);