idx
int64
0
63k
question
stringlengths
61
4.03k
target
stringlengths
6
1.23k
60,400
def _get_cmap_data ( data , kwargs ) -> Tuple [ colors . Normalize , np . ndarray ] : norm = kwargs . pop ( "cmap_normalize" , None ) if norm == "log" : cmap_max = kwargs . pop ( "cmap_max" , data . max ( ) ) cmap_min = kwargs . pop ( "cmap_min" , data [ data > 0 ] . min ( ) ) norm = colors . LogNorm ( cmap_min , cmap_...
Get normalized values to be used with a colormap .
60,401
def _get_alpha_data ( data : np . ndarray , kwargs ) -> Union [ float , np . ndarray ] : alpha = kwargs . pop ( "alpha" , 1 ) if hasattr ( alpha , "__call__" ) : return np . vectorize ( alpha ) ( data ) return alpha
Get alpha values for all data points .
60,402
def _add_values ( ax : Axes , h1 : Histogram1D , data , * , value_format = lambda x : x , ** kwargs ) : from . common import get_value_format value_format = get_value_format ( value_format ) text_kwargs = { "ha" : "center" , "va" : "bottom" , "clip_on" : True } text_kwargs . update ( kwargs ) for x , y in zip ( h1 . bi...
Show values next to each bin in a 1D plot .
60,403
def _add_colorbar ( ax : Axes , cmap : colors . Colormap , cmap_data : np . ndarray , norm : colors . Normalize ) : fig = ax . get_figure ( ) mappable = cm . ScalarMappable ( cmap = cmap , norm = norm ) mappable . set_array ( cmap_data ) fig . colorbar ( mappable , ax = ax )
Show a colorbar right of the plot .
60,404
def _add_stats_box ( h1 : Histogram1D , ax : Axes , stats : Union [ str , bool ] = "all" ) : if stats in [ "all" , True ] : text = "Total: {0}\nMean: {1:.2f}\nStd.dev: {2:.2f}" . format ( h1 . total , h1 . mean ( ) , h1 . std ( ) ) elif stats == "total" : text = "Total: {0}" . format ( h1 . total ) else : raise ValueEr...
Insert a small legend - like box with statistical information .
60,405
def normal_h1 ( size : int = 10000 , mean : float = 0 , sigma : float = 1 ) -> Histogram1D : data = np . random . normal ( mean , sigma , ( size , ) ) return h1 ( data , name = "normal" , axis_name = "x" , title = "1D normal distribution" )
A simple 1D histogram with normal distribution .
60,406
def normal_h2 ( size : int = 10000 ) -> Histogram2D : data1 = np . random . normal ( 0 , 1 , ( size , ) ) data2 = np . random . normal ( 0 , 1 , ( size , ) ) return h2 ( data1 , data2 , name = "normal" , axis_names = tuple ( "xy" ) , title = "2D normal distribution" )
A simple 2D histogram with normal distribution .
60,407
def normal_h3 ( size : int = 10000 ) -> HistogramND : data1 = np . random . normal ( 0 , 1 , ( size , ) ) data2 = np . random . normal ( 0 , 1 , ( size , ) ) data3 = np . random . normal ( 0 , 1 , ( size , ) ) return h3 ( [ data1 , data2 , data3 ] , name = "normal" , axis_names = tuple ( "xyz" ) , title = "3D normal di...
A simple 3D histogram with normal distribution .
60,408
def fist ( ) -> Histogram1D : import numpy as np from . . histogram1d import Histogram1D widths = [ 0 , 1.2 , 0.2 , 1 , 0.1 , 1 , 0.1 , 0.9 , 0.1 , 0.8 ] edges = np . cumsum ( widths ) heights = np . asarray ( [ 4 , 1 , 7.5 , 6 , 7.6 , 6 , 7.5 , 6 , 7.2 ] ) + 5 return Histogram1D ( edges , heights , axis_name = "Is thi...
A simple histogram in the shape of a fist .
60,409
def require_compatible_version ( compatible_version , word = "File" ) : if isinstance ( compatible_version , str ) : compatible_version = parse_version ( compatible_version ) elif not isinstance ( compatible_version , Version ) : raise ValueError ( "Type of `compatible_version` not understood." ) current_version = pars...
Check that compatible version of input data is not too new .
60,410
def save_json ( histogram : Union [ HistogramBase , HistogramCollection ] , path : Optional [ str ] = None , ** kwargs ) -> str : data = histogram . to_dict ( ) data [ "physt_version" ] = CURRENT_VERSION if isinstance ( histogram , HistogramBase ) : data [ "physt_compatible" ] = COMPATIBLE_VERSION elif isinstance ( his...
Save histogram to JSON format .
60,411
def load_json ( path : str , encoding : str = "utf-8" ) -> HistogramBase : with open ( path , "r" , encoding = encoding ) as f : text = f . read ( ) return parse_json ( text )
Load histogram from a JSON file .
60,412
def parse_json ( text : str , encoding : str = "utf-8" ) -> HistogramBase : data = json . loads ( text , encoding = encoding ) return create_from_dict ( data , format_name = "JSON" )
Create histogram from a JSON string .
60,413
def histogram ( data , bins = None , * args , ** kwargs ) : import numpy as np from . histogram1d import Histogram1D , calculate_frequencies from . binnings import calculate_bins adaptive = kwargs . pop ( "adaptive" , False ) dtype = kwargs . pop ( "dtype" , None ) if isinstance ( data , tuple ) and isinstance ( data [...
Facade function to create 1D histograms .
60,414
def histogram2d ( data1 , data2 , bins = 10 , * args , ** kwargs ) : import numpy as np if "axis_names" not in kwargs : if hasattr ( data1 , "name" ) and hasattr ( data2 , "name" ) : kwargs [ "axis_names" ] = [ data1 . name , data2 . name ] if data1 is not None and data2 is not None : data1 = np . asarray ( data1 ) dat...
Facade function to create 2D histograms .
60,415
def histogramdd ( data , bins = 10 , * args , ** kwargs ) : import numpy as np from . import histogram_nd from . binnings import calculate_bins_nd adaptive = kwargs . pop ( "adaptive" , False ) dropna = kwargs . pop ( "dropna" , True ) name = kwargs . pop ( "name" , None ) title = kwargs . pop ( "title" , None ) dim = ...
Facade function to create n - dimensional histograms .
60,416
def h3 ( data , * args , ** kwargs ) : import numpy as np if data is not None and isinstance ( data , ( list , tuple ) ) and not np . isscalar ( data [ 0 ] ) : if "axis_names" not in kwargs : kwargs [ "axis_names" ] = [ ( column . name if hasattr ( column , "name" ) else None ) for column in data ] data = np . concaten...
Facade function to create 3D histograms .
60,417
def collection ( data , bins = 10 , * args , ** kwargs ) : from physt . histogram_collection import HistogramCollection if hasattr ( data , "columns" ) : data = { column : data [ column ] for column in data . columns } return HistogramCollection . multi_h1 ( data , bins , ** kwargs )
Create histogram collection with shared binnning .
60,418
def write_root ( histogram : HistogramBase , hfile : uproot . write . TFile . TFileUpdate , name : str ) : hfile [ name ] = histogram
Write histogram to an open ROOT file .
60,419
def write ( histogram ) : histogram_dict = histogram . to_dict ( ) message = Histogram ( ) for field in SIMPLE_CONVERSION_FIELDS : setattr ( message , field , histogram_dict [ field ] ) message . frequencies . extend ( histogram . frequencies . flatten ( ) ) message . errors2 . extend ( histogram . errors2 . flatten ( ...
Convert a histogram to a protobuf message .
60,420
def read ( message ) : require_compatible_version ( message . physt_compatible ) a_dict = _dict_from_v0342 ( message ) return create_from_dict ( a_dict , "Message" )
Convert a parsed protobuf message into a histogram .
60,421
def make_bin_array ( bins ) -> np . ndarray : bins = np . asarray ( bins ) if bins . ndim == 1 : return np . hstack ( ( bins [ : - 1 , np . newaxis ] , bins [ 1 : , np . newaxis ] ) ) elif bins . ndim == 2 : if bins . shape [ 1 ] != 2 : raise RuntimeError ( "Binning schema with ndim==2 must have 2 columns" ) return bin...
Turn bin data into array understood by HistogramXX classes .
60,422
def to_numpy_bins ( bins ) -> np . ndarray : bins = np . asarray ( bins ) if bins . ndim == 1 : return bins if not is_consecutive ( bins ) : raise RuntimeError ( "Cannot create numpy bins from inconsecutive edges" ) return np . concatenate ( [ bins [ : 1 , 0 ] , bins [ : , 1 ] ] )
Convert physt bin format to numpy edges .
60,423
def to_numpy_bins_with_mask ( bins ) -> Tuple [ np . ndarray , np . ndarray ] : bins = np . asarray ( bins ) if bins . ndim == 1 : edges = bins if bins . shape [ 0 ] > 1 : mask = np . arange ( bins . shape [ 0 ] - 1 ) else : mask = [ ] elif bins . ndim == 2 : edges = [ ] mask = [ ] j = 0 if bins . shape [ 0 ] > 0 : edg...
Numpy binning edges including gaps .
60,424
def is_rising ( bins ) -> bool : bins = make_bin_array ( bins ) if np . any ( bins [ : , 0 ] >= bins [ : , 1 ] ) : return False if np . any ( bins [ 1 : , 0 ] < bins [ : - 1 , 1 ] ) : return False return True
Check whether the bins are in raising order .
60,425
def get_data ( histogram : HistogramBase , density : bool = False , cumulative : bool = False , flatten : bool = False ) -> np . ndarray : if density : if cumulative : data = ( histogram / histogram . total ) . cumulative_frequencies else : data = histogram . densities else : if cumulative : data = histogram . cumulati...
Get histogram data based on plotting parameters .
60,426
def get_err_data ( histogram : HistogramBase , density : bool = False , cumulative : bool = False , flatten : bool = False ) -> np . ndarray : if cumulative : raise RuntimeError ( "Error bars not supported for cumulative plots." ) if density : data = histogram . errors / histogram . bin_sizes else : data = histogram . ...
Get histogram error data based on plotting parameters .
60,427
def get_value_format ( value_format : Union [ Callable , str ] = str ) -> Callable [ [ float ] , str ] : if value_format is None : value_format = "" if isinstance ( value_format , str ) : format_str = "{0:" + value_format + "}" def value_format ( x ) : return format_str . format ( x ) return value_format
Create a formatting function from a generic value_format argument .
60,428
def pop_kwargs_with_prefix ( prefix : str , kwargs : dict ) -> dict : keys = [ key for key in kwargs if key . startswith ( prefix ) ] return { key [ len ( prefix ) : ] : kwargs . pop ( key ) for key in keys }
Pop all items from a dictionary that have keys beginning with a prefix .
60,429
def bins ( self ) -> List [ np . ndarray ] : return [ binning . bins for binning in self . _binnings ]
List of bin matrices .
60,430
def select ( self , axis : AxisIdentifier , index , force_copy : bool = False ) -> HistogramBase : if index == slice ( None ) and not force_copy : return self axis_id = self . _get_axis ( axis ) array_index = [ slice ( None , None , None ) for i in range ( self . ndim ) ] array_index [ axis_id ] = index frequencies = s...
Select in an axis .
60,431
def accumulate ( self , axis : AxisIdentifier ) -> HistogramBase : new_one = self . copy ( ) axis_id = self . _get_axis ( axis ) new_one . _frequencies = np . cumsum ( new_one . frequencies , axis_id [ 0 ] ) return new_one
Calculate cumulative frequencies along a certain axis .
60,432
def T ( self ) -> "Histogram2D" : a_copy = self . copy ( ) a_copy . _binnings = list ( reversed ( a_copy . _binnings ) ) a_copy . axis_names = list ( reversed ( a_copy . axis_names ) ) a_copy . _frequencies = a_copy . _frequencies . T a_copy . _errors2 = a_copy . _errors2 . T return a_copy
Histogram with swapped axes .
60,433
def partial_normalize ( self , axis : AxisIdentifier = 0 , inplace : bool = False ) : axis = self . _get_axis ( axis ) if not inplace : copy = self . copy ( ) copy . partial_normalize ( axis , inplace = True ) return copy else : self . _coerce_dtype ( float ) if axis == 0 : divisor = self . _frequencies . sum ( axis = ...
Normalize in rows or columns .
60,434
def numpy_binning ( data , bins = 10 , range = None , * args , ** kwargs ) -> NumpyBinning : if isinstance ( bins , int ) : if range : bins = np . linspace ( range [ 0 ] , range [ 1 ] , bins + 1 ) else : start = data . min ( ) stop = data . max ( ) bins = np . linspace ( start , stop , bins + 1 ) elif np . iterable ( b...
Construct binning schema compatible with numpy . histogram
60,435
def human_binning ( data = None , bin_count : Optional [ int ] = None , * , range = None , ** kwargs ) -> FixedWidthBinning : subscales = np . array ( [ 0.5 , 1 , 2 , 2.5 , 5 , 10 ] ) if data is None and range is None : raise RuntimeError ( "Cannot guess optimum bin width without data." ) if bin_count is None : bin_cou...
Construct fixed - width ninning schema with bins automatically optimized to human - friendly widths .
60,436
def quantile_binning ( data = None , bins = 10 , * , qrange = ( 0.0 , 1.0 ) , ** kwargs ) -> StaticBinning : if np . isscalar ( bins ) : bins = np . linspace ( qrange [ 0 ] * 100 , qrange [ 1 ] * 100 , bins + 1 ) bins = np . percentile ( data , bins ) return static_binning ( bins = make_bin_array ( bins ) , includes_ri...
Binning schema based on quantile ranges .
60,437
def static_binning ( data = None , bins = None , ** kwargs ) -> StaticBinning : return StaticBinning ( bins = make_bin_array ( bins ) , ** kwargs )
Construct static binning with whatever bins .
60,438
def integer_binning ( data = None , ** kwargs ) -> StaticBinning : if "range" in kwargs : kwargs [ "range" ] = tuple ( r - 0.5 for r in kwargs [ "range" ] ) return fixed_width_binning ( data = data , bin_width = kwargs . pop ( "bin_width" , 1 ) , align = True , bin_shift = 0.5 , ** kwargs )
Construct fixed - width binning schema with bins centered around integers .
60,439
def fixed_width_binning ( data = None , bin_width : Union [ float , int ] = 1 , * , range = None , includes_right_edge = False , ** kwargs ) -> FixedWidthBinning : result = FixedWidthBinning ( bin_width = bin_width , includes_right_edge = includes_right_edge , ** kwargs ) if range : result . _force_bin_existence ( rang...
Construct fixed - width binning schema .
60,440
def exponential_binning ( data = None , bin_count : Optional [ int ] = None , * , range = None , ** kwargs ) -> ExponentialBinning : if bin_count is None : bin_count = ideal_bin_count ( data ) if range : range = ( np . log10 ( range [ 0 ] ) , np . log10 ( range [ 1 ] ) ) else : range = ( np . log10 ( data . min ( ) ) ,...
Construct exponential binning schema .
60,441
def calculate_bins ( array , _ = None , * args , ** kwargs ) -> BinningBase : if array is not None : if kwargs . pop ( "check_nan" , True ) : if np . any ( np . isnan ( array ) ) : raise RuntimeError ( "Cannot calculate bins in presence of NaN's." ) if kwargs . get ( "range" , None ) : array = array [ ( array >= kwargs...
Find optimal binning from arguments .
60,442
def ideal_bin_count ( data , method : str = "default" ) -> int : n = data . size if n < 1 : return 1 if method == "default" : if n <= 32 : return 7 else : return ideal_bin_count ( data , "sturges" ) elif method == "sqrt" : return int ( np . ceil ( np . sqrt ( n ) ) ) elif method == "sturges" : return int ( np . ceil ( ...
A theoretically ideal bin count .
60,443
def as_binning ( obj , copy : bool = False ) -> BinningBase : if isinstance ( obj , BinningBase ) : if copy : return obj . copy ( ) else : return obj else : bins = make_bin_array ( obj ) return StaticBinning ( bins )
Ensure that an object is a binning
60,444
def to_dict ( self ) -> OrderedDict : result = OrderedDict ( ) result [ "adaptive" ] = self . _adaptive result [ "binning_type" ] = type ( self ) . __name__ self . _update_dict ( result ) return result
Dictionary representation of the binning schema .
60,445
def is_regular ( self , rtol : float = 1.e-5 , atol : float = 1.e-8 ) -> bool : return np . allclose ( np . diff ( self . bins [ 1 ] - self . bins [ 0 ] ) , 0.0 , rtol = rtol , atol = atol )
Whether all bins have the same width .
60,446
def is_consecutive ( self , rtol : float = 1.e-5 , atol : float = 1.e-8 ) -> bool : if self . inconsecutive_allowed : if self . _consecutive is None : if self . _numpy_bins is not None : self . _consecutive = True self . _consecutive = is_consecutive ( self . bins , rtol , atol ) return self . _consecutive else : retur...
Whether all bins are in a growing order .
60,447
def adapt ( self , other : 'BinningBase' ) : if np . array_equal ( self . bins , other . bins ) : return None , None elif not self . is_adaptive ( ) : raise RuntimeError ( "Cannot adapt non-adaptive binning." ) else : return self . _adapt ( other )
Adapt this binning so that it contains all bins of another binning .
60,448
def numpy_bins ( self ) -> np . ndarray : if self . _numpy_bins is None : self . _numpy_bins = to_numpy_bins ( self . bins ) return self . _numpy_bins
Bins in the numpy format
60,449
def numpy_bins_with_mask ( self ) -> Tuple [ np . ndarray , np . ndarray ] : bwm = to_numpy_bins_with_mask ( self . bins ) if not self . includes_right_edge : bwm [ 0 ] . append ( np . inf ) return bwm
Bins in the numpy format including the gaps in inconsecutive binnings .
60,450
def as_static ( self , copy : bool = True ) -> 'StaticBinning' : if copy : return StaticBinning ( bins = self . bins . copy ( ) , includes_right_edge = self . includes_right_edge ) else : return self
Convert binning to a static form .
60,451
def histogram1d ( data , bins = None , * args , ** kwargs ) : import dask if not hasattr ( data , "dask" ) : data = dask . array . from_array ( data , chunks = int ( data . shape [ 0 ] / options [ "chunk_split" ] ) ) if not kwargs . get ( "adaptive" , True ) : raise RuntimeError ( "Only adaptive histograms supported fo...
Facade function to create one - dimensional histogram using dask .
60,452
def histogram2d ( data1 , data2 , bins = None , * args , ** kwargs ) : import dask if "axis_names" not in kwargs : if hasattr ( data1 , "name" ) and hasattr ( data2 , "name" ) : kwargs [ "axis_names" ] = [ data1 . name , data2 . name ] if not hasattr ( data1 , "dask" ) : data1 = dask . array . from_array ( data1 , chun...
Facade function to create 2D histogram using dask .
60,453
def all_subclasses ( cls : type ) -> Tuple [ type , ... ] : subclasses = [ ] for subclass in cls . __subclasses__ ( ) : subclasses . append ( subclass ) subclasses . extend ( all_subclasses ( subclass ) ) return tuple ( subclasses )
All subclasses of a class .
60,454
def find_subclass ( base : type , name : str ) -> type : class_candidates = [ klass for klass in all_subclasses ( base ) if klass . __name__ == name ] if len ( class_candidates ) == 0 : raise RuntimeError ( "No \"{0}\" subclass of \"{1}\"." . format ( base . __name__ , name ) ) elif len ( class_candidates ) > 1 : raise...
Find a named subclass of a base class .
60,455
def add ( self , histogram : Histogram1D ) : if self . binning and not self . binning == histogram . binning : raise ValueError ( "Cannot add histogram with different binning." ) self . histograms . append ( histogram )
Add a histogram to the collection .
60,456
def normalize_bins ( self , inplace : bool = False ) -> "HistogramCollection" : col = self if inplace else self . copy ( ) sums = self . sum ( ) . frequencies for h in col . histograms : h . set_dtype ( float ) h . _frequencies /= sums h . _errors2 /= sums ** 2 return col
Normalize each bin in the collection so that the sum is 1 . 0 for each bin .
60,457
def multi_h1 ( cls , a_dict : Dict [ str , Any ] , bins = None , ** kwargs ) -> "HistogramCollection" : from physt . binnings import calculate_bins mega_values = np . concatenate ( list ( a_dict . values ( ) ) ) binning = calculate_bins ( mega_values , bins , ** kwargs ) title = kwargs . pop ( "title" , None ) name = k...
Create a collection from multiple datasets .
60,458
def to_json ( self , path : Optional [ str ] = None , ** kwargs ) -> str : from . io import save_json return save_json ( self , path , ** kwargs )
Convert to JSON representation .
60,459
def _get_axis ( self , name_or_index : AxisIdentifier ) -> int : if isinstance ( name_or_index , int ) : if name_or_index < 0 or name_or_index >= self . ndim : raise ValueError ( "No such axis, must be from 0 to {0}" . format ( self . ndim - 1 ) ) return name_or_index elif isinstance ( name_or_index , str ) : if name_o...
Get a zero - based index of an axis and check its existence .
60,460
def shape ( self ) -> Tuple [ int , ... ] : return tuple ( bins . bin_count for bins in self . _binnings )
Shape of histogram s data .
60,461
def set_dtype ( self , value , check : bool = True ) : value , type_info = self . _eval_dtype ( value ) if value == self . _dtype : return if self . dtype is None or np . can_cast ( self . dtype , value ) : pass elif check : if np . issubdtype ( value , np . integer ) : if self . dtype . kind == "f" : for array in ( se...
Change data type of the bin contents .
60,462
def _coerce_dtype ( self , other_dtype ) : if self . _dtype is None : new_dtype = np . dtype ( other_dtype ) else : new_dtype = np . find_common_type ( [ self . _dtype , np . dtype ( other_dtype ) ] , [ ] ) if new_dtype != self . dtype : self . set_dtype ( new_dtype )
Possibly change the bin content type to allow correct operations with other operand .
60,463
def normalize ( self , inplace : bool = False , percent : bool = False ) -> "HistogramBase" : if inplace : self /= self . total * ( .01 if percent else 1 ) return self else : return self / self . total * ( 100 if percent else 1 )
Normalize the histogram so that the total weight is equal to 1 .
60,464
def _change_binning ( self , new_binning , bin_map : Iterable [ Tuple [ int , int ] ] , axis : int = 0 ) : axis = int ( axis ) if axis < 0 or axis >= self . ndim : raise RuntimeError ( "Axis must be in range 0..(ndim-1)" ) self . _reshape_data ( new_binning . bin_count , bin_map , axis ) self . _binnings [ axis ] = new...
Set new binnning and update the bin contents according to a map .
60,465
def _reshape_data ( self , new_size , bin_map , axis = 0 ) : if bin_map is None : return else : new_shape = list ( self . shape ) new_shape [ axis ] = new_size new_frequencies = np . zeros ( new_shape , dtype = self . _frequencies . dtype ) new_errors2 = np . zeros ( new_shape , dtype = self . _frequencies . dtype ) se...
Reshape data to match new binning schema .
60,466
def _apply_bin_map ( self , old_frequencies , new_frequencies , old_errors2 , new_errors2 , bin_map , axis = 0 ) : if old_frequencies is not None and old_frequencies . shape [ axis ] > 0 : if isinstance ( bin_map , int ) : new_index = [ slice ( None ) for i in range ( self . ndim ) ] new_index [ axis ] = slice ( bin_ma...
Fill new data arrays using a map .
60,467
def has_same_bins ( self , other : "HistogramBase" ) -> bool : if self . shape != other . shape : return False elif self . ndim == 1 : return np . allclose ( self . bins , other . bins ) elif self . ndim > 1 : for i in range ( self . ndim ) : if not np . allclose ( self . bins [ i ] , other . bins [ i ] ) : return Fals...
Whether two histograms share the same binning .
60,468
def copy ( self , include_frequencies : bool = True ) -> "HistogramBase" : if include_frequencies : frequencies = np . copy ( self . frequencies ) missed = self . _missed . copy ( ) errors2 = np . copy ( self . errors2 ) stats = self . _stats or None else : frequencies = np . zeros_like ( self . _frequencies ) errors2 ...
Copy the histogram .
60,469
def to_dict ( self ) -> OrderedDict : result = OrderedDict ( ) result [ "histogram_type" ] = type ( self ) . __name__ result [ "binnings" ] = [ binning . to_dict ( ) for binning in self . _binnings ] result [ "frequencies" ] = self . frequencies . tolist ( ) result [ "dtype" ] = str ( np . dtype ( self . dtype ) ) resu...
Dictionary with all data in the histogram .
60,470
def _merge_meta_data ( cls , first : "HistogramBase" , second : "HistogramBase" ) -> dict : keys = set ( first . _meta_data . keys ( ) ) keys = keys . union ( set ( second . _meta_data . keys ( ) ) ) return { key : ( first . _meta_data . get ( key , None ) if first . _meta_data . get ( key , None ) == second . _meta_da...
Merge meta data of two histograms leaving only the equal values .
60,471
def mean ( self ) -> Optional [ float ] : if self . _stats : if self . total > 0 : return self . _stats [ "sum" ] / self . total else : return np . nan else : return None
Statistical mean of all values entered into histogram .
60,472
def std ( self ) -> Optional [ float ] : if self . _stats : return np . sqrt ( self . variance ( ) ) else : return None
Standard deviation of all values entered into histogram .
60,473
def variance ( self ) -> Optional [ float ] : if self . _stats : if self . total > 0 : return ( self . _stats [ "sum2" ] - self . _stats [ "sum" ] ** 2 / self . total ) / self . total else : return np . nan else : return None
Statistical variance of all values entered into histogram .
60,474
def find_bin ( self , value ) : ixbin = np . searchsorted ( self . bin_left_edges , value , side = "right" ) if ixbin == 0 : return - 1 elif ixbin == self . bin_count : if value <= self . bin_right_edges [ - 1 ] : return ixbin - 1 else : return self . bin_count elif value < self . bin_right_edges [ ixbin - 1 ] : return...
Index of bin corresponding to a value .
60,475
def fill ( self , value , weight = 1 ) : self . _coerce_dtype ( type ( weight ) ) if self . _binning . is_adaptive ( ) : map = self . _binning . force_bin_existence ( value ) self . _reshape_data ( self . _binning . bin_count , map ) ixbin = self . find_bin ( value ) if ixbin is None : self . overflow = np . nan self ....
Update histogram with a new value .
60,476
def fill_n ( self , values , weights = None , dropna : bool = True ) : values = np . asarray ( values ) if dropna : values = values [ ~ np . isnan ( values ) ] if self . _binning . is_adaptive ( ) : map = self . _binning . force_bin_existence ( values ) self . _reshape_data ( self . _binning . bin_count , map ) if weig...
Update histograms with a set of values .
60,477
def to_xarray ( self ) -> "xarray.Dataset" : import xarray as xr data_vars = { "frequencies" : xr . DataArray ( self . frequencies , dims = "bin" ) , "errors2" : xr . DataArray ( self . errors2 , dims = "bin" ) , "bins" : xr . DataArray ( self . bins , dims = ( "bin" , "x01" ) ) } coords = { } attrs = { "underflow" : s...
Convert to xarray . Dataset
60,478
def from_xarray ( cls , arr : "xarray.Dataset" ) -> "Histogram1D" : kwargs = { 'frequencies' : arr [ "frequencies" ] , 'binning' : arr [ "bins" ] , 'errors2' : arr [ "errors2" ] , 'overflow' : arr . attrs [ "overflow" ] , 'underflow' : arr . attrs [ "underflow" ] , 'keep_missed' : arr . attrs [ "keep_missed" ] } return...
Convert form xarray . Dataset
60,479
def set_default_backend ( name : str ) : global _default_backend if name == "bokeh" : raise RuntimeError ( "Support for bokeh has been discontinued. At some point, we may return to support holoviews." ) if not name in backends : raise RuntimeError ( "Backend {0} is not supported and cannot be set as default." . format ...
Choose a default backend .
60,480
def _get_backend ( name : str = None ) : if not backends : raise RuntimeError ( "No plotting backend available. Please, install matplotlib (preferred) or bokeh (limited)." ) if not name : name = _default_backend if name == "bokeh" : raise RuntimeError ( "Support for bokeh has been discontinued. At some point, we may re...
Get a plotting backend .
60,481
def plot ( histogram : HistogramBase , kind : Optional [ str ] = None , backend : Optional [ str ] = None , ** kwargs ) : backend_name , backend = _get_backend ( backend ) if kind is None : kinds = [ t for t in backend . types if histogram . ndim in backend . dims [ t ] ] if not kinds : raise RuntimeError ( "No plot ty...
Universal plotting function .
60,482
def enable_inline_view ( f ) : @ wraps ( f ) def wrapper ( hist , write_to = None , write_format = "auto" , display = "auto" , indent = 2 , ** kwargs ) : vega_data = f ( hist , ** kwargs ) if display is True and not VEGA_IPYTHON_PLUGIN_ENABLED : raise RuntimeError ( "Cannot display vega plot: {0}" . format ( VEGA_ERROR...
Decorator to enable in - line viewing in Python and saving to external file .
60,483
def write_vega ( vega_data , * , title : Optional [ str ] , write_to : str , write_format : str = "auto" , indent : int = 2 ) : spec = json . dumps ( vega_data , indent = indent ) if write_format == "html" or write_format is "auto" and write_to . endswith ( ".html" ) : output = HTML_TEMPLATE . replace ( "{{ title }}" ,...
Write vega dictionary to an external file .
60,484
def display_vega ( vega_data : dict , display : bool = True ) -> Union [ 'Vega' , dict ] : if VEGA_IPYTHON_PLUGIN_ENABLED and display : from vega3 import Vega return Vega ( vega_data ) else : return vega_data
Optionally display vega dictionary .
60,485
def bar ( h1 : Histogram1D , ** kwargs ) -> dict : vega = _create_figure ( kwargs ) _add_title ( h1 , vega , kwargs ) _create_scales ( h1 , vega , kwargs ) _create_axes ( h1 , vega , kwargs ) data = get_data ( h1 , kwargs . pop ( "density" , None ) , kwargs . pop ( "cumulative" , None ) ) . tolist ( ) lefts = h1 . bin_...
Bar plot of 1D histogram .
60,486
def scatter ( h1 : Histogram1D , ** kwargs ) -> dict : shape = kwargs . pop ( "shape" , DEFAULT_SCATTER_SHAPE ) mark_template = [ { "type" : "symbol" , "from" : { "data" : "series" } , "encode" : { "enter" : { "x" : { "scale" : "xscale" , "field" : "x" } , "y" : { "scale" : "yscale" , "field" : "y" } , "shape" : { "val...
Scatter plot of 1D histogram values .
60,487
def line ( h1 : Histogram1D , ** kwargs ) -> dict : lw = kwargs . pop ( "lw" , DEFAULT_STROKE_WIDTH ) mark_template = [ { "type" : "line" , "encode" : { "enter" : { "x" : { "scale" : "xscale" , "field" : "x" } , "y" : { "scale" : "yscale" , "field" : "y" } , "stroke" : { "scale" : "series" , "field" : "c" } , "strokeWi...
Line plot of 1D histogram values .
60,488
def _create_figure ( kwargs : Mapping [ str , Any ] ) -> dict : return { "$schema" : "https://vega.github.io/schema/vega/v3.json" , "width" : kwargs . pop ( "width" , DEFAULT_WIDTH ) , "height" : kwargs . pop ( "height" , DEFAULT_HEIGHT ) , "padding" : kwargs . pop ( "padding" , DEFAULT_PADDING ) }
Create basic dictionary object with figure properties .
60,489
def _create_scales ( hist : HistogramBase , vega : dict , kwargs : dict ) : if hist . ndim == 1 : bins0 = hist . bins . astype ( float ) else : bins0 = hist . bins [ 0 ] . astype ( float ) xlim = kwargs . pop ( "xlim" , "auto" ) ylim = kwargs . pop ( "ylim" , "auto" ) if xlim is "auto" : nice_x = True else : nice_x = F...
Find proper scales for axes .
60,490
def _create_axes ( hist : HistogramBase , vega : dict , kwargs : dict ) : xlabel = kwargs . pop ( "xlabel" , hist . axis_names [ 0 ] ) ylabel = kwargs . pop ( "ylabel" , hist . axis_names [ 1 ] if len ( hist . axis_names ) >= 2 else None ) vega [ "axes" ] = [ { "orient" : "bottom" , "scale" : "xscale" , "title" : xlabe...
Create axes in the figure .
60,491
def _create_tooltips ( hist : Histogram1D , vega : dict , kwargs : dict ) : if kwargs . pop ( "tooltips" , False ) : vega [ "signals" ] = vega . get ( "signals" , [ ] ) vega [ "signals" ] . append ( { "name" : "tooltip" , "value" : { } , "on" : [ { "events" : "rect:mouseover" , "update" : "datum" } , { "events" : "rect...
In one - dimensional plots show values above the value on hover .
60,492
def _add_title ( hist : HistogramBase , vega : dict , kwargs : dict ) : title = kwargs . pop ( "title" , hist . title ) if title : vega [ "title" ] = { "text" : title }
Display plot title if available .
60,493
def _prepare_data ( data , transformed , klass , * args , ** kwargs ) : data = np . asarray ( data ) if not transformed : data = klass . transform ( data ) dropna = kwargs . get ( "dropna" , False ) if dropna : data = data [ ~ np . isnan ( data ) . any ( axis = 1 ) ] return data
Transform data for binning .
60,494
def polar_histogram ( xdata , ydata , radial_bins = "numpy" , phi_bins = 16 , transformed = False , * args , ** kwargs ) : dropna = kwargs . pop ( "dropna" , True ) data = np . concatenate ( [ xdata [ : , np . newaxis ] , ydata [ : , np . newaxis ] ] , axis = 1 ) data = _prepare_data ( data , transformed = transformed ...
Facade construction function for the PolarHistogram .
60,495
def spherical_histogram ( data = None , radial_bins = "numpy" , theta_bins = 16 , phi_bins = 16 , transformed = False , * args , ** kwargs ) : dropna = kwargs . pop ( "dropna" , True ) data = _prepare_data ( data , transformed = transformed , klass = SphericalHistogram , dropna = dropna ) if isinstance ( theta_bins , i...
Facade construction function for the SphericalHistogram .
60,496
def cylindrical_histogram ( data = None , rho_bins = "numpy" , phi_bins = 16 , z_bins = "numpy" , transformed = False , * args , ** kwargs ) : dropna = kwargs . pop ( "dropna" , True ) data = _prepare_data ( data , transformed = transformed , klass = CylindricalHistogram , dropna = dropna ) if isinstance ( phi_bins , i...
Facade construction function for the CylindricalHistogram .
60,497
def projection ( self , * axes , ** kwargs ) : axes , _ = self . _get_projection_axes ( * axes ) axes = tuple ( sorted ( axes ) ) if axes in self . _projection_class_map : klass = self . _projection_class_map [ axes ] return HistogramND . projection ( self , * axes , type = klass , ** kwargs ) else : return HistogramND...
Projection to lower - dimensional histogram . The inheriting class should implement the _projection_class_map class attribute to suggest class for the projection . If the arguments don t match any of the map keys HistogramND is used .
60,498
def enable_collection ( f ) : @ wraps ( f ) def new_f ( h : AbstractHistogram1D , ** kwargs ) : from physt . histogram_collection import HistogramCollection if isinstance ( h , HistogramCollection ) : return f ( h , ** kwargs ) else : return f ( HistogramCollection ( h ) , ** kwargs ) return new_f
Call the wrapped function with a HistogramCollection as argument .
60,499
def bar ( h : Histogram2D , * , barmode : str = DEFAULT_BARMODE , alpha : float = DEFAULT_ALPHA , ** kwargs ) : get_data_kwargs = pop_many ( kwargs , "density" , "cumulative" , "flatten" ) data = [ go . Bar ( x = histogram . bin_centers , y = get_data ( histogram , ** get_data_kwargs ) , width = histogram . bin_widths ...
Bar plot .