Skip to content

Commit

Permalink
Created a git repository
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown authored and unknown committed Aug 31, 2011
0 parents commit ce5a4b3
Show file tree
Hide file tree
Showing 18 changed files with 1,928 additions and 0 deletions.
Empty file added __init__.py
Empty file.
Empty file added filelist/__init__.py
Empty file.
9 changes: 9 additions & 0 deletions filelist/filesettings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
IMG_LOCS = {"cimages":"C:\\images\\"}
RL_LOCS = {"crunlogs":"C:\\runlogs\\"}
STATIC_DIR = "./static/"
#THUMB_DIR = "C:\\thumb\\"
PNG_DIR = "C:\\django\\django\\clinamen\\static\\png\\"
PNG_URL = "../../../static/png/"
THUMB_DIR = "C:\\django\\django\\clinamen\\static\\thumb\\"
THUMB_URL = "../../../static/thumb/"
THUMB_SIZE = 64
82 changes: 82 additions & 0 deletions filelist/filetools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env python
"""Functions to easily process file names."""

import os
import glob
import re


def sort_files_by_date(filelist, newestfirst=True):
"""Return a list of files sorted by time, newest first by default"""

mod_time_file = [(os.lstat(item).st_mtime, item) for item in filelist]

if newestfirst:
mod_time_file.sort(reverse=True)
else:
mod_time_file.sort()

return [file[1] for file in mod_time_file]


def get_files_in_dir(dirname, ext='TIF', globexpr=None, sort=True):
"""Return a list of all files in a directory with extension ext
When ``globexpr`` is given, ``ext`` is ignored and the Python glob module
is used to search for all files with the given pattern.
**Inputs**
* dirname: string, full path to the directory
* ext: string, extension of the files to process
* globexpr: string, glob search expression (can contain wildcards)
* sort: bool, if True the results are sorted by file date/time, newest
first
**Outputs**
* imgs: list of strings, each string in the list is the complete path to
a file
"""

if globexpr:
imgs = glob.glob(os.path.join(dirname, ''.join(globexpr)))
else:
imgs = glob.glob(os.path.join(dirname, ''.join(['*.', ext])))
if sort:
imgs = sort_files_by_date(imgs)

return imgs


def find_imgnames(imglist, startstr, stopstr):
"""Finds names from imglist between startstr and stopstr in time-ordered way
**Inputs**
* imglist: list of str, containing paths of images on disc
* startstr: str, part of the name of the oldest image by date that is
wanted
* stopstr: str, part of the name of the newest image by date that is
wanted
**Outputs**
* imgs: list of str, containing the found paths to image files
"""

imgs = sort_files_by_date(imglist, newestfirst=False)
for img in imgs:
found_one = re.search(startstr, img)
if found_one:
start_idx = imgs.index(img)
break
for img in imgs:
found_one = re.search(stopstr, img)
if found_one:
stop_idx = imgs.index(img)
break

return imgs[start_idx:stop_idx+1]
207 changes: 207 additions & 0 deletions filelist/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
import os
import numpy as np
import scipy as sp
import re
import inspect
import collections
#from odysseus.imageio import imgimport_intelligent, list_of_frames
os.environ['DJANGO_SETTINGS_MODULE'] = "settings"
from django.db import models
import filelist.filesettings as filesettings
import Image

class RunLogInfo(models.Model):
#path = models.FilePathField(filesettings.RL_DIR, primary_key=True)
path = models.CharField(max_length=200, primary_key=True)
loc_key = models.CharField(max_length=100)
time = models.DateTimeField()
sequencepath = models.CharField(max_length=200)
listiterationnumber = models.IntegerField(null=True, blank=True)
liststarttime = models.DateTimeField(null=True,blank=True)
sequenceduration = models.FloatField(null=True,blank=True)
description = models.TextField(null=True, blank=True)
exceptions = models.TextField(null=True, blank=True)
class Meta:
ordering=['-time']

class VariableValue(models.Model):
name = models.CharField(max_length=30)
value = models.FloatField()
runlog = models.ForeignKey('RunLogInfo')

class ImageInfo(models.Model):
#path = models.FilePathField(filesettings.IMG_DIR, primary_key=True)
path = models.CharField(max_length=100, primary_key=True)
loc_key = models.CharField(max_length=100)
time = models.DateTimeField()
height = models.IntegerField(null=True, blank=True)
width = models.IntegerField(null=True, blank=True)
number_of_frames = models.IntegerField(null=True, blank=True)
imgtype = models.ForeignKey('ImageType', null=True, blank=True)
runlog = models.ForeignKey('RunLogInfo', null=True, blank=True)
def makeRawFrames(self):
frames = np.dstack(list_of_frames(self.path))

self.width = np.size(frames,0)
self.height = np.size(frames,1)
self.number_of_frames = np.size(frames,2)

for ii in range(np.size(frames,2)):
filename = os.path.splitext(os.path.split(self.path)[1])[0]+'_'+str(ii)+'.png'

newframe = RawFrame(sourceimage=self, framenumber=ii)
newframe.saveframe(frames[:,:,ii], filename)
newframe.save()
def deleteProcFrames(self):
ProcessedFrame.objects.filter(sourceimage=self).delete()
def ProcessImage(self):
for method in self.imgtype.methods.all():
procmodule = __import__(method.modulename)
procmethod = getattr(procmodule, method.name)
argdict = {}
for param in TypeParameters.objects.filter(imagetype=self.imgtype, methodargument__method=method):
argdict[param.methodargument.name]=param.value
for ROIparam in TypeROI.objects.filter(imagetype=self.imgtype, methodargument__method=method):
argdict[ROIParam.methodargument.name]=ROIparam.ToDict()
result=procmethod(self.path, **argdict)
if not isinstance(result,tuple):
result = (result,)
for ii in range(len(result)):
if isarray(result[ii]):
filename=os.path.splitext(os.path.split(self.path)[1])[0]+'_'+method.name+'_'+str(ii)+'.png'

