Commit 38c5201f authored by Luis Fernandez Ruiz's avatar Luis Fernandez Ruiz
Browse files

Python

-  multiv_multip_regression: added Multivariant Multiple Regression. Save reg models
- GUI_SANS: interface for evaluating one by one images. Read nxs files
- move_files: generalize for different scatter models the function: write_CNN_prediction
- label_classify_image_folder: adapt it to classify core-shell Sphere images

Matlab
- create_suggested_images: modify to create core_shell_sphere in adittion to Sphere. You can decide by inputting the model
- create_img_param_general: create core_shell_sphere in adittion to Sphere
-save_sim_sphere_coresphere: create core_shell_sphere in adittion to Sphere
parent fa7f4ad3
function create_img_param_general(save_img_path, scat_model, varargin)
%{
/*********************************************************************************************
given a set of instrument and sample parameters (varargin), and the
scattered model of the particles (Sphere, Core-Shell Sphere,...), this
script generate an image and save it in the path specified in 'save_img_path'
------------------------------------------------------
Author: FERNANDEZ RUIZ Luis
Last modified: 10/07/2019
File: create_img_param.m
*********************************************************************************************
%}
% Define the scattered model
scatter_model = scat_model; %'Sphere';
% Initialize variables with values by default. After, we will modify some of
% them with values contain in varargin (input parameter)
mono_tof = 'mono';
delta_wav = 10; %percentage
instrument = 'd22';
sample_shape = 'rectangular';
sample_size = [10e-3 10e-3];
attenuator = 1;
thickness = 0.1;
%Prepare scattering model parameter structure
params_scatter.contrast = 6e-6;
background_model = 'Empty Cell'; %Empty Cell,Cryostat Ox7T
horiz_beam_centre_params = [0 -randi([15 35])];
horiz_beam_centre = horiz_beam_centre_params(1);
%instrument
params_scatter.dist = 8;
params_scatter.col = 8;
params_scatter.wav = 6;
params_scatter.bg = 1;
params_scatter.poly_fwhm = 0;
params_scatter.scale = 0.01;
%sample
if strcmp(scatter_model, 'Sphere')
params_scatter.radius = 100;
params_scatter.contrast = 6e-6;
elseif strcmp(scatter_model, 'Core-Shell Sphere')
params_scatter.radius = 100;
params_scatter.shell = 100;
params_scatter.rhocore = 7e-07;
params_scatter.rhoshell = 1.6e-06;
params_scatter.rhomatrix = 3.5e-06;
end
% We replace values by default for those pass by the user in varargin.
% the structure of data inputted in varargin is: ['dist', dist_value, 'col', col_value, 'wav', wav_value, ...]
if nargin >=1 %i.e. varagin exists
params = fieldnames(params_scatter);
for n = 1:length(params) % we search between all the params by default (defined above)
% if we find them (e.g. 'col') we look for the next element in the
% array (col_value) and pass it to the algorithm to generate
% scattered images
idx=find(cellfun(@(varargin) any(strcmp(varargin,params{n})),varargin));
if not(isempty(idx))
params_scatter.(params{n}) = varargin{idx+1};
end
end
end
%%%%%%%calcul%%%%%%%%
params = zeros(1,39);
if strcmp(instrument,'d11')
params(1) = 1;
elseif strcmp(instrument, 'd22')
params(1) = 2;
elseif strcmp(instrument, 'd33')
params(1) = 3;
end
if strcmp(mono_tof,'mono')
params(2) = 1;
elseif strcmp(mono_tof, 'tof')
params(2) = 2;
end
params(3) = params_scatter.dist;
params(4) = params_scatter.col;
if strcmp(sample_shape,'rectangular')
params(5) = 1;
elseif strcmp(sample_shape, 'circular')
params(5) = 2;
end
if params(5) == 1
params(6) = sample_size(1);
elseif params(5) == 2
params(6) = sample_size;
end
params(7) = params_scatter.wav;
if attenuator == 1
params(8) = 1;
elseif attenuator == 10
params(8) = 2;
elseif attenuator == 100
params(8) = 3;
end
params(9) = thickness;
if strcmp(background_model,'Empty Cell')
params(10) = 1;
elseif strcmp(background_model,'Cryostat Ox7T')
params(10) = 2;
end
if strcmp(scatter_model,'Sphere')
params(11) = 1;
params(12) = params_scatter.radius;
params(13) = params_scatter.poly_fwhm;
params(14) = params_scatter.contrast;
params(15) = params_scatter.scale;
elseif strcmp(scatter_model,'Core-Shell Sphere')
params(11) = 2;
params(16) = params_scatter.radius;
params(17) = params_scatter.poly_fwhm;
params(18) = params_scatter.shell;
params(19) = params_scatter.rhocore;
params(20) = params_scatter.rhoshell;
params(21) = params_scatter.rhomatrix;
params(22) = params_scatter.scale;
elseif strcmp(scatter_model,'Guinier')
params(11) = 3;
params(23) = params_scatter.rg;
params(24) = params_scatter.i0;
elseif strcmp(scatter_model,'Porod')
params(11) = 4;
params(25) = params_scatter.surf;
params(26) = params_scatter.contrast;
elseif strcmp(scatter_model,'Flux Line Bragg Peak')
params(11) = 5;
params(27) = params_scatter.field;
params(28) = params_scatter.width;
params(29) = params_scatter.az_cor;
params(30) = params_scatter.rad_cor;
params(31) = params_scatter.pen_depth;
params(32) = params_scatter.san;
params(33) = params_scatter.phi;
params(34) = params_scatter.rot;
elseif strcmp(scatter_model,'Porod + Magnetic Porod')
params(11) = 6;
params(35) = params_scatter.surf;
params(36) = params_scatter.contrast;
params(37) = params_scatter.magsurf;
params(38) = params_scatter.magcontrast;
elseif strcmp(scatter_model,'H2O')
params(11) = 7;
elseif strcmp(scatter_model,'D2O')
params(11) = 8;
elseif strcmp(scatter_model,'Ribosome')
params(11) = 9;
elseif strcmp(scatter_model,'Gabel Protien')
params(11) = 10;
end
params(39) = params_scatter.bg;
%Call the instrument simulation
output = sim_calc('instrument',instrument,'mono_tof',mono_tof,'measurement_time',600,'det',params_scatter.dist,'det2',10,'det1',5,....
'col',params_scatter.col,'source_size',[45e-3,55e-3],'sample_size',sample_size,'wav',params_scatter.wav,'delta_wav',delta_wav,....
'bx',0,'by',0,'attenuator',attenuator,'thickness',thickness,....
'scatter_model',scatter_model,'scatter_model_params',params_scatter,....
'background_model',background_model,'background_model_params',[],....
'blocked_model','Blocked Beam','blocked_model_params',[], 'horiz_beam_centre', horiz_beam_centre);%[45e-3,55e-3]
if isempty(output)
disp('Sans model calculation failed somewhere: Please consult output text window')
end
if strcmp(scatter_model, 'Sphere')
%filename = ['./sim_data/Sphere/scatter_image/Sphere' background_model '_col' num2str(collimation) '_dist' num2str(distance) '_radius' num2str(params_scatter.radius) '_poly' num2str(params_scatter.poly_fwhm) '_contrast' num2str(params_scatter.contrast) '_scale' num2str(params_scatter.scale) '_bg' num2str(params_scatter.bg) '_wav' num2str(wav) '_thickness' num2str(thickness) '_beamcenter' num2str(horiz_beam_centre) '.txt'];
filename2 = [save_img_path '/Sphere' background_model '_col' num2str(params_scatter.col) '_dist' num2str(params_scatter.dist) '_radius' num2str(params_scatter.radius) '_poly' num2str(params_scatter.poly_fwhm) '_contrast' num2str(params_scatter.contrast) '_scale' num2str(params_scatter.scale) '_bg' num2str(params_scatter.bg) '_wav' num2str(params_scatter.wav) '_thickness' num2str(thickness) '_beamcenter' num2str(horiz_beam_centre) '.jpg'];
%filename3 = ['./sim_data/Sphere/params/Sphere' background_model '_col' num2str(collimation) '_dist' num2str(distance) '_radius' num2str(params_scatter.radius) '_poly' num2str(params_scatter.poly_fwhm) '_contrast' num2str(params_scatter.contrast) '_scale' num2str(params_scatter.scale) '_bg' num2str(params_scatter.bg) '_wav' num2str(wav) '_thickness' num2str(thickness) '_beamcenter' num2str(horiz_beam_centre) '.txt'];
elseif strcmp(scatter_model, 'Core-Shell Sphere')
%filename = ['./sim_data/CoreShellSphere/scatter_image/Sphere' background_model '_col' num2str(collimation) '_dist' num2str(distance) '_radius' num2str(params_scatter.radius) '_poly' num2str(params_scatter.poly_fwhm) '_contrast' num2str(params_scatter.contrast) '_scale' num2str(params_scatter.scale) '_bg' num2str(params_scatter.bg) '_wav' num2str(wav) '_thickness' num2str(thickness) '_beamcenter' num2str(horiz_beam_centre) '.txt'];
filename2 = [save_img_path '/CoreShellSphere' background_model '_col' num2str(params_scatter.col) '_dist' num2str(params_scatter.dist) '_radius' num2str(params_scatter.radius) '_poly' num2str(params_scatter.poly_fwhm) '_shell' num2str(params_scatter.shell) '_rhocore' num2str(params_scatter.rhocore) '_rhoshell' num2str(params_scatter.rhoshell) '_rhomatrix' num2str(params_scatter.rhomatrix) '_scale' num2str(params_scatter.scale) '_bg' num2str(params_scatter.bg) '_wav' num2str(params_scatter.wav) '_thickness' num2str(thickness) '_beamcenter' num2str(horiz_beam_centre) '.jpg'];
%filename3 = ['./sim_data/CoreShellSphere/params/Sphere' background_model '_col' num2str(collimation) '_dist' num2str(distance) '_radius' num2str(params_scatter.radius) '_poly' num2str(params_scatter.poly_fwhm) '_contrast' num2str(params_scatter.contrast) '_scale' num2str(params_scatter.scale) '_bg' num2str(params_scatter.bg) '_wav' num2str(wav) '_thickness' num2str(thickness) '_beamcenter' num2str(horiz_beam_centre) '.txt'];
end
data = abs(output.data1(:,:,1));
%fid = fopen(filename,'w');
%fid3 = fopen(filename3,'w');
%Plot results, just for fun
frames = size(output.data1,3);
for n = 1:frames
figure('Visible','off')
data = abs(output.data1(:,:,n));
%figure
pcolor(log10(data));shading interp;
ax = gca;
set(ax,'YTick',[],'XTick',[])
fig = gcf;%s\t%f\t
%csvwrite(filename, data);
%csvwrite(filename3, params);
[imageData, alpha] = export_fig;
imwrite(imageData,filename2);
end
close all
end
\ No newline at end of file
This diff is collapsed.
function save_sim_sphere_coresphere(save_img_path, scat_model) %save_img_path, scat_model
%save_img_path = '/home/dpt/fernandez-ruiz/sim/sim_data/CoreShellSphere';
%scat_model = 'Core-Shell Sphere'; %'Sphere' 'Core-Shell Sphere'
col_params = [2 2.8 4 5.6 8 11.2 14.4 17.6]; %D22
dist_params = [1.4 2.8 4 5.6 8 11.2 14.4 17.6];
background_params = [0 0.05 1]; % water = 1;D2O = 0.05
horiz_beam_centre_params = [0 -randi([15 35])];
mono_tof = 'mono';
wav_params = [5 6 8 12]; %i = 2:2:30
delta_wav = 10; %percentage
instrument = 'd22';
sample_shape = 'rectangular';
sample_size = [10e-3 10e-3];
attenuator = 1;
thickness = 0.1;
%Prepare scattering model parameter structure
scatter_model = scat_model; %'Sphere';
if strcmp(scatter_model, 'Sphere')
radius_params = linspace(20, 800, 10); %randi([20 800],1)
contrast_params = [1 2 3 4 5 6 7 8 9 10 11]/10e5; %randi([1 10],1)/10e5
scale_params = [1 10 20 30 40 50 60 70 80 90 100]/100; %randi([1 100],1)/100
poly_params = [1 20 40 60 80 100 120 140 160 180 200]/10; %randi([0 200],1)/10
params_scatter.radius = 150;
params_scatter.contrast = 6e-6;
elseif strcmp(scatter_model, 'Core-Shell Sphere')
radius_params = linspace(20, 800, 10);
poly_params = linspace(0, 20, 10);
scale_params = linspace(0, 1, 10);
shell_params = linspace(0, 200, 10);
rhocore_params = linspace(-5.7e-6, 6.5e-6, 10);
rhoshell_params = linspace(-5.7e-6, 6.5e-6, 10);
rhomatrix_params = linspace(-5.7e-6, 6.5e-6, 10);
params_scatter.radius = 450;
%params_scatter.poly_fwhm = 0;
%params_scatter.scale = 0.01;
params_scatter.shell = 100;
params_scatter.rho_core = 7e-07;
params_scatter.rho_shell = 1.6e-06;
params_scatter.rho_matrix = 3.5e-06;
end
params_scatter.poly_fwhm = 10;
params_scatter.scale = 0.01;
params_scatter.background = 0;
background_model = 'Empty Cell'; %Empty Cell,Cryostat Ox7T
horiz_beam_centre = horiz_beam_centre_params(1);
count = 0;
while count < 50
for k = 1:4 %1:4
wav = wav_params(k); %i = 2:2:30
for b = 2:2
params_scatter.background = background_params(b);
for i = 1:8 %1:8
collimation = col_params(i);
distance = dist_params(i);
for c = 1:10 %20:8:800
params_scatter.radius = radius_params(randi([1 10]));
params_scatter.shell = shell_params(randi([1 10]));
params_scatter.rho_core = rhocore_params(randi([1 10]));
params_scatter.rho_shell = rhoshell_params(randi([1 10]));
params_scatter.rho_matrix = rhomatrix_params(randi([1 10]));
horiz_beam_centre = 0;
%%%%%%%calcul%%%%%%%%
params = zeros(1,39);
if strcmp(instrument,'d11')
params(1) = 1;
elseif strcmp(instrument, 'd22')
params(1) = 2;
elseif strcmp(instrument, 'd33')
params(1) = 3;
end
if strcmp(mono_tof,'mono')
params(2) = 1;
elseif strcmp(mono_tof, 'tof')
params(2) = 2;
end
params(3) = distance;
params(4) = collimation;
if strcmp(sample_shape,'rectangular')
params(5) = 1;
elseif strcmp(sample_shape, 'circular')
params(5) = 2;
end
if params(5) == 1
params(6) = sample_size(1);
elseif params(5) == 2
params(6) = sample_size;
end
params(7) = wav;
if attenuator == 1
params(8) = 1;
elseif attenuator == 10
params(8) = 2;
elseif attenuator == 100
params(8) = 3;
end
params(9) = thickness;
if strcmp(background_model,'Empty Cell')
params(10) = 1;
elseif strcmp(background_model,'Cryostat Ox7T')
params(10) = 2;
end
if strcmp(scatter_model,'Sphere')
params(11) = 1;
params(12) = params_scatter.radius;
params(13) = params_scatter.poly_fwhm;
params(14) = params_scatter.contrast;
params(15) = params_scatter.scale;
elseif strcmp(scatter_model,'Core-Shell Sphere')
params(11) = 2;
params(16) = params_scatter.radius;
params(17) = params_scatter.poly_fwhm;
params(18) = params_scatter.shell;
params(19) = params_scatter.rho_core;
params(20) = params_scatter.rho_shell;
params(21) = params_scatter.rho_matrix;
params(22) = params_scatter.scale;
elseif strcmp(scatter_model,'Guinier')
params(11) = 3;
params(23) = params_scatter.rg;
params(24) = params_scatter.i0;
elseif strcmp(scatter_model,'Porod')
params(11) = 4;
params(25) = params_scatter.surf;
params(26) = params_scatter.contrast;
elseif strcmp(scatter_model,'Flux Line Bragg Peak')
params(11) = 5;
params(27) = params_scatter.field;
params(28) = params_scatter.width;
params(29) = params_scatter.az_cor;
params(30) = params_scatter.rad_cor;
params(31) = params_scatter.pen_depth;
params(32) = params_scatter.san;
params(33) = params_scatter.phi;
params(34) = params_scatter.rot;
elseif strcmp(scatter_model,'Porod + Magnetic Porod')
params(11) = 6;
params(35) = params_scatter.surf;
params(36) = params_scatter.contrast;
params(37) = params_scatter.magsurf;
params(38) = params_scatter.magcontrast;
elseif strcmp(scatter_model,'H2O')
params(11) = 7;
elseif strcmp(scatter_model,'D2O')
params(11) = 8;
elseif strcmp(scatter_model,'Ribosome')
params(11) = 9;
elseif strcmp(scatter_model,'Gabel Protien')
params(11) = 10;
end
params(39) = params_scatter.background;
%Call the instrument simulation
output = sim_calc('instrument',instrument,'mono_tof',mono_tof,'measurement_time',600,'det',distance,'det2',10,'det1',5,....
'col',collimation,'source_size',[45e-3,55e-3],'sample_size',sample_size,'wav',wav,'delta_wav',delta_wav,....
'bx',0,'by',0,'attenuator',attenuator,'thickness',thickness,....
'scatter_model',scatter_model,'scatter_model_params',params_scatter,....
'background_model',background_model,'background_model_params',[],....
'blocked_model','Blocked Beam','blocked_model_params',[], 'horiz_beam_centre', horiz_beam_centre);%[45e-3,55e-3]
if isempty(output)
disp('Sans model calculation failed somewhere: Please consult output text window')
end
if strcmp(scatter_model, 'Sphere')
%filename = ['./sim_data/Sphere/scatter_image/Sphere' background_model '_col' num2str(collimation) '_dist' num2str(distance) '_radius' num2str(params_scatter.radius) '_poly' num2str(params_scatter.poly_fwhm) '_contrast' num2str(params_scatter.contrast) '_scale' num2str(params_scatter.scale) '_bg' num2str(params_scatter.background) '_wav' num2str(wav) '_thickness' num2str(thickness) '_beamcenter' num2str(horiz_beam_centre) '.txt'];
filename2 = [save_img_path '/Sphere' background_model '_col' num2str(collimation) '_dist' num2str(distance) '_radius' num2str(params_scatter.radius) '_poly' num2str(params_scatter.poly_fwhm) '_contrast' num2str(params_scatter.contrast) '_scale' num2str(params_scatter.scale) '_bg' num2str(params_scatter.background) '_wav' num2str(wav) '_thickness' num2str(thickness) '_beamcenter' num2str(horiz_beam_centre) '.jpg'];
%filename3 = ['./sim_data/Sphere/params/Sphere' background_model '_col' num2str(collimation) '_dist' num2str(distance) '_radius' num2str(params_scatter.radius) '_poly' num2str(params_scatter.poly_fwhm) '_contrast' num2str(params_scatter.contrast) '_scale' num2str(params_scatter.scale) '_bg' num2str(params_scatter.background) '_wav' num2str(wav) '_thickness' num2str(thickness) '_beamcenter' num2str(horiz_beam_centre) '.txt'];
elseif strcmp(scatter_model, 'Core-Shell Sphere')
%filename = ['./sim_data/CoreShellSphere/scatter_image/Sphere' background_model '_col' num2str(collimation) '_dist' num2str(distance) '_radius' num2str(params_scatter.radius) '_poly' num2str(params_scatter.poly_fwhm) '_contrast' num2str(params_scatter.contrast) '_scale' num2str(params_scatter.scale) '_bg' num2str(params_scatter.background) '_wav' num2str(wav) '_thickness' num2str(thickness) '_beamcenter' num2str(horiz_beam_centre) '.txt'];
filename2 = [save_img_path '/CoreShellSphere' background_model '_col' num2str(collimation) '_dist' num2str(distance) '_radius' num2str(params_scatter.radius) '_poly' num2str(params_scatter.poly_fwhm) '_shell' num2str(params_scatter.shell) '_rhocore' num2str(params_scatter.rho_core) '_rhoshell' num2str(params_scatter.rho_shell) '_rhomatrix' num2str(params_scatter.rho_matrix) '_scale' num2str(params_scatter.scale) '_bg' num2str(params_scatter.background) '_wav' num2str(wav) '_thickness' num2str(thickness) '_beamcenter' num2str(horiz_beam_centre) '.jpg'];
%filename3 = ['./sim_data/CoreShellSphere/params/Sphere' background_model '_col' num2str(collimation) '_dist' num2str(distance) '_radius' num2str(params_scatter.radius) '_poly' num2str(params_scatter.poly_fwhm) '_contrast' num2str(params_scatter.contrast) '_scale' num2str(params_scatter.scale) '_bg' num2str(params_scatter.background) '_wav' num2str(wav) '_thickness' num2str(thickness) '_beamcenter' num2str(horiz_beam_centre) '.txt'];
end
data = abs(output.data1(:,:,1));
%fid = fopen(filename,'w');
%fid3 = fopen(filename3,'w');
%Plot results, just for fun
frames = size(output.data1,3);
for n = 1:frames
figure('Visible','off')
data = abs(output.data1(:,:,n));
%figure
pcolor(log10(data));shading interp;
ax = gca;
set(ax,'YTick',[],'XTick',[])
fig = gcf;%s\t%f\t
%csvwrite(filename, data);
%csvwrite(filename3, params);
[imageData, alpha] = export_fig;
imwrite(imageData,filename2);
end
%fclose(fid);
%fclose(fid3);
if strcmp(scatter_model, 'Sphere')
fprintf('%s\t%f\t%s\t%f\t%s\t%f\t%s\t%f\t%s\t%f\t%s\t%f\t%s\t%f\t%s\t%f\t%s\t%f\t%s\t%f\n','col',collimation,'dist', distance, 'radius',params_scatter.radius, 'poly', params_scatter.poly_fwhm, 'contrast',params_scatter.contrast, 'scale',params_scatter.scale,'bg',params_scatter.background, 'wav', wav, 'thickness', thickness, 'beam center', horiz_beam_centre)
elseif strcmp(scatter_model, 'Core-Shell Sphere')
fprintf('%s\t%f\t%s\t%f\t%s\t%f\t%s\t%f\t%s\t%f\t%s\t%f\t%s\t%f\t%s\t%f\t%s\t%f\t%s\t%f\t%s\t%f\t%s\t%f\t%s\t%f\n','col',collimation,'dist', distance, 'radius',params_scatter.radius, 'poly', params_scatter.poly_fwhm, 'shell', params_scatter.shell, 'rhocore', params_scatter.rho_core, 'rhoshell', params_scatter.rho_shell, 'rhomatrix', params_scatter.rho_matrix , 'scale',params_scatter.scale,'bg',params_scatter.background, 'wav', wav, 'thickness', thickness, 'beam center', horiz_beam_centre)
end
close all
end
end
end
end
count = count + 1;
end
\ No newline at end of file
'''
/*********************************************************************************************
Graphical User Interface for Small Angle Neutron Scattering
Input: scattered image, a type of scattered model, category of scattered
image searched.
Output: optimal instrument parameters, prediction for sample's parameters
and a simulated image with both set of parameters
------------------------------------------------------
Author: FERNANDEZ RUIZ Luis
Last modified: 12/07/2019
File: GUI_SANS.py
*********************************************************************************************
'''
import matplotlib.pyplot as plt # Plotting package
import numpy as np # Fundamental package for scientific computing
import h5py
# import csv # Read csv
import pandas as pd # Dataframe handling package
import argparse # Package for parse input argument
import os # Package for manipulate paths
from os.path import join, split # Join strings to make a path
import matlab.engine # Execute matlab code
import shutil # Delete directories and files inside
import subprocess # Execute codes from terminal
import datetime # Time functions
from sklearn.externals import joblib # Save regression models
'''
FUNCTION: read_nxs
Objective: given a .nxs file, extract their instrument parameters: distance, collimation, wavelength
Arguments:
input:
nxs_path (string): path to the nxs file
output:
inst_param (array): contains dist, col and wavelength
'''
def read_nxs(nxs_path):
f = h5py.File(nxs_path, 'r')
col_path = "entry0/D22/collimation/actual_position"
col = f[col_path][0]
dist = col # distance and collimation are symetric
wav_path = "entry0/D22/selector/wavelength"
wav = int(round(f[wav_path][0]))
return dist, col, wav
'''
FUNCTION: user_input
Objective: ask the user to select an option between the ones proposed in 'possible_values' array
Arguments:
input:
possible_values (array): array with the different possibilities offered to the user
output:
possible_values[int(decision)] (string): option selected
'''
def user_input(possible_values):
decision = 999 # Initialize variable
print("-------------------------------------------------------")
print("Type the number associated to one of the options shown:") # Ask the user to select an option
for idx in range(len(possible_values)): # Show all possible values with a number next to it
print("%i) %s" % (idx, possible_values[idx]))
decision = input() # The user writes
# Check if the user has typed a number...
try:
int(decision)
except ValueError: # ...if not we tell him/her to try again
decision = 999
print("Not a number. Please, try again.")
# If he/she has not succeded in the first try, we enter in a loop until he/she does it well.
while not any([x == int(decision) for x in range(len(possible_values))]):
print("Your input is not between the options proposed. Please, try again.\n")
for idx in range(len(possible_values)):
print("%i) %s" % (idx, possible_values[idx]))
decision = input()
# Check if the user has typed a number
try:
int(decision)
except ValueError:
decision = 999
return possible_values[int(decision)]
'''
FUNCTION: obtain_parameter_val
Objective: given the name of the parameter we want to obtain the value and a image file's name with the following format
"SphereEmpty Cell_col2_dist1.4_radius110_poly0_contrast6e-06_scale0.01_bg1_wav5_thickness0.1_beamcenter0.jpg"
it extracts the value of a parameter. e.g. radius = 110
Arguments:
input:
file_name (string): name of the file we want to extract parameter's values
parameter (string): name of the parameter we want to obtain
output:
file_value (float): value of the parameter
'''
def obtain_parameter_val(file_name, parameter):
# we search in 'file_name' the position where begins the value of the parameter we are looking for
begin_value = file_name.find(parameter) + len(parameter)
# we want the length of extension. For this purpose we use splitext.
# splitext[0] gives you a string with the name of file and splitext[1] the extension
len_ext = len(os.path.splitext(file_name)[1])
# Now we look for the position where ends the value of the parameter we are looking for
# usually, each parameter-value is split from the next with a '_'. If this is the case the following 'if' is True...
if file_name.find("_", begin_value, len(file_name) - len_ext) != -1:
end_value = file_name.find("_", begin_value, len(file_name) - len_ext)
else: # if not, is the case of the last parameter(beamcenter0) that finish with a '.'
end_value = len(file_name) - len_ext
# we extract parameter's value
file_value = float(file_name[begin_value:end_value])
return file_value
'''
FUNCTION: distinct_values
Objective: given a dataframe with results.csv structure, obtain for each parameter specified in 'params' the unique
values.
Arguments:
input:
df (dataframe): dataframe with results.csv structure
params (list of strings): name of the parameters we want to analyze. e.g. ["col", "dist", "wav", "bg", ...]
output:
void
'''
def distinct_values(df, params, prediction):
column_names = []
distinct_values_array = []
df_categ = df[df["predicted"] == prediction]
for param in params:
if param == 'col':
continue
value_list = []
value_list = [v for v in set(df_categ[param])]
value_list.sort()
distinct_values_array.append([str(v) for v in value_list])
column_names.append([param + str(v) for v in value_list])
return distinct_values_array, column_names
'''
FUNCTION: create_tol_list
Objective: given a dataframe with the struct given in results.csv, extract the max and min of each column and define
the tolerance for each param with which we are going to iterate in suggest_data
it extracts all the possible values for each parameter, the number of unique values, the max and the min
Arguments:
input: