<commit_before>from __future__ import unicode_literals
import sys
import json
import logging
import urllib2
import urllib

from utils import settings

"""
* PERSON:  People, including fictional.
* NORP:    Nationalities or religious or political groups.
* FAC: Facilities, such as buildings, airports, highways, bridges, etc.
* ORG: Companies, agencies, institutions, etc.
* GPE: Countries, cities, states.
* LOC: Non-GPE locations, mountain ranges, bodies of water.
* PRODUCT: Vehicles, weapons, foods, etc. (Not services)
* EVENT:   Named hurricanes, battles, wars, sports events, etc.
* WORK_OF_ART: Titles of books, songs, etc.
* LAW: Named documents made into laws
* LANGUAGE:    Any named language
"""

NLP_FIELDS = ['description', 'text']
NLP_GEO_KEYS = ['GPE', 'LOC', 'FAC']

# logging.basicConfig(filename="{0}/nlp_worker.log".format(settings.LOG_FILE_PATH), level=logging.DEBUG)

def post_to_nlp_service(text):
    text = unicode(text).encode('utf-8')
    text = urllib.quote(text)
    try:
        req = urllib2.Request("http://{0}:{1}/nlp".format(settings.SERVICE_ADDRESS, settings.SERVICE_PORT), text)
        response = urllib2.urlopen(req)
        result = response.read()
        # logging.debug("\n\n--> sent to NLP %s \n\nGOT BACK: %s" % (text, result))
        return result
    except Exception as e:
        logging.error("NLP error: \n {0}".format(e))

def run(entry, *args):
    if args is not None:
        NLP_FIELDS = list(args)

    new_entry = json.load(open(entry, "rb"))

    if 'path' not in new_entry['job'].keys():
        logging.info('no job path in this entry, skipping...')
        return

    text = ''
    for field in NLP_FIELDS:
        if field in new_entry['entry']['fields'].keys():
            v = new_entry['entry']['fields'][field]
            if v is not None:
                if isinstance(v, list):
                    text = u'{0}, {1}'.format(text, ', '.join(v))
                else:
                    text = u'{0}, {1}'.format(text, v)

    nlp_items = dict()

    if len(text) > 0:
        try:
            logging.info('sending {0} chars to nlp'.format(len(text)))
            nlp_items = json.loads(post_to_nlp_service(text))
            for nlp_item_key in nlp_items.keys():
                nlp_text_field_name = "fss_NLP_{0}".format(nlp_item_key)
                new_entry['entry']['fields'][nlp_text_field_name] = nlp_items[nlp_item_key]

            geo_text = ""
            for field in NLP_GEO_KEYS:
                if field in nlp_items.keys():
                    v = nlp_items[field]
                    geo_text = "{0} {1}".format(geo_text, ' '.join(v))

            new_entry['entry']['fields']['ft_NLP_Geo'] = geo_text

        except Exception as e:
            logging.error("could not get response from NLP parser. ")
            logging.error(e)

    else:
        logging.info('no text found to send to NLP in entry \n {0}'.format(json.dumps(new_entry,indent=4)))

    sys.stdout.write(json.dumps(new_entry))
    sys.stdout.flush()
    <commit_msg>VG-4798 - ensure empty ft_NLP_Geo field isn't created, added error handling and additional logging to show why / when nlp isn't requested or nothing is returned.<commit_after>from __future__ import unicode_literals
import sys
import json
import logging
import urllib2
import urllib

from utils import settings

"""
* PERSON:  People, including fictional.
* NORP:    Nationalities or religious or political groups.
* FAC: Facilities, such as buildings, airports, highways, bridges, etc.
* ORG: Companies, agencies, institutions, etc.
* GPE: Countries, cities, states.
* LOC: Non-GPE locations, mountain ranges, bodies of water.
* PRODUCT: Vehicles, weapons, foods, etc. (Not services)
* EVENT:   Named hurricanes, battles, wars, sports events, etc.
* WORK_OF_ART: Titles of books, songs, etc.
* LAW: Named documents made into laws
* LANGUAGE:    Any named language
"""

NLP_FIELDS = ['description', 'text']
NLP_GEO_KEYS = ['GPE', 'LOC', 'FAC']

logging.basicConfig(filename="{0}/nlp_worker.log".format(settings.LOG_FILE_PATH), level=logging.DEBUG)

def post_to_nlp_service(text):
    text = unicode(text).encode('utf-8')
    text = urllib.quote(text)
    try:
        req = urllib2.Request("http://{0}:{1}/nlp".format(settings.SERVICE_ADDRESS, settings.SERVICE_PORT), text)
        response = urllib2.urlopen(req)
        result = response.read()
        logging.info('\n\nsent {0} chars to nlp, got back {1} chars'.format(len(text), len(result)))
        return result
    except Exception as e:
        logging.error('\n\nsent {0} chars to nlp, got back error: {1}'.format(len(text), e))

def run(entry, *args):
    if args is not None:
        NLP_FIELDS = list(args)

    new_entry = json.load(open(entry, "rb"))

    if 'path' not in new_entry['job'].keys():
        logging.info('no job path in this entry, skipping...')
        return

    text = ''
    for field in NLP_FIELDS:
        if field in new_entry['entry']['fields'].keys():
            v = new_entry['entry']['fields'][field]
            if v is not None:
                if isinstance(v, list):
                    text = u'{0}, {1}'.format(text, ', '.join(v))
                else:
                    text = u'{0}, {1}'.format(text, v)

    nlp_items = dict()

    if len(text) > 0:
        try:
            nlp_items = json.loads(post_to_nlp_service(text))
            for nlp_item_key in nlp_items.keys():
                nlp_text_field_name = "fss_NLP_{0}".format(nlp_item_key)
                new_entry['entry']['fields'][nlp_text_field_name] = nlp_items[nlp_item_key]

            geo_text = " "
            for field in NLP_GEO_KEYS:
                if field in nlp_items.keys():
                    if len(nlp_items[field]) > 0:
                        geo_text = "{0} {1}".format(geo_text, ' '.join(nlp_items[field]))
            if not geo_text.isspace():
                new_entry['entry']['fields']['ft_NLP_Geo'] = geo_text

        except Exception as e:
            logging.error("could not get response from NLP parser. ")
            logging.error(e)

    else:
        logging.info('no text found to send to NLP in entry \n {0}'.format(json.dumps(new_entry,indent=4)))

    sys.stdout.write(json.dumps(new_entry))
    sys.stdout.flush()
    <|endoftext|>


<commit_before>import argparse
import sys
import os
from biopy.io import Fasta
from biopy.io import SV

DEFAULT_JUNCTION_LENGTH = 200
SUPPORTED_FORMAT_VERSIONS = ["1", "2"]
DEFAULT_FORMAT_VERSION = "2"

def add_options(main_parser):
    local_parser = main_parser.add_argument_group("Breakpoint library FASTA generation options")
    local_parser.add_argument("--bplib_gff", help="Breakpoint GFF input", required=False)
    local_parser.add_argument("--junction_length", help="Junction length", type=int, default=DEFAULT_JUNCTION_LENGTH)
    local_parser.add_argument("--format_version", help="Version of breakpoint library format to use", choices=SUPPORTED_FORMAT_VERSIONS, default=DEFAULT_FORMAT_VERSION)

def get_seq(sv, jn_type, seq, format_version):
    if format_version == "1":
        return ">%s:%s\n%s" % (sv.id, jn_type, seq)
    return ">%s:%s:%s\n%s" % (sv.id, sv.size(), jn_type, seq)

def generate_bplib(gff, reference, output, junction_length=DEFAULT_JUNCTION_LENGTH, format_version=DEFAULT_FORMAT_VERSION):
    outfd = open(output, "w") if output else sys.stdout
    insertion_sequence_file = gff.replace(".gff", "") + ".ins";
    if not os.path.isfile(insertion_sequence_file):
        raise Exception("Insertion sequence file %s missing" % insertion_sequence_file)
    for sv in SV.parse(gff, Fasta.Seqs(reference, junction_length)):
        flanks = sv.get_flanks()
        if sv.is_insertion():
            if flanks[0] is None or flanks[1] is None:
                raise Exception("No inserted sequence found for insertion %s" % sv.id)
            outfd.write("%s\n" % (get_seq(sv, "A", flanks[0], format_version)))
            outfd.write("%s\n" % (get_seq(sv, "B", flanks[1], format_version)))
        if sv.is_deletion():
            outfd.write("%s\n" % (get_seq(sv, "C", flanks[2], format_version)))
    outfd.close()
<commit_msg>check for missing gff<commit_after>import argparse
import sys
import os
from biopy.io import Fasta
from biopy.io import SV

DEFAULT_JUNCTION_LENGTH = 200
SUPPORTED_FORMAT_VERSIONS = ["1", "2"]
DEFAULT_FORMAT_VERSION = "2"

def add_options(main_parser):
    local_parser = main_parser.add_argument_group("Breakpoint library FASTA generation options")
    local_parser.add_argument("--bplib_gff", help="Breakpoint GFF input", required=False)
    local_parser.add_argument("--junction_length", help="Junction length", type=int, default=DEFAULT_JUNCTION_LENGTH)
    local_parser.add_argument("--format_version", help="Version of breakpoint library format to use", choices=SUPPORTED_FORMAT_VERSIONS, default=DEFAULT_FORMAT_VERSION)

def get_seq(sv, jn_type, seq, format_version):
    if format_version == "1":
        return ">%s:%s\n%s" % (sv.id, jn_type, seq)
    return ">%s:%s:%s\n%s" % (sv.id, sv.size(), jn_type, seq)

def generate_bplib(gff, reference, output, junction_length=DEFAULT_JUNCTION_LENGTH, format_version=DEFAULT_FORMAT_VERSION):
    if not gff or not os.path.isfile(gff):
        raise Exception("GFF file unspecified or missing")

    outfd = open(output, "w") if output else sys.stdout
    insertion_sequence_file = gff.replace(".gff", "") + ".ins";
    if not os.path.isfile(insertion_sequence_file):
        raise Exception("Insertion sequence file %s missing" % insertion_sequence_file)
    for sv in SV.parse(gff, Fasta.Seqs(reference, junction_length)):
        flanks = sv.get_flanks()
        if sv.is_insertion():
            if flanks[0] is None or flanks[1] is None:
                raise Exception("No inserted sequence found for insertion %s" % sv.id)
            outfd.write("%s\n" % (get_seq(sv, "A", flanks[0], format_version)))
            outfd.write("%s\n" % (get_seq(sv, "B", flanks[1], format_version)))
        if sv.is_deletion():
            outfd.write("%s\n" % (get_seq(sv, "C", flanks[2], format_version)))
    outfd.close()
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

# ***************************************************************************
# *                                                                         *
# *   Copyright (c) 2018 sliptonic <shopinthewoods@gmail.com>               *
# *                                                                         *
# *   This program is free software; you can redistribute it and/or modify  *
# *   it under the terms of the GNU Lesser General Public License (LGPL)    *
# *   as published by the Free Software Foundation; either version 2 of     *
# *   the License, or (at your option) any later version.                   *
# *   for detail see the LICENCE text file.                                 *
# *                                                                         *
# *   This program is distributed in the hope that it will be useful,       *
# *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
# *   GNU Library General Public License for more details.                  *
# *                                                                         *
# *   You should have received a copy of the GNU Library General Public     *
# *   License along with this program; if not, write to the Free Software   *
# *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
# *   USA                                                                   *
# *                                                                         *
# *   Bilinear interpolation code modified heavily from the interpolation   *
# *   library https://github.com/pmav99/interpolation                      *
# *   Copyright (c) 2013 by Panagiotis Mavrogiorgos                         *
# *                                                                         *
# ***************************************************************************
import FreeCAD
import FreeCADGui
import Path
import PathScripts.PathGeom as PathGeom
import PathScripts.PathLog as PathLog
import PathScripts.PathUtils as PathUtils

from PySide import QtCore, QtGui

# lazily loaded modules
from lazy_loader.lazy_loader import LazyLoader
Part = LazyLoader('Part', globals(), 'Part')

"""Z Depth Correction Dressup.  This dressup takes a probe file as input and does bilinear interpolation of the Zdepths to correct for a surface which is not parallel to the milling table/bed.  The probe file should conform to the format specified by the linuxcnc G38 probe logging: 9-number coordinate consisting of XYZABCUVW http://linuxcnc.org/docs/html/gcode/g-code.html#gcode:g38
"""

LOGLEVEL = False

LOG_MODULE = PathLog.thisModule()

if LOGLEVEL:
    PathLog.setLevel(PathLog.Level.DEBUG, LOG_MODULE)
    PathLog.trackModule(PathLog.thisModule())
else:
    PathLog.setLevel(PathLog.Level.NOTICE, LOG_MODULE)

# Qt translation handling
def translate(context, text, disambig=None):
    return QtCore.QCoreApplication.translate(context, text, disambig)

movecommands = ['G1', 'G01', 'G2', 'G02', 'G3', 'G03']
rapidcommands = ['G0', 'G00']
arccommands = ['G2', 'G3', 'G02', 'G03']

class ObjectDressup:

    def __init__(self, obj):
        obj.addProperty("App::PropertyLink", "Base", "Path", QtCore.QT_TRANSLATE_NOOP("Path_DressupAxisMap", "The base path to modify"))
        obj.addProperty("App::PropertyFile", "probefile", "ProbeData", QtCore.QT_TRANSLATE_NOOP("Path_DressupZCorrect", "The point file from the surface probing."))
        obj.Proxy = self
        obj.addProperty("Part::PropertyPartShape", "interpSurface", "Path")
        obj.addProperty("App::PropertyDistance", "ArcInterpolate", "Interpolate", QtCore.QT_TRANSLATE_NOOP("Path_DressupZCorrect", "Deflection distance for arc interpolation"))
        obj.addProperty("App::PropertyDistance", "SegInterpolate", "Interpolate", QtCore.QT_TRANSLATE_NOOP("Path_DressupZCorrectp", "break segments into smaller segments of this length."))
        obj.ArcInterpolate = 0.1
        obj.SegInterpolate = 1.0

    def __getstate__(self):
        return None

    def __setstate__(self, state):
        return None

    def onChanged(self, fp, prop):
        if str(prop) == "probefile":
            self._loadFile(fp, fp.probefile)

    def _bilinearInterpolate(self, surface, x, y):

        p1 = FreeCAD.Vector(x, y, 100.0)
        p2 = FreeCAD.Vector(x, y, -100.0)

        vertical_line = Part.Line(p1, p2)
        points, curves = vertical_line.intersectCS(surface)
        return points[0].Z

    def _loadFile(self, obj, filename):
        if filename == "":
            return

        f1 = open(filename, 'r')

        try:
            pointlist = []
            for line in f1.readlines():
                w = line.split()
                xval = round(float(w[0]), 2)
                yval = round(float(w[1]), 2)
                zval = round(float(w[2]), 2)

                pointlist.append([xval, yval, zval])
            PathLog.debug(pointlist)

            cols = list(zip(*pointlist))
            PathLog.debug("cols: {}".format(cols))
            yindex = list(sorted(set(cols[1])))
            PathLog.debug("yindex: {}".format(yindex))

            array = []
            for y in yindex:
                points = sorted([p for p in pointlist if p[1] == y])
                inner = []
                for p in points:
                    inner.append(FreeCAD.Vector(p[0], p[1], p[2]))
                array.append(inner)

            intSurf = Part.BSplineSurface()
            intSurf.interpolate(array)

            obj.interpSurface = intSurf.toShape()
        except Exception:
            raise ValueError("File does not contain appropriate point data")

    def execute(self, obj):

        sampleD = obj.SegInterpolate.Value
        curveD = obj.ArcInterpolate.Value

        if obj.interpSurface.isNull():  # No valid probe data.  return unchanged path
            obj.Path = obj.Base.Path
            return

        surface = obj.interpSurface.toNurbs().Faces[0].Surface

        if obj.Base:
            if obj.Base.isDerivedFrom("Path::Feature"):
                if obj.Base.Path:
                    if obj.Base.Path.Commands:
                        pathlist = obj.Base.Path.Commands

                        newcommandlist = []
                        currLocation = {'X': 0, 'Y': 0, 'Z': 0, 'F': 0}

                        for c in pathlist:
                            PathLog.debug(c)
                            PathLog.debug("     curLoc:{}".format(currLocation))
                            newparams = dict(c.Parameters)
                            zval = newparams.get("Z", currLocation['Z'])
                            if c.Name in movecommands:
                                curVec = FreeCAD.Vector(currLocation['X'], currLocation['Y'], currLocation['Z'])
                                arcwire = PathGeom.edgeForCmd(c, curVec)
                                if arcwire is None:
                                    continue
                                if c.Name in arccommands:
                                    pointlist = arcwire.discretize(Deflection=curveD)
                                else:
                                    disc_number = int(arcwire.Length / sampleD)
                                    if disc_number > 1:
                                        pointlist = arcwire.discretize(Number=int(arcwire.Length / sampleD))
                                    else:
                                        pointlist = [v.Point for v in arcwire.Vertexes]
                                for point in pointlist:
                                    offset = self._bilinearInterpolate(surface, point.x, point.y)
                                    newcommand = Path.Command("G1", {'X': point.x, 'Y': point.y, 'Z': point.z + offset})
                                    newcommandlist.append(newcommand)
                                    currLocation.update(newcommand.Parameters)
                                    currLocation['Z'] = zval

                            else:
                                # Non Feed Command
                                newcommandlist.append(c)
                                currLocation.update(c.Parameters)
                        path = Path.Path(newcommandlist)
                        obj.Path = path

class TaskPanel:

    def __init__(self, obj):
        self.obj = obj
        self.form = FreeCADGui.PySideUic.loadUi(":/panels/ZCorrectEdit.ui")
        FreeCAD.ActiveDocument.openTransaction(translate("Path_DressupZCorrect", "Edit Z Correction Dress-up"))
        self.interpshape = FreeCAD.ActiveDocument.addObject("Part::Feature", "InterpolationSurface")
        self.interpshape.Shape = obj.interpSurface
        self.interpshape.ViewObject.Transparency = 60
        self.interpshape.ViewObject.ShapeColor = (1.00000, 1.00000, 0.01961)
        self.interpshape.ViewObject.Selectable = False
        stock = PathUtils.findParentJob(obj).Stock
        self.interpshape.Placement.Base.z = stock.Shape.BoundBox.ZMax

    def reject(self):
        FreeCAD.ActiveDocument.abortTransaction()
        FreeCADGui.Control.closeDialog()
        FreeCAD.ActiveDocument.recompute()

    def accept(self):
        self.getFields()
        FreeCAD.ActiveDocument.commitTransaction()
        FreeCAD.ActiveDocument.removeObject(self.interpshape.Name)
        FreeCADGui.ActiveDocument.resetEdit()
        FreeCADGui.Control.closeDialog()
        FreeCAD.ActiveDocument.recompute()
        FreeCAD.ActiveDocument.recompute()

    def getFields(self):
        self.obj.Proxy.execute(self.obj)

    def updateUI(self):

        if PathLog.getLevel(LOG_MODULE) == PathLog.Level.DEBUG:
            for obj in FreeCAD.ActiveDocument.Objects:
                if obj.Name.startswith('Shape'):
                    FreeCAD.ActiveDocument.removeObject(obj.Name)
            print('object name %s' % self.obj.Name)
            if hasattr(self.obj.Proxy, "shapes"):
                PathLog.info("showing shapes attribute")
                for shapes in self.obj.Proxy.shapes.itervalues():
                    for shape in shapes:
                        Part.show(shape)
            else:
                PathLog.info("no shapes attribute found")

    def updateModel(self):
        self.getFields()
        self.updateUI()
        FreeCAD.ActiveDocument.recompute()

    def setFields(self):
        self.form.ProbePointFileName.setText(self.obj.probefile)

        self.updateUI()

    def open(self):
        pass

    def setupUi(self):
        self.setFields()
        # now that the form is filled, setup the signal handlers
        self.form.ProbePointFileName.editingFinished.connect(self.updateModel)
        self.form.SetProbePointFileName.clicked.connect(self.SetProbePointFileName)

    def SetProbePointFileName(self):
        filename = QtGui.QFileDialog.getOpenFileName(self.form, translate("Path_Probe", "Select Probe Point File"), None, translate("Path_Probe", "All Files (*.*)"))
        if filename and filename[0]:
            self.obj.probefile = str(filename[0])
            self.setFields()

class ViewProviderDressup:

    def __init__(self, vobj):
        vobj.Proxy = self

    def attach(self, vobj):
        self.obj = vobj.Object
        if self.obj and self.obj.Base:
            for i in self.obj.Base.InList:
                if hasattr(i, "Group"):
                    group = i.Group
                    for g in group:
                        if g.Name == self.obj.Base.Name:
                            group.remove(g)
                    i.Group = group
        return

    def claimChildren(self):
        return [self.obj.Base]

    def setEdit(self, vobj, mode=0):
        FreeCADGui.Control.closeDialog()
        panel = TaskPanel(vobj.Object)
        FreeCADGui.Control.showDialog(panel)
        panel.setupUi()
        return True

    def __getstate__(self):
        return None

    def __setstate__(self, state):
        return None

    def onDelete(self, arg1=None, arg2=None):
        '''this makes sure that the base operation is added back to the project and visible'''
        FreeCADGui.ActiveDocument.getObject(arg1.Object.Base.Name).Visibility = True
        job = PathUtils.findParentJob(arg1.Object)
        job.Proxy.addOperation(arg1.Object.Base)
        arg1.Object.Base = None
        return True

