A collection of computer systems and programming tips that you may find useful.
 
Brought to you by Craic Computing LLC, a bioinformatics consulting company.

Thursday, April 25, 2019

Ruby serializable_hash removes ruby/object from YAML output

I use YAML as a convenient way to serialize data in a number of projects, most of which are written in Ruby and Rails

You generate the YAML representation with the .to_yaml method - really simple.

p = Paper.find(359)
puts p.to_yaml

But if the input is a ruby or rails object the output is prefixed with !ruby/object and that causes problems when you try and load that document in another script that does not know about this object

For example - here is an example of a Paper object from a Rails application.

--- !ruby/object:Paper
attributes:
  id: 359
  pmid: 7945531
  title: 'Pharmacokinetics of a new human monoclonal antibody against cytomegalovirus.
    Third communication: correspondence of the idiotype activity and virus neutralization
    activity of the new monoclonal antibody, regavirumab in rat serum and its pharmacokinetics'
[...]

If I try and read this file in a separate script I get this error because that script has no concept of a Paper object.

y = YAML.load_file('test.yml')
ArgumentError: undefined class/module Paper
from /Users/jones/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/psych-2.0.5/lib/psych/class_loader.rb:53:in `path2class'
from /Users/jones/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/psych-2.0.5/lib/psych/class_loader.rb:53:in `resolve'
from /Users/jones/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/psych-2.0.5/lib/psych/class_loader.rb:45:in `find'
[...]

The way to strip off the ruby/object 'header' is to use serializable_hash before to_yaml.

---
abstract: TI-23 consists of lyophilized regavirumab (monoclonal antibody C23, MCA
[...]
id: 359
pmid: 7945531
publication_date: 1994-07-01
title: 'Pharmacokinetics of a new human monoclonal antibody against cytomegalovirus.
  Third communication: correspondence of the idiotype activity and virus neutralization
  activity of the new monoclonal antibody, regavirumab in rat serum and its pharmacokinetics'

It looks like the keys in the yaml block are output in alphabetical order.

It's a simple fix but I had to hunt around to find it.







Thursday, March 14, 2019

Using Google BERT to Classify Biomedical Papers

I have been using Google's BERT language representation model to help classify a certain type of biomedical paper based on abstracts in the PubMed database.

The scripts that I use for data preparation and a detailed walk through of the process are written up in a new GitHub repository that I have created.

My work was based on a blog post by Javed Qadrud-Din that I found extremely helpful.

With my dataset I am getting around 91% accuracy - which is much better than my earlier experiments with LSTM, CNN, etc approaches.

Tuesday, November 13, 2018

Sharing a folder on a Synology NAS with MacOS and Ubuntu linux using CIFS / SMB

I have a Synology DS418 Network Attached Storage (NAS) system.

I wanted to create a Shared Folder on the NAS that I could access from Mac OS (Mojave 10.14) and a Linux box running Ubuntu 16.04.

MacOS uses SMB to share folders and so I decided to use that on the Linux box.

There are 3 parts to this process:

#1 Set up the folder on the NAS.

In Synology File Station, create the folder (SMBtest is the name used here). Under the Permissions Tab I gave my user (jones) full control by selecting 'Administration' and 'Change Permissions' and 'Take Ownership'

I think that is all that is needed on the NAS.

#2 on the Mac

Under the Finder -> 'Go' -> 'Connect to Server'
In my case the address to use was 'smb://craic-nas-1'
Connect as Registered User on the NAS and then select the volume 'SMBtest'

I then had access to that volume in the Finder or at /Volumes/SMBtest on the command line

#3 on the Linux box

Ubuntu Linux uses the CIFS "common internet file system" protocol to communicate with SMB servers.

I had to install the CIFS software

sudo apt-get install cifs-utils

and I created a credentials file with my NAS username and password.

emacs ~/.smbcredentials

username=jones
password=<mypassword>

chmod 600 ~/.smbcredentials

I created a mount point on the Linux box and made myself the owner

mkdir /mnt/
mkdir /mnt/jones
sudo chown jones.jones /mnt/jones

To mount that shared folder

sudo mount -t cifs -o credentials=~/.smbcredentials,uid=1000,gid=1000,iocharset=utf8 //craic-nas-1/SMBtest /mnt/jones/SMBtest

My UID on the linux box is 1000 - the uid and gid options means that the mounted directory is owned by me on the linux box - otherwise there are all sorts of permission issues.

The iocharset=utf8 is important. Without it, if I edited a file on Linux then there would be strange characters when I looked at it on the Mac side.

With that mount command I can create, edit and run scripts on either MacOS or Linux

To unmount the share

sudo umount /mnt/jones/SMBtest

To mount the share automatically I added this to /etc/fstab

//craic-nas-1/SMBtest /mnt/jones/SMBtest cifs credentials=/home/jones/.smbcredentials,uid=1000,iocharset=utf8 0 0

To test that works

sudo mount -a











Wednesday, January 3, 2018

Audio Cover on Gigabyte Aorus Z370 Gaming 7 Motherboard Blocks PCIEX16 Slot

I've just finished building a machine learning Linux machine based on the Gigabyte Aorus Z370 Gaming 7 Motherboard and a Gigabyte Aorus Geforce GTX 1080 Ti graphics card.

Most high end motherboards these days are aimed at gamers and so they include all sorts of RGB Led lights and assorted bling so that the users can install them in cases with a clear side. I don't care about all that.

The problem with this motherboard and card combination is that, as part of the bling, they have covered part of the board with a piece of plastic, marked as 'AMP-UP AUDIO', which has no actual function... and one end of that lies in front of the PCIEX16 slot where the graphics card needs to sit (see the arrow).


Here is the tab on the graphics card that is blocked. I don't know if other cards have this tab.



So with the plastic in place, you can't seat the card... and both components are from the same manufacturer... sigh

Fortunately, because the cover has no function whatsoever, you can just remove it and solve the problem. Flip over the motherboard and remove these two small screws. Throw the nasty piece of plastic in the recycling. Job done...


Thursday, September 29, 2016

pi_power - power management for portable Raspberry Pi projects

I have been working on a portable Raspberry Pi project for a while - still not there yet but I've completed one important piece.

It's portable so it obviously has a battery. The Pi takes quite a bit of power so it needed to be rechargeable and include a charger. Adafruit make a great device called the PowerBoost 1000C which takes care of the core charging and power supply functions. But I really wanted my project to work like my phone does:

  • To power it up from a cold state, press a button for a few seconds
  • To power it off, press the same button for a few seconds
  • Indicate how much power remains in the battery
  • Provide an alert when that is running really low
  • Shut down safely without any data corruption if the battery does run out
  • To recharge the battery, just plug in a cable from a USB charger

  • I couldn't find anything that provided all the functionality I wanted - so I built my own

    pi_power consists of fairly straightforward circuitry that links the PowerBoost to the Pi in conjunction with two python scripts that run in the background and monitor the battery voltage.

    If you are building a portable RasPi project then you might want to take a look


    Wednesday, June 8, 2016

    Image capture software for cheap USB microscopes

    Look on Amazon and you will find a variety of cheap USB microscopes. These are basically a USB video camera with a macro lens. They are good for inspecting fine details of things like electronic components, plants, insects, stamps, etc. but they are not powerful enough for looking at, say, cells in biological samples.

    I just bought one to help me capture images of various plant pathologies.

    They are marketed as having a range of magnification from, for example, 20 to 300X and the cameras have resolutions anywhere from 0.3 to 5 M pixels. Be aware that the specified magnification range may include the magnification inherent in displaying the image on a large monitor !

    Dino-Lite make a range of professional USB microscopes of various sorts but these can cost hundreds of dollars. For basic experimentation there are a load of other vendors and products with prices in the $50 - $100 range.

    If you look on Amazon it will be obvious that the same basic models are being sold be several different vendors.

    Here is the one that I bought for $60 which is branded  DBPOWER

    The same microscope with the same stand is also branded as :


    Celestron 5 MP Handheld Digital Microscope Pro  $83

    And with a different stand, or without a stand, by a wide variety of other vendors

    I went with the $60 DBPOWER variant with a 5 MP camera. It comes with the stand in two pieces and a CD containing software called MicroCapturePro for PC and Mac.

    You need software to interact with it - it does not just show up as a camera in the MacOS ImageCapture or Preview tools.

    I don't have a CD drive on the machine I want to use this with and transferring the software from one that does is a bit of a pain. So I looked online for the software.

    You can get MicroCapturePro from Celestron but after digging around a bit I would recommend a different solution.

    Plugable have a similar microscope and they provide a piece of software called Digital Viewer - get that and install it on your Mac (they have a Windows version too). When I started it up it displayed the view from my webcam but click the Settings icon in the top left and select the microscope.

    The focussing mechanism on these microscopes is a bit rough but you'll get used to it. I can see that I may want to build myself a better stand and illumination rig.

    Under Settings you can select the image resolution and do a bit of image adjustment. You can take individual images, videos or a set of timed images.

    It looks like it will do exactly what I need it to do - for $60 that's not bad.

    Here is an example image:





    Thursday, January 28, 2016

    Raspberry Pi - strange icon in top right of screen

    I have been working with a mobile Raspberry Pi project powered by a LiPo battery and a Powerboost 1000c charger from Adafruit to handle battery charging.

    Several times I have seen a small square icon in the top right of the attached small HDMI screen that has a rainbow of colors. I had absolutely no idea where this came from or what it represented.

    But now I do... https://www.raspberrypi.org/forums/viewtopic.php?t=82373

    The B+ (and presumably later models) has an under-voltage trigger that shows itself as this 'rainbow' square in the top right of the display when the supply voltage drops below 4.65V.

    In addition, an over-temperature trigger displays a solid red square in the same location if the temperature goes above 85deg C.

    You can disable the warnings in config.txt - see the link for details

    I'm glad that the system has these triggers but I don't think the display of a red/rainbow square is a good way to inform the user.




    Saturday, November 28, 2015

    How I fixed a loose MagSafe connector in a MacBook Air

    The MagSafe power connector kept falling out of my wife's MacBook Air. I figured it might be due to just wear and tear on the edge of the plug but when I looked at it I could not see any problem.

    Last week I took another look at the plug and socket while I was wearning my magnifying reading glasses - and there was the problem !

    The MagSafe socket is magnetic and so any metal dust that gets rubbed off from the plug will stick to the magentic surface. As a result you can see dark dust adhering to that. But in addition, there was a small metal shaving that was stuck there as well - large enough to interfere with the plug being properly seated. It was small but 'chunky' enough to cause the problem. No idea where it came from - not from the plug. I removed that with a pairs of tweezers and cleaned up the adhered dush with the end of a slightly damp Q-tip.

    Problem solved !


    Monday, October 27, 2014

    USPTO Patent data sets are now distributed by Reed Tech, not Google

    For the past few years Google has been making US Patent Office datasets available for free on the sites:

    http://www.google.com/googlebooks/uspto-patents-grants-biblio.html
    http://www.google.com/googlebooks/uspto-patents-applications-biblio.html
    and others.

    But the sites have not been updated since around Oct 7th 2014 and no information about the delay was posted... I rely on these updates so that was a big problem.

    After some poking around I stumbled on the fact that Reed Tech, a division of Lexis Nexis, is now distributing these datasets at no charge.

    Fortunately the format of the zip files remains identical, at least for the ones I work with.

    You can find the Patent Grant Bibliographic Text at http://patents.reedtech.com/pgrbbib.php and the Application Bibliographic Text at http://patents.reedtech.com/parbbib.php

    At the time of writing these pages are up to date.

    This is good news, but it would have been even better if Google had announced the transition a month or so in advance.




    Monday, September 22, 2014

    Trends in Human Antibody Development - Charts

    I operate the TABS Therapeutic Antibody Database which help biotechnology companies working in the field of antibody development. TABS represents the most comprehensive resource in this field.

    With all that data available, I have compiled summary statistics that show the growth of this area of biotech over the years. I have made charts of those trands and have made those freely available on the TABS database site. You can find all the charts HERE.

    Here is an example showing the number of active projects per year




    You can get a free 30 day trial account at TABS.


    TV Eye - a way to view YouTube videos without all the clutter

    When you view a video on youtube.com you get not only the video, but a bunch of suggested related videos and often times a lot of comments and other text. All this clutter gets in the way of what you want to do - simply watching the video.

    So I wrote TV Eye, a simple service that embeds your desired video in a simple, plain web page, with none of the usual clutter.

    To use, go to youtube.com and find the video that you want to watch. Copy the URL for the video and paste it into the form on TV Eye.

    The service is really simple and the code is distributed freely under the temrs of the MIT license. You can get the code at Github at https://github.com/craic/tv_eye.


    Friday, September 19, 2014

    Installing Ruby in Docker Images

    Docker is a great way to package applications with all their dependent libraries etc and then deploy them easily on various hosts. It builds on tools like Vagrant.

    I am interested in using it to package Ruby applications built with Sinatra or Rails.

    The preferred way to build a Docker Image is to write a Dockerfile that contains instructions that load an operating system, installs system packages, copies user code, etc.

    I am building my Images on top of Ubuntu 14.04, the current Ubuntu Linux release. A problem with most of the Linux distributions is that the packages that install Ruby are often one or two releases behind. In this case the packaged Ruby is 1.9.3 and the current release if 2.1.2. In many cases this would not be a problem but if you want the latest version then you have do a bit more work and, specifically, compile Ruby from source.

    Once you have the Dockerfile working then this all happens very smoothly but it took me a while to get all the pieces working together. So I wrote up two versions of a minimal Sinatra application along with the Dockerfiles needed to get them to work.

    The code is on Github at https://github.com/craic/docker_sinatra_examples

    The Docker Images are on DockerHub at
    https://registry.hub.docker.com/u/craic/docker_sinatra_example_1/
    and
    https://registry.hub.docker.com/u/craic/docker_sinatra_example_2/

    The first example installs the packaged Ruby (1.9.3)
    The second compiles and installs Ruby 2.1.2 from source.

    I hope these examples help you get up to speed with Docker quickly.

    Clean up unused Docker Containers and Images

    Docker is a great way to package applications with all their dependent libraries etc and then deploy them easily on various hosts. It builds on tools like Vagrant.

    In Docker you create Images that contain your code, the OS, libraries, etc and Containers which are the instances of the Image which you actually run.

    Docker does not do a good job cleaning up old Images and Containers and when you are doing a lot of developing this can become a problem. Various people have proposed ways to handle the issue. The best of the web posts that I have seen is http://blog.stefanxo.com/2014/02/clean-up-after-docker/

    Here is a slight restating of those solutions which work for me on MacOSX.

    To clean up non-running containers

    $ docker ps -a --no-trunc | grep 'Exit' | awk '{print $1}' | xargs docker rm


    This removes any containers where the Status contains the string Exit. I have seen containers with no status which perhaps have crashed or hung. You just have to remove these manually.

    Once you have removed unused Containers you can then remove unused Images. If you try this the other way round you will get errors that the Images are still in use.

    Unused Images look like this in the output of 'docker ps' - they are the ones with <none> as the repository and tag

    craic/sinatra_example            v1                  f9d2702eb2f7        2 days ago          481.3 MB
    <none>                           <none>              196afed3dded        2 days ago          378.5 MB
    <none>                           <none>              56f86f0a985e        2 days ago          378.5 MB

    <none>                           <none>              011f588e88db        2 days ago          586.8 MB

    To remove them use this command

    $ docker images -f dangling=true -q | xargs docker rmi

    If you are using Dockerfiles to build your Images (recommended) then you can always rebuild an image should anything get removed accidentally.



    Monday, March 10, 2014

    Private Wi-Fi Networks, Raspberry Pi and Mac OS X Mavericks

    I ran into two problems connecting to my Wi-Fi network recently - both turned out to be the result of the network being private and not broadcasting its SSID (Service Set Identifier) - the 'name' of the network.

    My network runs on an Apple Time Capsule using WPA2 encryption. It has worked fine with other machines - MacBook, Win PC, Chromebook, iPhones...


    #1 Getting W-Fi running on a Raspberry Pi

    I was not able to get Wi-Fi running on a Raspberry Pi linux machine, despite trying a range of configurations that I found on the web.

    When I changed the network to make its SSID public then it connected just fine with the basic configuration.

    #2 Setting up a new MacBook Pro

    When you start up a brand new Mac it walks you through several setup steps, including connecting to a network. The new MacBook Pro only has Wi-Fi so connecting to the network is essential. But in my case it did not see the private network and so I was unable to get through the regular setup.

    The work around was to skip those steps until I got to the regular desktop. Then going into Preferences and Network and configuring the network manually I was able to get in.

    I think that if the SSID were broadcast then I would not have had any problems.


    The reason to keep an SSID private is to make it more difficult for someone to break into your network. If they don't know the network exists then they won't try and break in. In reality, however, it not that simple. A serious hacker will still be able to detect packets on all networks in the area including the 'hidden' ones and then attempt to break in. So a private network is a good idea but it does not offer great protection.



    Monday, February 10, 2014

    Using Ping to find machines on your network

    The UNIX command ping is used to test if specific machines are active on a network

    $ ping 10.0.1.9
    PING 10.0.1.9 (10.0.1.9): 56 data bytes
    64 bytes from 10.0.1.9: icmp_seq=0 ttl=255 time=0.619 ms
    [...]

    I have been using ping for years but last week I found out that you can ping the broadcast address of a network (x.x.x.255) and see all the machines on that network that are configured to respond to ping:

    $ ping 10.0.1.255
    PING 10.0.1.255 (10.0.1.255): 56 data bytes
    64 bytes from 10.0.1.10: icmp_seq=0 ttl=64 time=0.071 ms
    64 bytes from 10.0.1.9: icmp_seq=0 ttl=255 time=0.534 ms
    64 bytes from 10.0.1.1: icmp_seq=0 ttl=255 time=0.544 ms
    [...]

    Really useful if you need the IP address for a machine that used DHCP to assign its address

    A complementary command is arp -a 

    This displays information of all the network interfaces for a machine and reports the interface, the IP address and the MAC address of each entry in the address resolution tables.

    $ arp -a
    ? (10.0.1.1) at b8:8d:12:5a:ad:77 on en0 ifscope [ethernet]
    ? (10.0.1.2) at b8:8d:12:5a:ad:77 on en0 ifscope [ethernet]
    ? (10.0.1.6) at 70:56:81:ad:b6:d1 on en0 ifscope [ethernet]
    ? (10.0.1.7) at b8:8d:12:5a:ad:77 on en0 ifscope [ethernet]
    ? (10.0.1.9) at 70:56:81:c5:fb:2d on en0 ifscope [ethernet]
    [...]

    Note that these addresses are not necessarily active. You can see link reachability information using arp -al


    Tuesday, December 24, 2013

    Web Site for Lakou Lapè - working to end violence in Haiti

    Lakou Lapè is a non-profit organization based in Port-au-Prince, Haiti, that is working to end violence and conflict in some of the impoverished communities in that country.

    Lakou Lapè was created to extend the achievements of the past few years in the Port-au-Prince neighborhood of St.Martin. Starting in 2004, members of that community, the charity Concern Worldwide, and the Irish group Glencree Center for Peace and Reconcilliation, came together to limit and reduce the level of gang violence within that community.

    By using the approaches of reconcilliation, dialogue and respect that helped bring peace to Northern Ireland, the community was able to work together and greatly reduce the violence that was holding the entire community back.

    Lakou Lapè is applying the same approach to other communities in Haiti. Through training, dialogue and engagement with the people that live in these communities, their goal is to reduce conflict and enable people to improve their lives.

    I have been working with the group over the past year as it has started its work and I am proud to have helped build their web site http://lakoulape.org/ which describes their plans and their acheivements in French, Haitian Keyol and English.

    It has been a pleasure to work with old friends from Ireland and new friends in Haiti and I wish Lakou Lapè the best of luck in 2014.

    Thursday, November 14, 2013

    Web Apprentice - Tutorials on Web Technologies

    The range of web technologies that are available to web designers and developers is just incredible right now - from maps and graphs to translation and speech recognition.

    Many of these are easy to incorporate into your sites but getting started can be confusing. There are examples and tutorials that are obsolete or needlessly complex - I've written some of them myself !

    I want to help people get up and running with simple examples that they can use in their work today.

    So I have created Web Apprentice (http://apprentice.craic.com) - a site that contains clear, straightforward tutorials on web technologies across the spectrum.

    Tutorials range from simple embedding of Twitter feeds or Maps through to Typography, Translation and Geolocation.

    Each tutorial has a self-contained live demo and all the code is available and free to use. Tutorials walk you through all the steps and explains what is going on in the code.

    There are three levels of tutorial:

    Basic - embedding widgets into your pages - no need to know JavaScript
    Intermediate - involve some JavaScript
    Advanced - complex JavaScript and Server side programming - may involve new technologies that are not in all browsers.

    The goal is to add new tutorials every couple of weeks at least and over time build the site into a destination for learning and applying web technologies.

    Please check it out at http://apprentice.craic.com and let me know what you think.



    Monday, November 11, 2013

    Setting up a new Application on Heroku

    You can set up a new web application on Heroku in several ways. Here is my preferred way:

    1: Build and test your application locally

    2: Set it up as a git repository and commit all changes

    3: Go to your account on the Heroku web site and cerate your new app there with your preferred name.

    4: In your local git repo run:

    $ heroku git:remote -a <your-app-name>

    The response should be 'Git remote heroku added'

    5: Upload your code with:

    $ git push heroku master

    6: Finish up your Heroku configuration on the we b site

    7: Import any existing database as directed by this article.


    Tuesday, November 5, 2013

    HABTM relationships in Rails 4

    Rails 4 has changed the way has_and_belongs_to_many (HABTM) relationships are handled as well as introducing strong parameters in the controller as a replacement to having :attr_accessible in your model.

    Here is how you set up HABTM with a joining table.

    My models are Tutorial and Category - a tutorial can have more than one category and vice versa.

    Both models already existed and I wanted to create the relationship between them.

    1: Create a migration for the joining table

    The default table name should be the plural of the two models, in alphabetical order, separated by an underscore. (You can give it any name you want - you just need to include that later in the model)

    class CreateCategoriesTutorials < ActiveRecord::Migration
      def change
        create_table :categories_tutorials do |t|
          t.belongs_to :tutorial
          t.belongs_to :category
        end
      end
    end

    2: Add the relationships to the models

    Previously you would have done this (in the Tutorial model):

    has_many :categories_tutorials, :dependent => :destroy
    has_many :categories, :through => :categories_tutorials, :uniq => true

    That is now simplified to this:

    has_and_belongs_to_many :categories

    If you have a custom table name then specifiy it like this:

    has_and_belongs_to_many :categories, join_table: :my_category_tutorial_table

    The Category model has:

    has_and_belongs_to_many :tutorials

    The CategoryTutorial model has:

    belongs_to :category
    belongs_to :tutorial

    In my example I want to specify Categories when I create a Tutorial. 

    In order for this to work I need to allow the tutorials_controller to accept the category_ids parameter which is passed from the tutorial form.

    At the bottom of the controller Rails has created this:

    def tutorial_params
        params.require(:tutorial).permit(:title, :description, :status)
    end

    I need to add category_ids and have it set up as an array 

    def tutorial_params
        params.require(:tutorial).permit(:title, :description, :status, :category_ids => [])
    end

    That last step is critical - if you miss this then the associated rows are not created in the database.

    For more information on HABTM take a look at:

    http://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association


    Friday, September 20, 2013

    Stop e-mail spam from creating events in Google Calendar

    An ever growing number of email spam messages will attempt to create events in your Google calendar. This doubles your spam experience - in your email and in your calendar - great...

    The messages exploit the default settings in Google Calendar and fortunately this is easy to fix.

    Open up your Google Calendar, click on the Setting icon in the top right of the panel (the Gear wheel icon) and then select 'Settings' from the menu that appears.

    That gives you a long page of settings - look for this one, most of the way down:


    Change the current setting to 'No, only show invitations to which I have responded' and Save your settings.

    That should do it !

    Why Google doesn't have this as the default I have no idea...

    Archive of Tips