Musings of a dad with too much time on his hands and not enough to do. Wait. Reverse that.

Month: July 2018 (Page 2 of 3)

Rick Rolling with matplotlib

I know this meme is 100 Internet years old, but when I read this awesome post from Little Miss Data, I knew what I had to do:

Ok, ok.  So there are a few problems with the GIF:

  1. Rick’s head isn’t exactly centered and doesn’t pivot in a fluid way
  2. It would probably be nicer if the labels were over their respective wedges.

In Little Miss Data’s post, she uses R code to first generate her charts, then save them to PNG files, then load those PNG files as background images to pre-created, animated GIFs.  Unfortunately, I could find no way in Python to replicate that behavior.  So, I cheated a little: in a loop, I created my chart, dropped the Rickster on top of it, and saved the image to disk.  With every loop iteration, I slightly rotated Rick.  Outside of my loop, I used the imageio package to load all the saved images into one animated GIF.  Here’s a look at my code:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
data = [['Give you up', 30],
        ['Let you down', 20],
        ['Run around \nand dessert you', 20],
        ['Make you cry', 15],
        ['Say goodbye', 10],
        ['Tell a lie \nand \nhurt you', 5]]

headers = ['thing', 'percentage']

df = pd.DataFrame(data, columns=headers)

[os.remove(f) for f in glob.glob('fig_*.png')]  # remove any images from previous runs
pngs = []

for i in np.arange(0, 10):
    fig, ax = plt.subplots()
    img = plt.imread('rr2.gif')
    img_w, img_h, x = img.shape
    df.plot.pie(y='percentage', labels=df.thing, figsize=(9, 9), legend=False,
                title='Things Rick Astley would never do', ax=ax)
    ax.set_ylabel('')
    fig_w, fig_h = fig.get_size_inches()*fig.dpi
    rr = ndimage.rotate(img, i*36)
    _ = ax.figure.figimage(rr, fig_w/2 - img_w/2, fig_h/2 - img_h/2, zorder=1)
    fig.savefig('fig_{0}.png'.format(i))
    pngs.append('fig_{0}.png'.format(i))
   
images = []
for png in pngs:
    img = imread(png)
    images.append(img)
mimsave('rr_final.gif', images)

The full code is available on my Github page.

As for fixing the problems I identified earlier, I’m sure that can be done with some smarter calculations in the figimage line and the labels should be able to be adjusted through appropriate calls to the ax object, but I’ll leave that to someone with more time on his hands…or not, as this is probably a dumb thing to spend your time on.

Anyway, there you go: rick rolling the matplotlib way!

Comparing files for backup

For particular reasons I won’t go into here, when it comes to backing up family photos–photos taken by my family’s digital cameras and various phones–I try to pull all those files together periodically and back them up under one common folder on my NAS, organized in year and month sub-directories.  The process works out well for the most part but I occasionally have to deal with the problem of duplicate files–files I’ve already backed up once but weren’t removed from the device in question–or, worse, files sharing the same name but are entirely different pictures.

Compare-Object

One way to identify these problems is with PowerShell’s Compare-Object cmdlet.  Imagine this scenario: I have a folder of pictures I downloaded from one of my kids’ phones.  That folder contains pictures taken in both June and July but I only want to determine if there are any new July pictures that I need to backup.  I can run the following at the PowerShell command prompt:


1
2
3
4
PS > $backed_up_files = gci "C:\my_nas_backup\Pictures\2018\July"
PS > $files_to_be_backed_up = gci "J:\104APPLE" | ? {$_.LastWriteTime -ge "2018-07-01" -and $_.LastWriteTime -lt "2018-08-01"}
PS > $c = compare -ReferenceObject $files_to_be_backed_up -DifferenceObject $backed_up_files
PS > $c | ? {$_.SideIndicator -eq "<="}

This produces a list of files that are not presently backed up, but I have two problems with this approach:

  1. The Compare-Object cmdlet can be slow, often taking several seconds to run, particularly if you have a few thousand photos to examine and
  2. More importantly, if you have photos that share the same filename yet are completely different photos, Compare-Object doesn’t seem to be smart enough to see the difference and point this out.

Enter Robocopy, FTW

There are lots of hidden gems in Windows, not the least of which is Robust File Copy (robocopy for short).  Robocopy is a fast and clever command line tool for copying folders and files and a great solution for some of your more challenging backup needs.  Leveraging some help from this post, I constructed the following robocopy command to see what July photos still needed backing up:


1
C:\>robocopy "J:\104APPLE" "C:\my_nas_backup\Pictures\2018\July" /l /ns /ndl /njs /njh /xx /minage:20180801 /maxage:20180701 /log:rc_july.log

Lot of command line arguments there!  Here are their explanations:

  • l – List only – don’t copy (this will just list out the files robocopy would normally want to copy over)
  • ns – don’t log file sizes
  • ndl – don’t log directory names (I’m only looking at files, not directories, anyway)
  • njs – No Job Summary (trying to keep my log file trim)
  • njh – No Job Header
  • xx – eXclude eXtra files and directories; “extra” files seem to be those files already in my backup folder.  For this particular problem, I only want to know about files I’ve yet to back up.
  • minage – exclude files newer than n days/date
  • maxage – exclude files older than n days/date (I only want photos taken in July, so I need to set these values accordingly)
  • log – output status to LOG file (the results of the comparison will be written to this file)

