• (nodebb)
    img_list = [ "frist", "secnod", "thrid"]
    for index, name in enumerate(img_list):
        print(img_list[index])
        return
    
  • Davide (unregistered)

    I can understand the manual generation of a new filename. It's not uncommon that the filenames from big datasets (e.g. medical exports) are just random and we would like to sort them in a human-readable way. Of course any sane script would not care about filenames, but for development, debugging and archival reorganizing the files comes quite handy, not to mention other "scientific" software that might care about filenames more than metadata, and you get lung sections intermixed with kidney sections. Of course it shouldn't, but...

    The enumerate thing is a real WTF. Unfortunately I have the impression that CS is still taught in terms of C-like arrays and integer indexes, so that's what people end up doing in Python as well. See for an example the nice talk "Stop teaching C" from Kate Gregory at CppCon 2015, available on Youtube.

  • (nodebb)

    This line also annoys me:

    img_title 'img'+str(index).zfill(4)+'.jpg'
    

    Ignoring the missing = sign there (I assume that's just a typo), this code isn't otherwise wrong, but it's also not good. Older Python programmers might do this instead:

    img_title = 'img%04d.jpg' % index
    

    And more modern Python programmers might do this:

    img_title = f'img{index:04d}.jpg'
    
  • (nodebb)

    Filename generation depends on situation - sometimes you MUST discard the old fname and make something like that, whether because the old fname contains sensitive info, is random and hard to use, comes from a system with a different number offset, or whatever, so it's not a WTF.

  • (nodebb)

    From TFA:

    This means that the code frequently solves the problem in front of them, it often has issues.

    I think that you accidentally a word there :-P

    Addendum 2024-09-11 10:57: I agree with @Allexxann - in the absence of further info about the naming spec/requirements for the image files, the generation of the new filename might be situational and required.

  • (nodebb) in reply to The Beast in Black

    I suspect the missing word is "while":

    This means that while the code frequently solves the problem in front of them, it often has issues.

  • (nodebb) in reply to Steve_The_Cynic

    I suspect the missing word is "while"

    To quote Jules Winnfield: "Correctamundo!"

  • Fizzlecist (unregistered)

    Actually, this is some of the cleanest scientist-written code I've seen. If only I'd thought to save some of the examples instead of spending the subsequent trying to erase them from my memory...

  • Fizzlecist (unregistered) in reply to Allexxann

    Agree. Having worked with a number of scientists in the past there are often valid (if sometimes odd) reasons for renaming files, sometimes due to restrictions or oddities in the programs that consume the files later on

  • Alan Scrivener (unregistered)

    Frequently in my career doing presales tech support for scientific visualization software I encountered code created by a scientist & programmer pair. One time when the programmer was out of the room the scientist said “he’s an idiot — he doesn’t know anything about the science — he doesn’t understand what this code does.” Then when the other was gone I heard “he’s an idiot — he doesn’t know coding — he doesn’t understand what this code does.”

  • richarson (unregistered)

    I think @Allexxann and @The Beast in Black missed the point: they discard 'fname' and immediately load in 'data' with the same value 'fname' had ('img_list[index]').

    At least I believe that's what Remy was pointing at.

  • LZ79LRU (unregistered)

    This is why we need a guild, certification and licensing. That way if anyone ever wants to write even a line of code they would have to hire a certified software engineer. Not only would we avoid bad code but we would get full employment, high wages, quality control, full employment, collective bargaining, quality control, the power to rule the world and maybe even some quality.

  • (nodebb)

    serialized out as a NumPy array (e.g., just raw data)

    wow. this is the first time I have seen someone wrongly use "e.g." instead of "i.e."

  • (nodebb)

    One thing that stood out to me here is the line

    img = data[0][:,:]
    

    I'm going to assume, data[0] is an numpy.ndarray. The [:,:] indexing creates a new ndarray object, but it is backed by the same actual memory. So effectively the only difference between data[0] and data[0][:,:] is that the latter raises an exception, if data[0]isn't anndarray` with at least rank 2. (Curiously, it is valid for a rank 3 array.)

  • (nodebb) in reply to richarson

    Remy pointed at two different things - the wasteful (re)loading(?) and the renaming. I don't speak python, so I don't know how the loading part works, but me and @The Beast in Black are defending the second part.

  • (nodebb) in reply to richarson

    Exactly what @Allexxann said above: we're both referring to line 5 of the snippet in TFA, the one which sets img_title to be 'img'+str(index).zfill(4)+'.jpg' (albeit with all the other issues pointed out in TFA and herein), not lines 2 and 3.

    TFA says:

    Then we generate a filename based on the index- which, it so happens, we already HAVE a good filename that we've opted not to use (in fname).

    What @Allexxann and I are trying to say is that it may be that the original filename is unsuitable for whatever reason, and that the requirement may be that the final filename be the one generated in line 5 especially if the original filename is different; we have not been given sufficient data to determine that the original filename is indeed a "good" filename which can be used directly.

  • (nodebb)

    Taking out the truly redundant parts:

    for index, fname in enumerate(img_list):
        data = np.load(fname)
        img = data[0][:,:]
        cv2.imwrite(f'img{index:04d}.jpg', img)
    

    The weird slicing? Well, it's on a Numpy array so it might do something somewhat unexpected when it interacts with imwrite. I'd not be at all keen to alter that without some test cases set up first.

Leave a comment on “Enumerated Science”

Log In or post as a guest

Replying to comment #:

« Return to Article