Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Naffer
Oct 26, 2004

Not a good chemist
Humble Request
Problem:
I have a scientific instrument that collects data on samples to individual folders. I would like to be able to go back and calculate how much instrument time was used on each sample.

Description and requirements:

The instrument runs on Windows 7. I think the easiest way to do this would be via the created date and time on the files that the instrument generates.
The file structure for the datasets is :
code:
\[User]\[samplename]\frames\[samplename]_#_###.img
Essentially, the instrument time for that sample would be the difference between the created date and time for the oldest and newest file that matches that file name format in the folder.
I imagine this wouldn’t be too tough to do with a script, but it’s beyond my abilities. What would be useful to me would be a script that gives an output in time in hours on a per sample basis.

Nice to have features:
I think a really nice output format would be something along the lines of:
code:
[User]	[samplename]	[Start date]	[Instrument Time]
If I could run it once and have it produce an output for all samples under all users that would be ideal.

Adbot
ADBOT LOVES YOU

Naffer
Oct 26, 2004

Not a good chemist

Ptarmigans posted:

Here's what I got with a quick go at it in Python. I'm not 100% on what your preferred formatting is but let me know if you want any changes.

If anyone wants to point out things that can be improved then I am open to critique, I'm not exactly experienced.

Request Fill
Source Code:

Thanks for the effort, I installed python 2.7.14 for windows, but trying to run the script gives a syntax error:
code:
with open("output.txt", "w") as out:
    print("Instrument time per sample", file = out)
Some googling told me to swap the "=" out for "==" both times file = out appeared.
I also swapped out getctime for getmtime, since apparently copying the data to a new drive (which I do occasionally) gives new created dates but not modified dates.

Now when I call the script (F:\test>python test.py) it writes this to the console:

code:
('Instrument time per sample', False)
('user1\tSample01\t[2016-12-21]\t01:39:27', False)
('user2\tSample02\t[2016-08-01]\t01:19:59', False)
It creates the output file but leaves it blank.
It also doesn't appear to check that the files whose dates its using for the calculation match the string "Samplename_"

Seriously, this is a pretty awesome start. Thanks.

Naffer
Oct 26, 2004

Not a good chemist

Ptarmigans posted:

Sorry about that, I should have specified that it's for Python 3, which doesn't have compatibility with Python 2.

Awesome. I added a couple of features.
1) now it only uses files that match the sample name for the calculation.
2) Added error handling for when it encounters subfolders that don't have the expected path (would throw up if there was a missing frames folder)

code:
import os
import time

with open("output.txt", "w") as out:
    print("Instrument time per sample", file = out)

dirs = next(os.walk("."))[1]
for user in dirs:
    samples = next(os.walk(user))[1]
    for sample in samples:
        path = user + "/" + sample + "/frames"
        if os.path.isdir(path):
            files = next(os.walk(path))[2]
            new = 0
            old = 0
            for file in files:
                fileTime = os.path.getmtime(path + "/" + file)
                if file.startswith(sample):
                    if old == 0:
                        old = fileTime
                    if fileTime < old:
                        old = fileTime
                    if fileTime > new:
                        new = fileTime
            date = time.strftime("%Y-%m-%d", time.localtime(old))
            diff = round(new - old)
            minute, second = divmod(diff, 60)
            hour, minute = divmod(minute, 60)
            with open("output.txt", "a") as out:      
                print("%s\t%s\t[%s]\t%02d:%02d:%02d" %
                    (user, sample, date, hour, minute, second), file = out)
        else:
            print("skipping " + user + "/" + sample)

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply