andres marrugo

Group Images by Person

These are basically notes for my future self, but it may sort of work out for someone else, so here it is. I’m currently working on a project in which we obtain a large number of images all stored within a single folder. However, it would be ideal to store them on separate folders. The file names are like this export0_Lastname_FirstName_1.jpg, and there are several files per person.

What I basically wanted, was to organize the mess of files into something like the following:

├── 01-lastname_firstname
│   ├── export0_lastname_firstname.jpg
│   ├── export0_lastname_firstname_1.jpg
│   ├── export0_lastname_firstname_2.jpg
├── 02-lastname_firstname

So I made a Python script to deal with it. Link
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Import the necessary packages
import os
import re
import shutil

# Getting the list of people
personList = [f for f in os.listdir('.') if re.match(r'e.*\D\.jpg', f)]
personNames = [f.split("_") for f in personList]

# Getting the list of all jpg images
files = [f for f in os.listdir('.') if re.match(r'e.*\.jpg', f)]

# Getting existing subdirectories
# they start with ./01/
existing_dirs = next(os.walk('.'))[1]
N = len(existing_dirs)

# Iterate over all images to sort them per person
# and create a new folder if it does not already exist
for r in personNames:
    for f in files:
        if r[1] in f:
            NewDir = "{:2d}-{}/".format(N,r[1].replace (" ", "_"))
            if not os.path.exists(NewDir):
                os.mkdir(NewDir, 0o777)
                shutil.move(f, NewDir+f)
                print("The file does not exist.")

The personList line is done in such a way to get the unique number of people. This is done with the regex. Because there is a file that doesn’t end with a number, matching all files that begin with e, that don’t end with a number and .jpg, it serves as a list of the people for all the images in the directory. In the next line we build a list of the names of each person. This is later used to match and sort each file to its corresponding folder.

Because I had already created some directories, I could have the new directories start at 01. So I use os.walk to get the list and len(N) the number of existing directories. The directories to be created should start at N+1.

The outer-most loop iterates over every single person name, and then matches it the existing image files in the folder. When a match is found it creates a directory, if it does not exist. Then it moves file to the newly created folder. If the file doesn’t exist it prints the error, but it continues. And voilà that’s it. Just a quick tip. Make a copy of your files before starting to manipulate them (you could end with corrupt or no data at all). And also, before moving/renaming files, try printing the variables first to see if that’s what you intend to do.

Get Fridays

So, I was preparing my clases for the upcoming semester and I wanted to produce an outline of the lectures with the corresponding date and topic. For instance I have a class that’s once a week every Friday. I could look up the calendar and write every date, which is what I initially did. But then I though, I could write a script for this. And that’s when I entered the rabbit hole.1

The script I’m showing here went through several iterations. I had several difficulties until I got it to work. I wasn’t pleased with it, so I fiddled a bit more. Later when I saw Dr Drang’s post about formatting multimarkdown tables with tabulate, I knew I had to use that. So I did. Here’s the script.

Grading Lab Reports Workflow

Grades 2 CSV with Python.

Despite all the tools and applications at my disposal, I like to write my comments, feedback and grade the lab reports I get from my students using plain text files. To be precise, markdown formatted plain text files md.

Life Immutable

Life immutable.

We often live worrying about the future and longing for a less troublesome past; like when we were kids. I know that not everyone had a perfect childhood. I certainly did not, but I still had my moments in which my joy was true and worry free. Those moments are precious to me.

Adulthood has many upsides when viewed from a teenager’s perspective; the opposite is also true. If only they could talk to each other.

In Praise of Slow

I have a recurrent discussion with my wife.

She likes to do most things as fast as possible.

I like to pause and think about things, and do them often slowly. At my pace.

I think that if you do things too quickly you might not be fully aware of what you’re doing.

You might feel that you are not really in charge of your life.

You might feel that you can’t really remember things truly.

Like nothing becomes a memorable experience.

You have no time for metacognition and self-awareness.

I wrote these lines before finding out about this book In Praise of Slow by Carl Honoré. It seems this has inspired the slow-movement which tries to raise awareness of how we need to take it easy and enjoy life. Like the saying goes, anything worth doing is worth doing slowly.

It seems I have a new book to read.

On (Re)learning Things

My favorite topic in an introductory differential equations course is mechanical and electrical vibrations. I enjoyed learning about it as a student and I enjoyed teaching it later. (Or more accurately, I enjoyed being exposed to it as a student and really learning it later when I had to teach it.)

John D. Cook on Mechanical vibrations

I’ve always thought that education is about learning, as much as parenting is about maturing and growing up. On the parenting side, you can’t really teach your kids how to live – you can’t even imagine the kind of world they will have to live from the time they’re born – but you can expose them to the best things you know. In doing so, they can at least have the chance to form an opinion on the many matters of life, they’ll surely make mistakes, but I bet they won’t regret much.

Education is a difficult topic. Everyone has something to say about it, yet no one seems to have true answers. I do think that education is much more about being exposed to something, rather than being taught that something. There’s an incredible difference between the two things.

Date Commands

Dr. Drang recently shared two date commands he uses for: i) calculating how many days have passed since a given date, and ii) another that returns the day of the year of the current date.

I was mainly interested in the first one because I often have to do that calculation.1 Because the script can also compute the days remaining until a future date, you can imagine how handy it can be. For instance, if I’d like to know how many days until the day I start my vacations, I just run the script with a future date and it’ll return a negative result in days. Or if I’d like to know how old I am in days.

A CV for My Thesis

A Ph.D. thesis is one of those things that has a life of its own. Last January, I had the opportunity to write a two-page abstract of my thesis for publication in Electronic Letters on Computer Vision and Image Analysis. It has been published and is now available on the web along with the PhD thesis.

The paper is only a glimpse of all the hard work that went into the making of the thesis, but anyone who reads it may get a decent idea of what it is about, and where to find the results I’ve published in different journals. I don’t think anyone wants to go through the process of writing a thesis twice, but most will agree that it is a life changing experience. Many of the best moments of my life so far, happened while working towards the PhD, especially the birth of my two boys.

Not Collapsing the Wave Function

Today in “This American Life” I was listening to an episode about hidden truths. It was really interesting to find out about a secret letter that the prime minister of Britain writes to every commander of a nuclear submarine. It supposedly contains instructions on what to do if the nation has been wiped out and if the commander should retaliate or not. It poses a mystery on what the contents are, even more so because every time there’s a new prime minister in office the letters are destroyed unread.

There are many questions that come about in knowing what the contents of the letter are. But to my understanding it is a way of reassuring your people that both possibilities exist. To retaliate or not. It is like delaying the final call on a difficult decision. Something akin to a Schrödinger cat situation.

I’ve thought of this for while and I’ve come to understand that many people actually like to live their lives this way. Not wanting to know certain things, or more precisely clinging to the hope that a certain aspect in one’s life does not turn out to be what we fear so much. Keeping both options open (not collapsing the wave function) is probably a reassuring situation, relieving anxiety and allowing one to continue living unmoved by the cold hard reality.

The Perils of Talking Too Much

photo credit: mkorsakov via photopin cc

All too often I see people who simply talk too much.1 Wether what they speak is nonsense or even meaningful things, there’s a limit to how much one can tolerate.

By talking too much – and too often – we miss out on many things. We don’t let others speak (which to my understanding is common courtesy), and worse of all we lose our capacity to empathize with others. We can’t read faces well while uttering words out of our mouths continuously. Loosing that feedback is what prompts us to speak without pause and with no care for what the people around us may feel. It’s what turns you into a prick. We’re just too focused on ourselves to care about others.