class CommandPathDressup:

    def GetResources(self):
        return {'Pixmap': 'Path-Dressup',
                'MenuText': QtCore.QT_TRANSLATE_NOOP("Path_DressupZCorrect", "Z Depth Correction Dress-up"),
                'Accel': "",
                'ToolTip': QtCore.QT_TRANSLATE_NOOP("Path_DressupZCorrect", "Use Probe Map to correct Z depth")}

    def IsActive(self):
        if FreeCAD.ActiveDocument is not None:
            for o in FreeCAD.ActiveDocument.Objects:
                if o.Name[:3] == "Job":
                        return True
        return False

    def Activated(self):

        # check that the selection contains exactly what we want
        selection = FreeCADGui.Selection.getSelection()
        if len(selection) != 1:
            FreeCAD.Console.PrintError(translate("Path_Dressup", "Please select one path object\n"))
            return
        if not selection[0].isDerivedFrom("Path::Feature"):
            FreeCAD.Console.PrintError(translate("Path_Dressup", "The selected object is not a path\n"))
            return
        if selection[0].isDerivedFrom("Path::FeatureCompoundPython"):
            FreeCAD.Console.PrintError(translate("Path_Dressup", "Please select a Path object"))
            return

        # everything ok!
        FreeCAD.ActiveDocument.openTransaction(translate("Path_DressupZCorrect", "Create Dress-up"))
        FreeCADGui.addModule("PathScripts.PathDressupZCorrect")
        FreeCADGui.addModule("PathScripts.PathUtils")
        FreeCADGui.doCommand('obj = FreeCAD.ActiveDocument.addObject("Path::FeaturePython", "ZCorrectDressup")')
        FreeCADGui.doCommand('PathScripts.PathDressupZCorrect.ObjectDressup(obj)')
        FreeCADGui.doCommand('obj.Base = FreeCAD.ActiveDocument.' + selection[0].Name)
        FreeCADGui.doCommand('PathScripts.PathDressupZCorrect.ViewProviderDressup(obj.ViewObject)')
        FreeCADGui.doCommand('PathScripts.PathUtils.addToJob(obj)')
        FreeCADGui.doCommand('Gui.ActiveDocument.getObject(obj.Base.Name).Visibility = False')
        FreeCADGui.doCommand('obj.ViewObject.Document.setEdit(obj.ViewObject, 0)')
        FreeCAD.ActiveDocument.commitTransaction()
        FreeCAD.ActiveDocument.recompute()

if FreeCAD.GuiUp:
    # register the FreeCAD command
    FreeCADGui.addCommand('Path_DressupZCorrect', CommandPathDressup())

FreeCAD.Console.PrintLog("Loading PathDressup... done\n")
<commit_msg>Allow the probe data file to have blank lines.<commit_after># -*- coding: utf-8 -*-

# ***************************************************************************
# *                                                                         *
# *   Copyright (c) 2018 sliptonic <shopinthewoods@gmail.com>               *
# *                                                                         *
# *   This program is free software; you can redistribute it and/or modify  *
# *   it under the terms of the GNU Lesser General Public License (LGPL)    *
# *   as published by the Free Software Foundation; either version 2 of     *
# *   the License, or (at your option) any later version.                   *
# *   for detail see the LICENCE text file.                                 *
# *                                                                         *
# *   This program is distributed in the hope that it will be useful,       *
# *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
# *   GNU Library General Public License for more details.                  *
# *                                                                         *
# *   You should have received a copy of the GNU Library General Public     *
# *   License along with this program; if not, write to the Free Software   *
# *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
# *   USA                                                                   *
# *                                                                         *
# *   Bilinear interpolation code modified heavily from the interpolation   *
# *   library https://github.com/pmav99/interpolation                      *
# *   Copyright (c) 2013 by Panagiotis Mavrogiorgos                         *
# *                                                                         *
# ***************************************************************************
import FreeCAD
import FreeCADGui
import Path
import PathScripts.PathGeom as PathGeom
import PathScripts.PathLog as PathLog
import PathScripts.PathUtils as PathUtils

from PySide import QtCore, QtGui

# lazily loaded modules
from lazy_loader.lazy_loader import LazyLoader
Part = LazyLoader('Part', globals(), 'Part')

"""Z Depth Correction Dressup.  This dressup takes a probe file as input and does bilinear interpolation of the Zdepths to correct for a surface which is not parallel to the milling table/bed.  The probe file should conform to the format specified by the linuxcnc G38 probe logging: 9-number coordinate consisting of XYZABCUVW http://linuxcnc.org/docs/html/gcode/g-code.html#gcode:g38
"""

LOGLEVEL = False

LOG_MODULE = PathLog.thisModule()

if LOGLEVEL:
    PathLog.setLevel(PathLog.Level.DEBUG, LOG_MODULE)
    PathLog.trackModule(PathLog.thisModule())
else:
    PathLog.setLevel(PathLog.Level.NOTICE, LOG_MODULE)

# Qt translation handling
def translate(context, text, disambig=None):
    return QtCore.QCoreApplication.translate(context, text, disambig)

movecommands = ['G1', 'G01', 'G2', 'G02', 'G3', 'G03']
rapidcommands = ['G0', 'G00']
arccommands = ['G2', 'G3', 'G02', 'G03']

class ObjectDressup:

    def __init__(self, obj):
        obj.addProperty("App::PropertyLink", "Base", "Path", QtCore.QT_TRANSLATE_NOOP("Path_DressupAxisMap", "The base path to modify"))
        obj.addProperty("App::PropertyFile", "probefile", "ProbeData", QtCore.QT_TRANSLATE_NOOP("Path_DressupZCorrect", "The point file from the surface probing."))
        obj.Proxy = self
        obj.addProperty("Part::PropertyPartShape", "interpSurface", "Path")
        obj.addProperty("App::PropertyDistance", "ArcInterpolate", "Interpolate", QtCore.QT_TRANSLATE_NOOP("Path_DressupZCorrect", "Deflection distance for arc interpolation"))
        obj.addProperty("App::PropertyDistance", "SegInterpolate", "Interpolate", QtCore.QT_TRANSLATE_NOOP("Path_DressupZCorrectp", "break segments into smaller segments of this length."))
        obj.ArcInterpolate = 0.1
        obj.SegInterpolate = 1.0

    def __getstate__(self):
        return None

    def __setstate__(self, state):
        return None

    def onChanged(self, fp, prop):
        if str(prop) == "probefile":
            self._loadFile(fp, fp.probefile)

    def _bilinearInterpolate(self, surface, x, y):

        p1 = FreeCAD.Vector(x, y, 100.0)
        p2 = FreeCAD.Vector(x, y, -100.0)

        vertical_line = Part.Line(p1, p2)
        points, curves = vertical_line.intersectCS(surface)
        return points[0].Z

    def _loadFile(self, obj, filename):
        if filename == "":
            return

        f1 = open(filename, 'r')

        try:
            pointlist = []
            for line in f1.readlines():
                if line == '\n':
                    continue
                w = line.split()
                xval = round(float(w[0]), 2)
                yval = round(float(w[1]), 2)
                zval = round(float(w[2]), 2)

                pointlist.append([xval, yval, zval])
            PathLog.debug(pointlist)

            cols = list(zip(*pointlist))
            PathLog.debug("cols: {}".format(cols))
            yindex = list(sorted(set(cols[1])))
            PathLog.debug("yindex: {}".format(yindex))

            array = []
            for y in yindex:
                points = sorted([p for p in pointlist if p[1] == y])
                inner = []
                for p in points:
                    inner.append(FreeCAD.Vector(p[0], p[1], p[2]))
                array.append(inner)

            intSurf = Part.BSplineSurface()
            intSurf.interpolate(array)

            obj.interpSurface = intSurf.toShape()
        except Exception:
            raise ValueError("File does not contain appropriate point data")

    def execute(self, obj):

        sampleD = obj.SegInterpolate.Value
        curveD = obj.ArcInterpolate.Value

        if obj.interpSurface.isNull():  # No valid probe data.  return unchanged path
            obj.Path = obj.Base.Path
            return

        surface = obj.interpSurface.toNurbs().Faces[0].Surface

        if obj.Base:
            if obj.Base.isDerivedFrom("Path::Feature"):
                if obj.Base.Path:
                    if obj.Base.Path.Commands:
                        pathlist = obj.Base.Path.Commands

                        newcommandlist = []
                        currLocation = {'X': 0, 'Y': 0, 'Z': 0, 'F': 0}

                        for c in pathlist:
                            PathLog.debug(c)
                            PathLog.debug("     curLoc:{}".format(currLocation))
                            newparams = dict(c.Parameters)
                            zval = newparams.get("Z", currLocation['Z'])
                            if c.Name in movecommands:
                                curVec = FreeCAD.Vector(currLocation['X'], currLocation['Y'], currLocation['Z'])
                                arcwire = PathGeom.edgeForCmd(c, curVec)
                                if arcwire is None:
                                    continue
                                if c.Name in arccommands:
                                    pointlist = arcwire.discretize(Deflection=curveD)
                                else:
                                    disc_number = int(arcwire.Length / sampleD)
                                    if disc_number > 1:
                                        pointlist = arcwire.discretize(Number=int(arcwire.Length / sampleD))
                                    else:
                                        pointlist = [v.Point for v in arcwire.Vertexes]
                                for point in pointlist:
                                    offset = self._bilinearInterpolate(surface, point.x, point.y)
                                    newcommand = Path.Command("G1", {'X': point.x, 'Y': point.y, 'Z': point.z + offset})
                                    newcommandlist.append(newcommand)
                                    currLocation.update(newcommand.Parameters)
                                    currLocation['Z'] = zval

                            else:
                                # Non Feed Command
                                newcommandlist.append(c)
                                currLocation.update(c.Parameters)
                        path = Path.Path(newcommandlist)
                        obj.Path = path

class TaskPanel:

    def __init__(self, obj):
        self.obj = obj
        self.form = FreeCADGui.PySideUic.loadUi(":/panels/ZCorrectEdit.ui")
        FreeCAD.ActiveDocument.openTransaction(translate("Path_DressupZCorrect", "Edit Z Correction Dress-up"))
        self.interpshape = FreeCAD.ActiveDocument.addObject("Part::Feature", "InterpolationSurface")
        self.interpshape.Shape = obj.interpSurface
        self.interpshape.ViewObject.Transparency = 60
        self.interpshape.ViewObject.ShapeColor = (1.00000, 1.00000, 0.01961)
        self.interpshape.ViewObject.Selectable = False
        stock = PathUtils.findParentJob(obj).Stock
        self.interpshape.Placement.Base.z = stock.Shape.BoundBox.ZMax

    def reject(self):
        FreeCAD.ActiveDocument.abortTransaction()
        FreeCADGui.Control.closeDialog()
        FreeCAD.ActiveDocument.recompute()

    def accept(self):
        self.getFields()
        FreeCAD.ActiveDocument.commitTransaction()
        FreeCAD.ActiveDocument.removeObject(self.interpshape.Name)
        FreeCADGui.ActiveDocument.resetEdit()
        FreeCADGui.Control.closeDialog()
        FreeCAD.ActiveDocument.recompute()
        FreeCAD.ActiveDocument.recompute()

    def getFields(self):
        self.obj.Proxy.execute(self.obj)

    def updateUI(self):

        if PathLog.getLevel(LOG_MODULE) == PathLog.Level.DEBUG:
            for obj in FreeCAD.ActiveDocument.Objects:
                if obj.Name.startswith('Shape'):
                    FreeCAD.ActiveDocument.removeObject(obj.Name)
            print('object name %s' % self.obj.Name)
            if hasattr(self.obj.Proxy, "shapes"):
                PathLog.info("showing shapes attribute")
                for shapes in self.obj.Proxy.shapes.itervalues():
                    for shape in shapes:
                        Part.show(shape)
            else:
                PathLog.info("no shapes attribute found")

    def updateModel(self):
        self.getFields()
        self.updateUI()
        FreeCAD.ActiveDocument.recompute()

    def setFields(self):
        self.form.ProbePointFileName.setText(self.obj.probefile)

        self.updateUI()

    def open(self):
        pass

    def setupUi(self):
        self.setFields()
        # now that the form is filled, setup the signal handlers
        self.form.ProbePointFileName.editingFinished.connect(self.updateModel)
        self.form.SetProbePointFileName.clicked.connect(self.SetProbePointFileName)

    def SetProbePointFileName(self):
        filename = QtGui.QFileDialog.getOpenFileName(self.form, translate("Path_Probe", "Select Probe Point File"), None, translate("Path_Probe", "All Files (*.*)"))
        if filename and filename[0]:
            self.obj.probefile = str(filename[0])
            self.setFields()

