<commit_before>"""
Atlas class definitions
"""

# Author: John Griffiths
# License: simplified BSD

import os

from nilearn.image import index_img

from utils.readers import (load_connectivity,load_vol_file_mappings,load_vol_bboxes,
                           load_stream_file_mappings,load_stream_bboxes,
                           make_nx_graph)

from utils.stats import (compute_vol_hit_stats,compute_vol_scalar_stats,
                         compute_stream_hit_stats,compute_stream_scalar_stats,
                         hit_stats_to_nx)

#from viz.network import plot_network,plot_matrix
#from viz.volume import plot_vol_cnxn,plot_vol_tract
#from viz.streams import plot_stream_cnxn,plot_stream_tract

class _Atlas(): # object):
  """
  Base atlas class
  """

  def __init__(self):

    print 'blah'

class _VolAtlas(_Atlas):
  """
  Volumetric atlas class
  """

  def __init__(self,atlas_name):

    self.atlas_name = atlas_name

    # Load volumetric atlas info

    self.vfms,self.atlas_dir = load_vol_file_mappings(atlas_name=atlas_name)
    self.bbox = load_vol_bboxes(atlas_name=atlas_name)

  def get_vol_from_vfm(self,idx):
    """
    Convenience method to return nifti volume for an entry in the vfm table
    """

    fname = self.vfms.ix[idx]['nii_file']
    vol = self.vfms.ix[idx]['4dvolind']

    if not os.path.isfile(fname):

      candidate = os.path.join(self.atlas_dir,fname)
      if os.path.isfile(candidate):
        fname = candidate
      else:
          Exception('File not found')

    if os.path.isfile(fname):
      print 'getting atlas entry %s: volume %s from image file %s'  %(idx,vol,fname)
    img = index_img(fname,vol)

    return img

  def compute_hit_stats(self,roi,idxs,n_jobs=1,run_type='simple'):
    """
    Compute hit stats and store inside this object
    under 'name'
    """

    df_hit_stats = compute_vol_hit_stats(roi,self.vfms,self.bbox,
                                      idxs,n_jobs=n_jobs,
                                      atlas_name=self.atlas_name)

    #G_hit_stats = hit_stats_to_nx(df_hit_stats,self.Gnx,self.vfms)
    #return df_hit_stats,G_hit_stats
    return df_hit_stats

  def compute_scalar_stats(self,params,name):
    """
    Compute scalar stats and store inside this
    object under 'name'
    """

    res = compute_vol_scalar_stats(params)

    self.scalar_stats[name] = res

class VolTractAtlas(_VolAtlas):
  """
  Volumetric tract-based atlas base class
  """

  def __init__(self,atlas_name):

    _VolAtlas.__init__(self, atlas_name)

  def compute_hit_stats(self,roi,idxs,n_jobs=1,run_type='simple'):
    """
    Compute hit stats and store inside this object
      under 'name'
    """

    df_hit_stats = compute_vol_hit_stats(roi,self.vfms,self.bbox,
                                      idxs,n_jobs=n_jobs,
                                      atlas_name=self.atlas_name)
    return df_hit_stats

  def plot_tract(self,atlas_name):

    # (uses plotting param defaults tuned to JHU atlas)
    plot_tract()

class VolConnAtlas(_VolAtlas):
  """
  Volumetric connectivity-based atlas
  """

  def __init__(self,atlas_name):

    _VolAtlas.__init__(self, atlas_name)

    # Load connectivity info

    ws,rls,tls,rxyzs,rnii,ctx,hs,rmfslh,rmfsrh = load_connectivity(atlas_name)

    self.weights = ws
    self.region_labels = rls

    if tls is not None: self.tract_lengths = tls
    if rxyzs is not None: self.region_xyzs = rxyzs
    if rnii is not None: self.region_nii = rnii
    if ctx is not None: self.cortex = ctx
    if hs is not None: self.hemispheres = hs
    if rmfslh is not None: self.region_mapping_fsav_lh = rmfslh
    if rmfsrh is not None: self.region_mapping_fsav_rh = rmfsrh

    # Compile node and connectivity info into a networkx graph
    self.Gnx = make_nx_graph(self.vfms,self.bbox,ws,rls,hs,ctx)

  def compute_hit_stats(self,roi,idxs,n_jobs=1,run_type='simple'):
    """
    Compute hit stats and store inside this object
      under 'name'
    """

    df_hit_stats = compute_vol_hit_stats(roi,self.vfms,self.bbox,
                                      idxs,n_jobs=n_jobs,
                                      atlas_name=self.atlas_name)

    G_hit_stats = hit_stats_to_nx(df_hit_stats,self.Gnx,self.vfms)

    return df_hit_stats,G_hit_stats

  def get_vol_from_rois(self,roi1,roi2):

    g = self.Gnx[int(roi1)][int(roi2)]
    idx = g['idx']

    vfm = self.vfms.ix[idx]
    nii_file = vfm['nii_file']
    volnum = vfm['4dvolind']

    if not os.path.isfile(nii_file):
      candidate = os.path.join(self.atlas_dir,nii_file)
      if os.path.isfile(candidate):
        nii_file = candidate
      else:
        Exception('file not found')

    if os.path.isfile(nii_file): img = index_img(nii_file,volnum)

    return img

  def modify_connectome(self, name='mc1',function=''):
    """
    Modify canonical connectome using hit or scalar
    stats, and store in this obj
    """

    res = modify_vol_connectome()

    self.modcons[name] = res

  def plot_network(self):

    plot_network()

  def plot_matrix(self):

    plot_matrix()

  def nl_plot_cnxn_and_rois(self,roi1=None,roi2=None,idx=None):

    if roi1 and roi2:
      img = self.get_vol_from_rois(roi1,roi2)
    elif idx:
      img = self.get_vol_from_idx(idx)

class _StreamAtlas(_Atlas):
  """
  Streamlinetric atlas base class
  """

  def __init__(self,atlas_name):

    # Load streamlinetric atlas info

    self.sfms = load_stream_file_mappings(atlas_name)
    self.bbox = load_stream_bboxes(atlas_name)

class StreamTractAtlas(_StreamAtlas):
  """
  Streamlinetric tract-based atlas base class
  """

  def __init__(self,atlas_name):

    _StreamAtlas.__init__(self, atlas_name)

class StreamConnAtlas(_StreamAtlas):
  """
  Streamlinetric connectome-based atlas base class
  """

  def __init__(self,atlas_name):

    _StreamAtlas.__init__(self, atlas_name)

    # Load connectivity info
    self.G = load_connectivity(atlas_name)

  def modify_connectome(self, name='mc1',function=''):
    """
    Modify canonical connectome using hit or scalar
    stats, and store in this obj
    """

    res = modify_stream_connectome()

    self.modcons[name] = res

  def plot_network(self):

    plot_network()

  def plot_matrix(self):

    plot_matrix()

  def plot_cnxns(self):

    plot_stream_cnxns()

<commit_msg>added streamlinetric atlas parser<commit_after>"""
Atlas class definitions
"""

# Author: John Griffiths
# License: simplified BSD

import os

from nilearn.image import index_img

from utils.readers import (load_connectivity,load_vol_file_mappings,load_vol_bboxes,
                           load_stream_file_mappings,load_stream_bboxes,
                           make_nx_graph)

from utils.stats import (compute_vol_hit_stats,compute_vol_scalar_stats,
                         compute_stream_hit_stats,compute_stream_scalar_stats,
                         hit_stats_to_nx)

#from viz.network import plot_network,plot_matrix
#from viz.volume import plot_vol_cnxn,plot_vol_tract
#from viz.streams import plot_stream_cnxn,plot_stream_tract

class _Atlas(): # object):
  """
  Base atlas class
  """

  def __init__(self):

    print 'blah'

class _VolAtlas(_Atlas):
  """
  Volumetric atlas class
  """

  def __init__(self,atlas_name):

    self.atlas_name = atlas_name

    # Load volumetric atlas info

    self.vfms,self.atlas_dir = load_vol_file_mappings(atlas_name=atlas_name)
    self.bbox = load_vol_bboxes(atlas_name=atlas_name)

  def get_vol_from_vfm(self,idx):
    """
    Convenience method to return nifti volume for an entry in the vfm table
    """

    fname = self.vfms.ix[idx]['nii_file']
    vol = self.vfms.ix[idx]['4dvolind']

    if not os.path.isfile(fname):

      candidate = os.path.join(self.atlas_dir,fname)
      if os.path.isfile(candidate):
        fname = candidate
      else:
          Exception('File not found')

    if os.path.isfile(fname):
      print 'getting atlas entry %s: volume %s from image file %s'  %(idx,vol,fname)
    img = index_img(fname,vol)

    return img

  def compute_hit_stats(self,roi,idxs,n_jobs=1,run_type='simple'):
    """
    Compute hit stats and store inside this object
    under 'name'
    """

    df_hit_stats = compute_vol_hit_stats(roi,self.vfms,self.bbox,
                                      idxs,n_jobs=n_jobs,
                                      atlas_name=self.atlas_name)

    #G_hit_stats = hit_stats_to_nx(df_hit_stats,self.Gnx,self.vfms)
    #return df_hit_stats,G_hit_stats
    return df_hit_stats

  def compute_scalar_stats(self,params,name):
    """
    Compute scalar stats and store inside this
    object under 'name'
    """

    res = compute_vol_scalar_stats(params)

    self.scalar_stats[name] = res

class VolTractAtlas(_VolAtlas):
  """
  Volumetric tract-based atlas base class
  """

  def __init__(self,atlas_name):

    _VolAtlas.__init__(self, atlas_name)

  def compute_hit_stats(self,roi,idxs,n_jobs=1,run_type='simple'):
    """
    Compute hit stats and store inside this object
      under 'name'
    """

    df_hit_stats = compute_vol_hit_stats(roi,self.vfms,self.bbox,
                                      idxs,n_jobs=n_jobs,
                                      atlas_name=self.atlas_name)
    return df_hit_stats

  def plot_tract(self,atlas_name):

    # (uses plotting param defaults tuned to JHU atlas)
    plot_tract()

