2014-06-08

Running Linux on the Sony Vaio Flip 15A

The Vaio Flip 15A is a sleek and powerful laptop an attractive option for artists due to its large, high-resolution, stylus-enabled screen. I have installed Kubuntu 13.10 and 14.04 on this machine and will describe here what worked immediately, what required some effort to fix, and what still does not work.

Suggestions welcome!

What works:  Basically all hardware! 

  • Touch and stylus both work immediately, including pressure sensitivity  (try gimp, onboard, xournal)
  • Wireless, ethernet networking
  • Screen brightness controls and backlit keyboard
  • Both nVidia and Intel graphics (using nvidia-prime package)
  • 4-core I7 processor
  • Sound works fine, but the mixer controls are confusing (see below)

What can be fixed:

Difficult to use stylus when touch is enabled

Add a script to disable touch when the stylus is in proximity:
 
#!/usr/bin/python
import time, subprocess, os

def stylusProximity():
    """Return True if the stylus is in proximity"""
    state = subprocess.check_output(['/usr/bin/xinput', 'query-state', 'N-trig DuoSense Pen'])
    line = state.split('\n')[7]
    return line.endswith('=In')
  
def touchEnabled():
    """Return True if touch is enabled"""
    state = subprocess.check_output('xinput list-props "N-trig DuoSense" | grep 155', shell=True)
    return state.strip().endswith('1')

def disableTouch():
    subprocess.check_output(['/usr/bin/xinput', 'disable', 'N-trig DuoSense'])

def enableTouch():
    subprocess.check_output(['/usr/bin/xinput', 'enable', 'N-trig DuoSense'])

lastProxOut = None
lastProx = None

while True:
    prox = stylusProximity()
    if prox != lastProx:
        if prox:
            disableTouch()
            print "disable"
        else:
            lastProxOut = time.time()
    lastProx = prox
  
    if prox is False and lastProxOut is not None and time.time() > lastProxOut + 2.:
        lastProxOut = None
        enableTouch()
        print "enable"
    time.sleep(0.1)
   

Accidental touchpad clicks while typing

The touchpad is large, which means many accidental clicks while typing.
If using KDE:
System Settings -> Input Devices -> Touchpad -> Enable/Disable Touchpad -> Disable touchpad when typing (I use timeout = 2000 ms)

For other environments, you can use syndaemon to correct this:


syndaemon -R -t -k -i 2 &

However, I have found that syndaemon randomly stops working at times.




Fonts are too small by default

On KDE:
System Settings -> Application Appearance -> Fonts -> Force fonts DPI: 150

See: askubuntu.com/questions/197828/how-to-find-and-change-the-screen-dpi


Issues:

Dual boot is broken

I repartitioned the disk when installing Kubuntu, but Windows 8 fails to boot with the message"windows failed to start" 0xc000000e

Touch does not work in KDE

Although touch works via the "N-trig DuoSense" input device, KDE Plasma does not register touches as clicks. See: https://www.kubuntuforums.net/showthread.php?65296-Getting-N-trig-touch-screen-to-work-with-plasma-vaio-flip-15A&highlight=vaio+flip

Random display freezing

About every two hours, the display will completely freeze including the cursor. To fix, just do a quick VT switch each time it freezes (press ctrl-alt-f1 followed by ctrl-alt-f7).

If you have no sound

Check your mixer controls. You may need to unmute several channels simultaneously: Master, PCM, Headphone, and Speaker.



2012-05-17

Why sleep, anyway?

M.att writes to me:
"So seriously, why do I have to sleep? Can you fix that yet?"

I love neuroscience speculation. The fewer citations, the better. Why? Because early on, before we really understand, everything is so simple. We have only a few points of ambiguous or anecdotal data, and from there it is easy to draw a nice picture that explains the world and puts everything in perspective. It is effortless and satisfying; the candy of science. Nevermind that the real situation is always more complex and less elegant. Nevermind that this is all superficial, unsupported conjecture. Nevermind there's a good chance it's entirely incorrect. Just enjoy the candy with me.


As you go through your day, your brain accumulates short-term memories in the form of new synapses. The presence of these memories (synapses) causes your brain to become more active and use more energy. As memories build up, they require more and more energy and it becomes more and more difficult to store new short-term memories (see "synaptic homeostasis hypothesis", particularly this article). At the end of the day, your brain is more active, fizzling with new memories. This state is both exhausting and non-ideal for learning.

