Programming improvements 2

A few things of the many things I learned last weeks in Python.

This is what I used to do to conditionally set settings:

class MyClass:
    def method(self, args**):
        <some code block>
        if some_arg == "condition":
            self.option = "this setting"
            <run some code>
            <call a method>
        else:
            self.option = "another setting"
            <run some other code>
            <call a different method>
        <some more code block>

myclass = MyClass()
myclass.method(args)

And it could be much more complicated.

Using an object oriented approach, can be so much nicer.

class MyBaseClass:
    def method(self, args**):
        <some code block>
        self._submethod(subargs**)
        <some more code block>
    @abstractmethod
    def _submethod():
        pass

The private submethod would then be defined in the child classes. First for “condition”

class MyClass_condiontion(MyBaseClass):
    def _submethod()::
        self.option = "this setting"
        <run some code>
        <call a method>     # can be in this or the parent class

Second for the else:

class MyClass_else_condiontion(MyBaseClass):
    def _submethod()::
        self.option = "another setting"
        <run some other code>
        <call a different method>     # can be in this or the parent class

To call the classes one could use the condition:

if some_arg == "condition":
    myclass = MyClass_condiontion()
else:
    myclass = MyClass_else_condiontion()
myclass.method(args)

With this approach the coding is so much neater. Each method knows what it has to do and there is no fuss about the settings.

While I still feel, it would have been nice, if I’d have learned these little things before (which comes from the thought I would be liked more, and from the expectation of perfectionism by me and because of that I also think others expect perfectionism from me – which is just not achievable), it is nice to pick new things up now. While I thought in my 20s that in many areas of life I can stop learning at some point, in the last years I learned that learning will never end in any part of life, and that is good! Because if I stop being willing to learn, I will go backwards.

Programming improvements

A few things of the many things I learned last week in Python.

My draft when a new optional dictionary was added to the method:

def method(self, arg1, options={}):

However, a better version was suggested to me:

def method(self, arg1, options=None):
options = (options or {})

And the other big learning through code review was, when constructing a string from a dictionary. My complicated code:

for key1, value1 in options.items():
    if key1 == "searchString":
        for key, value in value1.items():
            result_string = f"someString.{key}.{value}"

The suggested version is so much neater:

for key, value in options.get("searchString", {}).items():
    result_string += f"someString.{key}.{value}"

While I still feel, it would have been nice, if I’d have learned these little things before (which comes from the thought I would be liked more, and from the expectation of perfectionism by me and because of that I also think others expect perfectionism from me – which is just not achievable), it is nice to pick new things up now. While I thought in my 20s that in many areas of life I can stop learning at some point, in the last years I learned that learning will never end in any part of life, and that is good! Because if I stop being willing to learn, I will go backwards.

Experiment: Melting a dyed ice cube in salt water and fresh water – which one melts first?

In a podcast I listened to yesterday the experiment was discussed, using normal ice cubes. And they suggested to repeat the experiment with dyed ice cubes or dyed water. Yesterday I prepared the ice cubes and today did the experiment.

Some words about the preparation: Dissolving salt creates heat, hence I allowed for time so that the water could reach room temperature again. Therefore, the interesting bit only starts after a minute into the time lapse, or 20 minutes in real time.

The ice cubes start melting immediately, both distribute a bit of blue water to their glasses. However, the difference becomes visible very quickly: In the fresh water a constant stream of blue water flows to the bottom of the glass. In the salt water glass the blue melt water from the ice cube creates a layer on the top of the glass. The ice cube swims in this cold water, is isolated from the warm water, and hence survives about twice as long.

The reason of the different behaviour is related to density. A cold liquid has a higher density than a warm liquid, and hence sinks to the bottom (in the same way that a piece of metal has a higher density than water, or humans have a higher density than air). However, dissolving salt in water increases the density of that water, and the saltwater has a higher density than the cold water from the ice cube.

When the experiment was described, I guessed wrong. Using salt to melt ice on streets, I thought the ice cube in the salt water will melt quicker, due to a chemical reactions. However, as the salt is already dissolved, no heat can come from destroying the crystal structure (and that would be a physical reaction and not a chemical reaction anyway).

I didn’t want to show an hour long video on youtube, hence speed up certain parts of the video. For this I used the free ffmpeg tool on a Ubuntu 20 laptop. Running the code below took about 10 Minutes on my Laptop:

