Skip to content

Commit

Permalink
Major code cleanup thanks to pyflakes.
Browse files Browse the repository at this point in the history
Ran pyflakes on the code, cleaned up a lot of fluff.
  • Loading branch information
mathuin committed Feb 28, 2012
1 parent 74527af commit 4b43f10
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 70 deletions.
2 changes: 1 addition & 1 deletion BuildMaps.py
Expand Up @@ -6,7 +6,7 @@
import sys import sys
sys.path.append('..') sys.path.append('..')
from utils import cleanmkdir from utils import cleanmkdir
from pymclevel import mclevel, box from pymclevel import mclevel
import argparse import argparse


def center(name): def center(name):
Expand Down
11 changes: 3 additions & 8 deletions BuildRegion.py
Expand Up @@ -2,24 +2,19 @@


import logging import logging
logging.basicConfig(level=logging.WARNING) logging.basicConfig(level=logging.WARNING)
from region import Region
from tile import Tile from tile import Tile
from utils import setspawnandsave, materialNamed, names from utils import setspawnandsave
import sys import sys
import argparse import argparse
import os import os
import yaml import yaml
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
from multiprocessing import Pool from multiprocessing import Pool
from itertools import product from itertools import product
from tree import Tree, treeObjs from tree import Tree, treeObjs
from ore import Ore, oreObjs, oreDQ from ore import Ore, oreObjs


sys.path.append('..') sys.path.append('..')
from pymclevel import mclevel, box from pymclevel import mclevel


def main(argv): def main(argv):
"""Builds a region.""" """Builds a region."""
Expand Down
1 change: 0 additions & 1 deletion BuildTile.py
Expand Up @@ -4,7 +4,6 @@


import logging import logging
logging.basicConfig(level=logging.WARNING) logging.basicConfig(level=logging.WARNING)
from region import Region
from tile import Tile from tile import Tile


import sys import sys
Expand Down
5 changes: 0 additions & 5 deletions PrepRegion.py
Expand Up @@ -2,15 +2,10 @@


import logging import logging
logging.basicConfig(level=logging.WARNING) logging.basicConfig(level=logging.WARNING)
from region import Region
import sys import sys
import argparse import argparse
import os import os
import yaml import yaml
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper


def main(argv): def main(argv):
"""Rebuilds maps on broken regions.""" """Rebuilds maps on broken regions."""
Expand Down
3 changes: 0 additions & 3 deletions bathy.py
@@ -1,8 +1,5 @@
# bathy # bathy


from numpy import zeros, uint8
from itertools import product
from math import hypot
from osgeo import gdal from osgeo import gdal


def getBathy(deptharray, maxdepth, geotrans, projection): def getBathy(deptharray, maxdepth, geotrans, projection):
Expand Down
42 changes: 19 additions & 23 deletions clidt.py
@@ -1,14 +1,13 @@
# OpenCL/IDT module # OpenCL/IDT module
import numpy as n import numpy as np
from itertools import product from itertools import product
from random import randint, uniform
from invdisttree import Invdisttree from invdisttree import Invdisttree
from multiprocessing import Pool
try: try:
import pyopencl as cl import pyopencl as cl
import pyopencl.array as cla import pyopencl.array as cla
hasCL = True
except ImportError: except ImportError:
pass hasCL = False


class CLIDT: class CLIDT:
"""Use OpenCL or Invdisttree to solve the IDT problem.""" """Use OpenCL or Invdisttree to solve the IDT problem."""
Expand All @@ -28,10 +27,10 @@ def genindices(self, arrayin):
# need to split if it's too long! # need to split if it's too long!
splitlist = tuple([x for x in xrange(CLIDT.indexmaxsize, arrayin.shape[0], CLIDT.indexmaxsize)]) splitlist = tuple([x for x in xrange(CLIDT.indexmaxsize, arrayin.shape[0], CLIDT.indexmaxsize)])
indexinc = 0 indexinc = 0
for chunk in n.vsplit(arrayin, splitlist): for chunk in np.vsplit(arrayin, splitlist):
chunkarr = cla.to_device(self.queue, n.asarray(chunk, dtype=n.int32)) chunkarr = cla.to_device(self.queue, np.asarray(chunk, dtype=np.int32))
template = cla.empty_like(chunkarr) template = cla.empty_like(chunkarr)
event = self.program.trim(self.queue, chunkarr.shape, None, chunkarr.data, template.data, n.int32(self.split)) event = self.program.trim(self.queue, chunkarr.shape, None, chunkarr.data, template.data, np.int32(self.split))
event.wait() event.wait()
for index, elem in enumerate(template.get()): for index, elem in enumerate(template.get()):
splitkey = tuple([x for x in elem],) splitkey = tuple([x for x in elem],)
Expand All @@ -44,50 +43,47 @@ def genindices(self, arrayin):
return retval return retval


def __init__(self, coords, values, base, wantCL=True, split=None, nnear=None, majority=True): def __init__(self, coords, values, base, wantCL=True, split=None, nnear=None, majority=True):
self.coords = n.asarray(coords, dtype=n.int32) self.coords = np.asarray(coords, dtype=np.int32)
self.values = n.asarray(values, dtype=n.int32) self.values = np.asarray(values, dtype=np.int32)
self.base = n.asarray(base, dtype=n.int32) self.base = np.asarray(base, dtype=np.int32)
(lencoords, null) = self.coords.shape (lencoords, null) = self.coords.shape
(lenvalues,) = self.values.shape (lenvalues,) = self.values.shape
(lenbase, null) = self.base.shape (lenbase, null) = self.base.shape
assert lencoords == lenvalues, "lencoords does not equal lenvalues" assert lencoords == lenvalues, "lencoords does not equal lenvalues"


self.wantCL = wantCL self.wantCL = wantCL
if self.wantCL == True: if hasCL == True and self.wantCL == True:
if split == None: if split == None:
self.split = CLIDT.OpenCLmaxsize self.split = CLIDT.OpenCLmaxsize
else: else:
self.split = split self.split = split
try: try:
import pyopencl as cl
import pyopencl.array as cla
self.ctx = cl.create_some_context() self.ctx = cl.create_some_context()
self.queue = cl.CommandQueue(self.ctx) self.queue = cl.CommandQueue(self.ctx)
filestr = ''.join(open('idt.cl', 'r').readlines()) filestr = ''.join(open('idt.cl', 'r').readlines())
self.program = cl.Program(self.ctx, filestr).build() self.program = cl.Program(self.ctx, filestr).build()
self.coordindices = self.genindices(self.coords) self.coordindices = self.genindices(self.coords)
self.baseindices = self.genindices(self.base) self.baseindices = self.genindices(self.base)
self.canCL = True self.canCL = True
except ImportError: except:
# prolly should be specific here print "warning: unable to use pyopencl, defaulting to Invdisttree"
print "warning: unable to import pyopencl, defaulting to Invdisttree"
self.canCL = False self.canCL = False


if nnear == None: if nnear == None:
self.nnear = n.int32(CLIDT.nnear) self.nnear = np.int32(CLIDT.nnear)
else: else:
self.nnear = n.int32(nnear) self.nnear = np.int32(nnear)


self.usemajority = n.int32(1 if majority else 0) self.usemajority = np.int32(1 if majority else 0)


def build(self, coords, values, base): def build(self, coords, values, base):
(lenbase, null) = base.shape (lenbase, null) = base.shape
(lencoords, null) = coords.shape (lencoords, null) = coords.shape
coords_array = cla.to_device(self.queue, coords) coords_array = cla.to_device(self.queue, coords)
values_array = cla.to_device(self.queue, values) values_array = cla.to_device(self.queue, values)
base_array = cla.to_device(self.queue, base) base_array = cla.to_device(self.queue, base)
template_array = cla.zeros(self.queue, (lenbase), dtype=n.int32) template_array = cla.zeros(self.queue, (lenbase), dtype=np.int32)
event = self.program.nearest(self.queue, base.shape, None, coords_array.data, values_array.data, base_array.data, template_array.data, n.int32(lencoords), self.nnear, self.usemajority) event = self.program.nearest(self.queue, base.shape, None, coords_array.data, values_array.data, base_array.data, template_array.data, np.int32(lencoords), self.nnear, self.usemajority)
event.wait() event.wait()


return template_array.get() return template_array.get()
Expand All @@ -96,7 +92,7 @@ def __call__(self):
# build output array # build output array
if self.wantCL and self.canCL: if self.wantCL and self.canCL:
(lenbase, null) = self.base.shape (lenbase, null) = self.base.shape
retval = n.zeros((lenbase), dtype=n.int32) retval = np.zeros((lenbase), dtype=np.int32)
for key, value in self.baseindices.items(): for key, value in self.baseindices.items():
(a, b) = key (a, b) = key
cindices = [] cindices = []
Expand All @@ -110,7 +106,7 @@ def __call__(self):
retval[value] = self.build(coords, values, base) retval[value] = self.build(coords, values, base)
else: else:
IDT = Invdisttree(self.coords, self.values) IDT = Invdisttree(self.coords, self.values)
retval = n.asarray(IDT(self.base, self.nnear, majority=(self.usemajority==1)), dtype=n.int32) retval = np.asarray(IDT(self.base, self.nnear, majority=(self.usemajority==1)), dtype=np.int32)
return retval return retval


def __del__(self): def __del__(self):
Expand Down
2 changes: 1 addition & 1 deletion crust.py
@@ -1,7 +1,7 @@
# crust module # crust module
from itertools import product from itertools import product
from random import randint, uniform from random import randint, uniform
from newclidt import CLIDT from clidt import CLIDT


class Crust: class Crust:
"""Smoothly irregular crust between the surface and the underlying stone.""" """Smoothly irregular crust between the surface and the underlying stone."""
Expand Down
5 changes: 2 additions & 3 deletions ore.py
@@ -1,10 +1,10 @@
# ore module # ore module
from __future__ import division from __future__ import division
from random import randint from random import randint
from math import pi, ceil from math import pi
from scipy.special import cbrt from scipy.special import cbrt
from itertools import product from itertools import product
from utils import materialNamed, names from utils import materialNamed


