本文整理匯總了Python中Struct.Struct類的典型用法代碼示例。如果您正苦於以下問題:Python Struct類的具體用法?Python Struct怎麽用?Python Struct使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Struct類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: cvtest
def cvtest(name, base_sp, nsp, fs, base_featstruct, kfold=2, clf_type='svm',
nfeats=100, norm=True, ppi_output=None, train_limit=None,
save_data=True, balance_train=False, keep_cols=None, clf_factory=None,
clffact_feats=None, **kwargs):
"""
"""
assert kfold>1, "CV K-fold 1 not possible"
exs = ppi.feature_array(base_sp, fs, base_featstruct,
nsp, **kwargs) if ppi_output is None else ppi_output
arrfeats, ntest_pos = fe.arr_copy(exs.arrfeats), exs.ntest_pos
assert len(arrfeats)>0, '0 examples not supported'
if train_limit: print 'Sampling %s train/cv examples' % train_limit
train_limit = train_limit or len(arrfeats)
arrfeats = arrfeats if keep_cols is None else fe.keep_cols(arrfeats, keep_cols)
arrfeats = fe.keep_rows(arrfeats, random.sample(range(len(arrfeats)),
int(train_limit))) # shuffle even if not sampling. don't random.shuffle
ntest_pos = int(ntest_pos * train_limit / len(arrfeats))
if clf_type in clf_factories and clf_factory is None:
clf_factory, clffact_feats = clf_factories[clf_type]
ppis = []
for k in range(kfold):
print 'Fold %s:' % k
ppis_fold,clf,scaler,feats = fold_test(arrfeats, kfold, k, clf_factory,
clffact_feats, nfeats, norm, balance_train)
ppis += ppis_fold
random.shuffle(ppis)
ppis.sort(key=lambda x: x[2], reverse=True)
result = Struct(traincv=arrfeats[['id1','id2','hit']], clf=clf,
scaler=scaler, ppis=ppis, ntest_pos=ntest_pos, name=name,
species=base_sp, ppi_params=str(clf), feats=feats,
source_feats=exs.arrfeats.dtype.names, balance_train=balance_train)
if save_data:
result.exs = exs
return result
示例2: PAI1
def PAI1(self, data):
pos = 8
header = self.PAI1Header()
header.unpack(data[pos:pos+len(header)])
pos = header.Off
for i in xrange(header.Count):
off = nameOff = Struct.uint32(data[pos:pos+4], endian='>')
pos += 4
name = data[off:].split('\0', 1)[0]
off += 0x14
numAtoms = ord(data[off])
off += 4
#print name
for i in xrange(numAtoms):
sub = Struct.uint32(data[off:off+4], endian='>')
off += 4
fourcc = data[nameOff+sub:nameOff+sub+4]
#if fourcc == 'RLTS':
# print 'RLTS unknown'
#else:
self.Atom(name, fourcc, data[nameOff+sub:])
示例3: Unpack
def Unpack(self, data, out_file, debug=False):
file = open(out_file, 'wb')
if file:
pos = 0
count = Struct.uint32(data[pos:pos+4], endian='>')
pos += 4
if debug == True:
print "Count: %08x" % count
print "\n%08x\n" % pos
offset_list = []
for x in xrange(count):
offset = Struct.uint32(data[pos:pos+4], endian='>')
pos += 4
if debug == True:
print "Offset: %08x" % offset
offset_list.append(offset)
if debug == True:
print "\n%08x\n" % pos
for x in xrange(count):
pos = offset_list[x]
string = nullterm(data[pos:])
string = unicode(string, 'utf_16_be')
if debug == True:
print "String: %s" % string.encode('utf-8')
pos += len(string)
file.write(string.encode('utf-8'))
file.write('\n')
file.close()
else:
print "Could not open file for writing"
sys.exit(1)
示例4: load_elution
def load_elution(fname, getname=True):
# expected file structure:
# first col: gene id
# second col: treat differently if 2nd col header is 'Total' or
# 'Description'
# remaining cols: elution profile data
lines = [l for l in ut.load_tab_file(fname)]
# final row: total count in msblender output; don't skip in cuihong's data
skip_final_row = lines[-1][0][0] == "#"
rows = lines[1:-1] if skip_final_row else lines[1:]
fractions = [f for f in lines[0][1:]]
if fractions[0].lower() in ["total", "totalcount", "description"]:
start_data_col = 2
fractions.remove(fractions[0])
else:
start_data_col = 1
mat = np.matrix([row[start_data_col:] for row in rows], dtype="float32")
prots = [row[0] for row in rows]
elut = Struct(mat=mat, prots=prots, fractions=fractions, filename=fname, filename_original=fname)
if start_data_col == 2:
col2name_vals = [row[1] for row in rows]
elut.column2vals = col2name_vals
if getname:
elut.name = os.path.basename(fname).split(".")[0]
return elut
示例5: load_go_ont
def load_go_ont(fname, filter_namespace='cellular_component'):
dterms = {}
term = None
for r in file(fname,'r'):
if r.strip()=='[Term]':
if hasattr(term,'set_is_a'):
if not filter_namespace or filter_namespace==term.namespace:
dterms[term.acc] = (term.name, term.set_is_a)
term = Struct()
elif r.strip() and term:
att = r.split(': ')[0]
val = ': '.join(r.split(': ')[1:]).strip()
if att=='id':
term.acc = val
elif att=='name':
term.name = val
elif att=='namespace':
term.namespace = val
elif att=='is_a':
val = val.split(' ! ')[0]
if hasattr(term,'set_is_a'):
term.set_is_a.add(val)
else:
term.set_is_a = set([val])
return dterms
示例6: Atom
def Atom(self, name, fourcc, data):
#print '\t' + fourcc
count = Struct.uint16(data[0x10:0x12], endian='>')
# file('rlat/%s.%s' % (name, fourcc), 'wb').write(data[:0x18])
triplets = []
# Calculate offset to triplets (added by dasda).
off = Struct.uint16(data[4:6], endian='<') * 8 + 16;
for i in xrange(count):
af = Struct.float(data[off:off+4], endian='>')
bf = Struct.float(data[off+4:off+8], endian='>')
cf = Struct.float(data[off+8:off+0xC], endian='>')
triplets.append((af, bf, cf))
off += 0xC
if fourcc == 'RLPA':
self.brlyt.Objects[name].RLPA(triplets, self.Loop)
elif fourcc == 'RLVC':
self.brlyt.Objects[name].RLVC(triplets, self.Loop)
elif fourcc == 'RLTS':
self.brlyt.Objects[name].RLTS(triplets, self.Loop)
elif fourcc == 'RLMC':
self.brlyt.Objects[name].RLMC(triplets, self.Loop)
else:
for trip in triplets:
print '\t\tTriplet: %f %f %f' % trip
示例7: __format__
def __format__(self):
self.Zeroes = Struct.uint8[0x40]
self.IMET = Struct.string(4)
self.Fixed = Struct.uint8[8]
self.Sizes = Struct.uint32[3]
self.Flag1 = Struct.uint32
self.Names = Struct.string(0x2A<<1, encoding='utf-16-be', stripNulls=True)[7]
self.Zeroes = Struct.uint8[0x348]
self.Crypto = Struct.uint8[0x10]
示例8: downsample_elution
def downsample_elution(elution, downsample, seed=0):
"""
Return a new elution with every downsample-th fraction.
"""
down_elut = Struct()
down_elut.__dict__ = elution.__dict__.copy()
down_elut.mat = elution.mat[:, seed::2]
down_elut.fractions = elution.fractions[::2]
down_elut.name = elution.name + "_down%i" % downsample
return down_elut
示例9: MAT1
def MAT1(self, data):
pos = 8
matCount = Struct.uint16(data[pos:pos+2], endian='>')
pos += 4
for i in xrange(matCount):
nameOff = Struct.uint32(data[pos:pos+4], endian='>')
pos += 4
name = data[nameOff:].split('\0', 1)[0]
wii.chexdump(data[nameOff:nameOff+0x60])
texid = Struct.uint16(data[nameOff + 0x40:nameOff + 0x42], endian='>')
wrap_s = Struct.uint8(data[nameOff + 0x42:nameOff + 0x43], endian='>')
wrap_t = Struct.uint8(data[nameOff + 0x43:nameOff + 0x44], endian='>')
texid2 = Struct.uint16(data[nameOff + 0x44:nameOff + 0x46], endian='>')
colorA = [Struct.uint16(data[nameOff + 0x1c + x:nameOff + 0x20 + x], endian='>')/255.0 for x in range(0,8,2)]
colorB = [Struct.uint16(data[nameOff + 0x22 + x:nameOff + 0x24 + x], endian='>')/255.0 for x in range(0,8,2)]
flags = Struct.uint32(data[nameOff + 0x3c:nameOff + 0x40], endian='>')
#numtex = 1 # Hardcoded for her pleasure #(flags>>8) & 0xf #guess
numtex = bit_extract(flags, 28, 31)
if numtex == 1:
img = self.Textures[texid]
tex = img[1].create_texture(Texture)
#print 'Material 0x%02x (%s) maps to texture %s' % (i, name, img[0])
elif numtex == 2:
img = self.Textures[texid]
alpha = self.Textures[texid2]
combo = self.ApplyMask(img[1],alpha[1])
tex = combo.create_texture(Texture)
#print 'Material 0x%02x (%s) maps to texture %s and mask %s' % (i, name, img[0], alpha[0])
else:
print "Bad num textures: %d"%numtex
self.Materials.append((name, tex, (colorA,colorB)))
示例10: __format__
def __format__(self):
self.magic = Struct.string(4)
self.magic2 = Struct.string(4)
self.length = Struct.uint32
self.chunk_cnt = Struct.uint32
self.unknown01 = Struct.uint8
self.unknown02 = Struct.uint8
self.unknown03 = Struct.uint16
self.unknown04 = Struct.uint32
self.unknown05 = Struct.uint32
self.unknown06 = Struct.uint32
示例11: subset_elution
def subset_elution(elution, prot_set):
"""
Return an elution only containing the proteins contained in the
provided prot_set.
"""
newel = Struct()
newel.__dict__ = elution.__dict__.copy()
prot_inds, newprots = zip(*[(i, p) for i, p in enumerate(elution.prots) if p in prot_set])
newel.mat = elution.mat[prot_inds, :]
newel.prots = newprots
print len(newel.prots), "prots from", elution.filename, "in set"
return newel
示例12: TXL1
def TXL1(self, data):
pos = 8
texCount = Struct.uint16(data[pos:pos+2], endian='>')
pos += 4
for i in xrange(texCount):
fnOff = Struct.uint32(data[pos:pos+4], endian='>')
pos += 8
fn = data[fnOff+0xC:].split('\0', 1)[0]
tex = TPL(self.Archive.Files['./arc/timg/' + fn]).Textures[0]
self.Textures.append((fn, tex))
示例13: split_muliple_elutions
def split_muliple_elutions(big_elut):
"""
Split an elution into multiple based on use of _fraction_elutions.
"""
elution_columns = _fraction_elutions(big_elut.fractions)
eluts = {}
for elution_name in elution_columns:
new_elut = Struct()
new_elut.__dict__ = big_elut.__dict__.copy()
new_elut.mat = big_elut.mat[:, elution_columns[elution_name]]
new_elut.fractions = list(np.array(big_elut.fractions)[elution_columns[elution_name]])
new_elut.filename = big_elut.filename + "__" + elution_name
eluts[elution_name] = new_elut
return eluts
示例14: PIC1
def PIC1(self, data):
wii.chexdump(data)
name = data[0xC:].split('\0', 1)[0]
mat = Struct.uint16(data[0x5C:0x5E], endian='>')
mat = self.Materials[mat]
xs = Struct.float(data[0x44:0x48], endian='>')
ys = Struct.float(data[0x48:0x4C], endian='>')
x = Struct.float(data[0x24:0x28], endian='>')
y = Struct.float(data[0x28:0x2C], endian='>')
#print 'Picture %s maps to material %s' % (name, mat[0])
#print '\t%fx%f (%f, %f)' % (xs, ys, x, y)
p=Picture(name, mat, [x, y, xs, ys])
self.PanePath[-1].Add(p)
self.Objects[name] = p
示例15: Unpack
def Unpack(self, data, outfile=None, debug=False):
if outfile != None:
file = open(outfile, 'wb')
pos = 0
version = Struct.uint16(data[pos:pos+2], endian='>')
pos += 2
if debug == True:
print "Version: %04x" % version
print
self.string_list = []
while pos < len(data):
string = nullterm(data[pos:])
string = unicode(string, 'utf_16_be')
if debug == True:
print "String: %s" % string
pos += len(string) * 2 + 2
self.string_list.append(string)
file.write(string.encode('utf-8'))
file.write('\n')
if pos < len(data):
file.write('\n')
file.close()