What are your policies regarding ownership of computer code in your lab? I work in a large US-based university, in biomedical sciences and am PI of my own research lab. One of my current interests is to develop a research line that uses machine/deep learning (ML) to solve certain challenges in my field. 

Because of the ongoing enthusiasm regarding ML, junior trainees and younger colleagues with background in computer science have approached me to collaborate. I certainly welcome this, because I am not a specialist in ML. They would bring to the table coding experience, energy and fresh approaches. Their status at my institution is of employees of the institution (not directly my employees). 

I have the (possibly wrong) impression that many people in their 20s-30s today grew up with heroes such as Zuckerberg, Jobs, and Larry Page, and want to succeed by inventing the next big thing and "selling it to Google". I very often catch some of these prospects inadvertently expressing a commercial motivation for endeavors like writing up code/software to solve some of the challenges in my field. I don't think this is necessarily wrong, but believe it is a somewhat misguided motivation, and should not be the primary driving force (IMHO, above all it should an unexplainable curiosity, urge for innovation and problem-solving).

My questions are:


how do you handle intellectual property / copyright in collaborations that involve developing code? Do you have different ways of dealing with this if it is with junior staff versus post-docs, grads, undergrads? 
Do you discuss code ownership issues before starting a project? Do you have recommendations on how this should be approached so as not to hamper enthusiasm?
have you had a situation in which your collaborator (regardless of rank) changed their mind from open-source to commercial (or patenting)? Do you obtain formal/written agreements at the outset to avoid problems down the road? (my concern again is how doing this could seem like discussing divorce before getting married)


My inclination is towards openness and sharing, and any subsequent benefit (even if commercial) is likely to derive from that. I have no problem in explicitly stating that to collaborators, but wanted to know what the community has experienced in this regard. In my current biomedical research, which has no realistic commercial application, I am very clear at the outset with collaborators (regardless of rank) regarding my standards for integrity and authorship roles.
I can only comment on your first question. Since you work for a large US-based institution I would be surprised if they do not have policies regarding IP generated by using university resources. That is, it may not be up to you to decide how ownership is handled. Many big universities have their own office to deal with technology licensing, patentable inventions, and so on. Since (in my limited understanding), this is a huge legal minefield, I would recommend contacting them first before you set any policies that you cannot keep. This is even more so if any external funding is involved, since funding agencies might also want a piece of the pie.