repo_name
stringlengths
7
92
path
stringlengths
5
149
copies
stringlengths
1
3
size
stringlengths
4
6
content
stringlengths
911
693k
license
stringclasses
15 values
sauliusl/seaborn
seaborn/tests/test_palettes.py
3
11509
import colorsys import numpy as np import matplotlib as mpl import pytest import nose.tools as nt import numpy.testing as npt import matplotlib.pyplot as plt from .. import palettes, utils, rcmod from ..external import husl from ..colors import xkcd_rgb, crayons from distutils.version import LooseVersion mpl_ge_150 = LooseVersion(mpl.__version__) >= '1.5.0' class TestColorPalettes(object): def test_current_palette(self): pal = palettes.color_palette(["red", "blue", "green"]) rcmod.set_palette(pal) assert pal == utils.get_color_cycle() rcmod.set() def test_palette_context(self): default_pal = palettes.color_palette() context_pal = palettes.color_palette("muted") with palettes.color_palette(context_pal): nt.assert_equal(utils.get_color_cycle(), context_pal) nt.assert_equal(utils.get_color_cycle(), default_pal) def test_big_palette_context(self): original_pal = palettes.color_palette("deep", n_colors=8) context_pal = palettes.color_palette("husl", 10) rcmod.set_palette(original_pal) with palettes.color_palette(context_pal, 10): nt.assert_equal(utils.get_color_cycle(), context_pal) nt.assert_equal(utils.get_color_cycle(), original_pal) # Reset default rcmod.set() def test_palette_size(self): pal = palettes.color_palette("deep") assert len(pal) == palettes.QUAL_PALETTE_SIZES["deep"] pal = palettes.color_palette("pastel6") assert len(pal) == palettes.QUAL_PALETTE_SIZES["pastel6"] pal = palettes.color_palette("Set3") assert len(pal) == palettes.QUAL_PALETTE_SIZES["Set3"] pal = palettes.color_palette("husl") assert len(pal) == 6 pal = palettes.color_palette("Greens") assert len(pal) == 6 def test_seaborn_palettes(self): pals = "deep", "muted", "pastel", "bright", "dark", "colorblind" for name in pals: full = palettes.color_palette(name, 10).as_hex() short = palettes.color_palette(name + "6", 6).as_hex() b, _, g, r, m, _, _, _, y, c = full assert [b, g, r, m, y, c] == list(short) def test_hls_palette(self): hls_pal1 = palettes.hls_palette() hls_pal2 = palettes.color_palette("hls") npt.assert_array_equal(hls_pal1, hls_pal2) def test_husl_palette(self): husl_pal1 = palettes.husl_palette() husl_pal2 = palettes.color_palette("husl") npt.assert_array_equal(husl_pal1, husl_pal2) def test_mpl_palette(self): mpl_pal1 = palettes.mpl_palette("Reds") mpl_pal2 = palettes.color_palette("Reds") npt.assert_array_equal(mpl_pal1, mpl_pal2) def test_mpl_dark_palette(self): mpl_pal1 = palettes.mpl_palette("Blues_d") mpl_pal2 = palettes.color_palette("Blues_d") npt.assert_array_equal(mpl_pal1, mpl_pal2) def test_bad_palette_name(self): with nt.assert_raises(ValueError): palettes.color_palette("IAmNotAPalette") def test_terrible_palette_name(self): with nt.assert_raises(ValueError): palettes.color_palette("jet") def test_bad_palette_colors(self): pal = ["red", "blue", "iamnotacolor"] with nt.assert_raises(ValueError): palettes.color_palette(pal) def test_palette_desat(self): pal1 = palettes.husl_palette(6) pal1 = [utils.desaturate(c, .5) for c in pal1] pal2 = palettes.color_palette("husl", desat=.5) npt.assert_array_equal(pal1, pal2) def test_palette_is_list_of_tuples(self): pal_in = np.array(["red", "blue", "green"]) pal_out = palettes.color_palette(pal_in, 3) nt.assert_is_instance(pal_out, list) nt.assert_is_instance(pal_out[0], tuple) nt.assert_is_instance(pal_out[0][0], float) nt.assert_equal(len(pal_out[0]), 3) def test_palette_cycles(self): deep = palettes.color_palette("deep6") double_deep = palettes.color_palette("deep6", 12) nt.assert_equal(double_deep, deep + deep) def test_hls_values(self): pal1 = palettes.hls_palette(6, h=0) pal2 = palettes.hls_palette(6, h=.5) pal2 = pal2[3:] + pal2[:3] npt.assert_array_almost_equal(pal1, pal2) pal_dark = palettes.hls_palette(5, l=.2) # noqa pal_bright = palettes.hls_palette(5, l=.8) # noqa npt.assert_array_less(list(map(sum, pal_dark)), list(map(sum, pal_bright))) pal_flat = palettes.hls_palette(5, s=.1) pal_bold = palettes.hls_palette(5, s=.9) npt.assert_array_less(list(map(np.std, pal_flat)), list(map(np.std, pal_bold))) def test_husl_values(self): pal1 = palettes.husl_palette(6, h=0) pal2 = palettes.husl_palette(6, h=.5) pal2 = pal2[3:] + pal2[:3] npt.assert_array_almost_equal(pal1, pal2) pal_dark = palettes.husl_palette(5, l=.2) # noqa pal_bright = palettes.husl_palette(5, l=.8) # noqa npt.assert_array_less(list(map(sum, pal_dark)), list(map(sum, pal_bright))) pal_flat = palettes.husl_palette(5, s=.1) pal_bold = palettes.husl_palette(5, s=.9) npt.assert_array_less(list(map(np.std, pal_flat)), list(map(np.std, pal_bold))) def test_cbrewer_qual(self): pal_short = palettes.mpl_palette("Set1", 4) pal_long = palettes.mpl_palette("Set1", 6) nt.assert_equal(pal_short, pal_long[:4]) pal_full = palettes.mpl_palette("Set2", 8) pal_long = palettes.mpl_palette("Set2", 10) nt.assert_equal(pal_full, pal_long[:8]) def test_mpl_reversal(self): pal_forward = palettes.mpl_palette("BuPu", 6) pal_reverse = palettes.mpl_palette("BuPu_r", 6) npt.assert_array_almost_equal(pal_forward, pal_reverse[::-1]) def test_rgb_from_hls(self): color = .5, .8, .4 rgb_got = palettes._color_to_rgb(color, "hls") rgb_want = colorsys.hls_to_rgb(*color) nt.assert_equal(rgb_got, rgb_want) def test_rgb_from_husl(self): color = 120, 50, 40 rgb_got = palettes._color_to_rgb(color, "husl") rgb_want = husl.husl_to_rgb(*color) nt.assert_equal(rgb_got, rgb_want) def test_rgb_from_xkcd(self): color = "dull red" rgb_got = palettes._color_to_rgb(color, "xkcd") rgb_want = xkcd_rgb[color] nt.assert_equal(rgb_got, rgb_want) def test_light_palette(self): pal_forward = palettes.light_palette("red") pal_reverse = palettes.light_palette("red", reverse=True) npt.assert_array_almost_equal(pal_forward, pal_reverse[::-1]) red = tuple(mpl.colors.colorConverter.to_rgba("red")) nt.assert_equal(tuple(pal_forward[-1]), red) pal_cmap = palettes.light_palette("blue", as_cmap=True) nt.assert_is_instance(pal_cmap, mpl.colors.LinearSegmentedColormap) def test_dark_palette(self): pal_forward = palettes.dark_palette("red") pal_reverse = palettes.dark_palette("red", reverse=True) npt.assert_array_almost_equal(pal_forward, pal_reverse[::-1]) red = tuple(mpl.colors.colorConverter.to_rgba("red")) nt.assert_equal(tuple(pal_forward[-1]), red) pal_cmap = palettes.dark_palette("blue", as_cmap=True) nt.assert_is_instance(pal_cmap, mpl.colors.LinearSegmentedColormap) def test_blend_palette(self): colors = ["red", "yellow", "white"] pal_cmap = palettes.blend_palette(colors, as_cmap=True) nt.assert_is_instance(pal_cmap, mpl.colors.LinearSegmentedColormap) def test_cubehelix_against_matplotlib(self): x = np.linspace(0, 1, 8) mpl_pal = mpl.cm.cubehelix(x)[:, :3].tolist() sns_pal = palettes.cubehelix_palette(8, start=0.5, rot=-1.5, hue=1, dark=0, light=1, reverse=True) nt.assert_list_equal(sns_pal, mpl_pal) def test_cubehelix_n_colors(self): for n in [3, 5, 8]: pal = palettes.cubehelix_palette(n) nt.assert_equal(len(pal), n) def test_cubehelix_reverse(self): pal_forward = palettes.cubehelix_palette() pal_reverse = palettes.cubehelix_palette(reverse=True) nt.assert_list_equal(pal_forward, pal_reverse[::-1]) def test_cubehelix_cmap(self): cmap = palettes.cubehelix_palette(as_cmap=True) nt.assert_is_instance(cmap, mpl.colors.ListedColormap) pal = palettes.cubehelix_palette() x = np.linspace(0, 1, 6) npt.assert_array_equal(cmap(x)[:, :3], pal) cmap_rev = palettes.cubehelix_palette(as_cmap=True, reverse=True) x = np.linspace(0, 1, 6) pal_forward = cmap(x).tolist() pal_reverse = cmap_rev(x[::-1]).tolist() nt.assert_list_equal(pal_forward, pal_reverse) def test_cubehelix_code(self): color_palette = palettes.color_palette cubehelix_palette = palettes.cubehelix_palette pal1 = color_palette("ch:", 8) pal2 = color_palette(cubehelix_palette(8)) assert pal1 == pal2 pal1 = color_palette("ch:.5, -.25,hue = .5,light=.75", 8) pal2 = color_palette(cubehelix_palette(8, .5, -.25, hue=.5, light=.75)) assert pal1 == pal2 pal1 = color_palette("ch:h=1,r=.5", 9) pal2 = color_palette(cubehelix_palette(9, hue=1, rot=.5)) assert pal1 == pal2 pal1 = color_palette("ch:_r", 6) pal2 = color_palette(cubehelix_palette(6, reverse=True)) assert pal1 == pal2 def test_xkcd_palette(self): names = list(xkcd_rgb.keys())[10:15] colors = palettes.xkcd_palette(names) for name, color in zip(names, colors): as_hex = mpl.colors.rgb2hex(color) nt.assert_equal(as_hex, xkcd_rgb[name]) def test_crayon_palette(self): names = list(crayons.keys())[10:15] colors = palettes.crayon_palette(names) for name, color in zip(names, colors): as_hex = mpl.colors.rgb2hex(color) nt.assert_equal(as_hex, crayons[name].lower()) def test_color_codes(self): palettes.set_color_codes("deep") colors = palettes.color_palette("deep6") + [".1"] for code, color in zip("bgrmyck", colors): rgb_want = mpl.colors.colorConverter.to_rgb(color) rgb_got = mpl.colors.colorConverter.to_rgb(code) nt.assert_equal(rgb_want, rgb_got) palettes.set_color_codes("reset") with pytest.raises(ValueError): palettes.set_color_codes("Set1") def test_as_hex(self): pal = palettes.color_palette("deep") for rgb, hex in zip(pal, pal.as_hex()): nt.assert_equal(mpl.colors.rgb2hex(rgb), hex) def test_preserved_palette_length(self): pal_in = palettes.color_palette("Set1", 10) pal_out = palettes.color_palette(pal_in) nt.assert_equal(pal_in, pal_out) def test_get_color_cycle(self): if mpl_ge_150: colors = [(1., 0., 0.), (0, 1., 0.)] prop_cycle = plt.cycler(color=colors) with plt.rc_context({"axes.prop_cycle": prop_cycle}): result = utils.get_color_cycle() assert result == colors
bsd-3-clause
Karel-van-de-Plassche/bokeh
bokeh/util/sampledata.py
2
6899
#----------------------------------------------------------------------------- # Copyright (c) 2012 - 2017, Anaconda, Inc. All rights reserved. # # Powered by the Bokeh Development Team. # # The full license is in the file LICENSE.txt, distributed with this software. #----------------------------------------------------------------------------- ''' Helper functions for downloading and accessing sample data. ''' #----------------------------------------------------------------------------- # Boilerplate #----------------------------------------------------------------------------- from __future__ import absolute_import, division, print_function, unicode_literals import logging log = logging.getLogger(__name__) #----------------------------------------------------------------------------- # Imports #----------------------------------------------------------------------------- # Standard library imports from os import mkdir, remove from os.path import abspath, dirname, exists, expanduser, isdir, isfile, join, splitext from sys import stdout from zipfile import ZipFile # External imports import six from six.moves.urllib.request import urlopen # Bokeh imports from .dependencies import import_required #----------------------------------------------------------------------------- # Globals and constants #----------------------------------------------------------------------------- __all__ = ( 'download', ) #----------------------------------------------------------------------------- # General API #----------------------------------------------------------------------------- def download(progress=True): ''' Download larger data sets for various Bokeh examples. ''' data_dir = external_data_dir(create=True) print("Using data directory: %s" % data_dir) s3 = 'https://s3.amazonaws.com/bokeh_data/' files = [ (s3, 'CGM.csv'), (s3, 'US_Counties.zip'), (s3, 'us_cities.json'), (s3, 'unemployment09.csv'), (s3, 'AAPL.csv'), (s3, 'FB.csv'), (s3, 'GOOG.csv'), (s3, 'IBM.csv'), (s3, 'MSFT.csv'), (s3, 'WPP2012_SA_DB03_POPULATION_QUINQUENNIAL.zip'), (s3, 'gapminder_fertility.csv'), (s3, 'gapminder_population.csv'), (s3, 'gapminder_life_expectancy.csv'), (s3, 'gapminder_regions.csv'), (s3, 'world_cities.zip'), (s3, 'airports.json'), (s3, 'movies.db.zip'), (s3, 'airports.csv'), (s3, 'routes.csv'), ] for base_url, filename in files: _download_file(base_url, filename, data_dir, progress=progress) #----------------------------------------------------------------------------- # Dev API #----------------------------------------------------------------------------- def external_csv(module, name, **kw): ''' ''' pd = import_required('pandas', '%s sample data requires Pandas (http://pandas.pydata.org) to be installed' % module) return pd.read_csv(external_path(name), **kw) def external_data_dir(create=False): ''' ''' try: import yaml except ImportError: raise RuntimeError("'yaml' and 'pyyaml' are required to use bokeh.sampledata functions") bokeh_dir = _bokeh_dir(create=create) data_dir = join(bokeh_dir, "data") try: config = yaml.load(open(join(bokeh_dir, 'config'))) data_dir = expanduser(config['sampledata_dir']) except (IOError, TypeError): pass if not exists(data_dir): if not create: raise RuntimeError('bokeh sample data directory does not exist, please execute bokeh.sampledata.download()') print("Creating %s directory" % data_dir) try: mkdir(data_dir) except OSError: raise RuntimeError("could not create bokeh data directory at %s" % data_dir) else: if not isdir(data_dir): raise RuntimeError("%s exists but is not a directory" % data_dir) return data_dir def external_path(filename): data_dir = external_data_dir() fn = join(data_dir, filename) if not exists(fn) and isfile(fn): raise RuntimeError('Could not locate external data file %e. Please execute bokeh.sampledata.download()' % fn) return fn def package_csv(module, name, **kw): ''' ''' pd = import_required('pandas', '%s sample data requires Pandas (http://pandas.pydata.org) to be installed' % module) return pd.read_csv(package_path(name), **kw) def package_dir(): ''' ''' return abspath(join(dirname(__file__), "..", "sampledata", "_data")) def package_path(filename): ''' ''' return join(package_dir(), filename) def open_csv(filename): ''' ''' # csv differs in Python 2.x and Python 3.x. Open the file differently in each. if six.PY2: return open(filename, 'rb') else: return open(filename, 'r', newline='', encoding='utf8') #----------------------------------------------------------------------------- # Private API #----------------------------------------------------------------------------- def _bokeh_dir(create=False): ''' ''' bokeh_dir = join(expanduser("~"), ".bokeh") if not exists(bokeh_dir): if not create: return bokeh_dir print("Creating %s directory" % bokeh_dir) try: mkdir(bokeh_dir) except OSError: raise RuntimeError("could not create bokeh config directory at %s" % bokeh_dir) else: if not isdir(bokeh_dir): raise RuntimeError("%s exists but is not a directory" % bokeh_dir) return bokeh_dir def _download_file(base_url, filename, data_dir, progress=True): ''' ''' file_url = join(base_url, filename) file_path = join(data_dir, filename) url = urlopen(file_url) with open(file_path, 'wb') as file: file_size = int(url.headers["Content-Length"]) print("Downloading: %s (%d bytes)" % (filename, file_size)) fetch_size = 0 block_size = 16384 while True: data = url.read(block_size) if not data: break fetch_size += len(data) file.write(data) if progress: status = "\r%10d [%6.2f%%]" % (fetch_size, fetch_size*100.0/file_size) stdout.write(status) stdout.flush() if progress: print() real_name, ext = splitext(filename) if ext == '.zip': if not splitext(real_name)[1]: real_name += ".csv" print("Unpacking: %s" % real_name) with ZipFile(file_path, 'r') as zip_file: zip_file.extract(real_name, data_dir) remove(file_path) #----------------------------------------------------------------------------- # Code #-----------------------------------------------------------------------------
bsd-3-clause
valexandersaulys/prudential_insurance_kaggle
venv/lib/python2.7/site-packages/pandas/tests/test_common.py
9
44081
# -*- coding: utf-8 -*- import collections from datetime import datetime import re import nose from nose.tools import assert_equal, assert_true import numpy as np import pandas as pd from pandas.tslib import iNaT, NaT from pandas import Series, DataFrame, date_range, DatetimeIndex, Timestamp, Float64Index from pandas import compat from pandas.compat import range, long, lrange, lmap, u from pandas.core.common import notnull, isnull, array_equivalent import pandas.core.common as com import pandas.core.convert as convert import pandas.core.format as fmt import pandas.util.testing as tm import pandas.core.config as cf _multiprocess_can_split_ = True def test_mut_exclusive(): msg = "mutually exclusive arguments: '[ab]' and '[ab]'" with tm.assertRaisesRegexp(TypeError, msg): com._mut_exclusive(a=1, b=2) assert com._mut_exclusive(a=1, b=None) == 1 assert com._mut_exclusive(major=None, major_axis=None) is None def test_is_sequence(): is_seq = com.is_sequence assert(is_seq((1, 2))) assert(is_seq([1, 2])) assert(not is_seq("abcd")) assert(not is_seq(u("abcd"))) assert(not is_seq(np.int64)) class A(object): def __getitem__(self): return 1 assert(not is_seq(A())) def test_get_callable_name(): from functools import partial getname = com._get_callable_name def fn(x): return x lambda_ = lambda x: x part1 = partial(fn) part2 = partial(part1) class somecall(object): def __call__(self): return x assert getname(fn) == 'fn' assert getname(lambda_) assert getname(part1) == 'fn' assert getname(part2) == 'fn' assert getname(somecall()) == 'somecall' assert getname(1) is None #Issue 10859 class TestABCClasses(tm.TestCase): tuples = [[1, 2, 2], ['red', 'blue', 'red']] multi_index = pd.MultiIndex.from_arrays(tuples, names=('number', 'color')) datetime_index = pd.to_datetime(['2000/1/1', '2010/1/1']) timedelta_index = pd.to_timedelta(np.arange(5), unit='s') period_index = pd.period_range('2000/1/1', '2010/1/1/', freq='M') categorical = pd.Categorical([1, 2, 3], categories=[2, 3, 1]) categorical_df = pd.DataFrame({"values": [1, 2, 3]}, index=categorical) df = pd.DataFrame({'names': ['a', 'b', 'c']}, index=multi_index) sparse_series = pd.Series([1, 2, 3]).to_sparse() sparse_array = pd.SparseArray(np.random.randn(10)) def test_abc_types(self): self.assertIsInstance(pd.Index(['a', 'b', 'c']), com.ABCIndex) self.assertIsInstance(pd.Int64Index([1, 2, 3]), com.ABCInt64Index) self.assertIsInstance(pd.Float64Index([1, 2, 3]), com.ABCFloat64Index) self.assertIsInstance(self.multi_index, com.ABCMultiIndex) self.assertIsInstance(self.datetime_index, com.ABCDatetimeIndex) self.assertIsInstance(self.timedelta_index, com.ABCTimedeltaIndex) self.assertIsInstance(self.period_index, com.ABCPeriodIndex) self.assertIsInstance(self.categorical_df.index, com.ABCCategoricalIndex) self.assertIsInstance(pd.Index(['a', 'b', 'c']), com.ABCIndexClass) self.assertIsInstance(pd.Int64Index([1, 2, 3]), com.ABCIndexClass) self.assertIsInstance(pd.Series([1, 2, 3]), com.ABCSeries) self.assertIsInstance(self.df, com.ABCDataFrame) self.assertIsInstance(self.df.to_panel(), com.ABCPanel) self.assertIsInstance(self.sparse_series, com.ABCSparseSeries) self.assertIsInstance(self.sparse_array, com.ABCSparseArray) self.assertIsInstance(self.categorical, com.ABCCategorical) self.assertIsInstance(pd.Period('2012', freq='A-DEC'), com.ABCPeriod) class TestInferDtype(tm.TestCase): def test_infer_dtype_from_scalar(self): # Test that _infer_dtype_from_scalar is returning correct dtype for int and float. for dtypec in [ np.uint8, np.int8, np.uint16, np.int16, np.uint32, np.int32, np.uint64, np.int64 ]: data = dtypec(12) dtype, val = com._infer_dtype_from_scalar(data) self.assertEqual(dtype, type(data)) data = 12 dtype, val = com._infer_dtype_from_scalar(data) self.assertEqual(dtype, np.int64) for dtypec in [ np.float16, np.float32, np.float64 ]: data = dtypec(12) dtype, val = com._infer_dtype_from_scalar(data) self.assertEqual(dtype, dtypec) data = np.float(12) dtype, val = com._infer_dtype_from_scalar(data) self.assertEqual(dtype, np.float64) for data in [ True, False ]: dtype, val = com._infer_dtype_from_scalar(data) self.assertEqual(dtype, np.bool_) for data in [ np.complex64(1), np.complex128(1) ]: dtype, val = com._infer_dtype_from_scalar(data) self.assertEqual(dtype, np.complex_) import datetime for data in [ np.datetime64(1,'ns'), pd.Timestamp(1), datetime.datetime(2000,1,1,0,0) ]: dtype, val = com._infer_dtype_from_scalar(data) self.assertEqual(dtype, 'M8[ns]') for data in [ np.timedelta64(1,'ns'), pd.Timedelta(1), datetime.timedelta(1) ]: dtype, val = com._infer_dtype_from_scalar(data) self.assertEqual(dtype, 'm8[ns]') for data in [ datetime.date(2000,1,1), pd.Timestamp(1,tz='US/Eastern'), 'foo' ]: dtype, val = com._infer_dtype_from_scalar(data) self.assertEqual(dtype, np.object_) def test_notnull(): assert notnull(1.) assert not notnull(None) assert not notnull(np.NaN) with cf.option_context("mode.use_inf_as_null", False): assert notnull(np.inf) assert notnull(-np.inf) arr = np.array([1.5, np.inf, 3.5, -np.inf]) result = notnull(arr) assert result.all() with cf.option_context("mode.use_inf_as_null", True): assert not notnull(np.inf) assert not notnull(-np.inf) arr = np.array([1.5, np.inf, 3.5, -np.inf]) result = notnull(arr) assert result.sum() == 2 with cf.option_context("mode.use_inf_as_null", False): for s in [tm.makeFloatSeries(),tm.makeStringSeries(), tm.makeObjectSeries(),tm.makeTimeSeries(),tm.makePeriodSeries()]: assert(isinstance(isnull(s), Series)) def test_isnull(): assert not isnull(1.) assert isnull(None) assert isnull(np.NaN) assert not isnull(np.inf) assert not isnull(-np.inf) # series for s in [tm.makeFloatSeries(),tm.makeStringSeries(), tm.makeObjectSeries(),tm.makeTimeSeries(),tm.makePeriodSeries()]: assert(isinstance(isnull(s), Series)) # frame for df in [tm.makeTimeDataFrame(),tm.makePeriodFrame(),tm.makeMixedDataFrame()]: result = isnull(df) expected = df.apply(isnull) tm.assert_frame_equal(result, expected) # panel for p in [ tm.makePanel(), tm.makePeriodPanel(), tm.add_nans(tm.makePanel()) ]: result = isnull(p) expected = p.apply(isnull) tm.assert_panel_equal(result, expected) # panel 4d for p in [ tm.makePanel4D(), tm.add_nans_panel4d(tm.makePanel4D()) ]: result = isnull(p) expected = p.apply(isnull) tm.assert_panel4d_equal(result, expected) def test_isnull_lists(): result = isnull([[False]]) exp = np.array([[False]]) assert(np.array_equal(result, exp)) result = isnull([[1], [2]]) exp = np.array([[False], [False]]) assert(np.array_equal(result, exp)) # list of strings / unicode result = isnull(['foo', 'bar']) assert(not result.any()) result = isnull([u('foo'), u('bar')]) assert(not result.any()) def test_isnull_nat(): result = isnull([NaT]) exp = np.array([True]) assert(np.array_equal(result, exp)) result = isnull(np.array([NaT], dtype=object)) exp = np.array([True]) assert(np.array_equal(result, exp)) def test_isnull_numpy_nat(): arr = np.array([NaT, np.datetime64('NaT'), np.timedelta64('NaT'), np.datetime64('NaT', 's')]) result = isnull(arr) expected = np.array([True] * 4) tm.assert_numpy_array_equal(result, expected) def test_isnull_datetime(): assert (not isnull(datetime.now())) assert notnull(datetime.now()) idx = date_range('1/1/1990', periods=20) assert(notnull(idx).all()) idx = np.asarray(idx) idx[0] = iNaT idx = DatetimeIndex(idx) mask = isnull(idx) assert(mask[0]) assert(not mask[1:].any()) # GH 9129 pidx = idx.to_period(freq='M') mask = isnull(pidx) assert(mask[0]) assert(not mask[1:].any()) mask = isnull(pidx[1:]) assert(not mask.any()) class TestIsNull(tm.TestCase): def test_0d_array(self): self.assertTrue(isnull(np.array(np.nan))) self.assertFalse(isnull(np.array(0.0))) self.assertFalse(isnull(np.array(0))) # test object dtype self.assertTrue(isnull(np.array(np.nan, dtype=object))) self.assertFalse(isnull(np.array(0.0, dtype=object))) self.assertFalse(isnull(np.array(0, dtype=object))) def test_downcast_conv(): # test downcasting arr = np.array([8.5, 8.6, 8.7, 8.8, 8.9999999999995]) result = com._possibly_downcast_to_dtype(arr, 'infer') assert (np.array_equal(result, arr)) arr = np.array([8., 8., 8., 8., 8.9999999999995]) result = com._possibly_downcast_to_dtype(arr, 'infer') expected = np.array([8, 8, 8, 8, 9]) assert (np.array_equal(result, expected)) arr = np.array([8., 8., 8., 8., 9.0000000000005]) result = com._possibly_downcast_to_dtype(arr, 'infer') expected = np.array([8, 8, 8, 8, 9]) assert (np.array_equal(result, expected)) # conversions expected = np.array([1,2]) for dtype in [np.float64,object,np.int64]: arr = np.array([1.0,2.0],dtype=dtype) result = com._possibly_downcast_to_dtype(arr,'infer') tm.assert_almost_equal(result, expected) expected = np.array([1.0,2.0,np.nan]) for dtype in [np.float64,object]: arr = np.array([1.0,2.0,np.nan],dtype=dtype) result = com._possibly_downcast_to_dtype(arr,'infer') tm.assert_almost_equal(result, expected) # empties for dtype in [np.int32,np.float64,np.float32,np.bool_,np.int64,object]: arr = np.array([],dtype=dtype) result = com._possibly_downcast_to_dtype(arr,'int64') tm.assert_almost_equal(result, np.array([],dtype=np.int64)) assert result.dtype == np.int64 def test_array_equivalent(): assert array_equivalent(np.array([np.nan, np.nan]), np.array([np.nan, np.nan])) assert array_equivalent(np.array([np.nan, 1, np.nan]), np.array([np.nan, 1, np.nan])) assert array_equivalent(np.array([np.nan, None], dtype='object'), np.array([np.nan, None], dtype='object')) assert array_equivalent(np.array([np.nan, 1+1j], dtype='complex'), np.array([np.nan, 1+1j], dtype='complex')) assert not array_equivalent(np.array([np.nan, 1+1j], dtype='complex'), np.array([np.nan, 1+2j], dtype='complex')) assert not array_equivalent(np.array([np.nan, 1, np.nan]), np.array([np.nan, 2, np.nan])) assert not array_equivalent(np.array(['a', 'b', 'c', 'd']), np.array(['e', 'e'])) assert array_equivalent(Float64Index([0, np.nan]), Float64Index([0, np.nan])) assert not array_equivalent(Float64Index([0, np.nan]), Float64Index([1, np.nan])) assert array_equivalent(DatetimeIndex([0, np.nan]), DatetimeIndex([0, np.nan])) assert not array_equivalent(DatetimeIndex([0, np.nan]), DatetimeIndex([1, np.nan])) def test_datetimeindex_from_empty_datetime64_array(): for unit in [ 'ms', 'us', 'ns' ]: idx = DatetimeIndex(np.array([], dtype='datetime64[%s]' % unit)) assert(len(idx) == 0) def test_nan_to_nat_conversions(): df = DataFrame(dict({ 'A' : np.asarray(lrange(10),dtype='float64'), 'B' : Timestamp('20010101') })) df.iloc[3:6,:] = np.nan result = df.loc[4,'B'].value assert(result == iNaT) s = df['B'].copy() s._data = s._data.setitem(indexer=tuple([slice(8,9)]),value=np.nan) assert(isnull(s[8])) # numpy < 1.7.0 is wrong from distutils.version import LooseVersion if LooseVersion(np.__version__) >= '1.7.0': assert(s[8].value == np.datetime64('NaT').astype(np.int64)) def test_any_none(): assert(com._any_none(1, 2, 3, None)) assert(not com._any_none(1, 2, 3, 4)) def test_all_not_none(): assert(com._all_not_none(1, 2, 3, 4)) assert(not com._all_not_none(1, 2, 3, None)) assert(not com._all_not_none(None, None, None, None)) def test_repr_binary_type(): import string letters = string.ascii_letters btype = compat.binary_type try: raw = btype(letters, encoding=cf.get_option('display.encoding')) except TypeError: raw = btype(letters) b = compat.text_type(compat.bytes_to_str(raw)) res = com.pprint_thing(b, quote_strings=True) assert_equal(res, repr(b)) res = com.pprint_thing(b, quote_strings=False) assert_equal(res, b) def test_adjoin(): data = [['a', 'b', 'c'], ['dd', 'ee', 'ff'], ['ggg', 'hhh', 'iii']] expected = 'a dd ggg\nb ee hhh\nc ff iii' adjoined = com.adjoin(2, *data) assert(adjoined == expected) class TestFormattBase(tm.TestCase): def test_adjoin(self): data = [['a', 'b', 'c'], ['dd', 'ee', 'ff'], ['ggg', 'hhh', 'iii']] expected = 'a dd ggg\nb ee hhh\nc ff iii' adjoined = com.adjoin(2, *data) self.assertEqual(adjoined, expected) def test_adjoin_unicode(self): data = [[u'あ', 'b', 'c'], ['dd', u'ええ', 'ff'], ['ggg', 'hhh', u'いいい']] expected = u'あ dd ggg\nb ええ hhh\nc ff いいい' adjoined = com.adjoin(2, *data) self.assertEqual(adjoined, expected) adj = fmt.EastAsianTextAdjustment() expected = u"""あ dd ggg b ええ hhh c ff いいい""" adjoined = adj.adjoin(2, *data) self.assertEqual(adjoined, expected) cols = adjoined.split('\n') self.assertEqual(adj.len(cols[0]), 13) self.assertEqual(adj.len(cols[1]), 13) self.assertEqual(adj.len(cols[2]), 16) expected = u"""あ dd ggg b ええ hhh c ff いいい""" adjoined = adj.adjoin(7, *data) self.assertEqual(adjoined, expected) cols = adjoined.split('\n') self.assertEqual(adj.len(cols[0]), 23) self.assertEqual(adj.len(cols[1]), 23) self.assertEqual(adj.len(cols[2]), 26) def test_justify(self): adj = fmt.EastAsianTextAdjustment() def just(x, *args, **kwargs): # wrapper to test single str return adj.justify([x], *args, **kwargs)[0] self.assertEqual(just('abc', 5, mode='left'), 'abc ') self.assertEqual(just('abc', 5, mode='center'), ' abc ') self.assertEqual(just('abc', 5, mode='right'), ' abc') self.assertEqual(just(u'abc', 5, mode='left'), 'abc ') self.assertEqual(just(u'abc', 5, mode='center'), ' abc ') self.assertEqual(just(u'abc', 5, mode='right'), ' abc') self.assertEqual(just(u'パンダ', 5, mode='left'), u'パンダ') self.assertEqual(just(u'パンダ', 5, mode='center'), u'パンダ') self.assertEqual(just(u'パンダ', 5, mode='right'), u'パンダ') self.assertEqual(just(u'パンダ', 10, mode='left'), u'パンダ ') self.assertEqual(just(u'パンダ', 10, mode='center'), u' パンダ ') self.assertEqual(just(u'パンダ', 10, mode='right'), u' パンダ') def test_east_asian_len(self): adj = fmt.EastAsianTextAdjustment() self.assertEqual(adj.len('abc'), 3) self.assertEqual(adj.len(u'abc'), 3) self.assertEqual(adj.len(u'パンダ'), 6) self.assertEqual(adj.len(u'パンダ'), 5) self.assertEqual(adj.len(u'パンダpanda'), 11) self.assertEqual(adj.len(u'パンダpanda'), 10) def test_ambiguous_width(self): adj = fmt.EastAsianTextAdjustment() self.assertEqual(adj.len(u'¡¡ab'), 4) with cf.option_context('display.unicode.ambiguous_as_wide', True): adj = fmt.EastAsianTextAdjustment() self.assertEqual(adj.len(u'¡¡ab'), 6) data = [[u'あ', 'b', 'c'], ['dd', u'ええ', 'ff'], ['ggg', u'¡¡ab', u'いいい']] expected = u'あ dd ggg \nb ええ ¡¡ab\nc ff いいい' adjoined = adj.adjoin(2, *data) self.assertEqual(adjoined, expected) def test_iterpairs(): data = [1, 2, 3, 4] expected = [(1, 2), (2, 3), (3, 4)] result = list(com.iterpairs(data)) assert(result == expected) def test_split_ranges(): def _bin(x, width): "return int(x) as a base2 string of given width" return ''.join(str((x >> i) & 1) for i in range(width - 1, -1, -1)) def test_locs(mask): nfalse = sum(np.array(mask) == 0) remaining = 0 for s, e in com.split_ranges(mask): remaining += e - s assert 0 not in mask[s:e] # make sure the total items covered by the ranges are a complete cover assert remaining + nfalse == len(mask) # exhaustively test all possible mask sequences of length 8 ncols = 8 for i in range(2 ** ncols): cols = lmap(int, list(_bin(i, ncols))) # count up in base2 mask = [cols[i] == 1 for i in range(len(cols))] test_locs(mask) # base cases test_locs([]) test_locs([0]) test_locs([1]) def test_indent(): s = 'a b c\nd e f' result = com.indent(s, spaces=6) assert(result == ' a b c\n d e f') def test_banner(): ban = com.banner('hi') assert(ban == ('%s\nhi\n%s' % ('=' * 80, '=' * 80))) def test_map_indices_py(): data = [4, 3, 2, 1] expected = {4: 0, 3: 1, 2: 2, 1: 3} result = com.map_indices_py(data) assert(result == expected) def test_union(): a = [1, 2, 3] b = [4, 5, 6] union = sorted(com.union(a, b)) assert((a + b) == union) def test_difference(): a = [1, 2, 3] b = [1, 2, 3, 4, 5, 6] inter = sorted(com.difference(b, a)) assert([4, 5, 6] == inter) def test_intersection(): a = [1, 2, 3] b = [1, 2, 3, 4, 5, 6] inter = sorted(com.intersection(a, b)) assert(a == inter) def test_groupby(): values = ['foo', 'bar', 'baz', 'baz2', 'qux', 'foo3'] expected = {'f': ['foo', 'foo3'], 'b': ['bar', 'baz', 'baz2'], 'q': ['qux']} grouped = com.groupby(values, lambda x: x[0]) for k, v in grouped: assert v == expected[k] def test_is_list_like(): passes = ([], [1], (1,), (1, 2), {'a': 1}, set([1, 'a']), Series([1]), Series([]), Series(['a']).str) fails = (1, '2', object()) for p in passes: assert com.is_list_like(p) for f in fails: assert not com.is_list_like(f) def test_is_named_tuple(): passes = (collections.namedtuple('Test',list('abc'))(1,2,3),) fails = ((1,2,3), 'a', Series({'pi':3.14})) for p in passes: assert com.is_named_tuple(p) for f in fails: assert not com.is_named_tuple(f) def test_is_hashable(): # all new-style classes are hashable by default class HashableClass(object): pass class UnhashableClass1(object): __hash__ = None class UnhashableClass2(object): def __hash__(self): raise TypeError("Not hashable") hashable = ( 1, 3.14, np.float64(3.14), 'a', tuple(), (1,), HashableClass(), ) not_hashable = ( [], UnhashableClass1(), ) abc_hashable_not_really_hashable = ( ([],), UnhashableClass2(), ) for i in hashable: assert com.is_hashable(i) for i in not_hashable: assert not com.is_hashable(i) for i in abc_hashable_not_really_hashable: assert not com.is_hashable(i) # numpy.array is no longer collections.Hashable as of # https://github.com/numpy/numpy/pull/5326, just test # pandas.common.is_hashable() assert not com.is_hashable(np.array([])) # old-style classes in Python 2 don't appear hashable to # collections.Hashable but also seem to support hash() by default if compat.PY2: class OldStyleClass(): pass c = OldStyleClass() assert not isinstance(c, collections.Hashable) assert com.is_hashable(c) hash(c) # this will not raise def test_ensure_int32(): values = np.arange(10, dtype=np.int32) result = com._ensure_int32(values) assert(result.dtype == np.int32) values = np.arange(10, dtype=np.int64) result = com._ensure_int32(values) assert(result.dtype == np.int32) def test_ensure_platform_int(): # verify that when we create certain types of indices # they remain the correct type under platform conversions from pandas.core.index import Int64Index # int64 x = Int64Index([1, 2, 3], dtype='int64') assert(x.dtype == np.int64) pi = com._ensure_platform_int(x) assert(pi.dtype == np.int_) # int32 x = Int64Index([1, 2, 3], dtype='int32') assert(x.dtype == np.int32) pi = com._ensure_platform_int(x) assert(pi.dtype == np.int_) # TODO: fix this broken test # def test_console_encode(): # """ # On Python 2, if sys.stdin.encoding is None (IPython with zmq frontend) # common.console_encode should encode things as utf-8. # """ # if compat.PY3: # raise nose.SkipTest # with tm.stdin_encoding(encoding=None): # result = com.console_encode(u"\u05d0") # expected = u"\u05d0".encode('utf-8') # assert (result == expected) def test_is_re(): passes = re.compile('ad'), fails = 'x', 2, 3, object() for p in passes: assert com.is_re(p) for f in fails: assert not com.is_re(f) def test_is_recompilable(): passes = (r'a', u('x'), r'asdf', re.compile('adsf'), u(r'\u2233\s*'), re.compile(r'')) fails = 1, [], object() for p in passes: assert com.is_re_compilable(p) for f in fails: assert not com.is_re_compilable(f) def test_random_state(): import numpy.random as npr # Check with seed state = com._random_state(5) assert_equal(state.uniform(), npr.RandomState(5).uniform()) # Check with random state object state2 = npr.RandomState(10) assert_equal(com._random_state(state2).uniform(), npr.RandomState(10).uniform()) # check with no arg random state assert isinstance(com._random_state(), npr.RandomState) # Error for floats or strings with tm.assertRaises(ValueError): com._random_state('test') with tm.assertRaises(ValueError): com._random_state(5.5) def test_maybe_match_name(): matched = com._maybe_match_name(Series([1], name='x'), Series([2], name='x')) assert(matched == 'x') matched = com._maybe_match_name(Series([1], name='x'), Series([2], name='y')) assert(matched is None) matched = com._maybe_match_name(Series([1]), Series([2], name='x')) assert(matched is None) matched = com._maybe_match_name(Series([1], name='x'), Series([2])) assert(matched is None) matched = com._maybe_match_name(Series([1], name='x'), [2]) assert(matched == 'x') matched = com._maybe_match_name([1], Series([2], name='y')) assert(matched == 'y') class TestTake(tm.TestCase): # standard incompatible fill error fill_error = re.compile("Incompatible type for fill_value") _multiprocess_can_split_ = True def test_1d_with_out(self): def _test_dtype(dtype, can_hold_na): data = np.random.randint(0, 2, 4).astype(dtype) indexer = [2, 1, 0, 1] out = np.empty(4, dtype=dtype) com.take_1d(data, indexer, out=out) expected = data.take(indexer) tm.assert_almost_equal(out, expected) indexer = [2, 1, 0, -1] out = np.empty(4, dtype=dtype) if can_hold_na: com.take_1d(data, indexer, out=out) expected = data.take(indexer) expected[3] = np.nan tm.assert_almost_equal(out, expected) else: with tm.assertRaisesRegexp(TypeError, self.fill_error): com.take_1d(data, indexer, out=out) # no exception o/w data.take(indexer, out=out) _test_dtype(np.float64, True) _test_dtype(np.float32, True) _test_dtype(np.uint64, False) _test_dtype(np.uint32, False) _test_dtype(np.uint16, False) _test_dtype(np.uint8, False) _test_dtype(np.int64, False) _test_dtype(np.int32, False) _test_dtype(np.int16, False) _test_dtype(np.int8, False) _test_dtype(np.object_, True) _test_dtype(np.bool, False) def test_1d_fill_nonna(self): def _test_dtype(dtype, fill_value, out_dtype): data = np.random.randint(0, 2, 4).astype(dtype) indexer = [2, 1, 0, -1] result = com.take_1d(data, indexer, fill_value=fill_value) assert((result[[0, 1, 2]] == data[[2, 1, 0]]).all()) assert(result[3] == fill_value) assert(result.dtype == out_dtype) indexer = [2, 1, 0, 1] result = com.take_1d(data, indexer, fill_value=fill_value) assert((result[[0, 1, 2, 3]] == data[indexer]).all()) assert(result.dtype == dtype) _test_dtype(np.int8, np.int16(127), np.int8) _test_dtype(np.int8, np.int16(128), np.int16) _test_dtype(np.int32, 1, np.int32) _test_dtype(np.int32, 2.0, np.float64) _test_dtype(np.int32, 3.0 + 4.0j, np.complex128) _test_dtype(np.int32, True, np.object_) _test_dtype(np.int32, '', np.object_) _test_dtype(np.float64, 1, np.float64) _test_dtype(np.float64, 2.0, np.float64) _test_dtype(np.float64, 3.0 + 4.0j, np.complex128) _test_dtype(np.float64, True, np.object_) _test_dtype(np.float64, '', np.object_) _test_dtype(np.complex128, 1, np.complex128) _test_dtype(np.complex128, 2.0, np.complex128) _test_dtype(np.complex128, 3.0 + 4.0j, np.complex128) _test_dtype(np.complex128, True, np.object_) _test_dtype(np.complex128, '', np.object_) _test_dtype(np.bool_, 1, np.object_) _test_dtype(np.bool_, 2.0, np.object_) _test_dtype(np.bool_, 3.0 + 4.0j, np.object_) _test_dtype(np.bool_, True, np.bool_) _test_dtype(np.bool_, '', np.object_) def test_2d_with_out(self): def _test_dtype(dtype, can_hold_na, writeable=True): data = np.random.randint(0, 2, (5, 3)).astype(dtype) data.flags.writeable = writeable indexer = [2, 1, 0, 1] out0 = np.empty((4, 3), dtype=dtype) out1 = np.empty((5, 4), dtype=dtype) com.take_nd(data, indexer, out=out0, axis=0) com.take_nd(data, indexer, out=out1, axis=1) expected0 = data.take(indexer, axis=0) expected1 = data.take(indexer, axis=1) tm.assert_almost_equal(out0, expected0) tm.assert_almost_equal(out1, expected1) indexer = [2, 1, 0, -1] out0 = np.empty((4, 3), dtype=dtype) out1 = np.empty((5, 4), dtype=dtype) if can_hold_na: com.take_nd(data, indexer, out=out0, axis=0) com.take_nd(data, indexer, out=out1, axis=1) expected0 = data.take(indexer, axis=0) expected1 = data.take(indexer, axis=1) expected0[3, :] = np.nan expected1[:, 3] = np.nan tm.assert_almost_equal(out0, expected0) tm.assert_almost_equal(out1, expected1) else: for i, out in enumerate([out0, out1]): with tm.assertRaisesRegexp(TypeError, self.fill_error): com.take_nd(data, indexer, out=out, axis=i) # no exception o/w data.take(indexer, out=out, axis=i) for writeable in [True, False]: # Check that take_nd works both with writeable arrays (in which # case fast typed memoryviews implementation) and read-only # arrays alike. _test_dtype(np.float64, True, writeable=writeable) _test_dtype(np.float32, True, writeable=writeable) _test_dtype(np.uint64, False, writeable=writeable) _test_dtype(np.uint32, False, writeable=writeable) _test_dtype(np.uint16, False, writeable=writeable) _test_dtype(np.uint8, False, writeable=writeable) _test_dtype(np.int64, False, writeable=writeable) _test_dtype(np.int32, False, writeable=writeable) _test_dtype(np.int16, False, writeable=writeable) _test_dtype(np.int8, False, writeable=writeable) _test_dtype(np.object_, True, writeable=writeable) _test_dtype(np.bool, False, writeable=writeable) def test_2d_fill_nonna(self): def _test_dtype(dtype, fill_value, out_dtype): data = np.random.randint(0, 2, (5, 3)).astype(dtype) indexer = [2, 1, 0, -1] result = com.take_nd(data, indexer, axis=0, fill_value=fill_value) assert((result[[0, 1, 2], :] == data[[2, 1, 0], :]).all()) assert((result[3, :] == fill_value).all()) assert(result.dtype == out_dtype) result = com.take_nd(data, indexer, axis=1, fill_value=fill_value) assert((result[:, [0, 1, 2]] == data[:, [2, 1, 0]]).all()) assert((result[:, 3] == fill_value).all()) assert(result.dtype == out_dtype) indexer = [2, 1, 0, 1] result = com.take_nd(data, indexer, axis=0, fill_value=fill_value) assert((result[[0, 1, 2, 3], :] == data[indexer, :]).all()) assert(result.dtype == dtype) result = com.take_nd(data, indexer, axis=1, fill_value=fill_value) assert((result[:, [0, 1, 2, 3]] == data[:, indexer]).all()) assert(result.dtype == dtype) _test_dtype(np.int8, np.int16(127), np.int8) _test_dtype(np.int8, np.int16(128), np.int16) _test_dtype(np.int32, 1, np.int32) _test_dtype(np.int32, 2.0, np.float64) _test_dtype(np.int32, 3.0 + 4.0j, np.complex128) _test_dtype(np.int32, True, np.object_) _test_dtype(np.int32, '', np.object_) _test_dtype(np.float64, 1, np.float64) _test_dtype(np.float64, 2.0, np.float64) _test_dtype(np.float64, 3.0 + 4.0j, np.complex128) _test_dtype(np.float64, True, np.object_) _test_dtype(np.float64, '', np.object_) _test_dtype(np.complex128, 1, np.complex128) _test_dtype(np.complex128, 2.0, np.complex128) _test_dtype(np.complex128, 3.0 + 4.0j, np.complex128) _test_dtype(np.complex128, True, np.object_) _test_dtype(np.complex128, '', np.object_) _test_dtype(np.bool_, 1, np.object_) _test_dtype(np.bool_, 2.0, np.object_) _test_dtype(np.bool_, 3.0 + 4.0j, np.object_) _test_dtype(np.bool_, True, np.bool_) _test_dtype(np.bool_, '', np.object_) def test_3d_with_out(self): def _test_dtype(dtype, can_hold_na): data = np.random.randint(0, 2, (5, 4, 3)).astype(dtype) indexer = [2, 1, 0, 1] out0 = np.empty((4, 4, 3), dtype=dtype) out1 = np.empty((5, 4, 3), dtype=dtype) out2 = np.empty((5, 4, 4), dtype=dtype) com.take_nd(data, indexer, out=out0, axis=0) com.take_nd(data, indexer, out=out1, axis=1) com.take_nd(data, indexer, out=out2, axis=2) expected0 = data.take(indexer, axis=0) expected1 = data.take(indexer, axis=1) expected2 = data.take(indexer, axis=2) tm.assert_almost_equal(out0, expected0) tm.assert_almost_equal(out1, expected1) tm.assert_almost_equal(out2, expected2) indexer = [2, 1, 0, -1] out0 = np.empty((4, 4, 3), dtype=dtype) out1 = np.empty((5, 4, 3), dtype=dtype) out2 = np.empty((5, 4, 4), dtype=dtype) if can_hold_na: com.take_nd(data, indexer, out=out0, axis=0) com.take_nd(data, indexer, out=out1, axis=1) com.take_nd(data, indexer, out=out2, axis=2) expected0 = data.take(indexer, axis=0) expected1 = data.take(indexer, axis=1) expected2 = data.take(indexer, axis=2) expected0[3, :, :] = np.nan expected1[:, 3, :] = np.nan expected2[:, :, 3] = np.nan tm.assert_almost_equal(out0, expected0) tm.assert_almost_equal(out1, expected1) tm.assert_almost_equal(out2, expected2) else: for i, out in enumerate([out0, out1, out2]): with tm.assertRaisesRegexp(TypeError, self.fill_error): com.take_nd(data, indexer, out=out, axis=i) # no exception o/w data.take(indexer, out=out, axis=i) _test_dtype(np.float64, True) _test_dtype(np.float32, True) _test_dtype(np.uint64, False) _test_dtype(np.uint32, False) _test_dtype(np.uint16, False) _test_dtype(np.uint8, False) _test_dtype(np.int64, False) _test_dtype(np.int32, False) _test_dtype(np.int16, False) _test_dtype(np.int8, False) _test_dtype(np.object_, True) _test_dtype(np.bool, False) def test_3d_fill_nonna(self): def _test_dtype(dtype, fill_value, out_dtype): data = np.random.randint(0, 2, (5, 4, 3)).astype(dtype) indexer = [2, 1, 0, -1] result = com.take_nd(data, indexer, axis=0, fill_value=fill_value) assert((result[[0, 1, 2], :, :] == data[[2, 1, 0], :, :]).all()) assert((result[3, :, :] == fill_value).all()) assert(result.dtype == out_dtype) result = com.take_nd(data, indexer, axis=1, fill_value=fill_value) assert((result[:, [0, 1, 2], :] == data[:, [2, 1, 0], :]).all()) assert((result[:, 3, :] == fill_value).all()) assert(result.dtype == out_dtype) result = com.take_nd(data, indexer, axis=2, fill_value=fill_value) assert((result[:, :, [0, 1, 2]] == data[:, :, [2, 1, 0]]).all()) assert((result[:, :, 3] == fill_value).all()) assert(result.dtype == out_dtype) indexer = [2, 1, 0, 1] result = com.take_nd(data, indexer, axis=0, fill_value=fill_value) assert((result[[0, 1, 2, 3], :, :] == data[indexer, :, :]).all()) assert(result.dtype == dtype) result = com.take_nd(data, indexer, axis=1, fill_value=fill_value) assert((result[:, [0, 1, 2, 3], :] == data[:, indexer, :]).all()) assert(result.dtype == dtype) result = com.take_nd(data, indexer, axis=2, fill_value=fill_value) assert((result[:, :, [0, 1, 2, 3]] == data[:, :, indexer]).all()) assert(result.dtype == dtype) _test_dtype(np.int8, np.int16(127), np.int8) _test_dtype(np.int8, np.int16(128), np.int16) _test_dtype(np.int32, 1, np.int32) _test_dtype(np.int32, 2.0, np.float64) _test_dtype(np.int32, 3.0 + 4.0j, np.complex128) _test_dtype(np.int32, True, np.object_) _test_dtype(np.int32, '', np.object_) _test_dtype(np.float64, 1, np.float64) _test_dtype(np.float64, 2.0, np.float64) _test_dtype(np.float64, 3.0 + 4.0j, np.complex128) _test_dtype(np.float64, True, np.object_) _test_dtype(np.float64, '', np.object_) _test_dtype(np.complex128, 1, np.complex128) _test_dtype(np.complex128, 2.0, np.complex128) _test_dtype(np.complex128, 3.0 + 4.0j, np.complex128) _test_dtype(np.complex128, True, np.object_) _test_dtype(np.complex128, '', np.object_) _test_dtype(np.bool_, 1, np.object_) _test_dtype(np.bool_, 2.0, np.object_) _test_dtype(np.bool_, 3.0 + 4.0j, np.object_) _test_dtype(np.bool_, True, np.bool_) _test_dtype(np.bool_, '', np.object_) def test_1d_other_dtypes(self): arr = np.random.randn(10).astype(np.float32) indexer = [1, 2, 3, -1] result = com.take_1d(arr, indexer) expected = arr.take(indexer) expected[-1] = np.nan tm.assert_almost_equal(result, expected) def test_2d_other_dtypes(self): arr = np.random.randn(10, 5).astype(np.float32) indexer = [1, 2, 3, -1] # axis=0 result = com.take_nd(arr, indexer, axis=0) expected = arr.take(indexer, axis=0) expected[-1] = np.nan tm.assert_almost_equal(result, expected) # axis=1 result = com.take_nd(arr, indexer, axis=1) expected = arr.take(indexer, axis=1) expected[:, -1] = np.nan tm.assert_almost_equal(result, expected) def test_1d_bool(self): arr = np.array([0, 1, 0], dtype=bool) result = com.take_1d(arr, [0, 2, 2, 1]) expected = arr.take([0, 2, 2, 1]) self.assert_numpy_array_equal(result, expected) result = com.take_1d(arr, [0, 2, -1]) self.assertEqual(result.dtype, np.object_) def test_2d_bool(self): arr = np.array([[0, 1, 0], [1, 0, 1], [0, 1, 1]], dtype=bool) result = com.take_nd(arr, [0, 2, 2, 1]) expected = arr.take([0, 2, 2, 1], axis=0) self.assert_numpy_array_equal(result, expected) result = com.take_nd(arr, [0, 2, 2, 1], axis=1) expected = arr.take([0, 2, 2, 1], axis=1) self.assert_numpy_array_equal(result, expected) result = com.take_nd(arr, [0, 2, -1]) self.assertEqual(result.dtype, np.object_) def test_2d_float32(self): arr = np.random.randn(4, 3).astype(np.float32) indexer = [0, 2, -1, 1, -1] # axis=0 result = com.take_nd(arr, indexer, axis=0) result2 = np.empty_like(result) com.take_nd(arr, indexer, axis=0, out=result2) tm.assert_almost_equal(result, result2) expected = arr.take(indexer, axis=0) expected[[2, 4], :] = np.nan tm.assert_almost_equal(result, expected) #### this now accepts a float32! # test with float64 out buffer out = np.empty((len(indexer), arr.shape[1]), dtype='float32') com.take_nd(arr, indexer, out=out) # it works! # axis=1 result = com.take_nd(arr, indexer, axis=1) result2 = np.empty_like(result) com.take_nd(arr, indexer, axis=1, out=result2) tm.assert_almost_equal(result, result2) expected = arr.take(indexer, axis=1) expected[:, [2, 4]] = np.nan tm.assert_almost_equal(result, expected) def test_2d_datetime64(self): # 2005/01/01 - 2006/01/01 arr = np.random.randint(long(11045376), long(11360736), (5, 3))*100000000000 arr = arr.view(dtype='datetime64[ns]') indexer = [0, 2, -1, 1, -1] # axis=0 result = com.take_nd(arr, indexer, axis=0) result2 = np.empty_like(result) com.take_nd(arr, indexer, axis=0, out=result2) tm.assert_almost_equal(result, result2) expected = arr.take(indexer, axis=0) expected.view(np.int64)[[2, 4], :] = iNaT tm.assert_almost_equal(result, expected) result = com.take_nd(arr, indexer, axis=0, fill_value=datetime(2007, 1, 1)) result2 = np.empty_like(result) com.take_nd(arr, indexer, out=result2, axis=0, fill_value=datetime(2007, 1, 1)) tm.assert_almost_equal(result, result2) expected = arr.take(indexer, axis=0) expected[[2, 4], :] = datetime(2007, 1, 1) tm.assert_almost_equal(result, expected) # axis=1 result = com.take_nd(arr, indexer, axis=1) result2 = np.empty_like(result) com.take_nd(arr, indexer, axis=1, out=result2) tm.assert_almost_equal(result, result2) expected = arr.take(indexer, axis=1) expected.view(np.int64)[:, [2, 4]] = iNaT tm.assert_almost_equal(result, expected) result = com.take_nd(arr, indexer, axis=1, fill_value=datetime(2007, 1, 1)) result2 = np.empty_like(result) com.take_nd(arr, indexer, out=result2, axis=1, fill_value=datetime(2007, 1, 1)) tm.assert_almost_equal(result, result2) expected = arr.take(indexer, axis=1) expected[:, [2, 4]] = datetime(2007, 1, 1) tm.assert_almost_equal(result, expected) class TestMaybe(tm.TestCase): def test_maybe_convert_string_to_array(self): result = com._maybe_convert_string_to_object('x') tm.assert_numpy_array_equal(result, np.array(['x'], dtype=object)) self.assertTrue(result.dtype == object) result = com._maybe_convert_string_to_object(1) self.assertEqual(result, 1) arr = np.array(['x', 'y'], dtype=str) result = com._maybe_convert_string_to_object(arr) tm.assert_numpy_array_equal(result, np.array(['x', 'y'], dtype=object)) self.assertTrue(result.dtype == object) # unicode arr = np.array(['x', 'y']).astype('U') result = com._maybe_convert_string_to_object(arr) tm.assert_numpy_array_equal(result, np.array(['x', 'y'], dtype=object)) self.assertTrue(result.dtype == object) # object arr = np.array(['x', 2], dtype=object) result = com._maybe_convert_string_to_object(arr) tm.assert_numpy_array_equal(result, np.array(['x', 2], dtype=object)) self.assertTrue(result.dtype == object) def test_possibly_convert_objects_copy(): values = np.array([1, 2]) out = convert._possibly_convert_objects(values, copy=False) assert_true(values is out) out = convert._possibly_convert_objects(values, copy=True) assert_true(values is not out) values = np.array(['apply','banana']) out = convert._possibly_convert_objects(values, copy=False) assert_true(values is out) out = convert._possibly_convert_objects(values, copy=True) assert_true(values is not out) def test_dict_compat(): data_datetime64 = {np.datetime64('1990-03-15'): 1, np.datetime64('2015-03-15'): 2} data_unchanged = {1: 2, 3: 4, 5: 6} expected = {Timestamp('1990-3-15'): 1, Timestamp('2015-03-15'): 2} assert(com._dict_compat(data_datetime64) == expected) assert(com._dict_compat(expected) == expected) assert(com._dict_compat(data_unchanged) == data_unchanged) if __name__ == '__main__': nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'], exit=False)
gpl-2.0
par2/lamana
lamana/models/fixtures/fixture_model_class.py
1
10351
#------------------------------------------------------------------------------ '''Class-style Model This fixture is used to test the importing of models, handled by the `theories.handshake()` module. As of 0.4.11, models can: - be located in the `lamana.models` folder - module and classes can have any pythonic name; hard-coding removed - any sub-package can be accessed by the "model" keyword in `Case.apply()` - search for the hook-containing class and it's hook method This module is here to test these aspects as the module is imported. The Wilson_LT model was adapted. No functions are expected in this module; there are tests against this. ''' import math import collections as ct import pandas as pd from lamana.input_ import BaseDefaults from lamana.theories import BaseModel from lamana.lt_exceptions import IndeterminateError # This class lacks a hook method; theories should skip it. class DummyModel(): pass # The class containing the hook method can have any name. class RandomName(BaseModel): '''A modified laminate theory for circular biaxial flexure disks, loaded with a flat piston punch on 3-ball support having two distinct materials (polymer and ceramic).''' '''Accept extra args and kwds here''' def __init__(self): self.Laminate = None self.FeatureInput = None self.LaminateModel = None # TODO: eventually abstract into BaseModel and deprecate direct coding # TODO: accept kwargs from Case -> handshake def _use_model_(self, Laminate, adjusted_z=False): '''Return updated DataFrame and FeatureInput Return None if exceptions raised. Parameters ---------- df : DataFrame LaminateModel with IDs and Dimensional Variables. FeatureInut : dict Geometry, laminate parameters and more. Updates Globals dict for parameters in the dashboard output. adjusted_z: bool; default=False If True, uses z(m)* values instead; different assumption for internal calc. Raises ------ ZeroDivisionError If zero `r` or `a` in the log term are zero. ValueError If negative numbers are in the log term or the support radius exceeds the sample radius. Returns ------- tuple The updated calculations and parameters stored in a tuple `(LaminateModel, FeatureInput)``. ''' self.Laminate = Laminate df = Laminate.LFrame.copy() FeatureInput = Laminate.FeatureInput # Author-defined Exception Handling if (FeatureInput['Parameters']['r'] == 0): raise ZeroDivisionError('r=0 is invalid for the log term in the moment eqn.') elif (FeatureInput['Parameters']['a'] == 0): raise ZeroDivisionError('a=0 is invalid for the log term in the moment eqn.') elif (FeatureInput['Parameters']['r'] < 0) | (FeatureInput['Parameters']['a'] < 0): raise ValueError('Negative numbers are invalid for the log term ' 'in moment eqn.') elif FeatureInput['Parameters']['a'] > FeatureInput['Parameters']['R']: raise ValueError('Support radius is larger than sample radius.') elif df['side'].str.contains('INDET').any(): print('INDET value found. Rolling back...') raise IndeterminateError('INDET value found. Unable to accurately calculate stress.') #raise AssertionError('Indeterminate value found. Unable to accurately calculate stress.') # Calling functions to calculate Qs and Ds df.loc[:, 'Q_11'] = self.calc_stiffness(df, FeatureInput['Properties']).q_11 df.loc[:, 'Q_12'] = self.calc_stiffness(df, FeatureInput['Properties']).q_12 df.loc[:, 'D_11'] = self.calc_bending(df, adj_z=adjusted_z).d_11 df.loc[:, 'D_12'] = self.calc_bending(df, adj_z=adjusted_z).d_12 # Global Variable Update if (FeatureInput['Parameters']['p'] == 1) & (Laminate.nplies%2 == 0): D_11T = sum(df['D_11']) D_12T = sum(df['D_12']) else: D_11T = sum(df.loc[df['label'] == 'interface', 'D_11']) # total D11 D_12T = sum(df.loc[df['label'] == 'interface', 'D_12']) #print(FeatureInput['Geometric']['p']) D_11p = (1./((D_11T**2 - D_12T**2)) * D_11T) # D_12n = -(1./((D_11T**2 - D_12T**2)) *D_12T) # v_eq = D_12T/D_11T # equiv. Poisson's ratio M_r = self.calc_moment(df, FeatureInput['Parameters'], v_eq).m_r M_t = self.calc_moment(df, FeatureInput['Parameters'], v_eq).m_t K_r = (D_11p*M_r) + (D_12n*M_t) # curvatures K_t = (D_12n*M_r) + (D_11p*M_t) # Update FeatureInput global_params = { 'D_11T': D_11T, 'D_12T': D_12T, 'D_11p': D_11p, 'D_12n': D_12n, 'v_eq ': v_eq, 'M_r': M_r, 'M_t': M_t, 'K_r': K_r, 'K_t:': K_t, } FeatureInput['Globals'] = global_params self.FeatureInput = FeatureInput # update with Globals #print(FeatureInput) # Calculate Strains and Stresses and Update DataFrame df.loc[:,'strain_r'] = K_r * df.loc[:, 'Z(m)'] df.loc[:,'strain_t'] = K_t * df.loc[:, 'Z(m)'] df.loc[:, 'stress_r (Pa/N)'] = (df.loc[:, 'strain_r'] * df.loc[:, 'Q_11'] ) + (df.loc[:, 'strain_t'] * df.loc[:, 'Q_12']) df.loc[:,'stress_t (Pa/N)'] = (df.loc[:, 'strain_t'] * df.loc[:, 'Q_11'] ) + (df.loc[:, 'strain_r'] * df.loc[:, 'Q_12']) df.loc[:,'stress_f (MPa/N)'] = df.loc[:, 'stress_t (Pa/N)']/1e6 del df['Modulus'] del df['Poissons'] self.LaminateModel = df return (df, FeatureInput) #------------------------------------------------------------------------------ '''Prefer staticmethods here. Add formulas to doc strings.''' def calc_stiffness(self, df, mat_props): '''Return tuple of Series of (Q11, Q12) floats per lamina.''' # Iterate to Apply Modulus and Poisson's to correct Material # TODO: Prefer cleaner ways to parse materials from mat_props df_mat_props = pd.DataFrame(mat_props) # df easier to munge df_mat_props.index.name = 'materials' ##for material in mat_props.index: for material in df_mat_props.index: mat_idx = df['matl'] == material df.loc[mat_idx, 'Modulus'] = df_mat_props.loc[material, 'Modulus'] df.loc[mat_idx, 'Poissons'] = df_mat_props.loc[material, 'Poissons'] E = df['Modulus'] # series of moduli v = df['Poissons'] stiffness = ct.namedtuple('stiffness', ['q_11', 'q_12']) q_11 = E / (1 - (v**2)) q_12 = (v*E) / (1 - (v**2)) return stiffness(q_11, q_12) def calc_bending(self, df, adj_z=False): '''Return tuple of Series of (D11, D12) floats.''' q_11 = df['Q_11'] q_12 = df['Q_12'] h = df['h(m)'] # TODO: need to fix kwargs passing first; tabled since affects many modules. if not adj_z: z = df['z(m)'] else: z = df['z(m)*'] bending = ct.namedtuple('bending', ['d_11', 'd_12']) d_11 = ((q_11*(h**3)) / 12.) + (q_11*h*(z**2)) d_12 = ((q_12*(h**3)) / 12.) + (q_12*h*(z**2)) return bending(d_11, d_12) def calc_moment(self, df, load_params, v_eq): '''Return tuple of moments (radial and tangential); floats. See Timishenko-Woinowsky: Eq. 91; default''' P_a = load_params['P_a'] a = load_params['a'] r = load_params['r'] moments = ct.namedtuple('moments', ['m_r', 'm_t']) m_r = ((P_a/(4*math.pi)) * ((1 + v_eq)*math.log10(a/r))) m_t = ((P_a/(4*math.pi)) * (((1 + v_eq)*math.log10(a/r)) + (1 - v_eq))) return moments(m_r, m_t) class Defaults(BaseDefaults): '''Return parameters for building distributions cases. Useful for consistent testing. Dimensional defaults are inherited from utils.BaseDefaults(). Material-specific parameters are defined here by he user. - Default geometric parameters - Default material properties - Default FeatureInput Examples -------- >>> dft = Defaults() >>> dft.load_params {'R' : 12e-3, 'a' : 7.5e-3, 'p' : 1, 'P_a' : 1, 'r' : 2e-4,} >>> dft.mat_props {'Modulus': {'HA': 5.2e10, 'PSu': 2.7e9}, 'Poissons': {'HA': 0.25, 'PSu': 0.33}} >>> dft.FeatureInput {'Geometry' : '400-[200]-800', 'Geometric' : {'R' : 12e-3, 'a' : 7.5e-3, 'p' : 1, 'P_a' : 1, 'r' : 2e-4,}, 'Materials' : {'HA' : [5.2e10, 0.25], 'PSu' : [2.7e9, 0.33],}, 'Custom' : None, 'Model' : Wilson_LT} Returns ------- class Updated attributes inherited from the `BaseDefaults` class. ''' def __init__(self): BaseDefaults.__init__(self) '''DEV: Add defaults first. Then adjust attributes.''' # DEFAULTS ------------------------------------------------------------ # Build dicts of geometric and material parameters self.load_params = { 'R': 12e-3, # specimen radius 'a': 7.5e-3, # support ring radius 'p': 5, # points/layer 'P_a': 1, # applied load 'r': 2e-4, # radial distance from center loading } self.mat_props = { 'Modulus': {'HA': 5.2e10, 'PSu': 2.7e9}, 'Poissons': {'HA': 0.25, 'PSu': 0.33} } # ATTRIBUTES ---------------------------------------------------------- # FeatureInput self.FeatureInput = self.get_FeatureInput( self.Geo_objects['standard'][0], load_params=self.load_params, mat_props=self.mat_props, ##custom_matls=None, model='Wilson_LT', global_vars=None )
bsd-3-clause
mxlei01/healthcareai-py
healthcareai/common/model_eval.py
4
13696
"""Model evaluation tools.""" import os import sklearn import itertools import numpy as np import pandas as pd import sklearn.metrics as skmetrics from matplotlib import pyplot as plt from healthcareai.common.healthcareai_error import HealthcareAIError DIAGONAL_LINE_COLOR = '#bbbbbb' DIAGONAL_LINE_STYLE = 'dotted' def compute_roc(y_test, probability_predictions): """ Compute TPRs, FPRs, best cutoff, ROC auc, and raw thresholds. Args: y_test (list) : true label values corresponding to the predictions. Also length n. probability_predictions (list) : predictions coming from an ML algorithm of length n. Returns: dict: """ _validate_predictions_and_labels_are_equal_length(probability_predictions, y_test) # Calculate ROC false_positive_rates, true_positive_rates, roc_thresholds = skmetrics.roc_curve(y_test, probability_predictions) roc_auc = skmetrics.roc_auc_score(y_test, probability_predictions) # get ROC ideal cutoffs (upper left, or 0,1) roc_distances = (false_positive_rates - 0) ** 2 + (true_positive_rates - 1) ** 2 # To prevent the case where there are two points with the same minimum distance, return only the first # np.where returns a tuple (we want the first element in the first array) roc_index = np.where(roc_distances == np.min(roc_distances))[0][0] best_tpr = true_positive_rates[roc_index] best_fpr = false_positive_rates[roc_index] ideal_roc_cutoff = roc_thresholds[roc_index] return {'roc_auc': roc_auc, 'best_roc_cutoff': ideal_roc_cutoff, 'best_true_positive_rate': best_tpr, 'best_false_positive_rate': best_fpr, 'true_positive_rates': true_positive_rates, 'false_positive_rates': false_positive_rates, 'roc_thresholds': roc_thresholds} def compute_pr(y_test, probability_predictions): """ Compute Precision-Recall, thresholds and PR AUC. Args: y_test (list) : true label values corresponding to the predictions. Also length n. probability_predictions (list) : predictions coming from an ML algorithm of length n. Returns: dict: """ _validate_predictions_and_labels_are_equal_length(probability_predictions, y_test) # Calculate PR precisions, recalls, pr_thresholds = skmetrics.precision_recall_curve(y_test, probability_predictions) pr_auc = skmetrics.average_precision_score(y_test, probability_predictions) # get ideal cutoffs for suggestions (upper right or 1,1) pr_distances = (precisions - 1) ** 2 + (recalls - 1) ** 2 # To prevent the case where there are two points with the same minimum distance, return only the first # np.where returns a tuple (we want the first element in the first array) pr_index = np.where(pr_distances == np.min(pr_distances))[0][0] best_precision = precisions[pr_index] best_recall = recalls[pr_index] ideal_pr_cutoff = pr_thresholds[pr_index] return {'pr_auc': pr_auc, 'best_pr_cutoff': ideal_pr_cutoff, 'best_precision': best_precision, 'best_recall': best_recall, 'precisions': precisions, 'recalls': recalls, 'pr_thresholds': pr_thresholds} def calculate_regression_metrics(trained_sklearn_estimator, x_test, y_test): """ Given a trained estimator, calculate metrics. Args: trained_sklearn_estimator (sklearn.base.BaseEstimator): a scikit-learn estimator that has been `.fit()` y_test (numpy.ndarray): A 1d numpy array of the y_test set (predictions) x_test (numpy.ndarray): A 2d numpy array of the x_test set (features) Returns: dict: A dictionary of metrics objects """ # Get predictions predictions = trained_sklearn_estimator.predict(x_test) # Calculate individual metrics mean_squared_error = skmetrics.mean_squared_error(y_test, predictions) mean_absolute_error = skmetrics.mean_absolute_error(y_test, predictions) result = {'mean_squared_error': mean_squared_error, 'mean_absolute_error': mean_absolute_error} return result def calculate_binary_classification_metrics(trained_sklearn_estimator, x_test, y_test): """ Given a trained estimator, calculate metrics. Args: trained_sklearn_estimator (sklearn.base.BaseEstimator): a scikit-learn estimator that has been `.fit()` x_test (numpy.ndarray): A 2d numpy array of the x_test set (features) y_test (numpy.ndarray): A 1d numpy array of the y_test set (predictions) Returns: dict: A dictionary of metrics objects """ # Squeeze down y_test to 1D y_test = np.squeeze(y_test) _validate_predictions_and_labels_are_equal_length(x_test, y_test) # Get binary and probability classification predictions binary_predictions = np.squeeze(trained_sklearn_estimator.predict(x_test)) probability_predictions = np.squeeze(trained_sklearn_estimator.predict_proba(x_test)[:, 1]) # Calculate accuracy accuracy = skmetrics.accuracy_score(y_test, binary_predictions) roc = compute_roc(y_test, probability_predictions) pr = compute_pr(y_test, probability_predictions) # Unpack the roc and pr dictionaries so the metric lookup is easier for plot and ensemble methods return {'accuracy': accuracy, **roc, **pr} def roc_plot_from_thresholds(roc_thresholds_by_model, save=False, debug=False): """ From a given dictionary of thresholds by model, create a ROC curve for each model. Args: roc_thresholds_by_model (dict): A dictionary of ROC thresholds by model name. save (bool): False to display the image (default) or True to save it (but not display it) debug (bool): verbost output. """ # TODO consolidate this and PR plotter into 1 function # TODO make the colors randomly generated from rgb values # Cycle through the colors list color_iterator = itertools.cycle(['b', 'g', 'r', 'c', 'm', 'y', 'k']) # Initialize plot plt.figure() plt.xlabel('False Positive Rate (FPR)') plt.ylabel('True Positive Rate (TRP)') plt.title('Receiver Operating Characteristic (ROC)') plt.xlim([0.0, 1.0]) plt.ylim([0.0, 1.05]) plt.plot([0, 1], [0, 1], linestyle=DIAGONAL_LINE_STYLE, color=DIAGONAL_LINE_COLOR) # Calculate and plot for each model for color, (model_name, metrics) in zip(color_iterator, roc_thresholds_by_model.items()): # Extract model name and metrics from dictionary roc_auc = metrics['roc_auc'] tpr = metrics['true_positive_rates'] fpr = metrics['false_positive_rates'] best_true_positive_rate = metrics['best_true_positive_rate'] best_false_positive_rate = metrics['best_false_positive_rate'] if debug: print('{} model:'.format(model_name)) print(pd.DataFrame({'FPR': fpr, 'TPR': tpr})) # plot the line label = '{} (ROC AUC = {})'.format(model_name, round(roc_auc, 2)) plt.plot(fpr, tpr, color=color, label=label) plt.plot([best_false_positive_rate], [best_true_positive_rate], marker='*', markersize=10, color=color) plt.legend(loc="lower right") if save: plt.savefig('ROC.png') source_path = os.path.dirname(os.path.abspath(__file__)) print('\nROC plot saved in: {}'.format(source_path)) plt.show() def pr_plot_from_thresholds(pr_thresholds_by_model, save=False, debug=False): """ From a given dictionary of thresholds by model, create a PR curve for each model. Args: pr_thresholds_by_model (dict): A dictionary of PR thresholds by model name. save (bool): False to display the image (default) or True to save it (but not display it) debug (bool): verbost output. """ # TODO consolidate this and PR plotter into 1 function # TODO make the colors randomly generated from rgb values # Cycle through the colors list color_iterator = itertools.cycle(['b', 'g', 'r', 'c', 'm', 'y', 'k']) # Initialize plot plt.figure() plt.xlabel('Recall') plt.ylabel('Precision') plt.title('Precision Recall (PR)') plt.xlim([0.0, 1.0]) plt.ylim([0.0, 1.05]) plt.plot([0, 1], [1, 0], linestyle=DIAGONAL_LINE_STYLE, color=DIAGONAL_LINE_COLOR) # Calculate and plot for each model for color, (model_name, metrics) in zip(color_iterator, pr_thresholds_by_model.items()): # Extract model name and metrics from dictionary pr_auc = metrics['pr_auc'] precision = metrics['precisions'] recall = metrics['recalls'] best_recall = metrics['best_recall'] best_precision = metrics['best_precision'] if debug: print('{} model:'.format(model_name)) print(pd.DataFrame({'Recall': recall, 'Precision': precision})) # plot the line label = '{} (PR AUC = {})'.format(model_name, round(pr_auc, 2)) plt.plot(recall, precision, color=color, label=label) plt.plot([best_recall], [best_precision], marker='*', markersize=10, color=color) plt.legend(loc="lower left") if save: plt.savefig('PR.png') source_path = os.path.dirname(os.path.abspath(__file__)) print('\nPR plot saved in: {}'.format(source_path)) plt.show() def plot_random_forest_feature_importance(trained_random_forest, x_train, feature_names, feature_limit=15, save=False): """ Given a random forest estimator, an x_train array, the feature names save or display a feature importance plot. Args: trained_random_forest (sklearn.ensemble.RandomForestClassifier or sklearn.ensemble.RandomForestRegressor): x_train (numpy.array): A 2D numpy array that was used for training feature_names (list): Column names in the x_train set feature_limit (int): Number of features to display on graph save (bool): True to save the plot, false to display it in a blocking thread """ _validate_random_forest_estimator(trained_random_forest) # Sort the feature names and relative importances # TODO this portion could probably be extracted and tested, since the plot is difficult to test aggregate_features_importances = trained_random_forest.feature_importances_ indices = np.argsort(aggregate_features_importances)[::-1] sorted_feature_names = [feature_names[i] for i in indices] # limit the plot to the top n features so it stays legible on models with lots of features subset_indices = indices[0:feature_limit] number_of_features = x_train.shape[1] # build a range using the lesser value max_features = min(number_of_features, feature_limit) x_axis_limit = range(max_features) # Get the standard deviations for error bars standard_deviations = _standard_deviations_of_importances(trained_random_forest) # Turn off matplotlib interactive mode plt.ioff() # Set up the plot and axes figure = plt.figure() plt.title('Top {} (of {}) Important Features'.format(max_features, number_of_features)) plt.ylabel('Relative Importance') # Plot each feature plt.bar( # this should go as far as the model or limit whichever is less x_axis_limit, aggregate_features_importances[subset_indices], color="g", yerr=standard_deviations[subset_indices], align="center") plt.xticks(x_axis_limit, sorted_feature_names, rotation=90) # x axis scales by default # set y axis min to zero plt.ylim(ymin=0) # plt.tight_layout() # Do not use tight_layout until https://github.com/matplotlib/matplotlib/issues/5456 is fixed # Because long feature names cause this error # Save or display the plot if save: plt.savefig('FeatureImportances.png') source_path = os.path.dirname(os.path.abspath(__file__)) print('\nFeature importance plot saved in: {}'.format(source_path)) # Close the figure so it does not get displayed plt.close(figure) else: plt.show() def _validate_random_forest_estimator(trained_random_forest): """ Validate that an input is a random forest estimator and raise an error if it is not. Args: trained_random_forest: any input """ is_rf_classifier = isinstance(trained_random_forest, sklearn.ensemble.RandomForestClassifier) is_rf_regressor = isinstance(trained_random_forest, sklearn.ensemble.RandomForestRegressor) if not (is_rf_classifier or is_rf_regressor): raise HealthcareAIError('Feature plotting only works with a scikit learn Random Forest estimator.') def _standard_deviations_of_importances(trained_random_forest): """ Given a scikit-learn trained random forest estimator, return the standard deviations of all feature importances. Args: trained_random_forest (sklearn.ensemble.RandomForestClassifier or sklearn.ensemble.RandomForestRegressor): the trained estimator Returns: list: A numeric list """ # Get the individual feature importances from each tree to find the standard deviation for plotting error bars individual_feature_importances = [tree.feature_importances_ for tree in trained_random_forest.estimators_] standard_deviations = np.std(individual_feature_importances, axis=0) return standard_deviations def _validate_predictions_and_labels_are_equal_length(predictions, true_values): if len(predictions) == len(true_values): return True else: raise HealthcareAIError('The number of predictions is not equal to the number of true_values.') if __name__ == '__main__': pass
mit
tmhm/scikit-learn
sklearn/mixture/tests/test_gmm.py
200
17427
import unittest import copy import sys from nose.tools import assert_true import numpy as np from numpy.testing import (assert_array_equal, assert_array_almost_equal, assert_raises) from scipy import stats from sklearn import mixture from sklearn.datasets.samples_generator import make_spd_matrix from sklearn.utils.testing import assert_greater from sklearn.utils.testing import assert_raise_message from sklearn.metrics.cluster import adjusted_rand_score from sklearn.externals.six.moves import cStringIO as StringIO rng = np.random.RandomState(0) def test_sample_gaussian(): # Test sample generation from mixture.sample_gaussian where covariance # is diagonal, spherical and full n_features, n_samples = 2, 300 axis = 1 mu = rng.randint(10) * rng.rand(n_features) cv = (rng.rand(n_features) + 1.0) ** 2 samples = mixture.sample_gaussian( mu, cv, covariance_type='diag', n_samples=n_samples) assert_true(np.allclose(samples.mean(axis), mu, atol=1.3)) assert_true(np.allclose(samples.var(axis), cv, atol=1.5)) # the same for spherical covariances cv = (rng.rand() + 1.0) ** 2 samples = mixture.sample_gaussian( mu, cv, covariance_type='spherical', n_samples=n_samples) assert_true(np.allclose(samples.mean(axis), mu, atol=1.5)) assert_true(np.allclose( samples.var(axis), np.repeat(cv, n_features), atol=1.5)) # and for full covariances A = rng.randn(n_features, n_features) cv = np.dot(A.T, A) + np.eye(n_features) samples = mixture.sample_gaussian( mu, cv, covariance_type='full', n_samples=n_samples) assert_true(np.allclose(samples.mean(axis), mu, atol=1.3)) assert_true(np.allclose(np.cov(samples), cv, atol=2.5)) # Numerical stability check: in SciPy 0.12.0 at least, eigh may return # tiny negative values in its second return value. from sklearn.mixture import sample_gaussian x = sample_gaussian([0, 0], [[4, 3], [1, .1]], covariance_type='full', random_state=42) print(x) assert_true(np.isfinite(x).all()) def _naive_lmvnpdf_diag(X, mu, cv): # slow and naive implementation of lmvnpdf ref = np.empty((len(X), len(mu))) stds = np.sqrt(cv) for i, (m, std) in enumerate(zip(mu, stds)): ref[:, i] = np.log(stats.norm.pdf(X, m, std)).sum(axis=1) return ref def test_lmvnpdf_diag(): # test a slow and naive implementation of lmvnpdf and # compare it to the vectorized version (mixture.lmvnpdf) to test # for correctness n_features, n_components, n_samples = 2, 3, 10 mu = rng.randint(10) * rng.rand(n_components, n_features) cv = (rng.rand(n_components, n_features) + 1.0) ** 2 X = rng.randint(10) * rng.rand(n_samples, n_features) ref = _naive_lmvnpdf_diag(X, mu, cv) lpr = mixture.log_multivariate_normal_density(X, mu, cv, 'diag') assert_array_almost_equal(lpr, ref) def test_lmvnpdf_spherical(): n_features, n_components, n_samples = 2, 3, 10 mu = rng.randint(10) * rng.rand(n_components, n_features) spherecv = rng.rand(n_components, 1) ** 2 + 1 X = rng.randint(10) * rng.rand(n_samples, n_features) cv = np.tile(spherecv, (n_features, 1)) reference = _naive_lmvnpdf_diag(X, mu, cv) lpr = mixture.log_multivariate_normal_density(X, mu, spherecv, 'spherical') assert_array_almost_equal(lpr, reference) def test_lmvnpdf_full(): n_features, n_components, n_samples = 2, 3, 10 mu = rng.randint(10) * rng.rand(n_components, n_features) cv = (rng.rand(n_components, n_features) + 1.0) ** 2 X = rng.randint(10) * rng.rand(n_samples, n_features) fullcv = np.array([np.diag(x) for x in cv]) reference = _naive_lmvnpdf_diag(X, mu, cv) lpr = mixture.log_multivariate_normal_density(X, mu, fullcv, 'full') assert_array_almost_equal(lpr, reference) def test_lvmpdf_full_cv_non_positive_definite(): n_features, n_samples = 2, 10 rng = np.random.RandomState(0) X = rng.randint(10) * rng.rand(n_samples, n_features) mu = np.mean(X, 0) cv = np.array([[[-1, 0], [0, 1]]]) expected_message = "'covars' must be symmetric, positive-definite" assert_raise_message(ValueError, expected_message, mixture.log_multivariate_normal_density, X, mu, cv, 'full') def test_GMM_attributes(): n_components, n_features = 10, 4 covariance_type = 'diag' g = mixture.GMM(n_components, covariance_type, random_state=rng) weights = rng.rand(n_components) weights = weights / weights.sum() means = rng.randint(-20, 20, (n_components, n_features)) assert_true(g.n_components == n_components) assert_true(g.covariance_type == covariance_type) g.weights_ = weights assert_array_almost_equal(g.weights_, weights) g.means_ = means assert_array_almost_equal(g.means_, means) covars = (0.1 + 2 * rng.rand(n_components, n_features)) ** 2 g.covars_ = covars assert_array_almost_equal(g.covars_, covars) assert_raises(ValueError, g._set_covars, []) assert_raises(ValueError, g._set_covars, np.zeros((n_components - 2, n_features))) assert_raises(ValueError, mixture.GMM, n_components=20, covariance_type='badcovariance_type') class GMMTester(): do_test_eval = True def _setUp(self): self.n_components = 10 self.n_features = 4 self.weights = rng.rand(self.n_components) self.weights = self.weights / self.weights.sum() self.means = rng.randint(-20, 20, (self.n_components, self.n_features)) self.threshold = -0.5 self.I = np.eye(self.n_features) self.covars = { 'spherical': (0.1 + 2 * rng.rand(self.n_components, self.n_features)) ** 2, 'tied': (make_spd_matrix(self.n_features, random_state=0) + 5 * self.I), 'diag': (0.1 + 2 * rng.rand(self.n_components, self.n_features)) ** 2, 'full': np.array([make_spd_matrix(self.n_features, random_state=0) + 5 * self.I for x in range(self.n_components)])} def test_eval(self): if not self.do_test_eval: return # DPGMM does not support setting the means and # covariances before fitting There is no way of fixing this # due to the variational parameters being more expressive than # covariance matrices g = self.model(n_components=self.n_components, covariance_type=self.covariance_type, random_state=rng) # Make sure the means are far apart so responsibilities.argmax() # picks the actual component used to generate the observations. g.means_ = 20 * self.means g.covars_ = self.covars[self.covariance_type] g.weights_ = self.weights gaussidx = np.repeat(np.arange(self.n_components), 5) n_samples = len(gaussidx) X = rng.randn(n_samples, self.n_features) + g.means_[gaussidx] ll, responsibilities = g.score_samples(X) self.assertEqual(len(ll), n_samples) self.assertEqual(responsibilities.shape, (n_samples, self.n_components)) assert_array_almost_equal(responsibilities.sum(axis=1), np.ones(n_samples)) assert_array_equal(responsibilities.argmax(axis=1), gaussidx) def test_sample(self, n=100): g = self.model(n_components=self.n_components, covariance_type=self.covariance_type, random_state=rng) # Make sure the means are far apart so responsibilities.argmax() # picks the actual component used to generate the observations. g.means_ = 20 * self.means g.covars_ = np.maximum(self.covars[self.covariance_type], 0.1) g.weights_ = self.weights samples = g.sample(n) self.assertEqual(samples.shape, (n, self.n_features)) def test_train(self, params='wmc'): g = mixture.GMM(n_components=self.n_components, covariance_type=self.covariance_type) g.weights_ = self.weights g.means_ = self.means g.covars_ = 20 * self.covars[self.covariance_type] # Create a training set by sampling from the predefined distribution. X = g.sample(n_samples=100) g = self.model(n_components=self.n_components, covariance_type=self.covariance_type, random_state=rng, min_covar=1e-1, n_iter=1, init_params=params) g.fit(X) # Do one training iteration at a time so we can keep track of # the log likelihood to make sure that it increases after each # iteration. trainll = [] for _ in range(5): g.params = params g.init_params = '' g.fit(X) trainll.append(self.score(g, X)) g.n_iter = 10 g.init_params = '' g.params = params g.fit(X) # finish fitting # Note that the log likelihood will sometimes decrease by a # very small amount after it has more or less converged due to # the addition of min_covar to the covariance (to prevent # underflow). This is why the threshold is set to -0.5 # instead of 0. delta_min = np.diff(trainll).min() self.assertTrue( delta_min > self.threshold, "The min nll increase is %f which is lower than the admissible" " threshold of %f, for model %s. The likelihoods are %s." % (delta_min, self.threshold, self.covariance_type, trainll)) def test_train_degenerate(self, params='wmc'): # Train on degenerate data with 0 in some dimensions # Create a training set by sampling from the predefined distribution. X = rng.randn(100, self.n_features) X.T[1:] = 0 g = self.model(n_components=2, covariance_type=self.covariance_type, random_state=rng, min_covar=1e-3, n_iter=5, init_params=params) g.fit(X) trainll = g.score(X) self.assertTrue(np.sum(np.abs(trainll / 100 / X.shape[1])) < 5) def test_train_1d(self, params='wmc'): # Train on 1-D data # Create a training set by sampling from the predefined distribution. X = rng.randn(100, 1) # X.T[1:] = 0 g = self.model(n_components=2, covariance_type=self.covariance_type, random_state=rng, min_covar=1e-7, n_iter=5, init_params=params) g.fit(X) trainll = g.score(X) if isinstance(g, mixture.DPGMM): self.assertTrue(np.sum(np.abs(trainll / 100)) < 5) else: self.assertTrue(np.sum(np.abs(trainll / 100)) < 2) def score(self, g, X): return g.score(X).sum() class TestGMMWithSphericalCovars(unittest.TestCase, GMMTester): covariance_type = 'spherical' model = mixture.GMM setUp = GMMTester._setUp class TestGMMWithDiagonalCovars(unittest.TestCase, GMMTester): covariance_type = 'diag' model = mixture.GMM setUp = GMMTester._setUp class TestGMMWithTiedCovars(unittest.TestCase, GMMTester): covariance_type = 'tied' model = mixture.GMM setUp = GMMTester._setUp class TestGMMWithFullCovars(unittest.TestCase, GMMTester): covariance_type = 'full' model = mixture.GMM setUp = GMMTester._setUp def test_multiple_init(): # Test that multiple inits does not much worse than a single one X = rng.randn(30, 5) X[:10] += 2 g = mixture.GMM(n_components=2, covariance_type='spherical', random_state=rng, min_covar=1e-7, n_iter=5) train1 = g.fit(X).score(X).sum() g.n_init = 5 train2 = g.fit(X).score(X).sum() assert_true(train2 >= train1 - 1.e-2) def test_n_parameters(): # Test that the right number of parameters is estimated n_samples, n_dim, n_components = 7, 5, 2 X = rng.randn(n_samples, n_dim) n_params = {'spherical': 13, 'diag': 21, 'tied': 26, 'full': 41} for cv_type in ['full', 'tied', 'diag', 'spherical']: g = mixture.GMM(n_components=n_components, covariance_type=cv_type, random_state=rng, min_covar=1e-7, n_iter=1) g.fit(X) assert_true(g._n_parameters() == n_params[cv_type]) def test_1d_1component(): # Test all of the covariance_types return the same BIC score for # 1-dimensional, 1 component fits. n_samples, n_dim, n_components = 100, 1, 1 X = rng.randn(n_samples, n_dim) g_full = mixture.GMM(n_components=n_components, covariance_type='full', random_state=rng, min_covar=1e-7, n_iter=1) g_full.fit(X) g_full_bic = g_full.bic(X) for cv_type in ['tied', 'diag', 'spherical']: g = mixture.GMM(n_components=n_components, covariance_type=cv_type, random_state=rng, min_covar=1e-7, n_iter=1) g.fit(X) assert_array_almost_equal(g.bic(X), g_full_bic) def assert_fit_predict_correct(model, X): model2 = copy.deepcopy(model) predictions_1 = model.fit(X).predict(X) predictions_2 = model2.fit_predict(X) assert adjusted_rand_score(predictions_1, predictions_2) == 1.0 def test_fit_predict(): """ test that gmm.fit_predict is equivalent to gmm.fit + gmm.predict """ lrng = np.random.RandomState(101) n_samples, n_dim, n_comps = 100, 2, 2 mu = np.array([[8, 8]]) component_0 = lrng.randn(n_samples, n_dim) component_1 = lrng.randn(n_samples, n_dim) + mu X = np.vstack((component_0, component_1)) for m_constructor in (mixture.GMM, mixture.VBGMM, mixture.DPGMM): model = m_constructor(n_components=n_comps, covariance_type='full', min_covar=1e-7, n_iter=5, random_state=np.random.RandomState(0)) assert_fit_predict_correct(model, X) model = mixture.GMM(n_components=n_comps, n_iter=0) z = model.fit_predict(X) assert np.all(z == 0), "Quick Initialization Failed!" def test_aic(): # Test the aic and bic criteria n_samples, n_dim, n_components = 50, 3, 2 X = rng.randn(n_samples, n_dim) SGH = 0.5 * (X.var() + np.log(2 * np.pi)) # standard gaussian entropy for cv_type in ['full', 'tied', 'diag', 'spherical']: g = mixture.GMM(n_components=n_components, covariance_type=cv_type, random_state=rng, min_covar=1e-7) g.fit(X) aic = 2 * n_samples * SGH * n_dim + 2 * g._n_parameters() bic = (2 * n_samples * SGH * n_dim + np.log(n_samples) * g._n_parameters()) bound = n_dim * 3. / np.sqrt(n_samples) assert_true(np.abs(g.aic(X) - aic) / n_samples < bound) assert_true(np.abs(g.bic(X) - bic) / n_samples < bound) def check_positive_definite_covars(covariance_type): r"""Test that covariance matrices do not become non positive definite Due to the accumulation of round-off errors, the computation of the covariance matrices during the learning phase could lead to non-positive definite covariance matrices. Namely the use of the formula: .. math:: C = (\sum_i w_i x_i x_i^T) - \mu \mu^T instead of: .. math:: C = \sum_i w_i (x_i - \mu)(x_i - \mu)^T while mathematically equivalent, was observed a ``LinAlgError`` exception, when computing a ``GMM`` with full covariance matrices and fixed mean. This function ensures that some later optimization will not introduce the problem again. """ rng = np.random.RandomState(1) # we build a dataset with 2 2d component. The components are unbalanced # (respective weights 0.9 and 0.1) X = rng.randn(100, 2) X[-10:] += (3, 3) # Shift the 10 last points gmm = mixture.GMM(2, params="wc", covariance_type=covariance_type, min_covar=1e-3) # This is a non-regression test for issue #2640. The following call used # to trigger: # numpy.linalg.linalg.LinAlgError: 2-th leading minor not positive definite gmm.fit(X) if covariance_type == "diag" or covariance_type == "spherical": assert_greater(gmm.covars_.min(), 0) else: if covariance_type == "tied": covs = [gmm.covars_] else: covs = gmm.covars_ for c in covs: assert_greater(np.linalg.det(c), 0) def test_positive_definite_covars(): # Check positive definiteness for all covariance types for covariance_type in ["full", "tied", "diag", "spherical"]: yield check_positive_definite_covars, covariance_type def test_verbose_first_level(): # Create sample data X = rng.randn(30, 5) X[:10] += 2 g = mixture.GMM(n_components=2, n_init=2, verbose=1) old_stdout = sys.stdout sys.stdout = StringIO() try: g.fit(X) finally: sys.stdout = old_stdout def test_verbose_second_level(): # Create sample data X = rng.randn(30, 5) X[:10] += 2 g = mixture.GMM(n_components=2, n_init=2, verbose=2) old_stdout = sys.stdout sys.stdout = StringIO() try: g.fit(X) finally: sys.stdout = old_stdout
bsd-3-clause
costypetrisor/scikit-learn
examples/linear_model/plot_ols_ridge_variance.py
387
2060
#!/usr/bin/python # -*- coding: utf-8 -*- """ ========================================================= Ordinary Least Squares and Ridge Regression Variance ========================================================= Due to the few points in each dimension and the straight line that linear regression uses to follow these points as well as it can, noise on the observations will cause great variance as shown in the first plot. Every line's slope can vary quite a bit for each prediction due to the noise induced in the observations. Ridge regression is basically minimizing a penalised version of the least-squared function. The penalising `shrinks` the value of the regression coefficients. Despite the few data points in each dimension, the slope of the prediction is much more stable and the variance in the line itself is greatly reduced, in comparison to that of the standard linear regression """ print(__doc__) # Code source: Gaël Varoquaux # Modified for documentation by Jaques Grobler # License: BSD 3 clause import numpy as np import matplotlib.pyplot as plt from sklearn import linear_model X_train = np.c_[.5, 1].T y_train = [.5, 1] X_test = np.c_[0, 2].T np.random.seed(0) classifiers = dict(ols=linear_model.LinearRegression(), ridge=linear_model.Ridge(alpha=.1)) fignum = 1 for name, clf in classifiers.items(): fig = plt.figure(fignum, figsize=(4, 3)) plt.clf() plt.title(name) ax = plt.axes([.12, .12, .8, .8]) for _ in range(6): this_X = .1 * np.random.normal(size=(2, 1)) + X_train clf.fit(this_X, y_train) ax.plot(X_test, clf.predict(X_test), color='.5') ax.scatter(this_X, y_train, s=3, c='.5', marker='o', zorder=10) clf.fit(X_train, y_train) ax.plot(X_test, clf.predict(X_test), linewidth=2, color='blue') ax.scatter(X_train, y_train, s=30, c='r', marker='+', zorder=10) ax.set_xticks(()) ax.set_yticks(()) ax.set_ylim((0, 1.6)) ax.set_xlabel('X') ax.set_ylabel('y') ax.set_xlim(0, 2) fignum += 1 plt.show()
bsd-3-clause
mne-tools/mne-tools.github.io
0.22/_downloads/52a5ebd4d6b8bcb7eccdf9bc2b0fcfcc/plot_cluster_stats_evoked.py
18
3021
""" ======================================================= Permutation F-test on sensor data with 1D cluster level ======================================================= One tests if the evoked response is significantly different between conditions. Multiple comparison problem is addressed with cluster level permutation test. """ # Authors: Alexandre Gramfort <alexandre.gramfort@inria.fr> # # License: BSD (3-clause) import matplotlib.pyplot as plt import mne from mne import io from mne.stats import permutation_cluster_test from mne.datasets import sample print(__doc__) ############################################################################### # Set parameters data_path = sample.data_path() raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif' event_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw-eve.fif' tmin = -0.2 tmax = 0.5 # Setup for reading the raw data raw = io.read_raw_fif(raw_fname) events = mne.read_events(event_fname) channel = 'MEG 1332' # include only this channel in analysis include = [channel] ############################################################################### # Read epochs for the channel of interest picks = mne.pick_types(raw.info, meg=False, eog=True, include=include, exclude='bads') event_id = 1 reject = dict(grad=4000e-13, eog=150e-6) epochs1 = mne.Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), reject=reject) condition1 = epochs1.get_data() # as 3D matrix event_id = 2 epochs2 = mne.Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), reject=reject) condition2 = epochs2.get_data() # as 3D matrix condition1 = condition1[:, 0, :] # take only one channel to get a 2D array condition2 = condition2[:, 0, :] # take only one channel to get a 2D array ############################################################################### # Compute statistic threshold = 6.0 T_obs, clusters, cluster_p_values, H0 = \ permutation_cluster_test([condition1, condition2], n_permutations=1000, threshold=threshold, tail=1, n_jobs=1, out_type='mask') ############################################################################### # Plot times = epochs1.times plt.close('all') plt.subplot(211) plt.title('Channel : ' + channel) plt.plot(times, condition1.mean(axis=0) - condition2.mean(axis=0), label="ERF Contrast (Event 1 - Event 2)") plt.ylabel("MEG (T / m)") plt.legend() plt.subplot(212) for i_c, c in enumerate(clusters): c = c[0] if cluster_p_values[i_c] <= 0.05: h = plt.axvspan(times[c.start], times[c.stop - 1], color='r', alpha=0.3) else: plt.axvspan(times[c.start], times[c.stop - 1], color=(0.3, 0.3, 0.3), alpha=0.3) hf = plt.plot(times, T_obs, 'g') plt.legend((h, ), ('cluster p-value < 0.05', )) plt.xlabel("time (ms)") plt.ylabel("f-values") plt.show()
bsd-3-clause
kevin-intel/scikit-learn
sklearn/cluster/tests/test_dbscan.py
14
15405
""" Tests for DBSCAN clustering algorithm """ import pickle import numpy as np import warnings from scipy.spatial import distance from scipy import sparse import pytest from sklearn.utils._testing import assert_array_equal from sklearn.neighbors import NearestNeighbors from sklearn.cluster import DBSCAN from sklearn.cluster import dbscan from sklearn.cluster.tests.common import generate_clustered_data from sklearn.metrics.pairwise import pairwise_distances n_clusters = 3 X = generate_clustered_data(n_clusters=n_clusters) def test_dbscan_similarity(): # Tests the DBSCAN algorithm with a similarity array. # Parameters chosen specifically for this task. eps = 0.15 min_samples = 10 # Compute similarities D = distance.squareform(distance.pdist(X)) D /= np.max(D) # Compute DBSCAN core_samples, labels = dbscan(D, metric="precomputed", eps=eps, min_samples=min_samples) # number of clusters, ignoring noise if present n_clusters_1 = len(set(labels)) - (1 if -1 in labels else 0) assert n_clusters_1 == n_clusters db = DBSCAN(metric="precomputed", eps=eps, min_samples=min_samples) labels = db.fit(D).labels_ n_clusters_2 = len(set(labels)) - int(-1 in labels) assert n_clusters_2 == n_clusters def test_dbscan_feature(): # Tests the DBSCAN algorithm with a feature vector array. # Parameters chosen specifically for this task. # Different eps to other test, because distance is not normalised. eps = 0.8 min_samples = 10 metric = 'euclidean' # Compute DBSCAN # parameters chosen for task core_samples, labels = dbscan(X, metric=metric, eps=eps, min_samples=min_samples) # number of clusters, ignoring noise if present n_clusters_1 = len(set(labels)) - int(-1 in labels) assert n_clusters_1 == n_clusters db = DBSCAN(metric=metric, eps=eps, min_samples=min_samples) labels = db.fit(X).labels_ n_clusters_2 = len(set(labels)) - int(-1 in labels) assert n_clusters_2 == n_clusters def test_dbscan_sparse(): core_sparse, labels_sparse = dbscan(sparse.lil_matrix(X), eps=.8, min_samples=10) core_dense, labels_dense = dbscan(X, eps=.8, min_samples=10) assert_array_equal(core_dense, core_sparse) assert_array_equal(labels_dense, labels_sparse) @pytest.mark.parametrize('include_self', [False, True]) def test_dbscan_sparse_precomputed(include_self): D = pairwise_distances(X) nn = NearestNeighbors(radius=.9).fit(X) X_ = X if include_self else None D_sparse = nn.radius_neighbors_graph(X=X_, mode='distance') # Ensure it is sparse not merely on diagonals: assert D_sparse.nnz < D.shape[0] * (D.shape[0] - 1) core_sparse, labels_sparse = dbscan(D_sparse, eps=.8, min_samples=10, metric='precomputed') core_dense, labels_dense = dbscan(D, eps=.8, min_samples=10, metric='precomputed') assert_array_equal(core_dense, core_sparse) assert_array_equal(labels_dense, labels_sparse) def test_dbscan_sparse_precomputed_different_eps(): # test that precomputed neighbors graph is filtered if computed with # a radius larger than DBSCAN's eps. lower_eps = 0.2 nn = NearestNeighbors(radius=lower_eps).fit(X) D_sparse = nn.radius_neighbors_graph(X, mode='distance') dbscan_lower = dbscan(D_sparse, eps=lower_eps, metric='precomputed') higher_eps = lower_eps + 0.7 nn = NearestNeighbors(radius=higher_eps).fit(X) D_sparse = nn.radius_neighbors_graph(X, mode='distance') dbscan_higher = dbscan(D_sparse, eps=lower_eps, metric='precomputed') assert_array_equal(dbscan_lower[0], dbscan_higher[0]) assert_array_equal(dbscan_lower[1], dbscan_higher[1]) @pytest.mark.parametrize('use_sparse', [True, False]) @pytest.mark.parametrize('metric', ['precomputed', 'minkowski']) def test_dbscan_input_not_modified(use_sparse, metric): # test that the input is not modified by dbscan X = np.random.RandomState(0).rand(10, 10) X = sparse.csr_matrix(X) if use_sparse else X X_copy = X.copy() dbscan(X, metric=metric) if use_sparse: assert_array_equal(X.toarray(), X_copy.toarray()) else: assert_array_equal(X, X_copy) def test_dbscan_no_core_samples(): rng = np.random.RandomState(0) X = rng.rand(40, 10) X[X < .8] = 0 for X_ in [X, sparse.csr_matrix(X)]: db = DBSCAN(min_samples=6).fit(X_) assert_array_equal(db.components_, np.empty((0, X_.shape[1]))) assert_array_equal(db.labels_, -1) assert db.core_sample_indices_.shape == (0,) def test_dbscan_callable(): # Tests the DBSCAN algorithm with a callable metric. # Parameters chosen specifically for this task. # Different eps to other test, because distance is not normalised. eps = 0.8 min_samples = 10 # metric is the function reference, not the string key. metric = distance.euclidean # Compute DBSCAN # parameters chosen for task core_samples, labels = dbscan(X, metric=metric, eps=eps, min_samples=min_samples, algorithm='ball_tree') # number of clusters, ignoring noise if present n_clusters_1 = len(set(labels)) - int(-1 in labels) assert n_clusters_1 == n_clusters db = DBSCAN(metric=metric, eps=eps, min_samples=min_samples, algorithm='ball_tree') labels = db.fit(X).labels_ n_clusters_2 = len(set(labels)) - int(-1 in labels) assert n_clusters_2 == n_clusters def test_dbscan_metric_params(): # Tests that DBSCAN works with the metrics_params argument. eps = 0.8 min_samples = 10 p = 1 # Compute DBSCAN with metric_params arg with warnings.catch_warnings(record=True) as warns: db = DBSCAN( metric='minkowski', metric_params={'p': p}, eps=eps, p=None, min_samples=min_samples, algorithm='ball_tree' ).fit(X) assert not warns core_sample_1, labels_1 = db.core_sample_indices_, db.labels_ # Test that sample labels are the same as passing Minkowski 'p' directly db = DBSCAN(metric='minkowski', eps=eps, min_samples=min_samples, algorithm='ball_tree', p=p).fit(X) core_sample_2, labels_2 = db.core_sample_indices_, db.labels_ assert_array_equal(core_sample_1, core_sample_2) assert_array_equal(labels_1, labels_2) # Minkowski with p=1 should be equivalent to Manhattan distance db = DBSCAN(metric='manhattan', eps=eps, min_samples=min_samples, algorithm='ball_tree').fit(X) core_sample_3, labels_3 = db.core_sample_indices_, db.labels_ assert_array_equal(core_sample_1, core_sample_3) assert_array_equal(labels_1, labels_3) with pytest.warns( SyntaxWarning, match="Parameter p is found in metric_params. " "The corresponding parameter from __init__ " "is ignored."): # Test that checks p is ignored in favor of metric_params={'p': <val>} db = DBSCAN(metric='minkowski', metric_params={'p': p}, eps=eps, p=p+1, min_samples=min_samples, algorithm='ball_tree').fit(X) core_sample_4, labels_4 = db.core_sample_indices_, db.labels_ assert_array_equal(core_sample_1, core_sample_4) assert_array_equal(labels_1, labels_4) def test_dbscan_balltree(): # Tests the DBSCAN algorithm with balltree for neighbor calculation. eps = 0.8 min_samples = 10 D = pairwise_distances(X) core_samples, labels = dbscan(D, metric="precomputed", eps=eps, min_samples=min_samples) # number of clusters, ignoring noise if present n_clusters_1 = len(set(labels)) - int(-1 in labels) assert n_clusters_1 == n_clusters db = DBSCAN(p=2.0, eps=eps, min_samples=min_samples, algorithm='ball_tree') labels = db.fit(X).labels_ n_clusters_2 = len(set(labels)) - int(-1 in labels) assert n_clusters_2 == n_clusters db = DBSCAN(p=2.0, eps=eps, min_samples=min_samples, algorithm='kd_tree') labels = db.fit(X).labels_ n_clusters_3 = len(set(labels)) - int(-1 in labels) assert n_clusters_3 == n_clusters db = DBSCAN(p=1.0, eps=eps, min_samples=min_samples, algorithm='ball_tree') labels = db.fit(X).labels_ n_clusters_4 = len(set(labels)) - int(-1 in labels) assert n_clusters_4 == n_clusters db = DBSCAN(leaf_size=20, eps=eps, min_samples=min_samples, algorithm='ball_tree') labels = db.fit(X).labels_ n_clusters_5 = len(set(labels)) - int(-1 in labels) assert n_clusters_5 == n_clusters def test_input_validation(): # DBSCAN.fit should accept a list of lists. X = [[1., 2.], [3., 4.]] DBSCAN().fit(X) # must not raise exception @pytest.mark.parametrize( "args", [{'eps': -1.0}, {'algorithm': 'blah'}, {'metric': 'blah'}, {'leaf_size': -1}, {'p': -1}] ) def test_dbscan_badargs(args): # Test bad argument values: these should all raise ValueErrors with pytest.raises(ValueError): dbscan(X, **args) def test_pickle(): obj = DBSCAN() s = pickle.dumps(obj) assert type(pickle.loads(s)) == obj.__class__ def test_boundaries(): # ensure min_samples is inclusive of core point core, _ = dbscan([[0], [1]], eps=2, min_samples=2) assert 0 in core # ensure eps is inclusive of circumference core, _ = dbscan([[0], [1], [1]], eps=1, min_samples=2) assert 0 in core core, _ = dbscan([[0], [1], [1]], eps=.99, min_samples=2) assert 0 not in core def test_weighted_dbscan(): # ensure sample_weight is validated with pytest.raises(ValueError): dbscan([[0], [1]], sample_weight=[2]) with pytest.raises(ValueError): dbscan([[0], [1]], sample_weight=[2, 3, 4]) # ensure sample_weight has an effect assert_array_equal([], dbscan([[0], [1]], sample_weight=None, min_samples=6)[0]) assert_array_equal([], dbscan([[0], [1]], sample_weight=[5, 5], min_samples=6)[0]) assert_array_equal([0], dbscan([[0], [1]], sample_weight=[6, 5], min_samples=6)[0]) assert_array_equal([0, 1], dbscan([[0], [1]], sample_weight=[6, 6], min_samples=6)[0]) # points within eps of each other: assert_array_equal([0, 1], dbscan([[0], [1]], eps=1.5, sample_weight=[5, 1], min_samples=6)[0]) # and effect of non-positive and non-integer sample_weight: assert_array_equal([], dbscan([[0], [1]], sample_weight=[5, 0], eps=1.5, min_samples=6)[0]) assert_array_equal([0, 1], dbscan([[0], [1]], sample_weight=[5.9, 0.1], eps=1.5, min_samples=6)[0]) assert_array_equal([0, 1], dbscan([[0], [1]], sample_weight=[6, 0], eps=1.5, min_samples=6)[0]) assert_array_equal([], dbscan([[0], [1]], sample_weight=[6, -1], eps=1.5, min_samples=6)[0]) # for non-negative sample_weight, cores should be identical to repetition rng = np.random.RandomState(42) sample_weight = rng.randint(0, 5, X.shape[0]) core1, label1 = dbscan(X, sample_weight=sample_weight) assert len(label1) == len(X) X_repeated = np.repeat(X, sample_weight, axis=0) core_repeated, label_repeated = dbscan(X_repeated) core_repeated_mask = np.zeros(X_repeated.shape[0], dtype=bool) core_repeated_mask[core_repeated] = True core_mask = np.zeros(X.shape[0], dtype=bool) core_mask[core1] = True assert_array_equal(np.repeat(core_mask, sample_weight), core_repeated_mask) # sample_weight should work with precomputed distance matrix D = pairwise_distances(X) core3, label3 = dbscan(D, sample_weight=sample_weight, metric='precomputed') assert_array_equal(core1, core3) assert_array_equal(label1, label3) # sample_weight should work with estimator est = DBSCAN().fit(X, sample_weight=sample_weight) core4 = est.core_sample_indices_ label4 = est.labels_ assert_array_equal(core1, core4) assert_array_equal(label1, label4) est = DBSCAN() label5 = est.fit_predict(X, sample_weight=sample_weight) core5 = est.core_sample_indices_ assert_array_equal(core1, core5) assert_array_equal(label1, label5) assert_array_equal(label1, est.labels_) @pytest.mark.parametrize('algorithm', ['brute', 'kd_tree', 'ball_tree']) def test_dbscan_core_samples_toy(algorithm): X = [[0], [2], [3], [4], [6], [8], [10]] n_samples = len(X) # Degenerate case: every sample is a core sample, either with its own # cluster or including other close core samples. core_samples, labels = dbscan(X, algorithm=algorithm, eps=1, min_samples=1) assert_array_equal(core_samples, np.arange(n_samples)) assert_array_equal(labels, [0, 1, 1, 1, 2, 3, 4]) # With eps=1 and min_samples=2 only the 3 samples from the denser area # are core samples. All other points are isolated and considered noise. core_samples, labels = dbscan(X, algorithm=algorithm, eps=1, min_samples=2) assert_array_equal(core_samples, [1, 2, 3]) assert_array_equal(labels, [-1, 0, 0, 0, -1, -1, -1]) # Only the sample in the middle of the dense area is core. Its two # neighbors are edge samples. Remaining samples are noise. core_samples, labels = dbscan(X, algorithm=algorithm, eps=1, min_samples=3) assert_array_equal(core_samples, [2]) assert_array_equal(labels, [-1, 0, 0, 0, -1, -1, -1]) # It's no longer possible to extract core samples with eps=1: # everything is noise. core_samples, labels = dbscan(X, algorithm=algorithm, eps=1, min_samples=4) assert_array_equal(core_samples, []) assert_array_equal(labels, np.full(n_samples, -1.)) def test_dbscan_precomputed_metric_with_degenerate_input_arrays(): # see https://github.com/scikit-learn/scikit-learn/issues/4641 for # more details X = np.eye(10) labels = DBSCAN(eps=0.5, metric='precomputed').fit(X).labels_ assert len(set(labels)) == 1 X = np.zeros((10, 10)) labels = DBSCAN(eps=0.5, metric='precomputed').fit(X).labels_ assert len(set(labels)) == 1 def test_dbscan_precomputed_metric_with_initial_rows_zero(): # sample matrix with initial two row all zero ar = np.array([ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.1, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.1, 0.0, 0.0], [0.0, 0.0, 0.1, 0.1, 0.0, 0.0, 0.3], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.1], [0.0, 0.0, 0.0, 0.0, 0.3, 0.1, 0.0] ]) matrix = sparse.csr_matrix(ar) labels = DBSCAN(eps=0.2, metric='precomputed', min_samples=2).fit(matrix).labels_ assert_array_equal(labels, [-1, -1, 0, 0, 0, 1, 1])
bsd-3-clause
wazeerzulfikar/scikit-learn
sklearn/svm/tests/test_bounds.py
6
2369
import numpy as np from scipy import sparse as sp from sklearn.svm.bounds import l1_min_c from sklearn.svm import LinearSVC from sklearn.linear_model.logistic import LogisticRegression from sklearn.utils.testing import assert_true, raises from sklearn.utils.testing import assert_raise_message dense_X = [[-1, 0], [0, 1], [1, 1], [1, 1]] sparse_X = sp.csr_matrix(dense_X) Y1 = [0, 1, 1, 1] Y2 = [2, 1, 0, 0] def test_l1_min_c(): losses = ['squared_hinge', 'log'] Xs = {'sparse': sparse_X, 'dense': dense_X} Ys = {'two-classes': Y1, 'multi-class': Y2} intercepts = {'no-intercept': {'fit_intercept': False}, 'fit-intercept': {'fit_intercept': True, 'intercept_scaling': 10}} for loss in losses: for X_label, X in Xs.items(): for Y_label, Y in Ys.items(): for intercept_label, intercept_params in intercepts.items(): check = lambda: check_l1_min_c(X, Y, loss, **intercept_params) check.description = ('Test l1_min_c loss=%r %s %s %s' % (loss, X_label, Y_label, intercept_label)) yield check # loss='l2' should raise ValueError assert_raise_message(ValueError, "loss type not in", l1_min_c, dense_X, Y1, "l2") def check_l1_min_c(X, y, loss, fit_intercept=True, intercept_scaling=None): min_c = l1_min_c(X, y, loss, fit_intercept, intercept_scaling) clf = { 'log': LogisticRegression(penalty='l1'), 'squared_hinge': LinearSVC(loss='squared_hinge', penalty='l1', dual=False), }[loss] clf.fit_intercept = fit_intercept clf.intercept_scaling = intercept_scaling clf.C = min_c clf.fit(X, y) assert_true((np.asarray(clf.coef_) == 0).all()) assert_true((np.asarray(clf.intercept_) == 0).all()) clf.C = min_c * 1.01 clf.fit(X, y) assert_true((np.asarray(clf.coef_) != 0).any() or (np.asarray(clf.intercept_) != 0).any()) @raises(ValueError) def test_ill_posed_min_c(): X = [[0, 0], [0, 0]] y = [0, 1] l1_min_c(X, y) @raises(ValueError) def test_unsupported_loss(): l1_min_c(dense_X, Y1, 'l1')
bsd-3-clause
ominux/scikit-learn
examples/linear_model/plot_sgd_iris.py
4
2171
""" ======================================== Plot multi-class SGD on the iris dataset ======================================== Plot decision surface of multi-class SGD on iris dataset. The hyperplanes corresponding to the three one-versus-all (OVA) classifiers are represented by the dashed lines. """ print __doc__ import numpy as np import pylab as pl from sklearn import datasets from sklearn.linear_model import SGDClassifier # import some data to play with iris = datasets.load_iris() X = iris.data[:, :2] # we only take the first two features. We could # avoid this ugly slicing by using a two-dim dataset y = iris.target colors = "bry" # shuffle idx = np.arange(X.shape[0]) np.random.seed(13) np.random.shuffle(idx) X = X[idx] y = y[idx] # standardize mean = X.mean(axis=0) std = X.std(axis=0) X = (X - mean) / std h = .02 # step size in the mesh clf = SGDClassifier(alpha=0.001, n_iter=100).fit(X, y) # create a mesh to plot in x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) pl.set_cmap(pl.cm.Paired) # Plot the decision boundary. For that, we will asign a color to each # point in the mesh [x_min, m_max]x[y_min, y_max]. Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) # Put the result into a color plot Z = Z.reshape(xx.shape) pl.set_cmap(pl.cm.Paired) cs = pl.contourf(xx, yy, Z) pl.axis('tight') # Plot also the training points for i, color in zip(clf.classes, colors): idx = np.where(y == i) pl.scatter(X[idx, 0], X[idx, 1], c=color, label=iris.target_names[i]) pl.title("Decision surface of multi-class SGD") pl.axis('tight') # Plot the three one-against-all classifiers xmin, xmax = pl.xlim() ymin, ymax = pl.ylim() coef = clf.coef_ intercept = clf.intercept_ def plot_hyperplane(c, color): def line(x0): return (-(x0 * coef[c, 0]) - intercept[c]) / coef[c, 1] pl.plot([xmin, xmax], [line(xmin), line(xmax)], ls="--", color=color) for i, color in zip(clf.classes, colors): plot_hyperplane(i, color) pl.legend() pl.show()
bsd-3-clause
Vvucinic/Wander
venv_2_7/lib/python2.7/site-packages/traitlets/config/loader.py
3
28215
# encoding: utf-8 """A simple configuration system.""" # Copyright (c) IPython Development Team. # Distributed under the terms of the Modified BSD License. import argparse import copy import logging import os import re import sys import json from ast import literal_eval from ipython_genutils.path import filefind from ipython_genutils import py3compat from ipython_genutils.encoding import DEFAULT_ENCODING from ipython_genutils.py3compat import unicode_type, iteritems from traitlets.traitlets import HasTraits, List, Any #----------------------------------------------------------------------------- # Exceptions #----------------------------------------------------------------------------- class ConfigError(Exception): pass class ConfigLoaderError(ConfigError): pass class ConfigFileNotFound(ConfigError): pass class ArgumentError(ConfigLoaderError): pass #----------------------------------------------------------------------------- # Argparse fix #----------------------------------------------------------------------------- # Unfortunately argparse by default prints help messages to stderr instead of # stdout. This makes it annoying to capture long help screens at the command # line, since one must know how to pipe stderr, which many users don't know how # to do. So we override the print_help method with one that defaults to # stdout and use our class instead. class ArgumentParser(argparse.ArgumentParser): """Simple argparse subclass that prints help to stdout by default.""" def print_help(self, file=None): if file is None: file = sys.stdout return super(ArgumentParser, self).print_help(file) print_help.__doc__ = argparse.ArgumentParser.print_help.__doc__ #----------------------------------------------------------------------------- # Config class for holding config information #----------------------------------------------------------------------------- class LazyConfigValue(HasTraits): """Proxy object for exposing methods on configurable containers Exposes: - append, extend, insert on lists - update on dicts - update, add on sets """ _value = None # list methods _extend = List() _prepend = List() def append(self, obj): self._extend.append(obj) def extend(self, other): self._extend.extend(other) def prepend(self, other): """like list.extend, but for the front""" self._prepend[:0] = other _inserts = List() def insert(self, index, other): if not isinstance(index, int): raise TypeError("An integer is required") self._inserts.append((index, other)) # dict methods # update is used for both dict and set _update = Any() def update(self, other): if self._update is None: if isinstance(other, dict): self._update = {} else: self._update = set() self._update.update(other) # set methods def add(self, obj): self.update({obj}) def get_value(self, initial): """construct the value from the initial one after applying any insert / extend / update changes """ if self._value is not None: return self._value value = copy.deepcopy(initial) if isinstance(value, list): for idx, obj in self._inserts: value.insert(idx, obj) value[:0] = self._prepend value.extend(self._extend) elif isinstance(value, dict): if self._update: value.update(self._update) elif isinstance(value, set): if self._update: value.update(self._update) self._value = value return value def to_dict(self): """return JSONable dict form of my data Currently update as dict or set, extend, prepend as lists, and inserts as list of tuples. """ d = {} if self._update: d['update'] = self._update if self._extend: d['extend'] = self._extend if self._prepend: d['prepend'] = self._prepend elif self._inserts: d['inserts'] = self._inserts return d def _is_section_key(key): """Is a Config key a section name (does it start with a capital)?""" if key and key[0].upper()==key[0] and not key.startswith('_'): return True else: return False class Config(dict): """An attribute based dict that can do smart merges.""" def __init__(self, *args, **kwds): dict.__init__(self, *args, **kwds) self._ensure_subconfig() def _ensure_subconfig(self): """ensure that sub-dicts that should be Config objects are casts dicts that are under section keys to Config objects, which is necessary for constructing Config objects from dict literals. """ for key in self: obj = self[key] if _is_section_key(key) \ and isinstance(obj, dict) \ and not isinstance(obj, Config): setattr(self, key, Config(obj)) def _merge(self, other): """deprecated alias, use Config.merge()""" self.merge(other) def merge(self, other): """merge another config object into this one""" to_update = {} for k, v in iteritems(other): if k not in self: to_update[k] = copy.deepcopy(v) else: # I have this key if isinstance(v, Config) and isinstance(self[k], Config): # Recursively merge common sub Configs self[k].merge(v) else: # Plain updates for non-Configs to_update[k] = copy.deepcopy(v) self.update(to_update) def collisions(self, other): """Check for collisions between two config objects. Returns a dict of the form {"Class": {"trait": "collision message"}}`, indicating which values have been ignored. An empty dict indicates no collisions. """ collisions = {} for section in self: if section not in other: continue mine = self[section] theirs = other[section] for key in mine: if key in theirs and mine[key] != theirs[key]: collisions.setdefault(section, {}) collisions[section][key] = "%r ignored, using %r" % (mine[key], theirs[key]) return collisions def __contains__(self, key): # allow nested contains of the form `"Section.key" in config` if '.' in key: first, remainder = key.split('.', 1) if first not in self: return False return remainder in self[first] return super(Config, self).__contains__(key) # .has_key is deprecated for dictionaries. has_key = __contains__ def _has_section(self, key): return _is_section_key(key) and key in self def copy(self): return type(self)(dict.copy(self)) def __copy__(self): return self.copy() def __deepcopy__(self, memo): new_config = type(self)() for key, value in self.items(): if isinstance(value, (Config, LazyConfigValue)): # deep copy config objects value = copy.deepcopy(value, memo) elif type(value) in {dict, list, set, tuple}: # shallow copy plain container traits value = copy.copy(value) new_config[key] = value return new_config def __getitem__(self, key): try: return dict.__getitem__(self, key) except KeyError: if _is_section_key(key): c = Config() dict.__setitem__(self, key, c) return c elif not key.startswith('_'): # undefined, create lazy value, used for container methods v = LazyConfigValue() dict.__setitem__(self, key, v) return v else: raise KeyError def __setitem__(self, key, value): if _is_section_key(key): if not isinstance(value, Config): raise ValueError('values whose keys begin with an uppercase ' 'char must be Config instances: %r, %r' % (key, value)) dict.__setitem__(self, key, value) def __getattr__(self, key): if key.startswith('__'): return dict.__getattr__(self, key) try: return self.__getitem__(key) except KeyError as e: raise AttributeError(e) def __setattr__(self, key, value): if key.startswith('__'): return dict.__setattr__(self, key, value) try: self.__setitem__(key, value) except KeyError as e: raise AttributeError(e) def __delattr__(self, key): if key.startswith('__'): return dict.__delattr__(self, key) try: dict.__delitem__(self, key) except KeyError as e: raise AttributeError(e) #----------------------------------------------------------------------------- # Config loading classes #----------------------------------------------------------------------------- class ConfigLoader(object): """A object for loading configurations from just about anywhere. The resulting configuration is packaged as a :class:`Config`. Notes ----- A :class:`ConfigLoader` does one thing: load a config from a source (file, command line arguments) and returns the data as a :class:`Config` object. There are lots of things that :class:`ConfigLoader` does not do. It does not implement complex logic for finding config files. It does not handle default values or merge multiple configs. These things need to be handled elsewhere. """ def _log_default(self): from traitlets.log import get_logger return get_logger() def __init__(self, log=None): """A base class for config loaders. log : instance of :class:`logging.Logger` to use. By default loger of :meth:`traitlets.config.application.Application.instance()` will be used Examples -------- >>> cl = ConfigLoader() >>> config = cl.load_config() >>> config {} """ self.clear() if log is None: self.log = self._log_default() self.log.debug('Using default logger') else: self.log = log def clear(self): self.config = Config() def load_config(self): """Load a config from somewhere, return a :class:`Config` instance. Usually, this will cause self.config to be set and then returned. However, in most cases, :meth:`ConfigLoader.clear` should be called to erase any previous state. """ self.clear() return self.config class FileConfigLoader(ConfigLoader): """A base class for file based configurations. As we add more file based config loaders, the common logic should go here. """ def __init__(self, filename, path=None, **kw): """Build a config loader for a filename and path. Parameters ---------- filename : str The file name of the config file. path : str, list, tuple The path to search for the config file on, or a sequence of paths to try in order. """ super(FileConfigLoader, self).__init__(**kw) self.filename = filename self.path = path self.full_filename = '' def _find_file(self): """Try to find the file by searching the paths.""" self.full_filename = filefind(self.filename, self.path) class JSONFileConfigLoader(FileConfigLoader): """A JSON file loader for config""" def load_config(self): """Load the config from a file and return it as a Config object.""" self.clear() try: self._find_file() except IOError as e: raise ConfigFileNotFound(str(e)) dct = self._read_file_as_dict() self.config = self._convert_to_config(dct) return self.config def _read_file_as_dict(self): with open(self.full_filename) as f: return json.load(f) def _convert_to_config(self, dictionary): if 'version' in dictionary: version = dictionary.pop('version') else: version = 1 self.log.warning("Unrecognized JSON config file version, assuming version {}".format(version)) if version == 1: return Config(dictionary) else: raise ValueError('Unknown version of JSON config file: {version}'.format(version=version)) class PyFileConfigLoader(FileConfigLoader): """A config loader for pure python files. This is responsible for locating a Python config file by filename and path, then executing it to construct a Config object. """ def load_config(self): """Load the config from a file and return it as a Config object.""" self.clear() try: self._find_file() except IOError as e: raise ConfigFileNotFound(str(e)) self._read_file_as_dict() return self.config def load_subconfig(self, fname, path=None): """Injected into config file namespace as load_subconfig""" if path is None: path = self.path loader = self.__class__(fname, path) try: sub_config = loader.load_config() except ConfigFileNotFound: # Pass silently if the sub config is not there, # treat it as an empty config file. pass else: self.config.merge(sub_config) def _read_file_as_dict(self): """Load the config file into self.config, with recursive loading.""" def get_config(): """Unnecessary now, but a deprecation warning is more trouble than it's worth.""" return self.config namespace = dict( c=self.config, load_subconfig=self.load_subconfig, get_config=get_config, __file__=self.full_filename, ) fs_encoding = sys.getfilesystemencoding() or 'ascii' conf_filename = self.full_filename.encode(fs_encoding) py3compat.execfile(conf_filename, namespace) class CommandLineConfigLoader(ConfigLoader): """A config loader for command line arguments. As we add more command line based loaders, the common logic should go here. """ def _exec_config_str(self, lhs, rhs): """execute self.config.<lhs> = <rhs> * expands ~ with expanduser * tries to assign with literal_eval, otherwise assigns with just the string, allowing `--C.a=foobar` and `--C.a="foobar"` to be equivalent. *Not* equivalent are `--C.a=4` and `--C.a='4'`. """ rhs = os.path.expanduser(rhs) try: # Try to see if regular Python syntax will work. This # won't handle strings as the quote marks are removed # by the system shell. value = literal_eval(rhs) except (NameError, SyntaxError, ValueError): # This case happens if the rhs is a string. value = rhs exec(u'self.config.%s = value' % lhs) def _load_flag(self, cfg): """update self.config from a flag, which can be a dict or Config""" if isinstance(cfg, (dict, Config)): # don't clobber whole config sections, update # each section from config: for sec,c in iteritems(cfg): self.config[sec].update(c) else: raise TypeError("Invalid flag: %r" % cfg) # raw --identifier=value pattern # but *also* accept '-' as wordsep, for aliases # accepts: --foo=a # --Class.trait=value # --alias-name=value # rejects: -foo=value # --foo # --Class.trait kv_pattern = re.compile(r'\-\-[A-Za-z][\w\-]*(\.[\w\-]+)*\=.*') # just flags, no assignments, with two *or one* leading '-' # accepts: --foo # -foo-bar-again # rejects: --anything=anything # --two.word flag_pattern = re.compile(r'\-\-?\w+[\-\w]*$') class KeyValueConfigLoader(CommandLineConfigLoader): """A config loader that loads key value pairs from the command line. This allows command line options to be gives in the following form:: ipython --profile="foo" --InteractiveShell.autocall=False """ def __init__(self, argv=None, aliases=None, flags=None, **kw): """Create a key value pair config loader. Parameters ---------- argv : list A list that has the form of sys.argv[1:] which has unicode elements of the form u"key=value". If this is None (default), then sys.argv[1:] will be used. aliases : dict A dict of aliases for configurable traits. Keys are the short aliases, Values are the resolved trait. Of the form: `{'alias' : 'Configurable.trait'}` flags : dict A dict of flags, keyed by str name. Vaues can be Config objects, dicts, or "key=value" strings. If Config or dict, when the flag is triggered, The flag is loaded as `self.config.update(m)`. Returns ------- config : Config The resulting Config object. Examples -------- >>> from traitlets.config.loader import KeyValueConfigLoader >>> cl = KeyValueConfigLoader() >>> d = cl.load_config(["--A.name='brian'","--B.number=0"]) >>> sorted(d.items()) [('A', {'name': 'brian'}), ('B', {'number': 0})] """ super(KeyValueConfigLoader, self).__init__(**kw) if argv is None: argv = sys.argv[1:] self.argv = argv self.aliases = aliases or {} self.flags = flags or {} def clear(self): super(KeyValueConfigLoader, self).clear() self.extra_args = [] def _decode_argv(self, argv, enc=None): """decode argv if bytes, using stdin.encoding, falling back on default enc""" uargv = [] if enc is None: enc = DEFAULT_ENCODING for arg in argv: if not isinstance(arg, unicode_type): # only decode if not already decoded arg = arg.decode(enc) uargv.append(arg) return uargv def load_config(self, argv=None, aliases=None, flags=None): """Parse the configuration and generate the Config object. After loading, any arguments that are not key-value or flags will be stored in self.extra_args - a list of unparsed command-line arguments. This is used for arguments such as input files or subcommands. Parameters ---------- argv : list, optional A list that has the form of sys.argv[1:] which has unicode elements of the form u"key=value". If this is None (default), then self.argv will be used. aliases : dict A dict of aliases for configurable traits. Keys are the short aliases, Values are the resolved trait. Of the form: `{'alias' : 'Configurable.trait'}` flags : dict A dict of flags, keyed by str name. Values can be Config objects or dicts. When the flag is triggered, The config is loaded as `self.config.update(cfg)`. """ self.clear() if argv is None: argv = self.argv if aliases is None: aliases = self.aliases if flags is None: flags = self.flags # ensure argv is a list of unicode strings: uargv = self._decode_argv(argv) for idx,raw in enumerate(uargv): # strip leading '-' item = raw.lstrip('-') if raw == '--': # don't parse arguments after '--' # this is useful for relaying arguments to scripts, e.g. # ipython -i foo.py --matplotlib=qt -- args after '--' go-to-foo.py self.extra_args.extend(uargv[idx+1:]) break if kv_pattern.match(raw): lhs,rhs = item.split('=',1) # Substitute longnames for aliases. if lhs in aliases: lhs = aliases[lhs] if '.' not in lhs: # probably a mistyped alias, but not technically illegal self.log.warning("Unrecognized alias: '%s', it will probably have no effect.", raw) try: self._exec_config_str(lhs, rhs) except Exception: raise ArgumentError("Invalid argument: '%s'" % raw) elif flag_pattern.match(raw): if item in flags: cfg,help = flags[item] self._load_flag(cfg) else: raise ArgumentError("Unrecognized flag: '%s'"%raw) elif raw.startswith('-'): kv = '--'+item if kv_pattern.match(kv): raise ArgumentError("Invalid argument: '%s', did you mean '%s'?"%(raw, kv)) else: raise ArgumentError("Invalid argument: '%s'"%raw) else: # keep all args that aren't valid in a list, # in case our parent knows what to do with them. self.extra_args.append(item) return self.config class ArgParseConfigLoader(CommandLineConfigLoader): """A loader that uses the argparse module to load from the command line.""" def __init__(self, argv=None, aliases=None, flags=None, log=None, *parser_args, **parser_kw): """Create a config loader for use with argparse. Parameters ---------- argv : optional, list If given, used to read command-line arguments from, otherwise sys.argv[1:] is used. parser_args : tuple A tuple of positional arguments that will be passed to the constructor of :class:`argparse.ArgumentParser`. parser_kw : dict A tuple of keyword arguments that will be passed to the constructor of :class:`argparse.ArgumentParser`. Returns ------- config : Config The resulting Config object. """ super(CommandLineConfigLoader, self).__init__(log=log) self.clear() if argv is None: argv = sys.argv[1:] self.argv = argv self.aliases = aliases or {} self.flags = flags or {} self.parser_args = parser_args self.version = parser_kw.pop("version", None) kwargs = dict(argument_default=argparse.SUPPRESS) kwargs.update(parser_kw) self.parser_kw = kwargs def load_config(self, argv=None, aliases=None, flags=None): """Parse command line arguments and return as a Config object. Parameters ---------- args : optional, list If given, a list with the structure of sys.argv[1:] to parse arguments from. If not given, the instance's self.argv attribute (given at construction time) is used.""" self.clear() if argv is None: argv = self.argv if aliases is None: aliases = self.aliases if flags is None: flags = self.flags self._create_parser(aliases, flags) self._parse_args(argv) self._convert_to_config() return self.config def get_extra_args(self): if hasattr(self, 'extra_args'): return self.extra_args else: return [] def _create_parser(self, aliases=None, flags=None): self.parser = ArgumentParser(*self.parser_args, **self.parser_kw) self._add_arguments(aliases, flags) def _add_arguments(self, aliases=None, flags=None): raise NotImplementedError("subclasses must implement _add_arguments") def _parse_args(self, args): """self.parser->self.parsed_data""" # decode sys.argv to support unicode command-line options enc = DEFAULT_ENCODING uargs = [py3compat.cast_unicode(a, enc) for a in args] self.parsed_data, self.extra_args = self.parser.parse_known_args(uargs) def _convert_to_config(self): """self.parsed_data->self.config""" for k, v in iteritems(vars(self.parsed_data)): exec("self.config.%s = v"%k, locals(), globals()) class KVArgParseConfigLoader(ArgParseConfigLoader): """A config loader that loads aliases and flags with argparse, but will use KVLoader for the rest. This allows better parsing of common args, such as `ipython -c 'print 5'`, but still gets arbitrary config with `ipython --InteractiveShell.use_readline=False`""" def _add_arguments(self, aliases=None, flags=None): self.alias_flags = {} # print aliases, flags if aliases is None: aliases = self.aliases if flags is None: flags = self.flags paa = self.parser.add_argument for key,value in iteritems(aliases): if key in flags: # flags nargs = '?' else: nargs = None if len(key) is 1: paa('-'+key, '--'+key, type=unicode_type, dest=value, nargs=nargs) else: paa('--'+key, type=unicode_type, dest=value, nargs=nargs) for key, (value, help) in iteritems(flags): if key in self.aliases: # self.alias_flags[self.aliases[key]] = value continue if len(key) is 1: paa('-'+key, '--'+key, action='append_const', dest='_flags', const=value) else: paa('--'+key, action='append_const', dest='_flags', const=value) def _convert_to_config(self): """self.parsed_data->self.config, parse unrecognized extra args via KVLoader.""" # remove subconfigs list from namespace before transforming the Namespace if '_flags' in self.parsed_data: subcs = self.parsed_data._flags del self.parsed_data._flags else: subcs = [] for k, v in iteritems(vars(self.parsed_data)): if v is None: # it was a flag that shares the name of an alias subcs.append(self.alias_flags[k]) else: # eval the KV assignment self._exec_config_str(k, v) for subc in subcs: self._load_flag(subc) if self.extra_args: sub_parser = KeyValueConfigLoader(log=self.log) sub_parser.load_config(self.extra_args) self.config.merge(sub_parser.config) self.extra_args = sub_parser.extra_args def load_pyconfig_files(config_files, path): """Load multiple Python config files, merging each of them in turn. Parameters ========== config_files : list of str List of config files names to load and merge into the config. path : unicode The full path to the location of the config files. """ config = Config() for cf in config_files: loader = PyFileConfigLoader(cf, path=path) try: next_config = loader.load_config() except ConfigFileNotFound: pass except: raise else: config.merge(next_config) return config
artistic-2.0
Jigsaw-Code/net-analysis
netanalysis/traffic/data/api_repository.py
1
3175
#!/usr/bin/python # # Copyright 2019 Jigsaw Operations LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """ Library to access Google's traffic data from its Transparency Report """ import datetime import json import ssl import time from urllib.request import urlopen, Request from urllib.parse import urlencode, quote import certifi import pandas as pd from netanalysis.traffic.data import model def _to_timestamp(time_point: datetime.datetime): return time.mktime(time_point.timetuple()) _SSL_CONTEXT = ssl.create_default_context(cafile=certifi.where()) class ApiTrafficRepository(model.TrafficRepository): """TrafficRepository that reads the traffic data from Google's Transparency Report.""" def _query_api(self, endpoint, params=None): query_url = "https://www.google.com/transparencyreport/api/v3/traffic/" + \ quote(endpoint) if params: query_url = query_url + "?" + urlencode(params) try: request = Request(query_url) request.add_header("User-Agent", "Jigsaw-Code/netanalysis") with urlopen(request, context=_SSL_CONTEXT) as response: return json.loads(response.read()[6:].decode("utf8")) except Exception as error: raise Exception("Failed to query url %s" % query_url, error) def list_regions(self): response_proto = self._query_api("regionlist") return sorted([e[0] for e in response_proto[0][1]]) def get_traffic(self, region_code: str, product_id: model.ProductId, start: datetime.datetime = None, end: datetime.datetime = None): DEFAULT_INTERVAL_DAYS = 2 * 365 POINTS_PER_DAY = 48 if not end: end = datetime.datetime.now() if not start: start = end - datetime.timedelta(days=DEFAULT_INTERVAL_DAYS) number_of_days = (end - start).days total_points = int(number_of_days * POINTS_PER_DAY) entries = [] params = [ ("start", int(_to_timestamp(start) * 1000)), ("end", int(_to_timestamp(end) * 1000)), ("width", total_points), ("product", product_id.value), ("region", region_code)] response_proto = self._query_api("fraction", params) entry_list_proto = response_proto[0][1] for entry_proto in entry_list_proto: timestamp = datetime.datetime.utcfromtimestamp( entry_proto[0] / 1000) value = entry_proto[1][0][1] entries.append((timestamp, value / POINTS_PER_DAY / 2)) dates, traffic = zip(*entries) return pd.Series(traffic, index=dates)
apache-2.0
maheshakya/scikit-learn
sklearn/semi_supervised/label_propagation.py
2
15104
# coding=utf8 """ Label propagation in the context of this module refers to a set of semisupervised classification algorithms. In the high level, these algorithms work by forming a fully-connected graph between all points given and solving for the steady-state distribution of labels at each point. These algorithms perform very well in practice. The cost of running can be very expensive, at approximately O(N^3) where N is the number of (labeled and unlabeled) points. The theory (why they perform so well) is motivated by intuitions from random walk algorithms and geometric relationships in the data. For more information see the references below. Model Features -------------- Label clamping: The algorithm tries to learn distributions of labels over the dataset. In the "Hard Clamp" mode, the true ground labels are never allowed to change. They are clamped into position. In the "Soft Clamp" mode, they are allowed some wiggle room, but some alpha of their original value will always be retained. Hard clamp is the same as soft clamping with alpha set to 1. Kernel: A function which projects a vector into some higher dimensional space. This implementation supprots RBF and KNN kernels. Using the RBF kernel generates a dense matrix of size O(N^2). KNN kernel will generate a sparse matrix of size O(k*N) which will run much faster. See the documentation for SVMs for more info on kernels. Examples -------- >>> from sklearn import datasets >>> from sklearn.semi_supervised import LabelPropagation >>> label_prop_model = LabelPropagation() >>> iris = datasets.load_iris() >>> random_unlabeled_points = np.where(np.random.random_integers(0, 1, ... size=len(iris.target))) >>> labels = np.copy(iris.target) >>> labels[random_unlabeled_points] = -1 >>> label_prop_model.fit(iris.data, labels) ... # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS LabelPropagation(...) Notes ----- References: [1] Yoshua Bengio, Olivier Delalleau, Nicolas Le Roux. In Semi-Supervised Learning (2006), pp. 193-216 [2] Olivier Delalleau, Yoshua Bengio, Nicolas Le Roux. Efficient Non-Parametric Function Induction in Semi-Supervised Learning. AISTAT 2005 """ # Authors: Clay Woolam <clay@woolam.org> # Licence: BSD from abc import ABCMeta, abstractmethod from scipy import sparse import numpy as np from ..base import BaseEstimator, ClassifierMixin from ..metrics.pairwise import rbf_kernel from ..utils.graph import graph_laplacian from ..utils.extmath import safe_sparse_dot from ..utils.validation import check_X_y, check_is_fitted from ..externals import six from ..neighbors.unsupervised import NearestNeighbors ### Helper functions def _not_converged(y_truth, y_prediction, tol=1e-3): """basic convergence check""" return np.abs(y_truth - y_prediction).sum() > tol class BaseLabelPropagation(six.with_metaclass(ABCMeta, BaseEstimator, ClassifierMixin)): """Base class for label propagation module. Parameters ---------- kernel : {'knn', 'rbf'} String identifier for kernel function to use. Only 'rbf' and 'knn' kernels are currently supported.. gamma : float Parameter for rbf kernel alpha : float Clamping factor max_iter : float Change maximum number of iterations allowed tol : float Convergence tolerance: threshold to consider the system at steady state """ def __init__(self, kernel='rbf', gamma=20, n_neighbors=7, alpha=1, max_iter=30, tol=1e-3): self.max_iter = max_iter self.tol = tol # kernel parameters self.kernel = kernel self.gamma = gamma self.n_neighbors = n_neighbors # clamping factor self.alpha = alpha def _get_kernel(self, X, y=None): if self.kernel == "rbf": if y is None: return rbf_kernel(X, X, gamma=self.gamma) else: return rbf_kernel(X, y, gamma=self.gamma) elif self.kernel == "knn": if self.nn_fit is None: self.nn_fit = NearestNeighbors(self.n_neighbors).fit(X) if y is None: return self.nn_fit.kneighbors_graph(self.nn_fit._fit_X, self.n_neighbors, mode='connectivity') else: return self.nn_fit.kneighbors(y, return_distance=False) else: raise ValueError("%s is not a valid kernel. Only rbf and knn" " are supported at this time" % self.kernel) @abstractmethod def _build_graph(self): raise NotImplementedError("Graph construction must be implemented" " to fit a label propagation model.") def predict(self, X): """Performs inductive inference across the model. Parameters ---------- X : array_like, shape = [n_samples, n_features] Returns ------- y : array_like, shape = [n_samples] Predictions for input data """ probas = self.predict_proba(X) return self.classes_[np.argmax(probas, axis=1)].ravel() def predict_proba(self, X): """Predict probability for each possible outcome. Compute the probability estimates for each single sample in X and each possible outcome seen during training (categorical distribution). Parameters ---------- X : array_like, shape = [n_samples, n_features] Returns ------- probabilities : array, shape = [n_samples, n_classes] Normalized probability distributions across class labels """ check_is_fitted(self, 'X_') if sparse.isspmatrix(X): X_2d = X else: X_2d = np.atleast_2d(X) weight_matrices = self._get_kernel(self.X_, X_2d) if self.kernel == 'knn': probabilities = [] for weight_matrix in weight_matrices: ine = np.sum(self.label_distributions_[weight_matrix], axis=0) probabilities.append(ine) probabilities = np.array(probabilities) else: weight_matrices = weight_matrices.T probabilities = np.dot(weight_matrices, self.label_distributions_) normalizer = np.atleast_2d(np.sum(probabilities, axis=1)).T probabilities /= normalizer return probabilities def fit(self, X, y): """Fit a semi-supervised label propagation model based All the input data is provided matrix X (labeled and unlabeled) and corresponding label matrix y with a dedicated marker value for unlabeled samples. Parameters ---------- X : array-like, shape = [n_samples, n_features] A {n_samples by n_samples} size matrix will be created from this y : array_like, shape = [n_samples] n_labeled_samples (unlabeled points are marked as -1) All unlabeled samples will be transductively assigned labels Returns ------- self : returns an instance of self. """ X, y = check_X_y(X, y) self.X_ = X # actual graph construction (implementations should override this) graph_matrix = self._build_graph() # label construction # construct a categorical distribution for classification only classes = np.unique(y) classes = (classes[classes != -1]) self.classes_ = classes n_samples, n_classes = len(y), len(classes) y = np.asarray(y) unlabeled = y == -1 clamp_weights = np.ones((n_samples, 1)) clamp_weights[unlabeled, 0] = self.alpha # initialize distributions self.label_distributions_ = np.zeros((n_samples, n_classes)) for label in classes: self.label_distributions_[y == label, classes == label] = 1 y_static = np.copy(self.label_distributions_) if self.alpha > 0.: y_static *= 1 - self.alpha y_static[unlabeled] = 0 l_previous = np.zeros((self.X_.shape[0], n_classes)) remaining_iter = self.max_iter if sparse.isspmatrix(graph_matrix): graph_matrix = graph_matrix.tocsr() while (_not_converged(self.label_distributions_, l_previous, self.tol) and remaining_iter > 1): l_previous = self.label_distributions_ self.label_distributions_ = safe_sparse_dot( graph_matrix, self.label_distributions_) # clamp self.label_distributions_ = np.multiply( clamp_weights, self.label_distributions_) + y_static remaining_iter -= 1 normalizer = np.sum(self.label_distributions_, axis=1)[:, np.newaxis] self.label_distributions_ /= normalizer # set the transduction item transduction = self.classes_[np.argmax(self.label_distributions_, axis=1)] self.transduction_ = transduction.ravel() self.n_iter_ = self.max_iter - remaining_iter return self class LabelPropagation(BaseLabelPropagation): """Label Propagation classifier Parameters ---------- kernel : {'knn', 'rbf'} String identifier for kernel function to use. Only 'rbf' and 'knn' kernels are currently supported.. gamma : float parameter for rbf kernel n_neighbors : integer > 0 parameter for knn kernel alpha : float clamping factor max_iter : float change maximum number of iterations allowed tol : float Convergence tolerance: threshold to consider the system at steady state Attributes ---------- X_ : array, shape = [n_samples, n_features] Input array. classes_ : array, shape = [n_classes] The distinct labels used in classifying instances. label_distributions_ : array, shape = [n_samples, n_classes] Categorical distribution for each item. transduction_ : array, shape = [n_samples] Label assigned to each item via the transduction. n_iter_ : int Number of iterations run. Examples -------- >>> from sklearn import datasets >>> from sklearn.semi_supervised import LabelPropagation >>> label_prop_model = LabelPropagation() >>> iris = datasets.load_iris() >>> random_unlabeled_points = np.where(np.random.random_integers(0, 1, ... size=len(iris.target))) >>> labels = np.copy(iris.target) >>> labels[random_unlabeled_points] = -1 >>> label_prop_model.fit(iris.data, labels) ... # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS LabelPropagation(...) References ---------- Xiaojin Zhu and Zoubin Ghahramani. Learning from labeled and unlabeled data with label propagation. Technical Report CMU-CALD-02-107, Carnegie Mellon University, 2002 http://pages.cs.wisc.edu/~jerryzhu/pub/CMU-CALD-02-107.pdf See Also -------- LabelSpreading : Alternate label propagation strategy more robust to noise """ def _build_graph(self): """Matrix representing a fully connected graph between each sample This basic implementation creates a non-stochastic affinity matrix, so class distributions will exceed 1 (normalization may be desired). """ if self.kernel == 'knn': self.nn_fit = None affinity_matrix = self._get_kernel(self.X_) normalizer = affinity_matrix.sum(axis=0) if sparse.isspmatrix(affinity_matrix): affinity_matrix.data /= np.diag(np.array(normalizer)) else: affinity_matrix /= normalizer[:, np.newaxis] return affinity_matrix class LabelSpreading(BaseLabelPropagation): """LabelSpreading model for semi-supervised learning This model is similar to the basic Label Propgation algorithm, but uses affinity matrix based on the normalized graph Laplacian and soft clamping across the labels. Parameters ---------- kernel : {'knn', 'rbf'} String identifier for kernel function to use. Only 'rbf' and 'knn' kernels are currently supported. gamma : float parameter for rbf kernel n_neighbors : integer > 0 parameter for knn kernel alpha : float clamping factor max_iter : float maximum number of iterations allowed tol : float Convergence tolerance: threshold to consider the system at steady state Attributes ---------- X_ : array, shape = [n_samples, n_features] Input array. classes_ : array, shape = [n_classes] The distinct labels used in classifying instances. label_distributions_ : array, shape = [n_samples, n_classes] Categorical distribution for each item. transduction_ : array, shape = [n_samples] Label assigned to each item via the transduction. n_iter_ : int Number of iterations run. Examples -------- >>> from sklearn import datasets >>> from sklearn.semi_supervised import LabelSpreading >>> label_prop_model = LabelSpreading() >>> iris = datasets.load_iris() >>> random_unlabeled_points = np.where(np.random.random_integers(0, 1, ... size=len(iris.target))) >>> labels = np.copy(iris.target) >>> labels[random_unlabeled_points] = -1 >>> label_prop_model.fit(iris.data, labels) ... # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS LabelSpreading(...) References ---------- Dengyong Zhou, Olivier Bousquet, Thomas Navin Lal, Jason Weston, Bernhard Schoelkopf. Learning with local and global consistency (2004) http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.115.3219 See Also -------- LabelPropagation : Unregularized graph based semi-supervised learning """ def __init__(self, kernel='rbf', gamma=20, n_neighbors=7, alpha=0.2, max_iter=30, tol=1e-3): # this one has different base parameters super(LabelSpreading, self).__init__(kernel=kernel, gamma=gamma, n_neighbors=n_neighbors, alpha=alpha, max_iter=max_iter, tol=tol) def _build_graph(self): """Graph matrix for Label Spreading computes the graph laplacian""" # compute affinity matrix (or gram matrix) if self.kernel == 'knn': self.nn_fit = None n_samples = self.X_.shape[0] affinity_matrix = self._get_kernel(self.X_) laplacian = graph_laplacian(affinity_matrix, normed=True) laplacian = -laplacian if sparse.isspmatrix(laplacian): diag_mask = (laplacian.row == laplacian.col) laplacian.data[diag_mask] = 0.0 else: laplacian.flat[::n_samples + 1] = 0.0 # set diag to 0.0 return laplacian
bsd-3-clause
LennonLab/ScalingMicroBiodiversity
ExtraTests/lognormal/Prestons_a.py
2
2188
from __future__ import division #from bigfloat import BigFloat, sqrt, exp, log, log2, erf, const_pi import numpy as np import math from numpy import log, log2, exp, sqrt,log10 from scipy.optimize import fsolve import scipy.optimize as opt import matplotlib.pyplot as plt from scipy.special import erf import sys pi = math.pi GO = [3.6*(10**28), 10.1*(10**28)] # estimated open ocean bacteria; Whitman et al. 1998 Pm = [2.8*(10**27), 3.0*(10**27)] # estimated Prochlorococcus; Flombaum et al. 2013 Syn = [6.7*(10**26), 7.3*(10**26)] # estimated Synechococcus; Flombaum et al. 2013 Earth = [9.2*(10**29), 31.7*(10**29)] # estimated bacteria on Earth; Kallmeyer et al. 2012 SAR11 = [2.0*(10**28), 2.0*(10**28)] # estimated percent abundance of SAR11; Morris et al. (2002) HGx = 10**14 # estimated bacteria in Human gut; add reference HGy = 0.1169*HGx # estimated most abundant bacteria in Human gut; add reference AvianN = 2.82*10**11 AvianNmax = 3*10**9 AvianS = 10500 def alpha1(a, Nmax, Nt): return (sqrt(pi) * Nmax)/(2.0*a) * erf(log(2.0)/a) - Nt # find alpha def s1(a): return sqrt(pi)/a * exp( (log(2.0)/(2.0*a))**2.0 ) # Using equation 8 def alpha2(a, N, Nmax, Nmin): y = sqrt(pi*Nmin*Nmax)/(2.0*a) * exp((a * log2(sqrt(Nmax/Nmin)))**2.0) y = y * exp((log(2.0)/(2.0*a))**2.0) y = y * erf(a * log2(sqrt(Nmax/Nmin)) - log(2.0)/(2.0*a)) + erf(a * log2(sqrt(Nmax/Nmin)) + log(2.0)/(2.0*a)) y -= N return y # find alpha def s2(a, Nmax, Nmin): return sqrt(pi)/a * exp( (a * log2(sqrt(Nmax/Nmin)))**2) # Using equation 10 def getNmax(N): return 10 ** (1.02*(log10(N)) - 0.71) def empS(N, b=log10(3.92), slope=0.4): # macrobes: b = 0.86, slope = 0.23 return 10 ** (b + slope*(log10(N))) #N = float(AvianN) #Nmax = AvianNmax #Nmin = 1.0 #Nmax = getNmax(AvianN) N = float(max(GO)) Nmax = float(max(Syn)) Nmin = 1.0 #Nmax = getNmax(N) ############################################### Assuming Nmin = 1 guess = 0.099 guess = 0.1019 a = opt.fsolve(alpha2, guess, (N, Nmax, Nmin))[0] print guess, a S2 = s2(a, Nmax, Nmin) print 'S2:','%.3e' % S2 # predicted from lognormal S = empS(N) print 'empS:','%.3e' % S # predicted from scaling
gpl-3.0
solarjoe/numpy
numpy/lib/npyio.py
3
75574
from __future__ import division, absolute_import, print_function import sys import os import re import itertools import warnings import weakref from operator import itemgetter, index as opindex import numpy as np from . import format from ._datasource import DataSource from numpy.core.multiarray import packbits, unpackbits from ._iotools import ( LineSplitter, NameValidator, StringConverter, ConverterError, ConverterLockError, ConversionWarning, _is_string_like, has_nested_fields, flatten_dtype, easy_dtype, _bytes_to_name ) from numpy.compat import ( asbytes, asstr, asbytes_nested, bytes, basestring, unicode, is_pathlib_path ) if sys.version_info[0] >= 3: import pickle else: import cPickle as pickle from future_builtins import map loads = pickle.loads __all__ = [ 'savetxt', 'loadtxt', 'genfromtxt', 'ndfromtxt', 'mafromtxt', 'recfromtxt', 'recfromcsv', 'load', 'loads', 'save', 'savez', 'savez_compressed', 'packbits', 'unpackbits', 'fromregex', 'DataSource' ] class BagObj(object): """ BagObj(obj) Convert attribute look-ups to getitems on the object passed in. Parameters ---------- obj : class instance Object on which attribute look-up is performed. Examples -------- >>> from numpy.lib.npyio import BagObj as BO >>> class BagDemo(object): ... def __getitem__(self, key): # An instance of BagObj(BagDemo) ... # will call this method when any ... # attribute look-up is required ... result = "Doesn't matter what you want, " ... return result + "you're gonna get this" ... >>> demo_obj = BagDemo() >>> bagobj = BO(demo_obj) >>> bagobj.hello_there "Doesn't matter what you want, you're gonna get this" >>> bagobj.I_can_be_anything "Doesn't matter what you want, you're gonna get this" """ def __init__(self, obj): # Use weakref to make NpzFile objects collectable by refcount self._obj = weakref.proxy(obj) def __getattribute__(self, key): try: return object.__getattribute__(self, '_obj')[key] except KeyError: raise AttributeError(key) def __dir__(self): """ Enables dir(bagobj) to list the files in an NpzFile. This also enables tab-completion in an interpreter or IPython. """ return object.__getattribute__(self, '_obj').keys() def zipfile_factory(file, *args, **kwargs): """ Create a ZipFile. Allows for Zip64, and the `file` argument can accept file, str, or pathlib.Path objects. `args` and `kwargs` are passed to the zipfile.ZipFile constructor. """ if is_pathlib_path(file): file = str(file) import zipfile kwargs['allowZip64'] = True return zipfile.ZipFile(file, *args, **kwargs) class NpzFile(object): """ NpzFile(fid) A dictionary-like object with lazy-loading of files in the zipped archive provided on construction. `NpzFile` is used to load files in the NumPy ``.npz`` data archive format. It assumes that files in the archive have a ``.npy`` extension, other files are ignored. The arrays and file strings are lazily loaded on either getitem access using ``obj['key']`` or attribute lookup using ``obj.f.key``. A list of all files (without ``.npy`` extensions) can be obtained with ``obj.files`` and the ZipFile object itself using ``obj.zip``. Attributes ---------- files : list of str List of all files in the archive with a ``.npy`` extension. zip : ZipFile instance The ZipFile object initialized with the zipped archive. f : BagObj instance An object on which attribute can be performed as an alternative to getitem access on the `NpzFile` instance itself. allow_pickle : bool, optional Allow loading pickled data. Default: True pickle_kwargs : dict, optional Additional keyword arguments to pass on to pickle.load. These are only useful when loading object arrays saved on Python 2 when using Python 3. Parameters ---------- fid : file or str The zipped archive to open. This is either a file-like object or a string containing the path to the archive. own_fid : bool, optional Whether NpzFile should close the file handle. Requires that `fid` is a file-like object. Examples -------- >>> from tempfile import TemporaryFile >>> outfile = TemporaryFile() >>> x = np.arange(10) >>> y = np.sin(x) >>> np.savez(outfile, x=x, y=y) >>> outfile.seek(0) >>> npz = np.load(outfile) >>> isinstance(npz, np.lib.io.NpzFile) True >>> npz.files ['y', 'x'] >>> npz['x'] # getitem access array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> npz.f.x # attribute lookup array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) """ def __init__(self, fid, own_fid=False, allow_pickle=True, pickle_kwargs=None): # Import is postponed to here since zipfile depends on gzip, an # optional component of the so-called standard library. _zip = zipfile_factory(fid) self._files = _zip.namelist() self.files = [] self.allow_pickle = allow_pickle self.pickle_kwargs = pickle_kwargs for x in self._files: if x.endswith('.npy'): self.files.append(x[:-4]) else: self.files.append(x) self.zip = _zip self.f = BagObj(self) if own_fid: self.fid = fid else: self.fid = None def __enter__(self): return self def __exit__(self, exc_type, exc_value, traceback): self.close() def close(self): """ Close the file. """ if self.zip is not None: self.zip.close() self.zip = None if self.fid is not None: self.fid.close() self.fid = None self.f = None # break reference cycle def __del__(self): self.close() def __getitem__(self, key): # FIXME: This seems like it will copy strings around # more than is strictly necessary. The zipfile # will read the string and then # the format.read_array will copy the string # to another place in memory. # It would be better if the zipfile could read # (or at least uncompress) the data # directly into the array memory. member = 0 if key in self._files: member = 1 elif key in self.files: member = 1 key += '.npy' if member: bytes = self.zip.open(key) magic = bytes.read(len(format.MAGIC_PREFIX)) bytes.close() if magic == format.MAGIC_PREFIX: bytes = self.zip.open(key) return format.read_array(bytes, allow_pickle=self.allow_pickle, pickle_kwargs=self.pickle_kwargs) else: return self.zip.read(key) else: raise KeyError("%s is not a file in the archive" % key) def __iter__(self): return iter(self.files) def items(self): """ Return a list of tuples, with each tuple (filename, array in file). """ return [(f, self[f]) for f in self.files] def iteritems(self): """Generator that returns tuples (filename, array in file).""" for f in self.files: yield (f, self[f]) def keys(self): """Return files in the archive with a ``.npy`` extension.""" return self.files def iterkeys(self): """Return an iterator over the files in the archive.""" return self.__iter__() def __contains__(self, key): return self.files.__contains__(key) def load(file, mmap_mode=None, allow_pickle=True, fix_imports=True, encoding='ASCII'): """ Load arrays or pickled objects from ``.npy``, ``.npz`` or pickled files. Parameters ---------- file : file-like object, string, or pathlib.Path The file to read. File-like objects must support the ``seek()`` and ``read()`` methods. Pickled files require that the file-like object support the ``readline()`` method as well. mmap_mode : {None, 'r+', 'r', 'w+', 'c'}, optional If not None, then memory-map the file, using the given mode (see `numpy.memmap` for a detailed description of the modes). A memory-mapped array is kept on disk. However, it can be accessed and sliced like any ndarray. Memory mapping is especially useful for accessing small fragments of large files without reading the entire file into memory. allow_pickle : bool, optional Allow loading pickled object arrays stored in npy files. Reasons for disallowing pickles include security, as loading pickled data can execute arbitrary code. If pickles are disallowed, loading object arrays will fail. Default: True fix_imports : bool, optional Only useful when loading Python 2 generated pickled files on Python 3, which includes npy/npz files containing object arrays. If `fix_imports` is True, pickle will try to map the old Python 2 names to the new names used in Python 3. encoding : str, optional What encoding to use when reading Python 2 strings. Only useful when loading Python 2 generated pickled files on Python 3, which includes npy/npz files containing object arrays. Values other than 'latin1', 'ASCII', and 'bytes' are not allowed, as they can corrupt numerical data. Default: 'ASCII' Returns ------- result : array, tuple, dict, etc. Data stored in the file. For ``.npz`` files, the returned instance of NpzFile class must be closed to avoid leaking file descriptors. Raises ------ IOError If the input file does not exist or cannot be read. ValueError The file contains an object array, but allow_pickle=False given. See Also -------- save, savez, savez_compressed, loadtxt memmap : Create a memory-map to an array stored in a file on disk. lib.format.open_memmap : Create or load a memory-mapped ``.npy`` file. Notes ----- - If the file contains pickle data, then whatever object is stored in the pickle is returned. - If the file is a ``.npy`` file, then a single array is returned. - If the file is a ``.npz`` file, then a dictionary-like object is returned, containing ``{filename: array}`` key-value pairs, one for each file in the archive. - If the file is a ``.npz`` file, the returned value supports the context manager protocol in a similar fashion to the open function:: with load('foo.npz') as data: a = data['a'] The underlying file descriptor is closed when exiting the 'with' block. Examples -------- Store data to disk, and load it again: >>> np.save('/tmp/123', np.array([[1, 2, 3], [4, 5, 6]])) >>> np.load('/tmp/123.npy') array([[1, 2, 3], [4, 5, 6]]) Store compressed data to disk, and load it again: >>> a=np.array([[1, 2, 3], [4, 5, 6]]) >>> b=np.array([1, 2]) >>> np.savez('/tmp/123.npz', a=a, b=b) >>> data = np.load('/tmp/123.npz') >>> data['a'] array([[1, 2, 3], [4, 5, 6]]) >>> data['b'] array([1, 2]) >>> data.close() Mem-map the stored array, and then access the second row directly from disk: >>> X = np.load('/tmp/123.npy', mmap_mode='r') >>> X[1, :] memmap([4, 5, 6]) """ own_fid = False if isinstance(file, basestring): fid = open(file, "rb") own_fid = True elif is_pathlib_path(file): fid = file.open("rb") own_fid = True else: fid = file if encoding not in ('ASCII', 'latin1', 'bytes'): # The 'encoding' value for pickle also affects what encoding # the serialized binary data of NumPy arrays is loaded # in. Pickle does not pass on the encoding information to # NumPy. The unpickling code in numpy.core.multiarray is # written to assume that unicode data appearing where binary # should be is in 'latin1'. 'bytes' is also safe, as is 'ASCII'. # # Other encoding values can corrupt binary data, and we # purposefully disallow them. For the same reason, the errors= # argument is not exposed, as values other than 'strict' # result can similarly silently corrupt numerical data. raise ValueError("encoding must be 'ASCII', 'latin1', or 'bytes'") if sys.version_info[0] >= 3: pickle_kwargs = dict(encoding=encoding, fix_imports=fix_imports) else: # Nothing to do on Python 2 pickle_kwargs = {} try: # Code to distinguish from NumPy binary files and pickles. _ZIP_PREFIX = b'PK\x03\x04' N = len(format.MAGIC_PREFIX) magic = fid.read(N) # If the file size is less than N, we need to make sure not # to seek past the beginning of the file fid.seek(-min(N, len(magic)), 1) # back-up if magic.startswith(_ZIP_PREFIX): # zip-file (assume .npz) # Transfer file ownership to NpzFile tmp = own_fid own_fid = False return NpzFile(fid, own_fid=tmp, allow_pickle=allow_pickle, pickle_kwargs=pickle_kwargs) elif magic == format.MAGIC_PREFIX: # .npy file if mmap_mode: return format.open_memmap(file, mode=mmap_mode) else: return format.read_array(fid, allow_pickle=allow_pickle, pickle_kwargs=pickle_kwargs) else: # Try a pickle if not allow_pickle: raise ValueError("allow_pickle=False, but file does not contain " "non-pickled data") try: return pickle.load(fid, **pickle_kwargs) except Exception: raise IOError( "Failed to interpret file %s as a pickle" % repr(file)) finally: if own_fid: fid.close() def save(file, arr, allow_pickle=True, fix_imports=True): """ Save an array to a binary file in NumPy ``.npy`` format. Parameters ---------- file : file, str, or pathlib.Path File or filename to which the data is saved. If file is a file-object, then the filename is unchanged. If file is a string or Path, a ``.npy`` extension will be appended to the file name if it does not already have one. allow_pickle : bool, optional Allow saving object arrays using Python pickles. Reasons for disallowing pickles include security (loading pickled data can execute arbitrary code) and portability (pickled objects may not be loadable on different Python installations, for example if the stored objects require libraries that are not available, and not all pickled data is compatible between Python 2 and Python 3). Default: True fix_imports : bool, optional Only useful in forcing objects in object arrays on Python 3 to be pickled in a Python 2 compatible way. If `fix_imports` is True, pickle will try to map the new Python 3 names to the old module names used in Python 2, so that the pickle data stream is readable with Python 2. arr : array_like Array data to be saved. See Also -------- savez : Save several arrays into a ``.npz`` archive savetxt, load Notes ----- For a description of the ``.npy`` format, see the module docstring of `numpy.lib.format` or the NumPy Enhancement Proposal http://docs.scipy.org/doc/numpy/neps/npy-format.html Examples -------- >>> from tempfile import TemporaryFile >>> outfile = TemporaryFile() >>> x = np.arange(10) >>> np.save(outfile, x) >>> outfile.seek(0) # Only needed here to simulate closing & reopening file >>> np.load(outfile) array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) """ own_fid = False if isinstance(file, basestring): if not file.endswith('.npy'): file = file + '.npy' fid = open(file, "wb") own_fid = True elif is_pathlib_path(file): if not file.name.endswith('.npy'): file = file.parent / (file.name + '.npy') fid = file.open("wb") own_fid = True else: fid = file if sys.version_info[0] >= 3: pickle_kwargs = dict(fix_imports=fix_imports) else: # Nothing to do on Python 2 pickle_kwargs = None try: arr = np.asanyarray(arr) format.write_array(fid, arr, allow_pickle=allow_pickle, pickle_kwargs=pickle_kwargs) finally: if own_fid: fid.close() def savez(file, *args, **kwds): """ Save several arrays into a single file in uncompressed ``.npz`` format. If arguments are passed in with no keywords, the corresponding variable names, in the ``.npz`` file, are 'arr_0', 'arr_1', etc. If keyword arguments are given, the corresponding variable names, in the ``.npz`` file will match the keyword names. Parameters ---------- file : str or file Either the file name (string) or an open file (file-like object) where the data will be saved. If file is a string or a Path, the ``.npz`` extension will be appended to the file name if it is not already there. args : Arguments, optional Arrays to save to the file. Since it is not possible for Python to know the names of the arrays outside `savez`, the arrays will be saved with names "arr_0", "arr_1", and so on. These arguments can be any expression. kwds : Keyword arguments, optional Arrays to save to the file. Arrays will be saved in the file with the keyword names. Returns ------- None See Also -------- save : Save a single array to a binary file in NumPy format. savetxt : Save an array to a file as plain text. savez_compressed : Save several arrays into a compressed ``.npz`` archive Notes ----- The ``.npz`` file format is a zipped archive of files named after the variables they contain. The archive is not compressed and each file in the archive contains one variable in ``.npy`` format. For a description of the ``.npy`` format, see `numpy.lib.format` or the NumPy Enhancement Proposal http://docs.scipy.org/doc/numpy/neps/npy-format.html When opening the saved ``.npz`` file with `load` a `NpzFile` object is returned. This is a dictionary-like object which can be queried for its list of arrays (with the ``.files`` attribute), and for the arrays themselves. Examples -------- >>> from tempfile import TemporaryFile >>> outfile = TemporaryFile() >>> x = np.arange(10) >>> y = np.sin(x) Using `savez` with \\*args, the arrays are saved with default names. >>> np.savez(outfile, x, y) >>> outfile.seek(0) # Only needed here to simulate closing & reopening file >>> npzfile = np.load(outfile) >>> npzfile.files ['arr_1', 'arr_0'] >>> npzfile['arr_0'] array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) Using `savez` with \\**kwds, the arrays are saved with the keyword names. >>> outfile = TemporaryFile() >>> np.savez(outfile, x=x, y=y) >>> outfile.seek(0) >>> npzfile = np.load(outfile) >>> npzfile.files ['y', 'x'] >>> npzfile['x'] array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) """ _savez(file, args, kwds, False) def savez_compressed(file, *args, **kwds): """ Save several arrays into a single file in compressed ``.npz`` format. If keyword arguments are given, then filenames are taken from the keywords. If arguments are passed in with no keywords, then stored file names are arr_0, arr_1, etc. Parameters ---------- file : str or file Either the file name (string) or an open file (file-like object) where the data will be saved. If file is a string or a Path, the ``.npz`` extension will be appended to the file name if it is not already there. args : Arguments, optional Arrays to save to the file. Since it is not possible for Python to know the names of the arrays outside `savez`, the arrays will be saved with names "arr_0", "arr_1", and so on. These arguments can be any expression. kwds : Keyword arguments, optional Arrays to save to the file. Arrays will be saved in the file with the keyword names. Returns ------- None See Also -------- numpy.save : Save a single array to a binary file in NumPy format. numpy.savetxt : Save an array to a file as plain text. numpy.savez : Save several arrays into an uncompressed ``.npz`` file format numpy.load : Load the files created by savez_compressed. Notes ----- The ``.npz`` file format is a zipped archive of files named after the variables they contain. The archive is compressed with ``zipfile.ZIP_DEFLATED`` and each file in the archive contains one variable in ``.npy`` format. For a description of the ``.npy`` format, see `numpy.lib.format` or the NumPy Enhancement Proposal http://docs.scipy.org/doc/numpy/neps/npy-format.html When opening the saved ``.npz`` file with `load` a `NpzFile` object is returned. This is a dictionary-like object which can be queried for its list of arrays (with the ``.files`` attribute), and for the arrays themselves. Examples -------- >>> test_array = np.random.rand(3, 2) >>> test_vector = np.random.rand(4) >>> np.savez_compressed('/tmp/123', a=test_array, b=test_vector) >>> loaded = np.load('/tmp/123.npz') >>> print(np.array_equal(test_array, loaded['a'])) True >>> print(np.array_equal(test_vector, loaded['b'])) True """ _savez(file, args, kwds, True) def _savez(file, args, kwds, compress, allow_pickle=True, pickle_kwargs=None): # Import is postponed to here since zipfile depends on gzip, an optional # component of the so-called standard library. import zipfile # Import deferred for startup time improvement import tempfile if isinstance(file, basestring): if not file.endswith('.npz'): file = file + '.npz' elif is_pathlib_path(file): if not file.name.endswith('.npz'): file = file.parent / (file.name + '.npz') namedict = kwds for i, val in enumerate(args): key = 'arr_%d' % i if key in namedict.keys(): raise ValueError( "Cannot use un-named variables and keyword %s" % key) namedict[key] = val if compress: compression = zipfile.ZIP_DEFLATED else: compression = zipfile.ZIP_STORED zipf = zipfile_factory(file, mode="w", compression=compression) # Stage arrays in a temporary file on disk, before writing to zip. # Since target file might be big enough to exceed capacity of a global # temporary directory, create temp file side-by-side with the target file. file_dir, file_prefix = os.path.split(file) if _is_string_like(file) else (None, 'tmp') fd, tmpfile = tempfile.mkstemp(prefix=file_prefix, dir=file_dir, suffix='-numpy.npy') os.close(fd) try: for key, val in namedict.items(): fname = key + '.npy' fid = open(tmpfile, 'wb') try: format.write_array(fid, np.asanyarray(val), allow_pickle=allow_pickle, pickle_kwargs=pickle_kwargs) fid.close() fid = None zipf.write(tmpfile, arcname=fname) except IOError as exc: raise IOError("Failed to write to %s: %s" % (tmpfile, exc)) finally: if fid: fid.close() finally: os.remove(tmpfile) zipf.close() def _getconv(dtype): """ Find the correct dtype converter. Adapted from matplotlib """ def floatconv(x): x.lower() if b'0x' in x: return float.fromhex(asstr(x)) return float(x) typ = dtype.type if issubclass(typ, np.bool_): return lambda x: bool(int(x)) if issubclass(typ, np.uint64): return np.uint64 if issubclass(typ, np.int64): return np.int64 if issubclass(typ, np.integer): return lambda x: int(float(x)) elif issubclass(typ, np.longdouble): return np.longdouble elif issubclass(typ, np.floating): return floatconv elif issubclass(typ, complex): return lambda x: complex(asstr(x)) elif issubclass(typ, np.bytes_): return asbytes else: return asstr def loadtxt(fname, dtype=float, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0): """ Load data from a text file. Each row in the text file must have the same number of values. Parameters ---------- fname : file, str, or pathlib.Path File, filename, or generator to read. If the filename extension is ``.gz`` or ``.bz2``, the file is first decompressed. Note that generators should return byte strings for Python 3k. dtype : data-type, optional Data-type of the resulting array; default: float. If this is a structured data-type, the resulting array will be 1-dimensional, and each row will be interpreted as an element of the array. In this case, the number of columns used must match the number of fields in the data-type. comments : str or sequence, optional The characters or list of characters used to indicate the start of a comment; default: '#'. delimiter : str, optional The string used to separate values. By default, this is any whitespace. converters : dict, optional A dictionary mapping column number to a function that will convert that column to a float. E.g., if column 0 is a date string: ``converters = {0: datestr2num}``. Converters can also be used to provide a default value for missing data (but see also `genfromtxt`): ``converters = {3: lambda s: float(s.strip() or 0)}``. Default: None. skiprows : int, optional Skip the first `skiprows` lines; default: 0. usecols : int or sequence, optional Which columns to read, with 0 being the first. For example, usecols = (1,4,5) will extract the 2nd, 5th and 6th columns. The default, None, results in all columns being read. .. versionadded:: 1.11.0 Also when a single column has to be read it is possible to use an integer instead of a tuple. E.g ``usecols = 3`` reads the fourth column the same way as `usecols = (3,)`` would. unpack : bool, optional If True, the returned array is transposed, so that arguments may be unpacked using ``x, y, z = loadtxt(...)``. When used with a structured data-type, arrays are returned for each field. Default is False. ndmin : int, optional The returned array will have at least `ndmin` dimensions. Otherwise mono-dimensional axes will be squeezed. Legal values: 0 (default), 1 or 2. .. versionadded:: 1.6.0 Returns ------- out : ndarray Data read from the text file. See Also -------- load, fromstring, fromregex genfromtxt : Load data with missing values handled as specified. scipy.io.loadmat : reads MATLAB data files Notes ----- This function aims to be a fast reader for simply formatted files. The `genfromtxt` function provides more sophisticated handling of, e.g., lines with missing values. .. versionadded:: 1.10.0 The strings produced by the Python float.hex method can be used as input for floats. Examples -------- >>> from io import StringIO # StringIO behaves like a file object >>> c = StringIO("0 1\\n2 3") >>> np.loadtxt(c) array([[ 0., 1.], [ 2., 3.]]) >>> d = StringIO("M 21 72\\nF 35 58") >>> np.loadtxt(d, dtype={'names': ('gender', 'age', 'weight'), ... 'formats': ('S1', 'i4', 'f4')}) array([('M', 21, 72.0), ('F', 35, 58.0)], dtype=[('gender', '|S1'), ('age', '<i4'), ('weight', '<f4')]) >>> c = StringIO("1,0,2\\n3,0,4") >>> x, y = np.loadtxt(c, delimiter=',', usecols=(0, 2), unpack=True) >>> x array([ 1., 3.]) >>> y array([ 2., 4.]) """ # Type conversions for Py3 convenience if comments is not None: if isinstance(comments, (basestring, bytes)): comments = [asbytes(comments)] else: comments = [asbytes(comment) for comment in comments] # Compile regex for comments beforehand comments = (re.escape(comment) for comment in comments) regex_comments = re.compile(b'|'.join(comments)) user_converters = converters if delimiter is not None: delimiter = asbytes(delimiter) if usecols is not None: # Allow usecols to be a single int or a sequence of ints try: usecols_as_list = list(usecols) except TypeError: usecols_as_list = [usecols] for col_idx in usecols_as_list: try: opindex(col_idx) except TypeError as e: e.args = ( "usecols must be an int or a sequence of ints but " "it contains at least one element of type %s" % type(col_idx), ) raise # Fall back to existing code usecols = usecols_as_list fown = False try: if is_pathlib_path(fname): fname = str(fname) if _is_string_like(fname): fown = True if fname.endswith('.gz'): import gzip fh = iter(gzip.GzipFile(fname)) elif fname.endswith('.bz2'): import bz2 fh = iter(bz2.BZ2File(fname)) elif sys.version_info[0] == 2: fh = iter(open(fname, 'U')) else: fh = iter(open(fname)) else: fh = iter(fname) except TypeError: raise ValueError('fname must be a string, file handle, or generator') X = [] # not to be confused with the flatten_dtype we import... def flatten_dtype_internal(dt): """Unpack a structured data-type, and produce re-packing info.""" if dt.names is None: # If the dtype is flattened, return. # If the dtype has a shape, the dtype occurs # in the list more than once. shape = dt.shape if len(shape) == 0: return ([dt.base], None) else: packing = [(shape[-1], list)] if len(shape) > 1: for dim in dt.shape[-2::-1]: packing = [(dim*packing[0][0], packing*dim)] return ([dt.base] * int(np.prod(dt.shape)), packing) else: types = [] packing = [] for field in dt.names: tp, bytes = dt.fields[field] flat_dt, flat_packing = flatten_dtype_internal(tp) types.extend(flat_dt) # Avoid extra nesting for subarrays if tp.ndim > 0: packing.extend(flat_packing) else: packing.append((len(flat_dt), flat_packing)) return (types, packing) def pack_items(items, packing): """Pack items into nested lists based on re-packing info.""" if packing is None: return items[0] elif packing is tuple: return tuple(items) elif packing is list: return list(items) else: start = 0 ret = [] for length, subpacking in packing: ret.append(pack_items(items[start:start+length], subpacking)) start += length return tuple(ret) def split_line(line): """Chop off comments, strip, and split at delimiter. Note that although the file is opened as text, this function returns bytes. """ line = asbytes(line) if comments is not None: line = regex_comments.split(asbytes(line), maxsplit=1)[0] line = line.strip(b'\r\n') if line: return line.split(delimiter) else: return [] try: # Make sure we're dealing with a proper dtype dtype = np.dtype(dtype) defconv = _getconv(dtype) # Skip the first `skiprows` lines for i in range(skiprows): next(fh) # Read until we find a line with some values, and use # it to estimate the number of columns, N. first_vals = None try: while not first_vals: first_line = next(fh) first_vals = split_line(first_line) except StopIteration: # End of lines reached first_line = '' first_vals = [] warnings.warn('loadtxt: Empty input file: "%s"' % fname, stacklevel=2) N = len(usecols or first_vals) dtype_types, packing = flatten_dtype_internal(dtype) if len(dtype_types) > 1: # We're dealing with a structured array, each field of # the dtype matches a column converters = [_getconv(dt) for dt in dtype_types] else: # All fields have the same dtype converters = [defconv for i in range(N)] if N > 1: packing = [(N, tuple)] # By preference, use the converters specified by the user for i, conv in (user_converters or {}).items(): if usecols: try: i = usecols.index(i) except ValueError: # Unused converter specified continue converters[i] = conv # Parse each line, including the first for i, line in enumerate(itertools.chain([first_line], fh)): vals = split_line(line) if len(vals) == 0: continue if usecols: vals = [vals[j] for j in usecols] if len(vals) != N: line_num = i + skiprows + 1 raise ValueError("Wrong number of columns at line %d" % line_num) # Convert each value according to its column and store items = [conv(val) for (conv, val) in zip(converters, vals)] # Then pack it according to the dtype's nesting items = pack_items(items, packing) X.append(items) finally: if fown: fh.close() X = np.array(X, dtype) # Multicolumn data are returned with shape (1, N, M), i.e. # (1, 1, M) for a single row - remove the singleton dimension there if X.ndim == 3 and X.shape[:2] == (1, 1): X.shape = (1, -1) # Verify that the array has at least dimensions `ndmin`. # Check correctness of the values of `ndmin` if ndmin not in [0, 1, 2]: raise ValueError('Illegal value of ndmin keyword: %s' % ndmin) # Tweak the size and shape of the arrays - remove extraneous dimensions if X.ndim > ndmin: X = np.squeeze(X) # and ensure we have the minimum number of dimensions asked for # - has to be in this order for the odd case ndmin=1, X.squeeze().ndim=0 if X.ndim < ndmin: if ndmin == 1: X = np.atleast_1d(X) elif ndmin == 2: X = np.atleast_2d(X).T if unpack: if len(dtype_types) > 1: # For structured arrays, return an array for each field. return [X[field] for field in dtype.names] else: return X.T else: return X def savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='', footer='', comments='# '): """ Save an array to a text file. Parameters ---------- fname : filename or file handle If the filename ends in ``.gz``, the file is automatically saved in compressed gzip format. `loadtxt` understands gzipped files transparently. X : array_like Data to be saved to a text file. fmt : str or sequence of strs, optional A single format (%10.5f), a sequence of formats, or a multi-format string, e.g. 'Iteration %d -- %10.5f', in which case `delimiter` is ignored. For complex `X`, the legal options for `fmt` are: a) a single specifier, `fmt='%.4e'`, resulting in numbers formatted like `' (%s+%sj)' % (fmt, fmt)` b) a full string specifying every real and imaginary part, e.g. `' %.4e %+.4ej %.4e %+.4ej %.4e %+.4ej'` for 3 columns c) a list of specifiers, one per column - in this case, the real and imaginary part must have separate specifiers, e.g. `['%.3e + %.3ej', '(%.15e%+.15ej)']` for 2 columns delimiter : str, optional String or character separating columns. newline : str, optional String or character separating lines. .. versionadded:: 1.5.0 header : str, optional String that will be written at the beginning of the file. .. versionadded:: 1.7.0 footer : str, optional String that will be written at the end of the file. .. versionadded:: 1.7.0 comments : str, optional String that will be prepended to the ``header`` and ``footer`` strings, to mark them as comments. Default: '# ', as expected by e.g. ``numpy.loadtxt``. .. versionadded:: 1.7.0 See Also -------- save : Save an array to a binary file in NumPy ``.npy`` format savez : Save several arrays into an uncompressed ``.npz`` archive savez_compressed : Save several arrays into a compressed ``.npz`` archive Notes ----- Further explanation of the `fmt` parameter (``%[flag]width[.precision]specifier``): flags: ``-`` : left justify ``+`` : Forces to precede result with + or -. ``0`` : Left pad the number with zeros instead of space (see width). width: Minimum number of characters to be printed. The value is not truncated if it has more characters. precision: - For integer specifiers (eg. ``d,i,o,x``), the minimum number of digits. - For ``e, E`` and ``f`` specifiers, the number of digits to print after the decimal point. - For ``g`` and ``G``, the maximum number of significant digits. - For ``s``, the maximum number of characters. specifiers: ``c`` : character ``d`` or ``i`` : signed decimal integer ``e`` or ``E`` : scientific notation with ``e`` or ``E``. ``f`` : decimal floating point ``g,G`` : use the shorter of ``e,E`` or ``f`` ``o`` : signed octal ``s`` : string of characters ``u`` : unsigned decimal integer ``x,X`` : unsigned hexadecimal integer This explanation of ``fmt`` is not complete, for an exhaustive specification see [1]_. References ---------- .. [1] `Format Specification Mini-Language <http://docs.python.org/library/string.html# format-specification-mini-language>`_, Python Documentation. Examples -------- >>> x = y = z = np.arange(0.0,5.0,1.0) >>> np.savetxt('test.out', x, delimiter=',') # X is an array >>> np.savetxt('test.out', (x,y,z)) # x,y,z equal sized 1D arrays >>> np.savetxt('test.out', x, fmt='%1.4e') # use exponential notation """ # Py3 conversions first if isinstance(fmt, bytes): fmt = asstr(fmt) delimiter = asstr(delimiter) own_fh = False if is_pathlib_path(fname): fname = str(fname) if _is_string_like(fname): own_fh = True if fname.endswith('.gz'): import gzip fh = gzip.open(fname, 'wb') else: if sys.version_info[0] >= 3: fh = open(fname, 'wb') else: fh = open(fname, 'w') elif hasattr(fname, 'write'): fh = fname else: raise ValueError('fname must be a string or file handle') try: X = np.asarray(X) # Handle 1-dimensional arrays if X.ndim == 1: # Common case -- 1d array of numbers if X.dtype.names is None: X = np.atleast_2d(X).T ncol = 1 # Complex dtype -- each field indicates a separate column else: ncol = len(X.dtype.descr) else: ncol = X.shape[1] iscomplex_X = np.iscomplexobj(X) # `fmt` can be a string with multiple insertion points or a # list of formats. E.g. '%10.5f\t%10d' or ('%10.5f', '$10d') if type(fmt) in (list, tuple): if len(fmt) != ncol: raise AttributeError('fmt has wrong shape. %s' % str(fmt)) format = asstr(delimiter).join(map(asstr, fmt)) elif isinstance(fmt, str): n_fmt_chars = fmt.count('%') error = ValueError('fmt has wrong number of %% formats: %s' % fmt) if n_fmt_chars == 1: if iscomplex_X: fmt = [' (%s+%sj)' % (fmt, fmt), ] * ncol else: fmt = [fmt, ] * ncol format = delimiter.join(fmt) elif iscomplex_X and n_fmt_chars != (2 * ncol): raise error elif ((not iscomplex_X) and n_fmt_chars != ncol): raise error else: format = fmt else: raise ValueError('invalid fmt: %r' % (fmt,)) if len(header) > 0: header = header.replace('\n', '\n' + comments) fh.write(asbytes(comments + header + newline)) if iscomplex_X: for row in X: row2 = [] for number in row: row2.append(number.real) row2.append(number.imag) fh.write(asbytes(format % tuple(row2) + newline)) else: for row in X: try: fh.write(asbytes(format % tuple(row) + newline)) except TypeError: raise TypeError("Mismatch between array dtype ('%s') and " "format specifier ('%s')" % (str(X.dtype), format)) if len(footer) > 0: footer = footer.replace('\n', '\n' + comments) fh.write(asbytes(comments + footer + newline)) finally: if own_fh: fh.close() def fromregex(file, regexp, dtype): """ Construct an array from a text file, using regular expression parsing. The returned array is always a structured array, and is constructed from all matches of the regular expression in the file. Groups in the regular expression are converted to fields of the structured array. Parameters ---------- file : str or file File name or file object to read. regexp : str or regexp Regular expression used to parse the file. Groups in the regular expression correspond to fields in the dtype. dtype : dtype or list of dtypes Dtype for the structured array. Returns ------- output : ndarray The output array, containing the part of the content of `file` that was matched by `regexp`. `output` is always a structured array. Raises ------ TypeError When `dtype` is not a valid dtype for a structured array. See Also -------- fromstring, loadtxt Notes ----- Dtypes for structured arrays can be specified in several forms, but all forms specify at least the data type and field name. For details see `doc.structured_arrays`. Examples -------- >>> f = open('test.dat', 'w') >>> f.write("1312 foo\\n1534 bar\\n444 qux") >>> f.close() >>> regexp = r"(\\d+)\\s+(...)" # match [digits, whitespace, anything] >>> output = np.fromregex('test.dat', regexp, ... [('num', np.int64), ('key', 'S3')]) >>> output array([(1312L, 'foo'), (1534L, 'bar'), (444L, 'qux')], dtype=[('num', '<i8'), ('key', '|S3')]) >>> output['num'] array([1312, 1534, 444], dtype=int64) """ own_fh = False if not hasattr(file, "read"): file = open(file, 'rb') own_fh = True try: if not hasattr(regexp, 'match'): regexp = re.compile(asbytes(regexp)) if not isinstance(dtype, np.dtype): dtype = np.dtype(dtype) seq = regexp.findall(file.read()) if seq and not isinstance(seq[0], tuple): # Only one group is in the regexp. # Create the new array as a single data-type and then # re-interpret as a single-field structured array. newdtype = np.dtype(dtype[dtype.names[0]]) output = np.array(seq, dtype=newdtype) output.dtype = dtype else: output = np.array(seq, dtype=dtype) return output finally: if own_fh: file.close() #####-------------------------------------------------------------------------- #---- --- ASCII functions --- #####-------------------------------------------------------------------------- def genfromtxt(fname, dtype=float, comments='#', delimiter=None, skip_header=0, skip_footer=0, converters=None, missing_values=None, filling_values=None, usecols=None, names=None, excludelist=None, deletechars=None, replace_space='_', autostrip=False, case_sensitive=True, defaultfmt="f%i", unpack=None, usemask=False, loose=True, invalid_raise=True, max_rows=None): """ Load data from a text file, with missing values handled as specified. Each line past the first `skip_header` lines is split at the `delimiter` character, and characters following the `comments` character are discarded. Parameters ---------- fname : file, str, pathlib.Path, list of str, generator File, filename, list, or generator to read. If the filename extension is `.gz` or `.bz2`, the file is first decompressed. Note that generators must return byte strings in Python 3k. The strings in a list or produced by a generator are treated as lines. dtype : dtype, optional Data type of the resulting array. If None, the dtypes will be determined by the contents of each column, individually. comments : str, optional The character used to indicate the start of a comment. All the characters occurring on a line after a comment are discarded delimiter : str, int, or sequence, optional The string used to separate values. By default, any consecutive whitespaces act as delimiter. An integer or sequence of integers can also be provided as width(s) of each field. skiprows : int, optional `skiprows` was removed in numpy 1.10. Please use `skip_header` instead. skip_header : int, optional The number of lines to skip at the beginning of the file. skip_footer : int, optional The number of lines to skip at the end of the file. converters : variable, optional The set of functions that convert the data of a column to a value. The converters can also be used to provide a default value for missing data: ``converters = {3: lambda s: float(s or 0)}``. missing : variable, optional `missing` was removed in numpy 1.10. Please use `missing_values` instead. missing_values : variable, optional The set of strings corresponding to missing data. filling_values : variable, optional The set of values to be used as default when the data are missing. usecols : sequence, optional Which columns to read, with 0 being the first. For example, ``usecols = (1, 4, 5)`` will extract the 2nd, 5th and 6th columns. names : {None, True, str, sequence}, optional If `names` is True, the field names are read from the first valid line after the first `skip_header` lines. If `names` is a sequence or a single-string of comma-separated names, the names will be used to define the field names in a structured dtype. If `names` is None, the names of the dtype fields will be used, if any. excludelist : sequence, optional A list of names to exclude. This list is appended to the default list ['return','file','print']. Excluded names are appended an underscore: for example, `file` would become `file_`. deletechars : str, optional A string combining invalid characters that must be deleted from the names. defaultfmt : str, optional A format used to define default field names, such as "f%i" or "f_%02i". autostrip : bool, optional Whether to automatically strip white spaces from the variables. replace_space : char, optional Character(s) used in replacement of white spaces in the variables names. By default, use a '_'. case_sensitive : {True, False, 'upper', 'lower'}, optional If True, field names are case sensitive. If False or 'upper', field names are converted to upper case. If 'lower', field names are converted to lower case. unpack : bool, optional If True, the returned array is transposed, so that arguments may be unpacked using ``x, y, z = loadtxt(...)`` usemask : bool, optional If True, return a masked array. If False, return a regular array. loose : bool, optional If True, do not raise errors for invalid values. invalid_raise : bool, optional If True, an exception is raised if an inconsistency is detected in the number of columns. If False, a warning is emitted and the offending lines are skipped. max_rows : int, optional The maximum number of rows to read. Must not be used with skip_footer at the same time. If given, the value must be at least 1. Default is to read the entire file. .. versionadded:: 1.10.0 Returns ------- out : ndarray Data read from the text file. If `usemask` is True, this is a masked array. See Also -------- numpy.loadtxt : equivalent function when no data is missing. Notes ----- * When spaces are used as delimiters, or when no delimiter has been given as input, there should not be any missing data between two fields. * When the variables are named (either by a flexible dtype or with `names`, there must not be any header in the file (else a ValueError exception is raised). * Individual values are not stripped of spaces by default. When using a custom converter, make sure the function does remove spaces. References ---------- .. [1] NumPy User Guide, section `I/O with NumPy <http://docs.scipy.org/doc/numpy/user/basics.io.genfromtxt.html>`_. Examples --------- >>> from io import StringIO >>> import numpy as np Comma delimited file with mixed dtype >>> s = StringIO("1,1.3,abcde") >>> data = np.genfromtxt(s, dtype=[('myint','i8'),('myfloat','f8'), ... ('mystring','S5')], delimiter=",") >>> data array((1, 1.3, 'abcde'), dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', '|S5')]) Using dtype = None >>> s.seek(0) # needed for StringIO example only >>> data = np.genfromtxt(s, dtype=None, ... names = ['myint','myfloat','mystring'], delimiter=",") >>> data array((1, 1.3, 'abcde'), dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', '|S5')]) Specifying dtype and names >>> s.seek(0) >>> data = np.genfromtxt(s, dtype="i8,f8,S5", ... names=['myint','myfloat','mystring'], delimiter=",") >>> data array((1, 1.3, 'abcde'), dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', '|S5')]) An example with fixed-width columns >>> s = StringIO("11.3abcde") >>> data = np.genfromtxt(s, dtype=None, names=['intvar','fltvar','strvar'], ... delimiter=[1,3,5]) >>> data array((1, 1.3, 'abcde'), dtype=[('intvar', '<i8'), ('fltvar', '<f8'), ('strvar', '|S5')]) """ if max_rows is not None: if skip_footer: raise ValueError( "The keywords 'skip_footer' and 'max_rows' can not be " "specified at the same time.") if max_rows < 1: raise ValueError("'max_rows' must be at least 1.") # Py3 data conversions to bytes, for convenience if comments is not None: comments = asbytes(comments) if isinstance(delimiter, unicode): delimiter = asbytes(delimiter) if isinstance(missing_values, (unicode, list, tuple)): missing_values = asbytes_nested(missing_values) # if usemask: from numpy.ma import MaskedArray, make_mask_descr # Check the input dictionary of converters user_converters = converters or {} if not isinstance(user_converters, dict): raise TypeError( "The input argument 'converter' should be a valid dictionary " "(got '%s' instead)" % type(user_converters)) # Initialize the filehandle, the LineSplitter and the NameValidator own_fhd = False try: if is_pathlib_path(fname): fname = str(fname) if isinstance(fname, basestring): if sys.version_info[0] == 2: fhd = iter(np.lib._datasource.open(fname, 'rbU')) else: fhd = iter(np.lib._datasource.open(fname, 'rb')) own_fhd = True else: fhd = iter(fname) except TypeError: raise TypeError( "fname must be a string, filehandle, list of strings, " "or generator. Got %s instead." % type(fname)) split_line = LineSplitter(delimiter=delimiter, comments=comments, autostrip=autostrip)._handyman validate_names = NameValidator(excludelist=excludelist, deletechars=deletechars, case_sensitive=case_sensitive, replace_space=replace_space) # Skip the first `skip_header` rows for i in range(skip_header): next(fhd) # Keep on until we find the first valid values first_values = None try: while not first_values: first_line = next(fhd) if names is True: if comments in first_line: first_line = ( b''.join(first_line.split(comments)[1:])) first_values = split_line(first_line) except StopIteration: # return an empty array if the datafile is empty first_line = b'' first_values = [] warnings.warn('genfromtxt: Empty input file: "%s"' % fname, stacklevel=2) # Should we take the first values as names ? if names is True: fval = first_values[0].strip() if fval in comments: del first_values[0] # Check the columns to use: make sure `usecols` is a list if usecols is not None: try: usecols = [_.strip() for _ in usecols.split(",")] except AttributeError: try: usecols = list(usecols) except TypeError: usecols = [usecols, ] nbcols = len(usecols or first_values) # Check the names and overwrite the dtype.names if needed if names is True: names = validate_names([_bytes_to_name(_.strip()) for _ in first_values]) first_line = b'' elif _is_string_like(names): names = validate_names([_.strip() for _ in names.split(',')]) elif names: names = validate_names(names) # Get the dtype if dtype is not None: dtype = easy_dtype(dtype, defaultfmt=defaultfmt, names=names, excludelist=excludelist, deletechars=deletechars, case_sensitive=case_sensitive, replace_space=replace_space) # Make sure the names is a list (for 2.5) if names is not None: names = list(names) if usecols: for (i, current) in enumerate(usecols): # if usecols is a list of names, convert to a list of indices if _is_string_like(current): usecols[i] = names.index(current) elif current < 0: usecols[i] = current + len(first_values) # If the dtype is not None, make sure we update it if (dtype is not None) and (len(dtype) > nbcols): descr = dtype.descr dtype = np.dtype([descr[_] for _ in usecols]) names = list(dtype.names) # If `names` is not None, update the names elif (names is not None) and (len(names) > nbcols): names = [names[_] for _ in usecols] elif (names is not None) and (dtype is not None): names = list(dtype.names) # Process the missing values ............................... # Rename missing_values for convenience user_missing_values = missing_values or () # Define the list of missing_values (one column: one list) missing_values = [list([b'']) for _ in range(nbcols)] # We have a dictionary: process it field by field if isinstance(user_missing_values, dict): # Loop on the items for (key, val) in user_missing_values.items(): # Is the key a string ? if _is_string_like(key): try: # Transform it into an integer key = names.index(key) except ValueError: # We couldn't find it: the name must have been dropped continue # Redefine the key as needed if it's a column number if usecols: try: key = usecols.index(key) except ValueError: pass # Transform the value as a list of string if isinstance(val, (list, tuple)): val = [str(_) for _ in val] else: val = [str(val), ] # Add the value(s) to the current list of missing if key is None: # None acts as default for miss in missing_values: miss.extend(val) else: missing_values[key].extend(val) # We have a sequence : each item matches a column elif isinstance(user_missing_values, (list, tuple)): for (value, entry) in zip(user_missing_values, missing_values): value = str(value) if value not in entry: entry.append(value) # We have a string : apply it to all entries elif isinstance(user_missing_values, bytes): user_value = user_missing_values.split(b",") for entry in missing_values: entry.extend(user_value) # We have something else: apply it to all entries else: for entry in missing_values: entry.extend([str(user_missing_values)]) # Process the filling_values ............................... # Rename the input for convenience user_filling_values = filling_values if user_filling_values is None: user_filling_values = [] # Define the default filling_values = [None] * nbcols # We have a dictionary : update each entry individually if isinstance(user_filling_values, dict): for (key, val) in user_filling_values.items(): if _is_string_like(key): try: # Transform it into an integer key = names.index(key) except ValueError: # We couldn't find it: the name must have been dropped, continue # Redefine the key if it's a column number and usecols is defined if usecols: try: key = usecols.index(key) except ValueError: pass # Add the value to the list filling_values[key] = val # We have a sequence : update on a one-to-one basis elif isinstance(user_filling_values, (list, tuple)): n = len(user_filling_values) if (n <= nbcols): filling_values[:n] = user_filling_values else: filling_values = user_filling_values[:nbcols] # We have something else : use it for all entries else: filling_values = [user_filling_values] * nbcols # Initialize the converters ................................ if dtype is None: # Note: we can't use a [...]*nbcols, as we would have 3 times the same # ... converter, instead of 3 different converters. converters = [StringConverter(None, missing_values=miss, default=fill) for (miss, fill) in zip(missing_values, filling_values)] else: dtype_flat = flatten_dtype(dtype, flatten_base=True) # Initialize the converters if len(dtype_flat) > 1: # Flexible type : get a converter from each dtype zipit = zip(dtype_flat, missing_values, filling_values) converters = [StringConverter(dt, locked=True, missing_values=miss, default=fill) for (dt, miss, fill) in zipit] else: # Set to a default converter (but w/ different missing values) zipit = zip(missing_values, filling_values) converters = [StringConverter(dtype, locked=True, missing_values=miss, default=fill) for (miss, fill) in zipit] # Update the converters to use the user-defined ones uc_update = [] for (j, conv) in user_converters.items(): # If the converter is specified by column names, use the index instead if _is_string_like(j): try: j = names.index(j) i = j except ValueError: continue elif usecols: try: i = usecols.index(j) except ValueError: # Unused converter specified continue else: i = j # Find the value to test - first_line is not filtered by usecols: if len(first_line): testing_value = first_values[j] else: testing_value = None converters[i].update(conv, locked=True, testing_value=testing_value, default=filling_values[i], missing_values=missing_values[i],) uc_update.append((i, conv)) # Make sure we have the corrected keys in user_converters... user_converters.update(uc_update) # Fixme: possible error as following variable never used. #miss_chars = [_.missing_values for _ in converters] # Initialize the output lists ... # ... rows rows = [] append_to_rows = rows.append # ... masks if usemask: masks = [] append_to_masks = masks.append # ... invalid invalid = [] append_to_invalid = invalid.append # Parse each line for (i, line) in enumerate(itertools.chain([first_line, ], fhd)): values = split_line(line) nbvalues = len(values) # Skip an empty line if nbvalues == 0: continue if usecols: # Select only the columns we need try: values = [values[_] for _ in usecols] except IndexError: append_to_invalid((i + skip_header + 1, nbvalues)) continue elif nbvalues != nbcols: append_to_invalid((i + skip_header + 1, nbvalues)) continue # Store the values append_to_rows(tuple(values)) if usemask: append_to_masks(tuple([v.strip() in m for (v, m) in zip(values, missing_values)])) if len(rows) == max_rows: break if own_fhd: fhd.close() # Upgrade the converters (if needed) if dtype is None: for (i, converter) in enumerate(converters): current_column = [itemgetter(i)(_m) for _m in rows] try: converter.iterupgrade(current_column) except ConverterLockError: errmsg = "Converter #%i is locked and cannot be upgraded: " % i current_column = map(itemgetter(i), rows) for (j, value) in enumerate(current_column): try: converter.upgrade(value) except (ConverterError, ValueError): errmsg += "(occurred line #%i for value '%s')" errmsg %= (j + 1 + skip_header, value) raise ConverterError(errmsg) # Check that we don't have invalid values nbinvalid = len(invalid) if nbinvalid > 0: nbrows = len(rows) + nbinvalid - skip_footer # Construct the error message template = " Line #%%i (got %%i columns instead of %i)" % nbcols if skip_footer > 0: nbinvalid_skipped = len([_ for _ in invalid if _[0] > nbrows + skip_header]) invalid = invalid[:nbinvalid - nbinvalid_skipped] skip_footer -= nbinvalid_skipped # # nbrows -= skip_footer # errmsg = [template % (i, nb) # for (i, nb) in invalid if i < nbrows] # else: errmsg = [template % (i, nb) for (i, nb) in invalid] if len(errmsg): errmsg.insert(0, "Some errors were detected !") errmsg = "\n".join(errmsg) # Raise an exception ? if invalid_raise: raise ValueError(errmsg) # Issue a warning ? else: warnings.warn(errmsg, ConversionWarning, stacklevel=2) # Strip the last skip_footer data if skip_footer > 0: rows = rows[:-skip_footer] if usemask: masks = masks[:-skip_footer] # Convert each value according to the converter: # We want to modify the list in place to avoid creating a new one... if loose: rows = list( zip(*[[conv._loose_call(_r) for _r in map(itemgetter(i), rows)] for (i, conv) in enumerate(converters)])) else: rows = list( zip(*[[conv._strict_call(_r) for _r in map(itemgetter(i), rows)] for (i, conv) in enumerate(converters)])) # Reset the dtype data = rows if dtype is None: # Get the dtypes from the types of the converters column_types = [conv.type for conv in converters] # Find the columns with strings... strcolidx = [i for (i, v) in enumerate(column_types) if v in (type('S'), np.string_)] # ... and take the largest number of chars. for i in strcolidx: column_types[i] = "|S%i" % max(len(row[i]) for row in data) # if names is None: # If the dtype is uniform, don't define names, else use '' base = set([c.type for c in converters if c._checked]) if len(base) == 1: (ddtype, mdtype) = (list(base)[0], bool) else: ddtype = [(defaultfmt % i, dt) for (i, dt) in enumerate(column_types)] if usemask: mdtype = [(defaultfmt % i, bool) for (i, dt) in enumerate(column_types)] else: ddtype = list(zip(names, column_types)) mdtype = list(zip(names, [bool] * len(column_types))) output = np.array(data, dtype=ddtype) if usemask: outputmask = np.array(masks, dtype=mdtype) else: # Overwrite the initial dtype names if needed if names and dtype.names: dtype.names = names # Case 1. We have a structured type if len(dtype_flat) > 1: # Nested dtype, eg [('a', int), ('b', [('b0', int), ('b1', 'f4')])] # First, create the array using a flattened dtype: # [('a', int), ('b1', int), ('b2', float)] # Then, view the array using the specified dtype. if 'O' in (_.char for _ in dtype_flat): if has_nested_fields(dtype): raise NotImplementedError( "Nested fields involving objects are not supported...") else: output = np.array(data, dtype=dtype) else: rows = np.array(data, dtype=[('', _) for _ in dtype_flat]) output = rows.view(dtype) # Now, process the rowmasks the same way if usemask: rowmasks = np.array( masks, dtype=np.dtype([('', bool) for t in dtype_flat])) # Construct the new dtype mdtype = make_mask_descr(dtype) outputmask = rowmasks.view(mdtype) # Case #2. We have a basic dtype else: # We used some user-defined converters if user_converters: ishomogeneous = True descr = [] for i, ttype in enumerate([conv.type for conv in converters]): # Keep the dtype of the current converter if i in user_converters: ishomogeneous &= (ttype == dtype.type) if ttype == np.string_: ttype = "|S%i" % max(len(row[i]) for row in data) descr.append(('', ttype)) else: descr.append(('', dtype)) # So we changed the dtype ? if not ishomogeneous: # We have more than one field if len(descr) > 1: dtype = np.dtype(descr) # We have only one field: drop the name if not needed. else: dtype = np.dtype(ttype) # output = np.array(data, dtype) if usemask: if dtype.names: mdtype = [(_, bool) for _ in dtype.names] else: mdtype = bool outputmask = np.array(masks, dtype=mdtype) # Try to take care of the missing data we missed names = output.dtype.names if usemask and names: for (name, conv) in zip(names or (), converters): missing_values = [conv(_) for _ in conv.missing_values if _ != b''] for mval in missing_values: outputmask[name] |= (output[name] == mval) # Construct the final array if usemask: output = output.view(MaskedArray) output._mask = outputmask if unpack: return output.squeeze().T return output.squeeze() def ndfromtxt(fname, **kwargs): """ Load ASCII data stored in a file and return it as a single array. Parameters ---------- fname, kwargs : For a description of input parameters, see `genfromtxt`. See Also -------- numpy.genfromtxt : generic function. """ kwargs['usemask'] = False return genfromtxt(fname, **kwargs) def mafromtxt(fname, **kwargs): """ Load ASCII data stored in a text file and return a masked array. Parameters ---------- fname, kwargs : For a description of input parameters, see `genfromtxt`. See Also -------- numpy.genfromtxt : generic function to load ASCII data. """ kwargs['usemask'] = True return genfromtxt(fname, **kwargs) def recfromtxt(fname, **kwargs): """ Load ASCII data from a file and return it in a record array. If ``usemask=False`` a standard `recarray` is returned, if ``usemask=True`` a MaskedRecords array is returned. Parameters ---------- fname, kwargs : For a description of input parameters, see `genfromtxt`. See Also -------- numpy.genfromtxt : generic function Notes ----- By default, `dtype` is None, which means that the data-type of the output array will be determined from the data. """ kwargs.setdefault("dtype", None) usemask = kwargs.get('usemask', False) output = genfromtxt(fname, **kwargs) if usemask: from numpy.ma.mrecords import MaskedRecords output = output.view(MaskedRecords) else: output = output.view(np.recarray) return output def recfromcsv(fname, **kwargs): """ Load ASCII data stored in a comma-separated file. The returned array is a record array (if ``usemask=False``, see `recarray`) or a masked record array (if ``usemask=True``, see `ma.mrecords.MaskedRecords`). Parameters ---------- fname, kwargs : For a description of input parameters, see `genfromtxt`. See Also -------- numpy.genfromtxt : generic function to load ASCII data. Notes ----- By default, `dtype` is None, which means that the data-type of the output array will be determined from the data. """ # Set default kwargs for genfromtxt as relevant to csv import. kwargs.setdefault("case_sensitive", "lower") kwargs.setdefault("names", True) kwargs.setdefault("delimiter", ",") kwargs.setdefault("dtype", None) output = genfromtxt(fname, **kwargs) usemask = kwargs.get("usemask", False) if usemask: from numpy.ma.mrecords import MaskedRecords output = output.view(MaskedRecords) else: output = output.view(np.recarray) return output
bsd-3-clause
zfrenchee/pandas
pandas/core/api.py
1
3146
# pylint: disable=W0614,W0401,W0611 # flake8: noqa import numpy as np from pandas.core.algorithms import factorize, unique, value_counts from pandas.core.dtypes.missing import isna, isnull, notna, notnull from pandas.core.categorical import Categorical from pandas.core.groupby import Grouper from pandas.io.formats.format import set_eng_float_format from pandas.core.index import (Index, CategoricalIndex, Int64Index, UInt64Index, RangeIndex, Float64Index, MultiIndex, IntervalIndex, TimedeltaIndex, DatetimeIndex, PeriodIndex, NaT) from pandas.core.indexes.period import Period, period_range, pnow from pandas.core.indexes.timedeltas import Timedelta, timedelta_range from pandas.core.indexes.datetimes import Timestamp, date_range, bdate_range from pandas.core.indexes.interval import Interval, interval_range from pandas.core.series import Series from pandas.core.frame import DataFrame from pandas.core.panel import Panel, WidePanel from pandas.core.panel4d import Panel4D # TODO: Remove import when statsmodels updates #18264 from pandas.core.reshape.reshape import get_dummies from pandas.core.indexing import IndexSlice from pandas.core.tools.numeric import to_numeric from pandas.tseries.offsets import DateOffset from pandas.core.tools.datetimes import to_datetime from pandas.core.tools.timedeltas import to_timedelta # see gh-14094. from pandas.util._depr_module import _DeprecatedModule _removals = ['day', 'bday', 'businessDay', 'cday', 'customBusinessDay', 'customBusinessMonthEnd', 'customBusinessMonthBegin', 'monthEnd', 'yearEnd', 'yearBegin', 'bmonthEnd', 'bmonthBegin', 'cbmonthEnd', 'cbmonthBegin', 'bquarterEnd', 'quarterEnd', 'byearEnd', 'week'] datetools = _DeprecatedModule(deprmod='pandas.core.datetools', removals=_removals) from pandas.core.config import (get_option, set_option, reset_option, describe_option, option_context, options) # deprecation, xref #13790 def match(*args, **kwargs): import warnings warnings.warn("pd.match() is deprecated and will be removed " "in a future version", FutureWarning, stacklevel=2) from pandas.core.algorithms import match return match(*args, **kwargs) def groupby(*args, **kwargs): import warnings warnings.warn("pd.groupby() is deprecated and will be removed; " "Please use the Series.groupby() or " "DataFrame.groupby() methods", FutureWarning, stacklevel=2) return args[0].groupby(*args[1:], **kwargs) # Deprecation: xref gh-16747 class TimeGrouper(object): def __new__(cls, *args, **kwargs): from pandas.core.resample import TimeGrouper import warnings warnings.warn("pd.TimeGrouper is deprecated and will be removed; " "Please use pd.Grouper(freq=...)", FutureWarning, stacklevel=2) return TimeGrouper(*args, **kwargs)
bsd-3-clause
woodscn/scipy
scipy/special/c_misc/struve_convergence.py
76
3725
""" Convergence regions of the expansions used in ``struve.c`` Note that for v >> z both functions tend rapidly to 0, and for v << -z, they tend to infinity. The floating-point functions over/underflow in the lower left and right corners of the figure. Figure legend ============= Red region Power series is close (1e-12) to the mpmath result Blue region Asymptotic series is close to the mpmath result Green region Bessel series is close to the mpmath result Dotted colored lines Boundaries of the regions Solid colored lines Boundaries estimated by the routine itself. These will be used for determining which of the results to use. Black dashed line The line z = 0.7*|v| + 12 """ from __future__ import absolute_import, division, print_function import numpy as np import matplotlib.pyplot as plt try: import mpmath except: from sympy import mpmath def err_metric(a, b, atol=1e-290): m = abs(a - b) / (atol + abs(b)) m[np.isinf(b) & (a == b)] = 0 return m def do_plot(is_h=True): from scipy.special._ufuncs import \ _struve_power_series, _struve_asymp_large_z, _struve_bessel_series vs = np.linspace(-1000, 1000, 91) zs = np.sort(np.r_[1e-5, 1.0, np.linspace(0, 700, 91)[1:]]) rp = _struve_power_series(vs[:,None], zs[None,:], is_h) ra = _struve_asymp_large_z(vs[:,None], zs[None,:], is_h) rb = _struve_bessel_series(vs[:,None], zs[None,:], is_h) mpmath.mp.dps = 50 if is_h: sh = lambda v, z: float(mpmath.struveh(mpmath.mpf(v), mpmath.mpf(z))) else: sh = lambda v, z: float(mpmath.struvel(mpmath.mpf(v), mpmath.mpf(z))) ex = np.vectorize(sh, otypes='d')(vs[:,None], zs[None,:]) err_a = err_metric(ra[0], ex) + 1e-300 err_p = err_metric(rp[0], ex) + 1e-300 err_b = err_metric(rb[0], ex) + 1e-300 err_est_a = abs(ra[1]/ra[0]) err_est_p = abs(rp[1]/rp[0]) err_est_b = abs(rb[1]/rb[0]) z_cutoff = 0.7*abs(vs) + 12 levels = [-1000, -12] plt.cla() plt.hold(1) plt.contourf(vs, zs, np.log10(err_p).T, levels=levels, colors=['r', 'r'], alpha=0.1) plt.contourf(vs, zs, np.log10(err_a).T, levels=levels, colors=['b', 'b'], alpha=0.1) plt.contourf(vs, zs, np.log10(err_b).T, levels=levels, colors=['g', 'g'], alpha=0.1) plt.contour(vs, zs, np.log10(err_p).T, levels=levels, colors=['r', 'r'], linestyles=[':', ':']) plt.contour(vs, zs, np.log10(err_a).T, levels=levels, colors=['b', 'b'], linestyles=[':', ':']) plt.contour(vs, zs, np.log10(err_b).T, levels=levels, colors=['g', 'g'], linestyles=[':', ':']) lp = plt.contour(vs, zs, np.log10(err_est_p).T, levels=levels, colors=['r', 'r'], linestyles=['-', '-']) la = plt.contour(vs, zs, np.log10(err_est_a).T, levels=levels, colors=['b', 'b'], linestyles=['-', '-']) lb = plt.contour(vs, zs, np.log10(err_est_b).T, levels=levels, colors=['g', 'g'], linestyles=['-', '-']) plt.clabel(lp, fmt={-1000: 'P', -12: 'P'}) plt.clabel(la, fmt={-1000: 'A', -12: 'A'}) plt.clabel(lb, fmt={-1000: 'B', -12: 'B'}) plt.plot(vs, z_cutoff, 'k--') plt.xlim(vs.min(), vs.max()) plt.ylim(zs.min(), zs.max()) plt.xlabel('v') plt.ylabel('z') def main(): plt.clf() plt.subplot(121) do_plot(True) plt.title('Struve H') plt.subplot(122) do_plot(False) plt.title('Struve L') plt.savefig('struve_convergence.png') plt.show() if __name__ == "__main__": import os import sys if '--main' in sys.argv: main() else: import subprocess subprocess.call([sys.executable, os.path.join('..', '..', '..', 'runtests.py'), '-g', '--python', __file__, '--main'])
bsd-3-clause
ashhher3/seaborn
seaborn/tests/test_axisgrid.py
11
41072
import warnings import numpy as np import pandas as pd from scipy import stats import matplotlib as mpl import matplotlib.pyplot as plt from distutils.version import LooseVersion import nose.tools as nt import numpy.testing as npt from numpy.testing.decorators import skipif import pandas.util.testing as tm from . import PlotTestCase from .. import axisgrid as ag from .. import rcmod from ..palettes import color_palette from ..distributions import kdeplot from ..categorical import pointplot from ..linearmodels import pairplot from ..utils import categorical_order rs = np.random.RandomState(0) old_matplotlib = LooseVersion(mpl.__version__) < "1.4" class TestFacetGrid(PlotTestCase): df = pd.DataFrame(dict(x=rs.normal(size=60), y=rs.gamma(4, size=60), a=np.repeat(list("abc"), 20), b=np.tile(list("mn"), 30), c=np.tile(list("tuv"), 20), d=np.tile(list("abcdefghij"), 6))) def test_self_data(self): g = ag.FacetGrid(self.df) nt.assert_is(g.data, self.df) def test_self_fig(self): g = ag.FacetGrid(self.df) nt.assert_is_instance(g.fig, plt.Figure) def test_self_axes(self): g = ag.FacetGrid(self.df, row="a", col="b", hue="c") for ax in g.axes.flat: nt.assert_is_instance(ax, plt.Axes) def test_axes_array_size(self): g1 = ag.FacetGrid(self.df) nt.assert_equal(g1.axes.shape, (1, 1)) g2 = ag.FacetGrid(self.df, row="a") nt.assert_equal(g2.axes.shape, (3, 1)) g3 = ag.FacetGrid(self.df, col="b") nt.assert_equal(g3.axes.shape, (1, 2)) g4 = ag.FacetGrid(self.df, hue="c") nt.assert_equal(g4.axes.shape, (1, 1)) g5 = ag.FacetGrid(self.df, row="a", col="b", hue="c") nt.assert_equal(g5.axes.shape, (3, 2)) for ax in g5.axes.flat: nt.assert_is_instance(ax, plt.Axes) def test_single_axes(self): g1 = ag.FacetGrid(self.df) nt.assert_is_instance(g1.ax, plt.Axes) g2 = ag.FacetGrid(self.df, row="a") with nt.assert_raises(AttributeError): g2.ax g3 = ag.FacetGrid(self.df, col="a") with nt.assert_raises(AttributeError): g3.ax g4 = ag.FacetGrid(self.df, col="a", row="b") with nt.assert_raises(AttributeError): g4.ax def test_col_wrap(self): g = ag.FacetGrid(self.df, col="d") nt.assert_equal(g.axes.shape, (1, 10)) nt.assert_is(g.facet_axis(0, 8), g.axes[0, 8]) g_wrap = ag.FacetGrid(self.df, col="d", col_wrap=4) nt.assert_equal(g_wrap.axes.shape, (10,)) nt.assert_is(g_wrap.facet_axis(0, 8), g_wrap.axes[8]) nt.assert_equal(g_wrap._ncol, 4) nt.assert_equal(g_wrap._nrow, 3) with nt.assert_raises(ValueError): g = ag.FacetGrid(self.df, row="b", col="d", col_wrap=4) df = self.df.copy() df.loc[df.d == "j"] = np.nan g_missing = ag.FacetGrid(df, col="d") nt.assert_equal(g_missing.axes.shape, (1, 9)) g_missing_wrap = ag.FacetGrid(df, col="d", col_wrap=4) nt.assert_equal(g_missing_wrap.axes.shape, (9,)) def test_normal_axes(self): null = np.empty(0, object).flat g = ag.FacetGrid(self.df) npt.assert_array_equal(g._bottom_axes, g.axes.flat) npt.assert_array_equal(g._not_bottom_axes, null) npt.assert_array_equal(g._left_axes, g.axes.flat) npt.assert_array_equal(g._not_left_axes, null) npt.assert_array_equal(g._inner_axes, null) g = ag.FacetGrid(self.df, col="c") npt.assert_array_equal(g._bottom_axes, g.axes.flat) npt.assert_array_equal(g._not_bottom_axes, null) npt.assert_array_equal(g._left_axes, g.axes[:, 0].flat) npt.assert_array_equal(g._not_left_axes, g.axes[:, 1:].flat) npt.assert_array_equal(g._inner_axes, null) g = ag.FacetGrid(self.df, row="c") npt.assert_array_equal(g._bottom_axes, g.axes[-1, :].flat) npt.assert_array_equal(g._not_bottom_axes, g.axes[:-1, :].flat) npt.assert_array_equal(g._left_axes, g.axes.flat) npt.assert_array_equal(g._not_left_axes, null) npt.assert_array_equal(g._inner_axes, null) g = ag.FacetGrid(self.df, col="a", row="c") npt.assert_array_equal(g._bottom_axes, g.axes[-1, :].flat) npt.assert_array_equal(g._not_bottom_axes, g.axes[:-1, :].flat) npt.assert_array_equal(g._left_axes, g.axes[:, 0].flat) npt.assert_array_equal(g._not_left_axes, g.axes[:, 1:].flat) npt.assert_array_equal(g._inner_axes, g.axes[:-1, 1:].flat) def test_wrapped_axes(self): null = np.empty(0, object).flat g = ag.FacetGrid(self.df, col="a", col_wrap=2) npt.assert_array_equal(g._bottom_axes, g.axes[np.array([1, 2])].flat) npt.assert_array_equal(g._not_bottom_axes, g.axes[:1].flat) npt.assert_array_equal(g._left_axes, g.axes[np.array([0, 2])].flat) npt.assert_array_equal(g._not_left_axes, g.axes[np.array([1])].flat) npt.assert_array_equal(g._inner_axes, null) def test_figure_size(self): g = ag.FacetGrid(self.df, row="a", col="b") npt.assert_array_equal(g.fig.get_size_inches(), (6, 9)) g = ag.FacetGrid(self.df, row="a", col="b", size=6) npt.assert_array_equal(g.fig.get_size_inches(), (12, 18)) g = ag.FacetGrid(self.df, col="c", size=4, aspect=.5) npt.assert_array_equal(g.fig.get_size_inches(), (6, 4)) def test_figure_size_with_legend(self): g1 = ag.FacetGrid(self.df, col="a", hue="c", size=4, aspect=.5) npt.assert_array_equal(g1.fig.get_size_inches(), (6, 4)) g1.add_legend() nt.assert_greater(g1.fig.get_size_inches()[0], 6) g2 = ag.FacetGrid(self.df, col="a", hue="c", size=4, aspect=.5, legend_out=False) npt.assert_array_equal(g2.fig.get_size_inches(), (6, 4)) g2.add_legend() npt.assert_array_equal(g2.fig.get_size_inches(), (6, 4)) def test_legend_data(self): g1 = ag.FacetGrid(self.df, hue="a") g1.map(plt.plot, "x", "y") g1.add_legend() palette = color_palette(n_colors=3) nt.assert_equal(g1._legend.get_title().get_text(), "a") a_levels = sorted(self.df.a.unique()) lines = g1._legend.get_lines() nt.assert_equal(len(lines), len(a_levels)) for line, hue in zip(lines, palette): nt.assert_equal(line.get_color(), hue) labels = g1._legend.get_texts() nt.assert_equal(len(labels), len(a_levels)) for label, level in zip(labels, a_levels): nt.assert_equal(label.get_text(), level) def test_legend_data_missing_level(self): g1 = ag.FacetGrid(self.df, hue="a", hue_order=list("azbc")) g1.map(plt.plot, "x", "y") g1.add_legend() b, g, r, p = color_palette(n_colors=4) palette = [b, r, p] nt.assert_equal(g1._legend.get_title().get_text(), "a") a_levels = sorted(self.df.a.unique()) lines = g1._legend.get_lines() nt.assert_equal(len(lines), len(a_levels)) for line, hue in zip(lines, palette): nt.assert_equal(line.get_color(), hue) labels = g1._legend.get_texts() nt.assert_equal(len(labels), 4) for label, level in zip(labels, list("azbc")): nt.assert_equal(label.get_text(), level) def test_get_boolean_legend_data(self): self.df["b_bool"] = self.df.b == "m" g1 = ag.FacetGrid(self.df, hue="b_bool") g1.map(plt.plot, "x", "y") g1.add_legend() palette = color_palette(n_colors=2) nt.assert_equal(g1._legend.get_title().get_text(), "b_bool") b_levels = list(map(str, categorical_order(self.df.b_bool))) lines = g1._legend.get_lines() nt.assert_equal(len(lines), len(b_levels)) for line, hue in zip(lines, palette): nt.assert_equal(line.get_color(), hue) labels = g1._legend.get_texts() nt.assert_equal(len(labels), len(b_levels)) for label, level in zip(labels, b_levels): nt.assert_equal(label.get_text(), level) def test_legend_options(self): g1 = ag.FacetGrid(self.df, hue="b") g1.map(plt.plot, "x", "y") g1.add_legend() def test_legendout_with_colwrap(self): g = ag.FacetGrid(self.df, col="d", hue='b', col_wrap=4, legend_out=False) g.map(plt.plot, "x", "y", linewidth=3) g.add_legend() def test_subplot_kws(self): g = ag.FacetGrid(self.df, subplot_kws=dict(axisbg="blue")) for ax in g.axes.flat: nt.assert_equal(ax.get_axis_bgcolor(), "blue") @skipif(old_matplotlib) def test_gridspec_kws(self): ratios = [3, 1, 2] sizes = [0.46, 0.15, 0.31] gskws = dict(width_ratios=ratios, height_ratios=ratios) g = ag.FacetGrid(self.df, col='c', row='a', gridspec_kws=gskws) # clear out all ticks for ax in g.axes.flat: ax.set_xticks([]) ax.set_yticks([]) g.fig.tight_layout() widths, heights = np.meshgrid(sizes, sizes) for n, ax in enumerate(g.axes.flat): npt.assert_almost_equal( ax.get_position().width, widths.flatten()[n], decimal=2 ) npt.assert_almost_equal( ax.get_position().height, heights.flatten()[n], decimal=2 ) @skipif(old_matplotlib) def test_gridspec_kws_col_wrap(self): ratios = [3, 1, 2, 1, 1] sizes = [0.46, 0.15, 0.31] gskws = dict(width_ratios=ratios) with warnings.catch_warnings(): warnings.resetwarnings() warnings.simplefilter("always") npt.assert_warns(UserWarning, ag.FacetGrid, self.df, col='d', col_wrap=5, gridspec_kws=gskws) @skipif(not old_matplotlib) def test_gridsic_kws_old_mpl(self): ratios = [3, 1, 2] sizes = [0.46, 0.15, 0.31] gskws = dict(width_ratios=ratios, height_ratios=ratios) with warnings.catch_warnings(): warnings.resetwarnings() warnings.simplefilter("always") npt.assert_warns(UserWarning, ag.FacetGrid, self.df, col='c', row='a', gridspec_kws=gskws) def test_data_generator(self): g = ag.FacetGrid(self.df, row="a") d = list(g.facet_data()) nt.assert_equal(len(d), 3) tup, data = d[0] nt.assert_equal(tup, (0, 0, 0)) nt.assert_true((data["a"] == "a").all()) tup, data = d[1] nt.assert_equal(tup, (1, 0, 0)) nt.assert_true((data["a"] == "b").all()) g = ag.FacetGrid(self.df, row="a", col="b") d = list(g.facet_data()) nt.assert_equal(len(d), 6) tup, data = d[0] nt.assert_equal(tup, (0, 0, 0)) nt.assert_true((data["a"] == "a").all()) nt.assert_true((data["b"] == "m").all()) tup, data = d[1] nt.assert_equal(tup, (0, 1, 0)) nt.assert_true((data["a"] == "a").all()) nt.assert_true((data["b"] == "n").all()) tup, data = d[2] nt.assert_equal(tup, (1, 0, 0)) nt.assert_true((data["a"] == "b").all()) nt.assert_true((data["b"] == "m").all()) g = ag.FacetGrid(self.df, hue="c") d = list(g.facet_data()) nt.assert_equal(len(d), 3) tup, data = d[1] nt.assert_equal(tup, (0, 0, 1)) nt.assert_true((data["c"] == "u").all()) def test_map(self): g = ag.FacetGrid(self.df, row="a", col="b", hue="c") g.map(plt.plot, "x", "y", linewidth=3) lines = g.axes[0, 0].lines nt.assert_equal(len(lines), 3) line1, _, _ = lines nt.assert_equal(line1.get_linewidth(), 3) x, y = line1.get_data() mask = (self.df.a == "a") & (self.df.b == "m") & (self.df.c == "t") npt.assert_array_equal(x, self.df.x[mask]) npt.assert_array_equal(y, self.df.y[mask]) def test_map_dataframe(self): g = ag.FacetGrid(self.df, row="a", col="b", hue="c") plot = lambda x, y, data=None, **kws: plt.plot(data[x], data[y], **kws) g.map_dataframe(plot, "x", "y", linestyle="--") lines = g.axes[0, 0].lines nt.assert_equal(len(lines), 3) line1, _, _ = lines nt.assert_equal(line1.get_linestyle(), "--") x, y = line1.get_data() mask = (self.df.a == "a") & (self.df.b == "m") & (self.df.c == "t") npt.assert_array_equal(x, self.df.x[mask]) npt.assert_array_equal(y, self.df.y[mask]) def test_set(self): g = ag.FacetGrid(self.df, row="a", col="b") xlim = (-2, 5) ylim = (3, 6) xticks = [-2, 0, 3, 5] yticks = [3, 4.5, 6] g.set(xlim=xlim, ylim=ylim, xticks=xticks, yticks=yticks) for ax in g.axes.flat: npt.assert_array_equal(ax.get_xlim(), xlim) npt.assert_array_equal(ax.get_ylim(), ylim) npt.assert_array_equal(ax.get_xticks(), xticks) npt.assert_array_equal(ax.get_yticks(), yticks) def test_set_titles(self): g = ag.FacetGrid(self.df, row="a", col="b") g.map(plt.plot, "x", "y") # Test the default titles nt.assert_equal(g.axes[0, 0].get_title(), "a = a | b = m") nt.assert_equal(g.axes[0, 1].get_title(), "a = a | b = n") nt.assert_equal(g.axes[1, 0].get_title(), "a = b | b = m") # Test a provided title g.set_titles("{row_var} == {row_name} \/ {col_var} == {col_name}") nt.assert_equal(g.axes[0, 0].get_title(), "a == a \/ b == m") nt.assert_equal(g.axes[0, 1].get_title(), "a == a \/ b == n") nt.assert_equal(g.axes[1, 0].get_title(), "a == b \/ b == m") # Test a single row g = ag.FacetGrid(self.df, col="b") g.map(plt.plot, "x", "y") # Test the default titles nt.assert_equal(g.axes[0, 0].get_title(), "b = m") nt.assert_equal(g.axes[0, 1].get_title(), "b = n") # test with dropna=False g = ag.FacetGrid(self.df, col="b", hue="b", dropna=False) g.map(plt.plot, 'x', 'y') def test_set_titles_margin_titles(self): g = ag.FacetGrid(self.df, row="a", col="b", margin_titles=True) g.map(plt.plot, "x", "y") # Test the default titles nt.assert_equal(g.axes[0, 0].get_title(), "b = m") nt.assert_equal(g.axes[0, 1].get_title(), "b = n") nt.assert_equal(g.axes[1, 0].get_title(), "") # Test the row "titles" nt.assert_equal(g.axes[0, 1].texts[0].get_text(), "a = a") nt.assert_equal(g.axes[1, 1].texts[0].get_text(), "a = b") # Test a provided title g.set_titles(col_template="{col_var} == {col_name}") nt.assert_equal(g.axes[0, 0].get_title(), "b == m") nt.assert_equal(g.axes[0, 1].get_title(), "b == n") nt.assert_equal(g.axes[1, 0].get_title(), "") def test_set_ticklabels(self): g = ag.FacetGrid(self.df, row="a", col="b") g.map(plt.plot, "x", "y") xlab = [l.get_text() + "h" for l in g.axes[1, 0].get_xticklabels()] ylab = [l.get_text() for l in g.axes[1, 0].get_yticklabels()] g.set_xticklabels(xlab) g.set_yticklabels(rotation=90) got_x = [l.get_text() + "h" for l in g.axes[1, 1].get_xticklabels()] got_y = [l.get_text() for l in g.axes[0, 0].get_yticklabels()] npt.assert_array_equal(got_x, xlab) npt.assert_array_equal(got_y, ylab) x, y = np.arange(10), np.arange(10) df = pd.DataFrame(np.c_[x, y], columns=["x", "y"]) g = ag.FacetGrid(df).map(pointplot, "x", "y") g.set_xticklabels(step=2) got_x = [int(l.get_text()) for l in g.axes[0, 0].get_xticklabels()] npt.assert_array_equal(x[::2], got_x) g = ag.FacetGrid(self.df, col="d", col_wrap=5) g.map(plt.plot, "x", "y") g.set_xticklabels(rotation=45) g.set_yticklabels(rotation=75) for ax in g._bottom_axes: for l in ax.get_xticklabels(): nt.assert_equal(l.get_rotation(), 45) for ax in g._left_axes: for l in ax.get_yticklabels(): nt.assert_equal(l.get_rotation(), 75) def test_set_axis_labels(self): g = ag.FacetGrid(self.df, row="a", col="b") g.map(plt.plot, "x", "y") xlab = 'xx' ylab = 'yy' g.set_axis_labels(xlab, ylab) got_x = [ax.get_xlabel() for ax in g.axes[-1, :]] got_y = [ax.get_ylabel() for ax in g.axes[:, 0]] npt.assert_array_equal(got_x, xlab) npt.assert_array_equal(got_y, ylab) def test_axis_lims(self): g = ag.FacetGrid(self.df, row="a", col="b", xlim=(0, 4), ylim=(-2, 3)) nt.assert_equal(g.axes[0, 0].get_xlim(), (0, 4)) nt.assert_equal(g.axes[0, 0].get_ylim(), (-2, 3)) def test_data_orders(self): g = ag.FacetGrid(self.df, row="a", col="b", hue="c") nt.assert_equal(g.row_names, list("abc")) nt.assert_equal(g.col_names, list("mn")) nt.assert_equal(g.hue_names, list("tuv")) nt.assert_equal(g.axes.shape, (3, 2)) g = ag.FacetGrid(self.df, row="a", col="b", hue="c", row_order=list("bca"), col_order=list("nm"), hue_order=list("vtu")) nt.assert_equal(g.row_names, list("bca")) nt.assert_equal(g.col_names, list("nm")) nt.assert_equal(g.hue_names, list("vtu")) nt.assert_equal(g.axes.shape, (3, 2)) g = ag.FacetGrid(self.df, row="a", col="b", hue="c", row_order=list("bcda"), col_order=list("nom"), hue_order=list("qvtu")) nt.assert_equal(g.row_names, list("bcda")) nt.assert_equal(g.col_names, list("nom")) nt.assert_equal(g.hue_names, list("qvtu")) nt.assert_equal(g.axes.shape, (4, 3)) def test_palette(self): rcmod.set() g = ag.FacetGrid(self.df, hue="c") nt.assert_equal(g._colors, color_palette(n_colors=3)) g = ag.FacetGrid(self.df, hue="d") nt.assert_equal(g._colors, color_palette("husl", 10)) g = ag.FacetGrid(self.df, hue="c", palette="Set2") nt.assert_equal(g._colors, color_palette("Set2", 3)) dict_pal = dict(t="red", u="green", v="blue") list_pal = color_palette(["red", "green", "blue"], 3) g = ag.FacetGrid(self.df, hue="c", palette=dict_pal) nt.assert_equal(g._colors, list_pal) list_pal = color_palette(["green", "blue", "red"], 3) g = ag.FacetGrid(self.df, hue="c", hue_order=list("uvt"), palette=dict_pal) nt.assert_equal(g._colors, list_pal) def test_hue_kws(self): kws = dict(marker=["o", "s", "D"]) g = ag.FacetGrid(self.df, hue="c", hue_kws=kws) g.map(plt.plot, "x", "y") for line, marker in zip(g.axes[0, 0].lines, kws["marker"]): nt.assert_equal(line.get_marker(), marker) def test_dropna(self): df = self.df.copy() hasna = pd.Series(np.tile(np.arange(6), 10), dtype=np.float) hasna[hasna == 5] = np.nan df["hasna"] = hasna g = ag.FacetGrid(df, dropna=False, row="hasna") nt.assert_equal(g._not_na.sum(), 60) g = ag.FacetGrid(df, dropna=True, row="hasna") nt.assert_equal(g._not_na.sum(), 50) class TestPairGrid(PlotTestCase): rs = np.random.RandomState(sum(map(ord, "PairGrid"))) df = pd.DataFrame(dict(x=rs.normal(size=80), y=rs.randint(0, 4, size=(80)), z=rs.gamma(3, size=80), a=np.repeat(list("abcd"), 20), b=np.repeat(list("abcdefgh"), 10))) def test_self_data(self): g = ag.PairGrid(self.df) nt.assert_is(g.data, self.df) def test_ignore_datelike_data(self): df = self.df.copy() df['date'] = pd.date_range('2010-01-01', periods=len(df), freq='d') result = ag.PairGrid(self.df).data expected = df.drop('date', axis=1) tm.assert_frame_equal(result, expected) def test_self_fig(self): g = ag.PairGrid(self.df) nt.assert_is_instance(g.fig, plt.Figure) def test_self_axes(self): g = ag.PairGrid(self.df) for ax in g.axes.flat: nt.assert_is_instance(ax, plt.Axes) def test_default_axes(self): g = ag.PairGrid(self.df) nt.assert_equal(g.axes.shape, (3, 3)) nt.assert_equal(g.x_vars, ["x", "y", "z"]) nt.assert_equal(g.y_vars, ["x", "y", "z"]) nt.assert_true(g.square_grid) def test_specific_square_axes(self): vars = ["z", "x"] g = ag.PairGrid(self.df, vars=vars) nt.assert_equal(g.axes.shape, (len(vars), len(vars))) nt.assert_equal(g.x_vars, vars) nt.assert_equal(g.y_vars, vars) nt.assert_true(g.square_grid) def test_specific_nonsquare_axes(self): x_vars = ["x", "y"] y_vars = ["z", "y", "x"] g = ag.PairGrid(self.df, x_vars=x_vars, y_vars=y_vars) nt.assert_equal(g.axes.shape, (len(y_vars), len(x_vars))) nt.assert_equal(g.x_vars, x_vars) nt.assert_equal(g.y_vars, y_vars) nt.assert_true(not g.square_grid) x_vars = ["x", "y"] y_vars = "z" g = ag.PairGrid(self.df, x_vars=x_vars, y_vars=y_vars) nt.assert_equal(g.axes.shape, (len(y_vars), len(x_vars))) nt.assert_equal(g.x_vars, list(x_vars)) nt.assert_equal(g.y_vars, list(y_vars)) nt.assert_true(not g.square_grid) def test_specific_square_axes_with_array(self): vars = np.array(["z", "x"]) g = ag.PairGrid(self.df, vars=vars) nt.assert_equal(g.axes.shape, (len(vars), len(vars))) nt.assert_equal(g.x_vars, list(vars)) nt.assert_equal(g.y_vars, list(vars)) nt.assert_true(g.square_grid) def test_specific_nonsquare_axes_with_array(self): x_vars = np.array(["x", "y"]) y_vars = np.array(["z", "y", "x"]) g = ag.PairGrid(self.df, x_vars=x_vars, y_vars=y_vars) nt.assert_equal(g.axes.shape, (len(y_vars), len(x_vars))) nt.assert_equal(g.x_vars, list(x_vars)) nt.assert_equal(g.y_vars, list(y_vars)) nt.assert_true(not g.square_grid) def test_size(self): g1 = ag.PairGrid(self.df, size=3) npt.assert_array_equal(g1.fig.get_size_inches(), (9, 9)) g2 = ag.PairGrid(self.df, size=4, aspect=.5) npt.assert_array_equal(g2.fig.get_size_inches(), (6, 12)) g3 = ag.PairGrid(self.df, y_vars=["z"], x_vars=["x", "y"], size=2, aspect=2) npt.assert_array_equal(g3.fig.get_size_inches(), (8, 2)) def test_map(self): vars = ["x", "y", "z"] g1 = ag.PairGrid(self.df) g1.map(plt.scatter) for i, axes_i in enumerate(g1.axes): for j, ax in enumerate(axes_i): x_in = self.df[vars[j]] y_in = self.df[vars[i]] x_out, y_out = ax.collections[0].get_offsets().T npt.assert_array_equal(x_in, x_out) npt.assert_array_equal(y_in, y_out) g2 = ag.PairGrid(self.df, "a") g2.map(plt.scatter) for i, axes_i in enumerate(g2.axes): for j, ax in enumerate(axes_i): x_in = self.df[vars[j]] y_in = self.df[vars[i]] for k, k_level in enumerate("abcd"): x_in_k = x_in[self.df.a == k_level] y_in_k = y_in[self.df.a == k_level] x_out, y_out = ax.collections[k].get_offsets().T npt.assert_array_equal(x_in_k, x_out) npt.assert_array_equal(y_in_k, y_out) def test_map_nonsquare(self): x_vars = ["x"] y_vars = ["y", "z"] g = ag.PairGrid(self.df, x_vars=x_vars, y_vars=y_vars) g.map(plt.scatter) x_in = self.df.x for i, i_var in enumerate(y_vars): ax = g.axes[i, 0] y_in = self.df[i_var] x_out, y_out = ax.collections[0].get_offsets().T npt.assert_array_equal(x_in, x_out) npt.assert_array_equal(y_in, y_out) def test_map_lower(self): vars = ["x", "y", "z"] g = ag.PairGrid(self.df) g.map_lower(plt.scatter) for i, j in zip(*np.tril_indices_from(g.axes, -1)): ax = g.axes[i, j] x_in = self.df[vars[j]] y_in = self.df[vars[i]] x_out, y_out = ax.collections[0].get_offsets().T npt.assert_array_equal(x_in, x_out) npt.assert_array_equal(y_in, y_out) for i, j in zip(*np.triu_indices_from(g.axes)): ax = g.axes[i, j] nt.assert_equal(len(ax.collections), 0) def test_map_upper(self): vars = ["x", "y", "z"] g = ag.PairGrid(self.df) g.map_upper(plt.scatter) for i, j in zip(*np.triu_indices_from(g.axes, 1)): ax = g.axes[i, j] x_in = self.df[vars[j]] y_in = self.df[vars[i]] x_out, y_out = ax.collections[0].get_offsets().T npt.assert_array_equal(x_in, x_out) npt.assert_array_equal(y_in, y_out) for i, j in zip(*np.tril_indices_from(g.axes)): ax = g.axes[i, j] nt.assert_equal(len(ax.collections), 0) @skipif(old_matplotlib) def test_map_diag(self): g1 = ag.PairGrid(self.df) g1.map_diag(plt.hist) for ax in g1.diag_axes: nt.assert_equal(len(ax.patches), 10) g2 = ag.PairGrid(self.df) g2.map_diag(plt.hist, bins=15) for ax in g2.diag_axes: nt.assert_equal(len(ax.patches), 15) g3 = ag.PairGrid(self.df, hue="a") g3.map_diag(plt.hist) for ax in g3.diag_axes: nt.assert_equal(len(ax.patches), 40) @skipif(old_matplotlib) def test_map_diag_and_offdiag(self): vars = ["x", "y", "z"] g = ag.PairGrid(self.df) g.map_offdiag(plt.scatter) g.map_diag(plt.hist) for ax in g.diag_axes: nt.assert_equal(len(ax.patches), 10) for i, j in zip(*np.triu_indices_from(g.axes, 1)): ax = g.axes[i, j] x_in = self.df[vars[j]] y_in = self.df[vars[i]] x_out, y_out = ax.collections[0].get_offsets().T npt.assert_array_equal(x_in, x_out) npt.assert_array_equal(y_in, y_out) for i, j in zip(*np.tril_indices_from(g.axes, -1)): ax = g.axes[i, j] x_in = self.df[vars[j]] y_in = self.df[vars[i]] x_out, y_out = ax.collections[0].get_offsets().T npt.assert_array_equal(x_in, x_out) npt.assert_array_equal(y_in, y_out) for i, j in zip(*np.diag_indices_from(g.axes)): ax = g.axes[i, j] nt.assert_equal(len(ax.collections), 0) def test_palette(self): rcmod.set() g = ag.PairGrid(self.df, hue="a") nt.assert_equal(g.palette, color_palette(n_colors=4)) g = ag.PairGrid(self.df, hue="b") nt.assert_equal(g.palette, color_palette("husl", 8)) g = ag.PairGrid(self.df, hue="a", palette="Set2") nt.assert_equal(g.palette, color_palette("Set2", 4)) dict_pal = dict(a="red", b="green", c="blue", d="purple") list_pal = color_palette(["red", "green", "blue", "purple"], 4) g = ag.PairGrid(self.df, hue="a", palette=dict_pal) nt.assert_equal(g.palette, list_pal) list_pal = color_palette(["purple", "blue", "red", "green"], 4) g = ag.PairGrid(self.df, hue="a", hue_order=list("dcab"), palette=dict_pal) nt.assert_equal(g.palette, list_pal) def test_hue_kws(self): kws = dict(marker=["o", "s", "d", "+"]) g = ag.PairGrid(self.df, hue="a", hue_kws=kws) g.map(plt.plot) for line, marker in zip(g.axes[0, 0].lines, kws["marker"]): nt.assert_equal(line.get_marker(), marker) g = ag.PairGrid(self.df, hue="a", hue_kws=kws, hue_order=list("dcab")) g.map(plt.plot) for line, marker in zip(g.axes[0, 0].lines, kws["marker"]): nt.assert_equal(line.get_marker(), marker) @skipif(old_matplotlib) def test_hue_order(self): order = list("dcab") g = ag.PairGrid(self.df, hue="a", hue_order=order) g.map(plt.plot) for line, level in zip(g.axes[1, 0].lines, order): x, y = line.get_xydata().T npt.assert_array_equal(x, self.df.loc[self.df.a == level, "x"]) npt.assert_array_equal(y, self.df.loc[self.df.a == level, "y"]) plt.close("all") g = ag.PairGrid(self.df, hue="a", hue_order=order) g.map_diag(plt.plot) for line, level in zip(g.axes[0, 0].lines, order): x, y = line.get_xydata().T npt.assert_array_equal(x, self.df.loc[self.df.a == level, "x"]) npt.assert_array_equal(y, self.df.loc[self.df.a == level, "x"]) plt.close("all") g = ag.PairGrid(self.df, hue="a", hue_order=order) g.map_lower(plt.plot) for line, level in zip(g.axes[1, 0].lines, order): x, y = line.get_xydata().T npt.assert_array_equal(x, self.df.loc[self.df.a == level, "x"]) npt.assert_array_equal(y, self.df.loc[self.df.a == level, "y"]) plt.close("all") g = ag.PairGrid(self.df, hue="a", hue_order=order) g.map_upper(plt.plot) for line, level in zip(g.axes[0, 1].lines, order): x, y = line.get_xydata().T npt.assert_array_equal(x, self.df.loc[self.df.a == level, "y"]) npt.assert_array_equal(y, self.df.loc[self.df.a == level, "x"]) plt.close("all") @skipif(old_matplotlib) def test_hue_order_missing_level(self): order = list("dcaeb") g = ag.PairGrid(self.df, hue="a", hue_order=order) g.map(plt.plot) for line, level in zip(g.axes[1, 0].lines, order): x, y = line.get_xydata().T npt.assert_array_equal(x, self.df.loc[self.df.a == level, "x"]) npt.assert_array_equal(y, self.df.loc[self.df.a == level, "y"]) plt.close("all") g = ag.PairGrid(self.df, hue="a", hue_order=order) g.map_diag(plt.plot) for line, level in zip(g.axes[0, 0].lines, order): x, y = line.get_xydata().T npt.assert_array_equal(x, self.df.loc[self.df.a == level, "x"]) npt.assert_array_equal(y, self.df.loc[self.df.a == level, "x"]) plt.close("all") g = ag.PairGrid(self.df, hue="a", hue_order=order) g.map_lower(plt.plot) for line, level in zip(g.axes[1, 0].lines, order): x, y = line.get_xydata().T npt.assert_array_equal(x, self.df.loc[self.df.a == level, "x"]) npt.assert_array_equal(y, self.df.loc[self.df.a == level, "y"]) plt.close("all") g = ag.PairGrid(self.df, hue="a", hue_order=order) g.map_upper(plt.plot) for line, level in zip(g.axes[0, 1].lines, order): x, y = line.get_xydata().T npt.assert_array_equal(x, self.df.loc[self.df.a == level, "y"]) npt.assert_array_equal(y, self.df.loc[self.df.a == level, "x"]) plt.close("all") def test_nondefault_index(self): df = self.df.copy().set_index("b") vars = ["x", "y", "z"] g1 = ag.PairGrid(df) g1.map(plt.scatter) for i, axes_i in enumerate(g1.axes): for j, ax in enumerate(axes_i): x_in = self.df[vars[j]] y_in = self.df[vars[i]] x_out, y_out = ax.collections[0].get_offsets().T npt.assert_array_equal(x_in, x_out) npt.assert_array_equal(y_in, y_out) g2 = ag.PairGrid(df, "a") g2.map(plt.scatter) for i, axes_i in enumerate(g2.axes): for j, ax in enumerate(axes_i): x_in = self.df[vars[j]] y_in = self.df[vars[i]] for k, k_level in enumerate("abcd"): x_in_k = x_in[self.df.a == k_level] y_in_k = y_in[self.df.a == k_level] x_out, y_out = ax.collections[k].get_offsets().T npt.assert_array_equal(x_in_k, x_out) npt.assert_array_equal(y_in_k, y_out) @skipif(old_matplotlib) def test_pairplot(self): vars = ["x", "y", "z"] g = pairplot(self.df) for ax in g.diag_axes: nt.assert_equal(len(ax.patches), 10) for i, j in zip(*np.triu_indices_from(g.axes, 1)): ax = g.axes[i, j] x_in = self.df[vars[j]] y_in = self.df[vars[i]] x_out, y_out = ax.collections[0].get_offsets().T npt.assert_array_equal(x_in, x_out) npt.assert_array_equal(y_in, y_out) for i, j in zip(*np.tril_indices_from(g.axes, -1)): ax = g.axes[i, j] x_in = self.df[vars[j]] y_in = self.df[vars[i]] x_out, y_out = ax.collections[0].get_offsets().T npt.assert_array_equal(x_in, x_out) npt.assert_array_equal(y_in, y_out) for i, j in zip(*np.diag_indices_from(g.axes)): ax = g.axes[i, j] nt.assert_equal(len(ax.collections), 0) @skipif(old_matplotlib) def test_pairplot_reg(self): vars = ["x", "y", "z"] g = pairplot(self.df, kind="reg") for ax in g.diag_axes: nt.assert_equal(len(ax.patches), 10) for i, j in zip(*np.triu_indices_from(g.axes, 1)): ax = g.axes[i, j] x_in = self.df[vars[j]] y_in = self.df[vars[i]] x_out, y_out = ax.collections[0].get_offsets().T npt.assert_array_equal(x_in, x_out) npt.assert_array_equal(y_in, y_out) nt.assert_equal(len(ax.lines), 1) nt.assert_equal(len(ax.collections), 2) for i, j in zip(*np.tril_indices_from(g.axes, -1)): ax = g.axes[i, j] x_in = self.df[vars[j]] y_in = self.df[vars[i]] x_out, y_out = ax.collections[0].get_offsets().T npt.assert_array_equal(x_in, x_out) npt.assert_array_equal(y_in, y_out) nt.assert_equal(len(ax.lines), 1) nt.assert_equal(len(ax.collections), 2) for i, j in zip(*np.diag_indices_from(g.axes)): ax = g.axes[i, j] nt.assert_equal(len(ax.collections), 0) @skipif(old_matplotlib) def test_pairplot_kde(self): vars = ["x", "y", "z"] g = pairplot(self.df, diag_kind="kde") for ax in g.diag_axes: nt.assert_equal(len(ax.lines), 1) for i, j in zip(*np.triu_indices_from(g.axes, 1)): ax = g.axes[i, j] x_in = self.df[vars[j]] y_in = self.df[vars[i]] x_out, y_out = ax.collections[0].get_offsets().T npt.assert_array_equal(x_in, x_out) npt.assert_array_equal(y_in, y_out) for i, j in zip(*np.tril_indices_from(g.axes, -1)): ax = g.axes[i, j] x_in = self.df[vars[j]] y_in = self.df[vars[i]] x_out, y_out = ax.collections[0].get_offsets().T npt.assert_array_equal(x_in, x_out) npt.assert_array_equal(y_in, y_out) for i, j in zip(*np.diag_indices_from(g.axes)): ax = g.axes[i, j] nt.assert_equal(len(ax.collections), 0) @skipif(old_matplotlib) def test_pairplot_markers(self): vars = ["x", "y", "z"] markers = ["o", "x", "s", "d"] g = pairplot(self.df, hue="a", vars=vars, markers=markers) nt.assert_equal(g.hue_kws["marker"], markers) plt.close("all") with nt.assert_raises(ValueError): g = pairplot(self.df, hue="a", vars=vars, markers=markers[:-2]) class TestJointGrid(PlotTestCase): rs = np.random.RandomState(sum(map(ord, "JointGrid"))) x = rs.randn(100) y = rs.randn(100) x_na = x.copy() x_na[10] = np.nan x_na[20] = np.nan data = pd.DataFrame(dict(x=x, y=y, x_na=x_na)) def test_margin_grid_from_arrays(self): g = ag.JointGrid(self.x, self.y) npt.assert_array_equal(g.x, self.x) npt.assert_array_equal(g.y, self.y) def test_margin_grid_from_series(self): g = ag.JointGrid(self.data.x, self.data.y) npt.assert_array_equal(g.x, self.x) npt.assert_array_equal(g.y, self.y) def test_margin_grid_from_dataframe(self): g = ag.JointGrid("x", "y", self.data) npt.assert_array_equal(g.x, self.x) npt.assert_array_equal(g.y, self.y) def test_margin_grid_axis_labels(self): g = ag.JointGrid("x", "y", self.data) xlabel, ylabel = g.ax_joint.get_xlabel(), g.ax_joint.get_ylabel() nt.assert_equal(xlabel, "x") nt.assert_equal(ylabel, "y") g.set_axis_labels("x variable", "y variable") xlabel, ylabel = g.ax_joint.get_xlabel(), g.ax_joint.get_ylabel() nt.assert_equal(xlabel, "x variable") nt.assert_equal(ylabel, "y variable") def test_dropna(self): g = ag.JointGrid("x_na", "y", self.data, dropna=False) nt.assert_equal(len(g.x), len(self.x_na)) g = ag.JointGrid("x_na", "y", self.data, dropna=True) nt.assert_equal(len(g.x), pd.notnull(self.x_na).sum()) def test_axlims(self): lim = (-3, 3) g = ag.JointGrid("x", "y", self.data, xlim=lim, ylim=lim) nt.assert_equal(g.ax_joint.get_xlim(), lim) nt.assert_equal(g.ax_joint.get_ylim(), lim) nt.assert_equal(g.ax_marg_x.get_xlim(), lim) nt.assert_equal(g.ax_marg_y.get_ylim(), lim) def test_marginal_ticks(self): g = ag.JointGrid("x", "y", self.data) nt.assert_true(~len(g.ax_marg_x.get_xticks())) nt.assert_true(~len(g.ax_marg_y.get_yticks())) def test_bivariate_plot(self): g = ag.JointGrid("x", "y", self.data) g.plot_joint(plt.plot) x, y = g.ax_joint.lines[0].get_xydata().T npt.assert_array_equal(x, self.x) npt.assert_array_equal(y, self.y) def test_univariate_plot(self): g = ag.JointGrid("x", "x", self.data) g.plot_marginals(kdeplot) _, y1 = g.ax_marg_x.lines[0].get_xydata().T y2, _ = g.ax_marg_y.lines[0].get_xydata().T npt.assert_array_equal(y1, y2) def test_plot(self): g = ag.JointGrid("x", "x", self.data) g.plot(plt.plot, kdeplot) x, y = g.ax_joint.lines[0].get_xydata().T npt.assert_array_equal(x, self.x) npt.assert_array_equal(y, self.x) _, y1 = g.ax_marg_x.lines[0].get_xydata().T y2, _ = g.ax_marg_y.lines[0].get_xydata().T npt.assert_array_equal(y1, y2) def test_annotate(self): g = ag.JointGrid("x", "y", self.data) rp = stats.pearsonr(self.x, self.y) g.annotate(stats.pearsonr) annotation = g.ax_joint.legend_.texts[0].get_text() nt.assert_equal(annotation, "pearsonr = %.2g; p = %.2g" % rp) g.annotate(stats.pearsonr, stat="correlation") annotation = g.ax_joint.legend_.texts[0].get_text() nt.assert_equal(annotation, "correlation = %.2g; p = %.2g" % rp) def rsquared(x, y): return stats.pearsonr(x, y)[0] ** 2 r2 = rsquared(self.x, self.y) g.annotate(rsquared) annotation = g.ax_joint.legend_.texts[0].get_text() nt.assert_equal(annotation, "rsquared = %.2g" % r2) template = "{stat} = {val:.3g} (p = {p:.3g})" g.annotate(stats.pearsonr, template=template) annotation = g.ax_joint.legend_.texts[0].get_text() nt.assert_equal(annotation, template.format(stat="pearsonr", val=rp[0], p=rp[1])) def test_space(self): g = ag.JointGrid("x", "y", self.data, space=0) joint_bounds = g.ax_joint.bbox.bounds marg_x_bounds = g.ax_marg_x.bbox.bounds marg_y_bounds = g.ax_marg_y.bbox.bounds nt.assert_equal(joint_bounds[2], marg_x_bounds[2]) nt.assert_equal(joint_bounds[3], marg_y_bounds[3])
bsd-3-clause
rseubert/scikit-learn
examples/applications/plot_out_of_core_classification.py
255
13919
""" ====================================================== Out-of-core classification of text documents ====================================================== This is an example showing how scikit-learn can be used for classification using an out-of-core approach: learning from data that doesn't fit into main memory. We make use of an online classifier, i.e., one that supports the partial_fit method, that will be fed with batches of examples. To guarantee that the features space remains the same over time we leverage a HashingVectorizer that will project each example into the same feature space. This is especially useful in the case of text classification where new features (words) may appear in each batch. The dataset used in this example is Reuters-21578 as provided by the UCI ML repository. It will be automatically downloaded and uncompressed on first run. The plot represents the learning curve of the classifier: the evolution of classification accuracy over the course of the mini-batches. Accuracy is measured on the first 1000 samples, held out as a validation set. To limit the memory consumption, we queue examples up to a fixed amount before feeding them to the learner. """ # Authors: Eustache Diemert <eustache@diemert.fr> # @FedericoV <https://github.com/FedericoV/> # License: BSD 3 clause from __future__ import print_function from glob import glob import itertools import os.path import re import tarfile import time import numpy as np import matplotlib.pyplot as plt from matplotlib import rcParams from sklearn.externals.six.moves import html_parser from sklearn.externals.six.moves import urllib from sklearn.datasets import get_data_home from sklearn.feature_extraction.text import HashingVectorizer from sklearn.linear_model import SGDClassifier from sklearn.linear_model import PassiveAggressiveClassifier from sklearn.linear_model import Perceptron from sklearn.naive_bayes import MultinomialNB def _not_in_sphinx(): # Hack to detect whether we are running by the sphinx builder return '__file__' in globals() ############################################################################### # Reuters Dataset related routines ############################################################################### class ReutersParser(html_parser.HTMLParser): """Utility class to parse a SGML file and yield documents one at a time.""" def __init__(self, encoding='latin-1'): html_parser.HTMLParser.__init__(self) self._reset() self.encoding = encoding def handle_starttag(self, tag, attrs): method = 'start_' + tag getattr(self, method, lambda x: None)(attrs) def handle_endtag(self, tag): method = 'end_' + tag getattr(self, method, lambda: None)() def _reset(self): self.in_title = 0 self.in_body = 0 self.in_topics = 0 self.in_topic_d = 0 self.title = "" self.body = "" self.topics = [] self.topic_d = "" def parse(self, fd): self.docs = [] for chunk in fd: self.feed(chunk.decode(self.encoding)) for doc in self.docs: yield doc self.docs = [] self.close() def handle_data(self, data): if self.in_body: self.body += data elif self.in_title: self.title += data elif self.in_topic_d: self.topic_d += data def start_reuters(self, attributes): pass def end_reuters(self): self.body = re.sub(r'\s+', r' ', self.body) self.docs.append({'title': self.title, 'body': self.body, 'topics': self.topics}) self._reset() def start_title(self, attributes): self.in_title = 1 def end_title(self): self.in_title = 0 def start_body(self, attributes): self.in_body = 1 def end_body(self): self.in_body = 0 def start_topics(self, attributes): self.in_topics = 1 def end_topics(self): self.in_topics = 0 def start_d(self, attributes): self.in_topic_d = 1 def end_d(self): self.in_topic_d = 0 self.topics.append(self.topic_d) self.topic_d = "" def stream_reuters_documents(data_path=None): """Iterate over documents of the Reuters dataset. The Reuters archive will automatically be downloaded and uncompressed if the `data_path` directory does not exist. Documents are represented as dictionaries with 'body' (str), 'title' (str), 'topics' (list(str)) keys. """ DOWNLOAD_URL = ('http://archive.ics.uci.edu/ml/machine-learning-databases/' 'reuters21578-mld/reuters21578.tar.gz') ARCHIVE_FILENAME = 'reuters21578.tar.gz' if data_path is None: data_path = os.path.join(get_data_home(), "reuters") if not os.path.exists(data_path): """Download the dataset.""" print("downloading dataset (once and for all) into %s" % data_path) os.mkdir(data_path) def progress(blocknum, bs, size): total_sz_mb = '%.2f MB' % (size / 1e6) current_sz_mb = '%.2f MB' % ((blocknum * bs) / 1e6) if _not_in_sphinx(): print('\rdownloaded %s / %s' % (current_sz_mb, total_sz_mb), end='') archive_path = os.path.join(data_path, ARCHIVE_FILENAME) urllib.request.urlretrieve(DOWNLOAD_URL, filename=archive_path, reporthook=progress) if _not_in_sphinx(): print('\r', end='') print("untarring Reuters dataset...") tarfile.open(archive_path, 'r:gz').extractall(data_path) print("done.") parser = ReutersParser() for filename in glob(os.path.join(data_path, "*.sgm")): for doc in parser.parse(open(filename, 'rb')): yield doc ############################################################################### # Main ############################################################################### # Create the vectorizer and limit the number of features to a reasonable # maximum vectorizer = HashingVectorizer(decode_error='ignore', n_features=2 ** 18, non_negative=True) # Iterator over parsed Reuters SGML files. data_stream = stream_reuters_documents() # We learn a binary classification between the "acq" class and all the others. # "acq" was chosen as it is more or less evenly distributed in the Reuters # files. For other datasets, one should take care of creating a test set with # a realistic portion of positive instances. all_classes = np.array([0, 1]) positive_class = 'acq' # Here are some classifiers that support the `partial_fit` method partial_fit_classifiers = { 'SGD': SGDClassifier(), 'Perceptron': Perceptron(), 'NB Multinomial': MultinomialNB(alpha=0.01), 'Passive-Aggressive': PassiveAggressiveClassifier(), } def get_minibatch(doc_iter, size, pos_class=positive_class): """Extract a minibatch of examples, return a tuple X_text, y. Note: size is before excluding invalid docs with no topics assigned. """ data = [(u'{title}\n\n{body}'.format(**doc), pos_class in doc['topics']) for doc in itertools.islice(doc_iter, size) if doc['topics']] if not len(data): return np.asarray([], dtype=int), np.asarray([], dtype=int) X_text, y = zip(*data) return X_text, np.asarray(y, dtype=int) def iter_minibatches(doc_iter, minibatch_size): """Generator of minibatches.""" X_text, y = get_minibatch(doc_iter, minibatch_size) while len(X_text): yield X_text, y X_text, y = get_minibatch(doc_iter, minibatch_size) # test data statistics test_stats = {'n_test': 0, 'n_test_pos': 0} # First we hold out a number of examples to estimate accuracy n_test_documents = 1000 tick = time.time() X_test_text, y_test = get_minibatch(data_stream, 1000) parsing_time = time.time() - tick tick = time.time() X_test = vectorizer.transform(X_test_text) vectorizing_time = time.time() - tick test_stats['n_test'] += len(y_test) test_stats['n_test_pos'] += sum(y_test) print("Test set is %d documents (%d positive)" % (len(y_test), sum(y_test))) def progress(cls_name, stats): """Report progress information, return a string.""" duration = time.time() - stats['t0'] s = "%20s classifier : \t" % cls_name s += "%(n_train)6d train docs (%(n_train_pos)6d positive) " % stats s += "%(n_test)6d test docs (%(n_test_pos)6d positive) " % test_stats s += "accuracy: %(accuracy).3f " % stats s += "in %.2fs (%5d docs/s)" % (duration, stats['n_train'] / duration) return s cls_stats = {} for cls_name in partial_fit_classifiers: stats = {'n_train': 0, 'n_train_pos': 0, 'accuracy': 0.0, 'accuracy_history': [(0, 0)], 't0': time.time(), 'runtime_history': [(0, 0)], 'total_fit_time': 0.0} cls_stats[cls_name] = stats get_minibatch(data_stream, n_test_documents) # Discard test set # We will feed the classifier with mini-batches of 1000 documents; this means # we have at most 1000 docs in memory at any time. The smaller the document # batch, the bigger the relative overhead of the partial fit methods. minibatch_size = 1000 # Create the data_stream that parses Reuters SGML files and iterates on # documents as a stream. minibatch_iterators = iter_minibatches(data_stream, minibatch_size) total_vect_time = 0.0 # Main loop : iterate on mini-batchs of examples for i, (X_train_text, y_train) in enumerate(minibatch_iterators): tick = time.time() X_train = vectorizer.transform(X_train_text) total_vect_time += time.time() - tick for cls_name, cls in partial_fit_classifiers.items(): tick = time.time() # update estimator with examples in the current mini-batch cls.partial_fit(X_train, y_train, classes=all_classes) # accumulate test accuracy stats cls_stats[cls_name]['total_fit_time'] += time.time() - tick cls_stats[cls_name]['n_train'] += X_train.shape[0] cls_stats[cls_name]['n_train_pos'] += sum(y_train) tick = time.time() cls_stats[cls_name]['accuracy'] = cls.score(X_test, y_test) cls_stats[cls_name]['prediction_time'] = time.time() - tick acc_history = (cls_stats[cls_name]['accuracy'], cls_stats[cls_name]['n_train']) cls_stats[cls_name]['accuracy_history'].append(acc_history) run_history = (cls_stats[cls_name]['accuracy'], total_vect_time + cls_stats[cls_name]['total_fit_time']) cls_stats[cls_name]['runtime_history'].append(run_history) if i % 3 == 0: print(progress(cls_name, cls_stats[cls_name])) if i % 3 == 0: print('\n') ############################################################################### # Plot results ############################################################################### def plot_accuracy(x, y, x_legend): """Plot accuracy as a function of x.""" x = np.array(x) y = np.array(y) plt.title('Classification accuracy as a function of %s' % x_legend) plt.xlabel('%s' % x_legend) plt.ylabel('Accuracy') plt.grid(True) plt.plot(x, y) rcParams['legend.fontsize'] = 10 cls_names = list(sorted(cls_stats.keys())) # Plot accuracy evolution plt.figure() for _, stats in sorted(cls_stats.items()): # Plot accuracy evolution with #examples accuracy, n_examples = zip(*stats['accuracy_history']) plot_accuracy(n_examples, accuracy, "training examples (#)") ax = plt.gca() ax.set_ylim((0.8, 1)) plt.legend(cls_names, loc='best') plt.figure() for _, stats in sorted(cls_stats.items()): # Plot accuracy evolution with runtime accuracy, runtime = zip(*stats['runtime_history']) plot_accuracy(runtime, accuracy, 'runtime (s)') ax = plt.gca() ax.set_ylim((0.8, 1)) plt.legend(cls_names, loc='best') # Plot fitting times plt.figure() fig = plt.gcf() cls_runtime = [] for cls_name, stats in sorted(cls_stats.items()): cls_runtime.append(stats['total_fit_time']) cls_runtime.append(total_vect_time) cls_names.append('Vectorization') bar_colors = rcParams['axes.color_cycle'][:len(cls_names)] ax = plt.subplot(111) rectangles = plt.bar(range(len(cls_names)), cls_runtime, width=0.5, color=bar_colors) ax.set_xticks(np.linspace(0.25, len(cls_names) - 0.75, len(cls_names))) ax.set_xticklabels(cls_names, fontsize=10) ymax = max(cls_runtime) * 1.2 ax.set_ylim((0, ymax)) ax.set_ylabel('runtime (s)') ax.set_title('Training Times') def autolabel(rectangles): """attach some text vi autolabel on rectangles.""" for rect in rectangles: height = rect.get_height() ax.text(rect.get_x() + rect.get_width() / 2., 1.05 * height, '%.4f' % height, ha='center', va='bottom') autolabel(rectangles) plt.show() # Plot prediction times plt.figure() #fig = plt.gcf() cls_runtime = [] cls_names = list(sorted(cls_stats.keys())) for cls_name, stats in sorted(cls_stats.items()): cls_runtime.append(stats['prediction_time']) cls_runtime.append(parsing_time) cls_names.append('Read/Parse\n+Feat.Extr.') cls_runtime.append(vectorizing_time) cls_names.append('Hashing\n+Vect.') bar_colors = rcParams['axes.color_cycle'][:len(cls_names)] ax = plt.subplot(111) rectangles = plt.bar(range(len(cls_names)), cls_runtime, width=0.5, color=bar_colors) ax.set_xticks(np.linspace(0.25, len(cls_names) - 0.75, len(cls_names))) ax.set_xticklabels(cls_names, fontsize=8) plt.setp(plt.xticks()[1], rotation=30) ymax = max(cls_runtime) * 1.2 ax.set_ylim((0, ymax)) ax.set_ylabel('runtime (s)') ax.set_title('Prediction Times (%d instances)' % n_test_documents) autolabel(rectangles) plt.show()
bsd-3-clause
andyr0id/PyGFNN
examples/gfnn/example1F.py
1
1657
#!/usr/bin/env python __author__ = 'Andrew J. Lambert, andy@andyroid.co.uk' """ example1P A one layer network with fixed internal connections """ from pygfnn.tools.plotting.gfnn import * import pygfnn.tools.shortcuts as gfnn import numpy as np import timeit import matplotlib.pyplot as plt import scipy.io as sio if __name__ == '__main__': # Network parameters oscParams = { 'a': 1, 'b1': -1, 'b2': -1000, 'd1': 0, 'd2': 0, 'e': 1 } # Limit cycle learnParams = gfnn.NOLEARN_ALLFREQ freqDist = { 'fspac': 'log', 'min': 0.5, 'max': 8 } # Make network n = gfnn.buildGFNN(196, oscParams = oscParams, freqDist = freqDist, learnParams = learnParams) n.recurrentConns[0].c0[:] = gfnn.getInitC(n, n, [(1,1), (1,2), (1,3), (1,4), (1,6), (1,8), (2,3), (3,4), (3,8)], thresh=0.01) n.reset() # First plots, showing initial connection state ampFig1, phaseFig1 = plotConns(n.recurrentConns[0].c, freqDist['min'], freqDist['max']) # Stimulus - 50 seconds of 1Hz sin t = np.arange(0, 50, n['h'].dt) x = np.sin(2 * np.pi * 1 * t) * 0.1 # Run the network timer = timeit.default_timer start = timer() for i in range(len(t)): out = n.activate(x[i]) end = timer() print('Elapsed time is %f seconds' % (end - start)) if learnParams is not None: # Second plots, showing final connection state ampFig2, phaseFig2 = plotConns(n.recurrentConns[0].c, freqDist['min'], freqDist['max']) Z = n['h'].outputbuffer[:n.offset] fig1 = ampx(Z, n.dt, freqDist['min'], freqDist['max']) fig2 = phasex(Z, n.dt, freqDist['min'], freqDist['max']) plt.show()
gpl-2.0
jereze/scikit-learn
examples/datasets/plot_iris_dataset.py
283
1928
#!/usr/bin/python # -*- coding: utf-8 -*- """ ========================================================= The Iris Dataset ========================================================= This data sets consists of 3 different types of irises' (Setosa, Versicolour, and Virginica) petal and sepal length, stored in a 150x4 numpy.ndarray The rows being the samples and the columns being: Sepal Length, Sepal Width, Petal Length and Petal Width. The below plot uses the first two features. See `here <http://en.wikipedia.org/wiki/Iris_flower_data_set>`_ for more information on this dataset. """ print(__doc__) # Code source: Gaël Varoquaux # Modified for documentation by Jaques Grobler # License: BSD 3 clause import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from sklearn import datasets from sklearn.decomposition import PCA # import some data to play with iris = datasets.load_iris() X = iris.data[:, :2] # we only take the first two features. Y = iris.target x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5 y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5 plt.figure(2, figsize=(8, 6)) plt.clf() # Plot the training points plt.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.Paired) plt.xlabel('Sepal length') plt.ylabel('Sepal width') plt.xlim(x_min, x_max) plt.ylim(y_min, y_max) plt.xticks(()) plt.yticks(()) # To getter a better understanding of interaction of the dimensions # plot the first three PCA dimensions fig = plt.figure(1, figsize=(8, 6)) ax = Axes3D(fig, elev=-150, azim=110) X_reduced = PCA(n_components=3).fit_transform(iris.data) ax.scatter(X_reduced[:, 0], X_reduced[:, 1], X_reduced[:, 2], c=Y, cmap=plt.cm.Paired) ax.set_title("First three PCA directions") ax.set_xlabel("1st eigenvector") ax.w_xaxis.set_ticklabels([]) ax.set_ylabel("2nd eigenvector") ax.w_yaxis.set_ticklabels([]) ax.set_zlabel("3rd eigenvector") ax.w_zaxis.set_ticklabels([]) plt.show()
bsd-3-clause
ashhher3/scikit-learn
sklearn/cluster/tests/test_affinity_propagation.py
31
2633
""" Testing for Clustering methods """ import numpy as np from sklearn.utils.testing import assert_equal from sklearn.utils.testing import assert_array_equal from sklearn.utils.testing import assert_raises from sklearn.cluster.affinity_propagation_ import AffinityPropagation from sklearn.cluster.affinity_propagation_ import affinity_propagation from sklearn.datasets.samples_generator import make_blobs from sklearn.metrics import euclidean_distances n_clusters = 3 centers = np.array([[1, 1], [-1, -1], [1, -1]]) + 10 X, _ = make_blobs(n_samples=60, n_features=2, centers=centers, cluster_std=0.4, shuffle=True, random_state=0) def test_affinity_propagation(): """Affinity Propagation algorithm """ # Compute similarities S = -euclidean_distances(X, squared=True) preference = np.median(S) * 10 # Compute Affinity Propagation cluster_centers_indices, labels = affinity_propagation( S, preference=preference) n_clusters_ = len(cluster_centers_indices) assert_equal(n_clusters, n_clusters_) af = AffinityPropagation(preference=preference, affinity="precomputed") labels_precomputed = af.fit(S).labels_ af = AffinityPropagation(preference=preference, verbose=True) labels = af.fit(X).labels_ assert_array_equal(labels, labels_precomputed) cluster_centers_indices = af.cluster_centers_indices_ n_clusters_ = len(cluster_centers_indices) assert_equal(np.unique(labels).size, n_clusters_) assert_equal(n_clusters, n_clusters_) # Test also with no copy _, labels_no_copy = affinity_propagation(S, preference=preference, copy=False) assert_array_equal(labels, labels_no_copy) # Test input validation assert_raises(ValueError, affinity_propagation, S[:, :-1]) assert_raises(ValueError, affinity_propagation, S, damping=0) af = AffinityPropagation(affinity="unknown") assert_raises(ValueError, af.fit, X) def test_affinity_propagation_predict(): """Test AffinityPropagation.predict""" af = AffinityPropagation(affinity="euclidean") labels = af.fit_predict(X) labels2 = af.predict(X) assert_array_equal(labels, labels2) def test_affinity_propagation_predict_error(): """Test exception in AffinityPropagation.predict""" # Not fitted. af = AffinityPropagation(affinity="euclidean") assert_raises(ValueError, af.predict, X) # Predict not supported when affinity="precomputed". S = np.dot(X, X.T) af = AffinityPropagation(affinity="precomputed") af.fit(S) assert_raises(ValueError, af.predict, X)
bsd-3-clause
earlbellinger/asteroseismology
grid/calibrate.py
1
3590
#### Calibrate a solar model #### Author: Earl Bellinger ( bellinger@mps.mpg.de ) #### Stellar Ages & Galactic Evolution Group #### Max-Planck-Institut fur Sonnensystemforschung #### Department of Astronomy, Yale University import numpy as np import pandas as pd from scipy import optimize from os import path from subprocess import Popen from math import log10 Z_div_X_solar = 0.02293 # GS98 # 0.0245 # GN93 # log10_Z_div_X_solar = np.log10(Z_div_X_solar) constraint_names = ("log L", "log R", "Fe/H") param_names = ("Y", "alpha", "Z") param_init = [0.273449170177157, 1.83413390909832, 0.0197444964340224] directory = 'calibrate_py' print(directory) def objective(): ## minimize sum(log(model values / solar values)**2) # searches in LOGS_MS subdirectory of the global 'directory' variable hstry_file = path.join(directory, 'LOGS_MS', 'history.data') if (not path.exists(hstry_file)): return np.inf hstry = pd.read_table(hstry_file, header=0, skiprows=5, delimiter='\s+') #header=1, mdl = hstry.loc[hstry.shape[0]-1] #hstry[nrow(hstry),] # [Fe/H] = log10 ( Z / X / (Z/X)_Sun ) mdl_Fe_H = mdl['log_surf_cell_z']-np.log10(mdl['surface_h1'])-log10_Z_div_X_solar mdl_vals = [mdl['log_L'], mdl['log_R'], mdl_Fe_H] print("*** Model values") print(constraint_names, mdl_vals) print('L', 10**mdl['log_L'], 'R', 10**mdl['log_R']) result = sum([ii**2 for ii in mdl_vals]) if np.isfinite(result): return log10(result) return 10**10 ### SEARCH iteration = 0 best_val = np.inf best_param = param_init #run = function(params) { def run(params): global iteration global best_val global best_param iteration = iteration + 1 print("**** iter:", iteration) Y, alpha, Z = params print(param_names, (Y, alpha, Z)) if (Y < 0.2 or Y > 0.4 or Z < 0 or Z > 0.04 or alpha < 1 or alpha > 3): return 10**10 #if (Y < 0.23): # Y = 0.23 #if (Y > 0.33): # Y = 0.33 #if (Z < 0.01): # Z = 0.01 #if (Z > 0.04): # Z = 0.04 #if (alpha < 1): # alpha = 1 #if (alpha > 3): # alpha = 3 command = "./dispatch.sh" + \ ' -Y ' + str(Y) + \ ' -a ' + str(alpha) + \ ' -o ' + '0' + \ ' -Z ' + str(Z) + \ ' -D ' + '1' + \ ' -g ' + '1' + \ ' -e ' + '0' + \ ' -c ' + "4572000000" + \ ' -d ' + directory print(command) #system(command) process = Popen(command.split(), shell=False) process.wait() obj_val = objective() print("**** objective value =", obj_val) if (obj_val < best_val): best_val = obj_val print("*****", param_names, params) best_param = params print("***** New record!") return obj_val result = optimize.minimize(fun=run, x0=param_init, method='Nelder-Mead', options={'disp': True, 'maxiter': 10000}) #, #bounds=((0.25, 0.32), (1, 3), (0.012, 0.03))) print("Optimization terminated. Saving best result") Y, alpha, Z = result.x command = "./dispatch.sh" + \ ' -Y ' + str(Y) + \ ' -a ' + str(alpha) + \ ' -o ' + '0' + \ ' -Z ' + str(Z) + \ ' -D ' + '1' + \ ' -g ' + '1' + \ ' -e ' + '0' + \ ' -c ' + "4572000000" + \ ' -d ' + directory print(command) process = Popen(command.split(), shell=False) process.wait() print(result)
gpl-2.0
shaneknapp/spark
python/pyspark/pandas/indexes/base.py
2
84541
# # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You under the Apache License, Version 2.0 # (the "License"); you may not use this file except in compliance with # the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # from functools import partial from typing import Any, Iterator, List, Optional, Tuple, Union, cast, no_type_check import warnings import pandas as pd import numpy as np from pandas.api.types import ( is_list_like, is_interval_dtype, is_bool_dtype, is_categorical_dtype, is_integer_dtype, is_float_dtype, is_numeric_dtype, is_object_dtype, ) from pandas.core.accessor import CachedAccessor from pandas.io.formats.printing import pprint_thing from pandas.api.types import CategoricalDtype, is_hashable from pandas._libs import lib from pyspark.sql import functions as F, Column from pyspark.sql.types import FractionalType, IntegralType, TimestampType from pyspark import pandas as ps # For running doctests and reference resolution in PyCharm. from pyspark.pandas._typing import Dtype, Scalar from pyspark.pandas.config import get_option, option_context from pyspark.pandas.base import IndexOpsMixin from pyspark.pandas.frame import DataFrame from pyspark.pandas.missing.indexes import MissingPandasLikeIndex from pyspark.pandas.series import Series, first_series from pyspark.pandas.spark import functions as SF from pyspark.pandas.spark.accessors import SparkIndexMethods from pyspark.pandas.utils import ( is_name_like_tuple, is_name_like_value, name_like_string, same_anchor, scol_for, verify_temp_column_name, validate_bool_kwarg, ERROR_MESSAGE_CANNOT_COMBINE, ) from pyspark.pandas.internal import ( InternalField, InternalFrame, DEFAULT_SERIES_NAME, SPARK_DEFAULT_INDEX_NAME, SPARK_INDEX_NAME_FORMAT, ) class Index(IndexOpsMixin): """ pandas-on-Spark Index that corresponds to pandas Index logically. This might hold Spark Column internally. Parameters ---------- data : array-like (1-dimensional) dtype : dtype, default None If dtype is None, we find the dtype that best fits the data. If an actual dtype is provided, we coerce to that dtype if it's safe. Otherwise, an error will be raised. copy : bool Make a copy of input ndarray. name : object Name to be stored in the index. tupleize_cols : bool (default: True) When True, attempt to create a MultiIndex if possible. See Also -------- MultiIndex : A multi-level, or hierarchical, Index. DatetimeIndex : Index of datetime64 data. Int64Index : A special case of :class:`Index` with purely integer labels. Float64Index : A special case of :class:`Index` with purely float labels. Examples -------- >>> ps.DataFrame({'a': ['a', 'b', 'c']}, index=[1, 2, 3]).index Int64Index([1, 2, 3], dtype='int64') >>> ps.DataFrame({'a': [1, 2, 3]}, index=list('abc')).index Index(['a', 'b', 'c'], dtype='object') >>> ps.Index([1, 2, 3]) Int64Index([1, 2, 3], dtype='int64') >>> ps.Index(list('abc')) Index(['a', 'b', 'c'], dtype='object') From a Series: >>> s = ps.Series([1, 2, 3], index=[10, 20, 30]) >>> ps.Index(s) Int64Index([1, 2, 3], dtype='int64') From an Index: >>> idx = ps.Index([1, 2, 3]) >>> ps.Index(idx) Int64Index([1, 2, 3], dtype='int64') """ def __new__( cls, data: Optional[Any] = None, dtype: Optional[Union[str, Dtype]] = None, copy: bool = False, name: Optional[Union[Any, Tuple]] = None, tupleize_cols: bool = True, **kwargs: Any ) -> "Index": if not is_hashable(name): raise TypeError("Index.name must be a hashable type") if isinstance(data, Series): if dtype is not None: data = data.astype(dtype) if name is not None: data = data.rename(name) internal = InternalFrame( spark_frame=data._internal.spark_frame, index_spark_columns=data._internal.data_spark_columns, index_names=data._internal.column_labels, index_fields=data._internal.data_fields, column_labels=[], data_spark_columns=[], data_fields=[], ) return DataFrame(internal).index elif isinstance(data, Index): if copy: data = data.copy() if dtype is not None: data = data.astype(dtype) if name is not None: data = data.rename(name) return data return cast( Index, ps.from_pandas( pd.Index( data=data, dtype=dtype, copy=copy, name=name, tupleize_cols=tupleize_cols, **kwargs ) ), ) @staticmethod def _new_instance(anchor: DataFrame) -> "Index": from pyspark.pandas.indexes.category import CategoricalIndex from pyspark.pandas.indexes.datetimes import DatetimeIndex from pyspark.pandas.indexes.multi import MultiIndex from pyspark.pandas.indexes.numeric import Float64Index, Int64Index if anchor._internal.index_level > 1: instance = object.__new__(MultiIndex) elif isinstance(anchor._internal.index_fields[0].dtype, CategoricalDtype): instance = object.__new__(CategoricalIndex) elif isinstance( anchor._internal.spark_type_for(anchor._internal.index_spark_columns[0]), IntegralType ): instance = object.__new__(Int64Index) elif isinstance( anchor._internal.spark_type_for(anchor._internal.index_spark_columns[0]), FractionalType ): instance = object.__new__(Float64Index) elif isinstance( anchor._internal.spark_type_for(anchor._internal.index_spark_columns[0]), TimestampType ): instance = object.__new__(DatetimeIndex) else: instance = object.__new__(Index) instance._anchor = anchor return instance @property def _psdf(self) -> DataFrame: return self._anchor @property def _internal(self) -> InternalFrame: internal = self._psdf._internal return internal.copy( column_labels=internal.index_names, data_spark_columns=internal.index_spark_columns, data_fields=internal.index_fields, column_label_names=None, ) @property def _column_label(self) -> Optional[Tuple]: return self._psdf._internal.index_names[0] def _with_new_scol(self, scol: Column, *, field: Optional[InternalField] = None) -> "Index": """ Copy pandas-on-Spark Index with the new Spark Column. :param scol: the new Spark Column :return: the copied Index """ internal = self._internal.copy( index_spark_columns=[scol.alias(SPARK_DEFAULT_INDEX_NAME)], index_fields=[ field if field is None or field.struct_field is None else field.copy(name=SPARK_DEFAULT_INDEX_NAME) ], column_labels=[], data_spark_columns=[], data_fields=[], ) return DataFrame(internal).index spark = CachedAccessor("spark", SparkIndexMethods) # This method is used via `DataFrame.info` API internally. def _summary(self, name: Optional[str] = None) -> str: """ Return a summarized representation. Parameters ---------- name : str name to use in the summary representation Returns ------- String with a summarized representation of the index """ head, tail, total_count = tuple( cast( pd.DataFrame, self._internal.spark_frame.select( F.first(self.spark.column), F.last(self.spark.column), F.count(F.expr("*")) ).toPandas(), ).iloc[0] ) if total_count > 0: index_summary = ", %s to %s" % (pprint_thing(head), pprint_thing(tail)) else: index_summary = "" if name is None: name = type(self).__name__ return "%s: %s entries%s" % (name, total_count, index_summary) @property def size(self) -> int: """ Return an int representing the number of elements in this object. Examples -------- >>> df = ps.DataFrame([(.2, .3), (.0, .6), (.6, .0), (.2, .1)], ... columns=['dogs', 'cats'], ... index=list('abcd')) >>> df.index.size 4 >>> df.set_index('dogs', append=True).index.size 4 """ return len(self) @property def shape(self) -> tuple: """ Return a tuple of the shape of the underlying data. Examples -------- >>> idx = ps.Index(['a', 'b', 'c']) >>> idx Index(['a', 'b', 'c'], dtype='object') >>> idx.shape (3,) >>> midx = ps.MultiIndex.from_tuples([('a', 'x'), ('b', 'y'), ('c', 'z')]) >>> midx # doctest: +SKIP MultiIndex([('a', 'x'), ('b', 'y'), ('c', 'z')], ) >>> midx.shape (3,) """ return (len(self._psdf),) def identical(self, other: "Index") -> bool: """ Similar to equals, but check that other comparable attributes are also equal. Returns ------- bool If two Index objects have equal elements and same type True, otherwise False. Examples -------- >>> from pyspark.pandas.config import option_context >>> idx = ps.Index(['a', 'b', 'c']) >>> midx = ps.MultiIndex.from_tuples([('a', 'x'), ('b', 'y'), ('c', 'z')]) For Index >>> idx.identical(idx) True >>> with option_context('compute.ops_on_diff_frames', True): ... idx.identical(ps.Index(['a', 'b', 'c'])) True >>> with option_context('compute.ops_on_diff_frames', True): ... idx.identical(ps.Index(['b', 'b', 'a'])) False >>> idx.identical(midx) False For MultiIndex >>> midx.identical(midx) True >>> with option_context('compute.ops_on_diff_frames', True): ... midx.identical(ps.MultiIndex.from_tuples([('a', 'x'), ('b', 'y'), ('c', 'z')])) True >>> with option_context('compute.ops_on_diff_frames', True): ... midx.identical(ps.MultiIndex.from_tuples([('c', 'z'), ('b', 'y'), ('a', 'x')])) False >>> midx.identical(idx) False """ from pyspark.pandas.indexes.multi import MultiIndex self_name = self.names if isinstance(self, MultiIndex) else self.name other_name = other.names if isinstance(other, MultiIndex) else other.name return ( self_name == other_name # to support non-index comparison by short-circuiting. and self.equals(other) ) def equals(self, other: "Index") -> bool: """ Determine if two Index objects contain the same elements. Returns ------- bool True if "other" is an Index and it has the same elements as calling index; False otherwise. Examples -------- >>> from pyspark.pandas.config import option_context >>> idx = ps.Index(['a', 'b', 'c']) >>> idx.name = "name" >>> midx = ps.MultiIndex.from_tuples([('a', 'x'), ('b', 'y'), ('c', 'z')]) >>> midx.names = ("nameA", "nameB") For Index >>> idx.equals(idx) True >>> with option_context('compute.ops_on_diff_frames', True): ... idx.equals(ps.Index(['a', 'b', 'c'])) True >>> with option_context('compute.ops_on_diff_frames', True): ... idx.equals(ps.Index(['b', 'b', 'a'])) False >>> idx.equals(midx) False For MultiIndex >>> midx.equals(midx) True >>> with option_context('compute.ops_on_diff_frames', True): ... midx.equals(ps.MultiIndex.from_tuples([('a', 'x'), ('b', 'y'), ('c', 'z')])) True >>> with option_context('compute.ops_on_diff_frames', True): ... midx.equals(ps.MultiIndex.from_tuples([('c', 'z'), ('b', 'y'), ('a', 'x')])) False >>> midx.equals(idx) False """ if same_anchor(self, other): return True elif type(self) == type(other): if get_option("compute.ops_on_diff_frames"): # TODO: avoid using default index? with option_context("compute.default_index_type", "distributed-sequence"): # Directly using Series from both self and other seems causing # some exceptions when 'compute.ops_on_diff_frames' is enabled. # Working around for now via using frame. return ( cast(Series, self.to_series("self").reset_index(drop=True)) == cast(Series, other.to_series("other").reset_index(drop=True)) ).all() else: raise ValueError(ERROR_MESSAGE_CANNOT_COMBINE) else: return False def transpose(self) -> "Index": """ Return the transpose, For index, It will be index itself. Examples -------- >>> idx = ps.Index(['a', 'b', 'c']) >>> idx Index(['a', 'b', 'c'], dtype='object') >>> idx.transpose() Index(['a', 'b', 'c'], dtype='object') For MultiIndex >>> midx = ps.MultiIndex.from_tuples([('a', 'x'), ('b', 'y'), ('c', 'z')]) >>> midx # doctest: +SKIP MultiIndex([('a', 'x'), ('b', 'y'), ('c', 'z')], ) >>> midx.transpose() # doctest: +SKIP MultiIndex([('a', 'x'), ('b', 'y'), ('c', 'z')], ) """ return self T = property(transpose) def _to_internal_pandas(self) -> pd.Index: """ Return a pandas Index directly from _internal to avoid overhead of copy. This method is for internal use only. """ return self._psdf._internal.to_pandas_frame.index def to_pandas(self) -> pd.Index: """ Return a pandas Index. .. note:: This method should only be used if the resulting pandas object is expected to be small, as all the data is loaded into the driver's memory. Examples -------- >>> df = ps.DataFrame([(.2, .3), (.0, .6), (.6, .0), (.2, .1)], ... columns=['dogs', 'cats'], ... index=list('abcd')) >>> df['dogs'].index.to_pandas() Index(['a', 'b', 'c', 'd'], dtype='object') """ return self._to_internal_pandas().copy() def to_numpy(self, dtype: Optional[Union[str, Dtype]] = None, copy: bool = False) -> np.ndarray: """ A NumPy ndarray representing the values in this Index or MultiIndex. .. note:: This method should only be used if the resulting NumPy ndarray is expected to be small, as all the data is loaded into the driver's memory. Parameters ---------- dtype : str or numpy.dtype, optional The dtype to pass to :meth:`numpy.asarray` copy : bool, default False Whether to ensure that the returned value is a not a view on another array. Note that ``copy=False`` does not *ensure* that ``to_numpy()`` is no-copy. Rather, ``copy=True`` ensure that a copy is made, even if not strictly necessary. Returns ------- numpy.ndarray Examples -------- >>> ps.Series([1, 2, 3, 4]).index.to_numpy() array([0, 1, 2, 3]) >>> ps.DataFrame({'a': ['a', 'b', 'c']}, index=[[1, 2, 3], [4, 5, 6]]).index.to_numpy() array([(1, 4), (2, 5), (3, 6)], dtype=object) """ result = np.asarray(self._to_internal_pandas()._values, dtype=dtype) if copy: result = result.copy() return result @property def values(self) -> np.ndarray: """ Return an array representing the data in the Index. .. warning:: We recommend using `Index.to_numpy()` instead. .. note:: This method should only be used if the resulting NumPy ndarray is expected to be small, as all the data is loaded into the driver's memory. Returns ------- numpy.ndarray Examples -------- >>> ps.Series([1, 2, 3, 4]).index.values array([0, 1, 2, 3]) >>> ps.DataFrame({'a': ['a', 'b', 'c']}, index=[[1, 2, 3], [4, 5, 6]]).index.values array([(1, 4), (2, 5), (3, 6)], dtype=object) """ warnings.warn("We recommend using `{}.to_numpy()` instead.".format(type(self).__name__)) return self.to_numpy() @property def asi8(self) -> np.ndarray: """ Integer representation of the values. .. warning:: We recommend using `Index.to_numpy()` instead. .. note:: This method should only be used if the resulting NumPy ndarray is expected to be small, as all the data is loaded into the driver's memory. Returns ------- numpy.ndarray An ndarray with int64 dtype. Examples -------- >>> ps.Index([1, 2, 3]).asi8 array([1, 2, 3]) Returns None for non-int64 dtype >>> ps.Index(['a', 'b', 'c']).asi8 is None True """ warnings.warn("We recommend using `{}.to_numpy()` instead.".format(type(self).__name__)) if isinstance(self.spark.data_type, IntegralType): return self.to_numpy() elif isinstance(self.spark.data_type, TimestampType): return np.array(list(map(lambda x: x.astype(np.int64), self.to_numpy()))) else: return None @property def has_duplicates(self) -> bool: """ If index has duplicates, return True, otherwise False. Examples -------- >>> idx = ps.Index([1, 5, 7, 7]) >>> idx.has_duplicates True >>> idx = ps.Index([1, 5, 7]) >>> idx.has_duplicates False >>> idx = ps.Index(["Watermelon", "Orange", "Apple", ... "Watermelon"]) >>> idx.has_duplicates True >>> idx = ps.Index(["Orange", "Apple", ... "Watermelon"]) >>> idx.has_duplicates False """ sdf = self._internal.spark_frame.select(self.spark.column) scol = scol_for(sdf, sdf.columns[0]) return sdf.select(F.count(scol) != F.countDistinct(scol)).first()[0] @property def is_unique(self) -> bool: """ Return if the index has unique values. Examples -------- >>> idx = ps.Index([1, 5, 7, 7]) >>> idx.is_unique False >>> idx = ps.Index([1, 5, 7]) >>> idx.is_unique True >>> idx = ps.Index(["Watermelon", "Orange", "Apple", ... "Watermelon"]) >>> idx.is_unique False >>> idx = ps.Index(["Orange", "Apple", ... "Watermelon"]) >>> idx.is_unique True """ return not self.has_duplicates @property def name(self) -> Union[Any, Tuple]: """Return name of the Index.""" return self.names[0] @name.setter def name(self, name: Union[Any, Tuple]) -> None: self.names = [name] @property def names(self) -> List[Union[Any, Tuple]]: """Return names of the Index.""" return [ name if name is None or len(name) > 1 else name[0] for name in self._internal.index_names # type: ignore ] @names.setter def names(self, names: List[Union[Any, Tuple]]) -> None: if not is_list_like(names): raise ValueError("Names must be a list-like") if self._internal.index_level != len(names): raise ValueError( "Length of new names must be {}, got {}".format( self._internal.index_level, len(names) ) ) if self._internal.index_level == 1: self.rename(names[0], inplace=True) else: self.rename(names, inplace=True) @property def nlevels(self) -> int: """ Number of levels in Index & MultiIndex. Examples -------- >>> psdf = ps.DataFrame({"a": [1, 2, 3]}, index=pd.Index(['a', 'b', 'c'], name="idx")) >>> psdf.index.nlevels 1 >>> psdf = ps.DataFrame({'a': [1, 2, 3]}, index=[list('abc'), list('def')]) >>> psdf.index.nlevels 2 """ return self._internal.index_level def rename( self, name: Union[Any, Tuple, List[Union[Any, Tuple]]], inplace: bool = False ) -> Optional["Index"]: """ Alter Index or MultiIndex name. Able to set new names without level. Defaults to returning new index. Parameters ---------- name : label or list of labels Name(s) to set. inplace : boolean, default False Modifies the object directly, instead of creating a new Index or MultiIndex. Returns ------- Index or MultiIndex The same type as the caller or None if inplace is True. Examples -------- >>> df = ps.DataFrame({'a': ['A', 'C'], 'b': ['A', 'B']}, columns=['a', 'b']) >>> df.index.rename("c") Int64Index([0, 1], dtype='int64', name='c') >>> df.set_index("a", inplace=True) >>> df.index.rename("d") Index(['A', 'C'], dtype='object', name='d') You can also change the index name in place. >>> df.index.rename("e", inplace=True) >>> df.index Index(['A', 'C'], dtype='object', name='e') >>> df # doctest: +NORMALIZE_WHITESPACE b e A A C B Support for MultiIndex >>> psidx = ps.MultiIndex.from_tuples([('a', 'x'), ('b', 'y')]) >>> psidx.names = ['hello', 'pandas-on-Spark'] >>> psidx # doctest: +SKIP MultiIndex([('a', 'x'), ('b', 'y')], names=['hello', 'pandas-on-Spark']) >>> psidx.rename(['aloha', 'databricks']) # doctest: +SKIP MultiIndex([('a', 'x'), ('b', 'y')], names=['aloha', 'databricks']) """ names = self._verify_for_rename(name) internal = self._psdf._internal.copy(index_names=names) if inplace: self._psdf._update_internal_frame(internal) return None else: return DataFrame(internal).index def _verify_for_rename(self, name: Union[Any, Tuple]) -> List[Tuple]: if is_hashable(name): if is_name_like_tuple(name): return [name] elif is_name_like_value(name): return [(name,)] raise TypeError("Index.name must be a hashable type") # TODO: add downcast parameter for fillna function def fillna(self, value: Scalar) -> "Index": """ Fill NA/NaN values with the specified value. Parameters ---------- value : scalar Scalar value to use to fill holes (example: 0). This value cannot be a list-likes. Returns ------- Index : filled with value Examples -------- >>> ki = ps.DataFrame({'a': ['a', 'b', 'c']}, index=[1, 2, None]).index >>> ki Float64Index([1.0, 2.0, nan], dtype='float64') >>> ki.fillna(0) Float64Index([1.0, 2.0, 0.0], dtype='float64') """ if not isinstance(value, (float, int, str, bool)): raise TypeError("Unsupported type %s" % type(value).__name__) sdf = self._internal.spark_frame.fillna(value) internal = InternalFrame( # TODO: dtypes? spark_frame=sdf, index_spark_columns=[ scol_for(sdf, col) for col in self._internal.index_spark_column_names ], index_names=self._internal.index_names, ) return DataFrame(internal).index # TODO: ADD keep parameter def drop_duplicates(self) -> "Index": """ Return Index with duplicate values removed. Returns ------- deduplicated : Index See Also -------- Series.drop_duplicates : Equivalent method on Series. DataFrame.drop_duplicates : Equivalent method on DataFrame. Examples -------- Generate an pandas.Index with duplicate values. >>> idx = ps.Index(['lama', 'cow', 'lama', 'beetle', 'lama', 'hippo']) >>> idx.drop_duplicates().sort_values() Index(['beetle', 'cow', 'hippo', 'lama'], dtype='object') """ sdf = self._internal.spark_frame.select( self._internal.index_spark_columns ).drop_duplicates() internal = InternalFrame( spark_frame=sdf, index_spark_columns=[ scol_for(sdf, col) for col in self._internal.index_spark_column_names ], index_names=self._internal.index_names, index_fields=self._internal.index_fields, ) return DataFrame(internal).index def to_series(self, name: Optional[Union[Any, Tuple]] = None) -> Series: """ Create a Series with both index and values equal to the index keys useful with map for returning an indexer based on an index. Parameters ---------- name : string, optional name of resulting Series. If None, defaults to name of original index Returns ------- Series : dtype will be based on the type of the Index values. Examples -------- >>> df = ps.DataFrame([(.2, .3), (.0, .6), (.6, .0), (.2, .1)], ... columns=['dogs', 'cats'], ... index=list('abcd')) >>> df['dogs'].index.to_series() a a b b c c d d dtype: object """ if not is_hashable(name): raise TypeError("Series.name must be a hashable type") scol = self.spark.column field = self._internal.data_fields[0] if name is not None: scol = scol.alias(name_like_string(name)) field = field.copy(name=name_like_string(name)) elif self._internal.index_level == 1: name = self.name column_labels = [ name if is_name_like_tuple(name) else (name,) ] # type: List[Optional[Tuple]] internal = self._internal.copy( column_labels=column_labels, data_spark_columns=[scol], data_fields=[field], column_label_names=None, ) return first_series(DataFrame(internal)) def to_frame(self, index: bool = True, name: Optional[Union[Any, Tuple]] = None) -> DataFrame: """ Create a DataFrame with a column containing the Index. Parameters ---------- index : boolean, default True Set the index of the returned DataFrame as the original Index. name : object, default None The passed name should substitute for the index name (if it has one). Returns ------- DataFrame DataFrame containing the original Index data. See Also -------- Index.to_series : Convert an Index to a Series. Series.to_frame : Convert Series to DataFrame. Examples -------- >>> idx = ps.Index(['Ant', 'Bear', 'Cow'], name='animal') >>> idx.to_frame() # doctest: +NORMALIZE_WHITESPACE animal animal Ant Ant Bear Bear Cow Cow By default, the original Index is reused. To enforce a new Index: >>> idx.to_frame(index=False) animal 0 Ant 1 Bear 2 Cow To override the name of the resulting column, specify `name`: >>> idx.to_frame(name='zoo') # doctest: +NORMALIZE_WHITESPACE zoo animal Ant Ant Bear Bear Cow Cow """ if name is None: if self._internal.index_names[0] is None: name = (DEFAULT_SERIES_NAME,) else: name = self._internal.index_names[0] elif not is_name_like_tuple(name): if is_name_like_value(name): name = (name,) else: raise TypeError("unhashable type: '{}'".format(type(name).__name__)) return self._to_frame(index=index, names=[name]) def _to_frame(self, index: bool, names: List[Tuple]) -> DataFrame: if index: index_spark_columns = self._internal.index_spark_columns index_names = self._internal.index_names index_fields = self._internal.index_fields else: index_spark_columns = [] index_names = [] index_fields = [] internal = InternalFrame( spark_frame=self._internal.spark_frame, index_spark_columns=index_spark_columns, index_names=index_names, index_fields=index_fields, column_labels=names, data_spark_columns=self._internal.index_spark_columns, data_fields=self._internal.index_fields, ) return DataFrame(internal) def is_boolean(self) -> bool: """ Return if the current index type is a boolean type. Examples -------- >>> ps.DataFrame({'a': [1]}, index=[True]).index.is_boolean() True """ return is_bool_dtype(self.dtype) def is_categorical(self) -> bool: """ Return if the current index type is a categorical type. Examples -------- >>> ps.DataFrame({'a': [1]}, index=[1]).index.is_categorical() False """ return is_categorical_dtype(self.dtype) def is_floating(self) -> bool: """ Return if the current index type is a floating type. Examples -------- >>> ps.DataFrame({'a': [1]}, index=[1]).index.is_floating() False """ return is_float_dtype(self.dtype) def is_integer(self) -> bool: """ Return if the current index type is a integer type. Examples -------- >>> ps.DataFrame({'a': [1]}, index=[1]).index.is_integer() True """ return is_integer_dtype(self.dtype) def is_interval(self) -> bool: """ Return if the current index type is an interval type. Examples -------- >>> ps.DataFrame({'a': [1]}, index=[1]).index.is_interval() False """ return is_interval_dtype(self.dtype) def is_numeric(self) -> bool: """ Return if the current index type is a numeric type. Examples -------- >>> ps.DataFrame({'a': [1]}, index=[1]).index.is_numeric() True """ return is_numeric_dtype(self.dtype) def is_object(self) -> bool: """ Return if the current index type is a object type. Examples -------- >>> ps.DataFrame({'a': [1]}, index=["a"]).index.is_object() True """ return is_object_dtype(self.dtype) def is_type_compatible(self, kind: str) -> bool: """ Whether the index type is compatible with the provided type. Examples -------- >>> psidx = ps.Index([1, 2, 3]) >>> psidx.is_type_compatible('integer') True >>> psidx = ps.Index([1.0, 2.0, 3.0]) >>> psidx.is_type_compatible('integer') False >>> psidx.is_type_compatible('floating') True """ return kind == self.inferred_type def dropna(self) -> "Index": """ Return Index or MultiIndex without NA/NaN values Examples -------- >>> df = ps.DataFrame([[1, 2], [4, 5], [7, 8]], ... index=['cobra', 'viper', None], ... columns=['max_speed', 'shield']) >>> df max_speed shield cobra 1 2 viper 4 5 NaN 7 8 >>> df.index.dropna() Index(['cobra', 'viper'], dtype='object') Also support for MultiIndex >>> midx = pd.MultiIndex([['lama', 'cow', 'falcon'], ... [None, 'weight', 'length']], ... [[0, 1, 1, 1, 1, 1, 2, 2, 2], ... [0, 1, 1, 0, 1, 2, 1, 1, 2]]) >>> s = ps.Series([45, 200, 1.2, 30, 250, 1.5, 320, 1, None], ... index=midx) >>> s lama NaN 45.0 cow weight 200.0 weight 1.2 NaN 30.0 weight 250.0 length 1.5 falcon weight 320.0 weight 1.0 length NaN dtype: float64 >>> s.index.dropna() # doctest: +SKIP MultiIndex([( 'cow', 'weight'), ( 'cow', 'weight'), ( 'cow', 'weight'), ( 'cow', 'length'), ('falcon', 'weight'), ('falcon', 'weight'), ('falcon', 'length')], ) """ sdf = self._internal.spark_frame.select(self._internal.index_spark_columns).dropna() internal = InternalFrame( spark_frame=sdf, index_spark_columns=[ scol_for(sdf, col) for col in self._internal.index_spark_column_names ], index_names=self._internal.index_names, index_fields=self._internal.index_fields, ) return DataFrame(internal).index def unique(self, level: Optional[Union[int, Any, Tuple]] = None) -> "Index": """ Return unique values in the index. Be aware the order of unique values might be different than pandas.Index.unique Parameters ---------- level : int or str, optional, default is None Returns ------- Index without duplicates See Also -------- Series.unique groupby.SeriesGroupBy.unique Examples -------- >>> ps.DataFrame({'a': ['a', 'b', 'c']}, index=[1, 1, 3]).index.unique().sort_values() Int64Index([1, 3], dtype='int64') >>> ps.DataFrame({'a': ['a', 'b', 'c']}, index=['d', 'e', 'e']).index.unique().sort_values() Index(['d', 'e'], dtype='object') MultiIndex >>> ps.MultiIndex.from_tuples([("A", "X"), ("A", "Y"), ("A", "X")]).unique() ... # doctest: +SKIP MultiIndex([('A', 'X'), ('A', 'Y')], ) """ if level is not None: self._validate_index_level(level) scols = self._internal.index_spark_columns sdf = self._psdf._internal.spark_frame.select(scols).distinct() return DataFrame( InternalFrame( spark_frame=sdf, index_spark_columns=[ scol_for(sdf, col) for col in self._internal.index_spark_column_names ], index_names=self._internal.index_names, index_fields=self._internal.index_fields, ) ).index # TODO: add error parameter def drop(self, labels: List[Any]) -> "Index": """ Make new Index with passed list of labels deleted. Parameters ---------- labels : array-like Returns ------- dropped : Index Examples -------- >>> index = ps.Index([1, 2, 3]) >>> index Int64Index([1, 2, 3], dtype='int64') >>> index.drop([1]) Int64Index([2, 3], dtype='int64') """ internal = self._internal.resolved_copy sdf = internal.spark_frame[~internal.index_spark_columns[0].isin(labels)] internal = InternalFrame( spark_frame=sdf, index_spark_columns=[ scol_for(sdf, col) for col in self._internal.index_spark_column_names ], index_names=self._internal.index_names, index_fields=self._internal.index_fields, column_labels=[], data_spark_columns=[], data_fields=[], ) return DataFrame(internal).index def _validate_index_level(self, level: Union[int, Any, Tuple]) -> None: """ Validate index level. For single-level Index getting level number is a no-op, but some verification must be done like in MultiIndex. """ if isinstance(level, int): if level < 0 and level != -1: raise IndexError( "Too many levels: Index has only 1 level," " %d is not a valid level number" % (level,) ) elif level > 0: raise IndexError("Too many levels:" " Index has only 1 level, not %d" % (level + 1)) elif level != self.name: raise KeyError( "Requested level ({}) does not match index name ({})".format(level, self.name) ) def get_level_values(self, level: Union[int, Any, Tuple]) -> "Index": """ Return Index if a valid level is given. Examples: -------- >>> psidx = ps.Index(['a', 'b', 'c'], name='ks') >>> psidx.get_level_values(0) Index(['a', 'b', 'c'], dtype='object', name='ks') >>> psidx.get_level_values('ks') Index(['a', 'b', 'c'], dtype='object', name='ks') """ self._validate_index_level(level) return self def copy( self, name: Optional[Union[Any, Tuple]] = None, deep: Optional[bool] = None ) -> "Index": """ Make a copy of this object. name sets those attributes on the new object. Parameters ---------- name : string, optional to set name of index deep : None this parameter is not supported but just dummy parameter to match pandas. Examples -------- >>> df = ps.DataFrame([[1, 2], [4, 5], [7, 8]], ... index=['cobra', 'viper', 'sidewinder'], ... columns=['max_speed', 'shield']) >>> df max_speed shield cobra 1 2 viper 4 5 sidewinder 7 8 >>> df.index Index(['cobra', 'viper', 'sidewinder'], dtype='object') Copy index >>> df.index.copy() Index(['cobra', 'viper', 'sidewinder'], dtype='object') Copy index with name >>> df.index.copy(name='snake') Index(['cobra', 'viper', 'sidewinder'], dtype='object', name='snake') """ result = self._psdf.copy().index if name: result.name = name return result def droplevel(self, level: Union[int, Any, Tuple, List[Union[int, Any, Tuple]]]) -> "Index": """ Return index with requested level(s) removed. If resulting index has only 1 level left, the result will be of Index type, not MultiIndex. Parameters ---------- level : int, str, tuple, or list-like, default 0 If a string is given, must be the name of a level If list-like, elements must be names or indexes of levels. Returns ------- Index or MultiIndex Examples -------- >>> midx = ps.DataFrame({'a': ['a', 'b']}, index=[['a', 'x'], ['b', 'y'], [1, 2]]).index >>> midx # doctest: +SKIP MultiIndex([('a', 'b', 1), ('x', 'y', 2)], ) >>> midx.droplevel([0, 1]) # doctest: +SKIP Int64Index([1, 2], dtype='int64') >>> midx.droplevel(0) # doctest: +SKIP MultiIndex([('b', 1), ('y', 2)], ) >>> midx.names = [("a", "b"), "b", "c"] >>> midx.droplevel([('a', 'b')]) # doctest: +SKIP MultiIndex([('b', 1), ('y', 2)], names=['b', 'c']) """ names = self.names nlevels = self.nlevels if not is_list_like(level): levels = [cast(Union[int, Any, Tuple], level)] else: levels = cast(List[Union[int, Any, Tuple]], level) int_level = set() for n in levels: if isinstance(n, int): if n < 0: n = n + nlevels if n < 0: raise IndexError( "Too many levels: Index has only {} levels, " "{} is not a valid level number".format(nlevels, (n - nlevels)) ) if n >= nlevels: raise IndexError( "Too many levels: Index has only {} levels, not {}".format(nlevels, n + 1) ) else: if n not in names: raise KeyError("Level {} not found".format(n)) n = names.index(n) int_level.add(n) if len(levels) >= nlevels: raise ValueError( "Cannot remove {} levels from an index with {} " "levels: at least one level must be " "left.".format(len(levels), nlevels) ) index_spark_columns, index_names, index_fields = zip( *[ item for i, item in enumerate( zip( self._internal.index_spark_columns, self._internal.index_names, self._internal.index_fields, ) ) if i not in int_level ] ) internal = self._internal.copy( index_spark_columns=list(index_spark_columns), index_names=list(index_names), index_fields=list(index_fields), column_labels=[], data_spark_columns=[], data_fields=[], ) return DataFrame(internal).index def symmetric_difference( self, other: "Index", result_name: Optional[Union[Any, Tuple]] = None, sort: Optional[bool] = None, ) -> "Index": """ Compute the symmetric difference of two Index objects. Parameters ---------- other : Index or array-like result_name : str sort : True or None, default None Whether to sort the resulting index. * True : Attempt to sort the result. * None : Do not sort the result. Returns ------- symmetric_difference : Index Notes ----- ``symmetric_difference`` contains elements that appear in either ``idx1`` or ``idx2`` but not both. Equivalent to the Index created by ``idx1.difference(idx2) | idx2.difference(idx1)`` with duplicates dropped. Examples -------- >>> s1 = ps.Series([1, 2, 3, 4], index=[1, 2, 3, 4]) >>> s2 = ps.Series([1, 2, 3, 4], index=[2, 3, 4, 5]) >>> s1.index.symmetric_difference(s2.index) # doctest: +SKIP Int64Index([5, 1], dtype='int64') You can set name of result Index. >>> s1.index.symmetric_difference(s2.index, result_name='pandas-on-Spark') # doctest: +SKIP Int64Index([5, 1], dtype='int64', name='pandas-on-Spark') You can set sort to `True`, if you want to sort the resulting index. >>> s1.index.symmetric_difference(s2.index, sort=True) Int64Index([1, 5], dtype='int64') You can also use the ``^`` operator: >>> s1.index ^ s2.index # doctest: +SKIP Int64Index([5, 1], dtype='int64') """ if type(self) != type(other): raise NotImplementedError( "Doesn't support symmetric_difference between Index & MultiIndex for now" ) sdf_self = self._psdf._internal.spark_frame.select(self._internal.index_spark_columns) sdf_other = other._psdf._internal.spark_frame.select(other._internal.index_spark_columns) sdf_symdiff = sdf_self.union(sdf_other).subtract(sdf_self.intersect(sdf_other)) if sort: sdf_symdiff = sdf_symdiff.sort(*self._internal.index_spark_column_names) internal = InternalFrame( spark_frame=sdf_symdiff, index_spark_columns=[ scol_for(sdf_symdiff, col) for col in self._internal.index_spark_column_names ], index_names=self._internal.index_names, index_fields=self._internal.index_fields, ) result = DataFrame(internal).index if result_name: result.name = result_name return result # TODO: return_indexer def sort_values(self, ascending: bool = True) -> "Index": """ Return a sorted copy of the index. .. note:: This method is not supported for pandas when index has NaN value. pandas raises unexpected TypeError, but we support treating NaN as the smallest value. Parameters ---------- ascending : bool, default True Should the index values be sorted in an ascending order. Returns ------- sorted_index : ps.Index or ps.MultiIndex Sorted copy of the index. See Also -------- Series.sort_values : Sort values of a Series. DataFrame.sort_values : Sort values in a DataFrame. Examples -------- >>> idx = ps.Index([10, 100, 1, 1000]) >>> idx Int64Index([10, 100, 1, 1000], dtype='int64') Sort values in ascending order (default behavior). >>> idx.sort_values() Int64Index([1, 10, 100, 1000], dtype='int64') Sort values in descending order. >>> idx.sort_values(ascending=False) Int64Index([1000, 100, 10, 1], dtype='int64') Support for MultiIndex. >>> psidx = ps.MultiIndex.from_tuples([('a', 'x', 1), ('c', 'y', 2), ('b', 'z', 3)]) >>> psidx # doctest: +SKIP MultiIndex([('a', 'x', 1), ('c', 'y', 2), ('b', 'z', 3)], ) >>> psidx.sort_values() # doctest: +SKIP MultiIndex([('a', 'x', 1), ('b', 'z', 3), ('c', 'y', 2)], ) >>> psidx.sort_values(ascending=False) # doctest: +SKIP MultiIndex([('c', 'y', 2), ('b', 'z', 3), ('a', 'x', 1)], ) """ sdf = self._internal.spark_frame sdf = sdf.orderBy(*self._internal.index_spark_columns, ascending=ascending).select( self._internal.index_spark_columns ) internal = InternalFrame( spark_frame=sdf, index_spark_columns=[ scol_for(sdf, col) for col in self._internal.index_spark_column_names ], index_names=self._internal.index_names, index_fields=self._internal.index_fields, ) return DataFrame(internal).index @no_type_check def sort(self, *args, **kwargs) -> None: """ Use sort_values instead. """ raise TypeError("cannot sort an Index object in-place, use sort_values instead") def min(self) -> Union[Scalar, Tuple[Scalar, ...]]: """ Return the minimum value of the Index. Returns ------- scalar Minimum value. See Also -------- Index.max : Return the maximum value of the object. Series.min : Return the minimum value in a Series. DataFrame.min : Return the minimum values in a DataFrame. Examples -------- >>> idx = ps.Index([3, 2, 1]) >>> idx.min() 1 >>> idx = ps.Index(['c', 'b', 'a']) >>> idx.min() 'a' For a MultiIndex, the maximum is determined lexicographically. >>> idx = ps.MultiIndex.from_tuples([('a', 'x', 1), ('b', 'y', 2)]) >>> idx.min() ('a', 'x', 1) """ sdf = self._internal.spark_frame min_row = cast( pd.DataFrame, sdf.select(F.min(F.struct(*self._internal.index_spark_columns)).alias("min_row")) .select("min_row.*") .toPandas(), ) result = tuple(min_row.iloc[0]) return result if len(result) > 1 else result[0] def max(self) -> Union[Scalar, Tuple[Scalar, ...]]: """ Return the maximum value of the Index. Returns ------- scalar Maximum value. See Also -------- Index.min : Return the minimum value in an Index. Series.max : Return the maximum value in a Series. DataFrame.max : Return the maximum values in a DataFrame. Examples -------- >>> idx = ps.Index([3, 2, 1]) >>> idx.max() 3 >>> idx = ps.Index(['c', 'b', 'a']) >>> idx.max() 'c' For a MultiIndex, the maximum is determined lexicographically. >>> idx = ps.MultiIndex.from_tuples([('a', 'x', 1), ('b', 'y', 2)]) >>> idx.max() ('b', 'y', 2) """ sdf = self._internal.spark_frame max_row = cast( pd.DataFrame, sdf.select(F.max(F.struct(*self._internal.index_spark_columns)).alias("max_row")) .select("max_row.*") .toPandas(), ) result = tuple(max_row.iloc[0]) return result if len(result) > 1 else result[0] def delete(self, loc: Union[int, List[int]]) -> "Index": """ Make new Index with passed location(-s) deleted. .. note:: this API can be pretty expensive since it is based on a global sequence internally. Returns ------- new_index : Index Examples -------- >>> psidx = ps.Index([10, 10, 9, 8, 4, 2, 4, 4, 2, 2, 10, 10]) >>> psidx Int64Index([10, 10, 9, 8, 4, 2, 4, 4, 2, 2, 10, 10], dtype='int64') >>> psidx.delete(0).sort_values() Int64Index([2, 2, 2, 4, 4, 4, 8, 9, 10, 10, 10], dtype='int64') >>> psidx.delete([0, 1, 2, 3, 10, 11]).sort_values() Int64Index([2, 2, 2, 4, 4, 4], dtype='int64') MultiIndex >>> psidx = ps.MultiIndex.from_tuples([('a', 'x', 1), ('b', 'y', 2), ('c', 'z', 3)]) >>> psidx # doctest: +SKIP MultiIndex([('a', 'x', 1), ('b', 'y', 2), ('c', 'z', 3)], ) >>> psidx.delete([0, 2]).sort_values() # doctest: +SKIP MultiIndex([('b', 'y', 2)], ) """ length = len(self) def is_len_exceeded(index: int) -> bool: """Check if the given index is exceeded the length or not""" return index >= length if index >= 0 else abs(index) > length if not is_list_like(loc): if is_len_exceeded(cast(int, loc)): raise IndexError( "index {} is out of bounds for axis 0 with size {}".format(loc, length) ) locs = [cast(int, loc)] else: for index in cast(List[int], loc): if is_len_exceeded(index): raise IndexError( "index {} is out of bounds for axis 0 with size {}".format(index, length) ) locs = cast(List[int], loc) locs = [int(item) for item in locs] locs = [item if item >= 0 else length + item for item in locs] # we need a temporary column such as '__index_value_0__' # since 'InternalFrame.attach_default_index' will be failed # when self._scol has name of '__index_level_0__' index_value_column_format = "__index_value_{}__" sdf = self._internal._sdf index_value_column_names = [ verify_temp_column_name(sdf, index_value_column_format.format(i)) for i in range(self._internal.index_level) ] index_value_columns = [ index_scol.alias(index_vcol_name) for index_scol, index_vcol_name in zip( self._internal.index_spark_columns, index_value_column_names ) ] sdf = sdf.select(index_value_columns) sdf, force_nullable = InternalFrame.attach_default_index( sdf, default_index_type="distributed-sequence" ) # sdf here looks as below # +-----------------+-----------------+-----------------+-----------------+ # |__index_level_0__|__index_value_0__|__index_value_1__|__index_value_2__| # +-----------------+-----------------+-----------------+-----------------+ # | 0| a| x| 1| # | 1| b| y| 2| # | 2| c| z| 3| # +-----------------+-----------------+-----------------+-----------------+ # delete rows which are matched with given `loc` sdf = sdf.where(~F.col(SPARK_INDEX_NAME_FORMAT(0)).isin(locs)) sdf = sdf.select(index_value_column_names) # sdf here looks as below, we should alias them back to origin spark column names # +-----------------+-----------------+-----------------+ # |__index_value_0__|__index_value_1__|__index_value_2__| # +-----------------+-----------------+-----------------+ # | c| z| 3| # +-----------------+-----------------+-----------------+ index_origin_columns = [ F.col(index_vcol_name).alias(index_scol_name) for index_vcol_name, index_scol_name in zip( index_value_column_names, self._internal.index_spark_column_names ) ] sdf = sdf.select(index_origin_columns) internal = InternalFrame( spark_frame=sdf, index_spark_columns=[ scol_for(sdf, col) for col in self._internal.index_spark_column_names ], index_names=self._internal.index_names, index_fields=( [field.copy(nullable=True) for field in self._internal.index_fields] if force_nullable else self._internal.index_fields ), ) return DataFrame(internal).index def append(self, other: "Index") -> "Index": """ Append a collection of Index options together. Parameters ---------- other : Index Returns ------- appended : Index Examples -------- >>> psidx = ps.Index([10, 5, 0, 5, 10, 5, 0, 10]) >>> psidx Int64Index([10, 5, 0, 5, 10, 5, 0, 10], dtype='int64') >>> psidx.append(psidx) Int64Index([10, 5, 0, 5, 10, 5, 0, 10, 10, 5, 0, 5, 10, 5, 0, 10], dtype='int64') Support for MiltiIndex >>> psidx = ps.MultiIndex.from_tuples([('a', 'x'), ('b', 'y')]) >>> psidx # doctest: +SKIP MultiIndex([('a', 'x'), ('b', 'y')], ) >>> psidx.append(psidx) # doctest: +SKIP MultiIndex([('a', 'x'), ('b', 'y'), ('a', 'x'), ('b', 'y')], ) """ from pyspark.pandas.indexes.multi import MultiIndex if type(self) is not type(other): raise NotImplementedError( "append() between Index & MultiIndex currently is not supported" ) sdf_self = self._internal.spark_frame.select(self._internal.index_spark_columns) sdf_other = other._internal.spark_frame.select(other._internal.index_spark_columns) sdf_appended = sdf_self.union(sdf_other) # names should be kept when MultiIndex, but Index wouldn't keep its name. if isinstance(self, MultiIndex): index_names = self._internal.index_names else: index_names = None internal = InternalFrame( # TODO: dtypes? spark_frame=sdf_appended, index_spark_columns=[ scol_for(sdf_appended, col) for col in self._internal.index_spark_column_names ], index_names=index_names, ) return DataFrame(internal).index def argmax(self) -> int: """ Return a maximum argument indexer. Parameters ---------- skipna : bool, default True Returns ------- maximum argument indexer Examples -------- >>> psidx = ps.Index([10, 9, 8, 7, 100, 5, 4, 3, 100, 3]) >>> psidx Int64Index([10, 9, 8, 7, 100, 5, 4, 3, 100, 3], dtype='int64') >>> psidx.argmax() 4 """ sdf = self._internal.spark_frame.select(self.spark.column) sequence_col = verify_temp_column_name(sdf, "__distributed_sequence_column__") sdf, _ = InternalFrame.attach_distributed_sequence_column(sdf, column_name=sequence_col) # spark_frame here looks like below # +-----------------+---------------+ # |__index_level_0__|__index_value__| # +-----------------+---------------+ # | 0| 10| # | 4| 100| # | 2| 8| # | 3| 7| # | 6| 4| # | 5| 5| # | 7| 3| # | 8| 100| # | 1| 9| # +-----------------+---------------+ return ( sdf.orderBy( scol_for(sdf, self._internal.data_spark_column_names[0]).desc(), F.col(sequence_col).asc(), ) .select(sequence_col) .first()[0] ) def argmin(self) -> int: """ Return a minimum argument indexer. Parameters ---------- skipna : bool, default True Returns ------- minimum argument indexer Examples -------- >>> psidx = ps.Index([10, 9, 8, 7, 100, 5, 4, 3, 100, 3]) >>> psidx Int64Index([10, 9, 8, 7, 100, 5, 4, 3, 100, 3], dtype='int64') >>> psidx.argmin() 7 """ sdf = self._internal.spark_frame.select(self.spark.column) sequence_col = verify_temp_column_name(sdf, "__distributed_sequence_column__") sdf, _ = InternalFrame.attach_distributed_sequence_column(sdf, column_name=sequence_col) return ( sdf.orderBy( scol_for(sdf, self._internal.data_spark_column_names[0]).asc(), F.col(sequence_col).asc(), ) .select(sequence_col) .first()[0] ) def set_names( self, names: Union[Any, Tuple, List[Union[Any, Tuple]]], level: Optional[Union[int, Any, Tuple, List[Union[int, Any, Tuple]]]] = None, inplace: bool = False, ) -> Optional["Index"]: """ Set Index or MultiIndex name. Able to set new names partially and by level. Parameters ---------- names : label or list of label Name(s) to set. level : int, label or list of int or label, optional If the index is a MultiIndex, level(s) to set (None for all levels). Otherwise level must be None. inplace : bool, default False Modifies the object directly, instead of creating a new Index or MultiIndex. Returns ------- Index The same type as the caller or None if inplace is True. See Also -------- Index.rename : Able to set new names without level. Examples -------- >>> idx = ps.Index([1, 2, 3, 4]) >>> idx Int64Index([1, 2, 3, 4], dtype='int64') >>> idx.set_names('quarter') Int64Index([1, 2, 3, 4], dtype='int64', name='quarter') For MultiIndex >>> idx = ps.MultiIndex.from_tuples([('a', 'x'), ('b', 'y')]) >>> idx # doctest: +SKIP MultiIndex([('a', 'x'), ('b', 'y')], ) >>> idx.set_names(['kind', 'year'], inplace=True) >>> idx # doctest: +SKIP MultiIndex([('a', 'x'), ('b', 'y')], names=['kind', 'year']) >>> idx.set_names('species', level=0) # doctest: +SKIP MultiIndex([('a', 'x'), ('b', 'y')], names=['species', 'year']) """ from pyspark.pandas.indexes.multi import MultiIndex if isinstance(self, MultiIndex): if level is not None: self_names = self.names self_names[level] = names # type: ignore names = self_names return self.rename(name=names, inplace=inplace) def difference(self, other: "Index", sort: Optional[bool] = None) -> "Index": """ Return a new Index with elements from the index that are not in `other`. This is the set difference of two Index objects. Parameters ---------- other : Index or array-like sort : True or None, default None Whether to sort the resulting index. * True : Attempt to sort the result. * None : Do not sort the result. Returns ------- difference : Index Examples -------- >>> idx1 = ps.Index([2, 1, 3, 4]) >>> idx2 = ps.Index([3, 4, 5, 6]) >>> idx1.difference(idx2, sort=True) Int64Index([1, 2], dtype='int64') MultiIndex >>> midx1 = ps.MultiIndex.from_tuples([('a', 'x', 1), ('b', 'y', 2), ('c', 'z', 3)]) >>> midx2 = ps.MultiIndex.from_tuples([('a', 'x', 1), ('b', 'z', 2), ('k', 'z', 3)]) >>> midx1.difference(midx2) # doctest: +SKIP MultiIndex([('b', 'y', 2), ('c', 'z', 3)], ) """ from pyspark.pandas.indexes.multi import MultiIndex # Check if the `self` and `other` have different index types. # 1. `self` is Index, `other` is MultiIndex # 2. `self` is MultiIndex, `other` is Index is_index_types_different = isinstance(other, Index) and not isinstance(self, type(other)) if is_index_types_different: if isinstance(self, MultiIndex): # In case `self` is MultiIndex and `other` is Index, # return MultiIndex without its names. return self.rename([None] * len(self)) elif isinstance(self, Index): # In case `self` is Index and `other` is MultiIndex, # return Index without its name. return self.rename(None) if not isinstance(other, (Index, Series, tuple, list, set, dict)): raise TypeError("Input must be Index or array-like") if not isinstance(sort, (type(None), type(True))): raise ValueError( "The 'sort' keyword only takes the values of None or True; {} was passed.".format( sort ) ) # Handling MultiIndex when `other` is not MultiIndex. if isinstance(self, MultiIndex) and not isinstance(other, MultiIndex): is_other_list_of_tuples = isinstance(other, (list, set, dict)) and all( [isinstance(item, tuple) for item in other] ) if is_other_list_of_tuples: other = MultiIndex.from_tuples(other) elif isinstance(other, Series): other = Index(other) else: raise TypeError("other must be a MultiIndex or a list of tuples") if not isinstance(other, Index): other = Index(other) sdf_self = self._internal.spark_frame sdf_other = other._internal.spark_frame idx_self = self._internal.index_spark_columns idx_other = other._internal.index_spark_columns sdf_diff = sdf_self.select(idx_self).subtract(sdf_other.select(idx_other)) internal = InternalFrame( spark_frame=sdf_diff, index_spark_columns=[ scol_for(sdf_diff, col) for col in self._internal.index_spark_column_names ], index_names=self._internal.index_names, index_fields=self._internal.index_fields, ) result = DataFrame(internal).index # Name(s) will be kept when only name(s) of (Multi)Index are the same. if isinstance(self, type(other)) and isinstance(self, MultiIndex): if self.names == other.names: result.names = self.names elif isinstance(self, type(other)) and not isinstance(self, MultiIndex): if self.name == other.name: result.name = self.name return result if sort is None else result.sort_values() @property def is_all_dates(self) -> bool: """ Return if all data types of the index are datetime. remember that since pandas-on-Spark does not support multiple data types in an index, so it returns True if any type of data is datetime. Examples -------- >>> from datetime import datetime >>> idx = ps.Index([datetime(2019, 1, 1, 0, 0, 0), datetime(2019, 2, 3, 0, 0, 0)]) >>> idx DatetimeIndex(['2019-01-01', '2019-02-03'], dtype='datetime64[ns]', freq=None) >>> idx.is_all_dates True >>> idx = ps.Index([datetime(2019, 1, 1, 0, 0, 0), None]) >>> idx DatetimeIndex(['2019-01-01', 'NaT'], dtype='datetime64[ns]', freq=None) >>> idx.is_all_dates True >>> idx = ps.Index([0, 1, 2]) >>> idx Int64Index([0, 1, 2], dtype='int64') >>> idx.is_all_dates False """ return isinstance(self.spark.data_type, TimestampType) def repeat(self, repeats: int) -> "Index": """ Repeat elements of a Index/MultiIndex. Returns a new Index/MultiIndex where each element of the current Index/MultiIndex is repeated consecutively a given number of times. Parameters ---------- repeats : int The number of repetitions for each element. This should be a non-negative integer. Repeating 0 times will return an empty Index. Returns ------- repeated_index : Index/MultiIndex Newly created Index/MultiIndex with repeated elements. See Also -------- Series.repeat : Equivalent function for Series. Examples -------- >>> idx = ps.Index(['a', 'b', 'c']) >>> idx Index(['a', 'b', 'c'], dtype='object') >>> idx.repeat(2) Index(['a', 'b', 'c', 'a', 'b', 'c'], dtype='object') For MultiIndex, >>> midx = ps.MultiIndex.from_tuples([('x', 'a'), ('x', 'b'), ('y', 'c')]) >>> midx # doctest: +SKIP MultiIndex([('x', 'a'), ('x', 'b'), ('y', 'c')], ) >>> midx.repeat(2) # doctest: +SKIP MultiIndex([('x', 'a'), ('x', 'b'), ('y', 'c'), ('x', 'a'), ('x', 'b'), ('y', 'c')], ) >>> midx.repeat(0) # doctest: +SKIP MultiIndex([], ) """ if not isinstance(repeats, int): raise TypeError( "`repeats` argument must be integer, but got {}".format(type(repeats).__name__) ) elif repeats < 0: raise ValueError("negative dimensions are not allowed") psdf = DataFrame(self._internal.resolved_copy) # type: DataFrame if repeats == 0: return DataFrame(psdf._internal.with_filter(SF.lit(False))).index else: return ps.concat([psdf] * repeats).index def asof(self, label: Any) -> Scalar: """ Return the label from the index, or, if not present, the previous one. Assuming that the index is sorted, return the passed index label if it is in the index, or return the previous index label if the passed one is not in the index. .. note:: This API is dependent on :meth:`Index.is_monotonic_increasing` which can be expensive. Parameters ---------- label : object The label up to which the method returns the latest index label. Returns ------- object The passed label if it is in the index. The previous label if the passed label is not in the sorted index or `NaN` if there is no such label. Examples -------- `Index.asof` returns the latest index label up to the passed label. >>> idx = ps.Index(['2013-12-31', '2014-01-02', '2014-01-03']) >>> idx.asof('2014-01-01') '2013-12-31' If the label is in the index, the method returns the passed label. >>> idx.asof('2014-01-02') '2014-01-02' If all of the labels in the index are later than the passed label, NaN is returned. >>> idx.asof('1999-01-02') nan """ sdf = self._internal.spark_frame if self.is_monotonic_increasing: sdf = sdf.where(self.spark.column <= SF.lit(label).cast(self.spark.data_type)).select( F.max(self.spark.column) ) elif self.is_monotonic_decreasing: sdf = sdf.where(self.spark.column >= SF.lit(label).cast(self.spark.data_type)).select( F.min(self.spark.column) ) else: raise ValueError("index must be monotonic increasing or decreasing") result = cast(pd.DataFrame, sdf.toPandas()).iloc[0, 0] return result if result is not None else np.nan def union( self, other: Union[DataFrame, Series, "Index", List], sort: Optional[bool] = None ) -> "Index": """ Form the union of two Index objects. Parameters ---------- other : Index or array-like sort : bool or None, default None Whether to sort the resulting Index. Returns ------- union : Index Examples -------- Index >>> idx1 = ps.Index([1, 2, 3, 4]) >>> idx2 = ps.Index([3, 4, 5, 6]) >>> idx1.union(idx2).sort_values() Int64Index([1, 2, 3, 4, 5, 6], dtype='int64') MultiIndex >>> midx1 = ps.MultiIndex.from_tuples([("x", "a"), ("x", "b"), ("x", "c"), ("x", "d")]) >>> midx2 = ps.MultiIndex.from_tuples([("x", "c"), ("x", "d"), ("x", "e"), ("x", "f")]) >>> midx1.union(midx2).sort_values() # doctest: +SKIP MultiIndex([('x', 'a'), ('x', 'b'), ('x', 'c'), ('x', 'd'), ('x', 'e'), ('x', 'f')], ) """ from pyspark.pandas.indexes.multi import MultiIndex sort = True if sort is None else sort sort = validate_bool_kwarg(sort, "sort") if type(self) is not type(other): if isinstance(self, MultiIndex): if not isinstance(other, list) or not all( [isinstance(item, tuple) for item in other] ): raise TypeError("other must be a MultiIndex or a list of tuples") other_idx = MultiIndex.from_tuples(other) # type: Index else: if isinstance(other, MultiIndex): # TODO: We can't support different type of values in a single column for now. raise NotImplementedError( "Union between Index and MultiIndex is not yet supported" ) elif isinstance(other, Series): other_frame = other.to_frame() other_idx = other_frame.set_index(other_frame.columns[0]).index elif isinstance(other, DataFrame): raise ValueError("Index data must be 1-dimensional") else: other_idx = Index(other) else: other_idx = cast(Index, other) sdf_self = self._internal.spark_frame.select(self._internal.index_spark_columns) sdf_other = other_idx._internal.spark_frame.select(other_idx._internal.index_spark_columns) sdf = sdf_self.union(sdf_other.subtract(sdf_self)) if isinstance(self, MultiIndex): sdf = sdf.drop_duplicates() if sort: sdf = sdf.sort(*self._internal.index_spark_column_names) internal = InternalFrame( # TODO: dtypes? spark_frame=sdf, index_spark_columns=[ scol_for(sdf, col) for col in self._internal.index_spark_column_names ], index_names=self._internal.index_names, ) return DataFrame(internal).index def holds_integer(self) -> bool: """ Whether the type is an integer type. Always return False for MultiIndex. Notes ----- When Index contains null values the result can be different with pandas since pandas-on-Spark cast integer to float when Index contains null values. >>> ps.Index([1, 2, 3, None]) Float64Index([1.0, 2.0, 3.0, nan], dtype='float64') Examples -------- >>> psidx = ps.Index([1, 2, 3, 4]) >>> psidx.holds_integer() True Returns False for string type. >>> psidx = ps.Index(["A", "B", "C", "D"]) >>> psidx.holds_integer() False Returns False for float type. >>> psidx = ps.Index([1.1, 2.2, 3.3, 4.4]) >>> psidx.holds_integer() False """ return isinstance(self.spark.data_type, IntegralType) def intersection(self, other: Union[DataFrame, Series, "Index", List]) -> "Index": """ Form the intersection of two Index objects. This returns a new Index with elements common to the index and `other`. Parameters ---------- other : Index or array-like Returns ------- intersection : Index Examples -------- >>> idx1 = ps.Index([1, 2, 3, 4]) >>> idx2 = ps.Index([3, 4, 5, 6]) >>> idx1.intersection(idx2).sort_values() Int64Index([3, 4], dtype='int64') """ from pyspark.pandas.indexes.multi import MultiIndex if isinstance(other, DataFrame): raise ValueError("Index data must be 1-dimensional") elif isinstance(other, MultiIndex): # Always returns a no-named empty Index if `other` is MultiIndex. return self._psdf.head(0).index.rename(None) elif isinstance(other, Index): spark_frame_other = other.to_frame().to_spark() keep_name = self.name == other.name elif isinstance(other, Series): spark_frame_other = other.to_frame().to_spark() keep_name = True elif is_list_like(other): other = Index(other) if isinstance(other, MultiIndex): return other.to_frame().head(0).index spark_frame_other = other.to_frame().to_spark() keep_name = True else: raise TypeError("Input must be Index or array-like") spark_frame_self = self.to_frame(name=SPARK_DEFAULT_INDEX_NAME).to_spark() spark_frame_intersected = spark_frame_self.intersect(spark_frame_other) if keep_name: index_names = self._internal.index_names else: index_names = None internal = InternalFrame( # TODO: dtypes? spark_frame=spark_frame_intersected, index_spark_columns=[scol_for(spark_frame_intersected, SPARK_DEFAULT_INDEX_NAME)], index_names=index_names, ) return DataFrame(internal).index def item(self) -> Union[Scalar, Tuple[Scalar, ...]]: """ Return the first element of the underlying data as a python scalar. Returns ------- scalar The first element of Index. Raises ------ ValueError If the data is not length-1. Examples -------- >>> psidx = ps.Index([10]) >>> psidx.item() 10 """ return self.to_series().item() def insert(self, loc: int, item: Any) -> "Index": """ Make new Index inserting new item at location. Follows Python list.append semantics for negative values. Parameters ---------- loc : int item : object Returns ------- new_index : Index Examples -------- >>> psidx = ps.Index([1, 2, 3, 4, 5]) >>> psidx.insert(3, 100) Int64Index([1, 2, 3, 100, 4, 5], dtype='int64') For negative values >>> psidx = ps.Index([1, 2, 3, 4, 5]) >>> psidx.insert(-3, 100) Int64Index([1, 2, 100, 3, 4, 5], dtype='int64') """ if loc < 0: length = len(self) loc = loc + length loc = 0 if loc < 0 else loc index_name = self._internal.index_spark_column_names[0] sdf_before = self.to_frame(name=index_name)[:loc].to_spark() sdf_middle = Index([item]).to_frame(name=index_name).to_spark() sdf_after = self.to_frame(name=index_name)[loc:].to_spark() sdf = sdf_before.union(sdf_middle).union(sdf_after) internal = InternalFrame( # TODO: dtype? spark_frame=sdf, index_spark_columns=[ scol_for(sdf, col) for col in self._internal.index_spark_column_names ], index_names=self._internal.index_names, ) return DataFrame(internal).index def view(self) -> "Index": """ this is defined as a copy with the same identity """ return self.copy() def to_list(self) -> List: """ Return a list of the values. These are each a scalar type, which is a Python scalar (for str, int, float) or a pandas scalar (for Timestamp/Timedelta/Interval/Period) .. note:: This method should only be used if the resulting list is expected to be small, as all the data is loaded into the driver's memory. Examples -------- Index >>> idx = ps.Index([1, 2, 3, 4, 5]) >>> idx.to_list() [1, 2, 3, 4, 5] MultiIndex >>> tuples = [(1, 'red'), (1, 'blue'), (2, 'red'), (2, 'green')] >>> midx = ps.MultiIndex.from_tuples(tuples) >>> midx.to_list() [(1, 'red'), (1, 'blue'), (2, 'red'), (2, 'green')] """ return self._to_internal_pandas().tolist() tolist = to_list @property def inferred_type(self) -> str: """ Return a string of the type inferred from the values. Examples -------- >>> from datetime import datetime >>> ps.Index([1, 2, 3]).inferred_type 'integer' >>> ps.Index([1.0, 2.0, 3.0]).inferred_type 'floating' >>> ps.Index(['a', 'b', 'c']).inferred_type 'string' >>> ps.Index([True, False, True, False]).inferred_type 'boolean' """ return lib.infer_dtype([self.to_series().head(1).item()]) def __getattr__(self, item: str) -> Any: if hasattr(MissingPandasLikeIndex, item): property_or_func = getattr(MissingPandasLikeIndex, item) if isinstance(property_or_func, property): return property_or_func.fget(self) # type: ignore else: return partial(property_or_func, self) raise AttributeError("'{}' object has no attribute '{}'".format(type(self).__name__, item)) def __repr__(self) -> str: max_display_count = get_option("display.max_rows") if max_display_count is None: return repr(self._to_internal_pandas()) pindex = self._psdf._get_or_create_repr_pandas_cache(max_display_count).index pindex_length = len(pindex) repr_string = repr(pindex[:max_display_count]) if pindex_length > max_display_count: footer = "\nShowing only the first {}".format(max_display_count) return repr_string + footer return repr_string def __iter__(self) -> Iterator: return MissingPandasLikeIndex.__iter__(self) def __xor__(self, other: "Index") -> "Index": return self.symmetric_difference(other) def __bool__(self) -> bool: raise ValueError( "The truth value of a {0} is ambiguous. " "Use a.empty, a.bool(), a.item(), a.any() or a.all().".format(self.__class__.__name__) ) def _test() -> None: import os import doctest import sys from pyspark.sql import SparkSession import pyspark.pandas.indexes.base os.chdir(os.environ["SPARK_HOME"]) globs = pyspark.pandas.indexes.base.__dict__.copy() globs["ps"] = pyspark.pandas spark = ( SparkSession.builder.master("local[4]") .appName("pyspark.pandas.indexes.base tests") .getOrCreate() ) (failure_count, test_count) = doctest.testmod( pyspark.pandas.indexes.base, globs=globs, optionflags=doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE, ) spark.stop() if failure_count: sys.exit(-1) if __name__ == "__main__": _test()
apache-2.0
danforthcenter/plantcv
tests/tests.py
1
288502
#!/usr/bin/env python import pytest import os import shutil import json import numpy as np import cv2 import sys import pandas as pd from plotnine import ggplot from plantcv import plantcv as pcv import plantcv.learn import plantcv.parallel import plantcv.utils # Import matplotlib and use a null Template to block plotting to screen # This will let us test debug = "plot" import matplotlib import matplotlib.pyplot as plt import dask from dask.distributed import Client from skimage import img_as_ubyte PARALLEL_TEST_DATA = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parallel_data") TEST_TMPDIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", ".cache") TEST_IMG_DIR = "images" TEST_IMG_DIR2 = "images_w_date" TEST_SNAPSHOT_DIR = "snapshots" TEST_PIPELINE = os.path.join(PARALLEL_TEST_DATA, "plantcv-script.py") META_FIELDS = {"imgtype": 0, "camera": 1, "frame": 2, "zoom": 3, "lifter": 4, "gain": 5, "exposure": 6, "id": 7} VALID_META = { # Camera settings "camera": { "label": "camera identifier", "datatype": "<class 'str'>", "value": "none" }, "imgtype": { "label": "image type", "datatype": "<class 'str'>", "value": "none" }, "zoom": { "label": "camera zoom setting", "datatype": "<class 'str'>", "value": "none" }, "exposure": { "label": "camera exposure setting", "datatype": "<class 'str'>", "value": "none" }, "gain": { "label": "camera gain setting", "datatype": "<class 'str'>", "value": "none" }, "frame": { "label": "image series frame identifier", "datatype": "<class 'str'>", "value": "none" }, "lifter": { "label": "imaging platform height setting", "datatype": "<class 'str'>", "value": "none" }, # Date-Time "timestamp": { "label": "datetime of image", "datatype": "<class 'datetime.datetime'>", "value": None }, # Sample attributes "id": { "label": "image identifier", "datatype": "<class 'str'>", "value": "none" }, "plantbarcode": { "label": "plant barcode identifier", "datatype": "<class 'str'>", "value": "none" }, "treatment": { "label": "treatment identifier", "datatype": "<class 'str'>", "value": "none" }, "cartag": { "label": "plant carrier identifier", "datatype": "<class 'str'>", "value": "none" }, # Experiment attributes "measurementlabel": { "label": "experiment identifier", "datatype": "<class 'str'>", "value": "none" }, # Other "other": { "label": "other identifier", "datatype": "<class 'str'>", "value": "none" } } METADATA_COPROCESS = { 'VIS_SV_0_z1_h1_g0_e82_117770.jpg': { 'path': os.path.join(PARALLEL_TEST_DATA, 'snapshots', 'snapshot57383', 'VIS_SV_0_z1_h1_g0_e82_117770.jpg'), 'camera': 'SV', 'imgtype': 'VIS', 'zoom': 'z1', 'exposure': 'e82', 'gain': 'g0', 'frame': '0', 'lifter': 'h1', 'timestamp': '2014-10-22 17:49:35.187', 'id': '117770', 'plantbarcode': 'Ca031AA010564', 'treatment': 'none', 'cartag': '2143', 'measurementlabel': 'C002ch_092214_biomass', 'other': 'none', 'coimg': 'NIR_SV_0_z1_h1_g0_e65_117779.jpg' }, 'NIR_SV_0_z1_h1_g0_e65_117779.jpg': { 'path': os.path.join(PARALLEL_TEST_DATA, 'snapshots', 'snapshot57383', 'NIR_SV_0_z1_h1_g0_e65_117779.jpg'), 'camera': 'SV', 'imgtype': 'NIR', 'zoom': 'z1', 'exposure': 'e65', 'gain': 'g0', 'frame': '0', 'lifter': 'h1', 'timestamp': '2014-10-22 17:49:35.187', 'id': '117779', 'plantbarcode': 'Ca031AA010564', 'treatment': 'none', 'cartag': '2143', 'measurementlabel': 'C002ch_092214_biomass', 'other': 'none' } } METADATA_VIS_ONLY = { 'VIS_SV_0_z1_h1_g0_e82_117770.jpg': { 'path': os.path.join(PARALLEL_TEST_DATA, 'snapshots', 'snapshot57383', 'VIS_SV_0_z1_h1_g0_e82_117770.jpg'), 'camera': 'SV', 'imgtype': 'VIS', 'zoom': 'z1', 'exposure': 'e82', 'gain': 'g0', 'frame': '0', 'lifter': 'h1', 'timestamp': '2014-10-22 17:49:35.187', 'id': '117770', 'plantbarcode': 'Ca031AA010564', 'treatment': 'none', 'cartag': '2143', 'measurementlabel': 'C002ch_092214_biomass', 'other': 'none' } } METADATA_NIR_ONLY = { 'NIR_SV_0_z1_h1_g0_e65_117779.jpg': { 'path': os.path.join(PARALLEL_TEST_DATA, 'snapshots', 'snapshot57383', 'NIR_SV_0_z1_h1_g0_e65_117779.jpg'), 'camera': 'SV', 'imgtype': 'NIR', 'zoom': 'z1', 'exposure': 'e65', 'gain': 'g0', 'frame': '0', 'lifter': 'h1', 'timestamp': '2014-10-22 17:49:35.187', 'id': '117779', 'plantbarcode': 'Ca031AA010564', 'treatment': 'none', 'cartag': '2143', 'measurementlabel': 'C002ch_092214_biomass', 'other': 'none' } } # Set the temp directory for dask dask.config.set(temporary_directory=TEST_TMPDIR) # ########################## # Tests setup function # ########################## def setup_function(): if not os.path.exists(TEST_TMPDIR): os.mkdir(TEST_TMPDIR) # ############################## # Tests for the parallel subpackage # ############################## def test_plantcv_parallel_workflowconfig_save_config_file(): # Create a test tmp directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_parallel_workflowconfig_save_config_file") os.mkdir(cache_dir) # Define output path/filename template_file = os.path.join(cache_dir, "config.json") # Create config instance config = plantcv.parallel.WorkflowConfig() # Save template file config.save_config(config_file=template_file) assert os.path.exists(template_file) def test_plantcv_parallel_workflowconfig_import_config_file(): # Define input path/filename config_file = os.path.join(PARALLEL_TEST_DATA, "workflow_config_template.json") # Create config instance config = plantcv.parallel.WorkflowConfig() # import config file config.import_config(config_file=config_file) assert config.cluster == "LocalCluster" def test_plantcv_parallel_workflowconfig_validate_config(): # Create a test tmp directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_parallel_workflowconfig_validate_config") os.mkdir(cache_dir) # Create config instance config = plantcv.parallel.WorkflowConfig() # Set valid values in config config.input_dir = os.path.join(PARALLEL_TEST_DATA, "images") config.json = os.path.join(cache_dir, "valid_config.json") config.filename_metadata = ["imgtype", "camera", "frame", "zoom", "lifter", "gain", "exposure", "id"] config.workflow = TEST_PIPELINE config.img_outdir = cache_dir # Validate config assert config.validate_config() def test_plantcv_parallel_workflowconfig_invalid_startdate(): # Create a test tmp directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_parallel_workflowconfig_invalid_startdate") os.mkdir(cache_dir) # Create config instance config = plantcv.parallel.WorkflowConfig() # Set valid values in config config.input_dir = os.path.join(PARALLEL_TEST_DATA, "images") config.json = os.path.join(cache_dir, "valid_config.json") config.filename_metadata = ["imgtype", "camera", "frame", "zoom", "lifter", "gain", "exposure", "id"] config.workflow = TEST_PIPELINE config.img_outdir = cache_dir config.start_date = "2020-05-10" # Validate config assert not config.validate_config() def test_plantcv_parallel_workflowconfig_invalid_enddate(): # Create a test tmp directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_parallel_workflowconfig_invalid_enddate") os.mkdir(cache_dir) # Create config instance config = plantcv.parallel.WorkflowConfig() # Set valid values in config config.input_dir = os.path.join(PARALLEL_TEST_DATA, "images") config.json = os.path.join(cache_dir, "valid_config.json") config.filename_metadata = ["imgtype", "camera", "frame", "zoom", "lifter", "gain", "exposure", "id"] config.workflow = TEST_PIPELINE config.img_outdir = cache_dir config.end_date = "2020-05-10" config.timestampformat = "%Y%m%d" # Validate config assert not config.validate_config() def test_plantcv_parallel_workflowconfig_invalid_metadata_terms(): # Create a test tmp directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_parallel_workflowconfig_invalid_metadata_terms") os.mkdir(cache_dir) # Create config instance config = plantcv.parallel.WorkflowConfig() # Set invalid values in config # input_dir and json are not defined by default, but are required # Set an incorrect metadata term config.filename_metadata.append("invalid") # Validate config assert not config.validate_config() def test_plantcv_parallel_workflowconfig_invalid_filename_metadata(): # Create a test tmp directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_parallel_workflowconfig_invalid_filename_metadata") os.mkdir(cache_dir) # Create config instance config = plantcv.parallel.WorkflowConfig() # Set invalid values in config # input_dir and json are not defined by default, but are required # Do not set required filename_metadata # Validate config assert not config.validate_config() def test_plantcv_parallel_workflowconfig_invalid_cluster(): # Create a test tmp directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_parallel_workflowconfig_invalid_cluster") os.mkdir(cache_dir) # Create config instance config = plantcv.parallel.WorkflowConfig() # Set invalid values in config # input_dir and json are not defined by default, but are required # Set invalid cluster type config.cluster = "MyCluster" # Validate config assert not config.validate_config() def test_plantcv_parallel_metadata_parser_snapshots(): # Create config instance config = plantcv.parallel.WorkflowConfig() config.input_dir = os.path.join(PARALLEL_TEST_DATA, TEST_SNAPSHOT_DIR) config.json = os.path.join(TEST_TMPDIR, "test_plantcv_parallel_metadata_parser_snapshots", "output.json") config.filename_metadata = ["imgtype", "camera", "frame", "zoom", "lifter", "gain", "exposure", "id"] config.workflow = TEST_PIPELINE config.metadata_filters = {"imgtype": "VIS", "camera": "SV"} config.start_date = "2014-10-21 00:00:00.0" config.end_date = "2014-10-23 00:00:00.0" config.timestampformat = '%Y-%m-%d %H:%M:%S.%f' config.imgformat = "jpg" meta = plantcv.parallel.metadata_parser(config=config) assert meta == METADATA_VIS_ONLY def test_plantcv_parallel_metadata_parser_snapshots_coimg(): # Create config instance config = plantcv.parallel.WorkflowConfig() config.input_dir = os.path.join(PARALLEL_TEST_DATA, TEST_SNAPSHOT_DIR) config.json = os.path.join(TEST_TMPDIR, "test_plantcv_parallel_metadata_parser_snapshots_coimg", "output.json") config.filename_metadata = ["imgtype", "camera", "frame", "zoom", "lifter", "gain", "exposure", "id"] config.workflow = TEST_PIPELINE config.metadata_filters = {"imgtype": "VIS"} config.start_date = "2014-10-21 00:00:00.0" config.end_date = "2014-10-23 00:00:00.0" config.timestampformat = '%Y-%m-%d %H:%M:%S.%f' config.imgformat = "jpg" config.coprocess = "FAKE" meta = plantcv.parallel.metadata_parser(config=config) assert meta == METADATA_VIS_ONLY def test_plantcv_parallel_metadata_parser_images(): # Create config instance config = plantcv.parallel.WorkflowConfig() config.input_dir = os.path.join(PARALLEL_TEST_DATA, TEST_IMG_DIR) config.json = os.path.join(TEST_TMPDIR, "test_plantcv_parallel_metadata_parser_images", "output.json") config.filename_metadata = ["imgtype", "camera", "frame", "zoom", "lifter", "gain", "exposure", "id"] config.workflow = TEST_PIPELINE config.metadata_filters = {"imgtype": "VIS"} config.start_date = "2014" config.end_date = "2014" config.timestampformat = '%Y' # no date in filename so check date range and date_format are ignored config.imgformat = "jpg" meta = plantcv.parallel.metadata_parser(config=config) expected = { 'VIS_SV_0_z1_h1_g0_e82_117770.jpg': { 'path': os.path.join(PARALLEL_TEST_DATA, 'images', 'VIS_SV_0_z1_h1_g0_e82_117770.jpg'), 'camera': 'SV', 'imgtype': 'VIS', 'zoom': 'z1', 'exposure': 'e82', 'gain': 'g0', 'frame': '0', 'lifter': 'h1', 'timestamp': None, 'id': '117770', 'plantbarcode': 'none', 'treatment': 'none', 'cartag': 'none', 'measurementlabel': 'none', 'other': 'none'} } assert meta == expected config.include_all_subdirs = False meta = plantcv.parallel.metadata_parser(config=config) assert meta == expected def test_plantcv_parallel_metadata_parser_multivalue_filter(): # Create config instance config = plantcv.parallel.WorkflowConfig() config.input_dir = os.path.join(PARALLEL_TEST_DATA, TEST_IMG_DIR) config.json = os.path.join(TEST_TMPDIR, "test_plantcv_parallel_metadata_parser_images", "output.json") config.filename_metadata = ["imgtype", "camera", "frame", "zoom", "lifter", "gain", "exposure", "id"] config.workflow = TEST_PIPELINE config.metadata_filters = {"imgtype": ["VIS", "NIR"]} config.imgformat = "jpg" meta = plantcv.parallel.metadata_parser(config=config) expected = { 'VIS_SV_0_z1_h1_g0_e82_117770.jpg': { 'path': os.path.join(PARALLEL_TEST_DATA, TEST_IMG_DIR, 'VIS_SV_0_z1_h1_g0_e82_117770.jpg'), 'camera': 'SV', 'imgtype': 'VIS', 'zoom': 'z1', 'exposure': 'e82', 'gain': 'g0', 'frame': '0', 'lifter': 'h1', 'timestamp': None, 'id': '117770', 'plantbarcode': 'none', 'treatment': 'none', 'cartag': 'none', 'measurementlabel': 'none', 'other': 'none' }, 'NIR_SV_0_z1_h1_g0_e65_117779.jpg': { 'path': os.path.join(PARALLEL_TEST_DATA, TEST_IMG_DIR, 'NIR_SV_0_z1_h1_g0_e65_117779.jpg'), 'camera': 'SV', 'imgtype': 'NIR', 'zoom': 'z1', 'exposure': 'e65', 'gain': 'g0', 'frame': '0', 'lifter': 'h1', 'timestamp': None, 'id': '117779', 'plantbarcode': 'none', 'treatment': 'none', 'cartag': 'none', 'measurementlabel': 'none', 'other': 'none' } } assert meta == expected def test_plantcv_parallel_metadata_parser_multivalue_filter_nomatch(): # Create config instance config = plantcv.parallel.WorkflowConfig() config.input_dir = os.path.join(PARALLEL_TEST_DATA, TEST_IMG_DIR) config.json = os.path.join(TEST_TMPDIR, "test_plantcv_parallel_metadata_parser_images", "output.json") config.filename_metadata = ["imgtype", "camera", "frame", "zoom", "lifter", "gain", "exposure", "id"] config.workflow = TEST_PIPELINE config.metadata_filters = {"imgtype": ["VIS", "PSII"]} config.imgformat = "jpg" meta = plantcv.parallel.metadata_parser(config=config) expected = { 'VIS_SV_0_z1_h1_g0_e82_117770.jpg': { 'path': os.path.join(PARALLEL_TEST_DATA, TEST_IMG_DIR, 'VIS_SV_0_z1_h1_g0_e82_117770.jpg'), 'camera': 'SV', 'imgtype': 'VIS', 'zoom': 'z1', 'exposure': 'e82', 'gain': 'g0', 'frame': '0', 'lifter': 'h1', 'timestamp': None, 'id': '117770', 'plantbarcode': 'none', 'treatment': 'none', 'cartag': 'none', 'measurementlabel': 'none', 'other': 'none' } } assert meta == expected def test_plantcv_parallel_metadata_parser_regex(): # Create config instance config = plantcv.parallel.WorkflowConfig() config.input_dir = os.path.join(PARALLEL_TEST_DATA, TEST_IMG_DIR) config.json = os.path.join(TEST_TMPDIR, "test_plantcv_parallel_metadata_parser_images", "output.json") config.filename_metadata = ["imgtype", "camera", "frame", "zoom", "lifter", "gain", "exposure", "id"] config.workflow = TEST_PIPELINE config.metadata_filters = {"imgtype": "VIS"} config.start_date = "2014-10-21 00:00:00.0" config.end_date = "2014-10-23 00:00:00.0" config.timestampformat = '%Y-%m-%d %H:%M:%S.%f' config.imgformat = "jpg" config.delimiter = r'(VIS)_(SV)_(\d+)_(z1)_(h1)_(g0)_(e82)_(\d+)' meta = plantcv.parallel.metadata_parser(config=config) expected = { 'VIS_SV_0_z1_h1_g0_e82_117770.jpg': { 'path': os.path.join(PARALLEL_TEST_DATA, 'images', 'VIS_SV_0_z1_h1_g0_e82_117770.jpg'), 'camera': 'SV', 'imgtype': 'VIS', 'zoom': 'z1', 'exposure': 'e82', 'gain': 'g0', 'frame': '0', 'lifter': 'h1', 'timestamp': None, 'id': '117770', 'plantbarcode': 'none', 'treatment': 'none', 'cartag': 'none', 'measurementlabel': 'none', 'other': 'none'} } assert meta == expected def test_plantcv_parallel_metadata_parser_images_outside_daterange(): # Create config instance config = plantcv.parallel.WorkflowConfig() config.input_dir = os.path.join(PARALLEL_TEST_DATA, TEST_IMG_DIR2) config.json = os.path.join(TEST_TMPDIR, "test_plantcv_parallel_metadata_parser_images_outside_daterange", "output.json") config.filename_metadata = ["imgtype", "camera", "frame", "zoom", "lifter", "gain", "exposure", "timestamp"] config.workflow = TEST_PIPELINE config.metadata_filters = {"imgtype": "NIR"} config.start_date = "1970-01-01 00_00_00" config.end_date = "1970-01-01 00_00_00" config.timestampformat = "%Y-%m-%d %H_%M_%S" config.imgformat = "jpg" config.delimiter = r"(NIR)_(SV)_(\d)_(z1)_(h1)_(g0)_(e65)_(\d{4}-\d{2}-\d{2} \d{2}_\d{2}_\d{2})" meta = plantcv.parallel.metadata_parser(config=config) assert meta == {} def test_plantcv_parallel_metadata_parser_no_default_dates(): # Create config instance config = plantcv.parallel.WorkflowConfig() config.input_dir = os.path.join(PARALLEL_TEST_DATA, TEST_SNAPSHOT_DIR) config.json = os.path.join(TEST_TMPDIR, "test_plantcv_parallel_metadata_parser_no_default_dates", "output.json") config.filename_metadata = ["imgtype", "camera", "frame", "zoom", "lifter", "gain", "exposure", "id"] config.workflow = TEST_PIPELINE config.metadata_filters = {"imgtype": "VIS", "camera": "SV", "id": "117770"} config.start_date = None config.end_date = None config.timestampformat = '%Y-%m-%d %H:%M:%S.%f' config.imgformat = "jpg" meta = plantcv.parallel.metadata_parser(config=config) assert meta == METADATA_VIS_ONLY def test_plantcv_parallel_workflowconfig_subdaily_timestampformat(): ''' timestampformats with only hours and smaller units of time were failing if the script was run earlier in the day than the images were taken. this was fixed by setting end_date to 23-59-59 if we don't detect the year-month-day ''' # Create config instance config = plantcv.parallel.WorkflowConfig() config.input_dir = os.path.join(PARALLEL_TEST_DATA, TEST_IMG_DIR2) config.json = os.path.join(TEST_IMG_DIR2, "test_plantcv_parallel_metadata_parser_subdaily_timestampformat", "output.json") config.filename_metadata = ["imgtype", "camera", "frame", "zoom", "lifter", "gain", "exposure", "timestamp"] config.workflow = TEST_PIPELINE config.metadata_filters = {"imgtype": "NIR", "camera": "SV"} config.start_date = None config.end_date = None config.timestampformat = "%H_%M_%S" config.imgformat = "jpg" config.delimiter = r"(NIR)_(SV)_(\d)_(z1)_(h1)_(g0)_(e65)_(\d{2}_\d{2}_\d{2})" meta = plantcv.parallel.metadata_parser(config=config) assert meta == { 'NIR_SV_0_z1_h1_g0_e65_23_59_59.jpg': { 'path': os.path.join(PARALLEL_TEST_DATA, 'images_w_date','NIR_SV_0_z1_h1_g0_e65_23_59_59.jpg'), 'imgtype': 'NIR', 'camera': 'SV', 'frame': '0', 'zoom': 'z1', 'lifter': 'h1', 'gain': 'g0', 'exposure': 'e65', 'timestamp': '23_59_59', 'measurementlabel': 'none', 'cartag':'none', 'id': 'none', 'treatment': 'none', 'plantbarcode': 'none', 'other': 'none' } } def test_plantcv_parallel_check_date_range_wrongdateformat(): start_date = 10 end_date = 10 img_time = '2010-10-10' with pytest.raises(SystemExit, match=r'does not match format'): date_format = '%Y%m%d' _ = plantcv.parallel.check_date_range( start_date, end_date, img_time, date_format) def test_plantcv_parallel_metadata_parser_snapshot_outside_daterange(): # Create config instance config = plantcv.parallel.WorkflowConfig() config.input_dir = os.path.join(PARALLEL_TEST_DATA, TEST_SNAPSHOT_DIR) config.json = os.path.join(TEST_TMPDIR, "test_plantcv_parallel_metadata_parser_snapshot_outside_daterange", "output.json") config.filename_metadata = ["imgtype", "camera", "frame", "zoom", "lifter", "gain", "exposure", "id"] config.workflow = TEST_PIPELINE config.metadata_filters = {"imgtype": "VIS"} config.start_date = "1970-01-01 00:00:00.0" config.end_date = "1970-01-01 00:00:00.0" config.timestampformat = '%Y-%m-%d %H:%M:%S.%f' config.imgformat = "jpg" meta = plantcv.parallel.metadata_parser(config=config) assert meta == {} def test_plantcv_parallel_metadata_parser_fail_images(): # Create config instance config = plantcv.parallel.WorkflowConfig() config.input_dir = os.path.join(PARALLEL_TEST_DATA, TEST_SNAPSHOT_DIR) config.json = os.path.join(TEST_TMPDIR, "test_plantcv_parallel_metadata_parser_fail_images", "output.json") config.filename_metadata = ["imgtype", "camera", "frame", "zoom", "lifter", "gain", "exposure", "id"] config.workflow = TEST_PIPELINE config.metadata_filters = {"cartag": "VIS"} config.start_date = "1970-01-01 00:00:00.0" config.end_date = "1970-01-01 00:00:00.0" config.timestampformat = '%Y-%m-%d %H:%M:%S.%f' config.imgformat = "jpg" config.coprocess = "NIR" meta = plantcv.parallel.metadata_parser(config=config) assert meta == METADATA_NIR_ONLY def test_plantcv_parallel_metadata_parser_images_with_frame(): # Create config instance config = plantcv.parallel.WorkflowConfig() config.input_dir = os.path.join(PARALLEL_TEST_DATA, TEST_SNAPSHOT_DIR) config.json = os.path.join(TEST_TMPDIR, "test_plantcv_parallel_metadata_parser_images_with_frame", "output.json") config.filename_metadata = ["imgtype", "camera", "frame", "zoom", "lifter", "gain", "exposure", "id"] config.workflow = TEST_PIPELINE config.metadata_filters = {"imgtype": "VIS"} config.start_date = "2014-10-21 00:00:00.0" config.end_date = "2014-10-23 00:00:00.0" config.timestampformat = '%Y-%m-%d %H:%M:%S.%f' config.imgformat = "jpg" config.coprocess = "NIR" meta = plantcv.parallel.metadata_parser(config=config) assert meta == { 'VIS_SV_0_z1_h1_g0_e82_117770.jpg': { 'path': os.path.join(PARALLEL_TEST_DATA, 'snapshots', 'snapshot57383', 'VIS_SV_0_z1_h1_g0_e82_117770.jpg'), 'camera': 'SV', 'imgtype': 'VIS', 'zoom': 'z1', 'exposure': 'e82', 'gain': 'g0', 'frame': '0', 'lifter': 'h1', 'timestamp': '2014-10-22 17:49:35.187', 'id': '117770', 'plantbarcode': 'Ca031AA010564', 'treatment': 'none', 'cartag': '2143', 'measurementlabel': 'C002ch_092214_biomass', 'other': 'none', 'coimg': 'NIR_SV_0_z1_h1_g0_e65_117779.jpg' }, 'NIR_SV_0_z1_h1_g0_e65_117779.jpg': { 'path': os.path.join(PARALLEL_TEST_DATA, 'snapshots', 'snapshot57383', 'NIR_SV_0_z1_h1_g0_e65_117779.jpg'), 'camera': 'SV', 'imgtype': 'NIR', 'zoom': 'z1', 'exposure': 'e65', 'gain': 'g0', 'frame': '0', 'lifter': 'h1', 'timestamp': '2014-10-22 17:49:35.187', 'id': '117779', 'plantbarcode': 'Ca031AA010564', 'treatment': 'none', 'cartag': '2143', 'measurementlabel': 'C002ch_092214_biomass', 'other': 'none' } } def test_plantcv_parallel_metadata_parser_images_no_frame(): # Create config instance config = plantcv.parallel.WorkflowConfig() config.input_dir = os.path.join(PARALLEL_TEST_DATA, TEST_SNAPSHOT_DIR) config.json = os.path.join(TEST_TMPDIR, "test_plantcv_parallel_metadata_parser_images_no_frame", "output.json") config.filename_metadata = ["imgtype", "camera", "X", "zoom", "lifter", "gain", "exposure", "id"] config.workflow = TEST_PIPELINE config.metadata_filters = {"imgtype": "VIS"} config.start_date = "2014-10-21 00:00:00.0" config.end_date = "2014-10-23 00:00:00.0" config.timestampformat = '%Y-%m-%d %H:%M:%S.%f' config.imgformat = "jpg" config.coprocess = "NIR" meta = plantcv.parallel.metadata_parser(config=config) assert meta == { 'VIS_SV_0_z1_h1_g0_e82_117770.jpg': { 'path': os.path.join(PARALLEL_TEST_DATA, 'snapshots', 'snapshot57383', 'VIS_SV_0_z1_h1_g0_e82_117770.jpg'), 'camera': 'SV', 'imgtype': 'VIS', 'zoom': 'z1', 'exposure': 'e82', 'gain': 'g0', 'frame': 'none', 'lifter': 'h1', 'timestamp': '2014-10-22 17:49:35.187', 'id': '117770', 'plantbarcode': 'Ca031AA010564', 'treatment': 'none', 'cartag': '2143', 'measurementlabel': 'C002ch_092214_biomass', 'other': 'none', 'coimg': 'NIR_SV_0_z1_h1_g0_e65_117779.jpg' }, 'NIR_SV_0_z1_h1_g0_e65_117779.jpg': { 'path': os.path.join(PARALLEL_TEST_DATA, 'snapshots', 'snapshot57383', 'NIR_SV_0_z1_h1_g0_e65_117779.jpg'), 'camera': 'SV', 'imgtype': 'NIR', 'zoom': 'z1', 'exposure': 'e65', 'gain': 'g0', 'frame': 'none', 'lifter': 'h1', 'timestamp': '2014-10-22 17:49:35.187', 'id': '117779', 'plantbarcode': 'Ca031AA010564', 'treatment': 'none', 'cartag': '2143', 'measurementlabel': 'C002ch_092214_biomass', 'other': 'none' } } def test_plantcv_parallel_metadata_parser_images_no_camera(): # Create config instance config = plantcv.parallel.WorkflowConfig() config.input_dir = os.path.join(PARALLEL_TEST_DATA, TEST_SNAPSHOT_DIR) config.json = os.path.join(TEST_TMPDIR, "test_plantcv_parallel_metadata_parser_images_no_frame", "output.json") config.filename_metadata = ["imgtype", "X", "frame", "zoom", "lifter", "gain", "exposure", "id"] config.workflow = TEST_PIPELINE config.metadata_filters = {"imgtype": "VIS"} config.start_date = "2014-10-21 00:00:00.0" config.end_date = "2014-10-23 00:00:00.0" config.timestampformat = '%Y-%m-%d %H:%M:%S.%f' config.imgformat = "jpg" config.coprocess = "NIR" meta = plantcv.parallel.metadata_parser(config=config) assert meta == { 'VIS_SV_0_z1_h1_g0_e82_117770.jpg': { 'path': os.path.join(PARALLEL_TEST_DATA, 'snapshots', 'snapshot57383', 'VIS_SV_0_z1_h1_g0_e82_117770.jpg'), 'camera': 'none', 'imgtype': 'VIS', 'zoom': 'z1', 'exposure': 'e82', 'gain': 'g0', 'frame': '0', 'lifter': 'h1', 'timestamp': '2014-10-22 17:49:35.187', 'id': '117770', 'plantbarcode': 'Ca031AA010564', 'treatment': 'none', 'cartag': '2143', 'measurementlabel': 'C002ch_092214_biomass', 'other': 'none', 'coimg': 'NIR_SV_0_z1_h1_g0_e65_117779.jpg' }, 'NIR_SV_0_z1_h1_g0_e65_117779.jpg': { 'path': os.path.join(PARALLEL_TEST_DATA, 'snapshots', 'snapshot57383', 'NIR_SV_0_z1_h1_g0_e65_117779.jpg'), 'camera': 'none', 'imgtype': 'NIR', 'zoom': 'z1', 'exposure': 'e65', 'gain': 'g0', 'frame': '0', 'lifter': 'h1', 'timestamp': '2014-10-22 17:49:35.187', 'id': '117779', 'plantbarcode': 'Ca031AA010564', 'treatment': 'none', 'cartag': '2143', 'measurementlabel': 'C002ch_092214_biomass', 'other': 'none' } } def test_plantcv_parallel_job_builder_single_image(): # Create cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_parallel_job_builder_single_image") os.mkdir(cache_dir) # Create config instance config = plantcv.parallel.WorkflowConfig() config.input_dir = os.path.join(PARALLEL_TEST_DATA, TEST_SNAPSHOT_DIR) config.json = os.path.join(cache_dir, "output.json") config.tmp_dir = cache_dir config.filename_metadata = ["imgtype", "camera", "frame", "zoom", "lifter", "gain", "exposure", "id"] config.workflow = TEST_PIPELINE config.img_outdir = cache_dir config.metadata_filters = {"imgtype": "VIS", "camera": "SV"} config.start_date = "2014-10-21 00:00:00.0" config.end_date = "2014-10-23 00:00:00.0" config.timestampformat = '%Y-%m-%d %H:%M:%S.%f' config.imgformat = "jpg" config.other_args = ["--other", "on"] config.writeimg = True jobs = plantcv.parallel.job_builder(meta=METADATA_VIS_ONLY, config=config) image_name = list(METADATA_VIS_ONLY.keys())[0] result_file = os.path.join(cache_dir, image_name + '.txt') expected = ['python', TEST_PIPELINE, '--image', METADATA_VIS_ONLY[image_name]['path'], '--outdir', cache_dir, '--result', result_file, '--writeimg', '--other', 'on'] if len(expected) != len(jobs[0]): assert False else: assert all([i == j] for i, j in zip(jobs[0], expected)) def test_plantcv_parallel_job_builder_coprocess(): # Create cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_parallel_job_builder_coprocess") os.mkdir(cache_dir) # Create config instance config = plantcv.parallel.WorkflowConfig() config.input_dir = os.path.join(PARALLEL_TEST_DATA, TEST_SNAPSHOT_DIR) config.json = os.path.join(cache_dir, "output.json") config.tmp_dir = cache_dir config.filename_metadata = ["imgtype", "camera", "frame", "zoom", "lifter", "gain", "exposure", "id"] config.workflow = TEST_PIPELINE config.img_outdir = cache_dir config.metadata_filters = {"imgtype": "VIS", "camera": "SV"} config.start_date = "2014-10-21 00:00:00.0" config.end_date = "2014-10-23 00:00:00.0" config.timestampformat = '%Y-%m-%d %H:%M:%S.%f' config.imgformat = "jpg" config.other_args = ["--other", "on"] config.writeimg = True config.coprocess = "NIR" jobs = plantcv.parallel.job_builder(meta=METADATA_COPROCESS, config=config) img_names = list(METADATA_COPROCESS.keys()) vis_name = img_names[0] vis_path = METADATA_COPROCESS[vis_name]['path'] result_file = os.path.join(cache_dir, vis_name + '.txt') nir_name = img_names[1] coresult_file = os.path.join(cache_dir, nir_name + '.txt') expected = ['python', TEST_PIPELINE, '--image', vis_path, '--outdir', cache_dir, '--result', result_file, '--coresult', coresult_file, '--writeimg', '--other', 'on'] if len(expected) != len(jobs[0]): assert False else: assert all([i == j] for i, j in zip(jobs[0], expected)) def test_plantcv_parallel_multiprocess_create_dask_cluster_local(): client = plantcv.parallel.create_dask_cluster(cluster="LocalCluster", cluster_config={}) status = client.status client.shutdown() assert status == "running" def test_plantcv_parallel_multiprocess_create_dask_cluster(): client = plantcv.parallel.create_dask_cluster(cluster="HTCondorCluster", cluster_config={"cores": 1, "memory": "1GB", "disk": "1GB"}) status = client.status client.shutdown() assert status == "running" def test_plantcv_parallel_multiprocess_create_dask_cluster_invalid_cluster(): with pytest.raises(ValueError): _ = plantcv.parallel.create_dask_cluster(cluster="Skynet", cluster_config={}) def test_plantcv_parallel_convert_datetime_to_unixtime(): unix_time = plantcv.parallel.convert_datetime_to_unixtime(timestamp_str="1970-01-01", date_format="%Y-%m-%d") assert unix_time == 0 def test_plantcv_parallel_convert_datetime_to_unixtime_bad_strptime(): with pytest.raises(SystemExit): _ = plantcv.parallel.convert_datetime_to_unixtime(timestamp_str="1970-01-01", date_format="%Y-%m") def test_plantcv_parallel_multiprocess(): image_name = list(METADATA_VIS_ONLY.keys())[0] image_path = os.path.join(METADATA_VIS_ONLY[image_name]['path'], image_name) result_file = os.path.join(TEST_TMPDIR, image_name + '.txt') jobs = [['python', TEST_PIPELINE, '--image', image_path, '--outdir', TEST_TMPDIR, '--result', result_file, '--writeimg', '--other', 'on']] # Create a dask LocalCluster client client = Client(n_workers=1) plantcv.parallel.multiprocess(jobs, client=client) assert os.path.exists(result_file) def test_plantcv_parallel_process_results(): # Create a test tmp directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_parallel_process_results") os.mkdir(cache_dir) plantcv.parallel.process_results(job_dir=os.path.join(PARALLEL_TEST_DATA, "results"), json_file=os.path.join(cache_dir, 'appended_results.json')) plantcv.parallel.process_results(job_dir=os.path.join(PARALLEL_TEST_DATA, "results"), json_file=os.path.join(cache_dir, 'appended_results.json')) # Assert that the output JSON file matches the expected output JSON file result_file = open(os.path.join(cache_dir, "appended_results.json"), "r") results = json.load(result_file) result_file.close() expected_file = open(os.path.join(PARALLEL_TEST_DATA, "appended_results.json")) expected = json.load(expected_file) expected_file.close() assert results == expected def test_plantcv_parallel_process_results_new_output(): # Create a test tmp directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_parallel_process_results_new_output") os.mkdir(cache_dir) plantcv.parallel.process_results(job_dir=os.path.join(PARALLEL_TEST_DATA, "results"), json_file=os.path.join(cache_dir, 'new_result.json')) # Assert output matches expected values result_file = open(os.path.join(cache_dir, "new_result.json"), "r") results = json.load(result_file) result_file.close() expected_file = open(os.path.join(PARALLEL_TEST_DATA, "new_result.json")) expected = json.load(expected_file) expected_file.close() assert results == expected def test_plantcv_parallel_process_results_valid_json(): # Test when the file is a valid json file but doesn't contain expected keys with pytest.raises(RuntimeError): plantcv.parallel.process_results(job_dir=os.path.join(PARALLEL_TEST_DATA, "results"), json_file=os.path.join(PARALLEL_TEST_DATA, "valid.json")) def test_plantcv_parallel_process_results_invalid_json(): # Create a test tmp directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_parallel_process_results_invalid_json") os.mkdir(cache_dir) # Move the test data to the tmp directory shutil.copytree(os.path.join(PARALLEL_TEST_DATA, "bad_results"), os.path.join(cache_dir, "bad_results")) with pytest.raises(RuntimeError): plantcv.parallel.process_results(job_dir=os.path.join(cache_dir, "bad_results"), json_file=os.path.join(cache_dir, "bad_results", "invalid.txt")) # #################################################################################################################### # ########################################### PLANTCV MAIN PACKAGE ################################################### matplotlib.use('Template') TEST_DATA = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data") HYPERSPECTRAL_TEST_DATA = os.path.join(os.path.dirname(os.path.abspath(__file__)), "hyperspectral_data") HYPERSPECTRAL_DATA = "darkReference" HYPERSPECTRAL_WHITE = "darkReference_whiteReference" HYPERSPECTRAL_DARK = "darkReference_darkReference" HYPERSPECTRAL_HDR = "darkReference.hdr" HYPERSPECTRAL_MASK = "darkReference_mask.png" HYPERSPECTRAL_DATA_NO_DEFAULT = "darkReference2" HYPERSPECTRAL_HDR_NO_DEFAULT = "darkReference2.hdr" HYPERSPECTRAL_DATA_APPROX_PSEUDO = "darkReference3" HYPERSPECTRAL_HDR_APPROX_PSEUDO = "darkReference3.hdr" HYPERSPECTRAL_DATA_BAD_INTERLEAVE = "darkReference4" HYPERSPECTRAL_HDR_BAD_INTERLEAVE = "darkReference4.hdr" HYPERSPECTRAL_HDR_SMALL_RANGE = {'description': '{[HEADWALL Hyperspec III]}', 'samples': '800', 'lines': '1', 'bands': '978', 'header offset': '0', 'file type': 'ENVI Standard', 'interleave': 'bil', 'sensor type': 'Unknown', 'byte order': '0', 'default bands': '159,253,520', 'wavelength units': 'nm', 'wavelength': ['379.027', '379.663', '380.3', '380.936', '381.573', '382.209']} FLUOR_TEST_DATA = os.path.join(os.path.dirname(os.path.abspath(__file__)), "photosynthesis_data") FLUOR_IMG = "PSII_PSD_supopt_temp_btx623_22_rep1.DAT" TEST_COLOR_DIM = (2056, 2454, 3) TEST_GRAY_DIM = (2056, 2454) TEST_BINARY_DIM = TEST_GRAY_DIM TEST_INPUT_COLOR = "input_color_img.jpg" TEST_INPUT_GRAY = "input_gray_img.jpg" TEST_INPUT_GRAY_SMALL = "input_gray_img_small.jpg" TEST_INPUT_BINARY = "input_binary_img.png" # Image from http://www.libpng.org/pub/png/png-OwlAlpha.html # This image may be used, edited and reproduced freely. TEST_INPUT_RGBA = "input_rgba.png" TEST_INPUT_BAYER = "bayer_img.png" TEST_INPUT_ROI_CONTOUR = "input_roi_contour.npz" TEST_INPUT_ROI_HIERARCHY = "input_roi_hierarchy.npz" TEST_INPUT_CONTOURS = "input_contours.npz" TEST_INPUT_OBJECT_CONTOURS = "input_object_contours.npz" TEST_INPUT_OBJECT_HIERARCHY = "input_object_hierarchy.npz" TEST_VIS = "VIS_SV_0_z300_h1_g0_e85_v500_93054.png" TEST_NIR = "NIR_SV_0_z300_h1_g0_e15000_v500_93059.png" TEST_VIS_TV = "VIS_TV_0_z300_h1_g0_e85_v500_93054.png" TEST_NIR_TV = "NIR_TV_0_z300_h1_g0_e15000_v500_93059.png" TEST_INPUT_MASK = "input_mask_binary.png" TEST_INPUT_MASK_OOB = "mask_outbounds.png" TEST_INPUT_MASK_RESIZE = "input_mask_resize.png" TEST_INPUT_NIR_MASK = "input_nir.png" TEST_INPUT_FDARK = "FLUO_TV_dark.png" TEST_INPUT_FDARK_LARGE = "FLUO_TV_DARK_large" TEST_INPUT_FMIN = "FLUO_TV_min.png" TEST_INPUT_FMAX = "FLUO_TV_max.png" TEST_INPUT_FMASK = "FLUO_TV_MASK.png" TEST_INPUT_GREENMAG = "input_green-magenta.jpg" TEST_INPUT_MULTI = "multi_ori_image.jpg" TEST_INPUT_MULTI_MASK = "multi_ori_mask.jpg" TEST_INPUT_MULTI_OBJECT = "roi_objects.npz" TEST_INPUT_MULTI_CONTOUR = "multi_contours.npz" TEST_INPUT_ClUSTER_CONTOUR = "clusters_i.npz" TEST_INPUT_MULTI_HIERARCHY = "multi_hierarchy.npz" TEST_INPUT_VISUALIZE_CONTOUR = "roi_objects_visualize.npz" TEST_INPUT_VISUALIZE_HIERARCHY = "roi_obj_hierarchy_visualize.npz" TEST_INPUT_VISUALIZE_CLUSTERS = "clusters_i_visualize.npz" TEST_INPUT_VISUALIZE_BACKGROUND = "visualize_background_img.png" TEST_INPUT_GENOTXT = "cluster_names.txt" TEST_INPUT_GENOTXT_TOO_MANY = "cluster_names_too_many.txt" TEST_INPUT_CROPPED = 'cropped_img.jpg' TEST_INPUT_CROPPED_MASK = 'cropped-mask.png' TEST_INPUT_MARKER = 'seed-image.jpg' TEST_INPUT_SKELETON = 'input_skeleton.png' TEST_INPUT_SKELETON_PRUNED = 'input_pruned_skeleton.png' TEST_FOREGROUND = "TEST_FOREGROUND.jpg" TEST_BACKGROUND = "TEST_BACKGROUND.jpg" TEST_PDFS = "naive_bayes_pdfs.txt" TEST_PDFS_BAD = "naive_bayes_pdfs_bad.txt" TEST_VIS_SMALL = "setaria_small_vis.png" TEST_MASK_SMALL = "setaria_small_mask.png" TEST_VIS_COMP_CONTOUR = "setaria_composed_contours.npz" TEST_ACUTE_RESULT = np.asarray([[[119, 285]], [[151, 280]], [[168, 267]], [[168, 262]], [[171, 261]], [[224, 269]], [[246, 271]], [[260, 277]], [[141, 248]], [[183, 194]], [[188, 237]], [[173, 240]], [[186, 260]], [[147, 244]], [[163, 246]], [[173, 268]], [[170, 272]], [[151, 320]], [[195, 289]], [[228, 272]], [[210, 272]], [[209, 247]], [[210, 232]]]) TEST_VIS_SMALL_PLANT = "setaria_small_plant_vis.png" TEST_MASK_SMALL_PLANT = "setaria_small_plant_mask.png" TEST_VIS_COMP_CONTOUR_SMALL_PLANT = "setaria_small_plant_composed_contours.npz" TEST_SAMPLED_RGB_POINTS = "sampled_rgb_points.txt" TEST_TARGET_IMG = "target_img.png" TEST_TARGET_IMG_WITH_HEXAGON = "target_img_w_hexagon.png" TEST_TARGET_IMG_TRIANGLE = "target_img copy.png" TEST_SOURCE1_IMG = "source1_img.png" TEST_SOURCE2_IMG = "source2_img.png" TEST_TARGET_MASK = "mask_img.png" TEST_TARGET_IMG_COLOR_CARD = "color_card_target.png" TEST_SOURCE2_MASK = "mask2_img.png" TEST_TARGET_MATRIX = "target_matrix.npz" TEST_SOURCE1_MATRIX = "source1_matrix.npz" TEST_SOURCE2_MATRIX = "source2_matrix.npz" TEST_MATRIX_B1 = "matrix_b1.npz" TEST_MATRIX_B2 = "matrix_b2.npz" TEST_TRANSFORM1 = "transformation_matrix1.npz" TEST_MATRIX_M1 = "matrix_m1.npz" TEST_MATRIX_M2 = "matrix_m2.npz" TEST_S1_CORRECTED = "source_corrected.png" TEST_SKELETON_OBJECTS = "skeleton_objects.npz" TEST_SKELETON_HIERARCHIES = "skeleton_hierarchies.npz" TEST_THERMAL_ARRAY = "thermal_img.npz" TEST_THERMAL_IMG_MASK = "thermal_img_mask.png" TEST_INPUT_THERMAL_CSV = "FLIR2600.csv" # TEST_BAD_MASK = "bad_mask_test.pkl" # TEST_IM_BAD_NONE = "bad_mask_none.pkl" # TEST_IM_BAD_BOTH = "bad_mask_both.pkl" # TEST_IM_BAD_NAN = "bad_mask_nan.pkl" # TEST_IM_BAD_INF = "bad_mask_inf.pkl" PIXEL_VALUES = "pixel_inspector_rgb_values.txt" # ########################## # Tests for the main package # ########################## @pytest.mark.parametrize("debug", ["print", "plot"]) def test_plantcv_debug(debug, tmpdir): from plantcv.plantcv._debug import _debug # Create a test tmp directory img_outdir = tmpdir.mkdir("sub") pcv.params.debug = debug img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) _debug(visual=img, filename=os.path.join(img_outdir, TEST_INPUT_COLOR)) assert True @pytest.mark.parametrize("datatype,value", [[list, []], [int, 2], [float, 2.2], [bool, True], [str, "2"], [dict, {}], [tuple, ()], [None, None]]) def test_plantcv_outputs_add_observation(datatype, value): # Create output instance outputs = pcv.Outputs() outputs.add_observation(sample='default', variable='test', trait='test variable', method='type', scale='none', datatype=datatype, value=value, label=[]) assert outputs.observations["default"]["test"]["value"] == value def test_plantcv_outputs_add_observation_invalid_type(): # Create output instance outputs = pcv.Outputs() with pytest.raises(RuntimeError): outputs.add_observation(sample='default', variable='test', trait='test variable', method='type', scale='none', datatype=list, value=np.array([2]), label=[]) def test_plantcv_outputs_save_results_json_newfile(tmpdir): # Create a test tmp directory cache_dir = tmpdir.mkdir("sub") outfile = os.path.join(cache_dir, "results.json") # Create output instance outputs = pcv.Outputs() outputs.add_observation(sample='default', variable='test', trait='test variable', method='test', scale='none', datatype=str, value="test", label="none") outputs.save_results(filename=outfile, outformat="json") with open(outfile, "r") as fp: results = json.load(fp) assert results["observations"]["default"]["test"]["value"] == "test" def test_plantcv_outputs_save_results_json_existing_file(tmpdir): # Create a test tmp directory cache_dir = tmpdir.mkdir("sub") outfile = os.path.join(cache_dir, "data_results.txt") shutil.copyfile(os.path.join(TEST_DATA, "data_results.txt"), outfile) # Create output instance outputs = pcv.Outputs() outputs.add_observation(sample='default', variable='test', trait='test variable', method='test', scale='none', datatype=str, value="test", label="none") outputs.save_results(filename=outfile, outformat="json") with open(outfile, "r") as fp: results = json.load(fp) assert results["observations"]["default"]["test"]["value"] == "test" def test_plantcv_outputs_save_results_csv(tmpdir): # Create a test tmp directory cache_dir = tmpdir.mkdir("sub") outfile = os.path.join(cache_dir, "results.csv") testfile = os.path.join(TEST_DATA, "data_results.csv") # Create output instance outputs = pcv.Outputs() outputs.add_observation(sample='default', variable='string', trait='string variable', method='string', scale='none', datatype=str, value="string", label="none") outputs.add_observation(sample='default', variable='boolean', trait='boolean variable', method='boolean', scale='none', datatype=bool, value=True, label="none") outputs.add_observation(sample='default', variable='list', trait='list variable', method='list', scale='none', datatype=list, value=[1, 2, 3], label=[1, 2, 3]) outputs.add_observation(sample='default', variable='tuple', trait='tuple variable', method='tuple', scale='none', datatype=tuple, value=(1, 2), label=(1, 2)) outputs.add_observation(sample='default', variable='tuple_list', trait='list of tuples variable', method='tuple_list', scale='none', datatype=list, value=[(1, 2), (3, 4)], label=[1, 2]) outputs.save_results(filename=outfile, outformat="csv") with open(outfile, "r") as fp: results = fp.read() with open(testfile, "r") as fp: test_results = fp.read() assert results == test_results def test_plantcv_acute(): # Read in test data mask = cv2.imread(os.path.join(TEST_DATA, TEST_MASK_SMALL), -1) contours_npz = np.load(os.path.join(TEST_DATA, TEST_VIS_COMP_CONTOUR), encoding="latin1") obj_contour = contours_npz['arr_0'] # Test with debug = "print" pcv.params.debug = "print" _ = pcv.acute(obj=obj_contour, win=5, thresh=15, mask=mask) _ = pcv.acute(obj=obj_contour, win=0, thresh=15, mask=mask) _ = pcv.acute(obj=np.array(([[213, 190]], [[83, 61]], [[149, 246]])), win=84, thresh=192, mask=mask) _ = pcv.acute(obj=np.array(([[3, 29]], [[31, 102]], [[161, 63]])), win=148, thresh=56, mask=mask) _ = pcv.acute(obj=np.array(([[103, 154]], [[27, 227]], [[152, 83]])), win=35, thresh=0, mask=mask) # Test with debug = None pcv.params.debug = None _ = pcv.acute(obj=np.array(([[103, 154]], [[27, 227]], [[152, 83]])), win=35, thresh=0, mask=mask) _ = pcv.acute(obj=obj_contour, win=0, thresh=15, mask=mask) homology_pts = pcv.acute(obj=obj_contour, win=5, thresh=15, mask=mask) assert all([i == j] for i, j in zip(np.shape(homology_pts), (29, 1, 2))) def test_plantcv_acute_vertex(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_acute_vertex") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_VIS_SMALL)) contours_npz = np.load(os.path.join(TEST_DATA, TEST_VIS_COMP_CONTOUR), encoding="latin1") obj_contour = contours_npz['arr_0'] # Test with debug = "print" pcv.params.debug = "print" _ = pcv.acute_vertex(obj=obj_contour, win=5, thresh=15, sep=5, img=img, label="prefix") _ = pcv.acute_vertex(obj=[], win=5, thresh=15, sep=5, img=img) _ = pcv.acute_vertex(obj=[], win=.01, thresh=.01, sep=1, img=img) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.acute_vertex(obj=obj_contour, win=5, thresh=15, sep=5, img=img) # Test with debug = None pcv.params.debug = None acute = pcv.acute_vertex(obj=obj_contour, win=5, thresh=15, sep=5, img=img) assert all([i == j] for i, j in zip(np.shape(acute), np.shape(TEST_ACUTE_RESULT))) pcv.outputs.clear() def test_plantcv_acute_vertex_bad_obj(): img = cv2.imread(os.path.join(TEST_DATA, TEST_VIS_SMALL)) obj_contour = np.array([]) pcv.params.debug = None result = pcv.acute_vertex(obj=obj_contour, win=5, thresh=15, sep=5, img=img) assert all([i == j] for i, j in zip(result, [0, ("NA", "NA")])) pcv.outputs.clear() def test_plantcv_analyze_bound_horizontal(): # Clear previous outputs pcv.outputs.clear() # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_analyze_bound_horizontal") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) img_above_bound_only = cv2.imread(os.path.join(TEST_DATA, TEST_MASK_SMALL_PLANT)) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) contours_npz = np.load(os.path.join(TEST_DATA, TEST_INPUT_CONTOURS), encoding="latin1") object_contours = contours_npz['arr_0'] # Test with debug = "print" pcv.params.debug = "print" _ = pcv.analyze_bound_horizontal(img=img, obj=object_contours, mask=mask, line_position=300, label="prefix") pcv.outputs.clear() _ = pcv.analyze_bound_horizontal(img=img, obj=object_contours, mask=mask, line_position=100) _ = pcv.analyze_bound_horizontal(img=img_above_bound_only, obj=object_contours, mask=mask, line_position=1756) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.analyze_bound_horizontal(img=img, obj=object_contours, mask=mask, line_position=1756) # Test with debug = None pcv.params.debug = None _ = pcv.analyze_bound_horizontal(img=img, obj=object_contours, mask=mask, line_position=1756) assert len(pcv.outputs.observations["default"]) == 7 def test_plantcv_analyze_bound_horizontal_grayscale_image(): # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) contours_npz = np.load(os.path.join(TEST_DATA, TEST_INPUT_CONTOURS), encoding="latin1") object_contours = contours_npz['arr_0'] # Test with a grayscale reference image and debug="plot" pcv.params.debug = "plot" boundary_img1 = pcv.analyze_bound_horizontal(img=img, obj=object_contours, mask=mask, line_position=1756) assert len(np.shape(boundary_img1)) == 3 def test_plantcv_analyze_bound_horizontal_neg_y(): # Clear previous outputs pcv.outputs.clear() # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_analyze_bound_horizontal") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) contours_npz = np.load(os.path.join(TEST_DATA, TEST_INPUT_CONTOURS), encoding="latin1") object_contours = contours_npz['arr_0'] # Test with debug=None, line position that will trigger -y pcv.params.debug = "plot" _ = pcv.analyze_bound_horizontal(img=img, obj=object_contours, mask=mask, line_position=-1000) _ = pcv.analyze_bound_horizontal(img=img, obj=object_contours, mask=mask, line_position=0) _ = pcv.analyze_bound_horizontal(img=img, obj=object_contours, mask=mask, line_position=2056) assert pcv.outputs.observations['default']['height_above_reference']['value'] == 713 def test_plantcv_analyze_bound_vertical(): # Clear previous outputs pcv.outputs.clear() # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_analyze_bound_vertical") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) contours_npz = np.load(os.path.join(TEST_DATA, TEST_INPUT_CONTOURS), encoding="latin1") object_contours = contours_npz['arr_0'] # Test with debug = "print" pcv.params.debug = "print" _ = pcv.analyze_bound_vertical(img=img, obj=object_contours, mask=mask, line_position=1000, label="prefix") # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.analyze_bound_vertical(img=img, obj=object_contours, mask=mask, line_position=1000) # Test with debug = None pcv.params.debug = None _ = pcv.analyze_bound_vertical(img=img, obj=object_contours, mask=mask, line_position=1000) assert pcv.outputs.observations['default']['width_left_reference']['value'] == 94 def test_plantcv_analyze_bound_vertical_grayscale_image(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_analyze_bound_vertical") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) contours_npz = np.load(os.path.join(TEST_DATA, TEST_INPUT_CONTOURS), encoding="latin1") object_contours = contours_npz['arr_0'] # Test with a grayscale reference image and debug="plot" pcv.params.debug = "plot" _ = pcv.analyze_bound_vertical(img=img, obj=object_contours, mask=mask, line_position=1000) assert pcv.outputs.observations['default']['width_left_reference']['value'] == 94 pcv.outputs.clear() def test_plantcv_analyze_bound_vertical_neg_x(): # Clear previous outputs pcv.outputs.clear() # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_analyze_bound_vertical") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) contours_npz = np.load(os.path.join(TEST_DATA, TEST_INPUT_CONTOURS), encoding="latin1") object_contours = contours_npz['arr_0'] # Test with debug="plot", line position that will trigger -x pcv.params.debug = "plot" _ = pcv.analyze_bound_vertical(img=img, obj=object_contours, mask=mask, line_position=2454) assert pcv.outputs.observations['default']['width_left_reference']['value'] == 441 def test_plantcv_analyze_bound_vertical_small_x(): # Clear previous outputs pcv.outputs.clear() # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_analyze_bound_vertical") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) contours_npz = np.load(os.path.join(TEST_DATA, TEST_INPUT_CONTOURS), encoding="latin1") object_contours = contours_npz['arr_0'] # Test with debug='plot', line position that will trigger -x, and two channel object pcv.params.debug = "plot" _ = pcv.analyze_bound_vertical(img=img, obj=object_contours, mask=mask, line_position=1) assert pcv.outputs.observations['default']['width_right_reference']['value'] == 441 def test_plantcv_analyze_color(): # Clear previous outputs pcv.outputs.clear() # Test with debug = None pcv.params.debug = None # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) _ = pcv.analyze_color(rgb_img=img, mask=mask, hist_plot_type="all") _ = pcv.analyze_color(rgb_img=img, mask=mask, hist_plot_type=None, label="prefix") _ = pcv.analyze_color(rgb_img=img, mask=mask, hist_plot_type=None) _ = pcv.analyze_color(rgb_img=img, mask=mask, hist_plot_type='lab') _ = pcv.analyze_color(rgb_img=img, mask=mask, hist_plot_type='hsv') _ = pcv.analyze_color(rgb_img=img, mask=mask, hist_plot_type=None) # Test with debug = "print" # pcv.params.debug = "print" _ = pcv.analyze_color(rgb_img=img, mask=mask, hist_plot_type="all") _ = pcv.analyze_color(rgb_img=img, mask=mask, hist_plot_type=None, label="prefix") # Test with debug = "plot" # pcv.params.debug = "plot" # _ = pcv.analyze_color(rgb_img=img, mask=mask, hist_plot_type=None) _ = pcv.analyze_color(rgb_img=img, mask=mask, hist_plot_type='lab') _ = pcv.analyze_color(rgb_img=img, mask=mask, hist_plot_type='hsv') # _ = pcv.analyze_color(rgb_img=img, mask=mask, hist_plot_type=None) # Test with debug = None # pcv.params.debug = None _ = pcv.analyze_color(rgb_img=img, mask=mask, hist_plot_type='rgb') assert pcv.outputs.observations['default']['hue_median']['value'] == 84.0 def test_plantcv_analyze_color_incorrect_image(): img_binary = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) with pytest.raises(RuntimeError): _ = pcv.analyze_color(rgb_img=img_binary, mask=mask, hist_plot_type=None) # # def test_plantcv_analyze_color_bad_hist_type(): img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) pcv.params.debug = "plot" with pytest.raises(RuntimeError): _ = pcv.analyze_color(rgb_img=img, mask=mask, hist_plot_type='bgr') def test_plantcv_analyze_color_incorrect_hist_plot_type(): img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) with pytest.raises(RuntimeError): pcv.params.debug = "plot" _ = pcv.analyze_color(rgb_img=img, mask=mask, hist_plot_type="bgr") def test_plantcv_analyze_nir(): # Clear previous outputs pcv.outputs.clear() # Test with debug=None pcv.params.debug = None # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR), 0) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) _ = pcv.analyze_nir_intensity(gray_img=img, mask=mask, bins=256, histplot=True) result = len(pcv.outputs.observations['default']['nir_frequencies']['value']) assert result == 256 def test_plantcv_analyze_nir_16bit(): # Clear previous outputs pcv.outputs.clear() # Test with debug=None pcv.params.debug = None # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR), 0) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) _ = pcv.analyze_nir_intensity(gray_img=np.uint16(img), mask=mask, bins=256, histplot=True) result = len(pcv.outputs.observations['default']['nir_frequencies']['value']) assert result == 256 def test_plantcv_analyze_object(): # Test with debug = None pcv.params.debug = None # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) contours_npz = np.load(os.path.join(TEST_DATA, TEST_INPUT_CONTOURS), encoding="latin1") obj_contour = contours_npz['arr_0'] obj_images = pcv.analyze_object(img=img, obj=obj_contour, mask=mask) pcv.outputs.clear() assert len(obj_images) != 0 def test_plantcv_analyze_object_grayscale_input(): # Test with debug = None pcv.params.debug = None # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR), 0) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) contours_npz = np.load(os.path.join(TEST_DATA, TEST_INPUT_CONTOURS), encoding="latin1") obj_contour = contours_npz['arr_0'] obj_images = pcv.analyze_object(img=img, obj=obj_contour, mask=mask) assert len(obj_images) != 1 def test_plantcv_analyze_object_zero_slope(): # Test with debug = None pcv.params.debug = None # Create a test image img = np.zeros((50, 50, 3), dtype=np.uint8) img[10:11, 10:40, 0] = 255 mask = img[:, :, 0] obj_contour = np.array([[[10, 10]], [[11, 10]], [[12, 10]], [[13, 10]], [[14, 10]], [[15, 10]], [[16, 10]], [[17, 10]], [[18, 10]], [[19, 10]], [[20, 10]], [[21, 10]], [[22, 10]], [[23, 10]], [[24, 10]], [[25, 10]], [[26, 10]], [[27, 10]], [[28, 10]], [[29, 10]], [[30, 10]], [[31, 10]], [[32, 10]], [[33, 10]], [[34, 10]], [[35, 10]], [[36, 10]], [[37, 10]], [[38, 10]], [[39, 10]], [[38, 10]], [[37, 10]], [[36, 10]], [[35, 10]], [[34, 10]], [[33, 10]], [[32, 10]], [[31, 10]], [[30, 10]], [[29, 10]], [[28, 10]], [[27, 10]], [[26, 10]], [[25, 10]], [[24, 10]], [[23, 10]], [[22, 10]], [[21, 10]], [[20, 10]], [[19, 10]], [[18, 10]], [[17, 10]], [[16, 10]], [[15, 10]], [[14, 10]], [[13, 10]], [[12, 10]], [[11, 10]]], dtype=np.int32) obj_images = pcv.analyze_object(img=img, obj=obj_contour, mask=mask) assert len(obj_images) != 0 def test_plantcv_analyze_object_longest_axis_2d(): # Test with debug = None pcv.params.debug = None # Create a test image img = np.zeros((50, 50, 3), dtype=np.uint8) img[0:5, 45:49, 0] = 255 img[0:5, 0:5, 0] = 255 mask = img[:, :, 0] obj_contour = np.array([[[45, 1]], [[45, 2]], [[45, 3]], [[45, 4]], [[46, 4]], [[47, 4]], [[48, 4]], [[48, 3]], [[48, 2]], [[48, 1]], [[47, 1]], [[46, 1]], [[1, 1]], [[1, 2]], [[1, 3]], [[1, 4]], [[2, 4]], [[3, 4]], [[4, 4]], [[4, 3]], [[4, 2]], [[4, 1]], [[3, 1]], [[2, 1]]], dtype=np.int32) obj_images = pcv.analyze_object(img=img, obj=obj_contour, mask=mask) assert len(obj_images) != 0 def test_plantcv_analyze_object_longest_axis_2e(): # Test with debug = None pcv.params.debug = None # Create a test image img = np.zeros((50, 50, 3), dtype=np.uint8) img[10:15, 10:40, 0] = 255 mask = img[:, :, 0] obj_contour = np.array([[[10, 10]], [[10, 11]], [[10, 12]], [[10, 13]], [[10, 14]], [[11, 14]], [[12, 14]], [[13, 14]], [[14, 14]], [[15, 14]], [[16, 14]], [[17, 14]], [[18, 14]], [[19, 14]], [[20, 14]], [[21, 14]], [[22, 14]], [[23, 14]], [[24, 14]], [[25, 14]], [[26, 14]], [[27, 14]], [[28, 14]], [[29, 14]], [[30, 14]], [[31, 14]], [[32, 14]], [[33, 14]], [[34, 14]], [[35, 14]], [[36, 14]], [[37, 14]], [[38, 14]], [[39, 14]], [[39, 13]], [[39, 12]], [[39, 11]], [[39, 10]], [[38, 10]], [[37, 10]], [[36, 10]], [[35, 10]], [[34, 10]], [[33, 10]], [[32, 10]], [[31, 10]], [[30, 10]], [[29, 10]], [[28, 10]], [[27, 10]], [[26, 10]], [[25, 10]], [[24, 10]], [[23, 10]], [[22, 10]], [[21, 10]], [[20, 10]], [[19, 10]], [[18, 10]], [[17, 10]], [[16, 10]], [[15, 10]], [[14, 10]], [[13, 10]], [[12, 10]], [[11, 10]]], dtype=np.int32) obj_images = pcv.analyze_object(img=img, obj=obj_contour, mask=mask) assert len(obj_images) != 0 def test_plantcv_analyze_object_small_contour(): # Test with debug = None pcv.params.debug = None # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) obj_contour = [np.array([[[0, 0]], [[0, 50]], [[50, 50]], [[50, 0]]], dtype=np.int32)] obj_images = pcv.analyze_object(img=img, obj=obj_contour, mask=mask) assert obj_images is None def test_plantcv_analyze_thermal_values(): # Clear previous outputs pcv.outputs.clear() # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_analyze_thermal_values") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data # img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR), 0) mask = cv2.imread(os.path.join(TEST_DATA, TEST_THERMAL_IMG_MASK), -1) contours_npz = np.load(os.path.join(TEST_DATA, TEST_THERMAL_ARRAY), encoding="latin1") img = contours_npz['arr_0'] pcv.params.debug = None thermal_hist = pcv.analyze_thermal_values(thermal_array=img, mask=mask, histplot=True) assert thermal_hist is not None and pcv.outputs.observations['default']['median_temp']['value'] == 33.20922 def test_plantcv_apply_mask_white(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_apply_mask_white") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.apply_mask(img=img, mask=mask, mask_color="white") # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.apply_mask(img=img, mask=mask, mask_color="white") # Test with debug = None pcv.params.debug = None masked_img = pcv.apply_mask(img=img, mask=mask, mask_color="white") assert all([i == j] for i, j in zip(np.shape(masked_img), TEST_COLOR_DIM)) def test_plantcv_apply_mask_black(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_apply_mask_black") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.apply_mask(img=img, mask=mask, mask_color="black") # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.apply_mask(img=img, mask=mask, mask_color="black") # Test with debug = None pcv.params.debug = None masked_img = pcv.apply_mask(img=img, mask=mask, mask_color="black") assert all([i == j] for i, j in zip(np.shape(masked_img), TEST_COLOR_DIM)) def test_plantcv_apply_mask_hyperspectral(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_apply_mask_hyperspectral") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) hyper_array = pcv.hyperspectral.read_data(filename=spectral_filename) img = np.ones((2056, 2454)) img_stacked = cv2.merge((img, img, img, img)) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.apply_mask(img=img_stacked, mask=img, mask_color="black") # Test with debug = "plot" pcv.params.debug = "plot" masked_array = pcv.apply_mask(img=hyper_array.array_data, mask=img, mask_color="black") assert np.mean(masked_array) == 13.97111260224949 def test_plantcv_apply_mask_bad_input(): # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) with pytest.raises(RuntimeError): pcv.params.debug = "plot" _ = pcv.apply_mask(img=img, mask=mask, mask_color="wite") def test_plantcv_auto_crop(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_auto_crop") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img1 = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MULTI), -1) contours = np.load(os.path.join(TEST_DATA, TEST_INPUT_MULTI_OBJECT), encoding="latin1") roi_contours = [contours[arr_n] for arr_n in contours] # Test with debug = "print" pcv.params.debug = "print" _ = pcv.auto_crop(img=img1, obj=roi_contours[1], padding_x=(20, 10), padding_y=(20, 10), color='black') # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.auto_crop(img=img1, obj=roi_contours[1], color='image') _ = pcv.auto_crop(img=img1, obj=roi_contours[1], padding_x=2000, padding_y=2000, color='image') # Test with debug = None pcv.params.debug = None cropped = pcv.auto_crop(img=img1, obj=roi_contours[1], padding_x=20, padding_y=20, color='black') x, y, z = np.shape(img1) x1, y1, z1 = np.shape(cropped) assert x > x1 def test_plantcv_auto_crop_grayscale_input(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_auto_crop_grayscale_input") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MULTI), -1) gray_img = cv2.cvtColor(rgb_img, cv2.COLOR_BGR2GRAY) contours = np.load(os.path.join(TEST_DATA, TEST_INPUT_MULTI_OBJECT), encoding="latin1") roi_contours = [contours[arr_n] for arr_n in contours] # Test with debug = "plot" pcv.params.debug = "plot" cropped = pcv.auto_crop(img=gray_img, obj=roi_contours[1], padding_x=20, padding_y=20, color='white') x, y = np.shape(gray_img) x1, y1 = np.shape(cropped) assert x > x1 def test_plantcv_auto_crop_bad_color_input(): # Read in test data rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MULTI), -1) gray_img = cv2.cvtColor(rgb_img, cv2.COLOR_BGR2GRAY) contours = np.load(os.path.join(TEST_DATA, TEST_INPUT_MULTI_OBJECT), encoding="latin1") roi_contours = [contours[arr_n] for arr_n in contours] with pytest.raises(RuntimeError): _ = pcv.auto_crop(img=gray_img, obj=roi_contours[1], padding_x=20, padding_y=20, color='wite') def test_plantcv_auto_crop_bad_padding_input(): # Read in test data rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MULTI), -1) gray_img = cv2.cvtColor(rgb_img, cv2.COLOR_BGR2GRAY) contours = np.load(os.path.join(TEST_DATA, TEST_INPUT_MULTI_OBJECT), encoding="latin1") roi_contours = [contours[arr_n] for arr_n in contours] with pytest.raises(RuntimeError): _ = pcv.auto_crop(img=gray_img, obj=roi_contours[1], padding_x="one", padding_y=20, color='white') def test_plantcv_canny_edge_detect(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_canny_edge_detect") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.canny_edge_detect(img=rgb_img, mask=mask, mask_color='white') _ = pcv.canny_edge_detect(img=img, mask=mask, mask_color='black') # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.canny_edge_detect(img=img, thickness=2) _ = pcv.canny_edge_detect(img=img) # Test with debug = None pcv.params.debug = None edge_img = pcv.canny_edge_detect(img=img) # Assert that the output image has the dimensions of the input image if all([i == j] for i, j in zip(np.shape(edge_img), TEST_BINARY_DIM)): # Assert that the image is binary if all([i == j] for i, j in zip(np.unique(edge_img), [0, 255])): assert 1 else: assert 0 else: assert 0 def test_plantcv_canny_edge_detect_bad_input(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_canny_edge_detect") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) with pytest.raises(RuntimeError): _ = pcv.canny_edge_detect(img=img, mask=mask, mask_color="gray") def test_plantcv_closing(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_closing") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MULTI), -1) gray_img = cv2.cvtColor(rgb_img, cv2.COLOR_BGR2GRAY) bin_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) # Test with debug=None pcv.params.debug = None _ = pcv.closing(gray_img) # Test with debug='plot' pcv.params.debug = 'plot' _ = pcv.closing(bin_img, np.ones((4, 4), np.uint8)) # Test with debug='print' pcv.params.debug = 'print' filtered_img = pcv.closing(bin_img) assert np.sum(filtered_img) == 16261860 def test_plantcv_closing_bad_input(): # Read in test data rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MULTI), -1) with pytest.raises(RuntimeError): _ = pcv.closing(rgb_img) def test_plantcv_cluster_contours(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_cluster_contours") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img1 = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MULTI), -1) roi_objects = np.load(os.path.join(TEST_DATA, TEST_INPUT_MULTI_OBJECT), encoding="latin1") hierarchy = np.load(os.path.join(TEST_DATA, TEST_INPUT_MULTI_HIERARCHY), encoding="latin1") objs = [roi_objects[arr_n] for arr_n in roi_objects] obj_hierarchy = hierarchy['arr_0'] # Test with debug = "print" pcv.params.debug = "print" _ = pcv.cluster_contours(img=img1, roi_objects=objs, roi_obj_hierarchy=obj_hierarchy, nrow=4, ncol=6) _ = pcv.cluster_contours(img=img1, roi_objects=objs, roi_obj_hierarchy=obj_hierarchy, show_grid=True) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.cluster_contours(img=img1, roi_objects=objs, roi_obj_hierarchy=obj_hierarchy, nrow=4, ncol=6) # Test with debug = None pcv.params.debug = None clusters_i, contours, hierarchy = pcv.cluster_contours(img=img1, roi_objects=objs, roi_obj_hierarchy=obj_hierarchy, nrow=4, ncol=6) lenori = len(objs) lenclust = len(clusters_i) assert lenori > lenclust def test_plantcv_cluster_contours_grayscale_input(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_cluster_contours_grayscale_input") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img1 = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MULTI), 0) roi_objects = np.load(os.path.join(TEST_DATA, TEST_INPUT_MULTI_OBJECT), encoding="latin1") hierachy = np.load(os.path.join(TEST_DATA, TEST_INPUT_MULTI_HIERARCHY), encoding="latin1") objs = [roi_objects[arr_n] for arr_n in roi_objects] obj_hierarchy = hierachy['arr_0'] # Test with debug = "print" pcv.params.debug = "print" _ = pcv.cluster_contours(img=img1, roi_objects=objs, roi_obj_hierarchy=obj_hierarchy, nrow=4, ncol=6) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.cluster_contours(img=img1, roi_objects=objs, roi_obj_hierarchy=obj_hierarchy, nrow=4, ncol=6) # Test with debug = None pcv.params.debug = None clusters_i, contours, hierachy = pcv.cluster_contours(img=img1, roi_objects=objs, roi_obj_hierarchy=obj_hierarchy, nrow=4, ncol=6) lenori = len(objs) lenclust = len(clusters_i) assert lenori > lenclust def test_plantcv_cluster_contours_splitimg(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_cluster_contours_splitimg") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img1 = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MULTI), -1) contours = np.load(os.path.join(TEST_DATA, TEST_INPUT_MULTI_CONTOUR), encoding="latin1") clusters = np.load(os.path.join(TEST_DATA, TEST_INPUT_ClUSTER_CONTOUR), encoding="latin1") hierachy = np.load(os.path.join(TEST_DATA, TEST_INPUT_MULTI_HIERARCHY), encoding="latin1") cluster_names = os.path.join(TEST_DATA, TEST_INPUT_GENOTXT) cluster_names_too_many = os.path.join(TEST_DATA, TEST_INPUT_GENOTXT_TOO_MANY) roi_contours = [contours[arr_n] for arr_n in contours] cluster_contours = [clusters[arr_n] for arr_n in clusters] obj_hierarchy = hierachy['arr_0'] # Test with debug = None pcv.params.debug = None _, _, _ = pcv.cluster_contour_splitimg(img=img1, grouped_contour_indexes=cluster_contours, contours=roi_contours, hierarchy=obj_hierarchy, outdir=cache_dir, file=None, filenames=None) _, _, _ = pcv.cluster_contour_splitimg(img=img1, grouped_contour_indexes=[[0]], contours=[], hierarchy=np.array([[[1, -1, -1, -1]]])) _, _, _ = pcv.cluster_contour_splitimg(img=img1, grouped_contour_indexes=cluster_contours, contours=roi_contours, hierarchy=obj_hierarchy, outdir=cache_dir, file='multi', filenames=None) _, _, _ = pcv.cluster_contour_splitimg(img=img1, grouped_contour_indexes=cluster_contours, contours=roi_contours, hierarchy=obj_hierarchy, outdir=None, file=None, filenames=cluster_names) _, _, _ = pcv.cluster_contour_splitimg(img=img1, grouped_contour_indexes=cluster_contours, contours=roi_contours, hierarchy=obj_hierarchy, outdir=None, file=None, filenames=cluster_names_too_many) output_path, imgs, masks = pcv.cluster_contour_splitimg(img=img1, grouped_contour_indexes=cluster_contours, contours=roi_contours, hierarchy=obj_hierarchy, outdir=None, file=None, filenames=None) assert len(output_path) != 0 def test_plantcv_cluster_contours_splitimg_grayscale(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_cluster_contours_splitimg_grayscale") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img1 = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MULTI), 0) contours = np.load(os.path.join(TEST_DATA, TEST_INPUT_MULTI_CONTOUR), encoding="latin1") clusters = np.load(os.path.join(TEST_DATA, TEST_INPUT_ClUSTER_CONTOUR), encoding="latin1") hierachy = np.load(os.path.join(TEST_DATA, TEST_INPUT_MULTI_HIERARCHY), encoding="latin1") cluster_names = os.path.join(TEST_DATA, TEST_INPUT_GENOTXT) cluster_names_too_many = os.path.join(TEST_DATA, TEST_INPUT_GENOTXT_TOO_MANY) roi_contours = [contours[arr_n] for arr_n in contours] cluster_contours = [clusters[arr_n] for arr_n in clusters] obj_hierarchy = hierachy['arr_0'] pcv.params.debug = None output_path, imgs, masks = pcv.cluster_contour_splitimg(img=img1, grouped_contour_indexes=cluster_contours, contours=roi_contours, hierarchy=obj_hierarchy, outdir=None, file=None, filenames=None) assert len(output_path) != 0 def test_plantcv_color_palette(): # Return a color palette colors = pcv.color_palette(num=10, saved=False) assert np.shape(colors) == (10, 3) def test_plantcv_color_palette_random(): # Return a color palette in random order pcv.params.color_sequence = "random" colors = pcv.color_palette(num=10, saved=False) assert np.shape(colors) == (10, 3) def test_plantcv_color_palette_saved(): # Return a color palette that was saved pcv.params.saved_color_scale = [[0, 0, 0], [255, 255, 255]] colors = pcv.color_palette(num=2, saved=True) assert colors == [[0, 0, 0], [255, 255, 255]] def test_plantcv_crop(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_crop") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir img, _, _ = pcv.readimage(os.path.join(TEST_DATA, TEST_INPUT_NIR_MASK), 'gray') # Test with debug = "print" pcv.params.debug = "print" _ = pcv.crop(img=img, x=10, y=10, h=50, w=50) # Test with debug = "plot" pcv.params.debug = "plot" cropped = pcv.crop(img=img, x=10, y=10, h=50, w=50) assert np.shape(cropped) == (50, 50) def test_plantcv_crop_hyperspectral(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_crop_hyperspectral") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = np.ones((2056, 2454)) img_stacked = cv2.merge((img, img, img, img)) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.crop(img=img_stacked, x=10, y=10, h=50, w=50) # Test with debug = "plot" pcv.params.debug = "plot" cropped = pcv.crop(img=img_stacked, x=10, y=10, h=50, w=50) assert np.shape(cropped) == (50, 50, 4) def test_plantcv_crop_position_mask(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_crop_position_mask") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data nir, path1, filename1 = pcv.readimage(os.path.join(TEST_DATA, TEST_INPUT_NIR_MASK), 'gray') mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MASK), -1) mask_three_channel = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MASK), -1) mask_resize = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MASK_RESIZE), -1) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.crop_position_mask(nir, mask, x=40, y=3, v_pos="top", h_pos="right") _ = pcv.crop_position_mask(nir, mask_resize, x=40, y=3, v_pos="top", h_pos="right") _ = pcv.crop_position_mask(nir, mask_three_channel, x=40, y=3, v_pos="top", h_pos="right") # Test with debug = "print" with bottom _ = pcv.crop_position_mask(nir, mask, x=40, y=3, v_pos="bottom", h_pos="left") # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.crop_position_mask(nir, mask, x=40, y=3, v_pos="top", h_pos="right") # Test with debug = "plot" with bottom _ = pcv.crop_position_mask(nir, mask, x=45, y=2, v_pos="bottom", h_pos="left") # Test with debug = None pcv.params.debug = None newmask = pcv.crop_position_mask(nir, mask, x=40, y=3, v_pos="top", h_pos="right") assert np.sum(newmask) == 707115 def test_plantcv_crop_position_mask_color(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_crop_position_mask") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data nir, path1, filename1 = pcv.readimage(os.path.join(TEST_DATA, TEST_INPUT_COLOR), mode='native') mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MASK), -1) mask_resize = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MASK_RESIZE)) mask_non_binary = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MASK)) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.crop_position_mask(nir, mask, x=40, y=3, v_pos="top", h_pos="right") # Test with debug = "print" with bottom _ = pcv.crop_position_mask(nir, mask, x=40, y=3, v_pos="bottom", h_pos="left") # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.crop_position_mask(nir, mask, x=40, y=3, v_pos="top", h_pos="right") # Test with debug = "plot" with bottom _ = pcv.crop_position_mask(nir, mask, x=45, y=2, v_pos="bottom", h_pos="left") _ = pcv.crop_position_mask(nir, mask_non_binary, x=45, y=2, v_pos="bottom", h_pos="left") _ = pcv.crop_position_mask(nir, mask_non_binary, x=45, y=2, v_pos="top", h_pos="left") _ = pcv.crop_position_mask(nir, mask_non_binary, x=45, y=2, v_pos="bottom", h_pos="right") _ = pcv.crop_position_mask(nir, mask_resize, x=45, y=2, v_pos="top", h_pos="left") # Test with debug = None pcv.params.debug = None newmask = pcv.crop_position_mask(nir, mask, x=40, y=3, v_pos="top", h_pos="right") assert np.sum(newmask) == 707115 def test_plantcv_crop_position_mask_bad_input_x(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_crop_position_mask") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MASK), -1) # Read in test data nir, path1, filename1 = pcv.readimage(os.path.join(TEST_DATA, TEST_INPUT_NIR_MASK)) pcv.params.debug = None with pytest.raises(RuntimeError): _ = pcv.crop_position_mask(nir, mask, x=-1, y=-1, v_pos="top", h_pos="right") def test_plantcv_crop_position_mask_bad_input_vpos(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_crop_position_mask") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MASK), -1) # Read in test data nir, path1, filename1 = pcv.readimage(os.path.join(TEST_DATA, TEST_INPUT_NIR_MASK)) pcv.params.debug = None with pytest.raises(RuntimeError): _ = pcv.crop_position_mask(nir, mask, x=40, y=3, v_pos="below", h_pos="right") def test_plantcv_crop_position_mask_bad_input_hpos(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_crop_position_mask") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MASK), -1) # Read in test data nir, path1, filename1 = pcv.readimage(os.path.join(TEST_DATA, TEST_INPUT_NIR_MASK)) pcv.params.debug = None with pytest.raises(RuntimeError): _ = pcv.crop_position_mask(nir, mask, x=40, y=3, v_pos="top", h_pos="starboard") def test_plantcv_dilate(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_dilate") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.dilate(gray_img=img, ksize=5, i=1) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.dilate(gray_img=img, ksize=5, i=1) # Test with debug = None pcv.params.debug = None dilate_img = pcv.dilate(gray_img=img, ksize=5, i=1) # Assert that the output image has the dimensions of the input image if all([i == j] for i, j in zip(np.shape(dilate_img), TEST_BINARY_DIM)): # Assert that the image is binary if all([i == j] for i, j in zip(np.unique(dilate_img), [0, 255])): assert 1 else: assert 0 else: assert 0 def test_plantcv_dilate_small_k(): # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) # Test with debug = None pcv.params.debug = None with pytest.raises(ValueError): _ = pcv.dilate(img, 1, 1) def test_plantcv_erode(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_erode") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.erode(gray_img=img, ksize=5, i=1) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.erode(gray_img=img, ksize=5, i=1) # Test with debug = None pcv.params.debug = None erode_img = pcv.erode(gray_img=img, ksize=5, i=1) # Assert that the output image has the dimensions of the input image if all([i == j] for i, j in zip(np.shape(erode_img), TEST_BINARY_DIM)): # Assert that the image is binary if all([i == j] for i, j in zip(np.unique(erode_img), [0, 255])): assert 1 else: assert 0 else: assert 0 def test_plantcv_erode_small_k(): # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) # Test with debug = None pcv.params.debug = None with pytest.raises(ValueError): _ = pcv.erode(img, 1, 1) def test_plantcv_distance_transform(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_distance_transform") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_CROPPED_MASK), -1) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.distance_transform(bin_img=mask, distance_type=1, mask_size=3) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.distance_transform(bin_img=mask, distance_type=1, mask_size=3) # Test with debug = None pcv.params.debug = None distance_transform_img = pcv.distance_transform(bin_img=mask, distance_type=1, mask_size=3) # Assert that the output image has the dimensions of the input image assert all([i == j] for i, j in zip(np.shape(distance_transform_img), np.shape(mask))) def test_plantcv_fatal_error(): # Verify that the fatal_error function raises a RuntimeError with pytest.raises(RuntimeError): pcv.fatal_error("Test error") def test_plantcv_fill(): # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) # Test with debug = None pcv.params.debug = None fill_img = pcv.fill(bin_img=img, size=63632) # Assert that the output image has the dimensions of the input image # assert all([i == j] for i, j in zip(np.shape(fill_img), TEST_BINARY_DIM)) assert np.sum(fill_img) == 0 def test_plantcv_fill_bad_input(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_fill_bad_input") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) with pytest.raises(RuntimeError): _ = pcv.fill(bin_img=img, size=1) def test_plantcv_fill_holes(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_fill_holes") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.fill_holes(bin_img=img) pcv.params.debug = "plot" _ = pcv.fill_holes(bin_img=img) # Test with debug = None pcv.params.debug = None fill_img = pcv.fill_holes(bin_img=img) assert np.sum(fill_img) > np.sum(img) def test_plantcv_fill_holes_bad_input(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_fill_holes_bad_input") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) with pytest.raises(RuntimeError): _ = pcv.fill_holes(bin_img=img) def test_plantcv_find_objects(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_find_objects") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.find_objects(img=img, mask=mask) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.find_objects(img=img, mask=mask) # Test with debug = None pcv.params.debug = None contours, hierarchy = pcv.find_objects(img=img, mask=mask) # Assert the correct number of contours are found assert len(contours) == 2 def test_plantcv_find_objects_grayscale_input(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_find_objects_grayscale_input") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR), 0) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) # Test with debug = "plot" pcv.params.debug = "plot" contours, hierarchy = pcv.find_objects(img=img, mask=mask) # Assert the correct number of contours are found assert len(contours) == 2 def test_plantcv_flip(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_flip") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) img_binary = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.flip(img=img, direction="horizontal") # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.flip(img=img, direction="vertical") _ = pcv.flip(img=img_binary, direction="vertical") # Test with debug = None pcv.params.debug = None flipped_img = pcv.flip(img=img, direction="horizontal") assert all([i == j] for i, j in zip(np.shape(flipped_img), TEST_COLOR_DIM)) def test_plantcv_flip_bad_input(): img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) pcv.params.debug = None with pytest.raises(RuntimeError): _ = pcv.flip(img=img, direction="vert") def test_plantcv_gaussian_blur(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_gaussian_blur") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) img_color = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR), -1) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.gaussian_blur(img=img, ksize=(51, 51), sigma_x=0, sigma_y=None) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.gaussian_blur(img=img, ksize=(51, 51), sigma_x=0, sigma_y=None) _ = pcv.gaussian_blur(img=img_color, ksize=(51, 51), sigma_x=0, sigma_y=None) # Test with debug = None pcv.params.debug = None gaussian_img = pcv.gaussian_blur(img=img, ksize=(51, 51), sigma_x=0, sigma_y=None) imgavg = np.average(img) gavg = np.average(gaussian_img) assert gavg != imgavg def test_plantcv_get_kernel_cross(): kernel = pcv.get_kernel(size=(3, 3), shape="cross") assert (kernel == np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]])).all() def test_plantcv_get_kernel_rectangle(): kernel = pcv.get_kernel(size=(3, 3), shape="rectangle") assert (kernel == np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]])).all() def test_plantcv_get_kernel_ellipse(): kernel = pcv.get_kernel(size=(3, 3), shape="ellipse") assert (kernel == np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]])).all() def test_plantcv_get_kernel_bad_input_size(): with pytest.raises(ValueError): _ = pcv.get_kernel(size=(1, 1), shape="ellipse") def test_plantcv_get_kernel_bad_input_shape(): with pytest.raises(RuntimeError): _ = pcv.get_kernel(size=(3, 1), shape="square") def test_plantcv_get_nir_sv(): nirpath = pcv.get_nir(TEST_DATA, TEST_VIS) nirpath1 = os.path.join(TEST_DATA, TEST_NIR) assert nirpath == nirpath1 def test_plantcv_get_nir_tv(): nirpath = pcv.get_nir(TEST_DATA, TEST_VIS_TV) nirpath1 = os.path.join(TEST_DATA, TEST_NIR_TV) assert nirpath == nirpath1 def test_plantcv_hist_equalization(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hist_equalization") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.hist_equalization(gray_img=img) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.hist_equalization(gray_img=img) # Test with debug = None pcv.params.debug = None hist = pcv.hist_equalization(gray_img=img) histavg = np.average(hist) imgavg = np.average(img) assert histavg != imgavg def test_plantcv_hist_equalization_bad_input(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hist_equalization_bad_input") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), 1) # Test with debug = None pcv.params.debug = None with pytest.raises(RuntimeError): _ = pcv.hist_equalization(gray_img=img) def test_plantcv_image_add(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_image_add") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img1 = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) img2 = np.copy(img1) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.image_add(gray_img1=img1, gray_img2=img2) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.image_add(gray_img1=img1, gray_img2=img2) # Test with debug = None pcv.params.debug = None added_img = pcv.image_add(gray_img1=img1, gray_img2=img2) assert all([i == j] for i, j in zip(np.shape(added_img), TEST_BINARY_DIM)) def test_plantcv_image_fusion(): # Read in test data # 16-bit image img1 = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_FMAX), -1) img2 = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_FMIN)) # 8-bit image img2 = img_as_ubyte(img2) fused_img = pcv.image_fusion(img1, img2, [480.0], [550.0, 640.0, 800.0]) assert str(type(fused_img)) == "<class 'plantcv.plantcv.classes.Spectral_data'>" def test_plantcv_image_fusion_size_diff(): img1 = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), 0) img2 = np.copy(img1) img2 = img2[0:10, 0:10] with pytest.raises(RuntimeError): _ = pcv.image_fusion(img1, img2, [480.0, 550.0, 670.0], [480.0, 550.0, 670.0]) def test_plantcv_image_subtract(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_image_sub") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # read in images img1 = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) img2 = np.copy(img1) # Test with debug = "print" pcv.params.debug = 'print' _ = pcv.image_subtract(img1, img2) # Test with debug = "plot" pcv.params.debug = 'plot' _ = pcv.image_subtract(img1, img2) # Test with debug = None pcv.params.debug = None new_img = pcv.image_subtract(img1, img2) assert np.array_equal(new_img, np.zeros(np.shape(new_img), np.uint8)) def test_plantcv_image_subtract_fail(): # read in images img1 = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) img2 = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY)) # test with pytest.raises(RuntimeError): _ = pcv.image_subtract(img1, img2) def test_plantcv_invert(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_invert") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.invert(gray_img=img) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.invert(gray_img=img) # Test with debug = None pcv.params.debug = None inverted_img = pcv.invert(gray_img=img) # Assert that the output image has the dimensions of the input image if all([i == j] for i, j in zip(np.shape(inverted_img), TEST_BINARY_DIM)): # Assert that the image is binary if all([i == j] for i, j in zip(np.unique(inverted_img), [0, 255])): assert 1 else: assert 0 else: assert 0 def test_plantcv_landmark_reference_pt_dist(): # Clear previous outputs pcv.outputs.clear() cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_landmark_reference") os.mkdir(cache_dir) points_rescaled = [(0.0139, 0.2569), (0.2361, 0.2917), (0.3542, 0.3819), (0.3542, 0.4167), (0.375, 0.4236), (0.7431, 0.3681), (0.8958, 0.3542), (0.9931, 0.3125), (0.1667, 0.5139), (0.4583, 0.8889), (0.4931, 0.5903), (0.3889, 0.5694), (0.4792, 0.4306), (0.2083, 0.5417), (0.3194, 0.5278), (0.3889, 0.375), (0.3681, 0.3472), (0.2361, 0.0139), (0.5417, 0.2292), (0.7708, 0.3472), (0.6458, 0.3472), (0.6389, 0.5208), (0.6458, 0.625)] centroid_rescaled = (0.4685, 0.4945) bottomline_rescaled = (0.4685, 0.2569) _ = pcv.landmark_reference_pt_dist(points_r=[], centroid_r=('a', 'b'), bline_r=(0, 0)) _ = pcv.landmark_reference_pt_dist(points_r=[(10, 1000)], centroid_r=(10, 10), bline_r=(10, 10)) _ = pcv.landmark_reference_pt_dist(points_r=[], centroid_r=(0, 0), bline_r=(0, 0)) _ = pcv.landmark_reference_pt_dist(points_r=points_rescaled, centroid_r=centroid_rescaled, bline_r=bottomline_rescaled, label="prefix") assert len(pcv.outputs.observations['prefix'].keys()) == 8 def test_plantcv_laplace_filter(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_laplace_filter") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.laplace_filter(gray_img=img, ksize=1, scale=1) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.laplace_filter(gray_img=img, ksize=1, scale=1) # Test with debug = None pcv.params.debug = None lp_img = pcv.laplace_filter(gray_img=img, ksize=1, scale=1) # Assert that the output image has the dimensions of the input image assert all([i == j] for i, j in zip(np.shape(lp_img), TEST_GRAY_DIM)) def test_plantcv_logical_and(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_logical_and") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img1 = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) img2 = np.copy(img1) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.logical_and(bin_img1=img1, bin_img2=img2) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.logical_and(bin_img1=img1, bin_img2=img2) # Test with debug = None pcv.params.debug = None and_img = pcv.logical_and(bin_img1=img1, bin_img2=img2) assert all([i == j] for i, j in zip(np.shape(and_img), TEST_BINARY_DIM)) def test_plantcv_logical_or(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_logical_or") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img1 = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) img2 = np.copy(img1) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.logical_or(bin_img1=img1, bin_img2=img2) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.logical_or(bin_img1=img1, bin_img2=img2) # Test with debug = None pcv.params.debug = None or_img = pcv.logical_or(bin_img1=img1, bin_img2=img2) assert all([i == j] for i, j in zip(np.shape(or_img), TEST_BINARY_DIM)) def test_plantcv_logical_xor(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_logical_xor") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img1 = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) img2 = np.copy(img1) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.logical_xor(bin_img1=img1, bin_img2=img2) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.logical_xor(bin_img1=img1, bin_img2=img2) # Test with debug = None pcv.params.debug = None xor_img = pcv.logical_xor(bin_img1=img1, bin_img2=img2) assert all([i == j] for i, j in zip(np.shape(xor_img), TEST_BINARY_DIM)) def test_plantcv_median_blur(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_median_blur") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.median_blur(gray_img=img, ksize=5) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.median_blur(gray_img=img, ksize=5) # Test with debug = None pcv.params.debug = None blur_img = pcv.median_blur(gray_img=img, ksize=5) # Assert that the output image has the dimensions of the input image if all([i == j] for i, j in zip(np.shape(blur_img), TEST_BINARY_DIM)): # Assert that the image is binary if all([i == j] for i, j in zip(np.unique(blur_img), [0, 255])): assert 1 else: assert 0 else: assert 0 def test_plantcv_median_blur_bad_input(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_median_blur_bad_input") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) with pytest.raises(RuntimeError): _ = pcv.median_blur(img, 5.) def test_plantcv_naive_bayes_classifier(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_naive_bayes_classifier") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.naive_bayes_classifier(rgb_img=img, pdf_file=os.path.join(TEST_DATA, TEST_PDFS)) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.naive_bayes_classifier(rgb_img=img, pdf_file=os.path.join(TEST_DATA, TEST_PDFS)) # Test with debug = None pcv.params.debug = None mask = pcv.naive_bayes_classifier(rgb_img=img, pdf_file=os.path.join(TEST_DATA, TEST_PDFS)) # Assert that the output image has the dimensions of the input image if all([i == j] for i, j in zip(np.shape(mask), TEST_GRAY_DIM)): # Assert that the image is binary if all([i == j] for i, j in zip(np.unique(mask), [0, 255])): assert 1 else: assert 0 else: assert 0 def test_plantcv_naive_bayes_classifier_bad_input(): # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) pcv.params.debug = None with pytest.raises(RuntimeError): _ = pcv.naive_bayes_classifier(rgb_img=img, pdf_file=os.path.join(TEST_DATA, TEST_PDFS_BAD)) def test_plantcv_object_composition(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_object_composition") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) object_contours_npz = np.load(os.path.join(TEST_DATA, TEST_INPUT_OBJECT_CONTOURS), encoding="latin1") object_contours = [object_contours_npz[arr_n] for arr_n in object_contours_npz] object_hierarchy_npz = np.load(os.path.join(TEST_DATA, TEST_INPUT_OBJECT_HIERARCHY), encoding="latin1") object_hierarchy = object_hierarchy_npz['arr_0'] # Test with debug = "print" pcv.params.debug = "print" _ = pcv.object_composition(img=img, contours=object_contours, hierarchy=object_hierarchy) _ = pcv.object_composition(img=img, contours=[], hierarchy=object_hierarchy) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.object_composition(img=img, contours=object_contours, hierarchy=object_hierarchy) # Test with debug = None pcv.params.debug = None contours, mask = pcv.object_composition(img=img, contours=object_contours, hierarchy=object_hierarchy) # Assert that the objects have been combined contour_shape = np.shape(contours) # type: tuple assert contour_shape[1] == 1 def test_plantcv_object_composition_grayscale_input(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_object_composition_grayscale_input") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR), 0) object_contours_npz = np.load(os.path.join(TEST_DATA, TEST_INPUT_OBJECT_CONTOURS), encoding="latin1") object_contours = [object_contours_npz[arr_n] for arr_n in object_contours_npz] object_hierarchy_npz = np.load(os.path.join(TEST_DATA, TEST_INPUT_OBJECT_HIERARCHY), encoding="latin1") object_hierarchy = object_hierarchy_npz['arr_0'] # Test with debug = "plot" pcv.params.debug = "plot" contours, mask = pcv.object_composition(img=img, contours=object_contours, hierarchy=object_hierarchy) # Assert that the objects have been combined contour_shape = np.shape(contours) # type: tuple assert contour_shape[1] == 1 def test_plantcv_within_frame(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_within_frame") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data mask_ib = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MASK), -1) mask_oob = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MASK_OOB), -1) in_bounds_ib = pcv.within_frame(mask=mask_ib, border_width=1, label="prefix") in_bounds_oob = pcv.within_frame(mask=mask_oob, border_width=1) assert (in_bounds_ib is True and in_bounds_oob is False) def test_plantcv_within_frame_bad_input(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_within_frame") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data grayscale_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR), 0) with pytest.raises(RuntimeError): _ = pcv.within_frame(grayscale_img) def test_plantcv_opening(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_closing") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MULTI), -1) gray_img = cv2.cvtColor(rgb_img, cv2.COLOR_BGR2GRAY) bin_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) # Test with debug=None pcv.params.debug = None _ = pcv.opening(gray_img) # Test with debug='plot' pcv.params.debug = 'plot' _ = pcv.opening(bin_img, np.ones((4, 4), np.uint8)) # Test with debug='print' pcv.params.debug = 'print' filtered_img = pcv.opening(bin_img) assert np.sum(filtered_img) == 16184595 def test_plantcv_opening_bad_input(): # Read in test data rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MULTI), -1) with pytest.raises(RuntimeError): _ = pcv.opening(rgb_img) def test_plantcv_output_mask(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_output_mask") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) img_color = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR), -1) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.output_mask(img=img, mask=mask, filename='test.png', outdir=None, mask_only=False) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.output_mask(img=img, mask=mask, filename='test.png', outdir=cache_dir, mask_only=False) _ = pcv.output_mask(img=img_color, mask=mask, filename='test.png', outdir=None, mask_only=False) # Remove tmp files in working direcctory shutil.rmtree("ori-images") shutil.rmtree("mask-images") # Test with debug = None pcv.params.debug = None imgpath, maskpath, analysis_images = pcv.output_mask(img=img, mask=mask, filename='test.png', outdir=cache_dir, mask_only=False) assert all([os.path.exists(imgpath) is True, os.path.exists(maskpath) is True]) def test_plantcv_output_mask_true(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_output_mask") pcv.params.debug_outdir = cache_dir os.mkdir(cache_dir) # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) img_color = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR), -1) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.output_mask(img=img, mask=mask, filename='test.png', outdir=cache_dir, mask_only=True) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.output_mask(img=img_color, mask=mask, filename='test.png', outdir=cache_dir, mask_only=True) pcv.params.debug = None imgpath, maskpath, analysis_images = pcv.output_mask(img=img, mask=mask, filename='test.png', outdir=cache_dir, mask_only=False) assert all([os.path.exists(imgpath) is True, os.path.exists(maskpath) is True]) def test_plantcv_plot_image_matplotlib_input(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_pseudocolor") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) pimg = pcv.visualize.pseudocolor(gray_img=img, mask=mask, min_value=10, max_value=200) with pytest.raises(RuntimeError): pcv.plot_image(pimg) def test_plantcv_plot_image_plotnine(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_plot_image_plotnine") os.mkdir(cache_dir) dataset = pd.DataFrame({'x': [1, 2, 3, 4], 'y': [1, 2, 3, 4]}) img = ggplot(data=dataset) try: pcv.plot_image(img=img) except RuntimeError: assert False # Assert that the image was plotted without error assert True def test_plantcv_print_image(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_print_image") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img, path, img_name = pcv.readimage(filename=os.path.join(TEST_DATA, TEST_INPUT_COLOR)) filename = os.path.join(cache_dir, 'plantcv_print_image.png') pcv.print_image(img=img, filename=filename) # Assert that the file was created assert os.path.exists(filename) is True def test_plantcv_print_image_bad_type(): with pytest.raises(RuntimeError): pcv.print_image(img=[], filename="/dev/null") def test_plantcv_print_image_plotnine(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_print_image_plotnine") os.mkdir(cache_dir) dataset = pd.DataFrame({'x': [1, 2, 3, 4], 'y': [1, 2, 3, 4]}) img = ggplot(data=dataset) filename = os.path.join(cache_dir, 'plantcv_print_image.png') pcv.print_image(img=img, filename=filename) # Assert that the file was created assert os.path.exists(filename) is True def test_plantcv_print_image_matplotlib(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_print_image_plotnine") os.mkdir(cache_dir) # Input data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) plt.figure() plt.imshow(img) plot = plt.gcf() filename = os.path.join(cache_dir, 'plantcv_print_image.png') pcv.print_image(img=plot, filename=filename) # Assert that the file was created assert os.path.exists(filename) is True def test_plantcv_print_results(tmpdir): # Create a tmp directory cache_dir = tmpdir.mkdir("sub") outfile = os.path.join(cache_dir, "results.json") pcv.print_results(filename=outfile) assert os.path.exists(outfile) def test_plantcv_readimage_native(): # Test with debug = None pcv.params.debug = None _ = pcv.readimage(filename=os.path.join(TEST_DATA, TEST_INPUT_COLOR), mode='rgba') _ = pcv.readimage(filename=os.path.join(TEST_DATA, TEST_INPUT_COLOR)) img, path, img_name = pcv.readimage(filename=os.path.join(TEST_DATA, TEST_INPUT_COLOR), mode='native') # Assert that the image name returned equals the name of the input image # Assert that the path of the image returned equals the path of the input image # Assert that the dimensions of the returned image equals the expected dimensions if img_name == TEST_INPUT_COLOR and path == TEST_DATA: if all([i == j] for i, j in zip(np.shape(img), TEST_COLOR_DIM)): assert 1 else: assert 0 else: assert 0 def test_plantcv_readimage_grayscale(): # Test with debug = None pcv.params.debug = None _, _, _ = pcv.readimage(filename=os.path.join(TEST_DATA, TEST_INPUT_GRAY), mode="grey") img, path, img_name = pcv.readimage(filename=os.path.join(TEST_DATA, TEST_INPUT_GRAY), mode="gray") assert len(np.shape(img)) == 2 def test_plantcv_readimage_rgb(): # Test with debug = None pcv.params.debug = None img, path, img_name = pcv.readimage(filename=os.path.join(TEST_DATA, TEST_INPUT_GRAY), mode="rgb") assert len(np.shape(img)) == 3 def test_plantcv_readimage_rgba_as_rgb(): # Test with debug = None pcv.params.debug = None img, path, img_name = pcv.readimage(filename=os.path.join(TEST_DATA, TEST_INPUT_RGBA), mode="native") assert np.shape(img)[2] == 3 def test_plantcv_readimage_csv(): # Test with debug = None pcv.params.debug = None img, path, img_name = pcv.readimage(filename=os.path.join(TEST_DATA, TEST_INPUT_THERMAL_CSV), mode="csv") assert len(np.shape(img)) == 2 def test_plantcv_readimage_envi(): # Test with debug = None pcv.params.debug = None array_data = pcv.readimage(filename=os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA), mode="envi") if sys.version_info[0] < 3: assert len(array_data.array_type) == 8 def test_plantcv_readimage_bad_file(): with pytest.raises(RuntimeError): _ = pcv.readimage(filename=TEST_INPUT_COLOR) def test_plantcv_readbayer_default_bg(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_readbayer_default_bg") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Test with debug = "print" pcv.params.debug = "print" _, _, _ = pcv.readbayer(filename=os.path.join(TEST_DATA, TEST_INPUT_BAYER), bayerpattern="BG", alg="default") # Test with debug = "plot" pcv.params.debug = "plot" img, path, img_name = pcv.readbayer(filename=os.path.join(TEST_DATA, TEST_INPUT_BAYER), bayerpattern="BG", alg="default") assert all([i == j] for i, j in zip(np.shape(img), (335, 400, 3))) def test_plantcv_readbayer_default_gb(): # Test with debug = None pcv.params.debug = None img, path, img_name = pcv.readbayer(filename=os.path.join(TEST_DATA, TEST_INPUT_BAYER), bayerpattern="GB", alg="default") assert all([i == j] for i, j in zip(np.shape(img), (335, 400, 3))) def test_plantcv_readbayer_default_rg(): # Test with debug = None pcv.params.debug = None img, path, img_name = pcv.readbayer(filename=os.path.join(TEST_DATA, TEST_INPUT_BAYER), bayerpattern="RG", alg="default") assert all([i == j] for i, j in zip(np.shape(img), (335, 400, 3))) def test_plantcv_readbayer_default_gr(): # Test with debug = None pcv.params.debug = None img, path, img_name = pcv.readbayer(filename=os.path.join(TEST_DATA, TEST_INPUT_BAYER), bayerpattern="GR", alg="default") assert all([i == j] for i, j in zip(np.shape(img), (335, 400, 3))) def test_plantcv_readbayer_edgeaware_bg(): # Test with debug = None pcv.params.debug = None img, path, img_name = pcv.readbayer(filename=os.path.join(TEST_DATA, TEST_INPUT_BAYER), bayerpattern="BG", alg="edgeaware") assert all([i == j] for i, j in zip(np.shape(img), (335, 400, 3))) def test_plantcv_readbayer_edgeaware_gb(): # Test with debug = None pcv.params.debug = None img, path, img_name = pcv.readbayer(filename=os.path.join(TEST_DATA, TEST_INPUT_BAYER), bayerpattern="GB", alg="edgeaware") assert all([i == j] for i, j in zip(np.shape(img), (335, 400, 3))) def test_plantcv_readbayer_edgeaware_rg(): # Test with debug = None pcv.params.debug = None img, path, img_name = pcv.readbayer(filename=os.path.join(TEST_DATA, TEST_INPUT_BAYER), bayerpattern="RG", alg="edgeaware") assert all([i == j] for i, j in zip(np.shape(img), (335, 400, 3))) def test_plantcv_readbayer_edgeaware_gr(): # Test with debug = None pcv.params.debug = None img, path, img_name = pcv.readbayer(filename=os.path.join(TEST_DATA, TEST_INPUT_BAYER), bayerpattern="GR", alg="edgeaware") assert all([i == j] for i, j in zip(np.shape(img), (335, 400, 3))) def test_plantcv_readbayer_variablenumbergradients_bg(): # Test with debug = None pcv.params.debug = None img, path, img_name = pcv.readbayer(filename=os.path.join(TEST_DATA, TEST_INPUT_BAYER), bayerpattern="BG", alg="variablenumbergradients") assert all([i == j] for i, j in zip(np.shape(img), (335, 400, 3))) def test_plantcv_readbayer_variablenumbergradients_gb(): # Test with debug = None pcv.params.debug = None img, path, img_name = pcv.readbayer(filename=os.path.join(TEST_DATA, TEST_INPUT_BAYER), bayerpattern="GB", alg="variablenumbergradients") assert all([i == j] for i, j in zip(np.shape(img), (335, 400, 3))) def test_plantcv_readbayer_variablenumbergradients_rg(): # Test with debug = None pcv.params.debug = None img, path, img_name = pcv.readbayer(filename=os.path.join(TEST_DATA, TEST_INPUT_BAYER), bayerpattern="RG", alg="variablenumbergradients") assert all([i == j] for i, j in zip(np.shape(img), (335, 400, 3))) def test_plantcv_readbayer_variablenumbergradients_gr(): # Test with debug = None pcv.params.debug = None img, path, img_name = pcv.readbayer(filename=os.path.join(TEST_DATA, TEST_INPUT_BAYER), bayerpattern="GR", alg="variablenumbergradients") assert all([i == j] for i, j in zip(np.shape(img), (335, 400, 3))) def test_plantcv_readbayer_default_bad_input(): # Test with debug = None pcv.params.debug = None with pytest.raises(RuntimeError): _, _, _ = pcv.readbayer(filename=os.path.join(TEST_DATA, "no-image.png"), bayerpattern="GR", alg="default") def test_plantcv_rectangle_mask(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_rectangle_mask") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) img_color = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.rectangle_mask(img=img, p1=(0, 0), p2=(2454, 2056), color="white") _ = pcv.rectangle_mask(img=img, p1=(0, 0), p2=(2454, 2056), color="white") # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.rectangle_mask(img=img_color, p1=(0, 0), p2=(2454, 2056), color="gray") # Test with debug = None pcv.params.debug = None masked, hist, contour, heir = pcv.rectangle_mask(img=img, p1=(0, 0), p2=(2454, 2056), color="black") maskedsum = np.sum(masked) imgsum = np.sum(img) assert maskedsum < imgsum def test_plantcv_rectangle_mask_bad_input(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_rectangle_mask") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) # Test with debug = None pcv.params.debug = None with pytest.raises(RuntimeError): _ = pcv.rectangle_mask(img=img, p1=(0, 0), p2=(2454, 2056), color="whit") def test_plantcv_report_size_marker_detect(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_report_size_marker_detect") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MARKER), -1) # ROI contour roi_contour = [np.array([[[3550, 850]], [[3550, 1349]], [[4049, 1349]], [[4049, 850]]], dtype=np.int32)] roi_hierarchy = np.array([[[-1, -1, -1, -1]]], dtype=np.int32) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.report_size_marker_area(img=img, roi_contour=roi_contour, roi_hierarchy=roi_hierarchy, marker='detect', objcolor='light', thresh_channel='s', thresh=120, label="prefix") # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.report_size_marker_area(img=img, roi_contour=roi_contour, roi_hierarchy=roi_hierarchy, marker='detect', objcolor='light', thresh_channel='s', thresh=120) # Test with debug = None pcv.params.debug = None images = pcv.report_size_marker_area(img=img, roi_contour=roi_contour, roi_hierarchy=roi_hierarchy, marker='detect', objcolor='light', thresh_channel='s', thresh=120) pcv.outputs.clear() assert len(images) != 0 def test_plantcv_report_size_marker_define(): # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MARKER), -1) # ROI contour roi_contour = [np.array([[[3550, 850]], [[3550, 1349]], [[4049, 1349]], [[4049, 850]]], dtype=np.int32)] roi_hierarchy = np.array([[[-1, -1, -1, -1]]], dtype=np.int32) # Test with debug = None pcv.params.debug = None images = pcv.report_size_marker_area(img=img, roi_contour=roi_contour, roi_hierarchy=roi_hierarchy, marker='define', objcolor='light', thresh_channel='s', thresh=120) assert len(images) != 0 def test_plantcv_report_size_marker_grayscale_input(): # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) # ROI contour roi_contour = [np.array([[[0, 0]], [[0, 49]], [[49, 49]], [[49, 0]]], dtype=np.int32)] roi_hierarchy = np.array([[[-1, -1, -1, -1]]], dtype=np.int32) # Test with debug = None pcv.params.debug = None images = pcv.report_size_marker_area(img=img, roi_contour=roi_contour, roi_hierarchy=roi_hierarchy, marker='define', objcolor='light', thresh_channel='s', thresh=120) assert len(images) != 0 def test_plantcv_report_size_marker_bad_marker_input(): # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MARKER), -1) # ROI contour roi_contour = [np.array([[[3550, 850]], [[3550, 1349]], [[4049, 1349]], [[4049, 850]]], dtype=np.int32)] roi_hierarchy = np.array([[[-1, -1, -1, -1]]], dtype=np.int32) with pytest.raises(RuntimeError): _ = pcv.report_size_marker_area(img=img, roi_contour=roi_contour, roi_hierarchy=roi_hierarchy, marker='none', objcolor='light', thresh_channel='s', thresh=120) def test_plantcv_report_size_marker_bad_threshold_input(): # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MARKER), -1) # ROI contour roi_contour = [np.array([[[3550, 850]], [[3550, 1349]], [[4049, 1349]], [[4049, 850]]], dtype=np.int32)] roi_hierarchy = np.array([[[-1, -1, -1, -1]]], dtype=np.int32) with pytest.raises(RuntimeError): _ = pcv.report_size_marker_area(img=img, roi_contour=roi_contour, roi_hierarchy=roi_hierarchy, marker='detect', objcolor='light', thresh_channel=None, thresh=120) def test_plantcv_rgb2gray_cmyk(): # Test with debug = None pcv.params.debug = None # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) c = pcv.rgb2gray_cmyk(rgb_img=img, channel="c") # Assert that the output image has the dimensions of the input image but is only a single channel assert all([i == j] for i, j in zip(np.shape(c), TEST_GRAY_DIM)) def test_plantcv_rgb2gray_cmyk_bad_channel(): # Test with debug = None pcv.params.debug = None # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) with pytest.raises(RuntimeError): # Channel S is not in CMYK _ = pcv.rgb2gray_cmyk(rgb_img=img, channel="s") def test_plantcv_rgb2gray_hsv(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_rgb2gray_hsv") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.rgb2gray_hsv(rgb_img=img, channel="s") # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.rgb2gray_hsv(rgb_img=img, channel="s") # Test with debug = None pcv.params.debug = None s = pcv.rgb2gray_hsv(rgb_img=img, channel="s") # Assert that the output image has the dimensions of the input image but is only a single channel assert all([i == j] for i, j in zip(np.shape(s), TEST_GRAY_DIM)) def test_plantcv_rgb2gray_hsv_bad_input(): img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) pcv.params.debug = None with pytest.raises(RuntimeError): _ = pcv.rgb2gray_hsv(rgb_img=img, channel="l") def test_plantcv_rgb2gray_lab(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_rgb2gray_lab") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.rgb2gray_lab(rgb_img=img, channel='b') # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.rgb2gray_lab(rgb_img=img, channel='b') # Test with debug = None pcv.params.debug = None b = pcv.rgb2gray_lab(rgb_img=img, channel='b') # Assert that the output image has the dimensions of the input image but is only a single channel assert all([i == j] for i, j in zip(np.shape(b), TEST_GRAY_DIM)) def test_plantcv_rgb2gray_lab_bad_input(): img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) pcv.params.debug = None with pytest.raises(RuntimeError): _ = pcv.rgb2gray_lab(rgb_img=img, channel="v") def test_plantcv_rgb2gray(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_rgb2gray") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) # Test with debug = None pcv.params.debug = None gray = pcv.rgb2gray(rgb_img=img) # Assert that the output image has the dimensions of the input image but is only a single channel assert all([i == j] for i, j in zip(np.shape(gray), TEST_GRAY_DIM)) def test_plantcv_roi2mask(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_acute_vertex") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_VIS_SMALL)) contours_npz = np.load(os.path.join(TEST_DATA, TEST_VIS_COMP_CONTOUR), encoding="latin1") obj_contour = contours_npz['arr_0'] pcv.params.debug = "plot" _ = pcv.roi.roi2mask(img=img, contour=obj_contour) pcv.params.debug = "print" mask = pcv.roi.roi2mask(img=img, contour=obj_contour) assert np.shape(mask)[0:2] == np.shape(img)[0:2] and np.sum(mask) == 255 def test_plantcv_roi_objects(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_roi_objects") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) roi_contour_npz = np.load(os.path.join(TEST_DATA, TEST_INPUT_ROI_CONTOUR), encoding="latin1") roi_contour = [roi_contour_npz[arr_n] for arr_n in roi_contour_npz] roi_hierarchy_npz = np.load(os.path.join(TEST_DATA, TEST_INPUT_ROI_HIERARCHY), encoding="latin1") roi_hierarchy = roi_hierarchy_npz['arr_0'] object_contours_npz = np.load(os.path.join(TEST_DATA, TEST_INPUT_OBJECT_CONTOURS), encoding="latin1") object_contours = [object_contours_npz[arr_n] for arr_n in object_contours_npz] object_hierarchy_npz = np.load(os.path.join(TEST_DATA, TEST_INPUT_OBJECT_HIERARCHY), encoding="latin1") object_hierarchy = object_hierarchy_npz['arr_0'] # Test with debug = "print" pcv.params.debug = "print" _ = pcv.roi_objects(img=img, roi_contour=roi_contour, roi_hierarchy=roi_hierarchy, object_contour=object_contours, obj_hierarchy=object_hierarchy, roi_type="largest") # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.roi_objects(img=img, roi_contour=roi_contour, roi_hierarchy=roi_hierarchy, object_contour=object_contours, obj_hierarchy=object_hierarchy, roi_type="partial") # Test with debug = None and roi_type = cutto pcv.params.debug = None _ = pcv.roi_objects(img=img, roi_contour=roi_contour, roi_hierarchy=roi_hierarchy, object_contour=object_contours, obj_hierarchy=object_hierarchy, roi_type="cutto") # Test with debug = None kept_contours, kept_hierarchy, mask, area = pcv.roi_objects(img=img, roi_contour=roi_contour, roi_hierarchy=roi_hierarchy, object_contour=object_contours, obj_hierarchy=object_hierarchy, roi_type="partial") # Assert that the contours were filtered as expected assert len(kept_contours) == 1891 def test_plantcv_roi_objects_bad_input(): # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) roi_contour_npz = np.load(os.path.join(TEST_DATA, TEST_INPUT_ROI_CONTOUR), encoding="latin1") roi_contour = [roi_contour_npz[arr_n] for arr_n in roi_contour_npz] roi_hierarchy_npz = np.load(os.path.join(TEST_DATA, TEST_INPUT_ROI_HIERARCHY), encoding="latin1") roi_hierarchy = roi_hierarchy_npz['arr_0'] object_contours_npz = np.load(os.path.join(TEST_DATA, TEST_INPUT_OBJECT_CONTOURS), encoding="latin1") object_contours = [object_contours_npz[arr_n] for arr_n in object_contours_npz] object_hierarchy_npz = np.load(os.path.join(TEST_DATA, TEST_INPUT_OBJECT_HIERARCHY), encoding="latin1") object_hierarchy = object_hierarchy_npz['arr_0'] pcv.params.debug = None with pytest.raises(RuntimeError): _ = pcv.roi_objects(img=img, roi_type="cut", roi_contour=roi_contour, roi_hierarchy=roi_hierarchy, object_contour=object_contours, obj_hierarchy=object_hierarchy) def test_plantcv_roi_objects_grayscale_input(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_roi_objects_grayscale_input") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR), 0) roi_contour_npz = np.load(os.path.join(TEST_DATA, TEST_INPUT_ROI_CONTOUR), encoding="latin1") roi_contour = [roi_contour_npz[arr_n] for arr_n in roi_contour_npz] roi_hierarchy_npz = np.load(os.path.join(TEST_DATA, TEST_INPUT_ROI_HIERARCHY), encoding="latin1") roi_hierarchy = roi_hierarchy_npz['arr_0'] object_contours_npz = np.load(os.path.join(TEST_DATA, TEST_INPUT_OBJECT_CONTOURS), encoding="latin1") object_contours = [object_contours_npz[arr_n] for arr_n in object_contours_npz] object_hierarchy_npz = np.load(os.path.join(TEST_DATA, TEST_INPUT_OBJECT_HIERARCHY), encoding="latin1") object_hierarchy = object_hierarchy_npz['arr_0'] # Test with debug = "plot" pcv.params.debug = "plot" kept_contours, kept_hierarchy, mask, area = pcv.roi_objects(img=img, roi_type="partial", roi_contour=roi_contour, roi_hierarchy=roi_hierarchy, object_contour=object_contours, obj_hierarchy=object_hierarchy) # Assert that the contours were filtered as expected assert len(kept_contours) == 1891 def test_plantcv_rotate(): img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) rotated = pcv.rotate(img=img, rotation_deg=45, crop=True) imgavg = np.average(img) rotateavg = np.average(rotated) assert rotateavg != imgavg def test_plantcv_transform_rotate(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_rotate_img") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.transform.rotate(img=img, rotation_deg=45, crop=True) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.transform.rotate(img=img, rotation_deg=45, crop=True) # Test with debug = None pcv.params.debug = None rotated = pcv.transform.rotate(img=img, rotation_deg=45, crop=True) imgavg = np.average(img) rotateavg = np.average(rotated) assert rotateavg != imgavg def test_plantcv_transform_rotate_gray(): img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.transform.rotate(img=img, rotation_deg=45, crop=False) # Test with debug = None pcv.params.debug = None rotated = pcv.transform.rotate(img=img, rotation_deg=45, crop=False) imgavg = np.average(img) rotateavg = np.average(rotated) assert rotateavg != imgavg def test_plantcv_scale_features(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_scale_features") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data mask = cv2.imread(os.path.join(TEST_DATA, TEST_MASK_SMALL), -1) contours_npz = np.load(os.path.join(TEST_DATA, TEST_VIS_COMP_CONTOUR), encoding="latin1") obj_contour = contours_npz['arr_0'] # Test with debug = "print" pcv.params.debug = "print" _ = pcv.scale_features(obj=obj_contour, mask=mask, points=TEST_ACUTE_RESULT, line_position=50) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.scale_features(obj=obj_contour, mask=mask, points=TEST_ACUTE_RESULT, line_position='NA') # Test with debug = None pcv.params.debug = None points_rescaled, centroid_rescaled, bottomline_rescaled = pcv.scale_features(obj=obj_contour, mask=mask, points=TEST_ACUTE_RESULT, line_position=50) assert len(points_rescaled) == 23 def test_plantcv_scale_features_bad_input(): mask = np.array([]) obj_contour = np.array([]) pcv.params.debug = None result = pcv.scale_features(obj=obj_contour, mask=mask, points=TEST_ACUTE_RESULT, line_position=50) assert all([i == j] for i, j in zip(result, [("NA", "NA"), ("NA", "NA"), ("NA", "NA")])) def test_plantcv_scharr_filter(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_scharr_filter") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) pcv.params.debug = "print" # Test with debug = "print" _ = pcv.scharr_filter(img=img, dx=1, dy=0, scale=1) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.scharr_filter(img=img, dx=1, dy=0, scale=1) # Test with debug = None pcv.params.debug = None scharr_img = pcv.scharr_filter(img=img, dx=1, dy=0, scale=1) # Assert that the output image has the dimensions of the input image assert all([i == j] for i, j in zip(np.shape(scharr_img), TEST_GRAY_DIM)) def test_plantcv_shift_img(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_shift_img") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.shift_img(img=img, number=300, side="top") # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.shift_img(img=img, number=300, side="top") # Test with debug = "plot" _ = pcv.shift_img(img=img, number=300, side="bottom") # Test with debug = "plot" _ = pcv.shift_img(img=img, number=300, side="right") # Test with debug = "plot" _ = pcv.shift_img(img=mask, number=300, side="left") # Test with debug = None pcv.params.debug = None rotated = pcv.shift_img(img=img, number=300, side="top") imgavg = np.average(img) shiftavg = np.average(rotated) assert shiftavg != imgavg def test_plantcv_shift_img_bad_input(): # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) with pytest.raises(RuntimeError): pcv.params.debug = None _ = pcv.shift_img(img=img, number=-300, side="top") def test_plantcv_shift_img_bad_side_input(): # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) with pytest.raises(RuntimeError): pcv.params.debug = None _ = pcv.shift_img(img=img, number=300, side="starboard") def test_plantcv_sobel_filter(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_sobel_filter") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.sobel_filter(gray_img=img, dx=1, dy=0, ksize=1) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.sobel_filter(gray_img=img, dx=1, dy=0, ksize=1) # Test with debug = None pcv.params.debug = None sobel_img = pcv.sobel_filter(gray_img=img, dx=1, dy=0, ksize=1) # Assert that the output image has the dimensions of the input image assert all([i == j] for i, j in zip(np.shape(sobel_img), TEST_GRAY_DIM)) def test_plantcv_stdev_filter(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_sobel_filter") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY_SMALL), -1) pcv.params.debug = "plot" _ = pcv.stdev_filter(img=img, ksize=11) pcv.params.debug = "print" filter_img = pcv.stdev_filter(img=img, ksize=11) assert (np.shape(filter_img) == np.shape(img)) def test_plantcv_watershed_segmentation(): # Clear previous outputs pcv.outputs.clear() # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_watershed_segmentation") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_CROPPED)) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_CROPPED_MASK), -1) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.watershed_segmentation(rgb_img=img, mask=mask, distance=10, label="prefix") # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.watershed_segmentation(rgb_img=img, mask=mask, distance=10) # Test with debug = None pcv.params.debug = None _ = pcv.watershed_segmentation(rgb_img=img, mask=mask, distance=10) assert pcv.outputs.observations['default']['estimated_object_count']['value'] > 9 def test_plantcv_white_balance_gray_16bit(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_white_balance_gray_16bit") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_NIR_MASK), -1) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.white_balance(img=img, mode='hist', roi=(5, 5, 80, 80)) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.white_balance(img=img, mode='max', roi=(5, 5, 80, 80)) # Test without an ROI pcv.params.debug = None _ = pcv.white_balance(img=img, mode='hist', roi=None) # Test with debug = None white_balanced = pcv.white_balance(img=img, roi=(5, 5, 80, 80)) imgavg = np.average(img) balancedavg = np.average(white_balanced) assert balancedavg != imgavg def test_plantcv_white_balance_gray_8bit(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_white_balance_gray_8bit") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_NIR_MASK)) img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.white_balance(img=img, mode='hist', roi=(5, 5, 80, 80)) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.white_balance(img=img, mode='max', roi=(5, 5, 80, 80)) # Test without an ROI pcv.params.debug = None _ = pcv.white_balance(img=img, mode='hist', roi=None) # Test with debug = None white_balanced = pcv.white_balance(img=img, roi=(5, 5, 80, 80)) imgavg = np.average(img) balancedavg = np.average(white_balanced) assert balancedavg != imgavg def test_plantcv_white_balance_rgb(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_white_balance_rgb") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MARKER)) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.white_balance(img=img, mode='hist', roi=(5, 5, 80, 80)) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.white_balance(img=img, mode='max', roi=(5, 5, 80, 80)) # Test without an ROI pcv.params.debug = None _ = pcv.white_balance(img=img, mode='hist', roi=None) # Test with debug = None white_balanced = pcv.white_balance(img=img, roi=(5, 5, 80, 80)) imgavg = np.average(img) balancedavg = np.average(white_balanced) assert balancedavg != imgavg def test_plantcv_white_balance_bad_input(): # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_NIR_MASK), -1) # Test with debug = None with pytest.raises(RuntimeError): pcv.params.debug = "plot" _ = pcv.white_balance(img=img, mode='hist', roi=(5, 5, 5, 5, 5)) def test_plantcv_white_balance_bad_mode_input(): # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MARKER)) # Test with debug = None with pytest.raises(RuntimeError): pcv.params.debug = "plot" _ = pcv.white_balance(img=img, mode='histogram', roi=(5, 5, 80, 80)) def test_plantcv_white_balance_bad_input_int(): # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_NIR_MASK), -1) # Test with debug = None with pytest.raises(RuntimeError): pcv.params.debug = "plot" _ = pcv.white_balance(img=img, mode='hist', roi=(5., 5, 5, 5)) def test_plantcv_x_axis_pseudolandmarks(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_x_axis_pseudolandmarks_debug") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir img = cv2.imread(os.path.join(TEST_DATA, TEST_VIS_SMALL)) mask = cv2.imread(os.path.join(TEST_DATA, TEST_MASK_SMALL), -1) contours_npz = np.load(os.path.join(TEST_DATA, TEST_VIS_COMP_CONTOUR), encoding="latin1") obj_contour = contours_npz['arr_0'] pcv.params.debug = "print" _ = pcv.x_axis_pseudolandmarks(obj=obj_contour, mask=mask, img=img) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.x_axis_pseudolandmarks(obj=obj_contour, mask=mask, img=img, label="prefix") _ = pcv.x_axis_pseudolandmarks(obj=np.array([[0, 0], [0, 0]]), mask=np.array([[0, 0], [0, 0]]), img=img) _ = pcv.x_axis_pseudolandmarks(obj=np.array(([[89, 222]], [[252, 39]], [[89, 207]])), mask=np.array(([[42, 161]], [[2, 47]], [[211, 222]])), img=img) _ = pcv.x_axis_pseudolandmarks(obj=(), mask=mask, img=img) # Test with debug = None pcv.params.debug = None top, bottom, center_v = pcv.x_axis_pseudolandmarks(obj=obj_contour, mask=mask, img=img) pcv.outputs.clear() assert all([all([i == j] for i, j in zip(np.shape(top), (20, 1, 2))), all([i == j] for i, j in zip(np.shape(bottom), (20, 1, 2))), all([i == j] for i, j in zip(np.shape(center_v), (20, 1, 2)))]) def test_plantcv_x_axis_pseudolandmarks_small_obj(): img = cv2.imread(os.path.join(TEST_DATA, TEST_VIS_SMALL_PLANT)) mask = cv2.imread(os.path.join(TEST_DATA, TEST_MASK_SMALL_PLANT), -1) contours_npz = np.load(os.path.join(TEST_DATA, TEST_VIS_COMP_CONTOUR_SMALL_PLANT), encoding="latin1") obj_contour = contours_npz['arr_0'] # Test with debug = "print" pcv.params.debug = "print" _, _, _ = pcv.x_axis_pseudolandmarks(obj=[], mask=mask, img=img) _, _, _ = pcv.x_axis_pseudolandmarks(obj=obj_contour, mask=mask, img=img) # Test with debug = "plot" pcv.params.debug = "plot" _, _, _ = pcv.x_axis_pseudolandmarks(obj=[], mask=mask, img=img) top, bottom, center_v = pcv.x_axis_pseudolandmarks(obj=obj_contour, mask=mask, img=img) assert all([all([i == j] for i, j in zip(np.shape(top), (20, 1, 2))), all([i == j] for i, j in zip(np.shape(bottom), (20, 1, 2))), all([i == j] for i, j in zip(np.shape(center_v), (20, 1, 2)))]) def test_plantcv_x_axis_pseudolandmarks_bad_input(): img = np.array([]) mask = np.array([]) obj_contour = np.array([]) pcv.params.debug = None result = pcv.x_axis_pseudolandmarks(obj=obj_contour, mask=mask, img=img) assert all([i == j] for i, j in zip(result, [("NA", "NA"), ("NA", "NA"), ("NA", "NA")])) def test_plantcv_x_axis_pseudolandmarks_bad_obj_input(): img = cv2.imread(os.path.join(TEST_DATA, TEST_VIS_SMALL_PLANT)) with pytest.raises(RuntimeError): _ = pcv.x_axis_pseudolandmarks(obj=np.array([[-2, -2], [-2, -2]]), mask=np.array([[-2, -2], [-2, -2]]), img=img) def test_plantcv_y_axis_pseudolandmarks(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_y_axis_pseudolandmarks_debug") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir img = cv2.imread(os.path.join(TEST_DATA, TEST_VIS_SMALL)) mask = cv2.imread(os.path.join(TEST_DATA, TEST_MASK_SMALL), -1) contours_npz = np.load(os.path.join(TEST_DATA, TEST_VIS_COMP_CONTOUR), encoding="latin1") obj_contour = contours_npz['arr_0'] pcv.params.debug = "print" _ = pcv.y_axis_pseudolandmarks(obj=obj_contour, mask=mask, img=img, label="prefix") # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.y_axis_pseudolandmarks(obj=obj_contour, mask=mask, img=img) pcv.outputs.clear() _ = pcv.y_axis_pseudolandmarks(obj=[], mask=mask, img=img) _ = pcv.y_axis_pseudolandmarks(obj=(), mask=mask, img=img) _ = pcv.y_axis_pseudolandmarks(obj=np.array(([[89, 222]], [[252, 39]], [[89, 207]])), mask=np.array(([[42, 161]], [[2, 47]], [[211, 222]])), img=img) _ = pcv.y_axis_pseudolandmarks(obj=np.array(([[21, 11]], [[159, 155]], [[237, 11]])), mask=np.array(([[38, 54]], [[144, 169]], [[81, 137]])), img=img) # Test with debug = None pcv.params.debug = None left, right, center_h = pcv.y_axis_pseudolandmarks(obj=obj_contour, mask=mask, img=img) pcv.outputs.clear() assert all([all([i == j] for i, j in zip(np.shape(left), (20, 1, 2))), all([i == j] for i, j in zip(np.shape(right), (20, 1, 2))), all([i == j] for i, j in zip(np.shape(center_h), (20, 1, 2)))]) def test_plantcv_y_axis_pseudolandmarks_small_obj(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_y_axis_pseudolandmarks_debug") os.mkdir(cache_dir) img = cv2.imread(os.path.join(TEST_DATA, TEST_VIS_SMALL_PLANT)) mask = cv2.imread(os.path.join(TEST_DATA, TEST_MASK_SMALL_PLANT), -1) contours_npz = np.load(os.path.join(TEST_DATA, TEST_VIS_COMP_CONTOUR_SMALL_PLANT), encoding="latin1") obj_contour = contours_npz['arr_0'] # Test with debug = "print" pcv.params.debug = "print" _, _, _ = pcv.y_axis_pseudolandmarks(obj=[], mask=mask, img=img) _, _, _ = pcv.y_axis_pseudolandmarks(obj=obj_contour, mask=mask, img=img) # Test with debug = "plot" pcv.params.debug = "plot" pcv.outputs.clear() left, right, center_h = pcv.y_axis_pseudolandmarks(obj=obj_contour, mask=mask, img=img) pcv.outputs.clear() assert all([all([i == j] for i, j in zip(np.shape(left), (20, 1, 2))), all([i == j] for i, j in zip(np.shape(right), (20, 1, 2))), all([i == j] for i, j in zip(np.shape(center_h), (20, 1, 2)))]) def test_plantcv_y_axis_pseudolandmarks_bad_input(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_y_axis_pseudolandmarks_debug") os.mkdir(cache_dir) img = np.array([]) mask = np.array([]) obj_contour = np.array([]) pcv.params.debug = None result = pcv.y_axis_pseudolandmarks(obj=obj_contour, mask=mask, img=img) pcv.outputs.clear() assert all([i == j] for i, j in zip(result, [("NA", "NA"), ("NA", "NA"), ("NA", "NA")])) def test_plantcv_y_axis_pseudolandmarks_bad_obj_input(): img = cv2.imread(os.path.join(TEST_DATA, TEST_VIS_SMALL_PLANT)) with pytest.raises(RuntimeError): _ = pcv.y_axis_pseudolandmarks(obj=np.array([[-2, -2], [-2, -2]]), mask=np.array([[-2, -2], [-2, -2]]), img=img) def test_plantcv_background_subtraction(): # List to hold result of all tests. truths = [] fg_img = cv2.imread(os.path.join(TEST_DATA, TEST_FOREGROUND)) bg_img = cv2.imread(os.path.join(TEST_DATA, TEST_BACKGROUND)) big_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) # Testing if background subtraction is actually still working. # This should return an array whose sum is greater than one pcv.params.debug = None fgmask = pcv.background_subtraction(background_image=bg_img, foreground_image=fg_img) truths.append(np.sum(fgmask) > 0) fgmask = pcv.background_subtraction(background_image=big_img, foreground_image=bg_img) truths.append(np.sum(fgmask) > 0) # The same foreground subtracted from itself should be 0 fgmask = pcv.background_subtraction(background_image=fg_img, foreground_image=fg_img) truths.append(np.sum(fgmask) == 0) # The same background subtracted from itself should be 0 fgmask = pcv.background_subtraction(background_image=bg_img, foreground_image=bg_img) truths.append(np.sum(fgmask) == 0) # All of these should be true for the function to pass testing. assert (all(truths)) def test_plantcv_background_subtraction_debug(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_background_subtraction_debug") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # List to hold result of all tests. truths = [] fg_img = cv2.imread(os.path.join(TEST_DATA, TEST_FOREGROUND)) bg_img = cv2.imread(os.path.join(TEST_DATA, TEST_BACKGROUND)) # Test with debug = "print" pcv.params.debug = "print" fgmask = pcv.background_subtraction(background_image=bg_img, foreground_image=fg_img) truths.append(np.sum(fgmask) > 0) # Test with debug = "plot" pcv.params.debug = "plot" fgmask = pcv.background_subtraction(background_image=bg_img, foreground_image=fg_img) truths.append(np.sum(fgmask) > 0) # All of these should be true for the function to pass testing. assert (all(truths)) def test_plantcv_background_subtraction_bad_img_type(): fg_color = cv2.imread(os.path.join(TEST_DATA, TEST_FOREGROUND)) bg_gray = cv2.imread(os.path.join(TEST_DATA, TEST_BACKGROUND), 0) pcv.params.debug = None with pytest.raises(RuntimeError): _ = pcv.background_subtraction(background_image=bg_gray, foreground_image=fg_color) def test_plantcv_background_subtraction_different_sizes(): fg_img = cv2.imread(os.path.join(TEST_DATA, TEST_FOREGROUND)) bg_img = cv2.imread(os.path.join(TEST_DATA, TEST_BACKGROUND)) bg_shp = np.shape(bg_img) # type: tuple bg_img_resized = cv2.resize(bg_img, (int(bg_shp[0] / 2), int(bg_shp[1] / 2)), interpolation=cv2.INTER_AREA) pcv.params.debug = None fgmask = pcv.background_subtraction(background_image=bg_img_resized, foreground_image=fg_img) assert np.sum(fgmask) > 0 def test_plantcv_spatial_clustering_dbscan(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_spatial_clustering_dbscan") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MULTI_MASK), -1) pcv.params.debug = "print" _ = pcv.spatial_clustering(img, algorithm="DBSCAN", min_cluster_size=10, max_distance=None) pcv.params.debug = "plot" spmask = pcv.spatial_clustering(img, algorithm="DBSCAN", min_cluster_size=10, max_distance=None) assert len(spmask[1]) == 2 def test_plantcv_spatial_clustering_optics(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_spatial_clustering_optics") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MULTI_MASK), -1) pcv.params.debug = None spmask = pcv.spatial_clustering(img, algorithm="OPTICS", min_cluster_size=100, max_distance=5000) assert len(spmask[1]) == 2 def test_plantcv_spatial_clustering_badinput(): img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MULTI_MASK), -1) pcv.params.debug = None with pytest.raises(NameError): _ = pcv.spatial_clustering(img, algorithm="Hydra", min_cluster_size=5, max_distance=100) # ############################## # Tests for the learn subpackage # ############################## def test_plantcv_learn_naive_bayes(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_learn_naive_bayes") os.mkdir(cache_dir) # Make image and mask directories in the cache directory imgdir = os.path.join(cache_dir, "images") maskdir = os.path.join(cache_dir, "masks") if not os.path.exists(imgdir): os.mkdir(imgdir) if not os.path.exists(maskdir): os.mkdir(maskdir) # Copy and image and mask to the image/mask directories shutil.copyfile(os.path.join(TEST_DATA, TEST_VIS_SMALL), os.path.join(imgdir, "image.png")) shutil.copyfile(os.path.join(TEST_DATA, TEST_MASK_SMALL), os.path.join(maskdir, "image.png")) # Run the naive Bayes training module outfile = os.path.join(cache_dir, "naive_bayes_pdfs.txt") plantcv.learn.naive_bayes(imgdir=imgdir, maskdir=maskdir, outfile=outfile, mkplots=True) assert os.path.exists(outfile) def test_plantcv_learn_naive_bayes_multiclass(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_learn_naive_bayes_multiclass") os.mkdir(cache_dir) # Run the naive Bayes multiclass training module outfile = os.path.join(cache_dir, "naive_bayes_multiclass_pdfs.txt") plantcv.learn.naive_bayes_multiclass(samples_file=os.path.join(TEST_DATA, TEST_SAMPLED_RGB_POINTS), outfile=outfile, mkplots=True) assert os.path.exists(outfile) # #################################### # Tests for the morphology subpackage # #################################### def test_plantcv_morphology_segment_curvature(): # Clear previous outputs pcv.outputs.clear() # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_morphology_curvature") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir skeleton = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_SKELETON_PRUNED), -1) pcv.params.debug = "print" segmented_img, seg_objects = pcv.morphology.segment_skeleton(skel_img=skeleton) pcv.outputs.clear() _ = pcv.morphology.segment_curvature(segmented_img, seg_objects, label="prefix") pcv.params.debug = "plot" pcv.outputs.clear() _ = pcv.morphology.segment_curvature(segmented_img, seg_objects) assert len(pcv.outputs.observations['default']['segment_curvature']['value']) == 22 def test_plantcv_morphology_check_cycles(): # Clear previous outputs pcv.outputs.clear() # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_morphology_branches") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) pcv.params.debug = "print" _ = pcv.morphology.check_cycles(mask, label="prefix") pcv.params.debug = "plot" _ = pcv.morphology.check_cycles(mask) pcv.params.debug = None _ = pcv.morphology.check_cycles(mask) assert pcv.outputs.observations['default']['num_cycles']['value'] == 1 def test_plantcv_morphology_find_branch_pts(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_morphology_branches") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) skeleton = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_SKELETON), -1) pcv.params.debug = "print" _ = pcv.morphology.find_branch_pts(skel_img=skeleton, mask=mask, label="prefix") pcv.params.debug = "plot" _ = pcv.morphology.find_branch_pts(skel_img=skeleton) pcv.params.debug = None branches = pcv.morphology.find_branch_pts(skel_img=skeleton) assert np.sum(branches) == 9435 def test_plantcv_morphology_find_tips(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_morphology_tips") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) skeleton = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_SKELETON), -1) pcv.params.debug = "print" _ = pcv.morphology.find_tips(skel_img=skeleton, mask=mask, label="prefix") pcv.params.debug = "plot" _ = pcv.morphology.find_tips(skel_img=skeleton) pcv.params.debug = None tips = pcv.morphology.find_tips(skel_img=skeleton) assert np.sum(tips) == 9435 def test_plantcv_morphology_prune(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_morphology_pruned") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir skeleton = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_SKELETON), -1) pcv.params.debug = "print" _ = pcv.morphology.prune(skel_img=skeleton, size=1) pcv.params.debug = "plot" _ = pcv.morphology.prune(skel_img=skeleton, size=1, mask=skeleton) pcv.params.debug = None pruned_img, _, _ = pcv.morphology.prune(skel_img=skeleton, size=3) assert np.sum(pruned_img) < np.sum(skeleton) def test_plantcv_morphology_prune_size0(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_morphology_pruned") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir skeleton = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_SKELETON), -1) pruned_img, _, _ = pcv.morphology.prune(skel_img=skeleton, size=0) assert np.sum(pruned_img) == np.sum(skeleton) def test_plantcv_morphology_iterative_prune(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_morphology_pruned") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir skeleton = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_SKELETON), -1) pruned_img = pcv.morphology._iterative_prune(skel_img=skeleton, size=3) assert np.sum(pruned_img) < np.sum(skeleton) def test_plantcv_morphology_segment_skeleton(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_morphology_segment_skeleton") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) skeleton = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_SKELETON), -1) pcv.params.debug = "print" _ = pcv.morphology.segment_skeleton(skel_img=skeleton, mask=mask) pcv.params.debug = "plot" segmented_img, segment_objects = pcv.morphology.segment_skeleton(skel_img=skeleton) assert len(segment_objects) == 73 def test_plantcv_morphology_fill_segments(): # Clear previous outputs pcv.outputs.clear() mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) obj_dic = np.load(os.path.join(TEST_DATA, TEST_SKELETON_OBJECTS)) obj = [] for key, val in obj_dic.items(): obj.append(val) pcv.params.debug = None _ = pcv.morphology.fill_segments(mask, obj) tests = [pcv.outputs.observations['default']['segment_area']['value'][42] == 5529, pcv.outputs.observations['default']['segment_area']['value'][20] == 5057, pcv.outputs.observations['default']['segment_area']['value'][49] == 3323] assert all(tests) def test_plantcv_morphology_fill_segments_with_stem(): # Clear previous outputs pcv.outputs.clear() mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) obj_dic = np.load(os.path.join(TEST_DATA, TEST_SKELETON_OBJECTS)) obj = [] for key, val in obj_dic.items(): obj.append(val) stem_obj = obj[0:4] pcv.params.debug = None _ = pcv.morphology.fill_segments(mask, obj, stem_obj) num_objects = len(pcv.outputs.observations['default']['leaf_area']['value']) assert num_objects == 69 def test_plantcv_morphology_segment_angle(): # Clear previous outputs pcv.outputs.clear() # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_morphology_segment_angles") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir skeleton = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_SKELETON_PRUNED), -1) pcv.params.debug = "print" segmented_img, segment_objects = pcv.morphology.segment_skeleton(skel_img=skeleton) _ = pcv.morphology.segment_angle(segmented_img=segmented_img, objects=segment_objects, label="prefix") pcv.params.debug = "plot" _ = pcv.morphology.segment_angle(segmented_img, segment_objects) assert len(pcv.outputs.observations['default']['segment_angle']['value']) == 22 def test_plantcv_morphology_segment_angle_overflow(): # Clear previous outputs pcv.outputs.clear() # Don't prune, would usually give overflow error without extra if statement in segment_angle # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_morphology_segment_angles") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir skeleton = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_SKELETON), -1) segmented_img, segment_objects = pcv.morphology.segment_skeleton(skel_img=skeleton) _ = pcv.morphology.segment_angle(segmented_img, segment_objects) assert len(pcv.outputs.observations['default']['segment_angle']['value']) == 73 def test_plantcv_morphology_segment_euclidean_length(): # Clear previous outputs pcv.outputs.clear() # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_morphology_segment_eu_length") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir skeleton = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_SKELETON_PRUNED), -1) pcv.params.debug = "print" segmented_img, segment_objects = pcv.morphology.segment_skeleton(skel_img=skeleton) _ = pcv.morphology.segment_euclidean_length(segmented_img, segment_objects, label="prefix") pcv.params.debug = "plot" _ = pcv.morphology.segment_euclidean_length(segmented_img, segment_objects) assert len(pcv.outputs.observations['default']['segment_eu_length']['value']) == 22 def test_plantcv_morphology_segment_euclidean_length_bad_input(): mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) skel = pcv.morphology.skeletonize(mask=mask) pcv.params.debug = None segmented_img, segment_objects = pcv.morphology.segment_skeleton(skel_img=skel) with pytest.raises(RuntimeError): _ = pcv.morphology.segment_euclidean_length(segmented_img, segment_objects) def test_plantcv_morphology_segment_path_length(): # Clear previous outputs pcv.outputs.clear() # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_morphology_segment_path_length") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir skeleton = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_SKELETON_PRUNED), -1) pcv.params.debug = "print" segmented_img, segment_objects = pcv.morphology.segment_skeleton(skel_img=skeleton) _ = pcv.morphology.segment_path_length(segmented_img, segment_objects, label="prefix") pcv.params.debug = "plot" _ = pcv.morphology.segment_path_length(segmented_img, segment_objects) assert len(pcv.outputs.observations['default']['segment_path_length']['value']) == 22 def test_plantcv_morphology_skeletonize(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_morphology_skeletonize") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) input_skeleton = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_SKELETON), -1) pcv.params.debug = "print" _ = pcv.morphology.skeletonize(mask=mask) pcv.params.debug = "plot" _ = pcv.morphology.skeletonize(mask=mask) pcv.params.debug = None skeleton = pcv.morphology.skeletonize(mask=mask) arr = np.array(skeleton == input_skeleton) assert arr.all() def test_plantcv_morphology_segment_sort(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_morphology_segment_sort") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir skeleton = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_SKELETON), -1) segmented_img, seg_objects = pcv.morphology.segment_skeleton(skel_img=skeleton) pcv.params.debug = "print" _ = pcv.morphology.segment_sort(skeleton, seg_objects, mask=skeleton) pcv.params.debug = "plot" leaf_obj, stem_obj = pcv.morphology.segment_sort(skeleton, seg_objects) assert len(leaf_obj) == 36 def test_plantcv_morphology_segment_tangent_angle(): # Clear previous outputs pcv.outputs.clear() # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_morphology_segment_tangent_angle") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir skel = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_SKELETON_PRUNED), -1) objects = np.load(os.path.join(TEST_DATA, TEST_SKELETON_OBJECTS), encoding="latin1") objs = [objects[arr_n] for arr_n in objects] pcv.params.debug = "print" _ = pcv.morphology.segment_tangent_angle(skel, objs, 2, label="prefix") pcv.params.debug = "plot" _ = pcv.morphology.segment_tangent_angle(skel, objs, 2) assert len(pcv.outputs.observations['default']['segment_tangent_angle']['value']) == 73 def test_plantcv_morphology_segment_id(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_morphology_segment_tangent_angle") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir skel = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_SKELETON_PRUNED), -1) objects = np.load(os.path.join(TEST_DATA, TEST_SKELETON_OBJECTS), encoding="latin1") objs = [objects[arr_n] for arr_n in objects] pcv.params.debug = "print" _ = pcv.morphology.segment_id(skel, objs) pcv.params.debug = "plot" _, labeled_img = pcv.morphology.segment_id(skel, objs, mask=skel) assert np.sum(labeled_img) > np.sum(skel) def test_plantcv_morphology_segment_insertion_angle(): # Clear previous outputs pcv.outputs.clear() # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_morphology_segment_insertion_angle") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir skeleton = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_SKELETON), -1) pruned, _, _ = pcv.morphology.prune(skel_img=skeleton, size=6) segmented_img, seg_objects = pcv.morphology.segment_skeleton(skel_img=pruned) leaf_obj, stem_obj = pcv.morphology.segment_sort(pruned, seg_objects) pcv.params.debug = "plot" _ = pcv.morphology.segment_insertion_angle(pruned, segmented_img, leaf_obj, stem_obj, 3, label="prefix") pcv.params.debug = "print" _ = pcv.morphology.segment_insertion_angle(pruned, segmented_img, leaf_obj, stem_obj, 10) assert pcv.outputs.observations['default']['segment_insertion_angle']['value'][:6] == ['NA', 'NA', 'NA', 24.956918822001636, 50.7313343343401, 56.427712102130734] def test_plantcv_morphology_segment_insertion_angle_bad_stem(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_morphology_segment_insertion_angle") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir skeleton = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_SKELETON), -1) pruned, _, _ = pcv.morphology.prune(skel_img=skeleton, size=5) segmented_img, seg_objects = pcv.morphology.segment_skeleton(skel_img=pruned) leaf_obj, stem_obj = pcv.morphology.segment_sort(pruned, seg_objects) stem_obj = [leaf_obj[0], leaf_obj[10]] with pytest.raises(RuntimeError): _ = pcv.morphology.segment_insertion_angle(pruned, segmented_img, leaf_obj, stem_obj, 10) def test_plantcv_morphology_segment_combine(): skel = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_SKELETON_PRUNED), -1) segmented_img, seg_objects = pcv.morphology.segment_skeleton(skel_img=skel) pcv.params.debug = "plot" # Test with list of IDs input _, new_objects = pcv.morphology.segment_combine([0, 1], seg_objects, skel) assert len(new_objects) + 1 == len(seg_objects) def test_plantcv_morphology_segment_combine_lists(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_morphology_segment_insertion_angle") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir skel = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_SKELETON_PRUNED), -1) segmented_img, seg_objects = pcv.morphology.segment_skeleton(skel_img=skel) pcv.params.debug = "print" # Test with list of lists input _, new_objects = pcv.morphology.segment_combine([[0, 1, 2], [3, 4]], seg_objects, skel) assert len(new_objects) + 3 == len(seg_objects) def test_plantcv_morphology_segment_combine_bad_input(): skel = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_SKELETON_PRUNED), -1) segmented_img, seg_objects = pcv.morphology.segment_skeleton(skel_img=skel) pcv.params.debug = "plot" with pytest.raises(RuntimeError): _, new_objects = pcv.morphology.segment_combine([0.5, 1.5], seg_objects, skel) def test_plantcv_morphology_analyze_stem(): # Clear previous outputs pcv.outputs.clear() # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_morphology_analyze_stem") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir skeleton = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_SKELETON), -1) pruned, segmented_img, _ = pcv.morphology.prune(skel_img=skeleton, size=6) segmented_img, seg_objects = pcv.morphology.segment_skeleton(skel_img=pruned) leaf_obj, stem_obj = pcv.morphology.segment_sort(pruned, seg_objects) pcv.params.debug = "plot" _ = pcv.morphology.analyze_stem(rgb_img=segmented_img, stem_objects=stem_obj, label="prefix") pcv.params.debug = "print" _ = pcv.morphology.analyze_stem(rgb_img=segmented_img, stem_objects=stem_obj) assert pcv.outputs.observations['default']['stem_angle']['value'] == -12.531776428222656 def test_plantcv_morphology_analyze_stem_bad_angle(): # Clear previous outputs pcv.outputs.clear() # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_morphology_segment_insertion_angle") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir skeleton = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_SKELETON), -1) pruned, _, _ = pcv.morphology.prune(skel_img=skeleton, size=5) segmented_img, seg_objects = pcv.morphology.segment_skeleton(skel_img=pruned) _, _ = pcv.morphology.segment_sort(pruned, seg_objects) # print([stem_obj[3]]) # stem_obj = [stem_obj[3]] stem_obj = [[[[1116, 1728]], [[1116, 1]]]] _ = pcv.morphology.analyze_stem(rgb_img=segmented_img, stem_objects=stem_obj) assert pcv.outputs.observations['default']['stem_angle']['value'] == 22877334.0 # ######################################## # Tests for the hyperspectral subpackage # ######################################## def test_plantcv_hyperspectral_read_data_default(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_read_data_default") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir pcv.params.debug = "plot" spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) _ = pcv.hyperspectral.read_data(filename=spectral_filename) pcv.params.debug = "print" array_data = pcv.hyperspectral.read_data(filename=spectral_filename) assert np.shape(array_data.array_data) == (1, 1600, 978) def test_plantcv_hyperspectral_read_data_no_default_bands(): pcv.params.debug = "plot" spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA_NO_DEFAULT) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) assert np.shape(array_data.array_data) == (1, 1600, 978) def test_plantcv_hyperspectral_read_data_approx_pseudorgb(): pcv.params.debug = "plot" spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA_APPROX_PSEUDO) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) assert np.shape(array_data.array_data) == (1, 1600, 978) def test_plantcv_hyperspectral_read_data_bad_interleave(): spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA_BAD_INTERLEAVE) with pytest.raises(RuntimeError): _ = pcv.hyperspectral.read_data(filename=spectral_filename) def test_plantcv_spectral_index_ndvi(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_index_ndvi") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir pcv.params.debug = None spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.ndvi(hsi=array_data, distance=20) assert np.shape(index_array.array_data) == (1, 1600) and np.nanmax(index_array.pseudo_rgb) == 255 def test_plantcv_spectral_index_ndvi_bad_input(): spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) pcv.params.debug = None array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.ndvi(hsi=array_data, distance=20) with pytest.raises(RuntimeError): _ = pcv.spectral_index.ndvi(hsi=index_array, distance=20) def test_plantcv_spectral_index_gdvi(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_index_gdvi") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir pcv.params.debug = None spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.gdvi(hsi=array_data, distance=20) assert np.shape(index_array.array_data) == (1, 1600) and np.nanmax(index_array.pseudo_rgb) == 255 def test_plantcv_spectral_index_gdvi_bad_input(): spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) pcv.params.debug = None array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.gdvi(hsi=array_data, distance=20) with pytest.raises(RuntimeError): _ = pcv.spectral_index.gdvi(hsi=index_array, distance=20) def test_plantcv_spectral_index_savi(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_index_savi") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir pcv.params.debug = None spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.savi(hsi=array_data, distance=20) assert np.shape(index_array.array_data) == (1, 1600) and np.nanmax(index_array.pseudo_rgb) == 255 def test_plantcv_spectral_index_savi_bad_input(): spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) pcv.params.debug = None array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.savi(hsi=array_data, distance=20) with pytest.raises(RuntimeError): _ = pcv.spectral_index.savi(hsi=index_array, distance=20) def test_plantcv_spectral_index_pri(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_index_pri") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir pcv.params.debug = None spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.pri(hsi=array_data, distance=20) assert np.shape(index_array.array_data) == (1, 1600) and np.nanmax(index_array.pseudo_rgb) == 255 def test_plantcv_spectral_index_pri_bad_input(): spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) pcv.params.debug = None array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.pri(hsi=array_data, distance=20) with pytest.raises(RuntimeError): _ = pcv.spectral_index.pri(hsi=index_array, distance=20) def test_plantcv_spectral_index_ari(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_index_ari") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir pcv.params.debug = None spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.ari(hsi=array_data, distance=20) assert np.shape(index_array.array_data) == (1, 1600) and np.nanmax(index_array.pseudo_rgb) == 255 def test_plantcv_spectral_index_ari_bad_input(): spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) pcv.params.debug = None array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.ari(hsi=array_data, distance=20) with pytest.raises(RuntimeError): _ = pcv.spectral_index.ari(hsi=index_array, distance=20) def test_plantcv_spectral_index_ci_rededge(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_index_ci_rededge") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir pcv.params.debug = None spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.ci_rededge(hsi=array_data, distance=20) assert np.shape(index_array.array_data) == (1, 1600) and np.nanmax(index_array.pseudo_rgb) == 255 def test_plantcv_spectral_index_ci_rededge_bad_input(): spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) pcv.params.debug = None array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.ci_rededge(hsi=array_data, distance=20) with pytest.raises(RuntimeError): _ = pcv.spectral_index.ci_rededge(hsi=index_array, distance=20) def test_plantcv_spectral_index_cri550(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_index_cri550") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir pcv.params.debug = None spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.cri550(hsi=array_data, distance=20) assert np.shape(index_array.array_data) == (1, 1600) and np.nanmax(index_array.pseudo_rgb) == 255 def test_plantcv_spectral_index_cri550_bad_input(): spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) pcv.params.debug = None array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.cri550(hsi=array_data, distance=20) with pytest.raises(RuntimeError): _ = pcv.spectral_index.cri550(hsi=index_array, distance=20) def test_plantcv_spectral_index_cri700(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_index_cri700") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir pcv.params.debug = None spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.cri700(hsi=array_data, distance=20) assert np.shape(index_array.array_data) == (1, 1600) and np.nanmax(index_array.pseudo_rgb) == 255 def test_plantcv_spectral_index_cri700_bad_input(): spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) pcv.params.debug = None array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.cri700(hsi=array_data, distance=20) with pytest.raises(RuntimeError): _ = pcv.spectral_index.cri700(hsi=index_array, distance=20) def test_plantcv_spectral_index_egi(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_index_egi") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir pcv.params.debug = None rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) index_array = pcv.spectral_index.egi(rgb_img=rgb_img) assert np.shape(index_array.array_data) == (2056, 2454) and np.nanmax(index_array.pseudo_rgb) == 255 def test_plantcv_spectral_index_evi(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_index_evi") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir pcv.params.debug = None spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.evi(hsi=array_data, distance=20) assert np.shape(index_array.array_data) == (1, 1600) and np.nanmax(index_array.pseudo_rgb) == 255 def test_plantcv_spectral_index_evi_bad_input(): spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) pcv.params.debug = None array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.evi(hsi=array_data, distance=20) with pytest.raises(RuntimeError): _ = pcv.spectral_index.evi(hsi=index_array, distance=20) def test_plantcv_spectral_index_mari(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_index_mari") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir pcv.params.debug = None spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.mari(hsi=array_data, distance=20) assert np.shape(index_array.array_data) == (1, 1600) and np.nanmax(index_array.pseudo_rgb) == 255 def test_plantcv_spectral_index_mari_bad_input(): spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) pcv.params.debug = None array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.mari(hsi=array_data, distance=20) with pytest.raises(RuntimeError): _ = pcv.spectral_index.mari(hsi=index_array, distance=20) def test_plantcv_spectral_index_mcari(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_index_mcari") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir pcv.params.debug = None spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.mcari(hsi=array_data, distance=20) assert np.shape(index_array.array_data) == (1, 1600) and np.nanmax(index_array.pseudo_rgb) == 255 def test_plantcv_spectral_index_mcari_bad_input(): spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) pcv.params.debug = None array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.mcari(hsi=array_data, distance=20) with pytest.raises(RuntimeError): _ = pcv.spectral_index.mcari(hsi=index_array, distance=20) def test_plantcv_spectral_index_mtci(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_index_mtci") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir pcv.params.debug = None spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.mtci(hsi=array_data, distance=20) assert np.shape(index_array.array_data) == (1, 1600) and np.nanmax(index_array.pseudo_rgb) == 255 def test_plantcv_spectral_index_mtci_bad_input(): spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) pcv.params.debug = None array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.mtci(hsi=array_data, distance=20) with pytest.raises(RuntimeError): _ = pcv.spectral_index.mtci(hsi=index_array, distance=20) def test_plantcv_spectral_index_ndre(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_index_ndre") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir pcv.params.debug = None spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.ndre(hsi=array_data, distance=20) assert np.shape(index_array.array_data) == (1, 1600) and np.nanmax(index_array.pseudo_rgb) == 255 def test_plantcv_spectral_index_ndre_bad_input(): spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) pcv.params.debug = None array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.ndre(hsi=array_data, distance=20) with pytest.raises(RuntimeError): _ = pcv.spectral_index.ndre(hsi=index_array, distance=20) def test_plantcv_spectral_index_psnd_chla(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_index_psnd_chla") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir pcv.params.debug = None spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.psnd_chla(hsi=array_data, distance=20) assert np.shape(index_array.array_data) == (1, 1600) and np.nanmax(index_array.pseudo_rgb) == 255 def test_plantcv_spectral_index_psnd_chla_bad_input(): spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) pcv.params.debug = None array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.psnd_chla(hsi=array_data, distance=20) with pytest.raises(RuntimeError): _ = pcv.spectral_index.psnd_chla(hsi=index_array, distance=20) def test_plantcv_spectral_index_psnd_chlb(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_index_psnd_chlb") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir pcv.params.debug = None spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.psnd_chlb(hsi=array_data, distance=20) assert np.shape(index_array.array_data) == (1, 1600) and np.nanmax(index_array.pseudo_rgb) == 255 def test_plantcv_spectral_index_psnd_chlb_bad_input(): spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) pcv.params.debug = None array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.psnd_chlb(hsi=array_data, distance=20) with pytest.raises(RuntimeError): _ = pcv.spectral_index.psnd_chlb(hsi=index_array, distance=20) def test_plantcv_spectral_index_psnd_car(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_index_psnd_car") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir pcv.params.debug = None spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.psnd_car(hsi=array_data, distance=20) assert np.shape(index_array.array_data) == (1, 1600) and np.nanmax(index_array.pseudo_rgb) == 255 def test_plantcv_spectral_index_psnd_car_bad_input(): spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) pcv.params.debug = None array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.psnd_car(hsi=array_data, distance=20) with pytest.raises(RuntimeError): _ = pcv.spectral_index.psnd_car(hsi=index_array, distance=20) def test_plantcv_spectral_index_psri(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_index_psri") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir pcv.params.debug = None spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.psri(hsi=array_data, distance=20) assert np.shape(index_array.array_data) == (1, 1600) and np.nanmax(index_array.pseudo_rgb) == 255 def test_plantcv_spectral_index_psri_bad_input(): spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) pcv.params.debug = None array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.psri(hsi=array_data, distance=20) with pytest.raises(RuntimeError): _ = pcv.spectral_index.psri(hsi=index_array, distance=20) def test_plantcv_spectral_index_pssr_chla(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_index_pssr_chla") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir pcv.params.debug = None spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.pssr_chla(hsi=array_data, distance=20) assert np.shape(index_array.array_data) == (1, 1600) and np.nanmax(index_array.pseudo_rgb) == 255 def test_plantcv_spectral_index_pssr_chla_bad_input(): spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) pcv.params.debug = None array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.pssr_chla(hsi=array_data, distance=20) with pytest.raises(RuntimeError): _ = pcv.spectral_index.pssr_chla(hsi=index_array, distance=20) def test_plantcv_spectral_index_pssr_chlb(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_index_pssr_chlb") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir pcv.params.debug = None spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.pssr_chlb(hsi=array_data, distance=20) assert np.shape(index_array.array_data) == (1, 1600) and np.nanmax(index_array.pseudo_rgb) == 255 def test_plantcv_spectral_index_pssr_chlb_bad_input(): spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) pcv.params.debug = None array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.pssr_chlb(hsi=array_data, distance=20) with pytest.raises(RuntimeError): _ = pcv.spectral_index.pssr_chlb(hsi=index_array, distance=20) def test_plantcv_spectral_index_pssr_car(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_index_pssr_car") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir pcv.params.debug = None spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.pssr_car(hsi=array_data, distance=20) assert np.shape(index_array.array_data) == (1, 1600) and np.nanmax(index_array.pseudo_rgb) == 255 def test_plantcv_spectral_index_pssr_car_bad_input(): spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) pcv.params.debug = None array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.pssr_car(hsi=array_data, distance=20) with pytest.raises(RuntimeError): _ = pcv.spectral_index.pssr_car(hsi=index_array, distance=20) def test_plantcv_spectral_index_rgri(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_index_rgri") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir pcv.params.debug = None spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.rgri(hsi=array_data, distance=20) assert np.shape(index_array.array_data) == (1, 1600) and np.nanmax(index_array.pseudo_rgb) == 255 def test_plantcv_spectral_index_rgri_bad_input(): spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) pcv.params.debug = None array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.rgri(hsi=array_data, distance=20) with pytest.raises(RuntimeError): _ = pcv.spectral_index.rgri(hsi=index_array, distance=20) def test_plantcv_spectral_index_rvsi(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_index_rvsi") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir pcv.params.debug = None spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.rvsi(hsi=array_data, distance=20) assert np.shape(index_array.array_data) == (1, 1600) and np.nanmax(index_array.pseudo_rgb) == 255 def test_plantcv_spectral_index_rvsi_bad_input(): spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) pcv.params.debug = None array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.rvsi(hsi=array_data, distance=20) with pytest.raises(RuntimeError): _ = pcv.spectral_index.rvsi(hsi=index_array, distance=20) def test_plantcv_spectral_index_sipi(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_index_sipi") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir pcv.params.debug = None spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.sipi(hsi=array_data, distance=20) assert np.shape(index_array.array_data) == (1, 1600) and np.nanmax(index_array.pseudo_rgb) == 255 def test_plantcv_spectral_index_sipi_bad_input(): spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) pcv.params.debug = None array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.sipi(hsi=array_data, distance=20) with pytest.raises(RuntimeError): _ = pcv.spectral_index.sipi(hsi=index_array, distance=20) def test_plantcv_spectral_index_sr(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_index_sr") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir pcv.params.debug = None spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.sr(hsi=array_data, distance=20) assert np.shape(index_array.array_data) == (1, 1600) and np.nanmax(index_array.pseudo_rgb) == 255 def test_plantcv_spectral_index_sr_bad_input(): spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) pcv.params.debug = None array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.sr(hsi=array_data, distance=20) with pytest.raises(RuntimeError): _ = pcv.spectral_index.sr(hsi=index_array, distance=20) def test_plantcv_spectral_index_vari(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_index_vari") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir pcv.params.debug = None spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.vari(hsi=array_data, distance=20) assert np.shape(index_array.array_data) == (1, 1600) and np.nanmax(index_array.pseudo_rgb) == 255 def test_plantcv_spectral_index_vari_bad_input(): spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) pcv.params.debug = None array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.vari(hsi=array_data, distance=20) with pytest.raises(RuntimeError): _ = pcv.spectral_index.vari(hsi=index_array, distance=20) def test_plantcv_spectral_index_vi_green(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_index_vi_green") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir pcv.params.debug = None spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.vi_green(hsi=array_data, distance=20) assert np.shape(index_array.array_data) == (1, 1600) and np.nanmax(index_array.pseudo_rgb) == 255 def test_plantcv_spectral_index_vi_green_bad_input(): spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) pcv.params.debug = None array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.vi_green(hsi=array_data, distance=20) with pytest.raises(RuntimeError): _ = pcv.spectral_index.vi_green(hsi=index_array, distance=20) def test_plantcv_spectral_index_wi(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_index_wi") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir pcv.params.debug = None spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.wi(hsi=array_data, distance=20) assert np.shape(index_array.array_data) == (1, 1600) and np.nanmax(index_array.pseudo_rgb) == 255 def test_plantcv_spectral_index_wi_bad_input(): spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) pcv.params.debug = None array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.wi(hsi=array_data, distance=20) with pytest.raises(RuntimeError): _ = pcv.spectral_index.wi(hsi=index_array, distance=20) def test_plantcv_hyperspectral_analyze_spectral(): # Clear previous outputs pcv.outputs.clear() cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_analyze_spectral") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir pcv.params.debug = None spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) mask = cv2.imread(os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_MASK), -1) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) # pcv.params.debug = "plot" # _ = pcv.hyperspectral.analyze_spectral(array=array_data, mask=mask, histplot=True) # pcv.params.debug = "print" # _ = pcv.hyperspectral.analyze_spectral(array=array_data, mask=mask, histplot=True, label="prefix") pcv.params.debug = None _ = pcv.hyperspectral.analyze_spectral(array=array_data, mask=mask, histplot=True, label="prefix") assert len(pcv.outputs.observations['prefix']['spectral_frequencies']['value']) == 978 def test_plantcv_hyperspectral_analyze_index(): # Clear previous outputs pcv.outputs.clear() cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_analyze_index") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.savi(hsi=array_data, distance=801) mask_img = np.ones(np.shape(index_array.array_data), dtype=np.uint8) * 255 # pcv.params.debug = "print" # pcv.hyperspectral.analyze_index(index_array=index_array, mask=mask_img, histplot=True) # pcv.params.debug = "plot" # pcv.hyperspectral.analyze_index(index_array=index_array, mask=mask_img, histplot=True) pcv.params.debug = None pcv.hyperspectral.analyze_index(index_array=index_array, mask=mask_img, histplot=True) assert pcv.outputs.observations['default']['mean_index_savi']['value'] > 0 def test_plantcv_hyperspectral_analyze_index_set_range(): # Clear previous outputs pcv.outputs.clear() cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_analyze_index_set_range") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.savi(hsi=array_data, distance=801) mask_img = np.ones(np.shape(index_array.array_data), dtype=np.uint8) * 255 pcv.params.debug = None pcv.hyperspectral.analyze_index(index_array=index_array, mask=mask_img, histplot=True, min_bin=0, max_bin=1) assert pcv.outputs.observations['default']['mean_index_savi']['value'] > 0 def test_plantcv_hyperspectral_analyze_index_auto_range(): # Clear previous outputs pcv.outputs.clear() cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_analyze_index_auto_range") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.savi(hsi=array_data, distance=801) mask_img = np.ones(np.shape(index_array.array_data), dtype=np.uint8) * 255 pcv.params.debug = None pcv.hyperspectral.analyze_index(index_array=index_array, mask=mask_img, min_bin="auto", max_bin="auto") assert pcv.outputs.observations['default']['mean_index_savi']['value'] > 0 def test_plantcv_hyperspectral_analyze_index_outside_range_warning(): import io from contextlib import redirect_stdout cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_analyze_index_auto_range") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.savi(hsi=array_data, distance=801) mask_img = np.ones(np.shape(index_array.array_data), dtype=np.uint8) * 255 f = io.StringIO() with redirect_stdout(f): pcv.params.debug = None pcv.hyperspectral.analyze_index(index_array=index_array, mask=mask_img, min_bin=.5, max_bin=.55, label="i") out = f.getvalue() # assert os.listdir(cache_dir) is 0 assert out[0:10] == 'WARNING!!!' def test_plantcv_hyperspectral_analyze_index_bad_input_mask(): pcv.params.debug = None spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.savi(hsi=array_data, distance=801) mask_img = cv2.imread(os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_MASK)) with pytest.raises(RuntimeError): pcv.hyperspectral.analyze_index(index_array=index_array, mask=mask_img) def test_plantcv_hyperspectral_analyze_index_bad_input_index(): pcv.params.debug = None spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) index_array = pcv.spectral_index.savi(hsi=array_data, distance=801) mask_img = cv2.imread(os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_MASK), -1) index_array.array_data = cv2.imread(os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_MASK)) with pytest.raises(RuntimeError): pcv.hyperspectral.analyze_index(index_array=index_array, mask=mask_img) def test_plantcv_hyperspectral_analyze_index_bad_input_datatype(): pcv.params.debug = None spectral_filename = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) array_data = pcv.hyperspectral.read_data(filename=spectral_filename) mask_img = cv2.imread(os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_MASK), -1) with pytest.raises(RuntimeError): pcv.hyperspectral.analyze_index(index_array=array_data, mask=mask_img) def test_plantcv_hyperspectral_calibrate(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_calibrate") os.mkdir(cache_dir) raw = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) white = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_WHITE) dark = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DARK) raw = pcv.hyperspectral.read_data(filename=raw) white = pcv.hyperspectral.read_data(filename=white) dark = pcv.hyperspectral.read_data(filename=dark) pcv.params.debug = "plot" _ = pcv.hyperspectral.calibrate(raw_data=raw, white_reference=white, dark_reference=dark) pcv.params.debug = "print" calibrated = pcv.hyperspectral.calibrate(raw_data=raw, white_reference=white, dark_reference=dark) assert np.shape(calibrated.array_data) == (1, 1600, 978) def test_plantcv_hyperspectral_extract_wavelength(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_hyperspectral_extract_wavelength") os.mkdir(cache_dir) spectral = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) spectral = pcv.hyperspectral.read_data(filename=spectral) pcv.params.debug = "plot" _ = pcv.hyperspectral.extract_wavelength(spectral_data=spectral, wavelength=500) pcv.params.debug = "print" new = pcv.hyperspectral.extract_wavelength(spectral_data=spectral, wavelength=500) assert np.shape(new.array_data) == (1, 1600) def test_plantcv_hyperspectral_avg_reflectance(): spectral = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) mask_img = cv2.imread(os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_MASK), -1) spectral = pcv.hyperspectral.read_data(filename=spectral) avg_reflect = pcv.hyperspectral._avg_reflectance(spectral, mask=mask_img) assert len(avg_reflect) == 978 def test_plantcv_hyperspectral_inverse_covariance(): spectral = os.path.join(HYPERSPECTRAL_TEST_DATA, HYPERSPECTRAL_DATA) spectral = pcv.hyperspectral.read_data(filename=spectral) inv_cov = pcv.hyperspectral._inverse_covariance(spectral) assert np.shape(inv_cov) == (978, 978) # ######################################## # Tests for the photosynthesis subpackage # ######################################## def test_plantcv_photosynthesis_read_dat(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_photosynthesis_read_dat") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir pcv.params.debug = "plot" fluor_filename = os.path.join(FLUOR_TEST_DATA, FLUOR_IMG) _, _, _ = pcv.photosynthesis.read_cropreporter(filename=fluor_filename) pcv.params.debug = "print" fdark, fmin, fmax = pcv.photosynthesis.read_cropreporter(filename=fluor_filename) assert np.sum(fmin) < np.sum(fmax) def test_plantcv_photosynthesis_analyze_fvfm(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_analyze_fvfm") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # filename = os.path.join(cache_dir, 'plantcv_fvfm_hist.png') # Read in test data fdark = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_FDARK), -1) fmin = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_FMIN), -1) fmax = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_FMAX), -1) fmask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_FMASK), -1) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.photosynthesis.analyze_fvfm(fdark=fdark, fmin=fmin, fmax=fmax, mask=fmask, bins=1000, label="prefix") # Test with debug = "plot" pcv.params.debug = "plot" fvfm_images = pcv.photosynthesis.analyze_fvfm(fdark=fdark, fmin=fmin, fmax=fmax, mask=fmask, bins=1000) assert len(fvfm_images) != 0 def test_plantcv_photosynthesis_analyze_fvfm_print_analysis_results(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_analyze_fvfm") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir fdark = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_FDARK), -1) fmin = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_FMIN), -1) fmax = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_FMAX), -1) fmask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_FMASK), -1) _ = pcv.photosynthesis.analyze_fvfm(fdark=fdark, fmin=fmin, fmax=fmax, mask=fmask, bins=1000) result_file = os.path.join(cache_dir, "results.txt") pcv.print_results(result_file) pcv.outputs.clear() assert os.path.exists(result_file) def test_plantcv_photosynthesis_analyze_fvfm_bad_fdark(): # Clear previous outputs pcv.outputs.clear() cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_analyze_fvfm") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data fdark = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_FDARK), -1) fmin = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_FMIN), -1) fmax = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_FMAX), -1) fmask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_FMASK), -1) _ = pcv.photosynthesis.analyze_fvfm(fdark=fdark + 3000, fmin=fmin, fmax=fmax, mask=fmask, bins=1000) check = pcv.outputs.observations['default']['fdark_passed_qc']['value'] is False assert check def test_plantcv_photosynthesis_analyze_fvfm_bad_input(): fdark = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) fmin = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_FMIN), -1) fmax = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_FMAX), -1) fmask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_FMASK), -1) with pytest.raises(RuntimeError): _ = pcv.photosynthesis.analyze_fvfm(fdark=fdark, fmin=fmin, fmax=fmax, mask=fmask, bins=1000) # ############################## # Tests for the roi subpackage # ############################## def test_plantcv_roi_from_binary_image(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_roi_from_binary_image") os.mkdir(cache_dir) # Read in test RGB image rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) # Create a binary image bin_img = np.zeros(np.shape(rgb_img)[0:2], dtype=np.uint8) cv2.rectangle(bin_img, (100, 100), (1000, 1000), 255, -1) # Test with debug = "print" pcv.params.debug = "print" pcv.params.debug_outdir = cache_dir _, _ = pcv.roi.from_binary_image(bin_img=bin_img, img=rgb_img) # Test with debug = "plot" pcv.params.debug = "plot" _, _ = pcv.roi.from_binary_image(bin_img=bin_img, img=rgb_img) # Test with debug = None pcv.params.debug = None roi_contour, roi_hierarchy = pcv.roi.from_binary_image(bin_img=bin_img, img=rgb_img) # Assert the contours and hierarchy lists contain only the ROI assert np.shape(roi_contour) == (1, 3600, 1, 2) def test_plantcv_roi_from_binary_image_grayscale_input(): # Read in a test grayscale image gray_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) # Create a binary image bin_img = np.zeros(np.shape(gray_img)[0:2], dtype=np.uint8) cv2.rectangle(bin_img, (100, 100), (1000, 1000), 255, -1) # Test with debug = "plot" pcv.params.debug = "plot" roi_contour, roi_hierarchy = pcv.roi.from_binary_image(bin_img=bin_img, img=gray_img) # Assert the contours and hierarchy lists contain only the ROI assert np.shape(roi_contour) == (1, 3600, 1, 2) def test_plantcv_roi_from_binary_image_bad_binary_input(): # Read in test RGB image rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) # Binary input is required but an RGB input is provided with pytest.raises(RuntimeError): _, _ = pcv.roi.from_binary_image(bin_img=rgb_img, img=rgb_img) def test_plantcv_roi_rectangle(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_roi_rectangle") os.mkdir(cache_dir) # Read in test RGB image rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) # Test with debug = "print" pcv.params.debug = "print" pcv.params.debug_outdir = cache_dir _, _ = pcv.roi.rectangle(x=100, y=100, h=500, w=500, img=rgb_img) # Test with debug = "plot" pcv.params.debug = "plot" _, _ = pcv.roi.rectangle(x=100, y=100, h=500, w=500, img=rgb_img) # Test with debug = None pcv.params.debug = None roi_contour, roi_hierarchy = pcv.roi.rectangle(x=100, y=100, h=500, w=500, img=rgb_img) # Assert the contours and hierarchy lists contain only the ROI assert np.shape(roi_contour) == (1, 4, 1, 2) def test_plantcv_roi_rectangle_grayscale_input(): # Read in a test grayscale image gray_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) # Test with debug = "plot" pcv.params.debug = "plot" roi_contour, roi_hierarchy = pcv.roi.rectangle(x=100, y=100, h=500, w=500, img=gray_img) # Assert the contours and hierarchy lists contain only the ROI assert np.shape(roi_contour) == (1, 4, 1, 2) def test_plantcv_roi_rectangle_out_of_frame(): # Read in test RGB image rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) # The resulting rectangle needs to be within the dimensions of the image with pytest.raises(RuntimeError): _, _ = pcv.roi.rectangle(x=100, y=100, h=500, w=3000, img=rgb_img) def test_plantcv_roi_circle(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_roi_circle") os.mkdir(cache_dir) # Read in test RGB image rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) # Test with debug = "print" pcv.params.debug = "print" pcv.params.debug_outdir = cache_dir _, _ = pcv.roi.circle(x=100, y=100, r=50, img=rgb_img) # Test with debug = "plot" pcv.params.debug = "plot" _, _ = pcv.roi.circle(x=100, y=100, r=50, img=rgb_img) # Test with debug = None pcv.params.debug = None roi_contour, roi_hierarchy = pcv.roi.circle(x=200, y=225, r=75, img=rgb_img) # Assert the contours and hierarchy lists contain only the ROI assert np.shape(roi_contour) == (1, 424, 1, 2) def test_plantcv_roi_circle_grayscale_input(): # Read in a test grayscale image gray_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) # Test with debug = "plot" pcv.params.debug = "plot" roi_contour, roi_hierarchy = pcv.roi.circle(x=200, y=225, r=75, img=gray_img) # Assert the contours and hierarchy lists contain only the ROI assert np.shape(roi_contour) == (1, 424, 1, 2) def test_plantcv_roi_circle_out_of_frame(): # Read in test RGB image rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) # The resulting rectangle needs to be within the dimensions of the image with pytest.raises(RuntimeError): _, _ = pcv.roi.circle(x=50, y=225, r=75, img=rgb_img) def test_plantcv_roi_ellipse(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_roi_ellipse") os.mkdir(cache_dir) # Read in test RGB image rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) # Test with debug = "print" pcv.params.debug = "print" pcv.params.debug_outdir = cache_dir _, _ = pcv.roi.ellipse(x=200, y=200, r1=75, r2=50, angle=0, img=rgb_img) # Test with debug = "plot" pcv.params.debug = "plot" _, _ = pcv.roi.ellipse(x=200, y=200, r1=75, r2=50, angle=0, img=rgb_img) # Test with debug = None pcv.params.debug = None roi_contour, roi_hierarchy = pcv.roi.ellipse(x=200, y=200, r1=75, r2=50, angle=0, img=rgb_img) # Assert the contours and hierarchy lists contain only the ROI assert np.shape(roi_contour) == (1, 360, 1, 2) def test_plantcv_roi_ellipse_grayscale_input(): # Read in a test grayscale image gray_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) # Test with debug = "plot" pcv.params.debug = "plot" roi_contour, roi_hierarchy = pcv.roi.ellipse(x=200, y=200, r1=75, r2=50, angle=0, img=gray_img) # Assert the contours and hierarchy lists contain only the ROI assert np.shape(roi_contour) == (1, 360, 1, 2) def test_plantcv_roi_ellipse_out_of_frame(): # Read in test RGB image rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) # The resulting rectangle needs to be within the dimensions of the image with pytest.raises(RuntimeError): _, _ = pcv.roi.ellipse(x=50, y=225, r1=75, r2=50, angle=0, img=rgb_img) def test_plantcv_roi_multi(): # Read in test RGB image rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.roi.multi(rgb_img, coord=[(25, 120), (100, 100)], radius=20) # Test with debug = None pcv.params.debug = None rois1, roi_hierarchy1 = pcv.roi.multi(rgb_img, coord=(25, 120), radius=20, spacing=(10, 10), nrows=3, ncols=6) # Assert the contours has 18 ROIs assert len(rois1) == 18 def test_plantcv_roi_multi_bad_input(): # Read in test RGB image rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) # The user must input a list of custom coordinates OR inputs to make a grid. Not both with pytest.raises(RuntimeError): _, _ = pcv.roi.multi(rgb_img, coord=[(25, 120), (100, 100)], radius=20, spacing=(10, 10), nrows=3, ncols=6) def test_plantcv_roi_multi_bad_input_oob(): # Read in test RGB image rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) # nputs to make a grid make ROIs that go off the screen with pytest.raises(RuntimeError): _, _ = pcv.roi.multi(rgb_img, coord=(25000, 12000), radius=2, spacing=(1, 1), nrows=3, ncols=6) def test_plantcv_roi_multi_bad_input_oob_list(): # Read in test RGB image rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) # All vertices in the list of centers must draw roi's that are inside the image with pytest.raises(RuntimeError): _, _ = pcv.roi.multi(rgb_img, coord=[(25000, 25000), (25000, 12000), (12000, 12000)], radius=20) def test_plantcv_roi_custom(): # Read in test RGB image img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) pcv.params.debug = "plot" cnt, hier = pcv.roi.custom(img=img, vertices=[[226, 1], [313, 184], [240, 202], [220, 229], [161, 171]]) assert np.shape(cnt) == (1, 5, 2) def test_plantcv_roi_custom_bad_input(): # Read in test RGB image img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) # ROI goes out of bounds with pytest.raises(RuntimeError): _ = pcv.roi.custom(img=img, vertices=[[226, -1], [3130, 1848], [2404, 2029], [2205, 2298], [1617, 1761]]) # ############################## # Tests for the transform subpackage # ############################## def test_plantcv_transform_get_color_matrix(): # load in target_matrix matrix_file = np.load(os.path.join(TEST_DATA, TEST_TARGET_MATRIX), encoding="latin1") matrix_compare = matrix_file['arr_0'] # Read in rgb_img and gray-scale mask rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_TARGET_IMG)) mask = cv2.imread(os.path.join(TEST_DATA, TEST_TARGET_MASK), -1) # The result should be a len(np.unique(mask))-1 x 4 matrix headers, matrix = pcv.transform.get_color_matrix(rgb_img, mask) assert np.array_equal(matrix, matrix_compare) def test_plantcv_transform_get_color_matrix_img(): # Read in two gray-scale images rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_TARGET_MASK), -1) mask = cv2.imread(os.path.join(TEST_DATA, TEST_TARGET_MASK), -1) # The input for rgb_img needs to be an RGB image with pytest.raises(RuntimeError): _, _ = pcv.transform.get_color_matrix(rgb_img, mask) def test_plantcv_transform_get_color_matrix_mask(): # Read in two gray-scale images rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_TARGET_IMG)) mask = cv2.imread(os.path.join(TEST_DATA, TEST_TARGET_MASK)) # The input for rgb_img needs to be an RGB image with pytest.raises(RuntimeError): _, _ = pcv.transform.get_color_matrix(rgb_img, mask) def test_plantcv_transform_get_matrix_m(): # load in comparison matrices matrix_m_file = np.load(os.path.join(TEST_DATA, TEST_MATRIX_M1), encoding="latin1") matrix_compare_m = matrix_m_file['arr_0'] matrix_b_file = np.load(os.path.join(TEST_DATA, TEST_MATRIX_B1), encoding="latin1") matrix_compare_b = matrix_b_file['arr_0'] # read in matrices t_matrix_file = np.load(os.path.join(TEST_DATA, TEST_TARGET_MATRIX), encoding="latin1") t_matrix = t_matrix_file['arr_0'] s_matrix_file = np.load(os.path.join(TEST_DATA, TEST_SOURCE1_MATRIX), encoding="latin1") s_matrix = s_matrix_file['arr_0'] # apply matrices to function matrix_a, matrix_m, matrix_b = pcv.transform.get_matrix_m(t_matrix, s_matrix) matrix_compare_m = np.rint(matrix_compare_m) matrix_compare_b = np.rint(matrix_compare_b) matrix_m = np.rint(matrix_m) matrix_b = np.rint(matrix_b) assert np.array_equal(matrix_m, matrix_compare_m) and np.array_equal(matrix_b, matrix_compare_b) def test_plantcv_transform_get_matrix_m_unequal_data(): # load in comparison matrices matrix_m_file = np.load(os.path.join(TEST_DATA, TEST_MATRIX_M2), encoding="latin1") matrix_compare_m = matrix_m_file['arr_0'] matrix_b_file = np.load(os.path.join(TEST_DATA, TEST_MATRIX_B2), encoding="latin1") matrix_compare_b = matrix_b_file['arr_0'] # read in matrices t_matrix_file = np.load(os.path.join(TEST_DATA, TEST_TARGET_MATRIX), encoding="latin1") t_matrix = t_matrix_file['arr_0'] s_matrix_file = np.load(os.path.join(TEST_DATA, TEST_SOURCE2_MATRIX), encoding="latin1") s_matrix = s_matrix_file['arr_0'] # apply matrices to function matrix_a, matrix_m, matrix_b = pcv.transform.get_matrix_m(t_matrix, s_matrix) matrix_compare_m = np.rint(matrix_compare_m) matrix_compare_b = np.rint(matrix_compare_b) matrix_m = np.rint(matrix_m) matrix_b = np.rint(matrix_b) assert np.array_equal(matrix_m, matrix_compare_m) and np.array_equal(matrix_b, matrix_compare_b) def test_plantcv_transform_calc_transformation_matrix(): # load in comparison matrices matrix_file = np.load(os.path.join(TEST_DATA, TEST_TRANSFORM1), encoding="latin1") matrix_compare = matrix_file['arr_0'] # read in matrices matrix_m_file = np.load(os.path.join(TEST_DATA, TEST_MATRIX_M1), encoding="latin1") matrix_m = matrix_m_file['arr_0'] matrix_b_file = np.load(os.path.join(TEST_DATA, TEST_MATRIX_B1), encoding="latin1") matrix_b = matrix_b_file['arr_0'] # apply to function _, matrix_t = pcv.transform.calc_transformation_matrix(matrix_m, matrix_b) matrix_t = np.rint(matrix_t) matrix_compare = np.rint(matrix_compare) assert np.array_equal(matrix_t, matrix_compare) def test_plantcv_transform_calc_transformation_matrix_b_incorrect(): # read in matrices matrix_m_file = np.load(os.path.join(TEST_DATA, TEST_MATRIX_M1), encoding="latin1") matrix_m = matrix_m_file['arr_0'] matrix_b_file = np.load(os.path.join(TEST_DATA, TEST_MATRIX_B1), encoding="latin1") matrix_b = matrix_b_file['arr_0'] matrix_b = np.asmatrix(matrix_b, float) with pytest.raises(RuntimeError): _, _ = pcv.transform.calc_transformation_matrix(matrix_m, matrix_b.T) def test_plantcv_transform_calc_transformation_matrix_not_mult(): # read in matrices matrix_m_file = np.load(os.path.join(TEST_DATA, TEST_MATRIX_M1), encoding="latin1") matrix_m = matrix_m_file['arr_0'] matrix_b_file = np.load(os.path.join(TEST_DATA, TEST_MATRIX_B1), encoding="latin1") matrix_b = matrix_b_file['arr_0'] with pytest.raises(RuntimeError): _, _ = pcv.transform.calc_transformation_matrix(matrix_m, matrix_b[:3]) def test_plantcv_transform_calc_transformation_matrix_not_mat(): # read in matrices matrix_m_file = np.load(os.path.join(TEST_DATA, TEST_MATRIX_M1), encoding="latin1") matrix_m = matrix_m_file['arr_0'] matrix_b_file = np.load(os.path.join(TEST_DATA, TEST_MATRIX_B1), encoding="latin1") matrix_b = matrix_b_file['arr_0'] with pytest.raises(RuntimeError): _, _ = pcv.transform.calc_transformation_matrix(matrix_m[:, 1], matrix_b[:, 1]) def test_plantcv_transform_apply_transformation(): # load corrected image to compare corrected_compare = cv2.imread(os.path.join(TEST_DATA, TEST_S1_CORRECTED)) # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_transform") os.mkdir(cache_dir) # Make image and mask directories in the cache directory imgdir = os.path.join(cache_dir, "images") # read in matrices matrix_t_file = np.load(os.path.join(TEST_DATA, TEST_TRANSFORM1), encoding="latin1") matrix_t = matrix_t_file['arr_0'] # read in images target_img = cv2.imread(os.path.join(TEST_DATA, TEST_TARGET_IMG)) source_img = cv2.imread(os.path.join(TEST_DATA, TEST_SOURCE1_IMG)) # Test with debug = "print" pcv.params.debug = "print" pcv.params.debug_outdir = imgdir _ = pcv.transform.apply_transformation_matrix(source_img, target_img, matrix_t) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.transform.apply_transformation_matrix(source_img, target_img, matrix_t) # Test with debug = None pcv.params.debug = None corrected_img = pcv.transform.apply_transformation_matrix(source_img, target_img, matrix_t) # assert source and corrected have same shape assert np.array_equal(corrected_img, corrected_compare) def test_plantcv_transform_apply_transformation_incorrect_t(): # read in matrices matrix_t_file = np.load(os.path.join(TEST_DATA, TEST_MATRIX_B1), encoding="latin1") matrix_t = matrix_t_file['arr_0'] # read in images target_img = cv2.imread(os.path.join(TEST_DATA, TEST_TARGET_IMG)) source_img = cv2.imread(os.path.join(TEST_DATA, TEST_SOURCE1_IMG)) with pytest.raises(RuntimeError): _ = pcv.transform.apply_transformation_matrix(source_img, target_img, matrix_t) def test_plantcv_transform_apply_transformation_incorrect_img(): # read in matrices matrix_t_file = np.load(os.path.join(TEST_DATA, TEST_TRANSFORM1), encoding="latin1") matrix_t = matrix_t_file['arr_0'] # read in images target_img = cv2.imread(os.path.join(TEST_DATA, TEST_TARGET_IMG)) source_img = cv2.imread(os.path.join(TEST_DATA, TEST_TARGET_MASK), -1) with pytest.raises(RuntimeError): _ = pcv.transform.apply_transformation_matrix(source_img, target_img, matrix_t) def test_plantcv_transform_save_matrix(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_transform") os.mkdir(cache_dir) # read in matrix matrix_t_file = np.load(os.path.join(TEST_DATA, TEST_TRANSFORM1), encoding="latin1") matrix_t = matrix_t_file['arr_0'] # .npz filename filename = os.path.join(cache_dir, 'test.npz') pcv.transform.save_matrix(matrix_t, filename) assert os.path.exists(filename) is True def test_plantcv_transform_save_matrix_incorrect_filename(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_transform") os.mkdir(cache_dir) # read in matrix matrix_t_file = np.load(os.path.join(TEST_DATA, TEST_TRANSFORM1), encoding="latin1") matrix_t = matrix_t_file['arr_0'] # .npz filename filename = "test" with pytest.raises(RuntimeError): pcv.transform.save_matrix(matrix_t, filename) def test_plantcv_transform_load_matrix(): # read in matrix_t matrix_t_file = np.load(os.path.join(TEST_DATA, TEST_TRANSFORM1), encoding="latin1") matrix_t = matrix_t_file['arr_0'] # test load function with matrix_t matrix_t_loaded = pcv.transform.load_matrix(os.path.join(TEST_DATA, TEST_TRANSFORM1)) assert np.array_equal(matrix_t, matrix_t_loaded) def test_plantcv_transform_correct_color(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_transform") os.mkdir(cache_dir) # load corrected image to compare corrected_compare = cv2.imread(os.path.join(TEST_DATA, TEST_S1_CORRECTED)) # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_transform_correct_color") os.mkdir(cache_dir) # Make image and mask directories in the cache directory imgdir = os.path.join(cache_dir, "images") matdir = os.path.join(cache_dir, "saved_matrices") # Read in target, source, and gray-scale mask target_img = cv2.imread(os.path.join(TEST_DATA, TEST_TARGET_IMG)) source_img = cv2.imread(os.path.join(TEST_DATA, TEST_SOURCE1_IMG)) mask = cv2.imread(os.path.join(TEST_DATA, TEST_TARGET_MASK), -1) output_path = os.path.join(matdir) # Test with debug = "print" pcv.params.debug = "print" pcv.params.debug_outdir = imgdir _, _, _, _ = pcv.transform.correct_color(target_img, mask, source_img, mask, cache_dir) # Test with debug = "plot" pcv.params.debug = "plot" _, _, _, _ = pcv.transform.correct_color(target_img, mask, source_img, mask, output_path) # Test with debug = None pcv.params.debug = None _, _, matrix_t, corrected_img = pcv.transform.correct_color(target_img, mask, source_img, mask, output_path) # assert source and corrected have same shape assert all([np.array_equal(corrected_img, corrected_compare), os.path.exists(os.path.join(output_path, "target_matrix.npz")) is True, os.path.exists(os.path.join(output_path, "source_matrix.npz")) is True, os.path.exists(os.path.join(output_path, "transformation_matrix.npz")) is True]) def test_plantcv_transform_correct_color_output_dne(): # load corrected image to compare corrected_compare = cv2.imread(os.path.join(TEST_DATA, TEST_S1_CORRECTED)) # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_transform_correct_color_output_dne") os.mkdir(cache_dir) # Make image and mask directories in the cache directory imgdir = os.path.join(cache_dir, "images") # Read in target, source, and gray-scale mask target_img = cv2.imread(os.path.join(TEST_DATA, TEST_TARGET_IMG)) source_img = cv2.imread(os.path.join(TEST_DATA, TEST_SOURCE1_IMG)) mask = cv2.imread(os.path.join(TEST_DATA, TEST_TARGET_MASK), -1) output_path = os.path.join(cache_dir, "saved_matrices_1") # output_directory that does not currently exist # Test with debug = "print" pcv.params.debug = "print" pcv.params.debug_outdir = imgdir _, _, _, _ = pcv.transform.correct_color(target_img, mask, source_img, mask, output_path) # Test with debug = "plot" pcv.params.debug = "plot" _, _, _, _ = pcv.transform.correct_color(target_img, mask, source_img, mask, output_path) # Test with debug = None pcv.params.debug = None _, _, matrix_t, corrected_img = pcv.transform.correct_color(target_img, mask, source_img, mask, output_path) # assert source and corrected have same shape assert all([np.array_equal(corrected_img, corrected_compare), os.path.exists(os.path.join(output_path, "target_matrix.npz")) is True, os.path.exists(os.path.join(output_path, "source_matrix.npz")) is True, os.path.exists(os.path.join(output_path, "transformation_matrix.npz")) is True]) def test_plantcv_transform_create_color_card_mask(): # Load target image rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_TARGET_IMG)) # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_transform_create_color_card_mask") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Test with debug = "print" pcv.params.debug = "print" _ = pcv.transform.create_color_card_mask(rgb_img=rgb_img, radius=6, start_coord=(166, 166), spacing=(21, 21), nrows=6, ncols=4, exclude=[20, 0]) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.transform.create_color_card_mask(rgb_img=rgb_img, radius=6, start_coord=(166, 166), spacing=(21, 21), nrows=6, ncols=4, exclude=[20, 0]) # Test with debug = None pcv.params.debug = None mask = pcv.transform.create_color_card_mask(rgb_img=rgb_img, radius=6, start_coord=(166, 166), spacing=(21, 21), nrows=6, ncols=4, exclude=[20, 0]) assert all([i == j] for i, j in zip(np.unique(mask), np.array([0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220], dtype=np.uint8))) def test_plantcv_transform_quick_color_check(): # Load target image t_matrix = np.load(os.path.join(TEST_DATA, TEST_TARGET_MATRIX), encoding="latin1") target_matrix = t_matrix['arr_0'] s_matrix = np.load(os.path.join(TEST_DATA, TEST_SOURCE1_MATRIX), encoding="latin1") source_matrix = s_matrix['arr_0'] # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_transform_quick_color_check") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Test with debug = "print" pcv.params.debug = "print" pcv.transform.quick_color_check(target_matrix, source_matrix, num_chips=22) # Test with debug = "plot" pcv.params.debug = "plot" pcv.transform.quick_color_check(target_matrix, source_matrix, num_chips=22) # Test with debug = None pcv.params.debug = None pcv.transform.quick_color_check(target_matrix, source_matrix, num_chips=22) assert os.path.exists(os.path.join(cache_dir, "color_quick_check.png")) def test_plantcv_transform_find_color_card(): # Load rgb image rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_TARGET_IMG)) # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_transform_find_color_card") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir df, start, space = pcv.transform.find_color_card(rgb_img=rgb_img, threshold_type='adaptgauss', blurry=False, threshvalue=90) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.transform.create_color_card_mask(rgb_img=rgb_img, radius=6, start_coord=start, spacing=space, nrows=6, ncols=4, exclude=[20, 0]) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.transform.create_color_card_mask(rgb_img=rgb_img, radius=6, start_coord=start, spacing=space, nrows=6, ncols=4, exclude=[20, 0]) # Test with debug = None pcv.params.debug = None mask = pcv.transform.create_color_card_mask(rgb_img=rgb_img, radius=6, start_coord=start, spacing=space, nrows=6, ncols=4, exclude=[20, 0]) assert all([i == j] for i, j in zip(np.unique(mask), np.array([0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220], dtype=np.uint8))) def test_plantcv_transform_find_color_card_optional_parameters(): # Clear previous outputs pcv.outputs.clear() # Load rgb image rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_TARGET_IMG_COLOR_CARD)) # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_transform_find_color_card") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Test with threshold ='normal' df1, start1, space1 = pcv.transform.find_color_card(rgb_img=rgb_img, threshold_type='normal', blurry=True, background='light', threshvalue=90, label="prefix") assert pcv.outputs.observations["prefix"]["color_chip_size"]["value"] > 15000 def test_plantcv_transform_find_color_card_otsu(): # Clear previous outputs pcv.outputs.clear() # Load rgb image rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_TARGET_IMG_COLOR_CARD)) # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_transform_find_color_card_otsu") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Test with threshold ='normal' df1, start1, space1 = pcv.transform.find_color_card(rgb_img=rgb_img, threshold_type='otsu', blurry=True, background='light', threshvalue=90, label="prefix") assert pcv.outputs.observations["prefix"]["color_chip_size"]["value"] > 15000 def test_plantcv_transform_find_color_card_optional_size_parameters(): # Clear previous outputs pcv.outputs.clear() # Load rgb image rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_TARGET_IMG_COLOR_CARD)) # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_transform_find_color_card") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir _, _, _ = pcv.transform.find_color_card(rgb_img=rgb_img, record_chip_size="mean") assert pcv.outputs.observations["default"]["color_chip_size"]["value"] > 15000 def test_plantcv_transform_find_color_card_optional_size_parameters_none(): # Clear previous outputs pcv.outputs.clear() # Load rgb image rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_TARGET_IMG_COLOR_CARD)) # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_transform_find_color_card") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir _, _, _ = pcv.transform.find_color_card(rgb_img=rgb_img, record_chip_size=None) assert pcv.outputs.observations.get("default") is None def test_plantcv_transform_find_color_card_bad_record_chip_size(): # Clear previous outputs pcv.outputs.clear() # Load rgb image rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_TARGET_IMG)) pcv.params.debug = None _, _, _ = pcv.transform.find_color_card(rgb_img=rgb_img, record_chip_size='averageeeed') assert pcv.outputs.observations["default"]["color_chip_size"]["value"] is None def test_plantcv_transform_find_color_card_bad_thresh_input(): # Load rgb image rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_TARGET_IMG)) with pytest.raises(RuntimeError): pcv.params.debug = None _, _, _ = pcv.transform.find_color_card(rgb_img=rgb_img, threshold_type='gaussian') def test_plantcv_transform_find_color_card_bad_background_input(): # Load rgb image rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_TARGET_IMG)) with pytest.raises(RuntimeError): pcv.params.debug = None _, _, _ = pcv.transform.find_color_card(rgb_img=rgb_img, background='lite') def test_plantcv_transform_find_color_card_bad_colorcard(): # Load rgb image rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_TARGET_IMG_WITH_HEXAGON)) with pytest.raises(RuntimeError): pcv.params.debug = None _, _, _ = pcv.transform.find_color_card(rgb_img=rgb_img) def test_plantcv_transform_rescale(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_transform_rescale") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir gray_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) # Test with debug = "print" pcv.params.debug = "print" _ = pcv.transform.rescale(gray_img=gray_img, min_value=0, max_value=100) pcv.params.debug = "plot" rescaled_img = pcv.transform.rescale(gray_img=gray_img, min_value=0, max_value=100) assert max(np.unique(rescaled_img)) == 100 def test_plantcv_transform_rescale_bad_input(): # Load rgb image rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_TARGET_IMG)) with pytest.raises(RuntimeError): _ = pcv.transform.rescale(gray_img=rgb_img) def test_plantcv_transform_resize(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_trancform_resize") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir gray_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY_SMALL), -1) size = (100, 100) # Test with debug "print" pcv.params.debug = "print" _ = pcv.transform.resize(img=gray_img, size=size, interpolation="auto") # Test with debug "plot" pcv.params.debug = "plot" resized_img = pcv.transform.resize(img=gray_img, size=size, interpolation="auto") assert resized_img.shape == size def test_plantcv_transform_resize_unsupported_method(): gray_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY_SMALL), -1) with pytest.raises(RuntimeError): _ = pcv.transform.resize(img=gray_img, size=(100, 100), interpolation="mymethod") def test_plantcv_transform_resize_crop(): gray_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY_SMALL), -1) size = (20, 20) resized_im = pcv.transform.resize(img=gray_img, size=size, interpolation=None) assert resized_im.shape == size def test_plantcv_transform_resize_pad(): gray_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY_SMALL), -1) size = (100, 100) resized_im = pcv.transform.resize(img=gray_img, size=size, interpolation=None) assert resized_im.shape == size def test_plantcv_transform_resize_pad_crop_color(): color_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY_SMALL)) size = (100, 100) resized_im = pcv.transform.resize(img=color_img, size=size, interpolation=None) assert resized_im.shape == (size[1], size[0], 3) def test_plantcv_transform_resize_factor(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_trancform_resize_factor") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir gray_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY_SMALL), -1) # Resizing factors factor_x = 0.5 factor_y = 0.2 # Test with debug "print" pcv.params.debug = "print" _ = pcv.transform.resize_factor(img=gray_img, factors=(factor_x, factor_y), interpolation="auto") # Test with debug "plot" pcv.params.debug = "plot" resized_img = pcv.transform.resize_factor(img=gray_img, factors=(factor_x, factor_y), interpolation="auto") output_size = resized_img.shape expected_size = (int(gray_img.shape[0] * factor_y), int(gray_img.shape[1] * factor_x)) assert output_size == expected_size def test_plantcv_transform_resize_factor_bad_input(): gray_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY_SMALL), -1) with pytest.raises(RuntimeError): _ = pcv.transform.resize_factor(img=gray_img, factors=(0, 2), interpolation="auto") def test_plantcv_transform_nonuniform_illumination_rgb(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_transform_nonuniform_illumination") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Load rgb image rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_TARGET_IMG)) pcv.params.debug = "plot" _ = pcv.transform.nonuniform_illumination(img=rgb_img, ksize=11) pcv.params.debug = "print" corrected = pcv.transform.nonuniform_illumination(img=rgb_img, ksize=11) assert np.mean(corrected) < np.mean(rgb_img) def test_plantcv_transform_nonuniform_illumination_gray(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_transform_nonuniform_illumination") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Load rgb image gray_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) pcv.params.debug = "plot" _ = pcv.transform.nonuniform_illumination(img=gray_img, ksize=11) pcv.params.debug = "print" corrected = pcv.transform.nonuniform_illumination(img=gray_img, ksize=11) assert np.shape(corrected) == np.shape(gray_img) def test_plantcv_transform_warp_default(): pcv.params.debug = "plot" img = create_test_img((12, 10, 3)) refimg = create_test_img((12, 10, 3)) pts = [(0, 0),(1, 0),(0, 3),(4, 4)] refpts = [(0, 0),(1, 0),(0, 3),(4, 4)] warped_img, mat = pcv.transform.warp(img, refimg, pts, refpts, method="default") assert mat.shape == (3, 3) def test_plantcv_transform_warp_lmeds(): pcv.params.debug = "plot" img = create_test_img((10, 10, 3)) refimg = create_test_img((11, 11)) pts = [(0, 0), (1, 0), (0, 3), (4, 4)] refpts = [(0, 0), (1, 0), (0, 3), (4, 4)] warped_img, mat = pcv.transform.warp(img, refimg, pts, refpts, method="lmeds") assert mat.shape == (3, 3) def test_plantcv_transform_warp_rho(): pcv.params.debug = "plot" img = create_test_img_bin((10, 10)) refimg = create_test_img((11, 11)) pts = [(0, 0), (1, 0), (0, 3), (4, 4)] refpts = [(0, 0), (1, 0), (0, 3), (4, 4)] warped_img, mat = pcv.transform.warp(img, refimg, pts, refpts, method="rho") assert mat.shape == (3, 3) def test_plantcv_transform_warp_ransac(): pcv.params.debug = "plot" img = create_test_img((100, 150)) refimg = create_test_img((10, 15)) pts = [(0, 0), (149, 0), (99, 149), (0, 99), (3, 3)] refpts = [(0, 0), (0, 14), (9, 14), (0, 9), (3, 3)] warped_img, mat = pcv.transform.warp(img, refimg, pts, refpts, method="ransac") assert mat.shape == (3, 3) @pytest.mark.parametrize("pts, refpts", [ [[(0,0)],[(0,0),(0,1)]], # different # of points provided for img and refimg [[(0,0)],[(0,0)]], # not enough pairs of points provided [[(0, 0), (0, 14), (9, 14), (0, 9), (3, 3)], [(0, 0), (149, 0), (99, 149), (0, 99), (3, 3)]] # homography not able to be calculated (cannot converge) ]) def test_plantcv_transform_warp_err(pts, refpts): img = create_test_img((10, 15)) refimg = create_test_img((100, 150)) method = "rho" with pytest.raises(RuntimeError): pcv.transform.warp(img, refimg, pts, refpts, method=method) def test_plantcv_transform_warp_align(): img = create_test_img((10, 10, 3)) refimg = create_test_img((11, 11)) mat = np.array([[ 1.00000000e+00, 1.04238500e-15, -7.69185075e-16], [ 1.44375646e-16, 1.00000000e+00, 0.00000000e+00], [-5.41315251e-16, 1.78930521e-15, 1.00000000e+00]]) warp_img = pcv.transform.warp_align(img=img, mat=mat, refimg=refimg) assert warp_img.shape == (11, 11, 3) # ############################## # Tests for the threshold subpackage # ############################## @pytest.mark.parametrize("objtype", ["dark", "light"]) def test_plantcv_threshold_binary(objtype): # Read in test data gray_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) # Test with object type = dark pcv.params.debug = None binary_img = pcv.threshold.binary(gray_img=gray_img, threshold=25, max_value=255, object_type=objtype) # Assert that the output image has the dimensions of the input image if all([i == j] for i, j in zip(np.shape(binary_img), TEST_GRAY_DIM)): # Assert that the image is binary if all([i == j] for i, j in zip(np.unique(binary_img), [0, 255])): assert 1 else: assert 0 else: assert 0 def test_plantcv_threshold_binary_incorrect_object_type(): gray_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) with pytest.raises(RuntimeError): pcv.params.debug = None _ = pcv.threshold.binary(gray_img=gray_img, threshold=25, max_value=255, object_type="lite") @pytest.mark.parametrize("objtype", ["dark", "light"]) def test_plantcv_threshold_gaussian(objtype): # Read in test data gray_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) # Test with object type = dark pcv.params.debug = None binary_img = pcv.threshold.gaussian(gray_img=gray_img, max_value=255, object_type=objtype) # Assert that the output image has the dimensions of the input image if all([i == j] for i, j in zip(np.shape(binary_img), TEST_GRAY_DIM)): # Assert that the image is binary if all([i == j] for i, j in zip(np.unique(binary_img), [0, 255])): assert 1 else: assert 0 else: assert 0 def test_plantcv_threshold_gaussian_incorrect_object_type(): gray_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) with pytest.raises(RuntimeError): pcv.params.debug = None _ = pcv.threshold.gaussian(gray_img=gray_img, max_value=255, object_type="lite") @pytest.mark.parametrize("objtype", ["dark", "light"]) def test_plantcv_threshold_mean(objtype): # Read in test data gray_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) # Test with object type = dark pcv.params.debug = None binary_img = pcv.threshold.mean(gray_img=gray_img, max_value=255, object_type=objtype) # Assert that the output image has the dimensions of the input image if all([i == j] for i, j in zip(np.shape(binary_img), TEST_GRAY_DIM)): # Assert that the image is binary if all([i == j] for i, j in zip(np.unique(binary_img), [0, 255])): assert 1 else: assert 0 else: assert 0 def test_plantcv_threshold_mean_incorrect_object_type(): gray_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) with pytest.raises(RuntimeError): pcv.params.debug = None _ = pcv.threshold.mean(gray_img=gray_img, max_value=255, object_type="lite") @pytest.mark.parametrize("objtype", ["dark", "light"]) def test_plantcv_threshold_otsu(objtype): # Read in test data gray_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GREENMAG), -1) # Test with object set to light pcv.params.debug = None binary_img = pcv.threshold.otsu(gray_img=gray_img, max_value=255, object_type=objtype) # Assert that the output image has the dimensions of the input image if all([i == j] for i, j in zip(np.shape(binary_img), TEST_GRAY_DIM)): # Assert that the image is binary if all([i == j] for i, j in zip(np.unique(binary_img), [0, 255])): assert 1 else: assert 0 else: assert 0 def test_plantcv_threshold_otsu_incorrect_object_type(): gray_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) with pytest.raises(RuntimeError): pcv.params.debug = None _ = pcv.threshold.otsu(gray_img=gray_img, max_value=255, object_type="lite") @pytest.mark.parametrize("channel,lower_thresh,upper_thresh", [["HSV", [0, 0, 0], [255, 255, 255]], ["LAB", [0, 0, 0], [255, 255, 255]], ["RGB", [0, 0, 0], [255, 255, 255]], ["GRAY", [0], [255]]]) def test_plantcv_threshold_custom_range_rgb(channel, lower_thresh, upper_thresh): # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) # Test with debug = None pcv.params.debug = None mask, binary_img = pcv.threshold.custom_range(img, lower_thresh=lower_thresh, upper_thresh=upper_thresh, channel=channel) # Assert that the output image has the dimensions of the input image if all([i == j] for i, j in zip(np.shape(binary_img), TEST_GRAY_DIM)): # Assert that the image is binary if all([i == j] for i, j in zip(np.unique(binary_img), [0, 255])): assert 1 else: assert 0 else: assert 0 def test_plantcv_threshold_custom_range_grayscale(): # Read in test data gray_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) # Test with debug = None pcv.params.debug = None # # Test channel='gray' mask, binary_img = pcv.threshold.custom_range(gray_img, lower_thresh=[0], upper_thresh=[255], channel='gray') # Assert that the output image has the dimensions of the input image if all([i == j] for i, j in zip(np.shape(binary_img), TEST_GRAY_DIM)): # Assert that the image is binary if all([i == j] for i, j in zip(np.unique(binary_img), [0, 255])): assert 1 else: assert 0 else: assert 0 def test_plantcv_threshold_custom_range_bad_input_hsv(): # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) with pytest.raises(RuntimeError): _, _ = pcv.threshold.custom_range(img, lower_thresh=[0, 0], upper_thresh=[2, 2, 2, 2], channel='HSV') def test_plantcv_threshold_custom_range_bad_input_rgb(): # Read in test data pcv.params.debug = None img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) with pytest.raises(RuntimeError): _, _ = pcv.threshold.custom_range(img, lower_thresh=[0, 0], upper_thresh=[2, 2, 2, 2], channel='RGB') def test_plantcv_threshold_custom_range_bad_input_lab(): # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) with pytest.raises(RuntimeError): _, _ = pcv.threshold.custom_range(img, lower_thresh=[0, 0], upper_thresh=[2, 2, 2], channel='LAB') def test_plantcv_threshold_custom_range_bad_input_gray(): # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) with pytest.raises(RuntimeError): _, _ = pcv.threshold.custom_range(img, lower_thresh=[0, 0], upper_thresh=[2], channel='gray') def test_plantcv_threshold_custom_range_bad_input_channel(): # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) with pytest.raises(RuntimeError): _, _ = pcv.threshold.custom_range(img, lower_thresh=[0], upper_thresh=[2], channel='CMYK') @pytest.mark.parametrize("channel", ["all", "any"]) def test_plantcv_threshold_saturation(channel): # Read in test data rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) # Test with debug = None pcv.params.debug = None thresh = pcv.threshold.saturation(rgb_img=rgb_img, threshold=254, channel=channel) assert len(np.unique(thresh)) == 2 def test_plantcv_threshold_saturation_bad_input(): # Read in test data rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) with pytest.raises(RuntimeError): _ = pcv.threshold.saturation(rgb_img=rgb_img, threshold=254, channel="red") def test_plantcv_threshold_triangle(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_threshold_triangle") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data gray_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) pcv.params.debug = None _ = pcv.threshold.triangle(gray_img=gray_img, max_value=255, object_type="dark", xstep=10) pcv.params.debug = "plot" _ = pcv.threshold.triangle(gray_img=gray_img, max_value=255, object_type="light", xstep=10) pcv.params.debug = "print" binary_img = pcv.threshold.triangle(gray_img=gray_img, max_value=255, object_type="light", xstep=10) # Assert that the output image has the dimensions of the input image if all([i == j] for i, j in zip(np.shape(binary_img), TEST_GRAY_DIM)): # Assert that the image is binary if all([i == j] for i, j in zip(np.unique(binary_img), [0, 255])): assert 1 else: assert 0 else: assert 0 def test_plantcv_threshold_triangle_incorrect_object_type(): gray_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) with pytest.raises(RuntimeError): pcv.params.debug = None _ = pcv.threshold.triangle(gray_img=gray_img, max_value=255, object_type="lite", xstep=10) def test_plantcv_threshold_texture(): # Test with debug = None pcv.params.debug = None gray_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY_SMALL), -1) binary_img = pcv.threshold.texture(gray_img, ksize=6, threshold=7, offset=3, texture_method='dissimilarity', borders='nearest', max_value=255) # Assert that the output image has the dimensions of the input image if all([i == j] for i, j in zip(np.shape(binary_img), TEST_GRAY_DIM)): # Assert that the image is binary if all([i == j] for i, j in zip(np.unique(binary_img), [0, 255])): assert 1 else: assert 0 else: assert 0 def create_test_img(sz_img): img = np.random.randint(np.prod(sz_img), size=sz_img) * 255 img = img.astype(np.uint8) return img def create_test_img_bin(sz_img): img = np.zeros(sz_img) img[3:7, 2:8] = 1 return img @pytest.mark.parametrize("bad_type", ["native", "nan", "inf"]) def test_plantcv_threshold_mask_bad(bad_type): # Create a synthetic bad image bad_img = np.reshape(np.random.rand(25), (5, 5)) bad_img[2, 2] = np.inf bad_img[2, 3] = np.nan sz = np.shape(bad_img) pcv.params.debug = None mask = pcv.threshold.mask_bad(bad_img, bad_type=bad_type) assert((np.shape(mask) == sz) and (len(np.unique(mask)) == 2)) def test_plantcv_threshold_mask_bad_native_bad_input(): # Create a synthetic bad image bad_img = np.reshape(np.random.rand(25), (5, 5)) sz = np.shape(bad_img) mask10 = pcv.threshold.mask_bad(bad_img, bad_type='native') assert mask10.all() == np.zeros(sz, dtype='uint8').all() def test_plantcv_threshold_mask_bad_nan_bad_input(): # Create a synthetic bad image bad_img = np.reshape(np.random.rand(25), (5, 5)) bad_img[2, 2] = np.inf sz = np.shape(bad_img) mask11 = pcv.threshold.mask_bad(bad_img, bad_type='nan') assert mask11.all() == np.zeros(sz, dtype='uint8').all() def test_plantcv_threshold_mask_bad_input_color_img(): # Read in test data bad_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) with pytest.raises(RuntimeError): pcv.threshold.mask_bad(bad_img, bad_type='nan') # ################################### # Tests for the visualize subpackage # ################################### def test_plantcv_visualize_auto_threshold_methods_bad_input(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_auto_threshold_methods") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) with pytest.raises(RuntimeError): _ = pcv.visualize.auto_threshold_methods(gray_img=img) def test_plantcv_visualize_auto_threshold_methods(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_auto_threshold_methods") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) pcv.params.debug = "print" _ = pcv.visualize.auto_threshold_methods(gray_img=img) pcv.params.debug = "plot" labeled_imgs = pcv.visualize.auto_threshold_methods(gray_img=img) assert len(labeled_imgs) == 5 and np.shape(labeled_imgs[0])[0] == np.shape(img)[0] @pytest.mark.parametrize("debug,axes", [["print", True], ["plot", False]]) def test_plantcv_visualize_pseudocolor(debug, axes, tmpdir): # Create a tmp directory cache_dir = tmpdir.mkdir("sub") pcv.params.debug_outdir = cache_dir # Input image img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) r, c = img.shape # generate 200 "bad" pixels mask_bad = np.zeros((r, c), dtype=np.uint8) mask_bad = np.reshape(mask_bad, (-1, 1)) mask_bad[0:100] = 255 mask_bad = np.reshape(mask_bad, (r, c)) # Debug mode pcv.params.debug = debug pseudo_img = pcv.visualize.pseudocolor(gray_img=img, mask=None, title="Pseudocolored image", axes=axes, bad_mask=mask_bad) # Assert that the output image has the dimensions of the input image assert all([i == j] for i, j in zip(np.shape(pseudo_img), TEST_BINARY_DIM)) @pytest.mark.parametrize("bkgrd,axes,pad", [["image", True, "auto"], ["white", False, 1], ["black", True, "auto"]]) def test_plantcv_visualize_pseudocolor_mask(bkgrd, axes, pad): # Test with debug = None pcv.params.debug = None # Input image img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) # Input mask mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) # Input contours contours_npz = np.load(os.path.join(TEST_DATA, TEST_INPUT_CONTOURS), encoding="latin1") obj_contour = contours_npz['arr_0'] r, c = img.shape # generate 200 "bad" pixels mask_bad = np.zeros((r, c), dtype=np.uint8) mask_bad = np.reshape(mask_bad, (-1, 1)) mask_bad[0:100] = 255 mask_bad = np.reshape(mask_bad, (r, c)) pseudo_img = pcv.visualize.pseudocolor(gray_img=img, obj=obj_contour, mask=mask, background=bkgrd, bad_mask=mask_bad, title="Pseudocolored image", axes=axes, obj_padding=pad) # Assert that the output image has the dimensions of the input image if all([i == j] for i, j in zip(np.shape(pseudo_img), TEST_BINARY_DIM)): assert 1 else: assert 0 def test_plantcv_visualize_pseudocolor_bad_input(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_pseudocolor") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) with pytest.raises(RuntimeError): _ = pcv.visualize.pseudocolor(gray_img=img) def test_plantcv_visualize_pseudocolor_bad_background(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_pseudocolor_bad_background") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) with pytest.raises(RuntimeError): _ = pcv.visualize.pseudocolor(gray_img=img, mask=mask, background="pink") def test_plantcv_visualize_pseudocolor_bad_padding(): cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_pseudocolor_bad_background") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) contours_npz = np.load(os.path.join(TEST_DATA, TEST_INPUT_CONTOURS), encoding="latin1") obj_contour = contours_npz['arr_0'] with pytest.raises(RuntimeError): _ = pcv.visualize.pseudocolor(gray_img=img, mask=mask, obj=obj_contour, obj_padding="pink") def test_plantcv_visualize_pseudocolor_bad_mask(): # Test with debug = None pcv.params.debug = None def test_plantcv_visualize_colorize_masks(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_naive_bayes_classifier") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) # Test with debug = "print" pcv.params.debug = "print" mask = pcv.naive_bayes_classifier(rgb_img=img, pdf_file=os.path.join(TEST_DATA, TEST_PDFS)) _ = pcv.visualize.colorize_masks(masks=[mask['plant'], mask['background']], colors=[(0, 0, 0), (1, 1, 1)]) # Test with debug = "plot" pcv.params.debug = "plot" _ = pcv.visualize.colorize_masks(masks=[mask['plant'], mask['background']], colors=[(0, 0, 0), (1, 1, 1)]) # Test with debug = None pcv.params.debug = None colored_img = pcv.visualize.colorize_masks(masks=[mask['plant'], mask['background']], colors=['red', 'blue']) # Assert that the output image has the dimensions of the input image assert not np.average(colored_img) == 0 def test_plantcv_visualize_colorize_masks_bad_input_empty(): with pytest.raises(RuntimeError): _ = pcv.visualize.colorize_masks(masks=[], colors=[]) def test_plantcv_visualize_colorize_masks_bad_input_mismatch_number(): # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) # Test with debug = "print" pcv.params.debug = "print" mask = pcv.naive_bayes_classifier(rgb_img=img, pdf_file=os.path.join(TEST_DATA, TEST_PDFS)) with pytest.raises(RuntimeError): _ = pcv.visualize.colorize_masks(masks=[mask['plant'], mask['background']], colors=['red', 'green', 'blue']) def test_plantcv_visualize_colorize_masks_bad_color_input(): # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) # Test with debug = "print" pcv.params.debug = "print" mask = pcv.naive_bayes_classifier(rgb_img=img, pdf_file=os.path.join(TEST_DATA, TEST_PDFS)) with pytest.raises(RuntimeError): _ = pcv.visualize.colorize_masks(masks=[mask['plant'], mask['background']], colors=['red', 1.123]) def test_plantcv_visualize_colorize_label_img(): label_img = np.array([[1,2,3],[4,5,6],[7,8,9]]) pcv.params.debug = None colored_img = pcv.visualize.colorize_label_img(label_img) assert (colored_img.shape[0:-1] == label_img.shape) and colored_img.shape[-1] == 3 @pytest.mark.parametrize("bins,lb,ub,title", [[200, 0, 255, "Include Title"], [100, None, None, None]]) def test_plantcv_visualize_histogram(bins, lb, ub, title): # Test with debug = None pcv.params.debug = None # Read test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) fig_hist, hist_df = pcv.visualize.histogram(img=img, mask=mask, bins=bins, lower_bound=lb, upper_bound=ub, title=title, hist_data=True) assert all([isinstance(fig_hist, ggplot), isinstance(hist_df, pd.core.frame.DataFrame)]) def test_plantcv_visualize_histogram_no_mask(): # Test with debug = None pcv.params.debug = None # Read test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) fig_hist = pcv.visualize.histogram(img=img, mask=None) assert isinstance(fig_hist, ggplot) def test_plantcv_visualize_histogram_rgb_img(): # Test with debug = None pcv.params.debug = None # Test RGB input image img_rgb = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) fig_hist = pcv.visualize.histogram(img=img_rgb) assert isinstance(fig_hist, ggplot) def test_plantcv_visualize_histogram_multispectral_img(): # Test with debug = None pcv.params.debug = None # Test multi-spectral image img_rgb = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) img_multi = np.concatenate((img_rgb, img_rgb), axis=2) fig_hist = pcv.visualize.histogram(img=img_multi) assert isinstance(fig_hist, ggplot) def test_plantcv_visualize_histogram_no_img(): with pytest.raises(RuntimeError): _ = pcv.visualize.histogram(img=None) def test_plantcv_visualize_histogram_array(): # Read test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) with pytest.raises(RuntimeError): _ = pcv.visualize.histogram(img=img[0, :]) def test_plantcv_visualize_clustered_contours(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_plot_hist") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) img1 = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_VISUALIZE_BACKGROUND), -1) roi_objects = np.load(os.path.join(TEST_DATA, TEST_INPUT_VISUALIZE_CONTOUR), encoding="latin1") hierarchy = np.load(os.path.join(TEST_DATA, TEST_INPUT_VISUALIZE_HIERARCHY), encoding="latin1") cluster_i = np.load(os.path.join(TEST_DATA, TEST_INPUT_VISUALIZE_CLUSTERS), encoding="latin1") objs = [roi_objects[arr_n] for arr_n in roi_objects] obj_hierarchy = hierarchy['arr_0'] cluster = [cluster_i[arr_n] for arr_n in cluster_i] # Test in plot mode pcv.params.debug = "plot" # Reset the saved color scale (can be saved between tests) pcv.params.saved_color_scale = None _ = pcv.visualize.clustered_contours(img=img1, grouped_contour_indices=cluster, roi_objects=objs, roi_obj_hierarchy=obj_hierarchy, bounding=False) # Test in print mode pcv.params.debug = "print" # Reset the saved color scale (can be saved between tests) pcv.params.saved_color_scale = None cluster_img = pcv.visualize.clustered_contours(img=img, grouped_contour_indices=cluster, roi_objects=objs, roi_obj_hierarchy=obj_hierarchy, nrow=2, ncol=2, bounding=True) assert np.sum(cluster_img) > np.sum(img) def test_plantcv_visualize_colorspaces(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_plot_hist") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) pcv.params.debug = "plot" vis_img_small = pcv.visualize.colorspaces(rgb_img=img, original_img=False) pcv.params.debug = "print" vis_img = pcv.visualize.colorspaces(rgb_img=img) assert np.shape(vis_img)[1] > (np.shape(img)[1]) and np.shape(vis_img_small)[1] > (np.shape(img)[1]) def test_plantcv_visualize_colorspaces_bad_input(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_plot_hist") os.mkdir(cache_dir) pcv.params.debug_outdir = cache_dir # Read in test data img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) with pytest.raises(RuntimeError): _ = pcv.visualize.colorspaces(rgb_img=img) def test_plantcv_visualize_overlay_two_imgs(): pcv.params.debug = None cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_visualize_overlay_two_imgs") os.mkdir(cache_dir) img1 = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) img2 = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY)) pcv.params.debug = None out_img = pcv.visualize.overlay_two_imgs(img1=img1, img2=img2) sample_pt1 = img1[1445, 1154] sample_pt2 = img2[1445, 1154] sample_pt3 = out_img[1445, 1154] pred_rgb = (sample_pt1 * 0.5) + (sample_pt2 * 0.5) pred_rgb = pred_rgb.astype(np.uint8) assert np.array_equal(sample_pt3, pred_rgb) def test_plantcv_visualize_overlay_two_imgs_grayscale(): pcv.params.debug = None cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_visualize_overlay_two_imgs_grayscale") os.mkdir(cache_dir) img1 = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) img2 = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) out_img = pcv.visualize.overlay_two_imgs(img1=img1, img2=img2) sample_pt1 = np.array([255, 255, 255], dtype=np.uint8) sample_pt2 = np.array([255, 255, 255], dtype=np.uint8) sample_pt3 = out_img[1445, 1154] pred_rgb = (sample_pt1 * 0.5) + (sample_pt2 * 0.5) pred_rgb = pred_rgb.astype(np.uint8) assert np.array_equal(sample_pt3, pred_rgb) def test_plantcv_visualize_overlay_two_imgs_bad_alpha(): pcv.params.debug = None cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_visualize_overlay_two_imgs_bad_alpha") os.mkdir(cache_dir) img1 = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) img2 = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY)) alpha = -1 with pytest.raises(RuntimeError): _ = pcv.visualize.overlay_two_imgs(img1=img1, img2=img2, alpha=alpha) def test_plantcv_visualize_overlay_two_imgs_size_mismatch(): pcv.params.debug = None cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_visualize_overlay_two_imgs_size_mismatch") os.mkdir(cache_dir) img1 = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR)) img2 = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_CROPPED)) with pytest.raises(RuntimeError): _ = pcv.visualize.overlay_two_imgs(img1=img1, img2=img2) @pytest.mark.parametrize("title", ["Include Title", None]) def test_plantcv_visualize_obj_size_ecdf(title): pcv.params.debug = None mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MASK), -1) fig_ecdf = plantcv.plantcv.visualize.obj_size_ecdf(mask=mask, title=title) assert isinstance(fig_ecdf, ggplot) # ############################## # Tests for the utils subpackage # ############################## def test_plantcv_utils_json2csv(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_utils_json2csv") os.mkdir(cache_dir) plantcv.utils.json2csv(json_file=os.path.join(TEST_DATA, "merged_output.json"), csv_file=os.path.join(cache_dir, "exports")) assert all([os.path.exists(os.path.join(cache_dir, "exports-single-value-traits.csv")), os.path.exists(os.path.join(cache_dir, "exports-multi-value-traits.csv"))]) def test_plantcv_utils_json2csv_no_json(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_utils_json2csv_no_json") os.mkdir(cache_dir) with pytest.raises(IOError): plantcv.utils.json2csv(json_file=os.path.join(TEST_DATA, "not_a_file.json"), csv_file=os.path.join(cache_dir, "exports")) def test_plantcv_utils_json2csv_bad_json(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_utils_json2csv_bad_json") os.mkdir(cache_dir) with pytest.raises(ValueError): plantcv.utils.json2csv(json_file=os.path.join(TEST_DATA, "incorrect_json_data.txt"), csv_file=os.path.join(cache_dir, "exports")) def test_plantcv_utils_sample_images_snapshot(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_utils_sample_images") os.mkdir(cache_dir) snapshot_dir = os.path.join(PARALLEL_TEST_DATA, TEST_SNAPSHOT_DIR) img_outdir = os.path.join(cache_dir, "snapshot") plantcv.utils.sample_images(source_path=snapshot_dir, dest_path=img_outdir, num=3) assert os.path.exists(os.path.join(cache_dir, "snapshot")) def test_plantcv_utils_sample_images_flatdir(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_utils_sample_images") os.mkdir(cache_dir) flat_dir = os.path.join(TEST_DATA) img_outdir = os.path.join(cache_dir, "images") plantcv.utils.sample_images(source_path=flat_dir, dest_path=img_outdir, num=30) random_images = os.listdir(img_outdir) assert all([len(random_images) == 30, len(np.unique(random_images)) == 30]) def test_plantcv_utils_sample_images_bad_source(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_utils_sample_images") os.mkdir(cache_dir) fake_dir = os.path.join(TEST_DATA, "snapshot") img_outdir = os.path.join(cache_dir, "images") with pytest.raises(IOError): plantcv.utils.sample_images(source_path=fake_dir, dest_path=img_outdir, num=3) def test_plantcv_utils_sample_images_bad_flat_num(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_utils_sample_images") os.mkdir(cache_dir) flat_dir = os.path.join(TEST_DATA) img_outdir = os.path.join(cache_dir, "images") with pytest.raises(RuntimeError): plantcv.utils.sample_images(source_path=flat_dir, dest_path=img_outdir, num=300) def test_plantcv_utils_sample_images_bad_phenofront_num(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_utils_sample_images") os.mkdir(cache_dir) snapshot_dir = os.path.join(PARALLEL_TEST_DATA, TEST_SNAPSHOT_DIR) img_outdir = os.path.join(cache_dir, "images") with pytest.raises(RuntimeError): plantcv.utils.sample_images(source_path=snapshot_dir, dest_path=img_outdir, num=300) def test_plantcv_utils_tabulate_bayes_classes(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_utils_tabulate_bayes_classes") os.mkdir(cache_dir) outfile = os.path.join(cache_dir, "rgb_table.txt") plantcv.utils.tabulate_bayes_classes(input_file=os.path.join(TEST_DATA, PIXEL_VALUES), output_file=outfile) table = pd.read_csv(outfile, sep="\t") assert table.shape == (228, 2) def test_plantcv_utils_tabulate_bayes_classes_missing_input(): # Test cache directory cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_utils_tabulate_bayes_classes_missing_input") os.mkdir(cache_dir) outfile = os.path.join(cache_dir, "rgb_table.txt") with pytest.raises(IOError): plantcv.utils.tabulate_bayes_classes(input_file=os.path.join(PIXEL_VALUES), output_file=outfile) # ############################## # Clean up test files # ############################## def teardown_function(): shutil.rmtree(TEST_TMPDIR)
mit
awanke/bokeh
examples/plotting/file/boxplot.py
43
2269
import numpy as np import pandas as pd from bokeh.plotting import figure, show, output_file # Generate some synthetic time series for six different categories cats = list("abcdef") yy = np.random.randn(2000) g = np.random.choice(cats, 2000) for i, l in enumerate(cats): yy[g == l] += i // 2 df = pd.DataFrame(dict(score=yy, group=g)) # Find the quartiles and IQR foor each category groups = df.groupby('group') q1 = groups.quantile(q=0.25) q2 = groups.quantile(q=0.5) q3 = groups.quantile(q=0.75) iqr = q3 - q1 upper = q3 + 1.5*iqr lower = q1 - 1.5*iqr # find the outliers for each category def outliers(group): cat = group.name return group[(group.score > upper.loc[cat][0]) | (group.score < lower.loc[cat][0])]['score'] out = groups.apply(outliers).dropna() # Prepare outlier data for plotting, we need coordinate for every outlier. outx = [] outy = [] for cat in cats: # only add outliers if they exist if not out.loc[cat].empty: for value in out[cat]: outx.append(cat) outy.append(value) output_file("boxplot.html") p = figure(tools="save", background_fill="#EFE8E2", title="", x_range=cats) # If no outliers, shrink lengths of stems to be no longer than the minimums or maximums qmin = groups.quantile(q=0.00) qmax = groups.quantile(q=1.00) upper.score = [min([x,y]) for (x,y) in zip(list(qmax.iloc[:,0]),upper.score) ] lower.score = [max([x,y]) for (x,y) in zip(list(qmin.iloc[:,0]),lower.score) ] # stems p.segment(cats, upper.score, cats, q3.score, line_width=2, line_color="black") p.segment(cats, lower.score, cats, q1.score, line_width=2, line_color="black") # boxes p.rect(cats, (q3.score+q2.score)/2, 0.7, q3.score-q2.score, fill_color="#E08E79", line_width=2, line_color="black") p.rect(cats, (q2.score+q1.score)/2, 0.7, q2.score-q1.score, fill_color="#3B8686", line_width=2, line_color="black") # whiskers (almost-0 height rects simpler than segments) p.rect(cats, lower.score, 0.2, 0.01, line_color="black") p.rect(cats, upper.score, 0.2, 0.01, line_color="black") # outliers p.circle(outx, outy, size=6, color="#F38630", fill_alpha=0.6) p.xgrid.grid_line_color = None p.ygrid.grid_line_color = "white" p.grid.grid_line_width = 2 p.xaxis.major_label_text_font_size="12pt" show(p)
bsd-3-clause
lin-credible/scikit-learn
sklearn/svm/tests/test_sparse.py
95
12156
from nose.tools import assert_raises, assert_true, assert_false import numpy as np from scipy import sparse from numpy.testing import (assert_array_almost_equal, assert_array_equal, assert_equal) from sklearn import datasets, svm, linear_model, base from sklearn.datasets import make_classification, load_digits, make_blobs from sklearn.svm.tests import test_svm from sklearn.utils import ConvergenceWarning from sklearn.utils.extmath import safe_sparse_dot from sklearn.utils.testing import assert_warns, assert_raise_message # test sample 1 X = np.array([[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1]]) X_sp = sparse.lil_matrix(X) Y = [1, 1, 1, 2, 2, 2] T = np.array([[-1, -1], [2, 2], [3, 2]]) true_result = [1, 2, 2] # test sample 2 X2 = np.array([[0, 0, 0], [1, 1, 1], [2, 0, 0, ], [0, 0, 2], [3, 3, 3]]) X2_sp = sparse.dok_matrix(X2) Y2 = [1, 2, 2, 2, 3] T2 = np.array([[-1, -1, -1], [1, 1, 1], [2, 2, 2]]) true_result2 = [1, 2, 3] iris = datasets.load_iris() # permute rng = np.random.RandomState(0) perm = rng.permutation(iris.target.size) iris.data = iris.data[perm] iris.target = iris.target[perm] # sparsify iris.data = sparse.csr_matrix(iris.data) def check_svm_model_equal(dense_svm, sparse_svm, X_train, y_train, X_test): dense_svm.fit(X_train.toarray(), y_train) if sparse.isspmatrix(X_test): X_test_dense = X_test.toarray() else: X_test_dense = X_test sparse_svm.fit(X_train, y_train) assert_true(sparse.issparse(sparse_svm.support_vectors_)) assert_true(sparse.issparse(sparse_svm.dual_coef_)) assert_array_almost_equal(dense_svm.support_vectors_, sparse_svm.support_vectors_.toarray()) assert_array_almost_equal(dense_svm.dual_coef_, sparse_svm.dual_coef_.toarray()) if dense_svm.kernel == "linear": assert_true(sparse.issparse(sparse_svm.coef_)) assert_array_almost_equal(dense_svm.coef_, sparse_svm.coef_.toarray()) assert_array_almost_equal(dense_svm.support_, sparse_svm.support_) assert_array_almost_equal(dense_svm.predict(X_test_dense), sparse_svm.predict(X_test)) assert_array_almost_equal(dense_svm.decision_function(X_test_dense), sparse_svm.decision_function(X_test)) assert_array_almost_equal(dense_svm.decision_function(X_test_dense), sparse_svm.decision_function(X_test_dense)) assert_array_almost_equal(dense_svm.predict_proba(X_test_dense), sparse_svm.predict_proba(X_test), 4) msg = "cannot use sparse input in 'SVC' trained on dense data" if sparse.isspmatrix(X_test): assert_raise_message(ValueError, msg, dense_svm.predict, X_test) def test_svc(): """Check that sparse SVC gives the same result as SVC""" # many class dataset: X_blobs, y_blobs = make_blobs(n_samples=100, centers=10, random_state=0) X_blobs = sparse.csr_matrix(X_blobs) datasets = [[X_sp, Y, T], [X2_sp, Y2, T2], [X_blobs[:80], y_blobs[:80], X_blobs[80:]], [iris.data, iris.target, iris.data]] kernels = ["linear", "poly", "rbf", "sigmoid"] for dataset in datasets: for kernel in kernels: clf = svm.SVC(kernel=kernel, probability=True, random_state=0) sp_clf = svm.SVC(kernel=kernel, probability=True, random_state=0) check_svm_model_equal(clf, sp_clf, *dataset) def test_unsorted_indices(): # test that the result with sorted and unsorted indices in csr is the same # we use a subset of digits as iris, blobs or make_classification didn't # show the problem digits = load_digits() X, y = digits.data[:50], digits.target[:50] X_test = sparse.csr_matrix(digits.data[50:100]) X_sparse = sparse.csr_matrix(X) coef_dense = svm.SVC(kernel='linear', probability=True, random_state=0).fit(X, y).coef_ sparse_svc = svm.SVC(kernel='linear', probability=True, random_state=0).fit(X_sparse, y) coef_sorted = sparse_svc.coef_ # make sure dense and sparse SVM give the same result assert_array_almost_equal(coef_dense, coef_sorted.toarray()) X_sparse_unsorted = X_sparse[np.arange(X.shape[0])] X_test_unsorted = X_test[np.arange(X_test.shape[0])] # make sure we scramble the indices assert_false(X_sparse_unsorted.has_sorted_indices) assert_false(X_test_unsorted.has_sorted_indices) unsorted_svc = svm.SVC(kernel='linear', probability=True, random_state=0).fit(X_sparse_unsorted, y) coef_unsorted = unsorted_svc.coef_ # make sure unsorted indices give same result assert_array_almost_equal(coef_unsorted.toarray(), coef_sorted.toarray()) assert_array_almost_equal(sparse_svc.predict_proba(X_test_unsorted), sparse_svc.predict_proba(X_test)) def test_svc_with_custom_kernel(): kfunc = lambda x, y: safe_sparse_dot(x, y.T) clf_lin = svm.SVC(kernel='linear').fit(X_sp, Y) clf_mylin = svm.SVC(kernel=kfunc).fit(X_sp, Y) assert_array_equal(clf_lin.predict(X_sp), clf_mylin.predict(X_sp)) def test_svc_iris(): # Test the sparse SVC with the iris dataset for k in ('linear', 'poly', 'rbf'): sp_clf = svm.SVC(kernel=k).fit(iris.data, iris.target) clf = svm.SVC(kernel=k).fit(iris.data.toarray(), iris.target) assert_array_almost_equal(clf.support_vectors_, sp_clf.support_vectors_.toarray()) assert_array_almost_equal(clf.dual_coef_, sp_clf.dual_coef_.toarray()) assert_array_almost_equal( clf.predict(iris.data.toarray()), sp_clf.predict(iris.data)) if k == 'linear': assert_array_almost_equal(clf.coef_, sp_clf.coef_.toarray()) def test_sparse_decision_function(): #Test decision_function #Sanity check, test that decision_function implemented in python #returns the same as the one in libsvm # multi class: clf = svm.SVC(kernel='linear', C=0.1).fit(iris.data, iris.target) dec = safe_sparse_dot(iris.data, clf.coef_.T) + clf.intercept_ assert_array_almost_equal(dec, clf.decision_function(iris.data)) # binary: clf.fit(X, Y) dec = np.dot(X, clf.coef_.T) + clf.intercept_ prediction = clf.predict(X) assert_array_almost_equal(dec.ravel(), clf.decision_function(X)) assert_array_almost_equal( prediction, clf.classes_[(clf.decision_function(X) > 0).astype(np.int).ravel()]) expected = np.array([-1., -0.66, -1., 0.66, 1., 1.]) assert_array_almost_equal(clf.decision_function(X), expected, 2) def test_error(): # Test that it gives proper exception on deficient input # impossible value of C assert_raises(ValueError, svm.SVC(C=-1).fit, X, Y) # impossible value of nu clf = svm.NuSVC(nu=0.0) assert_raises(ValueError, clf.fit, X_sp, Y) Y2 = Y[:-1] # wrong dimensions for labels assert_raises(ValueError, clf.fit, X_sp, Y2) clf = svm.SVC() clf.fit(X_sp, Y) assert_array_equal(clf.predict(T), true_result) def test_linearsvc(): # Similar to test_SVC clf = svm.LinearSVC(random_state=0).fit(X, Y) sp_clf = svm.LinearSVC(random_state=0).fit(X_sp, Y) assert_true(sp_clf.fit_intercept) assert_array_almost_equal(clf.coef_, sp_clf.coef_, decimal=4) assert_array_almost_equal(clf.intercept_, sp_clf.intercept_, decimal=4) assert_array_almost_equal(clf.predict(X), sp_clf.predict(X_sp)) clf.fit(X2, Y2) sp_clf.fit(X2_sp, Y2) assert_array_almost_equal(clf.coef_, sp_clf.coef_, decimal=4) assert_array_almost_equal(clf.intercept_, sp_clf.intercept_, decimal=4) def test_linearsvc_iris(): # Test the sparse LinearSVC with the iris dataset sp_clf = svm.LinearSVC(random_state=0).fit(iris.data, iris.target) clf = svm.LinearSVC(random_state=0).fit(iris.data.toarray(), iris.target) assert_equal(clf.fit_intercept, sp_clf.fit_intercept) assert_array_almost_equal(clf.coef_, sp_clf.coef_, decimal=1) assert_array_almost_equal(clf.intercept_, sp_clf.intercept_, decimal=1) assert_array_almost_equal( clf.predict(iris.data.toarray()), sp_clf.predict(iris.data)) # check decision_function pred = np.argmax(sp_clf.decision_function(iris.data), 1) assert_array_almost_equal(pred, clf.predict(iris.data.toarray())) # sparsify the coefficients on both models and check that they still # produce the same results clf.sparsify() assert_array_equal(pred, clf.predict(iris.data)) sp_clf.sparsify() assert_array_equal(pred, sp_clf.predict(iris.data)) def test_weight(): # Test class weights X_, y_ = make_classification(n_samples=200, n_features=100, weights=[0.833, 0.167], random_state=0) X_ = sparse.csr_matrix(X_) for clf in (linear_model.LogisticRegression(), svm.LinearSVC(random_state=0), svm.SVC()): clf.set_params(class_weight={0: 5}) clf.fit(X_[:180], y_[:180]) y_pred = clf.predict(X_[180:]) assert_true(np.sum(y_pred == y_[180:]) >= 11) def test_sample_weights(): # Test weights on individual samples clf = svm.SVC() clf.fit(X_sp, Y) assert_array_equal(clf.predict(X[2]), [1.]) sample_weight = [.1] * 3 + [10] * 3 clf.fit(X_sp, Y, sample_weight=sample_weight) assert_array_equal(clf.predict(X[2]), [2.]) def test_sparse_liblinear_intercept_handling(): # Test that sparse liblinear honours intercept_scaling param test_svm.test_dense_liblinear_intercept_handling(svm.LinearSVC) def test_sparse_realdata(): # Test on a subset from the 20newsgroups dataset. # This catchs some bugs if input is not correctly converted into # sparse format or weights are not correctly initialized. data = np.array([0.03771744, 0.1003567, 0.01174647, 0.027069]) indices = np.array([6, 5, 35, 31]) indptr = np.array( [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4]) X = sparse.csr_matrix((data, indices, indptr)) y = np.array( [1., 0., 2., 2., 1., 1., 1., 2., 2., 0., 1., 2., 2., 0., 2., 0., 3., 0., 3., 0., 1., 1., 3., 2., 3., 2., 0., 3., 1., 0., 2., 1., 2., 0., 1., 0., 2., 3., 1., 3., 0., 1., 0., 0., 2., 0., 1., 2., 2., 2., 3., 2., 0., 3., 2., 1., 2., 3., 2., 2., 0., 1., 0., 1., 2., 3., 0., 0., 2., 2., 1., 3., 1., 1., 0., 1., 2., 1., 1., 3.]) clf = svm.SVC(kernel='linear').fit(X.toarray(), y) sp_clf = svm.SVC(kernel='linear').fit(sparse.coo_matrix(X), y) assert_array_equal(clf.support_vectors_, sp_clf.support_vectors_.toarray()) assert_array_equal(clf.dual_coef_, sp_clf.dual_coef_.toarray()) def test_sparse_svc_clone_with_callable_kernel(): # Test that the "dense_fit" is called even though we use sparse input # meaning that everything works fine. a = svm.SVC(C=1, kernel=lambda x, y: x * y.T, probability=True, random_state=0) b = base.clone(a) b.fit(X_sp, Y) pred = b.predict(X_sp) b.predict_proba(X_sp) dense_svm = svm.SVC(C=1, kernel=lambda x, y: np.dot(x, y.T), probability=True, random_state=0) pred_dense = dense_svm.fit(X, Y).predict(X) assert_array_equal(pred_dense, pred) # b.decision_function(X_sp) # XXX : should be supported def test_timeout(): sp = svm.SVC(C=1, kernel=lambda x, y: x * y.T, probability=True, random_state=0, max_iter=1) assert_warns(ConvergenceWarning, sp.fit, X_sp, Y) def test_consistent_proba(): a = svm.SVC(probability=True, max_iter=1, random_state=0) proba_1 = a.fit(X, Y).predict_proba(X) a = svm.SVC(probability=True, max_iter=1, random_state=0) proba_2 = a.fit(X, Y).predict_proba(X) assert_array_almost_equal(proba_1, proba_2)
bsd-3-clause
lhirschfeld/JargonBot
custombot.py
1
3061
import pickle import praw import random from textblob import TextBlob from datetime import datetime from sklearn import linear_model class RedditBot: """A class that performs basic operations, working with Reddit's PRAW API.""" def __init__(self, botName): # Setup the bot and primary variables. self.r = praw.Reddit(botName) self.responses = [] with open('ids.pickle', 'rb') as handle: try: self.ids = pickle.load(handle) except EOFError: self.ids = [] with open('models.pickle', 'rb') as handle: try: self.models = pickle.load(handle) except EOFError: self.models = {} def updateIds(self): # Save the new ids of comments that have been responded to. with open('ids.pickle', 'wb') as handle: pickle.dump(self.ids, handle, protocol=pickle.HIGHEST_PROTOCOL) def createModel(self, sub, init_fit): new_model = linear_model.LinearRegression() new_model.fit(init_fit[0], init_fit[1]) # TODO: Create sub class that stores this data. self.models[sub] = (new_model, 1, init_fit[0], init_fit[1]) with open('models.pickle', 'wb') as handle: pickle.dump(self.models, handle, protocol=pickle.HIGHEST_PROTOCOL) def updateModels(self, modelParams): # Model params is a list of strings which contains the keys in # each result which should be used to update the model. # Models is a dictionary with a touple at each key containing: # (linear regression, randomness rate, x fits, y fits) currentTime = datetime.now() oldResponses = [(currentTime - r["time"]).total_seconds() > 3600 for r in self.responses] self.responses = [(currentTime - r["time"]).total_seconds() < 3600 for r in self.responses] for r in oldResponses: result = 0 url = "https://reddit.com/" + r["sID"] + "?comment=" + r["cID"] submission = self.r.get_submission(url=url) comment_queue = submission.comments[:] if comment_queue: com = comment_queue.pop(0) result += com.score comment_queue.extend(com.replies) while comment_queue: com = comment_queue.pop(0) text = TextBlob(com.text) result += text.sentiment.polarity * com.score x = [] for key in modelParams: x.append(r[key]) # Get old fits x_fits = self.models[r["sub"]][2].append(x) y_fits = self.models[r["sub"]][3].append(result) self.models[r["sub"]][0].fit(x_fits, y_fits) # Update odds of random choice self.models[r]["sub"][1] *= 0.96 with open('models.pickle', 'wb') as handle: pickle.dump(self.models, handle, protocol=pickle.HIGHEST_PROTOCOL)
mit
RPGOne/scikit-learn
examples/decomposition/plot_pca_vs_fa_model_selection.py
70
4523
""" =============================================================== Model selection with Probabilistic PCA and Factor Analysis (FA) =============================================================== Probabilistic PCA and Factor Analysis are probabilistic models. The consequence is that the likelihood of new data can be used for model selection and covariance estimation. Here we compare PCA and FA with cross-validation on low rank data corrupted with homoscedastic noise (noise variance is the same for each feature) or heteroscedastic noise (noise variance is the different for each feature). In a second step we compare the model likelihood to the likelihoods obtained from shrinkage covariance estimators. One can observe that with homoscedastic noise both FA and PCA succeed in recovering the size of the low rank subspace. The likelihood with PCA is higher than FA in this case. However PCA fails and overestimates the rank when heteroscedastic noise is present. Under appropriate circumstances the low rank models are more likely than shrinkage models. The automatic estimation from Automatic Choice of Dimensionality for PCA. NIPS 2000: 598-604 by Thomas P. Minka is also compared. """ # Authors: Alexandre Gramfort # Denis A. Engemann # License: BSD 3 clause import numpy as np import matplotlib.pyplot as plt from scipy import linalg from sklearn.decomposition import PCA, FactorAnalysis from sklearn.covariance import ShrunkCovariance, LedoitWolf from sklearn.model_selection import cross_val_score from sklearn.model_selection import GridSearchCV print(__doc__) ############################################################################### # Create the data n_samples, n_features, rank = 1000, 50, 10 sigma = 1. rng = np.random.RandomState(42) U, _, _ = linalg.svd(rng.randn(n_features, n_features)) X = np.dot(rng.randn(n_samples, rank), U[:, :rank].T) # Adding homoscedastic noise X_homo = X + sigma * rng.randn(n_samples, n_features) # Adding heteroscedastic noise sigmas = sigma * rng.rand(n_features) + sigma / 2. X_hetero = X + rng.randn(n_samples, n_features) * sigmas ############################################################################### # Fit the models n_components = np.arange(0, n_features, 5) # options for n_components def compute_scores(X): pca = PCA(svd_solver='full') fa = FactorAnalysis() pca_scores, fa_scores = [], [] for n in n_components: pca.n_components = n fa.n_components = n pca_scores.append(np.mean(cross_val_score(pca, X))) fa_scores.append(np.mean(cross_val_score(fa, X))) return pca_scores, fa_scores def shrunk_cov_score(X): shrinkages = np.logspace(-2, 0, 30) cv = GridSearchCV(ShrunkCovariance(), {'shrinkage': shrinkages}) return np.mean(cross_val_score(cv.fit(X).best_estimator_, X)) def lw_score(X): return np.mean(cross_val_score(LedoitWolf(), X)) for X, title in [(X_homo, 'Homoscedastic Noise'), (X_hetero, 'Heteroscedastic Noise')]: pca_scores, fa_scores = compute_scores(X) n_components_pca = n_components[np.argmax(pca_scores)] n_components_fa = n_components[np.argmax(fa_scores)] pca = PCA(svd_solver='full', n_components='mle') pca.fit(X) n_components_pca_mle = pca.n_components_ print("best n_components by PCA CV = %d" % n_components_pca) print("best n_components by FactorAnalysis CV = %d" % n_components_fa) print("best n_components by PCA MLE = %d" % n_components_pca_mle) plt.figure() plt.plot(n_components, pca_scores, 'b', label='PCA scores') plt.plot(n_components, fa_scores, 'r', label='FA scores') plt.axvline(rank, color='g', label='TRUTH: %d' % rank, linestyle='-') plt.axvline(n_components_pca, color='b', label='PCA CV: %d' % n_components_pca, linestyle='--') plt.axvline(n_components_fa, color='r', label='FactorAnalysis CV: %d' % n_components_fa, linestyle='--') plt.axvline(n_components_pca_mle, color='k', label='PCA MLE: %d' % n_components_pca_mle, linestyle='--') # compare with other covariance estimators plt.axhline(shrunk_cov_score(X), color='violet', label='Shrunk Covariance MLE', linestyle='-.') plt.axhline(lw_score(X), color='orange', label='LedoitWolf MLE' % n_components_pca_mle, linestyle='-.') plt.xlabel('nb of components') plt.ylabel('CV scores') plt.legend(loc='lower right') plt.title(title) plt.show()
bsd-3-clause
assad2012/ggplot
ggplot/geoms/geom_linerange.py
12
2881
from __future__ import (absolute_import, division, print_function, unicode_literals) import sys from .geom import geom from ggplot.utils import is_categorical import numpy as np class geom_linerange(geom): """Plot intervals represented by vertical lines Parameters --------- x x values of data ymin lower end of the interval for each x ymax upper end of the interval for each x alpha : float alpha value, defaults to 1 color : string line color, defaults to 'black' linetype : string line type, defaults to 'solid' size : string width of the line, defaults to 2 Examples -------- .. plot:: :include-source: import numpy as np import pandas as pd from ggplot import * np.random.seed(42) x = np.linspace(0.5, 9.5, num=10) y = np.random.randn(10) ymin = y - np.random.uniform(0,1, size=10) ymax = y + np.random.uniform(0,1, size=10) data = pd.DataFrame({'x': x, 'ymin': ymin, 'ymax': ymax}) ggplot(aes(x='x', ymin='ymin', ymax='ymax'), data) \ + geom_linerange() """ DEFAULT_AES = {'alpha': 1, 'color': 'black', 'linetype': 'solid', 'size': 2} REQUIRED_AES = {'x', 'ymin', 'ymax'} DEFAULT_PARAMS = {'stat': 'identity', 'position': 'identity', 'cmap': None} _aes_renames = {'size': 'linewidth', 'linetype': 'linestyle'} _units = {'alpha', 'color', 'linestyle'} def __init__(self, *args, **kwargs): super(geom_linerange, self).__init__(*args, **kwargs) self._warning_printed = False def _plot_unit(self, pinfo, ax): # If x is categorical, calculate positions to plot categorical = is_categorical(pinfo['x']) if categorical: x = pinfo.pop('x') new_x = np.arange(len(x)) ax.set_xticks(new_x) ax.set_xticklabels(x) pinfo['x'] = new_x if 'linewidth' in pinfo and isinstance(pinfo['linewidth'], list): # ggplot also supports aes(size=...) but the current mathplotlib # is not. See https://github.com/matplotlib/matplotlib/issues/2658 pinfo['linewidth'] = 4 if not self._warning_printed: msg = "'geom_line()' currenty does not support the mapping of " +\ "size ('aes(size=<var>'), using size=4 as a replacement.\n" +\ "Use 'geom_line(size=x)' to set the size for the whole line.\n" sys.stderr.write(msg) self._warning_printed = True x = pinfo.pop('x') x = np.vstack([x, x]) ymin = pinfo.pop('ymin') ymax = pinfo.pop('ymax') y = np.vstack([ymin, ymax]) ax.plot(x, y, **pinfo)
bsd-2-clause
RPGOne/scikit-learn
sklearn/decomposition/tests/test_dict_learning.py
46
9267
import numpy as np from sklearn.exceptions import ConvergenceWarning from sklearn.utils import check_array from sklearn.utils.testing import assert_array_almost_equal from sklearn.utils.testing import assert_array_equal from sklearn.utils.testing import assert_equal from sklearn.utils.testing import assert_true from sklearn.utils.testing import assert_less from sklearn.utils.testing import assert_raises from sklearn.utils.testing import ignore_warnings from sklearn.utils.testing import TempMemmap from sklearn.decomposition import DictionaryLearning from sklearn.decomposition import MiniBatchDictionaryLearning from sklearn.decomposition import SparseCoder from sklearn.decomposition import dict_learning_online from sklearn.decomposition import sparse_encode rng_global = np.random.RandomState(0) n_samples, n_features = 10, 8 X = rng_global.randn(n_samples, n_features) def test_dict_learning_shapes(): n_components = 5 dico = DictionaryLearning(n_components, random_state=0).fit(X) assert_true(dico.components_.shape == (n_components, n_features)) def test_dict_learning_overcomplete(): n_components = 12 dico = DictionaryLearning(n_components, random_state=0).fit(X) assert_true(dico.components_.shape == (n_components, n_features)) def test_dict_learning_reconstruction(): n_components = 12 dico = DictionaryLearning(n_components, transform_algorithm='omp', transform_alpha=0.001, random_state=0) code = dico.fit(X).transform(X) assert_array_almost_equal(np.dot(code, dico.components_), X) dico.set_params(transform_algorithm='lasso_lars') code = dico.transform(X) assert_array_almost_equal(np.dot(code, dico.components_), X, decimal=2) # used to test lars here too, but there's no guarantee the number of # nonzero atoms is right. def test_dict_learning_reconstruction_parallel(): # regression test that parallel reconstruction works with n_jobs=-1 n_components = 12 dico = DictionaryLearning(n_components, transform_algorithm='omp', transform_alpha=0.001, random_state=0, n_jobs=-1) code = dico.fit(X).transform(X) assert_array_almost_equal(np.dot(code, dico.components_), X) dico.set_params(transform_algorithm='lasso_lars') code = dico.transform(X) assert_array_almost_equal(np.dot(code, dico.components_), X, decimal=2) def test_dict_learning_lassocd_readonly_data(): n_components = 12 with TempMemmap(X) as X_read_only: dico = DictionaryLearning(n_components, transform_algorithm='lasso_cd', transform_alpha=0.001, random_state=0, n_jobs=-1) with ignore_warnings(category=ConvergenceWarning): code = dico.fit(X_read_only).transform(X_read_only) assert_array_almost_equal(np.dot(code, dico.components_), X_read_only, decimal=2) def test_dict_learning_nonzero_coefs(): n_components = 4 dico = DictionaryLearning(n_components, transform_algorithm='lars', transform_n_nonzero_coefs=3, random_state=0) code = dico.fit(X).transform(X[np.newaxis, 1]) assert_true(len(np.flatnonzero(code)) == 3) dico.set_params(transform_algorithm='omp') code = dico.transform(X[np.newaxis, 1]) assert_equal(len(np.flatnonzero(code)), 3) def test_dict_learning_unknown_fit_algorithm(): n_components = 5 dico = DictionaryLearning(n_components, fit_algorithm='<unknown>') assert_raises(ValueError, dico.fit, X) def test_dict_learning_split(): n_components = 5 dico = DictionaryLearning(n_components, transform_algorithm='threshold', random_state=0) code = dico.fit(X).transform(X) dico.split_sign = True split_code = dico.transform(X) assert_array_equal(split_code[:, :n_components] - split_code[:, n_components:], code) def test_dict_learning_online_shapes(): rng = np.random.RandomState(0) n_components = 8 code, dictionary = dict_learning_online(X, n_components=n_components, alpha=1, random_state=rng) assert_equal(code.shape, (n_samples, n_components)) assert_equal(dictionary.shape, (n_components, n_features)) assert_equal(np.dot(code, dictionary).shape, X.shape) def test_dict_learning_online_verbosity(): n_components = 5 # test verbosity from sklearn.externals.six.moves import cStringIO as StringIO import sys old_stdout = sys.stdout try: sys.stdout = StringIO() dico = MiniBatchDictionaryLearning(n_components, n_iter=20, verbose=1, random_state=0) dico.fit(X) dico = MiniBatchDictionaryLearning(n_components, n_iter=20, verbose=2, random_state=0) dico.fit(X) dict_learning_online(X, n_components=n_components, alpha=1, verbose=1, random_state=0) dict_learning_online(X, n_components=n_components, alpha=1, verbose=2, random_state=0) finally: sys.stdout = old_stdout assert_true(dico.components_.shape == (n_components, n_features)) def test_dict_learning_online_estimator_shapes(): n_components = 5 dico = MiniBatchDictionaryLearning(n_components, n_iter=20, random_state=0) dico.fit(X) assert_true(dico.components_.shape == (n_components, n_features)) def test_dict_learning_online_overcomplete(): n_components = 12 dico = MiniBatchDictionaryLearning(n_components, n_iter=20, random_state=0).fit(X) assert_true(dico.components_.shape == (n_components, n_features)) def test_dict_learning_online_initialization(): n_components = 12 rng = np.random.RandomState(0) V = rng.randn(n_components, n_features) dico = MiniBatchDictionaryLearning(n_components, n_iter=0, dict_init=V, random_state=0).fit(X) assert_array_equal(dico.components_, V) def test_dict_learning_online_partial_fit(): n_components = 12 rng = np.random.RandomState(0) V = rng.randn(n_components, n_features) # random init V /= np.sum(V ** 2, axis=1)[:, np.newaxis] dict1 = MiniBatchDictionaryLearning(n_components, n_iter=10 * len(X), batch_size=1, alpha=1, shuffle=False, dict_init=V, random_state=0).fit(X) dict2 = MiniBatchDictionaryLearning(n_components, alpha=1, n_iter=1, dict_init=V, random_state=0) for i in range(10): for sample in X: dict2.partial_fit(sample[np.newaxis, :]) assert_true(not np.all(sparse_encode(X, dict1.components_, alpha=1) == 0)) assert_array_almost_equal(dict1.components_, dict2.components_, decimal=2) def test_sparse_encode_shapes(): n_components = 12 rng = np.random.RandomState(0) V = rng.randn(n_components, n_features) # random init V /= np.sum(V ** 2, axis=1)[:, np.newaxis] for algo in ('lasso_lars', 'lasso_cd', 'lars', 'omp', 'threshold'): code = sparse_encode(X, V, algorithm=algo) assert_equal(code.shape, (n_samples, n_components)) def test_sparse_encode_input(): n_components = 100 rng = np.random.RandomState(0) V = rng.randn(n_components, n_features) # random init V /= np.sum(V ** 2, axis=1)[:, np.newaxis] Xf = check_array(X, order='F') for algo in ('lasso_lars', 'lasso_cd', 'lars', 'omp', 'threshold'): a = sparse_encode(X, V, algorithm=algo) b = sparse_encode(Xf, V, algorithm=algo) assert_array_almost_equal(a, b) def test_sparse_encode_error(): n_components = 12 rng = np.random.RandomState(0) V = rng.randn(n_components, n_features) # random init V /= np.sum(V ** 2, axis=1)[:, np.newaxis] code = sparse_encode(X, V, alpha=0.001) assert_true(not np.all(code == 0)) assert_less(np.sqrt(np.sum((np.dot(code, V) - X) ** 2)), 0.1) def test_sparse_encode_error_default_sparsity(): rng = np.random.RandomState(0) X = rng.randn(100, 64) D = rng.randn(2, 64) code = ignore_warnings(sparse_encode)(X, D, algorithm='omp', n_nonzero_coefs=None) assert_equal(code.shape, (100, 2)) def test_unknown_method(): n_components = 12 rng = np.random.RandomState(0) V = rng.randn(n_components, n_features) # random init assert_raises(ValueError, sparse_encode, X, V, algorithm="<unknown>") def test_sparse_coder_estimator(): n_components = 12 rng = np.random.RandomState(0) V = rng.randn(n_components, n_features) # random init V /= np.sum(V ** 2, axis=1)[:, np.newaxis] code = SparseCoder(dictionary=V, transform_algorithm='lasso_lars', transform_alpha=0.001).transform(X) assert_true(not np.all(code == 0)) assert_less(np.sqrt(np.sum((np.dot(code, V) - X) ** 2)), 0.1)
bsd-3-clause
glemaitre/UnbalancedDataset
examples/over-sampling/plot_smote.py
2
2231
""" ===== SMOTE ===== An illustration of the SMOTE method and its variant. """ # Authors: Fernando Nogueira # Christos Aridas # Guillaume Lemaitre <g.lemaitre58@gmail.com> # License: MIT import matplotlib.pyplot as plt from sklearn.datasets import make_classification from sklearn.decomposition import PCA from imblearn.over_sampling import SMOTE print(__doc__) def plot_resampling(ax, X, y, title): c0 = ax.scatter(X[y == 0, 0], X[y == 0, 1], label="Class #0", alpha=0.5) c1 = ax.scatter(X[y == 1, 0], X[y == 1, 1], label="Class #1", alpha=0.5) ax.set_title(title) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.get_xaxis().tick_bottom() ax.get_yaxis().tick_left() ax.spines['left'].set_position(('outward', 10)) ax.spines['bottom'].set_position(('outward', 10)) ax.set_xlim([-6, 8]) ax.set_ylim([-6, 6]) return c0, c1 # Generate the dataset X, y = make_classification(n_classes=2, class_sep=2, weights=[0.3, 0.7], n_informative=3, n_redundant=1, flip_y=0, n_features=20, n_clusters_per_class=1, n_samples=80, random_state=10) # Instanciate a PCA object for the sake of easy visualisation pca = PCA(n_components=2) # Fit and transform x to visualise inside a 2D feature space X_vis = pca.fit_transform(X) # Apply regular SMOTE kind = ['regular', 'borderline1', 'borderline2', 'svm'] sm = [SMOTE(kind=k) for k in kind] X_resampled = [] y_resampled = [] X_res_vis = [] for method in sm: X_res, y_res = method.fit_sample(X, y) X_resampled.append(X_res) y_resampled.append(y_res) X_res_vis.append(pca.transform(X_res)) # Two subplots, unpack the axes array immediately f, ((ax1, ax2), (ax3, ax4), (ax5, ax6)) = plt.subplots(3, 2) # Remove axis for second plot ax2.axis('off') ax_res = [ax3, ax4, ax5, ax6] c0, c1 = plot_resampling(ax1, X_vis, y, 'Original set') for i in range(len(kind)): plot_resampling(ax_res[i], X_res_vis[i], y_resampled[i], 'SMOTE {}'.format(kind[i])) ax2.legend((c0, c1), ('Class #0', 'Class #1'), loc='center', ncol=1, labelspacing=0.) plt.tight_layout() plt.show()
mit
rabrahm/ceres
utils/FastRotators/spfr.py
1
18831
from pylab import * import pyfits from PyAstronomy import pyasl import scipy from scipy import interpolate from scipy import ndimage from scipy import signal import pickle from matplotlib.backends.backend_pdf import PdfPages import os #from pyevolve import G1DList #from pyevolve import GSimpleGA from multiprocessing import Pool import time def download_models(webpage='http://svo2.cab.inta-csic.es/theory/models/coelho/high/data/',dest='../../data/'): os.system('mkdir '+dest+'/COELHO2014') cwd = os.getcwd() os.chdir(dest+'/COELHO2014') tf = np.arange(6000,10001,250) gf = np.arange(2.5,4.6,0.5) #gf = np.array([2.5]) zf = np.array([-1.,-0.5,0.0,0.2]) for t in tf: for g in gf: for z in zf: modname = get_modname(t,g,z) if z<0: sz = 'm' else: sz = 'p' sz = sz+str(float(np.absolute(z))).replace('.','')+'p00/' os.system('wget ' + webpage+sz+modname+'.fits') os.system('wget ' + webpage+sz+modname+'plc.fits') os.chdir(cwd) return True def n_Edlen(l): sigma = 1e4 / l sigma2 = sigma*sigma n = 1 + 1e-8 * (8342.13 + 2406030 / (130-sigma2) + 15997/(38.9-sigma2)) return n def n_Morton(l): sigma = 1e4 / l sigma2 = sigma*sigma n = 1 + 6.4328e-5 + 2.94981e-2 / (146.-sigma2) + 2.5540e-4/(41.-sigma2) return n def ToAir(l): return (l / n_Edlen(l)) def ToVacuum(l): cond = 1 l_prev = l.copy() while(cond): l_new = n_Edlen(l_prev) * l if (max(np.absolute(l_new - l_prev)) < 1e-10): cond = 0 l_prev = l_new return l_prev def get_modname(t,g,z): st = str(int(t)) if t<10000: st = '0'+st sg = '+'+str(np.around(g,1)) if z < 0: sz = 'm' else: sz = 'p' z=float(z) sz = sz + str(np.around(np.absolute(z),1)) sz = sz.replace('.','') return 't'+st+'_g'+sg+'_'+sz+'p00_hr' def get_model(t,g,z,model_path='../../data/COELHO2014/'): modname = model_path + get_modname(t,g,z) try: out = pyfits.getdata(modname+'.fits') except: out = pyfits.getdata(modname+'plc.fits') return out def get_near(x,vec): if x == vec[0]: mmin = vec[0] mmax = vec[1] elif x == vec[-1]: mmin = vec[-2] mmax = vec[-1] else: tvec = vec - x In = np.where(tvec < 0)[0] mmin = tvec[In].max() + x Ix = np.where(tvec >= 0)[0] mmax = tvec[Ix].min() + x return mmin,mmax def trilinear_interpolation(t,g,z,model_path='../../data/COELHO2014/'): teffs = np.arange(6000,10001,250) loggs = np.arange(2.5,4.6,0.5) fehs = np.array([-1.,-0.5,0.0,0.2]) x0,x1 = get_near(t,teffs) y0,y1 = get_near(g,loggs) z0,z1 = get_near(z,fehs) xd = (t-x0)/(x1-x0) yd = (g-y0)/(y1-y0) zd = (z-z0)/(z1-z0) try: hd = pyfits.getheader(model_path+get_modname(x0,y0,z0)+'.fits') except: hd = pyfits.getheader(model_path+get_modname(x0,y0,z0)+'plc.fits') c000 = get_model(x0,y0,z0,model_path) c001 = get_model(x0,y0,z1,model_path) c010 = get_model(x0,y1,z0,model_path) c100 = get_model(x1,y0,z0,model_path) c110 = get_model(x1,y1,z0,model_path) c101 = get_model(x1,y0,z1,model_path) c011 = get_model(x0,y1,z1,model_path) c111 = get_model(x1,y1,z1,model_path) wav = np.arange(len(c111))*hd['CDELT1'] + hd['CRVAL1'] c00 = c000*(1-xd) + c100*xd c01 = c001*(1-xd) + c101*xd c10 = c010*(1-xd) + c110*xd c11 = c011*(1-xd) + c111*xd c0 = c00*(1-yd) + c10*yd c1 = c01*(1-yd) + c11*yd c = c0*(1-zd) + c1*zd return wav,c def normalize_model(w,f): ow = w.copy() of = f.copy() #plot(w,f) while True: #medflts = scipy.signal.medfilt(f,1001) coef = np.polyfit(w,f,6) fited = np.polyval(coef,w) res = f - fited I = np.where(res > -np.sqrt(np.var(res)))[0] w,f = w[I],f[I] if len(w) < 0.3* len(ow): break #plot(ow,np.polyval(coef,ow)) #show() return coef def spec_ccf(sw,sf,mw,mf,vi,vf,dv): mf = mf -1 mf = -mf #plot(mw,mf) tck = interpolate.splrep(mw,mf,k=1) v = vi retccf = [] vels = [] while v<=vf: swt = sw * (1 + v/299792.458) mft = interpolate.splev(swt,tck) #if v == 0: # plot(swt,mft) # plot(swt,sft) # show() mft -= np.mean(mft) sft = sf - np.mean(sf) #sft = sf.copy() #print np.sum(mft**2),np.sum(sft**2) retccf.append(np.sum(mft*sft)/np.sqrt(np.sum(mft**2)*np.sum(sft**2))) vels.append(v) v+=dv return np.array(vels),np.array(retccf) def ccf_fft(swt,sft,mwt,mft): mf = mft -1 mf = -mf #plot(mw,mf) tck = interpolate.splrep(np.log(mwt),mf,k=1) sw = np.log(swt) tck2 = interpolate.splrep(sw,sft,k=1) nsw = np.linspace(sw[0], sw[-1], 5000) sf = interpolate.splev(nsw,tck2) mf = interpolate.splev(nsw,tck) sf -= np.mean(sf) mf -= np.mean(mf) plot(nsw,sf) plot(nsw,mf) show() retccf = np.fft.ifft(np.conj(np.fft.fft(sf))*np.fft.fft(mf)) retccf = np.hstack((retccf[2500:],retccf[:2500])) retvels = np.arange(len(retccf)) - 0.5*len(retccf) retvels *= (nsw[1]-nsw[0]) retvels = 299792.458*(np.exp(retvels)-1.) return retvels, retccf def ccf_simple(sw,sf,mw,mf,rv): mf = mf -1 mf = -mf #plot(mw,mf) tck = interpolate.splrep(mw,mf,k=1) swt = sw * (1 + rv/299792.458) mft = interpolate.splev(swt,tck) mft -= np.mean(mft) sft = sf - np.mean(sf) return np.sum(mft*sft)/np.sqrt(np.sum(mft**2)*np.sum(sft**2)) def clean_strong_lines(mw,sc,mode=1): if mode==1: #"""" I = np.where((mw>6520)&(mw<6600))[0] sc[I] = 1. I = np.where((mw>5888)&(mw<5897))[0] sc[I] = 1. I = np.where((mw>4310)&(mw<4360))[0] sc[I] = 1. I = np.where((mw>4840)&(mw<4880))[0] sc[I] = 1. I = np.where((mw>4070)&(mw<4130))[0] sc[I] = 1. I = np.where((mw>3875)&(mw<3900))[0] sc[I] = 1. I = np.where((mw>3920)&(mw<3945))[0] sc[I] = 1. I = np.where((mw>3955)&(mw<3980))[0] sc[I] = 1. I = np.where(mw<3850)[0] sc[I] = 1. #""" if mode==2: #"""" I = np.where((mw>6550)&(mw<6570))[0] sc[I] = 1. I = np.where((mw>5888)&(mw<5897))[0] sc[I] = 1. I = np.where((mw>4320)&(mw<4350))[0] sc[I] = 1. I = np.where((mw>4850)&(mw<4870))[0] sc[I] = 1. I = np.where((mw>4090)&(mw<4110))[0] sc[I] = 1. I = np.where((mw>3875)&(mw<3900))[0] sc[I] = 1. I = np.where((mw>3920)&(mw<3945))[0] sc[I] = 1. I = np.where((mw>3955)&(mw<3980))[0] sc[I] = 1. I = np.where(mw<3850)[0] sc[I] = 1. #""" return sc def RVforFR(wavs,flxs,teff=6700,logg=4.0,feh=-1.0,vsini=100.,model_path='../../data/COELHO2014/',vmin=-1000.,vmax=1000.,vstep=10.): def fitfunc(p,x): ret = p[3] + p[0] * np.exp(-.5*((x-p[1])/p[2])**2) return ret errfunc = lambda p,x,y: np.ravel( (fitfunc(p,x)-y) ) #sc = get_model(teff,logg,feh) #hd = pyfits.getheader(model_path+get_modname(7000,4.5,0.0)+'.fits') #wav = np.arange(len(sc))*hd['CDELT1'] + hd['CRVAL1'] teff = float(teff) try: sc = get_model(teff,logg,feh) hd = pyfits.getheader(model_path+get_modname(7000,4.5,0.0)+'.fits') mw = np.arange(len(sc))*hd['CDELT1'] + hd['CRVAL1'] except: mw,sc = trilinear_interpolation(teff,logg,feh,model_path) for order in range(len(flxs)): flxs[order] = clean_strong_lines(wavs[order],flxs[order]) sc = clean_strong_lines(mw,sc) II = np.where(sc != 1)[0] JJ = np.where(sc == 1)[0] coef = normalize_model(mw[II],sc[II]) sc /= np.polyval(coef,mw) sc[JJ] = 1. mw = ToVacuum(mw) weis1 = [] ccftot = [] for i in range(wavs.shape[0]): #plot(wavs[i],flxs[i]) scf = flxs[i] scw = wavs[i] J = np.where(scf!=0)[0] scw,scf = scw[J],scf[J] I = np.where((mw>scw[0]-100) & (mw<scw[-1]+100)) tmf = pyasl.fastRotBroad(mw[I], sc[I], 0.5, vsini) #plot(mw[I],tmf) J = np.where(scf!=1)[0] if len(J)>100: ccv,ccf = spec_ccf(scw,scf,mw[I],tmf,vmin,vmax,vstep) #plot(ccv,ccf) #show() #ccf = np.array(ccf) wei1 = len(np.where(scf!=1)[0])**2 weis1.append(wei1) if len(ccftot)==0: ccftot = ccf.copy()*wei1 else: ccftot = np.vstack((ccftot,ccf.copy()*wei1)) #show() weis1 = np.array(weis1) ccftot = np.sum(ccftot,axis=0)/ np.sum(weis1) p0 = [ccftot.min(),ccv[np.argmin(ccftot)],vsini,ccftot[0]] p1, success = scipy.optimize.leastsq(errfunc,p0, args=(ccv,ccftot)) return p1,ccv,ccftot,fitfunc(p1,ccv) def calc_bss2(vels,xc,coef, bot_i=0.15, bot_f=0.4, top_i=0.6, top_f=0.9, dt=0.01): try: I1 = np.where((vels>coef[1]-3*coef[2]) & (vels<coef[1]) )[0] I2 = np.where((vels<coef[1]+3*coef[2]) & (vels>coef[1]) )[0] I3 = np.where(vels<coef[1]-4*coef[2])[0] I4 = np.where(vels>coef[1]+4*coef[2])[0] I = np.hstack((I3,I4)) base = np.median(xc[I]) xc = base - xc xc /= xc.max() v1,x1 = vels[I1],xc[I1] v2,x2 = vels[I2],xc[I2] #plot(v1,x1) #plot(v2,x2) #show() dp = top_f vect = [] while dp >= top_i: lb = np.where(x1>dp)[0][0] m = (v1[lb] - v1[lb-1])/(x1[lb]-x1[lb-1]) n = v1[lb] - m*x1[lb] bs1 = m*dp+n lb = np.where(x2>dp)[0][-1] m = (v2[lb] - v2[lb+1])/(x2[lb]-x2[lb+1]) n = v2[lb] - m*x2[lb] bs2 = m*dp+n vect.append(0.5*(bs2+bs1)) dp-=dt vect = np.array(vect) dp = bot_f vecb = [] while dp >= bot_i: lb = np.where(x1>dp)[0][0] m = (v1[lb] - v1[lb-1])/(x1[lb]-x1[lb-1]) n = v1[lb] - m*x1[lb] bs1 = m*dp+n lb = np.where(x2>dp)[0][-1] m = (v2[lb] - v2[lb+1])/(x2[lb]-x2[lb+1]) n = v2[lb] - m*x2[lb] bs2 = m*dp+n vecb.append(0.5*(bs2+bs1)) dp-=dt vecb = np.array(vecb) return np.median(vecb) - np.median(vect) except: return -999.0 """ def lnlike(theta, W, F, Ferr): mw,sc = trilinear_interpolation(int(theta[0]),theta[1],theta[2]) sct = clean_strong_lines(mw,sc.copy()) #plot(mw,sc) #show() coef = normalize_model(mw,sct) sc /= np.polyval(coef,mw) #print gfd mw = ToVacuum(mw) mw *= 1 + theta[3]/299792.458 totD,totM,totE = np.array([]),np.array([]),np.array([]) for i in range(W.shape[0]): scf = F[i] scw = W[i] scfe = Ferr[i] J = np.where(scf!=0)[0] scw,scf,scfe = scw[J],scf[J],scfe[J] I = np.where((mw>scw[0]-10) & (mw<scw[-1]+10)) tmf = pyasl.fastRotBroad(mw[I], sc[I], 0.5, theta[4]) tck = interpolate.splrep(mw[I],tmf,k=1) tmf = interpolate.splev(scw,tck) tmf = clean_strong_lines(scw,tmf.copy()) I = np.where(tmf!=1)[0] #plot(scw,tmf) #plot(scw[I],tmf[I]) #plot(scw[I],scf[I]) #show() #print gfd tmf = tmf[I] scf = scf[I] scfe = scfe[I] tmf /= np.sum(tmf) tsf = scf/np.sum(scf) tse = scfe*(np.sum(scf)**2) totD = np.hstack((totD,tsf)) totM = np.hstack((totM,tmf)) totE = np.hstack((totE,tse)) #plot(scw[I],tsf) #plot(scw[I],tmf) #plot(scw[I],tsf + 1./np.sqrt(tse)) #show() #print fds #print theta #show() #print gvfd #ret = -np.log(2*np.pi) + np.log(np.sum(np.exp(-0.5*((y-model)/yerr)**2)/yerr)) #ret = -0.5*(np.sum(inv_sigma2*(F-model)**2 - np.log(inv_sigma2))) ret = -0.5*(np.sum(totE*(totD-totM)**2 - np.log(totE))) #for i in range(len(F)): # errorbar(Y,F[i],yerr=Ferr[i],fmt='b') #for j in model: # plot(Y,j,'r') #show() #print theta, ret if np.isnan(ret): return -np.inf else: return ret def lnprior(theta): if 6000 < theta[0] < 9000 and 3.0 < theta[1] < 4.5 and -1 < theta[2] < 0.2 and -500 < theta[3] < 500 and 1. < theta[4] < 500.: return 0.0 return -np.inf def lnprob(theta, W,F,Ferr): lp = lnprior(theta) if not np.isfinite(lp): return -np.inf return lp + lnlike(theta,W,F,Ferr) """ def multiccf(pars): teff,logg,feh,vsini=pars[0],pars[1],pars[2],pars[3] vmin=-500 vmax=500. vstep=20. sc = get_model(teff,logg,feh) hd = pyfits.getheader(model_path+get_modname(7000,4.5,0.0)+'.fits') wav = np.arange(len(sc))*hd['CDELT1'] + hd['CRVAL1'] try: sc = get_model(teff,logg,feh) hd = pyfits.getheader(model_path+get_modname(7000,4.5,0.0)+'.fits') mw = np.arange(len(sc))*hd['CDELT1'] + hd['CRVAL1'] except: mw,sc = trilinear_interpolation(teff,logg,feh,model_path) sc = clean_strong_lines(mw,sc) II = np.where(sc != 1)[0] JJ = np.where(sc == 1)[0] coef = normalize_model(mw[II],sc[II]) sc /= np.polyval(coef,mw) sc[JJ] = 1. mw = ToVacuum(mw) weis1 = [] ccftot = [] for i in range(wavs.shape[0]): scf = flxs[i].copy() scw = wavs[i].copy() J = np.where(scf!=0)[0] scw,scf = scw[J],scf[J] I = np.where((mw>scw[0]-100) & (mw<scw[-1]+100)) tmf = pyasl.fastRotBroad(mw[I], sc[I], 0.5, vsini) #plot(mw[I],tmf) J = np.where(scf!=1)[0] if len(J)>100: ccv,ccf = spec_ccf(scw,scf,mw[I],tmf,vmin,vmax,vstep) #ccv,ccf = ccf_fft(scw,scf,mw[I],tmf) #plot(ccv,ccf) #show() wei1 = len(np.where(scf!=1)[0])**2 weis1.append(wei1) if len(ccftot)==0: ccftot = ccf.copy()*wei1 else: ccftot = np.vstack((ccftot,ccf.copy()*wei1)) weis1 = np.array(weis1) ccftot = np.sum(ccftot,axis=0)/ np.sum(weis1) #print gfds #ccftot = np.mean(ccftot,axis=0) #print pars, ccftot.min() return ccftot.min() def get_pars_fr(wavst,flxst,model_patht='../../data/COELHO2014/',npools=4,fixG=1.0): for order in range(len(flxst)): flxst[order] = clean_strong_lines(wavst[order],flxst[order],mode=1) t0 = time.time() global wavs,flxs global model_path wavs,flxs=wavst.copy(),flxst.copy() model_path=model_patht gt = np.array([6000,7000,8000,9000,10000]) gg = np.array([2.5,3.0,3.5,4.0,4.5]) if fixG != -1: gg = np.array([fixG]) gz = np.array([-1,-0.5,0.0,0.2]) gr = np.array([10.,50.,100.,150.,200.,250.,300.]) #""" tr = np.tile(gr,len(gt)*len(gg)*len(gz)) tg = np.repeat(np.tile(gg,len(gt)),len(gr)*len(gz)) tz = np.repeat(np.tile(gz,len(gt)*len(gg)),len(gr)) tt = np.repeat(gt,len(gg)*len(gr)*len(gz)) tot = np.vstack((tt,tg,tz,tr)).T #for pars in tot: # pars = [8000,4.0,-0.5,40.0] # print pars, multiccf(pars) p = Pool(npools) vals = np.array((p.map(multiccf, list(tot)))) p.terminate() I = np.argmin(vals) best_vals = tot[I] bt,bg,bz,br = best_vals[0],best_vals[1],best_vals[2],best_vals[3] #""" t1 = time.time() print bt,bg,bz,br, (t1-t0)/60.,'mins' #bt,bg,bz,br = 7000.,4.5, 0.2, 100.0 gt = np.arange(bt-1000,bt+1001,250) I = np.where((gt>=6000) & (gt<=10000))[0] gt = gt[I] gr = np.arange(br-60.,br+61.,20.) I = np.where(gr>=10)[0] gr = gr[I] tr = np.tile(gr,len(gt)*len(gg)*len(gz)) tg = np.repeat(np.tile(gg,len(gt)),len(gr)*len(gz)) tz = np.repeat(np.tile(gz,len(gt)*len(gg)),len(gr)) tt = np.repeat(gt,len(gg)*len(gr)*len(gz)) tot = np.vstack((tt,tg,tz,tr)).T p = Pool(npools) vals = np.array((p.map(multiccf, list(tot)))) p.terminate() I = np.argmin(vals) best_vals = tot[I] bt,bg,bz,br = best_vals[0],best_vals[1],best_vals[2],best_vals[3] t2 = time.time() print bt,bg,bz,br, (t2-t1)/60.,'mins' #np.savetxt('temp_grid.txt',vals) if fixG==-1: grid = np.reshape(vals,(len(gt),len(gg),len(gz),len(gr))) tckt = interpolate.splrep(gt,np.arange(len(gt)),k=1) tckg = interpolate.splrep(gg,np.arange(len(gg)),k=1) tckz = interpolate.splrep(gz,np.arange(len(gz)),k=1) tckr = interpolate.splrep(gr,np.arange(len(gr)),k=1) itckt = interpolate.splrep(np.arange(len(gt)),gt,k=1) itckg = interpolate.splrep(np.arange(len(gg)),gg,k=1) itckz = interpolate.splrep(np.arange(len(gz)),gz,k=1) itckr = interpolate.splrep(np.arange(len(gr)),gr,k=1) st = np.arange(gt[0],gt[-1]+1,10.) sg = np.arange(gg[0],gg[-1]+0.01,0.1) sz = np.arange(gz[0],gz[-1]+0.01,0.1) sr = np.arange(gr[0],gr[-1]+1.,5.) st = interpolate.splev(st,tckt) sg = interpolate.splev(sg,tckg) sz = interpolate.splev(sz,tckz) sr = interpolate.splev(sr,tckr) tr2 = np.tile(sr,len(st)*len(sg)*len(sz)) tg2 = np.repeat(np.tile(sg,len(st)),len(sr)*len(sz)) tz2 = np.repeat(np.tile(sz,len(st)*len(sg)),len(sr)) tt2 = np.repeat(st,len(sg)*len(sr)*len(sz)) tot2 = np.vstack((tt2,tg2,tz2,tr2)) zi = ndimage.map_coordinates(grid, tot2, order=3, mode='nearest') I = np.argmin(zi) minval = tot2[:,I] mint = interpolate.splev(minval[0],itckt) ming = interpolate.splev(minval[1],itckg) minz = interpolate.splev(minval[2],itckz) minr = interpolate.splev(minval[3],itckr) else: grid = np.reshape(vals,(len(gt),len(gz),len(gr))) tckt = interpolate.splrep(gt,np.arange(len(gt)),k=1) tckz = interpolate.splrep(gz,np.arange(len(gz)),k=1) tckr = interpolate.splrep(gr,np.arange(len(gr)),k=1) itckt = interpolate.splrep(np.arange(len(gt)),gt,k=1) itckz = interpolate.splrep(np.arange(len(gz)),gz,k=1) itckr = interpolate.splrep(np.arange(len(gr)),gr,k=1) st = np.arange(gt[0],gt[-1]+1,10.) sz = np.arange(gz[0],gz[-1]+0.01,0.1) sr = np.arange(gr[0],gr[-1]+1.,5.) st = interpolate.splev(st,tckt) sz = interpolate.splev(sz,tckz) sr = interpolate.splev(sr,tckr) tr2 = np.tile(sr,len(st)*len(sz)) tz2 = np.repeat(np.tile(sz,len(st)),len(sr)) tt2 = np.repeat(st,len(sr)*len(sz)) tot2 = np.vstack((tt2,tz2,tr2)) zi = ndimage.map_coordinates(grid, tot2, order=3, mode='nearest') I = np.argmin(zi) minval = tot2[:,I] mint = interpolate.splev(minval[0],itckt) ming = fixG minz = interpolate.splev(minval[1],itckz) minr = interpolate.splev(minval[2],itckr) #d = {'grid':grid, 'zi':zi, 'tot2':tot2, 'gt':gt, 'gg':gg, 'gz':gz, 'gr':gr} #pickle.dump(d,open('temp_dict.pkl')) return float(mint),float(ming),float(minz),float(minr) def plot_CCF_FR(xc_dict,path='XC.pdf'): vels = xc_dict['vels'] xc_av = xc_dict['xc_av'] XCmodelgau = xc_dict['XCmodelgau'] #refvel = xc_dict['refvel'] p1gau = xc_dict['p1gau'] f1 = figure() pp = PdfPages(path) ax1 = f1.add_subplot(111) ax1.plot(vels, xc_av,'b.', label='CCF') ax1.plot(vels, XCmodelgau,'r-',label='Gaussian fit') xlabel('Velocity (km/s)') ylabel('XC') ax1.axvline(p1gau[1],linestyle=':',color='r') ax1.axhline(0.0,linestyle='-') title('Average Cross-Correlation Function + Fit') handles, labels = ax1.get_legend_handles_labels() ax1.legend(handles[::-1], labels[::-1],prop={'size':6}) pp.savefig() pp.close() clf() pass """ def trans_chromosome(chromosome): teff = chromosome[0]*100.+chromosome[1]*10.+chromosome[2] m = (10000.- 6000.)/999. n = 6000. teff = teff*m + n logg = chromosome[3] + chromosome[4]*0.1 m = (4.5 - 3.0)/9.9 n = 3. logg = logg*m + n feh = chromosome[5] + chromosome[6]*0.1 m = (0.2 - -1.)/9.9 n = -1. feh = feh*m + n vsini = chromosome[7]*10. + chromosome[8] m = (300. - 10.)/99. n = 10. vsini = vsini*m + n return teff, logg, feh, vsini global wavs, flxs def find_pars_GA(wavs,flxs,model_path='../../data/COELHO2014/'): def eval_func(chromosome): print list(chromosome) teff, logg, feh, vsini = trans_chromosome(chromosome) print teff, logg, feh, vsini pt,vels,ccf,mod = RVforFR(wavs,flxs,teff=teff,logg=logg,feh=feh,vsini=vsini,model_path=model_path) score = -ccf.min() return score genome = G1DList.G1DList(9) genome.evaluator.set(eval_func) ga = GSimpleGA.GSimpleGA(genome, interactiveMode=True) ga.setGenerations(40) ga.setMutationRate(0.2) ga.setPopulationSize(20) #ga.setCrossoverRate(1.0) genome.setParams(rangemin=0, rangemax=9) #ga.setMultiProcessing(True) ga.evolve(freq_stats=10) print ga.bestIndividual() print trans_chromosome(ga.bestIndividual()) """
mit
xiaoweih/DLV
networks/imageNet.py
1
1666
import os, struct from array import array as pyarray from cvxopt.base import matrix import numpy as np import PIL.Image # FIXME: need actual class names def LABELS(index): ls = labels() if len(ls) > 0: return ls[index] else: return range(1000)[index] def labels(): file = open('networks/imageNet/caffe_ilsvrc12/synset_words.txt', 'r') data = file.readlines() ls = [] for line in data: words = line.split() ls.append(' '.join(words[1:])) return ls def save(layer,image,filename): """ """ import cv2 import copy image_cv = copy.deepcopy(image) image_cv = image_cv.transpose(1, 2, 0) image_cv[:,:,0] += 103.939 image_cv[:,:,1] += 116.779 image_cv[:,:,2] += 123.68 #print(np.amax(image_cv),np.amin(image_cv)) cv2.imwrite(filename, image_cv) # from matplotlib import pyplot # import matplotlib as mpl # fig = pyplot.figure() # ax = fig.add_subplot(1,1,1) # # image = image.reshape(3,32,32).transpose(1,2,0) # imgplot = ax.imshow(image.T, cmap=mpl.cm.Greys) # imgplot.set_interpolation('nearest') # ax.xaxis.set_ticks_position('top') # ax.yaxis.set_ticks_position('left') # pyplot.savefig(filename) def show(image): """ """ from matplotlib import pyplot import matplotlib as mpl fig = pyplot.figure() ax = fig.add_subplot(1,1,1) #image = image.reshape(3,32,32).transpose(1,2,0) imgplot = ax.imshow(image.T, cmap=mpl.cm.Greys) imgplot.set_interpolation('nearest') ax.xaxis.set_ticks_position('top') ax.yaxis.set_ticks_position('left') pyplot.show()
gpl-3.0
CranleighAD/isams-tools
settings_example.py
1
2947
# enable or disable the whole program ENABLED = True # if we're in testing mode, output more debug and allow testers to add their own email DEBUG = True # used with above, you can check the output of emails that would have been sent SEND_EMAILS = True # iSAMS Batch API key API_KEY = "11D497FF-A7D9-4646-A6B8-D9D1B8718FAC" # iSAMS URL URL = 'https://isams.school.com' # Choose which connection method from: JSON, XML, MSSQL CONNECTION_METHOD = 'JSON' # Database settings DATABASE = '' DATABASE_SERVER = '' DATABASE_USER = '' DATABASE_PASSWORD = '' # specify your own dates to use when testing, e.g. a date that has already had the register taken for DEBUG_START_DATE = '2016-09-18' DEBUG_END_DATE = '2016-09-19' # allows you to specify a file with XML or JSON content to test with rather tha using live data DEBUG_DATA = 'test_data.xml' # outgoing SMTP details EMAIL = { 'server': 'smtp.example.com', 'port': 465, 'username': 'john@company.com', 'password': 'p455w0rd', 'subject': 'Register not completed', 'from': 'isams@company.com', 'to': 'isams@company.com', 'cc': 'reception@company.com', 'bcc': 'manager@company.com' } # whether to log into the SMTP server EMAIL_LOGIN = True # whether to create an SSL connection or not EMAIL_SSL = True # Default: Monday - Friday, 0 = Mon, 6 = Sun WORKING_DAYS = (0, 1, 2, 3, 4) # weekdays which are not school days # for help generating these: # import pandas # pandas.bdate_range('2016-12-15', '2017-01-07') HOLIDAYS = ( # Winter break '2016-12-15', '2016-12-16', '2016-12-19', '2016-12-20', '2016-12-21', '2016-12-22', '2016-12-23', '2016-12-26', '2016-12-27', '2016-12-28', '2016-12-29', '2016-12-30', '2017-01-02', '2017-01-03', '2017-01-04', '2017-01-05', '2017-01-06', ) # email templates FIRST_EMAIL = """ Dear Teacher, This is a friendly reminder to complete your register. One or more of your students has not yet been registered. If you are having problems completing it, please email XXX If this message is in error, please forward to the helpdesk. Regards, iSAMS Bot """ SECOND_EMAIL = """ Dear Teacher, There are still one or more of your students has not yet been registered. If you are having problems completing it, please email XXX If this message is in error, please forward to the helpdesk. Regards, iSAMS Bot """ # You can use %list_of_missing_registers% for a list in the template FINAL_EMAIL = """ Here is a list of forms that still are oustanding: %list_of_missing_registers% Regards, iSAMS Bot """ # separate with commas if you want more than one recipient FINAL_EMAIL_TO = "reception@company.com" ####################### # Data Check Settings # ####################### DATA_CHECK_ENABED = True # who to email when it fails DATA_CHECK_FAIL_EMAIL = "manager@company.com" # list of subjects to ignore from checks in single quotes DATA_CHECK_IGNORE_SUBJECTS = ["Games", "Physical Education"]
gpl-3.0
aabadie/scikit-learn
examples/manifold/plot_lle_digits.py
138
8594
""" ============================================================================= Manifold learning on handwritten digits: Locally Linear Embedding, Isomap... ============================================================================= An illustration of various embeddings on the digits dataset. The RandomTreesEmbedding, from the :mod:`sklearn.ensemble` module, is not technically a manifold embedding method, as it learn a high-dimensional representation on which we apply a dimensionality reduction method. However, it is often useful to cast a dataset into a representation in which the classes are linearly-separable. t-SNE will be initialized with the embedding that is generated by PCA in this example, which is not the default setting. It ensures global stability of the embedding, i.e., the embedding does not depend on random initialization. """ # Authors: Fabian Pedregosa <fabian.pedregosa@inria.fr> # Olivier Grisel <olivier.grisel@ensta.org> # Mathieu Blondel <mathieu@mblondel.org> # Gael Varoquaux # License: BSD 3 clause (C) INRIA 2011 print(__doc__) from time import time import numpy as np import matplotlib.pyplot as plt from matplotlib import offsetbox from sklearn import (manifold, datasets, decomposition, ensemble, discriminant_analysis, random_projection) digits = datasets.load_digits(n_class=6) X = digits.data y = digits.target n_samples, n_features = X.shape n_neighbors = 30 #---------------------------------------------------------------------- # Scale and visualize the embedding vectors def plot_embedding(X, title=None): x_min, x_max = np.min(X, 0), np.max(X, 0) X = (X - x_min) / (x_max - x_min) plt.figure() ax = plt.subplot(111) for i in range(X.shape[0]): plt.text(X[i, 0], X[i, 1], str(digits.target[i]), color=plt.cm.Set1(y[i] / 10.), fontdict={'weight': 'bold', 'size': 9}) if hasattr(offsetbox, 'AnnotationBbox'): # only print thumbnails with matplotlib > 1.0 shown_images = np.array([[1., 1.]]) # just something big for i in range(digits.data.shape[0]): dist = np.sum((X[i] - shown_images) ** 2, 1) if np.min(dist) < 4e-3: # don't show points that are too close continue shown_images = np.r_[shown_images, [X[i]]] imagebox = offsetbox.AnnotationBbox( offsetbox.OffsetImage(digits.images[i], cmap=plt.cm.gray_r), X[i]) ax.add_artist(imagebox) plt.xticks([]), plt.yticks([]) if title is not None: plt.title(title) #---------------------------------------------------------------------- # Plot images of the digits n_img_per_row = 20 img = np.zeros((10 * n_img_per_row, 10 * n_img_per_row)) for i in range(n_img_per_row): ix = 10 * i + 1 for j in range(n_img_per_row): iy = 10 * j + 1 img[ix:ix + 8, iy:iy + 8] = X[i * n_img_per_row + j].reshape((8, 8)) plt.imshow(img, cmap=plt.cm.binary) plt.xticks([]) plt.yticks([]) plt.title('A selection from the 64-dimensional digits dataset') #---------------------------------------------------------------------- # Random 2D projection using a random unitary matrix print("Computing random projection") rp = random_projection.SparseRandomProjection(n_components=2, random_state=42) X_projected = rp.fit_transform(X) plot_embedding(X_projected, "Random Projection of the digits") #---------------------------------------------------------------------- # Projection on to the first 2 principal components print("Computing PCA projection") t0 = time() X_pca = decomposition.TruncatedSVD(n_components=2).fit_transform(X) plot_embedding(X_pca, "Principal Components projection of the digits (time %.2fs)" % (time() - t0)) #---------------------------------------------------------------------- # Projection on to the first 2 linear discriminant components print("Computing Linear Discriminant Analysis projection") X2 = X.copy() X2.flat[::X.shape[1] + 1] += 0.01 # Make X invertible t0 = time() X_lda = discriminant_analysis.LinearDiscriminantAnalysis(n_components=2).fit_transform(X2, y) plot_embedding(X_lda, "Linear Discriminant projection of the digits (time %.2fs)" % (time() - t0)) #---------------------------------------------------------------------- # Isomap projection of the digits dataset print("Computing Isomap embedding") t0 = time() X_iso = manifold.Isomap(n_neighbors, n_components=2).fit_transform(X) print("Done.") plot_embedding(X_iso, "Isomap projection of the digits (time %.2fs)" % (time() - t0)) #---------------------------------------------------------------------- # Locally linear embedding of the digits dataset print("Computing LLE embedding") clf = manifold.LocallyLinearEmbedding(n_neighbors, n_components=2, method='standard') t0 = time() X_lle = clf.fit_transform(X) print("Done. Reconstruction error: %g" % clf.reconstruction_error_) plot_embedding(X_lle, "Locally Linear Embedding of the digits (time %.2fs)" % (time() - t0)) #---------------------------------------------------------------------- # Modified Locally linear embedding of the digits dataset print("Computing modified LLE embedding") clf = manifold.LocallyLinearEmbedding(n_neighbors, n_components=2, method='modified') t0 = time() X_mlle = clf.fit_transform(X) print("Done. Reconstruction error: %g" % clf.reconstruction_error_) plot_embedding(X_mlle, "Modified Locally Linear Embedding of the digits (time %.2fs)" % (time() - t0)) #---------------------------------------------------------------------- # HLLE embedding of the digits dataset print("Computing Hessian LLE embedding") clf = manifold.LocallyLinearEmbedding(n_neighbors, n_components=2, method='hessian') t0 = time() X_hlle = clf.fit_transform(X) print("Done. Reconstruction error: %g" % clf.reconstruction_error_) plot_embedding(X_hlle, "Hessian Locally Linear Embedding of the digits (time %.2fs)" % (time() - t0)) #---------------------------------------------------------------------- # LTSA embedding of the digits dataset print("Computing LTSA embedding") clf = manifold.LocallyLinearEmbedding(n_neighbors, n_components=2, method='ltsa') t0 = time() X_ltsa = clf.fit_transform(X) print("Done. Reconstruction error: %g" % clf.reconstruction_error_) plot_embedding(X_ltsa, "Local Tangent Space Alignment of the digits (time %.2fs)" % (time() - t0)) #---------------------------------------------------------------------- # MDS embedding of the digits dataset print("Computing MDS embedding") clf = manifold.MDS(n_components=2, n_init=1, max_iter=100) t0 = time() X_mds = clf.fit_transform(X) print("Done. Stress: %f" % clf.stress_) plot_embedding(X_mds, "MDS embedding of the digits (time %.2fs)" % (time() - t0)) #---------------------------------------------------------------------- # Random Trees embedding of the digits dataset print("Computing Totally Random Trees embedding") hasher = ensemble.RandomTreesEmbedding(n_estimators=200, random_state=0, max_depth=5) t0 = time() X_transformed = hasher.fit_transform(X) pca = decomposition.TruncatedSVD(n_components=2) X_reduced = pca.fit_transform(X_transformed) plot_embedding(X_reduced, "Random forest embedding of the digits (time %.2fs)" % (time() - t0)) #---------------------------------------------------------------------- # Spectral embedding of the digits dataset print("Computing Spectral embedding") embedder = manifold.SpectralEmbedding(n_components=2, random_state=0, eigen_solver="arpack") t0 = time() X_se = embedder.fit_transform(X) plot_embedding(X_se, "Spectral embedding of the digits (time %.2fs)" % (time() - t0)) #---------------------------------------------------------------------- # t-SNE embedding of the digits dataset print("Computing t-SNE embedding") tsne = manifold.TSNE(n_components=2, init='pca', random_state=0) t0 = time() X_tsne = tsne.fit_transform(X) plot_embedding(X_tsne, "t-SNE embedding of the digits (time %.2fs)" % (time() - t0)) plt.show()
bsd-3-clause
annashcherbina/FASTQSim
src/msa_from_sam.py
2
15121
#This file does three things: #1. calculate the frequency of mutations, insertions, and deletions at each position in an #NGS read. #2. find the degree of coverage of the reference genome #3. find the fraction of each subject read that was aligned to a corresponding sequence #in the refenece genome. # @author Anna Shcherbina (mailto: anna.shcherbina@ll.mit.edu) #License: GNU GPL license (http://www.gnu.org/licenses/gpl.html) # # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. #takes a sam file as input and produces characterization csv files as an output from helpers import *; from pandas import DataFrame; import sys; inputf=open(sys.argv[1],'r') outputprefix=sys.argv[2] plothistogram=False if len(sys.argv) > 3: if sys.argv[3]=="-plothistogram": plothistogram=True import matplotlib.pyplot as plt posCount=dict() delCount=dict() insertCount=dict() mutCount=dict() mutType=dict() insertSize=dict() delSize=dict() Usage=dict() insertsByRead=[] delsByRead=[] for line in inputf: if line.startswith('@'): continue line=line.strip(); line=line.split('\t') #print str(line)+'\n' seqname=line[0] aligned=int(line[1]) # print str(aligned)+'\n' if aligned !=0: continue refname=line[2] refstartpos=int(line[3]) cigar=line[5] strand=line[9] strandlength=len(strand) md="" #account for single base pair mutations for i in range(11,len(line)): if 'MD' in line[i]: md=line[i].replace('MD','') md=md.replace('Z','') md=md.replace(':','') break ref_snp_bases=parseMD(md) cigarlist=parseCIGAR(cigar) #posCount startpos,endpos=getPosCount(cigarlist,strandlength) Usage[seqname]=[strandlength,endpos-startpos+1,startpos,endpos] strandofinterest=strand[startpos:endpos+1] for p in range(startpos,endpos+1): if p not in posCount: posCount[p]=1 else: posCount[p]+=1 alignmentpos=0; curpos=startpos; insertcountforread=0; delcountforread=0; #get Insertions/Deletions for block in cigarlist: if 'h' in block: continue elif 'p' in block: continue elif 's' in block: continue elif ('m' in block) or ('=' in block) or ('x' in block): #match or single-base mutation numbases=int(block.replace('m','')) alignmentpos+=numbases curpos+=numbases elif 'i' in block: insertcountforread+=1 insertionsize=int(block.replace('i','')) insertionbases=strand[curpos+1:curpos+2+insertionsize] for p in range(curpos+1,curpos+2+insertionsize): if p not in insertCount: insertCount[p]=1 else: insertCount[p]+=1 longestRepeat=getLongestRepeat(insertionbases) if insertionsize not in insertSize: insertSize[insertionsize]=[1,0] else: insertSize[insertionsize][0]+=1 if (longestRepeat > 0) and (longestRepeat not in insertSize): insertSize[longestRepeat]=[0,1] elif longestRepeat > 0: insertSize[longestRepeat][1]+=1 curpos+=insertionsize alignmentpos+=insertionsize elif 'd' in block: delcountforread+=1 deletionsize=int(block.replace('d','')) deletionbases=strand[curpos+1:curpos+2+deletionsize] for p in range(curpos+1,curpos+2+deletionsize): if p not in delCount: delCount[p]=1 else: delCount[p]+=1 longestRepeat=getLongestRepeat(deletionbases) if deletionsize not in delSize: delSize[deletionsize]=[1,0] else: delSize[deletionsize][0]+=1 if (longestRepeat > 0) and (longestRepeat not in delSize): delSize[longestRepeat]=[0,1] elif longestRepeat > 0: delSize[longestRepeat][1]+=1 curpos+=deletionsize alignmentpos+=deletionsize else: print "unknown Op in CIGAR:"+str(block) #print "strandofinterest:"+str(strandofinterest)+'\n' #Handle single point mutations from MD tag insertsByRead.append(insertcountforread) delsByRead.append(delcountforread) mutation_pos=identifySNPs(cigarlist,strandofinterest); #offset each mutation position by the start of the alignment if len(ref_snp_bases)!=len(mutation_pos): print "MD does not agree with MN for strand:\n" print str(strand)+'\n' print "using conservative estimate for SNPs\n" for i in range(min(len(mutation_pos),len(ref_snp_bases))): ref_base=(ref_snp_bases[i]).lower() snp_pos=int(mutation_pos[i][0])+startpos seq_base=(mutation_pos[i][1]).lower() if snp_pos not in mutCount: mutCount[snp_pos]=1 else: mutCount[snp_pos]+=1 if ref_base not in mutType: mutType[ref_base]=dict() if seq_base not in mutType[ref_base]: mutType[ref_base][seq_base]=1 else: mutType[ref_base][seq_base]+=1 #generate the output files fout=open(outputprefix+"posCount.csv",'w') for entry in posCount: fout.write(str(entry)+','+str(posCount[entry])+'\n') fout=open(outputprefix+"delCount.csv",'w') for entry in delCount: fout.write(str(entry)+','+str(delCount[entry])+'\n') fout=open(outputprefix+"insertCount.csv",'w') for entry in insertCount: fout.write(str(entry)+','+str(insertCount[entry])+'\n') fout=open(outputprefix+"mutationCount.csv","w") for entry in mutCount: fout.write(str(entry)+','+str(mutCount[entry])+'\n') fout=open(outputprefix+"mutationType.csv","w") for entry in mutType: fout.write(entry) for subentry in mutType[entry]: fout.write(','+subentry+','+str(mutType[entry][subentry])) fout.write('\n') fout=open(outputprefix+"insertsByRead.csv","w") if len(insertsByRead)>0: fout.write(str(insertsByRead[0])) for i in range(1,len(insertsByRead)): fout.write(','+str(insertsByRead[i])) fout=open(outputprefix+"delsByRead.csv","w") if len(delsByRead) > 0: fout.write(str(delsByRead[0])) for i in range(1,len(delsByRead)): fout.write(','+str(delsByRead[i])) fout=open(outputprefix+"insertSize.csv","w") for entry in insertSize: fout.write(str(entry)+","+str(insertSize[entry][0])+','+str(insertSize[entry][1])+'\n') fout=open(outputprefix+"delSize.csv","w") for entry in delSize: fout.write(str(entry)+','+str(delSize[entry][0])+","+str(delSize[entry][1])+'\n') fout=open(outputprefix+"Usage.csv","w") for entry in Usage: fout.write(str(entry)+","+str(Usage[entry][0])+','+str(Usage[entry][1])+','+str(Usage[entry][2])+','+str(Usage[entry][3])+'\n') #write a summary file fout=open(outputprefix+"summary.csv",'w') fout.write(outputprefix+"posCount.csv\n") fout.write(outputprefix+"delCount.csv\n") fout.write(outputprefix+"insertCount.csv\n") fout.write(outputprefix+"mutationCount.csv\n") fout.write(outputprefix+"mutationType.csv\n") fout.write(outputprefix+"insertSize.csv\n") fout.write(outputprefix+"delSize.csv\n") fout.write(outputprefix+"Usage.csv\n") fout.write(outputprefix+"readHist.csv\n") fout.write(outputprefix+"qualHist.csv\n") fout.write(outputprefix+"insertsByRead.csv\n") fout.write(outputprefix+"delsByRead.csv\n") #if plotting is enabled, generate plots of the characterization statistics if plothistogram: #insertion probability from insertion count. insertprob=dict() for entry in posCount: if entry in insertCount: insertprob[entry]=float(insertCount[entry])/float(posCount[entry]) plotname=outputprefix+"CharacterizationInsertCount.png" fig=plt.figure(); ax=fig.add_subplot(111) ax.bar(insertprob.keys(), insertprob.values(),width=1,color='r',log=True) ax.set_ylim([10e-5,1]) plt.setp(ax.get_xticklabels(),fontsize=18) plt.setp(ax.get_yticklabels(),fontsize=18) ax.set_xlabel('Base Position Along a Read',fontsize=20) ax.set_ylabel('Probability of Insertion',fontsize=20) ax.set_title('Probability of Insertion as a Function of Base Position\n Dataset '+ str(sys.argv[1].split('/')[-1]),fontsize=20) plt.grid(True) plt.savefig(plotname,bbox_inches=0) #plot the insertion size distribution plotname=outputprefix+"CharacterizationInsertSize.png" fig=plt.figure() ax=fig.add_subplot(111) barwidth=0.2 overallSize=[i - barwidth for i in insertSize.keys()] overallCount=[i[0] for i in insertSize.values()] repeatCount=[i[1] for i in insertSize.values()] bar1=ax.bar(overallSize,overallCount,width=barwidth,color='b',align='center',log=True,label='Total Insertions') bar2=ax.bar(insertSize.keys(),repeatCount,width=barwidth,color='r',align='center',log=True,label='Repeat Insertions') ax.legend(loc=1,borderaxespad=0) plt.setp(ax.get_xticklabels(),fontsize=18) plt.setp(ax.get_yticklabels(),fontsize=18) ax.set_xlabel('Insert Size',fontsize=20) ax.set_ylabel('Insert Count',fontsize=20) ax.set_title('Insertion Size \n Dataset '+sys.argv[1].split('/')[-1],fontsize=20) plt.grid(True) plt.savefig(plotname,bbox_inches=0) #deletion probability from deletion count. delprob=dict() for entry in posCount: if entry in delCount: delprob[entry]=float(delCount[entry])/float(posCount[entry]) plotname=outputprefix+"CharacterizationDelCount.png" fig = plt.figure() ax=fig.add_subplot(111) ax.bar(delprob.keys(), delprob.values(),color='r',log=True) ax.set_ylim([10e-5,1]) plt.setp(ax.get_xticklabels(),fontsize=18) plt.setp(ax.get_yticklabels(),fontsize=18) ax.set_xlabel('Base Position Along a Read',fontsize=20) ax.set_ylabel('Probability of Deletion',fontsize=20) ax.set_title('Probability of Deletion as a Function of Base Position\n Dataset '+ str(sys.argv[1].split('/')[-1]),fontsize=20) plt.grid(True) plt.savefig(plotname,bbox_inches=0) #plot the deletion size distribution plotname=outputprefix+"CharacterizationDelSize.png" fig=plt.figure() ax=fig.add_subplot(111) ax.set_yscale('log') overallSize=[i - 0.1 for i in delSize.keys()] overallCount=[i[0] for i in delSize.values()] repeatCount=[i[1] for i in delSize.values()] bar1=ax.bar(overallSize,overallCount,width=0.2,color='b',align='center',label='Total Deletions',log=True) bar2=ax.bar(delSize.keys(),repeatCount,width=0.2,color='r',align='center',label='Repeat Deletions',log=True) ax.legend(loc=1,borderaxespad=0) plt.setp(ax.get_xticklabels(),fontsize=18) plt.setp(ax.get_yticklabels(),fontsize=18) ax.set_xlabel('Deletion Size',fontsize=20) ax.set_ylabel('Deletion Count',fontsize=20) ax.set_title('Deletion Size \n Dataset'+str(sys.argv[1].split('/')[-1]),fontsize=20) plt.grid(True) plt.savefig(plotname,bbox_inches=0) #plot the probability from mutation count mutprob=dict() for entry in posCount: if entry in mutCount: mutprob[entry]=float(mutCount[entry])/posCount[entry] plotname=outputprefix+"CharacterizationMutationCount.png" fig = plt.figure() ax=fig.add_subplot(111) ax.bar(mutprob.keys(), mutprob.values(),color='r',log=True) ax.set_ylim([10e-5,1]) plt.setp(ax.get_xticklabels(),fontsize=18) plt.setp(ax.get_yticklabels(),fontsize=18) ax.set_xlabel('Base Position Along a Read',fontsize=20) ax.set_ylabel('Probability of Mutation',fontsize=20) ax.set_title('Probability of Mutation as a Function of Base Position\n Dataset '+ str(sys.argv[1].split('/')[-1]),fontsize=20) plt.grid(True) plt.savefig(plotname,bbox_inches=0) #plot mutation type histogram mtp=dict() bases=['a','t','c','g','n'] for b1 in bases: mtp[b1]=dict() for b2 in bases: if b1==b2: continue mtp[b1][b2]=0 for key in mutType: totalmuts=float(sum(mutType[key].values())) for subkey in mutType[key]: mtp[key][subkey]=mutType[key][subkey]/max(0.01,totalmuts) mutationDF=DataFrame([[0,mtp['a']['t'],mtp['a']['c'],mtp['a']['g'],mtp['a']['n']],[mtp['t']['a'],0,mtp['t']['c'],mtp['t']['g'],mtp['t']['n']],[mtp['c']['a'],mtp['c']['t'],0,mtp['c']['g'],mtp['c']['n']],[mtp['g']['a'],mtp['g']['t'],mtp['g']['c'],0,mtp['g']['n']],[mtp['n']['a'],mtp['n']['t'],mtp['n']['c'],mtp['n']['g'],0]],columns=['A','T','C','G','N']) plotname=outputprefix+'CharacterizationMutType.png' mutplot=mutationDF.plot(kind='bar',stacked=True) group_labels=['A','T','C','G','N'] mutplot.set_xticklabels(group_labels) mutplot.set_ylim([0,1]) plt.setp(mutplot.get_xticklabels(),fontsize=18) plt.setp(mutplot.get_yticklabels(),fontsize=18) mutplot.set_xlabel('Original Base',fontsize=20) mutplot.set_ylabel('Mutated Base',fontsize=20) mutplot.set_title('Probability of Mutation by Base\n Dataset '+str(sys.argv[1].split('/')[-1]),fontsize=20) plt.grid(True) plt.savefig(plotname,bbox_inches=0) #plot read usage readUsageDist=[] for entry in Usage: totalReadLength=Usage[entry][0] usedReadLength=Usage[entry][1] fractionUsed=float(usedReadLength)/float(totalReadLength) readUsageDist.append(fractionUsed) plotname=outputprefix+'CharacterizationUsage.png' fig=plt.figure() ax = fig.add_subplot(111) ax.set_yscale('log') ax.set_xlim([0,1]) ax.hist(readUsageDist,100,histtype='bar',color='r') plt.setp(ax.get_xticklabels(),fontsize=18) plt.setp(ax.get_yticklabels(),fontsize=18) ax.set_xlabel('Fraction of Aligned Bases in Read',fontsize=20) ax.set_ylabel('Number of Reads',fontsize=20) ax.set_title('Read Alignment Quality \n Dataset '+str(sys.argv[1].split('/')[-1]),fontsize=20) plt.grid(True) plt.savefig(plotname,bbox_inches=0)
gpl-3.0
ffmmjj/desafio-dados-2016
data_preparation_pipeline/outliers_separation.py
1
1185
import luigi import pandas as pd from augment_data import AppendFeaturesAggregatedFromTeachersDatasetToSchool class SplitSchoolOutliersData(luigi.Task): input_task = AppendFeaturesAggregatedFromTeachersDatasetToSchool() def requires(self): return self.input_task def output(self): return {'average': luigi.LocalTarget('./dados/2013/TS_ESCOLA_average.csv'), 'outstanding': luigi.LocalTarget('./dados/2013/TS_ESCOLA_outstanding.csv')} def run(self): with self.input_task.output().open('r') as fp: escolas_pd = pd.read_csv(fp) escolas_statistics = escolas_pd['MEDIA_9EF_MT'].describe() math_avg, math_std = escolas_statistics.values[1], escolas_statistics.values[2] above_two_std_schools_indices = escolas_pd['MEDIA_9EF_MT'] > (math_avg + 2*math_std) below_two_std_schools_indices = escolas_pd['MEDIA_9EF_MT'] < (math_avg + 2*math_std) with self.output()['average'].open('w') as fp: escolas_pd[below_two_std_schools_indices].to_csv(fp) with self.output()['outstanding'].open('w') as fp: escolas_pd[above_two_std_schools_indices].to_csv(fp)
apache-2.0
nakul02/systemml
src/main/python/systemml/classloader.py
4
7952
#------------------------------------------------------------- # # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. # #------------------------------------------------------------- __all__ = ['createJavaObject', 'jvm_stdout', 'default_jvm_stdout', 'default_jvm_stdout_parallel_flush', 'set_default_jvm_stdout', 'get_spark_context' ] import os import numpy as np import pandas as pd import threading, time try: import py4j.java_gateway from py4j.java_gateway import JavaObject from pyspark import SparkContext from pyspark.sql import SparkSession except ImportError: raise ImportError('Unable to import `pyspark`. Hint: Make sure you are running with PySpark.') _loadedSystemML = False def get_spark_context(): """ Internal method to get already initialized SparkContext. Developers should always use get_spark_context() instead of SparkContext._active_spark_context to ensure SystemML loaded. Returns ------- sc: SparkContext SparkContext """ if SparkContext._active_spark_context is not None: sc = SparkContext._active_spark_context global _loadedSystemML if not _loadedSystemML: createJavaObject(sc, 'dummy') _loadedSystemML = True return sc else: raise Exception('Expected spark context to be created.') _in_jvm_stdout = False default_jvm_stdout = True default_jvm_stdout_parallel_flush = True def set_default_jvm_stdout(enable, parallel_flush=True): """ This is useful utility method to get the output of the driver JVM from within a Jupyter notebook Parameters ---------- enable: boolean Should flush the stdout by default when mlcontext.execute is invoked parallel_flush: boolean Should flush the stdout in parallel """ global default_jvm_stdout, default_jvm_stdout_parallel_flush default_jvm_stdout = enable default_jvm_stdout_parallel_flush = parallel_flush # This is useful utility class to get the output of the driver JVM from within a Jupyter notebook # Example usage: # with jvm_stdout(): # ml.execute(script) class jvm_stdout(object): """ This is useful utility class to get the output of the driver JVM from within a Jupyter notebook Parameters ---------- parallel_flush: boolean Should flush the stdout in parallel """ def __init__(self, parallel_flush=False): self.util = get_spark_context()._jvm.org.apache.sysml.api.ml.Utils() self.parallel_flush = parallel_flush self.t = threading.Thread(target=self.flush_stdout) self.stop = False def flush_stdout(self): while not self.stop: time.sleep(1) # flush stdout every 1 second str = self.util.flushStdOut() if str != '': str = str[:-1] if str.endswith('\n') else str print(str) def __enter__(self): global _in_jvm_stdout if _in_jvm_stdout: # Allow for nested jvm_stdout self.donotRedirect = True else: self.donotRedirect = False self.util.startRedirectStdOut() if self.parallel_flush: self.t.start() _in_jvm_stdout = True def __exit__(self, *args): global _in_jvm_stdout if not self.donotRedirect: if self.parallel_flush: self.stop = True self.t.join() print(self.util.stopRedirectStdOut()) _in_jvm_stdout = False _initializedSparkSession = False def _createJavaObject(sc, obj_type): # ----------------------------------------------------------------------------------- # Avoids race condition between locking of metastore_db of Scala SparkSession and PySpark SparkSession. # This is done at toDF() rather than import level to avoid creation of SparkSession in worker processes. global _initializedSparkSession if not _initializedSparkSession: _initializedSparkSession = True SparkSession.builder.getOrCreate().createDataFrame(pd.DataFrame(np.array([[1,2],[3,4]]))) # ----------------------------------------------------------------------------------- if obj_type == 'mlcontext': return sc._jvm.org.apache.sysml.api.mlcontext.MLContext(sc._jsc) elif obj_type == 'dummy': return sc._jvm.org.apache.sysml.utils.SystemMLLoaderUtils() else: raise ValueError('Incorrect usage: supported values: mlcontext or dummy') def _getJarFileNames(sc): import imp, fnmatch jar_file_name = '_ignore.jar' java_dir = os.path.join(imp.find_module("systemml")[1], "systemml-java") jar_file_names = [] for file in os.listdir(java_dir): if fnmatch.fnmatch(file, 'systemml-*-SNAPSHOT.jar') or fnmatch.fnmatch(file, 'systemml-*.jar'): jar_file_names = jar_file_names + [ os.path.join(java_dir, file) ] return jar_file_names def _getLoaderInstance(sc, jar_file_name, className, hint): err_msg = 'Unable to load systemml-*.jar into current pyspark session.' if os.path.isfile(jar_file_name): sc._jsc.addJar(jar_file_name) jar_file_url = sc._jvm.java.io.File(jar_file_name).toURI().toURL() url_class = sc._jvm.java.net.URL jar_file_url_arr = sc._gateway.new_array(url_class, 1) jar_file_url_arr[0] = jar_file_url url_class_loader = sc._jvm.java.net.URLClassLoader(jar_file_url_arr, sc._jsc.getClass().getClassLoader()) c1 = sc._jvm.java.lang.Class.forName(className, True, url_class_loader) return c1.newInstance() else: raise ImportError(err_msg + ' Hint: Download the jar from http://systemml.apache.org/download and ' + hint ) def createJavaObject(sc, obj_type): """ Performs appropriate check if SystemML.jar is available and returns the handle to MLContext object on JVM Parameters ---------- sc: SparkContext SparkContext obj_type: Type of object to create ('mlcontext' or 'dummy') """ try: return _createJavaObject(sc, obj_type) except (py4j.protocol.Py4JError, TypeError): ret = None err_msg = 'Unable to load systemml-*.jar into current pyspark session.' hint = 'Provide the following argument to pyspark: --driver-class-path ' jar_file_names = _getJarFileNames(sc) if len(jar_file_names) != 2: raise ImportError('Expected only systemml and systemml-extra jars, but found ' + str(jar_file_names)) for jar_file_name in jar_file_names: if 'extra' in jar_file_name: x = _getLoaderInstance(sc, jar_file_name, 'org.apache.sysml.api.dl.Caffe2DMLLoader', hint + 'systemml-*-extra.jar') x.loadCaffe2DML(jar_file_name) else: x = _getLoaderInstance(sc, jar_file_name, 'org.apache.sysml.utils.SystemMLLoaderUtils', hint + 'systemml-*.jar') x.loadSystemML(jar_file_name) try: ret = _createJavaObject(sc, obj_type) except (py4j.protocol.Py4JError, TypeError): raise ImportError(err_msg + ' Hint: ' + hint + jar_file_name) return ret
apache-2.0
nelango/ViralityAnalysis
model/lib/sklearn/gaussian_process/gaussian_process.py
78
34552
# -*- coding: utf-8 -*- # Author: Vincent Dubourg <vincent.dubourg@gmail.com> # (mostly translation, see implementation details) # Licence: BSD 3 clause from __future__ import print_function import numpy as np from scipy import linalg, optimize from ..base import BaseEstimator, RegressorMixin from ..metrics.pairwise import manhattan_distances from ..utils import check_random_state, check_array, check_X_y from ..utils.validation import check_is_fitted from . import regression_models as regression from . import correlation_models as correlation MACHINE_EPSILON = np.finfo(np.double).eps def l1_cross_distances(X): """ Computes the nonzero componentwise L1 cross-distances between the vectors in X. Parameters ---------- X: array_like An array with shape (n_samples, n_features) Returns ------- D: array with shape (n_samples * (n_samples - 1) / 2, n_features) The array of componentwise L1 cross-distances. ij: arrays with shape (n_samples * (n_samples - 1) / 2, 2) The indices i and j of the vectors in X associated to the cross- distances in D: D[k] = np.abs(X[ij[k, 0]] - Y[ij[k, 1]]). """ X = check_array(X) n_samples, n_features = X.shape n_nonzero_cross_dist = n_samples * (n_samples - 1) // 2 ij = np.zeros((n_nonzero_cross_dist, 2), dtype=np.int) D = np.zeros((n_nonzero_cross_dist, n_features)) ll_1 = 0 for k in range(n_samples - 1): ll_0 = ll_1 ll_1 = ll_0 + n_samples - k - 1 ij[ll_0:ll_1, 0] = k ij[ll_0:ll_1, 1] = np.arange(k + 1, n_samples) D[ll_0:ll_1] = np.abs(X[k] - X[(k + 1):n_samples]) return D, ij class GaussianProcess(BaseEstimator, RegressorMixin): """The Gaussian Process model class. Read more in the :ref:`User Guide <gaussian_process>`. Parameters ---------- regr : string or callable, optional A regression function returning an array of outputs of the linear regression functional basis. The number of observations n_samples should be greater than the size p of this basis. Default assumes a simple constant regression trend. Available built-in regression models are:: 'constant', 'linear', 'quadratic' corr : string or callable, optional A stationary autocorrelation function returning the autocorrelation between two points x and x'. Default assumes a squared-exponential autocorrelation model. Built-in correlation models are:: 'absolute_exponential', 'squared_exponential', 'generalized_exponential', 'cubic', 'linear' beta0 : double array_like, optional The regression weight vector to perform Ordinary Kriging (OK). Default assumes Universal Kriging (UK) so that the vector beta of regression weights is estimated using the maximum likelihood principle. storage_mode : string, optional A string specifying whether the Cholesky decomposition of the correlation matrix should be stored in the class (storage_mode = 'full') or not (storage_mode = 'light'). Default assumes storage_mode = 'full', so that the Cholesky decomposition of the correlation matrix is stored. This might be a useful parameter when one is not interested in the MSE and only plan to estimate the BLUP, for which the correlation matrix is not required. verbose : boolean, optional A boolean specifying the verbose level. Default is verbose = False. theta0 : double array_like, optional An array with shape (n_features, ) or (1, ). The parameters in the autocorrelation model. If thetaL and thetaU are also specified, theta0 is considered as the starting point for the maximum likelihood estimation of the best set of parameters. Default assumes isotropic autocorrelation model with theta0 = 1e-1. thetaL : double array_like, optional An array with shape matching theta0's. Lower bound on the autocorrelation parameters for maximum likelihood estimation. Default is None, so that it skips maximum likelihood estimation and it uses theta0. thetaU : double array_like, optional An array with shape matching theta0's. Upper bound on the autocorrelation parameters for maximum likelihood estimation. Default is None, so that it skips maximum likelihood estimation and it uses theta0. normalize : boolean, optional Input X and observations y are centered and reduced wrt means and standard deviations estimated from the n_samples observations provided. Default is normalize = True so that data is normalized to ease maximum likelihood estimation. nugget : double or ndarray, optional Introduce a nugget effect to allow smooth predictions from noisy data. If nugget is an ndarray, it must be the same length as the number of data points used for the fit. The nugget is added to the diagonal of the assumed training covariance; in this way it acts as a Tikhonov regularization in the problem. In the special case of the squared exponential correlation function, the nugget mathematically represents the variance of the input values. Default assumes a nugget close to machine precision for the sake of robustness (nugget = 10. * MACHINE_EPSILON). optimizer : string, optional A string specifying the optimization algorithm to be used. Default uses 'fmin_cobyla' algorithm from scipy.optimize. Available optimizers are:: 'fmin_cobyla', 'Welch' 'Welch' optimizer is dued to Welch et al., see reference [WBSWM1992]_. It consists in iterating over several one-dimensional optimizations instead of running one single multi-dimensional optimization. random_start : int, optional The number of times the Maximum Likelihood Estimation should be performed from a random starting point. The first MLE always uses the specified starting point (theta0), the next starting points are picked at random according to an exponential distribution (log-uniform on [thetaL, thetaU]). Default does not use random starting point (random_start = 1). random_state: integer or numpy.RandomState, optional The generator used to shuffle the sequence of coordinates of theta in the Welch optimizer. If an integer is given, it fixes the seed. Defaults to the global numpy random number generator. Attributes ---------- theta_ : array Specified theta OR the best set of autocorrelation parameters (the \ sought maximizer of the reduced likelihood function). reduced_likelihood_function_value_ : array The optimal reduced likelihood function value. Examples -------- >>> import numpy as np >>> from sklearn.gaussian_process import GaussianProcess >>> X = np.array([[1., 3., 5., 6., 7., 8.]]).T >>> y = (X * np.sin(X)).ravel() >>> gp = GaussianProcess(theta0=0.1, thetaL=.001, thetaU=1.) >>> gp.fit(X, y) # doctest: +ELLIPSIS GaussianProcess(beta0=None... ... Notes ----- The presentation implementation is based on a translation of the DACE Matlab toolbox, see reference [NLNS2002]_. References ---------- .. [NLNS2002] `H.B. Nielsen, S.N. Lophaven, H. B. Nielsen and J. Sondergaard. DACE - A MATLAB Kriging Toolbox.` (2002) http://www2.imm.dtu.dk/~hbn/dace/dace.pdf .. [WBSWM1992] `W.J. Welch, R.J. Buck, J. Sacks, H.P. Wynn, T.J. Mitchell, and M.D. Morris (1992). Screening, predicting, and computer experiments. Technometrics, 34(1) 15--25.` http://www.jstor.org/pss/1269548 """ _regression_types = { 'constant': regression.constant, 'linear': regression.linear, 'quadratic': regression.quadratic} _correlation_types = { 'absolute_exponential': correlation.absolute_exponential, 'squared_exponential': correlation.squared_exponential, 'generalized_exponential': correlation.generalized_exponential, 'cubic': correlation.cubic, 'linear': correlation.linear} _optimizer_types = [ 'fmin_cobyla', 'Welch'] def __init__(self, regr='constant', corr='squared_exponential', beta0=None, storage_mode='full', verbose=False, theta0=1e-1, thetaL=None, thetaU=None, optimizer='fmin_cobyla', random_start=1, normalize=True, nugget=10. * MACHINE_EPSILON, random_state=None): self.regr = regr self.corr = corr self.beta0 = beta0 self.storage_mode = storage_mode self.verbose = verbose self.theta0 = theta0 self.thetaL = thetaL self.thetaU = thetaU self.normalize = normalize self.nugget = nugget self.optimizer = optimizer self.random_start = random_start self.random_state = random_state def fit(self, X, y): """ The Gaussian Process model fitting method. Parameters ---------- X : double array_like An array with shape (n_samples, n_features) with the input at which observations were made. y : double array_like An array with shape (n_samples, ) or shape (n_samples, n_targets) with the observations of the output to be predicted. Returns ------- gp : self A fitted Gaussian Process model object awaiting data to perform predictions. """ # Run input checks self._check_params() self.random_state = check_random_state(self.random_state) # Force data to 2D numpy.array X, y = check_X_y(X, y, multi_output=True, y_numeric=True) self.y_ndim_ = y.ndim if y.ndim == 1: y = y[:, np.newaxis] # Check shapes of DOE & observations n_samples, n_features = X.shape _, n_targets = y.shape # Run input checks self._check_params(n_samples) # Normalize data or don't if self.normalize: X_mean = np.mean(X, axis=0) X_std = np.std(X, axis=0) y_mean = np.mean(y, axis=0) y_std = np.std(y, axis=0) X_std[X_std == 0.] = 1. y_std[y_std == 0.] = 1. # center and scale X if necessary X = (X - X_mean) / X_std y = (y - y_mean) / y_std else: X_mean = np.zeros(1) X_std = np.ones(1) y_mean = np.zeros(1) y_std = np.ones(1) # Calculate matrix of distances D between samples D, ij = l1_cross_distances(X) if (np.min(np.sum(D, axis=1)) == 0. and self.corr != correlation.pure_nugget): raise Exception("Multiple input features cannot have the same" " target value.") # Regression matrix and parameters F = self.regr(X) n_samples_F = F.shape[0] if F.ndim > 1: p = F.shape[1] else: p = 1 if n_samples_F != n_samples: raise Exception("Number of rows in F and X do not match. Most " "likely something is going wrong with the " "regression model.") if p > n_samples_F: raise Exception(("Ordinary least squares problem is undetermined " "n_samples=%d must be greater than the " "regression model size p=%d.") % (n_samples, p)) if self.beta0 is not None: if self.beta0.shape[0] != p: raise Exception("Shapes of beta0 and F do not match.") # Set attributes self.X = X self.y = y self.D = D self.ij = ij self.F = F self.X_mean, self.X_std = X_mean, X_std self.y_mean, self.y_std = y_mean, y_std # Determine Gaussian Process model parameters if self.thetaL is not None and self.thetaU is not None: # Maximum Likelihood Estimation of the parameters if self.verbose: print("Performing Maximum Likelihood Estimation of the " "autocorrelation parameters...") self.theta_, self.reduced_likelihood_function_value_, par = \ self._arg_max_reduced_likelihood_function() if np.isinf(self.reduced_likelihood_function_value_): raise Exception("Bad parameter region. " "Try increasing upper bound") else: # Given parameters if self.verbose: print("Given autocorrelation parameters. " "Computing Gaussian Process model parameters...") self.theta_ = self.theta0 self.reduced_likelihood_function_value_, par = \ self.reduced_likelihood_function() if np.isinf(self.reduced_likelihood_function_value_): raise Exception("Bad point. Try increasing theta0.") self.beta = par['beta'] self.gamma = par['gamma'] self.sigma2 = par['sigma2'] self.C = par['C'] self.Ft = par['Ft'] self.G = par['G'] if self.storage_mode == 'light': # Delete heavy data (it will be computed again if required) # (it is required only when MSE is wanted in self.predict) if self.verbose: print("Light storage mode specified. " "Flushing autocorrelation matrix...") self.D = None self.ij = None self.F = None self.C = None self.Ft = None self.G = None return self def predict(self, X, eval_MSE=False, batch_size=None): """ This function evaluates the Gaussian Process model at x. Parameters ---------- X : array_like An array with shape (n_eval, n_features) giving the point(s) at which the prediction(s) should be made. eval_MSE : boolean, optional A boolean specifying whether the Mean Squared Error should be evaluated or not. Default assumes evalMSE = False and evaluates only the BLUP (mean prediction). batch_size : integer, optional An integer giving the maximum number of points that can be evaluated simultaneously (depending on the available memory). Default is None so that all given points are evaluated at the same time. Returns ------- y : array_like, shape (n_samples, ) or (n_samples, n_targets) An array with shape (n_eval, ) if the Gaussian Process was trained on an array of shape (n_samples, ) or an array with shape (n_eval, n_targets) if the Gaussian Process was trained on an array of shape (n_samples, n_targets) with the Best Linear Unbiased Prediction at x. MSE : array_like, optional (if eval_MSE == True) An array with shape (n_eval, ) or (n_eval, n_targets) as with y, with the Mean Squared Error at x. """ check_is_fitted(self, "X") # Check input shapes X = check_array(X) n_eval, _ = X.shape n_samples, n_features = self.X.shape n_samples_y, n_targets = self.y.shape # Run input checks self._check_params(n_samples) if X.shape[1] != n_features: raise ValueError(("The number of features in X (X.shape[1] = %d) " "should match the number of features used " "for fit() " "which is %d.") % (X.shape[1], n_features)) if batch_size is None: # No memory management # (evaluates all given points in a single batch run) # Normalize input X = (X - self.X_mean) / self.X_std # Initialize output y = np.zeros(n_eval) if eval_MSE: MSE = np.zeros(n_eval) # Get pairwise componentwise L1-distances to the input training set dx = manhattan_distances(X, Y=self.X, sum_over_features=False) # Get regression function and correlation f = self.regr(X) r = self.corr(self.theta_, dx).reshape(n_eval, n_samples) # Scaled predictor y_ = np.dot(f, self.beta) + np.dot(r, self.gamma) # Predictor y = (self.y_mean + self.y_std * y_).reshape(n_eval, n_targets) if self.y_ndim_ == 1: y = y.ravel() # Mean Squared Error if eval_MSE: C = self.C if C is None: # Light storage mode (need to recompute C, F, Ft and G) if self.verbose: print("This GaussianProcess used 'light' storage mode " "at instantiation. Need to recompute " "autocorrelation matrix...") reduced_likelihood_function_value, par = \ self.reduced_likelihood_function() self.C = par['C'] self.Ft = par['Ft'] self.G = par['G'] rt = linalg.solve_triangular(self.C, r.T, lower=True) if self.beta0 is None: # Universal Kriging u = linalg.solve_triangular(self.G.T, np.dot(self.Ft.T, rt) - f.T, lower=True) else: # Ordinary Kriging u = np.zeros((n_targets, n_eval)) MSE = np.dot(self.sigma2.reshape(n_targets, 1), (1. - (rt ** 2.).sum(axis=0) + (u ** 2.).sum(axis=0))[np.newaxis, :]) MSE = np.sqrt((MSE ** 2.).sum(axis=0) / n_targets) # Mean Squared Error might be slightly negative depending on # machine precision: force to zero! MSE[MSE < 0.] = 0. if self.y_ndim_ == 1: MSE = MSE.ravel() return y, MSE else: return y else: # Memory management if type(batch_size) is not int or batch_size <= 0: raise Exception("batch_size must be a positive integer") if eval_MSE: y, MSE = np.zeros(n_eval), np.zeros(n_eval) for k in range(max(1, n_eval / batch_size)): batch_from = k * batch_size batch_to = min([(k + 1) * batch_size + 1, n_eval + 1]) y[batch_from:batch_to], MSE[batch_from:batch_to] = \ self.predict(X[batch_from:batch_to], eval_MSE=eval_MSE, batch_size=None) return y, MSE else: y = np.zeros(n_eval) for k in range(max(1, n_eval / batch_size)): batch_from = k * batch_size batch_to = min([(k + 1) * batch_size + 1, n_eval + 1]) y[batch_from:batch_to] = \ self.predict(X[batch_from:batch_to], eval_MSE=eval_MSE, batch_size=None) return y def reduced_likelihood_function(self, theta=None): """ This function determines the BLUP parameters and evaluates the reduced likelihood function for the given autocorrelation parameters theta. Maximizing this function wrt the autocorrelation parameters theta is equivalent to maximizing the likelihood of the assumed joint Gaussian distribution of the observations y evaluated onto the design of experiments X. Parameters ---------- theta : array_like, optional An array containing the autocorrelation parameters at which the Gaussian Process model parameters should be determined. Default uses the built-in autocorrelation parameters (ie ``theta = self.theta_``). Returns ------- reduced_likelihood_function_value : double The value of the reduced likelihood function associated to the given autocorrelation parameters theta. par : dict A dictionary containing the requested Gaussian Process model parameters: sigma2 Gaussian Process variance. beta Generalized least-squares regression weights for Universal Kriging or given beta0 for Ordinary Kriging. gamma Gaussian Process weights. C Cholesky decomposition of the correlation matrix [R]. Ft Solution of the linear equation system : [R] x Ft = F G QR decomposition of the matrix Ft. """ check_is_fitted(self, "X") if theta is None: # Use built-in autocorrelation parameters theta = self.theta_ # Initialize output reduced_likelihood_function_value = - np.inf par = {} # Retrieve data n_samples = self.X.shape[0] D = self.D ij = self.ij F = self.F if D is None: # Light storage mode (need to recompute D, ij and F) D, ij = l1_cross_distances(self.X) if (np.min(np.sum(D, axis=1)) == 0. and self.corr != correlation.pure_nugget): raise Exception("Multiple X are not allowed") F = self.regr(self.X) # Set up R r = self.corr(theta, D) R = np.eye(n_samples) * (1. + self.nugget) R[ij[:, 0], ij[:, 1]] = r R[ij[:, 1], ij[:, 0]] = r # Cholesky decomposition of R try: C = linalg.cholesky(R, lower=True) except linalg.LinAlgError: return reduced_likelihood_function_value, par # Get generalized least squares solution Ft = linalg.solve_triangular(C, F, lower=True) try: Q, G = linalg.qr(Ft, econ=True) except: #/usr/lib/python2.6/dist-packages/scipy/linalg/decomp.py:1177: # DeprecationWarning: qr econ argument will be removed after scipy # 0.7. The economy transform will then be available through the # mode='economic' argument. Q, G = linalg.qr(Ft, mode='economic') pass sv = linalg.svd(G, compute_uv=False) rcondG = sv[-1] / sv[0] if rcondG < 1e-10: # Check F sv = linalg.svd(F, compute_uv=False) condF = sv[0] / sv[-1] if condF > 1e15: raise Exception("F is too ill conditioned. Poor combination " "of regression model and observations.") else: # Ft is too ill conditioned, get out (try different theta) return reduced_likelihood_function_value, par Yt = linalg.solve_triangular(C, self.y, lower=True) if self.beta0 is None: # Universal Kriging beta = linalg.solve_triangular(G, np.dot(Q.T, Yt)) else: # Ordinary Kriging beta = np.array(self.beta0) rho = Yt - np.dot(Ft, beta) sigma2 = (rho ** 2.).sum(axis=0) / n_samples # The determinant of R is equal to the squared product of the diagonal # elements of its Cholesky decomposition C detR = (np.diag(C) ** (2. / n_samples)).prod() # Compute/Organize output reduced_likelihood_function_value = - sigma2.sum() * detR par['sigma2'] = sigma2 * self.y_std ** 2. par['beta'] = beta par['gamma'] = linalg.solve_triangular(C.T, rho) par['C'] = C par['Ft'] = Ft par['G'] = G return reduced_likelihood_function_value, par def _arg_max_reduced_likelihood_function(self): """ This function estimates the autocorrelation parameters theta as the maximizer of the reduced likelihood function. (Minimization of the opposite reduced likelihood function is used for convenience) Parameters ---------- self : All parameters are stored in the Gaussian Process model object. Returns ------- optimal_theta : array_like The best set of autocorrelation parameters (the sought maximizer of the reduced likelihood function). optimal_reduced_likelihood_function_value : double The optimal reduced likelihood function value. optimal_par : dict The BLUP parameters associated to thetaOpt. """ # Initialize output best_optimal_theta = [] best_optimal_rlf_value = [] best_optimal_par = [] if self.verbose: print("The chosen optimizer is: " + str(self.optimizer)) if self.random_start > 1: print(str(self.random_start) + " random starts are required.") percent_completed = 0. # Force optimizer to fmin_cobyla if the model is meant to be isotropic if self.optimizer == 'Welch' and self.theta0.size == 1: self.optimizer = 'fmin_cobyla' if self.optimizer == 'fmin_cobyla': def minus_reduced_likelihood_function(log10t): return - self.reduced_likelihood_function( theta=10. ** log10t)[0] constraints = [] for i in range(self.theta0.size): constraints.append(lambda log10t, i=i: log10t[i] - np.log10(self.thetaL[0, i])) constraints.append(lambda log10t, i=i: np.log10(self.thetaU[0, i]) - log10t[i]) for k in range(self.random_start): if k == 0: # Use specified starting point as first guess theta0 = self.theta0 else: # Generate a random starting point log10-uniformly # distributed between bounds log10theta0 = (np.log10(self.thetaL) + self.random_state.rand(*self.theta0.shape) * np.log10(self.thetaU / self.thetaL)) theta0 = 10. ** log10theta0 # Run Cobyla try: log10_optimal_theta = \ optimize.fmin_cobyla(minus_reduced_likelihood_function, np.log10(theta0).ravel(), constraints, iprint=0) except ValueError as ve: print("Optimization failed. Try increasing the ``nugget``") raise ve optimal_theta = 10. ** log10_optimal_theta optimal_rlf_value, optimal_par = \ self.reduced_likelihood_function(theta=optimal_theta) # Compare the new optimizer to the best previous one if k > 0: if optimal_rlf_value > best_optimal_rlf_value: best_optimal_rlf_value = optimal_rlf_value best_optimal_par = optimal_par best_optimal_theta = optimal_theta else: best_optimal_rlf_value = optimal_rlf_value best_optimal_par = optimal_par best_optimal_theta = optimal_theta if self.verbose and self.random_start > 1: if (20 * k) / self.random_start > percent_completed: percent_completed = (20 * k) / self.random_start print("%s completed" % (5 * percent_completed)) optimal_rlf_value = best_optimal_rlf_value optimal_par = best_optimal_par optimal_theta = best_optimal_theta elif self.optimizer == 'Welch': # Backup of the given atrributes theta0, thetaL, thetaU = self.theta0, self.thetaL, self.thetaU corr = self.corr verbose = self.verbose # This will iterate over fmin_cobyla optimizer self.optimizer = 'fmin_cobyla' self.verbose = False # Initialize under isotropy assumption if verbose: print("Initialize under isotropy assumption...") self.theta0 = check_array(self.theta0.min()) self.thetaL = check_array(self.thetaL.min()) self.thetaU = check_array(self.thetaU.max()) theta_iso, optimal_rlf_value_iso, par_iso = \ self._arg_max_reduced_likelihood_function() optimal_theta = theta_iso + np.zeros(theta0.shape) # Iterate over all dimensions of theta allowing for anisotropy if verbose: print("Now improving allowing for anisotropy...") for i in self.random_state.permutation(theta0.size): if verbose: print("Proceeding along dimension %d..." % (i + 1)) self.theta0 = check_array(theta_iso) self.thetaL = check_array(thetaL[0, i]) self.thetaU = check_array(thetaU[0, i]) def corr_cut(t, d): return corr(check_array(np.hstack([optimal_theta[0][0:i], t[0], optimal_theta[0][(i + 1)::]])), d) self.corr = corr_cut optimal_theta[0, i], optimal_rlf_value, optimal_par = \ self._arg_max_reduced_likelihood_function() # Restore the given atrributes self.theta0, self.thetaL, self.thetaU = theta0, thetaL, thetaU self.corr = corr self.optimizer = 'Welch' self.verbose = verbose else: raise NotImplementedError("This optimizer ('%s') is not " "implemented yet. Please contribute!" % self.optimizer) return optimal_theta, optimal_rlf_value, optimal_par def _check_params(self, n_samples=None): # Check regression model if not callable(self.regr): if self.regr in self._regression_types: self.regr = self._regression_types[self.regr] else: raise ValueError("regr should be one of %s or callable, " "%s was given." % (self._regression_types.keys(), self.regr)) # Check regression weights if given (Ordinary Kriging) if self.beta0 is not None: self.beta0 = np.atleast_2d(self.beta0) if self.beta0.shape[1] != 1: # Force to column vector self.beta0 = self.beta0.T # Check correlation model if not callable(self.corr): if self.corr in self._correlation_types: self.corr = self._correlation_types[self.corr] else: raise ValueError("corr should be one of %s or callable, " "%s was given." % (self._correlation_types.keys(), self.corr)) # Check storage mode if self.storage_mode != 'full' and self.storage_mode != 'light': raise ValueError("Storage mode should either be 'full' or " "'light', %s was given." % self.storage_mode) # Check correlation parameters self.theta0 = np.atleast_2d(self.theta0) lth = self.theta0.size if self.thetaL is not None and self.thetaU is not None: self.thetaL = np.atleast_2d(self.thetaL) self.thetaU = np.atleast_2d(self.thetaU) if self.thetaL.size != lth or self.thetaU.size != lth: raise ValueError("theta0, thetaL and thetaU must have the " "same length.") if np.any(self.thetaL <= 0) or np.any(self.thetaU < self.thetaL): raise ValueError("The bounds must satisfy O < thetaL <= " "thetaU.") elif self.thetaL is None and self.thetaU is None: if np.any(self.theta0 <= 0): raise ValueError("theta0 must be strictly positive.") elif self.thetaL is None or self.thetaU is None: raise ValueError("thetaL and thetaU should either be both or " "neither specified.") # Force verbose type to bool self.verbose = bool(self.verbose) # Force normalize type to bool self.normalize = bool(self.normalize) # Check nugget value self.nugget = np.asarray(self.nugget) if np.any(self.nugget) < 0.: raise ValueError("nugget must be positive or zero.") if (n_samples is not None and self.nugget.shape not in [(), (n_samples,)]): raise ValueError("nugget must be either a scalar " "or array of length n_samples.") # Check optimizer if self.optimizer not in self._optimizer_types: raise ValueError("optimizer should be one of %s" % self._optimizer_types) # Force random_start type to int self.random_start = int(self.random_start)
mit
daodaoliang/neural-network-animation
matplotlib/tests/test_table.py
10
2083
from __future__ import (absolute_import, division, print_function, unicode_literals) import six import matplotlib.pyplot as plt import numpy as np from matplotlib.testing.decorators import image_comparison @image_comparison(baseline_images=['table_zorder'], extensions=['png'], remove_text=True) def test_zorder(): data = [[66386, 174296], [58230, 381139]] colLabels = ('Freeze', 'Wind') rowLabels = ['%d year' % x for x in (100, 50)] cellText = [] yoff = np.array([0.0] * len(colLabels)) for row in reversed(data): yoff += row cellText.append(['%1.1f' % (x/1000.0) for x in yoff]) t = np.linspace(0, 2*np.pi, 100) plt.plot(t, np.cos(t), lw=4, zorder=2) plt.table(cellText=cellText, rowLabels=rowLabels, colLabels=colLabels, loc='center', zorder=-2, ) plt.table(cellText=cellText, rowLabels=rowLabels, colLabels=colLabels, loc='upper center', zorder=4, ) plt.yticks([]) @image_comparison(baseline_images=['table_labels'], extensions=['png']) def test_label_colours(): dim = 3 c = np.linspace(0, 1, dim) colours = plt.cm.RdYlGn(c) cellText = [['1'] * dim] * dim fig = plt.figure() ax1 = fig.add_subplot(4, 1, 1) ax1.axis('off') ax1.table(cellText=cellText, rowColours=colours, loc='best') ax2 = fig.add_subplot(4, 1, 2) ax2.axis('off') ax2.table(cellText=cellText, rowColours=colours, rowLabels=['Header'] * dim, loc='best') ax3 = fig.add_subplot(4, 1, 3) ax3.axis('off') ax3.table(cellText=cellText, colColours=colours, loc='best') ax4 = fig.add_subplot(4, 1, 4) ax4.axis('off') ax4.table(cellText=cellText, colColours=colours, colLabels=['Header'] * dim, loc='best')
mit
phvu/DDF
python/tests/test_ml.py
3
1794
from __future__ import unicode_literals import unittest import pandas as pd from py4j.java_gateway import Py4JJavaError import test_base from ddf import ml class TestMl(test_base.BaseTest): """ Test ML functions """ def testKmeans(self): model = ml.kmeans(self.mtcars, 2, 5, 10) self.assertIsInstance(model, ml.KMeansModel) self.assertIsInstance(model.centers, pd.DataFrame) self.assertEqual(len(model.centers), 2) self.assertItemsEqual(model.centers.columns.tolist(), self.mtcars.colnames) self.assertIsInstance(model.predict(range(0, self.mtcars.ncol)), float) with self.assertRaises(Py4JJavaError): model.predict([0, 1, 2]) def testLinearRegression(self): model = ml.linear_regression_gd(self.mtcars, 0.1, 0.1, 10) self.assertIsInstance(model, ml.LinearRegressionModel) self.assertIsInstance(model.weights, pd.DataFrame) self.assertEqual(len(model.weights), 1) self.assertEqual(len(model.weights.columns), self.mtcars.ncol) self.assertIsInstance(model.predict(range(0, self.mtcars.ncol - 1)), float) with self.assertRaises(Py4JJavaError): model.predict([0, 1, 2]) def testLogisticRegression(self): model = ml.logistic_regression_gd(self.mtcars, 0.1, 10) self.assertIsInstance(model, ml.LogisticRegressionModel) self.assertIsInstance(model.weights, pd.DataFrame) self.assertEqual(len(model.weights), 1) self.assertEqual(len(model.weights.columns), self.mtcars.ncol) self.assertIsInstance(model.predict(range(0, self.mtcars.ncol - 1)), float) with self.assertRaises(Py4JJavaError): model.predict([0, 1, 2]) if __name__ == '__main__': unittest.main()
apache-2.0
rbiswas4/SNsims
snsims_previous/snsims/tmp/models.py
1
2804
#!/usr/bin/env python import sncosmo.models import numpy class SEDFileSource(sncosmo.models.TimeSeriesSource): """A TimeSeriesSource stored in a 3-column ASCII file format, for PHASE, LAMBDA, and F_LAMBDA. The hash symbol # is a comment line. The spectral flux density of this model is given by .. math:: F(t, \lambda) = A \\times M(t, \lambda) where _M_ is the flux defined on a grid in phase and wavelength and _A_ (amplitude) is the single free parameter of the model. It should be noted that while t and \lambda are in the rest frame of the object, the flux density is defined at redshift zero. This means that for objects with the same intrinsic luminosity, the amplitude will be smaller for objects at larger luminosity distances. Parameters ---------- filename : str Name of the filename that contains the Time Series zero_before : bool, optional If True, flux at phases before minimum phase will be zeroed. The default is False, in which case the flux at such phases will be equal to the flux at the minimum phase (``flux[0, :]`` in the input array). version : str, optional Version of the model. Default is `None`. Returns ------- `~sncosmo.TimeSeriesSource` instance representing the TimeSeriesSource in file """ _param_names = ['amplitude'] param_names_latex = ['A'] def __init__(self, filename, zero_before=False, version=None): phase, wave, flux = numpy.loadtxt(filename, unpack=True) # Convert 3 column format to that expected by TimeSeriesSource phase_u = numpy.unique(phase) wave_u = numpy.unique(wave) lenp = len(phase_u) lenw = len(wave_u) if lenp * lenw != len(flux): raise TypeError('File is not a TimeSeriesSource') i = numpy.zeros(len(flux), dtype='int') j = numpy.zeros(len(flux), dtype='int') for index, p in enumerate(phase_u): i[phase == p] = index for index, w in enumerate(wave_u): j[wave == w] = index flux = flux[i * lenw + j] flux = numpy.reshape(flux, (lenp, lenw)) super(SEDFileSource, self).__init__(phase_u, wave_u, flux, zero_before=False, name=filename, version=None) if __name__ == '__main__': # filename = '/Users/akim/project/SNDATA_ROOT/snsed/NON1A/SDSS-019323.SED' # data = SEDFileSource(filename) sn = sncosmo.Model(source='snana-2007nc') print sn.param_names # wefwe import matplotlib.pyplot as plt plt.plot(data._wave, data.flux(0, data._wave)) plt.plot(sn.source._wave, sn.flux(0, sn.source._wave) * 0.95) plt.show()
mit
jereze/scikit-learn
sklearn/utils/fixes.py
39
13318
"""Compatibility fixes for older version of python, numpy and scipy If you add content to this file, please give the version of the package at which the fixe is no longer needed. """ # Authors: Emmanuelle Gouillart <emmanuelle.gouillart@normalesup.org> # Gael Varoquaux <gael.varoquaux@normalesup.org> # Fabian Pedregosa <fpedregosa@acm.org> # Lars Buitinck # # License: BSD 3 clause import warnings import sys import functools import os import errno import numpy as np import scipy.sparse as sp import scipy try: from inspect import signature except ImportError: from ..externals.funcsigs import signature def _parse_version(version_string): version = [] for x in version_string.split('.'): try: version.append(int(x)) except ValueError: # x may be of the form dev-1ea1592 version.append(x) return tuple(version) np_version = _parse_version(np.__version__) sp_version = _parse_version(scipy.__version__) try: from scipy.special import expit # SciPy >= 0.10 with np.errstate(invalid='ignore', over='ignore'): if np.isnan(expit(1000)): # SciPy < 0.14 raise ImportError("no stable expit in scipy.special") except ImportError: def expit(x, out=None): """Logistic sigmoid function, ``1 / (1 + exp(-x))``. See sklearn.utils.extmath.log_logistic for the log of this function. """ if out is None: out = np.empty(np.atleast_1d(x).shape, dtype=np.float64) out[:] = x # 1 / (1 + exp(-x)) = (1 + tanh(x / 2)) / 2 # This way of computing the logistic is both fast and stable. out *= .5 np.tanh(out, out) out += 1 out *= .5 return out.reshape(np.shape(x)) # little danse to see if np.copy has an 'order' keyword argument if 'order' in signature(np.copy).parameters: def safe_copy(X): # Copy, but keep the order return np.copy(X, order='K') else: # Before an 'order' argument was introduced, numpy wouldn't muck with # the ordering safe_copy = np.copy try: if (not np.allclose(np.divide(.4, 1, casting="unsafe"), np.divide(.4, 1, casting="unsafe", dtype=np.float)) or not np.allclose(np.divide(.4, 1), .4)): raise TypeError('Divide not working with dtype: ' 'https://github.com/numpy/numpy/issues/3484') divide = np.divide except TypeError: # Compat for old versions of np.divide that do not provide support for # the dtype args def divide(x1, x2, out=None, dtype=None): out_orig = out if out is None: out = np.asarray(x1, dtype=dtype) if out is x1: out = x1.copy() else: if out is not x1: out[:] = x1 if dtype is not None and out.dtype != dtype: out = out.astype(dtype) out /= x2 if out_orig is None and np.isscalar(x1): out = np.asscalar(out) return out try: np.array(5).astype(float, copy=False) except TypeError: # Compat where astype accepted no copy argument def astype(array, dtype, copy=True): if not copy and array.dtype == dtype: return array return array.astype(dtype) else: astype = np.ndarray.astype try: with warnings.catch_warnings(record=True): # Don't raise the numpy deprecation warnings that appear in # 1.9, but avoid Python bug due to simplefilter('ignore') warnings.simplefilter('always') sp.csr_matrix([1.0, 2.0, 3.0]).max(axis=0) except (TypeError, AttributeError): # in scipy < 14.0, sparse matrix min/max doesn't accept an `axis` argument # the following code is taken from the scipy 0.14 codebase def _minor_reduce(X, ufunc): major_index = np.flatnonzero(np.diff(X.indptr)) if X.data.size == 0 and major_index.size == 0: # Numpy < 1.8.0 don't handle empty arrays in reduceat value = np.zeros_like(X.data) else: value = ufunc.reduceat(X.data, X.indptr[major_index]) return major_index, value def _min_or_max_axis(X, axis, min_or_max): N = X.shape[axis] if N == 0: raise ValueError("zero-size array to reduction operation") M = X.shape[1 - axis] mat = X.tocsc() if axis == 0 else X.tocsr() mat.sum_duplicates() major_index, value = _minor_reduce(mat, min_or_max) not_full = np.diff(mat.indptr)[major_index] < N value[not_full] = min_or_max(value[not_full], 0) mask = value != 0 major_index = np.compress(mask, major_index) value = np.compress(mask, value) from scipy.sparse import coo_matrix if axis == 0: res = coo_matrix((value, (np.zeros(len(value)), major_index)), dtype=X.dtype, shape=(1, M)) else: res = coo_matrix((value, (major_index, np.zeros(len(value)))), dtype=X.dtype, shape=(M, 1)) return res.A.ravel() def _sparse_min_or_max(X, axis, min_or_max): if axis is None: if 0 in X.shape: raise ValueError("zero-size array to reduction operation") zero = X.dtype.type(0) if X.nnz == 0: return zero m = min_or_max.reduce(X.data.ravel()) if X.nnz != np.product(X.shape): m = min_or_max(zero, m) return m if axis < 0: axis += 2 if (axis == 0) or (axis == 1): return _min_or_max_axis(X, axis, min_or_max) else: raise ValueError("invalid axis, use 0 for rows, or 1 for columns") def sparse_min_max(X, axis): return (_sparse_min_or_max(X, axis, np.minimum), _sparse_min_or_max(X, axis, np.maximum)) else: def sparse_min_max(X, axis): return (X.min(axis=axis).toarray().ravel(), X.max(axis=axis).toarray().ravel()) try: from numpy import argpartition except ImportError: # numpy.argpartition was introduced in v 1.8.0 def argpartition(a, kth, axis=-1, kind='introselect', order=None): return np.argsort(a, axis=axis, order=order) try: from itertools import combinations_with_replacement except ImportError: # Backport of itertools.combinations_with_replacement for Python 2.6, # from Python 3.4 documentation (http://tinyurl.com/comb-w-r), copyright # Python Software Foundation (https://docs.python.org/3/license.html) def combinations_with_replacement(iterable, r): # combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC pool = tuple(iterable) n = len(pool) if not n and r: return indices = [0] * r yield tuple(pool[i] for i in indices) while True: for i in reversed(range(r)): if indices[i] != n - 1: break else: return indices[i:] = [indices[i] + 1] * (r - i) yield tuple(pool[i] for i in indices) try: from numpy import isclose except ImportError: def isclose(a, b, rtol=1.e-5, atol=1.e-8, equal_nan=False): """ Returns a boolean array where two arrays are element-wise equal within a tolerance. This function was added to numpy v1.7.0, and the version you are running has been backported from numpy v1.8.1. See its documentation for more details. """ def within_tol(x, y, atol, rtol): with np.errstate(invalid='ignore'): result = np.less_equal(abs(x - y), atol + rtol * abs(y)) if np.isscalar(a) and np.isscalar(b): result = bool(result) return result x = np.array(a, copy=False, subok=True, ndmin=1) y = np.array(b, copy=False, subok=True, ndmin=1) xfin = np.isfinite(x) yfin = np.isfinite(y) if all(xfin) and all(yfin): return within_tol(x, y, atol, rtol) else: finite = xfin & yfin cond = np.zeros_like(finite, subok=True) # Since we're using boolean indexing, x & y must be the same shape. # Ideally, we'd just do x, y = broadcast_arrays(x, y). It's in # lib.stride_tricks, though, so we can't import it here. x = x * np.ones_like(cond) y = y * np.ones_like(cond) # Avoid subtraction with infinite/nan values... cond[finite] = within_tol(x[finite], y[finite], atol, rtol) # Check for equality of infinite values... cond[~finite] = (x[~finite] == y[~finite]) if equal_nan: # Make NaN == NaN cond[np.isnan(x) & np.isnan(y)] = True return cond if np_version < (1, 7): # Prior to 1.7.0, np.frombuffer wouldn't work for empty first arg. def frombuffer_empty(buf, dtype): if len(buf) == 0: return np.empty(0, dtype=dtype) else: return np.frombuffer(buf, dtype=dtype) else: frombuffer_empty = np.frombuffer if np_version < (1, 8): def in1d(ar1, ar2, assume_unique=False, invert=False): # Backport of numpy function in1d 1.8.1 to support numpy 1.6.2 # Ravel both arrays, behavior for the first array could be different ar1 = np.asarray(ar1).ravel() ar2 = np.asarray(ar2).ravel() # This code is significantly faster when the condition is satisfied. if len(ar2) < 10 * len(ar1) ** 0.145: if invert: mask = np.ones(len(ar1), dtype=np.bool) for a in ar2: mask &= (ar1 != a) else: mask = np.zeros(len(ar1), dtype=np.bool) for a in ar2: mask |= (ar1 == a) return mask # Otherwise use sorting if not assume_unique: ar1, rev_idx = np.unique(ar1, return_inverse=True) ar2 = np.unique(ar2) ar = np.concatenate((ar1, ar2)) # We need this to be a stable sort, so always use 'mergesort' # here. The values from the first array should always come before # the values from the second array. order = ar.argsort(kind='mergesort') sar = ar[order] if invert: bool_ar = (sar[1:] != sar[:-1]) else: bool_ar = (sar[1:] == sar[:-1]) flag = np.concatenate((bool_ar, [invert])) indx = order.argsort(kind='mergesort')[:len(ar1)] if assume_unique: return flag[indx] else: return flag[indx][rev_idx] else: from numpy import in1d if sp_version < (0, 15): # Backport fix for scikit-learn/scikit-learn#2986 / scipy/scipy#4142 from ._scipy_sparse_lsqr_backport import lsqr as sparse_lsqr else: from scipy.sparse.linalg import lsqr as sparse_lsqr if sys.version_info < (2, 7, 0): # partial cannot be pickled in Python 2.6 # http://bugs.python.org/issue1398 class partial(object): def __init__(self, func, *args, **keywords): functools.update_wrapper(self, func) self.func = func self.args = args self.keywords = keywords def __call__(self, *args, **keywords): args = self.args + args kwargs = self.keywords.copy() kwargs.update(keywords) return self.func(*args, **kwargs) else: from functools import partial if np_version < (1, 6, 2): # Allow bincount to accept empty arrays # https://github.com/numpy/numpy/commit/40f0844846a9d7665616b142407a3d74cb65a040 def bincount(x, weights=None, minlength=None): if len(x) > 0: return np.bincount(x, weights, minlength) else: if minlength is None: minlength = 0 minlength = np.asscalar(np.asarray(minlength, dtype=np.intp)) return np.zeros(minlength, dtype=np.intp) else: from numpy import bincount if 'exist_ok' in signature(os.makedirs).parameters: makedirs = os.makedirs else: def makedirs(name, mode=0o777, exist_ok=False): """makedirs(name [, mode=0o777][, exist_ok=False]) Super-mkdir; create a leaf directory and all intermediate ones. Works like mkdir, except that any intermediate path segment (not just the rightmost) will be created if it does not exist. If the target directory already exists, raise an OSError if exist_ok is False. Otherwise no exception is raised. This is recursive. """ try: os.makedirs(name, mode=mode) except OSError as e: if (not exist_ok or e.errno != errno.EEXIST or not os.path.isdir(name)): raise if np_version < (1, 8, 1): def array_equal(a1, a2): # copy-paste from numpy 1.8.1 try: a1, a2 = np.asarray(a1), np.asarray(a2) except: return False if a1.shape != a2.shape: return False return bool(np.asarray(a1 == a2).all()) else: from numpy import array_equal
bsd-3-clause
mattilyra/scikit-learn
benchmarks/bench_isotonic.py
38
3047
""" Benchmarks of isotonic regression performance. We generate a synthetic dataset of size 10^n, for n in [min, max], and examine the time taken to run isotonic regression over the dataset. The timings are then output to stdout, or visualized on a log-log scale with matplotlib. This allows the scaling of the algorithm with the problem size to be visualized and understood. """ from __future__ import print_function import numpy as np import gc from datetime import datetime from sklearn.isotonic import isotonic_regression from sklearn.utils.bench import total_seconds import matplotlib.pyplot as plt import argparse def generate_perturbed_logarithm_dataset(size): return np.random.randint(-50, 50, size=n) \ + 50. * np.log(1 + np.arange(n)) def generate_logistic_dataset(size): X = np.sort(np.random.normal(size=size)) return np.random.random(size=size) < 1.0 / (1.0 + np.exp(-X)) DATASET_GENERATORS = { 'perturbed_logarithm': generate_perturbed_logarithm_dataset, 'logistic': generate_logistic_dataset } def bench_isotonic_regression(Y): """ Runs a single iteration of isotonic regression on the input data, and reports the total time taken (in seconds). """ gc.collect() tstart = datetime.now() isotonic_regression(Y) delta = datetime.now() - tstart return total_seconds(delta) if __name__ == '__main__': parser = argparse.ArgumentParser( description="Isotonic Regression benchmark tool") parser.add_argument('--iterations', type=int, required=True, help="Number of iterations to average timings over " "for each problem size") parser.add_argument('--log_min_problem_size', type=int, required=True, help="Base 10 logarithm of the minimum problem size") parser.add_argument('--log_max_problem_size', type=int, required=True, help="Base 10 logarithm of the maximum problem size") parser.add_argument('--show_plot', action='store_true', help="Plot timing output with matplotlib") parser.add_argument('--dataset', choices=DATASET_GENERATORS.keys(), required=True) args = parser.parse_args() timings = [] for exponent in range(args.log_min_problem_size, args.log_max_problem_size): n = 10 ** exponent Y = DATASET_GENERATORS[args.dataset](n) time_per_iteration = \ [bench_isotonic_regression(Y) for i in range(args.iterations)] timing = (n, np.mean(time_per_iteration)) timings.append(timing) # If we're not plotting, dump the timing to stdout if not args.show_plot: print(n, np.mean(time_per_iteration)) if args.show_plot: plt.plot(*zip(*timings)) plt.title("Average time taken running isotonic regression") plt.xlabel('Number of observations') plt.ylabel('Time (s)') plt.axis('tight') plt.loglog() plt.show()
bsd-3-clause
laserson/ibis
docs/sphinxext/ipython_sphinxext/ipython_directive.py
9
37645
# -*- coding: utf-8 -*- """ Sphinx directive to support embedded IPython code. This directive allows pasting of entire interactive IPython sessions, prompts and all, and their code will actually get re-executed at doc build time, with all prompts renumbered sequentially. It also allows you to input code as a pure python input by giving the argument python to the directive. The output looks like an interactive ipython section. To enable this directive, simply list it in your Sphinx ``conf.py`` file (making sure the directory where you placed it is visible to sphinx, as is needed for all Sphinx directives). For example, to enable syntax highlighting and the IPython directive:: extensions = ['IPython.sphinxext.ipython_console_highlighting', 'IPython.sphinxext.ipython_directive'] The IPython directive outputs code-blocks with the language 'ipython'. So if you do not have the syntax highlighting extension enabled as well, then all rendered code-blocks will be uncolored. By default this directive assumes that your prompts are unchanged IPython ones, but this can be customized. The configurable options that can be placed in conf.py are: ipython_savefig_dir: The directory in which to save the figures. This is relative to the Sphinx source directory. The default is `html_static_path`. ipython_rgxin: The compiled regular expression to denote the start of IPython input lines. The default is re.compile('In \[(\d+)\]:\s?(.*)\s*'). You shouldn't need to change this. ipython_rgxout: The compiled regular expression to denote the start of IPython output lines. The default is re.compile('Out\[(\d+)\]:\s?(.*)\s*'). You shouldn't need to change this. ipython_promptin: The string to represent the IPython input prompt in the generated ReST. The default is 'In [%d]:'. This expects that the line numbers are used in the prompt. ipython_promptout: The string to represent the IPython prompt in the generated ReST. The default is 'Out [%d]:'. This expects that the line numbers are used in the prompt. ipython_mplbackend: The string which specifies if the embedded Sphinx shell should import Matplotlib and set the backend. The value specifies a backend that is passed to `matplotlib.use()` before any lines in `ipython_execlines` are executed. If not specified in conf.py, then the default value of 'agg' is used. To use the IPython directive without matplotlib as a dependency, set the value to `None`. It may end up that matplotlib is still imported if the user specifies so in `ipython_execlines` or makes use of the @savefig pseudo decorator. ipython_execlines: A list of strings to be exec'd in the embedded Sphinx shell. Typical usage is to make certain packages always available. Set this to an empty list if you wish to have no imports always available. If specified in conf.py as `None`, then it has the effect of making no imports available. If omitted from conf.py altogether, then the default value of ['import numpy as np', 'import matplotlib.pyplot as plt'] is used. ipython_holdcount When the @suppress pseudo-decorator is used, the execution count can be incremented or not. The default behavior is to hold the execution count, corresponding to a value of `True`. Set this to `False` to increment the execution count after each suppressed command. As an example, to use the IPython directive when `matplotlib` is not available, one sets the backend to `None`:: ipython_mplbackend = None An example usage of the directive is: .. code-block:: rst .. ipython:: In [1]: x = 1 In [2]: y = x**2 In [3]: print(y) See http://matplotlib.org/sampledoc/ipython_directive.html for additional documentation. ToDo ---- - Turn the ad-hoc test() function into a real test suite. - Break up ipython-specific functionality from matplotlib stuff into better separated code. Authors ------- - John D Hunter: orignal author. - Fernando Perez: refactoring, documentation, cleanups, port to 0.11. - VáclavŠmilauer <eudoxos-AT-arcig.cz>: Prompt generalizations. - Skipper Seabold, refactoring, cleanups, pure python addition """ from __future__ import print_function from __future__ import unicode_literals #----------------------------------------------------------------------------- # Imports #----------------------------------------------------------------------------- # Stdlib import os import re import sys import tempfile import ast from pandas.compat import zip, range, map, lmap, u, cStringIO as StringIO import warnings # To keep compatibility with various python versions try: from hashlib import md5 except ImportError: from md5 import md5 # Third-party import sphinx from docutils.parsers.rst import directives from docutils import nodes from sphinx.util.compat import Directive # Our own try: from traitlets.config import Config except ImportError: from IPython import Config from IPython import InteractiveShell from IPython.core.profiledir import ProfileDir from IPython.utils import io from IPython.utils.py3compat import PY3 if PY3: from io import StringIO text_type = str else: from StringIO import StringIO text_type = unicode #----------------------------------------------------------------------------- # Globals #----------------------------------------------------------------------------- # for tokenizing blocks COMMENT, INPUT, OUTPUT = range(3) #----------------------------------------------------------------------------- # Functions and class declarations #----------------------------------------------------------------------------- def block_parser(part, rgxin, rgxout, fmtin, fmtout): """ part is a string of ipython text, comprised of at most one input, one ouput, comments, and blank lines. The block parser parses the text into a list of:: blocks = [ (TOKEN0, data0), (TOKEN1, data1), ...] where TOKEN is one of [COMMENT | INPUT | OUTPUT ] and data is, depending on the type of token:: COMMENT : the comment string INPUT: the (DECORATOR, INPUT_LINE, REST) where DECORATOR: the input decorator (or None) INPUT_LINE: the input as string (possibly multi-line) REST : any stdout generated by the input line (not OUTPUT) OUTPUT: the output string, possibly multi-line """ block = [] lines = part.split('\n') N = len(lines) i = 0 decorator = None while 1: if i==N: # nothing left to parse -- the last line break line = lines[i] i += 1 line_stripped = line.strip() if line_stripped.startswith('#'): block.append((COMMENT, line)) continue if line_stripped.startswith('@'): # we're assuming at most one decorator -- may need to # rethink decorator = line_stripped continue # does this look like an input line? matchin = rgxin.match(line) if matchin: lineno, inputline = int(matchin.group(1)), matchin.group(2) # the ....: continuation string continuation = ' %s:'%''.join(['.']*(len(str(lineno))+2)) Nc = len(continuation) # input lines can continue on for more than one line, if # we have a '\' line continuation char or a function call # echo line 'print'. The input line can only be # terminated by the end of the block or an output line, so # we parse out the rest of the input line if it is # multiline as well as any echo text rest = [] while i<N: # look ahead; if the next line is blank, or a comment, or # an output line, we're done nextline = lines[i] matchout = rgxout.match(nextline) #print "nextline=%s, continuation=%s, starts=%s"%(nextline, continuation, nextline.startswith(continuation)) if matchout or nextline.startswith('#'): break elif nextline.startswith(continuation): nextline = nextline[Nc:] if nextline and nextline[0] == ' ': nextline = nextline[1:] inputline += '\n' + nextline else: rest.append(nextline) i+= 1 block.append((INPUT, (decorator, inputline, '\n'.join(rest)))) continue # if it looks like an output line grab all the text to the end # of the block matchout = rgxout.match(line) if matchout: lineno, output = int(matchout.group(1)), matchout.group(2) if i<N-1: output = '\n'.join([output] + lines[i:]) block.append((OUTPUT, output)) break return block class DecodingStringIO(StringIO, object): def __init__(self,buf='',encodings=('utf8',), *args, **kwds): super(DecodingStringIO, self).__init__(buf, *args, **kwds) self.set_encodings(encodings) def set_encodings(self, encodings): self.encodings = encodings def write(self,data): if isinstance(data, text_type): return super(DecodingStringIO, self).write(data) else: for enc in self.encodings: try: data = data.decode(enc) return super(DecodingStringIO, self).write(data) except : pass # default to brute utf8 if no encoding succeded return super(DecodingStringIO, self).write(data.decode('utf8', 'replace')) class EmbeddedSphinxShell(object): """An embedded IPython instance to run inside Sphinx""" def __init__(self, exec_lines=None,state=None): self.cout = DecodingStringIO(u'') if exec_lines is None: exec_lines = [] self.state = state # Create config object for IPython config = Config() config.InteractiveShell.autocall = False config.InteractiveShell.autoindent = False config.InteractiveShell.colors = 'NoColor' # create a profile so instance history isn't saved tmp_profile_dir = tempfile.mkdtemp(prefix='profile_') profname = 'auto_profile_sphinx_build' pdir = os.path.join(tmp_profile_dir,profname) profile = ProfileDir.create_profile_dir(pdir) # Create and initialize global ipython, but don't start its mainloop. # This will persist across different EmbededSphinxShell instances. IP = InteractiveShell.instance(config=config, profile_dir=profile) # io.stdout redirect must be done after instantiating InteractiveShell io.stdout = self.cout io.stderr = self.cout # For debugging, so we can see normal output, use this: #from IPython.utils.io import Tee #io.stdout = Tee(self.cout, channel='stdout') # dbg #io.stderr = Tee(self.cout, channel='stderr') # dbg # Store a few parts of IPython we'll need. self.IP = IP self.user_ns = self.IP.user_ns self.user_global_ns = self.IP.user_global_ns self.input = '' self.output = '' self.is_verbatim = False self.is_doctest = False self.is_suppress = False # Optionally, provide more detailed information to shell. self.directive = None # on the first call to the savefig decorator, we'll import # pyplot as plt so we can make a call to the plt.gcf().savefig self._pyplot_imported = False # Prepopulate the namespace. for line in exec_lines: self.process_input_line(line, store_history=False) def clear_cout(self): self.cout.seek(0) self.cout.truncate(0) def process_input_line(self, line, store_history=True): """process the input, capturing stdout""" stdout = sys.stdout splitter = self.IP.input_splitter try: sys.stdout = self.cout splitter.push(line) more = splitter.push_accepts_more() if not more: try: source_raw = splitter.source_raw_reset()[1] except: # recent ipython #4504 source_raw = splitter.raw_reset() self.IP.run_cell(source_raw, store_history=store_history) finally: sys.stdout = stdout def process_image(self, decorator): """ # build out an image directive like # .. image:: somefile.png # :width 4in # # from an input like # savefig somefile.png width=4in """ savefig_dir = self.savefig_dir source_dir = self.source_dir saveargs = decorator.split(' ') filename = saveargs[1] # insert relative path to image file in source outfile = os.path.relpath(os.path.join(savefig_dir,filename), source_dir) imagerows = ['.. image:: %s'%outfile] for kwarg in saveargs[2:]: arg, val = kwarg.split('=') arg = arg.strip() val = val.strip() imagerows.append(' :%s: %s'%(arg, val)) image_file = os.path.basename(outfile) # only return file name image_directive = '\n'.join(imagerows) return image_file, image_directive # Callbacks for each type of token def process_input(self, data, input_prompt, lineno): """ Process data block for INPUT token. """ decorator, input, rest = data image_file = None image_directive = None is_verbatim = decorator=='@verbatim' or self.is_verbatim is_doctest = (decorator is not None and \ decorator.startswith('@doctest')) or self.is_doctest is_suppress = decorator=='@suppress' or self.is_suppress is_okexcept = decorator=='@okexcept' or self.is_okexcept is_okwarning = decorator=='@okwarning' or self.is_okwarning is_savefig = decorator is not None and \ decorator.startswith('@savefig') # set the encodings to be used by DecodingStringIO # to convert the execution output into unicode if # needed. this attrib is set by IpythonDirective.run() # based on the specified block options, defaulting to ['ut self.cout.set_encodings(self.output_encoding) input_lines = input.split('\n') if len(input_lines) > 1: if input_lines[-1] != "": input_lines.append('') # make sure there's a blank line # so splitter buffer gets reset continuation = ' %s:'%''.join(['.']*(len(str(lineno))+2)) if is_savefig: image_file, image_directive = self.process_image(decorator) ret = [] is_semicolon = False # Hold the execution count, if requested to do so. if is_suppress and self.hold_count: store_history = False else: store_history = True # Note: catch_warnings is not thread safe with warnings.catch_warnings(record=True) as ws: for i, line in enumerate(input_lines): if line.endswith(';'): is_semicolon = True if i == 0: # process the first input line if is_verbatim: self.process_input_line('') self.IP.execution_count += 1 # increment it anyway else: # only submit the line in non-verbatim mode self.process_input_line(line, store_history=store_history) formatted_line = '%s %s'%(input_prompt, line) else: # process a continuation line if not is_verbatim: self.process_input_line(line, store_history=store_history) formatted_line = '%s %s'%(continuation, line) if not is_suppress: ret.append(formatted_line) if not is_suppress and len(rest.strip()) and is_verbatim: # the "rest" is the standard output of the # input, which needs to be added in # verbatim mode ret.append(rest) self.cout.seek(0) output = self.cout.read() if not is_suppress and not is_semicolon: ret.append(output) elif is_semicolon: # get spacing right ret.append('') # context information filename = self.state.document.current_source lineno = self.state.document.current_line # output any exceptions raised during execution to stdout # unless :okexcept: has been specified. if not is_okexcept and "Traceback" in output: s = "\nException in %s at block ending on line %s\n" % (filename, lineno) s += "Specify :okexcept: as an option in the ipython:: block to suppress this message\n" sys.stdout.write('\n\n>>>' + ('-' * 73)) sys.stdout.write(s) sys.stdout.write(output) sys.stdout.write('<<<' + ('-' * 73) + '\n\n') # output any warning raised during execution to stdout # unless :okwarning: has been specified. if not is_okwarning: for w in ws: s = "\nWarning in %s at block ending on line %s\n" % (filename, lineno) s += "Specify :okwarning: as an option in the ipython:: block to suppress this message\n" sys.stdout.write('\n\n>>>' + ('-' * 73)) sys.stdout.write(s) sys.stdout.write('-' * 76 + '\n') s=warnings.formatwarning(w.message, w.category, w.filename, w.lineno, w.line) sys.stdout.write(s) sys.stdout.write('<<<' + ('-' * 73) + '\n') self.cout.truncate(0) return (ret, input_lines, output, is_doctest, decorator, image_file, image_directive) def process_output(self, data, output_prompt, input_lines, output, is_doctest, decorator, image_file): """ Process data block for OUTPUT token. """ TAB = ' ' * 4 if is_doctest and output is not None: found = output found = found.strip() submitted = data.strip() if self.directive is None: source = 'Unavailable' content = 'Unavailable' else: source = self.directive.state.document.current_source content = self.directive.content # Add tabs and join into a single string. content = '\n'.join([TAB + line for line in content]) # Make sure the output contains the output prompt. ind = found.find(output_prompt) if ind < 0: e = ('output does not contain output prompt\n\n' 'Document source: {0}\n\n' 'Raw content: \n{1}\n\n' 'Input line(s):\n{TAB}{2}\n\n' 'Output line(s):\n{TAB}{3}\n\n') e = e.format(source, content, '\n'.join(input_lines), repr(found), TAB=TAB) raise RuntimeError(e) found = found[len(output_prompt):].strip() # Handle the actual doctest comparison. if decorator.strip() == '@doctest': # Standard doctest if found != submitted: e = ('doctest failure\n\n' 'Document source: {0}\n\n' 'Raw content: \n{1}\n\n' 'On input line(s):\n{TAB}{2}\n\n' 'we found output:\n{TAB}{3}\n\n' 'instead of the expected:\n{TAB}{4}\n\n') e = e.format(source, content, '\n'.join(input_lines), repr(found), repr(submitted), TAB=TAB) raise RuntimeError(e) else: self.custom_doctest(decorator, input_lines, found, submitted) def process_comment(self, data): """Process data fPblock for COMMENT token.""" if not self.is_suppress: return [data] def save_image(self, image_file): """ Saves the image file to disk. """ self.ensure_pyplot() command = ('plt.gcf().savefig("%s", bbox_inches="tight", ' 'dpi=100)' % image_file) #print 'SAVEFIG', command # dbg self.process_input_line('bookmark ipy_thisdir', store_history=False) self.process_input_line('cd -b ipy_savedir', store_history=False) self.process_input_line(command, store_history=False) self.process_input_line('cd -b ipy_thisdir', store_history=False) self.process_input_line('bookmark -d ipy_thisdir', store_history=False) self.clear_cout() def process_block(self, block): """ process block from the block_parser and return a list of processed lines """ ret = [] output = None input_lines = None lineno = self.IP.execution_count input_prompt = self.promptin % lineno output_prompt = self.promptout % lineno image_file = None image_directive = None for token, data in block: if token == COMMENT: out_data = self.process_comment(data) elif token == INPUT: (out_data, input_lines, output, is_doctest, decorator, image_file, image_directive) = \ self.process_input(data, input_prompt, lineno) elif token == OUTPUT: out_data = \ self.process_output(data, output_prompt, input_lines, output, is_doctest, decorator, image_file) if out_data: ret.extend(out_data) # save the image files if image_file is not None: self.save_image(image_file) return ret, image_directive def ensure_pyplot(self): """ Ensures that pyplot has been imported into the embedded IPython shell. Also, makes sure to set the backend appropriately if not set already. """ # We are here if the @figure pseudo decorator was used. Thus, it's # possible that we could be here even if python_mplbackend were set to # `None`. That's also strange and perhaps worthy of raising an # exception, but for now, we just set the backend to 'agg'. if not self._pyplot_imported: if 'matplotlib.backends' not in sys.modules: # Then ipython_matplotlib was set to None but there was a # call to the @figure decorator (and ipython_execlines did # not set a backend). #raise Exception("No backend was set, but @figure was used!") import matplotlib matplotlib.use('agg') # Always import pyplot into embedded shell. self.process_input_line('import matplotlib.pyplot as plt', store_history=False) self._pyplot_imported = True def process_pure_python(self, content): """ content is a list of strings. it is unedited directive content This runs it line by line in the InteractiveShell, prepends prompts as needed capturing stderr and stdout, then returns the content as a list as if it were ipython code """ output = [] savefig = False # keep up with this to clear figure multiline = False # to handle line continuation multiline_start = None fmtin = self.promptin ct = 0 for lineno, line in enumerate(content): line_stripped = line.strip() if not len(line): output.append(line) continue # handle decorators if line_stripped.startswith('@'): output.extend([line]) if 'savefig' in line: savefig = True # and need to clear figure continue # handle comments if line_stripped.startswith('#'): output.extend([line]) continue # deal with lines checking for multiline continuation = u' %s:'% ''.join(['.']*(len(str(ct))+2)) if not multiline: modified = u"%s %s" % (fmtin % ct, line_stripped) output.append(modified) ct += 1 try: ast.parse(line_stripped) output.append(u'') except Exception: # on a multiline multiline = True multiline_start = lineno else: # still on a multiline modified = u'%s %s' % (continuation, line) output.append(modified) # if the next line is indented, it should be part of multiline if len(content) > lineno + 1: nextline = content[lineno + 1] if len(nextline) - len(nextline.lstrip()) > 3: continue try: mod = ast.parse( '\n'.join(content[multiline_start:lineno+1])) if isinstance(mod.body[0], ast.FunctionDef): # check to see if we have the whole function for element in mod.body[0].body: if isinstance(element, ast.Return): multiline = False else: output.append(u'') multiline = False except Exception: pass if savefig: # clear figure if plotted self.ensure_pyplot() self.process_input_line('plt.clf()', store_history=False) self.clear_cout() savefig = False return output def custom_doctest(self, decorator, input_lines, found, submitted): """ Perform a specialized doctest. """ from .custom_doctests import doctests args = decorator.split() doctest_type = args[1] if doctest_type in doctests: doctests[doctest_type](self, args, input_lines, found, submitted) else: e = "Invalid option to @doctest: {0}".format(doctest_type) raise Exception(e) class IPythonDirective(Directive): has_content = True required_arguments = 0 optional_arguments = 4 # python, suppress, verbatim, doctest final_argumuent_whitespace = True option_spec = { 'python': directives.unchanged, 'suppress' : directives.flag, 'verbatim' : directives.flag, 'doctest' : directives.flag, 'okexcept': directives.flag, 'okwarning': directives.flag, 'output_encoding': directives.unchanged_required } shell = None seen_docs = set() def get_config_options(self): # contains sphinx configuration variables config = self.state.document.settings.env.config # get config variables to set figure output directory confdir = self.state.document.settings.env.app.confdir savefig_dir = config.ipython_savefig_dir source_dir = os.path.dirname(self.state.document.current_source) if savefig_dir is None: savefig_dir = config.html_static_path if isinstance(savefig_dir, list): savefig_dir = savefig_dir[0] # safe to assume only one path? savefig_dir = os.path.join(confdir, savefig_dir) # get regex and prompt stuff rgxin = config.ipython_rgxin rgxout = config.ipython_rgxout promptin = config.ipython_promptin promptout = config.ipython_promptout mplbackend = config.ipython_mplbackend exec_lines = config.ipython_execlines hold_count = config.ipython_holdcount return (savefig_dir, source_dir, rgxin, rgxout, promptin, promptout, mplbackend, exec_lines, hold_count) def setup(self): # Get configuration values. (savefig_dir, source_dir, rgxin, rgxout, promptin, promptout, mplbackend, exec_lines, hold_count) = self.get_config_options() if self.shell is None: # We will be here many times. However, when the # EmbeddedSphinxShell is created, its interactive shell member # is the same for each instance. if mplbackend: import matplotlib # Repeated calls to use() will not hurt us since `mplbackend` # is the same each time. matplotlib.use(mplbackend) # Must be called after (potentially) importing matplotlib and # setting its backend since exec_lines might import pylab. self.shell = EmbeddedSphinxShell(exec_lines, self.state) # Store IPython directive to enable better error messages self.shell.directive = self # reset the execution count if we haven't processed this doc #NOTE: this may be borked if there are multiple seen_doc tmp files #check time stamp? if not self.state.document.current_source in self.seen_docs: self.shell.IP.history_manager.reset() self.shell.IP.execution_count = 1 self.shell.IP.prompt_manager.width = 0 self.seen_docs.add(self.state.document.current_source) # and attach to shell so we don't have to pass them around self.shell.rgxin = rgxin self.shell.rgxout = rgxout self.shell.promptin = promptin self.shell.promptout = promptout self.shell.savefig_dir = savefig_dir self.shell.source_dir = source_dir self.shell.hold_count = hold_count # setup bookmark for saving figures directory self.shell.process_input_line('bookmark ipy_savedir %s'%savefig_dir, store_history=False) self.shell.clear_cout() return rgxin, rgxout, promptin, promptout def teardown(self): # delete last bookmark self.shell.process_input_line('bookmark -d ipy_savedir', store_history=False) self.shell.clear_cout() def run(self): debug = False #TODO, any reason block_parser can't be a method of embeddable shell # then we wouldn't have to carry these around rgxin, rgxout, promptin, promptout = self.setup() options = self.options self.shell.is_suppress = 'suppress' in options self.shell.is_doctest = 'doctest' in options self.shell.is_verbatim = 'verbatim' in options self.shell.is_okexcept = 'okexcept' in options self.shell.is_okwarning = 'okwarning' in options self.shell.output_encoding = [options.get('output_encoding', 'utf8')] # handle pure python code if 'python' in self.arguments: content = self.content self.content = self.shell.process_pure_python(content) parts = '\n'.join(self.content).split('\n\n') lines = ['.. code-block:: ipython', ''] figures = [] for part in parts: block = block_parser(part, rgxin, rgxout, promptin, promptout) if len(block): rows, figure = self.shell.process_block(block) for row in rows: lines.extend([' %s'%line for line in row.split('\n')]) if figure is not None: figures.append(figure) for figure in figures: lines.append('') lines.extend(figure.split('\n')) lines.append('') if len(lines)>2: if debug: print('\n'.join(lines)) else: # This has to do with input, not output. But if we comment # these lines out, then no IPython code will appear in the # final output. self.state_machine.insert_input( lines, self.state_machine.input_lines.source(0)) # cleanup self.teardown() return [] # Enable as a proper Sphinx directive def setup(app): setup.app = app app.add_directive('ipython', IPythonDirective) app.add_config_value('ipython_savefig_dir', None, 'env') app.add_config_value('ipython_rgxin', re.compile('In \[(\d+)\]:\s?(.*)\s*'), 'env') app.add_config_value('ipython_rgxout', re.compile('Out\[(\d+)\]:\s?(.*)\s*'), 'env') app.add_config_value('ipython_promptin', 'In [%d]:', 'env') app.add_config_value('ipython_promptout', 'Out[%d]:', 'env') # We could just let matplotlib pick whatever is specified as the default # backend in the matplotlibrc file, but this would cause issues if the # backend didn't work in headless environments. For this reason, 'agg' # is a good default backend choice. app.add_config_value('ipython_mplbackend', 'agg', 'env') # If the user sets this config value to `None`, then EmbeddedSphinxShell's # __init__ method will treat it as []. execlines = ['import numpy as np', 'import matplotlib.pyplot as plt'] app.add_config_value('ipython_execlines', execlines, 'env') app.add_config_value('ipython_holdcount', True, 'env') # Simple smoke test, needs to be converted to a proper automatic test. def test(): examples = [ r""" In [9]: pwd Out[9]: '/home/jdhunter/py4science/book' In [10]: cd bookdata/ /home/jdhunter/py4science/book/bookdata In [2]: from pylab import * In [2]: ion() In [3]: im = imread('stinkbug.png') @savefig mystinkbug.png width=4in In [4]: imshow(im) Out[4]: <matplotlib.image.AxesImage object at 0x39ea850> """, r""" In [1]: x = 'hello world' # string methods can be # used to alter the string @doctest In [2]: x.upper() Out[2]: 'HELLO WORLD' @verbatim In [3]: x.st<TAB> x.startswith x.strip """, r""" In [130]: url = 'http://ichart.finance.yahoo.com/table.csv?s=CROX\ .....: &d=9&e=22&f=2009&g=d&a=1&br=8&c=2006&ignore=.csv' In [131]: print url.split('&') ['http://ichart.finance.yahoo.com/table.csv?s=CROX', 'd=9', 'e=22', 'f=2009', 'g=d', 'a=1', 'b=8', 'c=2006', 'ignore=.csv'] In [60]: import urllib """, r"""\ In [133]: import numpy.random @suppress In [134]: numpy.random.seed(2358) @doctest In [135]: numpy.random.rand(10,2) Out[135]: array([[ 0.64524308, 0.59943846], [ 0.47102322, 0.8715456 ], [ 0.29370834, 0.74776844], [ 0.99539577, 0.1313423 ], [ 0.16250302, 0.21103583], [ 0.81626524, 0.1312433 ], [ 0.67338089, 0.72302393], [ 0.7566368 , 0.07033696], [ 0.22591016, 0.77731835], [ 0.0072729 , 0.34273127]]) """, r""" In [106]: print x jdh In [109]: for i in range(10): .....: print i .....: .....: 0 1 2 3 4 5 6 7 8 9 """, r""" In [144]: from pylab import * In [145]: ion() # use a semicolon to suppress the output @savefig test_hist.png width=4in In [151]: hist(np.random.randn(10000), 100); @savefig test_plot.png width=4in In [151]: plot(np.random.randn(10000), 'o'); """, r""" # use a semicolon to suppress the output In [151]: plt.clf() @savefig plot_simple.png width=4in In [151]: plot([1,2,3]) @savefig hist_simple.png width=4in In [151]: hist(np.random.randn(10000), 100); """, r""" # update the current fig In [151]: ylabel('number') In [152]: title('normal distribution') @savefig hist_with_text.png In [153]: grid(True) @doctest float In [154]: 0.1 + 0.2 Out[154]: 0.3 @doctest float In [155]: np.arange(16).reshape(4,4) Out[155]: array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]]) In [1]: x = np.arange(16, dtype=float).reshape(4,4) In [2]: x[0,0] = np.inf In [3]: x[0,1] = np.nan @doctest float In [4]: x Out[4]: array([[ inf, nan, 2., 3.], [ 4., 5., 6., 7.], [ 8., 9., 10., 11.], [ 12., 13., 14., 15.]]) """, ] # skip local-file depending first example: examples = examples[1:] #ipython_directive.DEBUG = True # dbg #options = dict(suppress=True) # dbg options = dict() for example in examples: content = example.split('\n') IPythonDirective('debug', arguments=None, options=options, content=content, lineno=0, content_offset=None, block_text=None, state=None, state_machine=None, ) # Run test suite as a script if __name__=='__main__': if not os.path.isdir('_static'): os.mkdir('_static') test() print('All OK? Check figures in _static/')
apache-2.0
mjsauvinen/P4UL
pyRaster/tif2NumpyTile.py
1
1956
#!/usr/bin/env python3 import sys import argparse import numpy as np from mapTools import * from utilities import filesFromList, writeLog from plotTools import addImagePlot import matplotlib.pyplot as plt ''' Author: Mikko Auvinen mikko.auvinen@helsinki.fi University of Helsinki & Finnish Meteorological Institute ''' #==========================================================# parser = argparse.ArgumentParser(prog='tif2NumpyTile.py') parser.add_argument("-f", "--filename",type=str, help="Input tif-image file name.") parser.add_argument("-fo", "--fileout",type=str, help="Output npz file name.") parser.add_argument("-r", "--reso",type=float, help="Resolution of the tif-image.") parser.add_argument("-xo", "--xorig",type=float, nargs=2,default=[0.,0.],\ help="Coords [N,E] of the tif-images top-left corner. Default=[0,0]") parser.add_argument("-p", "--printOn", help="Print the numpy array data.",\ action="store_true", default=False) parser.add_argument("-pp", "--printOnly", help="Only print the numpy array data. Don't save.",\ action="store_true", default=False) parser.add_argument("-s", "--scale",type=float, default=1.,\ help="Scale factor for the output. Default=1.") args = parser.parse_args() writeLog( parser, args, args.printOnly ) #==========================================================# # Renaming, nothing more. filename = args.filename fileout = args.fileout reso = args.reso ROrig = args.xorig printOn = args.printOn printOnly = args.printOnly sc = args.scale R = openTifAsNumpy(filename) dPx = np.array([sc*reso, sc*reso]) Rdict = {'R' : R, 'GlobOrig' : ROrig, 'gridRot' : 0., 'dPx' : dPx} if( not printOnly ): print(' Writing file {} ... '.format(fileout) ) saveTileAsNumpyZ( fileout, Rdict) print(' ... done! ') if( printOn or printOnly ): pfig = plt.figure(num=1, figsize=(10.,10.)) pfig = addImagePlot( pfig, R, fileout, gridOn=True ) plt.show()
mit
jwiggins/scikit-image
skimage/future/graph/rag.py
2
14784
import networkx as nx import numpy as np from numpy.lib.stride_tricks import as_strided from scipy import ndimage as ndi import math from ... import draw, measure, segmentation, util, color try: from matplotlib import colors from matplotlib import cm except ImportError: pass def min_weight(graph, src, dst, n): """Callback to handle merging nodes by choosing minimum weight. Returns either the weight between (`src`, `n`) or (`dst`, `n`) in `graph` or the minimum of the two when both exist. Parameters ---------- graph : RAG The graph under consideration. src, dst : int The verices in `graph` to be merged. n : int A neighbor of `src` or `dst` or both. Returns ------- weight : float The weight between (`src`, `n`) or (`dst`, `n`) in `graph` or the minimum of the two when both exist. """ # cover the cases where n only has edge to either `src` or `dst` default = {'weight': np.inf} w1 = graph[n].get(src, default)['weight'] w2 = graph[n].get(dst, default)['weight'] return min(w1, w2) def _add_edge_filter(values, graph): """Create edge in `graph` between central element of `values` and the rest. Add an edge between the middle element in `values` and all other elements of `values` into `graph`. ``values[len(values) // 2]`` is expected to be the central value of the footprint used. Parameters ---------- values : array The array to process. graph : RAG The graph to add edges in. Returns ------- 0 : float Always returns 0. The return value is required so that `generic_filter` can put it in the output array, but it is ignored by this filter. """ values = values.astype(int) center = values[len(values) // 2] for value in values: if value != center and not graph.has_edge(center, value): graph.add_edge(center, value) return 0. class RAG(nx.Graph): """ The Region Adjacency Graph (RAG) of an image, subclasses `networx.Graph <http://networkx.github.io/documentation/latest/reference/classes.graph.html>`_ Parameters ---------- label_image : array of int An initial segmentation, with each region labeled as a different integer. Every unique value in ``label_image`` will correspond to a node in the graph. connectivity : int in {1, ..., ``label_image.ndim``}, optional The connectivity between pixels in ``label_image``. For a 2D image, a connectivity of 1 corresponds to immediate neighbors up, down, left, and right, while a connectivity of 2 also includes diagonal neighbors. See `scipy.ndimage.generate_binary_structure`. data : networkx Graph specification, optional Initial or additional edges to pass to the NetworkX Graph constructor. See `networkx.Graph`. Valid edge specifications include edge list (list of tuples), NumPy arrays, and SciPy sparse matrices. **attr : keyword arguments, optional Additional attributes to add to the graph. """ def __init__(self, label_image=None, connectivity=1, data=None, **attr): super(RAG, self).__init__(data, **attr) if self.number_of_nodes() == 0: self.max_id = 0 else: self.max_id = max(self.nodes_iter()) if label_image is not None: fp = ndi.generate_binary_structure(label_image.ndim, connectivity) ndi.generic_filter( label_image, function=_add_edge_filter, footprint=fp, mode='nearest', output=as_strided(np.empty((1,), dtype=np.float_), shape=label_image.shape, strides=((0,) * label_image.ndim)), extra_arguments=(self,)) def merge_nodes(self, src, dst, weight_func=min_weight, in_place=True, extra_arguments=[], extra_keywords={}): """Merge node `src` and `dst`. The new combined node is adjacent to all the neighbors of `src` and `dst`. `weight_func` is called to decide the weight of edges incident on the new node. Parameters ---------- src, dst : int Nodes to be merged. weight_func : callable, optional Function to decide edge weight of edges incident on the new node. For each neighbor `n` for `src and `dst`, `weight_func` will be called as follows: `weight_func(src, dst, n, *extra_arguments, **extra_keywords)`. `src`, `dst` and `n` are IDs of vertices in the RAG object which is in turn a subclass of `networkx.Graph`. in_place : bool, optional If set to `True`, the merged node has the id `dst`, else merged node has a new id which is returned. extra_arguments : sequence, optional The sequence of extra positional arguments passed to `weight_func`. extra_keywords : dictionary, optional The dict of keyword arguments passed to the `weight_func`. Returns ------- id : int The id of the new node. Notes ----- If `in_place` is `False` the resulting node has a new id, rather than `dst`. """ src_nbrs = set(self.neighbors(src)) dst_nbrs = set(self.neighbors(dst)) neighbors = (src_nbrs | dst_nbrs) - set([src, dst]) if in_place: new = dst else: new = self.next_id() self.add_node(new) for neighbor in neighbors: w = weight_func(self, src, new, neighbor, *extra_arguments, **extra_keywords) self.add_edge(neighbor, new, weight=w) self.node[new]['labels'] = (self.node[src]['labels'] + self.node[dst]['labels']) self.remove_node(src) if not in_place: self.remove_node(dst) return new def add_node(self, n, attr_dict=None, **attr): """Add node `n` while updating the maximum node id. .. seealso:: :func:`networkx.Graph.add_node`.""" super(RAG, self).add_node(n, attr_dict, **attr) self.max_id = max(n, self.max_id) def add_edge(self, u, v, attr_dict=None, **attr): """Add an edge between `u` and `v` while updating max node id. .. seealso:: :func:`networkx.Graph.add_edge`.""" super(RAG, self).add_edge(u, v, attr_dict, **attr) self.max_id = max(u, v, self.max_id) def copy(self): """Copy the graph with its max node id. .. seealso:: :func:`networkx.Graph.copy`.""" g = super(RAG, self).copy() g.max_id = self.max_id return g def next_id(self): """Returns the `id` for the new node to be inserted. The current implementation returns one more than the maximum `id`. Returns ------- id : int The `id` of the new node to be inserted. """ return self.max_id + 1 def _add_node_silent(self, n): """Add node `n` without updating the maximum node id. This is a convenience method used internally. .. seealso:: :func:`networkx.Graph.add_node`.""" super(RAG, self).add_node(n) def rag_mean_color(image, labels, connectivity=2, mode='distance', sigma=255.0): """Compute the Region Adjacency Graph using mean colors. Given an image and its initial segmentation, this method constructs the corresponding Region Adjacency Graph (RAG). Each node in the RAG represents a set of pixels within `image` with the same label in `labels`. The weight between two adjacent regions represents how similar or dissimilar two regions are depending on the `mode` parameter. Parameters ---------- image : ndarray, shape(M, N, [..., P,] 3) Input image. labels : ndarray, shape(M, N, [..., P,]) The labelled image. This should have one dimension less than `image`. If `image` has dimensions `(M, N, 3)` `labels` should have dimensions `(M, N)`. connectivity : int, optional Pixels with a squared distance less than `connectivity` from each other are considered adjacent. It can range from 1 to `labels.ndim`. Its behavior is the same as `connectivity` parameter in `scipy.ndimage.generate_binary_structure`. mode : {'distance', 'similarity'}, optional The strategy to assign edge weights. 'distance' : The weight between two adjacent regions is the :math:`|c_1 - c_2|`, where :math:`c_1` and :math:`c_2` are the mean colors of the two regions. It represents the Euclidean distance in their average color. 'similarity' : The weight between two adjacent is :math:`e^{-d^2/sigma}` where :math:`d=|c_1 - c_2|`, where :math:`c_1` and :math:`c_2` are the mean colors of the two regions. It represents how similar two regions are. sigma : float, optional Used for computation when `mode` is "similarity". It governs how close to each other two colors should be, for their corresponding edge weight to be significant. A very large value of `sigma` could make any two colors behave as though they were similar. Returns ------- out : RAG The region adjacency graph. Examples -------- >>> from skimage import data, segmentation >>> from skimage.future import graph >>> img = data.astronaut() >>> labels = segmentation.slic(img) >>> rag = graph.rag_mean_color(img, labels) References ---------- .. [1] Alain Tremeau and Philippe Colantoni "Regions Adjacency Graph Applied To Color Image Segmentation" http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.11.5274 """ graph = RAG(labels, connectivity=connectivity) for n in graph: graph.node[n].update({'labels': [n], 'pixel count': 0, 'total color': np.array([0, 0, 0], dtype=np.double)}) for index in np.ndindex(labels.shape): current = labels[index] graph.node[current]['pixel count'] += 1 graph.node[current]['total color'] += image[index] for n in graph: graph.node[n]['mean color'] = (graph.node[n]['total color'] / graph.node[n]['pixel count']) for x, y, d in graph.edges_iter(data=True): diff = graph.node[x]['mean color'] - graph.node[y]['mean color'] diff = np.linalg.norm(diff) if mode == 'similarity': d['weight'] = math.e ** (-(diff ** 2) / sigma) elif mode == 'distance': d['weight'] = diff else: raise ValueError("The mode '%s' is not recognised" % mode) return graph def draw_rag(labels, rag, img, border_color=None, node_color='#ffff00', edge_color='#00ff00', colormap=None, thresh=np.inf, desaturate=False, in_place=True): """Draw a Region Adjacency Graph on an image. Given a labelled image and its corresponding RAG, draw the nodes and edges of the RAG on the image with the specified colors. Nodes are marked by the centroids of the corresponding regions. Parameters ---------- labels : ndarray, shape (M, N) The labelled image. rag : RAG The Region Adjacency Graph. img : ndarray, shape (M, N, 3) Input image. border_color : colorspec, optional Any matplotlib colorspec. node_color : colorspec, optional Any matplotlib colorspec. Yellow by default. edge_color : colorspec, optional Any matplotlib colorspec. Green by default. colormap : colormap, optional Any matplotlib colormap. If specified the edges are colormapped with the specified color map. thresh : float, optional Edges with weight below `thresh` are not drawn, or considered for color mapping. desaturate : bool, optional Convert the image to grayscale before displaying. Particularly helps visualization when using the `colormap` option. in_place : bool, optional If set, the RAG is modified in place. For each node `n` the function will set a new attribute ``rag.node[n]['centroid']``. Returns ------- out : ndarray, shape (M, N, 3) The image with the RAG drawn. Examples -------- >>> from skimage import data, segmentation >>> from skimage.future import graph >>> img = data.coffee() >>> labels = segmentation.slic(img) >>> g = graph.rag_mean_color(img, labels) >>> out = graph.draw_rag(labels, g, img) """ if not in_place: rag = rag.copy() if desaturate: img = color.rgb2gray(img) img = color.gray2rgb(img) out = util.img_as_float(img, force_copy=True) cc = colors.ColorConverter() edge_color = cc.to_rgb(edge_color) node_color = cc.to_rgb(node_color) # Handling the case where one node has multiple labels # offset is 1 so that regionprops does not ignore 0 offset = 1 map_array = np.arange(labels.max() + 1) for n, d in rag.nodes_iter(data=True): for label in d['labels']: map_array[label] = offset offset += 1 rag_labels = map_array[labels] regions = measure.regionprops(rag_labels) for (n, data), region in zip(rag.nodes_iter(data=True), regions): data['centroid'] = region['centroid'] if border_color is not None: border_color = cc.to_rgb(border_color) out = segmentation.mark_boundaries(out, rag_labels, color=border_color) if colormap is not None: edge_weight_list = [d['weight'] for x, y, d in rag.edges_iter(data=True) if d['weight'] < thresh] norm = colors.Normalize() norm.autoscale(edge_weight_list) smap = cm.ScalarMappable(norm, colormap) for n1, n2, data in rag.edges_iter(data=True): if data['weight'] >= thresh: continue r1, c1 = map(int, rag.node[n1]['centroid']) r2, c2 = map(int, rag.node[n2]['centroid']) line = draw.line(r1, c1, r2, c2) if colormap is not None: out[line] = smap.to_rgba([data['weight']])[0][:-1] else: out[line] = edge_color circle = draw.circle(r1, c1, 2) out[circle] = node_color return out
bsd-3-clause
cyliustack/sofa
bin/sofa_analyze.py
1
50661
import argparse import matplotlib matplotlib.use('agg') import csv import json import multiprocessing as mp import os import random import re import sys from functools import partial from operator import attrgetter, itemgetter import networkx as nx import numpy as np import pandas as pd import time from sofa_aisi import * from sofa_common import * from sofa_config import * from sofa_print import * from matplotlib import pyplot as plt import grpc import potato_pb2 import potato_pb2_grpc import socket import random import subprocess from sofa_ml import hsg_v2 def random_generate_color(): rand = lambda: random.randint(0, 255) return '#%02X%02X%02X' % (64, rand(), rand()) def get_top_k_events(cfg, df, topk): topk_events=[] gby = df.groupby(['name']) df_agg = gby.aggregate(np.sum) df_agg_sorted = df_agg.sort_values(by=['duration'],ascending=False) #memcpy = ['copyKind_1_','copyKind_2_','copyKind_8_'] if cfg.verbose: print("Top %d Events: "%topk) print(df_agg_sorted[['duration']][0:topk]) eventName = df_agg_sorted[df_agg_sorted.columns[0:0]].head(topk).index.values.tolist() return eventName # input: pfv(performance feature vector), Pandas.DataFrame # output: hint, docker_image def get_hint(potato_server, features): if len(features) > 0: pfv = potato_pb2.PerformanceFeatureVector() for i in range(len(features)): name = features.iloc[i]['name'] value = features.iloc[i]['value'] #print('%s%s%s' % (str(i).ljust(10), name.ljust(30), ('%.3lf'%value).ljust(20))) pfv.name.append(name) pfv.value.append(value) #print('Wait for response from POTATO server...') myhostname = socket.gethostname() channel = grpc.insecure_channel(potato_server) stub = potato_pb2_grpc.HintStub(channel) request = potato_pb2.HintRequest( hostname = myhostname, pfv = pfv) response = stub.Hint(request) hint = response.hint docker_image = response.docker_image else: hint = 'There is no pfv to get hints.' docker_image = 'NA' return hint, docker_image def concurrency_breakdown(logdir, cfg, df_mpstat, df_cpu, df_gpu, df_nvsmi, df_bandwidth, features): if cfg.verbose: print_title('Concurrency Breakdown Analysis') total_elapsed_time = {'usr':0, 'sys':0, 'gpu':0, 'iow':0, 'idl':0} elapsed_time_ratio = {'usr':0, 'sys':0, 'gpu':0, 'iow':0, 'idl':0} total_interval_vector = [] total_performace_vector = [] if len(df_mpstat) == 0: print_warning(cfg, 'no mpstat and perf traces!') return features t_begin = df_mpstat.iloc[0]['timestamp'] t_end = df_mpstat.iloc[-1]['timestamp'] t = t_begin sample_time = (1 / float(cfg.sys_mon_rate)) while t < t_end: t = t + sample_time if cfg.roi_end > 0 and (t < cfg.roi_begin or t > cfg.roi_end): continue window_begin = t - sample_time window_end = t if len(df_cpu) > 0: if df_cpu.iloc[0].timestamp > window_end: continue cond1 = (df_cpu['timestamp'] > window_begin) cond2 = (df_cpu['timestamp'] <= window_end) df_cpu_interval = df_cpu[ cond1 & cond2 ] num_gpus = len(list(set(df_nvsmi['deviceId']))) cond1 = (df_nvsmi['timestamp'] > window_begin) cond2 = (df_nvsmi['timestamp'] <= window_end) sm = df_nvsmi['event'] == int(0) df_nvsmi_interval = df_nvsmi[ cond1 & cond2 & sm ] cond1 = (df_mpstat['timestamp'] > window_begin) cond2 = (df_mpstat['timestamp'] <= window_end) df_mpstat_interval = df_mpstat[ cond1 & cond2 ] cond1 = (df_bandwidth['timestamp'] > window_begin) cond2 = (df_bandwidth['timestamp'] <= window_end) tx = df_bandwidth['event'] == float(0) rx = df_bandwidth['event'] == float(1) df_tx_interval = df_bandwidth[ cond1 & cond2 & tx ] df_rx_interval = df_bandwidth[ cond1 & cond2 & rx ] mp_usr = [] mp_sys = [] mp_idl = [] mp_iow = [] usr = [] sys = [] irq = [] cpu_max = 0 cpu_min = 100 for i in range(len(df_mpstat_interval)): ratios = df_mpstat_interval.iloc[i]['name'].split(':')[1].split('|') #print(ratios) mp_usr.append(sample_time*int(ratios[1])/100.0) mp_sys.append(sample_time*int(ratios[2])/100.0) mp_idl.append(sample_time*int(ratios[3])/100.0) mp_iow.append(sample_time*int(ratios[4])/100.0) usr.append(int(ratios[1])) sys.append(int(ratios[2])) irq.append(int(ratios[5])) cpu_tmp = int(ratios[1]) + int(ratios[2]) + int(ratios[5]) if cpu_tmp > cpu_max: cpu_max = cpu_tmp if cpu_tmp < cpu_min: cpu_min = cpu_tmp mp_usr = np.asarray(mp_usr) mp_sys = np.asarray(mp_sys) mp_idl = np.asarray(mp_idl) mp_iow = np.asarray(mp_iow) usr = np.asarray(usr) sys = np.asarray(sys) irq = np.asarray(irq) elapsed_time = {'usr':0, 'sys':0, 'gpu':0, 'iow':0, 'idl':0} if len(df_mpstat_interval) > 0: elapsed_time['usr'] = mp_usr.max() elapsed_time['sys'] = mp_sys.max() elapsed_time['gpu'] = df_nvsmi_interval['duration'].max() * 0.01 * sample_time elapsed_time['iow'] = mp_iow.max() #print('gput,usrt = ', elapsed_time['gpu'], elapsed_time['usr']) dominator = max(elapsed_time, key=elapsed_time.get) #if elapsed_time['gpu'] > 0.1 : # dominator = 'gpu' if elapsed_time[dominator] > sample_time * int(cfg.is_idle_threshold)/100: total_elapsed_time[dominator] = total_elapsed_time[dominator] + sample_time else: total_elapsed_time['idl'] += sample_time if num_gpus > 0: time_gpu_avg = df_nvsmi_interval['duration'].sum() * 0.01 * sample_time / num_gpus else: time_gpu_avg = 0 interval_vector = [mp_usr.max(), mp_sys.max(), mp_iow.max(), mp_idl.max(), time_gpu_avg, df_tx_interval['bandwidth'].sum(), df_rx_interval['bandwidth'].sum()] total_interval_vector.append(tuple(interval_vector)) if num_gpus > 0: sm_avg = df_nvsmi_interval['duration'].sum() / int(len(list(set(df_nvsmi_interval['deviceId'])))) else: sm_avg = 0 performace_vector = [window_end, df_nvsmi_interval['duration'].max(), sm_avg, df_nvsmi_interval['duration'].min(), round((usr.mean() + sys.mean() + irq.mean()), 0), cpu_max, cpu_min] total_performace_vector.append(tuple(performace_vector)) total_all_elapsed_time = sum(total_elapsed_time.values()) if total_all_elapsed_time > 0 : elapsed_time_ratio['usr'] = 100 * total_elapsed_time['usr'] / total_all_elapsed_time elapsed_time_ratio['sys'] = 100 * total_elapsed_time['sys'] / total_all_elapsed_time elapsed_time_ratio['gpu'] = 100 * total_elapsed_time['gpu'] / total_all_elapsed_time elapsed_time_ratio['idl'] = 100 * total_elapsed_time['idl'] / total_all_elapsed_time elapsed_time_ratio['iow'] = 100 * total_elapsed_time['iow'] / total_all_elapsed_time if cfg.verbose: print('Elapsed Time = %.1lf ' % total_all_elapsed_time) print('USR = %.1lf %%' % elapsed_time_ratio['usr']) print('SYS = %.1lf %%' % elapsed_time_ratio['sys']) if num_gpus > 0: print('GPU = %.1lf %%' % elapsed_time_ratio['gpu']) print('IDL = %.1lf %%' % elapsed_time_ratio['idl']) print('IOW = %.1lf %%' % elapsed_time_ratio['iow']) if cfg.spotlight_gpu: elapsed_hotspot_time = cfg.roi_end - cfg.roi_begin else: elapsed_hotspot_time = 0 df = pd.DataFrame({ 'name':['elapsed_usr_time_ratio', 'elapsed_sys_time_ratio', 'elapsed_gpu_time_ratio', 'elapsed_iow_time_ratio', 'elapsed_hotspot_time'], 'value':[elapsed_time_ratio['usr'], elapsed_time_ratio['sys'], elapsed_time_ratio['gpu'], elapsed_time_ratio['iow'], elapsed_hotspot_time ] }, columns=['name','value']) features = pd.concat([features, df]) if len(total_performace_vector) > 0: performance_table = pd.DataFrame(total_performace_vector, columns = ['time', 'max_gpu_util', 'avg_gpu_util', 'min_gpu_util', 'cpu_util', 'cpu_max', 'cpu_min']) performance_table.to_csv('%s/performance.csv' % logdir) vector_table = pd.DataFrame(total_interval_vector, columns = ['usr' , 'sys', 'iow', 'idl','gpu', 'net_tx', 'net_rx']) pearson = vector_table.corr(method ='pearson').round(2) if cfg.verbose: print('Correlation Table :') print(pearson) df = pd.DataFrame({ 'name':['corr_gpu_usr', 'corr_gpu_sys', 'corr_gpu_iow', 'corr_gpu_ntx', 'corr_gpu_nrx'], 'value':[pearson['gpu'].usr, pearson['gpu'].sys, pearson['gpu'].iow, pearson['gpu'].net_tx, pearson['gpu'].net_rx]}, columns=['name','value']) features = pd.concat([features, df]) return features def payload_sum(df): print((len(df))) class Event: def __init__(self, name, ttype, timestamp, duration): self.name = name self.ttype = ttype # 0 for begin, 1 for end self.timestamp = timestamp self.duration = duration def __repr__(self): return repr((self.name, self.ttype, self.timestamp, self.duration)) def nvsmi_profile(logdir, cfg, df_nvsmi, features): if not cfg.cluster_ip and cfg.verbose: print_title('SM & MEM & ENCODE/DECODE Profiling') if cfg.spotlight_gpu: if cfg.roi_end == 0 : print_warning(cfg, 'spotlight_gpu has no effects.') else: cond1 = (df_nvsmi['timestamp'] > cfg.roi_begin) cond2 = (df_nvsmi['timestamp'] <= cfg.roi_end) df_nvsmi = df_nvsmi[ cond1 & cond2 ] sm_start = df_nvsmi.iloc[0].timestamp sm_end = df_nvsmi.iloc[-1].timestamp SM_time = sm_end - sm_start result = df_nvsmi.groupby(['deviceId','event'])['duration'].mean() result = result.astype(int) gpu_sm_util = df_nvsmi.groupby(['event'])['duration'].mean()[0] gpu_mem_util = df_nvsmi.groupby(['event'])['duration'].mean()[1] if cfg.nvsmi_data: gpu_enc_util = df_nvsmi.groupby(['event'])['duration'].mean()[2] gpu_dec_util = df_nvsmi.groupby(['event'])['duration'].mean()[3] else: gpu_enc_util = 0 gpu_dec_util = 0 sm = df_nvsmi['event'] == int(0) mem = df_nvsmi['event'] == int(1) enc = df_nvsmi['event'] == int(2) dec = df_nvsmi['event'] == int(3) gpunum = list(set(df_nvsmi['deviceId'])) res = pd.DataFrame([], columns=['sm', 'mem', 'enc', 'dec']) sm_q = pd.DataFrame([], columns=['Q1', 'Q2', 'Q3', 'Avg']) mem_q = pd.DataFrame([], columns=['Q1', 'Q2', 'Q3', 'Avg']) for i in gpunum: gpuid = df_nvsmi['deviceId'] == int(i) gpudata = [round(df_nvsmi[sm & gpuid]['duration'].mean(), 2), round(df_nvsmi[mem & gpuid]['duration'].mean(), 2), round(df_nvsmi[enc & gpuid]['duration'].mean(), 2), round(df_nvsmi[dec & gpuid]['duration'].mean(), 2)] smdata = [round(df_nvsmi[sm & gpuid]['duration'].quantile(0.25), 2), round(df_nvsmi[sm & gpuid]['duration'].quantile(0.5), 2), round(df_nvsmi[sm & gpuid]['duration'].quantile(0.75), 2), round(df_nvsmi[sm & gpuid]['duration'].mean(), 2)] memdata = [round(df_nvsmi[mem & gpuid]['duration'].quantile(0.25), 2), round(df_nvsmi[mem & gpuid]['duration'].quantile(0.5), 2), round(df_nvsmi[mem & gpuid]['duration'].quantile(0.75), 2), round(df_nvsmi[mem & gpuid]['duration'].mean(), 2)] gpu_tmp = pd.DataFrame([gpudata], columns=['sm', 'mem', 'enc', 'dec'], index=[i]) sm_tmp = pd.DataFrame([smdata], columns=['Q1', 'Q2', 'Q3', 'Avg'], index=[i]) mem_tmp = pd.DataFrame([memdata], columns=['Q1', 'Q2', 'Q3', 'Avg'], index=[i]) res = pd.concat([res, gpu_tmp]) sm_q = pd.concat([sm_q, sm_tmp]) mem_q = pd.concat([mem_q, mem_tmp]) res.index.name = 'gpu_id' sm_q.index.name = 'gpu_id' mem_q.index.name = 'gpu_id' if not cfg.cluster_ip and cfg.verbose: print('GPU Utilization (%):') print(res) print('\nGPU SM Quartile (%):') print(sm_q) print('\nGPU MEM Quartile (%):') print(mem_q) print('Overall Average SM Utilization (%): ', int(gpu_sm_util)) print('Overall Average MEM Utilization (%): ', int(gpu_mem_util)) print('Overall Average ENC Utilization (%): ', int(gpu_enc_util)) print('Overall Average DEC Utilization (%): ', int(gpu_dec_util)) print('Overall Active GPU Time (s): %.3lf' % (SM_time * gpu_sm_util/100.0)) df = pd.DataFrame({'name':['gpu_sm_util_q2', 'gpu_sm_util_q3', 'gpu_sm_util', 'gpu_mem_util_q2', 'gpu_mem_util_q3', 'gpu_mem_util'], 'value':[df_nvsmi[sm & gpuid]['duration'].quantile(0.5), df_nvsmi[sm & gpuid]['duration'].quantile(0.75), int(gpu_sm_util), df_nvsmi[mem & gpuid]['duration'].quantile(0.5), df_nvsmi[mem & gpuid]['duration'].quantile(0.75), int(gpu_mem_util), ]}, columns=['name','value']) features = pd.concat([features, df]) return features def gpu_profile(logdir, cfg, df_gpu, features): if cfg.verbose: print_title('GPU Profiling') print('Per-GPU time (s):') groups = df_gpu.groupby("deviceId")["duration"] gpu_time = 0 for key, item in groups: gpuid = int(float(key)) per_gpu_time = groups.get_group(key).sum() if cfg.verbose: print("[%d]: %lf" % (gpuid, per_gpu_time)) gpu_time = gpu_time + per_gpu_time num_gpus = len(groups) kernel_time = 0 grouped_df = df_gpu.groupby("copyKind")["duration"] for key, item in grouped_df: if key == 0: kernel_time = grouped_df.get_group(key).sum() nccl_time = 0 grouped_df = df_gpu.groupby("name")["duration"] for key, item in grouped_df: #print("[%s]: %lf" % (key, grouped_df.get_group(key).sum())) if key.find("nccl") != -1: nccl_time = nccl_time + grouped_df.get_group(key).sum() features = comm_profile(logdir, cfg, df_gpu, features) get_top_k_events(cfg, df_gpu, 10) df = pd.DataFrame({'name':['gpu_time', 'num_gpus', 'kernel_time', 'nccl_time'], 'value':[gpu_time, num_gpus, kernel_time, nccl_time] }, columns=['name','value']) features = pd.concat([features, df]) return features def strace_profile(logdir, cfg, df, features): print_title('STRACE Profiling:') return features def net_profile(logdir, cfg, df, features): if not cfg.cluster_ip: print_title("Network Profiling:") grouped_df = df.groupby("name")["duration"] net_time = 0 n_packets = 0 for key, item in grouped_df: #print("[%s]: %lf" % (key, grouped_df.get_group(key).sum())) if key.find("network:tcp:") != -1: net_time = net_time + grouped_df.get_group(key).sum() n_packets = n_packets + 1 #print(("total network time (s) = %.3lf" % net_time)) #print(("total amount of network packets = %d" % n_packets)) # total network packet packet_num_matrix = df.groupby(['pkt_src','pkt_dst','payload']).size().unstack(level=1, fill_value=0) # total network traffic packet_sum_matrix = df.groupby(['pkt_src','pkt_dst'])["payload"].sum().unstack(level=1, fill_value=0) # ================ change pandas table columns and index name ==== rename_index = packet_sum_matrix.index.tolist() rename_index2 = packet_num_matrix.index.tolist() rename_columns = packet_sum_matrix.columns.tolist() rename_columns2 = packet_num_matrix.columns.tolist() def zero(s): if s[0:2] == '00': s = s[2] elif (s[0] == '0') and (s[1] != '0'): s = s[1:3] return(s) def check_str(rename_list): rename_list_new = [] for j in rename_list: j = str(int(j)) a = j[-9:-6] b = j[-6:-3] c = j[-3:] j = j[:-9] + '.' + zero(a) + '.' + zero(b) + '.' + zero(c) rename_list_new.append(j) return(rename_list_new) def check_str2(rename_list): rename_columns_2 = [] for i in rename_list: i = str(int(i[0])) a = i[-9:-6] b = i[-6:-3] c = i[-3:] i = i[:-9] + '.' + zero(a) + '.' + zero(b) + '.' + zero(c) rename_columns_2.append(i) return(rename_columns_2) rename_index_new = check_str(rename_index) rename_index_new = dict(zip(rename_index, rename_index_new)) rename_index2_new = check_str2(rename_index2) rename_index2_final = list(set(rename_index2_new)) rename_index2_final.sort(key=rename_index2_new.index) rename_columns_new = check_str(rename_columns) rename_columns_new = dict(zip(rename_columns, rename_columns_new)) rename_columns2_new = check_str(rename_columns2) rename_columns2_new = dict(zip(rename_columns2, rename_columns2_new)) # rename here packet_sum_matrix = packet_sum_matrix.rename(columns=rename_columns_new) packet_num_matrix = packet_num_matrix.rename(columns=rename_columns2_new) packet_sum_matrix = packet_sum_matrix.rename(index=rename_index_new) packet_num_matrix.index.set_levels(rename_index2_final , level = 0, inplace = True) if cfg.verbose: print("total amount of network traffic : ", convertbyte(df['payload'].sum()), '\n', packet_sum_matrix.to_string(), "\n") print("total amount of network packets = %d\n" % packet_num_matrix.sum().sum() ,packet_num_matrix.to_string(), "\n") network_value = [] src = [] dst = [] final = [] for index in packet_sum_matrix.index: for column in packet_sum_matrix.columns: src.append(index) dst.append(column) network_value.append(packet_sum_matrix[column][index]) record = list(zip(src, dst, network_value)) record.sort(key=lambda tup:tup[2], reverse=True) for src, dst, value in record: if value == 0: pass else: item = [src, dst, convertbyte(value), round(value / df['payload'].sum(), 2)] final.append(item) summary = pd.DataFrame(final, columns=['Source', 'Destination', 'Amount', 'Percentage of a Node']) summary.to_csv(logdir + 'netrank.csv', mode='w', header=True, index=False) df = pd.DataFrame({'name':['net_time'], 'value':[net_time] }, columns=['name','value']) features = pd.concat([features, df]) return features def convertbyte(B): B = int(B) KB = float(1024) MB = float(KB ** 2) # 1,048,576 GB = float(KB ** 3) # 1,073,741,824 TB = float(KB ** 4) # 1,099,511,627,776 if B < KB: return '{} Bytes'.format(B) elif KB <= B < MB: return '{0:.2f} KB'.format(B/KB) elif MB <= B < GB: return '{0:.2f} MB'.format(B/MB) elif GB <= B < TB: return '{0:.2f} GB'.format(B/GB) elif TB <= B: return '{0:.2f} TB'.format(B/TB) def convertbytes(B): B = float(B) KB = float(1024) MB = float(KB ** 2) # 1,048,576 GB = float(KB ** 3) # 1,073,741,824 TB = float(KB ** 4) # 1,099,511,627,776 if B < KB: return '{0:.2f} B/s'.format(B) elif KB <= B < MB: return '{0:.2f} KB/s'.format(B/KB) elif MB <= B < GB: return '{0:.2f} MB/s'.format(B/MB) elif GB <= B < TB: return '{0:.2f} GB/s'.format(B/GB) elif TB <= B: return '{0:.2f} TB/s'.format(B/TB) def netbandwidth_profile(logdir, cfg, df, features): if not cfg.cluster_ip and cfg.verbose: print_title('Network Bandwidth Profiling:') tx = df['event'] == float(0) rx = df['event'] == float(1) bw_tx_q1 = df[tx]['bandwidth'].quantile(0.25) bw_tx_q2 = df[tx]['bandwidth'].quantile(0.5) bw_tx_q3 = df[tx]['bandwidth'].quantile(0.75) bw_tx_mean = int(df[tx]['bandwidth'].mean()) bw_rx_q1 = df[rx]['bandwidth'].quantile(0.25) bw_rx_q2 = df[rx]['bandwidth'].quantile(0.5) bw_rx_q3 = df[rx]['bandwidth'].quantile(0.75) bw_rx_mean = int(df[rx]['bandwidth'].mean()) with open('%s/netstat.txt' % logdir) as f: lines = f.readlines() first_line = lines[0] last_line = lines[-1] tx_begin = first_line.split(',')[1] rx_begin = first_line.split(',')[2] tx_end = last_line.split(',')[1] rx_end = last_line.split(',')[2] tx_amount = int(last_line.split(',')[1]) - int(first_line.split(',')[1]) rx_amount = int(last_line.split(',')[2]) - int(first_line.split(',')[2]) if not cfg.cluster_ip: bw_tx_q1 = df[tx]['bandwidth'].quantile(0.25) bw_tx_q2 = df[tx]['bandwidth'].quantile(0.5) bw_tx_q3 = df[tx]['bandwidth'].quantile(0.75) bw_tx_mean = int(df[tx]['bandwidth'].mean()) bw_rx_q1 = df[rx]['bandwidth'].quantile(0.25) bw_rx_q2 = df[rx]['bandwidth'].quantile(0.5) bw_rx_q3 = df[rx]['bandwidth'].quantile(0.75) bw_rx_mean = int(df[rx]['bandwidth'].mean()) if cfg.verbose: print('Amount of Network Traffic : %s' % (convertbyte(tx_amount + rx_amount))) print('Amount of tx : %s' % convertbyte(tx_amount)) print('Amount of rx : %s' % convertbyte(rx_amount)) print('Bandwidth Quartile :') print('Q1 tx : %s, rx : %s' % ( convertbytes(bw_tx_q1), convertbytes(bw_rx_q1))) print('Q2 tx : %s, rx : %s' % ( convertbytes(bw_tx_q2), convertbytes(bw_rx_q2))) print('Q3 tx : %s, rx : %s' % ( convertbytes(bw_tx_q3), convertbytes(bw_rx_q3))) print('Avg tx : %s, rx : %s'% ( convertbytes(bw_tx_mean), convertbytes(bw_rx_mean))) #network chart part all_time = df[tx]['timestamp'].tolist() all_tx = df[tx]['bandwidth'].tolist() all_rx = df[rx]['bandwidth'].tolist() fig = plt.figure(dpi=128, figsize=(16, 14)) plt.plot(all_time, all_tx, c='red', alpha=0.5, label='tx') plt.plot(all_time, all_rx, c='blue', alpha=0.5, label='rx') plt.legend(loc='upper right') plt.title("Network Report", fontsize=18) plt.xlabel('Timestamp (s)', fontsize=16) plt.ylabel("Bandwidth (bytes)", fontsize=16) fig.savefig("%s/network_report.pdf" % logdir, bbox_inches='tight') if not cfg.cluster_ip and cfg.verbose: print('Network Bandwidth Chart is saved at %s/network_report.pdf' %logdir) df_feature = pd.DataFrame({ 'name':['bw_tx_q2', 'bw_tx_q3', 'bw_rx_q2', 'bw_rx_q3'], 'value':[bw_tx_q2, bw_tx_q3, bw_rx_q2, bw_rx_q3] }, columns=['name','value']) features = pd.concat([features, df_feature]) return features def blktrace_latency_profile(logdir, cfg, df, features): with open('%s/btt.txt' % logdir) as f: lines = f.readlines() for i, line in enumerate(lines): if '==================== All Devices ====================' in line: start = i if '==================== Device Merge Information ====================' in line: end = i break bttoutput_result = lines[start:end] df_offset = pd.read_table('%s/offset_all.txt' % logdir, delim_whitespace=True, names=('time', 'start', 'end')) time = df_offset['time'].tolist() start_b = df_offset['start'].tolist() end_b = df_offset['end'].tolist() fig = plt.figure(dpi=128, figsize=(16, 14)) plt.plot(time, start_b, c='red', marker='o', alpha=0.3, label='Start block') plt.legend(loc='upper right') plt.title("Block Offset Report", fontsize=18) plt.xlabel('Timestamp (s)', fontsize=16) plt.ylabel("Block Number", fontsize=16) fig.savefig("%s/offset_of_device_report.pdf" % logdir, bbox_inches='tight') print('Offset of Device Report is saved at %s/offset_of_device_report.pdf' %logdir) if cfg.verbose: print_title('Storage Profiling:') print('Blktracae Latency (s):') for btt in bttoutput_result: print(btt[:-1]) blktrace_latency = df['event'] == 'C' blktrace_latency_q1 = df[blktrace_latency]['duration'].quantile(0.25) blktrace_latency_q2 = df[blktrace_latency]['duration'].quantile(0.5) blktrace_latency_q3 = df[blktrace_latency]['duration'].quantile(0.75) blktrace_latency_mean = df[blktrace_latency]['duration'].mean() df_feature = pd.DataFrame({ 'name':['blktrace_latency_q1','blktrace_latency_q2','blktrace_latency_q3'], 'value': [blktrace_latency_q1, blktrace_latency_q2, blktrace_latency_q3] }, columns=['name','value']) features = pd.concat([features, df_feature]) return features def diskstat_profile(logdir, cfg, df, features): #diskstat_dev = list(set(df['dev'])) diskstat_r_q1 = df.groupby('dev')['d_read'].quantile(0.25) diskstat_w_q1 = df.groupby('dev')['d_write'].quantile(0.25) diskstat_q1 = df.groupby('dev')['d_disk_total'].quantile(0.25) diskstat_r_q2 = df.groupby('dev')['d_read'].quantile(0.5) diskstat_w_q2 = df.groupby('dev')['d_write'].quantile(0.5) diskstat_q2 = df.groupby('dev')['d_disk_total'].quantile(0.5) diskstat_r_q3 = df.groupby('dev')['d_read'].quantile(0.75) diskstat_w_q3 = df.groupby('dev')['d_write'].quantile(0.75) diskstat_q3 = df.groupby('dev')['d_disk_total'].quantile(0.75) diskstat_r_avg = df.groupby('dev')['d_read'].mean() diskstat_w_avg = df.groupby('dev')['d_write'].mean() diskstat_avg = df.groupby('dev')['d_disk_total'].mean() diskstat_r_iops = df.groupby('dev')['r_iops'].mean() diskstat_w_iops = df.groupby('dev')['w_iops'].mean() diskstat_iops = df.groupby('dev')['iops'].mean() diskstat_wait = df.groupby('dev')['await_time'].mean() diskstat_table = pd.concat([diskstat_r_q1, diskstat_r_q2, diskstat_r_q3, diskstat_r_avg, diskstat_w_q1, diskstat_w_q2, diskstat_w_q3, diskstat_w_avg, diskstat_q1, diskstat_q2, diskstat_q3, diskstat_avg, diskstat_r_iops, diskstat_w_iops, diskstat_iops, diskstat_wait], axis=1, sort=False) diskstat_columns = ['Q1 throughput(Read)', 'Q2 throughput(Read)', 'Q3 throughput(Read)', 'Avg throughput(Read)', 'Q1 throughput(Write)', 'Q2 throughput(Write)', 'Q3 throughput(Write)', 'Avg throughput(Write)', 'Q1 throughput(R+W)', 'Q2 throughput(R+W)', 'Q3 throughput(R+W)', 'Avg throughput(R+W)', 'Avg IOPS(Read)', 'Avg IOPS(Write)', 'Avg IOPS(R+W)', 'Avg Await time(ms)'] diskstat_table.columns = diskstat_columns diskstat_dev = diskstat_table.index.format() final_table = pd.DataFrame(columns=diskstat_columns) for j, dev in enumerate(diskstat_dev): tmp_list = [] for i in diskstat_columns[:-4]: tmp_list.append(convertbytes(diskstat_table.iloc[j][i])) for i in diskstat_columns[-4:-1]: tmp_list.append('%d' % int(diskstat_table.iloc[j][i])) tmp_list.append('%.3lf ms' % diskstat_table.iloc[j][-1]) tmp_table = pd.DataFrame([tuple(tmp_list)], columns=diskstat_columns, index=[dev]) final_table = pd.concat([final_table, tmp_table]) if cfg.verbose: print_title('DISKSTAT Profiling:') print('Disk Throughput Quartile :') print(final_table.T) df_feature = pd.DataFrame({ 'name':['diskstat_q1','diskstat_q2','diskstat_q3'], 'value': [diskstat_q1.mean(), diskstat_q2.mean(), diskstat_q3.mean()] }, columns=['name','value']) features = pd.concat([features, df_feature]) return features def cpu_profile(logdir, cfg, df): if cfg.verbose: print_title('CPU Profiling:') print('elapsed_time (s) = %.6lf' % cfg.elapsed_time) grouped_df = df.groupby("deviceId")["duration"] total_exec_time = 0 for key, item in grouped_df: print(("[%d]: %lf" % (key, grouped_df.get_group(key).sum()))) total_exec_time = total_exec_time + grouped_df.get_group(key).sum() print("total execution time (s) = %.3lf" % total_exec_time) cpu_detail_profile_df = df[['timestamp','duration','name']] cpu_detail_profile_df = cpu_detail_profile_df.sort_values(by=['duration'], ascending=False) cpu_detail_profile_df['ratio(%)'] = cpu_detail_profile_df['duration']/total_exec_time * 100 cpu_detail_profile_df = cpu_detail_profile_df[['timestamp','ratio(%)','duration','name']] print(cpu_detail_profile_df[:20].to_string(index=False)) def vmstat_profile(logdir, cfg, df, features): _,_,_,_,_,_,df['si'],df['so'],df['bi'],df['bo'],df['in'],df['cs'],_,_,_,_,_=df['name'].str.split('|').str for col_name in ('si','so','bi','bo','in','cs'): df[col_name] = df[col_name].str[3:] vmstat_traces = df[['si','so','bi','bo','in','cs']].astype(float) vm_bi = vmstat_traces['bi'].mean() vm_bo = vmstat_traces['bo'].mean() vm_cs = vmstat_traces['cs'].mean() vm_in = vmstat_traces['in'].mean() if cfg.verbose: print_title('VMSTAT Profiling:') print('average bi/s: %d' % int(vm_cs)) print('average bo/s: %d' % int(vm_in)) print('average cs/s: %d' % int(vm_bi)) print('average in/s: %d' % int(vm_bo)) df_feature = pd.DataFrame({ 'name':['vm_bi', 'vm_bo', 'vm_cs', 'vm_in' ], 'value':[vm_bi, vm_bo, vm_cs, vm_in] }, columns=['name','value']) features = pd.concat([features, df_feature]) return features def mpstat_profile(logdir, cfg, df, features): if not cfg.cluster_ip and cfg.verbose: print_title('MPSTAT Profiling:') num_cores = int(df['deviceId'].max() + 1) df_summary = pd.DataFrame( np.zeros((num_cores,5)), columns=['USR','SYS','IDL','IOW','IRQ']) _,_,_,_,_,df['USR'],df['SYS'],df['IDL'],df['IOW'],df['IRQ'],_ = df["name"].str.split('|').str df[['USR','SYS','IDL','IOW','IRQ']] = df[['USR','SYS','IDL','IOW','IRQ']].astype(float) df["dt_all"] = np.where(df["IDL"]==100, 0.1, df["duration"]/((100-df["IDL"])/100.0)) df["t_USR"] = df['dt_all'] * df['USR']/100.0 df["t_SYS"] = df['dt_all'] * df['SYS']/100.0 df["t_IDL"] = df['dt_all'] * df['IDL']/100.0 df["t_IOW"] = df['dt_all'] * df['IOW']/100.0 df["t_IRQ"] = df['dt_all'] * df['IRQ']/100.0 dfs=[] for i in range(num_cores): dfs.append(df.loc[df['deviceId'] == float(i)]) for index,dff in enumerate(dfs): df_summary.iloc[index]['USR'] = dff['t_USR'].sum() df_summary.iloc[index]['SYS'] = dff['t_SYS'].sum() df_summary.iloc[index]['IDL'] = dff['t_IDL'].sum() df_summary.iloc[index]['IRQ'] = dff['t_IRQ'].sum() df_summary.iloc[index]['IOW'] = dff['t_IOW'].sum() if not cfg.cluster_ip and cfg.verbose: print('CPU Utilization (%):') print('core\tUSR\tSYS\tIDL\tIOW\tIRQ') for i in range(len(df_summary)): t_sum = df_summary.iloc[i].sum() if not cfg.cluster_ip and cfg.verbose: print('%3d\t%3d\t%3d\t%3d\t%3d\t%3d'%(i,int(100.0*df_summary.iloc[i]['USR']/t_sum), int(100.0*df_summary.iloc[i]['SYS']/t_sum), int(100.0*df_summary.iloc[i]['IDL']/t_sum), int(100.0*df_summary.iloc[i]['IOW']/t_sum), int(100.0*df_summary.iloc[i]['IRQ']/t_sum) )) if not cfg.cluster_ip and cfg.verbose: print('CPU Time (s):') print('core\tUSR\tSYS\tIDL\tIOW\tIRQ') for i in range(len(df_summary)): t_sum = df_summary.iloc[i].sum() if not cfg.cluster_ip and cfg.verbose: print('%3d\t%.2lf\t%.2lf\t%.2lf\t%.2lf\t%.2lf'%(i, df_summary.iloc[i]['USR'], df_summary.iloc[i]['SYS'], df_summary.iloc[i]['IDL'], df_summary.iloc[i]['IOW'], df_summary.iloc[i]['IRQ'] )) total_cpu_time = df_summary[['USR','SYS','IRQ']].sum().sum() cpu_util = int(100*total_cpu_time / (num_cores*cfg.elapsed_time)) if not cfg.cluster_ip and cfg.verbose: print('Active CPU Time (s): %.3lf' % total_cpu_time) print('Active CPU ratio (%%): %3d' % cpu_util) df_feature = pd.DataFrame({ 'name':['num_cores', 'cpu_util'], 'value':[num_cores, cpu_util] }, columns=['name','value']) features = pd.concat([features, df_feature]) return features def sofa_analyze(cfg): print_main_progress('SOFA analyzing...') filein = [] df_cpu = pd.DataFrame([], columns=cfg.columns) df_gpu = pd.DataFrame([], columns=cfg.columns) df_net = pd.DataFrame([], columns=cfg.columns) df_mpstat = pd.DataFrame([], columns=cfg.columns) df_vmstat = pd.DataFrame([], columns=cfg.columns) df_bandwidth = pd.DataFrame([], columns=cfg.columns) df_blktrace = pd.DataFrame([], columns=cfg.columns) df_diskstat = pd.DataFrame([], columns=cfg.columns) df_nvsmi = pd.DataFrame([], columns=cfg.columns) iter_summary = None logdir = cfg.logdir with open(logdir+'/misc.txt') as f: lines = f.readlines() elapsed_time = float(lines[0].split()[1]) vcores = int(lines[2].split()[1]) cfg.elapsed_time = float(lines[0].split()[1]) filein_gpu = logdir + "gputrace.csv" filein_cpu = logdir + "cputrace.csv" filein_net = logdir + "nettrace.csv" filein_vmstat = logdir + "vmstat.csv" filein_mpstat = logdir + "mpstat.csv" filein_strace = logdir + "strace.csv" filein_nvsmi = logdir + "nvsmi_trace.csv" filein_bandwidth = logdir + "netstat.csv" filein_blktrace = logdir + "blktrace.csv" filein_diskstat = logdir + "diskstat_vector.csv" if os.path.isfile('%s/nvlink_topo.txt' % logdir): with open(logdir + 'nvlink_topo.txt') as f: lines = f.readlines() if len(lines) > 0: title = lines[0] num_gpus = 1 for word in title.split(): if re.match(r'GPU', word) != None : num_gpus = num_gpus + 1 print_info(cfg,'# of GPUs: ' + str(num_gpus) ) edges = [] if len(lines) >= num_gpus+1: for i in range(num_gpus): connections = lines[1+i].split() for j in range(len(connections)): if connections[j] == 'NV1' or connections[j] == 'NV2': edges.append((i,j-1)) #print('%d connects to %d' % (i, j-1)) ring_found = False G = nx.DiGraph(edges) # Try to find ring with its length of num_gpus for cycle in nx.simple_cycles(G): if len(cycle) == num_gpus: if cfg.verbose: print('One of the recommended ring having length of %d' % len(cycle)) ring_found = True os.system("mkdir -p sofalog/sofa_hints/") xring_order = ','.join(map(str, cycle)) with open("sofalog/sofa_hints/xring_order.txt", "w") as f: f.write('export CUDA_VISIBLE_DEVICES=' + xring_order) break # Try to find ring with its length of num_gpus/2 if not ring_found: for cycle in nx.simple_cycles(G): if len(cycle) == num_gpus/2: print(("One of the recommended ring having length of %d" % len(cycle) )) ring_found = True os.system("mkdir -p sofalog/sofa_hints/") xring_order = ','.join(map(str, cycle)) with open("sofalog/sofa_hints/xring_order.txt", "w") as f: f.write('export CUDA_VISIBLE_DEVICES=' + xring_order) break # Construct Performance Features features = pd.DataFrame({'name':['elapsed_time'], 'value':[cfg.elapsed_time]}, columns=['name','value']) try: df_nvsmi = pd.read_csv(filein_nvsmi) if not df_nvsmi.empty and cfg.spotlight_gpu: state = 0 sm_high = 0 trigger = 10 for i in range(len(df_nvsmi)): if df_nvsmi.iloc[i].event == 0 and df_nvsmi.iloc[i].deviceId == 0 : if df_nvsmi.iloc[i].duration >= 50: sm_high = min(trigger, sm_high + 1) if df_nvsmi.iloc[i].duration < 10: sm_high = max(0, sm_high - 1) if state == 0 and sm_high == trigger: state = 1 cfg.roi_begin = df_nvsmi.iloc[i].timestamp elif state == 1 and sm_high == 0: state = 0 cfg.roi_end = df_nvsmi.iloc[i].timestamp #print('sm_high=%d state=%d' % (sm_high, state)) if cfg.roi_end - cfg.roi_begin < 0: cfg.roi_end = 0 cfg.roi_begin = 0 except IOError: print_warning(cfg, "nvsmi_trace.csv is not found") try: df_cpu = pd.read_csv(filein_cpu) if not df_cpu.empty: if cfg.verbose: cpu_profile(logdir, cfg, df_cpu) if cfg.enable_swarms and len(df_cpu) > cfg.num_swarms: df_cpu, swarms = hsg_v2(cfg, df_cpu) except IOError as e: df_cpu = pd.DataFrame([], columns=cfg.columns) print_warning(cfg, "%s is not found" % filein_cpu) try: df_strace = pd.read_csv(filein_strace) if not df_strace.empty: features = strace_profile(logdir, cfg, df_strace, features) except IOError as e: df_strace = pd.DataFrame([], columns=cfg.columns) print_warning(cfg, "%s is not found" % filein_strace) try: df_net = pd.read_csv(filein_net) if not df_net.empty: features = net_profile(logdir, cfg, df_net, features) except IOError as e: df_net = pd.DataFrame([], columns=cfg.columns) print_warning(cfg, "%s is not found" % filein_net) try: df_bandwidth = pd.read_csv(filein_bandwidth) if not df_bandwidth.empty: features = netbandwidth_profile(logdir, cfg, df_bandwidth, features) except IOError as e: df_bandwidth = pd.DataFrame([], columns=cfg.columns) print_warning(cfg, "%s is not found" % filein_bandwidth) try: df_blktrace = pd.read_csv(filein_blktrace) if not df_blktrace.empty: features = blktrace_latency_profile(logdir, cfg, df_blktrace, features) except IOError as e: df_blktrace = pd.DataFrame([], columns=cfg.columns) print_warning(cfg, "%s is not found" % filein_blktrace) try: df_diskstat = pd.read_csv(filein_diskstat) if not df_diskstat.empty: features = diskstat_profile(logdir, cfg, df_diskstat, features) except IOError as e: df_diskstat = pd.DataFrame([], columns=cfg.columns) print_warning(cfg, "%s is not found" % filein_diskstat) try: df_vmstat = pd.read_csv(filein_vmstat) if not df_vmstat.empty: features = vmstat_profile(logdir, cfg, df_vmstat, features) except IOError as e: df_vmstat = pd.DataFrame([], columns=cfg.columns) print_warning(cfg, "%s is not found" % filein_vmstat) try: df_mpstat = pd.read_csv(filein_mpstat) if not df_mpstat.empty: features = mpstat_profile(logdir, cfg, df_mpstat, features) except IOError as e: df_mpstat = pd.DataFrame([], columns=cfg.columns) print_warning(cfg, "%s is not found" % filein_mpstat) try: df_nvsmi = pd.read_csv(filein_nvsmi) features = nvsmi_profile(logdir, cfg, df_nvsmi, features) except IOError: print_warning(cfg, "nvsmi_trace.csv is not found") try: df_gpu = pd.read_csv(filein_gpu) if not df_gpu.empty: features = gpu_profile(logdir, cfg, df_gpu, features) except IOError: df_gpu = pd.DataFrame([], columns=cfg.columns) print_warning(cfg, "%s is not found. If there is no need to profile GPU, just ignore it." % filein_gpu) try: if len(df_mpstat)>0: df_nvsmi.append(df_mpstat.iloc[0]) features = concurrency_breakdown(logdir, cfg, df_mpstat, df_cpu, df_gpu, df_nvsmi, df_bandwidth, features) except IOError as e: print_warning(cfg, "Some files are not found, which are needed for concurrency_breakdown analysis") if cfg.enable_aisi: selected_pattern, iter_summary, features = sofa_aisi(logdir, cfg, df_cpu, df_gpu, df_strace, df_mpstat, features) if 'IS_SOFA_ON_HAIHUB' not in os.environ or os.environ['IS_SOFA_ON_HAIHUB'] == 'no': print_title('Final Performance Features') print('%s%s%s%s' % ('ID'.ljust(10),'Feature'.ljust(30),'Value'.ljust(20),'Unit'.ljust(20)) ) for i in range(len(features)): name = features.iloc[i]['name'] value = features.iloc[i]['value'] print('%s%s%s' % (str(i).ljust(10), name.ljust(30), ('%.3lf'%value).ljust(20))) if cfg.spotlight_gpu: try: print('Elapsed hotspot time: %.3lf' % features[features.name=='elapsed_hotspot_time'].value) except: print_warning(cfg, 'elpased_hostspot_time is not defined.') if cfg.potato_server: if cfg.potato_server.find(':') == -1: cfg.potato_server = cfg.potato_server + ':50051' hint, docker_image = get_hint(cfg.potato_server, features) df_report = pd.read_json(hint, orient='table') file_potato_report = cfg.logdir + 'potato_report.html' # Export report to HTML file. df_report.to_html(file_potato_report ) with open(file_potato_report, 'a') as f: f.write('<head><link rel=stylesheet type="text/css" href="potato_report.css"></head>') print_title('POTATO Feedback') print('%s%s%s%s' % ('ID'.ljust(5), 'Metric'.ljust(20), 'Value'.ljust(10), 'Reference-Value'.ljust(30) ) ) for i in range(len(df_report)): metric = df_report.iloc[i]['Metric'] if metric != 'hybrid_suggestion': value = df_report.iloc[i]['Value'] ref_value = df_report.iloc[i]['ReferenceValue'] print('%s%s%s%s' % (str(i).ljust(5), metric.ljust(20), ('%.3lf'%value).ljust(20), str(ref_value).ljust(30))) print('\n') print_hint('General Suggestions:') for i in range(len(df_report)): metric = df_report.iloc[i]['Metric'] if metric != 'hybrid_suggestion': suggestion = df_report.iloc[i]['Suggestion'] print('%d. %s' % (i, suggestion)) print('\n') print_hint('Framework-specific Optimization Suggestions:') for i in range(len(df_report)): metric = df_report.iloc[i]['Metric'] if metric == 'hybrid_suggestion': suggestion = df_report.iloc[i]['Suggestion'] print('%d. %s' % (i, suggestion)) #print(df_report[['Metric', 'Value', 'Reference Value']]) #print(df_report[['Suggestion']]) #print('Tag of optimal image recommended from POTATO: ' + highlight(docker_image)) print('\n') print_hint('Please re-launch KubeFlow Jupyter-notebook to have suggested images or resources if necessary.') sofa_home = os.path.dirname(os.path.realpath(__file__)) subprocess.Popen( ['bash', '-c', 'cp %s/../sofaboard/* %s;' % (sofa_home, cfg.logdir)]) subprocess.Popen(['sleep', '2']) print('\n\n') print('Complete!!') def cluster_analyze(cfg): if cfg.verbose: print_title('Cluster Network Profiling :') cluster = cfg.cluster_ip.split(',') summary_net = pd.DataFrame([], columns=['Source', 'Destination', 'Amount', 'Percentage of a Node']) summary_compute = pd.DataFrame([], columns=['gpu_sm_util','gpu_mem_util','cpu_util']) summary_band = pd.DataFrame([], columns=['Q1', 'Q2', 'Q3', 'Avg']) all = [] for i, ip in enumerate(cluster): features = pd.DataFrame({'name':['elapsed_time'], 'value':[cfg.elapsed_time]}, columns=['name','value']) node = 'node ' + str(i) if cfg.verbose: print('node ' + str(i) + ' is ' + ip) logdir = tmp_dir[0:-1] + '-' + ip + '/' filein_net = logdir + "nettrace.csv" filein_mpstat = logdir + "mpstat.csv" filein_nvsmi = logdir + "nvsmi_trace.csv" filein_bandwidth = logdir + "netstat.csv" with open(logdir+'/misc.txt') as f: lines = f.readlines() elapsed_time = float(lines[0].split()[1]) vcores = int(lines[2].split()[1]) cfg.elapsed_time = float(lines[0].split()[1]) try: df_net = pd.read_csv(filein_net) features = net_profile(logdir, cfg, df_net, features) except IOError as e: df_net = pd.DataFrame([], columns=cfg.columns) print_warning(cfg, "%s is not found" % filein_net) try: df_mpstat = pd.read_csv(filein_mpstat) features = mpstat_profile(logdir, cfg, df_mpstat, features) except IOError as e: df_mpstat = pd.DataFrame([], columns=cfg.columns) print_warning(cfg, "%s is not found" % filein_mpstat) try: df_nvsmi = pd.read_csv(filein_nvsmi) features = nvsmi_profile(logdir, cfg, df_nvsmi, features) except IOError: print_warning(cfg, "nvsmi_trace.csv is not found") try: df_bandwidth = pd.read_csv(filein_bandwidth) features = netbandwidth_profile(logdir, cfg, df_bandwidth, features) except IOError as e: df_bandwidth = pd.DataFrame([], columns=cfg.columns) print_warning(cfg, "%s is not found" % filein_bandwidth) sm = int(features[features['name'] == 'gpu_sm_util']['value']) mem = int(features[features['name'] == 'gpu_mem_util']['value']) cpu = int(features[features['name'] == 'cpu_util']['value']) sm_mem_cpu = [sm, mem, cpu] compute_tmp = pd.DataFrame([sm_mem_cpu], columns = ['gpu_sm_util', 'gpu_mem_util', 'cpu_util']) summary_compute = pd.concat([summary_compute, pd.concat([compute_tmp], keys=[node])]) net_tmp = pd.read_csv(logdir + "netrank.csv") summary_net = pd.concat([summary_net, pd.concat([net_tmp], keys=[node])]) # for bandwidth report tx = df_bandwidth['event'] == float(0) rx = df_bandwidth['event'] == float(1) tx_tmp = [convertbytes(df_bandwidth[tx]['bandwidth'].quantile(0.25)), convertbytes(df_bandwidth[tx]['bandwidth'].quantile(0.5)), convertbytes(df_bandwidth[tx]['bandwidth'].quantile(0.75)), convertbytes(df_bandwidth[tx]['bandwidth'].mean())] rx_tmp = [convertbytes(df_bandwidth[rx]['bandwidth'].quantile(0.25)), convertbytes(df_bandwidth[rx]['bandwidth'].quantile(0.5)), convertbytes(df_bandwidth[rx]['bandwidth'].quantile(0.75)), convertbytes(df_bandwidth[rx]['bandwidth'].mean())] band_tmp = pd.DataFrame([tx_tmp], columns = ['Q1', 'Q2', 'Q3', 'Avg'], index = ['tx']) rx_pd = pd.DataFrame([rx_tmp], columns = ['Q1', 'Q2', 'Q3', 'Avg'], index = ['rx']) band_tmp = pd.concat([band_tmp, rx_pd]) summary_band = pd.concat([summary_band, pd.concat([band_tmp], keys=[node])]) if cfg.verbose: with pd.option_context('display.max_rows', None, 'display.max_columns', None): # more options can be specified also print('Ranked Network Traffic : \n', summary_net, '\n') print('Cluster Bandwidth Quartile: \n', summary_band) print_title('Cluster Computation Profiling:') print(summary_compute)
apache-2.0
scls19fr/blaze
blaze/tests/test_interactive.py
8
9834
from blaze.interactive import Data, compute, concrete_head, expr_repr, to_html import datetime from odo import into, append from odo.backends.csv import CSV from blaze import discover from blaze.compute.core import compute from blaze.compute.python import compute from blaze.expr import symbol from datashape import dshape from blaze.utils import tmpfile, example from blaze.compatibility import xfail import pytest import sys from types import MethodType import pandas as pd import pandas.util.testing as tm import numpy as np data = (('Alice', 100), ('Bob', 200)) L = [[1, 'Alice', 100], [2, 'Bob', -200], [3, 'Charlie', 300], [4, 'Denis', 400], [5, 'Edith', -500]] t = Data(data, fields=['name', 'amount']) x = np.ones((2, 2)) def test_table_raises_on_inconsistent_inputs(): with pytest.raises(ValueError): t = Data(data, schema='{name: string, amount: float32}', dshape=dshape("{name: string, amount: float32}")) def test_resources(): assert t._resources() == {t: t.data} def test_resources_fail(): t = symbol('t', 'var * {x: int, y: int}') d = t[t['x'] > 100] with pytest.raises(ValueError): compute(d) def test_compute_on_Data_gives_back_data(): assert compute(Data([1, 2, 3])) == [1, 2, 3] def test_len(): assert len(t) == 2 assert len(t.name) == 2 def test_compute(): assert list(compute(t['amount'] + 1)) == [101, 201] def test_create_with_schema(): t = Data(data, schema='{name: string, amount: float32}') assert t.schema == dshape('{name: string, amount: float32}') def test_create_with_raw_data(): t = Data(data, fields=['name', 'amount']) assert t.schema == dshape('{name: string, amount: int64}') assert t.name assert t.data == data def test_repr(): result = expr_repr(t['name']) print(result) assert isinstance(result, str) assert 'Alice' in result assert 'Bob' in result assert '...' not in result result = expr_repr(t['amount'] + 1) print(result) assert '101' in result t2 = Data(tuple((i, i**2) for i in range(100)), fields=['x', 'y']) assert t2.dshape == dshape('100 * {x: int64, y: int64}') result = expr_repr(t2) print(result) assert len(result.split('\n')) < 20 assert '...' in result def test_repr_of_scalar(): assert repr(t.amount.sum()) == '300' def test_mutable_backed_repr(): mutable_backed_table = Data([[0]], fields=['col1']) repr(mutable_backed_table) def test_dataframe_backed_repr(): df = pd.DataFrame(data=[0], columns=['col1']) dataframe_backed_table = Data(df) repr(dataframe_backed_table) def test_dataframe_backed_repr_complex(): df = pd.DataFrame([(1, 'Alice', 100), (2, 'Bob', -200), (3, 'Charlie', 300), (4, 'Denis', 400), (5, 'Edith', -500)], columns=['id', 'name', 'balance']) t = Data(df) repr(t[t['balance'] < 0]) def test_repr_html_on_no_resources_symbol(): t = symbol('t', '5 * {id: int, name: string, balance: int}') assert to_html(t) == 't' def test_expr_repr_empty(): s = repr(t[t.amount > 1e9]) assert isinstance(s, str) assert 'amount' in s def test_to_html(): s = to_html(t) assert s assert 'Alice' in s assert '<table' in s assert to_html(1) == '1' assert to_html(t.count()) == '2' def test_to_html_on_arrays(): s = to_html(Data(np.ones((2, 2)))) assert '1' in s assert 'br>' in s def test_repr_html(): assert '<table' in t._repr_html_() assert '<table' in t.name._repr_html_() def test_into(): assert into(list, t) == into(list, data) def test_serialization(): import pickle t2 = pickle.loads(pickle.dumps(t)) assert t.schema == t2.schema assert t._name == t2._name def test_table_resource(): with tmpfile('csv') as filename: ds = dshape('var * {a: int, b: int}') csv = CSV(filename) append(csv, [[1, 2], [10, 20]], dshape=ds) t = Data(filename) assert isinstance(t.data, CSV) assert into(list, compute(t)) == into(list, csv) def test_concretehead_failure(): t = symbol('t', 'var * {x:int, y:int}') d = t[t['x'] > 100] with pytest.raises(ValueError): concrete_head(d) def test_into_np_ndarray_column(): t = Data(L, fields=['id', 'name', 'balance']) expr = t[t.balance < 0].name colarray = into(np.ndarray, expr) assert len(list(compute(expr))) == len(colarray) def test_into_nd_array_selection(): t = Data(L, fields=['id', 'name', 'balance']) expr = t[t['balance'] < 0] selarray = into(np.ndarray, expr) assert len(list(compute(expr))) == len(selarray) def test_into_nd_array_column_failure(): tble = Data(L, fields=['id', 'name', 'balance']) expr = tble[tble['balance'] < 0] colarray = into(np.ndarray, expr) assert len(list(compute(expr))) == len(colarray) def test_Data_attribute_repr(): t = Data(CSV(example('accounts-datetimes.csv'))) result = t.when.day expected = pd.DataFrame({'when_day': [1,2,3,4,5]}) assert repr(result) == repr(expected) def test_can_trivially_create_csv_Data(): Data(example('iris.csv')) # in context with Data(example('iris.csv')) as d: assert d is not None def test_can_trivially_create_csv_Data_with_unicode(): if sys.version[0] == '2': assert isinstance(Data(example(u'iris.csv')).data, CSV) def test_can_trivially_create_sqlite_table(): pytest.importorskip('sqlalchemy') Data('sqlite:///'+example('iris.db')+'::iris') # in context with Data('sqlite:///'+example('iris.db')+'::iris') as d: assert d is not None @xfail(reason="h5py/pytables mismatch") def test_can_trivially_create_pytables(): pytest.importorskip('tables') with Data(example('accounts.h5')+'::/accounts') as d: assert d is not None def test_data_passes_kwargs_to_resource(): assert Data(example('iris.csv'), encoding='ascii').data.encoding == 'ascii' def test_data_on_iterator_refies_data(): data = [1, 2, 3] d = Data(iter(data)) assert into(list, d) == data assert into(list, d) == data # in context with Data(iter(data)) as d: assert d is not None def test_Data_on_json_is_concrete(): d = Data(example('accounts-streaming.json')) assert compute(d.amount.sum()) == 100 - 200 + 300 + 400 - 500 assert compute(d.amount.sum()) == 100 - 200 + 300 + 400 - 500 def test_repr_on_nd_array_doesnt_err(): d = Data(np.ones((2, 2, 2))) repr(d + 1) def test_generator_reprs_concretely(): x = [1, 2, 3, 4, 5, 6] d = Data(x) expr = d[d > 2] + 1 assert '4' in repr(expr) def test_incompatible_types(): d = Data(pd.DataFrame(L, columns=['id', 'name', 'amount'])) with pytest.raises(ValueError): d.id == 'foo' result = compute(d.id == 3) expected = pd.Series([False, False, True, False, False], name='id') tm.assert_series_equal(result, expected) def test___array__(): x = np.ones(4) d = Data(x) assert (np.array(d + 1) == x + 1).all() d = Data(x[:2]) x[2:] = d + 1 assert x.tolist() == [1, 1, 2, 2] def test_python_scalar_protocols(): d = Data(1) assert int(d + 1) == 2 assert float(d + 1.0) == 2.0 assert bool(d > 0) is True assert complex(d + 1.0j) == 1 + 1.0j def test_iter(): x = np.ones(4) d = Data(x) assert list(d + 1) == [2, 2, 2, 2] @xfail(reason="DataFrame constructor doesn't yet support __array__") def test_DataFrame(): x = np.array([(1, 2), (1., 2.)], dtype=[('a', 'i4'), ('b', 'f4')]) d = Data(x) assert isinstance(pd.DataFrame(d), pd.DataFrame) def test_head_compute(): data = tm.makeMixedDataFrame() t = symbol('t', discover(data)) db = into('sqlite:///:memory:::t', data, dshape=t.dshape) n = 2 d = Data(db) # skip the header and the ... at the end of the repr expr = d.head(n) s = repr(expr) assert '...' not in s result = s.split('\n')[1:] assert len(result) == n def test_scalar_sql_compute(): t = into('sqlite:///:memory:::t', data, dshape=dshape('var * {name: string, amount: int}')) d = Data(t) assert repr(d.amount.sum()) == '300' def test_no_name_for_simple_data(): d = Data([1, 2, 3]) assert repr(d) == ' \n0 1\n1 2\n2 3' assert not d._name d = Data(1) assert not d._name assert repr(d) == '1' def test_coerce_date_and_datetime(): x = datetime.datetime.now().date() d = Data(x) assert repr(d) == repr(x) x = datetime.datetime.now() d = Data(x) assert repr(d) == repr(x) def test_highly_nested_repr(): data = [[0, [[1, 2], [3]], 'abc']] d = Data(data) assert 'abc' in repr(d.head()) def test_asarray_fails_on_different_column_names(): vs = {'first': [2., 5., 3.], 'second': [4., 1., 4.], 'third': [6., 4., 3.]} df = pd.DataFrame(vs) with pytest.raises(ValueError): Data(df, fields=list('abc')) def test_data_does_not_accept_columns_kwarg(): with pytest.raises(ValueError): Data([(1, 2), (3, 4)], columns=list('ab')) def test_functions_as_bound_methods(): """ Test that all functions on an InteractiveSymbol are instance methods of that object. """ # Filter out __class__ and friends that are special, these can be # callables without being instance methods. callable_attrs = filter( callable, (getattr(t, a, None) for a in dir(t) if not a.startswith('__')), ) for attr in callable_attrs: assert isinstance(attr, MethodType) # Make sure this is bound to the correct object. assert attr.__self__ is t
bsd-3-clause
ChanChiChoi/scikit-learn
sklearn/datasets/twenty_newsgroups.py
72
13586
"""Caching loader for the 20 newsgroups text classification dataset The description of the dataset is available on the official website at: http://people.csail.mit.edu/jrennie/20Newsgroups/ Quoting the introduction: The 20 Newsgroups data set is a collection of approximately 20,000 newsgroup documents, partitioned (nearly) evenly across 20 different newsgroups. To the best of my knowledge, it was originally collected by Ken Lang, probably for his Newsweeder: Learning to filter netnews paper, though he does not explicitly mention this collection. The 20 newsgroups collection has become a popular data set for experiments in text applications of machine learning techniques, such as text classification and text clustering. This dataset loader will download the recommended "by date" variant of the dataset and which features a point in time split between the train and test sets. The compressed dataset size is around 14 Mb compressed. Once uncompressed the train set is 52 MB and the test set is 34 MB. The data is downloaded, extracted and cached in the '~/scikit_learn_data' folder. The `fetch_20newsgroups` function will not vectorize the data into numpy arrays but the dataset lists the filenames of the posts and their categories as target labels. The `fetch_20newsgroups_tfidf` function will in addition do a simple tf-idf vectorization step. """ # Copyright (c) 2011 Olivier Grisel <olivier.grisel@ensta.org> # License: BSD 3 clause import os import logging import tarfile import pickle import shutil import re import codecs import numpy as np import scipy.sparse as sp from .base import get_data_home from .base import Bunch from .base import load_files from ..utils import check_random_state from ..feature_extraction.text import CountVectorizer from ..preprocessing import normalize from ..externals import joblib, six if six.PY3: from urllib.request import urlopen else: from urllib2 import urlopen logger = logging.getLogger(__name__) URL = ("http://people.csail.mit.edu/jrennie/" "20Newsgroups/20news-bydate.tar.gz") ARCHIVE_NAME = "20news-bydate.tar.gz" CACHE_NAME = "20news-bydate.pkz" TRAIN_FOLDER = "20news-bydate-train" TEST_FOLDER = "20news-bydate-test" def download_20newsgroups(target_dir, cache_path): """Download the 20 newsgroups data and stored it as a zipped pickle.""" archive_path = os.path.join(target_dir, ARCHIVE_NAME) train_path = os.path.join(target_dir, TRAIN_FOLDER) test_path = os.path.join(target_dir, TEST_FOLDER) if not os.path.exists(target_dir): os.makedirs(target_dir) if os.path.exists(archive_path): # Download is not complete as the .tar.gz file is removed after # download. logger.warning("Download was incomplete, downloading again.") os.remove(archive_path) logger.warning("Downloading dataset from %s (14 MB)", URL) opener = urlopen(URL) with open(archive_path, 'wb') as f: f.write(opener.read()) logger.info("Decompressing %s", archive_path) tarfile.open(archive_path, "r:gz").extractall(path=target_dir) os.remove(archive_path) # Store a zipped pickle cache = dict(train=load_files(train_path, encoding='latin1'), test=load_files(test_path, encoding='latin1')) compressed_content = codecs.encode(pickle.dumps(cache), 'zlib_codec') with open(cache_path, 'wb') as f: f.write(compressed_content) shutil.rmtree(target_dir) return cache def strip_newsgroup_header(text): """ Given text in "news" format, strip the headers, by removing everything before the first blank line. """ _before, _blankline, after = text.partition('\n\n') return after _QUOTE_RE = re.compile(r'(writes in|writes:|wrote:|says:|said:' r'|^In article|^Quoted from|^\||^>)') def strip_newsgroup_quoting(text): """ Given text in "news" format, strip lines beginning with the quote characters > or |, plus lines that often introduce a quoted section (for example, because they contain the string 'writes:'.) """ good_lines = [line for line in text.split('\n') if not _QUOTE_RE.search(line)] return '\n'.join(good_lines) def strip_newsgroup_footer(text): """ Given text in "news" format, attempt to remove a signature block. As a rough heuristic, we assume that signatures are set apart by either a blank line or a line made of hyphens, and that it is the last such line in the file (disregarding blank lines at the end). """ lines = text.strip().split('\n') for line_num in range(len(lines) - 1, -1, -1): line = lines[line_num] if line.strip().strip('-') == '': break if line_num > 0: return '\n'.join(lines[:line_num]) else: return text def fetch_20newsgroups(data_home=None, subset='train', categories=None, shuffle=True, random_state=42, remove=(), download_if_missing=True): """Load the filenames and data from the 20 newsgroups dataset. Read more in the :ref:`User Guide <20newsgroups>`. Parameters ---------- subset: 'train' or 'test', 'all', optional Select the dataset to load: 'train' for the training set, 'test' for the test set, 'all' for both, with shuffled ordering. data_home: optional, default: None Specify a download and cache folder for the datasets. If None, all scikit-learn data is stored in '~/scikit_learn_data' subfolders. categories: None or collection of string or unicode If None (default), load all the categories. If not None, list of category names to load (other categories ignored). shuffle: bool, optional Whether or not to shuffle the data: might be important for models that make the assumption that the samples are independent and identically distributed (i.i.d.), such as stochastic gradient descent. random_state: numpy random number generator or seed integer Used to shuffle the dataset. download_if_missing: optional, True by default If False, raise an IOError if the data is not locally available instead of trying to download the data from the source site. remove: tuple May contain any subset of ('headers', 'footers', 'quotes'). Each of these are kinds of text that will be detected and removed from the newsgroup posts, preventing classifiers from overfitting on metadata. 'headers' removes newsgroup headers, 'footers' removes blocks at the ends of posts that look like signatures, and 'quotes' removes lines that appear to be quoting another post. 'headers' follows an exact standard; the other filters are not always correct. """ data_home = get_data_home(data_home=data_home) cache_path = os.path.join(data_home, CACHE_NAME) twenty_home = os.path.join(data_home, "20news_home") cache = None if os.path.exists(cache_path): try: with open(cache_path, 'rb') as f: compressed_content = f.read() uncompressed_content = codecs.decode( compressed_content, 'zlib_codec') cache = pickle.loads(uncompressed_content) except Exception as e: print(80 * '_') print('Cache loading failed') print(80 * '_') print(e) if cache is None: if download_if_missing: cache = download_20newsgroups(target_dir=twenty_home, cache_path=cache_path) else: raise IOError('20Newsgroups dataset not found') if subset in ('train', 'test'): data = cache[subset] elif subset == 'all': data_lst = list() target = list() filenames = list() for subset in ('train', 'test'): data = cache[subset] data_lst.extend(data.data) target.extend(data.target) filenames.extend(data.filenames) data.data = data_lst data.target = np.array(target) data.filenames = np.array(filenames) else: raise ValueError( "subset can only be 'train', 'test' or 'all', got '%s'" % subset) data.description = 'the 20 newsgroups by date dataset' if 'headers' in remove: data.data = [strip_newsgroup_header(text) for text in data.data] if 'footers' in remove: data.data = [strip_newsgroup_footer(text) for text in data.data] if 'quotes' in remove: data.data = [strip_newsgroup_quoting(text) for text in data.data] if categories is not None: labels = [(data.target_names.index(cat), cat) for cat in categories] # Sort the categories to have the ordering of the labels labels.sort() labels, categories = zip(*labels) mask = np.in1d(data.target, labels) data.filenames = data.filenames[mask] data.target = data.target[mask] # searchsorted to have continuous labels data.target = np.searchsorted(labels, data.target) data.target_names = list(categories) # Use an object array to shuffle: avoids memory copy data_lst = np.array(data.data, dtype=object) data_lst = data_lst[mask] data.data = data_lst.tolist() if shuffle: random_state = check_random_state(random_state) indices = np.arange(data.target.shape[0]) random_state.shuffle(indices) data.filenames = data.filenames[indices] data.target = data.target[indices] # Use an object array to shuffle: avoids memory copy data_lst = np.array(data.data, dtype=object) data_lst = data_lst[indices] data.data = data_lst.tolist() return data def fetch_20newsgroups_vectorized(subset="train", remove=(), data_home=None): """Load the 20 newsgroups dataset and transform it into tf-idf vectors. This is a convenience function; the tf-idf transformation is done using the default settings for `sklearn.feature_extraction.text.Vectorizer`. For more advanced usage (stopword filtering, n-gram extraction, etc.), combine fetch_20newsgroups with a custom `Vectorizer` or `CountVectorizer`. Read more in the :ref:`User Guide <20newsgroups>`. Parameters ---------- subset: 'train' or 'test', 'all', optional Select the dataset to load: 'train' for the training set, 'test' for the test set, 'all' for both, with shuffled ordering. data_home: optional, default: None Specify an download and cache folder for the datasets. If None, all scikit-learn data is stored in '~/scikit_learn_data' subfolders. remove: tuple May contain any subset of ('headers', 'footers', 'quotes'). Each of these are kinds of text that will be detected and removed from the newsgroup posts, preventing classifiers from overfitting on metadata. 'headers' removes newsgroup headers, 'footers' removes blocks at the ends of posts that look like signatures, and 'quotes' removes lines that appear to be quoting another post. Returns ------- bunch : Bunch object bunch.data: sparse matrix, shape [n_samples, n_features] bunch.target: array, shape [n_samples] bunch.target_names: list, length [n_classes] """ data_home = get_data_home(data_home=data_home) filebase = '20newsgroup_vectorized' if remove: filebase += 'remove-' + ('-'.join(remove)) target_file = os.path.join(data_home, filebase + ".pk") # we shuffle but use a fixed seed for the memoization data_train = fetch_20newsgroups(data_home=data_home, subset='train', categories=None, shuffle=True, random_state=12, remove=remove) data_test = fetch_20newsgroups(data_home=data_home, subset='test', categories=None, shuffle=True, random_state=12, remove=remove) if os.path.exists(target_file): X_train, X_test = joblib.load(target_file) else: vectorizer = CountVectorizer(dtype=np.int16) X_train = vectorizer.fit_transform(data_train.data).tocsr() X_test = vectorizer.transform(data_test.data).tocsr() joblib.dump((X_train, X_test), target_file, compress=9) # the data is stored as int16 for compactness # but normalize needs floats X_train = X_train.astype(np.float64) X_test = X_test.astype(np.float64) normalize(X_train, copy=False) normalize(X_test, copy=False) target_names = data_train.target_names if subset == "train": data = X_train target = data_train.target elif subset == "test": data = X_test target = data_test.target elif subset == "all": data = sp.vstack((X_train, X_test)).tocsr() target = np.concatenate((data_train.target, data_test.target)) else: raise ValueError("%r is not a valid subset: should be one of " "['train', 'test', 'all']" % subset) return Bunch(data=data, target=target, target_names=target_names)
bsd-3-clause
equialgo/scikit-learn
examples/datasets/plot_random_dataset.py
348
2254
""" ============================================== Plot randomly generated classification dataset ============================================== Plot several randomly generated 2D classification datasets. This example illustrates the :func:`datasets.make_classification` :func:`datasets.make_blobs` and :func:`datasets.make_gaussian_quantiles` functions. For ``make_classification``, three binary and two multi-class classification datasets are generated, with different numbers of informative features and clusters per class. """ print(__doc__) import matplotlib.pyplot as plt from sklearn.datasets import make_classification from sklearn.datasets import make_blobs from sklearn.datasets import make_gaussian_quantiles plt.figure(figsize=(8, 8)) plt.subplots_adjust(bottom=.05, top=.9, left=.05, right=.95) plt.subplot(321) plt.title("One informative feature, one cluster per class", fontsize='small') X1, Y1 = make_classification(n_features=2, n_redundant=0, n_informative=1, n_clusters_per_class=1) plt.scatter(X1[:, 0], X1[:, 1], marker='o', c=Y1) plt.subplot(322) plt.title("Two informative features, one cluster per class", fontsize='small') X1, Y1 = make_classification(n_features=2, n_redundant=0, n_informative=2, n_clusters_per_class=1) plt.scatter(X1[:, 0], X1[:, 1], marker='o', c=Y1) plt.subplot(323) plt.title("Two informative features, two clusters per class", fontsize='small') X2, Y2 = make_classification(n_features=2, n_redundant=0, n_informative=2) plt.scatter(X2[:, 0], X2[:, 1], marker='o', c=Y2) plt.subplot(324) plt.title("Multi-class, two informative features, one cluster", fontsize='small') X1, Y1 = make_classification(n_features=2, n_redundant=0, n_informative=2, n_clusters_per_class=1, n_classes=3) plt.scatter(X1[:, 0], X1[:, 1], marker='o', c=Y1) plt.subplot(325) plt.title("Three blobs", fontsize='small') X1, Y1 = make_blobs(n_features=2, centers=3) plt.scatter(X1[:, 0], X1[:, 1], marker='o', c=Y1) plt.subplot(326) plt.title("Gaussian divided into three quantiles", fontsize='small') X1, Y1 = make_gaussian_quantiles(n_features=2, n_classes=3) plt.scatter(X1[:, 0], X1[:, 1], marker='o', c=Y1) plt.show()
bsd-3-clause
joshbohde/scikit-learn
sklearn/svm/tests/test_sparse.py
2
6699
import numpy as np import scipy.sparse from sklearn import datasets, svm, linear_model from numpy.testing import assert_array_almost_equal, \ assert_array_equal, assert_equal from nose.tools import assert_raises from sklearn.datasets.samples_generator import make_classification from . import test_svm # test sample 1 X = np.array([[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1]]) Y = [1, 1, 1, 2, 2, 2] T = np.array([[-1, -1], [2, 2], [3, 2]]) true_result = [1, 2, 2] # test sample 2 X2 = np.array([[0, 0, 0], [1, 1, 1], [2, 0, 0, ], [0, 0, 2], [3, 3, 3]]) Y2 = [1, 2, 2, 2, 3] T2 = np.array([[-1, -1, -1], [1, 1, 1], [2, 2, 2]]) true_result2 = [1, 2, 3] iris = datasets.load_iris() # permute perm = np.random.permutation(iris.target.size) iris.data = iris.data[perm] iris.target = iris.target[perm] # sparsify iris.data = scipy.sparse.csr_matrix(iris.data) def test_SVC(): """Check that sparse SVC gives the same result as SVC""" clf = svm.SVC(kernel='linear').fit(X, Y) sp_clf = svm.sparse.SVC(kernel='linear').fit(X, Y) assert_array_equal(sp_clf.predict(T), true_result) assert scipy.sparse.issparse(sp_clf.support_vectors_) assert_array_almost_equal(clf.support_vectors_, sp_clf.support_vectors_.todense()) assert scipy.sparse.issparse(sp_clf.dual_coef_) assert_array_almost_equal(clf.dual_coef_, sp_clf.dual_coef_.todense()) assert scipy.sparse.issparse(sp_clf.coef_) assert_array_almost_equal(clf.coef_, sp_clf.coef_.todense()) assert_array_almost_equal(clf.predict(T), sp_clf.predict(T)) # refit with a different dataset clf.fit(X2, Y2) sp_clf.fit(X2, Y2) assert_array_almost_equal(clf.support_vectors_, sp_clf.support_vectors_.todense()) assert_array_almost_equal(clf.dual_coef_, sp_clf.dual_coef_.todense()) assert_array_almost_equal(clf.coef_, sp_clf.coef_.todense()) assert_array_almost_equal(clf.predict(T2), sp_clf.predict(T2)) def test_SVC_iris(): """Test the sparse SVC with the iris dataset""" for k in ('linear', 'poly', 'rbf'): sp_clf = svm.sparse.SVC(kernel=k).fit(iris.data, iris.target) clf = svm.SVC(kernel=k).fit(iris.data.todense(), iris.target) assert_array_almost_equal(clf.support_vectors_, sp_clf.support_vectors_.todense()) assert_array_almost_equal(clf.dual_coef_, sp_clf.dual_coef_.todense()) assert_array_almost_equal( clf.predict(iris.data.todense()), sp_clf.predict(iris.data)) if k == 'linear': assert_array_almost_equal(clf.coef_, sp_clf.coef_.todense()) def test_error(): """ Test that it gives proper exception on deficient input """ # impossible value of C assert_raises(ValueError, svm.SVC(C=-1).fit, X, Y) # impossible value of nu clf = svm.sparse.NuSVC(nu=0.0) assert_raises(ValueError, clf.fit, X, Y) Y2 = Y[:-1] # wrong dimensions for labels assert_raises(ValueError, clf.fit, X, Y2) clf = svm.sparse.SVC() clf.fit(X, Y) assert_array_equal(clf.predict(T), true_result) def test_LinearSVC(): """ Similar to test_SVC """ clf = svm.LinearSVC().fit(X, Y) sp_clf = svm.sparse.LinearSVC().fit(X, Y) assert sp_clf.fit_intercept assert_array_almost_equal(clf.raw_coef_, sp_clf.raw_coef_, decimal=4) assert_array_almost_equal(clf.predict(X), sp_clf.predict(X)) clf.fit(X2, Y2) sp_clf.fit(X2, Y2) assert_array_almost_equal(clf.raw_coef_, sp_clf.raw_coef_, decimal=4) def test_LinearSVC_iris(): """Test the sparse LinearSVC with the iris dataset""" sp_clf = svm.sparse.LinearSVC().fit(iris.data, iris.target) clf = svm.LinearSVC().fit(iris.data.todense(), iris.target) assert_array_almost_equal(clf.label_, sp_clf.label_) assert_equal(clf.fit_intercept, sp_clf.fit_intercept) assert_array_almost_equal(clf.raw_coef_, sp_clf.raw_coef_, decimal=1) assert_array_almost_equal( clf.predict(iris.data.todense()), sp_clf.predict(iris.data)) # check decision_function pred = np.argmax(sp_clf.decision_function(iris.data), 1) assert_array_almost_equal(pred, clf.predict(iris.data.todense())) def test_weight(): """ Test class weights """ X_, y_ = make_classification(n_samples=200, n_features=100, weights=[0.833, 0.167], random_state=0) X_ = scipy.sparse.csr_matrix(X_) for clf in (linear_model.sparse.LogisticRegression(), svm.sparse.LinearSVC(), svm.sparse.SVC()): clf.fit(X_[:180], y_[:180], class_weight={0: 5}) y_pred = clf.predict(X_[180:]) assert np.sum(y_pred == y_[180:]) >= 11 def test_sample_weights(): """ Test weights on individual samples """ clf = svm.sparse.SVC() clf.fit(X, Y) assert_array_equal(clf.predict(X[2]), [1.]) sample_weight = [.1] * 3 + [10] * 3 clf.fit(X, Y, sample_weight=sample_weight) assert_array_equal(clf.predict(X[2]), [2.]) def test_sparse_liblinear_intercept_handling(): """ Test that sparse liblinear honours intercept_scaling param """ test_svm.test_dense_liblinear_intercept_handling(svm.sparse.LinearSVC) def test_sparse_realdata(): """ Test on a subset from the 20newsgroups dataset. This catchs some bugs if input is not correctly converted into sparse format or weights are not correctly initialized. """ data = np.array([ 0.03771744, 0.1003567, 0.01174647, 0.027069 ]) indices = np.array([6, 5, 35, 31]) indptr = np.array( [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4]) X = scipy.sparse.csr_matrix((data, indices, indptr)) y = np.array( [ 1., 0., 2., 2., 1., 1., 1., 2., 2., 0., 1., 2., 2., 0., 2., 0., 3., 0., 3., 0., 1., 1., 3., 2., 3., 2., 0., 3., 1., 0., 2., 1., 2., 0., 1., 0., 2., 3., 1., 3., 0., 1., 0., 0., 2., 0., 1., 2., 2., 2., 3., 2., 0., 3., 2., 1., 2., 3., 2., 2., 0., 1., 0., 1., 2., 3., 0., 0., 2., 2., 1., 3., 1., 1., 0., 1., 2., 1., 1., 3.]) clf = svm.SVC(kernel='linear').fit(X.todense(), y) sp_clf = svm.sparse.SVC(kernel='linear').fit(X, y) assert_array_equal(clf.support_vectors_, sp_clf.support_vectors_.todense()) assert_array_equal(clf.dual_coef_, sp_clf.dual_coef_.todense()) if __name__ == '__main__': import nose nose.runmodule()
bsd-3-clause
Insight-book/data-science-from-scratch
first-edition/code/gradient_descent.py
53
5895
from __future__ import division from collections import Counter from linear_algebra import distance, vector_subtract, scalar_multiply import math, random def sum_of_squares(v): """computes the sum of squared elements in v""" return sum(v_i ** 2 for v_i in v) def difference_quotient(f, x, h): return (f(x + h) - f(x)) / h def plot_estimated_derivative(): def square(x): return x * x def derivative(x): return 2 * x derivative_estimate = lambda x: difference_quotient(square, x, h=0.00001) # plot to show they're basically the same import matplotlib.pyplot as plt x = range(-10,10) plt.plot(x, map(derivative, x), 'rx') # red x plt.plot(x, map(derivative_estimate, x), 'b+') # blue + plt.show() # purple *, hopefully def partial_difference_quotient(f, v, i, h): # add h to just the i-th element of v w = [v_j + (h if j == i else 0) for j, v_j in enumerate(v)] return (f(w) - f(v)) / h def estimate_gradient(f, v, h=0.00001): return [partial_difference_quotient(f, v, i, h) for i, _ in enumerate(v)] def step(v, direction, step_size): """move step_size in the direction from v""" return [v_i + step_size * direction_i for v_i, direction_i in zip(v, direction)] def sum_of_squares_gradient(v): return [2 * v_i for v_i in v] def safe(f): """define a new function that wraps f and return it""" def safe_f(*args, **kwargs): try: return f(*args, **kwargs) except: return float('inf') # this means "infinity" in Python return safe_f # # # minimize / maximize batch # # def minimize_batch(target_fn, gradient_fn, theta_0, tolerance=0.000001): """use gradient descent to find theta that minimizes target function""" step_sizes = [100, 10, 1, 0.1, 0.01, 0.001, 0.0001, 0.00001] theta = theta_0 # set theta to initial value target_fn = safe(target_fn) # safe version of target_fn value = target_fn(theta) # value we're minimizing while True: gradient = gradient_fn(theta) next_thetas = [step(theta, gradient, -step_size) for step_size in step_sizes] # choose the one that minimizes the error function next_theta = min(next_thetas, key=target_fn) next_value = target_fn(next_theta) # stop if we're "converging" if abs(value - next_value) < tolerance: return theta else: theta, value = next_theta, next_value def negate(f): """return a function that for any input x returns -f(x)""" return lambda *args, **kwargs: -f(*args, **kwargs) def negate_all(f): """the same when f returns a list of numbers""" return lambda *args, **kwargs: [-y for y in f(*args, **kwargs)] def maximize_batch(target_fn, gradient_fn, theta_0, tolerance=0.000001): return minimize_batch(negate(target_fn), negate_all(gradient_fn), theta_0, tolerance) # # minimize / maximize stochastic # def in_random_order(data): """generator that returns the elements of data in random order""" indexes = [i for i, _ in enumerate(data)] # create a list of indexes random.shuffle(indexes) # shuffle them for i in indexes: # return the data in that order yield data[i] def minimize_stochastic(target_fn, gradient_fn, x, y, theta_0, alpha_0=0.01): data = zip(x, y) theta = theta_0 # initial guess alpha = alpha_0 # initial step size min_theta, min_value = None, float("inf") # the minimum so far iterations_with_no_improvement = 0 # if we ever go 100 iterations with no improvement, stop while iterations_with_no_improvement < 100: value = sum( target_fn(x_i, y_i, theta) for x_i, y_i in data ) if value < min_value: # if we've found a new minimum, remember it # and go back to the original step size min_theta, min_value = theta, value iterations_with_no_improvement = 0 alpha = alpha_0 else: # otherwise we're not improving, so try shrinking the step size iterations_with_no_improvement += 1 alpha *= 0.9 # and take a gradient step for each of the data points for x_i, y_i in in_random_order(data): gradient_i = gradient_fn(x_i, y_i, theta) theta = vector_subtract(theta, scalar_multiply(alpha, gradient_i)) return min_theta def maximize_stochastic(target_fn, gradient_fn, x, y, theta_0, alpha_0=0.01): return minimize_stochastic(negate(target_fn), negate_all(gradient_fn), x, y, theta_0, alpha_0) if __name__ == "__main__": print "using the gradient" v = [random.randint(-10,10) for i in range(3)] tolerance = 0.0000001 while True: #print v, sum_of_squares(v) gradient = sum_of_squares_gradient(v) # compute the gradient at v next_v = step(v, gradient, -0.01) # take a negative gradient step if distance(next_v, v) < tolerance: # stop if we're converging break v = next_v # continue if we're not print "minimum v", v print "minimum value", sum_of_squares(v) print print "using minimize_batch" v = [random.randint(-10,10) for i in range(3)] v = minimize_batch(sum_of_squares, sum_of_squares_gradient, v) print "minimum v", v print "minimum value", sum_of_squares(v)
unlicense
anntzer/scikit-learn
sklearn/utils/_estimator_html_repr.py
1
9497
from contextlib import closing from contextlib import suppress from io import StringIO from string import Template import uuid import html from sklearn import config_context class _VisualBlock: """HTML Representation of Estimator Parameters ---------- kind : {'serial', 'parallel', 'single'} kind of HTML block estimators : list of estimators or `_VisualBlock`s or a single estimator If kind != 'single', then `estimators` is a list of estimators. If kind == 'single', then `estimators` is a single estimator. names : list of str, default=None If kind != 'single', then `names` corresponds to estimators. If kind == 'single', then `names` is a single string corresponding to the single estimator. name_details : list of str, str, or None, default=None If kind != 'single', then `name_details` corresponds to `names`. If kind == 'single', then `name_details` is a single string corresponding to the single estimator. dash_wrapped : bool, default=True If true, wrapped HTML element will be wrapped with a dashed border. Only active when kind != 'single'. """ def __init__(self, kind, estimators, *, names=None, name_details=None, dash_wrapped=True): self.kind = kind self.estimators = estimators self.dash_wrapped = dash_wrapped if self.kind in ('parallel', 'serial'): if names is None: names = (None, ) * len(estimators) if name_details is None: name_details = (None, ) * len(estimators) self.names = names self.name_details = name_details def _sk_visual_block_(self): return self def _write_label_html(out, name, name_details, outer_class="sk-label-container", inner_class="sk-label", checked=False): """Write labeled html with or without a dropdown with named details""" out.write(f'<div class="{outer_class}">' f'<div class="{inner_class} sk-toggleable">') name = html.escape(name) if name_details is not None: checked_str = 'checked' if checked else '' est_id = uuid.uuid4() out.write(f'<input class="sk-toggleable__control sk-hidden--visually" ' f'id="{est_id}" type="checkbox" {checked_str}>' f'<label class="sk-toggleable__label" for="{est_id}">' f'{name}</label>' f'<div class="sk-toggleable__content"><pre>{name_details}' f'</pre></div>') else: out.write(f'<label>{name}</label>') out.write('</div></div>') # outer_class inner_class def _get_visual_block(estimator): """Generate information about how to display an estimator. """ with suppress(AttributeError): return estimator._sk_visual_block_() if isinstance(estimator, str): return _VisualBlock('single', estimator, names=estimator, name_details=estimator) elif estimator is None: return _VisualBlock('single', estimator, names='None', name_details='None') # check if estimator looks like a meta estimator wraps estimators if hasattr(estimator, 'get_params'): estimators = [] for key, value in estimator.get_params().items(): # Only look at the estimators in the first layer if '__' not in key and hasattr(value, 'get_params'): estimators.append(value) if len(estimators): return _VisualBlock('parallel', estimators, names=None) return _VisualBlock('single', estimator, names=estimator.__class__.__name__, name_details=str(estimator)) def _write_estimator_html(out, estimator, estimator_label, estimator_label_details, first_call=False): """Write estimator to html in serial, parallel, or by itself (single). """ if first_call: est_block = _get_visual_block(estimator) else: with config_context(print_changed_only=True): est_block = _get_visual_block(estimator) if est_block.kind in ('serial', 'parallel'): dashed_wrapped = first_call or est_block.dash_wrapped dash_cls = " sk-dashed-wrapped" if dashed_wrapped else "" out.write(f'<div class="sk-item{dash_cls}">') if estimator_label: _write_label_html(out, estimator_label, estimator_label_details) kind = est_block.kind out.write(f'<div class="sk-{kind}">') est_infos = zip(est_block.estimators, est_block.names, est_block.name_details) for est, name, name_details in est_infos: if kind == 'serial': _write_estimator_html(out, est, name, name_details) else: # parallel out.write('<div class="sk-parallel-item">') # wrap element in a serial visualblock serial_block = _VisualBlock('serial', [est], dash_wrapped=False) _write_estimator_html(out, serial_block, name, name_details) out.write('</div>') # sk-parallel-item out.write('</div></div>') elif est_block.kind == 'single': _write_label_html(out, est_block.names, est_block.name_details, outer_class="sk-item", inner_class="sk-estimator", checked=first_call) _STYLE = """ #$id { color: black; background-color: white; } #$id pre{ padding: 0; } #$id div.sk-toggleable { background-color: white; } #$id label.sk-toggleable__label { cursor: pointer; display: block; width: 100%; margin-bottom: 0; padding: 0.2em 0.3em; box-sizing: border-box; text-align: center; } #$id div.sk-toggleable__content { max-height: 0; max-width: 0; overflow: hidden; text-align: left; background-color: #f0f8ff; } #$id div.sk-toggleable__content pre { margin: 0.2em; color: black; border-radius: 0.25em; background-color: #f0f8ff; } #$id input.sk-toggleable__control:checked~div.sk-toggleable__content { max-height: 200px; max-width: 100%; overflow: auto; } #$id div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label { background-color: #d4ebff; } #$id div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label { background-color: #d4ebff; } #$id input.sk-hidden--visually { border: 0; clip: rect(1px 1px 1px 1px); clip: rect(1px, 1px, 1px, 1px); height: 1px; margin: -1px; overflow: hidden; padding: 0; position: absolute; width: 1px; } #$id div.sk-estimator { font-family: monospace; background-color: #f0f8ff; margin: 0.25em 0.25em; border: 1px dotted black; border-radius: 0.25em; box-sizing: border-box; } #$id div.sk-estimator:hover { background-color: #d4ebff; } #$id div.sk-parallel-item::after { content: ""; width: 100%; border-bottom: 1px solid gray; flex-grow: 1; } #$id div.sk-label:hover label.sk-toggleable__label { background-color: #d4ebff; } #$id div.sk-serial::before { content: ""; position: absolute; border-left: 1px solid gray; box-sizing: border-box; top: 2em; bottom: 0; left: 50%; } #$id div.sk-serial { display: flex; flex-direction: column; align-items: center; background-color: white; } #$id div.sk-item { z-index: 1; } #$id div.sk-parallel { display: flex; align-items: stretch; justify-content: center; background-color: white; } #$id div.sk-parallel-item { display: flex; flex-direction: column; position: relative; background-color: white; } #$id div.sk-parallel-item:first-child::after { align-self: flex-end; width: 50%; } #$id div.sk-parallel-item:last-child::after { align-self: flex-start; width: 50%; } #$id div.sk-parallel-item:only-child::after { width: 0; } #$id div.sk-dashed-wrapped { border: 1px dashed gray; margin: 0.2em; box-sizing: border-box; padding-bottom: 0.1em; background-color: white; position: relative; } #$id div.sk-label label { font-family: monospace; font-weight: bold; background-color: white; display: inline-block; line-height: 1.2em; } #$id div.sk-label-container { position: relative; z-index: 2; text-align: center; } #$id div.sk-container { display: inline-block; position: relative; } """.replace(' ', '').replace('\n', '') # noqa def estimator_html_repr(estimator): """Build a HTML representation of an estimator. Read more in the :ref:`User Guide <visualizing_composite_estimators>`. Parameters ---------- estimator : estimator object The estimator to visualize. Returns ------- html: str HTML representation of estimator. """ with closing(StringIO()) as out: container_id = "sk-" + str(uuid.uuid4()) style_template = Template(_STYLE) style_with_id = style_template.substitute(id=container_id) out.write(f'<style>{style_with_id}</style>' f'<div id="{container_id}" class"sk-top-container">' '<div class="sk-container">') _write_estimator_html(out, estimator, estimator.__class__.__name__, str(estimator), first_call=True) out.write('</div></div>') html_output = out.getvalue() return html_output
bsd-3-clause
henry0312/LightGBM
python-package/lightgbm/basic.py
1
150238
# coding: utf-8 """Wrapper for C API of LightGBM.""" import ctypes import json import os import warnings from collections import OrderedDict from copy import deepcopy from functools import wraps from logging import Logger from tempfile import NamedTemporaryFile from typing import Any, Dict, List, Set, Union import numpy as np import scipy.sparse from .compat import PANDAS_INSTALLED, concat, dt_DataTable, is_dtype_sparse, pd_DataFrame, pd_Series from .libpath import find_lib_path class _DummyLogger: def info(self, msg): print(msg) def warning(self, msg): warnings.warn(msg, stacklevel=3) _LOGGER = _DummyLogger() def register_logger(logger): """Register custom logger. Parameters ---------- logger : logging.Logger Custom logger. """ if not isinstance(logger, Logger): raise TypeError("Logger should inherit logging.Logger class") global _LOGGER _LOGGER = logger def _normalize_native_string(func): """Join log messages from native library which come by chunks.""" msg_normalized = [] @wraps(func) def wrapper(msg): nonlocal msg_normalized if msg.strip() == '': msg = ''.join(msg_normalized) msg_normalized = [] return func(msg) else: msg_normalized.append(msg) return wrapper def _log_info(msg): _LOGGER.info(msg) def _log_warning(msg): _LOGGER.warning(msg) @_normalize_native_string def _log_native(msg): _LOGGER.info(msg) def _log_callback(msg): """Redirect logs from native library into Python.""" _log_native(str(msg.decode('utf-8'))) def _load_lib(): """Load LightGBM library.""" lib_path = find_lib_path() if len(lib_path) == 0: return None lib = ctypes.cdll.LoadLibrary(lib_path[0]) lib.LGBM_GetLastError.restype = ctypes.c_char_p callback = ctypes.CFUNCTYPE(None, ctypes.c_char_p) lib.callback = callback(_log_callback) if lib.LGBM_RegisterLogCallback(lib.callback) != 0: raise LightGBMError(lib.LGBM_GetLastError().decode('utf-8')) return lib _LIB = _load_lib() NUMERIC_TYPES = (int, float, bool) def _safe_call(ret): """Check the return value from C API call. Parameters ---------- ret : int The return value from C API calls. """ if ret != 0: raise LightGBMError(_LIB.LGBM_GetLastError().decode('utf-8')) def is_numeric(obj): """Check whether object is a number or not, include numpy number, etc.""" try: float(obj) return True except (TypeError, ValueError): # TypeError: obj is not a string or a number # ValueError: invalid literal return False def is_numpy_1d_array(data): """Check whether data is a numpy 1-D array.""" return isinstance(data, np.ndarray) and len(data.shape) == 1 def is_numpy_column_array(data): """Check whether data is a column numpy array.""" if not isinstance(data, np.ndarray): return False shape = data.shape return len(shape) == 2 and shape[1] == 1 def cast_numpy_1d_array_to_dtype(array, dtype): """Cast numpy 1d array to given dtype.""" if array.dtype == dtype: return array return array.astype(dtype=dtype, copy=False) def is_1d_list(data): """Check whether data is a 1-D list.""" return isinstance(data, list) and (not data or is_numeric(data[0])) def list_to_1d_numpy(data, dtype=np.float32, name='list'): """Convert data to numpy 1-D array.""" if is_numpy_1d_array(data): return cast_numpy_1d_array_to_dtype(data, dtype) elif is_numpy_column_array(data): _log_warning('Converting column-vector to 1d array') array = data.ravel() return cast_numpy_1d_array_to_dtype(array, dtype) elif is_1d_list(data): return np.array(data, dtype=dtype, copy=False) elif isinstance(data, pd_Series): if _get_bad_pandas_dtypes([data.dtypes]): raise ValueError('Series.dtypes must be int, float or bool') return np.array(data, dtype=dtype, copy=False) # SparseArray should be supported as well else: raise TypeError(f"Wrong type({type(data).__name__}) for {name}.\n" "It should be list, numpy 1-D array or pandas Series") def cfloat32_array_to_numpy(cptr, length): """Convert a ctypes float pointer array to a numpy array.""" if isinstance(cptr, ctypes.POINTER(ctypes.c_float)): return np.ctypeslib.as_array(cptr, shape=(length,)).copy() else: raise RuntimeError('Expected float pointer') def cfloat64_array_to_numpy(cptr, length): """Convert a ctypes double pointer array to a numpy array.""" if isinstance(cptr, ctypes.POINTER(ctypes.c_double)): return np.ctypeslib.as_array(cptr, shape=(length,)).copy() else: raise RuntimeError('Expected double pointer') def cint32_array_to_numpy(cptr, length): """Convert a ctypes int pointer array to a numpy array.""" if isinstance(cptr, ctypes.POINTER(ctypes.c_int32)): return np.ctypeslib.as_array(cptr, shape=(length,)).copy() else: raise RuntimeError('Expected int32 pointer') def cint64_array_to_numpy(cptr, length): """Convert a ctypes int pointer array to a numpy array.""" if isinstance(cptr, ctypes.POINTER(ctypes.c_int64)): return np.ctypeslib.as_array(cptr, shape=(length,)).copy() else: raise RuntimeError('Expected int64 pointer') def c_str(string): """Convert a Python string to C string.""" return ctypes.c_char_p(string.encode('utf-8')) def c_array(ctype, values): """Convert a Python array to C array.""" return (ctype * len(values))(*values) def json_default_with_numpy(obj): """Convert numpy classes to JSON serializable objects.""" if isinstance(obj, (np.integer, np.floating, np.bool_)): return obj.item() elif isinstance(obj, np.ndarray): return obj.tolist() else: return obj def param_dict_to_str(data): """Convert Python dictionary to string, which is passed to C API.""" if data is None or not data: return "" pairs = [] for key, val in data.items(): if isinstance(val, (list, tuple, set)) or is_numpy_1d_array(val): def to_string(x): if isinstance(x, list): return f"[{','.join(map(str, x))}]" else: return str(x) pairs.append(f"{key}={','.join(map(to_string, val))}") elif isinstance(val, (str, NUMERIC_TYPES)) or is_numeric(val): pairs.append(f"{key}={val}") elif val is not None: raise TypeError(f'Unknown type of parameter:{key}, got:{type(val).__name__}') return ' '.join(pairs) class _TempFile: def __enter__(self): with NamedTemporaryFile(prefix="lightgbm_tmp_", delete=True) as f: self.name = f.name return self def __exit__(self, exc_type, exc_val, exc_tb): if os.path.isfile(self.name): os.remove(self.name) def readlines(self): with open(self.name, "r+") as f: ret = f.readlines() return ret def writelines(self, lines): with open(self.name, "w+") as f: f.writelines(lines) class LightGBMError(Exception): """Error thrown by LightGBM.""" pass # DeprecationWarning is not shown by default, so let's create our own with higher level class LGBMDeprecationWarning(UserWarning): """Custom deprecation warning.""" pass class _ConfigAliases: aliases = {"bin_construct_sample_cnt": {"bin_construct_sample_cnt", "subsample_for_bin"}, "boosting": {"boosting", "boosting_type", "boost"}, "categorical_feature": {"categorical_feature", "cat_feature", "categorical_column", "cat_column"}, "data_random_seed": {"data_random_seed", "data_seed"}, "early_stopping_round": {"early_stopping_round", "early_stopping_rounds", "early_stopping", "n_iter_no_change"}, "enable_bundle": {"enable_bundle", "is_enable_bundle", "bundle"}, "eval_at": {"eval_at", "ndcg_eval_at", "ndcg_at", "map_eval_at", "map_at"}, "group_column": {"group_column", "group", "group_id", "query_column", "query", "query_id"}, "header": {"header", "has_header"}, "ignore_column": {"ignore_column", "ignore_feature", "blacklist"}, "is_enable_sparse": {"is_enable_sparse", "is_sparse", "enable_sparse", "sparse"}, "label_column": {"label_column", "label"}, "local_listen_port": {"local_listen_port", "local_port", "port"}, "machines": {"machines", "workers", "nodes"}, "metric": {"metric", "metrics", "metric_types"}, "num_class": {"num_class", "num_classes"}, "num_iterations": {"num_iterations", "num_iteration", "n_iter", "num_tree", "num_trees", "num_round", "num_rounds", "num_boost_round", "n_estimators"}, "num_machines": {"num_machines", "num_machine"}, "num_threads": {"num_threads", "num_thread", "nthread", "nthreads", "n_jobs"}, "objective": {"objective", "objective_type", "app", "application"}, "pre_partition": {"pre_partition", "is_pre_partition"}, "tree_learner": {"tree_learner", "tree", "tree_type", "tree_learner_type"}, "two_round": {"two_round", "two_round_loading", "use_two_round_loading"}, "verbosity": {"verbosity", "verbose"}, "weight_column": {"weight_column", "weight"}} @classmethod def get(cls, *args): ret = set() for i in args: ret |= cls.aliases.get(i, {i}) return ret def _choose_param_value(main_param_name: str, params: Dict[str, Any], default_value: Any) -> Dict[str, Any]: """Get a single parameter value, accounting for aliases. Parameters ---------- main_param_name : str Name of the main parameter to get a value for. One of the keys of ``_ConfigAliases``. params : dict Dictionary of LightGBM parameters. default_value : Any Default value to use for the parameter, if none is found in ``params``. Returns ------- params : dict A ``params`` dict with exactly one value for ``main_param_name``, and all aliases ``main_param_name`` removed. If both ``main_param_name`` and one or more aliases for it are found, the value of ``main_param_name`` will be preferred. """ # avoid side effects on passed-in parameters params = deepcopy(params) # find a value, and remove other aliases with .pop() # prefer the value of 'main_param_name' if it exists, otherwise search the aliases found_value = None if main_param_name in params.keys(): found_value = params[main_param_name] for param in _ConfigAliases.get(main_param_name): val = params.pop(param, None) if found_value is None and val is not None: found_value = val if found_value is not None: params[main_param_name] = found_value else: params[main_param_name] = default_value return params MAX_INT32 = (1 << 31) - 1 """Macro definition of data type in C API of LightGBM""" C_API_DTYPE_FLOAT32 = 0 C_API_DTYPE_FLOAT64 = 1 C_API_DTYPE_INT32 = 2 C_API_DTYPE_INT64 = 3 """Matrix is row major in Python""" C_API_IS_ROW_MAJOR = 1 """Macro definition of prediction type in C API of LightGBM""" C_API_PREDICT_NORMAL = 0 C_API_PREDICT_RAW_SCORE = 1 C_API_PREDICT_LEAF_INDEX = 2 C_API_PREDICT_CONTRIB = 3 """Macro definition of sparse matrix type""" C_API_MATRIX_TYPE_CSR = 0 C_API_MATRIX_TYPE_CSC = 1 """Macro definition of feature importance type""" C_API_FEATURE_IMPORTANCE_SPLIT = 0 C_API_FEATURE_IMPORTANCE_GAIN = 1 """Data type of data field""" FIELD_TYPE_MAPPER = {"label": C_API_DTYPE_FLOAT32, "weight": C_API_DTYPE_FLOAT32, "init_score": C_API_DTYPE_FLOAT64, "group": C_API_DTYPE_INT32} """String name to int feature importance type mapper""" FEATURE_IMPORTANCE_TYPE_MAPPER = {"split": C_API_FEATURE_IMPORTANCE_SPLIT, "gain": C_API_FEATURE_IMPORTANCE_GAIN} def convert_from_sliced_object(data): """Fix the memory of multi-dimensional sliced object.""" if isinstance(data, np.ndarray) and isinstance(data.base, np.ndarray): if not data.flags.c_contiguous: _log_warning("Usage of np.ndarray subset (sliced data) is not recommended " "due to it will double the peak memory cost in LightGBM.") return np.copy(data) return data def c_float_array(data): """Get pointer of float numpy array / list.""" if is_1d_list(data): data = np.array(data, copy=False) if is_numpy_1d_array(data): data = convert_from_sliced_object(data) assert data.flags.c_contiguous if data.dtype == np.float32: ptr_data = data.ctypes.data_as(ctypes.POINTER(ctypes.c_float)) type_data = C_API_DTYPE_FLOAT32 elif data.dtype == np.float64: ptr_data = data.ctypes.data_as(ctypes.POINTER(ctypes.c_double)) type_data = C_API_DTYPE_FLOAT64 else: raise TypeError(f"Expected np.float32 or np.float64, met type({data.dtype})") else: raise TypeError(f"Unknown type({type(data).__name__})") return (ptr_data, type_data, data) # return `data` to avoid the temporary copy is freed def c_int_array(data): """Get pointer of int numpy array / list.""" if is_1d_list(data): data = np.array(data, copy=False) if is_numpy_1d_array(data): data = convert_from_sliced_object(data) assert data.flags.c_contiguous if data.dtype == np.int32: ptr_data = data.ctypes.data_as(ctypes.POINTER(ctypes.c_int32)) type_data = C_API_DTYPE_INT32 elif data.dtype == np.int64: ptr_data = data.ctypes.data_as(ctypes.POINTER(ctypes.c_int64)) type_data = C_API_DTYPE_INT64 else: raise TypeError(f"Expected np.int32 or np.int64, met type({data.dtype})") else: raise TypeError(f"Unknown type({type(data).__name__})") return (ptr_data, type_data, data) # return `data` to avoid the temporary copy is freed def _get_bad_pandas_dtypes(dtypes): pandas_dtype_mapper = {'int8': 'int', 'int16': 'int', 'int32': 'int', 'int64': 'int', 'uint8': 'int', 'uint16': 'int', 'uint32': 'int', 'uint64': 'int', 'bool': 'int', 'float16': 'float', 'float32': 'float', 'float64': 'float'} bad_indices = [i for i, dtype in enumerate(dtypes) if (dtype.name not in pandas_dtype_mapper and (not is_dtype_sparse(dtype) or dtype.subtype.name not in pandas_dtype_mapper))] return bad_indices def _data_from_pandas(data, feature_name, categorical_feature, pandas_categorical): if isinstance(data, pd_DataFrame): if len(data.shape) != 2 or data.shape[0] < 1: raise ValueError('Input data must be 2 dimensional and non empty.') if feature_name == 'auto' or feature_name is None: data = data.rename(columns=str) cat_cols = list(data.select_dtypes(include=['category']).columns) cat_cols_not_ordered = [col for col in cat_cols if not data[col].cat.ordered] if pandas_categorical is None: # train dataset pandas_categorical = [list(data[col].cat.categories) for col in cat_cols] else: if len(cat_cols) != len(pandas_categorical): raise ValueError('train and valid dataset categorical_feature do not match.') for col, category in zip(cat_cols, pandas_categorical): if list(data[col].cat.categories) != list(category): data[col] = data[col].cat.set_categories(category) if len(cat_cols): # cat_cols is list data = data.copy() # not alter origin DataFrame data[cat_cols] = data[cat_cols].apply(lambda x: x.cat.codes).replace({-1: np.nan}) if categorical_feature is not None: if feature_name is None: feature_name = list(data.columns) if categorical_feature == 'auto': # use cat cols from DataFrame categorical_feature = cat_cols_not_ordered else: # use cat cols specified by user categorical_feature = list(categorical_feature) if feature_name == 'auto': feature_name = list(data.columns) bad_indices = _get_bad_pandas_dtypes(data.dtypes) if bad_indices: bad_index_cols_str = ', '.join(data.columns[bad_indices]) raise ValueError("DataFrame.dtypes for data must be int, float or bool.\n" "Did not expect the data types in the following fields: " f"{bad_index_cols_str}") data = data.values if data.dtype != np.float32 and data.dtype != np.float64: data = data.astype(np.float32) else: if feature_name == 'auto': feature_name = None if categorical_feature == 'auto': categorical_feature = None return data, feature_name, categorical_feature, pandas_categorical def _label_from_pandas(label): if isinstance(label, pd_DataFrame): if len(label.columns) > 1: raise ValueError('DataFrame for label cannot have multiple columns') if _get_bad_pandas_dtypes(label.dtypes): raise ValueError('DataFrame.dtypes for label must be int, float or bool') label = np.ravel(label.values.astype(np.float32, copy=False)) return label def _dump_pandas_categorical(pandas_categorical, file_name=None): categorical_json = json.dumps(pandas_categorical, default=json_default_with_numpy) pandas_str = f'\npandas_categorical:{categorical_json}\n' if file_name is not None: with open(file_name, 'a') as f: f.write(pandas_str) return pandas_str def _load_pandas_categorical(file_name=None, model_str=None): pandas_key = 'pandas_categorical:' offset = -len(pandas_key) if file_name is not None: max_offset = -os.path.getsize(file_name) with open(file_name, 'rb') as f: while True: if offset < max_offset: offset = max_offset f.seek(offset, os.SEEK_END) lines = f.readlines() if len(lines) >= 2: break offset *= 2 last_line = lines[-1].decode('utf-8').strip() if not last_line.startswith(pandas_key): last_line = lines[-2].decode('utf-8').strip() elif model_str is not None: idx = model_str.rfind('\n', 0, offset) last_line = model_str[idx:].strip() if last_line.startswith(pandas_key): return json.loads(last_line[len(pandas_key):]) else: return None class _InnerPredictor: """_InnerPredictor of LightGBM. Not exposed to user. Used only for prediction, usually used for continued training. .. note:: Can be converted from Booster, but cannot be converted to Booster. """ def __init__(self, model_file=None, booster_handle=None, pred_parameter=None): """Initialize the _InnerPredictor. Parameters ---------- model_file : string or None, optional (default=None) Path to the model file. booster_handle : object or None, optional (default=None) Handle of Booster. pred_parameter: dict or None, optional (default=None) Other parameters for the prediciton. """ self.handle = ctypes.c_void_p() self.__is_manage_handle = True if model_file is not None: """Prediction task""" out_num_iterations = ctypes.c_int(0) _safe_call(_LIB.LGBM_BoosterCreateFromModelfile( c_str(model_file), ctypes.byref(out_num_iterations), ctypes.byref(self.handle))) out_num_class = ctypes.c_int(0) _safe_call(_LIB.LGBM_BoosterGetNumClasses( self.handle, ctypes.byref(out_num_class))) self.num_class = out_num_class.value self.num_total_iteration = out_num_iterations.value self.pandas_categorical = _load_pandas_categorical(file_name=model_file) elif booster_handle is not None: self.__is_manage_handle = False self.handle = booster_handle out_num_class = ctypes.c_int(0) _safe_call(_LIB.LGBM_BoosterGetNumClasses( self.handle, ctypes.byref(out_num_class))) self.num_class = out_num_class.value self.num_total_iteration = self.current_iteration() self.pandas_categorical = None else: raise TypeError('Need model_file or booster_handle to create a predictor') pred_parameter = {} if pred_parameter is None else pred_parameter self.pred_parameter = param_dict_to_str(pred_parameter) def __del__(self): try: if self.__is_manage_handle: _safe_call(_LIB.LGBM_BoosterFree(self.handle)) except AttributeError: pass def __getstate__(self): this = self.__dict__.copy() this.pop('handle', None) return this def predict(self, data, start_iteration=0, num_iteration=-1, raw_score=False, pred_leaf=False, pred_contrib=False, data_has_header=False, is_reshape=True): """Predict logic. Parameters ---------- data : string, numpy array, pandas DataFrame, H2O DataTable's Frame or scipy.sparse Data source for prediction. When data type is string, it represents the path of txt file. start_iteration : int, optional (default=0) Start index of the iteration to predict. num_iteration : int, optional (default=-1) Iteration used for prediction. raw_score : bool, optional (default=False) Whether to predict raw scores. pred_leaf : bool, optional (default=False) Whether to predict leaf index. pred_contrib : bool, optional (default=False) Whether to predict feature contributions. data_has_header : bool, optional (default=False) Whether data has header. Used only for txt data. is_reshape : bool, optional (default=True) Whether to reshape to (nrow, ncol). Returns ------- result : numpy array, scipy.sparse or list of scipy.sparse Prediction result. Can be sparse or a list of sparse objects (each element represents predictions for one class) for feature contributions (when ``pred_contrib=True``). """ if isinstance(data, Dataset): raise TypeError("Cannot use Dataset instance for prediction, please use raw data instead") data = _data_from_pandas(data, None, None, self.pandas_categorical)[0] predict_type = C_API_PREDICT_NORMAL if raw_score: predict_type = C_API_PREDICT_RAW_SCORE if pred_leaf: predict_type = C_API_PREDICT_LEAF_INDEX if pred_contrib: predict_type = C_API_PREDICT_CONTRIB int_data_has_header = 1 if data_has_header else 0 if isinstance(data, str): with _TempFile() as f: _safe_call(_LIB.LGBM_BoosterPredictForFile( self.handle, c_str(data), ctypes.c_int(int_data_has_header), ctypes.c_int(predict_type), ctypes.c_int(start_iteration), ctypes.c_int(num_iteration), c_str(self.pred_parameter), c_str(f.name))) lines = f.readlines() nrow = len(lines) preds = [float(token) for line in lines for token in line.split('\t')] preds = np.array(preds, dtype=np.float64, copy=False) elif isinstance(data, scipy.sparse.csr_matrix): preds, nrow = self.__pred_for_csr(data, start_iteration, num_iteration, predict_type) elif isinstance(data, scipy.sparse.csc_matrix): preds, nrow = self.__pred_for_csc(data, start_iteration, num_iteration, predict_type) elif isinstance(data, np.ndarray): preds, nrow = self.__pred_for_np2d(data, start_iteration, num_iteration, predict_type) elif isinstance(data, list): try: data = np.array(data) except BaseException: raise ValueError('Cannot convert data list to numpy array.') preds, nrow = self.__pred_for_np2d(data, start_iteration, num_iteration, predict_type) elif isinstance(data, dt_DataTable): preds, nrow = self.__pred_for_np2d(data.to_numpy(), start_iteration, num_iteration, predict_type) else: try: _log_warning('Converting data to scipy sparse matrix.') csr = scipy.sparse.csr_matrix(data) except BaseException: raise TypeError(f'Cannot predict data for type {type(data).__name__}') preds, nrow = self.__pred_for_csr(csr, start_iteration, num_iteration, predict_type) if pred_leaf: preds = preds.astype(np.int32) is_sparse = scipy.sparse.issparse(preds) or isinstance(preds, list) if is_reshape and not is_sparse and preds.size != nrow: if preds.size % nrow == 0: preds = preds.reshape(nrow, -1) else: raise ValueError(f'Length of predict result ({preds.size}) cannot be divide nrow ({nrow})') return preds def __get_num_preds(self, start_iteration, num_iteration, nrow, predict_type): """Get size of prediction result.""" if nrow > MAX_INT32: raise LightGBMError('LightGBM cannot perform prediction for data' f'with number of rows greater than MAX_INT32 ({MAX_INT32}).\n' 'You can split your data into chunks' 'and then concatenate predictions for them') n_preds = ctypes.c_int64(0) _safe_call(_LIB.LGBM_BoosterCalcNumPredict( self.handle, ctypes.c_int(nrow), ctypes.c_int(predict_type), ctypes.c_int(start_iteration), ctypes.c_int(num_iteration), ctypes.byref(n_preds))) return n_preds.value def __pred_for_np2d(self, mat, start_iteration, num_iteration, predict_type): """Predict for a 2-D numpy matrix.""" if len(mat.shape) != 2: raise ValueError('Input numpy.ndarray or list must be 2 dimensional') def inner_predict(mat, start_iteration, num_iteration, predict_type, preds=None): if mat.dtype == np.float32 or mat.dtype == np.float64: data = np.array(mat.reshape(mat.size), dtype=mat.dtype, copy=False) else: # change non-float data to float data, need to copy data = np.array(mat.reshape(mat.size), dtype=np.float32) ptr_data, type_ptr_data, _ = c_float_array(data) n_preds = self.__get_num_preds(start_iteration, num_iteration, mat.shape[0], predict_type) if preds is None: preds = np.zeros(n_preds, dtype=np.float64) elif len(preds.shape) != 1 or len(preds) != n_preds: raise ValueError("Wrong length of pre-allocated predict array") out_num_preds = ctypes.c_int64(0) _safe_call(_LIB.LGBM_BoosterPredictForMat( self.handle, ptr_data, ctypes.c_int(type_ptr_data), ctypes.c_int32(mat.shape[0]), ctypes.c_int32(mat.shape[1]), ctypes.c_int(C_API_IS_ROW_MAJOR), ctypes.c_int(predict_type), ctypes.c_int(start_iteration), ctypes.c_int(num_iteration), c_str(self.pred_parameter), ctypes.byref(out_num_preds), preds.ctypes.data_as(ctypes.POINTER(ctypes.c_double)))) if n_preds != out_num_preds.value: raise ValueError("Wrong length for predict results") return preds, mat.shape[0] nrow = mat.shape[0] if nrow > MAX_INT32: sections = np.arange(start=MAX_INT32, stop=nrow, step=MAX_INT32) # __get_num_preds() cannot work with nrow > MAX_INT32, so calculate overall number of predictions piecemeal n_preds = [self.__get_num_preds(start_iteration, num_iteration, i, predict_type) for i in np.diff([0] + list(sections) + [nrow])] n_preds_sections = np.array([0] + n_preds, dtype=np.intp).cumsum() preds = np.zeros(sum(n_preds), dtype=np.float64) for chunk, (start_idx_pred, end_idx_pred) in zip(np.array_split(mat, sections), zip(n_preds_sections, n_preds_sections[1:])): # avoid memory consumption by arrays concatenation operations inner_predict(chunk, start_iteration, num_iteration, predict_type, preds[start_idx_pred:end_idx_pred]) return preds, nrow else: return inner_predict(mat, start_iteration, num_iteration, predict_type) def __create_sparse_native(self, cs, out_shape, out_ptr_indptr, out_ptr_indices, out_ptr_data, indptr_type, data_type, is_csr=True): # create numpy array from output arrays data_indices_len = out_shape[0] indptr_len = out_shape[1] if indptr_type == C_API_DTYPE_INT32: out_indptr = cint32_array_to_numpy(out_ptr_indptr, indptr_len) elif indptr_type == C_API_DTYPE_INT64: out_indptr = cint64_array_to_numpy(out_ptr_indptr, indptr_len) else: raise TypeError("Expected int32 or int64 type for indptr") if data_type == C_API_DTYPE_FLOAT32: out_data = cfloat32_array_to_numpy(out_ptr_data, data_indices_len) elif data_type == C_API_DTYPE_FLOAT64: out_data = cfloat64_array_to_numpy(out_ptr_data, data_indices_len) else: raise TypeError("Expected float32 or float64 type for data") out_indices = cint32_array_to_numpy(out_ptr_indices, data_indices_len) # break up indptr based on number of rows (note more than one matrix in multiclass case) per_class_indptr_shape = cs.indptr.shape[0] # for CSC there is extra column added if not is_csr: per_class_indptr_shape += 1 out_indptr_arrays = np.split(out_indptr, out_indptr.shape[0] / per_class_indptr_shape) # reformat output into a csr or csc matrix or list of csr or csc matrices cs_output_matrices = [] offset = 0 for cs_indptr in out_indptr_arrays: matrix_indptr_len = cs_indptr[cs_indptr.shape[0] - 1] cs_indices = out_indices[offset + cs_indptr[0]:offset + matrix_indptr_len] cs_data = out_data[offset + cs_indptr[0]:offset + matrix_indptr_len] offset += matrix_indptr_len # same shape as input csr or csc matrix except extra column for expected value cs_shape = [cs.shape[0], cs.shape[1] + 1] # note: make sure we copy data as it will be deallocated next if is_csr: cs_output_matrices.append(scipy.sparse.csr_matrix((cs_data, cs_indices, cs_indptr), cs_shape)) else: cs_output_matrices.append(scipy.sparse.csc_matrix((cs_data, cs_indices, cs_indptr), cs_shape)) # free the temporary native indptr, indices, and data _safe_call(_LIB.LGBM_BoosterFreePredictSparse(out_ptr_indptr, out_ptr_indices, out_ptr_data, ctypes.c_int(indptr_type), ctypes.c_int(data_type))) if len(cs_output_matrices) == 1: return cs_output_matrices[0] return cs_output_matrices def __pred_for_csr(self, csr, start_iteration, num_iteration, predict_type): """Predict for a CSR data.""" def inner_predict(csr, start_iteration, num_iteration, predict_type, preds=None): nrow = len(csr.indptr) - 1 n_preds = self.__get_num_preds(start_iteration, num_iteration, nrow, predict_type) if preds is None: preds = np.zeros(n_preds, dtype=np.float64) elif len(preds.shape) != 1 or len(preds) != n_preds: raise ValueError("Wrong length of pre-allocated predict array") out_num_preds = ctypes.c_int64(0) ptr_indptr, type_ptr_indptr, __ = c_int_array(csr.indptr) ptr_data, type_ptr_data, _ = c_float_array(csr.data) assert csr.shape[1] <= MAX_INT32 csr_indices = csr.indices.astype(np.int32, copy=False) _safe_call(_LIB.LGBM_BoosterPredictForCSR( self.handle, ptr_indptr, ctypes.c_int(type_ptr_indptr), csr_indices.ctypes.data_as(ctypes.POINTER(ctypes.c_int32)), ptr_data, ctypes.c_int(type_ptr_data), ctypes.c_int64(len(csr.indptr)), ctypes.c_int64(len(csr.data)), ctypes.c_int64(csr.shape[1]), ctypes.c_int(predict_type), ctypes.c_int(start_iteration), ctypes.c_int(num_iteration), c_str(self.pred_parameter), ctypes.byref(out_num_preds), preds.ctypes.data_as(ctypes.POINTER(ctypes.c_double)))) if n_preds != out_num_preds.value: raise ValueError("Wrong length for predict results") return preds, nrow def inner_predict_sparse(csr, start_iteration, num_iteration, predict_type): ptr_indptr, type_ptr_indptr, __ = c_int_array(csr.indptr) ptr_data, type_ptr_data, _ = c_float_array(csr.data) csr_indices = csr.indices.astype(np.int32, copy=False) matrix_type = C_API_MATRIX_TYPE_CSR if type_ptr_indptr == C_API_DTYPE_INT32: out_ptr_indptr = ctypes.POINTER(ctypes.c_int32)() else: out_ptr_indptr = ctypes.POINTER(ctypes.c_int64)() out_ptr_indices = ctypes.POINTER(ctypes.c_int32)() if type_ptr_data == C_API_DTYPE_FLOAT32: out_ptr_data = ctypes.POINTER(ctypes.c_float)() else: out_ptr_data = ctypes.POINTER(ctypes.c_double)() out_shape = np.zeros(2, dtype=np.int64) _safe_call(_LIB.LGBM_BoosterPredictSparseOutput( self.handle, ptr_indptr, ctypes.c_int(type_ptr_indptr), csr_indices.ctypes.data_as(ctypes.POINTER(ctypes.c_int32)), ptr_data, ctypes.c_int(type_ptr_data), ctypes.c_int64(len(csr.indptr)), ctypes.c_int64(len(csr.data)), ctypes.c_int64(csr.shape[1]), ctypes.c_int(predict_type), ctypes.c_int(start_iteration), ctypes.c_int(num_iteration), c_str(self.pred_parameter), ctypes.c_int(matrix_type), out_shape.ctypes.data_as(ctypes.POINTER(ctypes.c_int64)), ctypes.byref(out_ptr_indptr), ctypes.byref(out_ptr_indices), ctypes.byref(out_ptr_data))) matrices = self.__create_sparse_native(csr, out_shape, out_ptr_indptr, out_ptr_indices, out_ptr_data, type_ptr_indptr, type_ptr_data, is_csr=True) nrow = len(csr.indptr) - 1 return matrices, nrow if predict_type == C_API_PREDICT_CONTRIB: return inner_predict_sparse(csr, start_iteration, num_iteration, predict_type) nrow = len(csr.indptr) - 1 if nrow > MAX_INT32: sections = [0] + list(np.arange(start=MAX_INT32, stop=nrow, step=MAX_INT32)) + [nrow] # __get_num_preds() cannot work with nrow > MAX_INT32, so calculate overall number of predictions piecemeal n_preds = [self.__get_num_preds(start_iteration, num_iteration, i, predict_type) for i in np.diff(sections)] n_preds_sections = np.array([0] + n_preds, dtype=np.intp).cumsum() preds = np.zeros(sum(n_preds), dtype=np.float64) for (start_idx, end_idx), (start_idx_pred, end_idx_pred) in zip(zip(sections, sections[1:]), zip(n_preds_sections, n_preds_sections[1:])): # avoid memory consumption by arrays concatenation operations inner_predict(csr[start_idx:end_idx], start_iteration, num_iteration, predict_type, preds[start_idx_pred:end_idx_pred]) return preds, nrow else: return inner_predict(csr, start_iteration, num_iteration, predict_type) def __pred_for_csc(self, csc, start_iteration, num_iteration, predict_type): """Predict for a CSC data.""" def inner_predict_sparse(csc, start_iteration, num_iteration, predict_type): ptr_indptr, type_ptr_indptr, __ = c_int_array(csc.indptr) ptr_data, type_ptr_data, _ = c_float_array(csc.data) csc_indices = csc.indices.astype(np.int32, copy=False) matrix_type = C_API_MATRIX_TYPE_CSC if type_ptr_indptr == C_API_DTYPE_INT32: out_ptr_indptr = ctypes.POINTER(ctypes.c_int32)() else: out_ptr_indptr = ctypes.POINTER(ctypes.c_int64)() out_ptr_indices = ctypes.POINTER(ctypes.c_int32)() if type_ptr_data == C_API_DTYPE_FLOAT32: out_ptr_data = ctypes.POINTER(ctypes.c_float)() else: out_ptr_data = ctypes.POINTER(ctypes.c_double)() out_shape = np.zeros(2, dtype=np.int64) _safe_call(_LIB.LGBM_BoosterPredictSparseOutput( self.handle, ptr_indptr, ctypes.c_int(type_ptr_indptr), csc_indices.ctypes.data_as(ctypes.POINTER(ctypes.c_int32)), ptr_data, ctypes.c_int(type_ptr_data), ctypes.c_int64(len(csc.indptr)), ctypes.c_int64(len(csc.data)), ctypes.c_int64(csc.shape[0]), ctypes.c_int(predict_type), ctypes.c_int(start_iteration), ctypes.c_int(num_iteration), c_str(self.pred_parameter), ctypes.c_int(matrix_type), out_shape.ctypes.data_as(ctypes.POINTER(ctypes.c_int64)), ctypes.byref(out_ptr_indptr), ctypes.byref(out_ptr_indices), ctypes.byref(out_ptr_data))) matrices = self.__create_sparse_native(csc, out_shape, out_ptr_indptr, out_ptr_indices, out_ptr_data, type_ptr_indptr, type_ptr_data, is_csr=False) nrow = csc.shape[0] return matrices, nrow nrow = csc.shape[0] if nrow > MAX_INT32: return self.__pred_for_csr(csc.tocsr(), start_iteration, num_iteration, predict_type) if predict_type == C_API_PREDICT_CONTRIB: return inner_predict_sparse(csc, start_iteration, num_iteration, predict_type) n_preds = self.__get_num_preds(start_iteration, num_iteration, nrow, predict_type) preds = np.zeros(n_preds, dtype=np.float64) out_num_preds = ctypes.c_int64(0) ptr_indptr, type_ptr_indptr, __ = c_int_array(csc.indptr) ptr_data, type_ptr_data, _ = c_float_array(csc.data) assert csc.shape[0] <= MAX_INT32 csc_indices = csc.indices.astype(np.int32, copy=False) _safe_call(_LIB.LGBM_BoosterPredictForCSC( self.handle, ptr_indptr, ctypes.c_int(type_ptr_indptr), csc_indices.ctypes.data_as(ctypes.POINTER(ctypes.c_int32)), ptr_data, ctypes.c_int(type_ptr_data), ctypes.c_int64(len(csc.indptr)), ctypes.c_int64(len(csc.data)), ctypes.c_int64(csc.shape[0]), ctypes.c_int(predict_type), ctypes.c_int(start_iteration), ctypes.c_int(num_iteration), c_str(self.pred_parameter), ctypes.byref(out_num_preds), preds.ctypes.data_as(ctypes.POINTER(ctypes.c_double)))) if n_preds != out_num_preds.value: raise ValueError("Wrong length for predict results") return preds, nrow def current_iteration(self): """Get the index of the current iteration. Returns ------- cur_iter : int The index of the current iteration. """ out_cur_iter = ctypes.c_int(0) _safe_call(_LIB.LGBM_BoosterGetCurrentIteration( self.handle, ctypes.byref(out_cur_iter))) return out_cur_iter.value class Dataset: """Dataset in LightGBM.""" def __init__(self, data, label=None, reference=None, weight=None, group=None, init_score=None, silent=False, feature_name='auto', categorical_feature='auto', params=None, free_raw_data=True): """Initialize Dataset. Parameters ---------- data : string, numpy array, pandas DataFrame, H2O DataTable's Frame, scipy.sparse or list of numpy arrays Data source of Dataset. If string, it represents the path to txt file. label : list, numpy 1-D array, pandas Series / one-column DataFrame or None, optional (default=None) Label of the data. reference : Dataset or None, optional (default=None) If this is Dataset for validation, training data should be used as reference. weight : list, numpy 1-D array, pandas Series or None, optional (default=None) Weight for each instance. group : list, numpy 1-D array, pandas Series or None, optional (default=None) Group/query data. Only used in the learning-to-rank task. sum(group) = n_samples. For example, if you have a 100-document dataset with ``group = [10, 20, 40, 10, 10, 10]``, that means that you have 6 groups, where the first 10 records are in the first group, records 11-30 are in the second group, records 31-70 are in the third group, etc. init_score : list, numpy 1-D array, pandas Series or None, optional (default=None) Init score for Dataset. silent : bool, optional (default=False) Whether to print messages during construction. feature_name : list of strings or 'auto', optional (default="auto") Feature names. If 'auto' and data is pandas DataFrame, data columns names are used. categorical_feature : list of strings or int, or 'auto', optional (default="auto") Categorical features. If list of int, interpreted as indices. If list of strings, interpreted as feature names (need to specify ``feature_name`` as well). If 'auto' and data is pandas DataFrame, pandas unordered categorical columns are used. All values in categorical features should be less than int32 max value (2147483647). Large values could be memory consuming. Consider using consecutive integers starting from zero. All negative values in categorical features will be treated as missing values. The output cannot be monotonically constrained with respect to a categorical feature. params : dict or None, optional (default=None) Other parameters for Dataset. free_raw_data : bool, optional (default=True) If True, raw data is freed after constructing inner Dataset. """ self.handle = None self.data = data self.label = label self.reference = reference self.weight = weight self.group = group self.init_score = init_score self.silent = silent self.feature_name = feature_name self.categorical_feature = categorical_feature self.params = deepcopy(params) self.free_raw_data = free_raw_data self.used_indices = None self.need_slice = True self._predictor = None self.pandas_categorical = None self.params_back_up = None self.feature_penalty = None self.monotone_constraints = None self.version = 0 def __del__(self): try: self._free_handle() except AttributeError: pass def get_params(self): """Get the used parameters in the Dataset. Returns ------- params : dict or None The used parameters in this Dataset object. """ if self.params is not None: # no min_data, nthreads and verbose in this function dataset_params = _ConfigAliases.get("bin_construct_sample_cnt", "categorical_feature", "data_random_seed", "enable_bundle", "feature_pre_filter", "forcedbins_filename", "group_column", "header", "ignore_column", "is_enable_sparse", "label_column", "linear_tree", "max_bin", "max_bin_by_feature", "min_data_in_bin", "pre_partition", "two_round", "use_missing", "weight_column", "zero_as_missing") return {k: v for k, v in self.params.items() if k in dataset_params} def _free_handle(self): if self.handle is not None: _safe_call(_LIB.LGBM_DatasetFree(self.handle)) self.handle = None self.need_slice = True if self.used_indices is not None: self.data = None return self def _set_init_score_by_predictor(self, predictor, data, used_indices=None): data_has_header = False if isinstance(data, str): # check data has header or not data_has_header = any(self.params.get(alias, False) for alias in _ConfigAliases.get("header")) num_data = self.num_data() if predictor is not None: init_score = predictor.predict(data, raw_score=True, data_has_header=data_has_header, is_reshape=False) if used_indices is not None: assert not self.need_slice if isinstance(data, str): sub_init_score = np.zeros(num_data * predictor.num_class, dtype=np.float32) assert num_data == len(used_indices) for i in range(len(used_indices)): for j in range(predictor.num_class): sub_init_score[i * predictor.num_class + j] = init_score[used_indices[i] * predictor.num_class + j] init_score = sub_init_score if predictor.num_class > 1: # need to regroup init_score new_init_score = np.zeros(init_score.size, dtype=np.float32) for i in range(num_data): for j in range(predictor.num_class): new_init_score[j * num_data + i] = init_score[i * predictor.num_class + j] init_score = new_init_score elif self.init_score is not None: init_score = np.zeros(self.init_score.shape, dtype=np.float32) else: return self self.set_init_score(init_score) def _lazy_init(self, data, label=None, reference=None, weight=None, group=None, init_score=None, predictor=None, silent=False, feature_name='auto', categorical_feature='auto', params=None): if data is None: self.handle = None return self if reference is not None: self.pandas_categorical = reference.pandas_categorical categorical_feature = reference.categorical_feature data, feature_name, categorical_feature, self.pandas_categorical = _data_from_pandas(data, feature_name, categorical_feature, self.pandas_categorical) label = _label_from_pandas(label) # process for args params = {} if params is None else params args_names = (getattr(self.__class__, '_lazy_init') .__code__ .co_varnames[:getattr(self.__class__, '_lazy_init').__code__.co_argcount]) for key, _ in params.items(): if key in args_names: _log_warning(f'{key} keyword has been found in `params` and will be ignored.\n' f'Please use {key} argument of the Dataset constructor to pass this parameter.') # user can set verbose with params, it has higher priority if not any(verbose_alias in params for verbose_alias in _ConfigAliases.get("verbosity")) and silent: params["verbose"] = -1 # get categorical features if categorical_feature is not None: categorical_indices = set() feature_dict = {} if feature_name is not None: feature_dict = {name: i for i, name in enumerate(feature_name)} for name in categorical_feature: if isinstance(name, str) and name in feature_dict: categorical_indices.add(feature_dict[name]) elif isinstance(name, int): categorical_indices.add(name) else: raise TypeError(f"Wrong type({type(name).__name__}) or unknown name({name}) in categorical_feature") if categorical_indices: for cat_alias in _ConfigAliases.get("categorical_feature"): if cat_alias in params: _log_warning(f'{cat_alias} in param dict is overridden.') params.pop(cat_alias, None) params['categorical_column'] = sorted(categorical_indices) params_str = param_dict_to_str(params) self.params = params # process for reference dataset ref_dataset = None if isinstance(reference, Dataset): ref_dataset = reference.construct().handle elif reference is not None: raise TypeError('Reference dataset should be None or dataset instance') # start construct data if isinstance(data, str): self.handle = ctypes.c_void_p() _safe_call(_LIB.LGBM_DatasetCreateFromFile( c_str(data), c_str(params_str), ref_dataset, ctypes.byref(self.handle))) elif isinstance(data, scipy.sparse.csr_matrix): self.__init_from_csr(data, params_str, ref_dataset) elif isinstance(data, scipy.sparse.csc_matrix): self.__init_from_csc(data, params_str, ref_dataset) elif isinstance(data, np.ndarray): self.__init_from_np2d(data, params_str, ref_dataset) elif isinstance(data, list) and len(data) > 0 and all(isinstance(x, np.ndarray) for x in data): self.__init_from_list_np2d(data, params_str, ref_dataset) elif isinstance(data, dt_DataTable): self.__init_from_np2d(data.to_numpy(), params_str, ref_dataset) else: try: csr = scipy.sparse.csr_matrix(data) self.__init_from_csr(csr, params_str, ref_dataset) except BaseException: raise TypeError(f'Cannot initialize Dataset from {type(data).__name__}') if label is not None: self.set_label(label) if self.get_label() is None: raise ValueError("Label should not be None") if weight is not None: self.set_weight(weight) if group is not None: self.set_group(group) if isinstance(predictor, _InnerPredictor): if self._predictor is None and init_score is not None: _log_warning("The init_score will be overridden by the prediction of init_model.") self._set_init_score_by_predictor(predictor, data) elif init_score is not None: self.set_init_score(init_score) elif predictor is not None: raise TypeError(f'Wrong predictor type {type(predictor).__name__}') # set feature names return self.set_feature_name(feature_name) def __init_from_np2d(self, mat, params_str, ref_dataset): """Initialize data from a 2-D numpy matrix.""" if len(mat.shape) != 2: raise ValueError('Input numpy.ndarray must be 2 dimensional') self.handle = ctypes.c_void_p() if mat.dtype == np.float32 or mat.dtype == np.float64: data = np.array(mat.reshape(mat.size), dtype=mat.dtype, copy=False) else: # change non-float data to float data, need to copy data = np.array(mat.reshape(mat.size), dtype=np.float32) ptr_data, type_ptr_data, _ = c_float_array(data) _safe_call(_LIB.LGBM_DatasetCreateFromMat( ptr_data, ctypes.c_int(type_ptr_data), ctypes.c_int32(mat.shape[0]), ctypes.c_int32(mat.shape[1]), ctypes.c_int(C_API_IS_ROW_MAJOR), c_str(params_str), ref_dataset, ctypes.byref(self.handle))) return self def __init_from_list_np2d(self, mats, params_str, ref_dataset): """Initialize data from a list of 2-D numpy matrices.""" ncol = mats[0].shape[1] nrow = np.zeros((len(mats),), np.int32) if mats[0].dtype == np.float64: ptr_data = (ctypes.POINTER(ctypes.c_double) * len(mats))() else: ptr_data = (ctypes.POINTER(ctypes.c_float) * len(mats))() holders = [] type_ptr_data = None for i, mat in enumerate(mats): if len(mat.shape) != 2: raise ValueError('Input numpy.ndarray must be 2 dimensional') if mat.shape[1] != ncol: raise ValueError('Input arrays must have same number of columns') nrow[i] = mat.shape[0] if mat.dtype == np.float32 or mat.dtype == np.float64: mats[i] = np.array(mat.reshape(mat.size), dtype=mat.dtype, copy=False) else: # change non-float data to float data, need to copy mats[i] = np.array(mat.reshape(mat.size), dtype=np.float32) chunk_ptr_data, chunk_type_ptr_data, holder = c_float_array(mats[i]) if type_ptr_data is not None and chunk_type_ptr_data != type_ptr_data: raise ValueError('Input chunks must have same type') ptr_data[i] = chunk_ptr_data type_ptr_data = chunk_type_ptr_data holders.append(holder) self.handle = ctypes.c_void_p() _safe_call(_LIB.LGBM_DatasetCreateFromMats( ctypes.c_int32(len(mats)), ctypes.cast(ptr_data, ctypes.POINTER(ctypes.POINTER(ctypes.c_double))), ctypes.c_int(type_ptr_data), nrow.ctypes.data_as(ctypes.POINTER(ctypes.c_int32)), ctypes.c_int32(ncol), ctypes.c_int(C_API_IS_ROW_MAJOR), c_str(params_str), ref_dataset, ctypes.byref(self.handle))) return self def __init_from_csr(self, csr, params_str, ref_dataset): """Initialize data from a CSR matrix.""" if len(csr.indices) != len(csr.data): raise ValueError(f'Length mismatch: {len(csr.indices)} vs {len(csr.data)}') self.handle = ctypes.c_void_p() ptr_indptr, type_ptr_indptr, __ = c_int_array(csr.indptr) ptr_data, type_ptr_data, _ = c_float_array(csr.data) assert csr.shape[1] <= MAX_INT32 csr_indices = csr.indices.astype(np.int32, copy=False) _safe_call(_LIB.LGBM_DatasetCreateFromCSR( ptr_indptr, ctypes.c_int(type_ptr_indptr), csr_indices.ctypes.data_as(ctypes.POINTER(ctypes.c_int32)), ptr_data, ctypes.c_int(type_ptr_data), ctypes.c_int64(len(csr.indptr)), ctypes.c_int64(len(csr.data)), ctypes.c_int64(csr.shape[1]), c_str(params_str), ref_dataset, ctypes.byref(self.handle))) return self def __init_from_csc(self, csc, params_str, ref_dataset): """Initialize data from a CSC matrix.""" if len(csc.indices) != len(csc.data): raise ValueError(f'Length mismatch: {len(csc.indices)} vs {len(csc.data)}') self.handle = ctypes.c_void_p() ptr_indptr, type_ptr_indptr, __ = c_int_array(csc.indptr) ptr_data, type_ptr_data, _ = c_float_array(csc.data) assert csc.shape[0] <= MAX_INT32 csc_indices = csc.indices.astype(np.int32, copy=False) _safe_call(_LIB.LGBM_DatasetCreateFromCSC( ptr_indptr, ctypes.c_int(type_ptr_indptr), csc_indices.ctypes.data_as(ctypes.POINTER(ctypes.c_int32)), ptr_data, ctypes.c_int(type_ptr_data), ctypes.c_int64(len(csc.indptr)), ctypes.c_int64(len(csc.data)), ctypes.c_int64(csc.shape[0]), c_str(params_str), ref_dataset, ctypes.byref(self.handle))) return self def construct(self): """Lazy init. Returns ------- self : Dataset Constructed Dataset object. """ if self.handle is None: if self.reference is not None: reference_params = self.reference.get_params() if self.get_params() != reference_params: _log_warning('Overriding the parameters from Reference Dataset.') self._update_params(reference_params) if self.used_indices is None: # create valid self._lazy_init(self.data, label=self.label, reference=self.reference, weight=self.weight, group=self.group, init_score=self.init_score, predictor=self._predictor, silent=self.silent, feature_name=self.feature_name, params=self.params) else: # construct subset used_indices = list_to_1d_numpy(self.used_indices, np.int32, name='used_indices') assert used_indices.flags.c_contiguous if self.reference.group is not None: group_info = np.array(self.reference.group).astype(np.int32, copy=False) _, self.group = np.unique(np.repeat(range(len(group_info)), repeats=group_info)[self.used_indices], return_counts=True) self.handle = ctypes.c_void_p() params_str = param_dict_to_str(self.params) _safe_call(_LIB.LGBM_DatasetGetSubset( self.reference.construct().handle, used_indices.ctypes.data_as(ctypes.POINTER(ctypes.c_int32)), ctypes.c_int32(used_indices.shape[0]), c_str(params_str), ctypes.byref(self.handle))) if not self.free_raw_data: self.get_data() if self.group is not None: self.set_group(self.group) if self.get_label() is None: raise ValueError("Label should not be None.") if isinstance(self._predictor, _InnerPredictor) and self._predictor is not self.reference._predictor: self.get_data() self._set_init_score_by_predictor(self._predictor, self.data, used_indices) else: # create train self._lazy_init(self.data, label=self.label, weight=self.weight, group=self.group, init_score=self.init_score, predictor=self._predictor, silent=self.silent, feature_name=self.feature_name, categorical_feature=self.categorical_feature, params=self.params) if self.free_raw_data: self.data = None return self def create_valid(self, data, label=None, weight=None, group=None, init_score=None, silent=False, params=None): """Create validation data align with current Dataset. Parameters ---------- data : string, numpy array, pandas DataFrame, H2O DataTable's Frame, scipy.sparse or list of numpy arrays Data source of Dataset. If string, it represents the path to txt file. label : list, numpy 1-D array, pandas Series / one-column DataFrame or None, optional (default=None) Label of the data. weight : list, numpy 1-D array, pandas Series or None, optional (default=None) Weight for each instance. group : list, numpy 1-D array, pandas Series or None, optional (default=None) Group/query data. Only used in the learning-to-rank task. sum(group) = n_samples. For example, if you have a 100-document dataset with ``group = [10, 20, 40, 10, 10, 10]``, that means that you have 6 groups, where the first 10 records are in the first group, records 11-30 are in the second group, records 31-70 are in the third group, etc. init_score : list, numpy 1-D array, pandas Series or None, optional (default=None) Init score for Dataset. silent : bool, optional (default=False) Whether to print messages during construction. params : dict or None, optional (default=None) Other parameters for validation Dataset. Returns ------- valid : Dataset Validation Dataset with reference to self. """ ret = Dataset(data, label=label, reference=self, weight=weight, group=group, init_score=init_score, silent=silent, params=params, free_raw_data=self.free_raw_data) ret._predictor = self._predictor ret.pandas_categorical = self.pandas_categorical return ret def subset(self, used_indices, params=None): """Get subset of current Dataset. Parameters ---------- used_indices : list of int Indices used to create the subset. params : dict or None, optional (default=None) These parameters will be passed to Dataset constructor. Returns ------- subset : Dataset Subset of the current Dataset. """ if params is None: params = self.params ret = Dataset(None, reference=self, feature_name=self.feature_name, categorical_feature=self.categorical_feature, params=params, free_raw_data=self.free_raw_data) ret._predictor = self._predictor ret.pandas_categorical = self.pandas_categorical ret.used_indices = sorted(used_indices) return ret def save_binary(self, filename): """Save Dataset to a binary file. .. note:: Please note that `init_score` is not saved in binary file. If you need it, please set it again after loading Dataset. Parameters ---------- filename : string Name of the output file. Returns ------- self : Dataset Returns self. """ _safe_call(_LIB.LGBM_DatasetSaveBinary( self.construct().handle, c_str(filename))) return self def _update_params(self, params): if not params: return self params = deepcopy(params) def update(): if not self.params: self.params = params else: self.params_back_up = deepcopy(self.params) self.params.update(params) if self.handle is None: update() elif params is not None: ret = _LIB.LGBM_DatasetUpdateParamChecking( c_str(param_dict_to_str(self.params)), c_str(param_dict_to_str(params))) if ret != 0: # could be updated if data is not freed if self.data is not None: update() self._free_handle() else: raise LightGBMError(_LIB.LGBM_GetLastError().decode('utf-8')) return self def _reverse_update_params(self): if self.handle is None: self.params = deepcopy(self.params_back_up) self.params_back_up = None return self def set_field(self, field_name, data): """Set property into the Dataset. Parameters ---------- field_name : string The field name of the information. data : list, numpy 1-D array, pandas Series or None The array of data to be set. Returns ------- self : Dataset Dataset with set property. """ if self.handle is None: raise Exception(f"Cannot set {field_name} before construct dataset") if data is None: # set to None _safe_call(_LIB.LGBM_DatasetSetField( self.handle, c_str(field_name), None, ctypes.c_int(0), ctypes.c_int(FIELD_TYPE_MAPPER[field_name]))) return self dtype = np.float32 if field_name == 'group': dtype = np.int32 elif field_name == 'init_score': dtype = np.float64 data = list_to_1d_numpy(data, dtype, name=field_name) if data.dtype == np.float32 or data.dtype == np.float64: ptr_data, type_data, _ = c_float_array(data) elif data.dtype == np.int32: ptr_data, type_data, _ = c_int_array(data) else: raise TypeError(f"Expected np.float32/64 or np.int32, met type({data.dtype})") if type_data != FIELD_TYPE_MAPPER[field_name]: raise TypeError("Input type error for set_field") _safe_call(_LIB.LGBM_DatasetSetField( self.handle, c_str(field_name), ptr_data, ctypes.c_int(len(data)), ctypes.c_int(type_data))) self.version += 1 return self def get_field(self, field_name): """Get property from the Dataset. Parameters ---------- field_name : string The field name of the information. Returns ------- info : numpy array A numpy array with information from the Dataset. """ if self.handle is None: raise Exception(f"Cannot get {field_name} before construct Dataset") tmp_out_len = ctypes.c_int(0) out_type = ctypes.c_int(0) ret = ctypes.POINTER(ctypes.c_void_p)() _safe_call(_LIB.LGBM_DatasetGetField( self.handle, c_str(field_name), ctypes.byref(tmp_out_len), ctypes.byref(ret), ctypes.byref(out_type))) if out_type.value != FIELD_TYPE_MAPPER[field_name]: raise TypeError("Return type error for get_field") if tmp_out_len.value == 0: return None if out_type.value == C_API_DTYPE_INT32: return cint32_array_to_numpy(ctypes.cast(ret, ctypes.POINTER(ctypes.c_int32)), tmp_out_len.value) elif out_type.value == C_API_DTYPE_FLOAT32: return cfloat32_array_to_numpy(ctypes.cast(ret, ctypes.POINTER(ctypes.c_float)), tmp_out_len.value) elif out_type.value == C_API_DTYPE_FLOAT64: return cfloat64_array_to_numpy(ctypes.cast(ret, ctypes.POINTER(ctypes.c_double)), tmp_out_len.value) else: raise TypeError("Unknown type") def set_categorical_feature(self, categorical_feature): """Set categorical features. Parameters ---------- categorical_feature : list of int or strings Names or indices of categorical features. Returns ------- self : Dataset Dataset with set categorical features. """ if self.categorical_feature == categorical_feature: return self if self.data is not None: if self.categorical_feature is None: self.categorical_feature = categorical_feature return self._free_handle() elif categorical_feature == 'auto': _log_warning('Using categorical_feature in Dataset.') return self else: _log_warning('categorical_feature in Dataset is overridden.\n' f'New categorical_feature is {sorted(list(categorical_feature))}') self.categorical_feature = categorical_feature return self._free_handle() else: raise LightGBMError("Cannot set categorical feature after freed raw data, " "set free_raw_data=False when construct Dataset to avoid this.") def _set_predictor(self, predictor): """Set predictor for continued training. It is not recommended for user to call this function. Please use init_model argument in engine.train() or engine.cv() instead. """ if predictor is self._predictor and (predictor is None or predictor.current_iteration() == self._predictor.current_iteration()): return self if self.handle is None: self._predictor = predictor elif self.data is not None: self._predictor = predictor self._set_init_score_by_predictor(self._predictor, self.data) elif self.used_indices is not None and self.reference is not None and self.reference.data is not None: self._predictor = predictor self._set_init_score_by_predictor(self._predictor, self.reference.data, self.used_indices) else: raise LightGBMError("Cannot set predictor after freed raw data, " "set free_raw_data=False when construct Dataset to avoid this.") return self def set_reference(self, reference): """Set reference Dataset. Parameters ---------- reference : Dataset Reference that is used as a template to construct the current Dataset. Returns ------- self : Dataset Dataset with set reference. """ self.set_categorical_feature(reference.categorical_feature) \ .set_feature_name(reference.feature_name) \ ._set_predictor(reference._predictor) # we're done if self and reference share a common upstrem reference if self.get_ref_chain().intersection(reference.get_ref_chain()): return self if self.data is not None: self.reference = reference return self._free_handle() else: raise LightGBMError("Cannot set reference after freed raw data, " "set free_raw_data=False when construct Dataset to avoid this.") def set_feature_name(self, feature_name): """Set feature name. Parameters ---------- feature_name : list of strings Feature names. Returns ------- self : Dataset Dataset with set feature name. """ if feature_name != 'auto': self.feature_name = feature_name if self.handle is not None and feature_name is not None and feature_name != 'auto': if len(feature_name) != self.num_feature(): raise ValueError(f"Length of feature_name({len(feature_name)}) and num_feature({self.num_feature()}) don't match") c_feature_name = [c_str(name) for name in feature_name] _safe_call(_LIB.LGBM_DatasetSetFeatureNames( self.handle, c_array(ctypes.c_char_p, c_feature_name), ctypes.c_int(len(feature_name)))) return self def set_label(self, label): """Set label of Dataset. Parameters ---------- label : list, numpy 1-D array, pandas Series / one-column DataFrame or None The label information to be set into Dataset. Returns ------- self : Dataset Dataset with set label. """ self.label = label if self.handle is not None: label = list_to_1d_numpy(_label_from_pandas(label), name='label') self.set_field('label', label) self.label = self.get_field('label') # original values can be modified at cpp side return self def set_weight(self, weight): """Set weight of each instance. Parameters ---------- weight : list, numpy 1-D array, pandas Series or None Weight to be set for each data point. Returns ------- self : Dataset Dataset with set weight. """ if weight is not None and np.all(weight == 1): weight = None self.weight = weight if self.handle is not None and weight is not None: weight = list_to_1d_numpy(weight, name='weight') self.set_field('weight', weight) self.weight = self.get_field('weight') # original values can be modified at cpp side return self def set_init_score(self, init_score): """Set init score of Booster to start from. Parameters ---------- init_score : list, numpy 1-D array, pandas Series or None Init score for Booster. Returns ------- self : Dataset Dataset with set init score. """ self.init_score = init_score if self.handle is not None and init_score is not None: init_score = list_to_1d_numpy(init_score, np.float64, name='init_score') self.set_field('init_score', init_score) self.init_score = self.get_field('init_score') # original values can be modified at cpp side return self def set_group(self, group): """Set group size of Dataset (used for ranking). Parameters ---------- group : list, numpy 1-D array, pandas Series or None Group/query data. Only used in the learning-to-rank task. sum(group) = n_samples. For example, if you have a 100-document dataset with ``group = [10, 20, 40, 10, 10, 10]``, that means that you have 6 groups, where the first 10 records are in the first group, records 11-30 are in the second group, records 31-70 are in the third group, etc. Returns ------- self : Dataset Dataset with set group. """ self.group = group if self.handle is not None and group is not None: group = list_to_1d_numpy(group, np.int32, name='group') self.set_field('group', group) return self def get_feature_name(self): """Get the names of columns (features) in the Dataset. Returns ------- feature_names : list The names of columns (features) in the Dataset. """ if self.handle is None: raise LightGBMError("Cannot get feature_name before construct dataset") num_feature = self.num_feature() tmp_out_len = ctypes.c_int(0) reserved_string_buffer_size = 255 required_string_buffer_size = ctypes.c_size_t(0) string_buffers = [ctypes.create_string_buffer(reserved_string_buffer_size) for _ in range(num_feature)] ptr_string_buffers = (ctypes.c_char_p * num_feature)(*map(ctypes.addressof, string_buffers)) _safe_call(_LIB.LGBM_DatasetGetFeatureNames( self.handle, ctypes.c_int(num_feature), ctypes.byref(tmp_out_len), ctypes.c_size_t(reserved_string_buffer_size), ctypes.byref(required_string_buffer_size), ptr_string_buffers)) if num_feature != tmp_out_len.value: raise ValueError("Length of feature names doesn't equal with num_feature") actual_string_buffer_size = required_string_buffer_size.value # if buffer length is not long enough, reallocate buffers if reserved_string_buffer_size < actual_string_buffer_size: string_buffers = [ctypes.create_string_buffer(actual_string_buffer_size) for _ in range(num_feature)] ptr_string_buffers = (ctypes.c_char_p * num_feature)(*map(ctypes.addressof, string_buffers)) _safe_call(_LIB.LGBM_DatasetGetFeatureNames( self.handle, ctypes.c_int(num_feature), ctypes.byref(tmp_out_len), ctypes.c_size_t(actual_string_buffer_size), ctypes.byref(required_string_buffer_size), ptr_string_buffers)) return [string_buffers[i].value.decode('utf-8') for i in range(num_feature)] def get_label(self): """Get the label of the Dataset. Returns ------- label : numpy array or None The label information from the Dataset. """ if self.label is None: self.label = self.get_field('label') return self.label def get_weight(self): """Get the weight of the Dataset. Returns ------- weight : numpy array or None Weight for each data point from the Dataset. """ if self.weight is None: self.weight = self.get_field('weight') return self.weight def get_init_score(self): """Get the initial score of the Dataset. Returns ------- init_score : numpy array or None Init score of Booster. """ if self.init_score is None: self.init_score = self.get_field('init_score') return self.init_score def get_data(self): """Get the raw data of the Dataset. Returns ------- data : string, numpy array, pandas DataFrame, H2O DataTable's Frame, scipy.sparse, list of numpy arrays or None Raw data used in the Dataset construction. """ if self.handle is None: raise Exception("Cannot get data before construct Dataset") if self.need_slice and self.used_indices is not None and self.reference is not None: self.data = self.reference.data if self.data is not None: if isinstance(self.data, np.ndarray) or scipy.sparse.issparse(self.data): self.data = self.data[self.used_indices, :] elif isinstance(self.data, pd_DataFrame): self.data = self.data.iloc[self.used_indices].copy() elif isinstance(self.data, dt_DataTable): self.data = self.data[self.used_indices, :] else: _log_warning(f"Cannot subset {type(self.data).__name__} type of raw data.\n" "Returning original raw data") self.need_slice = False if self.data is None: raise LightGBMError("Cannot call `get_data` after freed raw data, " "set free_raw_data=False when construct Dataset to avoid this.") return self.data def get_group(self): """Get the group of the Dataset. Returns ------- group : numpy array or None Group/query data. Only used in the learning-to-rank task. sum(group) = n_samples. For example, if you have a 100-document dataset with ``group = [10, 20, 40, 10, 10, 10]``, that means that you have 6 groups, where the first 10 records are in the first group, records 11-30 are in the second group, records 31-70 are in the third group, etc. """ if self.group is None: self.group = self.get_field('group') if self.group is not None: # group data from LightGBM is boundaries data, need to convert to group size self.group = np.diff(self.group) return self.group def num_data(self): """Get the number of rows in the Dataset. Returns ------- number_of_rows : int The number of rows in the Dataset. """ if self.handle is not None: ret = ctypes.c_int(0) _safe_call(_LIB.LGBM_DatasetGetNumData(self.handle, ctypes.byref(ret))) return ret.value else: raise LightGBMError("Cannot get num_data before construct dataset") def num_feature(self): """Get the number of columns (features) in the Dataset. Returns ------- number_of_columns : int The number of columns (features) in the Dataset. """ if self.handle is not None: ret = ctypes.c_int(0) _safe_call(_LIB.LGBM_DatasetGetNumFeature(self.handle, ctypes.byref(ret))) return ret.value else: raise LightGBMError("Cannot get num_feature before construct dataset") def get_ref_chain(self, ref_limit=100): """Get a chain of Dataset objects. Starts with r, then goes to r.reference (if exists), then to r.reference.reference, etc. until we hit ``ref_limit`` or a reference loop. Parameters ---------- ref_limit : int, optional (default=100) The limit number of references. Returns ------- ref_chain : set of Dataset Chain of references of the Datasets. """ head = self ref_chain = set() while len(ref_chain) < ref_limit: if isinstance(head, Dataset): ref_chain.add(head) if (head.reference is not None) and (head.reference not in ref_chain): head = head.reference else: break else: break return ref_chain def add_features_from(self, other): """Add features from other Dataset to the current Dataset. Both Datasets must be constructed before calling this method. Parameters ---------- other : Dataset The Dataset to take features from. Returns ------- self : Dataset Dataset with the new features added. """ if self.handle is None or other.handle is None: raise ValueError('Both source and target Datasets must be constructed before adding features') _safe_call(_LIB.LGBM_DatasetAddFeaturesFrom(self.handle, other.handle)) was_none = self.data is None old_self_data_type = type(self.data).__name__ if other.data is None: self.data = None elif self.data is not None: if isinstance(self.data, np.ndarray): if isinstance(other.data, np.ndarray): self.data = np.hstack((self.data, other.data)) elif scipy.sparse.issparse(other.data): self.data = np.hstack((self.data, other.data.toarray())) elif isinstance(other.data, pd_DataFrame): self.data = np.hstack((self.data, other.data.values)) elif isinstance(other.data, dt_DataTable): self.data = np.hstack((self.data, other.data.to_numpy())) else: self.data = None elif scipy.sparse.issparse(self.data): sparse_format = self.data.getformat() if isinstance(other.data, np.ndarray) or scipy.sparse.issparse(other.data): self.data = scipy.sparse.hstack((self.data, other.data), format=sparse_format) elif isinstance(other.data, pd_DataFrame): self.data = scipy.sparse.hstack((self.data, other.data.values), format=sparse_format) elif isinstance(other.data, dt_DataTable): self.data = scipy.sparse.hstack((self.data, other.data.to_numpy()), format=sparse_format) else: self.data = None elif isinstance(self.data, pd_DataFrame): if not PANDAS_INSTALLED: raise LightGBMError("Cannot add features to DataFrame type of raw data " "without pandas installed. " "Install pandas and restart your session.") if isinstance(other.data, np.ndarray): self.data = concat((self.data, pd_DataFrame(other.data)), axis=1, ignore_index=True) elif scipy.sparse.issparse(other.data): self.data = concat((self.data, pd_DataFrame(other.data.toarray())), axis=1, ignore_index=True) elif isinstance(other.data, pd_DataFrame): self.data = concat((self.data, other.data), axis=1, ignore_index=True) elif isinstance(other.data, dt_DataTable): self.data = concat((self.data, pd_DataFrame(other.data.to_numpy())), axis=1, ignore_index=True) else: self.data = None elif isinstance(self.data, dt_DataTable): if isinstance(other.data, np.ndarray): self.data = dt_DataTable(np.hstack((self.data.to_numpy(), other.data))) elif scipy.sparse.issparse(other.data): self.data = dt_DataTable(np.hstack((self.data.to_numpy(), other.data.toarray()))) elif isinstance(other.data, pd_DataFrame): self.data = dt_DataTable(np.hstack((self.data.to_numpy(), other.data.values))) elif isinstance(other.data, dt_DataTable): self.data = dt_DataTable(np.hstack((self.data.to_numpy(), other.data.to_numpy()))) else: self.data = None else: self.data = None if self.data is None: err_msg = (f"Cannot add features from {type(other.data).__name__} type of raw data to " f"{old_self_data_type} type of raw data.\n") err_msg += ("Set free_raw_data=False when construct Dataset to avoid this" if was_none else "Freeing raw data") _log_warning(err_msg) self.feature_name = self.get_feature_name() _log_warning("Reseting categorical features.\n" "You can set new categorical features via ``set_categorical_feature`` method") self.categorical_feature = "auto" self.pandas_categorical = None return self def _dump_text(self, filename): """Save Dataset to a text file. This format cannot be loaded back in by LightGBM, but is useful for debugging purposes. Parameters ---------- filename : string Name of the output file. Returns ------- self : Dataset Returns self. """ _safe_call(_LIB.LGBM_DatasetDumpText( self.construct().handle, c_str(filename))) return self class Booster: """Booster in LightGBM.""" def __init__(self, params=None, train_set=None, model_file=None, model_str=None, silent=False): """Initialize the Booster. Parameters ---------- params : dict or None, optional (default=None) Parameters for Booster. train_set : Dataset or None, optional (default=None) Training dataset. model_file : string or None, optional (default=None) Path to the model file. model_str : string or None, optional (default=None) Model will be loaded from this string. silent : bool, optional (default=False) Whether to print messages during construction. """ self.handle = None self.network = False self.__need_reload_eval_info = True self._train_data_name = "training" self.__attr = {} self.__set_objective_to_none = False self.best_iteration = -1 self.best_score = {} params = {} if params is None else deepcopy(params) # user can set verbose with params, it has higher priority if not any(verbose_alias in params for verbose_alias in _ConfigAliases.get("verbosity")) and silent: params["verbose"] = -1 if train_set is not None: # Training task if not isinstance(train_set, Dataset): raise TypeError(f'Training data should be Dataset instance, met {type(train_set).__name__}') params = _choose_param_value( main_param_name="machines", params=params, default_value=None ) # if "machines" is given, assume user wants to do distributed learning, and set up network if params["machines"] is None: params.pop("machines", None) else: machines = params["machines"] if isinstance(machines, str): num_machines_from_machine_list = len(machines.split(',')) elif isinstance(machines, (list, set)): num_machines_from_machine_list = len(machines) machines = ','.join(machines) else: raise ValueError("Invalid machines in params.") params = _choose_param_value( main_param_name="num_machines", params=params, default_value=num_machines_from_machine_list ) params = _choose_param_value( main_param_name="local_listen_port", params=params, default_value=12400 ) self.set_network( machines=machines, local_listen_port=params["local_listen_port"], listen_time_out=params.get("time_out", 120), num_machines=params["num_machines"] ) # construct booster object train_set.construct() # copy the parameters from train_set params.update(train_set.get_params()) params_str = param_dict_to_str(params) self.handle = ctypes.c_void_p() _safe_call(_LIB.LGBM_BoosterCreate( train_set.handle, c_str(params_str), ctypes.byref(self.handle))) # save reference to data self.train_set = train_set self.valid_sets = [] self.name_valid_sets = [] self.__num_dataset = 1 self.__init_predictor = train_set._predictor if self.__init_predictor is not None: _safe_call(_LIB.LGBM_BoosterMerge( self.handle, self.__init_predictor.handle)) out_num_class = ctypes.c_int(0) _safe_call(_LIB.LGBM_BoosterGetNumClasses( self.handle, ctypes.byref(out_num_class))) self.__num_class = out_num_class.value # buffer for inner predict self.__inner_predict_buffer = [None] self.__is_predicted_cur_iter = [False] self.__get_eval_info() self.pandas_categorical = train_set.pandas_categorical self.train_set_version = train_set.version elif model_file is not None: # Prediction task out_num_iterations = ctypes.c_int(0) self.handle = ctypes.c_void_p() _safe_call(_LIB.LGBM_BoosterCreateFromModelfile( c_str(model_file), ctypes.byref(out_num_iterations), ctypes.byref(self.handle))) out_num_class = ctypes.c_int(0) _safe_call(_LIB.LGBM_BoosterGetNumClasses( self.handle, ctypes.byref(out_num_class))) self.__num_class = out_num_class.value self.pandas_categorical = _load_pandas_categorical(file_name=model_file) elif model_str is not None: self.model_from_string(model_str, not silent) else: raise TypeError('Need at least one training dataset or model file or model string ' 'to create Booster instance') self.params = params def __del__(self): try: if self.network: self.free_network() except AttributeError: pass try: if self.handle is not None: _safe_call(_LIB.LGBM_BoosterFree(self.handle)) except AttributeError: pass def __copy__(self): return self.__deepcopy__(None) def __deepcopy__(self, _): model_str = self.model_to_string(num_iteration=-1) booster = Booster(model_str=model_str) return booster def __getstate__(self): this = self.__dict__.copy() handle = this['handle'] this.pop('train_set', None) this.pop('valid_sets', None) if handle is not None: this["handle"] = self.model_to_string(num_iteration=-1) return this def __setstate__(self, state): model_str = state.get('handle', None) if model_str is not None: handle = ctypes.c_void_p() out_num_iterations = ctypes.c_int(0) _safe_call(_LIB.LGBM_BoosterLoadModelFromString( c_str(model_str), ctypes.byref(out_num_iterations), ctypes.byref(handle))) state['handle'] = handle self.__dict__.update(state) def free_dataset(self): """Free Booster's Datasets. Returns ------- self : Booster Booster without Datasets. """ self.__dict__.pop('train_set', None) self.__dict__.pop('valid_sets', None) self.__num_dataset = 0 return self def _free_buffer(self): self.__inner_predict_buffer = [] self.__is_predicted_cur_iter = [] return self def set_network( self, machines: Union[List[str], Set[str], str], local_listen_port: int = 12400, listen_time_out: int = 120, num_machines: int = 1 ) -> "Booster": """Set the network configuration. Parameters ---------- machines : list, set or string Names of machines. local_listen_port : int, optional (default=12400) TCP listen port for local machines. listen_time_out : int, optional (default=120) Socket time-out in minutes. num_machines : int, optional (default=1) The number of machines for distributed learning application. Returns ------- self : Booster Booster with set network. """ if isinstance(machines, (list, set)): machines = ','.join(machines) _safe_call(_LIB.LGBM_NetworkInit(c_str(machines), ctypes.c_int(local_listen_port), ctypes.c_int(listen_time_out), ctypes.c_int(num_machines))) self.network = True return self def free_network(self): """Free Booster's network. Returns ------- self : Booster Booster with freed network. """ _safe_call(_LIB.LGBM_NetworkFree()) self.network = False return self def trees_to_dataframe(self): """Parse the fitted model and return in an easy-to-read pandas DataFrame. The returned DataFrame has the following columns. - ``tree_index`` : int64, which tree a node belongs to. 0-based, so a value of ``6``, for example, means "this node is in the 7th tree". - ``node_depth`` : int64, how far a node is from the root of the tree. The root node has a value of ``1``, its direct children are ``2``, etc. - ``node_index`` : string, unique identifier for a node. - ``left_child`` : string, ``node_index`` of the child node to the left of a split. ``None`` for leaf nodes. - ``right_child`` : string, ``node_index`` of the child node to the right of a split. ``None`` for leaf nodes. - ``parent_index`` : string, ``node_index`` of this node's parent. ``None`` for the root node. - ``split_feature`` : string, name of the feature used for splitting. ``None`` for leaf nodes. - ``split_gain`` : float64, gain from adding this split to the tree. ``NaN`` for leaf nodes. - ``threshold`` : float64, value of the feature used to decide which side of the split a record will go down. ``NaN`` for leaf nodes. - ``decision_type`` : string, logical operator describing how to compare a value to ``threshold``. For example, ``split_feature = "Column_10", threshold = 15, decision_type = "<="`` means that records where ``Column_10 <= 15`` follow the left side of the split, otherwise follows the right side of the split. ``None`` for leaf nodes. - ``missing_direction`` : string, split direction that missing values should go to. ``None`` for leaf nodes. - ``missing_type`` : string, describes what types of values are treated as missing. - ``value`` : float64, predicted value for this leaf node, multiplied by the learning rate. - ``weight`` : float64 or int64, sum of hessian (second-order derivative of objective), summed over observations that fall in this node. - ``count`` : int64, number of records in the training data that fall into this node. Returns ------- result : pandas DataFrame Returns a pandas DataFrame of the parsed model. """ if not PANDAS_INSTALLED: raise LightGBMError('This method cannot be run without pandas installed. ' 'You must install pandas and restart your session to use this method.') if self.num_trees() == 0: raise LightGBMError('There are no trees in this Booster and thus nothing to parse') def _is_split_node(tree): return 'split_index' in tree.keys() def create_node_record(tree, node_depth=1, tree_index=None, feature_names=None, parent_node=None): def _get_node_index(tree, tree_index): tree_num = f'{tree_index}-' if tree_index is not None else '' is_split = _is_split_node(tree) node_type = 'S' if is_split else 'L' # if a single node tree it won't have `leaf_index` so return 0 node_num = tree.get('split_index' if is_split else 'leaf_index', 0) return f"{tree_num}{node_type}{node_num}" def _get_split_feature(tree, feature_names): if _is_split_node(tree): if feature_names is not None: feature_name = feature_names[tree['split_feature']] else: feature_name = tree['split_feature'] else: feature_name = None return feature_name def _is_single_node_tree(tree): return set(tree.keys()) == {'leaf_value'} # Create the node record, and populate universal data members node = OrderedDict() node['tree_index'] = tree_index node['node_depth'] = node_depth node['node_index'] = _get_node_index(tree, tree_index) node['left_child'] = None node['right_child'] = None node['parent_index'] = parent_node node['split_feature'] = _get_split_feature(tree, feature_names) node['split_gain'] = None node['threshold'] = None node['decision_type'] = None node['missing_direction'] = None node['missing_type'] = None node['value'] = None node['weight'] = None node['count'] = None # Update values to reflect node type (leaf or split) if _is_split_node(tree): node['left_child'] = _get_node_index(tree['left_child'], tree_index) node['right_child'] = _get_node_index(tree['right_child'], tree_index) node['split_gain'] = tree['split_gain'] node['threshold'] = tree['threshold'] node['decision_type'] = tree['decision_type'] node['missing_direction'] = 'left' if tree['default_left'] else 'right' node['missing_type'] = tree['missing_type'] node['value'] = tree['internal_value'] node['weight'] = tree['internal_weight'] node['count'] = tree['internal_count'] else: node['value'] = tree['leaf_value'] if not _is_single_node_tree(tree): node['weight'] = tree['leaf_weight'] node['count'] = tree['leaf_count'] return node def tree_dict_to_node_list(tree, node_depth=1, tree_index=None, feature_names=None, parent_node=None): node = create_node_record(tree, node_depth=node_depth, tree_index=tree_index, feature_names=feature_names, parent_node=parent_node) res = [node] if _is_split_node(tree): # traverse the next level of the tree children = ['left_child', 'right_child'] for child in children: subtree_list = tree_dict_to_node_list( tree[child], node_depth=node_depth + 1, tree_index=tree_index, feature_names=feature_names, parent_node=node['node_index']) # In tree format, "subtree_list" is a list of node records (dicts), # and we add node to the list. res.extend(subtree_list) return res model_dict = self.dump_model() feature_names = model_dict['feature_names'] model_list = [] for tree in model_dict['tree_info']: model_list.extend(tree_dict_to_node_list(tree['tree_structure'], tree_index=tree['tree_index'], feature_names=feature_names)) return pd_DataFrame(model_list, columns=model_list[0].keys()) def set_train_data_name(self, name): """Set the name to the training Dataset. Parameters ---------- name : string Name for the training Dataset. Returns ------- self : Booster Booster with set training Dataset name. """ self._train_data_name = name return self def add_valid(self, data, name): """Add validation data. Parameters ---------- data : Dataset Validation data. name : string Name of validation data. Returns ------- self : Booster Booster with set validation data. """ if not isinstance(data, Dataset): raise TypeError(f'Validation data should be Dataset instance, met {type(data).__name__}') if data._predictor is not self.__init_predictor: raise LightGBMError("Add validation data failed, " "you should use same predictor for these data") _safe_call(_LIB.LGBM_BoosterAddValidData( self.handle, data.construct().handle)) self.valid_sets.append(data) self.name_valid_sets.append(name) self.__num_dataset += 1 self.__inner_predict_buffer.append(None) self.__is_predicted_cur_iter.append(False) return self def reset_parameter(self, params): """Reset parameters of Booster. Parameters ---------- params : dict New parameters for Booster. Returns ------- self : Booster Booster with new parameters. """ params_str = param_dict_to_str(params) if params_str: _safe_call(_LIB.LGBM_BoosterResetParameter( self.handle, c_str(params_str))) self.params.update(params) return self def update(self, train_set=None, fobj=None): """Update Booster for one iteration. Parameters ---------- train_set : Dataset or None, optional (default=None) Training data. If None, last training data is used. fobj : callable or None, optional (default=None) Customized objective function. Should accept two parameters: preds, train_data, and return (grad, hess). preds : list or numpy 1-D array The predicted values. Predicted values are returned before any transformation, e.g. they are raw margin instead of probability of positive class for binary task. train_data : Dataset The training dataset. grad : list or numpy 1-D array The value of the first order derivative (gradient) of the loss with respect to the elements of preds for each sample point. hess : list or numpy 1-D array The value of the second order derivative (Hessian) of the loss with respect to the elements of preds for each sample point. For multi-class task, the preds is group by class_id first, then group by row_id. If you want to get i-th row preds in j-th class, the access way is score[j * num_data + i] and you should group grad and hess in this way as well. Returns ------- is_finished : bool Whether the update was successfully finished. """ # need reset training data if train_set is None and self.train_set_version != self.train_set.version: train_set = self.train_set is_the_same_train_set = False else: is_the_same_train_set = train_set is self.train_set and self.train_set_version == train_set.version if train_set is not None and not is_the_same_train_set: if not isinstance(train_set, Dataset): raise TypeError(f'Training data should be Dataset instance, met {type(train_set).__name__}') if train_set._predictor is not self.__init_predictor: raise LightGBMError("Replace training data failed, " "you should use same predictor for these data") self.train_set = train_set _safe_call(_LIB.LGBM_BoosterResetTrainingData( self.handle, self.train_set.construct().handle)) self.__inner_predict_buffer[0] = None self.train_set_version = self.train_set.version is_finished = ctypes.c_int(0) if fobj is None: if self.__set_objective_to_none: raise LightGBMError('Cannot update due to null objective function.') _safe_call(_LIB.LGBM_BoosterUpdateOneIter( self.handle, ctypes.byref(is_finished))) self.__is_predicted_cur_iter = [False for _ in range(self.__num_dataset)] return is_finished.value == 1 else: if not self.__set_objective_to_none: self.reset_parameter({"objective": "none"}).__set_objective_to_none = True grad, hess = fobj(self.__inner_predict(0), self.train_set) return self.__boost(grad, hess) def __boost(self, grad, hess): """Boost Booster for one iteration with customized gradient statistics. .. note:: Score is returned before any transformation, e.g. it is raw margin instead of probability of positive class for binary task. For multi-class task, the score is group by class_id first, then group by row_id. If you want to get i-th row score in j-th class, the access way is score[j * num_data + i] and you should group grad and hess in this way as well. Parameters ---------- grad : list or numpy 1-D array The value of the first order derivative (gradient) of the loss with respect to the elements of score for each sample point. hess : list or numpy 1-D array The value of the second order derivative (Hessian) of the loss with respect to the elements of score for each sample point. Returns ------- is_finished : bool Whether the boost was successfully finished. """ grad = list_to_1d_numpy(grad, name='gradient') hess = list_to_1d_numpy(hess, name='hessian') assert grad.flags.c_contiguous assert hess.flags.c_contiguous if len(grad) != len(hess): raise ValueError(f"Lengths of gradient({len(grad)}) and hessian({len(hess)}) don't match") is_finished = ctypes.c_int(0) _safe_call(_LIB.LGBM_BoosterUpdateOneIterCustom( self.handle, grad.ctypes.data_as(ctypes.POINTER(ctypes.c_float)), hess.ctypes.data_as(ctypes.POINTER(ctypes.c_float)), ctypes.byref(is_finished))) self.__is_predicted_cur_iter = [False for _ in range(self.__num_dataset)] return is_finished.value == 1 def rollback_one_iter(self): """Rollback one iteration. Returns ------- self : Booster Booster with rolled back one iteration. """ _safe_call(_LIB.LGBM_BoosterRollbackOneIter( self.handle)) self.__is_predicted_cur_iter = [False for _ in range(self.__num_dataset)] return self def current_iteration(self): """Get the index of the current iteration. Returns ------- cur_iter : int The index of the current iteration. """ out_cur_iter = ctypes.c_int(0) _safe_call(_LIB.LGBM_BoosterGetCurrentIteration( self.handle, ctypes.byref(out_cur_iter))) return out_cur_iter.value def num_model_per_iteration(self): """Get number of models per iteration. Returns ------- model_per_iter : int The number of models per iteration. """ model_per_iter = ctypes.c_int(0) _safe_call(_LIB.LGBM_BoosterNumModelPerIteration( self.handle, ctypes.byref(model_per_iter))) return model_per_iter.value def num_trees(self): """Get number of weak sub-models. Returns ------- num_trees : int The number of weak sub-models. """ num_trees = ctypes.c_int(0) _safe_call(_LIB.LGBM_BoosterNumberOfTotalModel( self.handle, ctypes.byref(num_trees))) return num_trees.value def upper_bound(self): """Get upper bound value of a model. Returns ------- upper_bound : double Upper bound value of the model. """ ret = ctypes.c_double(0) _safe_call(_LIB.LGBM_BoosterGetUpperBoundValue( self.handle, ctypes.byref(ret))) return ret.value def lower_bound(self): """Get lower bound value of a model. Returns ------- lower_bound : double Lower bound value of the model. """ ret = ctypes.c_double(0) _safe_call(_LIB.LGBM_BoosterGetLowerBoundValue( self.handle, ctypes.byref(ret))) return ret.value def eval(self, data, name, feval=None): """Evaluate for data. Parameters ---------- data : Dataset Data for the evaluating. name : string Name of the data. feval : callable or None, optional (default=None) Customized evaluation function. Should accept two parameters: preds, eval_data, and return (eval_name, eval_result, is_higher_better) or list of such tuples. preds : list or numpy 1-D array The predicted values. If ``fobj`` is specified, predicted values are returned before any transformation, e.g. they are raw margin instead of probability of positive class for binary task in this case. eval_data : Dataset The evaluation dataset. eval_name : string The name of evaluation function (without whitespace). eval_result : float The eval result. is_higher_better : bool Is eval result higher better, e.g. AUC is ``is_higher_better``. For multi-class task, the preds is group by class_id first, then group by row_id. If you want to get i-th row preds in j-th class, the access way is preds[j * num_data + i]. Returns ------- result : list List with evaluation results. """ if not isinstance(data, Dataset): raise TypeError("Can only eval for Dataset instance") data_idx = -1 if data is self.train_set: data_idx = 0 else: for i in range(len(self.valid_sets)): if data is self.valid_sets[i]: data_idx = i + 1 break # need to push new valid data if data_idx == -1: self.add_valid(data, name) data_idx = self.__num_dataset - 1 return self.__inner_eval(name, data_idx, feval) def eval_train(self, feval=None): """Evaluate for training data. Parameters ---------- feval : callable or None, optional (default=None) Customized evaluation function. Should accept two parameters: preds, train_data, and return (eval_name, eval_result, is_higher_better) or list of such tuples. preds : list or numpy 1-D array The predicted values. If ``fobj`` is specified, predicted values are returned before any transformation, e.g. they are raw margin instead of probability of positive class for binary task in this case. train_data : Dataset The training dataset. eval_name : string The name of evaluation function (without whitespace). eval_result : float The eval result. is_higher_better : bool Is eval result higher better, e.g. AUC is ``is_higher_better``. For multi-class task, the preds is group by class_id first, then group by row_id. If you want to get i-th row preds in j-th class, the access way is preds[j * num_data + i]. Returns ------- result : list List with evaluation results. """ return self.__inner_eval(self._train_data_name, 0, feval) def eval_valid(self, feval=None): """Evaluate for validation data. Parameters ---------- feval : callable or None, optional (default=None) Customized evaluation function. Should accept two parameters: preds, valid_data, and return (eval_name, eval_result, is_higher_better) or list of such tuples. preds : list or numpy 1-D array The predicted values. If ``fobj`` is specified, predicted values are returned before any transformation, e.g. they are raw margin instead of probability of positive class for binary task in this case. valid_data : Dataset The validation dataset. eval_name : string The name of evaluation function (without whitespace). eval_result : float The eval result. is_higher_better : bool Is eval result higher better, e.g. AUC is ``is_higher_better``. For multi-class task, the preds is group by class_id first, then group by row_id. If you want to get i-th row preds in j-th class, the access way is preds[j * num_data + i]. Returns ------- result : list List with evaluation results. """ return [item for i in range(1, self.__num_dataset) for item in self.__inner_eval(self.name_valid_sets[i - 1], i, feval)] def save_model(self, filename, num_iteration=None, start_iteration=0, importance_type='split'): """Save Booster to file. Parameters ---------- filename : string Filename to save Booster. num_iteration : int or None, optional (default=None) Index of the iteration that should be saved. If None, if the best iteration exists, it is saved; otherwise, all iterations are saved. If <= 0, all iterations are saved. start_iteration : int, optional (default=0) Start index of the iteration that should be saved. importance_type : string, optional (default="split") What type of feature importance should be saved. If "split", result contains numbers of times the feature is used in a model. If "gain", result contains total gains of splits which use the feature. Returns ------- self : Booster Returns self. """ if num_iteration is None: num_iteration = self.best_iteration importance_type_int = FEATURE_IMPORTANCE_TYPE_MAPPER[importance_type] _safe_call(_LIB.LGBM_BoosterSaveModel( self.handle, ctypes.c_int(start_iteration), ctypes.c_int(num_iteration), ctypes.c_int(importance_type_int), c_str(filename))) _dump_pandas_categorical(self.pandas_categorical, filename) return self def shuffle_models(self, start_iteration=0, end_iteration=-1): """Shuffle models. Parameters ---------- start_iteration : int, optional (default=0) The first iteration that will be shuffled. end_iteration : int, optional (default=-1) The last iteration that will be shuffled. If <= 0, means the last available iteration. Returns ------- self : Booster Booster with shuffled models. """ _safe_call(_LIB.LGBM_BoosterShuffleModels( self.handle, ctypes.c_int(start_iteration), ctypes.c_int(end_iteration))) return self def model_from_string(self, model_str, verbose=True): """Load Booster from a string. Parameters ---------- model_str : string Model will be loaded from this string. verbose : bool, optional (default=True) Whether to print messages while loading model. Returns ------- self : Booster Loaded Booster object. """ if self.handle is not None: _safe_call(_LIB.LGBM_BoosterFree(self.handle)) self._free_buffer() self.handle = ctypes.c_void_p() out_num_iterations = ctypes.c_int(0) _safe_call(_LIB.LGBM_BoosterLoadModelFromString( c_str(model_str), ctypes.byref(out_num_iterations), ctypes.byref(self.handle))) out_num_class = ctypes.c_int(0) _safe_call(_LIB.LGBM_BoosterGetNumClasses( self.handle, ctypes.byref(out_num_class))) if verbose: _log_info(f'Finished loading model, total used {int(out_num_iterations.value)} iterations') self.__num_class = out_num_class.value self.pandas_categorical = _load_pandas_categorical(model_str=model_str) return self def model_to_string(self, num_iteration=None, start_iteration=0, importance_type='split'): """Save Booster to string. Parameters ---------- num_iteration : int or None, optional (default=None) Index of the iteration that should be saved. If None, if the best iteration exists, it is saved; otherwise, all iterations are saved. If <= 0, all iterations are saved. start_iteration : int, optional (default=0) Start index of the iteration that should be saved. importance_type : string, optional (default="split") What type of feature importance should be saved. If "split", result contains numbers of times the feature is used in a model. If "gain", result contains total gains of splits which use the feature. Returns ------- str_repr : string String representation of Booster. """ if num_iteration is None: num_iteration = self.best_iteration importance_type_int = FEATURE_IMPORTANCE_TYPE_MAPPER[importance_type] buffer_len = 1 << 20 tmp_out_len = ctypes.c_int64(0) string_buffer = ctypes.create_string_buffer(buffer_len) ptr_string_buffer = ctypes.c_char_p(*[ctypes.addressof(string_buffer)]) _safe_call(_LIB.LGBM_BoosterSaveModelToString( self.handle, ctypes.c_int(start_iteration), ctypes.c_int(num_iteration), ctypes.c_int(importance_type_int), ctypes.c_int64(buffer_len), ctypes.byref(tmp_out_len), ptr_string_buffer)) actual_len = tmp_out_len.value # if buffer length is not long enough, re-allocate a buffer if actual_len > buffer_len: string_buffer = ctypes.create_string_buffer(actual_len) ptr_string_buffer = ctypes.c_char_p(*[ctypes.addressof(string_buffer)]) _safe_call(_LIB.LGBM_BoosterSaveModelToString( self.handle, ctypes.c_int(start_iteration), ctypes.c_int(num_iteration), ctypes.c_int(importance_type_int), ctypes.c_int64(actual_len), ctypes.byref(tmp_out_len), ptr_string_buffer)) ret = string_buffer.value.decode('utf-8') ret += _dump_pandas_categorical(self.pandas_categorical) return ret def dump_model(self, num_iteration=None, start_iteration=0, importance_type='split'): """Dump Booster to JSON format. Parameters ---------- num_iteration : int or None, optional (default=None) Index of the iteration that should be dumped. If None, if the best iteration exists, it is dumped; otherwise, all iterations are dumped. If <= 0, all iterations are dumped. start_iteration : int, optional (default=0) Start index of the iteration that should be dumped. importance_type : string, optional (default="split") What type of feature importance should be dumped. If "split", result contains numbers of times the feature is used in a model. If "gain", result contains total gains of splits which use the feature. Returns ------- json_repr : dict JSON format of Booster. """ if num_iteration is None: num_iteration = self.best_iteration importance_type_int = FEATURE_IMPORTANCE_TYPE_MAPPER[importance_type] buffer_len = 1 << 20 tmp_out_len = ctypes.c_int64(0) string_buffer = ctypes.create_string_buffer(buffer_len) ptr_string_buffer = ctypes.c_char_p(*[ctypes.addressof(string_buffer)]) _safe_call(_LIB.LGBM_BoosterDumpModel( self.handle, ctypes.c_int(start_iteration), ctypes.c_int(num_iteration), ctypes.c_int(importance_type_int), ctypes.c_int64(buffer_len), ctypes.byref(tmp_out_len), ptr_string_buffer)) actual_len = tmp_out_len.value # if buffer length is not long enough, reallocate a buffer if actual_len > buffer_len: string_buffer = ctypes.create_string_buffer(actual_len) ptr_string_buffer = ctypes.c_char_p(*[ctypes.addressof(string_buffer)]) _safe_call(_LIB.LGBM_BoosterDumpModel( self.handle, ctypes.c_int(start_iteration), ctypes.c_int(num_iteration), ctypes.c_int(importance_type_int), ctypes.c_int64(actual_len), ctypes.byref(tmp_out_len), ptr_string_buffer)) ret = json.loads(string_buffer.value.decode('utf-8')) ret['pandas_categorical'] = json.loads(json.dumps(self.pandas_categorical, default=json_default_with_numpy)) return ret def predict(self, data, start_iteration=0, num_iteration=None, raw_score=False, pred_leaf=False, pred_contrib=False, data_has_header=False, is_reshape=True, **kwargs): """Make a prediction. Parameters ---------- data : string, numpy array, pandas DataFrame, H2O DataTable's Frame or scipy.sparse Data source for prediction. If string, it represents the path to txt file. start_iteration : int, optional (default=0) Start index of the iteration to predict. If <= 0, starts from the first iteration. num_iteration : int or None, optional (default=None) Total number of iterations used in the prediction. If None, if the best iteration exists and start_iteration <= 0, the best iteration is used; otherwise, all iterations from ``start_iteration`` are used (no limits). If <= 0, all iterations from ``start_iteration`` are used (no limits). raw_score : bool, optional (default=False) Whether to predict raw scores. pred_leaf : bool, optional (default=False) Whether to predict leaf index. pred_contrib : bool, optional (default=False) Whether to predict feature contributions. .. note:: If you want to get more explanations for your model's predictions using SHAP values, like SHAP interaction values, you can install the shap package (https://github.com/slundberg/shap). Note that unlike the shap package, with ``pred_contrib`` we return a matrix with an extra column, where the last column is the expected value. data_has_header : bool, optional (default=False) Whether the data has header. Used only if data is string. is_reshape : bool, optional (default=True) If True, result is reshaped to [nrow, ncol]. **kwargs Other parameters for the prediction. Returns ------- result : numpy array, scipy.sparse or list of scipy.sparse Prediction result. Can be sparse or a list of sparse objects (each element represents predictions for one class) for feature contributions (when ``pred_contrib=True``). """ predictor = self._to_predictor(deepcopy(kwargs)) if num_iteration is None: if start_iteration <= 0: num_iteration = self.best_iteration else: num_iteration = -1 return predictor.predict(data, start_iteration, num_iteration, raw_score, pred_leaf, pred_contrib, data_has_header, is_reshape) def refit(self, data, label, decay_rate=0.9, **kwargs): """Refit the existing Booster by new data. Parameters ---------- data : string, numpy array, pandas DataFrame, H2O DataTable's Frame or scipy.sparse Data source for refit. If string, it represents the path to txt file. label : list, numpy 1-D array or pandas Series / one-column DataFrame Label for refit. decay_rate : float, optional (default=0.9) Decay rate of refit, will use ``leaf_output = decay_rate * old_leaf_output + (1.0 - decay_rate) * new_leaf_output`` to refit trees. **kwargs Other parameters for refit. These parameters will be passed to ``predict`` method. Returns ------- result : Booster Refitted Booster. """ if self.__set_objective_to_none: raise LightGBMError('Cannot refit due to null objective function.') predictor = self._to_predictor(deepcopy(kwargs)) leaf_preds = predictor.predict(data, -1, pred_leaf=True) nrow, ncol = leaf_preds.shape out_is_linear = ctypes.c_bool(False) _safe_call(_LIB.LGBM_BoosterGetLinear( self.handle, ctypes.byref(out_is_linear))) new_params = deepcopy(self.params) new_params["linear_tree"] = out_is_linear.value train_set = Dataset(data, label, silent=True, params=new_params) new_params['refit_decay_rate'] = decay_rate new_booster = Booster(new_params, train_set) # Copy models _safe_call(_LIB.LGBM_BoosterMerge( new_booster.handle, predictor.handle)) leaf_preds = leaf_preds.reshape(-1) ptr_data, _, _ = c_int_array(leaf_preds) _safe_call(_LIB.LGBM_BoosterRefit( new_booster.handle, ptr_data, ctypes.c_int32(nrow), ctypes.c_int32(ncol))) new_booster.network = self.network new_booster.__attr = self.__attr.copy() return new_booster def get_leaf_output(self, tree_id, leaf_id): """Get the output of a leaf. Parameters ---------- tree_id : int The index of the tree. leaf_id : int The index of the leaf in the tree. Returns ------- result : float The output of the leaf. """ ret = ctypes.c_double(0) _safe_call(_LIB.LGBM_BoosterGetLeafValue( self.handle, ctypes.c_int(tree_id), ctypes.c_int(leaf_id), ctypes.byref(ret))) return ret.value def _to_predictor(self, pred_parameter=None): """Convert to predictor.""" predictor = _InnerPredictor(booster_handle=self.handle, pred_parameter=pred_parameter) predictor.pandas_categorical = self.pandas_categorical return predictor def num_feature(self): """Get number of features. Returns ------- num_feature : int The number of features. """ out_num_feature = ctypes.c_int(0) _safe_call(_LIB.LGBM_BoosterGetNumFeature( self.handle, ctypes.byref(out_num_feature))) return out_num_feature.value def feature_name(self): """Get names of features. Returns ------- result : list List with names of features. """ num_feature = self.num_feature() # Get name of features tmp_out_len = ctypes.c_int(0) reserved_string_buffer_size = 255 required_string_buffer_size = ctypes.c_size_t(0) string_buffers = [ctypes.create_string_buffer(reserved_string_buffer_size) for _ in range(num_feature)] ptr_string_buffers = (ctypes.c_char_p * num_feature)(*map(ctypes.addressof, string_buffers)) _safe_call(_LIB.LGBM_BoosterGetFeatureNames( self.handle, ctypes.c_int(num_feature), ctypes.byref(tmp_out_len), ctypes.c_size_t(reserved_string_buffer_size), ctypes.byref(required_string_buffer_size), ptr_string_buffers)) if num_feature != tmp_out_len.value: raise ValueError("Length of feature names doesn't equal with num_feature") actual_string_buffer_size = required_string_buffer_size.value # if buffer length is not long enough, reallocate buffers if reserved_string_buffer_size < actual_string_buffer_size: string_buffers = [ctypes.create_string_buffer(actual_string_buffer_size) for _ in range(num_feature)] ptr_string_buffers = (ctypes.c_char_p * num_feature)(*map(ctypes.addressof, string_buffers)) _safe_call(_LIB.LGBM_BoosterGetFeatureNames( self.handle, ctypes.c_int(num_feature), ctypes.byref(tmp_out_len), ctypes.c_size_t(actual_string_buffer_size), ctypes.byref(required_string_buffer_size), ptr_string_buffers)) return [string_buffers[i].value.decode('utf-8') for i in range(num_feature)] def feature_importance(self, importance_type='split', iteration=None): """Get feature importances. Parameters ---------- importance_type : string, optional (default="split") How the importance is calculated. If "split", result contains numbers of times the feature is used in a model. If "gain", result contains total gains of splits which use the feature. iteration : int or None, optional (default=None) Limit number of iterations in the feature importance calculation. If None, if the best iteration exists, it is used; otherwise, all trees are used. If <= 0, all trees are used (no limits). Returns ------- result : numpy array Array with feature importances. """ if iteration is None: iteration = self.best_iteration importance_type_int = FEATURE_IMPORTANCE_TYPE_MAPPER[importance_type] result = np.zeros(self.num_feature(), dtype=np.float64) _safe_call(_LIB.LGBM_BoosterFeatureImportance( self.handle, ctypes.c_int(iteration), ctypes.c_int(importance_type_int), result.ctypes.data_as(ctypes.POINTER(ctypes.c_double)))) if importance_type_int == 0: return result.astype(np.int32) else: return result def get_split_value_histogram(self, feature, bins=None, xgboost_style=False): """Get split value histogram for the specified feature. Parameters ---------- feature : int or string The feature name or index the histogram is calculated for. If int, interpreted as index. If string, interpreted as name. .. warning:: Categorical features are not supported. bins : int, string or None, optional (default=None) The maximum number of bins. If None, or int and > number of unique split values and ``xgboost_style=True``, the number of bins equals number of unique split values. If string, it should be one from the list of the supported values by ``numpy.histogram()`` function. xgboost_style : bool, optional (default=False) Whether the returned result should be in the same form as it is in XGBoost. If False, the returned value is tuple of 2 numpy arrays as it is in ``numpy.histogram()`` function. If True, the returned value is matrix, in which the first column is the right edges of non-empty bins and the second one is the histogram values. Returns ------- result_tuple : tuple of 2 numpy arrays If ``xgboost_style=False``, the values of the histogram of used splitting values for the specified feature and the bin edges. result_array_like : numpy array or pandas DataFrame (if pandas is installed) If ``xgboost_style=True``, the histogram of used splitting values for the specified feature. """ def add(root): """Recursively add thresholds.""" if 'split_index' in root: # non-leaf if feature_names is not None and isinstance(feature, str): split_feature = feature_names[root['split_feature']] else: split_feature = root['split_feature'] if split_feature == feature: if isinstance(root['threshold'], str): raise LightGBMError('Cannot compute split value histogram for the categorical feature') else: values.append(root['threshold']) add(root['left_child']) add(root['right_child']) model = self.dump_model() feature_names = model.get('feature_names') tree_infos = model['tree_info'] values = [] for tree_info in tree_infos: add(tree_info['tree_structure']) if bins is None or isinstance(bins, int) and xgboost_style: n_unique = len(np.unique(values)) bins = max(min(n_unique, bins) if bins is not None else n_unique, 1) hist, bin_edges = np.histogram(values, bins=bins) if xgboost_style: ret = np.column_stack((bin_edges[1:], hist)) ret = ret[ret[:, 1] > 0] if PANDAS_INSTALLED: return pd_DataFrame(ret, columns=['SplitValue', 'Count']) else: return ret else: return hist, bin_edges def __inner_eval(self, data_name, data_idx, feval=None): """Evaluate training or validation data.""" if data_idx >= self.__num_dataset: raise ValueError("Data_idx should be smaller than number of dataset") self.__get_eval_info() ret = [] if self.__num_inner_eval > 0: result = np.zeros(self.__num_inner_eval, dtype=np.float64) tmp_out_len = ctypes.c_int(0) _safe_call(_LIB.LGBM_BoosterGetEval( self.handle, ctypes.c_int(data_idx), ctypes.byref(tmp_out_len), result.ctypes.data_as(ctypes.POINTER(ctypes.c_double)))) if tmp_out_len.value != self.__num_inner_eval: raise ValueError("Wrong length of eval results") for i in range(self.__num_inner_eval): ret.append((data_name, self.__name_inner_eval[i], result[i], self.__higher_better_inner_eval[i])) if callable(feval): feval = [feval] if feval is not None: if data_idx == 0: cur_data = self.train_set else: cur_data = self.valid_sets[data_idx - 1] for eval_function in feval: if eval_function is None: continue feval_ret = eval_function(self.__inner_predict(data_idx), cur_data) if isinstance(feval_ret, list): for eval_name, val, is_higher_better in feval_ret: ret.append((data_name, eval_name, val, is_higher_better)) else: eval_name, val, is_higher_better = feval_ret ret.append((data_name, eval_name, val, is_higher_better)) return ret def __inner_predict(self, data_idx): """Predict for training and validation dataset.""" if data_idx >= self.__num_dataset: raise ValueError("Data_idx should be smaller than number of dataset") if self.__inner_predict_buffer[data_idx] is None: if data_idx == 0: n_preds = self.train_set.num_data() * self.__num_class else: n_preds = self.valid_sets[data_idx - 1].num_data() * self.__num_class self.__inner_predict_buffer[data_idx] = np.zeros(n_preds, dtype=np.float64) # avoid to predict many time in one iteration if not self.__is_predicted_cur_iter[data_idx]: tmp_out_len = ctypes.c_int64(0) data_ptr = self.__inner_predict_buffer[data_idx].ctypes.data_as(ctypes.POINTER(ctypes.c_double)) _safe_call(_LIB.LGBM_BoosterGetPredict( self.handle, ctypes.c_int(data_idx), ctypes.byref(tmp_out_len), data_ptr)) if tmp_out_len.value != len(self.__inner_predict_buffer[data_idx]): raise ValueError(f"Wrong length of predict results for data {data_idx}") self.__is_predicted_cur_iter[data_idx] = True return self.__inner_predict_buffer[data_idx] def __get_eval_info(self): """Get inner evaluation count and names.""" if self.__need_reload_eval_info: self.__need_reload_eval_info = False out_num_eval = ctypes.c_int(0) # Get num of inner evals _safe_call(_LIB.LGBM_BoosterGetEvalCounts( self.handle, ctypes.byref(out_num_eval))) self.__num_inner_eval = out_num_eval.value if self.__num_inner_eval > 0: # Get name of eval metrics tmp_out_len = ctypes.c_int(0) reserved_string_buffer_size = 255 required_string_buffer_size = ctypes.c_size_t(0) string_buffers = [ ctypes.create_string_buffer(reserved_string_buffer_size) for _ in range(self.__num_inner_eval) ] ptr_string_buffers = (ctypes.c_char_p * self.__num_inner_eval)(*map(ctypes.addressof, string_buffers)) _safe_call(_LIB.LGBM_BoosterGetEvalNames( self.handle, ctypes.c_int(self.__num_inner_eval), ctypes.byref(tmp_out_len), ctypes.c_size_t(reserved_string_buffer_size), ctypes.byref(required_string_buffer_size), ptr_string_buffers)) if self.__num_inner_eval != tmp_out_len.value: raise ValueError("Length of eval names doesn't equal with num_evals") actual_string_buffer_size = required_string_buffer_size.value # if buffer length is not long enough, reallocate buffers if reserved_string_buffer_size < actual_string_buffer_size: string_buffers = [ ctypes.create_string_buffer(actual_string_buffer_size) for _ in range(self.__num_inner_eval) ] ptr_string_buffers = (ctypes.c_char_p * self.__num_inner_eval)(*map(ctypes.addressof, string_buffers)) _safe_call(_LIB.LGBM_BoosterGetEvalNames( self.handle, ctypes.c_int(self.__num_inner_eval), ctypes.byref(tmp_out_len), ctypes.c_size_t(actual_string_buffer_size), ctypes.byref(required_string_buffer_size), ptr_string_buffers)) self.__name_inner_eval = [ string_buffers[i].value.decode('utf-8') for i in range(self.__num_inner_eval) ] self.__higher_better_inner_eval = [ name.startswith(('auc', 'ndcg@', 'map@', 'average_precision')) for name in self.__name_inner_eval ] def attr(self, key): """Get attribute string from the Booster. Parameters ---------- key : string The name of the attribute. Returns ------- value : string or None The attribute value. Returns None if attribute does not exist. """ return self.__attr.get(key, None) def set_attr(self, **kwargs): """Set attributes to the Booster. Parameters ---------- **kwargs The attributes to set. Setting a value to None deletes an attribute. Returns ------- self : Booster Booster with set attributes. """ for key, value in kwargs.items(): if value is not None: if not isinstance(value, str): raise ValueError("Only string values are accepted") self.__attr[key] = value else: self.__attr.pop(key, None) return self
mit
Dapid/pywt
demo/dwt_signal_decomposition.py
1
1789
#!/usr/bin/env python # -*- coding: utf-8 -*- import os import numpy as np import matplotlib.pyplot as plt import pywt ecg = np.load(os.path.join('data', 'ecg.npy')) data1 = np.concatenate((np.arange(1, 400), np.arange(398, 600), np.arange(601, 1024))) x = np.linspace(0.082, 2.128, num=1024)[::-1] data2 = np.sin(40 * np.log(x)) * np.sign((np.log(x))) mode = pywt.MODES.sp1 def plot_signal_decomp(data, w, title): """Decompose and plot a signal S. S = An + Dn + Dn-1 + ... + D1 """ w = pywt.Wavelet(w) a = data ca = [] cd = [] for i in range(5): (a, d) = pywt.dwt(a, w, mode) ca.append(a) cd.append(d) rec_a = [] rec_d = [] for i, coeff in enumerate(ca): coeff_list = [coeff, None] + [None] * i rec_a.append(pywt.waverec(coeff_list, w)) for i, coeff in enumerate(cd): coeff_list = [None, coeff] + [None] * i rec_d.append(pywt.waverec(coeff_list, w)) fig = plt.figure() ax_main = fig.add_subplot(len(rec_a) + 1, 1, 1) ax_main.set_title(title) ax_main.plot(data) ax_main.set_xlim(0, len(data) - 1) for i, y in enumerate(rec_a): ax = fig.add_subplot(len(rec_a) + 1, 2, 3 + i * 2) ax.plot(y, 'r') ax.set_xlim(0, len(y) - 1) ax.set_ylabel("A%d" % (i + 1)) for i, y in enumerate(rec_d): ax = fig.add_subplot(len(rec_d) + 1, 2, 4 + i * 2) ax.plot(y, 'g') ax.set_xlim(0, len(y) - 1) ax.set_ylabel("D%d" % (i + 1)) plot_signal_decomp(data1, 'coif5', "DWT: Signal irregularity") plot_signal_decomp(data2, 'sym5', "DWT: Frequency and phase change - Symmlets5") plot_signal_decomp(ecg, 'sym5', "DWT: Ecg sample - Symmlets5") plt.show()
mit
jmmease/pandas
pandas/conftest.py
7
2021
import pytest import numpy import pandas import pandas.util.testing as tm def pytest_addoption(parser): parser.addoption("--skip-slow", action="store_true", help="skip slow tests") parser.addoption("--skip-network", action="store_true", help="skip network tests") parser.addoption("--run-high-memory", action="store_true", help="run high memory tests") parser.addoption("--only-slow", action="store_true", help="run only slow tests") def pytest_runtest_setup(item): if 'slow' in item.keywords and item.config.getoption("--skip-slow"): pytest.skip("skipping due to --skip-slow") if 'slow' not in item.keywords and item.config.getoption("--only-slow"): pytest.skip("skipping due to --only-slow") if 'network' in item.keywords and item.config.getoption("--skip-network"): pytest.skip("skipping due to --skip-network") if 'high_memory' in item.keywords and not item.config.getoption( "--run-high-memory"): pytest.skip( "skipping high memory test since --run-high-memory was not set") # Configurations for all tests and all test modules @pytest.fixture(autouse=True) def configure_tests(): pandas.set_option('chained_assignment', 'raise') # For running doctests: make np and pd names available @pytest.fixture(autouse=True) def add_imports(doctest_namespace): doctest_namespace['np'] = numpy doctest_namespace['pd'] = pandas @pytest.fixture(params=['bsr', 'coo', 'csc', 'csr', 'dia', 'dok', 'lil']) def spmatrix(request): tm._skip_if_no_scipy() from scipy import sparse return getattr(sparse, request.param + '_matrix') @pytest.fixture def ip(): """ Get an instance of IPython.InteractiveShell. Will raise a skip if IPython is not installed. """ pytest.importorskip('IPython', minversion="6.0.0") from IPython.core.interactiveshell import InteractiveShell return InteractiveShell()
bsd-3-clause
keflavich/APEX_CMZ_H2CO
plot_codes/parameter_comparisons.py
2
20997
import matplotlib import paths matplotlib.rc_file(paths.pcpath('pubfiguresrc')) import os import pylab as pl from astropy import table from paths import analysispath import numpy as np from astropy import coordinates from astropy import units as u import heating pcfittable = table.Table.read(os.path.join(analysispath, 'fitted_line_parameters_Chi2Constraints.ipac'), format='ascii.ipac') lolim = pcfittable['tmax1sig_chi2'] > 340 maps = np.char.startswith(pcfittable['Source_Name'], 'Map') ok = ~np.isnan(pcfittable['tmin1sig_chi2']) & (pcfittable['width'] < 40) & (pcfittable['h2coratio321303']/pcfittable['eh2coratio321303'] > 5) & pcfittable['is_good'].astype('bool') flags = {'is_map': maps, 'is_lolim': lolim, 'is_ok': ok} # Don't plot these for now... pcfittable = pcfittable[(~lolim) & ok] maps = np.char.startswith(pcfittable['Source_Name'], 'Map') lolim_conservative = pcfittable['tmax1sig_chi2'] > 150 fig4 = pl.figure(4) fig4.clf() ax = fig4.add_subplot(1,3,1) ax.errorbar(pcfittable['temperature_chi2'], pcfittable['density_chi2'], yerr=[pcfittable['density_chi2']-pcfittable['dmin1sig_chi2'], pcfittable['dmax1sig_chi2']-pcfittable['density_chi2']], xerr=[pcfittable['temperature_chi2']-pcfittable['tmin1sig_chi2'], pcfittable['tmax1sig_chi2']-pcfittable['temperature_chi2']], linestyle='none', marker='s', linewidth=1, alpha=0.5) ax2 = fig4.add_subplot(1,3,2) # Don't do this any more: it relies on having the RADEX fits, which we don't. #ax2.errorbar(pcfittable['temperature_chi2'], pcfittable['temperature'], # yerr=pcfittable['etemperature'], # xerr=[pcfittable['temperature_chi2']-pcfittable['tmin1sig_chi2'], # pcfittable['tmax1sig_chi2']-pcfittable['temperature_chi2']], # linestyle='none', marker='s', linewidth=1, alpha=0.5) ax2.plot([0,300],[0,300],'k--',linewidth=2,alpha=0.5) fig5 = pl.figure(5) fig5.clf() ax5 = fig5.gca() ax5.errorbar(coordinates.Angle(pcfittable['GLON']*u.deg).wrap_at(180*u.deg).value[maps], pcfittable['temperature_chi2'][maps], yerr=[(pcfittable['temperature_chi2']-pcfittable['tmin1sig_chi2'])[maps], (pcfittable['tmax1sig_chi2']-pcfittable['temperature_chi2'])[maps]], capsize=0, markeredgecolor='none', linestyle='none', marker='s', linewidth=1, alpha=0.5, color='r') ax5.set_ylim(0,150) ax5.set_ylabel("Temperature (K)") ax5.set_xlabel("Galactic Longitude ($^{\\circ}$)") fig5.savefig(paths.fpath('chi2_temperature_vs_glon_byfield.pdf'), bbox_inches='tight') ax5.errorbar(coordinates.Angle(pcfittable['GLON']*u.deg).wrap_at(180*u.deg).value[~maps], pcfittable['temperature_chi2'][~maps], yerr=[(pcfittable['temperature_chi2']-pcfittable['tmin1sig_chi2'])[~maps], (pcfittable['tmax1sig_chi2']-pcfittable['temperature_chi2'])[~maps]], capsize=0, markeredgecolor='none', linestyle='none', marker='s', linewidth=1, alpha=0.5) fig5.savefig(paths.fpath('chi2_temperature_vs_glon_fieldsandsources.pdf'), bbox_inches='tight') fig6 = pl.figure(6) fig6.clf() ax6 = fig6.gca() mask = maps&~lolim_conservative ax6.errorbar(pcfittable['higaldusttem'][mask], pcfittable['temperature_chi2'][mask], yerr=[(pcfittable['temperature_chi2']-pcfittable['tmin1sig_chi2'])[mask], (pcfittable['tmax1sig_chi2']-pcfittable['temperature_chi2'])[mask]], linestyle='none', marker='s', linewidth=1, alpha=0.5, color='r', capsize=0) ax6.plot([15,30],[15,30],'k--') mask = maps&lolim_conservative ax6.plot(pcfittable['higaldusttem'][mask], pcfittable['tmin1sig_chi2'][mask], marker='^', markersize=10, markeredgecolor='none', color='r', alpha=0.5, linestyle='none') ax6.set_xlabel("HiGal Dust Temperature (K)") ax6.set_ylabel("H$_2$CO Temperature (K)") ax6.set_ylim(0,200) ax6.set_xlim(15,30) fig6.savefig(paths.fpath('chi2_temperature_vs_higaltemperature_byfield.pdf'), bbox_inches='tight') mask = (~maps)&(~lolim_conservative) ax6.errorbar(pcfittable['higaldusttem'][mask], pcfittable['temperature_chi2'][mask], yerr=[(pcfittable['temperature_chi2']-pcfittable['tmin1sig_chi2'])[mask], (pcfittable['tmax1sig_chi2']-pcfittable['temperature_chi2'])[mask]], capsize=0, markeredgecolor='none', markersize=10, linestyle='none', marker='s', linewidth=0.5, alpha=0.5, color='b') mask = (~maps)&lolim_conservative ax6.plot(pcfittable['higaldusttem'][mask], pcfittable['tmin1sig_chi2'][mask], marker='^', markersize=10, markeredgecolor='none', color='b', alpha=0.5, linestyle='none') ax6.set_ylim(10,150) ax6.set_xlim(15,30) fig6.savefig(paths.fpath('chi2_temperature_vs_higaltemperature_fieldsandsources_notitle.pdf'), bbox_inches='tight') ax6.set_title("Hand-selected regions") fig6.savefig(paths.fpath('chi2_temperature_vs_higaltemperature_fieldsandsources.pdf'), bbox_inches='tight') fig7 = pl.figure(7) fig7.clf() ax7 = fig7.gca() mask = maps&~lolim_conservative ax7.errorbar(pcfittable['width'][mask]*(8*np.log(2))**0.5, pcfittable['temperature_chi2'][mask], yerr=[(pcfittable['temperature_chi2']-pcfittable['tmin1sig_chi2'])[mask], (pcfittable['tmax1sig_chi2']-pcfittable['temperature_chi2'])[mask]], capsize=0, markersize=10, markeredgecolor='none', linestyle='none', marker='s', linewidth=0.5, alpha=0.6, color='r') mask = maps&lolim_conservative ax7.plot(pcfittable['width'][mask]*(8*np.log(2))**0.5, pcfittable['tmin1sig_chi2'][mask], marker='^', markersize=10, markeredgecolor='none', color='r', alpha=0.4, linestyle='none') linewidths = np.linspace(0,pcfittable['width'].max())*u.km/u.s ax7.plot(linewidths*2.35, [heating.tkin_all(10**4*u.cm**-3, sigma, 10*u.pc, 5*u.km/u.s/u.pc, 30*u.K) for sigma in linewidths], linestyle='--', color='k', label='$n=10^4$ cm$^{-3}$', zorder=-5) ax7.plot(linewidths*2.35, [heating.tkin_all(10**4*u.cm**-3, sigma, 10*u.pc, 1*u.km/u.s/u.pc, 30*u.K) for sigma in linewidths], linestyle='--', color='r', label='$n=10^4$ cm$^{-3}$, $dv/dr=1$', zorder=-5, linewidth=2, alpha=0.5) ax7.plot(linewidths*2.35, [heating.tkin_all(10**4*u.cm**-3, sigma, 20*u.pc, 5*u.km/u.s/u.pc, 30*u.K) for sigma in linewidths], linestyle='--', color='b', label='$n=10^4$ cm$^{-3}$, $L=20$ pc', zorder=-5, alpha=0.5, linewidth=2) ax7.plot(linewidths*2.35, [heating.tkin_all(10**5*u.cm**-3, sigma, 10*u.pc, 5*u.km/u.s/u.pc, 30*u.K) for sigma in linewidths], linestyle=':', color='k', label='$n=10^5$ cm$^{-3}$', zorder=-5) ax7.plot(linewidths*2.35, [heating.tkin_all(10**6*u.cm**-3, sigma, 10*u.pc, 5*u.km/u.s/u.pc, 30*u.K) for sigma in linewidths], linestyle='-.', color='k', label='$n=10^6$ cm$^{-3}$', zorder=-5) ax7.plot(linewidths*2.35, [heating.tkin_all(10**5*u.cm**-3, sigma, 10*u.pc, 5*u.km/u.s/u.pc, 30*u.K, crir=1e-15*u.s**-1) for sigma in linewidths], linestyle='-', color='g', label='$n=10^5$ cm$^{-3}$, $\zeta_{CR}=10^{-15}$ s$^{-1}$', zorder=-10, alpha=0.25, linewidth=4) ax7.plot(linewidths*2.35, [heating.tkin_all(10**5*u.cm**-3, sigma, 10*u.pc, 5*u.km/u.s/u.pc, 30*u.K, crir=1e-14*u.s**-1) for sigma in linewidths], linestyle=':', color='purple', label='$n=10^5$ cm$^{-3}$, $\zeta_{CR}=10^{-14}$ s$^{-1}$', zorder=-10, alpha=0.25, linewidth=4) box = ax7.get_position() ax7.set_position([box.x0, box.y0, box.width * 0.7, box.height]) ax7.legend(loc='center left', fontsize=16, bbox_to_anchor=(1.0, 0.75)) ax7.set_xlabel("Line FWHM (km s$^{-1}$)") ax7.set_ylabel("Temperature (K)") ax7.set_ylim(10,150) fig7.savefig(paths.fpath('chi2_temperature_vs_linewidth_byfield.pdf'), bbox_inches='tight') mask = (~maps)&(~lolim_conservative) ax7.errorbar(pcfittable['width'][mask]*(8*np.log(2))**0.5, pcfittable['temperature_chi2'][mask], yerr=[(pcfittable['temperature_chi2']-pcfittable['tmin1sig_chi2'])[mask], (pcfittable['tmax1sig_chi2']-pcfittable['temperature_chi2'])[mask]], capsize=0, markeredgecolor='none', markersize=10, linestyle='none', marker='s', linewidth=0.5, alpha=0.6, color='b') mask = (~maps)&lolim_conservative ax7.plot(pcfittable['width'][mask]*(8*np.log(2))**0.5, pcfittable['tmin1sig_chi2'][mask], marker='^', markersize=10, markeredgecolor='none', color='b', alpha=0.4, linestyle='none') ax7.set_ylim(10,150) fig7.savefig(paths.fpath('chi2_temperature_vs_linewidth_fieldsandsources.pdf'), bbox_inches='tight') fig8 = pl.figure(8) fig8.clf() ax8 = fig8.gca() ax8.errorbar(pcfittable['ampH2CO'][maps], pcfittable['temperature_chi2'][maps], yerr=[(pcfittable['temperature_chi2']-pcfittable['tmin1sig_chi2'])[maps], (pcfittable['tmax1sig_chi2']-pcfittable['temperature_chi2'])[maps]], linestyle='none', marker='s', linewidth=1, alpha=0.5, color='r') ax8.set_xlabel("H2CO Peak Amplitude") ax8.set_ylabel("Temperature (K)") fig8.savefig(paths.fpath('chi2_temperature_vs_h2coamp_byfield.pdf'), bbox_inches='tight') ax8.errorbar(pcfittable['ampH2CO'][~maps], pcfittable['temperature_chi2'][~maps], yerr=[(pcfittable['temperature_chi2']-pcfittable['tmin1sig_chi2'])[~maps], (pcfittable['tmax1sig_chi2']-pcfittable['temperature_chi2'])[~maps]], linestyle='none', marker='s', linewidth=1, alpha=0.5, color='b') fig8.savefig(paths.fpath('chi2_temperature_vs_h2coamp_fieldsandsources.pdf'), bbox_inches='tight') fig9 = pl.figure(9) fig9.clf() ax9 = fig9.gca() ax9.set_xscale('log') ax9.errorbar(pcfittable['higalcolumndens'][maps], pcfittable['temperature_chi2'][maps], yerr=[(pcfittable['temperature_chi2']-pcfittable['tmin1sig_chi2'])[maps], (pcfittable['tmax1sig_chi2']-pcfittable['temperature_chi2'])[maps]], linestyle='none', marker='s', linewidth=1, alpha=0.5, color='r') ax9.set_xlabel("Hi-Gal Fitted Column Density") ax9.set_ylabel("Temperature (K)") fig9.savefig(paths.fpath('chi2_temperature_vs_higalcolumn_byfield.pdf'), bbox_inches='tight') ax9.errorbar(pcfittable['higalcolumndens'][~maps], pcfittable['temperature_chi2'][~maps], yerr=[(pcfittable['temperature_chi2']-pcfittable['tmin1sig_chi2'])[~maps], (pcfittable['tmax1sig_chi2']-pcfittable['temperature_chi2'])[~maps]], linestyle='none', marker='s', linewidth=1, alpha=0.5, color='b') fig9.savefig(paths.fpath('chi2_temperature_vs_higalcolumn_fieldsandsources.pdf'), bbox_inches='tight') fig10 = pl.figure(10) fig10.clf() ax10 = fig10.gca() ax10.errorbar(pcfittable['width'][maps]*(8*np.log(2))**0.5, pcfittable['h2coratio321303'][maps], yerr=pcfittable['eh2coratio321303'][maps], linestyle='none', marker='s', linewidth=1, alpha=0.5, color='r') ax10.set_xlabel("Line FWHM (km s$^{-1}$)") ax10.set_ylabel("Ratio 321/303") fig10.savefig(paths.fpath('ratio_vs_linewidth_byfield.pdf'), bbox_inches='tight') ax10.errorbar(pcfittable['width'][~maps]*(8*np.log(2))**0.5, pcfittable['h2coratio321303'][~maps], yerr=pcfittable['eh2coratio321303'][~maps], linestyle='none', marker='s', linewidth=1, alpha=0.5, color='b') fig10.savefig(paths.fpath('ratio_vs_linewidth_fieldsandsources.pdf'), bbox_inches='tight') fig11 = pl.figure(11) fig11.clf() ax11 = fig11.gca() ax11.errorbar(pcfittable['higaldusttem'][maps], pcfittable['h2coratio321303'][maps], yerr=pcfittable['eh2coratio321303'][maps], linestyle='none', marker='s', linewidth=1, alpha=0.5, color='r') ax11.set_ylim(0,200) ax11.set_xlim(15,30) ax11.set_xlabel("HiGal Fitted Temperature") ax11.set_ylabel("Ratio 321/303") fig11.savefig(paths.fpath('ratio_vs_higaltemperature_byfield.pdf'), bbox_inches='tight') ax11.errorbar(pcfittable['higaldusttem'][~maps], pcfittable['h2coratio321303'][~maps], yerr=pcfittable['eh2coratio321303'][~maps], linestyle='none', marker='s', linewidth=1, alpha=0.5, color='b') fig11.savefig(paths.fpath('ratio_vs_higaltemperature_fieldsandsources.pdf'), bbox_inches='tight') # RADEX fitting has been removed #fig12 = pl.figure(12) #fig12.clf() #ax = fig12.add_subplot(1,1,1) #ax.errorbar(pcfittable['temperature_chi2'], pcfittable['temperature'], # yerr=pcfittable['etemperature'], # xerr=[pcfittable['temperature_chi2']-pcfittable['tmin1sig_chi2'], # pcfittable['tmax1sig_chi2']-pcfittable['temperature_chi2']], # linestyle='none', marker='s', linewidth=1, alpha=0.5) #ax.plot([0,300],[0,300],'k--',linewidth=2,alpha=0.5) #ax.set_title("DEBUG: RADEX+pyspeckit-fitted temperature vs. $\\chi^2$ temperature") #ax.set_xlabel("$\\chi^2$ Temperature") #ax.set_ylabel("RADEX+pyspeckit Temperature") #ax.axis([0,350,0,350]) fig13 = pl.figure(13) fig13.clf() ax13 = fig13.gca() ax13.errorbar(pcfittable['area'][maps], pcfittable['temperature_chi2'][maps], yerr=[pcfittable['temperature_chi2'][maps]-pcfittable['tmin1sig_chi2'][maps], pcfittable['tmax1sig_chi2'][maps]-pcfittable['temperature_chi2'][maps]], linestyle='none', marker='s', linewidth=1, alpha=0.5, color='r') ax13.set_xlabel("Area (square degrees)") ax13.set_ylabel("Temperature (K)") ax13.set_xscale('log') fig13.savefig(paths.fpath('temperature_vs_area_byfield.pdf'), bbox_inches='tight') ax13.errorbar(pcfittable['area'][~maps], pcfittable['temperature_chi2'][~maps], yerr=[pcfittable['temperature_chi2'][~maps]-pcfittable['tmin1sig_chi2'][~maps], pcfittable['tmax1sig_chi2'][~maps]-pcfittable['temperature_chi2'][~maps]], linestyle='none', marker='s', linewidth=1, alpha=0.5, color='b') fig13.savefig(paths.fpath('temperature_vs_area_fieldsandsources.pdf'), bbox_inches='tight') fig14 = pl.figure(14) fig14.clf() ax14 = fig14.gca() ax14.errorbar(pcfittable['higalcolumndens'][maps], pcfittable['temperature_chi2'][maps], yerr=[(pcfittable['temperature_chi2']-pcfittable['tmin1sig_chi2'])[maps], (pcfittable['tmax1sig_chi2']-pcfittable['temperature_chi2'])[maps]], linestyle='none', marker='s', linewidth=1, alpha=0.5, color='r') #ax14.plot([15,30],[15,30],'k--') ax14.set_xlabel("HiGal Fitted Column Density") ax14.set_ylabel("Temperature (K)") fig14.savefig(paths.fpath('chi2_temperature_vs_higaldustcol_byfield.pdf'), bbox_inches='tight') ax14.errorbar(pcfittable['higalcolumndens'][~maps], pcfittable['temperature_chi2'][~maps], yerr=[(pcfittable['temperature_chi2']-pcfittable['tmin1sig_chi2'])[~maps], (pcfittable['tmax1sig_chi2']-pcfittable['temperature_chi2'])[~maps]], linestyle='none', marker='s', linewidth=1, alpha=0.5, color='b') fig14.savefig(paths.fpath('chi2_temperature_vs_higaldustcol_fieldsandsources.pdf'), bbox_inches='tight') # pcfittable[np.abs(pcfittable['temperature_chi2']-pcfittable['higaldusttem'])/pcfittable['higaldusttem'] < 1.5].pprint() fig15 = pl.figure(15) fig15.clf() ax15 = fig15.gca() mask = maps&~lolim_conservative ax15.errorbar(pcfittable['tkin_turb'][mask], pcfittable['temperature_chi2'][mask], yerr=[(pcfittable['temperature_chi2']-pcfittable['tmin1sig_chi2'])[mask], (pcfittable['tmax1sig_chi2']-pcfittable['temperature_chi2'])[mask]], capsize=0, markersize=10, markeredgecolor='none', linestyle='none', marker='s', linewidth=0.5, alpha=0.6, color='r') mask = maps&lolim_conservative ax15.plot(pcfittable['tkin_turb'][mask], pcfittable['tmin1sig_chi2'][mask], marker='^', markersize=10, markeredgecolor='none', color='r', alpha=0.4, linestyle='none') mask = (maps) & (~lolim_conservative) & ((pcfittable['tmin1sig_chi2'] > pcfittable['tkin_turb']) | (pcfittable['tmax1sig_chi2'] < pcfittable['tkin_turb'])) ax15.plot(pcfittable['tkin_turb'][mask], pcfittable['temperature_chi2'][mask], marker='s', markersize=15, markeredgecolor='r', markerfacecolor='none', markeredgewidth=0.5, alpha=0.4, linestyle='none') mask = (maps) & (lolim_conservative) & ((pcfittable['tmin1sig_chi2'] > pcfittable['tkin_turb'])) ax15.plot(pcfittable['tkin_turb'][mask], pcfittable['tmin1sig_chi2'][mask], marker='^', markersize=15, markeredgecolor='r', markerfacecolor='none', markeredgewidth=0.5, alpha=0.4, linestyle='none') # Sources with T_predicted >> T_measured #high_badpredictions = (pcfittable['tkin_turb'] > pcfittable['tmax1sig_chi2'])&(~lolim_conservative) #high_badpredictions = (pcfittable['tkin_turb'] > 120)&(~lolim_conservative) #for row,is_map in zip(pcfittable[high_badpredictions], maps[high_badpredictions]): # xy = np.array((row['tkin_turb'], row['temperature_chi2'])) # ax15.annotate("{0}_{1}".format(row['Source_Name'], row['ComponentID']), # xy, # xytext=xy-(15, 7), # color='r' if is_map else 'b' # ) ax15.plot([0,200], [0,200], 'k--', alpha=0.5, zorder=-5) ax15.set_xlabel("Turbulence-driven Temperature (K)") ax15.set_ylabel("H$_2$CO Temperature (K)") ax15.set_ylim(10,150) ax15.set_xlim(10,180) fig15.savefig(paths.fpath('chi2_temperature_vs_turbulenttemperature_byfield.pdf'), bbox_inches='tight') mask = (~maps)&(~lolim_conservative) ax15.errorbar(pcfittable['tkin_turb'][mask], pcfittable['temperature_chi2'][mask], yerr=[(pcfittable['temperature_chi2']-pcfittable['tmin1sig_chi2'])[mask], (pcfittable['tmax1sig_chi2']-pcfittable['temperature_chi2'])[mask]], capsize=0, markeredgecolor='none', markersize=10, linestyle='none', marker='s', linewidth=0.5, alpha=0.6, color='b') mask = (~maps)&lolim_conservative ax15.plot(pcfittable['tkin_turb'][mask], pcfittable['tmin1sig_chi2'][mask], marker='^', markersize=10, markeredgecolor='none', color='b', alpha=0.4, linestyle='none') mask = (~maps) & (~lolim_conservative) & ((pcfittable['tmin1sig_chi2'] > pcfittable['tkin_turb']) | (pcfittable['tmax1sig_chi2'] < pcfittable['tkin_turb'])) ax15.plot(pcfittable['tkin_turb'][mask], pcfittable['temperature_chi2'][mask], marker='s', markersize=15, markeredgecolor='b', markerfacecolor='none', markeredgewidth=0.5, alpha=0.4, linestyle='none') mask = (~maps) & (lolim_conservative) & ((pcfittable['tmin1sig_chi2'] > pcfittable['tkin_turb'])) ax15.plot(pcfittable['tkin_turb'][mask], pcfittable['tmin1sig_chi2'][mask], marker='^', markersize=15, markeredgecolor='b', markerfacecolor='none', markeredgewidth=0.5, alpha=0.4, linestyle='none') ax15.set_ylim(10,150) fig15.savefig(paths.fpath('chi2_temperature_vs_turbulenttemperature_fieldsandsources_notitle.pdf'), bbox_inches='tight') ax15.set_title("Hand-selected regions") fig15.savefig(paths.fpath('chi2_temperature_vs_turbulenttemperature_fieldsandsources.pdf'), bbox_inches='tight')
bsd-3-clause
ilastikdev/opengm
src/interfaces/python/examples/potts_gui.py
14
1100
import numpy import opengm import vigra import matplotlib.pyplot as plt import matplotlib.cm as cm gradScale = 0.1 energyNotEqual = 0.2 sigma=0.2 resizeFactor=2 img=vigra.impex.readImage('lena.bmp') shape=img.shape imgLab=vigra.colors.transform_RGB2Lab(img) shape=(shape[0]*resizeFactor,shape[1]*resizeFactor) imgLab=vigra.sampling.resize(imgLab, shape,order=3) gradMag=vigra.filters.gaussianGradientMagnitude(imgLab,gradScale) unaries=numpy.zeros([shape[0],shape[1],2]) unaries[:,:,1]=numpy.exp(-1.0*gradMag[:,:,0]*sigma) unaries[:,:,0]=1.0-unaries[:,:,1] regularizer=opengm.PottsFunction([2,2],0.0,energyNotEqual) gm=opengm.grid2d2Order(unaries=unaries,regularizer=regularizer,order='numpy',operator='adder') inf=opengm.inference.GraphCut(gm) inf.infer() argmin=inf.arg().reshape(shape[0:2]) plt.figure(1) ax=plt.subplot(2,1,1) plt.imshow(unaries[:,:,1].T, interpolation="nearest") plt.set_cmap(cm.copper) plt.colorbar() ax.set_title('costs / unaries label=1') ax=plt.subplot(2,1,2) plt.imshow(argmin.T, interpolation="nearest") plt.colorbar() ax.set_title('argmin') plt.show()
mit
spbguru/repo1
external/linux32/lib/python2.6/site-packages/matplotlib/backends/backend_wxagg.py
70
9051
from __future__ import division """ backend_wxagg.py A wxPython backend for Agg. This uses the GUI widgets written by Jeremy O'Donoghue (jeremy@o-donoghue.com) and the Agg backend by John Hunter (jdhunter@ace.bsd.uchicago.edu) Copyright (C) 2003-5 Jeremy O'Donoghue, John Hunter, Illinois Institute of Technology License: This work is licensed under the matplotlib license( PSF compatible). A copy should be included with this source code. """ import wx import matplotlib from matplotlib.figure import Figure from backend_agg import FigureCanvasAgg import backend_wx from backend_wx import FigureManager, FigureManagerWx, FigureCanvasWx, \ FigureFrameWx, DEBUG_MSG, NavigationToolbar2Wx, error_msg_wx, \ draw_if_interactive, show, Toolbar, backend_version class FigureFrameWxAgg(FigureFrameWx): def get_canvas(self, fig): return FigureCanvasWxAgg(self, -1, fig) def _get_toolbar(self, statbar): if matplotlib.rcParams['toolbar']=='classic': toolbar = NavigationToolbarWx(self.canvas, True) elif matplotlib.rcParams['toolbar']=='toolbar2': toolbar = NavigationToolbar2WxAgg(self.canvas) toolbar.set_status_bar(statbar) else: toolbar = None return toolbar class FigureCanvasWxAgg(FigureCanvasAgg, FigureCanvasWx): """ The FigureCanvas contains the figure and does event handling. In the wxPython backend, it is derived from wxPanel, and (usually) lives inside a frame instantiated by a FigureManagerWx. The parent window probably implements a wxSizer to control the displayed control size - but we give a hint as to our preferred minimum size. """ def draw(self, drawDC=None): """ Render the figure using agg. """ DEBUG_MSG("draw()", 1, self) FigureCanvasAgg.draw(self) self.bitmap = _convert_agg_to_wx_bitmap(self.get_renderer(), None) self._isDrawn = True self.gui_repaint(drawDC=drawDC) def blit(self, bbox=None): """ Transfer the region of the agg buffer defined by bbox to the display. If bbox is None, the entire buffer is transferred. """ if bbox is None: self.bitmap = _convert_agg_to_wx_bitmap(self.get_renderer(), None) self.gui_repaint() return l, b, w, h = bbox.bounds r = l + w t = b + h x = int(l) y = int(self.bitmap.GetHeight() - t) srcBmp = _convert_agg_to_wx_bitmap(self.get_renderer(), None) srcDC = wx.MemoryDC() srcDC.SelectObject(srcBmp) destDC = wx.MemoryDC() destDC.SelectObject(self.bitmap) destDC.BeginDrawing() destDC.Blit(x, y, int(w), int(h), srcDC, x, y) destDC.EndDrawing() destDC.SelectObject(wx.NullBitmap) srcDC.SelectObject(wx.NullBitmap) self.gui_repaint() filetypes = FigureCanvasAgg.filetypes def print_figure(self, filename, *args, **kwargs): # Use pure Agg renderer to draw FigureCanvasAgg.print_figure(self, filename, *args, **kwargs) # Restore the current view; this is needed because the # artist contains methods rely on particular attributes # of the rendered figure for determining things like # bounding boxes. if self._isDrawn: self.draw() class NavigationToolbar2WxAgg(NavigationToolbar2Wx): def get_canvas(self, frame, fig): return FigureCanvasWxAgg(frame, -1, fig) def new_figure_manager(num, *args, **kwargs): """ Create a new figure manager instance """ # in order to expose the Figure constructor to the pylab # interface we need to create the figure here DEBUG_MSG("new_figure_manager()", 3, None) backend_wx._create_wx_app() FigureClass = kwargs.pop('FigureClass', Figure) fig = FigureClass(*args, **kwargs) frame = FigureFrameWxAgg(num, fig) figmgr = frame.get_figure_manager() if matplotlib.is_interactive(): figmgr.frame.Show() return figmgr # # agg/wxPython image conversion functions (wxPython <= 2.6) # def _py_convert_agg_to_wx_image(agg, bbox): """ Convert the region of the agg buffer bounded by bbox to a wx.Image. If bbox is None, the entire buffer is converted. Note: agg must be a backend_agg.RendererAgg instance. """ image = wx.EmptyImage(int(agg.width), int(agg.height)) image.SetData(agg.tostring_rgb()) if bbox is None: # agg => rgb -> image return image else: # agg => rgb -> image => bitmap => clipped bitmap => image return wx.ImageFromBitmap(_clipped_image_as_bitmap(image, bbox)) def _py_convert_agg_to_wx_bitmap(agg, bbox): """ Convert the region of the agg buffer bounded by bbox to a wx.Bitmap. If bbox is None, the entire buffer is converted. Note: agg must be a backend_agg.RendererAgg instance. """ if bbox is None: # agg => rgb -> image => bitmap return wx.BitmapFromImage(_py_convert_agg_to_wx_image(agg, None)) else: # agg => rgb -> image => bitmap => clipped bitmap return _clipped_image_as_bitmap( _py_convert_agg_to_wx_image(agg, None), bbox) def _clipped_image_as_bitmap(image, bbox): """ Convert the region of a wx.Image bounded by bbox to a wx.Bitmap. """ l, b, width, height = bbox.get_bounds() r = l + width t = b + height srcBmp = wx.BitmapFromImage(image) srcDC = wx.MemoryDC() srcDC.SelectObject(srcBmp) destBmp = wx.EmptyBitmap(int(width), int(height)) destDC = wx.MemoryDC() destDC.SelectObject(destBmp) destDC.BeginDrawing() x = int(l) y = int(image.GetHeight() - t) destDC.Blit(0, 0, int(width), int(height), srcDC, x, y) destDC.EndDrawing() srcDC.SelectObject(wx.NullBitmap) destDC.SelectObject(wx.NullBitmap) return destBmp # # agg/wxPython image conversion functions (wxPython >= 2.8) # def _py_WX28_convert_agg_to_wx_image(agg, bbox): """ Convert the region of the agg buffer bounded by bbox to a wx.Image. If bbox is None, the entire buffer is converted. Note: agg must be a backend_agg.RendererAgg instance. """ if bbox is None: # agg => rgb -> image image = wx.EmptyImage(int(agg.width), int(agg.height)) image.SetData(agg.tostring_rgb()) return image else: # agg => rgba buffer -> bitmap => clipped bitmap => image return wx.ImageFromBitmap(_WX28_clipped_agg_as_bitmap(agg, bbox)) def _py_WX28_convert_agg_to_wx_bitmap(agg, bbox): """ Convert the region of the agg buffer bounded by bbox to a wx.Bitmap. If bbox is None, the entire buffer is converted. Note: agg must be a backend_agg.RendererAgg instance. """ if bbox is None: # agg => rgba buffer -> bitmap return wx.BitmapFromBufferRGBA(int(agg.width), int(agg.height), agg.buffer_rgba(0, 0)) else: # agg => rgba buffer -> bitmap => clipped bitmap return _WX28_clipped_agg_as_bitmap(agg, bbox) def _WX28_clipped_agg_as_bitmap(agg, bbox): """ Convert the region of a the agg buffer bounded by bbox to a wx.Bitmap. Note: agg must be a backend_agg.RendererAgg instance. """ l, b, width, height = bbox.get_bounds() r = l + width t = b + height srcBmp = wx.BitmapFromBufferRGBA(int(agg.width), int(agg.height), agg.buffer_rgba(0, 0)) srcDC = wx.MemoryDC() srcDC.SelectObject(srcBmp) destBmp = wx.EmptyBitmap(int(width), int(height)) destDC = wx.MemoryDC() destDC.SelectObject(destBmp) destDC.BeginDrawing() x = int(l) y = int(int(agg.height) - t) destDC.Blit(0, 0, int(width), int(height), srcDC, x, y) destDC.EndDrawing() srcDC.SelectObject(wx.NullBitmap) destDC.SelectObject(wx.NullBitmap) return destBmp def _use_accelerator(state): """ Enable or disable the WXAgg accelerator, if it is present and is also compatible with whatever version of wxPython is in use. """ global _convert_agg_to_wx_image global _convert_agg_to_wx_bitmap if getattr(wx, '__version__', '0.0')[0:3] < '2.8': # wxPython < 2.8, so use the C++ accelerator or the Python routines if state and _wxagg is not None: _convert_agg_to_wx_image = _wxagg.convert_agg_to_wx_image _convert_agg_to_wx_bitmap = _wxagg.convert_agg_to_wx_bitmap else: _convert_agg_to_wx_image = _py_convert_agg_to_wx_image _convert_agg_to_wx_bitmap = _py_convert_agg_to_wx_bitmap else: # wxPython >= 2.8, so use the accelerated Python routines _convert_agg_to_wx_image = _py_WX28_convert_agg_to_wx_image _convert_agg_to_wx_bitmap = _py_WX28_convert_agg_to_wx_bitmap # try to load the WXAgg accelerator try: import _wxagg except ImportError: _wxagg = None # if it's present, use it _use_accelerator(True)
gpl-3.0
kinglogxzl/rqalpha
rqalpha/examples/simple_macd.py
2
2164
# 可以自己import我们平台支持的第三方python模块,比如pandas、numpy等。 import talib import numpy as np import math import pandas # 在这个方法中编写任何的初始化逻辑。context对象将会在你的算法策略的任何方法之间做传递。 def init(context): context.s1 = "000001.XSHE" # 使用MACD需要设置长短均线和macd平均线的参数 context.SHORTPERIOD = 12 context.LONGPERIOD = 26 context.SMOOTHPERIOD = 9 context.OBSERVATION = 100 # 你选择的证券的数据更新将会触发此段逻辑,例如日或分钟历史数据切片或者是实时数据切片更新 def handle_bar(context, bar_dict): # 开始编写你的主要的算法逻辑 # bar_dict[order_book_id] 可以拿到某个证券的bar信息 # context.portfolio 可以拿到现在的投资组合状态信息 # 使用order_shares(id_or_ins, amount)方法进行落单 # TODO: 开始编写你的算法吧! #读取历史数据,使用sma方式计算均线准确度和数据长度无关,但是在使用ema方式计算均线时建议将历史数据窗口适当放大,结果会更加准确 prices = history(context.OBSERVATION,'1d','close')[context.s1].values # 用Talib计算MACD取值,得到三个时间序列数组,分别为macd,signal 和 hist macd,signal,hist = talib.MACD(prices,context.SHORTPERIOD,context.LONGPERIOD,context.SMOOTHPERIOD) plot("macd",macd[-1]) plot("macd signal",signal[-1]) # macd 是长短均线的差值,signal是macd的均线,使用macd策略有几种不同的方法,我们这里采用macd线突破signal线的判断方法 # 如果macd从上往下跌破macd_signal if macd[-1]-signal[-1]<0 and macd[-2]-signal[-2]>0: # 计算现在portfolio中股票的仓位 curPosition = context.portfolio.positions[context.s1].quantity #进行清仓 if curPosition>0: order_target_value(context.s1,0) # 如果短均线从下往上突破长均线,为入场信号 if macd[-1]-signal[-1]>0 and macd[-2]-signal[-2]<0: #满仓入股 order_target_percent(context.s1, 1)
apache-2.0
MartinDelzant/scikit-learn
examples/classification/plot_lda.py
70
2413
""" ==================================================================== Normal and Shrinkage Linear Discriminant Analysis for classification ==================================================================== Shows how shrinkage improves classification. """ from __future__ import division import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import make_blobs from sklearn.discriminant_analysis import LinearDiscriminantAnalysis n_train = 20 # samples for training n_test = 200 # samples for testing n_averages = 50 # how often to repeat classification n_features_max = 75 # maximum number of features step = 4 # step size for the calculation def generate_data(n_samples, n_features): """Generate random blob-ish data with noisy features. This returns an array of input data with shape `(n_samples, n_features)` and an array of `n_samples` target labels. Only one feature contains discriminative information, the other features contain only noise. """ X, y = make_blobs(n_samples=n_samples, n_features=1, centers=[[-2], [2]]) # add non-discriminative features if n_features > 1: X = np.hstack([X, np.random.randn(n_samples, n_features - 1)]) return X, y acc_clf1, acc_clf2 = [], [] n_features_range = range(1, n_features_max + 1, step) for n_features in n_features_range: score_clf1, score_clf2 = 0, 0 for _ in range(n_averages): X, y = generate_data(n_train, n_features) clf1 = LinearDiscriminantAnalysis(solver='lsqr', shrinkage='auto').fit(X, y) clf2 = LinearDiscriminantAnalysis(solver='lsqr', shrinkage=None).fit(X, y) X, y = generate_data(n_test, n_features) score_clf1 += clf1.score(X, y) score_clf2 += clf2.score(X, y) acc_clf1.append(score_clf1 / n_averages) acc_clf2.append(score_clf2 / n_averages) features_samples_ratio = np.array(n_features_range) / n_train plt.plot(features_samples_ratio, acc_clf1, linewidth=2, label="Linear Discriminant Analysis with shrinkage", color='r') plt.plot(features_samples_ratio, acc_clf2, linewidth=2, label="Linear Discriminant Analysis", color='g') plt.xlabel('n_features / n_samples') plt.ylabel('Classification accuracy') plt.legend(loc=1, prop={'size': 12}) plt.suptitle('Linear Discriminant Analysis vs. \ shrinkage Linear Discriminant Analysis (1 discriminative feature)') plt.show()
bsd-3-clause
jmetzen/scikit-learn
examples/svm/plot_oneclass.py
80
2338
""" ========================================== One-class SVM with non-linear kernel (RBF) ========================================== An example using a one-class SVM for novelty detection. :ref:`One-class SVM <svm_outlier_detection>` is an unsupervised algorithm that learns a decision function for novelty detection: classifying new data as similar or different to the training set. """ print(__doc__) import numpy as np import matplotlib.pyplot as plt import matplotlib.font_manager from sklearn import svm xx, yy = np.meshgrid(np.linspace(-5, 5, 500), np.linspace(-5, 5, 500)) # Generate train data X = 0.3 * np.random.randn(100, 2) X_train = np.r_[X + 2, X - 2] # Generate some regular novel observations X = 0.3 * np.random.randn(20, 2) X_test = np.r_[X + 2, X - 2] # Generate some abnormal novel observations X_outliers = np.random.uniform(low=-4, high=4, size=(20, 2)) # fit the model clf = svm.OneClassSVM(nu=0.1, kernel="rbf", gamma=0.1) clf.fit(X_train) y_pred_train = clf.predict(X_train) y_pred_test = clf.predict(X_test) y_pred_outliers = clf.predict(X_outliers) n_error_train = y_pred_train[y_pred_train == -1].size n_error_test = y_pred_test[y_pred_test == -1].size n_error_outliers = y_pred_outliers[y_pred_outliers == 1].size # plot the line, the points, and the nearest vectors to the plane Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) plt.title("Novelty Detection") plt.contourf(xx, yy, Z, levels=np.linspace(Z.min(), 0, 7), cmap=plt.cm.PuBu) a = plt.contour(xx, yy, Z, levels=[0], linewidths=2, colors='darkred') plt.contourf(xx, yy, Z, levels=[0, Z.max()], colors='palevioletred') s = 40 b1 = plt.scatter(X_train[:, 0], X_train[:, 1], c='white', s=s) b2 = plt.scatter(X_test[:, 0], X_test[:, 1], c='blueviolet', s=s) c = plt.scatter(X_outliers[:, 0], X_outliers[:, 1], c='gold', s=s) plt.axis('tight') plt.xlim((-5, 5)) plt.ylim((-5, 5)) plt.legend([a.collections[0], b1, b2, c], ["learned frontier", "training observations", "new regular observations", "new abnormal observations"], loc="upper left", prop=matplotlib.font_manager.FontProperties(size=11)) plt.xlabel( "error train: %d/200 ; errors novel regular: %d/40 ; " "errors novel abnormal: %d/40" % (n_error_train, n_error_test, n_error_outliers)) plt.show()
bsd-3-clause
pdamodaran/yellowbrick
yellowbrick/text/dispersion.py
1
10916
# yellowbrick.text.dispersion # Implementations of lexical dispersions for text visualization. # # Author: Larry Gray # Created: 2018-06-21 10:06 # # Copyright (C) 2018 District Data Labs # For license information, see LICENSE.txt # # ID: dispersion.py [] lwgray@gmail.com $ """ Implementation of lexical dispersion for text visualization """ ########################################################################## ## Imports ########################################################################## from collections import defaultdict import itertools from yellowbrick.text.base import TextVisualizer from yellowbrick.style.colors import resolve_colors from yellowbrick.exceptions import YellowbrickValueError import numpy as np ########################################################################## ## Dispersion Plot Visualizer ########################################################################## class DispersionPlot(TextVisualizer): """ DispersionPlotVisualizer allows for visualization of the lexical dispersion of words in a corpus. Lexical dispersion is a measure of a word's homeogeneity across the parts of a corpus. This plot notes the occurences of a word and how many words from the beginning it appears. Parameters ---------- target_words : list A list of target words whose dispersion across a corpus passed at fit will be visualized. ax : matplotlib axes, default: None The axes to plot the figure on. labels : list of strings The names of the classes in the target, used to create a legend. Labels must match names of classes in sorted order. colors : list or tuple of colors Specify the colors for each individual class colormap : string or matplotlib cmap Qualitative colormap for discrete target ignore_case : boolean, default: False Specify whether input will be case-sensitive. annotate_docs : boolean, default: False Specify whether document boundaries will be displayed. Vertical lines are positioned at the end of each document. kwargs : dict Pass any additional keyword arguments to the super class. These parameters can be influenced later on in the visualization process, but can and should be set as early as possible. """ # NOTE: cannot be np.nan NULL_CLASS = None def __init__(self, target_words, ax=None, colors=None, ignore_case=False, annotate_docs=False, labels=None, colormap=None, **kwargs): super(DispersionPlot, self).__init__(ax=ax, **kwargs) self.labels = labels self.colors = colors self.colormap = colormap self.target_words = target_words self.ignore_case = ignore_case self.annotate_docs = annotate_docs def _compute_dispersion(self, text, y): self.boundaries_ = [] offset = 0 if y is None: y = itertools.repeat(None) for doc, target in zip(text, y): for word in doc: if self.ignore_case: word = word.lower() # NOTE: this will find all indices if duplicate words are supplied # In the case that word is not in target words, any empty list is # returned and no data will be yielded offset += 1 for y_coord in (self.indexed_words_ == word).nonzero()[0]: y_coord = int(y_coord) yield (offset, y_coord, target) if self.annotate_docs: self.boundaries_.append(offset) self.boundaries_ = np.array(self.boundaries_, dtype=int) def _check_missing_words(self, points): for index in range(len(self.indexed_words_)): if index in points[:,1]: pass else: raise YellowbrickValueError(( "The indexed word '{}' is not found in " "this corpus" ).format(self.indexed_words_[index])) def fit(self, X, y=None, **kwargs): """ The fit method is the primary drawing input for the dispersion visualization. Parameters ---------- X : list or generator Should be provided as a list of documents or a generator that yields a list of documents that contain a list of words in the order they appear in the document. y : ndarray or Series of length n An optional array or series of target or class values for instances. If this is specified, then the points will be colored according to their class. kwargs : dict Pass generic arguments to the drawing method Returns ------- self : instance Returns the instance of the transformer/visualizer """ if y is not None: self.classes_ = np.unique(y) elif y is None and self.labels is not None: self.classes_ = np.array([self.labels[0]]) else: self.classes_ = np.array([self.NULL_CLASS]) # Create an index (e.g. the y position) for the target words self.indexed_words_ = np.flip(self.target_words, axis=0) if self.ignore_case: self.indexed_words_ = np.array([w.lower() for w in self.indexed_words_]) # Stack is used to create a 2D array from the generator try: points_target = np.stack(self._compute_dispersion(X, y)) except ValueError: raise YellowbrickValueError(( "No indexed words were found in the corpus" )) points = np.stack(zip(points_target[:,0].astype(int), points_target[:,1].astype(int))) self.target = points_target[:,2] self._check_missing_words(points) self.draw(points, self.target) return self def draw(self, points, target=None, **kwargs): """ Called from the fit method, this method creates the canvas and draws the plot on it. Parameters ---------- kwargs: generic keyword arguments. """ # Resolve the labels with the classes labels = self.labels if self.labels is not None else self.classes_ if len(labels) != len(self.classes_): raise YellowbrickValueError(( "number of supplied labels ({}) does not " "match the number of classes ({})" ).format(len(labels), len(self.classes_))) # Create the color mapping for the labels. color_values = resolve_colors( n_colors=len(labels), colormap=self.colormap, colors=self.color) colors = dict(zip(labels, color_values)) # Transform labels into a map of class to label labels = dict(zip(self.classes_, labels)) # Define boundaries with a vertical line if self.annotate_docs: for xcoords in self.boundaries_: self.ax.axvline(x=xcoords, color='lightgray', linestyle='dashed') series = defaultdict(lambda: {'x':[], 'y':[]}) if target is not None: for point, t in zip(points, target): label = labels[t] series[label]['x'].append(point[0]) series[label]['y'].append(point[1]) else: label = self.classes_[0] for x, y in points: series[label]['x'].append(x) series[label]['y'].append(y) for label, points in series.items(): self.ax.scatter(points['x'], points['y'], marker='|', c=colors[label], zorder=100, label=label) self.ax.set_yticks(list(range(len(self.indexed_words_)))) self.ax.set_yticklabels(self.indexed_words_) def finalize(self, **kwargs): """ The finalize method executes any subclass-specific axes finalization steps. The user calls poof & poof calls finalize. Parameters ---------- kwargs: generic keyword arguments. """ self.ax.set_ylim(-1, len(self.indexed_words_)) self.ax.set_title("Lexical Dispersion Plot") self.ax.set_xlabel("Word Offset") self.ax.grid(False) # Add the legend outside of the figure box. if not all(self.classes_ == np.array([self.NULL_CLASS])): box = self.ax.get_position() self.ax.set_position([box.x0, box.y0, box.width * 0.8, box.height]) self.ax.legend(loc='center left', bbox_to_anchor=(1, 0.5)) ########################################################################## ## Quick Method ########################################################################## def dispersion(words, corpus, y=None, ax=None, colors=None, colormap=None, labels=None, annotate_docs=False, ignore_case=False, **kwargs): """ Displays lexical dispersion plot for words in a corpus This helper function is a quick wrapper to utilize the DisperstionPlot Visualizer for one-off analysis Parameters ---------- words : list A list of words whose dispersion will be examined within a corpus y : ndarray or Series of length n An optional array or series of target or class values for instances. If this is specified, then the points will be colored according to their class. corpus : list Should be provided as a list of documents that contain a list of words in the order they appear in the document. ax : matplotlib axes, default: None The axes to plot the figure on. labels : list of strings The names of the classes in the target, used to create a legend. Labels must match names of classes in sorted order. colors : list or tuple of colors Specify the colors for each individual class colormap : string or matplotlib cmap Qualitative colormap for discrete target annotate_docs : boolean, default: False Specify whether document boundaries will be displayed. Vertical lines are positioned at the end of each document. ignore_case : boolean, default: False Specify whether input will be case-sensitive. kwargs : dict Pass any additional keyword arguments to the super class. Returns ------- ax: matplotlib axes Returns the axes that the plot was drawn on """ # Instantiate the visualizer visualizer = DispersionPlot( words, ax=ax, colors=colors, colormap=colormap, ignore_case=ignore_case, labels=labels, annotate_docs=annotate_docs, **kwargs ) # Fit and transform the visualizer (calls draw) visualizer.fit(corpus, y, **kwargs) # Return the axes object on the visualizer return visualizer.ax
apache-2.0
robcarver17/systematictradingexamples
plots_for_perhaps/compareoptmethods.py
1
22426
import numpy as np import matplotlib.pyplot as plt from matplotlib.pyplot import plot, show, xticks, xlabel, ylabel, legend, yscale, title, savefig, rcParams, figure, hist, text, bar, subplots import Image def file_process(filename): fig = plt.gcf() fig.set_size_inches(18.5,10.5) fig.savefig("/home/rob/%s.png" % filename,dpi=300) fig.savefig("/home/rob/%sLOWRES.png" % filename,dpi=50) Image.open("/home/rob/%s.png" % filename).convert('L').save("/home/rob/%s.jpg" % filename) Image.open("/home/rob/%sLOWRES.png" % filename).convert('L').save("/home/rob/%sLOWRES.jpg" % filename) """ compare: handcrafting bootstrapped one shot equal weights market cap weights """ import pandas as pd from datetime import datetime as dt def read_ts_csv(fname, dindex="Date"): data=pd.read_csv(fname) dateindex=[dt.strptime(dx, "%d/%m/%y") for dx in list(data[dindex])] data.index=dateindex del(data[dindex]) return data def calc_asset_returns(rawdata, tickers): asset_returns=pd.concat([get_monthly_tr(tickname, rawdata) for tickname in tickers], axis=1) asset_returns.columns=tickers return asset_returns def get_monthly_tr(tickname, rawdata): total_returns=rawdata[tickname+"_TR"] return (total_returns / total_returns.shift(1)) - 1.0 def portfolio_return(asset_returns, cash_weights): index_returns=asset_returns.cumsum().ffill().diff() cash_align = cash_weights.reindex(asset_returns.index, method="ffill") cash_align[np.isnan(index_returns)]=0.0 cash_align[np.isnan(cash_align)]=0.0 vols=pd.ewmstd(asset_returns, span=100, min_periods=1) riskweights=pd.DataFrame(cash_align.values / vols.values, index=vols.index) riskweights.columns=asset_returns.columns riskweights[np.isnan(riskweights)]=0.0 def _rowfix(x): if all([y==0.0 for y in x]): return x sumx=sum(x) return [y/sumx for y in x] riskweights = riskweights.apply(_rowfix, axis=1) portfolio_returns=asset_returns*riskweights portfolio_returns[np.isnan(portfolio_returns)]=0.0 portfolio_returns=portfolio_returns.sum(axis=1) return portfolio_returns import matplotlib.pyplot as plt from scipy import stats import pandas as pd import numpy as np from datetime import datetime as dt import datetime from scipy.optimize import minimize from copy import copy import random def correlation_matrix(returns): """ Calcs a correlation matrix using weekly returns from a pandas time series We use weekly returns because otherwise end of day effects, especially over time zones, give unrealistically low correlations """ asset_index=returns.cumsum().ffill() asset_index=asset_index.resample('1W') ## Only want index, fill method is irrelevant asset_index = asset_index - asset_index.shift(1) return asset_index.corr().values def create_dull_pd_matrix(dullvalue=0.0, dullname="A", startdate=pd.datetime(1970,1,1).date(), enddate=datetime.datetime.now().date(), index=None): """ create a single valued pd matrix """ if index is None: index=pd.date_range(startdate, enddate) dullvalue=np.array([dullvalue]*len(index)) ans=pd.DataFrame(dullvalue, index, columns=[dullname]) return ans def addem(weights): ## Used for constraints return 1.0 - sum(weights) def variance(weights, sigma): ## returns the variance (NOT standard deviation) given weights and sigma return (np.matrix(weights)*sigma*np.matrix(weights).transpose())[0,0] def neg_SR(weights, sigma, mus): ## Returns minus the Sharpe Ratio (as we're minimising) """ estreturn=250.0*((np.matrix(x)*mus)[0,0]) variance=(variance(x,sigma)**.5)*16.0 """ estreturn=(np.matrix(weights)*mus)[0,0] std_dev=(variance(weights,sigma)**.5) return -estreturn/std_dev def sigma_from_corr(std, corr): sigma=std*corr*std return sigma def basic_opt(std,corr,mus): number_assets=mus.shape[0] sigma=sigma_from_corr(std, corr) start_weights=[1.0/number_assets]*number_assets ## Constraints - positive weights, adding to 1.0 bounds=[(0.0,1.0)]*number_assets cdict=[{'type':'eq', 'fun':addem}] return minimize(neg_SR_riskfree, start_weights, (sigma, mus), method='SLSQP', bounds=bounds, constraints=cdict, tol=0.00001) def neg_SR_riskfree(weights, sigma, mus, riskfree=0.005): ## Returns minus the Sharpe Ratio (as we're minimising) """ estreturn=250.0*((np.matrix(x)*mus)[0,0]) variance=(variance(x,sigma)**.5)*16.0 """ estreturn=(np.matrix(weights)*mus)[0,0] - riskfree std_dev=(variance(weights,sigma)**.5) return -estreturn/std_dev def equalise_vols(returns, default_vol): """ Normalises returns so they have the in sample vol of defaul_vol (annualised) Assumes daily returns """ factors=(default_vol/16.0)/returns.std(axis=0) facmat=create_dull_pd_matrix(dullvalue=factors, dullname=returns.columns, index=returns.index) norm_returns=returns*facmat norm_returns.columns=returns.columns return norm_returns def offdiag_matrix(offvalue, nlength): identity=np.diag([1.0]*nlength) for x in range(nlength): for y in range(nlength): if x!=y: identity[x][y]=offvalue return identity def get_avg_corr(sigma): new_sigma=copy(sigma) np.fill_diagonal(new_sigma,np.nan) return np.nanmean(new_sigma) def nearest_to_listvals(x, lvalues=[0.0, 0.25, 0.5, 0.75, 0.9]): ## return x rounded to nearest of lvalues if len(lvalues)==1: return lvalues[0] d1=abs(x - lvalues[0]) d2=abs(x - lvalues[1]) if d1<d2: return lvalues[0] newlvalues=lvalues[1:] return nearest_to_listvals(x, newlvalues) def handcrafted(returns, equalisevols=True, default_vol=0.2): """ Handcrafted optimiser """ count_assets=len(returns.columns) try: assert equalisevols is True assert count_assets<=3 except: raise Exception("Handcrafting only works with equalised vols and 3 or fewer assets") if count_assets<3: ## Equal weights return [1.0/count_assets]*count_assets est_corr=returns.corr().values c1=nearest_to_listvals(est_corr[0][1]) c2=nearest_to_listvals(est_corr[0][2]) c3=nearest_to_listvals(est_corr[1][2]) wts_to_use=HANDCRAFTED_WTS[(HANDCRAFTED_WTS.c1==c1) & (HANDCRAFTED_WTS.c2==c2) & (HANDCRAFTED_WTS.c3==c3)].irow(0) return [wts_to_use.w1, wts_to_use.w2, wts_to_use.w3] def opt_shrinkage(returns, shrinkage_factors, equalisevols=True, default_vol=0.2): """ Returns the optimal portfolio for the dataframe returns using shrinkage shrinkage_factors is a tuple, shrinkage of mean and correlation If equalisevols=True then normalises returns to have same standard deviation; the weights returned will be 'risk weightings' """ if equalisevols: use_returns=equalise_vols(returns, default_vol) else: use_returns=returns (shrinkage_mean, shrinkage_corr)=shrinkage_factors ## Sigma matrix ## Use correlation and then convert back to variance est_corr=use_returns.corr().values avg_corr=get_avg_corr(est_corr) prior_corr=offdiag_matrix(avg_corr, est_corr.shape[0]) sigma_corr=shrinkage_corr*prior_corr+(1-shrinkage_corr)*est_corr cov_vector=use_returns.std().values sigma=cov_vector*sigma_corr*cov_vector ## mus vector avg_return=np.mean(use_returns.mean()) est_mus=np.array([use_returns[asset_name].mean() for asset_name in use_returns.columns], ndmin=2).transpose() prior_mus=np.array([avg_return for asset_name in use_returns.columns], ndmin=2).transpose() mus=shrinkage_mean*prior_mus+(1-shrinkage_mean)*est_mus ## Starting weights number_assets=use_returns.shape[1] start_weights=[1.0/number_assets]*number_assets ## Constraints - positive weights, adding to 1.0 bounds=[(0.0,1.0)]*number_assets cdict=[{'type':'eq', 'fun':addem}] ans=minimize(neg_SR, start_weights, (sigma, mus), method='SLSQP', bounds=bounds, constraints=cdict, tol=0.00001) return ans['x'] def handcraft_equal(returns): """ dynamic handcrafting, equal weights only """ ## RETURNS Correlation matrix use_returns=equalise_vols(returns, default_vol=16.0) ## Sigma matrix = correlations sigma=use_returns.cov() sigma[sigma<0.0]=0.0 ungroupedreturns=dict([(x,returns[x]) for x in returns.columns]) tree_data=hc_sigma(sigma, ungroupedreturns) tree_data=grouping_tree(tree_data) weights=tree_to_weights(tree_data) return weights def hc_sigma(ungrouped_sigma, ungroupedreturns, groupdata=None): """ handcraft weights from sigma matrix Algo: - Find pair of assets with highest correlation - Form them into a new group with equal weights - The group becomes like a new asset - Once we only have two assets left, stop. Need to """ if len(ungroupedreturns)==1: return groupdata[1] if groupdata is None: ## first run ## groupdata stores grouping information ## To begin with each group just consists of one asset groupdata=[[],list(ungrouped_sigma.columns)] groupedreturns=dict() ## iteration while len(ungroupedreturns)>0: ## current_sigma consists of the correlation of things we currently have if len(ungroupedreturns)==1: idx_list=[0] else: idx_list=find_highest_corr(ungrouped_sigma) name_list=tuple([ungrouped_sigma.columns[idx] for idx in idx_list]) ## pair those things up (ungrouped_sigma, ungroupedreturns, groupedreturns, groupdata)=group_assets(ungrouped_sigma, ungroupedreturns, groupedreturns, groupdata, idx_list, name_list) new_returns=pd.concat(groupedreturns, axis=1) new_sigma=new_returns.corr() ## recursive return hc_sigma(new_sigma, groupedreturns, groupdata=[[],groupdata[0]]) def find_highest_corr(sigmat): new_sigmat=copy(sigmat.values) np.fill_diagonal(new_sigmat, -100.0) (i,j)=np.unravel_index(new_sigmat.argmax(), new_sigmat.shape) return (i,j) def group_assets(ungrouped_sigma, ungroupedreturns, groupedreturns, groupdata, idx_list, name_list): """ Group assets """ todelete=[] names=[] grouping=[] group_returns=[] weights=[1.0/len(idx_list)]*len(idx_list) ## could have more complex thing here... for (itemweight,idx, iname) in zip(weights,idx_list, name_list): gi=groupdata[1][idx] grouping.append(gi) gri=ungroupedreturns.pop(iname) group_returns.append(gri*itemweight) names.append(gri.name) ungrouped_sigma=ungrouped_sigma.drop(iname, axis=0) ungrouped_sigma=ungrouped_sigma.drop(iname, axis=1) todelete.append(idx) groupdata[0].append(grouping) gr_returns=pd.concat(group_returns, axis=1) gr_returns=gr_returns.sum(axis=1) gr_returns.name="[%s]" % "+".join(names) print "Pairing %s" % ", ".join(names) groupedreturns[gr_returns.name]=gr_returns groupdata[1]=[element for eindex, element in enumerate(groupdata[1]) if eindex not in todelete] return (ungrouped_sigma, ungroupedreturns, groupedreturns, groupdata) def grouping_tree(tree_data, sigma): """ Group branches of 2 into larger if possible """ pass def corrs_in_group(group, sigma): asset_list=sum(group, []) littlesigma=sigma.loc[asset_list, asset_list] def corr_from_leaf(leaf, sigma): return sigma[leaf[0]][leaf[1]] def tree_to_weights(tree_data): """ convert a tree into weights """ pass def markosolver(returns, equalisemeans=False, equalisevols=True, default_vol=0.2, default_SR=1.0): """ Returns the optimal portfolio for the dataframe returns If equalisemeans=True then assumes all assets have same return if False uses the asset means If equalisevols=True then normalises returns to have same standard deviation; the weights returned will be 'risk weightings' Note if usemeans=True and equalisevols=True effectively assumes all assets have same sharpe ratio """ if equalisevols: use_returns=equalise_vols(returns, default_vol) else: use_returns=returns ## Sigma matrix sigma=use_returns.cov().values ## Expected mean returns est_mus=[use_returns[asset_name].mean() for asset_name in use_returns.columns] missingvals=[np.isnan(x) for x in est_mus] if equalisemeans: ## Don't use the data - Set to the average Sharpe Ratio mus=[default_vol*default_SR]*returns.shape[1] else: mus=est_mus mus=np.array(mus, ndmin=2).transpose() ## Starting weights number_assets=use_returns.shape[1] start_weights=[1.0/number_assets]*number_assets ## Constraints - positive weights, adding to 1.0 bounds=[(0.0,1.0)]*number_assets cdict=[{'type':'eq', 'fun':addem}] ans=minimize(neg_SR, start_weights, (sigma, mus), method='SLSQP', bounds=bounds, constraints=cdict, tol=0.00001) wts=ans['x'] return wts def bootstrap_portfolio(returns_to_bs, monte_carlo=200, monte_length=250, equalisemeans=False, equalisevols=True, default_vol=0.2, default_SR=1.0): """ Given dataframe of returns; returns_to_bs, performs a bootstrap optimisation We run monte_carlo numbers of bootstraps Each one contains monte_length days drawn randomly, with replacement (so *not* block bootstrapping) The other arguments are passed to the optimisation function markosolver Note - doesn't deal gracefully with missing data. Will end up downweighting stuff depending on how much data is missing in each boostrap. You'll need to think about how to solve this problem. """ weightlist=[] for unused_index in range(monte_carlo): bs_idx=[int(random.uniform(0,1)*len(returns_to_bs)) for i in range(monte_length)] returns=returns_to_bs.iloc[bs_idx,:] weight=markosolver(returns, equalisemeans=equalisemeans, equalisevols=equalisevols, default_vol=default_vol, default_SR=default_SR) weightlist.append(weight) ### We can take an average here; only because our weights always add up to 1. If that isn't true ### then you will need to some kind of renormalisation theweights_mean=list(np.mean(weightlist, axis=0)) return theweights_mean def optimise_over_periods(data, date_method, fit_method, rollyears=20, equalisemeans=False, equalisevols=True, monte_carlo=100, monte_length=None, shrinkage_factors=(0.5, 0.5), weightdf=None): """ Do an optimisation Returns data frame of weights Note if fitting in sample weights will be somewhat boring Doesn't deal with eg missing data in certain subperiods """ if monte_length is None: monte_length=int(len(data.index)*.1) ## Get the periods fit_periods=generate_fitting_dates(data, date_method, rollyears=rollyears) ## Do the fitting ## Build up a list of weights, which we'll concat weight_list=[] for fit_tuple in fit_periods: ## Fit on the slice defined by first two parts of the tuple period_subset_data=data[fit_tuple[0]:fit_tuple[1]] ## Can be slow, if bootstrapping, so indicate where we are print "Fitting data for %s to %s" % (str(fit_tuple[2]), str(fit_tuple[3])) if fit_method=="one_period": weights=markosolver(period_subset_data, equalisemeans=equalisemeans, equalisevols=equalisevols) elif fit_method=="bootstrap": weights=bootstrap_portfolio(period_subset_data, equalisemeans=equalisemeans, equalisevols=equalisevols, monte_carlo=monte_carlo, monte_length=monte_length) elif fit_method=="shrinkage": weights=opt_shrinkage(period_subset_data, shrinkage_factors=shrinkage_factors, equalisevols=equalisevols) elif fit_method=="fixed": weights=[float(weightdf[weightdf.Country==ticker].Weight.values) for ticker in list(period_subset_data.columns)] else: raise Exception("Fitting method %s unknown" % fit_method) ## We adjust dates slightly to ensure no overlaps dindex=[fit_tuple[2]+datetime.timedelta(seconds=1), fit_tuple[3]-datetime.timedelta(seconds=1)] ## create a double row to delineate start and end of test period weight_row=pd.DataFrame([weights]*2, index=dindex, columns=data.columns) weight_list.append(weight_row) weight_df=pd.concat(weight_list, axis=0) return weight_df """ Now we need to do this with expanding or rolling window """ """ Generate the date tuples """ def generate_fitting_dates(data, date_method, rollyears=20): """ generate a list 4 tuples, one element for each year in the data each tuple contains [fit_start, fit_end, period_start, period_end] datetime objects the last period will be a 'stub' if we haven't got an exact number of years date_method can be one of 'in_sample', 'expanding', 'rolling' if 'rolling' then use rollyears variable """ start_date=data.index[0] end_date=data.index[-1] ## generate list of dates, one year apart, including the final date yearstarts=list(pd.date_range(start_date, end_date, freq="12M"))+[end_date] ## loop through each period periods=[] for tidx in range(len(yearstarts))[1:-1]: ## these are the dates we test in period_start=yearstarts[tidx] period_end=yearstarts[tidx+1] ## now generate the dates we use to fit if date_method=="in_sample": fit_start=start_date elif date_method=="expanding": fit_start=start_date elif date_method=="rolling": yearidx_to_use=max(0, tidx-rollyears) fit_start=yearstarts[yearidx_to_use] else: raise Exception("don't recognise date_method %s" % date_method) if date_method=="in_sample": fit_end=end_date elif date_method in ['rolling', 'expanding']: fit_end=period_start else: raise Exception("don't recognise date_method %s " % date_method) periods.append([fit_start, fit_end, period_start, period_end]) ## give the user back the list of periods return periods rawdata=read_ts_csv("/home/rob/workspace/systematictradingexamples/plots_for_perhaps/MSCI_data.csv") refdata=pd.read_csv("/home/rob/workspace/systematictradingexamples/plots_for_perhaps/MSCI_ref.csv") tickers=list(refdata[(refdata.EmorDEV=="DEV") & (refdata.Type=="Country")].Country.values) #mom 12bp #tickers=list(refdata[refdata.Type=="Country"].Country.values) #mom 12bp fix_hcweights=pd.read_csv("/home/rob/workspace/systematictradingexamples/plots_for_perhaps/devhcweights.csv") fix_capweights=pd.read_csv("/home/rob/workspace/systematictradingexamples/plots_for_perhaps/devcapweights.csv") fix_eqweights=pd.DataFrame(dict(Country=tickers, Weight=[1.0/len(tickers)]*len(tickers))) data=calc_asset_returns(rawdata, tickers) ### IDEA: to boostrap the results ### Repeatedly draw from 'data' to make new pseudo series oneperiodweights=optimise_over_periods(data, "expanding", "one_period", equalisemeans=False, equalisevols=True) #bootstrapweights=optimise_over_periods(data, "expanding", "bootstrap", equalisemeans=True, equalisevols=True) exposthcweights=optimise_over_periods(data, "expanding", "fixed", weightdf=fix_hcweights, equalisemeans=True, equalisevols=True) equalweights=optimise_over_periods(data, "expanding", "fixed", weightdf=fix_eqweights, equalisemeans=True, equalisevols=True) marketcapweights=optimise_over_periods(data, "expanding", "fixed", weightdf=fix_capweights, equalisemeans=True, equalisevols=True) index_returns=(1.0+data).cumprod().ffill() last_return=index_returns.irow(-1).values last_return=pd.DataFrame(np.array([last_return]*len(data)), data.index) last_return.columns=data.columns index_returns = index_returns / last_return marketcapweights = marketcapweights.reindex(index_returns.index, method="ffill") marketcapweights=marketcapweights*index_returns marketcapweights=marketcapweights.ffill() ## portfolio, take out missing weights p1=portfolio_return(data, oneperiodweights)[pd.datetime(1994,1,1):] #p2=portfolio_return(data, bootstrapweights) p3=portfolio_return(data, exposthcweights)[pd.datetime(1994,1,1):] p4=portfolio_return(data, equalweights)[pd.datetime(1994,1,1):] p5=portfolio_return(data, marketcapweights)[pd.datetime(1994,1,1):] drag1=p3 - p1 drag2=p4 - p5 def stats(x): ann_mean=x.mean()*12 ann_std = x.std()*(12**.5) geo_mean = ann_mean - (ann_std**2)/2.0 sharpe = geo_mean / ann_std return (ann_mean, ann_std, geo_mean, sharpe) print stats(p1) print stats(p3) print stats(p4) print stats(p5) toplot=pd.concat([p1, p3, p4, p5], axis=1) toplot.columns=["Optimised", "Handcraft", "Equal", "Market Cap"] toplot.cumsum().plot() show() p1.cumsum().plot(color="black", ls="solid") p3.cumsum().plot(color="gray", ls="solid") p4.cumsum().plot(color="black", ls="dashed") p5.cumsum().plot(color="gray", ls="dashed") legend( ["Optimised", "Handcraft", "Equal", "Market Cap"], loc="upper left") frame=plt.gca() #frame.get_yaxis().set_visible(False) rcParams.update({'font.size': 18}) file_process("compareoptmethods") show() drag1.cumsum().plot(color="gray", ls="solid") legend( [ "Handcraft vs MktCap"], loc="upper left") frame=plt.gca() #frame.get_yaxis().set_visible(False) rcParams.update({'font.size': 18}) file_process("compareoptmethodstracking") show()
gpl-2.0
cloud-fan/spark
python/pyspark/pandas/internal.py
1
68330
# # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You under the Apache License, Version 2.0 # (the "License"); you may not use this file except in compliance with # the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """ An internal immutable DataFrame with some metadata to manage indexes. """ import re from typing import Any, Dict, List, Optional, Sequence, Tuple, Union, TYPE_CHECKING, cast from itertools import accumulate import py4j import numpy as np import pandas as pd from pandas.api.types import CategoricalDtype # noqa: F401 from pyspark import sql as spark from pyspark._globals import _NoValue, _NoValueType from pyspark.sql import functions as F, Window from pyspark.sql.functions import pandas_udf from pyspark.sql.types import ( # noqa: F401 BooleanType, DataType, IntegralType, LongType, StructField, StructType, StringType, ) # For running doctests and reference resolution in PyCharm. from pyspark import pandas as ps if TYPE_CHECKING: # This is required in old Python 3.5 to prevent circular reference. from pyspark.pandas.series import Series # noqa: F401 (SPARK-34943) from pyspark.pandas.spark.utils import as_nullable_spark_type, force_decimal_precision_scale from pyspark.pandas.data_type_ops.base import DataTypeOps from pyspark.pandas.typedef import ( Dtype, as_spark_type, extension_dtypes, infer_pd_series_spark_type, spark_type_to_pandas_dtype, ) from pyspark.pandas.utils import ( column_labels_level, default_session, is_name_like_tuple, is_testing, lazy_property, name_like_string, scol_for, spark_column_equals, verify_temp_column_name, ) # A function to turn given numbers to Spark columns that represent pandas-on-Spark index. SPARK_INDEX_NAME_FORMAT = "__index_level_{}__".format SPARK_DEFAULT_INDEX_NAME = SPARK_INDEX_NAME_FORMAT(0) # A pattern to check if the name of a Spark column is a pandas-on-Spark index name or not. SPARK_INDEX_NAME_PATTERN = re.compile(r"__index_level_[0-9]+__") NATURAL_ORDER_COLUMN_NAME = "__natural_order__" HIDDEN_COLUMNS = {NATURAL_ORDER_COLUMN_NAME} DEFAULT_SERIES_NAME = 0 SPARK_DEFAULT_SERIES_NAME = str(DEFAULT_SERIES_NAME) class InternalField: """ The internal field to store the dtype as well as the Spark's StructField optionally. Parameters ---------- dtype : numpy.dtype or pandas' ExtensionDtype The dtype for the field struct_field : StructField, optional The `StructField` for the field. If None, InternalFrame will properly set. """ def __init__(self, dtype: Dtype, struct_field: Optional[StructField] = None): self._dtype = dtype self._struct_field = struct_field @staticmethod def from_struct_field( struct_field: StructField, *, use_extension_dtypes: bool = False ) -> "InternalField": """ Returns a new InternalField object created from the given StructField. The dtype will be inferred from the data type of the given StructField. Parameters ---------- struct_field : StructField The StructField used to create a new InternalField object. use_extension_dtypes : bool If True, try to use the extension dtypes. Returns ------- InternalField """ return InternalField( dtype=spark_type_to_pandas_dtype( struct_field.dataType, use_extension_dtypes=use_extension_dtypes ), struct_field=struct_field, ) @property def dtype(self) -> Dtype: """Return the dtype for the field.""" return self._dtype @property def struct_field(self) -> Optional[StructField]: """Return the StructField for the field.""" return self._struct_field @property def name(self) -> str: """Return the field name if the StructField exists.""" assert self.struct_field is not None return self.struct_field.name @property def spark_type(self) -> DataType: """Return the spark data type for the field if the StructField exists.""" assert self.struct_field is not None return self.struct_field.dataType @property def nullable(self) -> bool: """Return the nullability for the field if the StructField exists.""" assert self.struct_field is not None return self.struct_field.nullable @property def metadata(self) -> Dict[str, Any]: """Return the metadata for the field if the StructField exists.""" assert self.struct_field is not None return self.struct_field.metadata @property def is_extension_dtype(self) -> bool: """Return whether the dtype for the field is an extension type or not.""" return isinstance(self.dtype, extension_dtypes) def normalize_spark_type(self) -> "InternalField": """Return a new InternalField object with normalized Spark data type.""" assert self.struct_field is not None return self.copy( spark_type=force_decimal_precision_scale(as_nullable_spark_type(self.spark_type)), nullable=True, ) def copy( self, *, name: Union[str, _NoValueType] = _NoValue, dtype: Union[Dtype, _NoValueType] = _NoValue, spark_type: Union[DataType, _NoValueType] = _NoValue, nullable: Union[bool, _NoValueType] = _NoValue, metadata: Union[Optional[Dict[str, Any]], _NoValueType] = _NoValue, ) -> "InternalField": """Copy the InternalField object.""" if name is _NoValue: name = self.name if dtype is _NoValue: dtype = self.dtype if spark_type is _NoValue: spark_type = self.spark_type if nullable is _NoValue: nullable = self.nullable if metadata is _NoValue: metadata = self.metadata return InternalField( dtype=cast(Dtype, dtype), struct_field=StructField( name=cast(str, name), dataType=cast(DataType, spark_type), nullable=cast(bool, nullable), metadata=cast(Optional[Dict[str, Any]], metadata), ), ) def __repr__(self) -> str: return "InternalField(dtype={dtype},struct_field={struct_field})".format( dtype=self.dtype, struct_field=self.struct_field ) class InternalFrame(object): """ The internal immutable DataFrame which manages Spark DataFrame and column names and index information. .. note:: this is an internal class. It is not supposed to be exposed to users and users should not directly access to it. The internal immutable DataFrame represents the index information for a DataFrame it belongs to. For instance, if we have a pandas-on-Spark DataFrame as below, pandas DataFrame does not store the index as columns. >>> psdf = ps.DataFrame({ ... 'A': [1, 2, 3, 4], ... 'B': [5, 6, 7, 8], ... 'C': [9, 10, 11, 12], ... 'D': [13, 14, 15, 16], ... 'E': [17, 18, 19, 20]}, columns = ['A', 'B', 'C', 'D', 'E']) >>> psdf # doctest: +NORMALIZE_WHITESPACE A B C D E 0 1 5 9 13 17 1 2 6 10 14 18 2 3 7 11 15 19 3 4 8 12 16 20 However, all columns including index column are also stored in Spark DataFrame internally as below. >>> psdf._internal.to_internal_spark_frame.show() # doctest: +NORMALIZE_WHITESPACE +-----------------+---+---+---+---+---+ |__index_level_0__| A| B| C| D| E| +-----------------+---+---+---+---+---+ | 0| 1| 5| 9| 13| 17| | 1| 2| 6| 10| 14| 18| | 2| 3| 7| 11| 15| 19| | 3| 4| 8| 12| 16| 20| +-----------------+---+---+---+---+---+ In order to fill this gap, the current metadata is used by mapping Spark's internal column to pandas-on-Spark's index. See the method below: * `spark_frame` represents the internal Spark DataFrame * `data_spark_column_names` represents non-indexing Spark column names * `data_spark_columns` represents non-indexing Spark columns * `data_fields` represents non-indexing InternalFields * `index_spark_column_names` represents internal index Spark column names * `index_spark_columns` represents internal index Spark columns * `index_fields` represents index InternalFields * `spark_column_names` represents all columns * `index_names` represents the external index name as a label * `to_internal_spark_frame` represents Spark DataFrame derived by the metadata. Includes index. * `to_pandas_frame` represents pandas DataFrame derived by the metadata >>> internal = psdf._internal >>> internal.spark_frame.show() # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS +-----------------+---+---+---+---+---+-----------------+ |__index_level_0__| A| B| C| D| E|__natural_order__| +-----------------+---+---+---+---+---+-----------------+ | 0| 1| 5| 9| 13| 17| ...| | 1| 2| 6| 10| 14| 18| ...| | 2| 3| 7| 11| 15| 19| ...| | 3| 4| 8| 12| 16| 20| ...| +-----------------+---+---+---+---+---+-----------------+ >>> internal.data_spark_column_names ['A', 'B', 'C', 'D', 'E'] >>> internal.index_spark_column_names ['__index_level_0__'] >>> internal.spark_column_names ['__index_level_0__', 'A', 'B', 'C', 'D', 'E'] >>> internal.index_names [None] >>> internal.data_fields # doctest: +NORMALIZE_WHITESPACE [InternalField(dtype=int64,struct_field=StructField(A,LongType,false)), InternalField(dtype=int64,struct_field=StructField(B,LongType,false)), InternalField(dtype=int64,struct_field=StructField(C,LongType,false)), InternalField(dtype=int64,struct_field=StructField(D,LongType,false)), InternalField(dtype=int64,struct_field=StructField(E,LongType,false))] >>> internal.index_fields [InternalField(dtype=int64,struct_field=StructField(__index_level_0__,LongType,false))] >>> internal.to_internal_spark_frame.show() # doctest: +NORMALIZE_WHITESPACE +-----------------+---+---+---+---+---+ |__index_level_0__| A| B| C| D| E| +-----------------+---+---+---+---+---+ | 0| 1| 5| 9| 13| 17| | 1| 2| 6| 10| 14| 18| | 2| 3| 7| 11| 15| 19| | 3| 4| 8| 12| 16| 20| +-----------------+---+---+---+---+---+ >>> internal.to_pandas_frame A B C D E 0 1 5 9 13 17 1 2 6 10 14 18 2 3 7 11 15 19 3 4 8 12 16 20 In case that index is set to one of the existing column as below: >>> psdf1 = psdf.set_index("A") >>> psdf1 # doctest: +NORMALIZE_WHITESPACE B C D E A 1 5 9 13 17 2 6 10 14 18 3 7 11 15 19 4 8 12 16 20 >>> psdf1._internal.to_internal_spark_frame.show() # doctest: +NORMALIZE_WHITESPACE +---+---+---+---+---+ | A| B| C| D| E| +---+---+---+---+---+ | 1| 5| 9| 13| 17| | 2| 6| 10| 14| 18| | 3| 7| 11| 15| 19| | 4| 8| 12| 16| 20| +---+---+---+---+---+ >>> internal = psdf1._internal >>> internal.spark_frame.show() # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS +-----------------+---+---+---+---+---+-----------------+ |__index_level_0__| A| B| C| D| E|__natural_order__| +-----------------+---+---+---+---+---+-----------------+ | 0| 1| 5| 9| 13| 17| ...| | 1| 2| 6| 10| 14| 18| ...| | 2| 3| 7| 11| 15| 19| ...| | 3| 4| 8| 12| 16| 20| ...| +-----------------+---+---+---+---+---+-----------------+ >>> internal.data_spark_column_names ['B', 'C', 'D', 'E'] >>> internal.index_spark_column_names ['A'] >>> internal.spark_column_names ['A', 'B', 'C', 'D', 'E'] >>> internal.index_names [('A',)] >>> internal.data_fields [InternalField(dtype=int64,struct_field=StructField(B,LongType,false)), InternalField(dtype=int64,struct_field=StructField(C,LongType,false)), InternalField(dtype=int64,struct_field=StructField(D,LongType,false)), InternalField(dtype=int64,struct_field=StructField(E,LongType,false))] >>> internal.index_fields [InternalField(dtype=int64,struct_field=StructField(A,LongType,false))] >>> internal.to_internal_spark_frame.show() # doctest: +NORMALIZE_WHITESPACE +---+---+---+---+---+ | A| B| C| D| E| +---+---+---+---+---+ | 1| 5| 9| 13| 17| | 2| 6| 10| 14| 18| | 3| 7| 11| 15| 19| | 4| 8| 12| 16| 20| +---+---+---+---+---+ >>> internal.to_pandas_frame # doctest: +NORMALIZE_WHITESPACE B C D E A 1 5 9 13 17 2 6 10 14 18 3 7 11 15 19 4 8 12 16 20 In case that index becomes a multi index as below: >>> psdf2 = psdf.set_index("A", append=True) >>> psdf2 # doctest: +NORMALIZE_WHITESPACE B C D E A 0 1 5 9 13 17 1 2 6 10 14 18 2 3 7 11 15 19 3 4 8 12 16 20 >>> psdf2._internal.to_internal_spark_frame.show() # doctest: +NORMALIZE_WHITESPACE +-----------------+---+---+---+---+---+ |__index_level_0__| A| B| C| D| E| +-----------------+---+---+---+---+---+ | 0| 1| 5| 9| 13| 17| | 1| 2| 6| 10| 14| 18| | 2| 3| 7| 11| 15| 19| | 3| 4| 8| 12| 16| 20| +-----------------+---+---+---+---+---+ >>> internal = psdf2._internal >>> internal.spark_frame.show() # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS +-----------------+---+---+---+---+---+-----------------+ |__index_level_0__| A| B| C| D| E|__natural_order__| +-----------------+---+---+---+---+---+-----------------+ | 0| 1| 5| 9| 13| 17| ...| | 1| 2| 6| 10| 14| 18| ...| | 2| 3| 7| 11| 15| 19| ...| | 3| 4| 8| 12| 16| 20| ...| +-----------------+---+---+---+---+---+-----------------+ >>> internal.data_spark_column_names ['B', 'C', 'D', 'E'] >>> internal.index_spark_column_names ['__index_level_0__', 'A'] >>> internal.spark_column_names ['__index_level_0__', 'A', 'B', 'C', 'D', 'E'] >>> internal.index_names [None, ('A',)] >>> internal.data_fields # doctest: +NORMALIZE_WHITESPACE [InternalField(dtype=int64,struct_field=StructField(B,LongType,false)), InternalField(dtype=int64,struct_field=StructField(C,LongType,false)), InternalField(dtype=int64,struct_field=StructField(D,LongType,false)), InternalField(dtype=int64,struct_field=StructField(E,LongType,false))] >>> internal.index_fields # doctest: +NORMALIZE_WHITESPACE [InternalField(dtype=int64,struct_field=StructField(__index_level_0__,LongType,false)), InternalField(dtype=int64,struct_field=StructField(A,LongType,false))] >>> internal.to_internal_spark_frame.show() # doctest: +NORMALIZE_WHITESPACE +-----------------+---+---+---+---+---+ |__index_level_0__| A| B| C| D| E| +-----------------+---+---+---+---+---+ | 0| 1| 5| 9| 13| 17| | 1| 2| 6| 10| 14| 18| | 2| 3| 7| 11| 15| 19| | 3| 4| 8| 12| 16| 20| +-----------------+---+---+---+---+---+ >>> internal.to_pandas_frame # doctest: +NORMALIZE_WHITESPACE B C D E A 0 1 5 9 13 17 1 2 6 10 14 18 2 3 7 11 15 19 3 4 8 12 16 20 For multi-level columns, it also holds column_labels >>> columns = pd.MultiIndex.from_tuples([('X', 'A'), ('X', 'B'), ... ('Y', 'C'), ('Y', 'D')]) >>> psdf3 = ps.DataFrame([ ... [1, 2, 3, 4], ... [5, 6, 7, 8], ... [9, 10, 11, 12], ... [13, 14, 15, 16], ... [17, 18, 19, 20]], columns = columns) >>> psdf3 # doctest: +NORMALIZE_WHITESPACE X Y A B C D 0 1 2 3 4 1 5 6 7 8 2 9 10 11 12 3 13 14 15 16 4 17 18 19 20 >>> internal = psdf3._internal >>> internal.spark_frame.show() # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS +-----------------+------+------+------+------+-----------------+ |__index_level_0__|(X, A)|(X, B)|(Y, C)|(Y, D)|__natural_order__| +-----------------+------+------+------+------+-----------------+ | 0| 1| 2| 3| 4| ...| | 1| 5| 6| 7| 8| ...| | 2| 9| 10| 11| 12| ...| | 3| 13| 14| 15| 16| ...| | 4| 17| 18| 19| 20| ...| +-----------------+------+------+------+------+-----------------+ >>> internal.data_spark_column_names ['(X, A)', '(X, B)', '(Y, C)', '(Y, D)'] >>> internal.column_labels [('X', 'A'), ('X', 'B'), ('Y', 'C'), ('Y', 'D')] For Series, it also holds scol to represent the column. >>> psseries = psdf1.B >>> psseries A 1 5 2 6 3 7 4 8 Name: B, dtype: int64 >>> internal = psseries._internal >>> internal.spark_frame.show() # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS +-----------------+---+---+---+---+---+-----------------+ |__index_level_0__| A| B| C| D| E|__natural_order__| +-----------------+---+---+---+---+---+-----------------+ | 0| 1| 5| 9| 13| 17| ...| | 1| 2| 6| 10| 14| 18| ...| | 2| 3| 7| 11| 15| 19| ...| | 3| 4| 8| 12| 16| 20| ...| +-----------------+---+---+---+---+---+-----------------+ >>> internal.data_spark_column_names ['B'] >>> internal.index_spark_column_names ['A'] >>> internal.spark_column_names ['A', 'B'] >>> internal.index_names [('A',)] >>> internal.data_fields [InternalField(dtype=int64,struct_field=StructField(B,LongType,false))] >>> internal.index_fields [InternalField(dtype=int64,struct_field=StructField(A,LongType,false))] >>> internal.to_internal_spark_frame.show() # doctest: +NORMALIZE_WHITESPACE +---+---+ | A| B| +---+---+ | 1| 5| | 2| 6| | 3| 7| | 4| 8| +---+---+ >>> internal.to_pandas_frame # doctest: +NORMALIZE_WHITESPACE B A 1 5 2 6 3 7 4 8 """ def __init__( self, spark_frame: spark.DataFrame, index_spark_columns: Optional[List[spark.Column]], index_names: Optional[List[Optional[Tuple]]] = None, index_fields: Optional[List[InternalField]] = None, column_labels: Optional[List[Tuple]] = None, data_spark_columns: Optional[List[spark.Column]] = None, data_fields: Optional[List[InternalField]] = None, column_label_names: Optional[List[Optional[Tuple]]] = None, ): """ Create a new internal immutable DataFrame to manage Spark DataFrame, column fields and index fields and names. :param spark_frame: Spark DataFrame to be managed. :param index_spark_columns: list of Spark Column Spark Columns for the index. :param index_names: list of tuples the index names. :param index_fields: list of InternalField the InternalFields for the index columns :param column_labels: list of tuples with the same length The multi-level values in the tuples. :param data_spark_columns: list of Spark Column Spark Columns to appear as columns. If this is None, calculated from spark_frame. :param data_fields: list of InternalField the InternalFields for the data columns :param column_label_names: Names for each of the column index levels. See the examples below to refer what each parameter means. >>> column_labels = pd.MultiIndex.from_tuples( ... [('a', 'x'), ('a', 'y'), ('b', 'z')], names=["column_labels_a", "column_labels_b"]) >>> row_index = pd.MultiIndex.from_tuples( ... [('foo', 'bar'), ('foo', 'bar'), ('zoo', 'bar')], ... names=["row_index_a", "row_index_b"]) >>> psdf = ps.DataFrame( ... [[1, 2, 3], [4, 5, 6], [7, 8, 9]], index=row_index, columns=column_labels) >>> psdf.set_index(('a', 'x'), append=True, inplace=True) >>> psdf # doctest: +NORMALIZE_WHITESPACE column_labels_a a b column_labels_b y z row_index_a row_index_b (a, x) foo bar 1 2 3 4 5 6 zoo bar 7 8 9 >>> internal = psdf._internal >>> internal.spark_frame.show() # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS +-----------------+-----------------+------+------+------+... |__index_level_0__|__index_level_1__|(a, x)|(a, y)|(b, z)|... +-----------------+-----------------+------+------+------+... | foo| bar| 1| 2| 3|... | foo| bar| 4| 5| 6|... | zoo| bar| 7| 8| 9|... +-----------------+-----------------+------+------+------+... >>> internal.index_spark_columns # doctest: +SKIP [Column<'__index_level_0__'>, Column<'__index_level_1__'>, Column<'(a, x)'>] >>> internal.index_names [('row_index_a',), ('row_index_b',), ('a', 'x')] >>> internal.index_fields # doctest: +NORMALIZE_WHITESPACE [InternalField(dtype=object,struct_field=StructField(__index_level_0__,StringType,false)), InternalField(dtype=object,struct_field=StructField(__index_level_1__,StringType,false)), InternalField(dtype=int64,struct_field=StructField((a, x),LongType,false))] >>> internal.column_labels [('a', 'y'), ('b', 'z')] >>> internal.data_spark_columns # doctest: +SKIP [Column<'(a, y)'>, Column<'(b, z)'>] >>> internal.data_fields # doctest: +NORMALIZE_WHITESPACE [InternalField(dtype=int64,struct_field=StructField((a, y),LongType,false)), InternalField(dtype=int64,struct_field=StructField((b, z),LongType,false))] >>> internal.column_label_names [('column_labels_a',), ('column_labels_b',)] """ assert isinstance(spark_frame, spark.DataFrame) assert not spark_frame.isStreaming, "pandas-on-Spark does not support Structured Streaming." if not index_spark_columns: if data_spark_columns is not None: if column_labels is not None: data_spark_columns = [ scol.alias(name_like_string(label)) for scol, label in zip(data_spark_columns, column_labels) ] spark_frame = spark_frame.select(data_spark_columns) assert not any(SPARK_INDEX_NAME_PATTERN.match(name) for name in spark_frame.columns), ( "Index columns should not appear in columns of the Spark DataFrame. Avoid " "index column names [%s]." % SPARK_INDEX_NAME_PATTERN ) # Create default index. spark_frame, force_nullable = InternalFrame.attach_default_index(spark_frame) index_spark_columns = [scol_for(spark_frame, SPARK_DEFAULT_INDEX_NAME)] index_fields = [ InternalField.from_struct_field( StructField(SPARK_DEFAULT_INDEX_NAME, LongType(), nullable=False) ) ] if data_spark_columns is not None: data_struct_fields = [ field for field in spark_frame.schema.fields if field.name != SPARK_DEFAULT_INDEX_NAME ] data_spark_columns = [ scol_for(spark_frame, field.name) for field in data_struct_fields ] if data_fields is not None: data_fields = [ field.copy( name=name_like_string(struct_field.name), nullable=(force_nullable or field.nullable), ) for field, struct_field in zip(data_fields, data_struct_fields) ] if NATURAL_ORDER_COLUMN_NAME not in spark_frame.columns: spark_frame = spark_frame.withColumn( NATURAL_ORDER_COLUMN_NAME, F.monotonically_increasing_id() ) self._sdf = spark_frame # type: spark.DataFrame # index_spark_columns assert all( isinstance(index_scol, spark.Column) for index_scol in index_spark_columns ), index_spark_columns self._index_spark_columns = index_spark_columns # type: List[spark.Column] # data_spark_columns if data_spark_columns is None: data_spark_columns = [ scol_for(spark_frame, col) for col in spark_frame.columns if all( not spark_column_equals(scol_for(spark_frame, col), index_scol) for index_scol in index_spark_columns ) and col not in HIDDEN_COLUMNS ] self._data_spark_columns = data_spark_columns # type: List[spark.Column] else: assert all(isinstance(scol, spark.Column) for scol in data_spark_columns) self._data_spark_columns = data_spark_columns # fields if index_fields is None: index_fields = [None] * len(index_spark_columns) if data_fields is None: data_fields = [None] * len(data_spark_columns) assert len(index_spark_columns) == len(index_fields), ( len(index_spark_columns), len(index_fields), ) assert len(data_spark_columns) == len(data_fields), ( len(data_spark_columns), len(data_fields), ) if any(field is None or field.struct_field is None for field in index_fields) and any( field is None or field.struct_field is None for field in data_fields ): schema = spark_frame.select(index_spark_columns + data_spark_columns).schema fields = [ InternalField.from_struct_field(struct_field) if field is None else InternalField(field.dtype, struct_field) if field.struct_field is None else field for field, struct_field in zip(index_fields + data_fields, schema.fields) ] index_fields = fields[: len(index_spark_columns)] data_fields = fields[len(index_spark_columns) :] elif any(field is None or field.struct_field is None for field in index_fields): schema = spark_frame.select(index_spark_columns).schema index_fields = [ InternalField.from_struct_field(struct_field) if field is None else InternalField(field.dtype, struct_field) if field.struct_field is None else field for field, struct_field in zip(index_fields, schema.fields) ] elif any(field is None or field.struct_field is None for field in data_fields): schema = spark_frame.select(data_spark_columns).schema data_fields = [ InternalField.from_struct_field(struct_field) if field is None else InternalField(field.dtype, struct_field) if field.struct_field is None else field for field, struct_field in zip(data_fields, schema.fields) ] assert all( isinstance(ops.dtype, Dtype.__args__) # type: ignore and ( ops.dtype == np.dtype("object") or as_spark_type(ops.dtype, raise_error=False) is not None ) for ops in index_fields ), index_fields if is_testing(): struct_fields = spark_frame.select(index_spark_columns).schema.fields assert all( index_field.struct_field == struct_field for index_field, struct_field in zip(index_fields, struct_fields) ), (index_fields, struct_fields) self._index_fields = index_fields # type: List[InternalField] assert all( isinstance(ops.dtype, Dtype.__args__) # type: ignore and ( ops.dtype == np.dtype("object") or as_spark_type(ops.dtype, raise_error=False) is not None ) for ops in data_fields ), data_fields if is_testing(): struct_fields = spark_frame.select(data_spark_columns).schema.fields assert all( data_field.struct_field == struct_field for data_field, struct_field in zip(data_fields, struct_fields) ), (data_fields, struct_fields) self._data_fields = data_fields # type: List[InternalField] # index_names if not index_names: index_names = [None] * len(index_spark_columns) assert len(index_spark_columns) == len(index_names), ( len(index_spark_columns), len(index_names), ) assert all( is_name_like_tuple(index_name, check_type=True) for index_name in index_names ), index_names self._index_names = index_names # type: List[Optional[Tuple]] # column_labels if column_labels is None: self._column_labels = [ (col,) for col in spark_frame.select(self._data_spark_columns).columns ] # type: List[Tuple] else: assert len(column_labels) == len(self._data_spark_columns), ( len(column_labels), len(self._data_spark_columns), ) if len(column_labels) == 1: column_label = column_labels[0] assert is_name_like_tuple(column_label, check_type=True), column_label else: assert all( is_name_like_tuple(column_label, check_type=True) for column_label in column_labels ), column_labels assert len(set(len(label) for label in column_labels)) <= 1, column_labels self._column_labels = column_labels # column_label_names if column_label_names is None: self._column_label_names = [None] * column_labels_level( self._column_labels ) # type: List[Optional[Tuple]] else: if len(self._column_labels) > 0: assert len(column_label_names) == column_labels_level(self._column_labels), ( len(column_label_names), column_labels_level(self._column_labels), ) else: assert len(column_label_names) > 0, len(column_label_names) assert all( is_name_like_tuple(column_label_name, check_type=True) for column_label_name in column_label_names ), column_label_names self._column_label_names = column_label_names @staticmethod def attach_default_index( sdf: spark.DataFrame, default_index_type: Optional[str] = None ) -> Tuple[spark.DataFrame, bool]: """ This method attaches a default index to Spark DataFrame. Spark does not have the index notion so corresponding column should be generated. There are several types of default index can be configured by `compute.default_index_type`. >>> spark_frame = ps.range(10).to_spark() >>> spark_frame DataFrame[id: bigint] It adds the default index column '__index_level_0__'. >>> spark_frame = InternalFrame.attach_default_index(spark_frame)[0] >>> spark_frame DataFrame[__index_level_0__: bigint, id: bigint] It throws an exception if the given column name already exists. >>> InternalFrame.attach_default_index(spark_frame)[0] ... # doctest: +ELLIPSIS Traceback (most recent call last): ... AssertionError: '__index_level_0__' already exists... """ index_column = SPARK_DEFAULT_INDEX_NAME assert ( index_column not in sdf.columns ), "'%s' already exists in the Spark column names '%s'" % (index_column, sdf.columns) if default_index_type is None: default_index_type = ps.get_option("compute.default_index_type") if default_index_type == "sequence": return InternalFrame.attach_sequence_column(sdf, column_name=index_column) elif default_index_type == "distributed-sequence": return InternalFrame.attach_distributed_sequence_column(sdf, column_name=index_column) elif default_index_type == "distributed": return InternalFrame.attach_distributed_column(sdf, column_name=index_column) else: raise ValueError( "'compute.default_index_type' should be one of 'sequence'," " 'distributed-sequence' and 'distributed'" ) @staticmethod def attach_sequence_column( sdf: spark.DataFrame, column_name: str ) -> Tuple[spark.DataFrame, bool]: scols = [scol_for(sdf, column) for column in sdf.columns] sequential_index = ( F.row_number().over(Window.orderBy(F.monotonically_increasing_id())).cast("long") - 1 ) return sdf.select(sequential_index.alias(column_name), *scols), False @staticmethod def attach_distributed_column( sdf: spark.DataFrame, column_name: str ) -> Tuple[spark.DataFrame, bool]: scols = [scol_for(sdf, column) for column in sdf.columns] return sdf.select(F.monotonically_increasing_id().alias(column_name), *scols), False @staticmethod def attach_distributed_sequence_column( sdf: spark.DataFrame, column_name: str ) -> Tuple[spark.DataFrame, bool]: """ This method attaches a Spark column that has a sequence in a distributed manner. This is equivalent to the column assigned when default index type 'distributed-sequence'. >>> sdf = ps.DataFrame(['a', 'b', 'c']).to_spark() >>> sdf, force_nullable = ( ... InternalFrame.attach_distributed_sequence_column(sdf, column_name="sequence") ... ) >>> sdf.show() # doctest: +NORMALIZE_WHITESPACE +--------+---+ |sequence| 0| +--------+---+ | 0| a| | 1| b| | 2| c| +--------+---+ >>> force_nullable True """ if len(sdf.columns) > 0: try: jdf = sdf._jdf.toDF() # type: ignore sql_ctx = sdf.sql_ctx encoders = sql_ctx._jvm.org.apache.spark.sql.Encoders # type: ignore encoder = encoders.tuple(jdf.exprEnc(), encoders.scalaLong()) jrdd = jdf.localCheckpoint(False).rdd().zipWithIndex() df = spark.DataFrame( sql_ctx.sparkSession._jsparkSession.createDataset( # type: ignore jrdd, encoder ).toDF(), sql_ctx, ) columns = df.columns return ( df.selectExpr( "`{}` as `{}`".format(columns[1], column_name), "`{}`.*".format(columns[0]) ), True, ) except py4j.protocol.Py4JError: if is_testing(): raise return InternalFrame._attach_distributed_sequence_column(sdf, column_name) else: cnt = sdf.count() if cnt > 0: return default_session().range(cnt).toDF(column_name), False else: return ( default_session().createDataFrame( [], schema=StructType().add(column_name, data_type=LongType(), nullable=False), ), False, ) @staticmethod def _attach_distributed_sequence_column( sdf: spark.DataFrame, column_name: str ) -> Tuple[spark.DataFrame, bool]: """ >>> sdf = ps.DataFrame(['a', 'b', 'c']).to_spark() >>> sdf, force_nullable = ( ... InternalFrame._attach_distributed_sequence_column(sdf, column_name="sequence") ... ) >>> sdf.sort("sequence").show() # doctest: +NORMALIZE_WHITESPACE +--------+---+ |sequence| 0| +--------+---+ | 0| a| | 1| b| | 2| c| +--------+---+ >>> force_nullable False """ scols = [scol_for(sdf, column) for column in sdf.columns] spark_partition_column = verify_temp_column_name(sdf, "__spark_partition_id__") offset_column = verify_temp_column_name(sdf, "__offset__") row_number_column = verify_temp_column_name(sdf, "__row_number__") # 1. Calculates counts per each partition ID. `counts` here is, for instance, # { # 1: 83, # 6: 83, # 3: 83, # ... # } sdf = sdf.withColumn(spark_partition_column, F.spark_partition_id()) # Checkpoint the DataFrame to fix the partition ID. sdf = sdf.localCheckpoint(eager=False) counts = map( lambda x: (x["key"], x["count"]), sdf.groupby(sdf[spark_partition_column].alias("key")).count().collect(), ) # 2. Calculates cumulative sum in an order of partition id. # Note that it does not matter if partition id guarantees its order or not. # We just need a one-by-one sequential id. # sort by partition key. sorted_counts = sorted(counts, key=lambda x: x[0]) # get cumulative sum in an order of partition key. cumulative_counts = [0] + list(accumulate(map(lambda count: count[1], sorted_counts))) # zip it with partition key. sums = dict(zip(map(lambda count: count[0], sorted_counts), cumulative_counts)) # 3. Attach offset for each partition. @pandas_udf(returnType=LongType()) # type: ignore def offset(id: pd.Series) -> pd.Series: current_partition_offset = sums[id.iloc[0]] return pd.Series(current_partition_offset).repeat(len(id)) sdf = sdf.withColumn(offset_column, offset(spark_partition_column)) # 4. Calculate row_number in each partition. w = Window.partitionBy(spark_partition_column).orderBy(F.monotonically_increasing_id()) row_number = F.row_number().over(w) sdf = sdf.withColumn(row_number_column, row_number) # 5. Calculate the index. return ( sdf.select( (sdf[offset_column] + sdf[row_number_column] - 1).alias(column_name), *scols ), False, ) def spark_column_for(self, label: Tuple) -> spark.Column: """Return Spark Column for the given column label.""" column_labels_to_scol = dict(zip(self.column_labels, self.data_spark_columns)) if label in column_labels_to_scol: return column_labels_to_scol[label] else: raise KeyError(name_like_string(label)) def spark_column_name_for(self, label_or_scol: Union[Tuple, spark.Column]) -> str: """Return the actual Spark column name for the given column label.""" if isinstance(label_or_scol, spark.Column): return self.spark_frame.select(label_or_scol).columns[0] else: return self.field_for(label_or_scol).name def spark_type_for(self, label_or_scol: Union[Tuple, spark.Column]) -> DataType: """Return DataType for the given column label.""" if isinstance(label_or_scol, spark.Column): return self.spark_frame.select(label_or_scol).schema[0].dataType else: return self.field_for(label_or_scol).spark_type def spark_column_nullable_for(self, label_or_scol: Union[Tuple, spark.Column]) -> bool: """Return nullability for the given column label.""" if isinstance(label_or_scol, spark.Column): return self.spark_frame.select(label_or_scol).schema[0].nullable else: return self.field_for(label_or_scol).nullable def field_for(self, label: Tuple) -> InternalField: """Return InternalField for the given column label.""" column_labels_to_fields = dict(zip(self.column_labels, self.data_fields)) if label in column_labels_to_fields: return column_labels_to_fields[label] else: raise KeyError(name_like_string(label)) @property def spark_frame(self) -> spark.DataFrame: """Return the managed Spark DataFrame.""" return self._sdf @lazy_property def data_spark_column_names(self) -> List[str]: """Return the managed column field names.""" return [field.name for field in self.data_fields] @property def data_spark_columns(self) -> List[spark.Column]: """Return Spark Columns for the managed data columns.""" return self._data_spark_columns @property def index_spark_column_names(self) -> List[str]: """Return the managed index field names.""" return [field.name for field in self.index_fields] @property def index_spark_columns(self) -> List[spark.Column]: """Return Spark Columns for the managed index columns.""" return self._index_spark_columns @lazy_property def spark_column_names(self) -> List[str]: """Return all the field names including index field names.""" return self.spark_frame.select(self.spark_columns).columns @lazy_property def spark_columns(self) -> List[spark.Column]: """Return Spark Columns for the managed columns including index columns.""" index_spark_columns = self.index_spark_columns return index_spark_columns + [ spark_column for spark_column in self.data_spark_columns if all(not spark_column_equals(spark_column, scol) for scol in index_spark_columns) ] @property def index_names(self) -> List[Optional[Tuple]]: """Return the managed index names.""" return self._index_names @lazy_property def index_level(self) -> int: """Return the level of the index.""" return len(self._index_names) @property def column_labels(self) -> List[Tuple]: """Return the managed column index.""" return self._column_labels @lazy_property def column_labels_level(self) -> int: """Return the level of the column index.""" return len(self._column_label_names) @property def column_label_names(self) -> List[Optional[Tuple]]: """Return names of the index levels.""" return self._column_label_names @property def index_fields(self) -> List[InternalField]: """Return InternalFields for the managed index columns.""" return self._index_fields @property def data_fields(self) -> List[InternalField]: """Return InternalFields for the managed columns.""" return self._data_fields @lazy_property def to_internal_spark_frame(self) -> spark.DataFrame: """ Return as Spark DataFrame. This contains index columns as well and should be only used for internal purposes. """ index_spark_columns = self.index_spark_columns data_columns = [] for spark_column in self.data_spark_columns: if all(not spark_column_equals(spark_column, scol) for scol in index_spark_columns): data_columns.append(spark_column) return self.spark_frame.select(index_spark_columns + data_columns) @lazy_property def to_pandas_frame(self) -> pd.DataFrame: """Return as pandas DataFrame.""" sdf = self.to_internal_spark_frame pdf = sdf.toPandas() if len(pdf) == 0 and len(sdf.schema) > 0: pdf = pdf.astype( {field.name: spark_type_to_pandas_dtype(field.dataType) for field in sdf.schema} ) return InternalFrame.restore_index(pdf, **self.arguments_for_restore_index) @lazy_property def arguments_for_restore_index(self) -> Dict: """Create arguments for `restore_index`.""" column_names = [] fields = self.index_fields.copy() ext_fields = { col: field for col, field in zip(self.index_spark_column_names, self.index_fields) if isinstance(field.dtype, extension_dtypes) } for spark_column, column_name, field in zip( self.data_spark_columns, self.data_spark_column_names, self.data_fields ): for index_spark_column_name, index_spark_column in zip( self.index_spark_column_names, self.index_spark_columns ): if spark_column_equals(spark_column, index_spark_column): column_names.append(index_spark_column_name) break else: column_names.append(column_name) fields.append(field) if isinstance(field.dtype, extension_dtypes): ext_fields[column_name] = field return dict( index_columns=self.index_spark_column_names, index_names=self.index_names, data_columns=column_names, column_labels=self.column_labels, column_label_names=self.column_label_names, fields=fields, ext_fields=ext_fields, ) @staticmethod def restore_index( pdf: pd.DataFrame, *, index_columns: List[str], index_names: List[Tuple], data_columns: List[str], column_labels: List[Tuple], column_label_names: List[Tuple], fields: List[InternalField] = None, ext_fields: Dict[str, InternalField] = None, ) -> pd.DataFrame: """ Restore pandas DataFrame indices using the metadata. :param pdf: the pandas DataFrame to be processed. :param index_columns: the original column names for index columns. :param index_names: the index names after restored. :param data_columns: the original column names for data columns. :param column_labels: the column labels after restored. :param column_label_names: the column label names after restored. :param fields: the fields after restored. :param ext_fields: the map from the original column names to extension data fields. :return: the restored pandas DataFrame >>> from numpy import dtype >>> pdf = pd.DataFrame({"index": [10, 20, 30], "a": ['a', 'b', 'c'], "b": [0, 2, 1]}) >>> InternalFrame.restore_index( ... pdf, ... index_columns=["index"], ... index_names=[("idx",)], ... data_columns=["a", "b", "index"], ... column_labels=[("x",), ("y",), ("z",)], ... column_label_names=[("lv1",)], ... fields=[ ... InternalField( ... dtype=dtype('int64'), ... struct_field=StructField(name='index', dataType=LongType(), nullable=False), ... ), ... InternalField( ... dtype=dtype('object'), ... struct_field=StructField(name='a', dataType=StringType(), nullable=False), ... ), ... InternalField( ... dtype=CategoricalDtype(categories=["i", "j", "k"]), ... struct_field=StructField(name='b', dataType=LongType(), nullable=False), ... ), ... ], ... ext_fields=None, ... ) # doctest: +NORMALIZE_WHITESPACE lv1 x y z idx 10 a i 10 20 b k 20 30 c j 30 """ if ext_fields is not None and len(ext_fields) > 0: pdf = pdf.astype({col: field.dtype for col, field in ext_fields.items()}, copy=True) for col, field in zip(pdf.columns, fields): pdf[col] = DataTypeOps(field.dtype, field.spark_type).restore(pdf[col]) append = False for index_field in index_columns: drop = index_field not in data_columns pdf = pdf.set_index(index_field, drop=drop, append=append) append = True pdf = pdf[data_columns] pdf.index.names = [ name if name is None or len(name) > 1 else name[0] for name in index_names ] names = [name if name is None or len(name) > 1 else name[0] for name in column_label_names] if len(column_label_names) > 1: pdf.columns = pd.MultiIndex.from_tuples(column_labels, names=names) else: pdf.columns = pd.Index( [None if label is None else label[0] for label in column_labels], name=names[0], ) return pdf @lazy_property def resolved_copy(self) -> "InternalFrame": """Copy the immutable InternalFrame with the updates resolved.""" sdf = self.spark_frame.select(self.spark_columns + list(HIDDEN_COLUMNS)) return self.copy( spark_frame=sdf, index_spark_columns=[scol_for(sdf, col) for col in self.index_spark_column_names], data_spark_columns=[scol_for(sdf, col) for col in self.data_spark_column_names], ) def with_new_sdf( self, spark_frame: spark.DataFrame, *, index_fields: Optional[List[InternalField]] = None, data_columns: Optional[List[str]] = None, data_fields: Optional[List[InternalField]] = None, ) -> "InternalFrame": """Copy the immutable InternalFrame with the updates by the specified Spark DataFrame. :param spark_frame: the new Spark DataFrame :param index_fields: the new InternalFields for the index columns. If None, the original dtyeps are used. :param data_columns: the new column names. If None, the original one is used. :param data_fields: the new InternalFields for the data columns. If None, the original dtyeps are used. :return: the copied InternalFrame. """ if index_fields is None: index_fields = self.index_fields else: assert len(index_fields) == len(self.index_fields), ( len(index_fields), len(self.index_fields), ) if data_columns is None: data_columns = self.data_spark_column_names else: assert len(data_columns) == len(self.column_labels), ( len(data_columns), len(self.column_labels), ) if data_fields is None: data_fields = self.data_fields else: assert len(data_fields) == len(self.column_labels), ( len(data_fields), len(self.column_labels), ) sdf = spark_frame.drop(NATURAL_ORDER_COLUMN_NAME) return self.copy( spark_frame=sdf, index_spark_columns=[scol_for(sdf, col) for col in self.index_spark_column_names], index_fields=index_fields, data_spark_columns=[scol_for(sdf, col) for col in data_columns], data_fields=data_fields, ) def with_new_columns( self, scols_or_pssers: Sequence[Union[spark.Column, "Series"]], *, column_labels: Optional[List[Tuple]] = None, data_fields: Optional[List[InternalField]] = None, column_label_names: Union[Optional[List[Optional[Tuple]]], _NoValueType] = _NoValue, keep_order: bool = True, ) -> "InternalFrame": """ Copy the immutable InternalFrame with the updates by the specified Spark Columns or Series. :param scols_or_pssers: the new Spark Columns or Series. :param column_labels: the new column index. If None, the column_labels of the corresponding `scols_or_pssers` is used if it is Series; otherwise the original one is used. :param data_fields: the new InternalFields for the data columns. If None, the dtypes of the corresponding `scols_or_pssers` is used if it is Series; otherwise the dtypes will be inferred from the corresponding `scols_or_pssers`. :param column_label_names: the new names of the column index levels. :return: the copied InternalFrame. """ from pyspark.pandas.series import Series if column_labels is None: if all(isinstance(scol_or_psser, Series) for scol_or_psser in scols_or_pssers): column_labels = [cast(Series, psser)._column_label for psser in scols_or_pssers] else: assert len(scols_or_pssers) == len(self.column_labels), ( len(scols_or_pssers), len(self.column_labels), ) column_labels = [] for scol_or_psser, label in zip(scols_or_pssers, self.column_labels): if isinstance(scol_or_psser, Series): column_labels.append(scol_or_psser._column_label) else: column_labels.append(label) else: assert len(scols_or_pssers) == len(column_labels), ( len(scols_or_pssers), len(column_labels), ) data_spark_columns = [] for scol_or_psser in scols_or_pssers: if isinstance(scol_or_psser, Series): scol = scol_or_psser.spark.column else: scol = scol_or_psser data_spark_columns.append(scol) if data_fields is None: data_fields = [] for scol_or_psser in scols_or_pssers: if isinstance(scol_or_psser, Series): data_fields.append(scol_or_psser._internal.data_fields[0]) else: data_fields.append(None) else: assert len(scols_or_pssers) == len(data_fields), ( len(scols_or_pssers), len(data_fields), ) sdf = self.spark_frame if not keep_order: sdf = self.spark_frame.select(self.index_spark_columns + data_spark_columns) index_spark_columns = [scol_for(sdf, col) for col in self.index_spark_column_names] data_spark_columns = [ scol_for(sdf, col) for col in self.spark_frame.select(data_spark_columns).columns ] else: index_spark_columns = self.index_spark_columns if column_label_names is _NoValue: column_label_names = self._column_label_names return self.copy( spark_frame=sdf, index_spark_columns=index_spark_columns, column_labels=column_labels, data_spark_columns=data_spark_columns, data_fields=data_fields, column_label_names=column_label_names, ) def with_filter(self, pred: Union[spark.Column, "Series"]) -> "InternalFrame": """ Copy the immutable InternalFrame with the updates by the predicate. :param pred: the predicate to filter. :return: the copied InternalFrame. """ from pyspark.pandas.series import Series if isinstance(pred, Series): assert isinstance(pred.spark.data_type, BooleanType), pred.spark.data_type condition = pred.spark.column else: spark_type = self.spark_frame.select(pred).schema[0].dataType assert isinstance(spark_type, BooleanType), spark_type condition = pred return self.with_new_sdf(self.spark_frame.filter(condition).select(self.spark_columns)) def with_new_spark_column( self, column_label: Tuple, scol: spark.Column, *, field: Optional[InternalField] = None, keep_order: bool = True, ) -> "InternalFrame": """ Copy the immutable InternalFrame with the updates by the specified Spark Column. :param column_label: the column label to be updated. :param scol: the new Spark Column :param field: the new InternalField for the data column. If not specified, the InternalField will be inferred from the spark Column. :return: the copied InternalFrame. """ assert column_label in self.column_labels, column_label idx = self.column_labels.index(column_label) data_spark_columns = self.data_spark_columns.copy() data_spark_columns[idx] = scol data_fields = self.data_fields.copy() data_fields[idx] = field return self.with_new_columns( data_spark_columns, data_fields=data_fields, keep_order=keep_order ) def select_column(self, column_label: Tuple) -> "InternalFrame": """ Copy the immutable InternalFrame with the specified column. :param column_label: the column label to use. :return: the copied InternalFrame. """ assert column_label in self.column_labels, column_label return self.copy( column_labels=[column_label], data_spark_columns=[self.spark_column_for(column_label)], data_fields=[self.field_for(column_label)], column_label_names=None, ) def copy( self, *, spark_frame: Union[spark.DataFrame, _NoValueType] = _NoValue, index_spark_columns: Union[List[spark.Column], _NoValueType] = _NoValue, index_names: Union[Optional[List[Optional[Tuple]]], _NoValueType] = _NoValue, index_fields: Union[Optional[List[InternalField]], _NoValueType] = _NoValue, column_labels: Union[Optional[List[Tuple]], _NoValueType] = _NoValue, data_spark_columns: Union[Optional[List[spark.Column]], _NoValueType] = _NoValue, data_fields: Union[Optional[List[InternalField]], _NoValueType] = _NoValue, column_label_names: Union[Optional[List[Optional[Tuple]]], _NoValueType] = _NoValue, ) -> "InternalFrame": """ Copy the immutable InternalFrame. :param spark_frame: the new Spark DataFrame. If not specified, the original one is used. :param index_spark_columns: the list of Spark Column. If not specified, the original ones are used. :param index_names: the index names. If not specified, the original ones are used. :param index_fields: the new InternalFields for the index columns. If not specified, the original metadata are used. :param column_labels: the new column labels. If not specified, the original ones are used. :param data_spark_columns: the new Spark Columns. If not specified, the original ones are used. :param data_fields: the new InternalFields for the data columns. If not specified, the original metadata are used. :param column_label_names: the new names of the column index levels. If not specified, the original ones are used. :return: the copied immutable InternalFrame. """ if spark_frame is _NoValue: spark_frame = self.spark_frame if index_spark_columns is _NoValue: index_spark_columns = self.index_spark_columns if index_names is _NoValue: index_names = self.index_names if index_fields is _NoValue: index_fields = self.index_fields if column_labels is _NoValue: column_labels = self.column_labels if data_spark_columns is _NoValue: data_spark_columns = self.data_spark_columns if data_fields is _NoValue: data_fields = self.data_fields if column_label_names is _NoValue: column_label_names = self.column_label_names return InternalFrame( spark_frame=cast(spark.DataFrame, spark_frame), index_spark_columns=cast(List[spark.Column], index_spark_columns), index_names=cast(Optional[List[Optional[Tuple]]], index_names), index_fields=cast(Optional[List[InternalField]], index_fields), column_labels=cast(Optional[List[Tuple]], column_labels), data_spark_columns=cast(Optional[List[spark.Column]], data_spark_columns), data_fields=cast(Optional[List[InternalField]], data_fields), column_label_names=cast(Optional[List[Optional[Tuple]]], column_label_names), ) @staticmethod def from_pandas(pdf: pd.DataFrame) -> "InternalFrame": """Create an immutable DataFrame from pandas DataFrame. :param pdf: :class:`pd.DataFrame` :return: the created immutable DataFrame """ index_names = [ name if name is None or isinstance(name, tuple) else (name,) for name in pdf.index.names ] columns = pdf.columns if isinstance(columns, pd.MultiIndex): column_labels = columns.tolist() else: column_labels = [(col,) for col in columns] column_label_names = [ name if name is None or isinstance(name, tuple) else (name,) for name in columns.names ] ( pdf, index_columns, index_fields, data_columns, data_fields, ) = InternalFrame.prepare_pandas_frame(pdf) schema = StructType([field.struct_field for field in index_fields + data_fields]) sdf = default_session().createDataFrame(pdf, schema=schema) return InternalFrame( spark_frame=sdf, index_spark_columns=[scol_for(sdf, col) for col in index_columns], index_names=index_names, index_fields=index_fields, column_labels=column_labels, data_spark_columns=[scol_for(sdf, col) for col in data_columns], data_fields=data_fields, column_label_names=column_label_names, ) @staticmethod def prepare_pandas_frame( pdf: pd.DataFrame, *, retain_index: bool = True ) -> Tuple[pd.DataFrame, List[str], List[InternalField], List[str], List[InternalField]]: """ Prepare pandas DataFrame for creating Spark DataFrame. :param pdf: the pandas DataFrame to be prepared. :param retain_index: whether the indices should be retained. :return: the tuple of - the prepared pandas dataFrame - index column names for Spark DataFrame - the InternalFields for the index columns of the given pandas DataFrame - data column names for Spark DataFrame - the InternalFields for the data columns of the given pandas DataFrame >>> pdf = pd.DataFrame( ... {("x", "a"): ['a', 'b', 'c'], ... ("y", "b"): pd.Categorical(["i", "k", "j"], categories=["i", "j", "k"])}, ... index=[10, 20, 30]) >>> prepared, index_columns, index_fields, data_columns, data_fields = ( ... InternalFrame.prepare_pandas_frame(pdf) ... ) >>> prepared __index_level_0__ (x, a) (y, b) 0 10 a 0 1 20 b 2 2 30 c 1 >>> index_columns ['__index_level_0__'] >>> index_fields [InternalField(dtype=int64,struct_field=StructField(__index_level_0__,LongType,false))] >>> data_columns ['(x, a)', '(y, b)'] >>> data_fields # doctest: +NORMALIZE_WHITESPACE [InternalField(dtype=object,struct_field=StructField((x, a),StringType,false)), InternalField(dtype=category,struct_field=StructField((y, b),ByteType,false))] """ pdf = pdf.copy() data_columns = [name_like_string(col) for col in pdf.columns] pdf.columns = data_columns if retain_index: index_nlevels = pdf.index.nlevels index_columns = [SPARK_INDEX_NAME_FORMAT(i) for i in range(index_nlevels)] pdf.index.names = index_columns reset_index = pdf.reset_index() else: index_nlevels = 0 index_columns = [] reset_index = pdf index_dtypes = list(reset_index.dtypes)[:index_nlevels] data_dtypes = list(reset_index.dtypes)[index_nlevels:] for col, dtype in zip(reset_index.columns, reset_index.dtypes): spark_type = infer_pd_series_spark_type(reset_index[col], dtype) reset_index[col] = DataTypeOps(dtype, spark_type).prepare(reset_index[col]) fields = [ InternalField( dtype=dtype, struct_field=StructField( name=name, dataType=infer_pd_series_spark_type(col, dtype), nullable=bool(col.isnull().any()), ), ) for (name, col), dtype in zip(reset_index.iteritems(), index_dtypes + data_dtypes) ] return ( reset_index, index_columns, fields[:index_nlevels], data_columns, fields[index_nlevels:], ) def _test() -> None: import os import doctest import sys from pyspark.sql import SparkSession import pyspark.pandas.internal os.chdir(os.environ["SPARK_HOME"]) globs = pyspark.pandas.internal.__dict__.copy() globs["ps"] = pyspark.pandas spark = ( SparkSession.builder.master("local[4]") .appName("pyspark.pandas.internal tests") .getOrCreate() ) (failure_count, test_count) = doctest.testmod( pyspark.pandas.internal, globs=globs, optionflags=doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE, ) spark.stop() if failure_count: sys.exit(-1) if __name__ == "__main__": _test()
apache-2.0
phobson/statsmodels
statsmodels/tools/tests/test_tools.py
1
20793
""" Test functions for models.tools """ from statsmodels.compat.python import lrange, range import numpy as np from numpy.random import standard_normal from numpy.testing import (assert_equal, assert_array_equal, assert_almost_equal, assert_string_equal, TestCase) from nose.tools import (assert_true, assert_false, assert_raises) import pandas as pd from pandas.util.testing import assert_frame_equal, assert_series_equal from statsmodels.datasets import longley from statsmodels.tools import tools from statsmodels.tools.tools import pinv_extended from statsmodels.compat.numpy import np_matrix_rank class TestTools(TestCase): def test_add_constant_list(self): x = lrange(1,5) x = tools.add_constant(x) y = np.asarray([[1,1,1,1],[1,2,3,4.]]).T assert_equal(x, y) def test_add_constant_1d(self): x = np.arange(1,5) x = tools.add_constant(x) y = np.asarray([[1,1,1,1],[1,2,3,4.]]).T assert_equal(x, y) def test_add_constant_has_constant1d(self): x = np.ones(5) x = tools.add_constant(x, has_constant='skip') assert_equal(x, np.ones((5,1))) assert_raises(ValueError, tools.add_constant, x, has_constant='raise') assert_equal(tools.add_constant(x, has_constant='add'), np.ones((5, 2))) def test_add_constant_has_constant2d(self): x = np.asarray([[1,1,1,1],[1,2,3,4.]]).T y = tools.add_constant(x, has_constant='skip') assert_equal(x, y) assert_raises(ValueError, tools.add_constant, x, has_constant='raise') assert_equal(tools.add_constant(x, has_constant='add'), np.column_stack((np.ones(4), x))) def test_add_constant_recarray(self): dt = np.dtype([('', int), ('', '<S4'), ('', np.float32), ('', np.float64)]) x = np.array([(1, 'abcd', 1.0, 2.0), (7, 'abcd', 2.0, 4.0), (21, 'abcd', 2.0, 8.0)], dt) x = x.view(np.recarray) y = tools.add_constant(x) assert_equal(y['const'],np.array([1.0,1.0,1.0])) for f in x.dtype.fields: assert_true(y[f].dtype == x[f].dtype) def test_add_constant_series(self): s = pd.Series([1.0,2.0,3.0]) output = tools.add_constant(s) expected = pd.Series([1.0,1.0,1.0],name='const') assert_series_equal(expected, output['const']) def test_add_constant_dataframe(self): df = pd.DataFrame([[1.0, 'a', 4], [2.0, 'bc', 9], [3.0, 'def', 16]]) output = tools.add_constant(df) expected = pd.Series([1.0, 1.0, 1.0], name='const') assert_series_equal(expected, output['const']) dfc = df.copy() dfc.insert(0, 'const', np.ones(3)) assert_frame_equal(dfc, output) def test_add_constant_zeros(self): a = np.zeros(100) output = tools.add_constant(a) assert_equal(output[:,0],np.ones(100)) s = pd.Series([0.0,0.0,0.0]) output = tools.add_constant(s) expected = pd.Series([1.0, 1.0, 1.0], name='const') assert_series_equal(expected, output['const']) df = pd.DataFrame([[0.0, 'a', 4], [0.0, 'bc', 9], [0.0, 'def', 16]]) output = tools.add_constant(df) dfc = df.copy() dfc.insert(0, 'const', np.ones(3)) assert_frame_equal(dfc, output) df = pd.DataFrame([[1.0, 'a', 0], [0.0, 'bc', 0], [0.0, 'def', 0]]) output = tools.add_constant(df) dfc = df.copy() dfc.insert(0, 'const', np.ones(3)) assert_frame_equal(dfc, output) def test_recipr(self): X = np.array([[2,1],[-1,0]]) Y = tools.recipr(X) assert_almost_equal(Y, np.array([[0.5,1],[0,0]])) def test_recipr0(self): X = np.array([[2,1],[-4,0]]) Y = tools.recipr0(X) assert_almost_equal(Y, np.array([[0.5,1],[-0.25,0]])) def test_rank(self): import warnings with warnings.catch_warnings(): warnings.simplefilter("ignore") X = standard_normal((40,10)) self.assertEquals(tools.rank(X), np_matrix_rank(X)) X[:,0] = X[:,1] + X[:,2] self.assertEquals(tools.rank(X), np_matrix_rank(X)) def test_extendedpinv(self): X = standard_normal((40, 10)) np_inv = np.linalg.pinv(X) np_sing_vals = np.linalg.svd(X, 0, 0) sm_inv, sing_vals = pinv_extended(X) assert_almost_equal(np_inv, sm_inv) assert_almost_equal(np_sing_vals, sing_vals) def test_extendedpinv_singular(self): X = standard_normal((40, 10)) X[:, 5] = X[:, 1] + X[:, 3] np_inv = np.linalg.pinv(X) np_sing_vals = np.linalg.svd(X, 0, 0) sm_inv, sing_vals = pinv_extended(X) assert_almost_equal(np_inv, sm_inv) assert_almost_equal(np_sing_vals, sing_vals) def test_fullrank(self): import warnings with warnings.catch_warnings(): warnings.simplefilter("ignore") X = standard_normal((40,10)) X[:,0] = X[:,1] + X[:,2] Y = tools.fullrank(X) self.assertEquals(Y.shape, (40,9)) self.assertEquals(tools.rank(Y), 9) X[:,5] = X[:,3] + X[:,4] Y = tools.fullrank(X) self.assertEquals(Y.shape, (40,8)) warnings.simplefilter("ignore") self.assertEquals(tools.rank(Y), 8) def test_estimable(): rng = np.random.RandomState(20120713) N, P = (40, 10) X = rng.normal(size=(N, P)) C = rng.normal(size=(1, P)) isestimable = tools.isestimable assert_true(isestimable(C, X)) assert_true(isestimable(np.eye(P), X)) for row in np.eye(P): assert_true(isestimable(row, X)) X = np.ones((40, 2)) assert_true(isestimable([1, 1], X)) assert_false(isestimable([1, 0], X)) assert_false(isestimable([0, 1], X)) assert_false(isestimable(np.eye(2), X)) halfX = rng.normal(size=(N, 5)) X = np.hstack([halfX, halfX]) assert_false(isestimable(np.hstack([np.eye(5), np.zeros((5, 5))]), X)) assert_false(isestimable(np.hstack([np.zeros((5, 5)), np.eye(5)]), X)) assert_true(isestimable(np.hstack([np.eye(5), np.eye(5)]), X)) # Test array-like for design XL = X.tolist() assert_true(isestimable(np.hstack([np.eye(5), np.eye(5)]), XL)) # Test ValueError for incorrect number of columns X = rng.normal(size=(N, 5)) for n in range(1, 4): assert_raises(ValueError, isestimable, np.ones((n,)), X) assert_raises(ValueError, isestimable, np.eye(4), X) class TestCategoricalNumerical(object): #TODO: use assert_raises to check that bad inputs are taken care of def __init__(self): #import string stringabc = 'abcdefghijklmnopqrstuvwxy' self.des = np.random.randn(25,2) self.instr = np.floor(np.arange(10,60, step=2)/10) x=np.zeros((25,5)) x[:5,0]=1 x[5:10,1]=1 x[10:15,2]=1 x[15:20,3]=1 x[20:25,4]=1 self.dummy = x structdes = np.zeros((25,1),dtype=[('var1', 'f4'),('var2', 'f4'), ('instrument','f4'),('str_instr','a10')]) structdes['var1'] = self.des[:,0][:,None] structdes['var2'] = self.des[:,1][:,None] structdes['instrument'] = self.instr[:,None] string_var = [stringabc[0:5], stringabc[5:10], stringabc[10:15], stringabc[15:20], stringabc[20:25]] string_var *= 5 self.string_var = np.array(sorted(string_var)) structdes['str_instr'] = self.string_var[:,None] self.structdes = structdes self.recdes = structdes.view(np.recarray) def test_array2d(self): des = np.column_stack((self.des, self.instr, self.des)) des = tools.categorical(des, col=2) assert_array_equal(des[:,-5:], self.dummy) assert_equal(des.shape[1],10) def test_array1d(self): des = tools.categorical(self.instr) assert_array_equal(des[:,-5:], self.dummy) assert_equal(des.shape[1],6) def test_array2d_drop(self): des = np.column_stack((self.des, self.instr, self.des)) des = tools.categorical(des, col=2, drop=True) assert_array_equal(des[:,-5:], self.dummy) assert_equal(des.shape[1],9) def test_array1d_drop(self): des = tools.categorical(self.instr, drop=True) assert_array_equal(des, self.dummy) assert_equal(des.shape[1],5) def test_recarray2d(self): des = tools.categorical(self.recdes, col='instrument') # better way to do this? test_des = np.column_stack(([des[_] for _ in des.dtype.names[-5:]])) assert_array_equal(test_des, self.dummy) assert_equal(len(des.dtype.names), 9) def test_recarray2dint(self): des = tools.categorical(self.recdes, col=2) test_des = np.column_stack(([des[_] for _ in des.dtype.names[-5:]])) assert_array_equal(test_des, self.dummy) assert_equal(len(des.dtype.names), 9) def test_recarray1d(self): instr = self.structdes['instrument'].view(np.recarray) dum = tools.categorical(instr) test_dum = np.column_stack(([dum[_] for _ in dum.dtype.names[-5:]])) assert_array_equal(test_dum, self.dummy) assert_equal(len(dum.dtype.names), 6) def test_recarray1d_drop(self): instr = self.structdes['instrument'].view(np.recarray) dum = tools.categorical(instr, drop=True) test_dum = np.column_stack(([dum[_] for _ in dum.dtype.names])) assert_array_equal(test_dum, self.dummy) assert_equal(len(dum.dtype.names), 5) def test_recarray2d_drop(self): des = tools.categorical(self.recdes, col='instrument', drop=True) test_des = np.column_stack(([des[_] for _ in des.dtype.names[-5:]])) assert_array_equal(test_des, self.dummy) assert_equal(len(des.dtype.names), 8) def test_structarray2d(self): des = tools.categorical(self.structdes, col='instrument') test_des = np.column_stack(([des[_] for _ in des.dtype.names[-5:]])) assert_array_equal(test_des, self.dummy) assert_equal(len(des.dtype.names), 9) def test_structarray2dint(self): des = tools.categorical(self.structdes, col=2) test_des = np.column_stack(([des[_] for _ in des.dtype.names[-5:]])) assert_array_equal(test_des, self.dummy) assert_equal(len(des.dtype.names), 9) def test_structarray1d(self): instr = self.structdes['instrument'].view(dtype=[('var1', 'f4')]) dum = tools.categorical(instr) test_dum = np.column_stack(([dum[_] for _ in dum.dtype.names[-5:]])) assert_array_equal(test_dum, self.dummy) assert_equal(len(dum.dtype.names), 6) def test_structarray2d_drop(self): des = tools.categorical(self.structdes, col='instrument', drop=True) test_des = np.column_stack(([des[_] for _ in des.dtype.names[-5:]])) assert_array_equal(test_des, self.dummy) assert_equal(len(des.dtype.names), 8) def test_structarray1d_drop(self): instr = self.structdes['instrument'].view(dtype=[('var1', 'f4')]) dum = tools.categorical(instr, drop=True) test_dum = np.column_stack(([dum[_] for _ in dum.dtype.names])) assert_array_equal(test_dum, self.dummy) assert_equal(len(dum.dtype.names), 5) # def test_arraylike2d(self): # des = tools.categorical(self.structdes.tolist(), col=2) # test_des = des[:,-5:] # assert_array_equal(test_des, self.dummy) # assert_equal(des.shape[1], 9) # def test_arraylike1d(self): # instr = self.structdes['instrument'].tolist() # dum = tools.categorical(instr) # test_dum = dum[:,-5:] # assert_array_equal(test_dum, self.dummy) # assert_equal(dum.shape[1], 6) # def test_arraylike2d_drop(self): # des = tools.categorical(self.structdes.tolist(), col=2, drop=True) # test_des = des[:,-5:] # assert_array_equal(test__des, self.dummy) # assert_equal(des.shape[1], 8) # def test_arraylike1d_drop(self): # instr = self.structdes['instrument'].tolist() # dum = tools.categorical(instr, drop=True) # assert_array_equal(dum, self.dummy) # assert_equal(dum.shape[1], 5) class TestCategoricalString(TestCategoricalNumerical): # comment out until we have type coercion # def test_array2d(self): # des = np.column_stack((self.des, self.instr, self.des)) # des = tools.categorical(des, col=2) # assert_array_equal(des[:,-5:], self.dummy) # assert_equal(des.shape[1],10) # def test_array1d(self): # des = tools.categorical(self.instr) # assert_array_equal(des[:,-5:], self.dummy) # assert_equal(des.shape[1],6) # def test_array2d_drop(self): # des = np.column_stack((self.des, self.instr, self.des)) # des = tools.categorical(des, col=2, drop=True) # assert_array_equal(des[:,-5:], self.dummy) # assert_equal(des.shape[1],9) def test_array1d_drop(self): des = tools.categorical(self.string_var, drop=True) assert_array_equal(des, self.dummy) assert_equal(des.shape[1],5) def test_recarray2d(self): des = tools.categorical(self.recdes, col='str_instr') # better way to do this? test_des = np.column_stack(([des[_] for _ in des.dtype.names[-5:]])) assert_array_equal(test_des, self.dummy) assert_equal(len(des.dtype.names), 9) def test_recarray2dint(self): des = tools.categorical(self.recdes, col=3) test_des = np.column_stack(([des[_] for _ in des.dtype.names[-5:]])) assert_array_equal(test_des, self.dummy) assert_equal(len(des.dtype.names), 9) def test_recarray1d(self): instr = self.structdes['str_instr'].view(np.recarray) dum = tools.categorical(instr) test_dum = np.column_stack(([dum[_] for _ in dum.dtype.names[-5:]])) assert_array_equal(test_dum, self.dummy) assert_equal(len(dum.dtype.names), 6) def test_recarray1d_drop(self): instr = self.structdes['str_instr'].view(np.recarray) dum = tools.categorical(instr, drop=True) test_dum = np.column_stack(([dum[_] for _ in dum.dtype.names])) assert_array_equal(test_dum, self.dummy) assert_equal(len(dum.dtype.names), 5) def test_recarray2d_drop(self): des = tools.categorical(self.recdes, col='str_instr', drop=True) test_des = np.column_stack(([des[_] for _ in des.dtype.names[-5:]])) assert_array_equal(test_des, self.dummy) assert_equal(len(des.dtype.names), 8) def test_structarray2d(self): des = tools.categorical(self.structdes, col='str_instr') test_des = np.column_stack(([des[_] for _ in des.dtype.names[-5:]])) assert_array_equal(test_des, self.dummy) assert_equal(len(des.dtype.names), 9) def test_structarray2dint(self): des = tools.categorical(self.structdes, col=3) test_des = np.column_stack(([des[_] for _ in des.dtype.names[-5:]])) assert_array_equal(test_des, self.dummy) assert_equal(len(des.dtype.names), 9) def test_structarray1d(self): instr = self.structdes['str_instr'].view(dtype=[('var1', 'a10')]) dum = tools.categorical(instr) test_dum = np.column_stack(([dum[_] for _ in dum.dtype.names[-5:]])) assert_array_equal(test_dum, self.dummy) assert_equal(len(dum.dtype.names), 6) def test_structarray2d_drop(self): des = tools.categorical(self.structdes, col='str_instr', drop=True) test_des = np.column_stack(([des[_] for _ in des.dtype.names[-5:]])) assert_array_equal(test_des, self.dummy) assert_equal(len(des.dtype.names), 8) def test_structarray1d_drop(self): instr = self.structdes['str_instr'].view(dtype=[('var1', 'a10')]) dum = tools.categorical(instr, drop=True) test_dum = np.column_stack(([dum[_] for _ in dum.dtype.names])) assert_array_equal(test_dum, self.dummy) assert_equal(len(dum.dtype.names), 5) def test_arraylike2d(self): pass def test_arraylike1d(self): pass def test_arraylike2d_drop(self): pass def test_arraylike1d_drop(self): pass def test_rec_issue302(): arr = np.rec.fromrecords([[10], [11]], names='group') actual = tools.categorical(arr) expected = np.rec.array([(10, 1.0, 0.0), (11, 0.0, 1.0)], dtype=[('group', int), ('group_10', float), ('group_11', float)]) assert_array_equal(actual, expected) def test_issue302(): arr = np.rec.fromrecords([[10, 12], [11, 13]], names=['group', 'whatever']) actual = tools.categorical(arr, col=['group']) expected = np.rec.array([(10, 12, 1.0, 0.0), (11, 13, 0.0, 1.0)], dtype=[('group', int), ('whatever', int), ('group_10', float), ('group_11', float)]) assert_array_equal(actual, expected) def test_pandas_const_series(): dta = longley.load_pandas() series = dta.exog['GNP'] series = tools.add_constant(series, prepend=False) assert_string_equal('const', series.columns[1]) assert_equal(series.var(0)[1], 0) def test_pandas_const_series_prepend(): dta = longley.load_pandas() series = dta.exog['GNP'] series = tools.add_constant(series, prepend=True) assert_string_equal('const', series.columns[0]) assert_equal(series.var(0)[0], 0) def test_pandas_const_df(): dta = longley.load_pandas().exog dta = tools.add_constant(dta, prepend=False) assert_string_equal('const', dta.columns[-1]) assert_equal(dta.var(0)[-1], 0) def test_pandas_const_df_prepend(): dta = longley.load_pandas().exog # regression test for #1025 dta['UNEMP'] /= dta['UNEMP'].std() dta = tools.add_constant(dta, prepend=True) assert_string_equal('const', dta.columns[0]) assert_equal(dta.var(0)[0], 0) def test_chain_dot(): A = np.arange(1,13).reshape(3,4) B = np.arange(3,15).reshape(4,3) C = np.arange(5,8).reshape(3,1) assert_equal(tools.chain_dot(A,B,C), np.array([[1820],[4300],[6780]])) class TestNanDot(object): @classmethod def setupClass(cls): nan = np.nan cls.mx_1 = np.array([[nan, 1.], [2., 3.]]) cls.mx_2 = np.array([[nan, nan], [2., 3.]]) cls.mx_3 = np.array([[0., 0.], [0., 0.]]) cls.mx_4 = np.array([[1., 0.], [1., 0.]]) cls.mx_5 = np.array([[0., 1.], [0., 1.]]) cls.mx_6 = np.array([[1., 2.], [3., 4.]]) def test_11(self): test_res = tools.nan_dot(self.mx_1, self.mx_1) expected_res = np.array([[ np.nan, np.nan], [ np.nan, 11.]]) assert_array_equal(test_res, expected_res) def test_12(self): nan = np.nan test_res = tools.nan_dot(self.mx_1, self.mx_2) expected_res = np.array([[ nan, nan], [ nan, nan]]) assert_array_equal(test_res, expected_res) def test_13(self): nan = np.nan test_res = tools.nan_dot(self.mx_1, self.mx_3) expected_res = np.array([[ 0., 0.], [ 0., 0.]]) assert_array_equal(test_res, expected_res) def test_14(self): nan = np.nan test_res = tools.nan_dot(self.mx_1, self.mx_4) expected_res = np.array([[ nan, 0.], [ 5., 0.]]) assert_array_equal(test_res, expected_res) def test_41(self): nan = np.nan test_res = tools.nan_dot(self.mx_4, self.mx_1) expected_res = np.array([[ nan, 1.], [ nan, 1.]]) assert_array_equal(test_res, expected_res) def test_23(self): nan = np.nan test_res = tools.nan_dot(self.mx_2, self.mx_3) expected_res = np.array([[ 0., 0.], [ 0., 0.]]) assert_array_equal(test_res, expected_res) def test_32(self): nan = np.nan test_res = tools.nan_dot(self.mx_3, self.mx_2) expected_res = np.array([[ 0., 0.], [ 0., 0.]]) assert_array_equal(test_res, expected_res) def test_24(self): nan = np.nan test_res = tools.nan_dot(self.mx_2, self.mx_4) expected_res = np.array([[ nan, 0.], [ 5., 0.]]) assert_array_equal(test_res, expected_res) def test_25(self): nan = np.nan test_res = tools.nan_dot(self.mx_2, self.mx_5) expected_res = np.array([[ 0., nan], [ 0., 5.]]) assert_array_equal(test_res, expected_res) def test_66(self): nan = np.nan test_res = tools.nan_dot(self.mx_6, self.mx_6) expected_res = np.array([[ 7., 10.], [ 15., 22.]]) assert_array_equal(test_res, expected_res)
bsd-3-clause
nhuntwalker/astroML
book_figures/chapter1/fig_moving_objects.py
3
1911
""" SDSS Moving Object Data ----------------------- Figure 1.8. The orbital semimajor axis vs. the orbital inclination angle diagram for the first 10,000 catalog entries from the SDSS Moving Object Catalog (after applying several quality cuts). The gaps at approximately 2.5, 2.8, and 3.3 AU are called the Kirkwood gaps and are due to orbital resonances with Jupiter. The several distinct clumps are called asteroid families and represent remnants from collisions of larger asteroids. """ # Author: Jake VanderPlas # License: BSD # The figure produced by this code is published in the textbook # "Statistics, Data Mining, and Machine Learning in Astronomy" (2013) # For more information, see http://astroML.github.com # To report a bug or issue, use the following forum: # https://groups.google.com/forum/#!forum/astroml-general from matplotlib import pyplot as plt from astroML.datasets import fetch_moving_objects #---------------------------------------------------------------------- # This function adjusts matplotlib settings for a uniform feel in the textbook. # Note that with usetex=True, fonts are rendered with LaTeX. This may # result in an error if LaTeX is not installed on your system. In that case, # you can set usetex to False. from astroML.plotting import setup_text_plots setup_text_plots(fontsize=8, usetex=True) #------------------------------------------------------------ # Fetch the moving object data data = fetch_moving_objects(Parker2008_cuts=True) # Use only the first 10000 points data = data[:10000] a = data['aprime'] sini = data['sin_iprime'] #------------------------------------------------------------ # Plot the results fig, ax = plt.subplots(figsize=(5, 3.75)) ax.plot(a, sini, '.', markersize=2, color='black') ax.set_xlim(2.0, 3.6) ax.set_ylim(-0.01, 0.31) ax.set_xlabel('Semimajor Axis (AU)') ax.set_ylabel('Sine of Inclination Angle') plt.show()
bsd-2-clause
waidyanatha/pingsam
visualize.py
1
8668
import numpy as np import datetime as dtm from dateutil import rrule import pandas as pd import csv import matplotlib.pylab as plt import sys, os #lets first create the csv file # #change this to actual csv file name pingfile="weeklylogs.csv" #paramters @plotinterval = 10 minutes plotinterval = 10 #csv file columns col_seq=0 col_pingtime=1 col_domain=2 col_state=3 # ########## FUNCTION TO SYNTHESEIZE MISSING DATA POINTS ########## # def synth_data(synthdf, interval): #create a temporary dataframe to hold the syntheseized data tmpdf = pd.DataFrame(columns=['seqnum', 'pingdatetime', 'domain', 'statenow']) #first check we have a none empty dataframe if not synthdf.empty: #pick the originating TS data point synthdf.sort_values(by='pingdatetime') #check if first timestamp starts at 00:00:00; if not add a dumy record startseqnum = synthdf.index[0] startpingdt = synthdf.iloc[0]['pingdatetime'] startdomain = synthdf.iloc[0]['domain'] startstate = synthdf.iloc[0]['statenow'] #loop through each TS data point to synthetically add new TS points #to fill the gap between two consecutive data points for i, row in synthdf.iterrows(): #initiate the synthesiezed data point to the origin nextdatapoint = 0 pingdt_plus_interval = startpingdt #stepwise loop to add syntheseized points from relative origin to the next TS data point while row['pingdatetime'] > pingdt_plus_interval + dtm.timedelta(minutes = interval) : nextdatapoint += 1 pingdt_plus_interval = startpingdt + dtm.timedelta(minutes = nextdatapoint*interval) tmpdf.loc[len(tmpdf.index)] = [startseqnum,pingdt_plus_interval,startdomain,startstate] startseqnum = i startpingdt = row['pingdatetime'] startstate = row['statenow'] #after completing through all the TS datapoints check if a none empty dataframe was created if not tmpdf.empty: tmpdf = pd.concat([tmpdf,synthdf]) tmpdf = tmpdf.set_index('seqnum') #whether null or not return a dataframe with syntheseized TS data tmpdf.dropna(thresh=2) return tmpdf # ########## PLOT HISTOGRAM TO FIGURE ########## # def plot_hist_to_fig(histdf, dname): #get date range of the plot to use in suptitile begdt = histdf['pingdatetime'].min().date() findt = histdf['pingdatetime'].max().date() #create a new x-axis index using dataframe index; starting from 1 instead of 0 histdf['pingdate'] = histdf['pingdatetime'].apply(lambda x: x.date()) downdf = pd.DataFrame(columns=['xlabel','pingdate', 'downcount']) datelist = list(histdf.pingdate.unique()) for uniquedate in datelist: xlabel = str('{:02d}'.format(uniquedate.month))+'-'+str('{:02d}'.format(uniquedate.day)) downcount = len(histdf[(histdf.statenow == '0') & (histdf.pingdate == uniquedate)]) totalcount = len(histdf[(histdf.pingdate == uniquedate)]) downdf.loc[len(downdf.index)] = [xlabel, uniquedate,100*downcount//totalcount] downdf = downdf.as_matrix() #x-axis values are in the newly generated xvalues column xl = np.array(downdf[:,0]) x = np.array(downdf[:,1]) #y-axis values (1 or 0) are in the dateframe statenow column y = np.array(downdf[:,2]) histfig, ax = plt.subplots() ax.bar(x,y,color='red',width=0.5, align="center") #to give enough spacing for the suptitle; otherwise overlaps with title histfig.subplots_adjust(top=0.87) # plt.figure(figsize=(8,6), dpi=150) #beautify the plot and name the labels, titles ax.set_title('Percentage of time Server Failed each Day', fontsize=14, fontweight='bold', color='gray') histfig.suptitle(dname+'\n'+str(begdt)+' --- '+str(findt), fontsize=10, color='blue') ax.set_xlabel('Month-Day', fontsize=12, color='gray') ax.set_ylabel('Faile Rate (%)', fontsize=12, color='gray') plt.yticks(fontsize=10, color='gray', rotation='horizontal') plt.xticks(x, xl, fontsize=10, color='gray', rotation='vertical') ax.grid(True) return histfig # ########## PLOT DOWN TIMES FREQUENCY TO FIGURE ########## # def plot_freq_to_fig(plotdf, dname): #get date range of the plot to use in suptitile begdt = plotdf['pingdatetime'].min().date() findt = plotdf['pingdatetime'].max().date() failrate = 100-(sum(100*plotdf['statenow'].astype(int))/len(plotdf)) failrate = failrate.astype(float) #create a new x-axis index using dataframe index; starting from 1 instead of 0 plotdf['xvalues'] = range(1,len(plotdf)+1) plotdf = plotdf.as_matrix() #x-axis values are in the newly generated xvalues column x = np.array(plotdf[:,3].astype(int)) #y-axis values (1 or 0) are in the dateframe statenow column y = np.array(plotdf[:,2].astype(int)) #setup to catputure the plot into a figure plotfig = plt.figure(num=None, figsize=(8, 6), dpi=150, facecolor='y', edgecolor='k') ax = plotfig.add_subplot(311) ax.fill_between(x, 0, y, color='green') ax.plot(x,y,color='green',lw=2) #to give enough spacing for the suptitle; otherwise overlaps with title plotfig.subplots_adjust(top=0.87) #beautify the plot and name the labels, titles ax.set_title('Frequency of Server Access Failure ('+str(failrate)+'%)', fontsize=14, fontweight='bold', color='gray') plotfig.suptitle(dname+'\n'+str(begdt)+' --- '+str(findt), fontsize=10, color='blue') ax.set_xlabel('Attempted Machine Accesss Times', fontsize=12, color='gray') ax.set_ylabel('Machine State', fontsize=12, color='gray') plt.yticks(y, ['UP','DOWN'], fontsize=10, color='gray', rotation='vertical') plt.xticks(fontsize=10, color='gray', rotation='horizontal') plt.ylim(0,1.1) plt.xlim(0,x.max()+10) ax.grid(True) return plotfig # ############# MAIN ################################ # print("Complile data from file the log files") #os.system('./analytics.sh') print("Reading data from file "+pingfile) with open(pingfile, 'rb') as f: data = [i.split(",") for i in f.read().split()] df = pd.DataFrame(data, columns=['seqnum', 'pingdatetime', 'domain', 'statenow']) for index, row in df.iterrows(): row[col_pingtime] = dtm.datetime.strptime(row[col_pingtime], '%Y-%m-%d:%H:%M:%S') #to avoid duplicate data and to reflect ping time to be on the minute row[col_pingtime] = row[col_pingtime].replace(second = 0) #format pingdatetime as proper datetime, set it as the indext and then order them df['pingdatetime'] = pd.to_datetime(df['pingdatetime']) df.sort_values(by='pingdatetime') df = df.set_index('seqnum') #begin processing for each unique domain print(str(len(df.index))+" data rows added to the dataframe, ready for processing ...") print ('-----------------------------------------------------') for thedomain in df.domain.unique(): #insert syntheseised data points dompingdf = df[df['domain']==thedomain] print("Begin data synthesis for "+thedomain+" with data rows = "+str(len(dompingdf.index))) amenddf = synth_data(dompingdf,plotinterval) if not amenddf.empty: #output the syntheseized dataframe to output file print(str(len(amenddf.index))+" data rows of syntheseised added to "+thedomain ) amenddf['pingdatetime'] = pd.to_datetime(amenddf.pingdatetime) amenddf = amenddf.sort(['pingdatetime']) amenddf.index = range(0,len(amenddf)) print('writing data to file: ./data/syndata_'+thedomain+'.csv') amenddf.to_csv('./data/syndata_'+thedomain+'.csv') #plot timeseries with function (need to add if conditions to check if function returns valid fig) fig = plot_freq_to_fig(amenddf, thedomain) fig.savefig('./plots/freqplot_'+thedomain+'.png', bbox_inches='tight') print ('frequency plot created in file: ./plots/freqplot_'+thedomain+'.png') fig = plot_hist_to_fig(amenddf, thedomain) fig.savefig('./plots/histplot_'+thedomain+'.png', bbox_inches='tight') print ('histogram plot created in file: ./plots/histplot_'+thedomain+'.png') print ('process complete for '+thedomain) print ('-----------------------------------------------------') else: print ("Warning: no syntheseized data was added to: "+thedomain) print ('-----------------------------------------------------') print ('End processing data for visualization !!! ')
mit
pbauman/libmesh
doc/statistics/libmesh_pagehits.py
1
10542
#!/usr/bin/env python import matplotlib.pyplot as plt import numpy as np # Import stuff for working with dates from datetime import datetime from matplotlib.dates import date2num # Hits/month, pages, and gigabytes served. # To get the Google analytics data: # .) Go to analytics.google.com. # .) There should be (as of July 2017) a "Google Analytics Home" box at the top left of the dashboard. # .) Click the "Audience Overview" link at the bottom right corner of this box. # .) Adjust date range to previous month. # .) Record the number of "Pageviews" in the "Hits" column below. # The data below are from the libmesh.github.io site, which uses the # number UA-24978333-1. # # Note: we do not have control over the analytics for the # https://www.github.com/libMesh/libmesh page. If you look at the page # source, analytics code UA-3769691-2 appears, but if I try to add # this property in my analytics account, Google assigns me the number # UA-24978333-{2,3,...} (where the last digit may change depending on # how many times you tried to add/remove this property in the # Analytics Dashboard) and there does not seem to be a straightforward # way of inserting this code into the source. There have been some # README.md based hacks for doing this in the past, but I don't think # they are particularly reliable... # Hits, pages, GB served data = [ # 'Jan 2003', 616, 616, 0 # 'Feb 2003', 2078, 2078, 0, # 'Mar 2003', 3157, 3157, 0, # 'Apr 2003', 7800, 7800, 0, # 'May 2003', 4627, 4627, 0, # 'Jun 2003', 6156, 6156, 0, # 'Jul 2003', 6389, 6389, 0, # 'Aug 2003', 10136, 10136, 0, # 'Sep 2003', 8871, 8871, 0, # 'Oct 2003', 9703, 9703, 0, # 'Nov 2003', 9802, 9802, 0, # 'Dec 2003', 9123, 9123, 0, # 'Jan 2004', 13599, 13599, 0, # 'Feb 2004', 11018, 11018, 0, # 'Mar 2004', 11713, 11713, 0, # 'Apr 2004', 14995, 14995, 0, # 'May 2004', 11285, 11285, 0, # 'Jun 2004', 12974, 12974, 0, # 'Jul 2004', 12939, 12939, 0, # 'Aug 2004', 9708, 9708, 0, # 'Sep 2004', 7994, 7994, 0, # 'Oct 2004', 6920, 6920, 0, # 'Nov 2004', 10261, 10261, 0, # 'Dec 2004', 7483, 7483, 0, # 'Jan 2005', 3184, 3184, 0, # 'Feb 2005', 37733, 14077, .4373, # 'Mar 2005', 43927, 16408, .5637, # 'Apr 2005', 29792, 8518, .2890, # 'May 2005', 51288, 17629, .5689, # 'Jun 2005', 40617, 16599, .5379, # 'Jul 2005', 29944, 10006, .3363, # 'Aug 2005', 39592, 14556, .4577, # 'Sep 2005', 57638, 14666, .4881, # 'Oct 2005', 48336, 17976, .5749, # 'Nov 2005', 49563, 15308, .5810, # 'Dec 2005', 90863, 40736, .9415, # 'Jan 2006', 46723, 13487, .5662, # 'Feb 2006', 62285, 26567, .8229, # 'Mar 2006', 47446, 14711, .6534, # 'Apr 2006', 90314, 29635, .9762, # 'May 2006', 68209, 20998, .7949, # 'Jun 2006', 50495, 17128, .6881, # 'Jul 2006', 42387, 10958, .6016, # 'Aug 2006', 55658, 11793, .6174, # 'Sep 2006', 54919, 20591, .9056, # 'Oct 2006', 52916, 17944, .9015, # 'Nov 2006', 55382, 19833, .9439, # 'Dec 2006', 54265, 22688, .9162, # 'Jan 2007', 53813, 19881, 1.0 , # 'Feb 2007', 52434, 17920, .9472, # 'Mar 2007', 61530, 21172, 1.2, # 'Apr 2007', 125578, 77539, 1.3, # 'May 2007', 182764, 129596, 1.6, # 'Jun 2007', 115730, 38571, 1.7, # 'Jul 2007', 121054, 42757, 1.8, # 'Aug 2007', 81192, 28187, 1.3, # 'Sep 2007', 143553, 39734, 2.3, # 'Oct 2007', 110449, 42111, 2.4, # 'Nov 2007', 128307, 57851, 2.3, # 'Dec 2007', 80584, 42631, 2.0, # 'Jan 2008', 69623, 34155, 2.0, # 'Feb 2008', 144881, 111751, 2.5, # 'Mar 2008', 69801, 29211, 1.9, # 'Apr 2008', 74023, 31149, 2.0, # 'May 2008', 63123, 23277, 1.8, # 'Jun 2008', 66055, 25418, 2.1, # 'Jul 2008', 60046, 22082, 2.0, # 'Aug 2008', 60206, 24543, 2.0, # 'Sep 2008', 53057, 18635, 1.6, # 'Oct 2008', 64828, 27042, 2.1, # 'Nov 2008', 72406, 29767, 2.3, # 'Dec 2008', 76248, 31690, 2.3, # 'Jan 2009', 73002, 29744, 2.0, # 'Feb 2009', 70801, 29156, 2.1, # 'Mar 2009', 78200, 31139, 2.1, # 'Apr 2009', 70888, 26182, 1.7, # 'May 2009', 67263, 26210, 1.8, # 'Jun 2009', 73146, 31328, 2.6, # 'Jul 2009', 77828, 33711, 2.4, # 'Aug 2009', 64378, 28542, 1.9, # 'Sep 2009', 76167, 33484, 2.2, # 'Oct 2009', 95727, 41062, 2.8, # 'Nov 2009', 88042, 38869, 2.5, # 'Dec 2009', 76148, 37609, 2.3, # 'Jan 2010', 268856, 45983, 3.2, # 'Feb 2010', 208210, 42680, 3.0, # 'Mar 2010', 116263, 42660, 2.6, # 'Apr 2010', 102493, 32942, 2.4, # 'May 2010', 117023, 37107, 2.5, # 'Jun 2010', 128589, 38019, 2.5, # 'Jul 2010', 87183, 34026, 2.2, # 'Aug 2010', 99161, 33199, 2.5, # 'Sep 2010', 81657, 32305, 2.5, # 'Oct 2010', 98236, 42091, 3.4, # 'Nov 2010', 115603, 48695, 3.4, # 'Dec 2010', 105030, 45570, 3.4, # 'Jan 2011', 133476, 43549, 3.1, # 'Feb 2011', 34483, 15002, 1.1, # 'Mar 2011', 0, 0, 0.0, # 'Apr 2011', 0, 0, 0.0, # 'May 2011', 0, 0, 0.0, # 'Jun 2011', 0, 0, 0.0, # 'Jul 2011', 0, 0, 0.0, 'Aug 2011', 10185, 0, 0.0, # New "Pageviews" data from google analytics, does not seem comparable to sf.net pagehits data 'Sep 2011', 10305, 0, 0.0, 'Oct 2011', 14081, 0, 0.0, 'Nov 2011', 13397, 0, 0.0, 'Dec 2011', 13729, 0, 0.0, 'Jan 2012', 11050, 0, 0.0, 'Feb 2012', 12779, 0, 0.0, 'Mar 2012', 12970, 0, 0.0, 'Apr 2012', 13051, 0, 0.0, 'May 2012', 11857, 0, 0.0, 'Jun 2012', 12584, 0, 0.0, 'Jul 2012', 12995, 0, 0.0, 'Aug 2012', 13204, 0, 0.0, 'Sep 2012', 13170, 0, 0.0, 'Oct 2012', 13335, 0, 0.0, 'Nov 2012', 11337, 0, 0.0, 'Dec 2012', 10108, 0, 0.0, # libmesh switched to github on December 10, 2012 'Jan 2013', 13029, 0, 0.0, 'Feb 2013', 10420, 0, 0.0, 'Mar 2013', 13400, 0, 0.0, 'Apr 2013', 14416, 0, 0.0, 'May 2013', 13875, 0, 0.0, 'Jun 2013', 13747, 0, 0.0, 'Jul 2013', 14019, 0, 0.0, 'Aug 2013', 10828, 0, 0.0, 'Sep 2013', 9969, 0, 0.0, 'Oct 2013', 13083, 0, 0.0, 'Nov 2013', 12938, 0, 0.0, 'Dec 2013', 9079, 0, 0.0, 'Jan 2014', 9736, 0, 0.0, 'Feb 2014', 11824, 0, 0.0, 'Mar 2014', 10861, 0, 0.0, 'Apr 2014', 12711, 0, 0.0, 'May 2014', 11177, 0, 0.0, 'Jun 2014', 10738, 0, 0.0, 'Jul 2014', 10349, 0, 0.0, 'Aug 2014', 8877, 0, 0.0, 'Sep 2014', 9226, 0, 0.0, 'Oct 2014', 8052, 0, 0.0, # Google analytics number moved over to libmesh.github.io in Oct 2014 'Nov 2014', 9243, 0, 0.0, 'Dec 2014', 10714, 0, 0.0, 'Jan 2015', 11508, 0, 0.0, 'Feb 2015', 11278, 0, 0.0, 'Mar 2015', 13305, 0, 0.0, 'Apr 2015', 12347, 0, 0.0, 'May 2015', 11368, 0, 0.0, 'Jun 2015', 11203, 0, 0.0, 'Jul 2015', 10419, 0, 0.0, 'Aug 2015', 11282, 0, 0.0, 'Sep 2015', 13535, 0, 0.0, 'Oct 2015', 12912, 0, 0.0, 'Nov 2015', 13894, 0, 0.0, 'Dec 2015', 11694, 0, 0.0, 'Jan 2016', 11837, 0, 0.0, 'Feb 2016', 14102, 0, 0.0, 'Mar 2016', 13212, 0, 0.0, 'Apr 2016', 13355, 0, 0.0, 'May 2016', 12486, 0, 0.0, 'Jun 2016', 13973, 0, 0.0, 'Jul 2016', 10688, 0, 0.0, 'Aug 2016', 10048, 0, 0.0, 'Sep 2016', 10847, 0, 0.0, 'Oct 2016', 10984, 0, 0.0, 'Nov 2016', 12233, 0, 0.0, 'Dec 2016', 11430, 0, 0.0, 'Jan 2017', 10327, 0, 0.0, 'Feb 2017', 11039, 0, 0.0, 'Mar 2017', 12986, 0, 0.0, 'Apr 2017', 9773, 0, 0.0, 'May 2017', 10880, 0, 0.0, 'Jun 2017', 9179, 0, 0.0, 'Jul 2017', 8344, 0, 0.0, 'Aug 2017', 8617, 0, 0.0, 'Sep 2017', 8576, 0, 0.0, 'Oct 2017', 11255, 0, 0.0, 'Nov 2017', 10362, 0, 0.0, 'Dec 2017', 7948, 0, 0.0, 'Jan 2018', 9376, 0, 0.0, 'Feb 2018', 8864, 0, 0.0, 'Mar 2018', 10339, 0, 0.0, 'Apr 2018', 10958, 0, 0.0, 'May 2018', 10151, 0, 0.0, 'Jun 2018', 8981, 0, 0.0, 'Jul 2018', 8619, 0, 0.0, 'Aug 2018', 9226, 0, 0.0, 'Sep 2018', 8507, 0, 0.0, 'Oct 2018', 9150, 0, 0.0, 'Nov 2018', 8135, 0, 0.0, 'Dec 2018', 7522, 0, 0.0, 'Jan 2019', 8643, 0, 0.0, 'Feb 2019', 8729, 0, 0.0, 'Mar 2019', 7916, 0, 0.0, ] # Extract number of hits/month n_hits_month = data[1::4] # Divide by 1000 for plotting... n_hits_month = np.divide(n_hits_month, 1000.) # Extract list of date strings date_strings = data[0::4] # Convert date strings into numbers date_nums = [] for d in date_strings: date_nums.append(date2num(datetime.strptime(d, '%b %Y'))) # Get a reference to the figure fig = plt.figure() # 111 is equivalent to Matlab's subplot(1,1,1) command ax = fig.add_subplot(111) # Make the bar chart. We have one number/month, there are about 30 # days in each month, this defines the bar width... # The color used comes from sns.color_palette("muted").as_hex() They # are the "same basic order of hues as the default matplotlib color # cycle but more attractive colors." ax.plot(date_nums, n_hits_month, marker='o', linewidth=2, color=u'#4878cf') # Create title fig.suptitle('libmesh.github.io Hits/Month (in Thousands)') # Set up x-tick locations -- August of each year ticks_names = ['2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019'] # Get numerical values for the names tick_nums = [] for x in ticks_names: tick_nums.append(date2num(datetime.strptime('Jan ' + x, '%b %Y'))) # Set tick labels and positions ax.set_xticks(tick_nums) ax.set_xticklabels(ticks_names) # Set x limits for the plot plt.xlim(date_nums[0], date_nums[-1]+30); # Make x-axis ticks point outward ax.get_xaxis().set_tick_params(direction='out') # Save as PDF plt.savefig('libmesh_pagehits.pdf') # Local Variables: # python-indent: 2 # End:
lgpl-2.1
mfjb/scikit-learn
benchmarks/bench_sample_without_replacement.py
397
8008
""" Benchmarks for sampling without replacement of integer. """ from __future__ import division from __future__ import print_function import gc import sys import optparse from datetime import datetime import operator import matplotlib.pyplot as plt import numpy as np import random from sklearn.externals.six.moves import xrange from sklearn.utils.random import sample_without_replacement def compute_time(t_start, delta): mu_second = 0.0 + 10 ** 6 # number of microseconds in a second return delta.seconds + delta.microseconds / mu_second def bench_sample(sampling, n_population, n_samples): gc.collect() # start time t_start = datetime.now() sampling(n_population, n_samples) delta = (datetime.now() - t_start) # stop time time = compute_time(t_start, delta) return time if __name__ == "__main__": ########################################################################### # Option parser ########################################################################### op = optparse.OptionParser() op.add_option("--n-times", dest="n_times", default=5, type=int, help="Benchmark results are average over n_times experiments") op.add_option("--n-population", dest="n_population", default=100000, type=int, help="Size of the population to sample from.") op.add_option("--n-step", dest="n_steps", default=5, type=int, help="Number of step interval between 0 and n_population.") default_algorithms = "custom-tracking-selection,custom-auto," \ "custom-reservoir-sampling,custom-pool,"\ "python-core-sample,numpy-permutation" op.add_option("--algorithm", dest="selected_algorithm", default=default_algorithms, type=str, help="Comma-separated list of transformer to benchmark. " "Default: %default. \nAvailable: %default") # op.add_option("--random-seed", # dest="random_seed", default=13, type=int, # help="Seed used by the random number generators.") (opts, args) = op.parse_args() if len(args) > 0: op.error("this script takes no arguments.") sys.exit(1) selected_algorithm = opts.selected_algorithm.split(',') for key in selected_algorithm: if key not in default_algorithms.split(','): raise ValueError("Unknown sampling algorithm \"%s\" not in (%s)." % (key, default_algorithms)) ########################################################################### # List sampling algorithm ########################################################################### # We assume that sampling algorithm has the following signature: # sample(n_population, n_sample) # sampling_algorithm = {} ########################################################################### # Set Python core input sampling_algorithm["python-core-sample"] = \ lambda n_population, n_sample: \ random.sample(xrange(n_population), n_sample) ########################################################################### # Set custom automatic method selection sampling_algorithm["custom-auto"] = \ lambda n_population, n_samples, random_state=None: \ sample_without_replacement(n_population, n_samples, method="auto", random_state=random_state) ########################################################################### # Set custom tracking based method sampling_algorithm["custom-tracking-selection"] = \ lambda n_population, n_samples, random_state=None: \ sample_without_replacement(n_population, n_samples, method="tracking_selection", random_state=random_state) ########################################################################### # Set custom reservoir based method sampling_algorithm["custom-reservoir-sampling"] = \ lambda n_population, n_samples, random_state=None: \ sample_without_replacement(n_population, n_samples, method="reservoir_sampling", random_state=random_state) ########################################################################### # Set custom reservoir based method sampling_algorithm["custom-pool"] = \ lambda n_population, n_samples, random_state=None: \ sample_without_replacement(n_population, n_samples, method="pool", random_state=random_state) ########################################################################### # Numpy permutation based sampling_algorithm["numpy-permutation"] = \ lambda n_population, n_sample: \ np.random.permutation(n_population)[:n_sample] ########################################################################### # Remove unspecified algorithm sampling_algorithm = dict((key, value) for key, value in sampling_algorithm.items() if key in selected_algorithm) ########################################################################### # Perform benchmark ########################################################################### time = {} n_samples = np.linspace(start=0, stop=opts.n_population, num=opts.n_steps).astype(np.int) ratio = n_samples / opts.n_population print('Benchmarks') print("===========================") for name in sorted(sampling_algorithm): print("Perform benchmarks for %s..." % name, end="") time[name] = np.zeros(shape=(opts.n_steps, opts.n_times)) for step in xrange(opts.n_steps): for it in xrange(opts.n_times): time[name][step, it] = bench_sample(sampling_algorithm[name], opts.n_population, n_samples[step]) print("done") print("Averaging results...", end="") for name in sampling_algorithm: time[name] = np.mean(time[name], axis=1) print("done\n") # Print results ########################################################################### print("Script arguments") print("===========================") arguments = vars(opts) print("%s \t | %s " % ("Arguments".ljust(16), "Value".center(12),)) print(25 * "-" + ("|" + "-" * 14) * 1) for key, value in arguments.items(): print("%s \t | %s " % (str(key).ljust(16), str(value).strip().center(12))) print("") print("Sampling algorithm performance:") print("===============================") print("Results are averaged over %s repetition(s)." % opts.n_times) print("") fig = plt.figure('scikit-learn sample w/o replacement benchmark results') plt.title("n_population = %s, n_times = %s" % (opts.n_population, opts.n_times)) ax = fig.add_subplot(111) for name in sampling_algorithm: ax.plot(ratio, time[name], label=name) ax.set_xlabel('ratio of n_sample / n_population') ax.set_ylabel('Time (s)') ax.legend() # Sort legend labels handles, labels = ax.get_legend_handles_labels() hl = sorted(zip(handles, labels), key=operator.itemgetter(1)) handles2, labels2 = zip(*hl) ax.legend(handles2, labels2, loc=0) plt.show()
bsd-3-clause
crichardson17/starburst_atlas
Low_resolution_sims/Dusty_LowRes/Padova_inst/padova_inst_2/fullgrid/Rest.py
30
9192
import csv import matplotlib.pyplot as plt from numpy import * import scipy.interpolate import math from pylab import * from matplotlib.ticker import MultipleLocator, FormatStrFormatter import matplotlib.patches as patches from matplotlib.path import Path import os # ------------------------------------------------------------------------------------------------------ #inputs for file in os.listdir('.'): if file.endswith("1.grd"): gridfile1 = file for file in os.listdir('.'): if file.endswith("2.grd"): gridfile2 = file for file in os.listdir('.'): if file.endswith("3.grd"): gridfile3 = file # ------------------------ for file in os.listdir('.'): if file.endswith("1.txt"): Elines1 = file for file in os.listdir('.'): if file.endswith("2.txt"): Elines2 = file for file in os.listdir('.'): if file.endswith("3.txt"): Elines3 = file # ------------------------------------------------------------------------------------------------------ #Patches data #for the Kewley and Levesque data verts = [ (1., 7.97712125471966000000), # left, bottom (1., 9.57712125471966000000), # left, top (2., 10.57712125471970000000), # right, top (2., 8.97712125471966000000), # right, bottom (0., 0.), # ignored ] codes = [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY, ] path = Path(verts, codes) # ------------------------ #for the Kewley 01 data verts2 = [ (2.4, 9.243038049), # left, bottom (2.4, 11.0211893), # left, top (2.6, 11.0211893), # right, top (2.6, 9.243038049), # right, bottom (0, 0.), # ignored ] path = Path(verts, codes) path2 = Path(verts2, codes) # ------------------------- #for the Moy et al data verts3 = [ (1., 6.86712125471966000000), # left, bottom (1., 10.18712125471970000000), # left, top (3., 12.18712125471970000000), # right, top (3., 8.86712125471966000000), # right, bottom (0., 0.), # ignored ] path = Path(verts, codes) path3 = Path(verts3, codes) # ------------------------------------------------------------------------------------------------------ #the routine to add patches for others peoples' data onto our plots. def add_patches(ax): patch3 = patches.PathPatch(path3, facecolor='yellow', lw=0) patch2 = patches.PathPatch(path2, facecolor='green', lw=0) patch = patches.PathPatch(path, facecolor='red', lw=0) ax1.add_patch(patch3) ax1.add_patch(patch2) ax1.add_patch(patch) # ------------------------------------------------------------------------------------------------------ #the subplot routine def add_sub_plot(sub_num): numplots = 16 plt.subplot(numplots/4.,4,sub_num) rbf = scipy.interpolate.Rbf(x, y, z[:,sub_num-1], function='linear') zi = rbf(xi, yi) contour = plt.contour(xi,yi,zi, levels, colors='c', linestyles = 'dashed') contour2 = plt.contour(xi,yi,zi, levels2, colors='k', linewidths=1.5) plt.scatter(max_values[line[sub_num-1],2], max_values[line[sub_num-1],3], c ='k',marker = '*') plt.annotate(headers[line[sub_num-1]], xy=(8,11), xytext=(6,8.5), fontsize = 10) plt.annotate(max_values[line[sub_num-1],0], xy= (max_values[line[sub_num-1],2], max_values[line[sub_num-1],3]), xytext = (0, -10), textcoords = 'offset points', ha = 'right', va = 'bottom', fontsize=10) if sub_num == numplots / 2.: print "half the plots are complete" #axis limits yt_min = 8 yt_max = 23 xt_min = 0 xt_max = 12 plt.ylim(yt_min,yt_max) plt.xlim(xt_min,xt_max) plt.yticks(arange(yt_min+1,yt_max,1),fontsize=10) plt.xticks(arange(xt_min+1,xt_max,1), fontsize = 10) if sub_num in [2,3,4,6,7,8,10,11,12,14,15,16]: plt.tick_params(labelleft = 'off') else: plt.tick_params(labelleft = 'on') plt.ylabel('Log ($ \phi _{\mathrm{H}} $)') if sub_num in [1,2,3,4,5,6,7,8,9,10,11,12]: plt.tick_params(labelbottom = 'off') else: plt.tick_params(labelbottom = 'on') plt.xlabel('Log($n _{\mathrm{H}} $)') if sub_num == 1: plt.yticks(arange(yt_min+1,yt_max+1,1),fontsize=10) if sub_num == 13: plt.yticks(arange(yt_min,yt_max,1),fontsize=10) plt.xticks(arange(xt_min,xt_max,1), fontsize = 10) if sub_num == 16 : plt.xticks(arange(xt_min+1,xt_max+1,1), fontsize = 10) # --------------------------------------------------- #this is where the grid information (phi and hdens) is read in and saved to grid. grid1 = []; grid2 = []; grid3 = []; with open(gridfile1, 'rb') as f: csvReader = csv.reader(f,delimiter='\t') for row in csvReader: grid1.append(row); grid1 = asarray(grid1) with open(gridfile2, 'rb') as f: csvReader = csv.reader(f,delimiter='\t') for row in csvReader: grid2.append(row); grid2 = asarray(grid2) with open(gridfile3, 'rb') as f: csvReader = csv.reader(f,delimiter='\t') for row in csvReader: grid3.append(row); grid3 = asarray(grid3) #here is where the data for each line is read in and saved to dataEmissionlines dataEmissionlines1 = []; dataEmissionlines2 = []; dataEmissionlines3 = []; with open(Elines1, 'rb') as f: csvReader = csv.reader(f,delimiter='\t') headers = csvReader.next() for row in csvReader: dataEmissionlines1.append(row); dataEmissionlines1 = asarray(dataEmissionlines1) with open(Elines2, 'rb') as f: csvReader = csv.reader(f,delimiter='\t') headers2 = csvReader.next() for row in csvReader: dataEmissionlines2.append(row); dataEmissionlines2 = asarray(dataEmissionlines2) with open(Elines3, 'rb') as f: csvReader = csv.reader(f,delimiter='\t') headers3 = csvReader.next() for row in csvReader: dataEmissionlines3.append(row); dataEmissionlines3 = asarray(dataEmissionlines3) print "import files complete" # --------------------------------------------------- #for concatenating grid #pull the phi and hdens values from each of the runs. exclude header lines grid1new = zeros((len(grid1[:,0])-1,2)) grid1new[:,0] = grid1[1:,6] grid1new[:,1] = grid1[1:,7] grid2new = zeros((len(grid2[:,0])-1,2)) x = array(17.00000) grid2new[:,0] = repeat(x,len(grid2[:,0])-1) grid2new[:,1] = grid2[1:,6] grid3new = zeros((len(grid3[:,0])-1,2)) grid3new[:,0] = grid3[1:,6] grid3new[:,1] = grid3[1:,7] grid = concatenate((grid1new,grid2new,grid3new)) hdens_values = grid[:,1] phi_values = grid[:,0] # --------------------------------------------------- #for concatenating Emission lines data Emissionlines = concatenate((dataEmissionlines1[:,1:],dataEmissionlines2[:,1:],dataEmissionlines3[:,1:])) #for lines headers = headers[1:] concatenated_data = zeros((len(Emissionlines),len(Emissionlines[0]))) max_values = zeros((len(concatenated_data[0]),4)) # --------------------------------------------------- #constructing grid by scaling #select the scaling factor #for 1215 #incident = Emissionlines[1:,4] #for 4860 incident = concatenated_data[:,57] #take the ratio of incident and all the lines and put it all in an array concatenated_data for i in range(len(Emissionlines)): for j in range(len(Emissionlines[0])): if math.log(4860.*(float(Emissionlines[i,j])/float(Emissionlines[i,57])), 10) > 0: concatenated_data[i,j] = math.log(4860.*(float(Emissionlines[i,j])/float(Emissionlines[i,57])), 10) else: concatenated_data[i,j] == 0 # for 1215 #for i in range(len(Emissionlines)): # for j in range(len(Emissionlines[0])): # if math.log(1215.*(float(Emissionlines[i,j])/float(Emissionlines[i,4])), 10) > 0: # concatenated_data[i,j] = math.log(1215.*(float(Emissionlines[i,j])/float(Emissionlines[i,4])), 10) # else: # concatenated_data[i,j] == 0 # --------------------------------------------------- #find the maxima to plot onto the contour plots for j in range(len(concatenated_data[0])): max_values[j,0] = max(concatenated_data[:,j]) max_values[j,1] = argmax(concatenated_data[:,j], axis = 0) max_values[j,2] = hdens_values[max_values[j,1]] max_values[j,3] = phi_values[max_values[j,1]] #to round off the maxima max_values[:,0] = [ '%.1f' % elem for elem in max_values[:,0] ] print "data arranged" # --------------------------------------------------- #Creating the grid to interpolate with for contours. gridarray = zeros((len(concatenated_data),2)) gridarray[:,0] = hdens_values gridarray[:,1] = phi_values x = gridarray[:,0] y = gridarray[:,1] # --------------------------------------------------- #change desired lines here! line = [3,4,15,22,37,53,54,55,57,62,77,88,89,90,92,93] #create z array for this plot z = concatenated_data[:,line[:]] # --------------------------------------------------- # Interpolate print "starting interpolation" xi, yi = linspace(x.min(), x.max(), 10), linspace(y.min(), y.max(), 10) xi, yi = meshgrid(xi, yi) # --------------------------------------------------- print "interpolatation complete; now plotting" #plot plt.subplots_adjust(wspace=0, hspace=0) #remove space between plots levels = arange(10**-1,10, .2) levels2 = arange(10**-2,10**2, 1) plt.suptitle("Dusty Rest of the Lines", fontsize=14) # --------------------------------------------------- for i in range(16): add_sub_plot(i) ax1 = plt.subplot(4,4,1) add_patches(ax1) print "complete" plt.savefig('Dusty_Rest.pdf') plt.clf() print "figure saved"
gpl-2.0
andrewgiessel/folium
folium/utilities.py
1
19979
# -*- coding: utf-8 -*- """ Utilities ------- Utility module for Folium helper functions. """ from __future__ import absolute_import from __future__ import print_function from __future__ import division import time import math import zlib import struct import json import base64 from jinja2 import Environment, PackageLoader try: import pandas as pd except ImportError: pd = None try: import numpy as np except ImportError: np = None from folium.six import iteritems, text_type, binary_type def get_templates(): """Get Jinja templates.""" return Environment(loader=PackageLoader('folium', 'templates')) def legend_scaler(legend_values, max_labels=10.0): """ Downsamples the number of legend values so that there isn't a collision of text on the legend colorbar (within reason). The colorbar seems to support ~10 entries as a maximum. """ if len(legend_values) < max_labels: legend_ticks = legend_values else: spacer = int(math.ceil(len(legend_values)/max_labels)) legend_ticks = [] for i in legend_values[::spacer]: legend_ticks += [i] legend_ticks += ['']*(spacer-1) return legend_ticks def linear_gradient(hexList, nColors): """ Given a list of hexcode values, will return a list of length nColors where the colors are linearly interpolated between the (r, g, b) tuples that are given. Example: linear_gradient([(0, 0, 0), (255, 0, 0), (255, 255, 0)], 100) """ def _scale(start, finish, length, i): """ Return the value correct value of a number that is in between start and finish, for use in a loop of length *length*. """ base = 16 fraction = float(i) / (length - 1) raynge = int(finish, base) - int(start, base) thex = hex(int(int(start, base) + fraction * raynge)).split('x')[-1] if len(thex) != 2: thex = '0' + thex return thex allColors = [] # Separate (R, G, B) pairs. for start, end in zip(hexList[:-1], hexList[1:]): # Linearly intepolate between pair of hex ###### values and # add to list. nInterpolate = 765 for index in range(nInterpolate): r = _scale(start[1:3], end[1:3], nInterpolate, index) g = _scale(start[3:5], end[3:5], nInterpolate, index) b = _scale(start[5:7], end[5:7], nInterpolate, index) allColors.append(''.join(['#', r, g, b])) # Pick only nColors colors from the total list. result = [] for counter in range(nColors): fraction = float(counter) / (nColors - 1) index = int(fraction * (len(allColors) - 1)) result.append(allColors[index]) return result def color_brewer(color_code, n=6): """ Generate a colorbrewer color scheme of length 'len', type 'scheme. Live examples can be seen at http://colorbrewer2.org/ """ maximum_n = 253 scheme_info = {'BuGn': 'Sequential', 'BuPu': 'Sequential', 'GnBu': 'Sequential', 'OrRd': 'Sequential', 'PuBu': 'Sequential', 'PuBuGn': 'Sequential', 'PuRd': 'Sequential', 'RdPu': 'Sequential', 'YlGn': 'Sequential', 'YlGnBu': 'Sequential', 'YlOrBr': 'Sequential', 'YlOrRd': 'Sequential', 'BrBg': 'Diverging', 'PiYG': 'Diverging', 'PRGn': 'Diverging', 'PuOr': 'Diverging', 'RdBu': 'Diverging', 'RdGy': 'Diverging', 'RdYlBu': 'Diverging', 'RdYlGn': 'Diverging', 'Spectral': 'Diverging', 'Accent': 'Qualitative', 'Dark2': 'Qualitative', 'Paired': 'Qualitative', 'Pastel1': 'Qualitative', 'Pastel2': 'Qualitative', 'Set1': 'Qualitative', 'Set2': 'Qualitative', 'Set3': 'Qualitative', } schemes = {'BuGn': ['#EDF8FB', '#CCECE6', '#CCECE6', '#66C2A4', '#41AE76', '#238B45', '#005824'], 'BuPu': ['#EDF8FB', '#BFD3E6', '#9EBCDA', '#8C96C6', '#8C6BB1', '#88419D', '#6E016B'], 'GnBu': ['#F0F9E8', '#CCEBC5', '#A8DDB5', '#7BCCC4', '#4EB3D3', '#2B8CBE', '#08589E'], 'OrRd': ['#FEF0D9', '#FDD49E', '#FDBB84', '#FC8D59', '#EF6548', '#D7301F', '#990000'], 'PuBu': ['#F1EEF6', '#D0D1E6', '#A6BDDB', '#74A9CF', '#3690C0', '#0570B0', '#034E7B'], 'PuBuGn': ['#F6EFF7', '#D0D1E6', '#A6BDDB', '#67A9CF', '#3690C0', '#02818A', '#016450'], 'PuRd': ['#F1EEF6', '#D4B9DA', '#C994C7', '#DF65B0', '#E7298A', '#CE1256', '#91003F'], 'RdPu': ['#FEEBE2', '#FCC5C0', '#FA9FB5', '#F768A1', '#DD3497', '#AE017E', '#7A0177'], 'YlGn': ['#FFFFCC', '#D9F0A3', '#ADDD8E', '#78C679', '#41AB5D', '#238443', '#005A32'], 'YlGnBu': ['#FFFFCC', '#C7E9B4', '#7FCDBB', '#41B6C4', '#1D91C0', '#225EA8', '#0C2C84'], 'YlOrBr': ['#FFFFD4', '#FEE391', '#FEC44F', '#FE9929', '#EC7014', '#CC4C02', '#8C2D04'], 'YlOrRd': ['#FFFFB2', '#FED976', '#FEB24C', '#FD8D3C', '#FC4E2A', '#E31A1C', '#B10026'], 'BrBg': ['#8c510a', '#d8b365', '#f6e8c3', '#c7eae5', '#5ab4ac', '#01665e'], 'PiYG': ['#c51b7d', '#e9a3c9', '#fde0ef', '#e6f5d0', '#a1d76a', '#4d9221'], 'PRGn': ['#762a83', '#af8dc3', '#e7d4e8', '#d9f0d3', '#7fbf7b', '#1b7837'], 'PuOr': ['#b35806', '#f1a340', '#fee0b6', '#d8daeb', '#998ec3', '#542788'], 'RdBu': ['#b2182b', '#ef8a62', '#fddbc7', '#d1e5f0', '#67a9cf', '#2166ac'], 'RdGy': ['#b2182b', '#ef8a62', '#fddbc7', '#e0e0e0', '#999999', '#4d4d4d'], 'RdYlBu': ['#d73027', '#fc8d59', '#fee090', '#e0f3f8', '#91bfdb', '#4575b4'], 'RdYlGn': ['#d73027', '#fc8d59', '#fee08b', '#d9ef8b', '#91cf60', '#1a9850'], 'Spectral': ['#d53e4f', '#fc8d59', '#fee08b', '#e6f598', '#99d594', '#3288bd'], 'Accent': ['#7fc97f', '#beaed4', '#fdc086', '#ffff99', '#386cb0', '#f0027f'], 'Dark2': ['#1b9e77', '#d95f02', '#7570b3', '#e7298a', '#66a61e', '#e6ab02'], 'Paired': ['#a6cee3', '#1f78b4', '#b2df8a', '#33a02c', '#fb9a99', '#e31a1c'], 'Pastel1': ['#fbb4ae', '#b3cde3', '#ccebc5', '#decbe4', '#fed9a6', '#ffffcc'], 'Pastel2': ['#b3e2cd', '#fdcdac', '#cbd5e8', '#f4cae4', '#e6f5c9', '#fff2ae'], 'Set1': ['#e41a1c', '#377eb8', '#4daf4a', '#984ea3', '#ff7f00', '#ffff33'], 'Set2': ['#66c2a5', '#fc8d62', '#8da0cb', '#e78ac3', '#a6d854', '#ffd92f'], 'Set3': ['#8dd3c7', '#ffffb3', '#bebada', '#fb8072', '#80b1d3', '#fdb462'], } # Raise an error if the n requested is greater than the maximum. if n > maximum_n: raise ValueError("The maximum number of colors in a" " ColorBrewer sequential color series is 253") # Only if n is greater than six do we interpolate values. if n > 6: if color_code not in schemes: color_scheme = None else: # Check to make sure that it is not a qualitative scheme. if scheme_info[color_code] == 'Qualitative': raise ValueError("Expanded color support is not available" " for Qualitative schemes, restrict" " number of colors to 6") else: color_scheme = linear_gradient(schemes.get(color_code), n) else: color_scheme = schemes.get(color_code, None) return color_scheme def transform_data(data): """ Transform Pandas DataFrame into JSON format. Parameters ---------- data: DataFrame or Series Pandas DataFrame or Series Returns ------- JSON compatible dict Example ------- >>> transform_data(df) """ if pd is None: raise ImportError("The Pandas package is required" " for this functionality") if np is None: raise ImportError("The NumPy package is required" " for this functionality") def type_check(value): """ Type check values for JSON serialization. Native Python JSON serialization will not recognize some Numpy data types properly, so they must be explicitly converted. """ if pd.isnull(value): return None elif (isinstance(value, pd.tslib.Timestamp) or isinstance(value, pd.Period)): return time.mktime(value.timetuple()) elif isinstance(value, (int, np.integer)): return int(value) elif isinstance(value, (float, np.float_)): return float(value) elif isinstance(value, str): return str(value) else: return value if isinstance(data, pd.Series): json_data = [{type_check(x): type_check(y) for x, y in iteritems(data)}] elif isinstance(data, pd.DataFrame): json_data = [{type_check(y): type_check(z) for x, y, z in data.itertuples()}] return json_data def split_six(series=None): """ Given a Pandas Series, get a domain of values from zero to the 90% quantile rounded to the nearest order-of-magnitude integer. For example, 2100 is rounded to 2000, 2790 to 3000. Parameters ---------- series: Pandas series, default None Returns ------- list """ if pd is None: raise ImportError("The Pandas package is required" " for this functionality") if np is None: raise ImportError("The NumPy package is required" " for this functionality") def base(x): if x > 0: base = pow(10, math.floor(math.log10(x))) return round(x/base)*base else: return 0 quants = [0, 50, 75, 85, 90] # Some weirdness in series quantiles a la 0.13. arr = series.values return [base(np.percentile(arr, x)) for x in quants] def mercator_transform(data, lat_bounds, origin='upper', height_out=None): """Transforms an image computed in (longitude,latitude) coordinates into the a Mercator projection image. Parameters ---------- data: numpy array or equivalent list-like object. Must be NxM (mono), NxMx3 (RGB) or NxMx4 (RGBA) lat_bounds : length 2 tuple Minimal and maximal value of the latitude of the image. origin : ['upper' | 'lower'], optional, default 'upper' Place the [0,0] index of the array in the upper left or lower left corner of the axes. height_out : int, default None The expected height of the output. If None, the height of the input is used. """ if np is None: raise ImportError("The NumPy package is required" " for this functionality") mercator = lambda x: np.arcsinh(np.tan(x*np.pi/180.))*180./np.pi array = np.atleast_3d(data).copy() height, width, nblayers = array.shape lat_min, lat_max = lat_bounds if height_out is None: height_out = height # Eventually flip the image if origin == 'upper': array = array[::-1, :, :] lats = (lat_min + np.linspace(0.5/height, 1.-0.5/height, height) * (lat_max-lat_min)) latslats = (mercator(lat_min) + np.linspace(0.5/height_out, 1.-0.5/height_out, height_out) * (mercator(lat_max)-mercator(lat_min))) out = np.zeros((height_out, width, nblayers)) for i in range(width): for j in range(4): out[:, i, j] = np.interp(latslats, mercator(lats), array[:, i, j]) # Eventually flip the image. if origin == 'upper': out = out[::-1, :, :] return out def image_to_url(image, mercator_project=False, colormap=None, origin='upper', bounds=((-90, -180), (90, 180))): """Infers the type of an image argument and transforms it into a URL. Parameters ---------- image: string, file or array-like object * If string, it will be written directly in the output file. * If file, it's content will be converted as embedded in the output file. * If array-like, it will be converted to PNG base64 string and embedded in the output. origin : ['upper' | 'lower'], optional, default 'upper' Place the [0, 0] index of the array in the upper left or lower left corner of the axes. colormap : callable, used only for `mono` image. Function of the form [x -> (r,g,b)] or [x -> (r,g,b,a)] for transforming a mono image into RGB. It must output iterables of length 3 or 4, with values between 0. and 1. Hint : you can use colormaps from `matplotlib.cm`. mercator_project : bool, default False, used for array-like image. Transforms the data to project (longitude,latitude) coordinates to the Mercator projection. bounds: list-like, default ((-90, -180), (90, 180)) Image bounds on the map in the form [[lat_min, lon_min], [lat_max, lon_max]]. Only used if mercator_project is True. """ if hasattr(image, 'read'): # We got an image file. if hasattr(image, 'name'): # We try to get the image format from the file name. fileformat = image.name.lower().split('.')[-1] else: fileformat = 'png' url = "data:image/{};base64,{}".format( fileformat, base64.b64encode(image.read()).decode('utf-8')) elif (not (isinstance(image, text_type) or isinstance(image, binary_type))) and hasattr(image, '__iter__'): # We got an array-like object. if mercator_project: data = mercator_transform(image, [bounds[0][0], bounds[1][0]], origin=origin) else: data = image png = write_png(data, origin=origin, colormap=colormap) url = "data:image/png;base64," + base64.b64encode(png).decode('utf-8') else: # We got an URL. url = json.loads(json.dumps(image)) return url.replace('\n', ' ') def write_png(data, origin='upper', colormap=None): """ Transform an array of data into a PNG string. This can be written to disk using binary I/O, or encoded using base64 for an inline PNG like this: >>> png_str = write_png(array) >>> "data:image/png;base64,"+png_str.encode('base64') Inspired from http://stackoverflow.com/questions/902761/saving-a-numpy-array-as-an-image Parameters ---------- data: numpy array or equivalent list-like object. Must be NxM (mono), NxMx3 (RGB) or NxMx4 (RGBA) origin : ['upper' | 'lower'], optional, default 'upper' Place the [0,0] index of the array in the upper left or lower left corner of the axes. colormap : callable, used only for `mono` image. Function of the form [x -> (r,g,b)] or [x -> (r,g,b,a)] for transforming a mono image into RGB. It must output iterables of length 3 or 4, with values between 0. and 1. Hint: you can use colormaps from `matplotlib.cm`. Returns ------- PNG formatted byte string """ if np is None: raise ImportError("The NumPy package is required" " for this functionality") if colormap is None: colormap = lambda x: (x, x, x, 1) array = np.atleast_3d(data) height, width, nblayers = array.shape if nblayers not in [1, 3, 4]: raise ValueError("Data must be NxM (mono), " "NxMx3 (RGB), or NxMx4 (RGBA)") assert array.shape == (height, width, nblayers) if nblayers == 1: array = np.array(list(map(colormap, array.ravel()))) nblayers = array.shape[1] if nblayers not in [3, 4]: raise ValueError("colormap must provide colors of" "length 3 (RGB) or 4 (RGBA)") array = array.reshape((height, width, nblayers)) assert array.shape == (height, width, nblayers) if nblayers == 3: array = np.concatenate((array, np.ones((height, width, 1))), axis=2) nblayers = 4 assert array.shape == (height, width, nblayers) assert nblayers == 4 # Normalize to uint8 if it isn't already. if array.dtype != 'uint8': array = array * 255./array.max(axis=(0, 1)).reshape((1, 1, 4)) array = array.astype('uint8') # Eventually flip the image. if origin == 'lower': array = array[::-1, :, :] # Transform the array to bytes. raw_data = b''.join([b'\x00' + array[i, :, :].tobytes() for i in range(height)]) def png_pack(png_tag, data): chunk_head = png_tag + data return (struct.pack("!I", len(data)) + chunk_head + struct.pack("!I", 0xFFFFFFFF & zlib.crc32(chunk_head))) return b''.join([ b'\x89PNG\r\n\x1a\n', png_pack(b'IHDR', struct.pack("!2I5B", width, height, 8, 6, 0, 0, 0)), png_pack(b'IDAT', zlib.compress(raw_data, 9)), png_pack(b'IEND', b'')]) def _camelify(out): return (''.join(["_" + x.lower() if i < len(out)-1 and x.isupper() and out[i+1].islower() # noqa else x.lower() + "_" if i < len(out)-1 and x.islower() and out[i+1].isupper() # noqa else x.lower() for i, x in enumerate(list(out))])).lstrip('_').replace('__', '_') # noqa def _parse_size(value): try: if isinstance(value, int) or isinstance(value, float): value_type = 'px' value = float(value) assert value > 0 else: value_type = '%' value = float(value.strip('%')) assert 0 <= value <= 100 except: msg = "Cannot parse value {!r} as {!r}".format raise ValueError(msg(value, value_type)) return value, value_type def _locations_mirror(x): """Mirrors the points in a list-of-list-of-...-of-list-of-points. For example: >>> _locations_mirror([[[1, 2], [3, 4]], [5, 6], [7, 8]]) [[[2, 1], [4, 3]], [6, 5], [8, 7]] """ if hasattr(x, '__iter__'): if hasattr(x[0], '__iter__'): return list(map(_locations_mirror, x)) else: return list(x[::-1]) else: return x def _locations_tolist(x): """Transforms recursively a list of iterables into a list of list. """ if hasattr(x, '__iter__'): return list(map(_locations_tolist, x)) else: return x
mit
tomlof/scikit-learn
sklearn/decomposition/dict_learning.py
19
46220
""" Dictionary learning """ from __future__ import print_function # Author: Vlad Niculae, Gael Varoquaux, Alexandre Gramfort # License: BSD 3 clause import time import sys import itertools from math import sqrt, ceil import numpy as np from scipy import linalg from numpy.lib.stride_tricks import as_strided from ..base import BaseEstimator, TransformerMixin from ..externals.joblib import Parallel, delayed, cpu_count from ..externals.six.moves import zip from ..utils import (check_array, check_random_state, gen_even_slices, gen_batches, _get_n_jobs) from ..utils.extmath import randomized_svd, row_norms from ..utils.validation import check_is_fitted from ..linear_model import Lasso, orthogonal_mp_gram, LassoLars, Lars def _sparse_encode(X, dictionary, gram, cov=None, algorithm='lasso_lars', regularization=None, copy_cov=True, init=None, max_iter=1000, check_input=True, verbose=0): """Generic sparse coding Each column of the result is the solution to a Lasso problem. Parameters ---------- X : array of shape (n_samples, n_features) Data matrix. dictionary : array of shape (n_components, n_features) The dictionary matrix against which to solve the sparse coding of the data. Some of the algorithms assume normalized rows. gram : None | array, shape=(n_components, n_components) Precomputed Gram matrix, dictionary * dictionary' gram can be None if method is 'threshold'. cov : array, shape=(n_components, n_samples) Precomputed covariance, dictionary * X' algorithm : {'lasso_lars', 'lasso_cd', 'lars', 'omp', 'threshold'} lars: uses the least angle regression method (linear_model.lars_path) lasso_lars: uses Lars to compute the Lasso solution lasso_cd: uses the coordinate descent method to compute the Lasso solution (linear_model.Lasso). lasso_lars will be faster if the estimated components are sparse. omp: uses orthogonal matching pursuit to estimate the sparse solution threshold: squashes to zero all coefficients less than regularization from the projection dictionary * data' regularization : int | float The regularization parameter. It corresponds to alpha when algorithm is 'lasso_lars', 'lasso_cd' or 'threshold'. Otherwise it corresponds to n_nonzero_coefs. init : array of shape (n_samples, n_components) Initialization value of the sparse code. Only used if `algorithm='lasso_cd'`. max_iter : int, 1000 by default Maximum number of iterations to perform if `algorithm='lasso_cd'`. copy_cov : boolean, optional Whether to copy the precomputed covariance matrix; if False, it may be overwritten. check_input : boolean, optional If False, the input arrays X and dictionary will not be checked. verbose : int Controls the verbosity; the higher, the more messages. Defaults to 0. Returns ------- code : array of shape (n_components, n_features) The sparse codes See also -------- sklearn.linear_model.lars_path sklearn.linear_model.orthogonal_mp sklearn.linear_model.Lasso SparseCoder """ if X.ndim == 1: X = X[:, np.newaxis] n_samples, n_features = X.shape if cov is None and algorithm != 'lasso_cd': # overwriting cov is safe copy_cov = False cov = np.dot(dictionary, X.T) if algorithm == 'lasso_lars': alpha = float(regularization) / n_features # account for scaling try: err_mgt = np.seterr(all='ignore') # Not passing in verbose=max(0, verbose-1) because Lars.fit already # corrects the verbosity level. lasso_lars = LassoLars(alpha=alpha, fit_intercept=False, verbose=verbose, normalize=False, precompute=gram, fit_path=False) lasso_lars.fit(dictionary.T, X.T, Xy=cov) new_code = lasso_lars.coef_ finally: np.seterr(**err_mgt) elif algorithm == 'lasso_cd': alpha = float(regularization) / n_features # account for scaling # TODO: Make verbosity argument for Lasso? # sklearn.linear_model.coordinate_descent.enet_path has a verbosity # argument that we could pass in from Lasso. clf = Lasso(alpha=alpha, fit_intercept=False, normalize=False, precompute=gram, max_iter=max_iter, warm_start=True) if init is not None: clf.coef_ = init clf.fit(dictionary.T, X.T, check_input=check_input) new_code = clf.coef_ elif algorithm == 'lars': try: err_mgt = np.seterr(all='ignore') # Not passing in verbose=max(0, verbose-1) because Lars.fit already # corrects the verbosity level. lars = Lars(fit_intercept=False, verbose=verbose, normalize=False, precompute=gram, n_nonzero_coefs=int(regularization), fit_path=False) lars.fit(dictionary.T, X.T, Xy=cov) new_code = lars.coef_ finally: np.seterr(**err_mgt) elif algorithm == 'threshold': new_code = ((np.sign(cov) * np.maximum(np.abs(cov) - regularization, 0)).T) elif algorithm == 'omp': # TODO: Should verbose argument be passed to this? new_code = orthogonal_mp_gram( Gram=gram, Xy=cov, n_nonzero_coefs=int(regularization), tol=None, norms_squared=row_norms(X, squared=True), copy_Xy=copy_cov).T else: raise ValueError('Sparse coding method must be "lasso_lars" ' '"lasso_cd", "lasso", "threshold" or "omp", got %s.' % algorithm) return new_code # XXX : could be moved to the linear_model module def sparse_encode(X, dictionary, gram=None, cov=None, algorithm='lasso_lars', n_nonzero_coefs=None, alpha=None, copy_cov=True, init=None, max_iter=1000, n_jobs=1, check_input=True, verbose=0): """Sparse coding Each row of the result is the solution to a sparse coding problem. The goal is to find a sparse array `code` such that:: X ~= code * dictionary Read more in the :ref:`User Guide <SparseCoder>`. Parameters ---------- X : array of shape (n_samples, n_features) Data matrix dictionary : array of shape (n_components, n_features) The dictionary matrix against which to solve the sparse coding of the data. Some of the algorithms assume normalized rows for meaningful output. gram : array, shape=(n_components, n_components) Precomputed Gram matrix, dictionary * dictionary' cov : array, shape=(n_components, n_samples) Precomputed covariance, dictionary' * X algorithm : {'lasso_lars', 'lasso_cd', 'lars', 'omp', 'threshold'} lars: uses the least angle regression method (linear_model.lars_path) lasso_lars: uses Lars to compute the Lasso solution lasso_cd: uses the coordinate descent method to compute the Lasso solution (linear_model.Lasso). lasso_lars will be faster if the estimated components are sparse. omp: uses orthogonal matching pursuit to estimate the sparse solution threshold: squashes to zero all coefficients less than alpha from the projection dictionary * X' n_nonzero_coefs : int, 0.1 * n_features by default Number of nonzero coefficients to target in each column of the solution. This is only used by `algorithm='lars'` and `algorithm='omp'` and is overridden by `alpha` in the `omp` case. alpha : float, 1. by default If `algorithm='lasso_lars'` or `algorithm='lasso_cd'`, `alpha` is the penalty applied to the L1 norm. If `algorithm='threshold'`, `alpha` is the absolute value of the threshold below which coefficients will be squashed to zero. If `algorithm='omp'`, `alpha` is the tolerance parameter: the value of the reconstruction error targeted. In this case, it overrides `n_nonzero_coefs`. init : array of shape (n_samples, n_components) Initialization value of the sparse codes. Only used if `algorithm='lasso_cd'`. max_iter : int, 1000 by default Maximum number of iterations to perform if `algorithm='lasso_cd'`. copy_cov : boolean, optional Whether to copy the precomputed covariance matrix; if False, it may be overwritten. n_jobs : int, optional Number of parallel jobs to run. check_input : boolean, optional If False, the input arrays X and dictionary will not be checked. verbose : int, optional Controls the verbosity; the higher, the more messages. Defaults to 0. Returns ------- code : array of shape (n_samples, n_components) The sparse codes See also -------- sklearn.linear_model.lars_path sklearn.linear_model.orthogonal_mp sklearn.linear_model.Lasso SparseCoder """ if check_input: if algorithm == 'lasso_cd': dictionary = check_array(dictionary, order='C', dtype='float64') X = check_array(X, order='C', dtype='float64') else: dictionary = check_array(dictionary) X = check_array(X) n_samples, n_features = X.shape n_components = dictionary.shape[0] if gram is None and algorithm != 'threshold': gram = np.dot(dictionary, dictionary.T) if cov is None and algorithm != 'lasso_cd': copy_cov = False cov = np.dot(dictionary, X.T) if algorithm in ('lars', 'omp'): regularization = n_nonzero_coefs if regularization is None: regularization = min(max(n_features / 10, 1), n_components) else: regularization = alpha if regularization is None: regularization = 1. if n_jobs == 1 or algorithm == 'threshold': code = _sparse_encode(X, dictionary, gram, cov=cov, algorithm=algorithm, regularization=regularization, copy_cov=copy_cov, init=init, max_iter=max_iter, check_input=False, verbose=verbose) # This ensure that dimensionality of code is always 2, # consistant with the case n_jobs > 1 if code.ndim == 1: code = code[np.newaxis, :] return code # Enter parallel code block code = np.empty((n_samples, n_components)) slices = list(gen_even_slices(n_samples, _get_n_jobs(n_jobs))) code_views = Parallel(n_jobs=n_jobs, verbose=verbose)( delayed(_sparse_encode)( X[this_slice], dictionary, gram, cov[:, this_slice] if cov is not None else None, algorithm, regularization=regularization, copy_cov=copy_cov, init=init[this_slice] if init is not None else None, max_iter=max_iter, check_input=False) for this_slice in slices) for this_slice, this_view in zip(slices, code_views): code[this_slice] = this_view return code def _update_dict(dictionary, Y, code, verbose=False, return_r2=False, random_state=None): """Update the dense dictionary factor in place. Parameters ---------- dictionary : array of shape (n_features, n_components) Value of the dictionary at the previous iteration. Y : array of shape (n_features, n_samples) Data matrix. code : array of shape (n_components, n_samples) Sparse coding of the data against which to optimize the dictionary. verbose: Degree of output the procedure will print. return_r2 : bool Whether to compute and return the residual sum of squares corresponding to the computed solution. random_state : int or RandomState Pseudo number generator state used for random sampling. Returns ------- dictionary : array of shape (n_features, n_components) Updated dictionary. """ n_components = len(code) n_samples = Y.shape[0] random_state = check_random_state(random_state) # Residuals, computed 'in-place' for efficiency R = -np.dot(dictionary, code) R += Y R = np.asfortranarray(R) ger, = linalg.get_blas_funcs(('ger',), (dictionary, code)) for k in range(n_components): # R <- 1.0 * U_k * V_k^T + R R = ger(1.0, dictionary[:, k], code[k, :], a=R, overwrite_a=True) dictionary[:, k] = np.dot(R, code[k, :].T) # Scale k'th atom atom_norm_square = np.dot(dictionary[:, k], dictionary[:, k]) if atom_norm_square < 1e-20: if verbose == 1: sys.stdout.write("+") sys.stdout.flush() elif verbose: print("Adding new random atom") dictionary[:, k] = random_state.randn(n_samples) # Setting corresponding coefs to 0 code[k, :] = 0.0 dictionary[:, k] /= sqrt(np.dot(dictionary[:, k], dictionary[:, k])) else: dictionary[:, k] /= sqrt(atom_norm_square) # R <- -1.0 * U_k * V_k^T + R R = ger(-1.0, dictionary[:, k], code[k, :], a=R, overwrite_a=True) if return_r2: R **= 2 # R is fortran-ordered. For numpy version < 1.6, sum does not # follow the quick striding first, and is thus inefficient on # fortran ordered data. We take a flat view of the data with no # striding R = as_strided(R, shape=(R.size, ), strides=(R.dtype.itemsize,)) R = np.sum(R) return dictionary, R return dictionary def dict_learning(X, n_components, alpha, max_iter=100, tol=1e-8, method='lars', n_jobs=1, dict_init=None, code_init=None, callback=None, verbose=False, random_state=None, return_n_iter=False): """Solves a dictionary learning matrix factorization problem. Finds the best dictionary and the corresponding sparse code for approximating the data matrix X by solving:: (U^*, V^*) = argmin 0.5 || X - U V ||_2^2 + alpha * || U ||_1 (U,V) with || V_k ||_2 = 1 for all 0 <= k < n_components where V is the dictionary and U is the sparse code. Read more in the :ref:`User Guide <DictionaryLearning>`. Parameters ---------- X : array of shape (n_samples, n_features) Data matrix. n_components : int, Number of dictionary atoms to extract. alpha : int, Sparsity controlling parameter. max_iter : int, Maximum number of iterations to perform. tol : float, Tolerance for the stopping condition. method : {'lars', 'cd'} lars: uses the least angle regression method to solve the lasso problem (linear_model.lars_path) cd: uses the coordinate descent method to compute the Lasso solution (linear_model.Lasso). Lars will be faster if the estimated components are sparse. n_jobs : int, Number of parallel jobs to run, or -1 to autodetect. dict_init : array of shape (n_components, n_features), Initial value for the dictionary for warm restart scenarios. code_init : array of shape (n_samples, n_components), Initial value for the sparse code for warm restart scenarios. callback : Callable that gets invoked every five iterations. verbose : Degree of output the procedure will print. random_state : int or RandomState Pseudo number generator state used for random sampling. return_n_iter : bool Whether or not to return the number of iterations. Returns ------- code : array of shape (n_samples, n_components) The sparse code factor in the matrix factorization. dictionary : array of shape (n_components, n_features), The dictionary factor in the matrix factorization. errors : array Vector of errors at each iteration. n_iter : int Number of iterations run. Returned only if `return_n_iter` is set to True. See also -------- dict_learning_online DictionaryLearning MiniBatchDictionaryLearning SparsePCA MiniBatchSparsePCA """ if method not in ('lars', 'cd'): raise ValueError('Coding method %r not supported as a fit algorithm.' % method) method = 'lasso_' + method t0 = time.time() # Avoid integer division problems alpha = float(alpha) random_state = check_random_state(random_state) if n_jobs == -1: n_jobs = cpu_count() # Init the code and the dictionary with SVD of Y if code_init is not None and dict_init is not None: code = np.array(code_init, order='F') # Don't copy V, it will happen below dictionary = dict_init else: code, S, dictionary = linalg.svd(X, full_matrices=False) dictionary = S[:, np.newaxis] * dictionary r = len(dictionary) if n_components <= r: # True even if n_components=None code = code[:, :n_components] dictionary = dictionary[:n_components, :] else: code = np.c_[code, np.zeros((len(code), n_components - r))] dictionary = np.r_[dictionary, np.zeros((n_components - r, dictionary.shape[1]))] # Fortran-order dict, as we are going to access its row vectors dictionary = np.array(dictionary, order='F') residuals = 0 errors = [] current_cost = np.nan if verbose == 1: print('[dict_learning]', end=' ') # If max_iter is 0, number of iterations returned should be zero ii = -1 for ii in range(max_iter): dt = (time.time() - t0) if verbose == 1: sys.stdout.write(".") sys.stdout.flush() elif verbose: print("Iteration % 3i " "(elapsed time: % 3is, % 4.1fmn, current cost % 7.3f)" % (ii, dt, dt / 60, current_cost)) # Update code code = sparse_encode(X, dictionary, algorithm=method, alpha=alpha, init=code, n_jobs=n_jobs) # Update dictionary dictionary, residuals = _update_dict(dictionary.T, X.T, code.T, verbose=verbose, return_r2=True, random_state=random_state) dictionary = dictionary.T # Cost function current_cost = 0.5 * residuals + alpha * np.sum(np.abs(code)) errors.append(current_cost) if ii > 0: dE = errors[-2] - errors[-1] # assert(dE >= -tol * errors[-1]) if dE < tol * errors[-1]: if verbose == 1: # A line return print("") elif verbose: print("--- Convergence reached after %d iterations" % ii) break if ii % 5 == 0 and callback is not None: callback(locals()) if return_n_iter: return code, dictionary, errors, ii + 1 else: return code, dictionary, errors def dict_learning_online(X, n_components=2, alpha=1, n_iter=100, return_code=True, dict_init=None, callback=None, batch_size=3, verbose=False, shuffle=True, n_jobs=1, method='lars', iter_offset=0, random_state=None, return_inner_stats=False, inner_stats=None, return_n_iter=False): """Solves a dictionary learning matrix factorization problem online. Finds the best dictionary and the corresponding sparse code for approximating the data matrix X by solving:: (U^*, V^*) = argmin 0.5 || X - U V ||_2^2 + alpha * || U ||_1 (U,V) with || V_k ||_2 = 1 for all 0 <= k < n_components where V is the dictionary and U is the sparse code. This is accomplished by repeatedly iterating over mini-batches by slicing the input data. Read more in the :ref:`User Guide <DictionaryLearning>`. Parameters ---------- X : array of shape (n_samples, n_features) Data matrix. n_components : int, Number of dictionary atoms to extract. alpha : float, Sparsity controlling parameter. n_iter : int, Number of iterations to perform. return_code : boolean, Whether to also return the code U or just the dictionary V. dict_init : array of shape (n_components, n_features), Initial value for the dictionary for warm restart scenarios. callback : Callable that gets invoked every five iterations. batch_size : int, The number of samples to take in each batch. verbose : Degree of output the procedure will print. shuffle : boolean, Whether to shuffle the data before splitting it in batches. n_jobs : int, Number of parallel jobs to run, or -1 to autodetect. method : {'lars', 'cd'} lars: uses the least angle regression method to solve the lasso problem (linear_model.lars_path) cd: uses the coordinate descent method to compute the Lasso solution (linear_model.Lasso). Lars will be faster if the estimated components are sparse. iter_offset : int, default 0 Number of previous iterations completed on the dictionary used for initialization. random_state : int or RandomState Pseudo number generator state used for random sampling. return_inner_stats : boolean, optional Return the inner statistics A (dictionary covariance) and B (data approximation). Useful to restart the algorithm in an online setting. If return_inner_stats is True, return_code is ignored inner_stats : tuple of (A, B) ndarrays Inner sufficient statistics that are kept by the algorithm. Passing them at initialization is useful in online settings, to avoid loosing the history of the evolution. A (n_components, n_components) is the dictionary covariance matrix. B (n_features, n_components) is the data approximation matrix return_n_iter : bool Whether or not to return the number of iterations. Returns ------- code : array of shape (n_samples, n_components), the sparse code (only returned if `return_code=True`) dictionary : array of shape (n_components, n_features), the solutions to the dictionary learning problem n_iter : int Number of iterations run. Returned only if `return_n_iter` is set to `True`. See also -------- dict_learning DictionaryLearning MiniBatchDictionaryLearning SparsePCA MiniBatchSparsePCA """ if n_components is None: n_components = X.shape[1] if method not in ('lars', 'cd'): raise ValueError('Coding method not supported as a fit algorithm.') method = 'lasso_' + method t0 = time.time() n_samples, n_features = X.shape # Avoid integer division problems alpha = float(alpha) random_state = check_random_state(random_state) if n_jobs == -1: n_jobs = cpu_count() # Init V with SVD of X if dict_init is not None: dictionary = dict_init else: _, S, dictionary = randomized_svd(X, n_components, random_state=random_state) dictionary = S[:, np.newaxis] * dictionary r = len(dictionary) if n_components <= r: dictionary = dictionary[:n_components, :] else: dictionary = np.r_[dictionary, np.zeros((n_components - r, dictionary.shape[1]))] if verbose == 1: print('[dict_learning]', end=' ') if shuffle: X_train = X.copy() random_state.shuffle(X_train) else: X_train = X dictionary = check_array(dictionary.T, order='F', dtype=np.float64, copy=False) X_train = check_array(X_train, order='C', dtype=np.float64, copy=False) batches = gen_batches(n_samples, batch_size) batches = itertools.cycle(batches) # The covariance of the dictionary if inner_stats is None: A = np.zeros((n_components, n_components)) # The data approximation B = np.zeros((n_features, n_components)) else: A = inner_stats[0].copy() B = inner_stats[1].copy() # If n_iter is zero, we need to return zero. ii = iter_offset - 1 for ii, batch in zip(range(iter_offset, iter_offset + n_iter), batches): this_X = X_train[batch] dt = (time.time() - t0) if verbose == 1: sys.stdout.write(".") sys.stdout.flush() elif verbose: if verbose > 10 or ii % ceil(100. / verbose) == 0: print ("Iteration % 3i (elapsed time: % 3is, % 4.1fmn)" % (ii, dt, dt / 60)) this_code = sparse_encode(this_X, dictionary.T, algorithm=method, alpha=alpha, n_jobs=n_jobs).T # Update the auxiliary variables if ii < batch_size - 1: theta = float((ii + 1) * batch_size) else: theta = float(batch_size ** 2 + ii + 1 - batch_size) beta = (theta + 1 - batch_size) / (theta + 1) A *= beta A += np.dot(this_code, this_code.T) B *= beta B += np.dot(this_X.T, this_code.T) # Update dictionary dictionary = _update_dict(dictionary, B, A, verbose=verbose, random_state=random_state) # XXX: Can the residuals be of any use? # Maybe we need a stopping criteria based on the amount of # modification in the dictionary if callback is not None: callback(locals()) if return_inner_stats: if return_n_iter: return dictionary.T, (A, B), ii - iter_offset + 1 else: return dictionary.T, (A, B) if return_code: if verbose > 1: print('Learning code...', end=' ') elif verbose == 1: print('|', end=' ') code = sparse_encode(X, dictionary.T, algorithm=method, alpha=alpha, n_jobs=n_jobs, check_input=False) if verbose > 1: dt = (time.time() - t0) print('done (total time: % 3is, % 4.1fmn)' % (dt, dt / 60)) if return_n_iter: return code, dictionary.T, ii - iter_offset + 1 else: return code, dictionary.T if return_n_iter: return dictionary.T, ii - iter_offset + 1 else: return dictionary.T class SparseCodingMixin(TransformerMixin): """Sparse coding mixin""" def _set_sparse_coding_params(self, n_components, transform_algorithm='omp', transform_n_nonzero_coefs=None, transform_alpha=None, split_sign=False, n_jobs=1): self.n_components = n_components self.transform_algorithm = transform_algorithm self.transform_n_nonzero_coefs = transform_n_nonzero_coefs self.transform_alpha = transform_alpha self.split_sign = split_sign self.n_jobs = n_jobs def transform(self, X, y=None): """Encode the data as a sparse combination of the dictionary atoms. Coding method is determined by the object parameter `transform_algorithm`. Parameters ---------- X : array of shape (n_samples, n_features) Test data to be transformed, must have the same number of features as the data used to train the model. Returns ------- X_new : array, shape (n_samples, n_components) Transformed data """ check_is_fitted(self, 'components_') # XXX : kwargs is not documented X = check_array(X) n_samples, n_features = X.shape code = sparse_encode( X, self.components_, algorithm=self.transform_algorithm, n_nonzero_coefs=self.transform_n_nonzero_coefs, alpha=self.transform_alpha, n_jobs=self.n_jobs) if self.split_sign: # feature vector is split into a positive and negative side n_samples, n_features = code.shape split_code = np.empty((n_samples, 2 * n_features)) split_code[:, :n_features] = np.maximum(code, 0) split_code[:, n_features:] = -np.minimum(code, 0) code = split_code return code class SparseCoder(BaseEstimator, SparseCodingMixin): """Sparse coding Finds a sparse representation of data against a fixed, precomputed dictionary. Each row of the result is the solution to a sparse coding problem. The goal is to find a sparse array `code` such that:: X ~= code * dictionary Read more in the :ref:`User Guide <SparseCoder>`. Parameters ---------- dictionary : array, [n_components, n_features] The dictionary atoms used for sparse coding. Lines are assumed to be normalized to unit norm. transform_algorithm : {'lasso_lars', 'lasso_cd', 'lars', 'omp', \ 'threshold'} Algorithm used to transform the data: lars: uses the least angle regression method (linear_model.lars_path) lasso_lars: uses Lars to compute the Lasso solution lasso_cd: uses the coordinate descent method to compute the Lasso solution (linear_model.Lasso). lasso_lars will be faster if the estimated components are sparse. omp: uses orthogonal matching pursuit to estimate the sparse solution threshold: squashes to zero all coefficients less than alpha from the projection ``dictionary * X'`` transform_n_nonzero_coefs : int, ``0.1 * n_features`` by default Number of nonzero coefficients to target in each column of the solution. This is only used by `algorithm='lars'` and `algorithm='omp'` and is overridden by `alpha` in the `omp` case. transform_alpha : float, 1. by default If `algorithm='lasso_lars'` or `algorithm='lasso_cd'`, `alpha` is the penalty applied to the L1 norm. If `algorithm='threshold'`, `alpha` is the absolute value of the threshold below which coefficients will be squashed to zero. If `algorithm='omp'`, `alpha` is the tolerance parameter: the value of the reconstruction error targeted. In this case, it overrides `n_nonzero_coefs`. split_sign : bool, False by default Whether to split the sparse feature vector into the concatenation of its negative part and its positive part. This can improve the performance of downstream classifiers. n_jobs : int, number of parallel jobs to run Attributes ---------- components_ : array, [n_components, n_features] The unchanged dictionary atoms See also -------- DictionaryLearning MiniBatchDictionaryLearning SparsePCA MiniBatchSparsePCA sparse_encode """ def __init__(self, dictionary, transform_algorithm='omp', transform_n_nonzero_coefs=None, transform_alpha=None, split_sign=False, n_jobs=1): self._set_sparse_coding_params(dictionary.shape[0], transform_algorithm, transform_n_nonzero_coefs, transform_alpha, split_sign, n_jobs) self.components_ = dictionary def fit(self, X, y=None): """Do nothing and return the estimator unchanged This method is just there to implement the usual API and hence work in pipelines. """ return self class DictionaryLearning(BaseEstimator, SparseCodingMixin): """Dictionary learning Finds a dictionary (a set of atoms) that can best be used to represent data using a sparse code. Solves the optimization problem:: (U^*,V^*) = argmin 0.5 || Y - U V ||_2^2 + alpha * || U ||_1 (U,V) with || V_k ||_2 = 1 for all 0 <= k < n_components Read more in the :ref:`User Guide <DictionaryLearning>`. Parameters ---------- n_components : int, number of dictionary elements to extract alpha : float, sparsity controlling parameter max_iter : int, maximum number of iterations to perform tol : float, tolerance for numerical error fit_algorithm : {'lars', 'cd'} lars: uses the least angle regression method to solve the lasso problem (linear_model.lars_path) cd: uses the coordinate descent method to compute the Lasso solution (linear_model.Lasso). Lars will be faster if the estimated components are sparse. .. versionadded:: 0.17 *cd* coordinate descent method to improve speed. transform_algorithm : {'lasso_lars', 'lasso_cd', 'lars', 'omp', \ 'threshold'} Algorithm used to transform the data lars: uses the least angle regression method (linear_model.lars_path) lasso_lars: uses Lars to compute the Lasso solution lasso_cd: uses the coordinate descent method to compute the Lasso solution (linear_model.Lasso). lasso_lars will be faster if the estimated components are sparse. omp: uses orthogonal matching pursuit to estimate the sparse solution threshold: squashes to zero all coefficients less than alpha from the projection ``dictionary * X'`` .. versionadded:: 0.17 *lasso_cd* coordinate descent method to improve speed. transform_n_nonzero_coefs : int, ``0.1 * n_features`` by default Number of nonzero coefficients to target in each column of the solution. This is only used by `algorithm='lars'` and `algorithm='omp'` and is overridden by `alpha` in the `omp` case. transform_alpha : float, 1. by default If `algorithm='lasso_lars'` or `algorithm='lasso_cd'`, `alpha` is the penalty applied to the L1 norm. If `algorithm='threshold'`, `alpha` is the absolute value of the threshold below which coefficients will be squashed to zero. If `algorithm='omp'`, `alpha` is the tolerance parameter: the value of the reconstruction error targeted. In this case, it overrides `n_nonzero_coefs`. split_sign : bool, False by default Whether to split the sparse feature vector into the concatenation of its negative part and its positive part. This can improve the performance of downstream classifiers. n_jobs : int, number of parallel jobs to run code_init : array of shape (n_samples, n_components), initial value for the code, for warm restart dict_init : array of shape (n_components, n_features), initial values for the dictionary, for warm restart verbose : degree of verbosity of the printed output random_state : int or RandomState Pseudo number generator state used for random sampling. Attributes ---------- components_ : array, [n_components, n_features] dictionary atoms extracted from the data error_ : array vector of errors at each iteration n_iter_ : int Number of iterations run. Notes ----- **References:** J. Mairal, F. Bach, J. Ponce, G. Sapiro, 2009: Online dictionary learning for sparse coding (http://www.di.ens.fr/sierra/pdfs/icml09.pdf) See also -------- SparseCoder MiniBatchDictionaryLearning SparsePCA MiniBatchSparsePCA """ def __init__(self, n_components=None, alpha=1, max_iter=1000, tol=1e-8, fit_algorithm='lars', transform_algorithm='omp', transform_n_nonzero_coefs=None, transform_alpha=None, n_jobs=1, code_init=None, dict_init=None, verbose=False, split_sign=False, random_state=None): self._set_sparse_coding_params(n_components, transform_algorithm, transform_n_nonzero_coefs, transform_alpha, split_sign, n_jobs) self.alpha = alpha self.max_iter = max_iter self.tol = tol self.fit_algorithm = fit_algorithm self.code_init = code_init self.dict_init = dict_init self.verbose = verbose self.random_state = random_state def fit(self, X, y=None): """Fit the model from data in X. Parameters ---------- X : array-like, shape (n_samples, n_features) Training vector, where n_samples in the number of samples and n_features is the number of features. Returns ------- self : object Returns the object itself """ random_state = check_random_state(self.random_state) X = check_array(X) if self.n_components is None: n_components = X.shape[1] else: n_components = self.n_components V, U, E, self.n_iter_ = dict_learning( X, n_components, self.alpha, tol=self.tol, max_iter=self.max_iter, method=self.fit_algorithm, n_jobs=self.n_jobs, code_init=self.code_init, dict_init=self.dict_init, verbose=self.verbose, random_state=random_state, return_n_iter=True) self.components_ = U self.error_ = E return self class MiniBatchDictionaryLearning(BaseEstimator, SparseCodingMixin): """Mini-batch dictionary learning Finds a dictionary (a set of atoms) that can best be used to represent data using a sparse code. Solves the optimization problem:: (U^*,V^*) = argmin 0.5 || Y - U V ||_2^2 + alpha * || U ||_1 (U,V) with || V_k ||_2 = 1 for all 0 <= k < n_components Read more in the :ref:`User Guide <DictionaryLearning>`. Parameters ---------- n_components : int, number of dictionary elements to extract alpha : float, sparsity controlling parameter n_iter : int, total number of iterations to perform fit_algorithm : {'lars', 'cd'} lars: uses the least angle regression method to solve the lasso problem (linear_model.lars_path) cd: uses the coordinate descent method to compute the Lasso solution (linear_model.Lasso). Lars will be faster if the estimated components are sparse. transform_algorithm : {'lasso_lars', 'lasso_cd', 'lars', 'omp', \ 'threshold'} Algorithm used to transform the data. lars: uses the least angle regression method (linear_model.lars_path) lasso_lars: uses Lars to compute the Lasso solution lasso_cd: uses the coordinate descent method to compute the Lasso solution (linear_model.Lasso). lasso_lars will be faster if the estimated components are sparse. omp: uses orthogonal matching pursuit to estimate the sparse solution threshold: squashes to zero all coefficients less than alpha from the projection dictionary * X' transform_n_nonzero_coefs : int, ``0.1 * n_features`` by default Number of nonzero coefficients to target in each column of the solution. This is only used by `algorithm='lars'` and `algorithm='omp'` and is overridden by `alpha` in the `omp` case. transform_alpha : float, 1. by default If `algorithm='lasso_lars'` or `algorithm='lasso_cd'`, `alpha` is the penalty applied to the L1 norm. If `algorithm='threshold'`, `alpha` is the absolute value of the threshold below which coefficients will be squashed to zero. If `algorithm='omp'`, `alpha` is the tolerance parameter: the value of the reconstruction error targeted. In this case, it overrides `n_nonzero_coefs`. split_sign : bool, False by default Whether to split the sparse feature vector into the concatenation of its negative part and its positive part. This can improve the performance of downstream classifiers. n_jobs : int, number of parallel jobs to run dict_init : array of shape (n_components, n_features), initial value of the dictionary for warm restart scenarios verbose : degree of verbosity of the printed output batch_size : int, number of samples in each mini-batch shuffle : bool, whether to shuffle the samples before forming batches random_state : int or RandomState Pseudo number generator state used for random sampling. Attributes ---------- components_ : array, [n_components, n_features] components extracted from the data inner_stats_ : tuple of (A, B) ndarrays Internal sufficient statistics that are kept by the algorithm. Keeping them is useful in online settings, to avoid loosing the history of the evolution, but they shouldn't have any use for the end user. A (n_components, n_components) is the dictionary covariance matrix. B (n_features, n_components) is the data approximation matrix n_iter_ : int Number of iterations run. Notes ----- **References:** J. Mairal, F. Bach, J. Ponce, G. Sapiro, 2009: Online dictionary learning for sparse coding (http://www.di.ens.fr/sierra/pdfs/icml09.pdf) See also -------- SparseCoder DictionaryLearning SparsePCA MiniBatchSparsePCA """ def __init__(self, n_components=None, alpha=1, n_iter=1000, fit_algorithm='lars', n_jobs=1, batch_size=3, shuffle=True, dict_init=None, transform_algorithm='omp', transform_n_nonzero_coefs=None, transform_alpha=None, verbose=False, split_sign=False, random_state=None): self._set_sparse_coding_params(n_components, transform_algorithm, transform_n_nonzero_coefs, transform_alpha, split_sign, n_jobs) self.alpha = alpha self.n_iter = n_iter self.fit_algorithm = fit_algorithm self.dict_init = dict_init self.verbose = verbose self.shuffle = shuffle self.batch_size = batch_size self.split_sign = split_sign self.random_state = random_state def fit(self, X, y=None): """Fit the model from data in X. Parameters ---------- X : array-like, shape (n_samples, n_features) Training vector, where n_samples in the number of samples and n_features is the number of features. Returns ------- self : object Returns the instance itself. """ random_state = check_random_state(self.random_state) X = check_array(X) U, (A, B), self.n_iter_ = dict_learning_online( X, self.n_components, self.alpha, n_iter=self.n_iter, return_code=False, method=self.fit_algorithm, n_jobs=self.n_jobs, dict_init=self.dict_init, batch_size=self.batch_size, shuffle=self.shuffle, verbose=self.verbose, random_state=random_state, return_inner_stats=True, return_n_iter=True) self.components_ = U # Keep track of the state of the algorithm to be able to do # some online fitting (partial_fit) self.inner_stats_ = (A, B) self.iter_offset_ = self.n_iter return self def partial_fit(self, X, y=None, iter_offset=None): """Updates the model using the data in X as a mini-batch. Parameters ---------- X : array-like, shape (n_samples, n_features) Training vector, where n_samples in the number of samples and n_features is the number of features. iter_offset : integer, optional The number of iteration on data batches that has been performed before this call to partial_fit. This is optional: if no number is passed, the memory of the object is used. Returns ------- self : object Returns the instance itself. """ if not hasattr(self, 'random_state_'): self.random_state_ = check_random_state(self.random_state) X = check_array(X) if hasattr(self, 'components_'): dict_init = self.components_ else: dict_init = self.dict_init inner_stats = getattr(self, 'inner_stats_', None) if iter_offset is None: iter_offset = getattr(self, 'iter_offset_', 0) U, (A, B) = dict_learning_online( X, self.n_components, self.alpha, n_iter=self.n_iter, method=self.fit_algorithm, n_jobs=self.n_jobs, dict_init=dict_init, batch_size=len(X), shuffle=False, verbose=self.verbose, return_code=False, iter_offset=iter_offset, random_state=self.random_state_, return_inner_stats=True, inner_stats=inner_stats) self.components_ = U # Keep track of the state of the algorithm to be able to do # some online fitting (partial_fit) self.inner_stats_ = (A, B) self.iter_offset_ = iter_offset + self.n_iter return self
bsd-3-clause
xiaoxiamii/scikit-learn
examples/applications/plot_model_complexity_influence.py
323
6372
""" ========================== Model Complexity Influence ========================== Demonstrate how model complexity influences both prediction accuracy and computational performance. The dataset is the Boston Housing dataset (resp. 20 Newsgroups) for regression (resp. classification). For each class of models we make the model complexity vary through the choice of relevant model parameters and measure the influence on both computational performance (latency) and predictive power (MSE or Hamming Loss). """ print(__doc__) # Author: Eustache Diemert <eustache@diemert.fr> # License: BSD 3 clause import time import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1.parasite_axes import host_subplot from mpl_toolkits.axisartist.axislines import Axes from scipy.sparse.csr import csr_matrix from sklearn import datasets from sklearn.utils import shuffle from sklearn.metrics import mean_squared_error from sklearn.svm.classes import NuSVR from sklearn.ensemble.gradient_boosting import GradientBoostingRegressor from sklearn.linear_model.stochastic_gradient import SGDClassifier from sklearn.metrics import hamming_loss ############################################################################### # Routines # initialize random generator np.random.seed(0) def generate_data(case, sparse=False): """Generate regression/classification data.""" bunch = None if case == 'regression': bunch = datasets.load_boston() elif case == 'classification': bunch = datasets.fetch_20newsgroups_vectorized(subset='all') X, y = shuffle(bunch.data, bunch.target) offset = int(X.shape[0] * 0.8) X_train, y_train = X[:offset], y[:offset] X_test, y_test = X[offset:], y[offset:] if sparse: X_train = csr_matrix(X_train) X_test = csr_matrix(X_test) else: X_train = np.array(X_train) X_test = np.array(X_test) y_test = np.array(y_test) y_train = np.array(y_train) data = {'X_train': X_train, 'X_test': X_test, 'y_train': y_train, 'y_test': y_test} return data def benchmark_influence(conf): """ Benchmark influence of :changing_param: on both MSE and latency. """ prediction_times = [] prediction_powers = [] complexities = [] for param_value in conf['changing_param_values']: conf['tuned_params'][conf['changing_param']] = param_value estimator = conf['estimator'](**conf['tuned_params']) print("Benchmarking %s" % estimator) estimator.fit(conf['data']['X_train'], conf['data']['y_train']) conf['postfit_hook'](estimator) complexity = conf['complexity_computer'](estimator) complexities.append(complexity) start_time = time.time() for _ in range(conf['n_samples']): y_pred = estimator.predict(conf['data']['X_test']) elapsed_time = (time.time() - start_time) / float(conf['n_samples']) prediction_times.append(elapsed_time) pred_score = conf['prediction_performance_computer']( conf['data']['y_test'], y_pred) prediction_powers.append(pred_score) print("Complexity: %d | %s: %.4f | Pred. Time: %fs\n" % ( complexity, conf['prediction_performance_label'], pred_score, elapsed_time)) return prediction_powers, prediction_times, complexities def plot_influence(conf, mse_values, prediction_times, complexities): """ Plot influence of model complexity on both accuracy and latency. """ plt.figure(figsize=(12, 6)) host = host_subplot(111, axes_class=Axes) plt.subplots_adjust(right=0.75) par1 = host.twinx() host.set_xlabel('Model Complexity (%s)' % conf['complexity_label']) y1_label = conf['prediction_performance_label'] y2_label = "Time (s)" host.set_ylabel(y1_label) par1.set_ylabel(y2_label) p1, = host.plot(complexities, mse_values, 'b-', label="prediction error") p2, = par1.plot(complexities, prediction_times, 'r-', label="latency") host.legend(loc='upper right') host.axis["left"].label.set_color(p1.get_color()) par1.axis["right"].label.set_color(p2.get_color()) plt.title('Influence of Model Complexity - %s' % conf['estimator'].__name__) plt.show() def _count_nonzero_coefficients(estimator): a = estimator.coef_.toarray() return np.count_nonzero(a) ############################################################################### # main code regression_data = generate_data('regression') classification_data = generate_data('classification', sparse=True) configurations = [ {'estimator': SGDClassifier, 'tuned_params': {'penalty': 'elasticnet', 'alpha': 0.001, 'loss': 'modified_huber', 'fit_intercept': True}, 'changing_param': 'l1_ratio', 'changing_param_values': [0.25, 0.5, 0.75, 0.9], 'complexity_label': 'non_zero coefficients', 'complexity_computer': _count_nonzero_coefficients, 'prediction_performance_computer': hamming_loss, 'prediction_performance_label': 'Hamming Loss (Misclassification Ratio)', 'postfit_hook': lambda x: x.sparsify(), 'data': classification_data, 'n_samples': 30}, {'estimator': NuSVR, 'tuned_params': {'C': 1e3, 'gamma': 2 ** -15}, 'changing_param': 'nu', 'changing_param_values': [0.1, 0.25, 0.5, 0.75, 0.9], 'complexity_label': 'n_support_vectors', 'complexity_computer': lambda x: len(x.support_vectors_), 'data': regression_data, 'postfit_hook': lambda x: x, 'prediction_performance_computer': mean_squared_error, 'prediction_performance_label': 'MSE', 'n_samples': 30}, {'estimator': GradientBoostingRegressor, 'tuned_params': {'loss': 'ls'}, 'changing_param': 'n_estimators', 'changing_param_values': [10, 50, 100, 200, 500], 'complexity_label': 'n_trees', 'complexity_computer': lambda x: x.n_estimators, 'data': regression_data, 'postfit_hook': lambda x: x, 'prediction_performance_computer': mean_squared_error, 'prediction_performance_label': 'MSE', 'n_samples': 30}, ] for conf in configurations: prediction_performances, prediction_times, complexities = \ benchmark_influence(conf) plot_influence(conf, prediction_performances, prediction_times, complexities)
bsd-3-clause
costypetrisor/scikit-learn
examples/tree/plot_tree_regression_multioutput.py
43
1791
""" =================================================================== Multi-output Decision Tree Regression =================================================================== An example to illustrate multi-output regression with decision tree. The :ref:`decision trees <tree>` is used to predict simultaneously the noisy x and y observations of a circle given a single underlying feature. As a result, it learns local linear regressions approximating the circle. We can see that if the maximum depth of the tree (controlled by the `max_depth` parameter) is set too high, the decision trees learn too fine details of the training data and learn from the noise, i.e. they overfit. """ print(__doc__) import numpy as np import matplotlib.pyplot as plt from sklearn.tree import DecisionTreeRegressor # Create a random dataset rng = np.random.RandomState(1) X = np.sort(200 * rng.rand(100, 1) - 100, axis=0) y = np.array([np.pi * np.sin(X).ravel(), np.pi * np.cos(X).ravel()]).T y[::5, :] += (0.5 - rng.rand(20, 2)) # Fit regression model clf_1 = DecisionTreeRegressor(max_depth=2) clf_2 = DecisionTreeRegressor(max_depth=5) clf_3 = DecisionTreeRegressor(max_depth=8) clf_1.fit(X, y) clf_2.fit(X, y) clf_3.fit(X, y) # Predict X_test = np.arange(-100.0, 100.0, 0.01)[:, np.newaxis] y_1 = clf_1.predict(X_test) y_2 = clf_2.predict(X_test) y_3 = clf_3.predict(X_test) # Plot the results plt.figure() plt.scatter(y[:, 0], y[:, 1], c="k", label="data") plt.scatter(y_1[:, 0], y_1[:, 1], c="g", label="max_depth=2") plt.scatter(y_2[:, 0], y_2[:, 1], c="r", label="max_depth=5") plt.scatter(y_3[:, 0], y_3[:, 1], c="b", label="max_depth=8") plt.xlim([-6, 6]) plt.ylim([-6, 6]) plt.xlabel("data") plt.ylabel("target") plt.title("Multi-output Decision Tree Regression") plt.legend() plt.show()
bsd-3-clause
sjperkins/tensorflow
tensorflow/examples/learn/multiple_gpu.py
49
3078
# Copyright 2016 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Example of using Estimator with multiple GPUs to distribute one model. This example only runs if you have multiple GPUs to assign to. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function from sklearn import cross_validation from sklearn import datasets from sklearn import metrics import tensorflow as tf layers = tf.contrib.layers learn = tf.contrib.learn def my_model(features, target): """DNN with three hidden layers, and dropout of 0.1 probability. Note: If you want to run this example with multiple GPUs, Cuda Toolkit 7.0 and CUDNN 6.5 V2 from NVIDIA need to be installed beforehand. Args: features: `Tensor` of input features. target: `Tensor` of targets. Returns: Tuple of predictions, loss and training op. """ # Convert the target to a one-hot tensor of shape (length of features, 3) and # with a on-value of 1 for each one-hot vector of length 3. target = tf.one_hot(target, 3, 1, 0) # Create three fully connected layers respectively of size 10, 20, and 10 with # each layer having a dropout probability of 0.1. normalizer_fn = layers.dropout normalizer_params = {'keep_prob': 0.5} with tf.device('/gpu:1'): features = layers.stack( features, layers.fully_connected, [10, 20, 10], normalizer_fn=normalizer_fn, normalizer_params=normalizer_params) with tf.device('/gpu:2'): # Compute logits (1 per class) and compute loss. logits = layers.fully_connected(features, 3, activation_fn=None) loss = tf.losses.softmax_cross_entropy(target, logits) # Create a tensor for training op. train_op = tf.contrib.layers.optimize_loss( loss, tf.contrib.framework.get_global_step(), optimizer='Adagrad', learning_rate=0.1) return ({ 'class': tf.argmax(logits, 1), 'prob': tf.nn.softmax(logits) }, loss, train_op) def main(unused_argv): iris = datasets.load_iris() x_train, x_test, y_train, y_test = cross_validation.train_test_split( iris.data, iris.target, test_size=0.2, random_state=42) classifier = learn.Estimator(model_fn=my_model) classifier.fit(x_train, y_train, steps=1000) y_predicted = [ p['class'] for p in classifier.predict( x_test, as_iterable=True) ] score = metrics.accuracy_score(y_test, y_predicted) print('Accuracy: {0:f}'.format(score)) if __name__ == '__main__': tf.app.run()
apache-2.0
jkitchin/jasp
jasp/jasp_bandstructure.py
3
3749
'''Calculate bandstructure diagrams in jasp''' from jasp import * import os import matplotlib.pyplot as plt from ase.dft import DOS def get_bandstructure(self, kpts_path=None, kpts_nintersections=10): """Calculate band structure along :param kpts_path: :param list kpts_path: list of tuples of (label, k-point) to calculate path on. :param int kpts_nintersections: is the number of points between points in band structures. More makes the bands smoother. See :func:`jasp_kpts.write_kpoints`. >>> from jasp import * >>> from jasp.jasp_bandstructure import * >>> with jasp('bulk/tio2/step3') as calc: ... n, bands, p = calc.get_bandstructure(kpts_path=[('$\Gamma$',[0.0, 0.0, 0.0]), ('X',[0.5, 0.5, 0.0]), ('X',[0.5, 0.5, 0.0]), ('M',[0.0, 0.5, 0.5]), ('M',[0.0, 0.5, 0.5]), ('$\Gamma$',[0.0, 0.0, 0.0])]) >>> p.savefig('images/tio2-bandstructure-dos.png') returns (npoints, band_energies, fighandle) """ kpts = [k[1] for k in kpts_path] labels = [k[0] for k in kpts_path] dos = DOS(self, width=0.2) d = dos.get_dos() e = dos.get_energies() ef = self.get_fermi_level() # run in non-selfconsistent directory cwd = os.getcwd() base, end = os.path.split(cwd) wd = cwd + '/bandstructure' self.clone(wd) with jasp(wd, kpts=kpts, kpts_nintersections=kpts_nintersections, reciprocal=True, nsw=0, # no ionic updates required isif=None, ibrion=None, debug=logging.DEBUG, icharg=11) as calc: calc.calculate() fig = plt.figure() with open('EIGENVAL') as f: line1 = f.readline() line2 = f.readline() line3 = f.readline() line4 = f.readline() comment = f.readline() unknown, npoints, nbands = [int(x) for x in f.readline().split()] blankline = f.readline() band_energies = [[] for i in range(nbands)] for i in range(npoints): x, y, z, weight = [float(x) for x in f.readline().split()] for j in range(nbands): fields = f.readline().split() id, energy = int(fields[0]), float(fields[1]) band_energies[id-1].append(energy) blankline = f.readline() f.close() ax1 = plt.subplot(121) for i in range(nbands): plt.plot(range(npoints), np.array(band_energies[i]) - ef) ax = plt.gca() ax.set_xticks([]) # no tick marks plt.xlabel('k-vector') plt.ylabel('Energy (eV)') nticks = len(labels)/2 + 1 ax.set_xticks(np.linspace(0, npoints, nticks)) L = [] L.append(labels[0]) for i in range(2, len(labels)): if i % 2 == 0: L.append(labels[i]) else: pass L.append(labels[-1]) ax.set_xticklabels(L) plt.axhline(0, c='r') plt.subplot(122, sharey=ax1) plt.plot(d, e) plt.axhline(0, c='r') plt.ylabel('energy (eV)') plt.xlabel('DOS') plt.subplots_adjust(wspace=0.26) plt.show() return (npoints, band_energies, fig) Vasp.get_bandstructure = get_bandstructure
gpl-2.0
haeusser/tensorflow
tensorflow/contrib/learn/python/learn/dataframe/tensorflow_dataframe.py
75
29377
# Copyright 2016 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """TensorFlowDataFrame implements convenience functions using TensorFlow.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import collections import csv import numpy as np from tensorflow.contrib.learn.python.learn.dataframe import dataframe as df from tensorflow.contrib.learn.python.learn.dataframe.transforms import batch from tensorflow.contrib.learn.python.learn.dataframe.transforms import csv_parser from tensorflow.contrib.learn.python.learn.dataframe.transforms import example_parser from tensorflow.contrib.learn.python.learn.dataframe.transforms import in_memory_source from tensorflow.contrib.learn.python.learn.dataframe.transforms import reader_source from tensorflow.contrib.learn.python.learn.dataframe.transforms import sparsify from tensorflow.contrib.learn.python.learn.dataframe.transforms import split_mask from tensorflow.python.client import session as sess from tensorflow.python.framework import dtypes from tensorflow.python.framework import errors from tensorflow.python.framework import ops from tensorflow.python.ops import io_ops from tensorflow.python.ops import parsing_ops from tensorflow.python.ops import variables from tensorflow.python.platform import gfile from tensorflow.python.training import coordinator from tensorflow.python.training import queue_runner as qr def _expand_file_names(filepatterns): """Takes a list of file patterns and returns a list of resolved file names.""" if not isinstance(filepatterns, (list, tuple, set)): filepatterns = [filepatterns] filenames = set() for filepattern in filepatterns: names = set(gfile.Glob(filepattern)) filenames |= names return list(filenames) def _dtype_to_nan(dtype): if dtype is dtypes.string: return b"" elif dtype.is_integer: return np.nan elif dtype.is_floating: return np.nan elif dtype is dtypes.bool: return np.nan else: raise ValueError("Can't parse type without NaN into sparse tensor: %s" % dtype) def _get_default_value(feature_spec): if isinstance(feature_spec, parsing_ops.FixedLenFeature): return feature_spec.default_value else: return _dtype_to_nan(feature_spec.dtype) class TensorFlowDataFrame(df.DataFrame): """TensorFlowDataFrame implements convenience functions using TensorFlow.""" def run(self, num_batches=None, graph=None, session=None, start_queues=True, initialize_variables=True, **kwargs): """Builds and runs the columns of the `DataFrame` and yields batches. This is a generator that yields a dictionary mapping column names to evaluated columns. Args: num_batches: the maximum number of batches to produce. If none specified, the returned value will iterate through infinite batches. graph: the `Graph` in which the `DataFrame` should be built. session: the `Session` in which to run the columns of the `DataFrame`. start_queues: if true, queues will be started before running and halted after producting `n` batches. initialize_variables: if true, variables will be initialized. **kwargs: Additional keyword arguments e.g. `num_epochs`. Yields: A dictionary, mapping column names to the values resulting from running each column for a single batch. """ if graph is None: graph = ops.get_default_graph() with graph.as_default(): if session is None: session = sess.Session() self_built = self.build(**kwargs) keys = list(self_built.keys()) cols = list(self_built.values()) if initialize_variables: if variables.local_variables(): session.run(variables.local_variables_initializer()) if variables.global_variables(): session.run(variables.global_variables_initializer()) if start_queues: coord = coordinator.Coordinator() threads = qr.start_queue_runners(sess=session, coord=coord) i = 0 while num_batches is None or i < num_batches: i += 1 try: values = session.run(cols) yield collections.OrderedDict(zip(keys, values)) except errors.OutOfRangeError: break if start_queues: coord.request_stop() coord.join(threads) def select_rows(self, boolean_series): """Returns a `DataFrame` with only the rows indicated by `boolean_series`. Note that batches may no longer have consistent size after calling `select_rows`, so the new `DataFrame` may need to be rebatched. For example: ''' filtered_df = df.select_rows(df["country"] == "jp").batch(64) ''' Args: boolean_series: a `Series` that evaluates to a boolean `Tensor`. Returns: A new `DataFrame` with the same columns as `self`, but selecting only the rows where `boolean_series` evaluated to `True`. """ result = type(self)() for key, col in self._columns.items(): try: result[key] = col.select_rows(boolean_series) except AttributeError as e: raise NotImplementedError(( "The select_rows method is not implemented for Series type {}. " "Original error: {}").format(type(col), e)) return result def split(self, index_series, proportion, batch_size=None): """Deterministically split a `DataFrame` into two `DataFrame`s. Note this split is only as deterministic as the underlying hash function; see `tf.string_to_hash_bucket_fast`. The hash function is deterministic for a given binary, but may change occasionally. The only way to achieve an absolute guarantee that the split `DataFrame`s do not change across runs is to materialize them. Note too that the allocation of a row to one partition or the other is evaluated independently for each row, so the exact number of rows in each partition is binomially distributed. Args: index_series: a `Series` of unique strings, whose hash will determine the partitioning; or the name in this `DataFrame` of such a `Series`. (This `Series` must contain strings because TensorFlow provides hash ops only for strings, and there are no number-to-string converter ops.) proportion: The proportion of the rows to select for the 'left' partition; the remaining (1 - proportion) rows form the 'right' partition. batch_size: the batch size to use when rebatching the left and right `DataFrame`s. If None (default), the `DataFrame`s are not rebatched; thus their batches will have variable sizes, according to which rows are selected from each batch of the original `DataFrame`. Returns: Two `DataFrame`s containing the partitioned rows. """ if isinstance(index_series, str): index_series = self[index_series] left_mask, = split_mask.SplitMask(proportion)(index_series) right_mask = ~left_mask left_rows = self.select_rows(left_mask) right_rows = self.select_rows(right_mask) if batch_size: left_rows = left_rows.batch(batch_size=batch_size, shuffle=False) right_rows = right_rows.batch(batch_size=batch_size, shuffle=False) return left_rows, right_rows def split_fast(self, index_series, proportion, batch_size, base_batch_size=1000): """Deterministically split a `DataFrame` into two `DataFrame`s. Note this split is only as deterministic as the underlying hash function; see `tf.string_to_hash_bucket_fast`. The hash function is deterministic for a given binary, but may change occasionally. The only way to achieve an absolute guarantee that the split `DataFrame`s do not change across runs is to materialize them. Note too that the allocation of a row to one partition or the other is evaluated independently for each row, so the exact number of rows in each partition is binomially distributed. Args: index_series: a `Series` of unique strings, whose hash will determine the partitioning; or the name in this `DataFrame` of such a `Series`. (This `Series` must contain strings because TensorFlow provides hash ops only for strings, and there are no number-to-string converter ops.) proportion: The proportion of the rows to select for the 'left' partition; the remaining (1 - proportion) rows form the 'right' partition. batch_size: the batch size to use when rebatching the left and right `DataFrame`s. If None (default), the `DataFrame`s are not rebatched; thus their batches will have variable sizes, according to which rows are selected from each batch of the original `DataFrame`. base_batch_size: the batch size to use for materialized data, prior to the split. Returns: Two `DataFrame`s containing the partitioned rows. """ if isinstance(index_series, str): index_series = self[index_series] left_mask, = split_mask.SplitMask(proportion)(index_series) right_mask = ~left_mask self["left_mask__"] = left_mask self["right_mask__"] = right_mask # TODO(soergel): instead of base_batch_size can we just do one big batch? # avoid computing the hashes twice m = self.materialize_to_memory(batch_size=base_batch_size) left_rows_df = m.select_rows(m["left_mask__"]) right_rows_df = m.select_rows(m["right_mask__"]) del left_rows_df[["left_mask__", "right_mask__"]] del right_rows_df[["left_mask__", "right_mask__"]] # avoid recomputing the split repeatedly left_rows_df = left_rows_df.materialize_to_memory(batch_size=batch_size) right_rows_df = right_rows_df.materialize_to_memory(batch_size=batch_size) return left_rows_df, right_rows_df def run_one_batch(self): """Creates a new 'Graph` and `Session` and runs a single batch. Returns: A dictionary mapping column names to numpy arrays that contain a single batch of the `DataFrame`. """ return list(self.run(num_batches=1))[0] def run_one_epoch(self): """Creates a new 'Graph` and `Session` and runs a single epoch. Naturally this makes sense only for DataFrames that fit in memory. Returns: A dictionary mapping column names to numpy arrays that contain a single epoch of the `DataFrame`. """ # batches is a list of dicts of numpy arrays batches = [b for b in self.run(num_epochs=1)] # first invert that to make a dict of lists of numpy arrays pivoted_batches = {} for k in batches[0].keys(): pivoted_batches[k] = [] for b in batches: for k, v in b.items(): pivoted_batches[k].append(v) # then concat the arrays in each column result = {k: np.concatenate(column_batches) for k, column_batches in pivoted_batches.items()} return result def materialize_to_memory(self, batch_size): unordered_dict_of_arrays = self.run_one_epoch() # there may already be an 'index' column, in which case from_ordereddict) # below will complain because it wants to generate a new one. # for now, just remove it. # TODO(soergel): preserve index history, potentially many levels deep del unordered_dict_of_arrays["index"] # the order of the columns in this dict is arbitrary; we just need it to # remain consistent. ordered_dict_of_arrays = collections.OrderedDict(unordered_dict_of_arrays) return TensorFlowDataFrame.from_ordereddict(ordered_dict_of_arrays, batch_size=batch_size) def batch(self, batch_size, shuffle=False, num_threads=1, queue_capacity=None, min_after_dequeue=None, seed=None): """Resize the batches in the `DataFrame` to the given `batch_size`. Args: batch_size: desired batch size. shuffle: whether records should be shuffled. Defaults to true. num_threads: the number of enqueueing threads. queue_capacity: capacity of the queue that will hold new batches. min_after_dequeue: minimum number of elements that can be left by a dequeue operation. Only used if `shuffle` is true. seed: passed to random shuffle operations. Only used if `shuffle` is true. Returns: A `DataFrame` with `batch_size` rows. """ column_names = list(self._columns.keys()) if shuffle: batcher = batch.ShuffleBatch(batch_size, output_names=column_names, num_threads=num_threads, queue_capacity=queue_capacity, min_after_dequeue=min_after_dequeue, seed=seed) else: batcher = batch.Batch(batch_size, output_names=column_names, num_threads=num_threads, queue_capacity=queue_capacity) batched_series = batcher(list(self._columns.values())) dataframe = type(self)() dataframe.assign(**(dict(zip(column_names, batched_series)))) return dataframe @classmethod def _from_csv_base(cls, filepatterns, get_default_values, has_header, column_names, num_threads, enqueue_size, batch_size, queue_capacity, min_after_dequeue, shuffle, seed): """Create a `DataFrame` from CSV files. If `has_header` is false, then `column_names` must be specified. If `has_header` is true and `column_names` are specified, then `column_names` overrides the names in the header. Args: filepatterns: a list of file patterns that resolve to CSV files. get_default_values: a function that produces a list of default values for each column, given the column names. has_header: whether or not the CSV files have headers. column_names: a list of names for the columns in the CSV files. num_threads: the number of readers that will work in parallel. enqueue_size: block size for each read operation. batch_size: desired batch size. queue_capacity: capacity of the queue that will store parsed lines. min_after_dequeue: minimum number of elements that can be left by a dequeue operation. Only used if `shuffle` is true. shuffle: whether records should be shuffled. Defaults to true. seed: passed to random shuffle operations. Only used if `shuffle` is true. Returns: A `DataFrame` that has columns corresponding to `features` and is filled with examples from `filepatterns`. Raises: ValueError: no files match `filepatterns`. ValueError: `features` contains the reserved name 'index'. """ filenames = _expand_file_names(filepatterns) if not filenames: raise ValueError("No matching file names.") if column_names is None: if not has_header: raise ValueError("If column_names is None, has_header must be true.") with gfile.GFile(filenames[0]) as f: column_names = csv.DictReader(f).fieldnames if "index" in column_names: raise ValueError( "'index' is reserved and can not be used for a column name.") default_values = get_default_values(column_names) reader_kwargs = {"skip_header_lines": (1 if has_header else 0)} index, value = reader_source.TextFileSource( filenames, reader_kwargs=reader_kwargs, enqueue_size=enqueue_size, batch_size=batch_size, queue_capacity=queue_capacity, shuffle=shuffle, min_after_dequeue=min_after_dequeue, num_threads=num_threads, seed=seed)() parser = csv_parser.CSVParser(column_names, default_values) parsed = parser(value) column_dict = parsed._asdict() column_dict["index"] = index dataframe = cls() dataframe.assign(**column_dict) return dataframe @classmethod def from_csv(cls, filepatterns, default_values, has_header=True, column_names=None, num_threads=1, enqueue_size=None, batch_size=32, queue_capacity=None, min_after_dequeue=None, shuffle=True, seed=None): """Create a `DataFrame` from CSV files. If `has_header` is false, then `column_names` must be specified. If `has_header` is true and `column_names` are specified, then `column_names` overrides the names in the header. Args: filepatterns: a list of file patterns that resolve to CSV files. default_values: a list of default values for each column. has_header: whether or not the CSV files have headers. column_names: a list of names for the columns in the CSV files. num_threads: the number of readers that will work in parallel. enqueue_size: block size for each read operation. batch_size: desired batch size. queue_capacity: capacity of the queue that will store parsed lines. min_after_dequeue: minimum number of elements that can be left by a dequeue operation. Only used if `shuffle` is true. shuffle: whether records should be shuffled. Defaults to true. seed: passed to random shuffle operations. Only used if `shuffle` is true. Returns: A `DataFrame` that has columns corresponding to `features` and is filled with examples from `filepatterns`. Raises: ValueError: no files match `filepatterns`. ValueError: `features` contains the reserved name 'index'. """ def get_default_values(column_names): # pylint: disable=unused-argument return default_values return cls._from_csv_base(filepatterns, get_default_values, has_header, column_names, num_threads, enqueue_size, batch_size, queue_capacity, min_after_dequeue, shuffle, seed) @classmethod def from_csv_with_feature_spec(cls, filepatterns, feature_spec, has_header=True, column_names=None, num_threads=1, enqueue_size=None, batch_size=32, queue_capacity=None, min_after_dequeue=None, shuffle=True, seed=None): """Create a `DataFrame` from CSV files, given a feature_spec. If `has_header` is false, then `column_names` must be specified. If `has_header` is true and `column_names` are specified, then `column_names` overrides the names in the header. Args: filepatterns: a list of file patterns that resolve to CSV files. feature_spec: a dict mapping column names to `FixedLenFeature` or `VarLenFeature`. has_header: whether or not the CSV files have headers. column_names: a list of names for the columns in the CSV files. num_threads: the number of readers that will work in parallel. enqueue_size: block size for each read operation. batch_size: desired batch size. queue_capacity: capacity of the queue that will store parsed lines. min_after_dequeue: minimum number of elements that can be left by a dequeue operation. Only used if `shuffle` is true. shuffle: whether records should be shuffled. Defaults to true. seed: passed to random shuffle operations. Only used if `shuffle` is true. Returns: A `DataFrame` that has columns corresponding to `features` and is filled with examples from `filepatterns`. Raises: ValueError: no files match `filepatterns`. ValueError: `features` contains the reserved name 'index'. """ def get_default_values(column_names): return [_get_default_value(feature_spec[name]) for name in column_names] dataframe = cls._from_csv_base(filepatterns, get_default_values, has_header, column_names, num_threads, enqueue_size, batch_size, queue_capacity, min_after_dequeue, shuffle, seed) # replace the dense columns with sparse ones in place in the dataframe for name in dataframe.columns(): if name != "index" and isinstance(feature_spec[name], parsing_ops.VarLenFeature): strip_value = _get_default_value(feature_spec[name]) (dataframe[name],) = sparsify.Sparsify(strip_value)(dataframe[name]) return dataframe @classmethod def from_examples(cls, filepatterns, features, reader_cls=io_ops.TFRecordReader, num_threads=1, enqueue_size=None, batch_size=32, queue_capacity=None, min_after_dequeue=None, shuffle=True, seed=None): """Create a `DataFrame` from `tensorflow.Example`s. Args: filepatterns: a list of file patterns containing `tensorflow.Example`s. features: a dict mapping feature names to `VarLenFeature` or `FixedLenFeature`. reader_cls: a subclass of `tensorflow.ReaderBase` that will be used to read the `Example`s. num_threads: the number of readers that will work in parallel. enqueue_size: block size for each read operation. batch_size: desired batch size. queue_capacity: capacity of the queue that will store parsed `Example`s min_after_dequeue: minimum number of elements that can be left by a dequeue operation. Only used if `shuffle` is true. shuffle: whether records should be shuffled. Defaults to true. seed: passed to random shuffle operations. Only used if `shuffle` is true. Returns: A `DataFrame` that has columns corresponding to `features` and is filled with `Example`s from `filepatterns`. Raises: ValueError: no files match `filepatterns`. ValueError: `features` contains the reserved name 'index'. """ filenames = _expand_file_names(filepatterns) if not filenames: raise ValueError("No matching file names.") if "index" in features: raise ValueError( "'index' is reserved and can not be used for a feature name.") index, record = reader_source.ReaderSource( reader_cls, filenames, enqueue_size=enqueue_size, batch_size=batch_size, queue_capacity=queue_capacity, shuffle=shuffle, min_after_dequeue=min_after_dequeue, num_threads=num_threads, seed=seed)() parser = example_parser.ExampleParser(features) parsed = parser(record) column_dict = parsed._asdict() column_dict["index"] = index dataframe = cls() dataframe.assign(**column_dict) return dataframe @classmethod def from_pandas(cls, pandas_dataframe, num_threads=None, enqueue_size=None, batch_size=None, queue_capacity=None, min_after_dequeue=None, shuffle=True, seed=None, data_name="pandas_data"): """Create a `tf.learn.DataFrame` from a `pandas.DataFrame`. Args: pandas_dataframe: `pandas.DataFrame` that serves as a data source. num_threads: the number of threads to use for enqueueing. enqueue_size: the number of rows to enqueue per step. batch_size: desired batch size. queue_capacity: capacity of the queue that will store parsed `Example`s min_after_dequeue: minimum number of elements that can be left by a dequeue operation. Only used if `shuffle` is true. shuffle: whether records should be shuffled. Defaults to true. seed: passed to random shuffle operations. Only used if `shuffle` is true. data_name: a scope name identifying the data. Returns: A `tf.learn.DataFrame` that contains batches drawn from the given `pandas_dataframe`. """ pandas_source = in_memory_source.PandasSource( pandas_dataframe, num_threads=num_threads, enqueue_size=enqueue_size, batch_size=batch_size, queue_capacity=queue_capacity, shuffle=shuffle, min_after_dequeue=min_after_dequeue, seed=seed, data_name=data_name) dataframe = cls() dataframe.assign(**(pandas_source()._asdict())) return dataframe @classmethod def from_numpy(cls, numpy_array, num_threads=None, enqueue_size=None, batch_size=None, queue_capacity=None, min_after_dequeue=None, shuffle=True, seed=None, data_name="numpy_data"): """Creates a `tf.learn.DataFrame` from a `numpy.ndarray`. The returned `DataFrame` contains two columns: 'index' and 'value'. The 'value' column contains a row from the array. The 'index' column contains the corresponding row number. Args: numpy_array: `numpy.ndarray` that serves as a data source. num_threads: the number of threads to use for enqueueing. enqueue_size: the number of rows to enqueue per step. batch_size: desired batch size. queue_capacity: capacity of the queue that will store parsed `Example`s min_after_dequeue: minimum number of elements that can be left by a dequeue operation. Only used if `shuffle` is true. shuffle: whether records should be shuffled. Defaults to true. seed: passed to random shuffle operations. Only used if `shuffle` is true. data_name: a scope name identifying the data. Returns: A `tf.learn.DataFrame` that contains batches drawn from the given array. """ numpy_source = in_memory_source.NumpySource( numpy_array, num_threads=num_threads, enqueue_size=enqueue_size, batch_size=batch_size, queue_capacity=queue_capacity, shuffle=shuffle, min_after_dequeue=min_after_dequeue, seed=seed, data_name=data_name) dataframe = cls() dataframe.assign(**(numpy_source()._asdict())) return dataframe @classmethod def from_ordereddict(cls, ordered_dict_of_arrays, num_threads=None, enqueue_size=None, batch_size=None, queue_capacity=None, min_after_dequeue=None, shuffle=True, seed=None, data_name="numpy_data"): """Creates a `tf.learn.DataFrame` from an `OrderedDict` of `numpy.ndarray`. The returned `DataFrame` contains a column for each key of the dict plus an extra 'index' column. The 'index' column contains the row number. Each of the other columns contains a row from the corresponding array. Args: ordered_dict_of_arrays: `OrderedDict` of `numpy.ndarray` that serves as a data source. num_threads: the number of threads to use for enqueueing. enqueue_size: the number of rows to enqueue per step. batch_size: desired batch size. queue_capacity: capacity of the queue that will store parsed `Example`s min_after_dequeue: minimum number of elements that can be left by a dequeue operation. Only used if `shuffle` is true. shuffle: whether records should be shuffled. Defaults to true. seed: passed to random shuffle operations. Only used if `shuffle` is true. data_name: a scope name identifying the data. Returns: A `tf.learn.DataFrame` that contains batches drawn from the given arrays. Raises: ValueError: `ordered_dict_of_arrays` contains the reserved name 'index'. """ numpy_source = in_memory_source.OrderedDictNumpySource( ordered_dict_of_arrays, num_threads=num_threads, enqueue_size=enqueue_size, batch_size=batch_size, queue_capacity=queue_capacity, shuffle=shuffle, min_after_dequeue=min_after_dequeue, seed=seed, data_name=data_name) dataframe = cls() dataframe.assign(**(numpy_source()._asdict())) return dataframe
apache-2.0
CondensedOtters/PHYSIX_Utils
Projects/Moog_2016-2019/CO2/CO2_NN/analysis.py
1
9175
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Sun Jun 14 05:54:11 2020 @author: mathieumoog """ import cpmd import filexyz import numpy as np import matplotlib.pyplot as plt # MSMbuilder ( lacks CK validation ) from msmbuilder.msm import MarkovStateModel from msmbuilder.msm import BayesianMarkovStateModel from msmbuilder.utils import dump # PyEMMMA ( has CK validation ) import pyemma as pe from pyemma.datasets import double_well_discrete def getDistance1Dsq( position1, position2, length): dist = position1-position2 half_length = length*0.5 if dist > half_length : dist -= length elif dist < -half_length: dist += length return dist*dist def getDistanceOrtho( positions, index1, index2, cell_lengths ): dist=0 for i in range(3): dist += getDistance1Dsq( positions[index1,i], positions[index2,i], cell_lengths[i] ) return np.sqrt(dist) def computeContactMatrix( positions, cell_lengths, cut_off ): nb_atoms = len(positions[:,0]) matrix = np.zeros(( nb_atoms, nb_atoms )) for atom in range(nb_atoms): for atom2 in range(atom+1,nb_atoms): if getDistanceOrtho( positions, atom, atom2, cell_lengths ) < cut_off : matrix[atom,atom2] = 1 matrix[atom2,atom] = 1 return matrix def computeTransitionMatrix( states, nb_states, tau, step_max ): nb_step = len(states) matrix = np.zeros((nb_states,nb_states)) for step in range( nb_step-step_max ): matrix[ states[step], states[step+tau] ] += 1 return matrix def computeChapmanKolmogorov( matrix, nb_states ): matrix_ck = np.zeros((nb_states,nb_states),dtype=float) for state_i in range( nb_states ): for state_j in range( nb_states ): for i in range(nb_states): matrix_ck[ state_i, state_j ] += matrix[state_i,i]*matrix[i,state_j] return matrix_ck volume=8.82 temperature=3000 # run_nb=1 path_sim = str( "/Users/mathieumoog/Documents/CO2/" + str(volume) + "/" + str(temperature) + "K/" # + str(run_nb) + "-run/" ) cell_lengths = np.ones(3)*volume traj_path = str( path_sim + "TRAJEC_fdb_wrapped.xyz" ) traj = filexyz.readAsArray( traj_path ) nbC=32 nbO=64 nb_atoms=nbC+nbO max_neigh=5 nb_step=len(traj[:,0,0]) cut_off = 1.75 min_stat=1000 # Build States coordC = np.zeros( (nb_step,nbC), dtype=int ) coordO = np.zeros( (nb_step,nbO), dtype=int ) for step in range(nb_step): matrix = computeContactMatrix( traj[step,:,:], cell_lengths, cut_off) for carbon in range(0,nbC): coordC[ step, carbon ] = int( sum(matrix[carbon,:]) ) for oxygen in range(nbC,nb_atoms): coordO[ step, oxygen-nbC ] = int( sum(matrix[oxygen,:]) ) c_min = coordC.min() o_min = coordO.min() # Adapting the labels to make sure they are in the 0-nb_states range coordC -= c_min coordO -= c_min msm = MarkovStateModel( lag_time=1, n_timescales=6) msm.fit( coordC[:,0] ) msm.timescales_ # Computing nb of states (max) nb_states_C = coordC.max()+1 nb_states_O = coordO.max()+1 # Computing Equilibrium States Probabilities coordC_hist = np.zeros( nb_states_C ) ones_ = np.ones((nb_step,nbC), dtype=int ) for i in range( nb_states_C ): coordC_hist[i] = sum( ones_[ coordC == i ] ) # Clean marginal states # for state in range( nb_states_C ): # if coordC_hist[state] < min_stat: # mask_to_clean = coordC[ :, : ] coordC_hist /= sum(coordC_hist[:]) # Computing Equilibrium States Probabilities, cleaning marginals ones_ = np.ones((nb_step,nbO), dtype=int ) coordO_hist = np.zeros( nb_states_O ) for i in range( nb_states_O ): coordO_hist[i] = sum( ones_[ coordO == i ] ) coordO_hist /= sum(coordO_hist[:]) # Plotting Oxygens plt.figure() plt.plot(coordC_hist,"b.-") plt.plot(coordO_hist,"r.-") plt.legend(["C states","O states"]) plt.show() dt=5*0.001 frac = 0.75 max_step=int(nb_step*frac) nb_tau_min=int(250) nb_tau_max=int(2*nb_tau_min) # Computing Transition Matrix for a given tau matrix_tot=np.zeros((nb_states_C,nb_states_C,nb_tau_max), dtype=float ) matrix_tot_ck=np.zeros((nb_states_C,nb_states_C,nb_tau_min), dtype=float ) for tau in range(nb_tau_max): matrix = np.zeros((nb_states_C,nb_states_C),dtype=float) for carbon in range(nbC): matrix += computeTransitionMatrix( coordC[:,carbon], nb_states_C, tau+1, max_step ) for state in range(nb_states_C): matrix[state,:] /= sum( matrix[state,:] ) matrix_tot[:,:,tau] = matrix[:,:] if tau < nb_tau_min: matrix_tot_ck[:,:,tau] = computeChapmanKolmogorov( matrix_tot[:,:,tau], nb_states_C ) carbon_target=3 matrix_markov = np.zeros( (4,4,nb_tau_min), dtype=float ) matrix_markov_ck = np.zeros( (4,4,nb_tau_min), dtype=float ) for tau in range(1,nb_tau_min+1): msm_matrix = MarkovStateModel( lag_time=tau, reversible_type="mle" ,n_timescales=nb_states_C, ergodic_cutoff="on", sliding_window=True, verbose=True) msm_matrix.fit( coordC[:,carbon_target] ) matrix_markov[:,:,tau-1] = msm_matrix.transmat_ for state_i in range( len(matrix_markov) ): for state_j in range( len(matrix_markov) ): for i in range( len(matrix_markov) ): matrix_markov_ck[ state_i, state_j, tau-1 ] += matrix_markov[state_i,i,tau-1]*matrix_markov[i,state_j,tau-1] # PyEMMA lags = [1,5,10,15,20,50,100,200] implied_timescales = pe.msm.its(dtrajs=coordC[:,carbon_target].tolist(),lags=lags) pe.plots.plot_implied_timescales(implied_timescales,units='time-steps', ylog=False) M = pe.msm.estimate_markov_model(dtrajs=coordC[:,carbon_target].tolist(), lag = 10 ) cktest = M.cktest(nsets=3) cktplt = pe.plots.plot_cktest(cktest) plt.figure() plt.xlabel("Time lag (ps)") plt.ylabel("P_ij, P_ij^CK") # plt.plot( np.arange(0,dt*nb_tau_max,dt*1), matrix_tot[0,0,:], "k-" ) # plt.plot( np.arange(0,dt*nb_tau_max,dt*2), matrix_tot_ck[0,0,:], "k--" ) plt.plot( np.arange(0,dt*nb_tau_min,dt*1), matrix_markov[0,0,:], "k-" ) plt.plot( np.arange(0,2*dt*nb_tau_min,dt*2), matrix_markov_ck[0,0,:], "k--" ) plt.plot( np.arange(0,dt*nb_tau_max,dt*1), matrix_tot[1,1,:], "r-" ) plt.plot( np.arange(0,dt*nb_tau_max,dt*2), matrix_tot_ck[1,1,:], "r--" ) plt.plot( np.arange(0,dt*nb_tau_min,dt*1), matrix_markov[0,1,:], "k-" ) plt.plot( np.arange(0,2*dt*nb_tau_min,dt*2), matrix_markov_ck[0,1,:], "k--" ) plt.plot( np.arange(0,dt*nb_tau_max,dt*1), matrix_tot[1,2,:], "b-" ) plt.plot( np.arange(0,dt*nb_tau_max,dt*2), matrix_tot_ck[1,2,:], "b--" ) plt.plot( np.arange(0,dt*nb_tau_min,dt*1), matrix_markov[0,2,:], "k-" ) plt.plot( np.arange(0,2*dt*nb_tau_min,dt*2), matrix_markov_ck[0,2,:], "k--" ) plt.plot( np.arange(0,dt*nb_tau_max,dt*1), matrix_tot[1,3,:], "g-" ) plt.plot( np.arange(0,dt*nb_tau_max,dt*2), matrix_tot_ck[1,3,:], "g--" ) plt.plot( np.arange(0,dt*nb_tau_min,dt*1), matrix_markov[0,3,:], "k-" ) plt.plot( np.arange(0,2*dt*nb_tau_min,dt*2), matrix_markov_ck[0,3,:], "k--" ) plt.plot( np.arange(0,dt*nb_tau_max,dt*1), matrix_tot[1,4,:], "m-" ) plt.plot( np.arange(0,dt*nb_tau_max,dt*2), matrix_tot_ck[1,4,:], "m--" ) plt.show() rmseC = np.zeros(nb_tau_min, dtype=float) for tau in range(nb_tau_min): mat = matrix_tot[:,:,2*tau]-matrix_tot_ck[:,:,tau] rmseC[tau] = sum(sum( mat*mat ))/(nb_states_C*nb_states_C) plt.figure() plt.xlabel("Time lag (ps)") plt.ylabel("RMSE C (%)") plt.plot( np.arange(0,dt*nb_tau_max,dt*2), rmseC*100 ) plt.show() matrix_tot=np.zeros((nb_states_O,nb_states_O,nb_tau_max), dtype=float ) matrix_tot_ck=np.zeros((nb_states_O,nb_states_O,nb_tau_min), dtype=float ) for tau in range(nb_tau_max): matrix = np.zeros((nb_states_O,nb_states_O),dtype=float) for carbon in range(nbC): matrix += computeTransitionMatrix( coordO[:,carbon], nb_states_O, tau, max_step ) for state in range(nb_states_O): matrix[state,:] /= sum( matrix[state,:] ) matrix_tot[:,:,tau] = matrix[:,:] if tau < nb_tau_min: matrix_tot_ck[:,:,tau] = computeChapmanKolmogorov( matrix_tot[:,:,tau], nb_states_O ) plt.figure() plt.xlabel("Time lag (ps)") plt.ylabel("P_ij, P_ij^CK") plt.plot( np.arange(0,dt*nb_tau_max,dt*1), matrix_tot[0,0,:], "k-" ) plt.plot( np.arange(0,dt*nb_tau_max,dt*2), matrix_tot_ck[0,0,:], "k--" ) plt.plot( np.arange(0,dt*nb_tau_max,dt*1), matrix_tot[1,1,:], "r-" ) plt.plot( np.arange(0,dt*nb_tau_max,dt*2), matrix_tot_ck[1,1,:], "r--" ) plt.plot( np.arange(0,dt*nb_tau_max,dt*1), matrix_tot[2,2,:], "b-" ) plt.plot( np.arange(0,dt*nb_tau_max,dt*2), matrix_tot_ck[2,2,:], "b--" ) plt.plot( np.arange(0,dt*nb_tau_max,dt*1), matrix_tot[3,3,:], "g-" ) plt.plot( np.arange(0,dt*nb_tau_max,dt*2), matrix_tot_ck[3,3,:], "g--" ) plt.show() rmseO = np.zeros(nb_tau_min, dtype=float) for tau in range(nb_tau_min): mat = matrix_tot[:,:,2*tau]-matrix_tot_ck[:,:,tau] rmseO[tau] = sum(sum( mat*mat ))/(nb_states_O*nb_states_O) plt.figure() plt.xlabel("Time lag (ps)") plt.ylabel("RMSE O (%)") plt.plot( np.arange(0,dt*nb_tau_max,dt*2), rmseO*100 ) plt.show() plt.figure() plt.xlabel("Time lag (ps)") plt.ylabel("RMSE all (%)") plt.plot( np.arange(0,dt*nb_tau_max,dt*2), (rmseO+rmseC)*100*0.5 ) plt.show()
gpl-3.0
timestocome/Test-stock-prediction-algorithms
StockMarketLinearRegression/PredictGold.py
2
3318
# http://github.com/timestocome # Attempt to predict gold prices and find outliers # http://web.ipac.caltech.edu/staff/fmasci/home/astro_refs/TestForRandomness_RunsTest.pdf import numpy as np import pandas as pd import matplotlib.pyplot as plt ###################################################################### # load data ######################################################################## # read in gold file data = pd.read_csv('data/Gold_all.csv', parse_dates=True, index_col=0) data = data[['Open']] # convert to log values #data['Open'] = np.log(data['Open']) data['Open'] = pd.to_numeric(data['Open'], errors='coerce') data['Volatility'] = data['Open'] - data['Open'].shift(1) data = data.dropna() gold_standard = data.loc[data.index < '01-01-1971'] gold = data.loc[data.index > '01-01-1971'] print(len(gold_standard), len(gold)) ######################################################################## # try to fit linear regression model from sklearn import linear_model x1 = np.arange(1, len(gold)+ 1) x2 = x1 **2 x3 = x1 **3 x4 = x1 **4 # best so far x = [x1, x2, x3, x4] x = np.reshape(x, (4, len(gold))).T print(x.shape) regression = linear_model.LinearRegression() regression.fit(x, gold['Open']) coeffs = regression.coef_ intercept = regression.intercept_ print(coeffs[0], coeffs[1]) gold['Regression'] = intercept + coeffs[0] * x1 + coeffs[1] * x2 + coeffs[2] * x3 + coeffs[3] * x4 gold['Residuals'] = gold['Open'] - gold['Regression'] std_regression = gold['Regression'].std() std_open = gold['Open'].std() ################################################################## # Run's Test, part 3 of paper gold_mean = gold['Open'].mean() runs = gold['Open'] > gold['Regression'] # convert runs data to number of runs R = 0 r_prev = runs[0] for r in runs: if r != r_prev: R += 1 r_prev = r T = len(runs) Ta = runs.sum() Tb = T - Ta E = (T + 2 * Ta * Tb) / T # expected runs V = (2 * Ta * Tb * (2*Ta*Tb - T)) / (T **2 * (T - 1)) # variance of runs Z1 = (R - E) / std_open Z2 = (R -E) / std_regression print("Run's Test Results") print("R %lf, E %lf, V %lf" % (R, E, V)) print("Z (not random if Z > +/- 2.5)", Z1, Z2) print("Regression:") print("Start date", gold.ix[-1]) print("Start step", len(x)) print("intercept", intercept) print("coeff", coeffs) ####################################################################### # predict next 12 months ~253 trading days dates = pd.bdate_range('1971-01-01', '2018-12-31') x1 = np.arange(1, len(dates) + 1) x2 = x1 **2 x3 = x1 **3 x4 = x1 **4 gold_futures = intercept + coeffs[0] * x1 + coeffs[1] * x2 + coeffs[2] * x3 + coeffs[3] * x4 std_regression = gold['Regression'].std() predicted = pd.DataFrame(data=gold_futures, index=dates) predicted.index.name = 'Date' predicted.columns = ['Open'] actual = pd.read_csv('data/Gold_all.csv', parse_dates=True, index_col=0) actual = actual.loc[actual.index > '01-01-1971'] actual = actual['Open'] plt.figure(figsize=(18, 16)) plt.plot(actual, label="Actual") plt.plot(predicted, label="Predicted") plt.plot(predicted - std_regression, label='Predicted - std') plt.plot(predicted + std_regression, label='Predicted + std') plt.legend(loc='best') plt.title("Gold 1971 - predicted 2019") plt.savefig("Gold_Predictions_2018.png") plt.show()
mit
jseabold/scikit-learn
sklearn/linear_model/tests/test_omp.py
272
7752
# Author: Vlad Niculae # Licence: BSD 3 clause import numpy as np from sklearn.utils.testing import assert_raises from sklearn.utils.testing import assert_true from sklearn.utils.testing import assert_equal from sklearn.utils.testing import assert_array_equal from sklearn.utils.testing import assert_array_almost_equal from sklearn.utils.testing import assert_warns from sklearn.utils.testing import ignore_warnings from sklearn.linear_model import (orthogonal_mp, orthogonal_mp_gram, OrthogonalMatchingPursuit, OrthogonalMatchingPursuitCV, LinearRegression) from sklearn.utils import check_random_state from sklearn.datasets import make_sparse_coded_signal n_samples, n_features, n_nonzero_coefs, n_targets = 20, 30, 5, 3 y, X, gamma = make_sparse_coded_signal(n_targets, n_features, n_samples, n_nonzero_coefs, random_state=0) G, Xy = np.dot(X.T, X), np.dot(X.T, y) # this makes X (n_samples, n_features) # and y (n_samples, 3) def test_correct_shapes(): assert_equal(orthogonal_mp(X, y[:, 0], n_nonzero_coefs=5).shape, (n_features,)) assert_equal(orthogonal_mp(X, y, n_nonzero_coefs=5).shape, (n_features, 3)) def test_correct_shapes_gram(): assert_equal(orthogonal_mp_gram(G, Xy[:, 0], n_nonzero_coefs=5).shape, (n_features,)) assert_equal(orthogonal_mp_gram(G, Xy, n_nonzero_coefs=5).shape, (n_features, 3)) def test_n_nonzero_coefs(): assert_true(np.count_nonzero(orthogonal_mp(X, y[:, 0], n_nonzero_coefs=5)) <= 5) assert_true(np.count_nonzero(orthogonal_mp(X, y[:, 0], n_nonzero_coefs=5, precompute=True)) <= 5) def test_tol(): tol = 0.5 gamma = orthogonal_mp(X, y[:, 0], tol=tol) gamma_gram = orthogonal_mp(X, y[:, 0], tol=tol, precompute=True) assert_true(np.sum((y[:, 0] - np.dot(X, gamma)) ** 2) <= tol) assert_true(np.sum((y[:, 0] - np.dot(X, gamma_gram)) ** 2) <= tol) def test_with_without_gram(): assert_array_almost_equal( orthogonal_mp(X, y, n_nonzero_coefs=5), orthogonal_mp(X, y, n_nonzero_coefs=5, precompute=True)) def test_with_without_gram_tol(): assert_array_almost_equal( orthogonal_mp(X, y, tol=1.), orthogonal_mp(X, y, tol=1., precompute=True)) def test_unreachable_accuracy(): assert_array_almost_equal( orthogonal_mp(X, y, tol=0), orthogonal_mp(X, y, n_nonzero_coefs=n_features)) assert_array_almost_equal( assert_warns(RuntimeWarning, orthogonal_mp, X, y, tol=0, precompute=True), orthogonal_mp(X, y, precompute=True, n_nonzero_coefs=n_features)) def test_bad_input(): assert_raises(ValueError, orthogonal_mp, X, y, tol=-1) assert_raises(ValueError, orthogonal_mp, X, y, n_nonzero_coefs=-1) assert_raises(ValueError, orthogonal_mp, X, y, n_nonzero_coefs=n_features + 1) assert_raises(ValueError, orthogonal_mp_gram, G, Xy, tol=-1) assert_raises(ValueError, orthogonal_mp_gram, G, Xy, n_nonzero_coefs=-1) assert_raises(ValueError, orthogonal_mp_gram, G, Xy, n_nonzero_coefs=n_features + 1) def test_perfect_signal_recovery(): idx, = gamma[:, 0].nonzero() gamma_rec = orthogonal_mp(X, y[:, 0], 5) gamma_gram = orthogonal_mp_gram(G, Xy[:, 0], 5) assert_array_equal(idx, np.flatnonzero(gamma_rec)) assert_array_equal(idx, np.flatnonzero(gamma_gram)) assert_array_almost_equal(gamma[:, 0], gamma_rec, decimal=2) assert_array_almost_equal(gamma[:, 0], gamma_gram, decimal=2) def test_estimator(): omp = OrthogonalMatchingPursuit(n_nonzero_coefs=n_nonzero_coefs) omp.fit(X, y[:, 0]) assert_equal(omp.coef_.shape, (n_features,)) assert_equal(omp.intercept_.shape, ()) assert_true(np.count_nonzero(omp.coef_) <= n_nonzero_coefs) omp.fit(X, y) assert_equal(omp.coef_.shape, (n_targets, n_features)) assert_equal(omp.intercept_.shape, (n_targets,)) assert_true(np.count_nonzero(omp.coef_) <= n_targets * n_nonzero_coefs) omp.set_params(fit_intercept=False, normalize=False) omp.fit(X, y[:, 0]) assert_equal(omp.coef_.shape, (n_features,)) assert_equal(omp.intercept_, 0) assert_true(np.count_nonzero(omp.coef_) <= n_nonzero_coefs) omp.fit(X, y) assert_equal(omp.coef_.shape, (n_targets, n_features)) assert_equal(omp.intercept_, 0) assert_true(np.count_nonzero(omp.coef_) <= n_targets * n_nonzero_coefs) def test_identical_regressors(): newX = X.copy() newX[:, 1] = newX[:, 0] gamma = np.zeros(n_features) gamma[0] = gamma[1] = 1. newy = np.dot(newX, gamma) assert_warns(RuntimeWarning, orthogonal_mp, newX, newy, 2) def test_swapped_regressors(): gamma = np.zeros(n_features) # X[:, 21] should be selected first, then X[:, 0] selected second, # which will take X[:, 21]'s place in case the algorithm does # column swapping for optimization (which is the case at the moment) gamma[21] = 1.0 gamma[0] = 0.5 new_y = np.dot(X, gamma) new_Xy = np.dot(X.T, new_y) gamma_hat = orthogonal_mp(X, new_y, 2) gamma_hat_gram = orthogonal_mp_gram(G, new_Xy, 2) assert_array_equal(np.flatnonzero(gamma_hat), [0, 21]) assert_array_equal(np.flatnonzero(gamma_hat_gram), [0, 21]) def test_no_atoms(): y_empty = np.zeros_like(y) Xy_empty = np.dot(X.T, y_empty) gamma_empty = ignore_warnings(orthogonal_mp)(X, y_empty, 1) gamma_empty_gram = ignore_warnings(orthogonal_mp)(G, Xy_empty, 1) assert_equal(np.all(gamma_empty == 0), True) assert_equal(np.all(gamma_empty_gram == 0), True) def test_omp_path(): path = orthogonal_mp(X, y, n_nonzero_coefs=5, return_path=True) last = orthogonal_mp(X, y, n_nonzero_coefs=5, return_path=False) assert_equal(path.shape, (n_features, n_targets, 5)) assert_array_almost_equal(path[:, :, -1], last) path = orthogonal_mp_gram(G, Xy, n_nonzero_coefs=5, return_path=True) last = orthogonal_mp_gram(G, Xy, n_nonzero_coefs=5, return_path=False) assert_equal(path.shape, (n_features, n_targets, 5)) assert_array_almost_equal(path[:, :, -1], last) def test_omp_return_path_prop_with_gram(): path = orthogonal_mp(X, y, n_nonzero_coefs=5, return_path=True, precompute=True) last = orthogonal_mp(X, y, n_nonzero_coefs=5, return_path=False, precompute=True) assert_equal(path.shape, (n_features, n_targets, 5)) assert_array_almost_equal(path[:, :, -1], last) def test_omp_cv(): y_ = y[:, 0] gamma_ = gamma[:, 0] ompcv = OrthogonalMatchingPursuitCV(normalize=True, fit_intercept=False, max_iter=10, cv=5) ompcv.fit(X, y_) assert_equal(ompcv.n_nonzero_coefs_, n_nonzero_coefs) assert_array_almost_equal(ompcv.coef_, gamma_) omp = OrthogonalMatchingPursuit(normalize=True, fit_intercept=False, n_nonzero_coefs=ompcv.n_nonzero_coefs_) omp.fit(X, y_) assert_array_almost_equal(ompcv.coef_, omp.coef_) def test_omp_reaches_least_squares(): # Use small simple data; it's a sanity check but OMP can stop early rng = check_random_state(0) n_samples, n_features = (10, 8) n_targets = 3 X = rng.randn(n_samples, n_features) Y = rng.randn(n_samples, n_targets) omp = OrthogonalMatchingPursuit(n_nonzero_coefs=n_features) lstsq = LinearRegression() omp.fit(X, Y) lstsq.fit(X, Y) assert_array_almost_equal(omp.coef_, lstsq.coef_)
bsd-3-clause
Ziqi-Li/bknqgis
bokeh/bokeh/plotting/helpers.py
1
24268
from __future__ import absolute_import from collections import Iterable, OrderedDict, Sequence import difflib import itertools import re import textwrap import warnings import numpy as np import sys from six import string_types, reraise from ..models import ( BoxSelectTool, BoxZoomTool, CategoricalAxis, TapTool, CrosshairTool, DataRange1d, DatetimeAxis, FactorRange, Grid, HelpTool, HoverTool, LassoSelectTool, Legend, LegendItem, LinearAxis, LogAxis, PanTool, ZoomInTool, ZoomOutTool, PolySelectTool, ContinuousTicker, SaveTool, Range, Range1d, UndoTool, RedoTool, ResetTool, ResizeTool, Tool, WheelPanTool, WheelZoomTool, ColumnarDataSource, ColumnDataSource, GlyphRenderer, LogScale, LinearScale, CategoricalScale) from ..core.properties import ColorSpec, Datetime, value, field from ..transform import stack from ..util.dependencies import import_optional from ..util.deprecation import deprecated from ..util.string import nice_join pd = import_optional('pandas') DEFAULT_PALETTE = ["#f22c40", "#5ab738", "#407ee7", "#df5320", "#00ad9c", "#c33ff3"] def _stack(stackers, spec0, spec1, **kw): for name in (spec0, spec1): if name in kw: raise ValueError("Stack property '%s' cannot appear in keyword args" % name) lengths = { len(x) for x in kw.values() if isinstance(x, (list, tuple)) } # lengths will be empty if there are no kwargs supplied at all if len(lengths) > 0: if len(lengths) != 1: raise ValueError("Keyword argument sequences for broadcasting must all be the same lengths. Got lengths: %r" % sorted(list(lengths))) if lengths.pop() != len(stackers): raise ValueError("Keyword argument sequences for broadcasting must be the same length as stackers") s0 = [] s1 = [] _kw = [] for i, val in enumerate(stackers): d = {} s0 = list(s1) s1.append(val) d[spec0] = stack(*s0) d[spec1] = stack(*s1) for k, v in kw.items(): if isinstance(v, (list, tuple)): d[k] = v[i] else: d[k] = v _kw.append(d) return _kw def get_default_color(plot=None): colors = [ "#1f77b4", "#ff7f0e", "#ffbb78", "#2ca02c", "#98df8a", "#d62728", "#ff9896", "#9467bd", "#c5b0d5", "#8c564b", "#c49c94", "#e377c2", "#f7b6d2", "#7f7f7f", "#bcbd22", "#dbdb8d", "#17becf", "#9edae5" ] if plot: renderers = plot.renderers renderers = [x for x in renderers if x.__view_model__ == "GlyphRenderer"] num_renderers = len(renderers) return colors[num_renderers] else: return colors[0] def get_default_alpha(plot=None): return 1.0 def _pop_renderer_args(kwargs): result = dict(data_source=kwargs.pop('source', ColumnDataSource())) for attr in ['name', 'x_range_name', 'y_range_name', 'level', 'view', 'visible', 'muted']: val = kwargs.pop(attr, None) if val: result[attr] = val return result def _pop_colors_and_alpha(glyphclass, kwargs, prefix="", default_alpha=1.0): """ Given a kwargs dict, a prefix, and a default value, looks for different color and alpha fields of the given prefix, and fills in the default value if it doesn't exist. """ result = dict() # TODO: The need to do this and the complexity of managing this kind of # thing throughout the codebase really suggests that we need to have # a real stylesheet class, where defaults and Types can declaratively # substitute for this kind of imperative logic. color = kwargs.pop(prefix + "color", get_default_color()) for argname in ("fill_color", "line_color"): if argname not in glyphclass.properties(): continue result[argname] = kwargs.pop(prefix + argname, color) # NOTE: text fill color should really always default to black, hard coding # this here now until the stylesheet solution exists if "text_color" in glyphclass.properties(): result["text_color"] = kwargs.pop(prefix + "text_color", "black") alpha = kwargs.pop(prefix + "alpha", default_alpha) for argname in ("fill_alpha", "line_alpha", "text_alpha"): if argname not in glyphclass.properties(): continue result[argname] = kwargs.pop(prefix + argname, alpha) return result def _get_legend_item_label(kwargs): legend = kwargs.pop('legend', None) source = kwargs.get('source') legend_item_label = None if legend: if isinstance(legend, string_types): # Do the simple thing first legend_item_label = value(legend) # But if there's a source - try and do something smart if source and hasattr(source, 'column_names'): if legend in source.column_names: legend_item_label = field(legend) else: legend_item_label = legend return legend_item_label _GLYPH_SOURCE_MSG = """ Supplying a user-defined data source AND iterable values to glyph methods is deprecated. See https://github.com/bokeh/bokeh/issues/2056 for more information. """ def _process_sequence_literals(glyphclass, kwargs, source, is_user_source): dataspecs = glyphclass.dataspecs_with_props() for var, val in kwargs.items(): # ignore things that are not iterable if not isinstance(val, Iterable): continue # pass dicts (i.e., values or fields) on as-is if isinstance(val, dict): continue # let any non-dataspecs do their own validation (e.g., line_dash properties) if var not in dataspecs: continue # strings sequences are handled by the dataspec as-is if isinstance(val, string_types): continue # similarly colorspecs handle color tuple sequences as-is if (isinstance(dataspecs[var].property, ColorSpec) and isinstance(val, tuple)): continue if isinstance(val, np.ndarray) and val.ndim != 1: raise RuntimeError("Columns need to be 1D (%s is not)" % var) if is_user_source: deprecated(_GLYPH_SOURCE_MSG) source.add(val, name=var) kwargs[var] = var def _make_glyph(glyphclass, kws, extra): if extra is None: return None kws = kws.copy() kws.update(extra) return glyphclass(**kws) def _update_legend(plot, legend_item_label, glyph_renderer): # Get the plot's legend legends = plot.select(type=Legend) if not legends: legend = Legend() plot.add_layout(legend) elif len(legends) == 1: legend = legends[0] else: raise RuntimeError("Plot %s configured with more than one legend renderer" % plot) # If there is an existing legend with a matching label, then put the # renderer on that (if the source matches). Otherwise add a new one. added = False for item in legend.items: if item.label == legend_item_label: if item.label.get('value'): item.renderers.append(glyph_renderer) added = True break if item.label.get('field') and \ glyph_renderer.data_source is item.renderers[0].data_source: item.renderers.append(glyph_renderer) added = True break if not added: new_item = LegendItem(label=legend_item_label, renderers=[glyph_renderer]) legend.items.append(new_item) def _get_range(range_input): if range_input is None: return DataRange1d() if pd and isinstance(range_input, pd.core.groupby.GroupBy): return FactorRange(factors=sorted(list(range_input.groups.keys()))) if isinstance(range_input, Range): return range_input if isinstance(range_input, Sequence): if all(isinstance(x, string_types) for x in range_input): return FactorRange(factors=list(range_input)) if len(range_input) == 2: try: return Range1d(start=range_input[0], end=range_input[1]) except ValueError: # @mattpap suggests ValidationError instead pass raise ValueError("Unrecognized range input: '%s'" % str(range_input)) def _get_scale(range_input, axis_type): if isinstance(range_input, (DataRange1d, Range1d)) and axis_type in ["linear", "datetime", "auto", None]: return LinearScale() elif isinstance(range_input, (DataRange1d, Range1d)) and axis_type == "log": return LogScale() elif isinstance(range_input, FactorRange): return CategoricalScale() else: raise ValueError("Unable to determine proper scale for: '%s'" % str(range_input)) def _get_axis_class(axis_type, range_input): if axis_type is None: return None elif axis_type == "linear": return LinearAxis elif axis_type == "log": return LogAxis elif axis_type == "datetime": return DatetimeAxis elif axis_type == "auto": if isinstance(range_input, FactorRange): return CategoricalAxis elif isinstance(range_input, Range1d): try: # Easier way to validate type of Range1d parameters Datetime.validate(Datetime(), range_input.start) return DatetimeAxis except ValueError: pass return LinearAxis else: raise ValueError("Unrecognized axis_type: '%r'" % axis_type) def _get_num_minor_ticks(axis_class, num_minor_ticks): if isinstance(num_minor_ticks, int): if num_minor_ticks <= 1: raise ValueError("num_minor_ticks must be > 1") return num_minor_ticks if num_minor_ticks is None: return 0 if num_minor_ticks == 'auto': if axis_class is LogAxis: return 10 return 5 _known_tools = { "pan": lambda: PanTool(dimensions='both'), "xpan": lambda: PanTool(dimensions='width'), "ypan": lambda: PanTool(dimensions='height'), "wheel_zoom": lambda: WheelZoomTool(dimensions='both'), "xwheel_zoom": lambda: WheelZoomTool(dimensions='width'), "ywheel_zoom": lambda: WheelZoomTool(dimensions='height'), "zoom_in": lambda: ZoomInTool(dimensions='both'), "xzoom_in": lambda: ZoomInTool(dimensions='width'), "yzoom_in": lambda: ZoomInTool(dimensions='height'), "zoom_out": lambda: ZoomOutTool(dimensions='both'), "xzoom_out": lambda: ZoomOutTool(dimensions='width'), "yzoom_out": lambda: ZoomOutTool(dimensions='height'), "xwheel_pan": lambda: WheelPanTool(dimension="width"), "ywheel_pan": lambda: WheelPanTool(dimension="height"), "resize": lambda: ResizeTool(), "click": lambda: TapTool(behavior="inspect"), "tap": lambda: TapTool(), "crosshair": lambda: CrosshairTool(), "box_select": lambda: BoxSelectTool(), "xbox_select": lambda: BoxSelectTool(dimensions='width'), "ybox_select": lambda: BoxSelectTool(dimensions='height'), "poly_select": lambda: PolySelectTool(), "lasso_select": lambda: LassoSelectTool(), "box_zoom": lambda: BoxZoomTool(dimensions='both'), "xbox_zoom": lambda: BoxZoomTool(dimensions='width'), "ybox_zoom": lambda: BoxZoomTool(dimensions='height'), "hover": lambda: HoverTool(tooltips=[ ("index", "$index"), ("data (x, y)", "($x, $y)"), ("canvas (x, y)", "($sx, $sy)"), ]), "save": lambda: SaveTool(), "previewsave": "save", "undo": lambda: UndoTool(), "redo": lambda: RedoTool(), "reset": lambda: ResetTool(), "help": lambda: HelpTool(), } def _tool_from_string(name): """ Takes a string and returns a corresponding `Tool` instance. """ known_tools = sorted(_known_tools.keys()) if name in known_tools: tool_fn = _known_tools[name] if isinstance(tool_fn, string_types): tool_fn = _known_tools[tool_fn] return tool_fn() else: matches, text = difflib.get_close_matches(name.lower(), known_tools), "similar" if not matches: matches, text = known_tools, "possible" raise ValueError("unexpected tool name '%s', %s tools are %s" % (name, text, nice_join(matches))) def _process_axis_and_grid(plot, axis_type, axis_location, minor_ticks, axis_label, rng, dim): axiscls = _get_axis_class(axis_type, rng) if axiscls: if axiscls is LogAxis: if dim == 0: plot.x_scale = LogScale() elif dim == 1: plot.y_scale = LogScale() else: raise ValueError("received invalid dimension value: %r" % dim) # this is so we can get a ticker off the axis, even if we discard it axis = axiscls(plot=plot if axis_location else None) if isinstance(axis.ticker, ContinuousTicker): axis.ticker.num_minor_ticks = _get_num_minor_ticks(axiscls, minor_ticks) axis_label = axis_label if axis_label: axis.axis_label = axis_label grid = Grid(plot=plot, dimension=dim, ticker=axis.ticker); grid if axis_location is not None: getattr(plot, axis_location).append(axis) def _process_tools_arg(plot, tools): """ Adds tools to the plot object Args: plot (Plot): instance of a plot object tools (seq[Tool or str]|str): list of tool types or string listing the tool names. Those are converted using the _tool_from_string function. I.e.: `wheel_zoom,box_zoom,reset`. Returns: list of Tools objects added to plot, map of supplied string names to tools """ tool_objs = [] tool_map = {} temp_tool_str = "" repeated_tools = [] if isinstance(tools, (list, tuple)): for tool in tools: if isinstance(tool, Tool): tool_objs.append(tool) elif isinstance(tool, string_types): temp_tool_str += tool + ',' else: raise ValueError("tool should be a string or an instance of Tool class") tools = temp_tool_str for tool in re.split(r"\s*,\s*", tools.strip()): # re.split will return empty strings; ignore them. if tool == "": continue tool_obj = _tool_from_string(tool) tool_objs.append(tool_obj) tool_map[tool] = tool_obj for typename, group in itertools.groupby( sorted([tool.__class__.__name__ for tool in tool_objs])): if len(list(group)) > 1: repeated_tools.append(typename) if repeated_tools: warnings.warn("%s are being repeated" % ",".join(repeated_tools)) return tool_objs, tool_map def _process_active_tools(toolbar, tool_map, active_drag, active_inspect, active_scroll, active_tap): """ Adds tools to the plot object Args: toolbar (Toolbar): instance of a Toolbar object tools_map (dict[str]|Tool): tool_map from _process_tools_arg active_drag (str or Tool): the tool to set active for drag active_inspect (str or Tool): the tool to set active for inspect active_scroll (str or Tool): the tool to set active for scroll active_tap (str or Tool): the tool to set active for tap Returns: None Note: This function sets properties on Toolbar """ if active_drag in ['auto', None] or isinstance(active_drag, Tool): toolbar.active_drag = active_drag elif active_drag in tool_map: toolbar.active_drag = tool_map[active_drag] else: raise ValueError("Got unknown %r for 'active_drag', which was not a string supplied in 'tools' argument" % active_drag) if active_inspect in ['auto', None] or isinstance(active_inspect, Tool) or all([isinstance(t, Tool) for t in active_inspect]): toolbar.active_inspect = active_inspect elif active_inspect in tool_map: toolbar.active_inspect = tool_map[active_inspect] else: raise ValueError("Got unknown %r for 'active_inspect', which was not a string supplied in 'tools' argument" % active_scroll) if active_scroll in ['auto', None] or isinstance(active_scroll, Tool): toolbar.active_scroll = active_scroll elif active_scroll in tool_map: toolbar.active_scroll = tool_map[active_scroll] else: raise ValueError("Got unknown %r for 'active_scroll', which was not a string supplied in 'tools' argument" % active_scroll) if active_tap in ['auto', None] or isinstance(active_tap, Tool): toolbar.active_tap = active_tap elif active_tap in tool_map: toolbar.active_tap = tool_map[active_tap] else: raise ValueError("Got unknown %r for 'active_tap', which was not a string supplied in 'tools' argument" % active_tap) def _get_argspecs(glyphclass): argspecs = OrderedDict() for arg in glyphclass._args: spec = {} descriptor = getattr(glyphclass, arg) # running python with -OO will discard docstrings -> __doc__ is None if descriptor.__doc__: spec['desc'] = "\n ".join(textwrap.dedent(descriptor.__doc__).split("\n")) else: spec['desc'] = "" spec['default'] = descriptor.class_default(glyphclass) spec['type'] = descriptor.property._sphinx_type() argspecs[arg] = spec return argspecs # This template generates the following: # # def foo(self, x, y=10, kwargs): # kwargs['x'] = x # kwargs['y'] = y # return func(self, **kwargs) _sigfunc_template = """ def %s(self, %s, **kwargs): %s return func(self, **kwargs) """ def _get_sigfunc(func_name, func, argspecs): # This code is to wrap the generic func(*args, **kw) glyph method so that # a much better signature is available to users. E.g., for ``square`` we have: # # Signature: p.square(x, y, size=4, angle=0.0, **kwargs) # # which provides descriptive names for positional args, as well as any defaults func_args_with_defaults = [] for arg, spec in argspecs.items(): if spec['default'] is None: func_args_with_defaults.append(arg) else: func_args_with_defaults.append("%s=%r" % (arg, spec['default'])) args_text = ", ".join(func_args_with_defaults) kwargs_assign_text = "\n".join(" kwargs[%r] = %s" % (x, x) for x in argspecs) func_text = _sigfunc_template % (func_name, args_text, kwargs_assign_text) func_code = compile(func_text, "fakesource", "exec") func_globals = {} eval(func_code, {"func": func}, func_globals) return func_globals[func_name] _arg_template = """ %s (%s) : %s (default: %r) """ _doc_template = """ Configure and add %s glyphs to this Figure. Args: %s Keyword Args: %s Other Parameters: alpha (float) : an alias to set all alpha keyword args at once color (Color) : an alias to set all color keyword args at once source (ColumnDataSource) : a user supplied data source legend (str) : a legend tag for this glyph x_range_name (str) : name an extra range to use for mapping x-coordinates y_range_name (str) : name an extra range to use for mapping y-coordinates level (Enum) : control the render level order for this glyph It is also possible to set the color and alpha parameters of a "nonselection" glyph. To do so, prefix any visual parameter with ``'nonselection_'``. For example, pass ``nonselection_alpha`` or ``nonselection_fill_alpha``. Returns: GlyphRenderer """ def _add_sigfunc_info(func, argspecs, glyphclass, extra_docs): func.__name__ = glyphclass.__name__.lower() omissions = {'js_event_callbacks', 'js_property_callbacks', 'subscribed_events'} kwlines = [] kws = glyphclass.properties() - set(argspecs) for kw in kws: # these are not really useful, and should also really be private, just skip them if kw in omissions: continue descriptor = getattr(glyphclass, kw) typ = descriptor.property._sphinx_type() if descriptor.__doc__: desc = "\n ".join(textwrap.dedent(descriptor.__doc__).split("\n")) else: desc = "" kwlines.append(_arg_template % (kw, typ, desc, descriptor.class_default(glyphclass))) extra_kws = getattr(glyphclass, '_extra_kws', {}) for kw, (typ, desc) in extra_kws.items(): kwlines.append(" %s (%s) : %s" % (kw, typ, desc)) kwlines.sort() arglines = [] for arg, spec in argspecs.items(): arglines.append(_arg_template % (arg, spec['type'], spec['desc'], spec['default'])) func.__doc__ = _doc_template % (func.__name__, "\n".join(arglines), "\n".join(kwlines)) if extra_docs: func.__doc__ += extra_docs def _glyph_function(glyphclass, extra_docs=None): def func(self, **kwargs): # Process legend kwargs and remove legend before we get going legend_item_label = _get_legend_item_label(kwargs) # Need to check if user source is present before _pop_renderer_args is_user_source = kwargs.get('source', None) is not None renderer_kws = _pop_renderer_args(kwargs) source = renderer_kws['data_source'] if not isinstance(source, ColumnarDataSource): try: # try converting the soruce to ColumnDataSource source = ColumnDataSource(source) except ValueError as err: msg = "Failed to auto-convert {curr_type} to ColumnDataSource.\n Original error: {err}".format( curr_type=str(type(source)), err=err.message ) reraise(ValueError, ValueError(msg), sys.exc_info()[2]) # update reddered_kws so that others can use the new source renderer_kws['data_source'] = source # handle the main glyph, need to process literals glyph_ca = _pop_colors_and_alpha(glyphclass, kwargs) _process_sequence_literals(glyphclass, kwargs, source, is_user_source) _process_sequence_literals(glyphclass, glyph_ca, source, is_user_source) # handle the nonselection glyph, we always set one nsglyph_ca = _pop_colors_and_alpha(glyphclass, kwargs, prefix='nonselection_', default_alpha=0.1) # handle the selection glyph, if any properties were given if any(x.startswith('selection_') for x in kwargs): sglyph_ca = _pop_colors_and_alpha(glyphclass, kwargs, prefix='selection_') else: sglyph_ca = None # handle the hover glyph, if any properties were given if any(x.startswith('hover_') for x in kwargs): hglyph_ca = _pop_colors_and_alpha(glyphclass, kwargs, prefix='hover_') else: hglyph_ca = None # handle the mute glyph, if any properties were given if any(x.startswith('muted_') for x in kwargs): mglyph_ca = _pop_colors_and_alpha(glyphclass, kwargs, prefix='muted_') else: mglyph_ca = None glyph = _make_glyph(glyphclass, kwargs, glyph_ca) nsglyph = _make_glyph(glyphclass, kwargs, nsglyph_ca) sglyph = _make_glyph(glyphclass, kwargs, sglyph_ca) hglyph = _make_glyph(glyphclass, kwargs, hglyph_ca) mglyph = _make_glyph(glyphclass, kwargs, mglyph_ca) glyph_renderer = GlyphRenderer(glyph=glyph, nonselection_glyph=nsglyph, selection_glyph=sglyph, hover_glyph=hglyph, muted_glyph=mglyph, **renderer_kws) if legend_item_label: _update_legend(self, legend_item_label, glyph_renderer) for tool in self.select(type=BoxSelectTool): tool.renderers.append(glyph_renderer) self.renderers.append(glyph_renderer) return glyph_renderer argspecs = _get_argspecs(glyphclass) sigfunc = _get_sigfunc(glyphclass.__name__.lower(), func, argspecs) sigfunc.glyph_method = True _add_sigfunc_info(sigfunc, argspecs, glyphclass, extra_docs) return sigfunc
gpl-2.0
FlorisHoogenboom/sklearn-helpers
tests/test_preprocessing.py
1
3095
import unittest import numpy as np import pandas as pd from sklearn_helpers.preprocessing import \ EnhancedLabelEncoder, MultiColumnLabelEncoder class EnhancedLabelEncoderTest(unittest.TestCase): def test_accepts_only_1d(self): """It should only accept only a 1d array""" ehe = EnhancedLabelEncoder() train = np.array([ [1,2], [2,1] ]) self.assertRaises(ValueError, lambda: ehe.fit(train)) # If it is flattened, it should not raise. train = train.flatten() ehe.fit(train) def test_handle_unknown_error(self): """If handle_unkown is 'error' it should throw on unseen labels""" ehe = EnhancedLabelEncoder(handle_unknown='error') train = np.array(['a', 'b', 'a']) test = np.array(['a','c']) ehe.fit(train) # Check that a ValueError is raised on transform self.assertRaises(ValueError, lambda: ehe.transform(test)) def test_handle_unknown_ignore(self): """If handle_unknown is 'ignore' it should map unseen labels to a new value""" ehe = EnhancedLabelEncoder(handle_unknown='ignore') train = np.array(['a', 'b', 'a']) test = np.array(['a','c']) ehe.fit(train) # Check that the new label is mapped to the next value self.assertTrue( (np.array([0,2]) == ehe.transform(test)).all() ) class MultiColumnLabelEncoderTest(unittest.TestCase): def test_handle_ignore(self): """If handle_unknown is 'ignore' it should map unseen labels to a new value""" mce = MultiColumnLabelEncoder(handle_unknown='ignore') train = np.array([ ['a', 'b'], ['c', 'a'] ]) test = np.array([ ['a', 'd'], ['c', 'd'] ]) mce.fit(train) test_transformed = np.array([ [0.,2.], [1.,2.] ]) self.assertTrue( (mce.transform(test) == test_transformed).all() ) def test_accepts_pandas(self): """It shouold accept a Pandas dataframe""" mce = MultiColumnLabelEncoder(handle_unknown='ignore') train = pd.DataFrame( np.array([ ['a', 'b'], ['c', 'a'] ]), columns=['col1', 'col2'] ) # This should not throw mce.fit_transform(train, np.array([1,2])) def test_classes(self): """It should return classes for each column""" def test_accepts_pandas(self): """It shouold accept a Pandas dataframe""" mce = MultiColumnLabelEncoder( handle_unknown='ignore' ) train = pd.DataFrame( np.array([ ['a', 'b'], ['c', 'a'] ]), columns=['col1', 'col2'] ) mce.fit(train, np.array([1,2])) self.assertEqual( mce.classes_[0][0], 'a' ) self.assertEqual( mce.classes_[1][1], 'b' )
mit
aerrami/mtools
mtools/test/test_all_import.py
7
1549
from nose.tools import nottest, make_decorator from functools import wraps # tools without any external dependencies from mtools.mlogfilter.mlogfilter import MLogFilterTool from mtools.mlogvis.mlogvis import MLogVisTool from mtools.mloginfo.mloginfo import MLogInfoTool tools = [MLogFilterTool, MLogVisTool, MLogInfoTool] # mlaunch depends on pymongo try: from mtools.mlaunch.mlaunch import MLaunchTool tools.append(MLaunchTool) except ImportError: pass # mplotqueries depends on matplotlib try: from mtools.mplotqueries.mplotqueries import MPlotQueriesTool tools.append(MPlotQueriesTool) except ImportError: pass def all_tools(fn): """ This is a decorator for test functions, that runs a loop over all command line tool classes imported above and passes each class to the test function. To use this decorator, the test function must accept a single parameter. Example: @all_tools def test_something(tool_cls): tool = tool_cls() # test tool here ... """ @wraps(fn) # copies __name__ of the original function, nose requires the name to start with "test_" def new_func(): for tool in tools: fn(tool) return new_func def test_import_all(): """ Import all tools from mtools module. The tools that have external dependencies will only be imported if the dependencies are fulfilled. This test just passes by default because the imports are tested implicitly by loading this file. """ pass
apache-2.0
hadim/spindle_tracker
spindle_tracker/io/trackmate.py
1
5663
import itertools import xml.etree.cElementTree as et import networkx as nx import pandas as pd import numpy as np def trackmate_peak_import(trackmate_xml_path, get_tracks=False): """Import detected peaks with TrackMate Fiji plugin. Parameters ---------- trackmate_xml_path : str TrackMate XML file path. get_tracks : boolean Add tracks to label """ root = et.fromstring(open(trackmate_xml_path).read()) objects = [] object_labels = {'FRAME': 't_stamp', 'POSITION_T': 't', 'POSITION_X': 'x', 'POSITION_Y': 'y', 'POSITION_Z': 'z', 'MEAN_INTENSITY': 'I', 'ESTIMATED_DIAMETER': 'w', 'QUALITY': 'q', 'ID': 'spot_id', 'MEAN_INTENSITY': 'mean_intensity', 'MEDIAN_INTENSITY': 'median_intensity', 'MIN_INTENSITY': 'min_intensity', 'MAX_INTENSITY': 'max_intensity', 'TOTAL_INTENSITY': 'total_intensity', 'STANDARD_DEVIATION': 'std_intensity', 'CONTRAST': 'contrast', 'SNR': 'snr'} features = root.find('Model').find('FeatureDeclarations').find('SpotFeatures') features = [c.get('feature') for c in features.getchildren()] + ['ID'] spots = root.find('Model').find('AllSpots') trajs = pd.DataFrame([]) objects = [] for frame in spots.findall('SpotsInFrame'): for spot in frame.findall('Spot'): single_object = [] for label in features: single_object.append(spot.get(label)) objects.append(single_object) trajs = pd.DataFrame(objects, columns=features) trajs = trajs.astype(np.float) # Apply initial filtering initial_filter = root.find("Settings").find("InitialSpotFilter") trajs = filter_spots(trajs, name=initial_filter.get('feature'), value=float(initial_filter.get('value')), isabove=True if initial_filter.get('isabove') == 'true' else False) # Apply filters spot_filters = root.find("Settings").find("SpotFilterCollection") for spot_filter in spot_filters.findall('Filter'): trajs = filter_spots(trajs, name=spot_filter.get('feature'), value=float(spot_filter.get('value')), isabove=True if spot_filter.get('isabove') == 'true' else False) trajs = trajs.loc[:, object_labels.keys()] trajs.columns = [object_labels[k] for k in object_labels.keys()] trajs['label'] = np.arange(trajs.shape[0]) # Get tracks if get_tracks: filtered_track_ids = [int(track.get('TRACK_ID')) for track in root.find('Model').find('FilteredTracks').findall('TrackID')] new_trajs = pd.DataFrame() label_id = 0 trajs = trajs.set_index('spot_id') tracks = root.find('Model').find('AllTracks') for track in tracks.findall('Track'): track_id = int(track.get("TRACK_ID")) if track_id in filtered_track_ids: spot_ids = [(edge.get('SPOT_SOURCE_ID'), edge.get('SPOT_TARGET_ID'), edge.get('EDGE_TIME')) for edge in track.findall('Edge')] spot_ids = np.array(spot_ids).astype('float') spot_ids = pd.DataFrame(spot_ids, columns=['source', 'target', 'time']) spot_ids = spot_ids.sort_values(by='time') spot_ids = spot_ids.set_index('time') # Build graph graph = nx.Graph() for t, spot in spot_ids.iterrows(): graph.add_edge(int(spot['source']), int(spot['target']), attr_dict=dict(t=t)) # Find graph extremities by checking if number of neighbors is equal to 1 tracks_extremities = [node for node in graph.nodes() if len(graph.neighbors(node)) == 1] paths = [] # Find all possible paths between extremities for source, target in itertools.combinations(tracks_extremities, 2): # Find all path between two nodes for path in nx.all_simple_paths(graph, source=source, target=target): # Now we need to check wether this path respect the time logic contraint # edges can only go in one direction of the time # Build times vector according to path t = [] for i, node_srce in enumerate(path[:-1]): node_trgt = path[i+1] t.append(graph.edge[node_srce][node_trgt]['t']) # Will be equal to 1 if going to one time direction if len(np.unique(np.sign(np.diff(t)))) == 1: paths.append(path) # Add each individual trajectory to a new DataFrame called new_trajs for path in paths: traj = trajs.loc[path].copy() traj['label'] = label_id label_id += 1 new_trajs = new_trajs.append(traj) trajs = new_trajs trajs.set_index(['t_stamp', 'label'], inplace=True) trajs = trajs.sort_index() return trajs def filter_spots(spots, name, value, isabove): if isabove: spots = spots[spots[name] > value] else: spots = spots[spots[name] < value] return spots
bsd-3-clause
nikitasingh981/scikit-learn
examples/linear_model/plot_sparse_recovery.py
70
7486
""" ============================================================ Sparse recovery: feature selection for sparse linear models ============================================================ Given a small number of observations, we want to recover which features of X are relevant to explain y. For this :ref:`sparse linear models <l1_feature_selection>` can outperform standard statistical tests if the true model is sparse, i.e. if a small fraction of the features are relevant. As detailed in :ref:`the compressive sensing notes <compressive_sensing>`, the ability of L1-based approach to identify the relevant variables depends on the sparsity of the ground truth, the number of samples, the number of features, the conditioning of the design matrix on the signal subspace, the amount of noise, and the absolute value of the smallest non-zero coefficient [Wainwright2006] (http://statistics.berkeley.edu/sites/default/files/tech-reports/709.pdf). Here we keep all parameters constant and vary the conditioning of the design matrix. For a well-conditioned design matrix (small mutual incoherence) we are exactly in compressive sensing conditions (i.i.d Gaussian sensing matrix), and L1-recovery with the Lasso performs very well. For an ill-conditioned matrix (high mutual incoherence), regressors are very correlated, and the Lasso randomly selects one. However, randomized-Lasso can recover the ground truth well. In each situation, we first vary the alpha parameter setting the sparsity of the estimated model and look at the stability scores of the randomized Lasso. This analysis, knowing the ground truth, shows an optimal regime in which relevant features stand out from the irrelevant ones. If alpha is chosen too small, non-relevant variables enter the model. On the opposite, if alpha is selected too large, the Lasso is equivalent to stepwise regression, and thus brings no advantage over a univariate F-test. In a second time, we set alpha and compare the performance of different feature selection methods, using the area under curve (AUC) of the precision-recall. """ print(__doc__) # Author: Alexandre Gramfort and Gael Varoquaux # License: BSD 3 clause import warnings import matplotlib.pyplot as plt import numpy as np from scipy import linalg from sklearn.linear_model import (RandomizedLasso, lasso_stability_path, LassoLarsCV) from sklearn.feature_selection import f_regression from sklearn.preprocessing import StandardScaler from sklearn.metrics import auc, precision_recall_curve from sklearn.ensemble import ExtraTreesRegressor from sklearn.utils.extmath import pinvh from sklearn.exceptions import ConvergenceWarning def mutual_incoherence(X_relevant, X_irelevant): """Mutual incoherence, as defined by formula (26a) of [Wainwright2006]. """ projector = np.dot(np.dot(X_irelevant.T, X_relevant), pinvh(np.dot(X_relevant.T, X_relevant))) return np.max(np.abs(projector).sum(axis=1)) for conditioning in (1, 1e-4): ########################################################################### # Simulate regression data with a correlated design n_features = 501 n_relevant_features = 3 noise_level = .2 coef_min = .2 # The Donoho-Tanner phase transition is around n_samples=25: below we # will completely fail to recover in the well-conditioned case n_samples = 25 block_size = n_relevant_features rng = np.random.RandomState(42) # The coefficients of our model coef = np.zeros(n_features) coef[:n_relevant_features] = coef_min + rng.rand(n_relevant_features) # The correlation of our design: variables correlated by blocs of 3 corr = np.zeros((n_features, n_features)) for i in range(0, n_features, block_size): corr[i:i + block_size, i:i + block_size] = 1 - conditioning corr.flat[::n_features + 1] = 1 corr = linalg.cholesky(corr) # Our design X = rng.normal(size=(n_samples, n_features)) X = np.dot(X, corr) # Keep [Wainwright2006] (26c) constant X[:n_relevant_features] /= np.abs( linalg.svdvals(X[:n_relevant_features])).max() X = StandardScaler().fit_transform(X.copy()) # The output variable y = np.dot(X, coef) y /= np.std(y) # We scale the added noise as a function of the average correlation # between the design and the output variable y += noise_level * rng.normal(size=n_samples) mi = mutual_incoherence(X[:, :n_relevant_features], X[:, n_relevant_features:]) ########################################################################### # Plot stability selection path, using a high eps for early stopping # of the path, to save computation time alpha_grid, scores_path = lasso_stability_path(X, y, random_state=42, eps=0.05) plt.figure() # We plot the path as a function of alpha/alpha_max to the power 1/3: the # power 1/3 scales the path less brutally than the log, and enables to # see the progression along the path hg = plt.plot(alpha_grid[1:] ** .333, scores_path[coef != 0].T[1:], 'r') hb = plt.plot(alpha_grid[1:] ** .333, scores_path[coef == 0].T[1:], 'k') ymin, ymax = plt.ylim() plt.xlabel(r'$(\alpha / \alpha_{max})^{1/3}$') plt.ylabel('Stability score: proportion of times selected') plt.title('Stability Scores Path - Mutual incoherence: %.1f' % mi) plt.axis('tight') plt.legend((hg[0], hb[0]), ('relevant features', 'irrelevant features'), loc='best') ########################################################################### # Plot the estimated stability scores for a given alpha # Use 6-fold cross-validation rather than the default 3-fold: it leads to # a better choice of alpha: # Stop the user warnings outputs- they are not necessary for the example # as it is specifically set up to be challenging. with warnings.catch_warnings(): warnings.simplefilter('ignore', UserWarning) warnings.simplefilter('ignore', ConvergenceWarning) lars_cv = LassoLarsCV(cv=6).fit(X, y) # Run the RandomizedLasso: we use a paths going down to .1*alpha_max # to avoid exploring the regime in which very noisy variables enter # the model alphas = np.linspace(lars_cv.alphas_[0], .1 * lars_cv.alphas_[0], 6) clf = RandomizedLasso(alpha=alphas, random_state=42).fit(X, y) trees = ExtraTreesRegressor(100).fit(X, y) # Compare with F-score F, _ = f_regression(X, y) plt.figure() for name, score in [('F-test', F), ('Stability selection', clf.scores_), ('Lasso coefs', np.abs(lars_cv.coef_)), ('Trees', trees.feature_importances_), ]: precision, recall, thresholds = precision_recall_curve(coef != 0, score) plt.semilogy(np.maximum(score / np.max(score), 1e-4), label="%s. AUC: %.3f" % (name, auc(recall, precision))) plt.plot(np.where(coef != 0)[0], [2e-4] * n_relevant_features, 'mo', label="Ground truth") plt.xlabel("Features") plt.ylabel("Score") # Plot only the 100 first coefficients plt.xlim(0, 100) plt.legend(loc='best') plt.title('Feature selection scores - Mutual incoherence: %.1f' % mi) plt.show()
bsd-3-clause
zaxtax/scikit-learn
benchmarks/bench_lasso.py
297
3305
""" Benchmarks of Lasso vs LassoLars First, we fix a training set and increase the number of samples. Then we plot the computation time as function of the number of samples. In the second benchmark, we increase the number of dimensions of the training set. Then we plot the computation time as function of the number of dimensions. In both cases, only 10% of the features are informative. """ import gc from time import time import numpy as np from sklearn.datasets.samples_generator import make_regression def compute_bench(alpha, n_samples, n_features, precompute): lasso_results = [] lars_lasso_results = [] it = 0 for ns in n_samples: for nf in n_features: it += 1 print('==================') print('Iteration %s of %s' % (it, max(len(n_samples), len(n_features)))) print('==================') n_informative = nf // 10 X, Y, coef_ = make_regression(n_samples=ns, n_features=nf, n_informative=n_informative, noise=0.1, coef=True) X /= np.sqrt(np.sum(X ** 2, axis=0)) # Normalize data gc.collect() print("- benchmarking Lasso") clf = Lasso(alpha=alpha, fit_intercept=False, precompute=precompute) tstart = time() clf.fit(X, Y) lasso_results.append(time() - tstart) gc.collect() print("- benchmarking LassoLars") clf = LassoLars(alpha=alpha, fit_intercept=False, normalize=False, precompute=precompute) tstart = time() clf.fit(X, Y) lars_lasso_results.append(time() - tstart) return lasso_results, lars_lasso_results if __name__ == '__main__': from sklearn.linear_model import Lasso, LassoLars import pylab as pl alpha = 0.01 # regularization parameter n_features = 10 list_n_samples = np.linspace(100, 1000000, 5).astype(np.int) lasso_results, lars_lasso_results = compute_bench(alpha, list_n_samples, [n_features], precompute=True) pl.figure('scikit-learn LASSO benchmark results') pl.subplot(211) pl.plot(list_n_samples, lasso_results, 'b-', label='Lasso') pl.plot(list_n_samples, lars_lasso_results, 'r-', label='LassoLars') pl.title('precomputed Gram matrix, %d features, alpha=%s' % (n_features, alpha)) pl.legend(loc='upper left') pl.xlabel('number of samples') pl.ylabel('Time (s)') pl.axis('tight') n_samples = 2000 list_n_features = np.linspace(500, 3000, 5).astype(np.int) lasso_results, lars_lasso_results = compute_bench(alpha, [n_samples], list_n_features, precompute=False) pl.subplot(212) pl.plot(list_n_features, lasso_results, 'b-', label='Lasso') pl.plot(list_n_features, lars_lasso_results, 'r-', label='LassoLars') pl.title('%d samples, alpha=%s' % (n_samples, alpha)) pl.legend(loc='upper left') pl.xlabel('number of features') pl.ylabel('Time (s)') pl.axis('tight') pl.show()
bsd-3-clause
gskielian/SimpleCV
scripts/install/win/OpenKinect/freenect-examples/demo_mp_async.py
15
1082
#!/usr/bin/env python import freenect import matplotlib.pyplot as mp import signal import frame_convert mp.ion() image_rgb = None image_depth = None keep_running = True def display_depth(dev, data, timestamp): global image_depth data = frame_convert.pretty_depth(data) mp.gray() mp.figure(1) if image_depth: image_depth.set_data(data) else: image_depth = mp.imshow(data, interpolation='nearest', animated=True) mp.draw() def display_rgb(dev, data, timestamp): global image_rgb mp.figure(2) if image_rgb: image_rgb.set_data(data) else: image_rgb = mp.imshow(data, interpolation='nearest', animated=True) mp.draw() def body(*args): if not keep_running: raise freenect.Kill def handler(signum, frame): global keep_running keep_running = False print('Press Ctrl-C in terminal to stop') signal.signal(signal.SIGINT, handler) freenect.runloop(depth=display_depth, video=display_rgb, body=body)
bsd-3-clause
YukiKita/fio
tools/hist/fiologparser_hist.py
3
15090
#!/usr/bin/env python2.7 """ Utility for converting *_clat_hist* files generated by fio into latency statistics. Example usage: $ fiologparser_hist.py *_clat_hist* end-time, samples, min, avg, median, 90%, 95%, 99%, max 1000, 15, 192, 1678.107, 1788.859, 1856.076, 1880.040, 1899.208, 1888.000 2000, 43, 152, 1642.368, 1714.099, 1816.659, 1845.552, 1888.131, 1888.000 4000, 39, 1152, 1546.962, 1545.785, 1627.192, 1640.019, 1691.204, 1744 ... @author Karl Cronburg <karl.cronburg@gmail.com> """ import os import sys import pandas import numpy as np err = sys.stderr.write def weighted_percentile(percs, vs, ws): """ Use linear interpolation to calculate the weighted percentile. Value and weight arrays are first sorted by value. The cumulative distribution function (cdf) is then computed, after which np.interp finds the two values closest to our desired weighted percentile(s) and linearly interpolates them. percs :: List of percentiles we want to calculate vs :: Array of values we are computing the percentile of ws :: Array of weights for our corresponding values return :: Array of percentiles """ idx = np.argsort(vs) vs, ws = vs[idx], ws[idx] # weights and values sorted by value cdf = 100 * (ws.cumsum() - ws / 2.0) / ws.sum() return np.interp(percs, cdf, vs) # linear interpolation def weights(start_ts, end_ts, start, end): """ Calculate weights based on fraction of sample falling in the given interval [start,end]. Weights computed using vector / array computation instead of for-loops. Note that samples with zero time length are effectively ignored (we set their weight to zero). start_ts :: Array of start times for a set of samples end_ts :: Array of end times for a set of samples start :: int end :: int return :: Array of weights """ sbounds = np.maximum(start_ts, start).astype(float) ebounds = np.minimum(end_ts, end).astype(float) ws = (ebounds - sbounds) / (end_ts - start_ts) if np.any(np.isnan(ws)): err("WARNING: zero-length sample(s) detected. Log file corrupt" " / bad time values? Ignoring these samples.\n") ws[np.where(np.isnan(ws))] = 0.0; return ws def weighted_average(vs, ws): return np.sum(vs * ws) / np.sum(ws) columns = ["end-time", "samples", "min", "avg", "median", "90%", "95%", "99%", "max"] percs = [50, 90, 95, 99] def fmt_float_list(ctx, num=1): """ Return a comma separated list of float formatters to the required number of decimal places. For instance: fmt_float_list(ctx.decimals=4, num=3) == "%.4f, %.4f, %.4f" """ return ', '.join(["%%.%df" % ctx.decimals] * num) # Default values - see beginning of main() for how we detect number columns in # the input files: __HIST_COLUMNS = 1216 __NON_HIST_COLUMNS = 3 __TOTAL_COLUMNS = __HIST_COLUMNS + __NON_HIST_COLUMNS def read_chunk(rdr, sz): """ Read the next chunk of size sz from the given reader. """ try: """ StopIteration occurs when the pandas reader is empty, and AttributeError occurs if rdr is None due to the file being empty. """ new_arr = rdr.read().values except (StopIteration, AttributeError): return None """ Extract array of just the times, and histograms matrix without times column. """ times, rws, szs = new_arr[:,0], new_arr[:,1], new_arr[:,2] hists = new_arr[:,__NON_HIST_COLUMNS:] times = times.reshape((len(times),1)) arr = np.append(times, hists, axis=1) return arr def get_min(fps, arrs): """ Find the file with the current first row with the smallest start time """ return min([fp for fp in fps if not arrs[fp] is None], key=lambda fp: arrs.get(fp)[0][0]) def histogram_generator(ctx, fps, sz): # Create a chunked pandas reader for each of the files: rdrs = {} for fp in fps: try: rdrs[fp] = pandas.read_csv(fp, dtype=int, header=None, chunksize=sz) except ValueError as e: if e.message == 'No columns to parse from file': if ctx.warn: sys.stderr.write("WARNING: Empty input file encountered.\n") rdrs[fp] = None else: raise(e) # Initial histograms from disk: arrs = {fp: read_chunk(rdr, sz) for fp,rdr in rdrs.items()} while True: try: """ ValueError occurs when nothing more to read """ fp = get_min(fps, arrs) except ValueError: return arr = arrs[fp] yield np.insert(arr[0], 1, fps.index(fp)) arrs[fp] = arr[1:] if arrs[fp].shape[0] == 0: arrs[fp] = read_chunk(rdrs[fp], sz) def _plat_idx_to_val(idx, edge=0.5, FIO_IO_U_PLAT_BITS=6, FIO_IO_U_PLAT_VAL=64): """ Taken from fio's stat.c for calculating the latency value of a bin from that bin's index. idx : the value of the index into the histogram bins edge : fractional value in the range [0,1]** indicating how far into the bin we wish to compute the latency value of. ** edge = 0.0 and 1.0 computes the lower and upper latency bounds respectively of the given bin index. """ # MSB <= (FIO_IO_U_PLAT_BITS-1), cannot be rounded off. Use # all bits of the sample as index if (idx < (FIO_IO_U_PLAT_VAL << 1)): return idx # Find the group and compute the minimum value of that group error_bits = (idx >> FIO_IO_U_PLAT_BITS) - 1 base = 1 << (error_bits + FIO_IO_U_PLAT_BITS) # Find its bucket number of the group k = idx % FIO_IO_U_PLAT_VAL # Return the mean (if edge=0.5) of the range of the bucket return base + ((k + edge) * (1 << error_bits)) def plat_idx_to_val_coarse(idx, coarseness, edge=0.5): """ Converts the given *coarse* index into a non-coarse index as used by fio in stat.h:plat_idx_to_val(), subsequently computing the appropriate latency value for that bin. """ # Multiply the index by the power of 2 coarseness to get the bin # bin index with a max of 1536 bins (FIO_IO_U_PLAT_GROUP_NR = 24 in stat.h) stride = 1 << coarseness idx = idx * stride lower = _plat_idx_to_val(idx, edge=0.0) upper = _plat_idx_to_val(idx + stride, edge=1.0) return lower + (upper - lower) * edge def print_all_stats(ctx, end, mn, ss_cnt, vs, ws, mx): ps = weighted_percentile(percs, vs, ws) avg = weighted_average(vs, ws) values = [mn, avg] + list(ps) + [mx] row = [end, ss_cnt] + map(lambda x: float(x) / ctx.divisor, values) fmt = "%d, %d, %d, " + fmt_float_list(ctx, 5) + ", %d" print (fmt % tuple(row)) def update_extreme(val, fncn, new_val): """ Calculate min / max in the presence of None values """ if val is None: return new_val else: return fncn(val, new_val) # See beginning of main() for how bin_vals are computed bin_vals = [] lower_bin_vals = [] # lower edge of each bin upper_bin_vals = [] # upper edge of each bin def process_interval(ctx, samples, iStart, iEnd): """ Construct the weighted histogram for the given interval by scanning through all the histograms and figuring out which of their bins have samples with latencies which overlap with the given interval [iStart,iEnd]. """ times, files, hists = samples[:,0], samples[:,1], samples[:,2:] iHist = np.zeros(__HIST_COLUMNS) ss_cnt = 0 # number of samples affecting this interval mn_bin_val, mx_bin_val = None, None for end_time,file,hist in zip(times,files,hists): # Only look at bins of the current histogram sample which # started before the end of the current time interval [start,end] start_times = (end_time - 0.5 * ctx.interval) - bin_vals / 1000.0 idx = np.where(start_times < iEnd) s_ts, l_bvs, u_bvs, hs = start_times[idx], lower_bin_vals[idx], upper_bin_vals[idx], hist[idx] # Increment current interval histogram by weighted values of future histogram: ws = hs * weights(s_ts, end_time, iStart, iEnd) iHist[idx] += ws # Update total number of samples affecting current interval histogram: ss_cnt += np.sum(hs) # Update min and max bin values seen if necessary: idx = np.where(hs != 0)[0] if idx.size > 0: mn_bin_val = update_extreme(mn_bin_val, min, l_bvs[max(0, idx[0] - 1)]) mx_bin_val = update_extreme(mx_bin_val, max, u_bvs[min(len(hs) - 1, idx[-1] + 1)]) if ss_cnt > 0: print_all_stats(ctx, iEnd, mn_bin_val, ss_cnt, bin_vals, iHist, mx_bin_val) def guess_max_from_bins(ctx, hist_cols): """ Try to guess the GROUP_NR from given # of histogram columns seen in an input file """ max_coarse = 8 if ctx.group_nr < 19 or ctx.group_nr > 26: bins = [ctx.group_nr * (1 << 6)] else: bins = [1216,1280,1344,1408,1472,1536,1600,1664] coarses = range(max_coarse + 1) fncn = lambda z: list(map(lambda x: z/2**x if z % 2**x == 0 else -10, coarses)) arr = np.transpose(list(map(fncn, bins))) idx = np.where(arr == hist_cols) if len(idx[1]) == 0: table = repr(arr.astype(int)).replace('-10', 'N/A').replace('array',' ') err("Unable to determine bin values from input clat_hist files. Namely \n" "the first line of file '%s' " % ctx.FILE[0] + "has %d \n" % (__TOTAL_COLUMNS,) + "columns of which we assume %d " % (hist_cols,) + "correspond to histogram bins. \n" "This number needs to be equal to one of the following numbers:\n\n" + table + "\n\n" "Possible reasons and corresponding solutions:\n" " - Input file(s) does not contain histograms.\n" " - You recompiled fio with a different GROUP_NR. If so please specify this\n" " new GROUP_NR on the command line with --group_nr\n") exit(1) return bins[idx[1][0]] def main(ctx): if ctx.job_file: try: from configparser import SafeConfigParser, NoOptionError except ImportError: from ConfigParser import SafeConfigParser, NoOptionError cp = SafeConfigParser(allow_no_value=True) with open(ctx.job_file, 'r') as fp: cp.readfp(fp) if ctx.interval is None: # Auto detect --interval value for s in cp.sections(): try: hist_msec = cp.get(s, 'log_hist_msec') if hist_msec is not None: ctx.interval = int(hist_msec) except NoOptionError: pass if ctx.interval is None: ctx.interval = 1000 # Automatically detect how many columns are in the input files, # calculate the corresponding 'coarseness' parameter used to generate # those files, and calculate the appropriate bin latency values: with open(ctx.FILE[0], 'r') as fp: global bin_vals,lower_bin_vals,upper_bin_vals,__HIST_COLUMNS,__TOTAL_COLUMNS __TOTAL_COLUMNS = len(fp.readline().split(',')) __HIST_COLUMNS = __TOTAL_COLUMNS - __NON_HIST_COLUMNS max_cols = guess_max_from_bins(ctx, __HIST_COLUMNS) coarseness = int(np.log2(float(max_cols) / __HIST_COLUMNS)) bin_vals = np.array(map(lambda x: plat_idx_to_val_coarse(x, coarseness), np.arange(__HIST_COLUMNS)), dtype=float) lower_bin_vals = np.array(map(lambda x: plat_idx_to_val_coarse(x, coarseness, 0.0), np.arange(__HIST_COLUMNS)), dtype=float) upper_bin_vals = np.array(map(lambda x: plat_idx_to_val_coarse(x, coarseness, 1.0), np.arange(__HIST_COLUMNS)), dtype=float) fps = [open(f, 'r') for f in ctx.FILE] gen = histogram_generator(ctx, fps, ctx.buff_size) print(', '.join(columns)) try: start, end = 0, ctx.interval arr = np.empty(shape=(0,__TOTAL_COLUMNS - 1)) more_data = True while more_data or len(arr) > 0: # Read up to ctx.max_latency (default 20 seconds) of data from end of current interval. while len(arr) == 0 or arr[-1][0] < ctx.max_latency * 1000 + end: try: new_arr = next(gen) except StopIteration: more_data = False break arr = np.append(arr, new_arr.reshape((1,__TOTAL_COLUMNS - 1)), axis=0) arr = arr.astype(int) if arr.size > 0: # Jump immediately to the start of the input, rounding # down to the nearest multiple of the interval (useful when --log_unix_epoch # was used to create these histograms): if start == 0 and arr[0][0] - ctx.max_latency > end: start = arr[0][0] - ctx.max_latency start = start - (start % ctx.interval) end = start + ctx.interval process_interval(ctx, arr, start, end) # Update arr to throw away samples we no longer need - samples which # end before the start of the next interval, i.e. the end of the # current interval: idx = np.where(arr[:,0] > end) arr = arr[idx] start += ctx.interval end = start + ctx.interval finally: map(lambda f: f.close(), fps) if __name__ == '__main__': import argparse p = argparse.ArgumentParser() arg = p.add_argument arg("FILE", help='space separated list of latency log filenames', nargs='+') arg('--buff_size', default=10000, type=int, help='number of samples to buffer into numpy at a time') arg('--max_latency', default=20, type=float, help='number of seconds of data to process at a time') arg('-i', '--interval', type=int, help='interval width (ms), default 1000 ms') arg('-d', '--divisor', required=False, type=int, default=1, help='divide the results by this value.') arg('--decimals', default=3, type=int, help='number of decimal places to print floats to') arg('--warn', dest='warn', action='store_true', default=False, help='print warning messages to stderr') arg('--group_nr', default=19, type=int, help='FIO_IO_U_PLAT_GROUP_NR as defined in stat.h') arg('--job-file', default=None, type=str, help='Optional argument pointing to the job file used to create the ' 'given histogram files. Useful for auto-detecting --log_hist_msec and ' '--log_unix_epoch (in fio) values.') main(p.parse_args())
gpl-2.0
h2educ/scikit-learn
examples/covariance/plot_outlier_detection.py
235
3891
""" ========================================== Outlier detection with several methods. ========================================== When the amount of contamination is known, this example illustrates two different ways of performing :ref:`outlier_detection`: - based on a robust estimator of covariance, which is assuming that the data are Gaussian distributed and performs better than the One-Class SVM in that case. - using the One-Class SVM and its ability to capture the shape of the data set, hence performing better when the data is strongly non-Gaussian, i.e. with two well-separated clusters; The ground truth about inliers and outliers is given by the points colors while the orange-filled area indicates which points are reported as inliers by each method. Here, we assume that we know the fraction of outliers in the datasets. Thus rather than using the 'predict' method of the objects, we set the threshold on the decision_function to separate out the corresponding fraction. """ print(__doc__) import numpy as np import matplotlib.pyplot as plt import matplotlib.font_manager from scipy import stats from sklearn import svm from sklearn.covariance import EllipticEnvelope # Example settings n_samples = 200 outliers_fraction = 0.25 clusters_separation = [0, 1, 2] # define two outlier detection tools to be compared classifiers = { "One-Class SVM": svm.OneClassSVM(nu=0.95 * outliers_fraction + 0.05, kernel="rbf", gamma=0.1), "robust covariance estimator": EllipticEnvelope(contamination=.1)} # Compare given classifiers under given settings xx, yy = np.meshgrid(np.linspace(-7, 7, 500), np.linspace(-7, 7, 500)) n_inliers = int((1. - outliers_fraction) * n_samples) n_outliers = int(outliers_fraction * n_samples) ground_truth = np.ones(n_samples, dtype=int) ground_truth[-n_outliers:] = 0 # Fit the problem with varying cluster separation for i, offset in enumerate(clusters_separation): np.random.seed(42) # Data generation X1 = 0.3 * np.random.randn(0.5 * n_inliers, 2) - offset X2 = 0.3 * np.random.randn(0.5 * n_inliers, 2) + offset X = np.r_[X1, X2] # Add outliers X = np.r_[X, np.random.uniform(low=-6, high=6, size=(n_outliers, 2))] # Fit the model with the One-Class SVM plt.figure(figsize=(10, 5)) for i, (clf_name, clf) in enumerate(classifiers.items()): # fit the data and tag outliers clf.fit(X) y_pred = clf.decision_function(X).ravel() threshold = stats.scoreatpercentile(y_pred, 100 * outliers_fraction) y_pred = y_pred > threshold n_errors = (y_pred != ground_truth).sum() # plot the levels lines and the points Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) subplot = plt.subplot(1, 2, i + 1) subplot.set_title("Outlier detection") subplot.contourf(xx, yy, Z, levels=np.linspace(Z.min(), threshold, 7), cmap=plt.cm.Blues_r) a = subplot.contour(xx, yy, Z, levels=[threshold], linewidths=2, colors='red') subplot.contourf(xx, yy, Z, levels=[threshold, Z.max()], colors='orange') b = subplot.scatter(X[:-n_outliers, 0], X[:-n_outliers, 1], c='white') c = subplot.scatter(X[-n_outliers:, 0], X[-n_outliers:, 1], c='black') subplot.axis('tight') subplot.legend( [a.collections[0], b, c], ['learned decision function', 'true inliers', 'true outliers'], prop=matplotlib.font_manager.FontProperties(size=11)) subplot.set_xlabel("%d. %s (errors: %d)" % (i + 1, clf_name, n_errors)) subplot.set_xlim((-7, 7)) subplot.set_ylim((-7, 7)) plt.subplots_adjust(0.04, 0.1, 0.96, 0.94, 0.1, 0.26) plt.show()
bsd-3-clause
rubikloud/scikit-learn
examples/decomposition/plot_kernel_pca.py
353
2011
""" ========== Kernel PCA ========== This example shows that Kernel PCA is able to find a projection of the data that makes data linearly separable. """ print(__doc__) # Authors: Mathieu Blondel # Andreas Mueller # License: BSD 3 clause import numpy as np import matplotlib.pyplot as plt from sklearn.decomposition import PCA, KernelPCA from sklearn.datasets import make_circles np.random.seed(0) X, y = make_circles(n_samples=400, factor=.3, noise=.05) kpca = KernelPCA(kernel="rbf", fit_inverse_transform=True, gamma=10) X_kpca = kpca.fit_transform(X) X_back = kpca.inverse_transform(X_kpca) pca = PCA() X_pca = pca.fit_transform(X) # Plot results plt.figure() plt.subplot(2, 2, 1, aspect='equal') plt.title("Original space") reds = y == 0 blues = y == 1 plt.plot(X[reds, 0], X[reds, 1], "ro") plt.plot(X[blues, 0], X[blues, 1], "bo") plt.xlabel("$x_1$") plt.ylabel("$x_2$") X1, X2 = np.meshgrid(np.linspace(-1.5, 1.5, 50), np.linspace(-1.5, 1.5, 50)) X_grid = np.array([np.ravel(X1), np.ravel(X2)]).T # projection on the first principal component (in the phi space) Z_grid = kpca.transform(X_grid)[:, 0].reshape(X1.shape) plt.contour(X1, X2, Z_grid, colors='grey', linewidths=1, origin='lower') plt.subplot(2, 2, 2, aspect='equal') plt.plot(X_pca[reds, 0], X_pca[reds, 1], "ro") plt.plot(X_pca[blues, 0], X_pca[blues, 1], "bo") plt.title("Projection by PCA") plt.xlabel("1st principal component") plt.ylabel("2nd component") plt.subplot(2, 2, 3, aspect='equal') plt.plot(X_kpca[reds, 0], X_kpca[reds, 1], "ro") plt.plot(X_kpca[blues, 0], X_kpca[blues, 1], "bo") plt.title("Projection by KPCA") plt.xlabel("1st principal component in space induced by $\phi$") plt.ylabel("2nd component") plt.subplot(2, 2, 4, aspect='equal') plt.plot(X_back[reds, 0], X_back[reds, 1], "ro") plt.plot(X_back[blues, 0], X_back[blues, 1], "bo") plt.title("Original space after inverse transform") plt.xlabel("$x_1$") plt.ylabel("$x_2$") plt.subplots_adjust(0.02, 0.10, 0.98, 0.94, 0.04, 0.35) plt.show()
bsd-3-clause
deeplook/bokeh
bokeh/charts/builder/line_builder.py
43
5360
"""This is the Bokeh charts interface. It gives you a high level API to build complex plot is a simple way. This is the Line class which lets you build your Line charts just passing the arguments to the Chart class and calling the proper functions. """ #----------------------------------------------------------------------------- # Copyright (c) 2012 - 2014, Continuum Analytics, Inc. All rights reserved. # # Powered by the Bokeh Development Team. # # The full license is in the file LICENSE.txt, distributed with this software. #----------------------------------------------------------------------------- #----------------------------------------------------------------------------- # Imports #----------------------------------------------------------------------------- from __future__ import absolute_import from six import string_types import numpy as np from ..utils import cycle_colors from .._builder import Builder, create_and_build from ...models import ColumnDataSource, DataRange1d, GlyphRenderer, Range1d from ...models.glyphs import Line as LineGlyph from ...properties import Any #----------------------------------------------------------------------------- # Classes and functions #----------------------------------------------------------------------------- def Line(values, index=None, **kws): """ Create a line chart using :class:`LineBuilder <bokeh.charts.builder.line_builder.LineBuilder>` to render the geometry from values and index. Args: values (iterable): iterable 2d representing the data series values matrix. index (str|1d iterable, optional): can be used to specify a common custom index for all data series as an **1d iterable** of any sort that will be used as series common index or a **string** that corresponds to the key of the mapping to be used as index (and not as data series) if area.values is a mapping (like a dict, an OrderedDict or a pandas DataFrame) In addition the the parameters specific to this chart, :ref:`userguide_charts_generic_arguments` are also accepted as keyword parameters. Returns: a new :class:`Chart <bokeh.charts.Chart>` Examples: .. bokeh-plot:: :source-position: above import numpy as np from bokeh.charts import Line, output_file, show # (dict, OrderedDict, lists, arrays and DataFrames are valid inputs) xyvalues = np.array([[2, 3, 7, 5, 26], [12, 33, 47, 15, 126], [22, 43, 10, 25, 26]]) line = Line(xyvalues, title="line", legend="top_left", ylabel='Languages') output_file('line.html') show(line) """ return create_and_build(LineBuilder, values, index=index, **kws) class LineBuilder(Builder): """This is the Line class and it is in charge of plotting Line charts in an easy and intuitive way. Essentially, we provide a way to ingest the data, make the proper calculations and push the references into a source object. We additionally make calculations for the ranges. And finally add the needed lines taking the references from the source. """ index = Any(help=""" An index to be used for all data series as follows: - A 1d iterable of any sort that will be used as series common index - As a string that corresponds to the key of the mapping to be used as index (and not as data series) if area.values is a mapping (like a dict, an OrderedDict or a pandas DataFrame) """) def _process_data(self): """Calculate the chart properties accordingly from line.values. Then build a dict containing references to all the points to be used by the line glyph inside the ``_yield_renderers`` method. """ self._data = dict() # list to save all the attributes we are going to create self._attr = [] xs = self._values_index self.set_and_get("x", "", np.array(xs)) for col, values in self._values.items(): if isinstance(self.index, string_types) and col == self.index: continue # save every new group we find self._groups.append(col) self.set_and_get("y_", col, values) def _set_sources(self): """ Push the Line data into the ColumnDataSource and calculate the proper ranges. """ self._source = ColumnDataSource(self._data) self.x_range = DataRange1d() y_names = self._attr[1:] endy = max(max(self._data[i]) for i in y_names) starty = min(min(self._data[i]) for i in y_names) self.y_range = Range1d( start=starty - 0.1 * (endy - starty), end=endy + 0.1 * (endy - starty) ) def _yield_renderers(self): """Use the line glyphs to connect the xy points in the Line. Takes reference points from the data loaded at the ColumnDataSource. """ colors = cycle_colors(self._attr, self.palette) for i, duplet in enumerate(self._attr[1:], start=1): glyph = LineGlyph(x='x', y=duplet, line_color=colors[i - 1]) renderer = GlyphRenderer(data_source=self._source, glyph=glyph) self._legends.append((self._groups[i-1], [renderer])) yield renderer
bsd-3-clause
molpopgen/pyseq
docs/conf.py
2
9974
# -*- coding: utf-8 -*- # # pylibseq documentation build configuration file, created by # sphinx-quickstart on Mon Oct 19 19:11:29 2015. # # This file is execfile()d with the current directory set to its # containing dir. # # Note that not all possible configuration values are present in this # autogenerated file. # # All configuration values have a default; values that are commented out # serve to show the default. import sys import os import subprocess import shlex #os.environ['LD_LIBRARY_PATH']=sys.prefix+'/lib' # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. if (os.environ.get('READTHEDOCS')=="True") is False: sys.path.insert(0, os.path.abspath('..')) else: import site p=site.getsitepackages()[0] sys.path.insert(0,p) # -- General configuration ------------------------------------------------ # If your documentation needs a minimal Sphinx version, state it here. #needs_sphinx = '1.0' # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. extensions = [ 'sphinx.ext.autodoc', 'sphinx.ext.doctest', 'sphinx.ext.todo', 'sphinx.ext.coverage', 'sphinx.ext.mathjax', 'sphinx.ext.viewcode', 'sphinxcontrib.bibtex', 'matplotlib.sphinxext.plot_directive', 'IPython.sphinxext.ipython_console_highlighting', 'IPython.sphinxext.ipython_directive', ] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] # The suffix(es) of source filenames. # You can specify multiple suffix as a list of string: # source_suffix = ['.rst', '.md'] source_suffix = '.rst' # The encoding of source files. #source_encoding = 'utf-8-sig' # The master toctree document. master_doc = 'index' # General information about the project. project = u'pylibseq' copyright = u'2015, Kevin Thornton' author = u'Kevin Thornton' # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the # built documents. # # The short X.Y version. version = '0.2.3' # The full version, including alpha/beta/rc tags. release = '0.2.3' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. # # This is also used if you do content translation via gettext catalogs. # Usually you set "language" from the command line for these cases. language = None # There are two options for replacing |today|: either, you set today to some # non-false value, then it is used: #today = '' # Else, today_fmt is used as the format for a strftime call. #today_fmt = '%B %d, %Y' # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. exclude_patterns = ['_build'] # The reST default role (used for this markup: `text`) to use for all # documents. #default_role = None # If true, '()' will be appended to :func: etc. cross-reference text. #add_function_parentheses = True # If true, the current module name will be prepended to all description # unit titles (such as .. function::). #add_module_names = True # If true, sectionauthor and moduleauthor directives will be shown in the # output. They are ignored by default. #show_authors = False # The name of the Pygments (syntax highlighting) style to use. pygments_style = 'sphinx' # A list of ignored prefixes for module index sorting. #modindex_common_prefix = [] # If true, keep warnings as "system message" paragraphs in the built documents. #keep_warnings = False # If true, `todo` and `todoList` produce output, else they produce nothing. todo_include_todos = True # -- Options for HTML output ---------------------------------------------- # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. html_theme = 'default' if (os.environ.get('READTHEDOCS')=="True") is True: html_theme_options = { 'github_user':'molpopgen', 'github_repo':'pylibseq', # 'github_button':True, # 'github_banner':True, } # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the # documentation. #html_theme_options = {} # Add any paths that contain custom themes here, relative to this directory. #html_theme_path = [] # The name for this set of Sphinx documents. If None, it defaults to # "<project> v<release> documentation". #html_title = None # A shorter title for the navigation bar. Default is the same as html_title. #html_short_title = None # The name of an image file (relative to this directory) to place at the top # of the sidebar. #html_logo = None # The name of an image file (within the static path) to use as favicon of the # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 # pixels large. #html_favicon = None # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". html_static_path = ['_static'] # Add any extra paths that contain custom files (such as robots.txt or # .htaccess) here, relative to this directory. These files are copied # directly to the root of the documentation. #html_extra_path = [] # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, # using the given strftime format. #html_last_updated_fmt = '%b %d, %Y' # If true, SmartyPants will be used to convert quotes and dashes to # typographically correct entities. #html_use_smartypants = True # Custom sidebar templates, maps document names to template names. #html_sidebars = {} # Additional templates that should be rendered to pages, maps page names to # template names. #html_additional_pages = {} # If false, no module index is generated. #html_domain_indices = True # If false, no index is generated. #html_use_index = True # If true, the index is split into individual pages for each letter. #html_split_index = False # If true, links to the reST sources are added to the pages. #html_show_sourcelink = True # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. #html_show_sphinx = True # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. #html_show_copyright = True # If true, an OpenSearch description file will be output, and all pages will # contain a <link> tag referring to it. The value of this option must be the # base URL from which the finished HTML is served. #html_use_opensearch = '' # This is the file name suffix for HTML files (e.g. ".xhtml"). #html_file_suffix = None # Language to be used for generating the HTML full-text search index. # Sphinx supports the following languages: # 'da', 'de', 'en', 'es', 'fi', 'fr', 'hu', 'it', 'ja' # 'nl', 'no', 'pt', 'ro', 'ru', 'sv', 'tr' #html_search_language = 'en' # A dictionary with options for the search language support, empty by default. # Now only 'ja' uses this config value #html_search_options = {'type': 'default'} # The name of a javascript file (relative to the configuration directory) that # implements a search results scorer. If empty, the default will be used. #html_search_scorer = 'scorer.js' # Output file base name for HTML help builder. htmlhelp_basename = 'pylibseqdoc' # -- Options for LaTeX output --------------------------------------------- latex_elements = { # The paper size ('letterpaper' or 'a4paper'). #'papersize': 'letterpaper', # The font size ('10pt', '11pt' or '12pt'). #'pointsize': '10pt', # Additional stuff for the LaTeX preamble. #'preamble': '', # Latex figure (float) alignment #'figure_align': 'htbp', } # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, # author, documentclass [howto, manual, or own class]). latex_documents = [ (master_doc, 'pylibseq.tex', u'pylibseq Documentation', u'Kevin Thornton', 'manual'), ] autoclass_content = 'both' # The name of an image file (relative to this directory) to place at the top of # the title page. #latex_logo = None # For "manual" documents, if this is true, then toplevel headings are parts, # not chapters. #latex_use_parts = False # If true, show page references after internal links. #latex_show_pagerefs = False # If true, show URL addresses after external links. #latex_show_urls = False # Documents to append as an appendix to all manuals. #latex_appendices = [] # If false, no module index is generated. #latex_domain_indices = True # -- Options for manual page output --------------------------------------- # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ (master_doc, 'pylibseq', u'pylibseq Documentation', [author], 1) ] # If true, show URL addresses after external links. #man_show_urls = False # -- Options for Texinfo output ------------------------------------------- # Grouping the document tree into Texinfo files. List of tuples # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ (master_doc, 'pylibseq', u'pylibseq Documentation', author, 'pylibseq', 'One line description of project.', 'Miscellaneous'), ] # Documents to append as an appendix to all manuals. #texinfo_appendices = [] # If false, no module index is generated. #texinfo_domain_indices = True # How to display URL addresses: 'footnote', 'no', or 'inline'. #texinfo_show_urls = 'footnote' # If true, do not generate a @detailmenu in the "Top" node's menu. #texinfo_no_detailmenu = False
gpl-2.0
sriki18/scipy
scipy/signal/_max_len_seq.py
41
4942
# Author: Eric Larson # 2014 """Tools for MLS generation""" import numpy as np from ._max_len_seq_inner import _max_len_seq_inner __all__ = ['max_len_seq'] # These are definitions of linear shift register taps for use in max_len_seq() _mls_taps = {2: [1], 3: [2], 4: [3], 5: [3], 6: [5], 7: [6], 8: [7, 6, 1], 9: [5], 10: [7], 11: [9], 12: [11, 10, 4], 13: [12, 11, 8], 14: [13, 12, 2], 15: [14], 16: [15, 13, 4], 17: [14], 18: [11], 19: [18, 17, 14], 20: [17], 21: [19], 22: [21], 23: [18], 24: [23, 22, 17], 25: [22], 26: [25, 24, 20], 27: [26, 25, 22], 28: [25], 29: [27], 30: [29, 28, 7], 31: [28], 32: [31, 30, 10]} def max_len_seq(nbits, state=None, length=None, taps=None): """ Maximum length sequence (MLS) generator. Parameters ---------- nbits : int Number of bits to use. Length of the resulting sequence will be ``(2**nbits) - 1``. Note that generating long sequences (e.g., greater than ``nbits == 16``) can take a long time. state : array_like, optional If array, must be of length ``nbits``, and will be cast to binary (bool) representation. If None, a seed of ones will be used, producing a repeatable representation. If ``state`` is all zeros, an error is raised as this is invalid. Default: None. length : int, optional Number of samples to compute. If None, the entire length ``(2**nbits) - 1`` is computed. taps : array_like, optional Polynomial taps to use (e.g., ``[7, 6, 1]`` for an 8-bit sequence). If None, taps will be automatically selected (for up to ``nbits == 32``). Returns ------- seq : array Resulting MLS sequence of 0's and 1's. state : array The final state of the shift register. Notes ----- The algorithm for MLS generation is generically described in: https://en.wikipedia.org/wiki/Maximum_length_sequence The default values for taps are specifically taken from the first option listed for each value of ``nbits`` in: http://www.newwaveinstruments.com/resources/articles/ m_sequence_linear_feedback_shift_register_lfsr.htm .. versionadded:: 0.15.0 Examples -------- MLS uses binary convention: >>> from scipy.signal import max_len_seq >>> max_len_seq(4)[0] array([1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0], dtype=int8) MLS has a white spectrum (except for DC): >>> import matplotlib.pyplot as plt >>> from numpy.fft import fft, ifft, fftshift, fftfreq >>> seq = max_len_seq(6)[0]*2-1 # +1 and -1 >>> spec = fft(seq) >>> N = len(seq) >>> plt.plot(fftshift(fftfreq(N)), fftshift(np.abs(spec)), '.-') >>> plt.margins(0.1, 0.1) >>> plt.grid(True) >>> plt.show() Circular autocorrelation of MLS is an impulse: >>> acorrcirc = ifft(spec * np.conj(spec)).real >>> plt.figure() >>> plt.plot(np.arange(-N/2+1, N/2+1), fftshift(acorrcirc), '.-') >>> plt.margins(0.1, 0.1) >>> plt.grid(True) >>> plt.show() Linear autocorrelation of MLS is approximately an impulse: >>> acorr = np.correlate(seq, seq, 'full') >>> plt.figure() >>> plt.plot(np.arange(-N+1, N), acorr, '.-') >>> plt.margins(0.1, 0.1) >>> plt.grid(True) >>> plt.show() """ if taps is None: if nbits not in _mls_taps: known_taps = np.array(list(_mls_taps.keys())) raise ValueError('nbits must be between %s and %s if taps is None' % (known_taps.min(), known_taps.max())) taps = np.array(_mls_taps[nbits], np.intp) else: taps = np.unique(np.array(taps, np.intp))[::-1] if np.any(taps < 0) or np.any(taps > nbits) or taps.size < 1: raise ValueError('taps must be non-empty with values between ' 'zero and nbits (inclusive)') taps = np.ascontiguousarray(taps) # needed for Cython n_max = (2**nbits) - 1 if length is None: length = n_max else: length = int(length) if length < 0: raise ValueError('length must be greater than or equal to 0') # We use int8 instead of bool here because numpy arrays of bools # don't seem to work nicely with Cython if state is None: state = np.ones(nbits, dtype=np.int8, order='c') else: # makes a copy if need be, ensuring it's 0's and 1's state = np.array(state, dtype=bool, order='c').astype(np.int8) if state.ndim != 1 or state.size != nbits: raise ValueError('state must be a 1-dimensional array of size nbits') if np.all(state == 0): raise ValueError('state must not be all zeros') seq = np.empty(length, dtype=np.int8, order='c') state = _max_len_seq_inner(taps, state, nbits, length, seq) return seq, state
bsd-3-clause
OpenSourcePolicyCenter/taxdata
puf_stage1/factors_finalprep.py
1
3867
""" Transform Stage_I_factors.csv (written by the stage1.py script) and benefit_growth_rates.csv into growfactors.csv (used by Tax-Calculator). """ import numpy as np import pandas as pd import os # pylint: disable=invalid-name CUR_PATH = os.path.abspath(os.path.dirname(__file__)) first_benefit_year = 2014 inben_filename = os.path.join(CUR_PATH, 'benefit_growth_rates.csv') first_data_year = 2011 infac_filename = os.path.join(CUR_PATH, 'Stage_I_factors.csv') output_filename = os.path.join(CUR_PATH, 'growfactors.csv') # -------------------------------------------------------------------------- # read in raw average benefit amounts by year and # convert into "one plus annual proportion change" factors bgr_all = pd.read_csv(inben_filename, index_col='YEAR') bnames = ['mcare', 'mcaid', 'ssi', 'snap', 'wic', 'housing', 'tanf', 'vet'] keep_cols = ['{}_average_benefit'.format(bname) for bname in bnames] bgr_raw = bgr_all[keep_cols] gf_bnames = ['ABEN{}'.format(bname.upper()) for bname in bnames] bgr_raw.columns = gf_bnames bgf = 1.0 + bgr_raw.astype('float64').pct_change() # specify first row values because pct_change() leaves first year undefined for var in list(bgf): bgf[var][first_benefit_year] = 1.0 # add rows of ones for years from first_data_year thru first_benefit_year-1 ones = [1.0] * len(bnames) for year in range(first_data_year, first_benefit_year): row = pd.DataFrame(data=[ones], columns=gf_bnames, index=[year]) bgf = pd.concat([bgf, row], verify_integrity=True) bgf.sort_index(inplace=True) # round converted factors to six decimal digits of accuracy bgf = bgf.round(6) # -------------------------------------------------------------------------- # read in blowup factors used internally in taxdata repository data = pd.read_csv(infac_filename, index_col='YEAR') # convert some aggregate factors into per-capita factors elderly_pop = data['APOPSNR'] data['ASOCSEC'] = data['ASOCSEC'] / elderly_pop pop = data['APOPN'] data['AWAGE'] = data['AWAGE'] / pop data['ATXPY'] = data['ATXPY'] / pop data['ASCHCI'] = data['ASCHCI'] / pop data['ASCHCL'] = data['ASCHCL'] / pop data['ASCHF'] = data['ASCHF'] / pop data['AINTS'] = data['AINTS'] / pop data['ADIVS'] = data['ADIVS'] / pop data['ASCHEI'] = data['ASCHEI'] / pop data['ASCHEL'] = data['ASCHEL'] / pop data['ACGNS'] = data['ACGNS'] / pop data['ABOOK'] = data['ABOOK'] / pop data['ABENEFITS'] = data['ABENEFITS'] / pop data.rename(columns={'ABENEFITS': 'ABENOTHER'}, inplace=True) # convert factors into "one plus annual proportion change" format data = 1.0 + data.pct_change() # specify first row values because pct_change() leaves first year undefined for var in list(data): data[var][first_data_year] = 1.0 # round converted factors to six decimal digits of accuracy data = data.round(6) # -------------------------------------------------------------------------- # combine data and bgf DataFrames gfdf = pd.concat([data, bgf], axis='columns', verify_integrity=True) # -------------------------------------------------------------------------- # delete from data the variables not used by Tax-Calculator (TC) TC_USED_VARS = set(['ABOOK', 'ACGNS', 'ACPIM', 'ACPIU', 'ADIVS', 'AINTS', 'AIPD', 'ASCHCI', 'ASCHCL', 'ASCHEI', 'ASCHEL', 'ASCHF', 'ASOCSEC', 'ATXPY', 'AUCOMP', 'AWAGE', 'ABENOTHER'] + gf_bnames) ALL_VARS = set(list(gfdf)) TC_UNUSED_VARS = ALL_VARS - TC_USED_VARS gfdf = gfdf.drop(TC_UNUSED_VARS, axis=1) # write out grow factors used in blowup logic in Tax-Calculator repository gfdf.to_csv(output_filename, index_label='YEAR')
mit
aminert/scikit-learn
sklearn/cross_validation.py
96
58309
""" The :mod:`sklearn.cross_validation` module includes utilities for cross- validation and performance evaluation. """ # Author: Alexandre Gramfort <alexandre.gramfort@inria.fr>, # Gael Varoquaux <gael.varoquaux@normalesup.org>, # Olivier Grisel <olivier.grisel@ensta.org> # License: BSD 3 clause from __future__ import print_function from __future__ import division import warnings from itertools import chain, combinations from math import ceil, floor, factorial import numbers import time from abc import ABCMeta, abstractmethod import numpy as np import scipy.sparse as sp from .base import is_classifier, clone from .utils import indexable, check_random_state, safe_indexing from .utils.validation import (_is_arraylike, _num_samples, check_array, column_or_1d) from .utils.multiclass import type_of_target from .externals.joblib import Parallel, delayed, logger from .externals.six import with_metaclass from .externals.six.moves import zip from .metrics.scorer import check_scoring from .utils.fixes import bincount __all__ = ['KFold', 'LeaveOneLabelOut', 'LeaveOneOut', 'LeavePLabelOut', 'LeavePOut', 'ShuffleSplit', 'StratifiedKFold', 'StratifiedShuffleSplit', 'PredefinedSplit', 'check_cv', 'cross_val_score', 'cross_val_predict', 'permutation_test_score', 'train_test_split'] class _PartitionIterator(with_metaclass(ABCMeta)): """Base class for CV iterators where train_mask = ~test_mask Implementations must define `_iter_test_masks` or `_iter_test_indices`. Parameters ---------- n : int Total number of elements in dataset. """ def __init__(self, n): if abs(n - int(n)) >= np.finfo('f').eps: raise ValueError("n must be an integer") self.n = int(n) def __iter__(self): ind = np.arange(self.n) for test_index in self._iter_test_masks(): train_index = np.logical_not(test_index) train_index = ind[train_index] test_index = ind[test_index] yield train_index, test_index # Since subclasses must implement either _iter_test_masks or # _iter_test_indices, neither can be abstract. def _iter_test_masks(self): """Generates boolean masks corresponding to test sets. By default, delegates to _iter_test_indices() """ for test_index in self._iter_test_indices(): test_mask = self._empty_mask() test_mask[test_index] = True yield test_mask def _iter_test_indices(self): """Generates integer indices corresponding to test sets.""" raise NotImplementedError def _empty_mask(self): return np.zeros(self.n, dtype=np.bool) class LeaveOneOut(_PartitionIterator): """Leave-One-Out cross validation iterator. Provides train/test indices to split data in train test sets. Each sample is used once as a test set (singleton) while the remaining samples form the training set. Note: ``LeaveOneOut(n)`` is equivalent to ``KFold(n, n_folds=n)`` and ``LeavePOut(n, p=1)``. Due to the high number of test sets (which is the same as the number of samples) this cross validation method can be very costly. For large datasets one should favor KFold, StratifiedKFold or ShuffleSplit. Read more in the :ref:`User Guide <cross_validation>`. Parameters ---------- n : int Total number of elements in dataset. Examples -------- >>> from sklearn import cross_validation >>> X = np.array([[1, 2], [3, 4]]) >>> y = np.array([1, 2]) >>> loo = cross_validation.LeaveOneOut(2) >>> len(loo) 2 >>> print(loo) sklearn.cross_validation.LeaveOneOut(n=2) >>> for train_index, test_index in loo: ... print("TRAIN:", train_index, "TEST:", test_index) ... X_train, X_test = X[train_index], X[test_index] ... y_train, y_test = y[train_index], y[test_index] ... print(X_train, X_test, y_train, y_test) TRAIN: [1] TEST: [0] [[3 4]] [[1 2]] [2] [1] TRAIN: [0] TEST: [1] [[1 2]] [[3 4]] [1] [2] See also -------- LeaveOneLabelOut for splitting the data according to explicit, domain-specific stratification of the dataset. """ def _iter_test_indices(self): return range(self.n) def __repr__(self): return '%s.%s(n=%i)' % ( self.__class__.__module__, self.__class__.__name__, self.n, ) def __len__(self): return self.n class LeavePOut(_PartitionIterator): """Leave-P-Out cross validation iterator Provides train/test indices to split data in train test sets. This results in testing on all distinct samples of size p, while the remaining n - p samples form the training set in each iteration. Note: ``LeavePOut(n, p)`` is NOT equivalent to ``KFold(n, n_folds=n // p)`` which creates non-overlapping test sets. Due to the high number of iterations which grows combinatorically with the number of samples this cross validation method can be very costly. For large datasets one should favor KFold, StratifiedKFold or ShuffleSplit. Read more in the :ref:`User Guide <cross_validation>`. Parameters ---------- n : int Total number of elements in dataset. p : int Size of the test sets. Examples -------- >>> from sklearn import cross_validation >>> X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]]) >>> y = np.array([1, 2, 3, 4]) >>> lpo = cross_validation.LeavePOut(4, 2) >>> len(lpo) 6 >>> print(lpo) sklearn.cross_validation.LeavePOut(n=4, p=2) >>> for train_index, test_index in lpo: ... print("TRAIN:", train_index, "TEST:", test_index) ... X_train, X_test = X[train_index], X[test_index] ... y_train, y_test = y[train_index], y[test_index] TRAIN: [2 3] TEST: [0 1] TRAIN: [1 3] TEST: [0 2] TRAIN: [1 2] TEST: [0 3] TRAIN: [0 3] TEST: [1 2] TRAIN: [0 2] TEST: [1 3] TRAIN: [0 1] TEST: [2 3] """ def __init__(self, n, p): super(LeavePOut, self).__init__(n) self.p = p def _iter_test_indices(self): for comb in combinations(range(self.n), self.p): yield np.array(comb) def __repr__(self): return '%s.%s(n=%i, p=%i)' % ( self.__class__.__module__, self.__class__.__name__, self.n, self.p, ) def __len__(self): return int(factorial(self.n) / factorial(self.n - self.p) / factorial(self.p)) class _BaseKFold(with_metaclass(ABCMeta, _PartitionIterator)): """Base class to validate KFold approaches""" @abstractmethod def __init__(self, n, n_folds, shuffle, random_state): super(_BaseKFold, self).__init__(n) if abs(n_folds - int(n_folds)) >= np.finfo('f').eps: raise ValueError("n_folds must be an integer") self.n_folds = n_folds = int(n_folds) if n_folds <= 1: raise ValueError( "k-fold cross validation requires at least one" " train / test split by setting n_folds=2 or more," " got n_folds={0}.".format(n_folds)) if n_folds > self.n: raise ValueError( ("Cannot have number of folds n_folds={0} greater" " than the number of samples: {1}.").format(n_folds, n)) if not isinstance(shuffle, bool): raise TypeError("shuffle must be True or False;" " got {0}".format(shuffle)) self.shuffle = shuffle self.random_state = random_state class KFold(_BaseKFold): """K-Folds cross validation iterator. Provides train/test indices to split data in train test sets. Split dataset into k consecutive folds (without shuffling). Each fold is then used a validation set once while the k - 1 remaining fold form the training set. Read more in the :ref:`User Guide <cross_validation>`. Parameters ---------- n : int Total number of elements. n_folds : int, default=3 Number of folds. Must be at least 2. shuffle : boolean, optional Whether to shuffle the data before splitting into batches. random_state : None, int or RandomState Pseudo-random number generator state used for random sampling. If None, use default numpy RNG for shuffling Examples -------- >>> from sklearn import cross_validation >>> X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]]) >>> y = np.array([1, 2, 3, 4]) >>> kf = cross_validation.KFold(4, n_folds=2) >>> len(kf) 2 >>> print(kf) # doctest: +NORMALIZE_WHITESPACE sklearn.cross_validation.KFold(n=4, n_folds=2, shuffle=False, random_state=None) >>> for train_index, test_index in kf: ... print("TRAIN:", train_index, "TEST:", test_index) ... X_train, X_test = X[train_index], X[test_index] ... y_train, y_test = y[train_index], y[test_index] TRAIN: [2 3] TEST: [0 1] TRAIN: [0 1] TEST: [2 3] Notes ----- The first n % n_folds folds have size n // n_folds + 1, other folds have size n // n_folds. See also -------- StratifiedKFold: take label information into account to avoid building folds with imbalanced class distributions (for binary or multiclass classification tasks). """ def __init__(self, n, n_folds=3, shuffle=False, random_state=None): super(KFold, self).__init__(n, n_folds, shuffle, random_state) self.idxs = np.arange(n) if shuffle: rng = check_random_state(self.random_state) rng.shuffle(self.idxs) def _iter_test_indices(self): n = self.n n_folds = self.n_folds fold_sizes = (n // n_folds) * np.ones(n_folds, dtype=np.int) fold_sizes[:n % n_folds] += 1 current = 0 for fold_size in fold_sizes: start, stop = current, current + fold_size yield self.idxs[start:stop] current = stop def __repr__(self): return '%s.%s(n=%i, n_folds=%i, shuffle=%s, random_state=%s)' % ( self.__class__.__module__, self.__class__.__name__, self.n, self.n_folds, self.shuffle, self.random_state, ) def __len__(self): return self.n_folds class StratifiedKFold(_BaseKFold): """Stratified K-Folds cross validation iterator Provides train/test indices to split data in train test sets. This cross-validation object is a variation of KFold that returns stratified folds. The folds are made by preserving the percentage of samples for each class. Read more in the :ref:`User Guide <cross_validation>`. Parameters ---------- y : array-like, [n_samples] Samples to split in K folds. n_folds : int, default=3 Number of folds. Must be at least 2. shuffle : boolean, optional Whether to shuffle each stratification of the data before splitting into batches. random_state : None, int or RandomState Pseudo-random number generator state used for random sampling. If None, use default numpy RNG for shuffling Examples -------- >>> from sklearn import cross_validation >>> X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]]) >>> y = np.array([0, 0, 1, 1]) >>> skf = cross_validation.StratifiedKFold(y, n_folds=2) >>> len(skf) 2 >>> print(skf) # doctest: +NORMALIZE_WHITESPACE sklearn.cross_validation.StratifiedKFold(labels=[0 0 1 1], n_folds=2, shuffle=False, random_state=None) >>> for train_index, test_index in skf: ... print("TRAIN:", train_index, "TEST:", test_index) ... X_train, X_test = X[train_index], X[test_index] ... y_train, y_test = y[train_index], y[test_index] TRAIN: [1 3] TEST: [0 2] TRAIN: [0 2] TEST: [1 3] Notes ----- All the folds have size trunc(n_samples / n_folds), the last one has the complementary. """ def __init__(self, y, n_folds=3, shuffle=False, random_state=None): super(StratifiedKFold, self).__init__( len(y), n_folds, shuffle, random_state) y = np.asarray(y) n_samples = y.shape[0] unique_labels, y_inversed = np.unique(y, return_inverse=True) label_counts = bincount(y_inversed) min_labels = np.min(label_counts) if self.n_folds > min_labels: warnings.warn(("The least populated class in y has only %d" " members, which is too few. The minimum" " number of labels for any class cannot" " be less than n_folds=%d." % (min_labels, self.n_folds)), Warning) # don't want to use the same seed in each label's shuffle if self.shuffle: rng = check_random_state(self.random_state) else: rng = self.random_state # pre-assign each sample to a test fold index using individual KFold # splitting strategies for each label so as to respect the # balance of labels per_label_cvs = [ KFold(max(c, self.n_folds), self.n_folds, shuffle=self.shuffle, random_state=rng) for c in label_counts] test_folds = np.zeros(n_samples, dtype=np.int) for test_fold_idx, per_label_splits in enumerate(zip(*per_label_cvs)): for label, (_, test_split) in zip(unique_labels, per_label_splits): label_test_folds = test_folds[y == label] # the test split can be too big because we used # KFold(max(c, self.n_folds), self.n_folds) instead of # KFold(c, self.n_folds) to make it possible to not crash even # if the data is not 100% stratifiable for all the labels # (we use a warning instead of raising an exception) # If this is the case, let's trim it: test_split = test_split[test_split < len(label_test_folds)] label_test_folds[test_split] = test_fold_idx test_folds[y == label] = label_test_folds self.test_folds = test_folds self.y = y def _iter_test_masks(self): for i in range(self.n_folds): yield self.test_folds == i def __repr__(self): return '%s.%s(labels=%s, n_folds=%i, shuffle=%s, random_state=%s)' % ( self.__class__.__module__, self.__class__.__name__, self.y, self.n_folds, self.shuffle, self.random_state, ) def __len__(self): return self.n_folds class LeaveOneLabelOut(_PartitionIterator): """Leave-One-Label_Out cross-validation iterator Provides train/test indices to split data according to a third-party provided label. This label information can be used to encode arbitrary domain specific stratifications of the samples as integers. For instance the labels could be the year of collection of the samples and thus allow for cross-validation against time-based splits. Read more in the :ref:`User Guide <cross_validation>`. Parameters ---------- labels : array-like of int with shape (n_samples,) Arbitrary domain-specific stratification of the data to be used to draw the splits. Examples -------- >>> from sklearn import cross_validation >>> X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]]) >>> y = np.array([1, 2, 1, 2]) >>> labels = np.array([1, 1, 2, 2]) >>> lol = cross_validation.LeaveOneLabelOut(labels) >>> len(lol) 2 >>> print(lol) sklearn.cross_validation.LeaveOneLabelOut(labels=[1 1 2 2]) >>> for train_index, test_index in lol: ... print("TRAIN:", train_index, "TEST:", test_index) ... X_train, X_test = X[train_index], X[test_index] ... y_train, y_test = y[train_index], y[test_index] ... print(X_train, X_test, y_train, y_test) TRAIN: [2 3] TEST: [0 1] [[5 6] [7 8]] [[1 2] [3 4]] [1 2] [1 2] TRAIN: [0 1] TEST: [2 3] [[1 2] [3 4]] [[5 6] [7 8]] [1 2] [1 2] """ def __init__(self, labels): super(LeaveOneLabelOut, self).__init__(len(labels)) # We make a copy of labels to avoid side-effects during iteration self.labels = np.array(labels, copy=True) self.unique_labels = np.unique(labels) self.n_unique_labels = len(self.unique_labels) def _iter_test_masks(self): for i in self.unique_labels: yield self.labels == i def __repr__(self): return '%s.%s(labels=%s)' % ( self.__class__.__module__, self.__class__.__name__, self.labels, ) def __len__(self): return self.n_unique_labels class LeavePLabelOut(_PartitionIterator): """Leave-P-Label_Out cross-validation iterator Provides train/test indices to split data according to a third-party provided label. This label information can be used to encode arbitrary domain specific stratifications of the samples as integers. For instance the labels could be the year of collection of the samples and thus allow for cross-validation against time-based splits. The difference between LeavePLabelOut and LeaveOneLabelOut is that the former builds the test sets with all the samples assigned to ``p`` different values of the labels while the latter uses samples all assigned the same labels. Read more in the :ref:`User Guide <cross_validation>`. Parameters ---------- labels : array-like of int with shape (n_samples,) Arbitrary domain-specific stratification of the data to be used to draw the splits. p : int Number of samples to leave out in the test split. Examples -------- >>> from sklearn import cross_validation >>> X = np.array([[1, 2], [3, 4], [5, 6]]) >>> y = np.array([1, 2, 1]) >>> labels = np.array([1, 2, 3]) >>> lpl = cross_validation.LeavePLabelOut(labels, p=2) >>> len(lpl) 3 >>> print(lpl) sklearn.cross_validation.LeavePLabelOut(labels=[1 2 3], p=2) >>> for train_index, test_index in lpl: ... print("TRAIN:", train_index, "TEST:", test_index) ... X_train, X_test = X[train_index], X[test_index] ... y_train, y_test = y[train_index], y[test_index] ... print(X_train, X_test, y_train, y_test) TRAIN: [2] TEST: [0 1] [[5 6]] [[1 2] [3 4]] [1] [1 2] TRAIN: [1] TEST: [0 2] [[3 4]] [[1 2] [5 6]] [2] [1 1] TRAIN: [0] TEST: [1 2] [[1 2]] [[3 4] [5 6]] [1] [2 1] """ def __init__(self, labels, p): # We make a copy of labels to avoid side-effects during iteration super(LeavePLabelOut, self).__init__(len(labels)) self.labels = np.array(labels, copy=True) self.unique_labels = np.unique(labels) self.n_unique_labels = len(self.unique_labels) self.p = p def _iter_test_masks(self): comb = combinations(range(self.n_unique_labels), self.p) for idx in comb: test_index = self._empty_mask() idx = np.array(idx) for l in self.unique_labels[idx]: test_index[self.labels == l] = True yield test_index def __repr__(self): return '%s.%s(labels=%s, p=%s)' % ( self.__class__.__module__, self.__class__.__name__, self.labels, self.p, ) def __len__(self): return int(factorial(self.n_unique_labels) / factorial(self.n_unique_labels - self.p) / factorial(self.p)) class BaseShuffleSplit(with_metaclass(ABCMeta)): """Base class for ShuffleSplit and StratifiedShuffleSplit""" def __init__(self, n, n_iter=10, test_size=0.1, train_size=None, random_state=None): self.n = n self.n_iter = n_iter self.test_size = test_size self.train_size = train_size self.random_state = random_state self.n_train, self.n_test = _validate_shuffle_split(n, test_size, train_size) def __iter__(self): for train, test in self._iter_indices(): yield train, test return @abstractmethod def _iter_indices(self): """Generate (train, test) indices""" class ShuffleSplit(BaseShuffleSplit): """Random permutation cross-validation iterator. Yields indices to split data into training and test sets. Note: contrary to other cross-validation strategies, random splits do not guarantee that all folds will be different, although this is still very likely for sizeable datasets. Read more in the :ref:`User Guide <cross_validation>`. Parameters ---------- n : int Total number of elements in the dataset. n_iter : int (default 10) Number of re-shuffling & splitting iterations. test_size : float (default 0.1), int, or None If float, should be between 0.0 and 1.0 and represent the proportion of the dataset to include in the test split. If int, represents the absolute number of test samples. If None, the value is automatically set to the complement of the train size. train_size : float, int, or None (default is None) If float, should be between 0.0 and 1.0 and represent the proportion of the dataset to include in the train split. If int, represents the absolute number of train samples. If None, the value is automatically set to the complement of the test size. random_state : int or RandomState Pseudo-random number generator state used for random sampling. Examples -------- >>> from sklearn import cross_validation >>> rs = cross_validation.ShuffleSplit(4, n_iter=3, ... test_size=.25, random_state=0) >>> len(rs) 3 >>> print(rs) ... # doctest: +ELLIPSIS ShuffleSplit(4, n_iter=3, test_size=0.25, ...) >>> for train_index, test_index in rs: ... print("TRAIN:", train_index, "TEST:", test_index) ... TRAIN: [3 1 0] TEST: [2] TRAIN: [2 1 3] TEST: [0] TRAIN: [0 2 1] TEST: [3] >>> rs = cross_validation.ShuffleSplit(4, n_iter=3, ... train_size=0.5, test_size=.25, random_state=0) >>> for train_index, test_index in rs: ... print("TRAIN:", train_index, "TEST:", test_index) ... TRAIN: [3 1] TEST: [2] TRAIN: [2 1] TEST: [0] TRAIN: [0 2] TEST: [3] """ def _iter_indices(self): rng = check_random_state(self.random_state) for i in range(self.n_iter): # random partition permutation = rng.permutation(self.n) ind_test = permutation[:self.n_test] ind_train = permutation[self.n_test:self.n_test + self.n_train] yield ind_train, ind_test def __repr__(self): return ('%s(%d, n_iter=%d, test_size=%s, ' 'random_state=%s)' % ( self.__class__.__name__, self.n, self.n_iter, str(self.test_size), self.random_state, )) def __len__(self): return self.n_iter def _validate_shuffle_split(n, test_size, train_size): if test_size is None and train_size is None: raise ValueError( 'test_size and train_size can not both be None') if test_size is not None: if np.asarray(test_size).dtype.kind == 'f': if test_size >= 1.: raise ValueError( 'test_size=%f should be smaller ' 'than 1.0 or be an integer' % test_size) elif np.asarray(test_size).dtype.kind == 'i': if test_size >= n: raise ValueError( 'test_size=%d should be smaller ' 'than the number of samples %d' % (test_size, n)) else: raise ValueError("Invalid value for test_size: %r" % test_size) if train_size is not None: if np.asarray(train_size).dtype.kind == 'f': if train_size >= 1.: raise ValueError("train_size=%f should be smaller " "than 1.0 or be an integer" % train_size) elif np.asarray(test_size).dtype.kind == 'f' and \ train_size + test_size > 1.: raise ValueError('The sum of test_size and train_size = %f, ' 'should be smaller than 1.0. Reduce ' 'test_size and/or train_size.' % (train_size + test_size)) elif np.asarray(train_size).dtype.kind == 'i': if train_size >= n: raise ValueError("train_size=%d should be smaller " "than the number of samples %d" % (train_size, n)) else: raise ValueError("Invalid value for train_size: %r" % train_size) if np.asarray(test_size).dtype.kind == 'f': n_test = ceil(test_size * n) elif np.asarray(test_size).dtype.kind == 'i': n_test = float(test_size) if train_size is None: n_train = n - n_test else: if np.asarray(train_size).dtype.kind == 'f': n_train = floor(train_size * n) else: n_train = float(train_size) if test_size is None: n_test = n - n_train if n_train + n_test > n: raise ValueError('The sum of train_size and test_size = %d, ' 'should be smaller than the number of ' 'samples %d. Reduce test_size and/or ' 'train_size.' % (n_train + n_test, n)) return int(n_train), int(n_test) class StratifiedShuffleSplit(BaseShuffleSplit): """Stratified ShuffleSplit cross validation iterator Provides train/test indices to split data in train test sets. This cross-validation object is a merge of StratifiedKFold and ShuffleSplit, which returns stratified randomized folds. The folds are made by preserving the percentage of samples for each class. Note: like the ShuffleSplit strategy, stratified random splits do not guarantee that all folds will be different, although this is still very likely for sizeable datasets. Read more in the :ref:`User Guide <cross_validation>`. Parameters ---------- y : array, [n_samples] Labels of samples. n_iter : int (default 10) Number of re-shuffling & splitting iterations. test_size : float (default 0.1), int, or None If float, should be between 0.0 and 1.0 and represent the proportion of the dataset to include in the test split. If int, represents the absolute number of test samples. If None, the value is automatically set to the complement of the train size. train_size : float, int, or None (default is None) If float, should be between 0.0 and 1.0 and represent the proportion of the dataset to include in the train split. If int, represents the absolute number of train samples. If None, the value is automatically set to the complement of the test size. random_state : int or RandomState Pseudo-random number generator state used for random sampling. Examples -------- >>> from sklearn.cross_validation import StratifiedShuffleSplit >>> X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]]) >>> y = np.array([0, 0, 1, 1]) >>> sss = StratifiedShuffleSplit(y, 3, test_size=0.5, random_state=0) >>> len(sss) 3 >>> print(sss) # doctest: +ELLIPSIS StratifiedShuffleSplit(labels=[0 0 1 1], n_iter=3, ...) >>> for train_index, test_index in sss: ... print("TRAIN:", train_index, "TEST:", test_index) ... X_train, X_test = X[train_index], X[test_index] ... y_train, y_test = y[train_index], y[test_index] TRAIN: [1 2] TEST: [3 0] TRAIN: [0 2] TEST: [1 3] TRAIN: [0 2] TEST: [3 1] """ def __init__(self, y, n_iter=10, test_size=0.1, train_size=None, random_state=None): super(StratifiedShuffleSplit, self).__init__( len(y), n_iter, test_size, train_size, random_state) self.y = np.array(y) self.classes, self.y_indices = np.unique(y, return_inverse=True) n_cls = self.classes.shape[0] if np.min(bincount(self.y_indices)) < 2: raise ValueError("The least populated class in y has only 1" " member, which is too few. The minimum" " number of labels for any class cannot" " be less than 2.") if self.n_train < n_cls: raise ValueError('The train_size = %d should be greater or ' 'equal to the number of classes = %d' % (self.n_train, n_cls)) if self.n_test < n_cls: raise ValueError('The test_size = %d should be greater or ' 'equal to the number of classes = %d' % (self.n_test, n_cls)) def _iter_indices(self): rng = check_random_state(self.random_state) cls_count = bincount(self.y_indices) p_i = cls_count / float(self.n) n_i = np.round(self.n_train * p_i).astype(int) t_i = np.minimum(cls_count - n_i, np.round(self.n_test * p_i).astype(int)) for n in range(self.n_iter): train = [] test = [] for i, cls in enumerate(self.classes): permutation = rng.permutation(cls_count[i]) cls_i = np.where((self.y == cls))[0][permutation] train.extend(cls_i[:n_i[i]]) test.extend(cls_i[n_i[i]:n_i[i] + t_i[i]]) # Because of rounding issues (as n_train and n_test are not # dividers of the number of elements per class), we may end # up here with less samples in train and test than asked for. if len(train) < self.n_train or len(test) < self.n_test: # We complete by affecting randomly the missing indexes missing_idx = np.where(bincount(train + test, minlength=len(self.y)) == 0, )[0] missing_idx = rng.permutation(missing_idx) train.extend(missing_idx[:(self.n_train - len(train))]) test.extend(missing_idx[-(self.n_test - len(test)):]) train = rng.permutation(train) test = rng.permutation(test) yield train, test def __repr__(self): return ('%s(labels=%s, n_iter=%d, test_size=%s, ' 'random_state=%s)' % ( self.__class__.__name__, self.y, self.n_iter, str(self.test_size), self.random_state, )) def __len__(self): return self.n_iter class PredefinedSplit(_PartitionIterator): """Predefined split cross validation iterator Splits the data into training/test set folds according to a predefined scheme. Each sample can be assigned to at most one test set fold, as specified by the user through the ``test_fold`` parameter. Read more in the :ref:`User Guide <cross_validation>`. Parameters ---------- test_fold : "array-like, shape (n_samples,) test_fold[i] gives the test set fold of sample i. A value of -1 indicates that the corresponding sample is not part of any test set folds, but will instead always be put into the training fold. Examples -------- >>> from sklearn.cross_validation import PredefinedSplit >>> X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]]) >>> y = np.array([0, 0, 1, 1]) >>> ps = PredefinedSplit(test_fold=[0, 1, -1, 1]) >>> len(ps) 2 >>> print(ps) # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS sklearn.cross_validation.PredefinedSplit(test_fold=[ 0 1 -1 1]) >>> for train_index, test_index in ps: ... print("TRAIN:", train_index, "TEST:", test_index) ... X_train, X_test = X[train_index], X[test_index] ... y_train, y_test = y[train_index], y[test_index] TRAIN: [1 2 3] TEST: [0] TRAIN: [0 2] TEST: [1 3] """ def __init__(self, test_fold): super(PredefinedSplit, self).__init__(len(test_fold)) self.test_fold = np.array(test_fold, dtype=np.int) self.test_fold = column_or_1d(self.test_fold) self.unique_folds = np.unique(self.test_fold) self.unique_folds = self.unique_folds[self.unique_folds != -1] def _iter_test_indices(self): for f in self.unique_folds: yield np.where(self.test_fold == f)[0] def __repr__(self): return '%s.%s(test_fold=%s)' % ( self.__class__.__module__, self.__class__.__name__, self.test_fold) def __len__(self): return len(self.unique_folds) ############################################################################## def _index_param_value(X, v, indices): """Private helper function for parameter value indexing.""" if not _is_arraylike(v) or _num_samples(v) != _num_samples(X): # pass through: skip indexing return v if sp.issparse(v): v = v.tocsr() return safe_indexing(v, indices) def cross_val_predict(estimator, X, y=None, cv=None, n_jobs=1, verbose=0, fit_params=None, pre_dispatch='2*n_jobs'): """Generate cross-validated estimates for each input data point Read more in the :ref:`User Guide <cross_validation>`. Parameters ---------- estimator : estimator object implementing 'fit' and 'predict' The object to use to fit the data. X : array-like The data to fit. Can be, for example a list, or an array at least 2d. y : array-like, optional, default: None The target variable to try to predict in the case of supervised learning. cv : cross-validation generator or int, optional, default: None A cross-validation generator to use. If int, determines the number of folds in StratifiedKFold if y is binary or multiclass and estimator is a classifier, or the number of folds in KFold otherwise. If None, it is equivalent to cv=3. This generator must include all elements in the test set exactly once. Otherwise, a ValueError is raised. n_jobs : integer, optional The number of CPUs to use to do the computation. -1 means 'all CPUs'. verbose : integer, optional The verbosity level. fit_params : dict, optional Parameters to pass to the fit method of the estimator. pre_dispatch : int, or string, optional Controls the number of jobs that get dispatched during parallel execution. Reducing this number can be useful to avoid an explosion of memory consumption when more jobs get dispatched than CPUs can process. This parameter can be: - None, in which case all the jobs are immediately created and spawned. Use this for lightweight and fast-running jobs, to avoid delays due to on-demand spawning of the jobs - An int, giving the exact number of total jobs that are spawned - A string, giving an expression as a function of n_jobs, as in '2*n_jobs' Returns ------- preds : ndarray This is the result of calling 'predict' """ X, y = indexable(X, y) cv = check_cv(cv, X, y, classifier=is_classifier(estimator)) # We clone the estimator to make sure that all the folds are # independent, and that it is pickle-able. parallel = Parallel(n_jobs=n_jobs, verbose=verbose, pre_dispatch=pre_dispatch) preds_blocks = parallel(delayed(_fit_and_predict)(clone(estimator), X, y, train, test, verbose, fit_params) for train, test in cv) p = np.concatenate([p for p, _ in preds_blocks]) locs = np.concatenate([loc for _, loc in preds_blocks]) if not _check_is_partition(locs, _num_samples(X)): raise ValueError('cross_val_predict only works for partitions') preds = p.copy() preds[locs] = p return preds def _fit_and_predict(estimator, X, y, train, test, verbose, fit_params): """Fit estimator and predict values for a given dataset split. Read more in the :ref:`User Guide <cross_validation>`. Parameters ---------- estimator : estimator object implementing 'fit' and 'predict' The object to use to fit the data. X : array-like of shape at least 2D The data to fit. y : array-like, optional, default: None The target variable to try to predict in the case of supervised learning. train : array-like, shape (n_train_samples,) Indices of training samples. test : array-like, shape (n_test_samples,) Indices of test samples. verbose : integer The verbosity level. fit_params : dict or None Parameters that will be passed to ``estimator.fit``. Returns ------- preds : sequence Result of calling 'estimator.predict' test : array-like This is the value of the test parameter """ # Adjust length of sample weights fit_params = fit_params if fit_params is not None else {} fit_params = dict([(k, _index_param_value(X, v, train)) for k, v in fit_params.items()]) X_train, y_train = _safe_split(estimator, X, y, train) X_test, _ = _safe_split(estimator, X, y, test, train) if y_train is None: estimator.fit(X_train, **fit_params) else: estimator.fit(X_train, y_train, **fit_params) preds = estimator.predict(X_test) return preds, test def _check_is_partition(locs, n): """Check whether locs is a reordering of the array np.arange(n) Parameters ---------- locs : ndarray integer array to test n : int number of expected elements Returns ------- is_partition : bool True iff sorted(locs) is range(n) """ if len(locs) != n: return False hit = np.zeros(n, bool) hit[locs] = True if not np.all(hit): return False return True def cross_val_score(estimator, X, y=None, scoring=None, cv=None, n_jobs=1, verbose=0, fit_params=None, pre_dispatch='2*n_jobs'): """Evaluate a score by cross-validation Read more in the :ref:`User Guide <cross_validation>`. Parameters ---------- estimator : estimator object implementing 'fit' The object to use to fit the data. X : array-like The data to fit. Can be, for example a list, or an array at least 2d. y : array-like, optional, default: None The target variable to try to predict in the case of supervised learning. scoring : string, callable or None, optional, default: None A string (see model evaluation documentation) or a scorer callable object / function with signature ``scorer(estimator, X, y)``. cv : cross-validation generator or int, optional, default: None A cross-validation generator to use. If int, determines the number of folds in StratifiedKFold if y is binary or multiclass and estimator is a classifier, or the number of folds in KFold otherwise. If None, it is equivalent to cv=3. n_jobs : integer, optional The number of CPUs to use to do the computation. -1 means 'all CPUs'. verbose : integer, optional The verbosity level. fit_params : dict, optional Parameters to pass to the fit method of the estimator. pre_dispatch : int, or string, optional Controls the number of jobs that get dispatched during parallel execution. Reducing this number can be useful to avoid an explosion of memory consumption when more jobs get dispatched than CPUs can process. This parameter can be: - None, in which case all the jobs are immediately created and spawned. Use this for lightweight and fast-running jobs, to avoid delays due to on-demand spawning of the jobs - An int, giving the exact number of total jobs that are spawned - A string, giving an expression as a function of n_jobs, as in '2*n_jobs' Returns ------- scores : array of float, shape=(len(list(cv)),) Array of scores of the estimator for each run of the cross validation. """ X, y = indexable(X, y) cv = check_cv(cv, X, y, classifier=is_classifier(estimator)) scorer = check_scoring(estimator, scoring=scoring) # We clone the estimator to make sure that all the folds are # independent, and that it is pickle-able. parallel = Parallel(n_jobs=n_jobs, verbose=verbose, pre_dispatch=pre_dispatch) scores = parallel(delayed(_fit_and_score)(clone(estimator), X, y, scorer, train, test, verbose, None, fit_params) for train, test in cv) return np.array(scores)[:, 0] class FitFailedWarning(RuntimeWarning): pass def _fit_and_score(estimator, X, y, scorer, train, test, verbose, parameters, fit_params, return_train_score=False, return_parameters=False, error_score='raise'): """Fit estimator and compute scores for a given dataset split. Parameters ---------- estimator : estimator object implementing 'fit' The object to use to fit the data. X : array-like of shape at least 2D The data to fit. y : array-like, optional, default: None The target variable to try to predict in the case of supervised learning. scorer : callable A scorer callable object / function with signature ``scorer(estimator, X, y)``. train : array-like, shape (n_train_samples,) Indices of training samples. test : array-like, shape (n_test_samples,) Indices of test samples. verbose : integer The verbosity level. error_score : 'raise' (default) or numeric Value to assign to the score if an error occurs in estimator fitting. If set to 'raise', the error is raised. If a numeric value is given, FitFailedWarning is raised. This parameter does not affect the refit step, which will always raise the error. parameters : dict or None Parameters to be set on the estimator. fit_params : dict or None Parameters that will be passed to ``estimator.fit``. return_train_score : boolean, optional, default: False Compute and return score on training set. return_parameters : boolean, optional, default: False Return parameters that has been used for the estimator. Returns ------- train_score : float, optional Score on training set, returned only if `return_train_score` is `True`. test_score : float Score on test set. n_test_samples : int Number of test samples. scoring_time : float Time spent for fitting and scoring in seconds. parameters : dict or None, optional The parameters that have been evaluated. """ if verbose > 1: if parameters is None: msg = "no parameters to be set" else: msg = '%s' % (', '.join('%s=%s' % (k, v) for k, v in parameters.items())) print("[CV] %s %s" % (msg, (64 - len(msg)) * '.')) # Adjust length of sample weights fit_params = fit_params if fit_params is not None else {} fit_params = dict([(k, _index_param_value(X, v, train)) for k, v in fit_params.items()]) if parameters is not None: estimator.set_params(**parameters) start_time = time.time() X_train, y_train = _safe_split(estimator, X, y, train) X_test, y_test = _safe_split(estimator, X, y, test, train) try: if y_train is None: estimator.fit(X_train, **fit_params) else: estimator.fit(X_train, y_train, **fit_params) except Exception as e: if error_score == 'raise': raise elif isinstance(error_score, numbers.Number): test_score = error_score if return_train_score: train_score = error_score warnings.warn("Classifier fit failed. The score on this train-test" " partition for these parameters will be set to %f. " "Details: \n%r" % (error_score, e), FitFailedWarning) else: raise ValueError("error_score must be the string 'raise' or a" " numeric value. (Hint: if using 'raise', please" " make sure that it has been spelled correctly.)" ) else: test_score = _score(estimator, X_test, y_test, scorer) if return_train_score: train_score = _score(estimator, X_train, y_train, scorer) scoring_time = time.time() - start_time if verbose > 2: msg += ", score=%f" % test_score if verbose > 1: end_msg = "%s -%s" % (msg, logger.short_format_time(scoring_time)) print("[CV] %s %s" % ((64 - len(end_msg)) * '.', end_msg)) ret = [train_score] if return_train_score else [] ret.extend([test_score, _num_samples(X_test), scoring_time]) if return_parameters: ret.append(parameters) return ret def _safe_split(estimator, X, y, indices, train_indices=None): """Create subset of dataset and properly handle kernels.""" if hasattr(estimator, 'kernel') and callable(estimator.kernel): # cannot compute the kernel values with custom function raise ValueError("Cannot use a custom kernel function. " "Precompute the kernel matrix instead.") if not hasattr(X, "shape"): if getattr(estimator, "_pairwise", False): raise ValueError("Precomputed kernels or affinity matrices have " "to be passed as arrays or sparse matrices.") X_subset = [X[idx] for idx in indices] else: if getattr(estimator, "_pairwise", False): # X is a precomputed square kernel matrix if X.shape[0] != X.shape[1]: raise ValueError("X should be a square kernel matrix") if train_indices is None: X_subset = X[np.ix_(indices, indices)] else: X_subset = X[np.ix_(indices, train_indices)] else: X_subset = safe_indexing(X, indices) if y is not None: y_subset = safe_indexing(y, indices) else: y_subset = None return X_subset, y_subset def _score(estimator, X_test, y_test, scorer): """Compute the score of an estimator on a given test set.""" if y_test is None: score = scorer(estimator, X_test) else: score = scorer(estimator, X_test, y_test) if not isinstance(score, numbers.Number): raise ValueError("scoring must return a number, got %s (%s) instead." % (str(score), type(score))) return score def _permutation_test_score(estimator, X, y, cv, scorer): """Auxiliary function for permutation_test_score""" avg_score = [] for train, test in cv: estimator.fit(X[train], y[train]) avg_score.append(scorer(estimator, X[test], y[test])) return np.mean(avg_score) def _shuffle(y, labels, random_state): """Return a shuffled copy of y eventually shuffle among same labels.""" if labels is None: ind = random_state.permutation(len(y)) else: ind = np.arange(len(labels)) for label in np.unique(labels): this_mask = (labels == label) ind[this_mask] = random_state.permutation(ind[this_mask]) return y[ind] def check_cv(cv, X=None, y=None, classifier=False): """Input checker utility for building a CV in a user friendly way. Parameters ---------- cv : int, a cv generator instance, or None The input specifying which cv generator to use. It can be an integer, in which case it is the number of folds in a KFold, None, in which case 3 fold is used, or another object, that will then be used as a cv generator. X : array-like The data the cross-val object will be applied on. y : array-like The target variable for a supervised learning problem. classifier : boolean optional Whether the task is a classification task, in which case stratified KFold will be used. Returns ------- checked_cv: a cross-validation generator instance. The return value is guaranteed to be a cv generator instance, whatever the input type. """ is_sparse = sp.issparse(X) if cv is None: cv = 3 if isinstance(cv, numbers.Integral): if classifier: if type_of_target(y) in ['binary', 'multiclass']: cv = StratifiedKFold(y, cv) else: cv = KFold(_num_samples(y), cv) else: if not is_sparse: n_samples = len(X) else: n_samples = X.shape[0] cv = KFold(n_samples, cv) return cv def permutation_test_score(estimator, X, y, cv=None, n_permutations=100, n_jobs=1, labels=None, random_state=0, verbose=0, scoring=None): """Evaluate the significance of a cross-validated score with permutations Read more in the :ref:`User Guide <cross_validation>`. Parameters ---------- estimator : estimator object implementing 'fit' The object to use to fit the data. X : array-like of shape at least 2D The data to fit. y : array-like The target variable to try to predict in the case of supervised learning. scoring : string, callable or None, optional, default: None A string (see model evaluation documentation) or a scorer callable object / function with signature ``scorer(estimator, X, y)``. cv : integer or cross-validation generator, optional If an integer is passed, it is the number of fold (default 3). Specific cross-validation objects can be passed, see sklearn.cross_validation module for the list of possible objects. n_permutations : integer, optional Number of times to permute ``y``. n_jobs : integer, optional The number of CPUs to use to do the computation. -1 means 'all CPUs'. labels : array-like of shape [n_samples] (optional) Labels constrain the permutation among groups of samples with a same label. random_state : RandomState or an int seed (0 by default) A random number generator instance to define the state of the random permutations generator. verbose : integer, optional The verbosity level. Returns ------- score : float The true score without permuting targets. permutation_scores : array, shape (n_permutations,) The scores obtained for each permutations. pvalue : float The returned value equals p-value if `scoring` returns bigger numbers for better scores (e.g., accuracy_score). If `scoring` is rather a loss function (i.e. when lower is better such as with `mean_squared_error`) then this is actually the complement of the p-value: 1 - p-value. Notes ----- This function implements Test 1 in: Ojala and Garriga. Permutation Tests for Studying Classifier Performance. The Journal of Machine Learning Research (2010) vol. 11 """ X, y = indexable(X, y) cv = check_cv(cv, X, y, classifier=is_classifier(estimator)) scorer = check_scoring(estimator, scoring=scoring) random_state = check_random_state(random_state) # We clone the estimator to make sure that all the folds are # independent, and that it is pickle-able. score = _permutation_test_score(clone(estimator), X, y, cv, scorer) permutation_scores = Parallel(n_jobs=n_jobs, verbose=verbose)( delayed(_permutation_test_score)( clone(estimator), X, _shuffle(y, labels, random_state), cv, scorer) for _ in range(n_permutations)) permutation_scores = np.array(permutation_scores) pvalue = (np.sum(permutation_scores >= score) + 1.0) / (n_permutations + 1) return score, permutation_scores, pvalue permutation_test_score.__test__ = False # to avoid a pb with nosetests def train_test_split(*arrays, **options): """Split arrays or matrices into random train and test subsets Quick utility that wraps input validation and ``next(iter(ShuffleSplit(n_samples)))`` and application to input data into a single call for splitting (and optionally subsampling) data in a oneliner. Read more in the :ref:`User Guide <cross_validation>`. Parameters ---------- *arrays : sequence of arrays or scipy.sparse matrices with same shape[0] Python lists or tuples occurring in arrays are converted to 1D numpy arrays. test_size : float, int, or None (default is None) If float, should be between 0.0 and 1.0 and represent the proportion of the dataset to include in the test split. If int, represents the absolute number of test samples. If None, the value is automatically set to the complement of the train size. If train size is also None, test size is set to 0.25. train_size : float, int, or None (default is None) If float, should be between 0.0 and 1.0 and represent the proportion of the dataset to include in the train split. If int, represents the absolute number of train samples. If None, the value is automatically set to the complement of the test size. random_state : int or RandomState Pseudo-random number generator state used for random sampling. stratify : array-like or None (default is None) If not None, data is split in a stratified fashion, using this as the labels array. Returns ------- splitting : list of arrays, length=2 * len(arrays) List containing train-test split of input array. Examples -------- >>> import numpy as np >>> from sklearn.cross_validation import train_test_split >>> X, y = np.arange(10).reshape((5, 2)), range(5) >>> X array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]) >>> list(y) [0, 1, 2, 3, 4] >>> X_train, X_test, y_train, y_test = train_test_split( ... X, y, test_size=0.33, random_state=42) ... >>> X_train array([[4, 5], [0, 1], [6, 7]]) >>> y_train [2, 0, 3] >>> X_test array([[2, 3], [8, 9]]) >>> y_test [1, 4] """ n_arrays = len(arrays) if n_arrays == 0: raise ValueError("At least one array required as input") test_size = options.pop('test_size', None) train_size = options.pop('train_size', None) random_state = options.pop('random_state', None) dtype = options.pop('dtype', None) if dtype is not None: warnings.warn("dtype option is ignored and will be removed in 0.18.", DeprecationWarning) allow_nd = options.pop('allow_nd', None) allow_lists = options.pop('allow_lists', None) stratify = options.pop('stratify', None) if allow_lists is not None: warnings.warn("The allow_lists option is deprecated and will be " "assumed True in 0.18 and removed.", DeprecationWarning) if options: raise TypeError("Invalid parameters passed: %s" % str(options)) if allow_nd is not None: warnings.warn("The allow_nd option is deprecated and will be " "assumed True in 0.18 and removed.", DeprecationWarning) if allow_lists is False or allow_nd is False: arrays = [check_array(x, 'csr', allow_nd=allow_nd, force_all_finite=False, ensure_2d=False) if x is not None else x for x in arrays] if test_size is None and train_size is None: test_size = 0.25 arrays = indexable(*arrays) if stratify is not None: cv = StratifiedShuffleSplit(stratify, test_size=test_size, train_size=train_size, random_state=random_state) else: n_samples = _num_samples(arrays[0]) cv = ShuffleSplit(n_samples, test_size=test_size, train_size=train_size, random_state=random_state) train, test = next(iter(cv)) return list(chain.from_iterable((safe_indexing(a, train), safe_indexing(a, test)) for a in arrays)) train_test_split.__test__ = False # to avoid a pb with nosetests
bsd-3-clause
wazeerzulfikar/scikit-learn
sklearn/linear_model/tests/test_base.py
33
17862
# Author: Alexandre Gramfort <alexandre.gramfort@inria.fr> # Fabian Pedregosa <fabian.pedregosa@inria.fr> # # License: BSD 3 clause import numpy as np from scipy import sparse from scipy import linalg from itertools import product from sklearn.utils.testing import assert_array_almost_equal from sklearn.utils.testing import assert_almost_equal from sklearn.utils.testing import assert_equal from sklearn.utils.testing import ignore_warnings from sklearn.linear_model.base import LinearRegression from sklearn.linear_model.base import _preprocess_data from sklearn.linear_model.base import sparse_center_data, center_data from sklearn.linear_model.base import _rescale_data from sklearn.utils import check_random_state from sklearn.utils.testing import assert_greater from sklearn.datasets.samples_generator import make_sparse_uncorrelated from sklearn.datasets.samples_generator import make_regression rng = np.random.RandomState(0) def test_linear_regression(): # Test LinearRegression on a simple dataset. # a simple dataset X = [[1], [2]] Y = [1, 2] reg = LinearRegression() reg.fit(X, Y) assert_array_almost_equal(reg.coef_, [1]) assert_array_almost_equal(reg.intercept_, [0]) assert_array_almost_equal(reg.predict(X), [1, 2]) # test it also for degenerate input X = [[1]] Y = [0] reg = LinearRegression() reg.fit(X, Y) assert_array_almost_equal(reg.coef_, [0]) assert_array_almost_equal(reg.intercept_, [0]) assert_array_almost_equal(reg.predict(X), [0]) def test_linear_regression_sample_weights(): # TODO: loop over sparse data as well rng = np.random.RandomState(0) # It would not work with under-determined systems for n_samples, n_features in ((6, 5), ): y = rng.randn(n_samples) X = rng.randn(n_samples, n_features) sample_weight = 1.0 + rng.rand(n_samples) for intercept in (True, False): # LinearRegression with explicit sample_weight reg = LinearRegression(fit_intercept=intercept) reg.fit(X, y, sample_weight=sample_weight) coefs1 = reg.coef_ inter1 = reg.intercept_ assert_equal(reg.coef_.shape, (X.shape[1], )) # sanity checks assert_greater(reg.score(X, y), 0.5) # Closed form of the weighted least square # theta = (X^T W X)^(-1) * X^T W y W = np.diag(sample_weight) if intercept is False: X_aug = X else: dummy_column = np.ones(shape=(n_samples, 1)) X_aug = np.concatenate((dummy_column, X), axis=1) coefs2 = linalg.solve(X_aug.T.dot(W).dot(X_aug), X_aug.T.dot(W).dot(y)) if intercept is False: assert_array_almost_equal(coefs1, coefs2) else: assert_array_almost_equal(coefs1, coefs2[1:]) assert_almost_equal(inter1, coefs2[0]) def test_raises_value_error_if_sample_weights_greater_than_1d(): # Sample weights must be either scalar or 1D n_sampless = [2, 3] n_featuress = [3, 2] for n_samples, n_features in zip(n_sampless, n_featuress): X = rng.randn(n_samples, n_features) y = rng.randn(n_samples) sample_weights_OK = rng.randn(n_samples) ** 2 + 1 sample_weights_OK_1 = 1. sample_weights_OK_2 = 2. reg = LinearRegression() # make sure the "OK" sample weights actually work reg.fit(X, y, sample_weights_OK) reg.fit(X, y, sample_weights_OK_1) reg.fit(X, y, sample_weights_OK_2) def test_fit_intercept(): # Test assertions on betas shape. X2 = np.array([[0.38349978, 0.61650022], [0.58853682, 0.41146318]]) X3 = np.array([[0.27677969, 0.70693172, 0.01628859], [0.08385139, 0.20692515, 0.70922346]]) y = np.array([1, 1]) lr2_without_intercept = LinearRegression(fit_intercept=False).fit(X2, y) lr2_with_intercept = LinearRegression(fit_intercept=True).fit(X2, y) lr3_without_intercept = LinearRegression(fit_intercept=False).fit(X3, y) lr3_with_intercept = LinearRegression(fit_intercept=True).fit(X3, y) assert_equal(lr2_with_intercept.coef_.shape, lr2_without_intercept.coef_.shape) assert_equal(lr3_with_intercept.coef_.shape, lr3_without_intercept.coef_.shape) assert_equal(lr2_without_intercept.coef_.ndim, lr3_without_intercept.coef_.ndim) def test_linear_regression_sparse(random_state=0): # Test that linear regression also works with sparse data random_state = check_random_state(random_state) for i in range(10): n = 100 X = sparse.eye(n, n) beta = random_state.rand(n) y = X * beta[:, np.newaxis] ols = LinearRegression() ols.fit(X, y.ravel()) assert_array_almost_equal(beta, ols.coef_ + ols.intercept_) assert_array_almost_equal(ols.predict(X) - y.ravel(), 0) def test_linear_regression_multiple_outcome(random_state=0): # Test multiple-outcome linear regressions X, y = make_regression(random_state=random_state) Y = np.vstack((y, y)).T n_features = X.shape[1] reg = LinearRegression(fit_intercept=True) reg.fit((X), Y) assert_equal(reg.coef_.shape, (2, n_features)) Y_pred = reg.predict(X) reg.fit(X, y) y_pred = reg.predict(X) assert_array_almost_equal(np.vstack((y_pred, y_pred)).T, Y_pred, decimal=3) def test_linear_regression_sparse_multiple_outcome(random_state=0): # Test multiple-outcome linear regressions with sparse data random_state = check_random_state(random_state) X, y = make_sparse_uncorrelated(random_state=random_state) X = sparse.coo_matrix(X) Y = np.vstack((y, y)).T n_features = X.shape[1] ols = LinearRegression() ols.fit(X, Y) assert_equal(ols.coef_.shape, (2, n_features)) Y_pred = ols.predict(X) ols.fit(X, y.ravel()) y_pred = ols.predict(X) assert_array_almost_equal(np.vstack((y_pred, y_pred)).T, Y_pred, decimal=3) def test_preprocess_data(): n_samples = 200 n_features = 2 X = rng.rand(n_samples, n_features) y = rng.rand(n_samples) expected_X_mean = np.mean(X, axis=0) expected_X_norm = np.std(X, axis=0) * np.sqrt(X.shape[0]) expected_y_mean = np.mean(y, axis=0) Xt, yt, X_mean, y_mean, X_norm = \ _preprocess_data(X, y, fit_intercept=False, normalize=False) assert_array_almost_equal(X_mean, np.zeros(n_features)) assert_array_almost_equal(y_mean, 0) assert_array_almost_equal(X_norm, np.ones(n_features)) assert_array_almost_equal(Xt, X) assert_array_almost_equal(yt, y) Xt, yt, X_mean, y_mean, X_norm = \ _preprocess_data(X, y, fit_intercept=True, normalize=False) assert_array_almost_equal(X_mean, expected_X_mean) assert_array_almost_equal(y_mean, expected_y_mean) assert_array_almost_equal(X_norm, np.ones(n_features)) assert_array_almost_equal(Xt, X - expected_X_mean) assert_array_almost_equal(yt, y - expected_y_mean) Xt, yt, X_mean, y_mean, X_norm = \ _preprocess_data(X, y, fit_intercept=True, normalize=True) assert_array_almost_equal(X_mean, expected_X_mean) assert_array_almost_equal(y_mean, expected_y_mean) assert_array_almost_equal(X_norm, expected_X_norm) assert_array_almost_equal(Xt, (X - expected_X_mean) / expected_X_norm) assert_array_almost_equal(yt, y - expected_y_mean) def test_preprocess_data_multioutput(): n_samples = 200 n_features = 3 n_outputs = 2 X = rng.rand(n_samples, n_features) y = rng.rand(n_samples, n_outputs) expected_y_mean = np.mean(y, axis=0) args = [X, sparse.csc_matrix(X)] for X in args: _, yt, _, y_mean, _ = _preprocess_data(X, y, fit_intercept=False, normalize=False) assert_array_almost_equal(y_mean, np.zeros(n_outputs)) assert_array_almost_equal(yt, y) _, yt, _, y_mean, _ = _preprocess_data(X, y, fit_intercept=True, normalize=False) assert_array_almost_equal(y_mean, expected_y_mean) assert_array_almost_equal(yt, y - y_mean) _, yt, _, y_mean, _ = _preprocess_data(X, y, fit_intercept=True, normalize=True) assert_array_almost_equal(y_mean, expected_y_mean) assert_array_almost_equal(yt, y - y_mean) def test_preprocess_data_weighted(): n_samples = 200 n_features = 2 X = rng.rand(n_samples, n_features) y = rng.rand(n_samples) sample_weight = rng.rand(n_samples) expected_X_mean = np.average(X, axis=0, weights=sample_weight) expected_y_mean = np.average(y, axis=0, weights=sample_weight) # XXX: if normalize=True, should we expect a weighted standard deviation? # Currently not weighted, but calculated with respect to weighted mean expected_X_norm = (np.sqrt(X.shape[0]) * np.mean((X - expected_X_mean) ** 2, axis=0) ** .5) Xt, yt, X_mean, y_mean, X_norm = \ _preprocess_data(X, y, fit_intercept=True, normalize=False, sample_weight=sample_weight) assert_array_almost_equal(X_mean, expected_X_mean) assert_array_almost_equal(y_mean, expected_y_mean) assert_array_almost_equal(X_norm, np.ones(n_features)) assert_array_almost_equal(Xt, X - expected_X_mean) assert_array_almost_equal(yt, y - expected_y_mean) Xt, yt, X_mean, y_mean, X_norm = \ _preprocess_data(X, y, fit_intercept=True, normalize=True, sample_weight=sample_weight) assert_array_almost_equal(X_mean, expected_X_mean) assert_array_almost_equal(y_mean, expected_y_mean) assert_array_almost_equal(X_norm, expected_X_norm) assert_array_almost_equal(Xt, (X - expected_X_mean) / expected_X_norm) assert_array_almost_equal(yt, y - expected_y_mean) def test_sparse_preprocess_data_with_return_mean(): n_samples = 200 n_features = 2 # random_state not supported yet in sparse.rand X = sparse.rand(n_samples, n_features, density=.5) # , random_state=rng X = X.tolil() y = rng.rand(n_samples) XA = X.toarray() expected_X_norm = np.std(XA, axis=0) * np.sqrt(X.shape[0]) Xt, yt, X_mean, y_mean, X_norm = \ _preprocess_data(X, y, fit_intercept=False, normalize=False, return_mean=True) assert_array_almost_equal(X_mean, np.zeros(n_features)) assert_array_almost_equal(y_mean, 0) assert_array_almost_equal(X_norm, np.ones(n_features)) assert_array_almost_equal(Xt.A, XA) assert_array_almost_equal(yt, y) Xt, yt, X_mean, y_mean, X_norm = \ _preprocess_data(X, y, fit_intercept=True, normalize=False, return_mean=True) assert_array_almost_equal(X_mean, np.mean(XA, axis=0)) assert_array_almost_equal(y_mean, np.mean(y, axis=0)) assert_array_almost_equal(X_norm, np.ones(n_features)) assert_array_almost_equal(Xt.A, XA) assert_array_almost_equal(yt, y - np.mean(y, axis=0)) Xt, yt, X_mean, y_mean, X_norm = \ _preprocess_data(X, y, fit_intercept=True, normalize=True, return_mean=True) assert_array_almost_equal(X_mean, np.mean(XA, axis=0)) assert_array_almost_equal(y_mean, np.mean(y, axis=0)) assert_array_almost_equal(X_norm, expected_X_norm) assert_array_almost_equal(Xt.A, XA / expected_X_norm) assert_array_almost_equal(yt, y - np.mean(y, axis=0)) def test_csr_preprocess_data(): # Test output format of _preprocess_data, when input is csr X, y = make_regression() X[X < 2.5] = 0.0 csr = sparse.csr_matrix(X) csr_, y, _, _, _ = _preprocess_data(csr, y, True) assert_equal(csr_.getformat(), 'csr') def test_dtype_preprocess_data(): n_samples = 200 n_features = 2 X = rng.rand(n_samples, n_features) y = rng.rand(n_samples) X_32 = np.asarray(X, dtype=np.float32) y_32 = np.asarray(y, dtype=np.float32) X_64 = np.asarray(X, dtype=np.float64) y_64 = np.asarray(y, dtype=np.float64) for fit_intercept in [True, False]: for normalize in [True, False]: Xt_32, yt_32, X_mean_32, y_mean_32, X_norm_32 = _preprocess_data( X_32, y_32, fit_intercept=fit_intercept, normalize=normalize, return_mean=True) Xt_64, yt_64, X_mean_64, y_mean_64, X_norm_64 = _preprocess_data( X_64, y_64, fit_intercept=fit_intercept, normalize=normalize, return_mean=True) Xt_3264, yt_3264, X_mean_3264, y_mean_3264, X_norm_3264 = ( _preprocess_data(X_32, y_64, fit_intercept=fit_intercept, normalize=normalize, return_mean=True)) Xt_6432, yt_6432, X_mean_6432, y_mean_6432, X_norm_6432 = ( _preprocess_data(X_64, y_32, fit_intercept=fit_intercept, normalize=normalize, return_mean=True)) assert_equal(Xt_32.dtype, np.float32) assert_equal(yt_32.dtype, np.float32) assert_equal(X_mean_32.dtype, np.float32) assert_equal(y_mean_32.dtype, np.float32) assert_equal(X_norm_32.dtype, np.float32) assert_equal(Xt_64.dtype, np.float64) assert_equal(yt_64.dtype, np.float64) assert_equal(X_mean_64.dtype, np.float64) assert_equal(y_mean_64.dtype, np.float64) assert_equal(X_norm_64.dtype, np.float64) assert_equal(Xt_3264.dtype, np.float32) assert_equal(yt_3264.dtype, np.float32) assert_equal(X_mean_3264.dtype, np.float32) assert_equal(y_mean_3264.dtype, np.float32) assert_equal(X_norm_3264.dtype, np.float32) assert_equal(Xt_6432.dtype, np.float64) assert_equal(yt_6432.dtype, np.float64) assert_equal(X_mean_6432.dtype, np.float64) assert_equal(y_mean_6432.dtype, np.float64) assert_equal(X_norm_6432.dtype, np.float64) assert_equal(X_32.dtype, np.float32) assert_equal(y_32.dtype, np.float32) assert_equal(X_64.dtype, np.float64) assert_equal(y_64.dtype, np.float64) assert_array_almost_equal(Xt_32, Xt_64) assert_array_almost_equal(yt_32, yt_64) assert_array_almost_equal(X_mean_32, X_mean_64) assert_array_almost_equal(y_mean_32, y_mean_64) assert_array_almost_equal(X_norm_32, X_norm_64) def test_rescale_data(): n_samples = 200 n_features = 2 sample_weight = 1.0 + rng.rand(n_samples) X = rng.rand(n_samples, n_features) y = rng.rand(n_samples) rescaled_X, rescaled_y = _rescale_data(X, y, sample_weight) rescaled_X2 = X * np.sqrt(sample_weight)[:, np.newaxis] rescaled_y2 = y * np.sqrt(sample_weight) assert_array_almost_equal(rescaled_X, rescaled_X2) assert_array_almost_equal(rescaled_y, rescaled_y2) @ignore_warnings # all deprecation warnings def test_deprecation_center_data(): n_samples = 200 n_features = 2 w = 1.0 + rng.rand(n_samples) X = rng.rand(n_samples, n_features) y = rng.rand(n_samples) param_grid = product([True, False], [True, False], [True, False], [None, w]) for (fit_intercept, normalize, copy, sample_weight) in param_grid: XX = X.copy() # such that we can try copy=False as well X1, y1, X1_mean, X1_var, y1_mean = \ center_data(XX, y, fit_intercept=fit_intercept, normalize=normalize, copy=copy, sample_weight=sample_weight) XX = X.copy() X2, y2, X2_mean, X2_var, y2_mean = \ _preprocess_data(XX, y, fit_intercept=fit_intercept, normalize=normalize, copy=copy, sample_weight=sample_weight) assert_array_almost_equal(X1, X2) assert_array_almost_equal(y1, y2) assert_array_almost_equal(X1_mean, X2_mean) assert_array_almost_equal(X1_var, X2_var) assert_array_almost_equal(y1_mean, y2_mean) # Sparse cases X = sparse.csr_matrix(X) for (fit_intercept, normalize, copy, sample_weight) in param_grid: X1, y1, X1_mean, X1_var, y1_mean = \ center_data(X, y, fit_intercept=fit_intercept, normalize=normalize, copy=copy, sample_weight=sample_weight) X2, y2, X2_mean, X2_var, y2_mean = \ _preprocess_data(X, y, fit_intercept=fit_intercept, normalize=normalize, copy=copy, sample_weight=sample_weight, return_mean=False) assert_array_almost_equal(X1.toarray(), X2.toarray()) assert_array_almost_equal(y1, y2) assert_array_almost_equal(X1_mean, X2_mean) assert_array_almost_equal(X1_var, X2_var) assert_array_almost_equal(y1_mean, y2_mean) for (fit_intercept, normalize) in product([True, False], [True, False]): X1, y1, X1_mean, X1_var, y1_mean = \ sparse_center_data(X, y, fit_intercept=fit_intercept, normalize=normalize) X2, y2, X2_mean, X2_var, y2_mean = \ _preprocess_data(X, y, fit_intercept=fit_intercept, normalize=normalize, return_mean=True) assert_array_almost_equal(X1.toarray(), X2.toarray()) assert_array_almost_equal(y1, y2) assert_array_almost_equal(X1_mean, X2_mean) assert_array_almost_equal(X1_var, X2_var) assert_array_almost_equal(y1_mean, y2_mean)
bsd-3-clause