class VolConnAtlas(_VolAtlas):
  """
  Volumetric connectivity-based atlas
  """

  def __init__(self,atlas_name):

    _VolAtlas.__init__(self, atlas_name)

    # Load connectivity info

    ws,rls,tls,rxyzs,rnii,ctx,hs,rmfslh,rmfsrh = load_connectivity(atlas_name)

    self.weights = ws
    self.region_labels = rls

    if tls is not None: self.tract_lengths = tls
    if rxyzs is not None: self.region_xyzs = rxyzs
    if rnii is not None: self.region_nii = rnii
    if ctx is not None: self.cortex = ctx
    if hs is not None: self.hemispheres = hs
    if rmfslh is not None: self.region_mapping_fsav_lh = rmfslh
    if rmfsrh is not None: self.region_mapping_fsav_rh = rmfsrh

    # Compile node and connectivity info into a networkx graph
    self.Gnx = make_nx_graph(self.vfms,self.bbox,ws,rls,hs,ctx)

  def compute_hit_stats(self,roi,idxs,n_jobs=1,run_type='simple'):
    """
    Compute hit stats and store inside this object
      under 'name'
    """

    df_hit_stats = compute_vol_hit_stats(roi,self.vfms,self.bbox,
                                      idxs,n_jobs=n_jobs,
                                      atlas_name=self.atlas_name)

    G_hit_stats = hit_stats_to_nx(df_hit_stats,self.Gnx,self.vfms)

    return df_hit_stats,G_hit_stats

  def get_vol_from_rois(self,roi1,roi2):

    g = self.Gnx[int(roi1)][int(roi2)]
    idx = g['idx']

    vfm = self.vfms.ix[idx]
    nii_file = vfm['nii_file']
    volnum = vfm['4dvolind']

    if not os.path.isfile(nii_file):
      candidate = os.path.join(self.atlas_dir,nii_file)
      if os.path.isfile(candidate):
        nii_file = candidate
      else:
        Exception('file not found')

    if os.path.isfile(nii_file): img = index_img(nii_file,volnum)

    return img

  def modify_connectome(self, name='mc1',function=''):
    """
    Modify canonical connectome using hit or scalar
    stats, and store in this obj
    """

    res = modify_vol_connectome()

    self.modcons[name] = res

  def plot_network(self):

    plot_network()

  def plot_matrix(self):

    plot_matrix()

  def nl_plot_cnxn_and_rois(self,roi1=None,roi2=None,idx=None):

    if roi1 and roi2:
      img = self.get_vol_from_rois(roi1,roi2)
    elif idx:
      img = self.get_vol_from_idx(idx)

class _StreamAtlas(_Atlas):
  """
  Streamlinetric atlas base class
  """

  def __init__(self,atlas_name):

    self.atlas_name = atlas_name

    # Load streamlinetric atlas info

    self.sfms,self.atlas_dir = load_stream_file_mappings(atlas_name=atlas_name)
    self.bbox = load_stream_bboxes(atlas_name=atlas_name)

class StreamTractAtlas(_StreamAtlas):
  """
  Streamlinetric tract-based atlas base class
  """

  def __init__(self,atlas_name):

    _StreamAtlas.__init__(self, atlas_name)

class StreamConnAtlas(_StreamAtlas):
  """
  Streamlinetric connectome-based atlas base class
  """

  def __init__(self,atlas_name):
    # A lot of this is shared between StreamConn atlas and VolConn atlas.
    # modify...

    _StreamAtlas.__init__(self, atlas_name)

    # Load connectivity info

    ws,rls,tls,rxyzs,rnii,ctx,hs,rmfslh,rmfsrh = load_connectivity(atlas_name)

    self.weights = ws
    self.region_labels = rls

    if tls is not None: self.tract_lengths = tls
    if rxyzs is not None: self.region_xyzs = rxyzs
    if rnii is not None: self.region_nii = rnii
    if ctx is not None: self.cortex = ctx
    if hs is not None: self.hemispheres = hs
    if rmfslh is not None: self.region_mapping_fsav_lh = rmfslh
    if rmfsrh is not None: self.region_mapping_fsav_rh = rmfsrh

    # Compile node and connectivity info into a networkx graph
    self.Gnx = make_nx_graph(self.sfms,self.bbox,ws,rls,hs,ctx)

  def modify_connectome(self, name='mc1',function=''):
    """
    Modify canonical connectome using hit or scalar
    stats, and store in this obj
    """

    res = modify_stream_connectome()

    self.modcons[name] = res

  def plot_network(self):

    plot_network()

  def plot_matrix(self):

    plot_matrix()

  def plot_cnxns(self):

    plot_stream_cnxns()

<|endoftext|>


<commit_before># Copyright 2015 Oleg Girko <ol@infoserver.lv>
#
# This file is part of ldbox.site Python package.
#
# ldbox.site Python package is free software:
# you can redistribute it and/or modify
# it under the terms of the GNU Affero Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Foobar 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 Affero Public License for more details.
#
# You should have received a copy of the GNU Affero Public License
# along with Foobar.  If not, see <http://www.gnu.org/licenses/>.

from django.conf.urls import include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

from django.contrib import admin
admin.autodiscover()

urlpatterns = [
    # Examples:
    # url(r'^$', 'ldbox.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
    url(r'^accounts/', include('django.contrib.auth.urls')),
    url(r'^', include('waliki.urls')),
]

urlpatterns += staticfiles_urlpatterns()
<commit_msg>Update to Django 2.0.<commit_after># Copyright 2015 Oleg Girko <ol@infoserver.lv>
#
# This file is part of ldbox.site Python package.
#
# ldbox.site Python package is free software:
# you can redistribute it and/or modify
# it under the terms of the GNU Affero Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Foobar 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 Affero Public License for more details.
#
# You should have received a copy of the GNU Affero Public License
# along with Foobar.  If not, see <http://www.gnu.org/licenses/>.

from django.conf.urls import include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

from django.contrib import admin
from django.urls import path
admin.autodiscover()

urlpatterns = [
    # Examples:
    # url(r'^$', 'ldbox.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    path('admin/', admin.site.urls),
    url(r'^accounts/', include('django.contrib.auth.urls')),
    url(r'^', include('waliki.urls')),
]

urlpatterns += staticfiles_urlpatterns()
<|endoftext|>


<commit_before>"""
FastCGI server that implements the WSGI protocol.

Uses the flup python package: http://www.saddi.com/software/flup/

This is a adaptation of the flup package to add FastCGI server support
to run Django apps from Web servers that support the FastCGI protocol.
This module can be run standalone or from the django-admin / manage.py
scripts using the "runfcgi" directive.

Run with the extra option "help" for a list of additional options you can
pass to this server.
"""

import sys, os

__version__ = "0.1"
__all__ = ["runfastcgi"]

FASTCGI_HELP = r"""runfcgi:
  Run this project as a fastcgi application. To do this, the
  flup package from http://www.saddi.com/software/flup/ is
  required.

Usage:
   django-admin.py runfcgi --settings=yourproject.settings [fcgi settings]
   manage.py runfcgi [fcgi settings]

Optional Fcgi settings: (setting=value)
  host=HOSTNAME        hostname to listen on..
  port=PORTNUM         port to listen on.
  socket=FILE          UNIX socket to listen on.
  method=IMPL          prefork or threaded (default prefork)
  maxspare=NUMBER      max number of spare processes to keep running.
  minspare=NUMBER      min number of spare processes to prefork.
  maxchildren=NUMBER   hard limit number of processes in prefork mode.
  daemonize=BOOL       whether to detach from terminal.
  pidfile=FILE         write the spawned process-id to this file.
  workdir=DIRECTORY    change to this directory when daemonizing

Examples:
  Run a "standard" fastcgi process on a file-descriptor
  (for webservers which spawn your processes for you)
    $ manage.py runfcgi method=threaded

  Run a fastcgi server on a TCP host/port
    $ manage.py runfcgi method=prefork host=127.0.0.1 port=8025

  Run a fastcgi server on a UNIX domain socket (posix platforms only)
    $ manage.py runfcgi method=prefork socket=/tmp/fcgi.sock

  Run a fastCGI as a daemon and write the spawned PID in a file
    $ manage.py runfcgi socket=/tmp/fcgi.sock method=prefork \
        daemonize=true pidfile=/var/run/django-fcgi.pid

"""

FASTCGI_OPTIONS = {
    'host': None,
    'port': None,
    'socket': None,
    'method': 'fork',
    'daemonize': None,
    'workdir': '/',
    'pidfile': None,
    'maxspare': 5,
    'minspare': 2,
    'maxchildren': 50,
}

def fastcgi_help(message=None):
    print FASTCGI_HELP
    if message:
        print message
    return False

def runfastcgi(argset, **kwargs):
    options = FASTCGI_OPTIONS.copy()
    options.update(kwargs)
    for x in argset:
        if "=" in x:
            k, v = x.split('=', 1)
        else:
            k, v = x, True
        options[k.lower()] = v

    if "help" in options:
        return fastcgi_help()

    try:
        import flup
    except ImportError, e:
        print >> sys.stderr, "ERROR: %s" % e
        print >> sys.stderr, "  Unable to load the flup package.  In order to run django"
        print >> sys.stderr, "  as a FastCGI application, you will need to get flup from"
        print >> sys.stderr, "  http://www.saddi.com/software/flup/   If you've already"
        print >> sys.stderr, "  installed flup, then make sure you have it in your PYTHONPATH."
        return False

    if options['method'] in ('prefork', 'fork'):
        from flup.server.fcgi_fork import WSGIServer
        wsgi_opts = {
            'maxSpare': int(options["maxspare"]),
            'minSpare': int(options["minspare"]),
            'maxChildren': int(options["maxchildren"]),
        }
    elif options['method'] in ('thread', 'threaded'):
        from flup.server.fcgi import WSGIServer
        wsgi_opts = {}
    else:
        return fastcgi_help("ERROR: Implementation must be one of prefork or thread.")

    # Prep up and go
    from django.core.handlers.wsgi import WSGIHandler

    if options["host"] and options["port"] and not options["socket"]:
        wsgi_opts['bindAddress'] = (options["host"], int(options["port"]))
    elif options["socket"] and not options["host"] and not options["port"]:
        wsgi_opts['bindAddress'] = options["socket"]
    elif not options["socket"] and not options["host"] and not options["port"]:
        wsgi_opts['bindAddress'] = None
    else:
        return fastcgi_help("Invalid combination of host, port, socket.")

    if options["daemonize"] is None:
        # Default to daemonizing if we're running on a socket/named pipe.
        daemonize = (wsgi_opts['bindAddress'] is not None)
    else:
        if options["daemonize"].lower() in ('true', 'yes', 't'):
            daemonize = True
        elif options["daemonize"].lower() in ('false', 'no', 'f'):
            daemonize = False
        else:
            return fastcgi_help("ERROR: Invalid option for daemonize parameter.")

    if daemonize:
        from django.utils.daemonize import become_daemon
        become_daemon(our_home_dir=options["workdir"])

    if options["pidfile"]:
        fp = open(options["pidfile"], "w")
        fp.write("%d\n" % os.getpid())
        fp.close()

    WSGIServer(WSGIHandler(), **wsgi_opts).run()

if __name__ == '__main__':
    runfastcgi(sys.argv[1:])
<commit_msg>Fixed #2732 -- Allow first positional argument to runfastcgi() to be optional. Patch from James Crasta.<commit_after>"""
FastCGI server that implements the WSGI protocol.

Uses the flup python package: http://www.saddi.com/software/flup/

This is a adaptation of the flup package to add FastCGI server support
to run Django apps from Web servers that support the FastCGI protocol.
This module can be run standalone or from the django-admin / manage.py
scripts using the "runfcgi" directive.

Run with the extra option "help" for a list of additional options you can
pass to this server.
"""

import sys, os

__version__ = "0.1"
__all__ = ["runfastcgi"]

FASTCGI_HELP = r"""runfcgi:
  Run this project as a fastcgi application. To do this, the
  flup package from http://www.saddi.com/software/flup/ is
  required.

Usage:
   django-admin.py runfcgi --settings=yourproject.settings [fcgi settings]
   manage.py runfcgi [fcgi settings]