class ViewProviderDressup:

    def __init__(self, vobj):
        vobj.Proxy = self

    def attach(self, vobj):
        self.obj = vobj.Object
        if self.obj and self.obj.Base:
            for i in self.obj.Base.InList:
                if hasattr(i, "Group"):
                    group = i.Group
                    for g in group:
                        if g.Name == self.obj.Base.Name:
                            group.remove(g)
                    i.Group = group
        return

    def claimChildren(self):
        return [self.obj.Base]

    def setEdit(self, vobj, mode=0):
        FreeCADGui.Control.closeDialog()
        panel = TaskPanel(vobj.Object)
        FreeCADGui.Control.showDialog(panel)
        panel.setupUi()
        return True

    def __getstate__(self):
        return None

    def __setstate__(self, state):
        return None

    def onDelete(self, arg1=None, arg2=None):
        '''this makes sure that the base operation is added back to the project and visible'''
        FreeCADGui.ActiveDocument.getObject(arg1.Object.Base.Name).Visibility = True
        job = PathUtils.findParentJob(arg1.Object)
        job.Proxy.addOperation(arg1.Object.Base)
        arg1.Object.Base = None
        return True

class CommandPathDressup:

    def GetResources(self):
        return {'Pixmap': 'Path-Dressup',
                'MenuText': QtCore.QT_TRANSLATE_NOOP("Path_DressupZCorrect", "Z Depth Correction Dress-up"),
                'Accel': "",
                'ToolTip': QtCore.QT_TRANSLATE_NOOP("Path_DressupZCorrect", "Use Probe Map to correct Z depth")}

    def IsActive(self):
        if FreeCAD.ActiveDocument is not None:
            for o in FreeCAD.ActiveDocument.Objects:
                if o.Name[:3] == "Job":
                        return True
        return False

    def Activated(self):

        # check that the selection contains exactly what we want
        selection = FreeCADGui.Selection.getSelection()
        if len(selection) != 1:
            FreeCAD.Console.PrintError(translate("Path_Dressup", "Please select one path object\n"))
            return
        if not selection[0].isDerivedFrom("Path::Feature"):
            FreeCAD.Console.PrintError(translate("Path_Dressup", "The selected object is not a path\n"))
            return
        if selection[0].isDerivedFrom("Path::FeatureCompoundPython"):
            FreeCAD.Console.PrintError(translate("Path_Dressup", "Please select a Path object"))
            return

        # everything ok!
        FreeCAD.ActiveDocument.openTransaction(translate("Path_DressupZCorrect", "Create Dress-up"))
        FreeCADGui.addModule("PathScripts.PathDressupZCorrect")
        FreeCADGui.addModule("PathScripts.PathUtils")
        FreeCADGui.doCommand('obj = FreeCAD.ActiveDocument.addObject("Path::FeaturePython", "ZCorrectDressup")')
        FreeCADGui.doCommand('PathScripts.PathDressupZCorrect.ObjectDressup(obj)')
        FreeCADGui.doCommand('obj.Base = FreeCAD.ActiveDocument.' + selection[0].Name)
        FreeCADGui.doCommand('PathScripts.PathDressupZCorrect.ViewProviderDressup(obj.ViewObject)')
        FreeCADGui.doCommand('PathScripts.PathUtils.addToJob(obj)')
        FreeCADGui.doCommand('Gui.ActiveDocument.getObject(obj.Base.Name).Visibility = False')
        FreeCADGui.doCommand('obj.ViewObject.Document.setEdit(obj.ViewObject, 0)')
        FreeCAD.ActiveDocument.commitTransaction()
        FreeCAD.ActiveDocument.recompute()

if FreeCAD.GuiUp:
    # register the FreeCAD command
    FreeCADGui.addCommand('Path_DressupZCorrect', CommandPathDressup())

FreeCAD.Console.PrintLog("Loading PathDressup... done\n")
<|endoftext|>


<commit_before>VERSION = "0.4.1"
<commit_msg>Bump version<commit_after>VERSION = "0.4.2"
<|endoftext|>


<commit_before>"""
PF: Gravity: Laguna del Maule Bouguer Gravity
=============================================

This notebook illustrates the SimPEG code used to invert Bouguer
gravity data collected at Laguna del Maule volcanic field, Chile.
Refer to Miller et al 2016 EPSL for full details.

We run the inversion in two steps.  Firstly creating a L2 model and
then applying an Lp norm to produce a compact model.

Craig Miller
"""
import os
import shutil
import tarfile
from SimPEG.potential_fields import gravity
from SimPEG import (
    data,
    data_misfit,
    maps,
    regularization,
    optimization,
    inverse_problem,
    directives,
    inversion,
)
from SimPEG import utils
from SimPEG.utils import download, plot2Ddata

import matplotlib.pyplot as plt
import numpy as np
from SimPEG.utils.drivers.gravity_driver import GravityDriver_Inv

def run(plotIt=True, cleanAfterRun=True):

    # Start by downloading files from the remote repository
    # directory where the downloaded files are

    url = "https://storage.googleapis.com/simpeg/Chile_GRAV_4_Miller/Chile_GRAV_4_Miller.tar.gz"
    downloads = download(url, overwrite=True)
    basePath = downloads.split(".")[0]

    # unzip the tarfile
    tar = tarfile.open(downloads, "r")
    tar.extractall()
    tar.close()

    input_file = basePath + os.path.sep + "LdM_input_file.inp"
    # %% User input
    # Plotting parameters, max and min densities in g/cc
    vmin = -0.6
    vmax = 0.6

    # weight exponent for default weighting
    wgtexp = 3.0
    # %%
    # Read in the input file which included all parameters at once
    # (mesh, topo, model, survey, inv param, etc.)
    driver = GravityDriver_Inv(input_file)
    # %%
    # Now we need to create the survey and model information.

    # Access the mesh and survey information
    mesh = driver.mesh  #
    survey = driver.survey
    data_object = driver.data
    # [survey, data_object] = driver.survey

    # define gravity survey locations
    rxLoc = survey.source_field.receiver_list[0].locations

    # define gravity data and errors
    d = data_object.dobs

    # Get the active cells
    active = driver.activeCells
    nC = len(active)  # Number of active cells

    # Create active map to go from reduce set to full
    activeMap = maps.InjectActiveCells(mesh, active, -100)

    # Create static map
    static = driver.staticCells
    dynamic = driver.dynamicCells

    staticCells = maps.InjectActiveCells(None, dynamic, driver.m0[static], nC=nC)
    mstart = driver.m0[dynamic]

    # Get index of the center
    midx = int(mesh.nCx / 2)
    # %%
    # Now that we have a model and a survey we can build the linear system ...
    # Create the forward model operator
    simulation = gravity.simulation.Simulation3DIntegral(
        survey=survey, mesh=mesh, rhoMap=staticCells, actInd=active
    )

    # %% Create inversion objects
    reg = regularization.Sparse(
        mesh, indActive=active, mapping=staticCells, gradientType="total"
    )
    reg.mref = driver.mref[dynamic]

    reg.norms = np.c_[0.0, 1.0, 1.0, 1.0]
    # reg.norms = driver.lpnorms

    # Specify how the optimization will proceed
    opt = optimization.ProjectedGNCG(
        maxIter=20,
        lower=driver.bounds[0],
        upper=driver.bounds[1],
        maxIterLS=10,
        maxIterCG=20,
        tolCG=1e-4,
    )

    # Define misfit function (obs-calc)
    dmis = data_misfit.L2DataMisfit(data=data_object, simulation=simulation)

    # create the default L2 inverse problem from the above objects
    invProb = inverse_problem.BaseInvProblem(dmis, reg, opt)

    # Specify how the initial beta is found
    betaest = directives.BetaEstimate_ByEig(beta0_ratio=1e-2)

    # IRLS sets up the Lp inversion problem
    # Set the eps parameter parameter in Line 11 of the
    # input file based on the distribution of model (DEFAULT = 95th %ile)
    IRLS = directives.Update_IRLS(
        f_min_change=1e-4, max_irls_iterations=40, coolEpsFact=1.5, beta_tol=5e-1
    )

    # Preconditioning refreshing for each IRLS iteration
    update_Jacobi = directives.UpdatePreconditioner()
    sensitivity_weights = directives.UpdateSensitivityWeights()

    # Create combined the L2 and Lp problem
    inv = inversion.BaseInversion(
        invProb, directiveList=[sensitivity_weights, IRLS, update_Jacobi, betaest]
    )

    # %%
    # Run L2 and Lp inversion
    mrec = inv.run(mstart)

    if cleanAfterRun:
        os.remove(downloads)
        shutil.rmtree(basePath)

    # %%
    if plotIt:
        # Plot observed data
        # The sign of the data is flipped here for the change of convention
        # between Cartesian (SimPEG) and traditional gravity data (positive down)
        plot2Ddata(rxLoc, -d)

        # %%
        # Write output model and data files and print misfit stats.

        # reconstructing l2 model mesh with air cells and active dynamic cells
        L2out = activeMap * invProb.l2model

        # reconstructing lp model mesh with air cells and active dynamic cells
        Lpout = activeMap * mrec

        # %%
        # Plot out sections and histograms of the smooth l2 model.
        # The ind= parameter is the slice of the model from top down.
        yslice = midx + 1
        L2out[L2out == -100] = np.nan  # set "air" to nan

        plt.figure(figsize=(10, 7))
        plt.suptitle("Smooth Inversion: Depth weight = " + str(wgtexp))
        ax = plt.subplot(221)
        dat1 = mesh.plotSlice(
            L2out,
            ax=ax,
            normal="Z",
            ind=-16,
            clim=(vmin, vmax),
            pcolorOpts={"cmap": "bwr"},
        )
        plt.plot(
            np.array([mesh.vectorCCx[0], mesh.vectorCCx[-1]]),
            np.array([mesh.vectorCCy[yslice], mesh.vectorCCy[yslice]]),
            c="gray",
            linestyle="--",
        )
        plt.scatter(rxLoc[0:, 0], rxLoc[0:, 1], color="k", s=1)
        plt.title("Z: " + str(mesh.vectorCCz[-16]) + " m")
        plt.xlabel("Easting (m)")
        plt.ylabel("Northing (m)")
        plt.gca().set_aspect("equal", adjustable="box")
        cb = plt.colorbar(
            dat1[0], orientation="vertical", ticks=np.linspace(vmin, vmax, 4)
        )
        cb.set_label("Density (g/cc$^3$)")

        ax = plt.subplot(222)
        dat = mesh.plotSlice(
            L2out,
            ax=ax,
            normal="Z",
            ind=-27,
            clim=(vmin, vmax),
            pcolorOpts={"cmap": "bwr"},
        )
        plt.plot(
            np.array([mesh.vectorCCx[0], mesh.vectorCCx[-1]]),
            np.array([mesh.vectorCCy[yslice], mesh.vectorCCy[yslice]]),
            c="gray",
            linestyle="--",
        )
        plt.scatter(rxLoc[0:, 0], rxLoc[0:, 1], color="k", s=1)
        plt.title("Z: " + str(mesh.vectorCCz[-27]) + " m")
        plt.xlabel("Easting (m)")
        plt.ylabel("Northing (m)")
        plt.gca().set_aspect("equal", adjustable="box")
        cb = plt.colorbar(
            dat1[0], orientation="vertical", ticks=np.linspace(vmin, vmax, 4)
        )
        cb.set_label("Density (g/cc$^3$)")

        ax = plt.subplot(212)
        mesh.plotSlice(
            L2out,
            ax=ax,
            normal="Y",
            ind=yslice,
            clim=(vmin, vmax),
            pcolorOpts={"cmap": "bwr"},
        )
        plt.title("Cross Section")
        plt.xlabel("Easting(m)")
        plt.ylabel("Elevation")
        plt.gca().set_aspect("equal", adjustable="box")
        cb = plt.colorbar(
            dat1[0],
            orientation="vertical",
            ticks=np.linspace(vmin, vmax, 4),
            cmap="bwr",
        )
        cb.set_label("Density (g/cc$^3$)")

        # %%
        # Make plots of Lp model
        yslice = midx + 1
        Lpout[Lpout == -100] = np.nan  # set "air" to nan

        plt.figure(figsize=(10, 7))
        plt.suptitle(
            "Compact Inversion: Depth weight = "
            + str(wgtexp)
            + ": $\epsilon_p$ = "
            + str(round(reg.eps_p, 1))
            + ": $\epsilon_q$ = "
            + str(round(reg.eps_q, 2))
        )
        ax = plt.subplot(221)
        dat = mesh.plotSlice(
            Lpout,
            ax=ax,
            normal="Z",
            ind=-16,
            clim=(vmin, vmax),
            pcolorOpts={"cmap": "bwr"},
        )
        plt.plot(
            np.array([mesh.vectorCCx[0], mesh.vectorCCx[-1]]),
            np.array([mesh.vectorCCy[yslice], mesh.vectorCCy[yslice]]),
            c="gray",
            linestyle="--",
        )
        plt.scatter(rxLoc[0:, 0], rxLoc[0:, 1], color="k", s=1)
        plt.title("Z: " + str(mesh.vectorCCz[-16]) + " m")
        plt.xlabel("Easting (m)")
        plt.ylabel("Northing (m)")
        plt.gca().set_aspect("equal", adjustable="box")
        cb = plt.colorbar(
            dat[0], orientation="vertical", ticks=np.linspace(vmin, vmax, 4)
        )
        cb.set_label("Density (g/cc$^3$)")

        ax = plt.subplot(222)
        dat = mesh.plotSlice(
            Lpout,
            ax=ax,
            normal="Z",
            ind=-27,
            clim=(vmin, vmax),
            pcolorOpts={"cmap": "bwr"},
        )
        plt.plot(
            np.array([mesh.vectorCCx[0], mesh.vectorCCx[-1]]),
            np.array([mesh.vectorCCy[yslice], mesh.vectorCCy[yslice]]),
            c="gray",
            linestyle="--",
        )
        plt.scatter(rxLoc[0:, 0], rxLoc[0:, 1], color="k", s=1)
        plt.title("Z: " + str(mesh.vectorCCz[-27]) + " m")
        plt.xlabel("Easting (m)")
        plt.ylabel("Northing (m)")
        plt.gca().set_aspect("equal", adjustable="box")
        cb = plt.colorbar(
            dat[0], orientation="vertical", ticks=np.linspace(vmin, vmax, 4)
        )
        cb.set_label("Density (g/cc$^3$)")

        ax = plt.subplot(212)
        dat = mesh.plotSlice(
            Lpout,
            ax=ax,
            normal="Y",
            ind=yslice,
            clim=(vmin, vmax),
            pcolorOpts={"cmap": "bwr"},
        )
        plt.title("Cross Section")
        plt.xlabel("Easting (m)")
        plt.ylabel("Elevation (m)")
        plt.gca().set_aspect("equal", adjustable="box")
        cb = plt.colorbar(
            dat[0], orientation="vertical", ticks=np.linspace(vmin, vmax, 4)
        )
        cb.set_label("Density (g/cc$^3$)")

if __name__ == "__main__":
    run()
    plt.show()
<commit_msg>Update plot_laguna_del_maule_inversion.py<commit_after>"""
PF: Gravity: Laguna del Maule Bouguer Gravity
=============================================

This notebook illustrates the SimPEG code used to invert Bouguer
gravity data collected at Laguna del Maule volcanic field, Chile.
Refer to Miller et al 2016 EPSL for full details.

We run the inversion in two steps.  Firstly creating a L2 model and
then applying an Lp norm to produce a compact model.

Craig Miller
"""
import os
import shutil
import tarfile
from SimPEG.potential_fields import gravity
from SimPEG import (
    data,
    data_misfit,
    maps,
    regularization,
    optimization,
    inverse_problem,
    directives,
    inversion,
)
from SimPEG import utils
from SimPEG.utils import download, plot2Ddata

import matplotlib.pyplot as plt
import numpy as np
from SimPEG.utils.drivers.gravity_driver import GravityDriver_Inv

def run(plotIt=True, cleanAfterRun=True):

    # Start by downloading files from the remote repository
    # directory where the downloaded files are

    url = "https://storage.googleapis.com/simpeg/Chile_GRAV_4_Miller/Chile_GRAV_4_Miller.tar.gz"
    downloads = download(url, overwrite=True)
    basePath = downloads.split(".")[0]

    # unzip the tarfile
    tar = tarfile.open(downloads, "r")
    tar.extractall()
    tar.close()

    input_file = basePath + os.path.sep + "LdM_input_file.inp"
    # %% User input
    # Plotting parameters, max and min densities in g/cc
    vmin = -0.6
    vmax = 0.6

    # weight exponent for default weighting
    wgtexp = 3.0
    # %%
    # Read in the input file which included all parameters at once
    # (mesh, topo, model, survey, inv param, etc.)
    driver = GravityDriver_Inv(input_file)
    # %%
    # Now we need to create the survey and model information.

    # Access the mesh and survey information
    mesh = driver.mesh  #
    survey = driver.survey
    data_object = driver.data
    # [survey, data_object] = driver.survey

    # define gravity survey locations
    rxLoc = survey.source_field.receiver_list[0].locations

    # define gravity data and errors
    d = data_object.dobs

    # Get the active cells
    active = driver.activeCells
    nC = len(active)  # Number of active cells

    # Create active map to go from reduce set to full
    activeMap = maps.InjectActiveCells(mesh, active, -100)

    # Create static map
    static = driver.staticCells
    dynamic = driver.dynamicCells

    staticCells = maps.InjectActiveCells(None, dynamic, driver.m0[static], nC=nC)
    mstart = driver.m0[dynamic]

    # Get index of the center
    midx = int(mesh.nCx / 2)
    # %%
    # Now that we have a model and a survey we can build the linear system ...
    # Create the forward model operator
    simulation = gravity.simulation.Simulation3DIntegral(
        survey=survey, mesh=mesh, rhoMap=staticCells, actInd=active
    )

    # %% Create inversion objects
    reg = regularization.Sparse(
        mesh, indActive=active, mapping=staticCells, gradientType="total"
    )
    reg.mref = driver.mref[dynamic]

    reg.norms = np.c_[0.0, 1.0, 1.0, 1.0]
    # reg.norms = driver.lpnorms

    # Specify how the optimization will proceed
    opt = optimization.ProjectedGNCG(
        maxIter=20,
        lower=driver.bounds[0],
        upper=driver.bounds[1],
        maxIterLS=10,
        maxIterCG=20,
        tolCG=1e-4,
    )

    # Define misfit function (obs-calc)
    dmis = data_misfit.L2DataMisfit(data=data_object, simulation=simulation)

    # create the default L2 inverse problem from the above objects
    invProb = inverse_problem.BaseInvProblem(dmis, reg, opt)

    # Specify how the initial beta is found
    betaest = directives.BetaEstimate_ByEig(beta0_ratio=1e-2)

    # IRLS sets up the Lp inversion problem
    # Set the eps parameter parameter in Line 11 of the
    # input file based on the distribution of model (DEFAULT = 95th %ile)
    IRLS = directives.Update_IRLS(
        f_min_change=1e-4, max_irls_iterations=40, coolEpsFact=1.5, beta_tol=5e-1
    )

    # Preconditioning refreshing for each IRLS iteration
    update_Jacobi = directives.UpdatePreconditioner()
    sensitivity_weights = directives.UpdateSensitivityWeights()

    # Create combined the L2 and Lp problem
    inv = inversion.BaseInversion(
        invProb, directiveList=[sensitivity_weights, IRLS, update_Jacobi, betaest]
    )

    # %%
    # Run L2 and Lp inversion
    mrec = inv.run(mstart)

    if cleanAfterRun:
        os.remove(downloads)
        shutil.rmtree(basePath)

    # %%
    if plotIt:
        # Plot observed data
        # The sign of the data is flipped here for the change of convention
        # between Cartesian coordinate system (internal SimPEG format that
        # expects "positive up" gravity signal) and traditional gravity data
        # conventions (positive down). For example a traditional negative
        # gravity anomaly is described as "positive up" in Cartesian coordinates
        # and hence the sign needs to be flipped for use in SimPEG.
        plot2Ddata(rxLoc, -d)

        # %%
        # Write output model and data files and print misfit stats.

        # reconstructing l2 model mesh with air cells and active dynamic cells
        L2out = activeMap * invProb.l2model

        # reconstructing lp model mesh with air cells and active dynamic cells
        Lpout = activeMap * mrec

        # %%
        # Plot out sections and histograms of the smooth l2 model.
        # The ind= parameter is the slice of the model from top down.
        yslice = midx + 1
        L2out[L2out == -100] = np.nan  # set "air" to nan

        plt.figure(figsize=(10, 7))
        plt.suptitle("Smooth Inversion: Depth weight = " + str(wgtexp))
        ax = plt.subplot(221)
        dat1 = mesh.plotSlice(
            L2out,
            ax=ax,
            normal="Z",
            ind=-16,
            clim=(vmin, vmax),
            pcolorOpts={"cmap": "bwr"},
        )
        plt.plot(
            np.array([mesh.vectorCCx[0], mesh.vectorCCx[-1]]),
            np.array([mesh.vectorCCy[yslice], mesh.vectorCCy[yslice]]),
            c="gray",
            linestyle="--",
        )
        plt.scatter(rxLoc[0:, 0], rxLoc[0:, 1], color="k", s=1)
        plt.title("Z: " + str(mesh.vectorCCz[-16]) + " m")
        plt.xlabel("Easting (m)")
        plt.ylabel("Northing (m)")
        plt.gca().set_aspect("equal", adjustable="box")
        cb = plt.colorbar(
            dat1[0], orientation="vertical", ticks=np.linspace(vmin, vmax, 4)
        )
        cb.set_label("Density (g/cc$^3$)")

        ax = plt.subplot(222)
        dat = mesh.plotSlice(
            L2out,
            ax=ax,
            normal="Z",
            ind=-27,
            clim=(vmin, vmax),
            pcolorOpts={"cmap": "bwr"},
        )
        plt.plot(
            np.array([mesh.vectorCCx[0], mesh.vectorCCx[-1]]),
            np.array([mesh.vectorCCy[yslice], mesh.vectorCCy[yslice]]),
            c="gray",
            linestyle="--",
        )
        plt.scatter(rxLoc[0:, 0], rxLoc[0:, 1], color="k", s=1)
        plt.title("Z: " + str(mesh.vectorCCz[-27]) + " m")
        plt.xlabel("Easting (m)")
        plt.ylabel("Northing (m)")
        plt.gca().set_aspect("equal", adjustable="box")
        cb = plt.colorbar(
            dat1[0], orientation="vertical", ticks=np.linspace(vmin, vmax, 4)
        )
        cb.set_label("Density (g/cc$^3$)")

        ax = plt.subplot(212)
        mesh.plotSlice(
            L2out,
            ax=ax,
            normal="Y",
            ind=yslice,
            clim=(vmin, vmax),
            pcolorOpts={"cmap": "bwr"},
        )
        plt.title("Cross Section")
        plt.xlabel("Easting(m)")
        plt.ylabel("Elevation")
        plt.gca().set_aspect("equal", adjustable="box")
        cb = plt.colorbar(
            dat1[0],
            orientation="vertical",
            ticks=np.linspace(vmin, vmax, 4),
            cmap="bwr",
        )
        cb.set_label("Density (g/cc$^3$)")

        # %%
        # Make plots of Lp model
        yslice = midx + 1
        Lpout[Lpout == -100] = np.nan  # set "air" to nan

        plt.figure(figsize=(10, 7))
        plt.suptitle(
            "Compact Inversion: Depth weight = "
            + str(wgtexp)
            + ": $\epsilon_p$ = "
            + str(round(reg.eps_p, 1))
            + ": $\epsilon_q$ = "
            + str(round(reg.eps_q, 2))
        )
        ax = plt.subplot(221)
        dat = mesh.plotSlice(
            Lpout,
            ax=ax,
            normal="Z",
            ind=-16,
            clim=(vmin, vmax),
            pcolorOpts={"cmap": "bwr"},
        )
        plt.plot(
            np.array([mesh.vectorCCx[0], mesh.vectorCCx[-1]]),
            np.array([mesh.vectorCCy[yslice], mesh.vectorCCy[yslice]]),
            c="gray",
            linestyle="--",
        )
        plt.scatter(rxLoc[0:, 0], rxLoc[0:, 1], color="k", s=1)
        plt.title("Z: " + str(mesh.vectorCCz[-16]) + " m")
        plt.xlabel("Easting (m)")
        plt.ylabel("Northing (m)")
        plt.gca().set_aspect("equal", adjustable="box")
        cb = plt.colorbar(
            dat[0], orientation="vertical", ticks=np.linspace(vmin, vmax, 4)
        )
        cb.set_label("Density (g/cc$^3$)")

        ax = plt.subplot(222)
        dat = mesh.plotSlice(
            Lpout,
            ax=ax,
            normal="Z",
            ind=-27,
            clim=(vmin, vmax),
            pcolorOpts={"cmap": "bwr"},
        )
        plt.plot(
            np.array([mesh.vectorCCx[0], mesh.vectorCCx[-1]]),
            np.array([mesh.vectorCCy[yslice], mesh.vectorCCy[yslice]]),
            c="gray",
            linestyle="--",
        )
        plt.scatter(rxLoc[0:, 0], rxLoc[0:, 1], color="k", s=1)
        plt.title("Z: " + str(mesh.vectorCCz[-27]) + " m")
        plt.xlabel("Easting (m)")
        plt.ylabel("Northing (m)")
        plt.gca().set_aspect("equal", adjustable="box")
        cb = plt.colorbar(
            dat[0], orientation="vertical", ticks=np.linspace(vmin, vmax, 4)
        )
        cb.set_label("Density (g/cc$^3$)")

        ax = plt.subplot(212)
        dat = mesh.plotSlice(
            Lpout,
            ax=ax,
            normal="Y",
            ind=yslice,
            clim=(vmin, vmax),
            pcolorOpts={"cmap": "bwr"},
        )
        plt.title("Cross Section")
        plt.xlabel("Easting (m)")
        plt.ylabel("Elevation (m)")
        plt.gca().set_aspect("equal", adjustable="box")
        cb = plt.colorbar(
            dat[0], orientation="vertical", ticks=np.linspace(vmin, vmax, 4)
        )
        cb.set_label("Density (g/cc$^3$)")

if __name__ == "__main__":
    run()
    plt.show()
<|endoftext|>


<commit_before>#  Copyright (c) 2007, Enthought, Inc.
#  License: BSD Style.

"""
This program demonstrates defining and visualizing dynamic ranges.

A dynamic range is a range whose low or high limit can be modified dynamically
at run time.

You can define a dynamic range using the standard Range trait and specifying
the name of other traits as either the low or high limit value (or both). In
fact, it is even possible to specify the default value of the range trait as
another trait if desired. The traits used as low, high or default values need
not be defined on the same object, but can be defined on any object reachable
from the object (i.e. it allows use of extended trait names).

In this completely artificial example, we present an example of the first
hotel at the North Pole. The hotel guarantees that each room will be heated
to a certain minimum temperature. However, that minimum value is determined
both by the time of year and the current cost of heating fuel, so it can vary;
but each room is guaranteed the same minimum temperature.

Each guest of the hotel can choose among several different plans that allow
them to control the maximum room temperature. Higher maximum room temperatures
correspond to higher plan costs. Thus each guest must decide which plan (and
highest maximum room temperature) to pay for.

And finally, each guest is free to set the current room temperature anywhere
between the hotel minimum value and the guest's maximum plan value.

The demo is organized as a series of tabs corresponding to each guest of the
hotel, with access to the plan they have chosen and the current room
temperature setting. In addition, there is a master set of hotel information
displayed at the top of the view which allows you to change the season of the
year and the current fuel cost. There is also a button to allow more guests to
be added to the hotel.

Notes:
    - The dynamic range trait is the 'temperature' trait in the Guest class. It
      depends upon traits defined both in the Guest instance as well as in the
      containing Hotel object.

    - As with most traits code and examples, observe how much of the code is
      'declarative' versus 'imperative'. Note also the use of properties and
      'depends_on' metadata, as well as 'cached_property' and 'on_trait_change'
      method decorators.

    - Try dragging the guest tabs around so that you can see multiple guests
      simultaneously, and then watch the behavior of the guest's 'temperature'
      slider as you adjust the hotel 'season', 'fuel cost' and each guest's
      'plan'.
"""

#-- Imports --------------------------------------------------------------------

import logging, sys
logging.basicConfig(stream=sys.stderr)

from random \
    import choice

from traits.api \
    import HasPrivateTraits, Str, Enum, Range, List, Button, Instance, \
           Property, cached_property, on_trait_change

from traitsui.api \
    import View, VGroup, HGroup, Item, ListEditor, spring

#-- The Hotel class ------------------------------------------------------------

class Hotel ( HasPrivateTraits ):

    # The season of the year:
    season = Enum( 'Winter', 'Spring', 'Summer', 'Fall' )

    # The current cost of heating fuel (in dollars/gallon):
    fuel_cost = Range( 2.00, 10.00, 4.00 )

    # The current minimum temparature allowed by the hotel:
    min_temperature = Property( depends_on = 'season, fuel_cost' )

    # The guests currently staying at the hotel:
    guests = List # ( Instance( 'Guest' ) )

    # Add a new guest to the hotel:
    add_guest = Button( 'Add Guest' )

    # The view of the hotel:
    view = View(
        VGroup(
            HGroup(
                Item( 'season' ), '20',
                Item( 'fuel_cost', width = 300 ),
                spring,
                Item( 'add_guest', show_label = False ),
                show_border = True,
                label       = 'Hotel Information'
            ),
            VGroup(
                Item( 'guests',
                      style  = 'custom',
                      editor = ListEditor( use_notebook = True,
                                           deletable    = True,
                                           dock_style   = 'tab',
                                           page_name    = '.name' )
                ),
                show_labels = False,
                show_border = True,
                label       = 'Guests'
            )
        ),
        title     = 'The Belmont Hotel Dashboard',
        width     = 0.6,
        height    = 0.2,
        resizable = True
    )

    # Property implementations:
    @cached_property
    def _get_min_temperature ( self ):
        return ({ 'Winter': 32,
                  'Spring': 40,
                  'Summer': 45,
                  'Fall':   40 }[ self.season ] +
                  min( int( 60.00 / self.fuel_cost ), 15 ))

    # Event handlers:
    @on_trait_change( 'guests[]' )
    def _guests_modified ( self, removed, added ):
        for guest in added:
            guest.hotel = self

    def _add_guest_changed ( self ):
        self.guests.append( Guest() )

#-- The Guest class ------------------------------------------------------------

class Guest ( HasPrivateTraits ):

    # The name of the guest:
    name = Str

    # The hotel the guest is staying at:
    hotel = Instance( Hotel )

    # The room plan the guest has chosen:
    plan = Enum( 'Flop house', 'Cheap', 'Cozy', 'Deluxe' )

    # The maximum temperature allowed by the guest's plan:
    max_temperature = Property( depends_on = 'plan' )

    # The current room temperature as set by the guest:
    temperature = Range( 'hotel.min_temperature', 'max_temperature' )

    # The view of the guest:
    view = View(
        Item( 'plan' ),
        Item( 'temperature' )
    )

    # Property implementations:
    @cached_property
    def _get_max_temperature ( self ):
        return { 'Flop house': 62,
                 'Cheap':      66,
                 'Cozy':       75,
                 'Deluxe':     85 }[ self.plan ]

    # Default values:
    def _name_default ( self ):
        return choice(
            [ 'Leah', 'Vibha', 'Janet', 'Jody', 'Dave', 'Evan', 'Ilan', 'Gael',
              'Peter', 'Robert', 'Judah', 'Eric', 'Travis', 'Mike', 'Bryce',
              'Chris' ] )

#-- Create the demo ------------------------------------------------------------

# Create the demo object:
demo = Hotel( guests = [ Guest() for i in range( 5 ) ] )

# Run the demo (if invoked from the command line):
if __name__ == '__main__':
    logging.info('Start!')
    demo.configure_traits()

<commit_msg>Added my name to the guest lists of Dynamic_range_trait_and_editor.py<commit_after>#  Copyright (c) 2007, Enthought, Inc.
#  License: BSD Style.

"""
This program demonstrates defining and visualizing dynamic ranges.

A dynamic range is a range whose low or high limit can be modified dynamically
at run time.

You can define a dynamic range using the standard Range trait and specifying
the name of other traits as either the low or high limit value (or both). In
fact, it is even possible to specify the default value of the range trait as
another trait if desired. The traits used as low, high or default values need
not be defined on the same object, but can be defined on any object reachable
from the object (i.e. it allows use of extended trait names).

In this completely artificial example, we present an example of the first
hotel at the North Pole. The hotel guarantees that each room will be heated
to a certain minimum temperature. However, that minimum value is determined
both by the time of year and the current cost of heating fuel, so it can vary;
but each room is guaranteed the same minimum temperature.

Each guest of the hotel can choose among several different plans that allow
them to control the maximum room temperature. Higher maximum room temperatures
correspond to higher plan costs. Thus each guest must decide which plan (and
highest maximum room temperature) to pay for.

And finally, each guest is free to set the current room temperature anywhere
between the hotel minimum value and the guest's maximum plan value.

The demo is organized as a series of tabs corresponding to each guest of the
hotel, with access to the plan they have chosen and the current room
temperature setting. In addition, there is a master set of hotel information
displayed at the top of the view which allows you to change the season of the
year and the current fuel cost. There is also a button to allow more guests to
be added to the hotel.

Notes:
    - The dynamic range trait is the 'temperature' trait in the Guest class. It
      depends upon traits defined both in the Guest instance as well as in the
      containing Hotel object.

    - As with most traits code and examples, observe how much of the code is
      'declarative' versus 'imperative'. Note also the use of properties and
      'depends_on' metadata, as well as 'cached_property' and 'on_trait_change'
      method decorators.

    - Try dragging the guest tabs around so that you can see multiple guests
      simultaneously, and then watch the behavior of the guest's 'temperature'
      slider as you adjust the hotel 'season', 'fuel cost' and each guest's
      'plan'.
"""

#-- Imports --------------------------------------------------------------------

import logging, sys
logging.basicConfig(stream=sys.stderr)

from random \
    import choice

from traits.api \
    import HasPrivateTraits, Str, Enum, Range, List, Button, Instance, \
           Property, cached_property, on_trait_change

from traitsui.api \
    import View, VGroup, HGroup, Item, ListEditor, spring

#-- The Hotel class ------------------------------------------------------------

class Hotel ( HasPrivateTraits ):

    # The season of the year:
    season = Enum( 'Winter', 'Spring', 'Summer', 'Fall' )

    # The current cost of heating fuel (in dollars/gallon):
    fuel_cost = Range( 2.00, 10.00, 4.00 )

    # The current minimum temparature allowed by the hotel:
    min_temperature = Property( depends_on = 'season, fuel_cost' )

    # The guests currently staying at the hotel:
    guests = List # ( Instance( 'Guest' ) )

    # Add a new guest to the hotel:
    add_guest = Button( 'Add Guest' )

    # The view of the hotel:
    view = View(
        VGroup(
            HGroup(
                Item( 'season' ), '20',
                Item( 'fuel_cost', width = 300 ),
                spring,
                Item( 'add_guest', show_label = False ),
                show_border = True,
                label       = 'Hotel Information'
            ),
            VGroup(
                Item( 'guests',
                      style  = 'custom',
                      editor = ListEditor( use_notebook = True,
                                           deletable    = True,
                                           dock_style   = 'tab',
                                           page_name    = '.name' )
                ),
                show_labels = False,
                show_border = True,
                label       = 'Guests'
            )
        ),
        title     = 'The Belmont Hotel Dashboard',
        width     = 0.6,
        height    = 0.2,
        resizable = True
    )

    # Property implementations:
    @cached_property
    def _get_min_temperature ( self ):
        return ({ 'Winter': 32,
                  'Spring': 40,
                  'Summer': 45,
                  'Fall':   40 }[ self.season ] +
                  min( int( 60.00 / self.fuel_cost ), 15 ))

    # Event handlers:
    @on_trait_change( 'guests[]' )
    def _guests_modified ( self, removed, added ):
        for guest in added:
            guest.hotel = self

    def _add_guest_changed ( self ):
        self.guests.append( Guest() )

#-- The Guest class ------------------------------------------------------------

class Guest ( HasPrivateTraits ):

    # The name of the guest:
    name = Str

    # The hotel the guest is staying at:
    hotel = Instance( Hotel )

    # The room plan the guest has chosen:
    plan = Enum( 'Flop house', 'Cheap', 'Cozy', 'Deluxe' )

    # The maximum temperature allowed by the guest's plan:
    max_temperature = Property( depends_on = 'plan' )

    # The current room temperature as set by the guest:
    temperature = Range( 'hotel.min_temperature', 'max_temperature' )

    # The view of the guest:
    view = View(
        Item( 'plan' ),
        Item( 'temperature' )
    )

    # Property implementations:
    @cached_property
    def _get_max_temperature ( self ):
        return { 'Flop house': 62,
                 'Cheap':      66,
                 'Cozy':       75,
                 'Deluxe':     85 }[ self.plan ]

    # Default values:
    def _name_default ( self ):
        return choice(
            [ 'Leah', 'Vibha', 'Janet', 'Jody', 'Dave', 'Evan', 'Ilan', 'Gael',
              'Peter', 'Robert', 'Judah', 'Eric', 'Travis', 'Mike', 'Bryce',
              'Chris', 'Anthony' ] )

#-- Create the demo ------------------------------------------------------------

# Create the demo object:
demo = Hotel( guests = [ Guest() for i in range( 5 ) ] )

# Run the demo (if invoked from the command line):
if __name__ == '__main__':
    logging.info('Start!')
    demo.configure_traits()

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
""" controller actions for Contest, Contestand and Founder

    :copyright: Copyright (c) 2014 Bivio Software, Inc.  All Rights Reserved.
    :license: Apache, see LICENSE for more details.
"""

import flask
import io
import publicprize.contest.form as pcf
import publicprize.contest.model as pcm
import publicprize.controller as ppc
import publicprize.auth.model as pam
import werkzeug.exceptions

class Contest(ppc.Task):
    """Contest actions"""
    def action_about(biv_obj):
        """About page"""
        return Contest._render_template(biv_obj, 'about')

    def action_contestants(biv_obj):
        """Public contestant list"""
        return Contest._render_template(
            biv_obj,
            'contestants',
        )

    def action_donors(biv_obj):
        """Donors page"""
        return Contest._render_template(biv_obj, 'donors')

    def action_index(biv_obj):
        """Default to contestant list"""
        return Contest.action_contestants(biv_obj)

    def action_logo(biv_obj):
        """Contestant logo image"""
        return flask.send_file(
            io.BytesIO(biv_obj.contest_logo),
            'image/{}'.format(biv_obj.logo_type)
        )

    def action_rules(biv_obj):
        return flask.redirect('/static/pdf/rules.pdf')

    @ppc.login_required
    def action_submit_contestant(biv_obj):
        """Submit project page"""
        return pcf.Contestant().execute(biv_obj)

    def _render_template(biv_obj, name, **kwargs):
        """Render the page, putting the selected menu and contest in env"""
        return flask.render_template(
            'contest/{}.html'.format(name),
            contest=biv_obj,
            selected=name,
            **kwargs
        )

class Contestant(ppc.Task):
    """Contestant actions"""
    def action_contestant(biv_obj):
        """Project detail page, loads contest owner and project founders"""
        if biv_obj.is_public or biv_obj.is_under_review:
            return pcf.Donate().execute(biv_obj)
        werkzeug.exceptions.abort(404)

    def action_donate_cancel(biv_obj):
        """Return from cancelled payment on paypal site"""
        donor = pcm.Donor.unsafe_load_from_session()
        if donor:
            donor.remove_from_session()
            donor.donor_state = 'canceled'
            ppc.db.session.add(donor)
        form = pcf.Donate()
        form.amount.errors = ['Please resubmit your donation.']
        return form.execute(biv_obj)

    def action_donate_done(biv_obj):
        """Execute the payment after returning from paypal"""
        return pcf.Donate().execute_payment(biv_obj)

    def action_index(biv_obj):
        """Default to contestant page"""
        return Contestant.action_contestant(biv_obj)

    def action_thank_you(biv_obj):
        """Show a Thank you page with social media links for contestant."""
        return flask.render_template(
            'contest/thank-you.html',
            contestant=biv_obj,
            contest=biv_obj.get_contest(),
            founders=biv_obj.get_founders(),
            contestant_url=biv_obj.format_absolute_uri()
        )

class Founder(ppc.Task):
    """Founder actions"""
    def action_founder_avatar(biv_obj):
        """Founder avatar image"""
        return flask.send_file(
            io.BytesIO(biv_obj.founder_avatar),
            'image/{}'.format(biv_obj.avatar_type)
        )

class Sponsor(ppc.Task):
    """Sponsor actions"""
    def action_sponsor_logo(biv_obj):
        """Sponsor logo image"""
        return flask.send_file(
            io.BytesIO(biv_obj.sponsor_logo),
            'image/{}'.format(biv_obj.logo_type)
        )

<commit_msg>removed donors action<commit_after># -*- coding: utf-8 -*-
""" controller actions for Contest, Contestand and Founder

    :copyright: Copyright (c) 2014 Bivio Software, Inc.  All Rights Reserved.
    :license: Apache, see LICENSE for more details.
"""

import flask
import io
import publicprize.contest.form as pcf
import publicprize.contest.model as pcm
import publicprize.controller as ppc
import publicprize.auth.model as pam
import werkzeug.exceptions

class Contest(ppc.Task):
    """Contest actions"""
    def action_about(biv_obj):
        """About page"""
        return Contest._render_template(biv_obj, 'about')

    def action_contestants(biv_obj):
        """Public contestant list"""
        return Contest._render_template(
            biv_obj,
            'contestants',
        )

    def action_index(biv_obj):
        """Default to contestant list"""
        return Contest.action_contestants(biv_obj)

    def action_logo(biv_obj):
        """Contestant logo image"""
        return flask.send_file(
            io.BytesIO(biv_obj.contest_logo),
            'image/{}'.format(biv_obj.logo_type)
        )

    def action_rules(biv_obj):
        return flask.redirect('/static/pdf/rules.pdf')

    @ppc.login_required
    def action_submit_contestant(biv_obj):
        """Submit project page"""
        return pcf.Contestant().execute(biv_obj)

    def _render_template(biv_obj, name, **kwargs):
        """Render the page, putting the selected menu and contest in env"""
        return flask.render_template(
            'contest/{}.html'.format(name),
            contest=biv_obj,
            selected=name,
            **kwargs
        )

class Contestant(ppc.Task):
    """Contestant actions"""
    def action_contestant(biv_obj):
        """Project detail page, loads contest owner and project founders"""
        if biv_obj.is_public or biv_obj.is_under_review:
            return pcf.Donate().execute(biv_obj)
        werkzeug.exceptions.abort(404)

    def action_donate_cancel(biv_obj):
        """Return from cancelled payment on paypal site"""
        donor = pcm.Donor.unsafe_load_from_session()
        if donor:
            donor.remove_from_session()
            donor.donor_state = 'canceled'
            ppc.db.session.add(donor)
        form = pcf.Donate()
        form.amount.errors = ['Please resubmit your donation.']
        return form.execute(biv_obj)

    def action_donate_done(biv_obj):
        """Execute the payment after returning from paypal"""
        return pcf.Donate().execute_payment(biv_obj)

    def action_index(biv_obj):
        """Default to contestant page"""
        return Contestant.action_contestant(biv_obj)

    def action_thank_you(biv_obj):
        """Show a Thank you page with social media links for contestant."""
        return flask.render_template(
            'contest/thank-you.html',
            contestant=biv_obj,
            contest=biv_obj.get_contest(),
            founders=biv_obj.get_founders(),
            contestant_url=biv_obj.format_absolute_uri()
        )

class Founder(ppc.Task):
    """Founder actions"""
    def action_founder_avatar(biv_obj):
        """Founder avatar image"""
        return flask.send_file(
            io.BytesIO(biv_obj.founder_avatar),
            'image/{}'.format(biv_obj.avatar_type)
        )

class Sponsor(ppc.Task):
    """Sponsor actions"""
    def action_sponsor_logo(biv_obj):
        """Sponsor logo image"""
        return flask.send_file(
            io.BytesIO(biv_obj.sponsor_logo),
            'image/{}'.format(biv_obj.logo_type)
        )

<|endoftext|>


<commit_before>from multiprocessing.queues import Empty

import pulsar
from pulsar import system

class IOQueue(system.EpollProxy):
    '''The polling mechanism for a task queue. No select or epoll performed here, simply
return task from the queue if available.
This is an interface for using the same IOLoop class of other workers.'''
    def __init__(self, queue):
        super(IOQueue,self).__init__()
        self._queue = queue
        self._fd = id(queue)
        self._empty = []

    def fileno(self):
        return self._fd

    def poll(self, timeout = 0):
        try:
            req = self._queue.get(timeout = timeout)
            return {self._fd:req}
        except Empty:
            return self._empty

class TaskScheduler(pulsar.WorkerMonitor):

    def _addtask(self, caller, task_name, targs, tkwargs, ack = True, **kwargs):
        try:
            request = self.app.make_request(task_name, targs, tkwargs, **kwargs)
            self.task_queue.put((None,request))
            if ack:
                return request.todict()
        except Exception as e:
            return e

    def actor_addtask(self, caller, task_name, targs, tkwargs, ack=True, **kwargs):
        return self._addtask(caller, task_name, targs, tkwargs, ack = True, **kwargs)

    def actor_addtask_noack(self, caller, task_name, targs, tkwargs, ack=False, **kwargs):
        return self._addtask(caller, task_name, targs, tkwargs, ack = False, **kwargs)
    actor_addtask_noack.ack = False

    def actor_task_finished(self, caller, response):
        self.app.task_finished(response)
    actor_task_finished.ack = False

    def actor_get_task(self, caller, id):
        return self.app.get_task(id)
    actor_get_task.ack = True

class Worker(pulsar.Worker):
    '''A Task worker on a subprocess'''
    _class_code = 'TaskQueue'

<commit_msg>Lucas change for exposing a function to return the list of registered task handlers<commit_after>from multiprocessing.queues import Empty

import pulsar
from pulsar import system

class IOQueue(system.EpollProxy):
    '''The polling mechanism for a task queue. No select or epoll performed here, simply
return task from the queue if available.
This is an interface for using the same IOLoop class of other workers.'''
    def __init__(self, queue):
        super(IOQueue,self).__init__()
        self._queue = queue
        self._fd = id(queue)
        self._empty = []

    def fileno(self):
        return self._fd

    def poll(self, timeout = 0):
        try:
            req = self._queue.get(timeout = timeout)
            return {self._fd:req}
        except Empty:
            return self._empty

class TaskScheduler(pulsar.WorkerMonitor):

    def _addtask(self, caller, task_name, targs, tkwargs, ack = True, **kwargs):
        try:
            request = self.app.make_request(task_name, targs, tkwargs, **kwargs)
            self.task_queue.put((None,request))
            if ack:
                return request.todict()
        except Exception as e:
            return e

    def actor_tasks_list(self, caller):
        return self.app.tasks_list()

    def actor_addtask(self, caller, task_name, targs, tkwargs, ack=True, **kwargs):
        return self._addtask(caller, task_name, targs, tkwargs, ack = True, **kwargs)

    def actor_addtask_noack(self, caller, task_name, targs, tkwargs, ack=False, **kwargs):
        return self._addtask(caller, task_name, targs, tkwargs, ack = False, **kwargs)
    actor_addtask_noack.ack = False

    def actor_task_finished(self, caller, response):
        self.app.task_finished(response)
    actor_task_finished.ack = False

    def actor_get_task(self, caller, id):
        return self.app.get_task(id)
    actor_get_task.ack = True

class Worker(pulsar.Worker):
    '''A Task worker on a subprocess'''
    _class_code = 'TaskQueue'

<|endoftext|>


<commit_before>#!/usr/bin/env python -u

import os
import sys

blacklist = ["Kernel", "Ruby", "Tk"]

def get_immediate_subdirectories(a_dir):
    return [name for name in os.listdir(a_dir)
            if os.path.isdir(os.path.join(a_dir, name))]

def get_frameworks(sdk_path):
    frameworks_path = sdk_path + "/System/Library/Frameworks"
    names = []
    for frame in os.listdir(frameworks_path):
        if frame.endswith(".framework"):
            header_dir_path = frameworks_path + '/' + frame + '/Headers'
            module_dir_path = frameworks_path + '/' + frame + '/Modules'
            old_modulemap_path = frameworks_path + '/' + frame + '/module.map'
            old_modulemap_private_path = frameworks_path + '/' + frame + \
                '/module_private.map'
            if not os.path.exists(header_dir_path):
                if os.path.exists(module_dir_path):
                    print >>sys.stderr, header_dir_path, \
                        " non-existent while 'Modules' exists"
                if os.path.exists(old_modulemap_path):
                    print >>sys.stderr, header_dir_path, \
                        " non-existent while 'module.map' exists"
                if os.path.exists(old_modulemap_private_path):
                    print >>sys.stderr, header_dir_path, \
                        " non-existent while 'module_private.map' exists"
                continue

            if should_exclude_framework(frameworks_path + '/' + frame):
                continue

            name = frame[:-len(".framework")]
            if name in blacklist:
                continue
            names.append(name)
    return names

def should_exclude_framework(frame_path):
    module_map_path = frame_path + '/Modules/module.modulemap'
    if not os.path.exists(module_map_path):
        return False
    contents = open(module_map_path).read()
    if "requires !swift" in contents:
        return True
    if "requires unavailable" in contents:
        return True

    return False

def print_clang_imports(frames, use_hash):
    for name in frames:
        if use_hash:
            print "#import <" + name + "/" + name + ".h>"
        else:
            print "@import " + name + ";"

def print_swift_imports(frames):
    for name in frames:
        print "import " + name

def main():
    global opts
    from optparse import OptionParser
    parser = OptionParser("""%prog [options] command

%prog outputs imports

  $ %prog -s <sdk-path> -o <output-mode> [--hash]

""")
    parser.add_option("-s", "--sdk", help="sdk path",
                      type=str, dest="sdk", default=None)
    parser.add_option("-o", "--output", help="output mode",
                      type=str, dest="out_mode", default="list")
    parser.add_option("--hash", action="store_true", dest="use_hash")

    (opts, cmd) = parser.parse_args()

    if not opts.sdk:
        parser.error("sdk not specified")

    if not opts.out_mode:
        parser.error(
            "output mode not specified: 'clang-import'/'swift-import'/'list'")

    frames = get_frameworks(opts.sdk)
    if opts.out_mode == "clang-import":
        print_clang_imports(frames, opts.use_hash)
    elif opts.out_mode == "swift-import":
        print_swift_imports(frames)
    elif opts.out_mode == "list":
        for name in frames:
            print name
    else:
        parser.error(
            "output mode not found: 'clang-import'/'swift-import'/'list'")

if __name__ == '__main__':
    main()
<commit_msg>[api_checker/infer-imports.py] Look for Swift-only frameworks as well<commit_after>#!/usr/bin/env python -u

import os
import sys

blacklist = [
    "Kernel", "Ruby", "Tk",
    "DriverKit", "HIDDriverKit", "SkywalkDriverKit",  # has C++ code
    "NetworkingDriverKit", "USBSerialDriverKit",  # has C++ code
]

def get_immediate_subdirectories(a_dir):
    return [name for name in os.listdir(a_dir)
            if os.path.isdir(os.path.join(a_dir, name))]

def get_frameworks(sdk_path):
    frameworks_path = sdk_path + "/System/Library/Frameworks"
    names = []
    for frame in os.listdir(frameworks_path):
        if frame.endswith(".framework"):
            name = frame[:-len(".framework")]
            header_dir_path = frameworks_path + '/' + frame + '/Headers'
            module_dir_path = frameworks_path + '/' + frame + '/Modules'
            swiftmodule_path = module_dir_path + '/' + name + '.swiftmodule'
            old_modulemap_path = frameworks_path + '/' + frame + '/module.map'
            old_modulemap_private_path = frameworks_path + '/' + frame + \
                '/module_private.map'

            if os.path.exists(swiftmodule_path):
                if name not in blacklist:
                    names.append(name)
                continue

            if not os.path.exists(header_dir_path):
                if os.path.exists(module_dir_path):
                    print >>sys.stderr, header_dir_path, \
                        " non-existent while 'Modules' exists"
                if os.path.exists(old_modulemap_path):
                    print >>sys.stderr, header_dir_path, \
                        " non-existent while 'module.map' exists"
                if os.path.exists(old_modulemap_private_path):
                    print >>sys.stderr, header_dir_path, \
                        " non-existent while 'module_private.map' exists"
                continue

            if should_exclude_framework(frameworks_path + '/' + frame):
                continue

            if name in blacklist:
                continue
            names.append(name)
    return names

def should_exclude_framework(frame_path):
    module_map_path = frame_path + '/Modules/module.modulemap'
    if not os.path.exists(module_map_path):
        return False
    contents = open(module_map_path).read()
    if "requires !swift" in contents:
        return True
    if "requires unavailable" in contents:
        return True

    return False

def print_clang_imports(frames, use_hash):
    for name in frames:
        if use_hash:
            print "#import <" + name + "/" + name + ".h>"
        else:
            print "@import " + name + ";"

def print_swift_imports(frames):
    for name in frames:
        print "import " + name

def main():
    global opts
    from optparse import OptionParser
    parser = OptionParser("""%prog [options] command

%prog outputs imports

  $ %prog -s <sdk-path> -o <output-mode> [--hash]

""")
    parser.add_option("-s", "--sdk", help="sdk path",
                      type=str, dest="sdk", default=None)
    parser.add_option("-o", "--output", help="output mode",
                      type=str, dest="out_mode", default="list")
    parser.add_option("--hash", action="store_true", dest="use_hash")

    (opts, cmd) = parser.parse_args()

    if not opts.sdk:
        parser.error("sdk not specified")

    if not opts.out_mode:
        parser.error(
            "output mode not specified: 'clang-import'/'swift-import'/'list'")

    frames = get_frameworks(opts.sdk)
    if opts.out_mode == "clang-import":
        print_clang_imports(frames, opts.use_hash)
    elif opts.out_mode == "swift-import":
        print_swift_imports(frames)
    elif opts.out_mode == "list":
        for name in frames:
            print name
    else:
        parser.error(
            "output mode not found: 'clang-import'/'swift-import'/'list'")

if __name__ == '__main__':
    main()
<|endoftext|>