Skip to content

Commit a321af7

Browse files
committed
fix #815 : Dropped possibility to read and export Axis and Group objects from/to CSV and Excel files
1 parent a51ce85 commit a321af7

File tree

11 files changed

+23
-232
lines changed

11 files changed

+23
-232
lines changed

doc/source/changes/version_0_32.rst.inc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ Syntax changes
1010
Backward incompatible changes
1111
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212

13-
* other backward incompatible changes
13+
* Because it was broken, the possibility to dump and load Axis and Group objects
14+
contained in a session has been removed for the CSV and Excel formats.
15+
Fixing it would have taken too much time considering it is very rarely used
16+
(no one complains it was broken) so the decision to remove it was taken.
17+
However, this is still possible using the HDF format.
18+
Closes :issue:`815`.
1419

1520

1621
New features

doc/source/tutorial/tutorial_IO.ipyml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -660,13 +660,7 @@ cells:
660660

661661
- markdown: |
662662
<div class="alert alert-info">
663-
Note: Concerning the CSV and Excel formats:
664-
665-
- all Axis objects are saved together in the same Excel sheet (CSV file) named `__axes__(.csv)`
666-
- all Group objects are saved together in the same Excel sheet (CSV file) named `__groups__(.csv)`
667-
- metadata is saved in one Excel sheet (CSV file) named `__metadata__(.csv)`
668-
669-
These sheet (CSV file) names cannot be changed.
663+
Note: Concerning the CSV and Excel formats, the metadata is saved in one Excel sheet (CSV file) named `__metadata__(.csv)`. This sheet (CSV file) name cannot be changed.
670664
</div>
671665

672666

doc/source/tutorial/tutorial_IO.ipynb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -961,13 +961,7 @@
961961
"metadata": {},
962962
"source": [
963963
"<div class=\"alert alert-info\">\n",
964-
" Note: Concerning the CSV and Excel formats: \n",
965-
" \n",
966-
" - all Axis objects are saved together in the same Excel sheet (CSV file) named `__axes__(.csv)` \n",
967-
" - all Group objects are saved together in the same Excel sheet (CSV file) named `__groups__(.csv)` \n",
968-
" - metadata is saved in one Excel sheet (CSV file) named `__metadata__(.csv)` \n",
969-
" \n",
970-
" These sheet (CSV file) names cannot be changed. \n",
964+
" Note: Concerning the CSV and Excel formats, the metadata is saved in one Excel sheet (CSV file) named `__metadata__(.csv)`. This sheet (CSV file) name cannot be changed. \n",
971965
"</div>"
972966
]
973967
},

larray/core/session.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,8 @@ def __setstate__(self, d):
344344

