save a files with a same digits format in julia? - julia

I'm using Julia programming language.
I save each simulation result with a loop, and the parameter value is saved in a file name.
Is there any good way to unify the number of decimal places in file names?
For example,
using DelimitedFiles
for i=0:0.05:1
a = rand(1)
writedlm("i=$i.txt", a)
end
then I got
What I want is like below
Thanks for your comment

you can use Formatting.jl package. Here is an example (there are many more options; I show you one that I typically use when I want the file names to be nicely sorted):
julia> format.(0.0:0.5:10.0, precision=2, width=5, zeropadding=true)
21-element Vector{String}:
"00.00"
"00.50"
"01.00"
"01.50"
"02.00"
"02.50"
"03.00"
"03.50"
"04.00"
"04.50"
"05.00"
"05.50"
"06.00"
"06.50"
"07.00"
"07.50"
"08.00"
"08.50"
"09.00"
"09.50"
"10.00"

Related

Applying a System Call for ImageJ over a List in R

I am working with a large number of image files within several subdirectories of one parent folder.
I am attempting to run an ImageJ macro to batch-process the images (specifically, I am trying to stitch together a series of images taken on the microscope into single images). Unfortunately, I don't think I can't run this as an ImageJ Macro because the images were taken with varying grid sizes, ie some are 2x3, some are 3x3, some are 3x2, etc.
I've written an R script that is able to evaluate the image folders and determine the grid size, now I am trying to feed that information to my ImageJ macro to batch process the folder.
The issue I am running into seems like it should be easy to solve, but I haven't had any luck figuring it out: in R, I have a data.frame that I need to pass to the system command line-by-line with the columns concatenated into a single character string delimited by *'s.
Here's an example from the data.frame I have in R:
X xcoord ycoord input
1 4_10249_XY01_Fused_CH2 2 3 /XY01
2 4_10249_XY02_Fused_CH2 2 2 /XY02
3 4_10249_XY03_Fused_CH2 3 3 /XY03
4 4_10249_XY04_Fused_CH2 2 2 /XY04
5 4_10249_XY05_Fused_CH2 2 2 /XY05
6 4_10249_XY06_Fused_CH2 2 3 /XY06
Here's what each row needs to be transformed into so that ImageJ can understand it:
4_10249_XY01_Fused_CH2*2*3*/XY01
4_10249_XY02_Fused_CH2*2*2*/XY02
4_10249_XY03_Fused_CH2*3*3*/XY03
4_10249_XY04_Fused_CH2*2*2*/XY04
4_10249_XY05_Fused_CH2*2*2*/XY05
4_10249_XY06_Fused_CH2*2*3*/XY06
I tried achieving this with a for loop inside of a function that I thought would pass each row into the system command, but the macro only runs for the first line, none of the others.
macro <- function(i) {
for (row in 1:nrow(i)) {
df<-paste(i$X, i$xcoord, i$ycoord, i$input, sep='*')
}
system2('/Applications/Fiji.app/Contents/MacOS/ImageJ-macosx', args=c('-batch "/Users/All Stitched CH2.ijm"', df))
}
macro(table)
I think this is because the for loop is not maintaining the list-form of the data.frame. How do I concatenate the table by row and maintain the list-structure? I don't know if I'm asking the right question, but hopefully I'm close enough that someone here understands what I'm trying to do.
I appreciate any help or tips you can provide!
Turns out taking a break helps a lot!
I came back to this after lunch and came up with an easy solution (duh!)- I thought I would post it in case anyone comes along later with a similar issue.
I used stringr to combine my datatable by columns, then put them back into list form using as.list. Finally, for feeding the list into my macro, I edited the macro to only contain the system command and then used lapply to apply the macro to my list of inputs. Here is what my code looks like in the end:
library(stringr)
tablecombined<- str_c(table$X, table$xcoord, table$ycoord, table$input, sep = "*")
listylist<-as.list(tablecombined)
macro <- function(i) {
system2('/Applications/Fiji.app/Contents/MacOS/ImageJ-macosx', args=c('-batch "/Users/All Stitched CH2.ijm"', i))
}
runme<- lapply(listylist, macro)
Note: I am using the system2 command because it can take arguments, which is necessary for me to be able to feed it a series of images to iterate over. I started with the solution posted here: How can I call/execute an imageJ macro with R?
but needed additional flexibility for my specific situation. Hopefully someone may find this useful in the future when running ImageJ Macros from R!

Can I use the output of one DynamicResource after a map() for multiple solids?

