def test_decode_known_and_compare(self): """ Create a decoder and read a numpy array from it """ hdfdecoder = HDFDecoder(self.known_hdf_as_string) nparray = hdfdecoder.read_hdf_dataset(self.path_to_dataset) # compare the read numpy array to a known value from the stringed input self.assertEqual(nparray.tostring(),self.known_array.tostring())
def add_two_datasets_read_compare(self, filename, dataset_name1, dataset_name2): array1 = numpy.ones((4,5)) array2 = numpy.ones((2,3)) # first create the file hdfencoder = HDFEncoder(filename) hdfencoder.add_hdf_dataset(dataset_name1, array1) hdfstring = hdfencoder.encoder_close() # now open the file and add another branch hdfencoder = HDFEncoder(filename) hdfencoder.add_hdf_dataset(dataset_name2, array2) hdfstring = hdfencoder.encoder_close() hdfdecoder = HDFDecoder(hdfstring) # Read the first dataset array_decoded_1 = hdfdecoder.read_hdf_dataset(dataset_name1) hdfdecoder = HDFDecoder(hdfstring) # Read the second dataset array_decoded_2 = hdfdecoder.read_hdf_dataset(dataset_name2) self.assertEqual(array1.tostring(), array_decoded_1.tostring()) self.assertEqual(array2.tostring(), array_decoded_2.tostring())
def test_encode_decode(self): """ Encode some arrays """ hdfencoder = HDFEncoder() # put array into the encoder hdfencoder.add_hdf_dataset(self.path_to_dataset, self.known_array) # get the string out from encoder hdf_string = hdfencoder.encoder_close() # Compare the arrays hdfdecoder = HDFDecoder(hdf_string) # put string in decoder... nparray = hdfdecoder.read_hdf_dataset(self.path_to_dataset) # get array out self.assertEqual(nparray.tostring(), self.known_array.tostring()) # works for arbitrarily shaped arrays
def test_decode_encode(self): """ Try a decode-encode sequence and compare if its the same string """ # decode an existing hdf file and read out an array hdfdecoder = HDFDecoder(self.known_hdf_as_string) # put known string in decoder... nparray = hdfdecoder.read_hdf_dataset(self.path_to_dataset) # get array out # encode the array and get the binary string containing the encoded hdf file hdfencoder = HDFEncoder() # put the array in the encoder... hdfencoder.add_hdf_dataset(self.path_to_dataset, self.known_array) hdf_string = hdfencoder.encoder_close() # get string out # compare the two strings self.assertEqual(hdf_string,self.known_hdf_as_string)
def test_encode_with_filename_and_compare(self): """ Create an encoder and add some (one) dataset/array """ testfilename = '/tmp/testFile.hdf5' hdfencoder = HDFEncoder(testfilename) hdfencoder.add_hdf_dataset(self.path_to_dataset, self.known_array) # get the string out from encoder hdf_string = hdfencoder.encoder_close() self.assertEqual(hdf_string,self.known_hdf_as_string) hdfdecoder = HDFDecoder(self.known_hdf_as_string) nparray = hdfdecoder.read_hdf_dataset(self.path_to_dataset) self.assertEqual(nparray.tostring(), self.known_array.tostring())
# Convert all the data to a binary string for easy transportation hdfstring2 = hdfencoder.encoder_close() # Create another encoder. This time pass on name of hdf5 file to write hdfencoder = HDFEncoder('/tmp/testHDFEncoder.hdf5') hdfencoder.add_hdf_dataset(dname, array3) # Convert all the data to a binary string for easy transportation hdfstring3 = hdfencoder.encoder_close() ########################################################## print('Dataset: %s ' % dataset_name2) # Create a decoder object hdfdecoder = HDFDecoder(hdfstring1) # Read the array out of the decoder print hdfdecoder.read_hdf_dataset(dataset_name2) print('Dataset: %s ' % dataset_name1) # Create a decoder object hdfdecoder = HDFDecoder(hdfstring2) # Read the array out of the decoder print hdfdecoder.read_hdf_dataset(dataset_name1) #print "Third decoded hdf_string: " ## Create a decoder object hdfdecoder = HDFDecoder(hdfstring3) # Read the array out of the decoder print('Dataset: %s ' % dataset_name1) print hdfdecoder.read_hdf_dataset(dataset_name1) hdfdecoder = HDFDecoder(hdfstring3)