# http://www.minecraftforum.net/topic/25886-elites-of-minecraft-the-miner-first-ore-loss-calculated/ (must be logged in) # http://www.minecraftforum.net/topic/25886-elites-of-minecraft-the-miner-first-ore-loss-calculated/ (must be logged in)


Expand Down Expand Up @@ -58,7 +58,6 @@ def placeoreintile(tile):
oreobjs = dict([(ore.name, ore) for ore in oreObjs]) oreobjs = dict([(ore.name, ore) for ore in oreObjs])
tile.ores = dict([(name, list()) for name in oreobjs]) tile.ores = dict([(name, list()) for name in oreobjs])


oreblocks = []
for ore in oreobjs: for ore in oreobjs:
extent = cbrt(oreobjs[ore].size)*2 extent = cbrt(oreobjs[ore].size)*2
maxy = pow(2,oreobjs[ore].depth) maxy = pow(2,oreobjs[ore].depth)
Expand Down
11 changes: 1 addition & 10 deletions region.py
Expand Up @@ -7,15 +7,9 @@
import os import os
import urllib2 import urllib2
import yaml import yaml
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
import logging import logging
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
#logging.getLogger('suds.client').setLevel(logging.DEBUG) from time import sleep
from time import sleep, clock
from tempfile import NamedTemporaryFile
import zipfile import zipfile
import tarfile import tarfile
from utils import cleanmkdir from utils import cleanmkdir
Expand All @@ -29,8 +23,6 @@
from bathy import getBathy from bathy import getBathy
from crust import Crust from crust import Crust
import numpy import numpy
from itertools import product
from random import uniform, randint
# #
from clidt import CLIDT from clidt import CLIDT


Expand Down Expand Up @@ -413,7 +405,6 @@ def downloadfile(self, layerID, downloadURL):
requestID = result[startPos:endPos] requestID = result[startPos:endPos]
print " request ID is %s" % requestID print " request ID is %s" % requestID


downloadDict = {'downloadID': requestID}
sleep(5) sleep(5)
while True: while True:
dsPage = urllib2.urlopen("http://extract.cr.usgs.gov/axis2/services/DownloadService/getDownloadStatus?downloadID=%s" % requestID) dsPage = urllib2.urlopen("http://extract.cr.usgs.gov/axis2/services/DownloadService/getDownloadStatus?downloadID=%s" % requestID)
Expand Down
5 changes: 0 additions & 5 deletions terrain.py
@@ -1,11 +1,6 @@
from random import random, choice from random import random, choice
from utils import materialNamed, height from utils import materialNamed, height
from schematic import Schematic from schematic import Schematic
import yaml
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper


class Terrain: class Terrain:
"""Base class for landcover definitions.""" """Base class for landcover definitions."""
Expand Down
11 changes: 2 additions & 9 deletions tile.py
Expand Up @@ -4,27 +4,20 @@


from __future__ import division from __future__ import division
import yaml import yaml
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
from region import Region from region import Region
import os import os
from itertools import product from itertools import product
import numpy


from utils import cleanmkdir, setspawnandsave from utils import cleanmkdir, setspawnandsave
from random import randint
from osgeo import gdal from osgeo import gdal
from osgeo.gdalconst import GDT_Int16, GA_ReadOnly from osgeo.gdalconst import GA_ReadOnly


import sys import sys
sys.path.append('..') sys.path.append('..')
from pymclevel import mclevel, box from pymclevel import mclevel, box
from terrain import Terrain from terrain import Terrain
from tree import Tree, treeObjs from tree import Tree, treeObjs
from ore import Ore, oreObjs, oreDQ from ore import Ore
from scipy.special import cbrt


class Tile: class Tile:
"""Tiles are the base render object. or something.""" """Tiles are the base render object. or something."""
Expand Down
2 changes: 1 addition & 1 deletion tree.py
Expand Up @@ -91,7 +91,7 @@ def placetreeintile(tile, tree, mcx, mcy, mcz):
tile.trees[tree].append(coords) tile.trees[tree].append(coords)
else: else:
# plant it now! # plant it now!
(blocks, datas) = treeobjs[tree](coords) (blocks, datas) = treeObjs[tree](coords)
[ tile.world.setBlockAt(x, y, z, materialNamed(block)) for (x, y, z, block) in blocks if block != 'Air' ] [ tile.world.setBlockAt(x, y, z, materialNamed(block)) for (x, y, z, block) in blocks if block != 'Air' ]
[ tile.world.setBlockDataAt(x, y, z, data) for (x, y, z, data) in datas if data != 0 ] [ tile.world.setBlockDataAt(x, y, z, data) for (x, y, z, data) in datas if data != 0 ]


Expand Down

0 comments on commit 4b43f10

Please sign in to comment.