When you sleep, your brain re-activates all these short-term memories over and over in order to commit them to long-term storage. At the same time, it removes many of the synapses that were created during the day. When you wake up, you have a blank slate; you are ready to form new memories. Your brain is calmer, quieter, and uses much less energy.

This may partially explain dreaming --  your brain recalls bits and pieces of short-term memories and plays them out to long-term memory. But because the pieces are recalled in isolated chunks, out of order, in the wrong combinations, etc., your brain has room to 'read between the lines' and construct novel stories around them. (This kind of creative dreaming might also act as a simulator where we get to try out (and learn from) dangerous things without consequences).

It explains why, when learning a new kind of task, you will tend to dream more about that task at night. After you have learned the task, your brain understands that it no longer needs to spend so much energy memorizing it. It explains why small children need to sleep so frequently--literally everything they see is new and it is completely overwhelming.

So it seems likely this is how sleep works. Is there some requirement that it work this way? Ideally, your brain would immediately begin committing these memories to long-term storage so they could be cleared out of short-term, restoring your ability to generate new memories (and to a small extent, this probably does happen). This would keep more room and energy available for focusing on new tasks. Most importantly, you would never need to sleep.

The problem may be that in order to move short-term memories to long-term, you have to think about them.   And because the process of storing long term memories is so slow, you have to think about them for a long time. Most people can't be bothered to stop what they're doing every 5 minutes and memorize everything that just happened, so we store the memories up (at the expense of energy and brain function) and dump them all at the end of the day (when presumably, there's not much else to do since the sun is down).

Another problem may be that memories are difficult to isolate--the brain cannot automatically separate the short-term memories you are actively using from those you are ready to put away. Perhaps when you sleep, all short-term memories are stored and erased equally, so the time it makes the most sense to do this is when you are 'done for the day'. Of course, you can do it more frequently if you like; that's what naps are for. There are some species of songbirds that learn their song from their parents one play at a time--the parent sings the song, the child listens intently, then takes a 5-second nap.

So can it be fixed? Probably not without better hardware. There are a variety of drugs that claim to allow longer periods of wakefulness, but I would be cautious about these--there's a good chance they will have some effect on long term memory, so you get to stay awake at the cost of learning less (not to mention other harmful side effects).

On the other hand, perhaps most of the memories we store are totally useless anyway.

2011-09-01

An Unqualified Armchair-Neuroscience Rant


First, a disclaimer: What I am about to tell you is a load of crap. It is a steaming pile of unqualified, hand-waving generalizations. It is based partially on a lot of reading I have done, but I might venture that none of those authors were qualified to say these things either. It is also based largely on GUESSING. Also, it's too long to edit; I don't have the time. So that out of the way:

This post is about curiosity and creativity. I want to define both concepts in pseudoneuroscientific terms, with the specific aim of addressing what happens to our curiosity and creativity as we get older. Children seem to have an abundance of both, and adults often lament the loss of those qualities. Does pseudoneuroscience agree? [Spoiler: it's complicated.]

First, curiosity. But before that:

Here's how unsupervised learning works. Let's start way-way back at the beginning (REAL examples from a 3-day old). We all start with a very rudimentary model that attempts to explain everything we know about the world.
  1. Make observations.
     "I moved some muscles. Also, the stuff I see changed. Also, I pooped."
  2. Notice correlations in your observations (we are really good at this, often to our detriment)
     "OH, when I move THIS muscle, the stuff I see changes THIS way."
     "That blob I like to look at always moves the same as that other blob."
  3. Were you able to predict these correlations before they happened, or were you surprised by them?
        --> If NO, then can you update your model of EVERYTHING to include this new information?
             --> If YES, then update your model! You are smarter!
                  This process releases neurotransmitters that make you feel good.
                  (tangentially, this feelgood is also what makes stand-up comedy entertaining)
             --> If NO, then you are confused.
                  This process releases neurotransmitters that make you feel frustrated.
        --> If yes (you did predict what you saw), but you weren't really sure about that part of the model, then strengthen that part of the model. You are more confident!
             This process releases neurotransmitters that make you feel good.
        --> If yes, and you were not remotely surprised, then get bored.
  4. Repeat.

As we get older, our model improves, which makes it harder to find novel observations that challenge the model. It also becomes harder to figure out how to fit new observations into the model. But we keep trying, because it still feels good. Our model continues to improve. We also learn to direct our learning to answer specific questions.

Curiosity is our word for our innate desire to run this loop. It is part of a motivational system that rewards its host (you) for learning new information. It is why we look longer at things that are new to us, or that don't make any sense. It is also (at least in part) why children think everything is awesome.

We also learn about supervised learning, where observing other people who know stuff can quickly reveal new information. In school, we take this process to its most abstract, where people who know can simply tell you the things they know, and you can integrate this into your model (to some extent) without ever going through the 4-step process (although we learn much more strongly by going through the process). Supervised learning generally doesn't feel nearly as good, sometimes to the extent that students are bored out of their skulls and learn to hate learning. We keep learning anyway, because our curiosity demands it of us.

Eventually, however, the real questions become so incredibly complex and difficult to explain, that most people give up, at least to some extent. Rather than continue to feel frustrated, we find more comfortable shortcuts (for examples: Zeus is responsible for lightning. My favorite radio persona said it, so it's probably true. It's in the bible, so it must be true.) Or we just learn to stop caring. We lose our childhood curiosity.

We also become more _resistant_ to altering the model, even in light of contradictory evidence. This is extremely detrimental in some cases, but it is also a very important part of learning--if we were comfortable throwing out 20 years of experience based on one new experience, we would probably do that _too_ frequently. In many cases, forgetting things you already learned is much worse than learning something new.

We are said to learn much more quickly at a young age. Some of theis change is actually physiologically pre-programmed; certain parts of the brain 'solidify' at an early age to prevent further changes. Language acquisition is a good example of this (try learning a new accent; it's really hard now, but seemingly effortless at 3). (Tangential: I suspect solidifying language learning early on actually helps to stabilize language (which is an enormous learning investment) itself. If language changed too frequently, there would be fewer people who speak the same language as you, and it would become less valuable) We also learn less quickly because information is harder to get, and sometimes too costly to integrate into our world model. (This is not remotely the end of the story, of course; I would argue that the _quality_ of learning improves over time, even if the rate declines.)

Loss of childhood curiosity is both good and bad. It is USEFUL that we are no longer delighted to discover that the light switch makes the lamp turn on. If it were not so, we would not move on to more important questions. In this sense, being an adult means simply that we delight in discovering much more challenging or profound knowledge. That delight is the very same curiosity you had as a child, it's just grown up with you.

On the other hand, it is a shame (but somewhat understandable) that some people become disillusioned or disinterested in learning. So much for curiosity.


The life-cycle of creativity follows a similar developmental arc, but for a very different reason. I'll start with another algorithm:

How we solve problems:
  1. There's a problem! Identify your goal. What is the endpoint you wish to reach?
  2. Search your memory (at random, but with some hints). Does anything you remember
     have a vaguely similar result?
     --> If NO, keep searching! Broaden your search criteria and your definition of 'vaguely'
     --> If YES, then predict what happens if you try to solve the problem using what you remember.
           Do you have a complete working solution?
           --> NO! Why not? If the solution created a new problem, go back to 1.
                 If you think the solution is hopeless, go back to 2.
           --> YES! You are done. Good job. (it feels good)
       
This is, in a nutshell, what I call creativity. I'll throw in some caveats: creativity has many overlapping definitions, not all of which fit this description perfectly. Also, if it seems like I'm suggesting that creativity is simply repeating the things we already know, that is partially true (but read on). Also note that "solving a problem" can mean forming a sentence, or painting a picture, or solving the mysteries of the universe, etc.

Notice something very important about this process: it is recursive. Finding a partial solution can lead to several new problems, all of which need to be solved before the original problem is solved. These new sub-problems may have partial solutions as well, which introduce new sub-sub-problems (and so on). The object is to keep adding components onto this recursive tree of dependency-hell until, finally, you come across something that you think will work.

At your disposal is a vast library (your memory) of information from which you will construct the tree. Some of the components in the library are very simple (example: rocks are hard), and some are more complex (the transition from G major to B minor makes people sad). Often, constructing you solution from very high-level pieces is the easiest way to go, but at the extreme end of this, you are simply copying. Novelty lies in the particular arrangement of pieces you have chosen, so if you work harder to build a solution from smaller pieces, then the solution will be more novel (but not necessarily better).

At the other extreme, building your solution from the smallest possible pieces may be prohibitively difficult--your tree is too complex to juggle in your head and each sub-problem takes longer to solve because you are searching through a much larger library of tiny objects.

In general, the longer you search, the more (and hopefully better) solutions you can come up with. Being a good problem solver comes from 1) having a really good library, 2) being able to search quickly, and 3) being able to search efficiently--knowing where to look and, more importantly, where not to look. Knowing where not to look is tricky business. It's essential to being a good problem solver, but often invites us to overlook a less obvious solution.

