Skip to content

Commit

Permalink
Merge pull request #902 from mantidproject/532-set-default-save-direc…
Browse files Browse the repository at this point in the history
…tory

Set default save directory
  • Loading branch information
SilkeSchomann committed Sep 15, 2023
2 parents da88046 + 17cdac0 commit a8d0bef
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/mslice/models/workspacemanager/file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os.path
from qtpy.QtWidgets import QFileDialog
from mantid.api import MDNormalization
from mantid.kernel import ConfigService
from mslice.util.mantid.mantid_algorithms import CreateMDHistoWorkspace, SaveAscii, SaveMD, SaveNexus, SaveNXSPE
from mslice.models.workspacemanager.workspace_provider import get_workspace_handle
from mslice.models.axis import Axis
Expand Down Expand Up @@ -111,7 +112,7 @@ def save_matlab(workspace, path):
y = workspace.raw_ws.extractY()
e = workspace.raw_ws.extractE()
mdict = {'x': x, 'y': y, 'e': e, 'labels': labels}
savemat(path, mdict=mdict)
savemat(_to_absolute_path(path), mdict=mdict)


def _save_histogram_workspace(workspace, path):
Expand Down Expand Up @@ -220,4 +221,11 @@ def _get_slice_mdhisto_xye(histo_ws):


def _output_data_to_ascii(output_path, out_data, header):
np.savetxt(str(output_path), out_data, fmt='%12.9e', header=header)
np.savetxt(_to_absolute_path(str(output_path)), out_data, fmt='%12.9e', header=header)


def _to_absolute_path(filepath: str) -> str:
"""Returns an absolute path in which a file should be saved."""
if os.path.isabs(filepath):
return filepath
return os.path.join(ConfigService.getString("defaultsave.directory"), filepath)
15 changes: 14 additions & 1 deletion tests/file_io_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
from tempfile import gettempdir
import unittest
from mantid.simpleapi import CreateMDHistoWorkspace
from mantid.kernel import ConfigService

from mslice.models.axis import Axis
from mslice.models.cut.cut_functions import output_workspace_name
from mslice.models.workspacemanager.file_io import _save_cut_to_ascii, _save_slice_to_ascii, get_save_directory
from mslice.models.workspacemanager.file_io import (_save_cut_to_ascii, _save_slice_to_ascii, get_save_directory,
_to_absolute_path)
from mslice.workspace.histogram_workspace import HistogramWorkspace


Expand Down Expand Up @@ -100,3 +102,14 @@ def test_save_slice_to_ascii(self, output_method):
self.assertEqual(output_method.call_args[0][0], 'path1')
np.testing.assert_array_equal(output_method.call_args[0][1], [[-10, -10, 1, 3], [-10, 10, 3, 5],
[10, -10, 2, 4], [10, 10, 4, 6]])

def test_to_absolute_path_returns_the_same_path_if_it_is_already_absolute(self):
abs_path = "/c/user/documents/filename.txt"
self.assertEqual(abs_path, _to_absolute_path(abs_path))

def test_to_absolute_path_returns_a_path_in_the_default_save_dir_if_the_path_provided_is_relative(self):
default_save_directory = "/d/user/documents/default_save_dir"
ConfigService.setString("defaultsave.directory", default_save_directory)

rel_path = "datadir/filename.txt"
self.assertEqual(f"{default_save_directory}/{rel_path}", _to_absolute_path(rel_path))

0 comments on commit a8d0bef

Please sign in to comment.