Optional Fcgi settings: (setting=value)
  host=HOSTNAME        hostname to listen on..
  port=PORTNUM         port to listen on.
  socket=FILE          UNIX socket to listen on.
  method=IMPL          prefork or threaded (default prefork)
  maxspare=NUMBER      max number of spare processes to keep running.
  minspare=NUMBER      min number of spare processes to prefork.
  maxchildren=NUMBER   hard limit number of processes in prefork mode.
  daemonize=BOOL       whether to detach from terminal.
  pidfile=FILE         write the spawned process-id to this file.
  workdir=DIRECTORY    change to this directory when daemonizing

Examples:
  Run a "standard" fastcgi process on a file-descriptor
  (for webservers which spawn your processes for you)
    $ manage.py runfcgi method=threaded

  Run a fastcgi server on a TCP host/port
    $ manage.py runfcgi method=prefork host=127.0.0.1 port=8025

  Run a fastcgi server on a UNIX domain socket (posix platforms only)
    $ manage.py runfcgi method=prefork socket=/tmp/fcgi.sock

  Run a fastCGI as a daemon and write the spawned PID in a file
    $ manage.py runfcgi socket=/tmp/fcgi.sock method=prefork \
        daemonize=true pidfile=/var/run/django-fcgi.pid

"""

FASTCGI_OPTIONS = {
    'host': None,
    'port': None,
    'socket': None,
    'method': 'fork',
    'daemonize': None,
    'workdir': '/',
    'pidfile': None,
    'maxspare': 5,
    'minspare': 2,
    'maxchildren': 50,
}

def fastcgi_help(message=None):
    print FASTCGI_HELP
    if message:
        print message
    return False

def runfastcgi(argset=[], **kwargs):
    options = FASTCGI_OPTIONS.copy()
    options.update(kwargs)
    for x in argset:
        if "=" in x:
            k, v = x.split('=', 1)
        else:
            k, v = x, True
        options[k.lower()] = v

    if "help" in options:
        return fastcgi_help()

    try:
        import flup
    except ImportError, e:
        print >> sys.stderr, "ERROR: %s" % e
        print >> sys.stderr, "  Unable to load the flup package.  In order to run django"
        print >> sys.stderr, "  as a FastCGI application, you will need to get flup from"
        print >> sys.stderr, "  http://www.saddi.com/software/flup/   If you've already"
        print >> sys.stderr, "  installed flup, then make sure you have it in your PYTHONPATH."
        return False

    if options['method'] in ('prefork', 'fork'):
        from flup.server.fcgi_fork import WSGIServer
        wsgi_opts = {
            'maxSpare': int(options["maxspare"]),
            'minSpare': int(options["minspare"]),
            'maxChildren': int(options["maxchildren"]),
        }
    elif options['method'] in ('thread', 'threaded'):
        from flup.server.fcgi import WSGIServer
        wsgi_opts = {}
    else:
        return fastcgi_help("ERROR: Implementation must be one of prefork or thread.")

    # Prep up and go
    from django.core.handlers.wsgi import WSGIHandler

    if options["host"] and options["port"] and not options["socket"]:
        wsgi_opts['bindAddress'] = (options["host"], int(options["port"]))
    elif options["socket"] and not options["host"] and not options["port"]:
        wsgi_opts['bindAddress'] = options["socket"]
    elif not options["socket"] and not options["host"] and not options["port"]:
        wsgi_opts['bindAddress'] = None
    else:
        return fastcgi_help("Invalid combination of host, port, socket.")

    if options["daemonize"] is None:
        # Default to daemonizing if we're running on a socket/named pipe.
        daemonize = (wsgi_opts['bindAddress'] is not None)
    else:
        if options["daemonize"].lower() in ('true', 'yes', 't'):
            daemonize = True
        elif options["daemonize"].lower() in ('false', 'no', 'f'):
            daemonize = False
        else:
            return fastcgi_help("ERROR: Invalid option for daemonize parameter.")

    if daemonize:
        from django.utils.daemonize import become_daemon
        become_daemon(our_home_dir=options["workdir"])

    if options["pidfile"]:
        fp = open(options["pidfile"], "w")
        fp.write("%d\n" % os.getpid())
        fp.close()

    WSGIServer(WSGIHandler(), **wsgi_opts).run()

if __name__ == '__main__':
    runfastcgi(sys.argv[1:])
<|endoftext|>


<commit_before>#!/usr/bin/python

import logging
import datetime
import re
from . import register, Site, Section, Chapter

logger = logging.getLogger(__name__)

@register
class ArchiveOfOurOwn(Site):
    """Archive of Our Own: it has its own epub export, but the formatting is awful"""
    @staticmethod
    def matches(url):
        # e.g. http://archiveofourown.org/works/5683105/chapters/13092007
        match = re.match(r'^(https?://archiveofourown\.org/works/\d+)/?.*', url)
        if match:
            return match.group(1) + '/'

    def extract(self, url):
        workid = re.match(r'^https?://archiveofourown\.org/works/(\d+)/?.*', url).group(1)
        return self._extract_work(workid)

    def _extract_work(self, workid):
        soup = self._soup('http://archiveofourown.org/works/{}/navigate?view_adult=true'.format(workid))

        metadata = soup.select('#main h2.heading a')
        story = Section(
            title=metadata[0].string,
            author=metadata[1].string,
            url='http://archiveofourown.org/works/{}'.format(workid)
        )

        for chapter in soup.select('#main ol[role="navigation"] li'):
            link = chapter.find('a')
            chapter_url = str(link.get('href'))
            if chapter_url.startswith('/works/'):
                chapter_url = 'http://archiveofourown.org' + chapter_url
            chapter_url += '?view_adult=true'

            updated = datetime.datetime.strptime(
                chapter.find('span', class_='datetime').string,
                "(%Y-%m-%d)"
            )

            story.add(Chapter(title=link.string, contents=self._chapter(chapter_url), date=updated))

        return story

    def _chapter(self, url):
        logger.info("Extracting chapter @ %s", url)
        soup = self._soup(url)
        content = soup.find('div', role='article')

        for landmark in content.find_all(class_='landmark'):
            landmark.decompose()

        # TODO: Maybe these should be footnotes instead?
        notes = soup.select('#chapters .end.notes')
        if notes:
            notes = notes[0]
            for landmark in notes.find_all(class_='landmark'):
                landmark.decompose()

        return content.prettify() + (notes and notes.prettify() or '')

@register
class ArchiveOfOurOwnSeries(ArchiveOfOurOwn):
    @staticmethod
    def matches(url):
        # e.g. http://archiveofourown.org/series/5683105/
        match = re.match(r'^(https?://archiveofourown\.org/series/\d+)/?.*', url)
        if match:
            return match.group(1) + '/'

    def extract(self, url):
        seriesid = re.match(r'^https?://archiveofourown\.org/series/(\d+)/?.*', url).group(1)

        soup = self._soup('http://archiveofourown.org/series/{}?view_adult=true'.format(seriesid))

        story = Section(
            title=soup.select('#main h2.heading')[0].string,
            author=soup.select('#main dl.series.meta a[rel="author"]')[0].string
        )

        for work in soup.select('#main ul.series li.work'):
            workid = work.get('id').replace('work_', '')
            substory = self._extract_work(workid)

            # TODO: improve epub-writer to be able to generate a toc.ncx with nested headings
            story.add(substory)

        return story
<commit_msg>Oh hey, another missing Section URL<commit_after>#!/usr/bin/python

import logging
import datetime
import re
from . import register, Site, Section, Chapter

logger = logging.getLogger(__name__)

@register
class ArchiveOfOurOwn(Site):
    """Archive of Our Own: it has its own epub export, but the formatting is awful"""
    @staticmethod
    def matches(url):
        # e.g. http://archiveofourown.org/works/5683105/chapters/13092007
        match = re.match(r'^(https?://archiveofourown\.org/works/\d+)/?.*', url)
        if match:
            return match.group(1) + '/'

    def extract(self, url):
        workid = re.match(r'^https?://archiveofourown\.org/works/(\d+)/?.*', url).group(1)
        return self._extract_work(workid)

    def _extract_work(self, workid):
        soup = self._soup('http://archiveofourown.org/works/{}/navigate?view_adult=true'.format(workid))

        metadata = soup.select('#main h2.heading a')
        story = Section(
            title=metadata[0].string,
            author=metadata[1].string,
            url='http://archiveofourown.org/works/{}'.format(workid)
        )

        for chapter in soup.select('#main ol[role="navigation"] li'):
            link = chapter.find('a')
            chapter_url = str(link.get('href'))
            if chapter_url.startswith('/works/'):
                chapter_url = 'http://archiveofourown.org' + chapter_url
            chapter_url += '?view_adult=true'

            updated = datetime.datetime.strptime(
                chapter.find('span', class_='datetime').string,
                "(%Y-%m-%d)"
            )

            story.add(Chapter(title=link.string, contents=self._chapter(chapter_url), date=updated))

        return story

    def _chapter(self, url):
        logger.info("Extracting chapter @ %s", url)
        soup = self._soup(url)
        content = soup.find('div', role='article')

        for landmark in content.find_all(class_='landmark'):
            landmark.decompose()

        # TODO: Maybe these should be footnotes instead?
        notes = soup.select('#chapters .end.notes')
        if notes:
            notes = notes[0]
            for landmark in notes.find_all(class_='landmark'):
                landmark.decompose()

        return content.prettify() + (notes and notes.prettify() or '')

@register
class ArchiveOfOurOwnSeries(ArchiveOfOurOwn):
    @staticmethod
    def matches(url):
        # e.g. http://archiveofourown.org/series/5683105/
        match = re.match(r'^(https?://archiveofourown\.org/series/\d+)/?.*', url)
        if match:
            return match.group(1) + '/'

    def extract(self, url):
        seriesid = re.match(r'^https?://archiveofourown\.org/series/(\d+)/?.*', url).group(1)

        soup = self._soup('http://archiveofourown.org/series/{}?view_adult=true'.format(seriesid))

        story = Section(
            title=soup.select('#main h2.heading')[0].string,
            author=soup.select('#main dl.series.meta a[rel="author"]')[0].string,
            url='http://archiveofourown.org/series/{}'.format(seriesid)
        )

        for work in soup.select('#main ul.series li.work'):
            workid = work.get('id').replace('work_', '')
            substory = self._extract_work(workid)

            # TODO: improve epub-writer to be able to generate a toc.ncx with nested headings
            story.add(substory)

        return story
<|endoftext|>


<commit_before>"""
FastCGI (or SCGI, or AJP1.3 ...) server that implements the WSGI protocol.

Uses the flup python package: http://www.saddi.com/software/flup/

This is a adaptation of the flup package to add FastCGI server support
to run Django apps from Web servers that support the FastCGI protocol.
This module can be run standalone or from the django-admin / manage.py
scripts using the "runfcgi" directive.