ffmpeg -i ../orig.mp4 -ss 00:00:00 -to 00:00:05 -c copy temp.mp4 && ffmpeg -i temp.mp4 -r 16 -filter:v "setpts=0.10*PTS" output_01.mp4 && rm temp.mp4
ffmpeg -i ../orig.mp4 -ss 00:00:05 -to 00:00:27 -c copy temp.mp4 && ffmpeg -i temp.mp4 -r 16 -filter:v "setpts=0.50*PTS" output_02.mp4 && rm temp.mp4
ffmpeg -i ../orig.mp4 -ss 00:00:27 -to 00:00:54 -c copy temp.mp4 && ffmpeg -i temp.mp4 -r 16 -filter:v "setpts=0.10*PTS" output_03.mp4 && rm temp.mp4
ffmpeg -i ../orig.mp4 -ss 00:00:54 -to 00:01:12 -c copy temp.mp4 && ffmpeg -i temp.mp4 -r 16 -filter:v "setpts=0.50*PTS" output_04.mp4 && rm temp.mp4
ffmpeg -i ../orig.mp4 -ss 00:01:12 -to 00:03:05 -c copy temp.mp4 && ffmpeg -i temp.mp4 -r 16 -filter:v "setpts=0.10*PTS" output_05.mp4 && rm temp.mp4
ffmpeg -i ../orig.mp4 -ss 00:03:05 -to 00:03:12 -c copy temp.mp4 && ffmpeg -i temp.mp4 -r 16 -filter:v "setpts=0.33*PTS" output_06.mp4 && rm temp.mp4
ffmpeg -i ../orig.mp4 -ss 00:03:12 -to 00:03:20 -c copy temp.mp4 && ffmpeg -i temp.mp4 -r 16 -filter:v "setpts=0.10*PTS" output_07.mp4 && rm temp.mp4
ffmpeg -i ../orig.mp4 -ss 00:03:20 -to 00:19:50 -c copy temp.mp4 && ffmpeg -i temp.mp4 -r 16 -filter:v "setpts=0.01*PTS" output_08.mp4 && rm temp.mp4
ffmpeg -i ../orig.mp4 -ss 00:19:50 -to 00:20:10 -c copy temp.mp4 && ffmpeg -i temp.mp4 -r 16 -filter:v "setpts=0.10*PTS" output_09.mp4 && rm temp.mp4
ffmpeg -i ../orig.mp4 -ss 00:20:10 -to 00:20:40 -c copy output_80.mp4
ffmpeg -i ../orig.mp4 -ss 00:20:40 -to 00:21:00 -c copy temp.mp4 && ffmpeg -i temp.mp4 -r 16 -filter:v "setpts=0.15*PTS" output_81.mp4 && rm temp.mp4
ffmpeg -i ../orig.mp4 -ss 00:21:00 -to 00:27:11 -c copy temp.mp4 && ffmpeg -i temp.mp4 -r 16 -filter:v "setpts=0.05*PTS" output_82.mp4 && rm temp.mp4
ffmpeg -i ../orig_02.mp4 -ss 00:00:00 -to 00:07:20 -c copy temp.mp4 && ffmpeg -i temp.mp4 -r 16 -filter:v "setpts=0.05*PTS" output_83.mp4 && rm temp.mp4
ffmpeg -i ../orig_02.mp4 -ss 00:07:20 -to 00:07:27 -c copy temp.mp4 && ffmpeg -i temp.mp4 -r 16 -filter:v "setpts=0.15*PTS" output_84.mp4 && rm temp.mp4
ffmpeg -i ../orig_02.mp4 -ss 00:07:27 -to 00:07:42 -c copy temp.mp4 && ffmpeg -i temp.mp4 -r 16 -filter:v "setpts=0.50*PTS" output_85.mp4 && rm temp.mp4
ffmpeg -i ../orig_02.mp4 -ss 00:07:42 -to 00:07:50 -c copy temp.mp4 && ffmpeg -i temp.mp4 -r 16 -filter:v "setpts=0.15*PTS" output_86.mp4 && rm temp.mp4
ffmpeg -i ../orig_02.mp4 -ss 00:07:50 -to 00:27:11 -c copy temp.mp4 && ffmpeg -i temp.mp4 -r 16 -filter:v "setpts=0.05*PTS" output_87.mp4 && rm temp.mp4
ffmpeg -i ../orig_03.mp4 -ss 00:00:00 -to 00:05:11 -c copy temp.mp4 && ffmpeg -i temp.mp4 -r 16 -filter:v "setpts=0.05*PTS" output_88.mp4 && rm temp.mp4
ffmpeg -i ../orig_03.mp4 -ss 00:05:11 -to 00:05:33 -c copy temp.mp4 && ffmpeg -i temp.mp4 -r 16 -filter:v "setpts=0.20*PTS" output_89.mp4 && rm temp.mp4