I am doing something similar to the dynamic mapping and collect example in the documentation. The example lists files in a directory, maps each to a solid which computes the file size, and then collects the output for a summary of the overall size.
However, I would like to run multiple solids in parallel on each solid. So to continue with the example: I would list files in a directory; then map so that for each file I would compute the size, check the file permissions, and compute the md5sum all in parallel; and finally collect the output.
I can run these in sequence on each file with something like:
file_results = list_files()
.map(compute_size)
.map(check_permissions)
.map(compute_md5sum)
summarize(file_results.collect())
But if these aren't actually serial dependencies, it would be nice to parallelize the work on each file. Is there some syntax like this:
file_results = list_files().map(
compute_md5sum(check_permissions(compute_size)))
summarize(file_results.collect())
If I understand correctly, something like this should accomplish what you are looking for:
def _process_file(file):
size = compute_size(file)
perms = check_permissions(file)
hash = compute_md5sum(file)
return summarize_file(size, perms, hash)
file_results = list_files().map(_process_file)
summarize(file_results.collect)

I want to change "postive_" to "negative_" throughout my code depending on a variables value

I have written some code that imports .Rda files and then uses the data loaded from the files in the code. The .Rda files are from a machine that runs in positive and negative mode, and this is reflected in the file names. THey both have the same file name but one file name will have positive in it and the other will have negative in it. For example,
B_positive_mode_fat_names.Rdaand B_negative_mode_fat_names.Rda. The variables loaded from each one will be
B.positive_mode_fat_names and B.negative_mode_fat_names respectively.
I am trying to use a variable called device mode which i will put it equal to either positive or negative, and I want to code to change all the "positive_" to "negative_" if the device mode is negative, as i have written the code for negative. Is it possible to create an R script to go through the code and change all the "positive_" to "negative_".
device_mode <- negative
load("B_positive_mode_fat_names.Rda")
load("A_positive_mode_fat_names.Rda")
merged <- union(B.positive_mode_fat_names,A.positive_mode_fat_names)
For example with the above code, I would like the code to change the positive_ to negative_. The code in total is 600 lines long, and I would like to have one version instead of having a positive version and negative version of the code. Many thanks in advance,
There are two ways to go:
use eval(parse(text))). i.e:
device_mode <- "negative"
eval(parse(text=paste0("load('A_",device_mode,"_mode_fat_names.Rda')")))
eval(parse(text=paste0("load('B_",device_mode,"_mode_fat_names.Rda')")))
create a function along with the eval-parse-text
device_mode <- "negative"
myFun<-function(device_mode){
eval(parse(text=paste0("load('A_",device_mode,"_mode_fat_names.Rda')")))
eval(parse(text=paste0("load('B_",device_mode,"_mode_fat_names.Rda')")))
# do other stuff also
}
results<-myFun(device_mode)

Count number of rows Lua

I'm doing a project that involves file reading and I need to know the exact number of row in the file. Does anyone know how to count the number of rows in a file without having to read the whole file? I mean is there a built-in function for that in Lua? Thanks in advance.
Lua has built-in file lines iterator.
Very convenient.
Recommended for using. :-)
local ctr = 0
for _ in io.lines'filename.txt' do
ctr = ctr + 1
end
There is no built-in function for that. The only way is to read the whole file, as in Egor's answer for instance.

How can you print an Ada.Calendar.Time variable in Ada?

Need to know how to print a Time variable in Ada. I assume there is no portable way because Time is implementation defined. I've already seen the GNAT.Calendar.Formatting package available under GNAT, I'd also be interested in a GHS for VME.
See package "Ada.Calendar.Formatting" function "Image" for Ada2005. If you have an Ada95 compiler you could and this package isn't available, try my implementation from here
This was written using GNAT 3.15p, so pretty old.
Sure, time output can be portable, Ada.Calendar contains standard functions that extract the components of a time value, so it's straightforward to put together your own conversion package.
For example, here's one. One just needs to either create a minor addition to create a "Formatted_Time" record for a given Time value (see the package's Get_Time() function for guidance), or make Main_Formatter() visible in the package spec.
Generally what I do is use Calendar.Split and then do a 'image on the parts I care about.
Here's an example that displays the date and time using the GNAT.Calendar.Time_IO package:
with ada.calendar;
with gnat.calendar.time_io;
procedure display_time is
begin
gnat.calendar.time_io.put_time(ada.calendar.clock, "Date: %Y/%m/%d Time: %H:%M:%S");
end display_time;
Date/time format options are available here:
https://en.wikibooks.org/wiki/Ada_Programming/Libraries/GNAT.Calendar.Time_IO

Resources