LEARN #3 – Week 117 of PhD

Almost a week late, the third issue of LEARN is out for your own enjoyment! In my defense, this week I have also been challenging myself with #Inktober. If you are into these kind of stuff, you can also check my tumblr posts!

l3_mon

Lots, lots of boring coding these days! Not those funny simulations that would make my next paper (and first paper (if I ever get to that point)) look better, but lots of tedious scripts to help me analyse data. To spice things up, I decided to write the code slightly better and at least learn a bit more about Matlab. Now my functions look much more professional and elegant thanks to the inputParser Class, that I use to execute some quick check on the input parameters to the function, as well as to define optional parameters and their default value.

l3_tueGorillas can learn sign language and love cats too. One video of Koko the Gorilla sharing her ideas on global warming was shared from one of my FB  friend. Despite I think the video is a great opportunity to deliver a message in a very powerful way, I have my doubts that a gorilla can really grab the meaning of global warming. Nevertheless, scanning through the videos in Koko’s website, I could really get amazed not only by the amount of words in sign language Koko has learnt during the years, but also about her capacity to formulate small concepts (‘I am Koko, I am a Gorilla’, ‘ Koko loves babies’, etc… check her youtube channel for more), and even invent new words!

l3_wed

Wednesday afternoon I was so tired to code Matlab that I needed a little procrastination, so I coded short script in python. A friend asked for a way to shuffle her data to perform some blind test. Here it what I came out with:

# BLIND TEST FILE NAMING
# This script copies the files in the input folder, renaming them with a random number. 
# It then saves the correspondes to file, that it is sent to the email address provided

###### Import relevant libraries to modify the files

import os, random, shutil

import smtplib, imaplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encoders

import readline
readline.parse_and_bind("tab: complete")

from ConfigParser import SafeConfigParser
config = SafeConfigParser()
config.read('pyEmailConf.ini')

###### Start the shuffling!

# Input folder and output folder from user
INPATH = input('Input folder: ');
OUTPATH = input('Output folder: ');

# check if saving directory exists, otherwise it creates one
if not os.path.exists(OUTPATH):			
	os.makedirs(OUTPATH)

# create the file where to save the correspondence between orinal files names and new names
file = open(OUTPATH+"/blindtest_reference.txt","w")		

filenames = sorted(os.listdir(INPATH))			# ordered list of the file in the input dir
total=len(filenames)					
numbers=random.sample(range(total),total)		# generate not repeated sample of random numbers
extension = os.path.splitext(filenames[0])[1]		# extract the extension of the files

# Copy a new file into the output folder renaming it accordingly to the random number generator 
for i in range(total):
	text=filenames[i] + "\t" + str(numbers[i])
	file.write(text + "\n")	
	shutil.copy(INPATH + "/" +filenames[i],OUTPATH +"/"+str(numbers[i])+extension) 

file.close()

print("All Blinded! \n")



##### Sending the reference file by email

# Parsing the config file
fromaddr = config.get('userID','Address')
password = config.get('userID','Password')
servername = config.get('userID','ServerSend')
serverReadname = config.get('imapID','ServerRead')
sentmail = config.get('imapID','sentbox')

# Creating the message
toaddr = input('Send the reference file to: ')
subject = input('Subject: ')

msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = subject
body = input('Enter your message: ')

# Open, encrypt and attach the reference file
msg.attach(MIMEText(body, 'plain'))
filename = "blindtest_reference.txt"
attachment = open(OUTPATH+"/blindtest_reference.txt", "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(part)
 
# Sending the message and logging out
server = smtplib.SMTP(servername, 587)
server.starttls()
server.login(fromaddr, password)
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()

# Deleting the file from local folder
print('\n Email sent, deleting reference file...')
os.remove(OUTPATH+"/blindtest_reference.txt")


##### Connecting to email account with imap protocol to delete the email from sent messages
sRead = imaplib.IMAP4_SSL(serverReadname)
print "Deleting email in sent folder..."
sRead.login(fromaddr,password)
sRead.select(sentmail)
result, data = sRead.search(None, '(TO "%s" SUBJECT "%s")' % (toaddr, subject) )
data=data[0].split()
if len(data) == 0:
    print("No email found, closing connection.")
else:
    print("%d email(s) found" % len(data))
    for i in range(len(data)):
        sRead.store(data[i], '+FLAGS', '\\Deleted')
        sRead.expunge()

# logging out
print("Done!")
sRead.close()
sRead.logout()

print("The Truth is safe!")

In the version above, some files in the input folder (you will be asked to insert both the input and the output folder – Don’t forget the quotation marks!) are shuffled and saved in an output folder. A file with all the references will then be sent to an email address of your choice, provided that you have formatted a configuration file (the one provided works for gmail, let me know if you can make it work with other servers!) with your email address and password. For the email implementation, I followed this post; a very important step, if you are using gmail, is to allow Google to trust this “less secure app”. Also, I the use the imap protocol to then check the sent mailbox and delete the sent message. There must be a better way to do it, using just one lib, but I am lazy, and this script was well enough!

Alternatively you can decide to go for a simplified version of the code, that instead to email the reference to someone trusted, will save it locally. Although, I am sure your supervisor/boss would be positively surprised by receiving an email sent through a python script, that you have diligently took care to implement in order to perform a very serious blind test! 😉

l3_thu

Quintessence, the re-brand of Aether, is actually a theory now. Although the best candidate to explain dark matter are still WIMPs particles, last week Nature’s Outlook on the subject mentions other two runner-ups: an elementary particle called axion, that would also solve the CP symmetry, and Quintessence, a scalar field that would replace the cosmological constant being attractive or repulsive instead of -guess what- constant!

l3_fri

Nanotechnology is a lot of fun! Actually is the reason I am not doing particle physics, as for my bachelor studies plans. Now, I can tell you what is more fun than nanotechnology: nanotechnology applied to biology! I could write you about how another one of my clever friends is going to save the world and cure cancer using her clever set of MIPs, Molecular Imprinted Polymers, but I would not give the topic justice. Please, check her twitter instead! 

On the other end, talking about nanotechnology on the same day, we discussed about a paper developing biomimetic MEMS inspired by biological cilia.  The clever design employs polymeric pillars connected by piezoelectric nanofibers encapsuled in a protective matrix (pictured) not only to measure very low flow velocity, but also to detect flow direction, which apparently was never achieved before, at least not in water.

Enjoy your weekend folks! And as always Learn Every day A Remarkable Notion!

One thought on “LEARN #3 – Week 117 of PhD

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s