How to use binned_statistic_2d with Non-monotonic boundary?











up vote
0
down vote

favorite












I'm using Lambert projection which means if longitude is monotonic then latitude is non-monotonic.



It's difficult to using binned_statistic_2d like this:



from scipy import stats
x = [-118,-117.8,-117.7,-80,-70]
y = [18.1,18,18,17.8,18]
lon = [-118.09100342, -117.987854, -117.88466644, -117.78141785, -117.67811584, -72.32189178, -72.21858215, -72.11533356, -72.012146, -71.90899658]
lat = [17.94124222, 17.96657181, 17.99184036, 18.01698875, 18.04202652,
18.04202652, 18.01698875, 17.99184036, 17.96657181, 17.94124222]
binx = lon
biny = lat
ret = stats.binned_statistic_2d(x, y, None, 'count', bins=[binx,biny])


Error:



ValueError: bins must be monotonically increasing or decreasing


How to deal with this issue?



My solution




  1. Use zip to get boundaries of every grid;


  2. Add condition to make sure the order is correct



    for row in np.arange(lon_b.shape[0]-1):



    k = 0
    for lon_1,lat_1,lon_2,lat_2 in zip(*[iter(lon_b[row,:]),iter(lat_b[row,:])]*2):
    # Because binned_statistic_2d need bin_* increase monotonically
    if lat_1 < lat_2:
    CG_bin[row,k] = stats.binned_statistic_2d(lon_CG, lat_CG, None,
    'count', bins=[[lat_1,lat_2],[lon_1,lon_2]]).statistic
    IC_bin[row,k] = stats.binned_statistic_2d(lon_IC, lat_IC, None,
    'count', bins=[[lat_1,lat_2],[lon_1,lon_2]]).statistic
    else:
    CG_bin[row,k] = stats.binned_statistic_2d(lon_CG, lat_CG, None,
    'count', bins=[[lat_2,lat_1],[lon_1,lon_2]]).statistic
    IC_bin[row,k] = stats.binned_statistic_2d(lon_IC, lat_IC, None,
    'count', bins=[[lat_2,lat_1],[lon_1,lon_2]]).statistic

    k += 2

    k = 1
    for lon_1,lat_1,lon_2,lat_2 in zip(*[iter(lon_b[row,1:]),iter(lat_b[row,1:])]*2):
    if lat_1 < lat_2:
    CG_bin[row,k] = stats.binned_statistic_2d(lon_CG, lat_CG, None,
    'count', bins=[[lat_1,lat_2],[lon_1,lon_2]]).statistic
    IC_bin[row,k] = stats.binned_statistic_2d(lon_IC, lat_IC, None,
    'count', bins=[[lat_1,lat_2],[lon_1,lon_2]]).statistic
    else:
    CG_bin[row,k] = stats.binned_statistic_2d(lon_CG, lat_CG, None,
    'count', bins=[[lat_2,lat_1],[lon_1,lon_2]]).statistic
    IC_bin[row,k] = stats.binned_statistic_2d(lon_IC, lat_IC, None,
    'count', bins=[[lat_2,lat_1],[lon_1,lon_2]]).statistic
    k += 2