People are called 'creative' for having strengths in various parts of this process. Some people are just better (or more persistent) at finding non-obvious solutions. Some people prefer to use smaller pieces and thus construct more elaborate solutions. Some people simply find any solution using the most non-obvious components (even if the solution is not good, it is certainly creative and novel).

Children are in an interesting situation here--they are forced to work with smaller libraries of simpler pieces and they have not yet developed the skill (or handicap, depending on your viewpoint) of ignoring useless pieces. Thus the solutions they do come up with are often not the ones we would find (or even consider), and sometimes serendipitously their solutions are clever as well as non-obvious. For all these reasons, we call children creative.

Like with curiosity, there is a developmental story here as well. Early in development, our brains are growing (literally) quite rapidly. Our neurons furiously form new branches and synapses, expanding their receptive fields in search of new correlations to represent. As time goes on, this process of growth slows and is eventually replaced by the opposite process--refinement of connectivity by removing those connections which get in the way or get little use. This, in the hand-wavingiest way, is exactly the process of expanding one's library, then learning to limit one's search paths.

Like with curiosity, creativity changes as we age in order to maximize our ability to learn and understand our world. These changes come with a price, though. Losing your childhood creativity means learning how to solve problems more quickly (or how to come up with better solutions in the same amount of time). It also means losing the ability to explore solutions broadly and free of bias. Of course you can practice creativity, but to some extent these changes are physiological and inevitable.