Robocopy applies different labels to the files it examines.  In my case, it applied the “extra” label to photos already in my backup directory.  Since I’m not concerned about these, I used the “xx” argument to suppress that information.  Next, it applied the label “new” to files in the phone directory but not in the backup directory.  Those are photos I definitely need to add to the backup.  Finally, it applied the label “newer” to photos in both directories that share the same file names but are completely different photos.  Sweet!

All this and robocopy copy ran in sub-seconds.  I think this will be my go-to tool going forward when comparing photos for backup.

The struggle to staying fit

Across my gym’s parking lot sits a great American icon: McDonalds. Twenty feet to the West is Burger King. Every time I leave the gym after a hard workout, I look across the parking lot and see the drive-throughs of both establishments stacked four-to-six cars deep. What irony.

My family has busy schedules and lots of needs, financially and otherwise. To be as supportive as possible, I know I must be as healthy as possible. Gym, yes. Fast food, no. Now, I’m no Jim Jupiter and I’ve had my share of health concerns, but I try to place a high priority on staying healthy. Here are some of the ways I try to maintain and improve my health:

1. Weightlift

The book, The Abs Diet, places a high premium on weightlifting to promote fat burning and overall health:

“It turns out that while lifters didn’t burn as many calories during their workouts as the folks who ran or biked, they burned far more calories over the course of the next several hours. This phenomenon is known as the afterburn–the additional calories your body burns off in the hours and days after a workout. When researchers looked at the metabolic increases after exercise, they found that the increased metabolic effect of aerobics lasted only 30 to 60 minutes. The effects of weight training lasted as long as 48 hours (p46).”

With our schedule, I’m lucky to weightlift about 3-4 times per week. I try to practice a four-day split so I can give each body part appropriate attention and break my workouts down into “warm up” sets (3-5 sets of 20 repetitions each of a lower, warm-up weight) and “working” sets (3-5 sets of 4-8 repetitions of a much higher weight).

2. Run

Aerobic work is still important–your heart is a muscle, after all–so I try to run or use one of the elliptical machines at the gym as much as possible. I do wuss-out a bit if the temperature is under 40 degrees, but otherwise I try to get 2-3 three-mile runs in per week.

3. Eat right

In a recent interview, renown physicist Michio Kaku discussed his interest in the paleo diet. While I think that diet makes a lot of sense, I haven’t quite committed to it. Instead, I simply try to keep the carbs low and protein high.

4. Drink water

I have an app on my phone to remind me to periodically drink water. Admittedly, I tend to turn it off after a few annoying reminders, but one part of my diet I’ve been fairly successful at is nearly eliminating drinking sodas. For me, it’s water, tea, and coffee.

5. Garden

The fitness industry loves to talk about developing a healthy diet, but I rarely hear them discuss where the foods that compose a healthy diet come from. What sort of pesticides were sprayed on those foods as they were grown? What sort of preservatives coat those foods to give them longer shelf life in the grocery store? Why does no one in the fitness industry talk about growing your own healthy food–at least a fraction of it, anyway?

Cultivating a garden is a great way to grow foods you like and know for a fact what pesticides you apply, or withhold, from those foods. Every year, I plant a small raised bed and several containers with tomatoes, peppers, lettuce, strawberries, and other plants. I also grow raspberries, blackberries, pears, and even paw-paws on my small suburban lot.

With gardening, not only can I grow the food I like and keep it chemical free, but I find it a bit of a stress relief, as well.

6. Mow my own grass, take the dog for a walk, and other sundries

Mowing the lawn can be grueling in the hot summer sun, but I consider it a small workout. It also helps me keep in touch with my property, noting if that side hedge needs trimming or if there’s some trash under one of my pine trees. Also, it affords me another opportunity to listen to my podcasts!

Another necessary choir is taking the family dog for a walk. I struggle getting the kids to take on this responsibility, so I often find myself and my four-legged friend out for a stroll, but I count that as a small workout, too, and I know he appreciates getting out of the house a little.

7. Martial Arts/Boxing

In my younger days, I participated extensively in martial arts and boxing–great forms of exercise that provide the added benefit of teaching self defense. These days, I’ve had to back away from that regimen, but I still do occasional bag and shadow boxing work and will hopefully get to someday pass on some of my knowledge to my children.

8. Train my brain

Staying healthy isn’t entirely about the physical body. I love to learn and always try to learn something new everyday. Podcasts are great in this effort. Also, I often attend both free and pay training sessions such as those at EdX and DataCamp.

9. Sleep?

Experts seem to suggest 7-9 hours of sleep for old dads like me. Unfortunately, I’m more in the 5-6 hour camp. So, this is definitely one point of fitness I need to improve upon.

10. Stretch

I tore my calf a few years ago playing soccer with my boy. The recovery was long and painful and directly impacted our schedules. Since then, I’ve tried to be more thoughtful about stretching, but certainly need to do a better job working this into my daily routine.

11. Meditate

A lot of people I listen to are big advocates of meditation. Meditation was a part of my martial arts experience, but I could never get the hang of it. I really want to give the practice another go, I’m just struggling to figure out a proper time and place amongst the hustle and bustle that is my home life.

Whatever you do, try to do some form of physical activity every day. If you’re like me, a lot of people are depending on you to be at top performance now and years to come!

« Older posts Newer posts »

© 2024 DadOverflow.com

Theme by Anders NorenUp ↑