Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Defining directory/Datas the perl script should work with

by perlnewbie (Novice)
on Oct 21, 2021 at 13:20 UTC ( [id://11137850]=perlquestion: print w/replies, xml ) Need Help??

perlnewbie has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I want to write a script which should use extern programms for MRI-preprocessing, doing several processing commands to a bunch of data. For example: running brain extraction from the command line in linux I would write bet <inputimage> <nameforoutputimage> -m Then the programm bet in fsl would be opened and skull stripping would be performed, the output image would be saved in the same folder of the inputimage. In my script I want the outputimage to be saved in an other folder. Could anyone help me with this? I think there a lot of syntax errors ...

#!/usr/bin/perl use strict; use warnings; use diagnostics; my $dir = "usr/bin/Test_Protokoll" my $001= "prob_001"; my $002= "prob_002"; my $003= "prob_003"; @filter: ("001", "002", "003"); #Brainextraction foreach ("001", "002", "003") { system ("bet SYNT_MPRAGE_T1_PD_B0corr YNT_MPRAGE_T1_PD_B0corr_brain + -m"); }

Replies are listed 'Best First'.
Re: Defining directory/Datas the perl script should work with
by marto (Cardinal) on Oct 21, 2021 at 14:04 UTC

    "I think there a lot of syntax errors ...

    Welcome. Your running with strict, warnings and diagnostics, perl will tell you about problems.

    my $dir = "usr/bin/Test_Protokoll"

    Should be:

    my $dir = "usr/bin/Test_Protokoll";

    If this is the same bet you're talking about, you need to specify the output path/file, bet input.file outdir/out.file -m. You'll also have seen:

    "Numeric variables with more than one digit may not start with '0' at bet.pl line 9 (#1)"

    So don't do that. You improperly create @filter and then never use it. Consider:

    #!/usr/bin/perl use strict; use warnings; use diagnostics; my $dir = 'usr/bin/Test_Protokoll'; my @filters = ('001', '002', '003'); foreach my $filter ( @filters ){ print "filter: $filter\n"; system("bet SYNT_MPRAGE_T1_PD_B0corr YNT_MPRAGE_T1_PD_B0corr_brain + -m"); }

    You don't say what you want to do with these filters, and perhaps you want to include $dir as the output path to your bet call. See also $?

      "...should be..." Where is usr/bin/?

      «The Crux of the Biscuit is the Apostrophe»

        Does the OP mean my $dir = "/usr/bin/Test_Protokoll";? The change is a slash before usr.

      Thanks for your reply! I want to use the filters later on. I want to do a lot of processing steps to for example 30 datasets, so I want to tell the script in each processing step to do it for all the @filters defined datasets, but at the moment i'm not that far. -M

Re: Defining directory/Datas the perl script should work with
by kcott (Archbishop) on Oct 21, 2021 at 23:29 UTC

    G'day perlnewbie,

    Welcome to the Monastery.

    "I think there a lot of syntax errors"

    Yes, there are but these seem to have been addressed. I was concerned with this block of code:

    my $001= "prob_001"; my $002= "prob_002"; my $003= "prob_003"; @filter: ("001", "002", "003"); #Brainextraction foreach ("001", "002", "003")

    I may be wrong, but it looks like you were planning to, or at least thinking about, using symbolic references. This is generally considered to be a bad move and I would recommend that you steer clear of them until you're more experienced; even then, think very carefully before using them.

    Follow the link I provided. Note that they can easily result in coding errors or simply confusing code. Your lexical (my) variables do not work with this mechanism. Perhaps inadvertently, you've expressly forbidden your code from using them (with use strict;): it will abort compilation if you attempt to do it. The strict pragma restricts their usage for a very good reason.

    "In my script I want the outputimage to be saved in an other folder. Could anyone help me with this?"

    I would set up default paths for your input and output directories; then allow users to specify alternatives via options. I would include thorough checking of all input from options and clean any tainted data (see perlsec for more about that).

    Unsurprisingly, I have neither the bet program nor any MRI data. For testing, I set up an extremely minimal script to replace bet:

    $ cat zombie cp $1 $2

    and two directories, whole_head and just_brain, for input and output, respectively. The former has four tiny files, the latter starts empty:

    ken@titan ~/tmp/pm_11137850/whole_head $ for i in `ls -1 wh_*`; do echo $i; cat $i; done wh_001 W_001 wh_002 X_002 wh_003 Y_003 wh_004 Z_004 ken@titan ~/tmp/pm_11137850/just_brain $ ls -l total 0

    Here's the code (pm_11137850_system.pl):

    #!/usr/bin/env perl use strict; use warnings; BEGIN { delete @ENV{qw{PATH IFS CDPATH ENV BASH_ENV}} } use autodie ':all'; use constant { INDIR => '/home/ken/tmp/pm_11137850/whole_head', OUTDIR => '/home/ken/tmp/pm_11137850/just_brain', ZOMBIE => '/home/ken/tmp/pm_11137850/zombie', }; use Getopt::Long; my ($in_dir, $out_dir); my $filter_string = ''; GetOptions( 'indir=s' => \$in_dir, 'outdir=s' => \$out_dir, 'filters=s' => \$filter_string, ); my @filters; my @raw_filters = split /,/, $filter_string; my @problems; _set_dirs(\$in_dir, \$out_dir, \@problems); _get_filters(\@raw_filters, \@filters, \@problems); die @problems if @problems; system(ZOMBIE, INDIR."/wh_$_", OUTDIR."/jb_$_") for @filters; sub _get_filters { my ($raw_filters, $filters, $problems) = @_; my $re = qr{^(\d+)$}; if (! @$raw_filters) { push @$problems, "No filters specified.\n"; } else { for my $raw_filter (@$raw_filters) { if ($raw_filter =~ $re) { push @$filters, $1; } else { push @$problems, "Invalid filter: '$raw_filter'\n"; } } } return; } sub _set_dirs { my ($in, $out, $problems) = @_; my $re = qr{^(\/[\w\/]+)$}; if (length $$in) { if ($$in =~ $re) { $$in = $1; if (! -d $$in) { push @$problems, "Non-existent input directory: '$$in' +\n"; } } else { push @$problems, "Invalid input directory name: '$$in'\n"; } } else { $$in = INDIR; } if (length $$out) { if ($$out =~ $re) { $$out = $1; if (! -d $$out) { push @$problems, "Non-existent output directory: '$$ou +t'\n"; } } else { push @$problems, "Invalid output directory name: '$$out'\n +"; } } else { $$out = OUTDIR; } return; }

    You'll obviously need to make a number of changes for your own needs: the paths with '/home/ken/...'; perhaps s/ZOMBIE/BETCMD/; the regexes for checking user input; and so on.

    Do note that the system() function takes a list. You'll probably want to expand my simplistic one line

    system(ZOMBIE, INDIR."/wh_$_", OUTDIR."/jb_$_") for @filters;

    to include some error checking, handling and reporting. And, obviously, the list of arguments will need to be changed.

    Take a look at Getopt::Long to change or extend options. Also, the "Documentation and help texts" section, along with Pod::Usage, for an improved user experience.

    Here's a sample run showing that there's no tainting:

    ken@titan ~/tmp/pm_11137850 $ perl -T ./pm_11137850_system.pl -filters 001,004 ken@titan ~/tmp/pm_11137850/just_brain $ for i in `ls -1 jb_*`; do echo $i; cat $i; done jb_001 W_001 jb_004 Z_004

    Here's another showing what would probably be more normal usage:

    ken@titan ~/tmp/pm_11137850 $ ./pm_11137850_system.pl -filters 003 ken@titan ~/tmp/pm_11137850/just_brain $ for i in `ls -1 jb_*`; do echo $i; cat $i; done jb_001 W_001 jb_003 Y_003 jb_004 Z_004

    Here's some example error messages:

    ken@titan ~/tmp/pm_11137850 $ ./pm_11137850_system.pl -outdir /fred -indir wilma Invalid input directory name: 'wilma' Non-existent output directory: '/fred' No filters specified.

    — Ken

      Thank you very much!! But I think I still have some questions to this code:  BEGIN { delete @ENV{qw{PATH IFS CDPATH ENV BASH_ENV}} } --> why should I use this? I haven't used it in former scripts... Moreover, I don't understand why I need the sub_ after I run the system? And somehow when I try to run the modified script (with my directories etc) an error " "use" not allowed in expression at script.pl line 17, at end of line" " is given out. So do I need the use  GetOptions::LONG at all? Thanks again. Best Regards, M

        kcott's script is a good way to start this project.

        So do I need the use  GetOptions::LONG at all?

        I would encourage you to use it (note that it's Getopt::Long click to see examples of use) as it offers the best way of dealing with user-supplied parameters at runtime instead of hardcoding those in the script. It's even better than a configuration file read when your script starts because it can allow specifying both a configuration file and extra command-line options which can override options in the configuration file. E.g. myprog --config a/b/c  --iterations 100 --bin-dir /usr/bin/protokoll2. So, it will help you start this project from a solid foundation. I guess extract-brain needs a hell of parameter tweaking depending on protocol, resolution, quality. That's a good way to deal with that complexity. Next step would be to decide on a configuration-file format, plenty of options in Perl's CPAN repository of modules. Additionally, you may want to run your script with different configurations and parameters in parallel to assess their efficacy efficiently. Again, using configuration files and command-line parameters will help immensely.

        Also, note that you can specify subs (functions) anywhere in your Perl script and not necessarily before calling them(re: I don't understand why I need the sub_ after I run the system?). I personally define all my subs at the end of a script, leaving the beginning for the "main" section.

        bw, bliako

        "I think I still have some questions to this code"

        I'm pleased to see that you are not a cargo cult programmer. See "Cargo cult programming" if you are unfamiliar with that term.

        I provided a variety of links which you should follow. The one to perlsec explains the inclusion of the BEGIN block. If you're stumped by the meaning of the BEGIN itself, read "perlmod: BEGIN, UNITCHECK, CHECK, INIT and END". You may also want to look at: the delete() function; "perldata: Slices"; "perlvar: %ENV"; and, perlop for qw (search the page: qw appears in a number of places). You can also try commenting out the BEGIN line, running with the taint checking I showed (perl -T ...); then checking the messages output.

        I'm not too sure about "sub_" as it doesn't appear in my code (perhaps a typo in your code). There are two subroutine declarations of the form sub _name {...}. These are called earlier in the code with _name(...). Prefixing a subroutine name with an underscore is an informal way of indicating it is intended to be private (used by the code itself, but not directly called by the user of the code): this is fairly common practice and you're likely to encounter it in many places. Also look at perlsub; in particular, the To declare subroutines: and To call subroutines: sections of its SYNOPSIS.

        If you have GetOptions::LONG in your code, it's a fairly major typo: the first 'O' should be lowercase; there's no 'ions'; and, the last part is capitalised, not all uppercase. Case and spelling matter. I wrote: "use Getopt::Long;" at line 14 and "GetOptions(...);" at line 17. You may have somehow confused those two lines, omitted a semicolon near those lines, or something else: without seeing exactly what code you wrote, I'm completely guessing.

        That covers everything you enquired about. If more questions arise, feel free to ask.

        — Ken

Re: Defining directory/Datas the perl script should work with
by perlfan (Vicar) on Oct 21, 2021 at 22:41 UTC
    > I want to write a script which should use extern programms (sic)

    You should write a bash script if that's all you're doing. Perl will help if you're going to be getting a little more involved with data itself.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11137850]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-24 06:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found