for f in $(ls output*.mp4); do
ffmpeg -i $f -c copy -bsf:v h264_mp4toannexb -f mpegts $f.ts
done
CONCAT=$(echo $(ls *.ts) | sed -e "s/ /|/g")
ffmpeg -i "concat:$CONCAT" -c copy -bsf:a aac_adtstoasc VID_20220224_094623206.mp4
rm *.ts

How to code, an additional note

(by Ronny Errmann)

My last post could be read quite negatively, in the way that I didn’t make any progress. So I wanted to add some notes to counteract that feeling. When I looked into new methods actively, because I had a problem, I learned a lot. And I easily remember a few bits from the last years:

  • Replacing loops and conditions with numpy operations on the whole array or part of the array. This made execution so much faster. For example using
array[array > 0] = numpy.average(another_array, axis=1)
  • Making my program compatible to Windows. It’s just a few things to keep Python code compatible to different operating systems.
  • Adding multiprocessing to the program.

All of these helped me to learn new concepts and improve.

How to code, to my younger me

(by Ronny Errmann)

Every time I prepared for interviews or when I started a new role, I learned so many new concepts. Unfortunately, once working in a scientific role, I found it harder to keep this learning up. Making the time to learn about concepts and to talk about code was quite low on my priority list. While reviewing code is something, which I still think is difficult, learning new concepts is something that can easily done. For example, freeing up one hour once or twice a week can make a big difference. Recently I look into C++ methods and just by looking through a few pages of a tutorial (e.g. about classes), I feel I increased my knowledge by a large amount. So there is really no reason to use that as an excuse.

Discussion about code is a bit more difficult. Being the sole programmer in a team doesn’t make things easier, discussion of code means I take time from a different project. Furthermore, in a scientific environment, most people were also self-taught and, like me, only learned the necessary information and were goal driven. But of course, it is important to discuss code and the few experiences, that I have had in the last months, together with the explanation how companies practise these discussions, have changed my view a lot. It is worth the effort, and try to find a group to discuss things can be really beneficial. So similarly to my last post, I would answer to the question, if I would do things differently now with a clear yes.

A beginners code

(by Ronny Errmann)

As mentioned in my last post, I started coding without asking for advice. Which meant I would do things inefficiently. For example to calculate the standard deviation I used the following code:

mittel=sum(l for l in temp)/len(temp)
stdev=math.sqrt(sum((l-mittel)**2 for l in temp)/(len(temp)-1))

Later I learned about numpy, which made things a bit easier:

stdev = numpy.std(temp, ddof=1)

This is also quite a bit more efficient in computing time as it is run in compiled code and not has the overhead of an interpreter language.

Looking back I can smile about the code. In an interview I was once asked about that program if I would do things differently now. To which my answer was a clear yes.

How I came to writing code

(by Ronny Errmann)

The first device I wrote my first programs was the CASIO CFX-9850G, a graphical calculator. We needed it for school (I think 8th grade) and to get it for a lower price, were suggested to buy it even half a year earlier. That was in the mid 90s and, out of interest and out of boredom, I read the manual to see what one can do with it. And by talking to friends, being showed programs from others, I started to understand other people’s code and then tried to solve smaller questions. With time the programs got bigger. In the end I programmed a battleship game and 4 wins, ready to be played in boring lessons against the person sitting next to you. While the programs are long gone (maybe the hand-copied code is still in a folder at my parents place), the calculator is still a part of my desk, although, primarily just as a calculator.

A few years later (or at the same time?) we learned Delphi in School. It was only one year with one or two hours a week, so it couldn’t go into details, however, I know I tried to improve the programs and write more complex ones after school or at home. The GUI made some parts easy, however, looking back it also prevented me from understanding that there is a big benefit for writing back-end code.

After School my interest in coding decreased, mostly because I thought I wasn’t good enough or didn’t see the application. And interest in learning background theory was limited at that age. When I started my Physics studies live was also busy enough. To analyse the laboratory experiments I would work with Excel/OpenOffice Calc and Origin. The built-in functions were enough to solve everything, not elegant, but efficient (in terms of my time).

In my masters project I found the limitations of OpenOffice Calc. To do calculations I just needed so many columns that I would crash it on a regular basis. However, it took until I had to do the first analysis of observational data, that I noticed I need to learn programming. Other people at the institute suggested Python as a start. And so my first basic program was just a python script that would do the steps I would do by hand by itself. It was very procedural, and when I look back, quite funny in how I wrote it. I definitely didn’t search for advice. Luckily this has changed over the years.