Changeset 168d359 in sasview for src/sas/sascalc


Ignore:
Timestamp:
Apr 8, 2017 7:54:23 AM (8 years ago)
Author:
Ricardo Ferraz Leal <ricleal@…>
Branches:
master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
c0ca2e3d
Parents:
fa4af76
Message:

Log binnning working for SectorQ

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sascalc/dataloader/manipulations.py

    rfa4af76 r168d359  
    44Using the meta data information, various types of averaging 
    55are performed in Q-space 
     6 
     7To test this module use: 
     8``` 
     9cd test 
     10PYTHONPATH=../src/ python2  -m sasdataloader.test.utest_averaging DataInfoTests.test_sectorphi_quarter 
     11``` 
    612""" 
    713##################################################################### 
     
    1319###################################################################### 
    1420 
    15 # If you want to run just a single test from this file: 
    16 # PYTHONPATH=../src/ python2  -m sasdataloader.test.utest_averaging data_info_tests.test_sectorq_full 
     21 
    1722# TODO: copy the meta data from the 2D object to the resulting 1D object 
    1823import math 
    1924import numpy as np 
    20  
     25import sys 
     26  
    2127#from data_info import plottable_2D 
    2228from data_info import Data1D 
     
    266272################################################################################ 
    267273 
     274class Binning(object): 
     275    ''' 
     276    This class just creates a binning object 
     277    either linear or log 
     278    ''' 
     279 
     280    def __init__(self, min_value, max_value, n_bins, base=None): 
     281        ''' 
     282        if base is None: Linear binning 
     283        ''' 
     284        self.min = min_value if min_value > 0 else 0.0001 
     285        self.max = max_value 
     286        self.n_bins = n_bins 
     287        self.base = base 
     288 
     289    def get_bin_index(self, value): 
     290        ''' 
     291        The general formula logarithm binning is: 
     292        bin = floor(N * (log(x) - log(min)) / (log(max) - log(min))) 
     293        ''' 
     294        if self.base: 
     295            temp_x = self.n_bins * (math.log(value, self.base) - math.log(self.min, self.base)) 
     296            temp_y = math.log(self.max, self.base) - math.log(self.min, self.base) 
     297        else: 
     298            temp_x = self.n_bins * (value - self.min) 
     299            temp_y = self.max - self.min 
     300        # Bin index calulation 
     301        return int(math.floor(temp_x / temp_y)) 
     302 
     303 
     304################################################################################ 
     305 
    268306class _Slab(object): 
    269307    """ 
     
    765803    """ 
    766804 
    767     def __init__(self, r_min, r_max, phi_min=0, phi_max=2 * math.pi, nbins=20): 
     805    def __init__(self, r_min, r_max, phi_min=0, phi_max=2 * math.pi, nbins=20, base = None): 
    768806        self.r_min = r_min 
    769807        self.r_max = r_max 
     
    771809        self.phi_max = phi_max 
    772810        self.nbins = nbins 
     811        self.base = base 
    773812 
    774813    def _agv(self, data2D, run='phi'): 
     
    805844        phi_min = flip_phi(self.phi_min) 
    806845        phi_max = flip_phi(self.phi_max) 
    807  
     846         
     847        #  binning object 
     848        if run.lower() == 'phi': 
     849            binning = Binning(self.phi_min, self.phi_max, self.nbins, self.base) 
     850        else: 
     851            binning = Binning(self.r_min, self.r_max, self.nbins, self.base) 
     852         
    808853        for n in range(len(data)): 
    809854 
     
    848893            if not is_in: 
    849894                continue 
    850          
    851             # Check which type of averaging we need 
     895 
     896            # Get the binning index 
    852897            if run.lower() == 'phi': 
    853                 temp_x = (self.nbins) * (phi_value - self.phi_min) 
    854                 temp_y = (self.phi_max - self.phi_min) 
     898                i_bin = binning.get_bin_index(phi_value) 
    855899            else: 
    856                 temp_x = (self.nbins) * (q_value - self.r_min) 
    857                 temp_y = (self.r_max - self.r_min) 
    858             # Bin index calulation 
    859             i_bin = int(math.floor(temp_x / temp_y)) 
     900                i_bin = binning.get_bin_index(q_value) 
    860901 
    861902            # Take care of the edge case at phi = 2pi. 
Note: See TracChangeset for help on using the changeset viewer.