345345
def load(self, fname, names=None, engine='auto', display=False, **kwargs):
346346
r"""
347-
Load Array, Axis and Group objects from a file, or several .csv files.
347+
Load Array objects from a file, or several .csv files (all formats).
348+
Load also Axis and Group objects from a file (HDF and pickle formats).
348349
349350
WARNING: never load a file using the pickle engine (.pkl or .pickle) from an untrusted source, as it can lead
350351
to arbitrary code execution.
@@ -431,7 +432,8 @@ def load(self, fname, names=None, engine='auto', display=False, **kwargs):
431432

432433
def save(self, fname, names=None, engine='auto', overwrite=True, display=False, **kwargs):
433434
r"""
434-
Dumps Array, Axis and Group objects from the current session to a file.
435+
Dumps Array objects from the current session to a file (all formats).
436+
Dumps also Axis and Group objects from the current session to a file (HDF and pickle format).
435437
436438
Parameters
437439
----------
@@ -450,10 +452,6 @@ def save(self, fname, names=None, engine='auto', overwrite=True, display=False,
450452
display : bool, optional
451453
Whether or not to display which file is being worked on. Defaults to False.
452454
453-
Notes
454-
-----
455-
See Notes section from :py:meth:`~Session.to_csv` and :py:meth:`~Session.to_excel`.
456-
457455
Examples
458456
--------
459457
>>> # axes
@@ -652,15 +650,15 @@ def to_hdf(self, fname, names=None, overwrite=True, display=False, **kwargs):
652650

653651
def to_excel(self, fname, names=None, overwrite=True, display=False, **kwargs):
654652
r"""
655-
Dumps Array, Axis and Group objects from the current session to an Excel file.
653+
Dumps Array objects from the current session to an Excel file.
656654
657655
Parameters
658656
----------
659657
fname : str
660658
Path of the file for the dump.
661659
names : list of str or None, optional
662-
Names of Array/Axis/Group objects to dump.
663-
Defaults to all objects present in the Session.
660+
Names of Array objects to dump.
661+
Defaults to all Array objects present in the Session.
664662
overwrite: bool, optional
665663
Whether or not to overwrite an existing file, if any. If False, file is updated. Defaults to True.
666664
display : bool, optional
@@ -669,8 +667,6 @@ def to_excel(self, fname, names=None, overwrite=True, display=False, **kwargs):
669667
Notes
670668
-----
671669
- each array is saved in a separate sheet
672-
- all Axis objects are saved together in the same sheet named __axes__
673-
- all Group objects are saved together in the same sheet named __groups__
674670
- all session metadata is saved in the same sheet named __metadata__
675671
676672
Examples
@@ -700,23 +696,21 @@ def to_excel(self, fname, names=None, overwrite=True, display=False, **kwargs):
700696

701697
def to_csv(self, fname, names=None, display=False, **kwargs):
702698
r"""
703-
Dumps Array, Axis and Group objects from the current session to CSV files.
699+
Dumps Array objects from the current session to CSV files.
704700
705701
Parameters
706702
----------
707703
fname : str
708704
Path for the directory that will contain CSV files.
709705
names : list of str or None, optional
710-
Names of Array/Axis/Group objects to dump.
711-
Defaults to all objects present in the Session.
706+
Names of Array objects to dump.
707+
Defaults to all Array objects present in the Session.
712708
display : bool, optional
713709
Whether or not to display which file is being worked on. Defaults to False.
714710
715711
Notes
716712
-----
717713
- each array is saved in a separate file
718-
- all Axis objects are saved together in the same CSV file named __axes__.csv
719-
- all Group objects are saved together in the same CSV file named __groups__.csv
720714
- all session metadata is saved in the same CSV file named __metadata__.csv
721715
722716
Examples

larray/inout/csv.py

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from larray.util.misc import skip_comment_cells, strip_rows, csv_open, deprecate_kwarg
1818
from larray.inout.session import register_file_handler
1919
from larray.inout.common import _get_index_col, FileHandler
20-
from larray.inout.pandas import df_asarray, _axes_to_df, _df_to_axes, _groups_to_df, _df_to_groups
20+
from larray.inout.pandas import df_asarray
2121
from larray.example import get_example_filepath
2222

2323

@@ -284,26 +284,9 @@ def _to_filepath(self, key):
284284
else:
285285
return key
286286

287-
def _load_axes_and_groups(self):
288-
# load all axes
289-
filepath_axes = self._to_filepath('__axes__')
290-
if os.path.isfile(filepath_axes):
291-
df = pd.read_csv(filepath_axes, sep=self.sep)
292-
self.axes = _df_to_axes(df)
293-
else:
294-
self.axes = OrderedDict()
295-
# load all groups
296-
filepath_groups = self._to_filepath('__groups__')
297-
if os.path.isfile(filepath_groups):
298-
df = pd.read_csv(filepath_groups, sep=self.sep)
299-
self.groups = _df_to_groups(df, self.axes)
300-
else:
301-
self.groups = OrderedDict()
302-
303287
def _open_for_read(self):
304288
if self.directory and not os.path.isdir(self.directory):
305289
raise ValueError("Directory '{}' does not exist".format(self.directory))
306-
self._load_axes_and_groups()
307290

308291
def _open_for_write(self):
309292
if self.directory is not None:
@@ -312,8 +295,6 @@ def _open_for_write(self):
312295
except OSError:
313296
if not os.path.isdir(self.directory):
314297
raise ValueError("Path {} must represent a directory".format(self.directory))
315-
self.axes = OrderedDict()
316-
self.groups = OrderedDict()
317298

318299
def list_items(self):
319300
fnames = glob(self.pattern) if self.pattern is not None else []
@@ -327,36 +308,18 @@ def list_items(self):
327308
fnames.remove('__metadata__')
328309
except:
329310
pass
330-
try:
331-
fnames.remove('__axes__')
332-
items = [(name, 'Axis') for name in sorted(self.axes.keys())]
333-
except:
334-
pass
335-
try:
336-
fnames.remove('__groups__')
337-
items += [(name, 'Group') for name in sorted(self.groups.keys())]
338-
except:
339-
pass
340311
items += [(name, 'Array') for name in fnames]
341312
return items
342313

343314
def _read_item(self, key, type, *args, **kwargs):
344315
if type == 'Array':
345316
return read_csv(self._to_filepath(key), *args, **kwargs)
346-
elif type == 'Axis':
347-
return self.axes[key]
348-
elif type == 'Group':
349-
return self.groups[key]
350317
else:
351318
raise TypeError()
352319

353320
def _dump_item(self, key, value, *args, **kwargs):
354321
if isinstance(value, Array):
355322
value.to_csv(self._to_filepath(key), *args, **kwargs)
356-
elif isinstance(value, Axis):
357-
self.axes[key] = value
358-
elif isinstance(value, Group):
359-
self.groups[key] = value
360323
else:
361324
raise TypeError()
362325

@@ -374,12 +337,7 @@ def _dump_metadata(self, metadata):
374337
meta.to_csv(self._to_filepath('__metadata__'), sep=self.sep, wide=False, value_name='')
375338

376339
def save(self):
377-
if len(self.axes) > 0:
378-
df = _axes_to_df(self.axes.values())
379-
df.to_csv(self._to_filepath('__axes__'), sep=self.sep, index=False)
380-
if len(self.groups) > 0:
381-
df = _groups_to_df(self.groups.values())
382-
df.to_csv(self._to_filepath('__groups__'), sep=self.sep, index=False)
340+
pass
383341

384342
def close(self):
385343
pass

0 commit comments

Comments
 (0)