I am building a script to import image sets. Each imageset is contained in a directory but the depth of the deepest directory is unknown. It could be 1, 2, 3, 4 or more.
Now I want to use the Symfony Finder component to get a list of the deepest directories. So I can filter out the images in that directory and group them.
Or maybe a better solution would be to filter out all images using:
$files = Finder::create()
->in($path)
->files()
->date('until 1 hour ago')
->name('/\.(' . implode('|', $this->extensions) . ')$/')
->exclude('.gitignore');
But now I have an array with all the images but I need to group them bij their deepest directory so that I have a multidimensional array with as key the deepest directory. For example:
[
'/path1/path2' => ['/path1/path2/img1', '/path1/path2/img2', ...],
'/path1/path3/path4' => ['/path1/path3/path4/img3', '/path1/path3/path4/img4', ...],
...
]
Related
i have the following requirements:
Given a source node
Find all nodes within a certain range (e.g. 4 hops)
and the destination nodes has a special label "x"
Restrict the label type of the nodes in the path to the destination
return only the shortest path length (e.g. if i find a node with 2 hops, dont find also nodes with 3 or 4 hops)
Return all nodes needed for showing the destination nodes and the path between
i managed to create a query but the performance is not so well. i assume it is because of the high amount of nodes with the label "x"
MATCH path = allShortestPaths((source)-[*..4]-(destination))
WHERE source.objectID IN ['001614914']
AND source:Y
AND destination:X
AND ALL(x IN nodes(path)[1..] WHERE any(l in labels(x) WHERE l in ['A', 'B', 'C']))
WITH path
LIMIT 1000
WITH COLLECT(path) AS paths, MIN(length(path)) AS minLength
WITH FILTER(p IN paths WHERE length(p)= minLength) AS pathList
LIMIT 25
UNWIND pathList as path
WITH [n in nodes(path)] as nodes
return nodes
Profile:
if i change the query to use not the shortest path function, this works well when the source has not a lot of outgoing Paths
MATCH path = ((source)-[*..4]-(destination))
WHERE source.objectID IN ['001614914']
AND source:Y
AND destination:X
AND ALL(x IN nodes(path)[1..] WHERE any(l in labels(x) WHERE l in ['A', 'B', 'C']))
WITH path
LIMIT 1000
WITH COLLECT(path) AS paths, MIN(length(path)) AS minLength
WITH FILTER(p IN paths WHERE length(p)= minLength) AS pathList
LIMIT 25
UNWIND pathList as path
WITH [n in nodes(path)] as nodes
return nodes
Profile:
But if i have a source node with many children this has also a bad performance...
in the moment i am thinking if i start with a simple search for all destinations and call a shortestPath on each found destination this might be better but i am not very sure.
e.g.
MATCH (source)-[*..4]-(destination)
WHERE source.objectID IN ['001614914']
AND source:Y
AND destination:X
WITH destination
LIMIT 100
call apoc (shortest path ...)
...
Or is there a better approach?
You may want to try APOC's path expander using 'NODE_GLOBAL' uniqueness, it typically performs better than a variable-length match. It also has a means for whitelisting nodes during traversal, but this does apply to the start node too, so we'd have to include :Y in the whitelist.
See if this works for you:
MATCH path = (source:Y)
WHERE source.objectID IN ['001614914']
CALL apoc.path.expandConfig(source, {labelFilter:'+A|B|C|Y', maxLevel:4, uniqueness:'NODE_GLOBAL'}) YIELD path
WITH path, last(nodes(path)) as destination
WHERE destination:X AND NONE(node in TAIL(nodes(path)) WHERE node:Y)
// all the rest is the same as your old query
WITH path
LIMIT 1000
WITH COLLECT(path) AS paths, MIN(length(path)) AS minLength
WITH FILTER(p IN paths WHERE length(p)= minLength) AS pathList
LIMIT 25
UNWIND pathList as path
RETURN NODES(path) as nodes
I am trying to generate thumbnails with Ruby,on a linux machine.
The process,includes determining which of the 5 thumbnails,already generated,is the most meaningful(by meaningful,here,i meant to pick the one with the highest size,since a bigger size means more details).
Afterwards i went to rename the file having the biggest size into a generic name in order to use it later.The code doesn't seem to be working for me,and i can't understand the reason,is there any suggestions to improve it?
Thank you in advance.
Here is my code:
For your possible needs, the variable thumb_dir contains the path of the directory we are getting the thumbnails,from.
max = File.size("#{thumb_dir}/thumb01.jpg").to_f #
name = "thumb01.jpg"
for i in 2..5
if max < File.size("#{thumb_dir}/thumb0'"#{i}"'.jpg" ).to_f?
max = File.size("#{thumb_dir}/thumb0'"{i}"'.jpg"
name = "thumb0" + "#{i}" + ".jpg"
end
end
File.rename("#{thumb_dir}/#{name}", "thumbnail.jpg") `
i = (1..5).map {|i| File.size("#{thumb_dir}/thumb#{i}.jpg").to_f }.each_with_index.max[1]
File.rename("#{thumb_dir}/thumb#{i + 1}.jpg", "thumbnail.jpg")
What does it do ?
(1..5).map {|i| File.size("#{thumb_dir}/thumb#{i}.jpg").to_f }
We get an array of file sizes for thumb1.jpg up to thumb5.jpg
array.each_with_index.max[1]
Used to get the index of the greatest value of the array.
File.rename("#{thumb_dir}/thumb#{i+1}.jpg", "thumbnail.jpg")
Now that we know that i is the index of the greatest value in the array, then thumb#{(i+1)}.jpg is the file with the greatest size, so that's the one we want to replace the name of.
Remember that in Ruby there's a number of useful methods in Enumerable that make this sort of thing pretty straightforward:
# Expand to a list of possible thumbnail paths
thumbnails = (2..5).map { |n| '%s/thumb%02d.jpg' % [ thumb_dir, n ] }
# Find the biggest thumbnail by...
biggest_thumbnail = thumbnails.select do |path|
# ...only dealing with those files that exist...
File.exist?(path)
end.max_by do |path|
# ...and looking for the one with the maximum size.
File.size(path)
end
That should return the largest file if one exists. If not you get nil.
You can use that to rename:
if (biggest_thumbnail)
File.rename(biggest_thumbnail, 'thumbnail.jpg')
end
You'll want to back up your original images before unleashing something like this on it that could potentially delete a bunch of files.
I want to write a simple program that recurses through a given directory, skips if one of a set of excluded directories / strings is found in the complete path&filename OR a file does not exist / is empty and lists the rest, along with file size and creation time.
My attempt looks like this, but it doesnt work:
dir_ignore=[".AppleDouble",".AppleDB",".AppleDesktop",".DS_Store"]
require 'find'
Find.find('/Volumes/Downloads') { |dir_entry|
next if dir_ignore.any? {|skip| dir_entry.include? skip} || !File.exist?(dir_entry)
dir = ["filename" => dir_entry],["filesize" => File.size(dir_entry)],["creationdate" => File.ctime(dir_entry)]
puts dir
}
The program still lists all the files in an ".AppleDouble" directory and finally crashes on a non-existant file. So obviously my boolean expression does not work...
Why?
Problem in generating file names
I have around 4000 .txt files each containing three columns of data. I want to read all the 3 columns from a single file one at a time and then plot three values which correspond to x,y,z values on a contour plot.
These files are created at various time step. So a plot from one file will be a level curve and plots from all of them will give me a contour plot.
But the problem I want to do something which I can do in bash like this:
for n in `seq -f "%09g" 30001 200 830001`; do
./someFile$n.whateverFileFormat
done
How can I do this in matlab so that if I have let's say:
t-000030001.txt
1 2 3
......
......
......
t-0000320001.txt
2 4 5
. . .
. . .
. . .
and so on to
t-0008300001.txt
3 5 6
. . .
. . .
and on it goes.
I want to load all these files one at a time store the values in a infx3 array plot them on a contour plot and do this again and again for all the files so that I can have all of them on a single plot.
P.S. I need to reproduce something equivalent to that bash script mentioned above so as to load files appropriately then only I will be read from them
One way to get the list of file names is this:
fnames = arrayfun(#(num)sprintf('t-%09g.txt', num), 30001:200:830001, 'Uniformoutput', 0);
Let's have a closer look: 30001:200:830001 generates an array, starting at 30001, incrementing by 200, ending at 830001. sprintf generates a formatted string, and arrayfun applies the anonymous function passed as its first argument to each element of the array in its second argument (the sequence). The output is a cell array containing the file names.
EDIT
The solution above is equivalent to the following code:
ind = 30001:200:830001;
fnames = cell(numel(ind), 1);
for i = 1:numel(ind)
fnames{i} = sprintf('t-%09g.txt',ind(i));
end
This stores all the values in the a cell array.
Writing #(num)sprintf('t-%09g.txt', num) creates an anonymous function. The looping happens in arrayfun.
I need to sort the images present inside some directory with the following order:
00a.jpg
00b.jpg
00c.jpg
...
00x.jpg
00y.jpg
00z.jpg
0aa.jpg
0bb.jpg
0cc.jpg
...
0xx.jpg
0yy.jpg
0zz.jpg
001.jpg
002.jpg
003.jpg
...
097.jpg
098.jpg
099.jpg
100.jpg
101.jpg
102.jpg
But I am not getting any logic to put inside my sort_by? Can anyone has any idea what logic would be best suited for sorting all images in the above mentioned order..
I am expecting something like this :
Dir.entries('.').sort_by { |x| ?? }
Thanks,
Dean
Your requested sort order is not apparent, so I'm going to assume that you want all the images which contain a letter to be before those with numbers only.
For this logic, you can return an array from sort_by, which be evaluated in order - firs item first, second one if the first is tied, etc.
In this example this would be something like:
jpgs.sort_by { |j| [j[/.*[a-z].*\.jpg/] ? 0 : 1, j] }
The first item in the array returned answers the question of whether the image name contains a letter before the extension, and if it does returns a smaller number than if it doesn't. This assures us that images with letters in their names will be before images with only numbers in their names.
Will result in this order:
[
"00a.jpg",
"00b.jpg",
"00c.jpg",
"00x.jpg",
"00y.jpg",
"00z.jpg",
"0aa.jpg",
"0bb.jpg",
"0cc.jpg",
"0xx.jpg",
"0yy.jpg",
"0zz.jpg",
...,
"001.jpg",
"002.jpg",
"003.jpg",
"097.jpg",
"098.jpg",
"099.jpg",
"100.jpg",
"101.jpg",
"102.jpg"
]
I would use:
Dir.entries('.').sort { |a,b| a.split('.').first <=> b.split('.').first }
I think it may be faster than regexp option. Also, its simplier and easier to customize (due using 2 iterators and comparator).