At the beginning import the library (it is assumed you have already installed it - see https://neuroinflab.wordpress.com/research/pymice/ for details). The name pm
will be assigned to the library.
import pymice as pm
Before you can start the tutorial you have to have the training data stored in your working directory.
The library might do it for you after you call getTutorialData()
function.
pm.getTutorialData(quiet=True)
At the beginning load data from one of files to investigate it.
ml = pm.Loader('C57_AB/2012-08-28 15.33.58.zip')
The most important piece of data obtained from IntelliCage system are recordings of visits performed by mice to corners. You can obtain list of objects representing all recorded visits with .getVisits()
method.
WARNING: The visits might be in a random order!
For details on selecting a subset of available visits and on ordering the visits see "Tutorial on advanced topics"
visits = ml.getVisits()
print(type(visits))
print(len(visits))
visit = visits[0]
print(type(visit))
print(repr(visit))
Every Visit
object contains information about one visit to a corner.
The .Start
and .End
attributes of the visit object are respectively start and end times of the visit and they are instances of datatime.datatime
class. The .Duration
attribute is their derivative (and therefore a datatime.timedelta
object).
The .Cage
and .Corner
attributes indicates whhich corner of which cage was visited; they are guaranted to be convertable to int
.
Another important attribute is .Animal
which is an Animal
object representing the mouse performing the visit.
print("visit of %s in cage #%d to corner #%d" % (visit.Animal, visit.Cage, visit.Corner))
print("visit duration: %.2fs" % visit.Duration.total_seconds())
print("(from %s to %s)" % (visit.Start, visit.End))
An Animal
object represents a mouse housed in the system, which contains basic information about the animal.
Names of its .Name
and Sex
attributes are self-explanatory (both are instances of unicode
class). The Tag
attribute is a set of animal's transponder identificators (containing more than one identificator if mouse's transponder has been changed during the experiment).
animal = visit.Animal
print(type(animal))
print(repr(animal))
print(animal.Name)
print(animal.Sex)
print(animal.Tag)
You can directly access an Animal
object associated with any mouse registered in the system with the .getAnimal()
method. You can also use the method to obtain names of all animals registered in the system.
animal = ml.getAnimal('C57 A 1')
print(repr(animal))
print(animal.Name)
print(animal.Sex)
print()
print('All registered mice:')
for name in ml.getAnimal():
print(name)
It is also possible to use "shortcuts" to access the information.
print(str(animal))
print(str(animal) == animal.Name)
Animals are housed in cages and with .getCage()
method you can check in which cage(s) the animal was detected.
The cage object is either guaranted to be convertable to an integer or to be a collection of such objects.
You can also check (with .getInmates()
method) which mice were housed in the cage.
cage = ml.getCage('C57 A 1')
print(int(cage))
print()
print("Mice housed in cage #%d:" % cage)
mice = ml.getInmates(cage)
for mouse in mice:
print(mouse)
The method (.getInmates()
) can be also used to list available cages.
print(ml.getInmates())
Animals might be also assigned to certain groups. To list them (as well to obtaing object containing information about a particular group), use the .getGroup()
method.
print(ml.getGroup())
group = ml.getGroup('C57 A')
print("Animals of group %s:" % group.Name)
for mouse in group.Animals:
print(repr(mouse))
If the noseopke data are loaded (see "Tutorial on advanced topics" for details), a Visit
object has a .Nosepoke
attribute containing tuple of objects representing nosepoke events.
WARNING: The nosepokes might be in a random order.
The order
parameter of .getVisits()
method is there solely for technical purposes of the tutorial - it enforces the order of visits so the fifth one (of index 4) is always the same.
visits = ml.getVisits(order=('Start', 'End'))
visit = visits[4]
print(type(visit.Nosepokes))
print(len(visit.Nosepokes))
.Start
, .End
and .Duration
attributes of a Nosepoke
object are analogous to those of the visit object. .Side
attribute is guaranted to be convertable to int
. The .Visit
attribute is the same visit object the nosepoke is assigned to.
.Door
attribute is an auxilary attribute indicating which (left or right) side of the corner was nosepoked.
The Visit
object also provides several auxilary aggregate attributes providing summary of corresponding attributes of its Nosepoke
objects.
visit = visits[4]
nosepoke = visit.Nosepokes[1]
print(type(nosepoke))
print(repr(nosepoke))
from datetime import timedelta
print("nosepoke to side #%d (%s) of the cage #%d" % (nosepoke.Side, nosepoke.Door, nosepoke.Visit.Cage))
print("nosepoke duration: %s (from %s to %s)" % (nosepoke.Duration, nosepoke.Start, nosepoke.End))
print("licks taken %d, licking time: %s" % (nosepoke.LickNumber, nosepoke.LickDuration))
print(sum(n.LickNumber for n in visit.Nosepokes) == visit.LickNumber)
print(sum((n.LickDuration for n in visit.Nosepokes), timedelta(0)) == visit.LickDuration)
print(sum((n.LickContactTime for n in visit.Nosepokes), timedelta(0)) == visit.LickContactTime)
print(sum((n.Duration for n in visit.Nosepokes), timedelta(0)) == visit.NosepokeDuration)
print(nosepoke.Visit is visit)
There is often many data files recorded from one experiment. To merge them into one object you have to load them first into a Loader
and then create an object of Merger
class.
To obtain list of files matching 'C57_AB/*.zip' pattern you might use the glob
module of The Python Standard Library.
import glob
dataFiles = glob.glob('C57_AB/*.zip')
loaders = [pm.Loader(filename) for filename in dataFiles]
mm = pm.Merger(*loaders)
n = len(mm.getVisits())
print("Total number of visits in data: %d" % n)
print(n == sum(len(ml.getVisits()) for ml in loaders))