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

"""
This module implements a simple cross-correlation code for determining RVs.

It is a heavily cleaned up version of Jane Lin's GUESS code, as used by GALAH. Original code taken from
<https://github.com/jlin0504/GUESS>.
"""

from math import sqrt

import numpy as np
from scipy.optimize import leastsq
import logging

import fourgp_speclib
from fourgp_degrade.resample import SpectrumResampler

logger = logging.getLogger(__name__)

class RvInstanceCrossCorrelation(object):
    """
    A class which is adapted from Jane Lin's GUESS code, as used by GALAH.
    """

    def __init__(self, spectrum_library):
        """
        Instantiate the RV code, and read from disk the library of template spectra used for cross correlation.

        :param spectrum_library:
            A SpectrumLibrary containing the template spectra we use for modelling.

        :type spectrum_library:
            SpectrumLibrary
        """

        assert isinstance(spectrum_library, fourgp_speclib.SpectrumLibrary), \
            "Argument to RvInstanceCrossCorrelation should be a SpectrumLibrary."
        self._spectrum_library = spectrum_library

        # Load template spectra
        spectrum_list = self._spectrum_library.search(continuum_normalised=1)

        # Make a list of templates by 4MOST wavelength arm
        self.templates_by_arm = {}
        self.arm_rasters = {}
        self.arm_properties = {}
        self.window_functions = {}

        # Sort template spectra by the arm they are sampled on
        for template_id in [i["specId"] for i in spectrum_list]:
            template_metadata = self._spectrum_library.get_metadata(ids=(template_id,))[0]
            mode = template_metadata['mode']
            arm_name = template_metadata['arm_name']

            if mode not in self.templates_by_arm:
                self.templates_by_arm[mode] = {}
            if arm_name not in self.templates_by_arm[mode]:
                self.templates_by_arm[mode][arm_name] = []

            # Add this template to the list of available templates for this wavelength arm
            self.templates_by_arm[mode][arm_name].append(template_id)

            # If we haven't already recreated the fixed-log-step wavelength raster for this arm, do it now
            if arm_name not in self.arm_rasters:
                self.arm_properties[arm_name] = {
                    'lambda_min': template_metadata['lambda_min'],
                    'lambda_max': template_metadata['lambda_max'],
                    'lambda_step': template_metadata['lambda_step'],
                }

        # Load library of template spectra for each arm
        self.template_spectra = {}

        for mode in self.templates_by_arm:
            for arm_name in self.templates_by_arm[mode]:
                self.template_spectra[arm_name] = self._spectrum_library.open(
                    ids=self.templates_by_arm[mode][arm_name],
                    shared_memory=True
                )

                self.arm_rasters[arm_name] = self.template_spectra[arm_name].wavelengths

                self.arm_properties[arm_name]['multiplicative_step'] = (self.arm_rasters[arm_name][1] /
                                                                        self.arm_rasters[arm_name][0])

                self.window_functions[arm_name] = self.window_function(template_length=len(self.arm_rasters[arm_name]))

        # Multiply template spectra by window function
        for arm_name in self.template_spectra:
            for index in range(len(self.template_spectra[arm_name])):
                template_spectrum = self.template_spectra[arm_name].extract_item(index=index)
                template_spectrum.values *= self.window_functions[arm_name]

    @staticmethod
    def window_function(template_length):
        """
        Create a window function that we multiply each template by to avoid edge-effects. This goes to zero at either
        end.

        :param template_length:
            The number of pixels in the template spectrum we want to create a window function for.
        :return:
            np.array
        """

        # Create a linear ramp covering 10% of the length of the spectrum
        left_ramp = np.arange(int(template_length / 10))
        left_ramp = left_ramp / max(left_ramp)

        # Ramp to use at right-hand end of spectrum is simply a back-to-front version of the left ramp
        right_ramp = left_ramp[::-1]

        middle_length = template_length - len(left_ramp) - len(right_ramp)

        window_function = np.hstack([left_ramp,
                                     np.ones(middle_length),
                                     right_ramp
                                     ])

        return window_function

    def resample_single_arm(self, input_spectrum, arm_name):
        """
        Resample an input spectrum onto a raster with fixed logarithmic stride, representing a single 4MOST arm. We
        use the same wavelength rasters that the template spectra were sampled onto.

        :param input_spectrum:
            A Spectrum object, containing an observed spectrum
        :param arm_name:
            The name of the arm within this 4MOST mode
        :return:
            A Spectrum object containing a single arm of 4MOST data, resampled with a fixed logarithmic stride.
        """

        new_raster = self.arm_rasters[arm_name]

        resampler = SpectrumResampler(input_spectrum=input_spectrum)

        resampled_spectrum = resampler.onto_raster(output_raster=new_raster, resample_errors=True, resample_mask=False)

        return resampled_spectrum

    def estimate_rv_from_single_arm(self, input_spectrum, mode, arm_name):
        """
        Estimate the RV of a spectrum on the basis of data from a single arm. We return a list of RV estimates from
        cross correlation with each of the template spectra, and the chi-squared mismatch of each template spectrum
        which should be used to weight the RV estimates.

        :param input_spectrum:
            A Spectrum object, containing an observed spectrum. This should represent one arm of data only, and should
            be sampled onto the same fixed logarithmic wavelength stride as the template spectra.
        :param mode:
            The name of the 4MOST mode this arm is part of -- either LRS or HRS
        :param arm_name:
            The name of the arm within this 4MOST mode
        :return:
            List of [RV value, weight]
        """

        rv_fits = []

        # Loop over all template spectra
        for template_index, template_id in enumerate(self.templates_by_arm[mode][arm_name]):
            template_spectrum = self.template_spectra[arm_name].extract_item(template_index)

            input_array = input_spectrum.values * self.window_functions[arm_name]

            cross_correlation = np.correlate(a=template_spectrum.values,
                                             v=input_array,
                                             mode='same')

            # Find the index of the maximum of cross correlation function
            max_position = np.where(cross_correlation == max(cross_correlation))[0][0]

            # Make sure we don't go off the end of the array
            if max_position < 1:
                max_position = 1
            if max_position > len(input_array) - 2:
                max_position = len(input_array) - 2

            # Now make three points which straddle the maximum
            x_vals = np.array([max_position - 1, max_position, max_position + 1])
            y_vals = cross_correlation[x_vals]

            # Put peak close to zero for numerical stability
            x_vals = x_vals - max_position
            y_peak = y_vals[1]
            y_vals = y_vals - y_peak

            # Fit a quadratic curve through three points
            def quadratic_curve(p, x):
                return p[0] * x ** 2 + p[1] * x + p[2]

            def quadratic_peak_x(p):
                return -p[1] / (2 * p[0])

            def quadratic_mismatch(p, x, y):
                return quadratic_curve(p, x) - y

            p0, p1, p2 = leastsq(func=quadratic_mismatch,
                                 x0=np.array([0, 0, 0]),
                                 args=(x_vals, y_vals),
                                 maxfev=10000
                                 )[0]

            peak_x = quadratic_peak_x(p=(p0, p1, p2))

            # Shift peak back from zero to original position
            peak_x = peak_x + max_position

            # Convert position of peak of correlation function into a shift in pixels
            pixel_shift = peak_x - len(input_array) // 2

            # Convert pixel shift into multiplicative change in wavelength, based on fixed logarithmic stride
            multiplicative_shift = pow(self.arm_properties[arm_name]['multiplicative_step'], pixel_shift)

            # Convert multiplicative wavelength shift into a radial velocity
            c = 299792458.0
            velocity = -c * (pow(multiplicative_shift, 2) - 1) / (pow(multiplicative_shift, 2) + 1)
            weight = max(cross_correlation)

            rv_fits.append((velocity, weight, (max_position, len(input_array), x_vals, y_vals, (p0,p1,p2), peak_x, pixel_shift, multiplicative_shift, velocity, weight)))

        return rv_fits

    def estimate_rv(self, input_spectrum, mode):
        """
        Estimate the RV of a spectrum on the basis of all of the 4MOST arms of either HRS or LRS.

        :param input_spectrum:
            A Spectrum object, containing an observed spectrum
        :param mode:
            The name of the 4MOST mode this arm is part of -- either LRS or HRS
        :return:
            [RV, error in RV]
        """

        rv_estimates = []

        arm_names = self.templates_by_arm[mode].keys()

        for arm_name in arm_names:
            new_rv_estimates = self.estimate_rv_from_single_arm(
                input_spectrum=self.resample_single_arm(input_spectrum=input_spectrum, arm_name=arm_name),
                mode=mode,
                arm_name=arm_name
            )
            rv_estimates.extend(new_rv_estimates)

        rv_mean = (sum([rv * weight for rv, weight, metadata in rv_estimates]) /
                   sum([weight for rv, weight, metadata in rv_estimates]))

        rv_variance = (sum([pow(rv - rv_mean, 2) * weight for rv, weight, metadata in rv_estimates]) /
                       sum([weight for rv, weight, metadata in rv_estimates]))

        rv_std_dev = sqrt(rv_variance)

        all_metadata = [metadata for rv, weight, metadata in rv_estimates]

        return rv_mean, rv_std_dev, all_metadata
<commit_msg>Calculate the interquartile mean of the RV estimates, excluding outlying points<commit_after># -*- coding: utf-8 -*-

"""
This module implements a simple cross-correlation code for determining RVs.

It is a heavily cleaned up version of Jane Lin's GUESS code, as used by GALAH. Original code taken from
<https://github.com/jlin0504/GUESS>.
"""

from math import sqrt

import numpy as np
from scipy.optimize import leastsq
import logging
from operator import itemgetter

import fourgp_speclib
from fourgp_degrade.resample import SpectrumResampler

logger = logging.getLogger(__name__)

