Login  Register

Re: replace substring of filename with string

Posted by MChapman on Feb 23, 2018; 11:17pm
URL: http://imagej.273.s1.nabble.com/replace-substring-of-filename-with-string-tp5020154p5020155.html

Owen,
If I understand what is going on in your macro correctly, I think you may be looking at the Grid Collection/Stitching plugin wrong. 
The plugin doesn't want all of the images to have the {ii} nomenclature but to actually have the numbers where the "i"s are located.  The rest of the name besides the {ii} needs to be the exact same in order for the plugin to work.  When you call the plugin is when you need to have the "{ii}" in the filename so that it knows the structure of what to look for.
Have you gotten the grid collection/stitching to work with the image names that you are trying to convert to manually yet, or are you trying to do both steps (creating the names and testing the automation of the process in the macro) together?
Mike

    On Friday, February 23, 2018 1:40 PM, OwenGwydion <[hidden email]> wrote:
 

 Hi everyone,

I'm writing a macro for grid/collection stitching and I've hit a big, hard
brick wall.

I would like to replace the middle part of a string with a new piece of
string. I've defined the middle part of the string as the variable
"fullSiteName", and the string I would like to replace it with is "_s{ii}".


example filename =
Condition1_A01_s01_w1591FD421-91D8-454A-B944-B5D678E36C5D.tif

FirstPartOfFileName = Condition1_A01
fullSiteName = _s01
SecondPartOfFileName = _w1591FD421-91D8-454A-B944-B5D678E36C5D.tif (this is
unique to filename)


The problem I'm getting is that, though "fullSiteName" is replaced by the
new string, every file in my directory ends up with the same
SecondPartOfFileName as the first file it processes, and so the stitching
crashes.

I can't work out why this is. If I perform the same replace function on a
specified list (see example below), then it works fine. But if it is part of
my long macro with user-specified subfolders etc, it crashes.




//Working example:
list=
newArray("Condition1_A01_s01_w1591FD421-91D8-454A-B944-B5D678E36C5D.tif",
"Condition1_A01_s02_w145F67118-77DB-4613-A0E4-43F7F911C05E.tif",
"Condition1_A01_s03_w171933024-AE6E-4642-8BAB-6D78DADBEDFF.tif");
Array.print(list);
for(i=0; i<list.length; i++){
    indexSite=indexOf(list[i], "_s");
                indexEndSite=indexOf(list[i], "_w");
                fullSiteName=substring(list[i], indexSite, indexEndSite);
                print(fullSiteName);
                VariableName="_s{ii}";
                genericFilename= replace(list[i], fullSiteName, VariableName);            // this
replaces the full site name eg. "_s01" with "_s{ii}" to make it variable for
the stitching plugin
                print("this is the generic fileName: " + genericFilename);   
}





I've provided a section of my longer macro. In order to test it out, you
will need to make a directory called "newDir". Then a subdirectory eg.
"Well_A01_Channel_1". Fill that directory with three files with the
filenames: Condition1_A01_s01_w1591FD421-91D8-454A-B944-B5D678E36C5D.tif",
"Condition1_A01_s02_w145F67118-77DB-4613-A0E4-43F7F911C05E.tif",
"Condition1_A01_s03_w171933024-AE6E-4642-8BAB-6D78DADBEDFF.tif"




//Problem macro:
newDir=getDirectory("specify the newDir directory");
// Creating a list of the Well+Channel subfolders
    newDirlist=getFileList(newDir);   
    wellChannelList=newArray();
        for(j=0; j<newDirlist.length;
j++){wellChannelList=Array.concat(wellChannelList, newDirlist[j]);}
        Array.print(wellChannelList);


// Choose which Well+Channel subfolder you want to process for tiled
stitching
    checkSelection();

    function checkSelection(){
        Dialog.create("Prepare to start stitching files");
        Dialog.addMessage("Choose which Well+Channel folder you want to stitch, or
select 'Exit macro' when finished");
        Dialog.addChoice("Folder to stitch", wellChannelList);                                //Choose
which channel you want to stitch
        Dialog.addCheckbox("Exit macro", false);                                             //Opt to exit macro
        Dialog.show();
        Exit=Dialog.getCheckbox();
        Choice=Dialog.getChoice();
        tempDir=newDir+Choice;
        File.makeDirectory(tempDir);                                                        //Creates new directory based on
user choice called tempDir
        File.isDirectory(tempDir);   
        tempList=getFileList(tempDir);
        Array.print(tempList);
       
        for(i=0; i<tempList.length; i++){

            // identifies the full site name and replaces with variable {ii}
            if(endsWith(tempList[i],".tif") || endsWith(tempList[i], ".TIF")){
                indexSite=indexOf(tempList[i], "_s");
                indexEndSite=indexOf(tempList[i], "_w");
                fullSiteName=substring(tempList[i], indexSite, indexEndSite);
                print(fullSiteName);
                VariableName="_s{ii}";
                genericFilename= replace(tempList[i], fullSiteName, VariableName);            //
this replaces the full site name eg. "_s01" with "_s{ii}" to make it
variable for the stitching plugin
                print("this is the generic fileName: " + genericFilename);   
                }


                    // run grid/collection stitching
                    if(Exit==false){
                            print("Prepare to stitch: " +Choice);
                            run("Grid/Collection stitching", "type=[Grid: row-by-row]
order=[Right & Down                ] grid_size_x=3 grid_size_y=1
tile_overlap=10 first_file_index_i=1 directory=[&tempDir]
file_names=[&genericFilename] output_textfile_name=TileConfiguration.txt
fusion_method=[Linear Blending] regression_threshold=0.30
max/avg_displacement_threshold=2.50 absolute_displacement_threshold=3.50
compute_overlap computation_parameters=[Save memory (but be slower)]
image_output=[Fuse and display]");
                            saveAs("tiff", tempDir + "Stitched_image" + ".tif");
                            checkSelection();                                              //loop to provide option for stitching
another channel, or exit macro
                            }               
                            else {
                            exit("You have exited the macro")
                            }
                    }
}



Sorry for the convoluted message. I'm not sure why the long macro fails, and
its driving me insane. If somebody out there can help me out, I would be
really grateful!

Thanks!!

Owen




--
Sent from: http://imagej.1557.x6.nabble.com/

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


   

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