share|improve this question




























    up vote
    0
    down vote

    favorite












    I'm using Lambert projection which means if longitude is monotonic then latitude is non-monotonic.



    It's difficult to using binned_statistic_2d like this:



    from scipy import stats
    x = [-118,-117.8,-117.7,-80,-70]
    y = [18.1,18,18,17.8,18]
    lon = [-118.09100342, -117.987854, -117.88466644, -117.78141785, -117.67811584, -72.32189178, -72.21858215, -72.11533356, -72.012146, -71.90899658]
    lat = [17.94124222, 17.96657181, 17.99184036, 18.01698875, 18.04202652,
    18.04202652, 18.01698875, 17.99184036, 17.96657181, 17.94124222]
    binx = lon
    biny = lat
    ret = stats.binned_statistic_2d(x, y, None, 'count', bins=[binx,biny])


    Error:



    ValueError: bins must be monotonically increasing or decreasing


    How to deal with this issue?



    My solution




    1. Use zip to get boundaries of every grid;


    2. Add condition to make sure the order is correct



      for row in np.arange(lon_b.shape[0]-1):



      k = 0
      for lon_1,lat_1,lon_2,lat_2 in zip(*[iter(lon_b[row,:]),iter(lat_b[row,:])]*2):
      # Because binned_statistic_2d need bin_* increase monotonically
      if lat_1 < lat_2:
      CG_bin[row,k] = stats.binned_statistic_2d(lon_CG, lat_CG, None,
      'count', bins=[[lat_1,lat_2],[lon_1,lon_2]]).statistic
      IC_bin[row,k] = stats.binned_statistic_2d(lon_IC, lat_IC, None,
      'count', bins=[[lat_1,lat_2],[lon_1,lon_2]]).statistic
      else:
      CG_bin[row,k] = stats.binned_statistic_2d(lon_CG, lat_CG, None,
      'count', bins=[[lat_2,lat_1],[lon_1,lon_2]]).statistic
      IC_bin[row,k] = stats.binned_statistic_2d(lon_IC, lat_IC, None,
      'count', bins=[[lat_2,lat_1],[lon_1,lon_2]]).statistic

      k += 2

      k = 1
      for lon_1,lat_1,lon_2,lat_2 in zip(*[iter(lon_b[row,1:]),iter(lat_b[row,1:])]*2):
      if lat_1 < lat_2:
      CG_bin[row,k] = stats.binned_statistic_2d(lon_CG, lat_CG, None,
      'count', bins=[[lat_1,lat_2],[lon_1,lon_2]]).statistic
      IC_bin[row,k] = stats.binned_statistic_2d(lon_IC, lat_IC, None,
      'count', bins=[[lat_1,lat_2],[lon_1,lon_2]]).statistic
      else:
      CG_bin[row,k] = stats.binned_statistic_2d(lon_CG, lat_CG, None,
      'count', bins=[[lat_2,lat_1],[lon_1,lon_2]]).statistic
      IC_bin[row,k] = stats.binned_statistic_2d(lon_IC, lat_IC, None,
      'count', bins=[[lat_2,lat_1],[lon_1,lon_2]]).statistic
      k += 2











    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm using Lambert projection which means if longitude is monotonic then latitude is non-monotonic.



      It's difficult to using binned_statistic_2d like this:



      from scipy import stats
      x = [-118,-117.8,-117.7,-80,-70]
      y = [18.1,18,18,17.8,18]
      lon = [-118.09100342, -117.987854, -117.88466644, -117.78141785, -117.67811584, -72.32189178, -72.21858215, -72.11533356, -72.012146, -71.90899658]
      lat = [17.94124222, 17.96657181, 17.99184036, 18.01698875, 18.04202652,
      18.04202652, 18.01698875, 17.99184036, 17.96657181, 17.94124222]
      binx = lon
      biny = lat
      ret = stats.binned_statistic_2d(x, y, None, 'count', bins=[binx,biny])


      Error:



      ValueError: bins must be monotonically increasing or decreasing


      How to deal with this issue?



      My solution




      1. Use zip to get boundaries of every grid;


      2. Add condition to make sure the order is correct



        for row in np.arange(lon_b.shape[0]-1):



        k = 0
        for lon_1,lat_1,lon_2,lat_2 in zip(*[iter(lon_b[row,:]),iter(lat_b[row,:])]*2):
        # Because binned_statistic_2d need bin_* increase monotonically
        if lat_1 < lat_2:
        CG_bin[row,k] = stats.binned_statistic_2d(lon_CG, lat_CG, None,
        'count', bins=[[lat_1,lat_2],[lon_1,lon_2]]).statistic
        IC_bin[row,k] = stats.binned_statistic_2d(lon_IC, lat_IC, None,
        'count', bins=[[lat_1,lat_2],[lon_1,lon_2]]).statistic
        else:
        CG_bin[row,k] = stats.binned_statistic_2d(lon_CG, lat_CG, None,
        'count', bins=[[lat_2,lat_1],[lon_1,lon_2]]).statistic
        IC_bin[row,k] = stats.binned_statistic_2d(lon_IC, lat_IC, None,
        'count', bins=[[lat_2,lat_1],[lon_1,lon_2]]).statistic

        k += 2

        k = 1
        for lon_1,lat_1,lon_2,lat_2 in zip(*[iter(lon_b[row,1:]),iter(lat_b[row,1:])]*2):
        if lat_1 < lat_2:
        CG_bin[row,k] = stats.binned_statistic_2d(lon_CG, lat_CG, None,
        'count', bins=[[lat_1,lat_2],[lon_1,lon_2]]).statistic
        IC_bin[row,k] = stats.binned_statistic_2d(lon_IC, lat_IC, None,
        'count', bins=[[lat_1,lat_2],[lon_1,lon_2]]).statistic
        else:
        CG_bin[row,k] = stats.binned_statistic_2d(lon_CG, lat_CG, None,
        'count', bins=[[lat_2,lat_1],[lon_1,lon_2]]).statistic
        IC_bin[row,k] = stats.binned_statistic_2d(lon_IC, lat_IC, None,
        'count', bins=[[lat_2,lat_1],[lon_1,lon_2]]).statistic
        k += 2











      share|improve this question















      I'm using Lambert projection which means if longitude is monotonic then latitude is non-monotonic.



      It's difficult to using binned_statistic_2d like this:



      from scipy import stats
      x = [-118,-117.8,-117.7,-80,-70]
      y = [18.1,18,18,17.8,18]
      lon = [-118.09100342, -117.987854, -117.88466644, -117.78141785, -117.67811584, -72.32189178, -72.21858215, -72.11533356, -72.012146, -71.90899658]
      lat = [17.94124222, 17.96657181, 17.99184036, 18.01698875, 18.04202652,
      18.04202652, 18.01698875, 17.99184036, 17.96657181, 17.94124222]
      binx = lon
      biny = lat
      ret = stats.binned_statistic_2d(x, y, None, 'count', bins=[binx,biny])


      Error:



      ValueError: bins must be monotonically increasing or decreasing


      How to deal with this issue?



      My solution




      1. Use zip to get boundaries of every grid;


      2. Add condition to make sure the order is correct



        for row in np.arange(lon_b.shape[0]-1):



        k = 0
        for lon_1,lat_1,lon_2,lat_2 in zip(*[iter(lon_b[row,:]),iter(lat_b[row,:])]*2):
        # Because binned_statistic_2d need bin_* increase monotonically
        if lat_1 < lat_2:
        CG_bin[row,k] = stats.binned_statistic_2d(lon_CG, lat_CG, None,
        'count', bins=[[lat_1,lat_2],[lon_1,lon_2]]).statistic
        IC_bin[row,k] = stats.binned_statistic_2d(lon_IC, lat_IC, None,
        'count', bins=[[lat_1,lat_2],[lon_1,lon_2]]).statistic
        else:
        CG_bin[row,k] = stats.binned_statistic_2d(lon_CG, lat_CG, None,
        'count', bins=[[lat_2,lat_1],[lon_1,lon_2]]).statistic
        IC_bin[row,k] = stats.binned_statistic_2d(lon_IC, lat_IC, None,
        'count', bins=[[lat_2,lat_1],[lon_1,lon_2]]).statistic

        k += 2

        k = 1
        for lon_1,lat_1,lon_2,lat_2 in zip(*[iter(lon_b[row,1:]),iter(lat_b[row,1:])]*2):
        if lat_1 < lat_2:
        CG_bin[row,k] = stats.binned_statistic_2d(lon_CG, lat_CG, None,
        'count', bins=[[lat_1,lat_2],[lon_1,lon_2]]).statistic
        IC_bin[row,k] = stats.binned_statistic_2d(lon_IC, lat_IC, None,
        'count', bins=[[lat_1,lat_2],[lon_1,lon_2]]).statistic
        else:
        CG_bin[row,k] = stats.binned_statistic_2d(lon_CG, lat_CG, None,
        'count', bins=[[lat_2,lat_1],[lon_1,lon_2]]).statistic
        IC_bin[row,k] = stats.binned_statistic_2d(lon_IC, lat_IC, None,
        'count', bins=[[lat_2,lat_1],[lon_1,lon_2]]).statistic
        k += 2








      python scipy interpolation histogram2d






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 at 12:07

























      asked Nov 22 at 3:11









      Xin Zhang

      5518




      5518





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423331%2fhow-to-use-binned-statistic-2d-with-non-monotonic-boundary%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423331%2fhow-to-use-binned-statistic-2d-with-non-monotonic-boundary%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Catalogne

          Violoncelliste

          Héron pourpré