This repository has been archived by the owner on Aug 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwriters.py
831 lines (722 loc) · 27.5 KB
/
writers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
# TmLibrary - TissueMAPS library for distibuted image analysis routines.
# Copyright (C) 2016 Markus D. Herrmann, University of Zurich and Robin Hafen
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import os
import re
import h5py
import cv2
import numpy as np
import pandas as pd
import logging
import lxml.etree
import json
import ruamel.yaml
import traceback
from abc import ABCMeta
from abc import abstractmethod
from tmlib.utils import same_docstring_as
logger = logging.getLogger(__name__)
class Writer(object):
'''Abstract base class for writing data to files.
Writers make use of the
`with statement context manager <https://docs.python.org/2/reference/datamodel.html#context-managers>`_.
and follow a similar syntax::
with Writer('/path/to/file') as f:
f.write()
'''
__metaclass__ = ABCMeta
def __init__(self, filename):
'''
Parameters
----------
filename: str
absolute path to a file
'''
self.filename = filename
def __enter__(self):
self._stream = open(self.filename, 'w+')
return self
def __exit__(self, except_type, except_value, except_trace):
self._stream.close()
if except_value:
sys.stdout.write(
'The following error occurred while writing to file:\n%s'
% str(except_value)
)
for tb in traceback.format_tb(except_trace):
sys.stdout.write(tb)
sys.exit(1)
@abstractmethod
def write(self, data):
pass
class TextWriter(Writer):
'''Class for writing text data to a file.'''
@same_docstring_as(Writer.__init__)
def __init__(self, filename):
super(TextWriter, self).__init__(filename)
def write(self, data):
'''Writes data to file.
Parameters
----------
data: str
text that should be written to the file
'''
logger.debug('write data to file: %s' % self.filename)
self._stream.write(data)
class XmlWriter(Writer):
'''Class for writing data to a file in XML format.'''
@same_docstring_as(Writer.__init__)
def __init__(self, filename):
super(XmlWriter, self).__init__(filename)
def write(self, data):
'''Writes data to XML file.
Parameters
----------
data: lxml.etree._Element
xml element that should be written to the file
'''
logger.debug('write data to file: %s' % self.filename)
self._stream.write(lxml.etree.tostring(data))
class JsonWriter(Writer):
'''Class for writing data to file on disk in JSON format.'''
@same_docstring_as(Writer.__init__)
def __init__(self, filename):
super(JsonWriter, self).__init__(filename)
def write(self, data):
'''Writes data to JSON file.
Parameters
----------
data: list or dict
the JSON string that should be written to the file
Note
----
`filename` will be truncated in case it already exists.
'''
logger.debug('write data to file: %s' % self.filename)
json.dump(data, self._stream, sort_keys=True)
class YamlWriter(Writer):
'''Class for writing data to file on disk in YAML 1.2 format'''
@same_docstring_as(Writer.__init__)
def __init__(self, filename):
super(YamlWriter, self).__init__(filename)
def write(self, data):
'''Writes data to YAML file.
Parameters
----------
data: list or dict
the YAML string that should be written to the file
Note
----
`filename` will be truncated in case it already exists.
'''
logger.debug('write data to file: %s' % self.filename)
self._stream.write(
ruamel.yaml.dump(
data, Dumper=ruamel.yaml.RoundTripDumper, explicit_start=True
)
)
class ImageWriter(Writer):
'''Class for writing :class:`numpy.ndarray` objects to image files
using the `OpenCV <http://docs.opencv.org>`_ library.
'''
@same_docstring_as(Writer.__init__)
def __init__(self, filename):
super(ImageWriter, self).__init__(filename)
def write(self, data):
'''Writes pixels array data to image file.
The format depends on the file extension:
- \*.png for PNG (8-bit and 16-bit)
- \*.tiff or \*.tif for TIFF (8-bit and 16-bit)
- \*.jpeg or \*.jpg for JPEG (only supports 8-bit)
Parameters
----------
data: numpy.ndarray
2D pixels plane that should be saved
Raises
------
TypeError
when `data` is not of type numpy.ndarray
ValueError
when `data` has more than 2 dimensions
'''
logger.debug('write data to file: %s' % self.filename)
if not isinstance(data, np.ndarray):
raise TypeError('Data must have type numpy.ndarray.')
if data.ndim > 2:
raise ValueError('Only 2D arrays are supported.')
binary = cv2.imencode(os.path.splitext(self.filename)[1], data)[1]
self._stream.write(binary)
class DataTableWriter(Writer):
'''Class for writing data to a HDF5 file using the
`pytables <http://www.pytables.org/>`_ library.'''
def __init__(self, filename, truncate=False):
'''
Parameters
----------
filename: str
absolute path to a file
truncate: bool, optional
truncate the file if it already exists (default: ``False``)
'''
super(DataTableWriter, self).__init__(filename)
self.truncate = truncate
def __enter__(self):
logger.debug('open file: %s', self.filename)
if self.truncate:
self._stream = pd.HDFStore(self.filename, 'w')
else:
self._stream = pd.HDFStore(self.filename, 'a')
return self
def exists(self, path):
'''Check whether a `path` exists within the file.
Parameters
----------
path: str
absolute path to a group or dataset in the file
Returns
-------
bool
``True`` if `path` exists and ``False`` otherwise
'''
if path in self._stream:
return True
else:
return False
def write(self, path, data):
'''Write a data table.
Parameters
----------
path: str
absolute path to the dataset within the file
data: pandas.DataFrame
data table
'''
self._stream.put(path, data, format='table', data_columns=True)
def append(self, path, data):
'''Append an existing data table.
Parameters
----------
path: str
absolute path to the dataset within the file
data: pandas.DataFrame
data table
'''
self._stream.append(path, data, format='table', data_columns=True)
class DatasetWriter(Writer):
'''Class for writing data to a HDF5 file using the
`h5py <http://docs.h5py.org/en/latest/index.html>`_ library.
'''
def __init__(self, filename, truncate=False):
'''
Parameters
----------
filename: str
absolute path to the HDF5 file
truncate: bool, optional
truncate the file if it already exists (default: ``False``)
'''
super(DatasetWriter, self).__init__(filename)
self.truncate = truncate
def __enter__(self):
logger.debug('open file: %s', self.filename)
if self.truncate:
self._stream = h5py.File(self.filename, 'w')
else:
self._stream = h5py.File(self.filename, 'a')
return self
def exists(self, path):
'''Checks whether `path` exists within the file.
Parameters
----------
path: str
absolute path to a group or dataset in the file
Returns
-------
bool
``True`` if `path` exists and ``False`` otherwise
'''
if path in self._stream:
return True
else:
return False
def write(self, path, data, compression=False):
'''Creates a dataset and writes data to it.
Parameters
----------
path: str
absolute path to the dataset within the file
data:
dataset; will be put through ``numpy.array(data)``
compression: bool, optional
whether zip compression filter should be applied
(default: ``False``)
Raises
------
IOError
when `path` already exists
Note
----
If `data` is a nested list or array of arrays,
a *variable_length* dataset with dimensions ``(len(data),)`` is
created. For more information on *variable-length* types, see
`h5py docs <http://docs.h5py.org/en/latest/special.html>`_.
'''
if isinstance(data, basestring):
data = np.string_(data)
if ((isinstance(data, np.ndarray) or isinstance(data, list)) and
all([isinstance(d, basestring) for d in data])):
data = [np.string_(d) for d in data]
if isinstance(data, list):
if len(data) == 1 and isinstance(data[0], np.ndarray):
# Work around inconsistent numpy behavior for vlen datasets:
# A list containing multiple numpy arrays of different shapes
# are converted to a one-dimensional nested array of arrays
# with object type, but a list containing a single numpy array
# or multiple numpy arrays with the same shape to a
# multi-dimensional array.
empty = np.empty((1,), dtype='O')
empty[0] = data[0]
data = empty
else:
data = np.array(data)
if isinstance(data, np.ndarray) and data.dtype == 'O':
logger.debug('write dataset "%s" as variable length', path)
self._write_vlen(path, data)
else:
logger.debug('write dataset "%s"', path)
if self.exists(path):
logger.warning(
'dataset "%s" in file "%s" will be overwritten',
path, self.filename
)
try:
self._stream[path][...] = data
except:
raise IOError(
'Dataset "%s" in file "%s" could not be overwritten.'
% (path, self.filename)
)
else:
if compression:
self._stream.create_dataset(
path, data=data, compression='gzip'
)
else:
self._stream.create_dataset(path, data=data)
def write_subset(self, path, data,
index=None, row_index=None, column_index=None):
'''Writes data to a subset of an existing dataset.
Parameters
----------
path: str
absolute path to the dataset within the file
data:
dataset; will be put through ``numpy.array(data)``
index: int or List[int], optional
zero-based index
row_index: int or List[int], optional
zero-based row index
column_index: int or List[int], optional
zero-based column index
Raises
------
TypeError
when `data` has a different data type than an existing dataset
IndexError
when a provided index exceeds dimensions of an existing dataset
KeyError
when a subset of the dataset should be written, i.e. an index is
provided, but the dataset does not yet exist
Note
----
If `data` is a nested list or array of arrays,
a *variable_length* dataset with dimensions ``(len(data),)`` is
created. For more information on *variable-length* types, see
`h5py docs <http://docs.h5py.org/en/latest/special.html>`_.
'''
if isinstance(data, basestring):
data = np.string_(data)
if ((isinstance(data, np.ndarray) or isinstance(data, list)) and
all([isinstance(d, basestring) for d in data])):
data = [np.string_(d) for d in data]
if isinstance(data, list):
if len(data) == 1 and isinstance(data[0], np.ndarray):
# Work around inconsistent numpy behavior for vlen datasets:
# A list containing multiple numpy arrays of different shapes
# are converted to a one-dimensional nested array of arrays
# with object type, but a list containing a single numpy array
# or multiple numpy arrays with the same shape to a
# multi-dimensional array.
empty = np.empty((1,), dtype='O')
empty[0] = data[0]
data = empty
else:
data = np.array(data)
if not self.exists(path):
raise KeyError(
'In order to be able to write a subset of data, '
'the dataset has to exist: %s', path)
dset = self._stream[path]
if dset.dtype != data.dtype:
raise TypeError(
'Data must have data type as dataset: '
'Dataset dtype: {0} - Data dtype: {1}'.format(
dset.dtype, data.dtype
))
if any(np.array(data.shape) > np.array(dset.shape)):
raise IndexError(
'Data dimensions exceed dataset dimensions: '
'Dataset dims: {0} - Data dims: {1}'.format(
dset.shape, data.shape
))
if row_index is not None:
if len(dset.shape) == 1:
raise IndexError(
'One-dimensional dataset does not allow '
'row-wise indexing: Dataset dims: {0}'.format(
dset.shape))
if (len(list(row_index)) > data.shape[0] or
any(np.array(row_index) > dset.shape[0])):
raise IndexError(
'Row index exceeds dataset dimensions: '
'Dataset dims: {0}'.format(dset.shape))
if column_index is not None:
if len(dset.shape) == 1:
raise IndexError(
'One-dimensional dataset does not allow '
'column-wise indexing: Dataset dims: {0}'.format(
dset.shape))
if (len(list(column_index)) > data.shape[1] or
any(np.array(column_index) > dset.shape[1])):
raise IndexError(
'Column index exceeds dataset dimension: '
'Dataset dims: {0}'.format(dset.shape))
if index is not None:
if len(dset.shape) > 1:
raise IndexError(
'Multi-dimensional dataset does not allow '
'element-wise indexing: Dataset dims: {0}'.format(
dset.shape))
if (isinstance(index, list) and
isinstance(data, np.ndarray)):
if (len(index) > len(data) or
any(np.array(index) > len(dset))):
raise IndexError(
'Index exceeds dataset dimensions: '
'Dataset dims: {0}'.format(dset.shape))
elif (isinstance(index, int) and
not isinstance(data, np.ndarray)):
if index > data:
raise IndexError(
'Index exceeds dataset dimensions: '
'Dataset dims: {0}'.format(dset.shape))
else:
TypeError(
'Index must have have type int or list of int.')
logger.debug('write data to a subset of dataset "%s"', path)
if row_index and not column_index:
dset[row_index, :] = data
elif not row_index and column_index:
dset[:, column_index] = data
elif row_index and column_index:
dset[row_index, column_index] = data
elif index is not None:
if (isinstance(index, list) and
isinstance(data, np.ndarray)):
for i, d in zip(index, data):
dset[i] = d.tolist()
else:
dset[index] = data
@staticmethod
def _is_dataset(element):
if isinstance(element.id, h5py.h5d.DatasetID):
return True
else:
return False
def list_datasets(self, path='/', pattern='.*'):
'''Lists datasets within a given group.
Parameters
----------
path: str, optional
absolute path to a group in the file (default: ``"/"``)
pattern: str, optional
regular expression pattern to filter datasets (default: ``".*"``)
Returns
-------
List[str]
names of the datasets in `path`
Raises
------
KeyError
when `path` does not exist
'''
try:
group = self._stream[path]
except KeyError:
raise KeyError('Group does not exist: %s' % path)
names = list()
r = re.compile(pattern)
for name, value in group.iteritems():
if self._is_dataset(value) and r.search(name):
names.append(name)
return names
def list_groups(self, path, pattern='.*'):
'''Lists groups within a given group.
Parameters
----------
path: str
absolute path to a group in the file
pattern: str, optional
regular expression pattern to filter groups (default: ``".*"``)
Returns
-------
List[str]
names of the groups in `path`
Raises
------
KeyError
when `path` does not exist
'''
try:
group = self._stream[path]
except KeyError:
raise KeyError('Group does not exist: %s' % path)
names = list()
r = re.compile(pattern)
for name, value in group.iteritems():
if not self._is_dataset(value) and r.search(name):
names.append(name)
return names
def _write_vlen(self, path, data):
data_type = np.unique([d.dtype for d in data])
if len(data_type) == 0:
dt = h5py.special_dtype(vlen=np.int64)
else:
dt = h5py.special_dtype(vlen=data_type[0])
dset = self._stream.create_dataset(path, data.shape, dtype=dt)
for i, d in enumerate(data):
dset[i] = d.tolist() # doesn't work with numpy.ndarray!!!
return dset
def create(self, path, dims, dtype, max_dims=None):
'''Creates a dataset with a given size and data type without actually
writing data to it.
Parameters
----------
path: str
absolute path to the dataset within the file
dims: Tuple[int]
dimensions of the dataset (number of rows and columns)
dtype: type
datatype the dataset
max_dims: Tuple[int]
maximal dimensions of the dataset, useful if the dataset should
be extendable along one or more dimensions (defaults to `dims`);
``(None, None)`` would mean extendable infinitely along both
dimensions
Returns
-------
h5py._hl.dataset.Dataset
Raises
------
IOError
when `path` already exists
'''
if max_dims is None:
max_dims = dims
if self.exists(path):
raise IOError('Dataset already exists: %s' % path)
return self._stream.create_dataset(
path, shape=dims, dtype=dtype, maxshape=max_dims)
def append(self, path, data):
'''Appends data to an existing one-dimensional dataset.
The dataset needs to be created first using the
:func:`tmlib.writers.DatasetWriter.create` method and the
`max_dims` entry for the vertical dimension needs to be
set to ``None``.
Parameters
----------
path: str
absolute path to the dataset within the file
data:
dataset; will be put through ``numpy.array(data)``
Raises
------
ValueError
when the dataset is one-dimensional or when vertical dimensions of
`data` and the dataset don't match
TypeError
when data types of `data` and the dataset don't match
Note
----
Creates the dataset in case it doesn't yet exist.
'''
data = np.array(data)
if not self.exists(path):
logger.debug('create dataset "%s"', path)
# preallocate an empty dataset that can be extended
self.create(
path, dims=(0, ), dtype=data.dtype,
max_dims=(None, ))
dset = self._stream[path]
if len(dset.shape) > 1:
raise ValueError('Data must be one-dimensional: %s', path)
if len(data.shape) > 1:
raise ValueError('Data dimensions do not match.')
if dset.dtype != data.dtype:
raise TypeError('Data types don\'t match.')
start_index = len(dset)
end_index = start_index + len(data)
dset.resize((len(dset) + len(data), ))
self.write(path, data, index=range(start_index, end_index))
# dset[start_index:] = data
def vstack(self, path, data):
'''Vertically appends data to an existing multi-dimensional dataset.
The dataset needs to be created first using the
:func:`tmlib.writers.DatasetWriter.create` method and the
`max_dims` entry for the vertical dimension needs to be
set to ``None``.
Parameters
----------
path: str
absolute path to the dataset within the file
data:
dataset; will be put through ``numpy.array(data)``
Raises
------
ValueError
when the dataset is one-dimensional or when vertical dimensions of
`data` and the dataset don't match
TypeError
when data types of `data` and the dataset don't match
Note
----
Creates the dataset in case it doesn't yet exist.
If `data` is one-dimensional a dataset with dimensions
``(0, len(data))`` will be created.
'''
data = np.array(data)
if not self.exists(path):
logger.debug('create dataset "%s"', path)
# preallocate an empty dataset that can be extended along the
# vertical axis
if len(data.shape) > 1:
self.create(
path, dims=(0, data.shape[1]), dtype=data.dtype,
max_dims=(None, data.shape[1]))
else:
self.create(
path, dims=(0, len(data)), dtype=data.dtype,
max_dims=(None, len(data)))
dset = self._stream[path]
if not len(dset.shape) > 1:
raise ValueError('Data must be multi-dimensional: %s', path)
if len(data.shape) > 1:
if data.shape[1] != dset.shape[1]:
raise ValueError('Dataset dimensions do not match.')
add = data.shape[0]
else:
if len(data) != dset.shape[1]:
raise ValueError('Dataset dimensions do not match.')
add = 1
if dset.dtype != data.dtype:
raise TypeError('Data types don\'t match.')
start_index = dset.shape[0]
dset.resize((dset.shape[0] + add, dset.shape[1]))
dset[start_index:, :] = data
def hstack(self, path, data):
'''Horizontally appends data to an existing multi-dimensional dataset.
The dataset needs to be created first using the
:func:`tmlib.writers.DatasetWriter.create` method and the
`max_dims` entry for the horizontal dimension needs to be
set to ``None``.
Parameters
----------
path: str
absolute path to the dataset within the file
data:
dataset; will be put through ``numpy.array(data)``
Raises
------
IOError
when `path` doesn't exist
ValueError
when the dataset is one-dimensional or when horizontal dimensions
of `data` and the dataset don't match
TypeError
when data types of `data` and the dataset don't match
Note
----
Creates the dataset in case it doesn't yet exist.
If `data` is one-dimensional a dataset with dimensions
``(len(data), 0)`` will be created.
'''
data = np.array(data)
if not self.exists(path):
logger.debug('create dataset "%s"', path)
# preallocate an empty dataset that can be extended along the
# horizontal axis
if len(data.shape) > 1:
self.create(
path, dims=(data.shape[0], 0), dtype=data.dtype,
max_dims=(data.shape[0], None))
else:
self.create(
path, dims=(len(data), 0), dtype=data.dtype,
max_dims=(len(data), None))
dset = self._stream[path]
if not len(dset.shape) > 1:
raise ValueError('Data must be multi-dimensional: %s', path)
if len(data.shape) > 1:
if data.shape[0] != dset.shape[0]:
raise ValueError('Dataset dimensions don\'t match.')
add = data.shape[1]
else:
if len(data) != dset.shape[0]:
raise ValueError('Dataset dimensions don\'t match.')
add = 1
if dset.dtype != data.dtype:
raise TypeError('Data types don\'t match.')
start_index = dset.shape[1]
dset.resize((dset.shape[0], dset.shape[1] + add))
dset[:, start_index:] = data
def set_attribute(self, path, name, data):
'''Attachs an attribute to a dataset.
Parameters
----------
path: str
absolute path to the dataset within the file
name: str
name of the attribute
data:
value of the attribute; will be put through ``numpy.array(data)``
'''
if isinstance(data, basestring):
data = np.string_(data)
elif isinstance(data, list):
data = [
np.string_(d) if isinstance(d, basestring) else d
for d in data
]
self._stream[path].attrs.create(name, data)
def create_group(self, path):
'''Creates a group.
Parameters
----------
path: str
absolute path to the group within the file
'''
if not self.exists(path):
self._stream.create_group(path)