retrieve variables from txt file and run command

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

retrieve variables from txt file and run command

Matti Christoph
Dear all,

I have a macro, helping to annotate images with scale bar, channel
names, doing a montage and so on. So far, the initial dialogue gives the
user several options, and is filled with default values.

I like to have a text file with those variables and recall them
automatically when the Macro is started, and yes, that works.

setOption("ExpandableArrays", true);

mp = getDirectory("macros");
pathset = mp+"CnZsettings.txt";
     string2 = newArray();
     file = File.openAsRawString(pathset);
     delimiter= '\n';
     string2 = replace(file, delimiter, "@");
     values = newArray();
     values = split(string2, "@");

However, when taking the value from the text file (e.g. Green) and
assigning to a variable:

a=values[0];

the run(a); or SetColor(a); to change either the channel colour or the
foreground colour don't work, giving me /Unrecognised command: "Green"/.
Retrieving however numerical values from the same .txt file and using
them e.g. for the scale bar do work.

Do you have any suggestion where to look for a solution? I believe that
I somehow have to change the "type" of the variable or something?

I already tried a="\""+values[0]+"\""; but this didn't work, giving me
/Unrecognised command: ""Green""/.

I am very happy if one of you could help me.

Best,

Chris


--
Signature

Christoph Matti


Biotechnologie Institut Thurgau an der Universität Konstanz
Unterseestr. 47
CH - 8280 Kreuzlingen
T: +41 71 6785022
E: [hidden email] <mailto:[hidden email]>


--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: retrieve variables from txt file and run command

Michael Schmid-3
Hi Chris,

you can have a list of commands for 'run' in a file.
The following works:

    path="/tmp/filename.txt";
    txt = File.openAsRawString(path);
    delimiter= '\n';
    lines = split(txt, delimiter);
    for (i=0; i<lines.length; i++)
       run(lines[i]);

And the file may contain:
Blobs (25K)
Green
Smooth
Invert

There should be no quotes, no spaces, no blank lines in the file.
It will open the blobs sample, apply the Green LUT, smooth and invert it.
Like this, it only works with 'run' commands that need no further
arguments, like
    run("Green"): //apply green LUT


If you want 'run' commands with arguments, you can use e.g. tabs to
separate the name and argument:

    path="/tmp/filename.txt";
    txt = File.openAsRawString(path);
    delimiter= '\n';
    lines = split(txt, delimiter);
    for (i=0; i<lines.length; i++) {
       parts = split(lines[i],"\t");
       if (parts.length ==1)
          run(parts[0]);
       else if (parts.length ==2)
          run(parts[0], parts[1]);
    }

Now you can process this file:
Blobs (25K)
Gaussian Blur... sigma=10

What cannot be done so easily this way is calling macro commands other
than 'run'.


Michael
________________________________________________________________
On 14.12.18 15:31, Matti Christoph wrote:

> Dear all,
>
> I have a macro, helping to annotate images with scale bar, channel
> names, doing a montage and so on. So far, the initial dialogue gives the
> user several options, and is filled with default values.
>
> I like to have a text file with those variables and recall them
> automatically when the Macro is started, and yes, that works.
>
> setOption("ExpandableArrays", true);
>
> mp = getDirectory("macros");
> pathset = mp+"CnZsettings.txt";
>      string2 = newArray();
>      file = File.openAsRawString(pathset);
>      delimiter= '\n';
>      string2 = replace(file, delimiter, "@");
>      values = newArray();
>      values = split(string2, "@");
>
> However, when taking the value from the text file (e.g. Green) and
> assigning to a variable:
>
> a=values[0];
>
> the run(a); or SetColor(a); to change either the channel colour or the
> foreground colour don't work, giving me /Unrecognised command: "Green"/.
> Retrieving however numerical values from the same .txt file and using
> them e.g. for the scale bar do work.
>
> Do you have any suggestion where to look for a solution? I believe that
> I somehow have to change the "type" of the variable or something?
>
> I already tried a="\""+values[0]+"\""; but this didn't work, giving me
> /Unrecognised command: ""Green""/.
>
> I am very happy if one of you could help me.
>
> Best,
>
> Chris
>
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: retrieve variables from txt file and run command

Matti Christoph
Dear Michael,

it worked, thank you! The issue was apparently the

string2 = replace(file, delimiter, "@");

which I included to be sure that there are no issues with the replace,
but after removing all goes fine.

However, i have a follow up question. I give the user the option, to
save the new set values as standard settings (saves the array with new
data as a txt file using a for loop and print(file lines[i]);) and I
want to give the user the option, to re-load "default values". This is
achieved by opening a second txt file (default_settings.txt), again add
it to an array and save this one as the settings.txt file. So far so good.

         file = File.open(pathset);
         for(i=0; i<lines.length; i++) {
                 print(file, lines[i]);
           }

I then want to re-open the macro (call it xy.ijm), and tried this with
the runMacro("xy.ijm"), however there it gives me an error that the file
from which it reads the values is empty.

         wait(2000);
         runMacro("xy.ijm");

Even adding a wait(2000) after printing the new values and opening the
macro again didn't work. If I put an "exit()" after the print, and then
start the macro again by hand, the new values are propperly loaded. I
can leave it at that, however a totally automated way of closing and
re-starting the macro would be nice. Or even, say, go back to line xy
and start again.

Do you have any clues how to achieve this?

Best wishes,

Chris

Am 14.12.2018 um 16:40 schrieb Michael Schmid:

> Hi Chris,
>
> you can have a list of commands for 'run' in a file.
> The following works:
>
>    path="/tmp/filename.txt";
>    txt = File.openAsRawString(path);
>    delimiter= '\n';
>    lines = split(txt, delimiter);
>    for (i=0; i<lines.length; i++)
>       run(lines[i]);
>
> And the file may contain:
> Blobs (25K)
> Green
> Smooth
> Invert
>
> There should be no quotes, no spaces, no blank lines in the file.
> It will open the blobs sample, apply the Green LUT, smooth and invert it.
> Like this, it only works with 'run' commands that need no further
> arguments, like
>    run("Green"): //apply green LUT
>
>
> If you want 'run' commands with arguments, you can use e.g. tabs to
> separate the name and argument:
>
>    path="/tmp/filename.txt";
>    txt = File.openAsRawString(path);
>    delimiter= '\n';
>    lines = split(txt, delimiter);
>    for (i=0; i<lines.length; i++) {
>       parts = split(lines[i],"\t");
>       if (parts.length ==1)
>          run(parts[0]);
>       else if (parts.length ==2)
>          run(parts[0], parts[1]);
>    }
>
> Now you can process this file:
> Blobs (25K)
> Gaussian Blur...    sigma=10
>
> What cannot be done so easily this way is calling macro commands other
> than 'run'.
>
>
> Michael
> ________________________________________________________________
> On 14.12.18 15:31, Matti Christoph wrote:
>> Dear all,
>>
>> I have a macro, helping to annotate images with scale bar, channel
>> names, doing a montage and so on. So far, the initial dialogue gives
>> the user several options, and is filled with default values.
>>
>> I like to have a text file with those variables and recall them
>> automatically when the Macro is started, and yes, that works.
>>
>> setOption("ExpandableArrays", true);
>>
>> mp = getDirectory("macros");
>> pathset = mp+"CnZsettings.txt";
>>      string2 = newArray();
>>      file = File.openAsRawString(pathset);
>>      delimiter= '\n';
>>      string2 = replace(file, delimiter, "@");
>>      values = newArray();
>>      values = split(string2, "@");
>>
>> However, when taking the value from the text file (e.g. Green) and
>> assigning to a variable:
>>
>> a=values[0];
>>
>> the run(a); or SetColor(a); to change either the channel colour or
>> the foreground colour don't work, giving me /Unrecognised command:
>> "Green"/. Retrieving however numerical values from the same .txt file
>> and using them e.g. for the scale bar do work.
>>
>> Do you have any suggestion where to look for a solution? I believe
>> that I somehow have to change the "type" of the variable or something?
>>
>> I already tried a="\""+values[0]+"\""; but this didn't work, giving
>> me /Unrecognised command: ""Green""/.
>>
>> I am very happy if one of you could help me.
>>
>> Best,
>>
>> Chris
>>
>>
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: retrieve variables from txt file and run command

Michael Schmid-3
Hi Chris,

concerning splitting lines with "\n" as delimiter:

The split function of ImageJ works independent of the type of actual
line termination (CR, LF, or CRLF). If you replace "\n" with something
else, it will work correctly only for files created on Linux-like
systems, where the line terminator is LF (= "\n"). On Windows, the line
terminator is CRLF, so you will get an extra CR ("\r", hex 0D) character
that does not get replaced. ImageJ commands preceded by a CR character
won't be recognized.

Technical details behind it: To avoid problems with different line
terminations, ImageJ uses BufferedReader.readLine for "split" with a
"\n" character:
 
https://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html#readLine()
"A line is considered to be terminated by any one of a line feed ('\n'),
a carriage return ('\r'), or a carriage return followed immediately by a
linefeed."

---

Concerning writing a macro and then executing it: Did you close the file
after writing?
You need to close a file after writing, to make sure all data are
actually transferred to disk.


Michael
________________________________________________________________
On 10.01.19 16:15, Matti Christoph wrote:

> Dear Michael,
>
> it worked, thank you! The issue was apparently the
>
> string2 = replace(file, delimiter, "@");
>
> which I included to be sure that there are no issues with the replace,
> but after removing all goes fine.
>
> However, i have a follow up question. I give the user the option, to
> save the new set values as standard settings (saves the array with new
> data as a txt file using a for loop and print(file lines[i]);) and I
> want to give the user the option, to re-load "default values". This is
> achieved by opening a second txt file (default_settings.txt), again add
> it to an array and save this one as the settings.txt file. So far so good.
>
>          file = File.open(pathset);
>          for(i=0; i<lines.length; i++) {
>                  print(file, lines[i]);
>            }
>
> I then want to re-open the macro (call it xy.ijm), and tried this with
> the runMacro("xy.ijm"), however there it gives me an error that the file
> from which it reads the values is empty.
>
>          wait(2000);
>          runMacro("xy.ijm");
>
> Even adding a wait(2000) after printing the new values and opening the
> macro again didn't work. If I put an "exit()" after the print, and then
> start the macro again by hand, the new values are propperly loaded. I
> can leave it at that, however a totally automated way of closing and
> re-starting the macro would be nice. Or even, say, go back to line xy
> and start again.
>
> Do you have any clues how to achieve this?
>
> Best wishes,
>
> Chris
>
> Am 14.12.2018 um 16:40 schrieb Michael Schmid:
>> Hi Chris,
>>
>> you can have a list of commands for 'run' in a file.
>> The following works:
>>
>>    path="/tmp/filename.txt";
>>    txt = File.openAsRawString(path);
>>    delimiter= '\n';
>>    lines = split(txt, delimiter);
>>    for (i=0; i<lines.length; i++)
>>       run(lines[i]);
>>
>> And the file may contain:
>> Blobs (25K)
>> Green
>> Smooth
>> Invert
>>
>> There should be no quotes, no spaces, no blank lines in the file.
>> It will open the blobs sample, apply the Green LUT, smooth and invert it.
>> Like this, it only works with 'run' commands that need no further
>> arguments, like
>>    run("Green"): //apply green LUT
>>
>>
>> If you want 'run' commands with arguments, you can use e.g. tabs to
>> separate the name and argument:
>>
>>    path="/tmp/filename.txt";
>>    txt = File.openAsRawString(path);
>>    delimiter= '\n';
>>    lines = split(txt, delimiter);
>>    for (i=0; i<lines.length; i++) {
>>       parts = split(lines[i],"\t");
>>       if (parts.length ==1)
>>          run(parts[0]);
>>       else if (parts.length ==2)
>>          run(parts[0], parts[1]);
>>    }
>>
>> Now you can process this file:
>> Blobs (25K)
>> Gaussian Blur...    sigma=10
>>
>> What cannot be done so easily this way is calling macro commands other
>> than 'run'.
>>
>>
>> Michael
>> ________________________________________________________________
>> On 14.12.18 15:31, Matti Christoph wrote:
>>> Dear all,
>>>
>>> I have a macro, helping to annotate images with scale bar, channel
>>> names, doing a montage and so on. So far, the initial dialogue gives
>>> the user several options, and is filled with default values.
>>>
>>> I like to have a text file with those variables and recall them
>>> automatically when the Macro is started, and yes, that works.
>>>
>>> setOption("ExpandableArrays", true);
>>>
>>> mp = getDirectory("macros");
>>> pathset = mp+"CnZsettings.txt";
>>>      string2 = newArray();
>>>      file = File.openAsRawString(pathset);
>>>      delimiter= '\n';
>>>      string2 = replace(file, delimiter, "@");
>>>      values = newArray();
>>>      values = split(string2, "@");
>>>
>>> However, when taking the value from the text file (e.g. Green) and
>>> assigning to a variable:
>>>
>>> a=values[0];
>>>
>>> the run(a); or SetColor(a); to change either the channel colour or
>>> the foreground colour don't work, giving me /Unrecognised command:
>>> "Green"/. Retrieving however numerical values from the same .txt file
>>> and using them e.g. for the scale bar do work.
>>>
>>> Do you have any suggestion where to look for a solution? I believe
>>> that I somehow have to change the "type" of the variable or something?
>>>
>>> I already tried a="\""+values[0]+"\""; but this didn't work, giving
>>> me /Unrecognised command: ""Green""/.
>>>
>>> I am very happy if one of you could help me.
>>>
>>> Best,
>>>
>>> Chris
>>>
>>>
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: retrieve variables from txt file and run command

Matti Christoph
Dear Michael,

thank you for the hint. I had the file close in other parts of the
macro, but totally forgot it there. Now it works with:

         File.close(file);
         wait(1000);
         runMacro("xy.ijm");
         exit();

I also realised that the file I am now using as settings.txt was one
generated entirely with imageJ, using print(), therefore also the line
terminators are only LF and properly recognised.

Best wishes,

Chris

Am 10.01.2019 um 17:15 schrieb Michael Schmid:

> Hi Chris,
>
> concerning splitting lines with "\n" as delimiter:
>
> The split function of ImageJ works independent of the type of actual
> line termination (CR, LF, or CRLF). If you replace "\n" with something
> else, it will work correctly only for files created on Linux-like
> systems, where the line terminator is LF (= "\n"). On Windows, the
> line terminator is CRLF, so you will get an extra CR ("\r", hex 0D)
> character that does not get replaced. ImageJ commands preceded by a CR
> character won't be recognized.
>
> Technical details behind it: To avoid problems with different line
> terminations, ImageJ uses BufferedReader.readLine for "split" with a
> "\n" character:
>
> https://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html#readLine()
>
> "A line is considered to be terminated by any one of a line feed
> ('\n'), a carriage return ('\r'), or a carriage return followed
> immediately by a linefeed."
>
> ---
>
> Concerning writing a macro and then executing it: Did you close the
> file after writing?
> You need to close a file after writing, to make sure all data are
> actually transferred to disk.
>
>
> Michael
> ________________________________________________________________
> On 10.01.19 16:15, Matti Christoph wrote:
>> Dear Michael,
>>
>> it worked, thank you! The issue was apparently the
>>
>> string2 = replace(file, delimiter, "@");
>>
>> which I included to be sure that there are no issues with the
>> replace, but after removing all goes fine.
>>
>> However, i have a follow up question. I give the user the option, to
>> save the new set values as standard settings (saves the array with
>> new data as a txt file using a for loop and print(file lines[i]);)
>> and I want to give the user the option, to re-load "default values".
>> This is achieved by opening a second txt file (default_settings.txt),
>> again add it to an array and save this one as the settings.txt file.
>> So far so good.
>>
>>          file = File.open(pathset);
>>          for(i=0; i<lines.length; i++) {
>>                  print(file, lines[i]);
>>            }
>>
>> I then want to re-open the macro (call it xy.ijm), and tried this
>> with the runMacro("xy.ijm"), however there it gives me an error that
>> the file from which it reads the values is empty.
>>
>>          wait(2000);
>>          runMacro("xy.ijm");
>>
>> Even adding a wait(2000) after printing the new values and opening
>> the macro again didn't work. If I put an "exit()" after the print,
>> and then start the macro again by hand, the new values are propperly
>> loaded. I can leave it at that, however a totally automated way of
>> closing and re-starting the macro would be nice. Or even, say, go
>> back to line xy and start again.
>>
>> Do you have any clues how to achieve this?
>>
>> Best wishes,
>>
>> Chris
>>
>> Am 14.12.2018 um 16:40 schrieb Michael Schmid:
>>> Hi Chris,
>>>
>>> you can have a list of commands for 'run' in a file.
>>> The following works:
>>>
>>>    path="/tmp/filename.txt";
>>>    txt = File.openAsRawString(path);
>>>    delimiter= '\n';
>>>    lines = split(txt, delimiter);
>>>    for (i=0; i<lines.length; i++)
>>>       run(lines[i]);
>>>
>>> And the file may contain:
>>> Blobs (25K)
>>> Green
>>> Smooth
>>> Invert
>>>
>>> There should be no quotes, no spaces, no blank lines in the file.
>>> It will open the blobs sample, apply the Green LUT, smooth and
>>> invert it.
>>> Like this, it only works with 'run' commands that need no further
>>> arguments, like
>>>    run("Green"): //apply green LUT
>>>
>>>
>>> If you want 'run' commands with arguments, you can use e.g. tabs to
>>> separate the name and argument:
>>>
>>>    path="/tmp/filename.txt";
>>>    txt = File.openAsRawString(path);
>>>    delimiter= '\n';
>>>    lines = split(txt, delimiter);
>>>    for (i=0; i<lines.length; i++) {
>>>       parts = split(lines[i],"\t");
>>>       if (parts.length ==1)
>>>          run(parts[0]);
>>>       else if (parts.length ==2)
>>>          run(parts[0], parts[1]);
>>>    }
>>>
>>> Now you can process this file:
>>> Blobs (25K)
>>> Gaussian Blur...    sigma=10
>>>
>>> What cannot be done so easily this way is calling macro commands
>>> other than 'run'.
>>>
>>>
>>> Michael
>>> ________________________________________________________________
>>> On 14.12.18 15:31, Matti Christoph wrote:
>>>> Dear all,
>>>>
>>>> I have a macro, helping to annotate images with scale bar, channel
>>>> names, doing a montage and so on. So far, the initial dialogue
>>>> gives the user several options, and is filled with default values.
>>>>
>>>> I like to have a text file with those variables and recall them
>>>> automatically when the Macro is started, and yes, that works.
>>>>
>>>> setOption("ExpandableArrays", true);
>>>>
>>>> mp = getDirectory("macros");
>>>> pathset = mp+"CnZsettings.txt";
>>>>      string2 = newArray();
>>>>      file = File.openAsRawString(pathset);
>>>>      delimiter= '\n';
>>>>      string2 = replace(file, delimiter, "@");
>>>>      values = newArray();
>>>>      values = split(string2, "@");
>>>>
>>>> However, when taking the value from the text file (e.g. Green) and
>>>> assigning to a variable:
>>>>
>>>> a=values[0];
>>>>
>>>> the run(a); or SetColor(a); to change either the channel colour or
>>>> the foreground colour don't work, giving me /Unrecognised command:
>>>> "Green"/. Retrieving however numerical values from the same .txt
>>>> file and using them e.g. for the scale bar do work.
>>>>
>>>> Do you have any suggestion where to look for a solution? I believe
>>>> that I somehow have to change the "type" of the variable or something?
>>>>
>>>> I already tried a="\""+values[0]+"\""; but this didn't work, giving
>>>> me /Unrecognised command: ""Green""/.
>>>>
>>>> I am very happy if one of you could help me.
>>>>
>>>> Best,
>>>>
>>>> Chris
>>>>
>>>>
>>>
>>> --
>>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html