newrecord = ProcessedFrame(sourceimage=self, method=method, framenumber=ii)
newrecord.saveframe(result[ii], filename)
newrecord.save()
else:
newrecord = ProcessedValue(sourceimage=self, method=method, value=float(item[ii]), index=ii)
newrecord.save()
def getClosestSequence(self):
sql = "SELECT * FROM filelist_runloginfo ORDER BY ABS(TIMESTAMPDIFF(SECOND, time,'" + self.time.strftime('%Y-%m-%d %H:%M:%S') + "')) LIMIT 1"
for retrunlog in RunLogInfo.objects.raw(sql):
self.runlog = retrunlog
def getTypeFromDescription(self):
matchObj = re.search("imgtype=(\w+)", self.runlog.description)
if matchObj:
self.imgtype= matchObj.group(1)
class Meta:
ordering=['-time']

class FrameInfo(models.Model):
pngpath = models.CharField(max_length=100, primary_key=True)
pngurl = models.CharField(max_length=100)
pngheight = models.IntegerField()
pngwidth = models.IntegerField()
thumbpath = models.CharField(max_length=100)
thumburl = models.CharField(max_length=100)
thumbheight = models.IntegerField()
thumbwidth = models.IntegerField()
framenumber = models.IntegerField(null=True, blank=True)
sourceimage = models.ForeignKey('ImageInfo', null=True, blank=True)
def saveframe(self, frame, filename):
self.pngpath=os.path.join(filesettings.PNG_DIR,filename)
self.pngurl=filesettings.PNG_URL+filename
self.thumbpath=os.path.join(filesettings.THUMB_DIR,filename)
self.thumburl=filesettings.THUMB_URL+filename

im=sp.misc.toimage(frame)
im.save(self.pngpath)

self.pngwidth = np.size(frame,0)
self.pngheight = np.size(frame,1)
aspect=float(self.pngwidth)/float(self.pngheight)

if aspect>1:
self.thumbwidth = filesettings.THUMB_SIZE
self.thumbheight = int(filesettings.THUMB_SIZE/aspect)
else:
self.thumbwidth = int(filesettings.THUMB_SIZE * aspect)
self.thumbheight = filesettings.THUMB_SIZE
im.thumbnail([self.thumbwidth, self.thumbheight], Image.ANTIALIAS)
im.save(self.thumbpath)
class Meta:
abstract=True
ordering=['framenumber']

class ProcessingMethod(models.Model):
modulename = models.CharField(max_length=100)
name = models.CharField(max_length=30)
def getargs(self):
procmodule = __import__(self.modulename)
#procmethod = procmodule.getAttr(procmodule, self.name)
procmethod = getattr(procmodule, self.name)
argspec = inspect.getargspec(procmethod)
for keyword in argspec.args[1:]:
newmetharg, created = MethodArgument.objects.get_or_create(method=self, name=keyword)
if re.search("roi_(\w+)", keyword):
newmetharg.isROI = True
newmetharg.save()
class Meta:
unique_together = ("modulename", "name")

class MethodArgument(models.Model):
method = models.ForeignKey('ProcessingMethod')
name = models.CharField(max_length=30)
isROI = models.NullBooleanField(null=True, blank=True)

class ImageType(models.Model):
name = models.CharField(max_length=30, primary_key=True)
methods = models.ManyToManyField('ProcessingMethod')
parameters = models.ManyToManyField('MethodArgument', through='TypeParameters')
sample = models.ForeignKey('RawFrame', null=True, blank=True)
def ClearProcessed(self):
ProcessedFrame.objects.filter(sourceimage__imgtype=self).delete()
def ProcessType(self):
#imtype = ImageType.objects.get(name=self.name)
#for iminfo in imtype.ImageInfo_set.all():
for iminfo in self.imageinfo_set.all():
iminfo.ProcessImage()

class TypeParameters(models.Model):
imagetype = models.ForeignKey('ImageType')
methodargument = models.ForeignKey('MethodArgument')
value = models.FloatField()
class Meta:
unique_together = ("imagetype", "methodargument")

class TypeROI(models.Model):
imagetype = models.ForeignKey('ImageType')
methodargument = models.ForeignKey('MethodArgument')
x1 = models.IntegerField(null=True, blank=True)
x2 = models.IntegerField(null=True, blank=True)
y1 = models.IntegerField(null=True, blank=True)
y2 = models.IntegerField(null=True, blank=True)
def ToDict(self):
return {"x1": x1, "x2": x2, "y1": y1, "y2": y2}
class Meta:
unique_together = ("imagetype", "methodargument")

class ProcessedFrame(FrameInfo):
method = models.ForeignKey('ProcessingMethod', null=True, blank=True)

class RawFrame(FrameInfo):
class Meta:
abstract=False

class ProcessedValue(models.Model):
sourceimage = models.ForeignKey('ImageInfo')
method = models.ForeignKey('ProcessingMethod', null=True, blank=True)
value = models.FloatField()
index = models.IntegerField(null=True, blank=True)
class Meta:
unique_together = ("sourceimage", "method")

def get_iterable(x):
if isinstance(x, collections.Iterable):
return x
else:
return (x,)

def isarray(s):
try:
len(s)
return True
except TypeError:
return False


Loading

0 comments on commit ce5a4b3

Please sign in to comment.