Run with the extra option "help" for a list of additional options you can
pass to this server.
"""

from django.utils import importlib
import sys, os

__version__ = "0.1"
__all__ = ["runfastcgi"]

FASTCGI_HELP = r"""
  Run this project as a fastcgi (or some other protocol supported
  by flup) application. To do this, the flup package from
  http://www.saddi.com/software/flup/ is required.

   runfcgi [options] [fcgi settings]

Optional Fcgi settings: (setting=value)
  protocol=PROTOCOL    fcgi, scgi, ajp, ... (default fcgi)
  host=HOSTNAME        hostname to listen on..
  port=PORTNUM         port to listen on.
  socket=FILE          UNIX socket to listen on.
  method=IMPL          prefork or threaded (default prefork)
  maxrequests=NUMBER   number of requests a child handles before it is
                       killed and a new child is forked (0 = no limit).
  maxspare=NUMBER      max number of spare processes / threads
  minspare=NUMBER      min number of spare processes / threads.
  maxchildren=NUMBER   hard limit number of processes / threads
  daemonize=BOOL       whether to detach from terminal.
  pidfile=FILE         write the spawned process-id to this file.
  workdir=DIRECTORY    change to this directory when daemonizing.
  outlog=FILE          write stdout to this file.
  errlog=FILE          write stderr to this file.
  umask=UMASK          umask to use when daemonizing (default 022).

Examples:
  Run a "standard" fastcgi process on a file-descriptor
  (for webservers which spawn your processes for you)
    $ manage.py runfcgi method=threaded

  Run a scgi server on a TCP host/port
    $ manage.py runfcgi protocol=scgi method=prefork host=127.0.0.1 port=8025

  Run a fastcgi server on a UNIX domain socket (posix platforms only)
    $ manage.py runfcgi method=prefork socket=/tmp/fcgi.sock

  Run a fastCGI as a daemon and write the spawned PID in a file
    $ manage.py runfcgi socket=/tmp/fcgi.sock method=prefork \
        daemonize=true pidfile=/var/run/django-fcgi.pid

"""

FASTCGI_OPTIONS = {
    'protocol': 'fcgi',
    'host': None,
    'port': None,
    'socket': None,
    'method': 'fork',
    'daemonize': None,
    'workdir': '/',
    'pidfile': None,
    'maxspare': 5,
    'minspare': 2,
    'maxchildren': 50,
    'maxrequests': 0,
    'outlog': None,
    'errlog': None,
    'umask': None,
}

def fastcgi_help(message=None):
    print FASTCGI_HELP
    if message:
        print message
    return False

def runfastcgi(argset=[], **kwargs):
    options = FASTCGI_OPTIONS.copy()
    options.update(kwargs)
    for x in argset:
        if "=" in x:
            k, v = x.split('=', 1)
        else:
            k, v = x, True
        options[k.lower()] = v

    if "help" in options:
        return fastcgi_help()

    try:
        import flup
    except ImportError, e:
        print >> sys.stderr, "ERROR: %s" % e
        print >> sys.stderr, "  Unable to load the flup package.  In order to run django"
        print >> sys.stderr, "  as a FastCGI application, you will need to get flup from"
        print >> sys.stderr, "  http://www.saddi.com/software/flup/   If you've already"
        print >> sys.stderr, "  installed flup, then make sure you have it in your PYTHONPATH."
        return False

    flup_module = 'server.' + options['protocol']

    if options['method'] in ('prefork', 'fork'):
        wsgi_opts = {
            'maxSpare': int(options["maxspare"]),
            'minSpare': int(options["minspare"]),
            'maxChildren': int(options["maxchildren"]),
            'maxRequests': int(options["maxrequests"]),
        }
        flup_module += '_fork'
    elif options['method'] in ('thread', 'threaded'):
        wsgi_opts = {
            'maxSpare': int(options["maxspare"]),
            'minSpare': int(options["minspare"]),
            'maxThreads': int(options["maxchildren"]),
        }
    else:
        return fastcgi_help("ERROR: Implementation must be one of prefork or thread.")

    wsgi_opts['debug'] = False # Turn off flup tracebacks

    try:
        module = importlib_import_module('.%s' % flup_module, 'flup')
        WSGIServer = module.WSGIServer
    except:
        print "Can't import flup." + flup_module
        return False

    # Prep up and go
    from django.core.handlers.wsgi import WSGIHandler

    if options["host"] and options["port"] and not options["socket"]:
        wsgi_opts['bindAddress'] = (options["host"], int(options["port"]))
    elif options["socket"] and not options["host"] and not options["port"]:
        wsgi_opts['bindAddress'] = options["socket"]
    elif not options["socket"] and not options["host"] and not options["port"]:
        wsgi_opts['bindAddress'] = None
    else:
        return fastcgi_help("Invalid combination of host, port, socket.")

    if options["daemonize"] is None:
        # Default to daemonizing if we're running on a socket/named pipe.
        daemonize = (wsgi_opts['bindAddress'] is not None)
    else:
        if options["daemonize"].lower() in ('true', 'yes', 't'):
            daemonize = True
        elif options["daemonize"].lower() in ('false', 'no', 'f'):
            daemonize = False
        else:
            return fastcgi_help("ERROR: Invalid option for daemonize parameter.")

    daemon_kwargs = {}
    if options['outlog']:
        daemon_kwargs['out_log'] = options['outlog']
    if options['errlog']:
        daemon_kwargs['err_log'] = options['errlog']
    if options['umask']:
        daemon_kwargs['umask'] = int(options['umask'])

    if daemonize:
        from django.utils.daemonize import become_daemon
        become_daemon(our_home_dir=options["workdir"], **daemon_kwargs)

    if options["pidfile"]:
        fp = open(options["pidfile"], "w")
        fp.write("%d\n" % os.getpid())
        fp.close()

    WSGIServer(WSGIHandler(), **wsgi_opts).run()

if __name__ == '__main__':
    runfastcgi(sys.argv[1:])
<commit_msg>Fixed #10556 -- Fixed a problem in the fastcgi server after r10088. Thanks, Boo.<commit_after>"""
FastCGI (or SCGI, or AJP1.3 ...) server that implements the WSGI protocol.

Uses the flup python package: http://www.saddi.com/software/flup/

This is a adaptation of the flup package to add FastCGI server support
to run Django apps from Web servers that support the FastCGI protocol.
This module can be run standalone or from the django-admin / manage.py
scripts using the "runfcgi" directive.

Run with the extra option "help" for a list of additional options you can
pass to this server.
"""

from django.utils import importlib
import sys, os

__version__ = "0.1"
__all__ = ["runfastcgi"]

FASTCGI_HELP = r"""
  Run this project as a fastcgi (or some other protocol supported
  by flup) application. To do this, the flup package from
  http://www.saddi.com/software/flup/ is required.

   runfcgi [options] [fcgi settings]

Optional Fcgi settings: (setting=value)
  protocol=PROTOCOL    fcgi, scgi, ajp, ... (default fcgi)
  host=HOSTNAME        hostname to listen on..
  port=PORTNUM         port to listen on.
  socket=FILE          UNIX socket to listen on.
  method=IMPL          prefork or threaded (default prefork)
  maxrequests=NUMBER   number of requests a child handles before it is
                       killed and a new child is forked (0 = no limit).
  maxspare=NUMBER      max number of spare processes / threads
  minspare=NUMBER      min number of spare processes / threads.
  maxchildren=NUMBER   hard limit number of processes / threads
  daemonize=BOOL       whether to detach from terminal.
  pidfile=FILE         write the spawned process-id to this file.
  workdir=DIRECTORY    change to this directory when daemonizing.
  outlog=FILE          write stdout to this file.
  errlog=FILE          write stderr to this file.
  umask=UMASK          umask to use when daemonizing (default 022).

Examples:
  Run a "standard" fastcgi process on a file-descriptor
  (for webservers which spawn your processes for you)
    $ manage.py runfcgi method=threaded

  Run a scgi server on a TCP host/port
    $ manage.py runfcgi protocol=scgi method=prefork host=127.0.0.1 port=8025

  Run a fastcgi server on a UNIX domain socket (posix platforms only)
    $ manage.py runfcgi method=prefork socket=/tmp/fcgi.sock

  Run a fastCGI as a daemon and write the spawned PID in a file
    $ manage.py runfcgi socket=/tmp/fcgi.sock method=prefork \
        daemonize=true pidfile=/var/run/django-fcgi.pid

"""

FASTCGI_OPTIONS = {
    'protocol': 'fcgi',
    'host': None,
    'port': None,
    'socket': None,
    'method': 'fork',
    'daemonize': None,
    'workdir': '/',
    'pidfile': None,
    'maxspare': 5,
    'minspare': 2,
    'maxchildren': 50,
    'maxrequests': 0,
    'outlog': None,
    'errlog': None,
    'umask': None,
}

def fastcgi_help(message=None):
    print FASTCGI_HELP
    if message:
        print message
    return False

def runfastcgi(argset=[], **kwargs):
    options = FASTCGI_OPTIONS.copy()
    options.update(kwargs)
    for x in argset:
        if "=" in x:
            k, v = x.split('=', 1)
        else:
            k, v = x, True
        options[k.lower()] = v

    if "help" in options:
        return fastcgi_help()

    try:
        import flup
    except ImportError, e:
        print >> sys.stderr, "ERROR: %s" % e
        print >> sys.stderr, "  Unable to load the flup package.  In order to run django"
        print >> sys.stderr, "  as a FastCGI application, you will need to get flup from"
        print >> sys.stderr, "  http://www.saddi.com/software/flup/   If you've already"
        print >> sys.stderr, "  installed flup, then make sure you have it in your PYTHONPATH."
        return False

    flup_module = 'server.' + options['protocol']

    if options['method'] in ('prefork', 'fork'):
        wsgi_opts = {
            'maxSpare': int(options["maxspare"]),
            'minSpare': int(options["minspare"]),
            'maxChildren': int(options["maxchildren"]),
            'maxRequests': int(options["maxrequests"]),
        }
        flup_module += '_fork'
    elif options['method'] in ('thread', 'threaded'):
        wsgi_opts = {
            'maxSpare': int(options["maxspare"]),
            'minSpare': int(options["minspare"]),
            'maxThreads': int(options["maxchildren"]),
        }
    else:
        return fastcgi_help("ERROR: Implementation must be one of prefork or thread.")

    wsgi_opts['debug'] = False # Turn off flup tracebacks

    try:
        module = importlib.import_module('.%s' % flup_module, 'flup')
        WSGIServer = module.WSGIServer
    except:
        print "Can't import flup." + flup_module
        return False

    # Prep up and go
    from django.core.handlers.wsgi import WSGIHandler

    if options["host"] and options["port"] and not options["socket"]:
        wsgi_opts['bindAddress'] = (options["host"], int(options["port"]))
    elif options["socket"] and not options["host"] and not options["port"]:
        wsgi_opts['bindAddress'] = options["socket"]
    elif not options["socket"] and not options["host"] and not options["port"]:
        wsgi_opts['bindAddress'] = None
    else:
        return fastcgi_help("Invalid combination of host, port, socket.")

    if options["daemonize"] is None:
        # Default to daemonizing if we're running on a socket/named pipe.
        daemonize = (wsgi_opts['bindAddress'] is not None)
    else:
        if options["daemonize"].lower() in ('true', 'yes', 't'):
            daemonize = True
        elif options["daemonize"].lower() in ('false', 'no', 'f'):
            daemonize = False
        else:
            return fastcgi_help("ERROR: Invalid option for daemonize parameter.")

    daemon_kwargs = {}
    if options['outlog']:
        daemon_kwargs['out_log'] = options['outlog']
    if options['errlog']:
        daemon_kwargs['err_log'] = options['errlog']
    if options['umask']:
        daemon_kwargs['umask'] = int(options['umask'])

    if daemonize:
        from django.utils.daemonize import become_daemon
        become_daemon(our_home_dir=options["workdir"], **daemon_kwargs)

    if options["pidfile"]:
        fp = open(options["pidfile"], "w")
        fp.write("%d\n" % os.getpid())
        fp.close()

    WSGIServer(WSGIHandler(), **wsgi_opts).run()

if __name__ == '__main__':
    runfastcgi(sys.argv[1:])
<|endoftext|>


<commit_before>"""
SPDX-FileCopyrightText: 2019 oemof developer group <contact@oemof.org>
SPDX-License-Identifier: MIT
"""
from typing import Dict

import numpy as np
import pandas as pd
import pytest
from numpy.testing import assert_allclose
from pandas.util.testing import assert_series_equal
from windpowerlib.power_output import (
    power_coefficient_curve,
    power_curve,
    power_curve_density_correction,
)

class TestPowerOutput:
    def setup_class(self):
        self.parameters: Dict = {
            "wind_speed": pd.Series(data=[2.0, 5.5, 7.0]),
            "density": pd.Series(data=[1.3, 1.3, 1.3]),
            "rotor_diameter": 80,
            "power_coefficient_curve_wind_speeds": pd.Series([4.0, 5.0, 6.0]),
            "power_coefficient_curve_values": pd.Series([0.3, 0.4, 0.5]),
        }

    def test_power_coefficient_curve_1(self):
        # Test wind_speed as pd.Series with density and power_coefficient_curve
        # as pd.Series and np.array
        power_output_exp = pd.Series(
            data=[0.0, 244615.399, 0.0], name="feedin_power_plant"
        )
        assert_series_equal(
            power_coefficient_curve(**self.parameters), power_output_exp
        )

        parameters = self.parameters
        parameters["density"].to_numpy()
        assert_series_equal(
            power_coefficient_curve(**parameters), power_output_exp
        )

        parameters["power_coefficient_curve_values"] = np.array(
            parameters["power_coefficient_curve_values"]
        )
        parameters["power_coefficient_curve_wind_speeds"] = np.array(
            parameters["power_coefficient_curve_wind_speeds"]
        )
        assert_series_equal(
            power_coefficient_curve(**parameters), power_output_exp
        )

    def test_power_coefficient_curve_output_types(self):
        parameters = self.parameters
        # Test wind_speed as np.array with density and power_coefficient_curve
        # as np.array and pd.Series
        assert isinstance(power_coefficient_curve(**parameters), pd.Series)
        parameters["wind_speed"] = np.array(parameters["wind_speed"])
        assert isinstance(power_coefficient_curve(**parameters), np.ndarray)

    def test_power_coefficient_curve_2(self):
        parameters = self.parameters
        power_output_exp = np.array([0.0, 244615.399, 0.0])
        parameters["wind_speed"] = np.array(parameters["wind_speed"])
        assert_allclose(
            power_coefficient_curve(**parameters), power_output_exp
        )
        parameters["density"] = pd.Series(data=parameters["density"])
        assert_allclose(
            power_coefficient_curve(**parameters), power_output_exp
        )
        assert isinstance(power_coefficient_curve(**parameters), np.ndarray)
        parameters["power_coefficient_curve_wind_speeds"] = pd.Series(
            data=parameters["power_coefficient_curve_wind_speeds"]
        )
        parameters["power_coefficient_curve_values"] = pd.Series(
            data=parameters["power_coefficient_curve_values"]
        )
        assert_allclose(
            power_coefficient_curve(**parameters), power_output_exp
        )
        assert isinstance(power_coefficient_curve(**parameters), np.ndarray)

    def test_power_curve(self):
        parameters = {
            "wind_speed": pd.Series(data=[2.0, 5.5, 7.0]),
            "density": pd.Series(data=[1.3, 1.3, 1.3]),
            "density_correction": False,
            "power_curve_wind_speeds": pd.Series([4.0, 5.0, 6.0]),
            "power_curve_values": pd.Series([300, 400, 500]),
        }

        # Tests without density correction:
        # Test wind_speed as pd.Series and power_curve as pd.Series and
        # np.array
        power_output_exp = pd.Series(
            data=[0.0, 450.0, 0.0], name="feedin_power_plant"
        )
        assert_series_equal(power_curve(**parameters), power_output_exp)
        parameters["power_curve_values"] = np.array(
            parameters["power_curve_values"]
        )
        parameters["power_curve_wind_speeds"] = np.array(
            parameters["power_curve_wind_speeds"]
        )
        assert_series_equal(power_curve(**parameters), power_output_exp)

        # Test wind_speed as np.array and power_curve as pd.Series and np.array
        power_output_exp = np.array([0.0, 450.0, 0.0])
        parameters["wind_speed"] = np.array(parameters["wind_speed"])
        assert_allclose(power_curve(**parameters), power_output_exp)
        assert isinstance(power_curve(**parameters), np.ndarray)
        parameters["power_curve_wind_speeds"] = pd.Series(
            data=parameters["power_curve_wind_speeds"]
        )
        parameters["power_curve_values"] = pd.Series(
            data=parameters["power_curve_values"]
        )
        assert_allclose(power_curve(**parameters), power_output_exp)
        assert isinstance(power_curve(**parameters), np.ndarray)

        # Tests with density correction:
        # Test wind_speed as np.array with density and power_curve as pd.Series
        # and np.array
        power_output_exp = np.array([0.0, 461.00290572, 0.0])
        parameters["density_correction"] = True
        assert_allclose(power_curve(**parameters), power_output_exp)
        assert isinstance(power_curve(**parameters), np.ndarray)
        parameters["density"] = np.array(parameters["density"])
        assert_allclose(power_curve(**parameters), power_output_exp)
        assert isinstance(power_curve(**parameters), np.ndarray)
        parameters["power_curve_values"] = np.array(
            parameters["power_curve_values"]
        )
        parameters["power_curve_wind_speeds"] = np.array(
            parameters["power_curve_wind_speeds"]
        )
        assert_allclose(power_curve(**parameters), power_output_exp)
        assert isinstance(power_curve(**parameters), np.ndarray)

        # Test wind_speed as pd.Series with density and power_curve as
        # np. array and pd.Series
        power_output_exp = pd.Series(
            data=[0.0, 461.00290572, 0.0], name="feedin_power_plant"
        )
        parameters["wind_speed"] = pd.Series(data=parameters["wind_speed"])
        assert_series_equal(power_curve(**parameters), power_output_exp)
        parameters["density"] = pd.Series(data=parameters["density"])
        assert_series_equal(power_curve(**parameters), power_output_exp)
        parameters["power_curve_wind_speeds"] = pd.Series(
            data=parameters["power_curve_wind_speeds"]
        )
        parameters["power_curve_values"] = pd.Series(
            data=parameters["power_curve_values"]
        )
        assert_series_equal(power_curve(**parameters), power_output_exp)

        # Raise TypeErrors due to wrong type of `density_correction`
        with pytest.raises(TypeError):
            parameters["density"] = "wrong_type"
            power_curve(**parameters)

    def test_power_curve_density_correction(self):
        parameters = {
            "wind_speed": pd.Series(data=[2.0, 5.5, 7.0]),
            "density": pd.Series(data=[1.3, 1.3, 1.3]),
            "power_curve_wind_speeds": pd.Series([4.0, 5.0, 6.0]),
            "power_curve_values": pd.Series([300, 400, 500]),
        }

        # Test wind_speed as pd.Series with density and power_curve as
        # pd.Series and np.array
        power_output_exp = pd.Series(
            data=[0.0, 461.00290572, 0.0], name="feedin_power_plant"
        )
        assert_series_equal(
            power_curve_density_correction(**parameters), power_output_exp
        )
        parameters["density"] = np.array(parameters["density"])
        assert_series_equal(
            power_curve_density_correction(**parameters), power_output_exp
        )
        parameters["power_curve_values"] = np.array(
            parameters["power_curve_values"]
        )
        parameters["power_curve_wind_speeds"] = np.array(
            parameters["power_curve_wind_speeds"]
        )
        assert_series_equal(
            power_curve_density_correction(**parameters), power_output_exp
        )

        # Test wind_speed as np.array with density and power_curve as np.array
        # and pd.Series
        parameters["wind_speed"] = np.array(parameters["wind_speed"])
        power_output_exp = np.array([0.0, 461.00290572, 0.0])
        assert_allclose(
            power_curve_density_correction(**parameters), power_output_exp
        )
        assert isinstance(power_curve(**parameters), np.ndarray)
        parameters["density"] = pd.Series(data=parameters["density"])
        assert_allclose(
            power_curve_density_correction(**parameters), power_output_exp
        )
        assert isinstance(power_curve(**parameters), np.ndarray)
        parameters["power_curve_wind_speeds"] = pd.Series(
            data=parameters["power_curve_wind_speeds"]
        )
        parameters["power_curve_values"] = pd.Series(
            data=parameters["power_curve_values"]
        )
        assert_allclose(
            power_curve_density_correction(**parameters), power_output_exp
        )
        assert isinstance(power_curve(**parameters), np.ndarray)

        # Raise TypeError due to density is None
        with pytest.raises(TypeError):
            parameters["density"] = None
            power_curve_density_correction(**parameters)

    def test_wrong_spelling_density_correction(self):
        parameters = {
            "wind_speed": pd.Series(data=[2.0, 5.5, 7.0]),
            "density": pd.Series(data=[1.3, 1.3, 1.3]),
            "power_curve_wind_speeds": pd.Series([4.0, 5.0, 6.0]),
            "power_curve_values": pd.Series([300, 400, 500]),
        }
        msg = "is an invalid type. `density_correction` must be Boolean"
        with pytest.raises(TypeError, match=msg):
            parameters["density_correction"] = None
            power_curve(**parameters)
<commit_msg>Split long test method to make it easier to find failing tests<commit_after>"""
SPDX-FileCopyrightText: 2019 oemof developer group <contact@oemof.org>
SPDX-License-Identifier: MIT
"""
from typing import Dict

import numpy as np
import pandas as pd
import pytest
from numpy.testing import assert_allclose
from pandas.util.testing import assert_series_equal
from windpowerlib.power_output import (
    power_coefficient_curve,
    power_curve,
    power_curve_density_correction,
)

class TestPowerOutput:
    def setup_class(self):
        self.parameters: Dict = {
            "wind_speed": pd.Series(data=[2.0, 5.5, 7.0]),
            "density": pd.Series(data=[1.3, 1.3, 1.3]),
            "rotor_diameter": 80,
            "power_coefficient_curve_wind_speeds": pd.Series([4.0, 5.0, 6.0]),
            "power_coefficient_curve_values": pd.Series([0.3, 0.4, 0.5]),
        }
        self.parameters2: Dict = {
            "wind_speed": pd.Series(data=[2.0, 5.5, 7.0]),
            "density": pd.Series(data=[1.3, 1.3, 1.3]),
            "density_correction": False,
            "power_curve_wind_speeds": pd.Series([4.0, 5.0, 6.0]),
            "power_curve_values": pd.Series([300, 400, 500]),
        }
        self.power_output_exp1 = pd.Series(
            data=[0.0, 450.0, 0.0], name="feedin_power_plant"
        )
        self.power_output_exp2 = pd.Series(
            data=[0.0, 461.00290572, 0.0], name="feedin_power_plant"
        )

    def test_power_coefficient_curve_1(self):
        """
        Test wind_speed as pd.Series with density and power_coefficient_curve
        as pd.Series and np.array
        """
        power_output_exp = pd.Series(
            data=[0.0, 244615.399, 0.0], name="feedin_power_plant"
        )
        assert_series_equal(
            power_coefficient_curve(**self.parameters), power_output_exp
        )

        parameters = self.parameters
        parameters["density"].to_numpy()
        assert_series_equal(
            power_coefficient_curve(**parameters), power_output_exp
        )

        parameters["power_coefficient_curve_values"] = np.array(
            parameters["power_coefficient_curve_values"]
        )
        parameters["power_coefficient_curve_wind_speeds"] = np.array(
            parameters["power_coefficient_curve_wind_speeds"]
        )
        assert_series_equal(
            power_coefficient_curve(**parameters), power_output_exp
        )

    def test_power_coefficient_curve_output_types(self):
        """
        Test wind_speed as np.array with density and power_coefficient_curve
        as np.array and pd.Series
        """
        assert isinstance(
            power_coefficient_curve(**self.parameters), pd.Series
        )
        self.parameters["wind_speed"] = np.array(self.parameters["wind_speed"])
        assert isinstance(
            power_coefficient_curve(**self.parameters), np.ndarray
        )

    def test_power_coefficient_curve_2(self):
        """TODO: Explain this test"""
        parameters = self.parameters
        power_output_exp = np.array([0.0, 244615.399, 0.0])
        parameters["wind_speed"] = np.array(parameters["wind_speed"])
        assert_allclose(
            power_coefficient_curve(**parameters), power_output_exp
        )
        parameters["density"] = pd.Series(data=parameters["density"])
        assert_allclose(
            power_coefficient_curve(**parameters), power_output_exp
        )
        assert isinstance(power_coefficient_curve(**parameters), np.ndarray)
        parameters["power_coefficient_curve_wind_speeds"] = pd.Series(
            data=parameters["power_coefficient_curve_wind_speeds"]
        )
        parameters["power_coefficient_curve_values"] = pd.Series(
            data=parameters["power_coefficient_curve_values"]
        )
        assert_allclose(
            power_coefficient_curve(**parameters), power_output_exp
        )
        assert isinstance(power_coefficient_curve(**parameters), np.ndarray)

    def test_power_curve_1(self):
        # Tests without density correction:
        # Test wind_speed as pd.Series and power_curve as pd.Series and
        # np.array

        assert_series_equal(
            power_curve(**self.parameters2), self.power_output_exp1
        )

    def test_power_curve_2(self):
        """TODO: Explain this test"""
        self.parameters2["power_curve_values"] = np.array(
            self.parameters2["power_curve_values"]
        )
        self.parameters2["power_curve_wind_speeds"] = np.array(
            self.parameters2["power_curve_wind_speeds"]
        )
        assert_series_equal(
            power_curve(**self.parameters2), self.power_output_exp1
        )

    def test_power_curve_3(self):
        """
        Test wind_speed as np.array and power_curve as
        pd.Series and np.array
        """
        power_output_exp = np.array(self.power_output_exp1)
        self.parameters2["wind_speed"] = np.array(
            self.parameters2["wind_speed"]
        )
        assert_allclose(power_curve(**self.parameters2), power_output_exp)
        assert isinstance(power_curve(**self.parameters2), np.ndarray)

    def test_power_curve_4(self):
        """TODO: Explain this test"""
        self.parameters2["power_curve_wind_speeds"] = pd.Series(
            data=self.parameters2["power_curve_wind_speeds"]
        )
        self.parameters2["power_curve_values"] = pd.Series(
            data=self.parameters2["power_curve_values"]
        )
        assert_allclose(
            power_curve(**self.parameters2), self.power_output_exp1
        )
        assert isinstance(power_curve(**self.parameters2), np.ndarray)

    def test_power_curve_5(self):
        """
        Tests with density correction:
        Test wind_speed as np.array with density and power_curve as pd.Series
        and np.array
        """
        power_output_exp = np.array(self.power_output_exp2)
        self.parameters2["density_correction"] = True
        assert_allclose(power_curve(**self.parameters2), power_output_exp)
        assert isinstance(power_curve(**self.parameters2), np.ndarray)

    def test_power_curve_6(self):
        """TODO: Explain this test"""
        self.parameters2["density"] = np.array(self.parameters2["density"])
        assert_allclose(
            power_curve(**self.parameters2), self.power_output_exp2
        )
        assert isinstance(power_curve(**self.parameters2), np.ndarray)

    def test_power_curve_7(self):
        """TODO: Explain this test"""
        self.parameters2["power_curve_values"] = np.array(
            self.parameters2["power_curve_values"]
        )
        self.parameters2["power_curve_wind_speeds"] = np.array(
            self.parameters2["power_curve_wind_speeds"]
        )
        assert_allclose(
            power_curve(**self.parameters2), self.power_output_exp2
        )
        assert isinstance(power_curve(**self.parameters2), np.ndarray)

    def test_power_curve_8(self):
        """
        Test wind_speed as pd.Series with density and power_curve as np. array
         and pd.Series
        """
        self.parameters2["wind_speed"] = pd.Series(
            data=self.parameters2["wind_speed"]
        )
        assert_series_equal(
            power_curve(**self.parameters2), self.power_output_exp2
        )

    def test_power_curve_9(self):
        """TODO: Explain this test"""
        self.parameters2["density"] = pd.Series(
            data=self.parameters2["density"]
        )
        assert_series_equal(
            power_curve(**self.parameters2), self.power_output_exp2
        )

    def test_power_curve_10(self):
        """TODO: Explain this test"""
        self.parameters2["power_curve_wind_speeds"] = pd.Series(
            data=self.parameters2["power_curve_wind_speeds"]
        )
        self.parameters2["power_curve_values"] = pd.Series(
            data=self.parameters2["power_curve_values"]
        )
        assert_series_equal(
            power_curve(**self.parameters2), self.power_output_exp2
        )

        # Raise TypeErrors due to wrong type of `density_correction`
        with pytest.raises(TypeError):
            parameters["density"] = "wrong_type"
            power_curve(**parameters)

    def test_power_curve_density_correction(self):
        """TODO: Explain and split this test."""
        parameters = {
            "wind_speed": pd.Series(data=[2.0, 5.5, 7.0]),
            "density": pd.Series(data=[1.3, 1.3, 1.3]),
            "power_curve_wind_speeds": pd.Series([4.0, 5.0, 6.0]),
            "power_curve_values": pd.Series([300, 400, 500]),
        }

        # Test wind_speed as pd.Series with density and power_curve as
        # pd.Series and np.array
        power_output_exp = pd.Series(
            data=[0.0, 461.00290572, 0.0], name="feedin_power_plant"
        )
        assert_series_equal(
            power_curve_density_correction(**parameters), power_output_exp
        )
        parameters["density"] = np.array(parameters["density"])
        assert_series_equal(
            power_curve_density_correction(**parameters), power_output_exp
        )
        parameters["power_curve_values"] = np.array(
            parameters["power_curve_values"]
        )
        parameters["power_curve_wind_speeds"] = np.array(
            parameters["power_curve_wind_speeds"]
        )
        assert_series_equal(
            power_curve_density_correction(**parameters), power_output_exp
        )

        # Test wind_speed as np.array with density and power_curve as np.array
        # and pd.Series
        parameters["wind_speed"] = np.array(parameters["wind_speed"])
        power_output_exp = np.array([0.0, 461.00290572, 0.0])
        assert_allclose(
            power_curve_density_correction(**parameters), power_output_exp
        )
        assert isinstance(power_curve(**parameters), np.ndarray)
        parameters["density"] = pd.Series(data=parameters["density"])
        assert_allclose(
            power_curve_density_correction(**parameters), power_output_exp
        )
        assert isinstance(power_curve(**parameters), np.ndarray)
        parameters["power_curve_wind_speeds"] = pd.Series(
            data=parameters["power_curve_wind_speeds"]
        )
        parameters["power_curve_values"] = pd.Series(
            data=parameters["power_curve_values"]
        )
        assert_allclose(
            power_curve_density_correction(**parameters), power_output_exp
        )
        assert isinstance(power_curve(**parameters), np.ndarray)

        # Raise TypeError due to density is None
        with pytest.raises(TypeError):
            parameters["density"] = None
            power_curve_density_correction(**parameters)

    def test_wrong_spelling_density_correction(self):
        parameters = {
            "wind_speed": pd.Series(data=[2.0, 5.5, 7.0]),
            "density": pd.Series(data=[1.3, 1.3, 1.3]),
            "power_curve_wind_speeds": pd.Series([4.0, 5.0, 6.0]),
            "power_curve_values": pd.Series([300, 400, 500]),
        }
        msg = "is an invalid type. `density_correction` must be Boolean"
        with pytest.raises(TypeError, match=msg):
            parameters["density_correction"] = None
            power_curve(**parameters)
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Arne Neumann <discoursegraphs.programming@arne.cl>

"""
The ``merging`` module combines several document graphs into one.
So far, it is able to merge rhetorical structure theory (RS3), syntax
(TigerXML) and anaphora (ad-hoc format) annotations of the same document.
"""

import os
import sys
import argparse
from networkx import write_dot

import discoursegraphs as dg
from discoursegraphs import DiscourseDocumentGraph
from discoursegraphs.util import create_dir

def merging_cli(debug=False):
    """
    simple commandline interface of the merging module.

    This function is called when you use the ``discoursegraphs`` application
    directly on the command line.
    """
    parser = argparse.ArgumentParser()

    parser.add_argument('-t', '--tiger-file',
                        help='TigerXML (syntax) file to be merged')
    parser.add_argument('-r', '--rst-file',
                        help='RS3 (rhetorical structure) file to be merged')
    parser.add_argument('-a', '--anaphoricity-file',
                        help='anaphoricity file to be merged')
    parser.add_argument('-c', '--conano-file',
                        help='conano file to be merged')
    parser.add_argument('-m', '--mmax-file',
                        help='MMAX2 file to be merged')
    parser.add_argument(
        '-o', '--output-format', default='dot',
        help=('output format: brackets, brat, dot, pickle, geoff, gexf, gml, graphml, '
              'neo4j, exmaralda, conll, paula, no-output'))
    parser.add_argument('output_file', nargs='?', default=sys.stdout)

    args = parser.parse_args(sys.argv[1:])

    for filepath in (args.tiger_file, args.rst_file, args.anaphoricity_file,
                     args.conano_file):
        if filepath:  # if it was specified on the command line
            assert os.path.isfile(filepath), \
                "File '{}' doesn't exist".format(filepath)

    # create an empty document graph. merge it with other graphs later on.
    discourse_docgraph = DiscourseDocumentGraph()

    if args.tiger_file:
        from discoursegraphs.readwrite.tiger import TigerDocumentGraph
        tiger_docgraph = TigerDocumentGraph(args.tiger_file)
        discourse_docgraph.merge_graphs(tiger_docgraph)

    if args.rst_file:
        from discoursegraphs.readwrite.rst import RSTGraph
        rst_graph = RSTGraph(args.rst_file)
        discourse_docgraph.merge_graphs(rst_graph)

    if args.anaphoricity_file:
        from discoursegraphs.readwrite import AnaphoraDocumentGraph
        anaphora_graph = AnaphoraDocumentGraph(args.anaphoricity_file)
        discourse_docgraph.merge_graphs(anaphora_graph)
        # the anaphora doc graph only contains trivial edges from its root
        # node.
        try:
            discourse_docgraph.remove_node('anaphoricity:root_node')
        except:
            pass

    if args.conano_file:
        from discoursegraphs.readwrite import ConanoDocumentGraph
        conano_graph = ConanoDocumentGraph(args.conano_file)
        discourse_docgraph.merge_graphs(conano_graph)

    if args.mmax_file:
        from discoursegraphs.readwrite import MMAXDocumentGraph
        mmax_graph = MMAXDocumentGraph(args.mmax_file)
        discourse_docgraph.merge_graphs(mmax_graph)

    if isinstance(args.output_file, str):  # if we're not piping to stdout ...
        # we need abspath to handle files in the current directory
        path_to_output_file = \
            os.path.dirname(os.path.abspath(args.output_file))
        if not os.path.isdir(path_to_output_file):
            create_dir(path_to_output_file)

    if args.output_format == 'dot':
        write_dot(discourse_docgraph, args.output_file)
    elif args.output_format == 'brat':
        dg.write_brat(discourse_docgraph, args.output_file)
    elif args.output_format == 'brackets':
        dg.write_brackets(discourse_docgraph, args.output_file)
    elif args.output_format == 'pickle':
        import cPickle as pickle
        with open(args.output_file, 'wb') as pickle_file:
            pickle.dump(discourse_docgraph, pickle_file)
    elif args.output_format == 'geoff':
        from discoursegraphs.readwrite.neo4j import write_geoff
        write_geoff(discourse_docgraph, args.output_file)
        print ''  # this is just cosmetic for stdout
    elif args.output_format == 'gexf':
        dg.write_gexf(discourse_docgraph, args.output_file)
    elif args.output_format == 'gml':
        dg.write_gml(discourse_docgraph, args.output_file)
    elif args.output_format == 'graphml':
        dg.write_graphml(discourse_docgraph, args.output_file)
    elif args.output_format == 'neo4j':
        import requests
        from discoursegraphs.readwrite.neo4j import upload_to_neo4j
        try:
            upload_to_neo4j(discourse_docgraph)
        except requests.exceptions.ConnectionError as e:
            sys.stderr.write(
                ("Can't upload graph to Neo4j server. "
                 "Is it running?\n{}\n".format(e)))
    elif args.output_format == 'exmaralda':
        from discoursegraphs.readwrite.exmaralda import write_exb
        write_exb(discourse_docgraph, args.output_file)
    elif args.output_format == 'conll':
        from discoursegraphs.readwrite.conll import write_conll
        write_conll(discourse_docgraph, args.output_file)
    elif args.output_format == 'paula':
        from discoursegraphs.readwrite.paulaxml.paula import write_paula
        write_paula(discourse_docgraph, args.output_file)

    elif args.output_format == 'no-output':
        pass  # just testing if the merging works
    else:
        raise ValueError(
            "Unsupported output format: {}".format(args.output_format))

    if debug:
        print "Merged successfully: ", args.tiger_file

if __name__ == '__main__':
    merging_cli(debug=True)
<commit_msg>fixed cli RSTGraph creation<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Arne Neumann <discoursegraphs.programming@arne.cl>

"""
The ``merging`` module combines several document graphs into one.
So far, it is able to merge rhetorical structure theory (RS3), syntax
(TigerXML) and anaphora (ad-hoc format) annotations of the same document.
"""

import os
import sys
import argparse
from networkx import write_dot

import discoursegraphs as dg
from discoursegraphs import DiscourseDocumentGraph
from discoursegraphs.util import create_dir

def merging_cli(debug=False):
    """
    simple commandline interface of the merging module.

    This function is called when you use the ``discoursegraphs`` application
    directly on the command line.
    """
    parser = argparse.ArgumentParser()

    parser.add_argument('-t', '--tiger-file',
                        help='TigerXML (syntax) file to be merged')
    parser.add_argument('-r', '--rst-file',
                        help='RS3 (rhetorical structure) file to be merged')
    parser.add_argument('-a', '--anaphoricity-file',
                        help='anaphoricity file to be merged')
    parser.add_argument('-c', '--conano-file',
                        help='conano file to be merged')
    parser.add_argument('-m', '--mmax-file',
                        help='MMAX2 file to be merged')
    parser.add_argument(
        '-o', '--output-format', default='dot',
        help=('output format: brackets, brat, dot, pickle, geoff, gexf, gml, graphml, '
              'neo4j, exmaralda, conll, paula, no-output'))
    parser.add_argument('output_file', nargs='?', default=sys.stdout)

    args = parser.parse_args(sys.argv[1:])

    for filepath in (args.tiger_file, args.rst_file, args.anaphoricity_file,
                     args.conano_file):
        if filepath:  # if it was specified on the command line
            assert os.path.isfile(filepath), \
                "File '{}' doesn't exist".format(filepath)

    # create an empty document graph. merge it with other graphs later on.
    discourse_docgraph = DiscourseDocumentGraph()

    if args.tiger_file:
        from discoursegraphs.readwrite.tiger import TigerDocumentGraph
        tiger_docgraph = TigerDocumentGraph(args.tiger_file)
        discourse_docgraph.merge_graphs(tiger_docgraph)

    if args.rst_file:
        rst_graph = dg.read_rs3(args.rst_file)
        discourse_docgraph.merge_graphs(rst_graph)

    if args.anaphoricity_file:
        from discoursegraphs.readwrite import AnaphoraDocumentGraph
        anaphora_graph = AnaphoraDocumentGraph(args.anaphoricity_file)
        discourse_docgraph.merge_graphs(anaphora_graph)
        # the anaphora doc graph only contains trivial edges from its root
        # node.
        try:
            discourse_docgraph.remove_node('anaphoricity:root_node')
        except:
            pass

    if args.conano_file:
        from discoursegraphs.readwrite import ConanoDocumentGraph
        conano_graph = ConanoDocumentGraph(args.conano_file)
        discourse_docgraph.merge_graphs(conano_graph)

    if args.mmax_file:
        from discoursegraphs.readwrite import MMAXDocumentGraph
        mmax_graph = MMAXDocumentGraph(args.mmax_file)
        discourse_docgraph.merge_graphs(mmax_graph)

    if isinstance(args.output_file, str):  # if we're not piping to stdout ...
        # we need abspath to handle files in the current directory
        path_to_output_file = \
            os.path.dirname(os.path.abspath(args.output_file))
        if not os.path.isdir(path_to_output_file):
            create_dir(path_to_output_file)

    if args.output_format == 'dot':
        write_dot(discourse_docgraph, args.output_file)
    elif args.output_format == 'brat':
        dg.write_brat(discourse_docgraph, args.output_file)
    elif args.output_format == 'brackets':
        dg.write_brackets(discourse_docgraph, args.output_file)
    elif args.output_format == 'pickle':
        import cPickle as pickle
        with open(args.output_file, 'wb') as pickle_file:
            pickle.dump(discourse_docgraph, pickle_file)
    elif args.output_format == 'geoff':
        from discoursegraphs.readwrite.neo4j import write_geoff
        write_geoff(discourse_docgraph, args.output_file)
        print ''  # this is just cosmetic for stdout
    elif args.output_format == 'gexf':
        dg.write_gexf(discourse_docgraph, args.output_file)
    elif args.output_format == 'gml':
        dg.write_gml(discourse_docgraph, args.output_file)
    elif args.output_format == 'graphml':
        dg.write_graphml(discourse_docgraph, args.output_file)
    elif args.output_format == 'neo4j':
        import requests
        from discoursegraphs.readwrite.neo4j import upload_to_neo4j
        try:
            upload_to_neo4j(discourse_docgraph)
        except requests.exceptions.ConnectionError as e:
            sys.stderr.write(
                ("Can't upload graph to Neo4j server. "
                 "Is it running?\n{}\n".format(e)))
    elif args.output_format == 'exmaralda':
        from discoursegraphs.readwrite.exmaralda import write_exb
        write_exb(discourse_docgraph, args.output_file)
    elif args.output_format == 'conll':
        from discoursegraphs.readwrite.conll import write_conll
        write_conll(discourse_docgraph, args.output_file)
    elif args.output_format == 'paula':
        from discoursegraphs.readwrite.paulaxml.paula import write_paula
        write_paula(discourse_docgraph, args.output_file)

    elif args.output_format == 'no-output':
        pass  # just testing if the merging works
    else:
        raise ValueError(
            "Unsupported output format: {}".format(args.output_format))

    if debug:
        print "Merged successfully: ", args.tiger_file

if __name__ == '__main__':
    merging_cli(debug=True)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals

import futures
import logging

from weckan import redirections, templates, urls, wsgihelpers, contexts, queries, territories

from weckan.controllers import dashboard, dataset, group, organization, redirect, reuse, resource, tags, wiki

log = logging.getLogger(__name__)
router = None

NB_DATASETS = 12

@wsgihelpers.wsgify
def home(request):
    context = contexts.Ctx(request)
    _ = context.translator.ugettext

    fake, results = dataset.search(None, request, page_size=NB_DATASETS)
    last_datasets = queries.last_datasets(False).limit(NB_DATASETS)

    dataset_tabs = (
        ('popular', _('Most popular'), results['results']),
        ('recent', _('Latest'), dataset.serialize(last_datasets)),
    )

    return templates.render_site('home.html', context,
        featured_reuses=queries.featured_reuses().limit(NB_DATASETS),
        territory=territories.get_cookie(request),
        dataset_tabs=dataset_tabs,
    )

@wsgihelpers.wsgify
def redevances(request):
    return templates.render_site('redevances.html', contexts.Ctx(request))

@wsgihelpers.wsgify
def search_results(request):
    query = request.params.get('q', '')

    with futures.ThreadPoolExecutor(max_workers=3) as executor:
        workers = [
            executor.submit(dataset.search, query, request),
            executor.submit(organization.search, query),
            executor.submit(wiki.search, query),
        ]

    results = dict(worker.result() for worker in futures.as_completed(workers))

    return templates.render_site('search.html', request, search_query=query, has_ckan=False, **results)

@wsgihelpers.wsgify
def forbidden(request):
    return wsgihelpers.forbidden(contexts.Ctx(request))

@wsgihelpers.wsgify
def error404(request):
    return wsgihelpers.error(contexts.Ctx(request), 404)

@wsgihelpers.wsgify
def error(request):
    context = contexts.Ctx(request)
    code = int(request.urlvars.get('code', 500) or 500)
    return wsgihelpers.error(context, code)

@wsgihelpers.wsgify
def redirect_old_dataset(request):
    context = contexts.Ctx(request)
    dataset_name = redirections.dataset_name_by_old_id.get(request.urlvars['id'])
    if dataset_name is None:
        return wsgihelpers.not_found(context)
    return wsgihelpers.redirect(context, location = urls.get_url(context, 'fr', 'dataset', dataset_name))

def make_router(app):
    """Return a WSGI application that searches requests to controllers """
    global router
    controllers = group, organization, dashboard, dataset, redirect, resource, reuse, tags
    routes = (controller.routes for controller in controllers)
    routes = sum(routes, tuple())
    catchall = (
        ('GET', r'^(/(?P<lang>\w{2}))?/[\w\d_-]+/?$', error404),
    )
    routes = routes + catchall
    router = urls.make_router(app,
        ('GET', r'^/DataSet/(?P<id>\d+)/?$', redirect_old_dataset),
        ('GET', r'^/Redevances/?$', redevances),
        ('GET', r'^(/(?P<lang>\w{2}))?/?$', home),
        ('GET', r'^(/(?P<lang>\w{2}))?/error(/(?P<code>\d{3}))?/?$', error),
        ('GET', r'^(/(?P<lang>\w{2}))?/search/?$', search_results),
        ('GET', r'^(/(?P<lang>\w{2}))?/users/?$', forbidden),
        *routes
    )

    return router
<commit_msg>Redirect from old /donnees/view/... URLs.<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

import futures
import logging

from weckan import redirections, templates, urls, wsgihelpers, contexts, queries, territories

from weckan.controllers import dashboard, dataset, group, organization, redirect, reuse, resource, tags, wiki

log = logging.getLogger(__name__)
router = None

NB_DATASETS = 12

@wsgihelpers.wsgify
def home(request):
    context = contexts.Ctx(request)
    _ = context.translator.ugettext

    fake, results = dataset.search(None, request, page_size=NB_DATASETS)
    last_datasets = queries.last_datasets(False).limit(NB_DATASETS)

    dataset_tabs = (
        ('popular', _('Most popular'), results['results']),
        ('recent', _('Latest'), dataset.serialize(last_datasets)),
    )

    return templates.render_site('home.html', context,
        featured_reuses=queries.featured_reuses().limit(NB_DATASETS),
        territory=territories.get_cookie(request),
        dataset_tabs=dataset_tabs,
    )

@wsgihelpers.wsgify
def redevances(request):
    return templates.render_site('redevances.html', contexts.Ctx(request))

@wsgihelpers.wsgify
def search_results(request):
    query = request.params.get('q', '')

    with futures.ThreadPoolExecutor(max_workers=3) as executor:
        workers = [
            executor.submit(dataset.search, query, request),
            executor.submit(organization.search, query),
            executor.submit(wiki.search, query),
        ]

    results = dict(worker.result() for worker in futures.as_completed(workers))

    return templates.render_site('search.html', request, search_query=query, has_ckan=False, **results)

@wsgihelpers.wsgify
def forbidden(request):
    return wsgihelpers.forbidden(contexts.Ctx(request))

@wsgihelpers.wsgify
def error404(request):
    return wsgihelpers.error(contexts.Ctx(request), 404)

@wsgihelpers.wsgify
def error(request):
    context = contexts.Ctx(request)
    code = int(request.urlvars.get('code', 500) or 500)
    return wsgihelpers.error(context, code)

@wsgihelpers.wsgify
def redirect_old_dataset(request):
    context = contexts.Ctx(request)
    dataset_name = redirections.dataset_name_by_old_id.get(request.urlvars['id'])
    if dataset_name is None:
        return wsgihelpers.not_found(context)
    return wsgihelpers.redirect(context, location = urls.get_url(context, 'fr', 'dataset', dataset_name))

def make_router(app):
    """Return a WSGI application that searches requests to controllers """
    global router
    controllers = group, organization, dashboard, dataset, redirect, resource, reuse, tags
    routes = (controller.routes for controller in controllers)
    routes = sum(routes, tuple())
    catchall = (
        ('GET', r'^(/(?P<lang>\w{2}))?/[\w\d_-]+/?$', error404),
    )
    routes = routes + catchall
    router = urls.make_router(app,
        ('GET', r'^/DataSet/(?P<id>\d+)/?$', redirect_old_dataset),
        ('GET', r'^/donnees/view/[^/]+-(?P<id>\d+)/?$', redirect_old_dataset),
        ('GET', r'^/Redevances/?$', redevances),
        ('GET', r'^(/(?P<lang>\w{2}))?/?$', home),
        ('GET', r'^(/(?P<lang>\w{2}))?/error(/(?P<code>\d{3}))?/?$', error),
        ('GET', r'^(/(?P<lang>\w{2}))?/search/?$', search_results),
        ('GET', r'^(/(?P<lang>\w{2}))?/users/?$', forbidden),
        *routes
    )

    return router
<|endoftext|>


<commit_before>import os
from distutils import log
import itertools

from setuptools.extern.six.moves import map

flatten = itertools.chain.from_iterable

class Installer:

    nspkg_ext = '-nspkg.pth'

    def install_namespaces(self):
        nsp = self._get_all_ns_packages()
        if not nsp:
            return
        filename, ext = os.path.splitext(self._get_target())
        filename += self.nspkg_ext
        self.outputs.append(filename)
        log.info("Installing %s", filename)
        lines = map(self._gen_nspkg_line, nsp)

        if self.dry_run:
            # always generate the lines, even in dry run
            list(lines)
            return

        with open(filename, 'wt') as f:
            f.writelines(lines)

    def uninstall_namespaces(self):
        filename, ext = os.path.splitext(self._get_target())
        filename += self.nspkg_ext
        if not os.path.exists(filename):
            return
        log.info("Removing %s", filename)
        os.remove(filename)

    def _get_target(self):
        return self.target

    _nspkg_tmpl = (
        "import sys, types, os",
        "pep420 = sys.version_info > (3, 3)",
        "p = os.path.join(%(root)s, *%(pth)r)",
        "m = not pep420 and "
            "sys.modules.setdefault(%(pkg)r, types.ModuleType(%(pkg)r))",
        "mp = (m or []) and m.__dict__.setdefault('__path__',[])",
        "(p not in mp) and mp.append(p)",
    )
    "lines for the namespace installer"

    _nspkg_tmpl_multi = (
        'm and setattr(sys.modules[%(parent)r], %(child)r, m)',
    )
    "additional line(s) when a parent package is indicated"

    def _get_root(self):
        return "sys._getframe(1).f_locals['sitedir']"

    def _gen_nspkg_line(self, pkg):
        # ensure pkg is not a unicode string under Python 2.7
        pkg = str(pkg)
        pth = tuple(pkg.split('.'))
        root = self._get_root()
        tmpl_lines = self._nspkg_tmpl
        parent, sep, child = pkg.rpartition('.')
        if parent:
            tmpl_lines += self._nspkg_tmpl_multi
        return ';'.join(tmpl_lines) % locals() + '\n'

    def _get_all_ns_packages(self):
        """Return sorted list of all package namespaces"""
        pkgs = self.distribution.namespace_packages or []
        return sorted(flatten(map(self._pkg_names, pkgs)))

    @staticmethod
    def _pkg_names(pkg):
        """
        Given a namespace package, yield the components of that
        package.

        >>> names = Installer._pkg_names('a.b.c')
        >>> set(names) == set(['a', 'a.b', 'a.b.c'])
        True
        """
        parts = pkg.split('.')
        while parts:
            yield '.'.join(parts)
            parts.pop()

class DevelopInstaller(Installer):
    def _get_root(self):
        return repr(str(self.egg_path))

    def _get_target(self):
        return self.egg_link
<commit_msg>Provisionally revert the -nspkg.pth suppression on PEP 420 Pythons. Ref #250.<commit_after>import os
from distutils import log
import itertools

from setuptools.extern.six.moves import map

flatten = itertools.chain.from_iterable

class Installer:

    nspkg_ext = '-nspkg.pth'

    def install_namespaces(self):
        nsp = self._get_all_ns_packages()
        if not nsp:
            return
        filename, ext = os.path.splitext(self._get_target())
        filename += self.nspkg_ext
        self.outputs.append(filename)
        log.info("Installing %s", filename)
        lines = map(self._gen_nspkg_line, nsp)

        if self.dry_run:
            # always generate the lines, even in dry run
            list(lines)
            return

        with open(filename, 'wt') as f:
            f.writelines(lines)

    def uninstall_namespaces(self):
        filename, ext = os.path.splitext(self._get_target())
        filename += self.nspkg_ext
        if not os.path.exists(filename):
            return
        log.info("Removing %s", filename)
        os.remove(filename)

    def _get_target(self):
        return self.target

    _nspkg_tmpl = (
        "import sys, types, os",
        "pep420 = sys.version_info > (3, 3)",
        "p = os.path.join(%(root)s, *%(pth)r)",
        "m = "
            "sys.modules.setdefault(%(pkg)r, types.ModuleType(%(pkg)r))",
        "mp = (m or []) and m.__dict__.setdefault('__path__',[])",
        "(p not in mp) and mp.append(p)",
    )
    "lines for the namespace installer"

    _nspkg_tmpl_multi = (
        'm and setattr(sys.modules[%(parent)r], %(child)r, m)',
    )
    "additional line(s) when a parent package is indicated"

    def _get_root(self):
        return "sys._getframe(1).f_locals['sitedir']"

    def _gen_nspkg_line(self, pkg):
        # ensure pkg is not a unicode string under Python 2.7
        pkg = str(pkg)
        pth = tuple(pkg.split('.'))
        root = self._get_root()
        tmpl_lines = self._nspkg_tmpl
        parent, sep, child = pkg.rpartition('.')
        if parent:
            tmpl_lines += self._nspkg_tmpl_multi
        return ';'.join(tmpl_lines) % locals() + '\n'

    def _get_all_ns_packages(self):
        """Return sorted list of all package namespaces"""
        pkgs = self.distribution.namespace_packages or []
        return sorted(flatten(map(self._pkg_names, pkgs)))

    @staticmethod
    def _pkg_names(pkg):
        """
        Given a namespace package, yield the components of that
        package.

        >>> names = Installer._pkg_names('a.b.c')
        >>> set(names) == set(['a', 'a.b', 'a.b.c'])
        True
        """
        parts = pkg.split('.')
        while parts:
            yield '.'.join(parts)
            parts.pop()

class DevelopInstaller(Installer):
    def _get_root(self):
        return repr(str(self.egg_path))

    def _get_target(self):
        return self.egg_link
<|endoftext|>