2010-12-26

Easy, Automated Reloading in Python

I love Python, but I have to admit it has a few weak points. One of the biggest for me has been the (in)ability to reload Python code at runtime. While there is a function (aptly named 'reload') which does just that, it is notoriously difficult to use correctly. The essential difficulty stems from the fact that when you reload a module, any references you had to objects in the old version still point to the old objects. This means that in addition to reloading the module, you generally have to re-import the module's symbols, re-initialize old class instances, etc. Compounding this difficulty is the fact that modules have to be reloaded in the correct order so that symbols are re-imported and propagated correctly. Several people have addressed this issue and explain it in greater detail:
Solutions to this problem usually involve hijacking Python's import mechanism to record module dependencies, and then making sure that modules are reloaded in the correct order. Others have suggested classes that track their instances and automatically update the __class__ attribute of each instance whenever the module is reloaded.

I haven't been very pleased with any of these options because they all require significant changes to my existing code to implement properly. In my search for the ultimate reload function, I eventually came up with a system that works very well for my application:

  1. Look through the entire set of imported modules and reload anything that has a new .py file available
  2. For every function and class method in the module, update the __code__ to point to the new function's __code__
  3. For every class, find all instances of the class and set __class__ to the new version. (it does not require extra work to track instances; just use gc.get_referrers to find them)
And that's it. In the end I have a single function called reloadAll which performs exactly the way you'd expect; all of your existing class instances start using the updated methods immediately, all modules point to the new objects, all functions update as well. Notably, it is not required to reload any modules in the correct order, nor is it required to re-import anything (with one exception, see below).

This works well with my existing applications, but there are a few situations where it won't work. If I make changes to the __init__ function of any class, or otherwise expect the internal state of an instance to be changed, I will have to re-create the instances manually (This is generally true of all languages I can think of; there is no general way to automatically re-initialize the state of the program). Additionally, any explicit references to objects in the module other than classes and functions (references to lists, for example) will still point to the old  objects. I can't think of a clever way around this, but it can be avoided easily: avoid using "from ... import ..." at all costs. If you are always forced to refer to the module when accessing its objects, then you automatically get the new versions when the module is reloaded.

Without further delay, the magic reloadAll function: http://luke.campagnola.me/code/downloads/reload.py

Leave a comment if you find a bug or have suggestions.