class RvInstanceCrossCorrelation(object):
    """
    A class which is adapted from Jane Lin's GUESS code, as used by GALAH.
    """

    def __init__(self, spectrum_library):
        """
        Instantiate the RV code, and read from disk the library of template spectra used for cross correlation.

        :param spectrum_library:
            A SpectrumLibrary containing the template spectra we use for modelling.

        :type spectrum_library:
            SpectrumLibrary
        """

        assert isinstance(spectrum_library, fourgp_speclib.SpectrumLibrary), \
            "Argument to RvInstanceCrossCorrelation should be a SpectrumLibrary."
        self._spectrum_library = spectrum_library

        # Load template spectra
        spectrum_list = self._spectrum_library.search(continuum_normalised=1)

        # Make a list of templates by 4MOST wavelength arm
        self.templates_by_arm = {}
        self.arm_rasters = {}
        self.arm_properties = {}
        self.window_functions = {}

        # Sort template spectra by the arm they are sampled on
        for template_id in [i["specId"] for i in spectrum_list]:
            template_metadata = self._spectrum_library.get_metadata(ids=(template_id,))[0]
            mode = template_metadata['mode']
            arm_name = template_metadata['arm_name']

            if mode not in self.templates_by_arm:
                self.templates_by_arm[mode] = {}
            if arm_name not in self.templates_by_arm[mode]:
                self.templates_by_arm[mode][arm_name] = []

            # Add this template to the list of available templates for this wavelength arm
            self.templates_by_arm[mode][arm_name].append(template_id)

            # If we haven't already recreated the fixed-log-step wavelength raster for this arm, do it now
            if arm_name not in self.arm_rasters:
                self.arm_properties[arm_name] = {
                    'lambda_min': template_metadata['lambda_min'],
                    'lambda_max': template_metadata['lambda_max'],
                    'lambda_step': template_metadata['lambda_step'],
                }

        # Load library of template spectra for each arm
        self.template_spectra = {}

        for mode in self.templates_by_arm:
            for arm_name in self.templates_by_arm[mode]:
                self.template_spectra[arm_name] = self._spectrum_library.open(
                    ids=self.templates_by_arm[mode][arm_name],
                    shared_memory=True
                )

                self.arm_rasters[arm_name] = self.template_spectra[arm_name].wavelengths

                self.arm_properties[arm_name]['multiplicative_step'] = (self.arm_rasters[arm_name][1] /
                                                                        self.arm_rasters[arm_name][0])

                self.window_functions[arm_name] = self.window_function(template_length=len(self.arm_rasters[arm_name]))

        # Multiply template spectra by window function
        for arm_name in self.template_spectra:
            for index in range(len(self.template_spectra[arm_name])):
                template_spectrum = self.template_spectra[arm_name].extract_item(index=index)
                template_spectrum.values *= self.window_functions[arm_name]

    @staticmethod
    def window_function(template_length):
        """
        Create a window function that we multiply each template by to avoid edge-effects. This goes to zero at either
        end.

        :param template_length:
            The number of pixels in the template spectrum we want to create a window function for.
        :return:
            np.array
        """

        # Create a linear ramp covering 10% of the length of the spectrum
        left_ramp = np.arange(int(template_length / 10))
        left_ramp = left_ramp / max(left_ramp)

        # Ramp to use at right-hand end of spectrum is simply a back-to-front version of the left ramp
        right_ramp = left_ramp[::-1]

        middle_length = template_length - len(left_ramp) - len(right_ramp)

        window_function = np.hstack([left_ramp,
                                     np.ones(middle_length),
                                     right_ramp
                                     ])

        return window_function

    def resample_single_arm(self, input_spectrum, arm_name):
        """
        Resample an input spectrum onto a raster with fixed logarithmic stride, representing a single 4MOST arm. We
        use the same wavelength rasters that the template spectra were sampled onto.

        :param input_spectrum:
            A Spectrum object, containing an observed spectrum
        :param arm_name:
            The name of the arm within this 4MOST mode
        :return:
            A Spectrum object containing a single arm of 4MOST data, resampled with a fixed logarithmic stride.
        """

        new_raster = self.arm_rasters[arm_name]

        resampler = SpectrumResampler(input_spectrum=input_spectrum)

        resampled_spectrum = resampler.onto_raster(output_raster=new_raster, resample_errors=True, resample_mask=False)

        return resampled_spectrum

    def estimate_rv_from_single_arm(self, input_spectrum, mode, arm_name):
        """
        Estimate the RV of a spectrum on the basis of data from a single arm. We return a list of RV estimates from
        cross correlation with each of the template spectra, and the chi-squared mismatch of each template spectrum
        which should be used to weight the RV estimates.

        :param input_spectrum:
            A Spectrum object, containing an observed spectrum. This should represent one arm of data only, and should
            be sampled onto the same fixed logarithmic wavelength stride as the template spectra.
        :param mode:
            The name of the 4MOST mode this arm is part of -- either LRS or HRS
        :param arm_name:
            The name of the arm within this 4MOST mode
        :return:
            List of [RV value, weight]
        """

        rv_fits = []

        # Loop over all template spectra
        for template_index, template_id in enumerate(self.templates_by_arm[mode][arm_name]):
            template_spectrum = self.template_spectra[arm_name].extract_item(template_index)

            input_array = input_spectrum.values * self.window_functions[arm_name]

            cross_correlation = np.correlate(a=template_spectrum.values,
                                             v=input_array,
                                             mode='same')

            # Find the index of the maximum of cross correlation function
            max_position = np.where(cross_correlation == max(cross_correlation))[0][0]

            # Make sure we don't go off the end of the array
            if max_position < 1:
                max_position = 1
            if max_position > len(input_array) - 2:
                max_position = len(input_array) - 2

            # Now make three points which straddle the maximum
            x_vals = np.array([max_position - 1, max_position, max_position + 1])
            y_vals = cross_correlation[x_vals]

            # Put peak close to zero for numerical stability
            x_vals = x_vals - max_position
            y_peak = y_vals[1]
            y_vals = y_vals - y_peak

            # Fit a quadratic curve through three points
            def quadratic_curve(p, x):
                return p[0] * x ** 2 + p[1] * x + p[2]

            def quadratic_peak_x(p):
                return -p[1] / (2 * p[0])

            def quadratic_mismatch(p, x, y):
                return quadratic_curve(p, x) - y

            p0, p1, p2 = leastsq(func=quadratic_mismatch,
                                 x0=np.array([0, 0, 0]),
                                 args=(x_vals, y_vals),
                                 maxfev=10000
                                 )[0]

            peak_x = quadratic_peak_x(p=(p0, p1, p2))

            # Shift peak back from zero to original position
            peak_x = peak_x + max_position

            # Convert position of peak of correlation function into a shift in pixels
            pixel_shift = peak_x - len(input_array) // 2

            # Convert pixel shift into multiplicative change in wavelength, based on fixed logarithmic stride
            multiplicative_shift = pow(self.arm_properties[arm_name]['multiplicative_step'], pixel_shift)

            # Convert multiplicative wavelength shift into a radial velocity
            c = 299792458.0
            velocity = -c * (pow(multiplicative_shift, 2) - 1) / (pow(multiplicative_shift, 2) + 1)
            weight = max(cross_correlation)

            rv_fits.append((velocity, weight, (max_position, len(input_array), x_vals, y_vals, (p0, p1, p2), peak_x,
                                               pixel_shift, multiplicative_shift, velocity, weight)
                            ))

        return rv_fits

    def estimate_rv(self, input_spectrum, mode):
        """
        Estimate the RV of a spectrum on the basis of all of the 4MOST arms of either HRS or LRS.

        :param input_spectrum:
            A Spectrum object, containing an observed spectrum
        :param mode:
            The name of the 4MOST mode this arm is part of -- either LRS or HRS
        :return:
            [RV, error in RV]
        """

        rv_estimates = []

        arm_names = self.templates_by_arm[mode].keys()

        # Compile a list of all the RV estimates, from all the arms and all the templates
        for arm_name in arm_names:
            new_rv_estimates = self.estimate_rv_from_single_arm(
                input_spectrum=self.resample_single_arm(input_spectrum=input_spectrum, arm_name=arm_name),
                mode=mode,
                arm_name=arm_name
            )
            rv_estimates.extend(new_rv_estimates)

        # Sort all the RV estimates into order of RV, and chuck out the bottom and top quartiles. This
        # excludes estimates close to the speed of light from the subsequent statistics.
        rv_estimates.sort(key=itemgetter(0))
        rv_estimates = rv_estimates[len(rv_estimates) // 4: len(rv_estimates) * 3 // 4]

        # Now form a weighted mean of all the RV estimates
        rv_mean = (sum([rv * weight for rv, weight, metadata in rv_estimates]) /
                   sum([weight for rv, weight, metadata in rv_estimates]))

        # Work out the standard deviation of the mean
        rv_variance = (sum([pow(rv - rv_mean, 2) * weight for rv, weight, metadata in rv_estimates]) /
                       sum([weight for rv, weight, metadata in rv_estimates]))

        rv_std_dev = sqrt(rv_variance)

        all_metadata = [metadata for rv, weight, metadata in rv_estimates]

        return rv_mean, rv_std_dev, all_metadata
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# =============================================================================
# DOC
# =============================================================================

"""All settings tests"""

# =============================================================================
# IMPORTS
# =============================================================================

import inspect

import mock

from corral import db, util

from . import models
from .base import BaseTest

# =============================================================================
# BASE CLASS
# =============================================================================

class TestDB(BaseTest):

    def setup(self):
        self.tn2model = {
            sc.__tablename__: sc for sc in util.collect_subclasses(db.Model)}

    def test_models_subsclasess(self):
        actual = [
            v for k, v in vars(models).items()
            if inspect.isclass(v) and issubclass(v, db.Model)
            and not k.startswith("_")]
        self.assertCountEqual(actual, self.tn2model.values())

    def test_model_module_correctly_imported(self):
        self.assertIs(models, db.load_models_module())

    def test_tables_attributes(self):
        metadata = db.Model.metadata
        for table_name, table in metadata.tables.items():
            model = self.tn2model[table_name]
            self.assertIs(model.__table__, table)

    @mock.patch("corral.db.Model")
    @mock.patch("corral.db.Session")
    @mock.patch("corral.db.engine")
    def test_setup_don_change_engine_model_and_session(self, *args):
        engine, Session, Model = args
        db.setup()
        self.assertIs(db.engine, engine)
        self.assertIs(db.Session, Session)
        self.assertIs(db.Model, Model)

    def tests_create_all(self):
        with mock.patch("corral.db.Model.metadata.create_all") as m_create_all:
            db.create_all()
            self.assertTrue(m_create_all.called)

        with mock.patch("corral.db.Model.metadata.create_all") as m_create_all:
            db.create_all(a=1)
            self.assertTrue(m_create_all.called)
            self.assertEquals(m_create_all.call_args[1], {"a": 1})

# =============================================================================
# MAIN
# =============================================================================

if __name__ == "__main__":
    print(__doc__)
<commit_msg>fix on tests and the coverege fail :(<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# =============================================================================
# DOC
# =============================================================================

"""All settings tests"""

# =============================================================================
# IMPORTS
# =============================================================================

import inspect

import mock

from corral import db, util

from . import models
from .base import BaseTest

# =============================================================================
# BASE CLASS
# =============================================================================

class TestDB(BaseTest):

    def setup(self):
        self.tn2model = {
            sc.__tablename__: sc for sc in util.collect_subclasses(db.Model)}

    def test_models_subsclasess(self):
        actual = [
            v for k, v in vars(models).items()
            if inspect.isclass(v) and issubclass(v, db.Model)
            and not k.startswith("_")]
        actual += [
            v for k, v in vars(db.load_default_models()).items()
            if inspect.isclass(v) and issubclass(v, db.Model)
            and not k.startswith("_")]
        self.assertCountEqual(actual, self.tn2model.values())

    def test_model_module_correctly_imported(self):
        self.assertIs(models, db.load_models_module())

    def test_tables_attributes(self):
        metadata = db.Model.metadata
        for table_name, table in metadata.tables.items():
            model = self.tn2model[table_name]
            self.assertIs(model.__table__, table)

    @mock.patch("corral.db.Model")
    @mock.patch("corral.db.Session")
    @mock.patch("corral.db.engine")
    def test_setup_don_change_engine_model_and_session(self, *args):
        engine, Session, Model = args
        db.setup()
        self.assertIs(db.engine, engine)
        self.assertIs(db.Session, Session)
        self.assertIs(db.Model, Model)

    def tests_create_all(self):
        with mock.patch("corral.db.Model.metadata.create_all") as m_create_all:
            db.create_all()
            self.assertTrue(m_create_all.called)

        with mock.patch("corral.db.Model.metadata.create_all") as m_create_all:
            db.create_all(a=1)
            self.assertTrue(m_create_all.called)
            self.assertEquals(m_create_all.call_args[1], {"a": 1})

# =============================================================================
# MAIN
# =============================================================================

if __name__ == "__main__":
    print(__doc__)
<|endoftext|>


<commit_before>from __future__ import absolute_import, print_function, unicode_literals
from builtins import dict, str
from indra.java_vm import autoclass, JavaException
from indra.biopax import pathway_commons_client as pcc
from indra.biopax.processor import BiopaxProcessor

def process_pc_neighborhood(gene_names, neighbor_limit=1):
    """Returns a BiopaxProcessor for a PathwayCommons neighborhood query.

    The neighborhood query finds the neighborhood around a set of source genes.

    http://www.pathwaycommons.org/pc2/#graph

    http://www.pathwaycommons.org/pc2/#graph_kind

    Parameters
    ----------
    gene_names : list
        A list of HGNC gene symbols to search the neighborhood of.
        Examples: ['BRAF'], ['BRAF', 'MAP2K1']
    neighbor_limit : Optional[int]
        The number of steps to limit the size of the neighborhood around
        the gene names being queried. Default: 1

    Returns
    -------
    bp : BiopaxProcessor
        A BiopaxProcessor containing the obtained BioPAX model in bp.model.

    Notes
    -----
    As returned from this function, bp.statements is empty and one
    has to call methods of bp (e.g. bp.get_complexes()) to
    populate bp.statements.
    """
    model = pcc.graph_query('neighborhood', gene_names,
                            neighbor_limit=neighbor_limit)
    if model is not None:
        return process_model(model)

def process_pc_pathsbetween(gene_names, neighbor_limit=1):
    """Returns a BiopaxProcessor for a PathwayCommons paths-between query.

    The paths-between query finds the paths between a set of genes. Here
    source gene names are given in a single list and all directions of paths
    between these genes are considered.

    http://www.pathwaycommons.org/pc2/#graph

    http://www.pathwaycommons.org/pc2/#graph_kind

    Parameters
    ----------
    gene_names : list
        A list of HGNC gene symbols to search for paths between.
        Examples: ['BRAF', 'MAP2K1']
    neighbor_limit : Optional[int]
        The number of steps to limit the length of the paths between
        the gene names being queried. Default: 1

    Returns
    -------
    bp : BiopaxProcessor
        A BiopaxProcessor containing the obtained BioPAX model in bp.model.

    Notes
    -----
    As returned from this function, bp.statements is empty and one
    has to call methods of bp (e.g. bp.get_complexes()) to
    populate bp.statements.
    """
    model = pcc.graph_query('pathsbetween', gene_names,
                             neighbor_limit=neighbor_limit)
    if model is not None:
        return process_model(model)

def process_pc_pathsfromto(source_genes, target_genes, neighbor_limit=1):
    """Returns a BiopaxProcessor for a PathwayCommons paths-from-to query.

    The paths-from-to query finds the paths from a set of source genes to
    a set of target genes.

    http://www.pathwaycommons.org/pc2/#graph

    http://www.pathwaycommons.org/pc2/#graph_kind

    Parameters
    ----------
    source_genes : list
        A list of HGNC gene symbols that are the sources of paths being
        searched for.
        Examples: ['BRAF', 'RAF1', 'ARAF']
    target_genes : list
        A list of HGNC gene symbols that are the targets of paths being
        searched for.
        Examples: ['MAP2K1', 'MAP2K2']
    neighbor_limit : Optional[int]
        The number of steps to limit the length of the paths
        between the source genes and target genes being queried. Default: 1

    Returns
    -------
    bp : BiopaxProcessor
        A BiopaxProcessor containing the obtained BioPAX model in bp.model.

    Notes
    -----
    As returned from this function, bp.statements is empty and one
    has to call methods of bp (e.g. bp.get_complexes()) to
    populate bp.statements.
    """
    model = pcc.graph_query('pathsfromto', source_genes,
                             target_genes, neighbor_limit)
    if model is not None:
        return process_model(model)

def process_owl(owl_filename):
    """Returns a BiopaxProcessor for a BioPAX OWL file.

    Parameters
    ----------
    owl_filename : string
        The name of the OWL file to process.

    Returns
    -------
    bp : BiopaxProcessor
        A BiopaxProcessor containing the obtained BioPAX model in bp.model.

    Notes
    -----
    As returned from this function, bp.statements is empty and one
    has to call methods of bp (e.g. bp.get_complexes()) to
    populate bp.statements.
    """
    model = pcc.owl_to_model(owl_filename)
    return process_model(model)

def process_model(model):
    """Returns a BiopaxProcessor for a BioPAX model object.

    Parameters
    ----------
    model : org.biopax.paxtools.model.Model
        A BioPAX model object.

    Returns
    -------
    bp : BiopaxProcessor
        A BiopaxProcessor containing the obtained BioPAX model in bp.model.

    Notes
    -----
    As returned from this function, bp.statements is empty and one
    has to call methods of bp (e.g. bp.get_complexes()) to
    populate bp.statements.
    """
    bproc = BiopaxProcessor(model)
    # bproc.get_complexes()
    # bproc.get_phosphorylation()
    # bproc.print_statements()
    return bproc
<commit_msg>Run extraction functions by default in BioPAX API<commit_after>from __future__ import absolute_import, print_function, unicode_literals
from builtins import dict, str
from indra.java_vm import autoclass, JavaException
from indra.biopax import pathway_commons_client as pcc
from indra.biopax.processor import BiopaxProcessor

def process_pc_neighborhood(gene_names, neighbor_limit=1):
    """Returns a BiopaxProcessor for a PathwayCommons neighborhood query.

    The neighborhood query finds the neighborhood around a set of source genes.

    http://www.pathwaycommons.org/pc2/#graph

    http://www.pathwaycommons.org/pc2/#graph_kind

    Parameters
    ----------
    gene_names : list
        A list of HGNC gene symbols to search the neighborhood of.
        Examples: ['BRAF'], ['BRAF', 'MAP2K1']
    neighbor_limit : Optional[int]
        The number of steps to limit the size of the neighborhood around
        the gene names being queried. Default: 1

    Returns
    -------
    bp : BiopaxProcessor
        A BiopaxProcessor containing the obtained BioPAX model in bp.model.

    Notes
    -----
    As returned from this function, bp.statements is empty and one
    has to call methods of bp (e.g. bp.get_complexes()) to
    populate bp.statements.
    """
    model = pcc.graph_query('neighborhood', gene_names,
                            neighbor_limit=neighbor_limit)
    if model is not None:
        return process_model(model)

def process_pc_pathsbetween(gene_names, neighbor_limit=1):
    """Returns a BiopaxProcessor for a PathwayCommons paths-between query.

    The paths-between query finds the paths between a set of genes. Here
    source gene names are given in a single list and all directions of paths
    between these genes are considered.

    http://www.pathwaycommons.org/pc2/#graph

    http://www.pathwaycommons.org/pc2/#graph_kind

    Parameters
    ----------
    gene_names : list
        A list of HGNC gene symbols to search for paths between.
        Examples: ['BRAF', 'MAP2K1']
    neighbor_limit : Optional[int]
        The number of steps to limit the length of the paths between
        the gene names being queried. Default: 1

    Returns
    -------
    bp : BiopaxProcessor
        A BiopaxProcessor containing the obtained BioPAX model in bp.model.

    Notes
    -----
    As returned from this function, bp.statements is empty and one
    has to call methods of bp (e.g. bp.get_complexes()) to
    populate bp.statements.
    """
    model = pcc.graph_query('pathsbetween', gene_names,
                             neighbor_limit=neighbor_limit)
    if model is not None:
        return process_model(model)

def process_pc_pathsfromto(source_genes, target_genes, neighbor_limit=1):
    """Returns a BiopaxProcessor for a PathwayCommons paths-from-to query.

    The paths-from-to query finds the paths from a set of source genes to
    a set of target genes.

    http://www.pathwaycommons.org/pc2/#graph

    http://www.pathwaycommons.org/pc2/#graph_kind

    Parameters
    ----------
    source_genes : list
        A list of HGNC gene symbols that are the sources of paths being
        searched for.
        Examples: ['BRAF', 'RAF1', 'ARAF']
    target_genes : list
        A list of HGNC gene symbols that are the targets of paths being
        searched for.
        Examples: ['MAP2K1', 'MAP2K2']
    neighbor_limit : Optional[int]
        The number of steps to limit the length of the paths
        between the source genes and target genes being queried. Default: 1

    Returns
    -------
    bp : BiopaxProcessor
        A BiopaxProcessor containing the obtained BioPAX model in bp.model.

    Notes
    -----
    As returned from this function, bp.statements is empty and one
    has to call methods of bp (e.g. bp.get_complexes()) to
    populate bp.statements.
    """
    model = pcc.graph_query('pathsfromto', source_genes,
                             target_genes, neighbor_limit)
    if model is not None:
        return process_model(model)

def process_owl(owl_filename):
    """Returns a BiopaxProcessor for a BioPAX OWL file.

    Parameters
    ----------
    owl_filename : string
        The name of the OWL file to process.

    Returns
    -------
    bp : BiopaxProcessor
        A BiopaxProcessor containing the obtained BioPAX model in bp.model.

    Notes
    -----
    As returned from this function, bp.statements is empty and one
    has to call methods of bp (e.g. bp.get_complexes()) to
    populate bp.statements.
    """
    model = pcc.owl_to_model(owl_filename)
    return process_model(model)

def process_model(model):
    """Returns a BiopaxProcessor for a BioPAX model object.

    Parameters
    ----------
    model : org.biopax.paxtools.model.Model
        A BioPAX model object.

    Returns
    -------
    bp : BiopaxProcessor
        A BiopaxProcessor containing the obtained BioPAX model in bp.model.

    Notes
    -----
    As returned from this function, bp.statements is empty and one
    has to call methods of bp (e.g. bp.get_complexes()) to
    populate bp.statements.
    """
    bp = BiopaxProcessor(model)
    # bproc.get_complexes()
    bp.get_phosphorylation()
    bp.get_dephosphorylation()
    bp.get_acetylation()
    bp.get_palmitoylation()
    bp.get_ubiquitination()
    bp.get_activity_modification()
    bp.get_regulate_amounts()
    return bp
<|endoftext|>


<commit_before>import chembl_client
from chembl_process import Activity
from indra.databases import chebi_client
import mesh_to_chembl
import requests

def process_query(drug, target,
                  save_json_name = 'chembl_output.json'):

    drug_id_db = drug.db_refs.keys()[0]
    if drug_id_db == 'CHEBI':
        chebi_id = drug.db_refs['CHEBI']
        drug_chembl_id = chebi_client.get_chembl_id(chebi_id)
    elif drug_id_db == 'MESH':
        mesh_id = drug.db_refs['MESH']
        drug_chembl_id = mesh_to_chembl.get_chembl_id(mesh_id)

    target_uid = target.db_refs['UP']
    target_chembl_id = get_target_chemblid(target_uid)

    dict = chembl_client.query(drug_chembl_id, target_chembl_id)
    return process_json(dict)

def process_json(dict):
    assays = []
    for assay in dict['activities']:
        p = Activity(assay)
        p.pmid(assay)
        assays.append(p)
    return assays

def get_target_chemblid(target_uid):

    url = 'https://www.ebi.ac.uk/chembl/api/data/target.json?target_components__accession=%s' % target_uid
    r = requests.get(url)
    r.raise_for_status()
    js = r.json()
    target_chemblid = js['targets'][1]['target_chembl_id']
    return target_chemblid

<commit_msg>refactored drug db_refs lookup<commit_after>import chembl_client
from chembl_process import Activity
from indra.databases import chebi_client
import mesh_to_chembl
import requests

def process_query(drug, target,
                  save_json_name = 'chembl_output.json'):

    # drug_id_db = drug.db_refs.keys()[0]
    if 'CHEBI' in drug.db_refs.keys():
        chebi_id = drug.db_refs['CHEBI']
        drug_chembl_id = chebi_client.get_chembl_id(chebi_id)
    elif 'MESH' in drug.db_refs.keys():
        mesh_id = drug.db_refs['MESH']
        drug_chembl_id = mesh_to_chembl.get_chembl_id(mesh_id)

    target_uid = target.db_refs['UP']
    target_chembl_id = get_target_chemblid(target_uid)

    dict = chembl_client.query(drug_chembl_id, target_chembl_id)
    return process_json(dict)

def process_json(dict):
    assays = []
    for assay in dict['activities']:
        p = Activity(assay)
        p.pmid(assay)
        assays.append(p)
    return assays

def get_target_chemblid(target_uid):

    url = 'https://www.ebi.ac.uk/chembl/api/data/target.json?target_components__accession=%s' % target_uid
    r = requests.get(url)
    r.raise_for_status()
    js = r.json()
    target_chemblid = js['targets'][1]['target_chembl_id']
    return target_chemblid

import chembl_client
from chembl_process import Activity
from indra.databases import chebi_client
import mesh_to_chembl
import requests

def process_query(drug, target,
                  save_json_name = 'chembl_output.json'):

    drug_id_db = drug.db_refs.keys()[0]
    if drug_id_db == 'CHEBI':
        chebi_id = drug.db_refs['CHEBI']
        drug_chembl_id = chebi_client.get_chembl_id(chebi_id)
    elif drug_id_db == 'MESH':
        mesh_id = drug.db_refs['MESH']
        drug_chembl_id = mesh_to_chembl.get_chembl_id(mesh_id)

    target_uid = target.db_refs['UP']
    target_chembl_id = get_target_chemblid(target_uid)

    dict = chembl_client.query(drug_chembl_id, target_chembl_id)
    return process_json(dict)

def process_json(dict):
    assays = []
    for assay in dict['activities']:
        p = Activity(assay)
        p.pmid(assay)
        assays.append(p)
    return assays

def get_target_chemblid(target_uid):

    url = 'https://www.ebi.ac.uk/chembl/api/data/target.json?target_components__accession=%s' % target_uid
    r = requests.get(url)
    r.raise_for_status()
    js = r.json()
    target_chemblid = js['targets'][1]['target_chembl_id']
    return target_chemblid

<|endoftext|>


<commit_before>import unittest

try:
    from http.client import responses
except ImportError:
    from httplib import responses

try:
    from django.conf import settings
except ImportError:
    settings = None
    DjangoResource = object
else:
    from django.http import Http404
    from django.core.exceptions import ObjectDoesNotExist

    # Ugh. Settings for Django.
    settings.configure(DEBUG=True)

    from restless.dj import DjangoResource

from restless.exceptions import Unauthorized
from restless.preparers import FieldsPreparer
from restless.resources import skip_prepare
from restless.utils import json

from .fakes import FakeHttpRequest, FakeModel

class DjTestResource(DjangoResource):
    preparer = FieldsPreparer(fields={
        'id': 'id',
        'title': 'title',
        'author': 'username',
        'body': 'content'
    })
    fake_db = []

    def __init__(self, *args, **kwargs):
        super(DjTestResource, self).__init__(*args, **kwargs)

        self.http_methods.update({
            'schema': {
                'GET': 'schema',
            }
        })

    def fake_init(self):
        # Just for testing.
        self.__class__.fake_db = [
            FakeModel(
                id='dead-beef',
                title='First post',
                username='daniel',
                content='Hello world!'),
            FakeModel(
                id='de-faced',
                title='Another',
                username='daniel',
                content='Stuff here.'),
            FakeModel(
                id='bad-f00d',
                title='Last',
                username='daniel',
                content="G'bye!"),
        ]

    def is_authenticated(self):
        if self.request_method() == 'DELETE' and self.endpoint == 'list':
            return False

        return True

    def list(self):
        return self.fake_db

    def detail(self, pk):
        for item in self.fake_db:
            if item.id == pk:
                return item

        # If it wasn't found in our fake DB, raise a Django-esque exception.
        raise ObjectDoesNotExist("Model with pk {0} not found.".format(pk))

    def create(self):
        self.fake_db.append(FakeModel(
            **self.data
        ))

    def update(self, pk):
        for item in self.fake_db:
            if item.id == pk:
                for k, v in self.data:
                    setattr(item, k, v)
                    return

    def create_detail(self):
        raise ValueError({
            'code': 'random-crazy',
            'message': 'This is a random & crazy exception.',
        })

    def delete(self, pk):
        for i, item in enumerate(self.fake_db):
            if item.id == pk:
                del self.fake_db[i]
                return

    @skip_prepare
    def schema(self):
        # A WILD SCHEMA VIEW APPEARS!
        return {
            'fields': {
                'id': {
                    'type': 'integer',
                    'required': True,
                    'help_text': 'The unique id for the post',
                },
                'title': {
                    'type': 'string',
                    'required': True,
                    'help_text': "The post's title",
                },
                'author': {
                    'type': 'string',
                    'required': True,
                    'help_text': 'The username of the author of the post',
                },
                'body': {
                    'type': 'string',
                    'required': False,
                    'default': '',
                    'help_text': 'The content of the post',
                }
            },
            'format': 'application/json',
            'allowed_list_http_methods': ['GET', 'POST'],
            'allowed_detail_http_methods': ['GET', 'PUT', 'DELETE'],
        }

class DjTestResourceHttp404Handling(DjTestResource):
    def detail(self, pk):
        for item in self.fake_db:
            if item.id == pk:
                return item

        # If it wasn't found in our fake DB, raise a Django-esque exception.
        raise Http404("Model with pk {0} not found.".format(pk))

@unittest.skipIf(not settings, "Django is not available")
class DjangoResourceTestCase(unittest.TestCase):
    def setUp(self):
        super(DjangoResourceTestCase, self).setUp()
        self.res = DjTestResource()
        # Just for the fake data.
        self.res.fake_init()

    def test_as_list(self):
        list_endpoint = DjTestResource.as_list()
        req = FakeHttpRequest('GET')

        resp = list_endpoint(req)
        self.assertEqual(resp['Content-Type'], 'application/json')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(json.loads(resp.content.decode('utf-8')), {
            'objects': [
                {
                    'author': 'daniel',
                    'body': 'Hello world!',
                    'id': 'dead-beef',
                    'title': 'First post'
                },
                {
                    'author': 'daniel',
                    'body': 'Stuff here.',
                    'id': 'de-faced',
                    'title': 'Another'
                },
                {
                    'author': 'daniel',
                    'body': "G'bye!",
                    'id': 'bad-f00d',
                    'title': 'Last'
                }
            ]
        })

    def test_as_detail(self):
        detail_endpoint = DjTestResource.as_detail()
        req = FakeHttpRequest('GET')

        resp = detail_endpoint(req, 'de-faced')
        self.assertEqual(resp['Content-Type'], 'application/json')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(json.loads(resp.content.decode('utf-8')), {
            'author': 'daniel',
            'body': 'Stuff here.',
            'id': 'de-faced',
            'title': 'Another'
        })

    def test_as_view(self):
        # This would be hooked up via the URLconf...
        schema_endpoint = DjTestResource.as_view('schema')
        req = FakeHttpRequest('GET')

        resp = schema_endpoint(req)
        self.assertEqual(resp['Content-Type'], 'application/json')
        self.assertEqual(resp.status_code, 200)
        schema = json.loads(resp.content.decode('utf-8'))
        self.assertEqual(
            sorted(list(schema['fields'].keys())),
            [
                'author',
                'body',
                'id',
                'title',
            ]
        )
        self.assertEqual(schema['fields']['id']['type'], 'integer')
        self.assertEqual(schema['format'], 'application/json')

    def test_handle_not_implemented(self):
        self.res.request = FakeHttpRequest('TRACE')

        resp = self.res.handle('list')
        self.assertEqual(resp['Content-Type'], 'application/json')
        self.assertEqual(resp.status_code, 501)
        self.assertEqual(resp.reason_phrase, responses[501])

        resp_json = json.loads(resp.content.decode('utf-8'))
        self.assertEqual(
            resp_json['error'], "Unsupported method 'TRACE' for list endpoint.")
        self.assertTrue('traceback' in resp_json)

    def test_handle_not_authenticated(self):
        # Special-cased above for testing.
        self.res.request = FakeHttpRequest('DELETE')

        # First with DEBUG on
        resp = self.res.handle('list')
        self.assertEqual(resp['Content-Type'], 'application/json')
        self.assertEqual(resp.status_code, 401)
        self.assertEqual(resp.reason_phrase, responses[401])

        resp_json = json.loads(resp.content.decode('utf-8'))
        self.assertEqual(resp_json['error'], 'Unauthorized.')
        self.assertTrue('traceback' in resp_json)

        # Now with DEBUG off.
        settings.DEBUG = False
        self.addCleanup(setattr, settings, 'DEBUG', True)
        resp = self.res.handle('list')
        self.assertEqual(resp['Content-Type'], 'application/json')
        self.assertEqual(resp.status_code, 401)
        resp_json = json.loads(resp.content.decode('utf-8'))
        self.assertEqual(resp_json, {
            'error': 'Unauthorized.',
        })
        self.assertFalse('traceback' in resp_json)

        # Last, with bubble_exceptions.
        class Bubbly(DjTestResource):
            def bubble_exceptions(self):
                return True

        with self.assertRaises(Unauthorized):
            bubb = Bubbly()
            bubb.request = FakeHttpRequest('DELETE')
            bubb.handle('list')

    def test_handle_build_err(self):
        # Special-cased above for testing.
        self.res.request = FakeHttpRequest('POST')
        settings.DEBUG = False
        self.addCleanup(setattr, settings, 'DEBUG', True)

        resp = self.res.handle('detail')
        self.assertEqual(resp['Content-Type'], 'application/json')
        self.assertEqual(resp.status_code, 500)
        self.assertEqual(resp.reason_phrase, responses[500])
        self.assertEqual(json.loads(resp.content.decode('utf-8')), {
            'error': {
                'code': 'random-crazy',
                'message': 'This is a random & crazy exception.',
            }
        })

    def test_object_does_not_exist(self):
        # Make sure we get a proper Not Found exception rather than a
        # generic 500, when code raises a ObjectDoesNotExist exception.
        self.res.request = FakeHttpRequest('GET')
        settings.DEBUG = False
        self.addCleanup(setattr, settings, 'DEBUG', True)

        resp = self.res.handle('detail', 1001)
        self.assertEqual(resp['Content-Type'], 'application/json')
        self.assertEqual(resp.status_code, 404)
        self.assertEqual(resp.reason_phrase, responses[404])
        self.assertEqual(json.loads(resp.content.decode('utf-8')), {
            'error': 'Model with pk 1001 not found.'
        })

    def test_http404_exception_handling(self):
        # Make sure we get a proper Not Found exception rather than a
        # generic 500, when code raises a Http404 exception.
        res = DjTestResourceHttp404Handling()
        res.request = FakeHttpRequest('GET')
        settings.DEBUG = False
        self.addCleanup(setattr, settings, 'DEBUG', True)

        resp = res.handle('detail', 1001)
        self.assertEqual(resp['Content-Type'], 'application/json')
        self.assertEqual(resp.status_code, 404)
        self.assertEqual(resp.reason_phrase, responses[404])
        self.assertEqual(json.loads(resp.content.decode('utf-8')), {
            'error': 'Model with pk 1001 not found.'
        })

    def test_build_url_name(self):
        self.assertEqual(
            DjTestResource.build_url_name('list'),
            'api_djtest_list'
        )
        self.assertEqual(
            DjTestResource.build_url_name('detail'),
            'api_djtest_detail'
        )
        self.assertEqual(
            DjTestResource.build_url_name('schema'),
            'api_djtest_schema'
        )

        self.assertEqual(
            DjTestResource.build_url_name('list', name_prefix='v2_'),
            'v2_list'
        )
        self.assertEqual(
            DjTestResource.build_url_name('detail', name_prefix='v2_'),
            'v2_detail'
        )
        self.assertEqual(
            DjTestResource.build_url_name('schema', name_prefix='v2_'),
            'v2_schema'
        )

    def test_urls(self):
        patterns = DjTestResource.urls()
        self.assertEqual(len(patterns), 2)
        self.assertEqual(patterns[0].name, 'api_djtest_list')
        self.assertEqual(patterns[1].name, 'api_djtest_detail')

        patterns = DjTestResource.urls(name_prefix='v2_tests')
        self.assertEqual(len(patterns), 2)
        self.assertEqual(patterns[0].name, 'v2_tests_list')
        self.assertEqual(patterns[1].name, 'v2_tests_detail')

    def test_create(self):
        self.res.request = FakeHttpRequest(
            'POST',
            body='{"id": 6, "title": "Moved hosts", "author": "daniel"}')
        self.assertEqual(len(self.res.fake_db), 3)

        resp = self.res.handle('list')
        self.assertEqual(resp['Content-Type'], 'application/json')
        self.assertEqual(resp.status_code, 201)
        self.assertEqual(resp.content.decode('utf-8'), '')

        # Check the internal state.
        self.assertEqual(len(self.res.fake_db), 4)
        self.assertEqual(self.res.data, {
            'author': 'daniel',
            'id': 6,
            'title': 'Moved hosts'
        })

    def test_delete(self):
        self.res.request = FakeHttpRequest('DELETE')
        self.assertEqual(len(self.res.fake_db), 3)

        resp = self.res.handle('detail', pk='de-faced')
        self.assertEqual(resp['Content-Type'], 'text/plain')
        self.assertEqual(resp.status_code, 204)
        self.assertEqual(resp.content.decode('utf-8'), '')

        # Check the internal state.
        self.res.request = FakeHttpRequest('GET')
        self.assertEqual(len(self.res.fake_db), 2)
        resp = self.res.handle('detail', pk='de-faced')
        self.assertEqual(resp.status_code, 404)
<commit_msg>Fixed tests for Django 1.8<commit_after>import unittest

try:
    from http.client import responses
except ImportError:
    from httplib import responses

try:
    from django.conf import settings
except ImportError:
    settings = None
    DjangoResource = object
else:
    from django.http import Http404
    from django.core.exceptions import ObjectDoesNotExist

    # Ugh. Settings for Django.
    settings.configure(DEBUG=True)

    from restless.dj import DjangoResource

from restless.exceptions import Unauthorized
from restless.preparers import FieldsPreparer
from restless.resources import skip_prepare
from restless.utils import json

from .fakes import FakeHttpRequest, FakeModel

class DjTestResource(DjangoResource):
    preparer = FieldsPreparer(fields={
        'id': 'id',
        'title': 'title',
        'author': 'username',
        'body': 'content'
    })
    fake_db = []

    def __init__(self, *args, **kwargs):
        super(DjTestResource, self).__init__(*args, **kwargs)

        self.http_methods.update({
            'schema': {
                'GET': 'schema',
            }
        })

    def fake_init(self):
        # Just for testing.
        self.__class__.fake_db = [
            FakeModel(
                id='dead-beef',
                title='First post',
                username='daniel',
                content='Hello world!'),
            FakeModel(
                id='de-faced',
                title='Another',
                username='daniel',
                content='Stuff here.'),
            FakeModel(
                id='bad-f00d',
                title='Last',
                username='daniel',
                content="G'bye!"),
        ]

    def is_authenticated(self):
        if self.request_method() == 'DELETE' and self.endpoint == 'list':
            return False

        return True

    def list(self):
        return self.fake_db

    def detail(self, pk):
        for item in self.fake_db:
            if item.id == pk:
                return item

        # If it wasn't found in our fake DB, raise a Django-esque exception.
        raise ObjectDoesNotExist("Model with pk {0} not found.".format(pk))

    def create(self):
        self.fake_db.append(FakeModel(
            **self.data
        ))

    def update(self, pk):
        for item in self.fake_db:
            if item.id == pk:
                for k, v in self.data:
                    setattr(item, k, v)
                    return

    def create_detail(self):
        raise ValueError({
            'code': 'random-crazy',
            'message': 'This is a random & crazy exception.',
        })

    def delete(self, pk):
        for i, item in enumerate(self.fake_db):
            if item.id == pk:
                del self.fake_db[i]
                return

    @skip_prepare
    def schema(self):
        # A WILD SCHEMA VIEW APPEARS!
        return {
            'fields': {
                'id': {
                    'type': 'integer',
                    'required': True,
                    'help_text': 'The unique id for the post',
                },
                'title': {
                    'type': 'string',
                    'required': True,
                    'help_text': "The post's title",
                },
                'author': {
                    'type': 'string',
                    'required': True,
                    'help_text': 'The username of the author of the post',
                },
                'body': {
                    'type': 'string',
                    'required': False,
                    'default': '',
                    'help_text': 'The content of the post',
                }
            },
            'format': 'application/json',
            'allowed_list_http_methods': ['GET', 'POST'],
            'allowed_detail_http_methods': ['GET', 'PUT', 'DELETE'],
        }

class DjTestResourceHttp404Handling(DjTestResource):
    def detail(self, pk):
        for item in self.fake_db:
            if item.id == pk:
                return item

        # If it wasn't found in our fake DB, raise a Django-esque exception.
        raise Http404("Model with pk {0} not found.".format(pk))

@unittest.skipIf(not settings, "Django is not available")
class DjangoResourceTestCase(unittest.TestCase):
    def setUp(self):
        super(DjangoResourceTestCase, self).setUp()
        self.res = DjTestResource()
        # Just for the fake data.
        self.res.fake_init()

    def test_as_list(self):
        list_endpoint = DjTestResource.as_list()
        req = FakeHttpRequest('GET')

        resp = list_endpoint(req)
        self.assertEqual(resp['Content-Type'], 'application/json')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(json.loads(resp.content.decode('utf-8')), {
            'objects': [
                {
                    'author': 'daniel',
                    'body': 'Hello world!',
                    'id': 'dead-beef',
                    'title': 'First post'
                },
                {
                    'author': 'daniel',
                    'body': 'Stuff here.',
                    'id': 'de-faced',
                    'title': 'Another'
                },
                {
                    'author': 'daniel',
                    'body': "G'bye!",
                    'id': 'bad-f00d',
                    'title': 'Last'
                }
            ]
        })

    def test_as_detail(self):
        detail_endpoint = DjTestResource.as_detail()
        req = FakeHttpRequest('GET')

        resp = detail_endpoint(req, 'de-faced')
        self.assertEqual(resp['Content-Type'], 'application/json')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(json.loads(resp.content.decode('utf-8')), {
            'author': 'daniel',
            'body': 'Stuff here.',
            'id': 'de-faced',
            'title': 'Another'
        })

    def test_as_view(self):
        # This would be hooked up via the URLconf...
        schema_endpoint = DjTestResource.as_view('schema')
        req = FakeHttpRequest('GET')

        resp = schema_endpoint(req)
        self.assertEqual(resp['Content-Type'], 'application/json')
        self.assertEqual(resp.status_code, 200)
        schema = json.loads(resp.content.decode('utf-8'))
        self.assertEqual(
            sorted(list(schema['fields'].keys())),
            [
                'author',
                'body',
                'id',
                'title',
            ]
        )
        self.assertEqual(schema['fields']['id']['type'], 'integer')
        self.assertEqual(schema['format'], 'application/json')

    def test_handle_not_implemented(self):
        self.res.request = FakeHttpRequest('TRACE')

        resp = self.res.handle('list')
        self.assertEqual(resp['Content-Type'], 'application/json')
        self.assertEqual(resp.status_code, 501)
        self.assertEqual(resp.reason_phrase.title(), responses[501])

        resp_json = json.loads(resp.content.decode('utf-8'))
        self.assertEqual(
            resp_json['error'], "Unsupported method 'TRACE' for list endpoint.")
        self.assertTrue('traceback' in resp_json)

    def test_handle_not_authenticated(self):
        # Special-cased above for testing.
        self.res.request = FakeHttpRequest('DELETE')

        # First with DEBUG on
        resp = self.res.handle('list')
        self.assertEqual(resp['Content-Type'], 'application/json')
        self.assertEqual(resp.status_code, 401)
        self.assertEqual(resp.reason_phrase.title(), responses[401])

        resp_json = json.loads(resp.content.decode('utf-8'))
        self.assertEqual(resp_json['error'], 'Unauthorized.')
        self.assertTrue('traceback' in resp_json)

        # Now with DEBUG off.
        settings.DEBUG = False
        self.addCleanup(setattr, settings, 'DEBUG', True)
        resp = self.res.handle('list')
        self.assertEqual(resp['Content-Type'], 'application/json')
        self.assertEqual(resp.status_code, 401)
        resp_json = json.loads(resp.content.decode('utf-8'))
        self.assertEqual(resp_json, {
            'error': 'Unauthorized.',
        })
        self.assertFalse('traceback' in resp_json)

        # Last, with bubble_exceptions.
        class Bubbly(DjTestResource):
            def bubble_exceptions(self):
                return True

        with self.assertRaises(Unauthorized):
            bubb = Bubbly()
            bubb.request = FakeHttpRequest('DELETE')
            bubb.handle('list')

    def test_handle_build_err(self):
        # Special-cased above for testing.
        self.res.request = FakeHttpRequest('POST')
        settings.DEBUG = False
        self.addCleanup(setattr, settings, 'DEBUG', True)

        resp = self.res.handle('detail')
        self.assertEqual(resp['Content-Type'], 'application/json')
        self.assertEqual(resp.status_code, 500)
        self.assertEqual(resp.reason_phrase.title(), responses[500])
        self.assertEqual(json.loads(resp.content.decode('utf-8')), {
            'error': {
                'code': 'random-crazy',
                'message': 'This is a random & crazy exception.',
            }
        })

    def test_object_does_not_exist(self):
        # Make sure we get a proper Not Found exception rather than a
        # generic 500, when code raises a ObjectDoesNotExist exception.
        self.res.request = FakeHttpRequest('GET')
        settings.DEBUG = False
        self.addCleanup(setattr, settings, 'DEBUG', True)

        resp = self.res.handle('detail', 1001)
        self.assertEqual(resp['Content-Type'], 'application/json')
        self.assertEqual(resp.status_code, 404)
        self.assertEqual(resp.reason_phrase.title(), responses[404])
        self.assertEqual(json.loads(resp.content.decode('utf-8')), {
            'error': 'Model with pk 1001 not found.'
        })

    def test_http404_exception_handling(self):
        # Make sure we get a proper Not Found exception rather than a
        # generic 500, when code raises a Http404 exception.
        res = DjTestResourceHttp404Handling()
        res.request = FakeHttpRequest('GET')
        settings.DEBUG = False
        self.addCleanup(setattr, settings, 'DEBUG', True)

        resp = res.handle('detail', 1001)
        self.assertEqual(resp['Content-Type'], 'application/json')
        self.assertEqual(resp.status_code, 404)
        self.assertEqual(resp.reason_phrase.title(), responses[404])
        self.assertEqual(json.loads(resp.content.decode('utf-8')), {
            'error': 'Model with pk 1001 not found.'
        })

    def test_build_url_name(self):
        self.assertEqual(
            DjTestResource.build_url_name('list'),
            'api_djtest_list'
        )
        self.assertEqual(
            DjTestResource.build_url_name('detail'),
            'api_djtest_detail'
        )
        self.assertEqual(
            DjTestResource.build_url_name('schema'),
            'api_djtest_schema'
        )

        self.assertEqual(
            DjTestResource.build_url_name('list', name_prefix='v2_'),
            'v2_list'
        )
        self.assertEqual(
            DjTestResource.build_url_name('detail', name_prefix='v2_'),
            'v2_detail'
        )
        self.assertEqual(
            DjTestResource.build_url_name('schema', name_prefix='v2_'),
            'v2_schema'
        )

    def test_urls(self):
        patterns = DjTestResource.urls()
        self.assertEqual(len(patterns), 2)
        self.assertEqual(patterns[0].name, 'api_djtest_list')
        self.assertEqual(patterns[1].name, 'api_djtest_detail')

        patterns = DjTestResource.urls(name_prefix='v2_tests')
        self.assertEqual(len(patterns), 2)
        self.assertEqual(patterns[0].name, 'v2_tests_list')
        self.assertEqual(patterns[1].name, 'v2_tests_detail')

    def test_create(self):
        self.res.request = FakeHttpRequest(
            'POST',
            body='{"id": 6, "title": "Moved hosts", "author": "daniel"}')
        self.assertEqual(len(self.res.fake_db), 3)

        resp = self.res.handle('list')
        self.assertEqual(resp['Content-Type'], 'application/json')
        self.assertEqual(resp.status_code, 201)
        self.assertEqual(resp.content.decode('utf-8'), '')

        # Check the internal state.
        self.assertEqual(len(self.res.fake_db), 4)
        self.assertEqual(self.res.data, {
            'author': 'daniel',
            'id': 6,
            'title': 'Moved hosts'
        })

    def test_delete(self):
        self.res.request = FakeHttpRequest('DELETE')
        self.assertEqual(len(self.res.fake_db), 3)

        resp = self.res.handle('detail', pk='de-faced')
        self.assertEqual(resp['Content-Type'], 'text/plain')
        self.assertEqual(resp.status_code, 204)
        self.assertEqual(resp.content.decode('utf-8'), '')

        # Check the internal state.
        self.res.request = FakeHttpRequest('GET')
        self.assertEqual(len(self.res.fake_db), 2)
        resp = self.res.handle('detail', pk='de-faced')
        self.assertEqual(resp.status_code, 404)
<|endoftext|>


<commit_before>"""
    Work with
        whitelist, blacklist,
        seleb account,
"""

import os
import io

def check_if_file_exists(file_path):
    if not os.path.exists(file_path):
        print ("Can't find '%s' file." % file_path)
        return False
    return True

def read_list_from_file(file_path):
    """
        Reads list from file. One line - one item.
        Returns the list if file items.
    """
    try:
        if not check_if_file_exists(file_path):
            return False
        with io.open(file_path, "r", encoding="utf8") as f:
            content = f.readlines()
            content = [item.strip() for item in content if len(item.strip()) > 0]
            return content
    except:
        return False

def add_whitelist(self, file_path):
    file_contents = read_list_from_file(file_path)
    self.whitelist = [convert_to_user_id(item) for item in file_contents]
    return not not self.whitelist

def add_blacklist(self, file_path):
    file_contents = read_list_from_file(file_path)
    self.blacklist = [convert_to_user_id(item) for item in file_contents]
    return not not self.blacklist

def get_media_owner(self, media_id):
    self.mediaInfo(media_id)
    try:
        return self.LastJson["items"][0]["user"]["pk"]
    except:
        return False

def check_media(self, media_id):
    return check_user(self, get_media_owner(self, media_id))

def check_user(self, user_id):
    """
        Decide should you interract with that user_id or not.
        Decision based on
            1) Following/followers ratio
            2) Approved account
            3) Black-whitelist
            4) Closed accs
    """
    if not user_id:
        return True
    if self.whitelist:
        if user_id in self.whitelist:
            return True
    if self.blacklist:
        if user_id in self.blacklist:
            return False

    user_info = self.get_user_info(user_id)
    if not user_info:
        return True # closed acc
    if "is_business" in user_info:
        if user_info["is_business"]:
            return False
    if "is_verified" in user_info:
        if user_info["is_verified"]:
            return False
    if "follower_count" in user_info and "following_count" in user_info:
        if user_info["follower_count"] < 100:
            return True # not famous user
        if user_info["following_count"] < 10:
            return False
        if user_info["follower_count"] / user_info["following_count"] > 10:
            return False # too many
        if user_info["following_count"] / user_info["follower_count"] > 2:
            return True # too many
    if 'media_count' in user_info:
        if user_info["media_count"] < 3:
            return False # bot or inactive user
    return True

def convert_to_user_id(self, something):
    if type(something) == str and not something.isdigit():
        if something[0] == "@": # cut first @
            something = something[1:]
        something = self.get_userid_from_username(something)
    # if type is not str than it is int so user_id passed
    return something
<commit_msg>add unicode support<commit_after>"""
    Work with
        whitelist, blacklist,
        seleb account,
"""

import os
import io

def check_if_file_exists(file_path):
    if not os.path.exists(file_path):
        print ("Can't find '%s' file." % file_path)
        return False
    return True

def read_list_from_file(file_path):
    """
        Reads list from file. One line - one item.
        Returns the list if file items.
    """
    try:
        if not check_if_file_exists(file_path):
            return False
        with io.open(file_path, "r", encoding="utf8") as f:
            content = f.readlines()
            content = [item.strip() for item in content if len(item.strip()) > 0]
            return content
    except:
        return False

def add_whitelist(self, file_path):
    file_contents = read_list_from_file(file_path)
    self.whitelist = [convert_to_user_id(item) for item in file_contents]
    return not not self.whitelist

def add_blacklist(self, file_path):
    file_contents = read_list_from_file(file_path)
    self.blacklist = [convert_to_user_id(item) for item in file_contents]
    return not not self.blacklist

def get_media_owner(self, media_id):
    self.mediaInfo(media_id)
    try:
        return self.LastJson["items"][0]["user"]["pk"]
    except:
        return False

def check_media(self, media_id):
    return check_user(self, get_media_owner(self, media_id))

def check_user(self, user_id):
    """
        Decide should you interract with that user_id or not.
        Decision based on
            1) Following/followers ratio
            2) Approved account
            3) Black-whitelist
            4) Closed accs
    """
    if not user_id:
        return True
    if self.whitelist:
        if user_id in self.whitelist:
            return True
    if self.blacklist:
        if user_id in self.blacklist:
            return False

    user_info = self.get_user_info(user_id)
    if not user_info:
        return True # closed acc
    if "is_business" in user_info:
        if user_info["is_business"]:
            return False
    if "is_verified" in user_info:
        if user_info["is_verified"]:
            return False
    if "follower_count" in user_info and "following_count" in user_info:
        if user_info["follower_count"] < 100:
            return True # not famous user
        if user_info["following_count"] < 10:
            return False
        if user_info["follower_count"] / user_info["following_count"] > 10:
            return False # too many
        if user_info["following_count"] / user_info["follower_count"] > 2:
            return True # too many
    if 'media_count' in user_info:
        if user_info["media_count"] < 3:
            return False # bot or inactive user
    return True

def convert_to_user_id(self, smth):
    if (type(smth) == str or type(smth) == unicode) and not smth.isdigit():
        if smth[0] == "@": # cut first @
            smth = smth[1:]
        smth = self.get_userid_from_username(smth)
    # if type is not str than it is int so user_id passed
    return smth
<|endoftext|>


<commit_before>#!/usr/bin/python

# Install to Project Repo
# A script for installing jars to an in-project Maven repository.
# v0.1.1
#
# MIT License
# (c) 2012, Nikita Volkov. All rights reserved.
# simplified version adapted by Tom Carchrae
# https://github.com/carchrae/install-to-project-repo

import os
import re
import shutil

def jars(dir):
  return [dir + "/" + f for f in os.listdir(dir) if f.lower().endswith(".jar")]

def parse_by_eclipse_standard(path):
  file = os.path.splitext(os.path.basename(path))[0]

  return {
  "group": "local",
	"name": file,
	"version": "1.0",
	"snapshot": "",
	"source": ""
  }

def maven_dependencies(parsing_results):
  def artifact(parsing):
    return {
      "groupId": parsing["group"],
      "artifactId": parsing["name"],
      "version": parsing["version"] + ("-SNAPSHOT" if parsing["snapshot"] else "")
    }
  def maven_dependency(artifact):
    return """
<dependency>
  <groupId>%(groupId)s</groupId>
  <artifactId>%(artifactId)s</artifactId>
  <version>%(version)s</version>
</dependency>
""" % artifact
  def unique_artifacts():
    artifacts = []
    for (_, parsing) in parsing_results:
      a = artifact(parsing)
      if a not in artifacts:
        artifacts.append(a)
    return artifacts

  return "\n".join([maven_dependency(a).strip() for a in unique_artifacts()])

def install(path, parsing):
  os.system(
    "mvn install:install-file" + \
    " -Dfile=" + path + \
    " -DgroupId=" + parsing["group"] + \
    " -DartifactId=" + parsing["name"] + \
    " -Dversion=" + parsing["version"] + ("-SNAPSHOT" if parsing["snapshot"] else "") + \
    " -Dpackaging=jar" + \
    " -DlocalRepositoryPath=repo" + \
    " -DcreateChecksum=true" + \
    (" -Dclassifier=sources" if parsing["source"] else "")
  )

from optparse import OptionParser

parser = OptionParser()
parser.add_option("-d", "--delete",
                  dest="delete", action="store_true", default=False,
                  help="Delete successfully installed libs in source location")
(options, args) = parser.parse_args()

parsings = (
  [(path, parse_by_eclipse_standard(path)) for path in jars("lib")]
)

unparsable_files = [r[0] for r in parsings if r[1] == None]
if unparsable_files:
  print "The following files could not be parsed:"
  for f in unparsable_files:
    print "| - " + f

parsings = [p for p in parsings if p[1] != None]

for (path, parsing) in parsings:
  install(path, parsing)
  if options.delete:
    os.remove(path)

print maven_dependencies(parsings)
print "These dependenceis have also been saved in the file : repo/dependencies.txt"
f = open('repo/dependencies.txt', 'w')
f.write(maven_dependencies(parsings))
<commit_msg>Add ~future~ to make script compatible with Python 3<commit_after>#!/usr/bin/python

# Install to Project Repo
# A script for installing jars to an in-project Maven repository.
# v0.1.1
#
# MIT License
# (c) 2012, Nikita Volkov. All rights reserved.
# simplified version adapted by Tom Carchrae
# https://github.com/carchrae/install-to-project-repo
from __future__ import (absolute_import, division,
                        print_function, unicode_literals)
from builtins import (
         bytes, dict, int, list, object, range, str,
         ascii, chr, hex, input, next, oct, open,
         pow, round, super,
         filter, map, zip)

import os
import re
import shutil

def jars(dir):
  return [dir + "/" + f for f in os.listdir(dir) if f.lower().endswith(".jar")]

def parse_by_eclipse_standard(path):
  file = os.path.splitext(os.path.basename(path))[0]

  return {
  "group": "local",
	"name": file,
	"version": "1.0",
	"snapshot": "",
	"source": ""
  }

def maven_dependencies(parsing_results):
  def artifact(parsing):
    return {
      "groupId": parsing["group"],
      "artifactId": parsing["name"],
      "version": parsing["version"] + ("-SNAPSHOT" if parsing["snapshot"] else "")
    }
  def maven_dependency(artifact):
    return """
<dependency>
  <groupId>%(groupId)s</groupId>
  <artifactId>%(artifactId)s</artifactId>
  <version>%(version)s</version>
</dependency>
""" % artifact
  def unique_artifacts():
    artifacts = []
    for (_, parsing) in parsing_results:
      a = artifact(parsing)
      if a not in artifacts:
        artifacts.append(a)
    return artifacts

  return "\n".join([maven_dependency(a).strip() for a in unique_artifacts()])

def install(path, parsing):
  os.system(
    "mvn install:install-file" + \
    " -Dfile=" + path + \
    " -DgroupId=" + parsing["group"] + \
    " -DartifactId=" + parsing["name"] + \
    " -Dversion=" + parsing["version"] + ("-SNAPSHOT" if parsing["snapshot"] else "") + \
    " -Dpackaging=jar" + \
    " -DlocalRepositoryPath=repo" + \
    " -DcreateChecksum=true" + \
    (" -Dclassifier=sources" if parsing["source"] else "")
  )

from optparse import OptionParser

parser = OptionParser()
parser.add_option("-d", "--delete",
                  dest="delete", action="store_true", default=False,
                  help="Delete successfully installed libs in source location")
(options, args) = parser.parse_args()

parsings = (
  [(path, parse_by_eclipse_standard(path)) for path in jars("lib")]
)

unparsable_files = [r[0] for r in parsings if r[1] == None]
if unparsable_files:
  print("The following files could not be parsed:")
  for f in unparsable_files:
    print("| - {}".format(f))

parsings = [p for p in parsings if p[1] != None]

for (path, parsing) in parsings:
  install(path, parsing)
  if options.delete:
    os.remove(path)

print(maven_dependencies(parsings))
print("These dependenceis have also been saved in the file : repo/dependencies.txt")
f = open('repo/dependencies.txt', 'w')
f.write(maven_dependencies(parsings))
<|endoftext|>


<commit_before># Copyright (c) Charl P. Botha, TU Delft.
# All rights reserved.
# See COPYRIGHT for details.

import config
from install_package import InstallPackage
import os
import shutil
import sys
import utils

# cvs -d :pserver:anonymous@public.kitware.com:/cvsroot/VTK login
# password vtk
#  cvs -d :pserver:anonymous@public.kitware.com:/cvsroot/VTK checkout
#  VTK
# cd VTK
# cvs -z3 update -R blahblah

BASENAME = "VTK"
# password "vtk" integrated in CVS -d spec
CVS_REPO = ":pserver:anonymous:vtk@public.kitware.com:/cvsroot/" + BASENAME
CVS_VERSION = "-r VTK-5-4-1"

VTK_BASE_VERSION = "vtk-5.4"

# this patch does three things:
# 1. adds try/catch blocks to all python method calls in order
#    to trap bad_alloc exceptions
# 2. implements my scheme for turning all VTK errors into Python exceptions
#    by making use of a special output window class
# 3. gives up the GIL around all VTK calls.  This is also necessary
#    for 2 not to deadlock on multi-cores.
EXC_PATCH = "pyvtk_tryexcept_and_pyexceptions.diff"

# fixes attributes in vtkproperty for shader use in python
VTKPRPRTY_PATCH = "vtkProperty_PyShaderVar.diff"

dependencies = ['cmake']

class VTK(InstallPackage):

    def __init__(self):
        self.source_dir = os.path.join(config.archive_dir, BASENAME)
        self.build_dir = os.path.join(config.build_dir, '%s-build' %
                                      (BASENAME,))
        self.inst_dir = os.path.join(config.inst_dir, BASENAME)
        self.exc_patch_filename = os.path.join(config.patches_dir,
                                               EXC_PATCH)
        self.vtkprprty_patch_filename = os.path.join(config.patches_dir,
                                                 VTKPRPRTY_PATCH)

    def update_wpvi(self):
        """Update Wrapping/Python/vtk/__init__.py to fix nasty DL bug
        on AMD64.  Thanks to Mathieu Malaterre for finding this!

        Not necessary on VTK-5-2 20080802.

        Keeping this as example on how to update select files in
        checkout.
        """

        utils.output("Updating Wrapping/Python/vtk/__init__.py.")

        utils.goto_archive()
        os.chdir(os.path.join(BASENAME, 'Wrapping', 'Python', 'vtk'))

        ret1 = os.system(
        "%s -z3 update -r 1.14 __init__.py" %
        (config.CVS,))

        if ret1 != 0:
            utils.error("Error updating W/P/v/__init__.py.")

    def get(self):
        if os.path.exists(self.source_dir):
            utils.output("VTK already checked out, skipping step.")

        else:
            utils.goto_archive()
            ret = os.system("%s -d %s co %s %s" %
                            (config.CVS, CVS_REPO, CVS_VERSION, BASENAME))
            if ret != 0:
                utils.error("Could not CVS checkout.  Fix and try again.")

            #self.update_wpvi()

            # EXC PATCH
            utils.output("Applying EXC patch")
            os.chdir(self.source_dir)
            ret = os.system(
                "%s -p0 < %s" % (config.PATCH, self.exc_patch_filename))
            if ret != 0:
                utils.error(
                    "Could not apply EXC patch.  Fix and try again.")

            # VTKPRPRTY PATCH
            utils.output("Applying VTKPRPRTY patch")
            os.chdir(os.path.join(self.source_dir, 'Rendering'))
            ret = os.system(
                "%s -p0 < %s" % (config.PATCH, self.vtkprprty_patch_filename))
            if ret != 0:
                utils.error(
                    "Could not apply VTKPRPRTY patch.  Fix and try again.")

    def unpack(self):
        pass

    def configure(self):
        if os.path.exists(
            os.path.join(self.build_dir, 'CMakeFiles/cmake.check_cache')):
            utils.output("VTK build already configured.")
            return

        if not os.path.exists(self.build_dir):
            os.mkdir(self.build_dir)

        cmake_params = "-DBUILD_SHARED_LIBS=ON " \
                       "-DBUILD_TESTING=OFF " \
                       "-DCMAKE_BUILD_TYPE=RelWithDebInfo " \
                       "-DCMAKE_INSTALL_PREFIX=%s " \
		               "-DVTK_USE_TK=NO " \
                       "-DVTK_USE_METAIO=ON " \
                       "-DVTK_USE_PARALLEL=ON " \
                       "-DPYTHON_EXECUTABLE=%s " \
                       "-DPYTHON_LIBRARY=%s " \
                       "-DPYTHON_INCLUDE_PATH=%s " \
                       "-DVTK_WRAP_PYTHON=ON " % (self.inst_dir,
                                                 config.PYTHON_EXECUTABLE,
                                                 config.PYTHON_LIBRARY,
                                                 config.PYTHON_INCLUDE_PATH)

        ret = utils.cmake_command(self.build_dir, self.source_dir,
                cmake_params)

        if ret != 0:
            utils.error("Could not configure VTK.  Fix and try again.")

    def build(self):
        posix_file = os.path.join(self.build_dir,
            'bin/libvtkWidgetsPython.so')
        nt_file = os.path.join(self.build_dir, 'bin', config.BUILD_TARGET,
                'vtkWidgetsPythonD.dll')

        if utils.file_exists(posix_file, nt_file):
            utils.output("VTK already built.  Skipping build step.")

        else:
            os.chdir(self.build_dir)
            ret = utils.make_command('VTK.sln')
            if ret != 0:
                utils.error("Error building VTK.  Fix and try again.")

    def install(self):
        config.VTK_LIB = os.path.join(self.inst_dir, 'lib')

        # whatever the case may be, we have to register VTK variables
        if os.name == 'nt':
            # on Win, inst/VTK/bin contains the so files
            config.VTK_SODIR = os.path.join(self.inst_dir, 'bin')
            # inst/VTK/lib/site-packages the VTK python package
            config.VTK_PYTHON = os.path.join(
                    config.VTK_LIB, 'site-packages')

        else:
            # on *ix, inst/VTK/lib contains DLLs
            config.VTK_SODIR = os.path.join(
                    config.VTK_LIB, VTK_BASE_VERSION)
            # on *ix, inst/lib/python2.5/site-packages contains the
            # VTK python package
            # sys.version is (2, 5, 0, 'final', 0)
            config.VTK_PYTHON = os.path.join(
                config.VTK_LIB, 'python%d.%d/site-packages' % \
                sys.version_info[0:2])

        # this contains the VTK cmake config (same on *ix and Win)
        config.VTK_DIR = os.path.join(config.VTK_LIB, VTK_BASE_VERSION)

        posix_file = os.path.join(self.inst_dir, 'bin/vtkpython')
        nt_file = os.path.join(self.inst_dir, 'bin', 'vtkpython.exe')

        if utils.file_exists(posix_file, nt_file):
            utils.output("VTK already installed.  Skipping build step.")

        else:
            # python 2.5.2 setup.py complains that this does not exist
            # with VTK PV-3-2-1.  This is only on installations with
            # EasyInstall / Python Eggs, then the VTK setup.py uses
            # EasyInstall and not standard distutils.  gah!
            if not os.path.exists(config.VTK_PYTHON):
                os.makedirs(config.VTK_PYTHON)

            os.chdir(self.build_dir)

            # we save, set and restore the PP env variable, else
            # stupid setuptools complains
            save_env = os.environ.get('PYTHONPATH', '')
            os.environ['PYTHONPATH'] = config.VTK_PYTHON
            ret = utils.make_command('VTK.sln', install=True)
            os.environ['PYTHONPATH'] = save_env

            if ret != 0:
                utils.error("Could not install VTK.  Fix and try again.")

    def clean_build(self):
        utils.output("Removing build and installation directories.")
        if os.path.exists(self.inst_dir):
            shutil.rmtree(self.inst_dir)

        if os.path.exists(self.build_dir):
            shutil.rmtree(self.build_dir)

    def get_installed_version(self):
        import vtk
        return vtk.vtkVersion.GetVTKVersion()

<commit_msg>Updated VTK to 5.4.2<commit_after># Copyright (c) Charl P. Botha, TU Delft.
# All rights reserved.
# See COPYRIGHT for details.

import config
from install_package import InstallPackage
import os
import shutil
import sys
import utils

# cvs -d :pserver:anonymous@public.kitware.com:/cvsroot/VTK login
# password vtk
#  cvs -d :pserver:anonymous@public.kitware.com:/cvsroot/VTK checkout
#  VTK
# cd VTK
# cvs -z3 update -R blahblah

BASENAME = "VTK"
# password "vtk" integrated in CVS -d spec
CVS_REPO = ":pserver:anonymous:vtk@public.kitware.com:/cvsroot/" + BASENAME
CVS_VERSION = "-r VTK-5-4-2"

VTK_BASE_VERSION = "vtk-5.4"

# this patch does three things:
# 1. adds try/catch blocks to all python method calls in order
#    to trap bad_alloc exceptions
# 2. implements my scheme for turning all VTK errors into Python exceptions
#    by making use of a special output window class
# 3. gives up the GIL around all VTK calls.  This is also necessary
#    for 2 not to deadlock on multi-cores.
EXC_PATCH = "pyvtk_tryexcept_and_pyexceptions.diff"

# fixes attributes in vtkproperty for shader use in python
VTKPRPRTY_PATCH = "vtkProperty_PyShaderVar.diff"

dependencies = ['cmake']

class VTK(InstallPackage):

    def __init__(self):
        self.source_dir = os.path.join(config.archive_dir, BASENAME)
        self.build_dir = os.path.join(config.build_dir, '%s-build' %
                                      (BASENAME,))
        self.inst_dir = os.path.join(config.inst_dir, BASENAME)
        self.exc_patch_filename = os.path.join(config.patches_dir,
                                               EXC_PATCH)
        self.vtkprprty_patch_filename = os.path.join(config.patches_dir,
                                                 VTKPRPRTY_PATCH)

    def update_wpvi(self):
        """Update Wrapping/Python/vtk/__init__.py to fix nasty DL bug
        on AMD64.  Thanks to Mathieu Malaterre for finding this!

        Not necessary on VTK-5-2 20080802.

        Keeping this as example on how to update select files in
        checkout.
        """

        utils.output("Updating Wrapping/Python/vtk/__init__.py.")

        utils.goto_archive()
        os.chdir(os.path.join(BASENAME, 'Wrapping', 'Python', 'vtk'))

        ret1 = os.system(
        "%s -z3 update -r 1.14 __init__.py" %
        (config.CVS,))

        if ret1 != 0:
            utils.error("Error updating W/P/v/__init__.py.")

    def get(self):
        if os.path.exists(self.source_dir):
            utils.output("VTK already checked out, skipping step.")

        else:
            utils.goto_archive()
            ret = os.system("%s -d %s co %s %s" %
                            (config.CVS, CVS_REPO, CVS_VERSION, BASENAME))
            if ret != 0:
                utils.error("Could not CVS checkout.  Fix and try again.")

            #self.update_wpvi()

            # EXC PATCH
            utils.output("Applying EXC patch")
            os.chdir(self.source_dir)
            ret = os.system(
                "%s -p0 < %s" % (config.PATCH, self.exc_patch_filename))
            if ret != 0:
                utils.error(
                    "Could not apply EXC patch.  Fix and try again.")

            # VTKPRPRTY PATCH
            utils.output("Applying VTKPRPRTY patch")
            os.chdir(os.path.join(self.source_dir, 'Rendering'))
            ret = os.system(
                "%s -p0 < %s" % (config.PATCH, self.vtkprprty_patch_filename))
            if ret != 0:
                utils.error(
                    "Could not apply VTKPRPRTY patch.  Fix and try again.")

    def unpack(self):
        pass

    def configure(self):
        if os.path.exists(
            os.path.join(self.build_dir, 'CMakeFiles/cmake.check_cache')):
            utils.output("VTK build already configured.")
            return

        if not os.path.exists(self.build_dir):
            os.mkdir(self.build_dir)

        cmake_params = "-DBUILD_SHARED_LIBS=ON " \
                       "-DBUILD_TESTING=OFF " \
                       "-DCMAKE_BUILD_TYPE=RelWithDebInfo " \
                       "-DCMAKE_INSTALL_PREFIX=%s " \
		               "-DVTK_USE_TK=NO " \
                       "-DVTK_USE_METAIO=ON " \
                       "-DVTK_USE_PARALLEL=ON " \
                       "-DPYTHON_EXECUTABLE=%s " \
                       "-DPYTHON_LIBRARY=%s " \
                       "-DPYTHON_INCLUDE_PATH=%s " \
                       "-DVTK_WRAP_PYTHON=ON " % (self.inst_dir,
                                                 config.PYTHON_EXECUTABLE,
                                                 config.PYTHON_LIBRARY,
                                                 config.PYTHON_INCLUDE_PATH)

        ret = utils.cmake_command(self.build_dir, self.source_dir,
                cmake_params)

        if ret != 0:
            utils.error("Could not configure VTK.  Fix and try again.")

    def build(self):
        posix_file = os.path.join(self.build_dir,
            'bin/libvtkWidgetsPython.so')
        nt_file = os.path.join(self.build_dir, 'bin', config.BUILD_TARGET,
                'vtkWidgetsPythonD.dll')

        if utils.file_exists(posix_file, nt_file):
            utils.output("VTK already built.  Skipping build step.")

        else:
            os.chdir(self.build_dir)
            ret = utils.make_command('VTK.sln')
            if ret != 0:
                utils.error("Error building VTK.  Fix and try again.")

    def install(self):
        config.VTK_LIB = os.path.join(self.inst_dir, 'lib')

        # whatever the case may be, we have to register VTK variables
        if os.name == 'nt':
            # on Win, inst/VTK/bin contains the so files
            config.VTK_SODIR = os.path.join(self.inst_dir, 'bin')
            # inst/VTK/lib/site-packages the VTK python package
            config.VTK_PYTHON = os.path.join(
                    config.VTK_LIB, 'site-packages')

        else:
            # on *ix, inst/VTK/lib contains DLLs
            config.VTK_SODIR = os.path.join(
                    config.VTK_LIB, VTK_BASE_VERSION)
            # on *ix, inst/lib/python2.5/site-packages contains the
            # VTK python package
            # sys.version is (2, 5, 0, 'final', 0)
            config.VTK_PYTHON = os.path.join(
                config.VTK_LIB, 'python%d.%d/site-packages' % \
                sys.version_info[0:2])

        # this contains the VTK cmake config (same on *ix and Win)
        config.VTK_DIR = os.path.join(config.VTK_LIB, VTK_BASE_VERSION)

        posix_file = os.path.join(self.inst_dir, 'bin/vtkpython')
        nt_file = os.path.join(self.inst_dir, 'bin', 'vtkpython.exe')

        if utils.file_exists(posix_file, nt_file):
            utils.output("VTK already installed.  Skipping build step.")

        else:
            # python 2.5.2 setup.py complains that this does not exist
            # with VTK PV-3-2-1.  This is only on installations with
            # EasyInstall / Python Eggs, then the VTK setup.py uses
            # EasyInstall and not standard distutils.  gah!
            if not os.path.exists(config.VTK_PYTHON):
                os.makedirs(config.VTK_PYTHON)

            os.chdir(self.build_dir)

            # we save, set and restore the PP env variable, else
            # stupid setuptools complains
            save_env = os.environ.get('PYTHONPATH', '')
            os.environ['PYTHONPATH'] = config.VTK_PYTHON
            ret = utils.make_command('VTK.sln', install=True)
            os.environ['PYTHONPATH'] = save_env

            if ret != 0:
                utils.error("Could not install VTK.  Fix and try again.")

    def clean_build(self):
        utils.output("Removing build and installation directories.")
        if os.path.exists(self.inst_dir):
            shutil.rmtree(self.inst_dir)

        if os.path.exists(self.build_dir):
            shutil.rmtree(self.build_dir)

    def get_installed_version(self):
        import vtk
        return vtk.vtkVersion.GetVTKVersion()

<|endoftext|>