Skip to content
Snippets Groups Projects
Commit 4be11211 authored by Henry Luetcke's avatar Henry Luetcke Committed by Adam Laskowski
Browse files

removing legacy folder

parent 309fc82a
No related branches found
No related tags found
1 merge request!40SSDM-13578 : 2PT : Database and V3 Implementation - include the new AFS "free"...
% wrapper / demo script for Matlab openBIS toolbox
% Todo: convert to ML live script
%% Enter connection info for openBIS
% connection details can be stored in the file user_data.json or entered manually
try
user_data = jsondecode(fileread('user_data.json'));
username = user_data.User;
url = user_data.URL;
catch
username = ''; % enter valid user name
url = 'https://XYZ.ethz.ch/openbis:8443'; % replace with correct URL
end
% specify openBIS password
pw = passwordEntryDialog('CheckPasswordLength',0);
%% Connect to openBIS
login(url, username, pw)
clear pw % remove password from workspace
% login will create a global variable called obi
global obi
%% Check if connection is still active
% we can check if the connection has timed-out or is still valid
if obi.is_session_active
fprintf('\nConnection to host %s is still valid.\n', char(obi.url))
else
disp('Connection is not active. Please re-connect.')
end
%% Logout
obi.logout()
\ No newline at end of file
% demo script for accessing openBIS from Matlab using Pybis
% we use Matlab's capability to call Python libraries
% Pybis documentation is available here:
% https://sissource.ethz.ch/sispub/openbis/blob/master/pybis/src/python/README.md
%% first check the Python version
pyversion
% a different Python version can be selected by specifying the Python
% executable
% on OS X: pyversion /Users/Henry/miniconda3/bin/python
% on Windows: pyversion 3.6
%% connection details for openBIS
% connection details can be stored in the file user_data.json or entered manually
try
user_data = jsondecode(fileread('user_data.json'));
username = user_data.User;
url = user_data.URL;
catch
username = ''; % enter valid user name
url = 'https://XYZ.ethz.ch/openbis:8443'; % replace with correct URL
end
% specify openBIS password
pw = passwordEntryDialog('CheckPasswordLength',0);
%% connect to openBIS
obi = py.pybis.Openbis(url, pyargs('verify_certificates', 0));
obi.login(username, pw, pyargs('save_token', 1));
clear pw
obi
%% select datasets
datasets = obi.get_datasets(pyargs('type','HISTOLOGY'));
datasets
%% download dataset
% get specific dataset
ds_id = '20101105142049776-6512';
ds = obi.get_dataset(ds_id);
% download
data_dir = 'data';
ds.download(pyargs('destination', data_dir, 'wait_until_finished', false));
%% list files / directories in dataset
% get list and convert to Matlab cell array
files = cell(ds.get_file_list);
for iii = 1:numel(files)
iii_file = files{iii};
fprintf('%d - %s - isDir: %d\n', iii, string(iii_file{'pathInDataSet'}), iii_file{'isDirectory'})
end
%% Spaces
% list spaces
spaces = obi.get_spaces;
spaces
% create a new space
space_id = 'MATLAB_TEST';
space = obi.new_space(pyargs('code', space_id, 'description', 'test space for Matlab access to openBIS'));
space
space.save;
% delete created space afterwards
% space.delete('just a test');
%% Projects
% list projects in Space
space.get_projects
% create new project
project_code = 'test_project';
project_description = 'my awsome project';
project = space.new_project(pyargs('code', project_code, ...
'description' , project_description));
project = project.save();
space.get_projects
%% clean up
% logout
obi.logout()
function delete_space(code, reason)
% DELETE_SPACE
% Delete space in the openBIS instance. A reason has to be specified.
global obi
% test connection
test_connection(obi)
space = obi.get_space(code);
space.delete(reason);
end
\ No newline at end of file
function [matlab_table] = df_to_table(df)
%df_to_table
% Returns a Matlab table for a Python dataframe
csv_temp = sprintf('%s.csv', tempname);
df.to_csv(csv_temp);
matlab_table = readtable(csv_temp);
delete(csv_temp);
end
\ No newline at end of file
function [file_list] = download_ds_files(obi, ds_id, data_dir)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
ds = obi.get_dataset(ds_id);
% download
ds.download(pyargs('destination', data_dir, 'wait_until_finished', false));
% get list and convert to Matlab cell array
file_list = cell(ds.get_file_list);
end
function dataset_types = get_dataset_types
%get_dataset_types
% Returns a table of all available dataset types
global obi
% test connection
test_connection(obi)
dataset_types = obi.get_dataset_types();
dataset_types = df_to_table(dataset_types.df);
end
function [datasets] = get_datasets(ds_type)
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
global obi
datasets = obi.get_datasets(pyargs('type',ds_type));
datasets = df_to_table(datasets.df);
end
function experiment_types = get_experiment_types
%get_experiment_types
% Returns a table of all available experiment types
global obi
% test connection
test_connection(obi)
experiment_types = obi.get_experiment_types();
experiment_types = df_to_table(experiment_types.df);
end
function material_types = get_material_types
%get_material_types
% Returns a table of all available material types
global obi
% test connection
test_connection(obi)
material_types = obi.get_material_types();
material_types = df_to_table(material_types.df);
end
function sample_types = get_sample_types
%get_sample_types
% Returns a table of all available sample types
global obi
% test connection
test_connection(obi)
sample_types = obi.get_sample_types();
sample_types = df_to_table(sample_types.df);
end
function spaces = get_spaces
% GET_SPACES
% Get a list of all available spaces (DataFrame object). To create a sample or a
% dataset, you need to specify in which space it should live.
global obi
% test connection
test_connection(obi)
spaces = obi.get_spaces();
spaces = df_to_table(spaces.df);
end
function terms = get_terms
%get_sample_types
%Returns information about existing vocabulary terms.
%If a vocabulary code is provided, it only returns the terms of that vocabulary
global obi
% test connection
test_connection(obi)
terms = obi.get_terms();
terms = df_to_table(terms.df);
end
function login(varargin)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
global obi
if nargin
url = varargin{1};
user = varargin{2};
pw = varargin{3};
else
[url, user, pw] = user_url_pw_inputdlg;
end
obi = py.pybis.Openbis(url, pyargs('verify_certificates', 0));
obi.login(user, pw, pyargs('save_token', 1));
end
function [url, user, pw] = user_url_pw_inputdlg
prompt = {'openBIS URL:', 'openBIS user:'};
title = 'openBIS connection details';
definput = {'https://XYZ.ethz.ch/openbis:8443', ''};
answer = inputdlg(prompt, title, 1, definput);
url = answer{1};
user = answer{2};
pw = passwordEntryDialog('CheckPasswordLength',0);
end
\ No newline at end of file
function logout
%LOGOUT
% Log out of openBIS. After logout, the session token is no longer valid.
global obi
% test connection
test_connection(obi)
obi.logout;
end
function new_space(code, description)
% NEW_SPACE
% Creates a new space in the openBIS instance.
global obi
% test connection
test_connection(obi)
space = obi.new_space(pyargs('code', code, 'description', description));
space.save;
end
\ No newline at end of file
function test_connection(obi)
if ~ismethod(obi, 'is_session_active')
error('No connection to openBIS. Did you log in?')
end
if ~obi.is_session_active
error('No active connection found. Try to re-connect.')
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment