Copying files with a Macro?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Copying files with a Macro?

Adam Cliffe
I'm trying to get a macro to make a copy of a few XML files while it
is processing images.

I've tried using: File.rename(path1, path2);

But this removes the original. Does anyone know a good trick for
copying a file? Or do I just need to open it, save it somewhere else
and close it?

thanks

Adam


Dr Adam Cliffe
Research Scientist, Rorth Lab

Institute of Molecular and Cell Biology
61 Biopolis Drive
Proteos
Singapore 138673

tel: +65 6586 9731









Note: This message may contain confidential information. If this Email/Fax has been sent to you by mistake, please notify the sender and delete it immediately. Thank you.
Reply | Threaded
Open this post in threaded view
|

Re: Copying files with a Macro?

Michael Schmid
Hi Adam,

neither Java nor the ImageJ macro language has a File.copy method.

Method (1)
You can either open it and save it somewhere else in a macro.

Method (2)
Use the 'exec' command.
I have not tried, but it should be something like the following:
On unix-style and Mac:
   exec("cp", oldPath, newPath);
and on Windows
   exec("cmd", "/c", "copy", , oldPath, newPath);

Method (3)
Write a plugin that has a static method and use the 'call' macro  
function. The code of the plugin could be similar to that of
   http://www.java2s.com/Code/Java/File-Input-Output/FileCopyinJava.htm
except that the static method should not be named 'main' and should  
not use a String array 'args' but have two Strings as an argument.

Michael
________________________________________________________________

On 21 Jan 2010, at 10:46, Adam Cliffe wrote:

> I'm trying to get a macro to make a copy of a few XML files while it
> is processing images.
>
> I've tried using: File.rename(path1, path2);
>
> But this removes the original. Does anyone know a good trick for
> copying a file? Or do I just need to open it, save it somewhere else
> and close it?
>
> thanks
>
> Adam
>
>
> Dr Adam Cliffe
> Research Scientist, Rorth Lab
>
> Institute of Molecular and Cell Biology
> 61 Biopolis Drive
> Proteos
> Singapore 138673
>
> tel: +65 6586 9731
>
>
>
>
>
>
>
>
>
> Note: This message may contain confidential information. If this  
> Email/Fax has been sent to you by mistake, please notify the sender  
> and delete it immediately. Thank you.
Reply | Threaded
Open this post in threaded view
|

Macro function for random seed?

Norbert Vischer-2
Hello all,

does the macro language allow to reproduce a random series from a seed number?

Obviously, random() does not accept any seed argument.

best regards, Norbert
Reply | Threaded
Open this post in threaded view
|

Re: Macro function for random seed?

Gabriel Landini
On Thursday 21 Jan 2010  11:54:48 you wrote:
> does the macro language allow to reproduce a random series from a seed
>  number?

The linear congruential method is not the best, but it is easy to implement:

http://en.wikipedia.org/wiki/Linear_congruential_generator

I hope it helps.

Cheers

G.
Reply | Threaded
Open this post in threaded view
|

Re: Macro function for random seed?

Michael Schmid
In reply to this post by Norbert Vischer-2
Hi Norbert,

sorry, no way to have a seed for the random number in a macro with  
the current ImageJ version.

You could consider writing a short plugin that is evoked via 'call':
Roughly like this (I have not tried it)

import ij.plugin.PlugIn;
import ij.util.Tools;
import java.util.Random;

public class Random_ implements PlugIn {
     static String random = new Random();
     public void run(String arg) {} //not used
     public void getRandom () {
         return Double.toString(random.nextDouble());
     }
     public void setSeed(String seedS) {
        random.setSeed((long)(tools.parseDouble(seedS)));
     }
}

In the macro:
   call("Random_.setSeed", mySeed);
   myRandomNumber = parseFloat(call("Random_.getRandom"));

Michael
________________________________________________________________

On 21 Jan 2010, at 12:48, Norbert Vischer wrote:

> Hello all,
>
> does the macro language allow to reproduce a random series from a  
> seed number?
>
> Obviously, random() does not accept any seed argument.
>
> best regards, Norbert
Reply | Threaded
Open this post in threaded view
|

Re: Macro function for random seed?

Gabriel Landini
On Thursday 21 Jan 2010  14:26:47 you wrote:
> sorry, no way to have a seed for the random number in a macro with
> the current ImageJ version.
>
> You could consider writing a short plugin that is evoked via 'call':
> Roughly like this (I have not tried it)

I am curious, would the above work for subsequent independent calls of the
plugin from the macro?
I mean for how long would the seed remain valid?

Cheers

G.
Reply | Threaded
Open this post in threaded view
|

Re: Copying files with a Macro?

Chris Mawata
In reply to this post by Michael Schmid
Depends on the version of Java -- if you are using Java 5 or later and
you just want to copy a file then the java.nio package will
give you much better performance. Do something like:

FileChannel in = null, out = null;
try {
in = new FileInputStream(args[0]).getChannel();
out = new FileOutputStream(args[1]).getChannel();
out.transferFrom(in, 0, in.size());
} catch (IOException e) {
e.printStackTrace();
}
finally{
//…close the channels
}



Michael Schmid wrote:

> Hi Adam,
>
> neither Java nor the ImageJ macro language has a File.copy method.
>
> Method (1)
> You can either open it and save it somewhere else in a macro.
>
> Method (2)
> Use the 'exec' command.
> I have not tried, but it should be something like the following:
> On unix-style and Mac:
> exec("cp", oldPath, newPath);
> and on Windows
> exec("cmd", "/c", "copy", , oldPath, newPath);
>
> Method (3)
> Write a plugin that has a static method and use the 'call' macro
> function. The code of the plugin could be similar to that of
> http://www.java2s.com/Code/Java/File-Input-Output/FileCopyinJava.htm
> except that the static method should not be named 'main' and should
> not use a String array 'args' but have two Strings as an argument.
>
> Michael
> ________________________________________________________________
>
> On 21 Jan 2010, at 10:46, Adam Cliffe wrote:
>
>> I'm trying to get a macro to make a copy of a few XML files while it
>> is processing images.
>>
>> I've tried using: File.rename(path1, path2);
>>
>> But this removes the original. Does anyone know a good trick for
>> copying a file? Or do I just need to open it, save it somewhere else
>> and close it?
>>
>> thanks
>>
>> Adam
>>
>>
>> Dr Adam Cliffe
>> Research Scientist, Rorth Lab
>>
>> Institute of Molecular and Cell Biology
>> 61 Biopolis Drive
>> Proteos
>> Singapore 138673
>>
>> tel: +65 6586 9731
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> Note: This message may contain confidential information. If this
>> Email/Fax has been sent to you by mistake, please notify the sender
>> and delete it immediately. Thank you.
>
Reply | Threaded
Open this post in threaded view
|

Re: Macro function for random seed?

Michael Schmid
In reply to this post by Gabriel Landini
Hi Gabriel,

that's a good question! The seed will be lost if the plugin gets  
removed by the Garbage Collector and the next time it will be  
initialized using the default seed.
So one should avoid this:

public class Random_ implements PlugIn {
     static Random random = new Random(); //(sorry, there was a  
mistake in this line)
     IJ.register(Random_.class); //protect static class variables  
from gc

In practice, I don't remember having ever seen a plugin's static  
variables being removed by the Garbage Collector, but maybe that's  
because I usually have plenty of free memory...

Michael
________________________________________________________________

On 21 Jan 2010, at 15:29, Gabriel Landini wrote:

> On Thursday 21 Jan 2010  14:26:47 you wrote:
>> sorry, no way to have a seed for the random number in a macro with
>> the current ImageJ version.
>>
>> You could consider writing a short plugin that is evoked via 'call':
>> Roughly like this (I have not tried it)
>
> I am curious, would the above work for subsequent independent calls  
> of the
> plugin from the macro?
> I mean for how long would the seed remain valid?
>
> Cheers
>
> G.
Reply | Threaded
Open this post in threaded view
|

Re: Macro function for random seed?

Chris Mawata
Static variables will survive garbage collection of any instance (they
don't belong to the instance).
Remember that they can be called using classname.variable even when
there are 0 instances.
However in programs with multiple classloaders, they will be garbage
collected when the classloader
is garbage collected but at that point there are no instances of classes
loaded by that classloader
available so it is moot.

Michael Schmid wrote:

> Hi Gabriel,
>
> that's a good question! The seed will be lost if the plugin gets
> removed by the Garbage Collector and the next time it will be
> initialized using the default seed.
> So one should avoid this:
>
> public class Random_ implements PlugIn {
>     static Random random = new Random(); //(sorry, there was a mistake
> in this line)
>     IJ.register(Random_.class); //protect static class variables from gc
>
> In practice, I don't remember having ever seen a plugin's static
> variables being removed by the Garbage Collector, but maybe that's
> because I usually have plenty of free memory...
>
> Michael
> ________________________________________________________________
>
> On 21 Jan 2010, at 15:29, Gabriel Landini wrote:
>
>> On Thursday 21 Jan 2010  14:26:47 you wrote:
>>> sorry, no way to have a seed for the random number in a macro with
>>> the current ImageJ version.
>>>
>>> You could consider writing a short plugin that is evoked via 'call':
>>> Roughly like this (I have not tried it)
>>
>> I am curious, would the above work for subsequent independent calls
>> of the
>> plugin from the macro?
>> I mean for how long would the seed remain valid?
>>
>> Cheers
>>
>> G.
>
Reply | Threaded
Open this post in threaded view
|

Re: Macro function for random seed?

Rasband, Wayne (NIH/NIMH) [E]
In reply to this post by Norbert Vischer-2
On Jan 21, 2010, at 6:48 AM, Norbert Vischer wrote:

> Hello all,
>
> does the macro language allow to reproduce a random series from a seed number?
>
> Obviously, random() does not accept any seed argument.
>
> best regards, Norbert

The random function in the v1.43o daily build accepts a seed argument (a whole number):

    random("seed", seed);

-wayne