4
$\begingroup$

I have created my first mathematica 'program' as I think of it, to do some image processing. After reading in an image, it does a lot of stuff (including smoothing, fourier transform, functional representation, searching for critical points, outputing various files).

Now, I have hundreds of images that I want to process: obviously a job for a script. But I am somehow not understanding the whole mess of CommandLine, ScriptCommandline, or even how to properly edit script files (am I supposed to do it in the notebook? Because I'm using a lot of mathematica symbols, so I'm not sure how I could edit otherwise) or how to get scripts to run! It seems part of the problem is that scripting has changed in Mathematica 9 and a lot of previous Q&A entries focus on solutions for prior versions.

I have tried to get a basic script working on my system and I get really odd errors. E.g.,

#!/Applications/Mathematica.app/Contents/MacOS/MathematicaScript - script

(*generate "num" samples of a mixed distribution*)

num = ToExpression[$ScriptCommandLine[[2]]];
Print /@ RandomVariate[
  MixtureDistribution[{1, 2}, {NormalDistribution[1, 0.2], 
    NormalDistribution[3, 0.1]}], num, WorkingPrecision -> 50]

I get:

$ ./test.m 10

./test.m: line 1: F814W_knotD_ACSHRC_2.91772_J8L001031_50_.fits: command not found

./test.m: line 3: F814W_knotD_ACSHRC_2.91772_J8L001031_50_.fits: command not found

./test.m: line 4: *#!/Applications/Mathematica.app/Contents/MacOS/MathematicaScript: No such file or directory

./test.m: line 5: F814W_knotD_ACSHRC_2.91772_J8L001031_50_.fits: command not found

./test.m: line 6: syntax error near unexpected token `*generate'

./test.m: line 6: `(*(generate "num" samples of a mixed distribution)*)'

(those fits files are in the same directory, and I have NO IDEA why it is talking about them)

Apologies for the newbie questions... hopefully this will be the last one for a while!!

$\endgroup$
5
  • 4
    $\begingroup$ If you have it working fine for 1 image, then the simplest solution would be to use FileNames to get a list of your image files and map your processing function on the list of files. Ditch the command line stuff for now (unless you have something on the order of thousands/millions of files and want to run it as a batch job in the background) $\endgroup$
    – rm -rf
    Mar 18, 2013 at 19:29
  • $\begingroup$ Yes, any reason not to use the frontend? Setting $HistoryLength=0 is your friend in any case... $\endgroup$
    – Yves Klett
    Mar 19, 2013 at 12:10
  • $\begingroup$ Yves and rm: He clearly states that he wants it scriptable. The front end may not be appropriate for his tasks. $\endgroup$
    – Eric Brown
    Jul 2, 2013 at 13:21
  • $\begingroup$ @EricBrown OP said: "I have created my first mathematica 'program'..." — I would assume that for someone with a perl/python background, using a script is probably what comes to mind first, since the notebook interface is "new" and "different". That's why I suggested that they try to do it in the FE and just map the function onto the list of files with the caveat that they use the command line tools only if there are thousands of such files and it needs to be done in the bg. $\endgroup$
    – rm -rf
    Jul 2, 2013 at 18:54
  • $\begingroup$ @rm-rf He only has hundreds, so I guess he should do it in the front end ;-) $\endgroup$
    – Eric Brown
    Jul 2, 2013 at 19:11

1 Answer 1

2
$\begingroup$

Running this script (using the command line ./m.m 10):

#!/Applications/Mathematica.app/Contents/MacOS/MathematicaScript -script

Print["$CommandLine ",$CommandLine]
Print["$ScriptCommandLine ",$ScriptCommandLine]

num  = ToExpression[$ScriptCommandLine[[2]]];
Print /@ RandomVariate[
  MixtureDistribution[ 
    {1, 2},
    {NormalDistribution[1, 0.2],
     NormalDistribution[3, 0.1]}
  ], num,  WorkingPrecision -> 50]

produces this output:

"$CommandLine "{"/Applications/Mathematica.app/Contents/MacOS/MathKernel",
    "-runfirst", "$TopDirectory=\"/Applications/Mathematica.app\"",
    "-script", "./m.m", "--", "./m.m", "10"}

"$ScriptCommandLine "{"./m.m", "10"}

RandomVariate::precw: The precision of the argument function (0.2) is less than WorkingPrecision (50.).    
RandomVariate::precw: The precision of the argument function (0.1) is less than WorkingPrecision (50.).
0.92362819946979828682106332648179186999782644688375288593250014006949862225735`50.
2.96103460393397517361865538246094728357244256713761606288848501196722308810617`50.
3.22939297821123529215296685035285675313397350043182381985784401189668224360831`50.
1.1299906869216789003021775704042950015847131552578997137000319851764860372795`50.
0.98372766259941810455456369928428955216530068876264782478190250513548418549102`50.
2.99474740714212023905930071261105968607684691113025981921878898633890621560002`50.
3.08308256890408917851510742142231108891166198857011370317940248018144299347434`50.
2.90810822969521393130661661374500034380445967807811953513718200317327510030055`50.
1.10703367896318353371179385146615947292441105821579138236874372016639707194483`50.
3.08119877332762386092842323142602437311553703195855620999359603331598402631764`50.

Your example had a space between - and script; that may be the problem, although when I tried it with a space the errors were different from yours.

I'm on Mac OSX, Mathematica version 9.

$\endgroup$
1
  • $\begingroup$ Concur about the space between - and MathematicaScript $\endgroup$
    – dearN
    Mar 18, 2013 at 23:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.