def test_write_subregion_to_file(self, matrix, prepend_n_rows, prepend_n_cols, partition, sliced_dimension): # Create the matrix region mr = MatrixRegion(matrix, prepend_n_rows=prepend_n_rows, prepend_n_columns=prepend_n_cols, sliced_dimension=sliced_dimension) partitioned_matrix = matrix[mr.expanded_slice(partition)] # Get the temporary file fp = tempfile.TemporaryFile() # Write the subregion mr.write_subregion_to_file(fp, partition) # Read and check # Check the prepended data fp.seek(0) if prepend_n_rows: n_rows = fp.read(4) assert (partitioned_matrix.shape[0] == struct.unpack('I', n_rows)[0]) if prepend_n_cols: n_cols = fp.read(4) assert (partitioned_matrix.shape[1] == struct.unpack('I', n_cols)[0]) # Check the actual data data = np.frombuffer(fp.read(), dtype=matrix.dtype).reshape( partitioned_matrix.shape) assert np.all(data == partitioned_matrix)