{"action":"created","comment":{"url":"https://api.github.com/repos/gammapy/gammapy/pulls/comments/28024779","id":28024779,"diff_hunk":"@@ -1,2 +1,110 @@\n # Licensed under a 3-clause BSD style license - see LICENSE.rst\n from __future__ import print_function, division\n+\n+from gammapy.background import fill_acceptance_image\n+from gammapy.image import make_empty_image\n+\n+from astropy.io import fits\n+from astropy.coordinates import SkyCoord, Angle\n+from astropy.wcs import WCS\n+from astropy.wcs.utils import pixel_to_skycoord\n+import astropy.units as u\n+from astropy.units.quantity import Quantity\n+\n+import numpy as np\n+from scipy.integrate import quad\n+\n+def radial_gaussian_2D(r, sig):\n+    \"\"\"Radially symmetric Gaussian function for emulating acceptance curve.\n+\n+    Please give parameters in radians.\n+\n+    TODO: I can't define parameters as `~astropy.coordinates.Angle` because it conflicts with the integral call afterwards!!!\n+\n+    Parameters\n+    ----------\n+    r : `~numpy.ndarray`\n+    Radial coordinate.\n+    sig : float\n+    Gaussian width.\n+\n+    Returns\n+    -------\n+    val: `~numpy.ndarray`\n+    Gaussian evaluated at the requested radii.\n+    \"\"\"\n+    ##norm = 1./(sig * np.sqrt(2. * np.pi))\n+    ##norm *= 2. #due to definition only for r>=0\n+    ##norm /= (2.*np.pi) #due to radial symmetry\n+    norm = 1.\n+    val = norm * np.exp(-np.power(r, 2.) / (2. * np.power(sig, 2.)))\n+    return val\n+\n+\n+def test_fill_acceptance_image():\n+\n+    #create empty image\n+    image = make_empty_image()\n+\n+    #define center coordinate of the image\n+    #in FITS the reference pixel is given by CRPIXn, and its coordinate by CRVALn\n+    #the coordinate increment along the axis is given by CDELTn\n+    x = image.header['CRVAL1']\n+    y = image.header['CRVAL2']\n+\n+    #sanity checks: am I using galactic coordinates in degrees?\n+    assert image.header['CTYPE1'] == 'GLON-CAR', \"Error: x coordinate is not in Galactic coordinates!\"\n+    assert image.header['CTYPE2'] == 'GLAT-CAR', \"Error: y coordinate is not in Galactic coordinates!\"\n+    assert image.header['CUNIT1'] == 'deg', \"Error: x coordinate is not in degrees!\"\n+    assert image.header['CUNIT2'] == 'deg', \"Error: y coordinate is not in degrees!\"\n+\n+    center = SkyCoord(l=x*u.degree, b=y*u.degree, frame='galactic')\n+\n+    #define pixel sizes\n+    x_pix_size = Angle(abs(image.header['CDELT1'])*u.degree)\n+    y_pix_size = Angle(abs(image.header['CDELT2'])*u.degree)\n+\n+    #define radial acceptance and offset angles\n+    offset = Angle(np.arange(0., 30., 0.1), unit=u.degree)\n+    acceptance = np.zeros_like(offset)\n+    sigma = Angle(1.0, unit=u.degree) #gaussian width\n+    acceptance = radial_gaussian_2D(offset.to(u.radian).value, sigma.to(u.radian).value)\n+\n+    #fill acceptance in the image\n+    image = fill_acceptance_image(image, center, offset, acceptance)\n+\n+    #test: sum of the image (weigthed by the pixel areas) should be equal to the integral of the radial acceptance\n+\n+    #initialize WCS to the header of the image\n+    w = WCS(image.header)\n+\n+    #define grids of pixel coorinates\n+    nx, ny = image.data.shape\n+    xpix_coord_grid = np.zeros(image.shape)\n+    for y in range(0, ny):\n+        xpix_coord_grid[0:nx, y] = np.arange(0, nx)\n+    ypix_coord_grid = np.zeros(image.shape)\n+    for x in range(0, nx):\n+        ypix_coord_grid[x, 0:nx] = np.arange(0, ny)\n+\n+    #calculate pixel coordinates (in world coordinates)\n+    coord = pixel_to_skycoord(xpix_coord_grid, ypix_coord_grid, w, 1)\n+\n+    #pixel area = delta x * delta y * cos(zenith)\n+    pix_area_grid = np.cos(coord.l.to(u.radian))*x_pix_size.to(u.radian)*y_pix_size.to(u.radian)\n+\n+    #sum image, weighted by pixel sizes (i.e. calculate integral of the image)\n+    image_int_grid = image.data*pix_area_grid\n+    image_int = image_int_grid.sum()\n+\n+    #integrate acceptance (i.e. gaussian function)\n+    #1st integrate in r\n+    acc_int = Quantity(quad(radial_gaussian_2D, Angle(0.*u.degree).to(u.radian).value,  Angle(10.*u.degree).to(u.radian).value, args=(sigma.to(u.radian).value))*u.radian)\n+    acc_int_value = acc_int[0]\n+    #2nd integrate in phi: phi range [0, 2pi)\n+    acc_int_value *= Angle(2.*np.pi*u.radian)\n+\n+    #check sum of the image:\n+    # int ~= sum (bin content * bin size)\n+    epsilon = 1.e-4\n+    assert abs(image_int.to(u.rad**2).value - acc_int_value.to(u.rad**2).value) < epsilon, \"image integral not compatible with radial acceptance integral\"","path":"gammapy/background/tests/test_fov.py","position":null,"original_position":110,"commit_id":"fdd3bcf8346d84060dcd65a5d21d07c3560c93e4","original_commit_id":"79b5c4ef8a7694ae90fffe7a7ba41e4ed26277ff","user":{"login":"mapazarr","id":11326731,"avatar_url":"https://avatars.githubusercontent.com/u/11326731?v=3","gravatar_id":"","url":"https://api.github.com/users/mapazarr","html_url":"https://github.com/mapazarr","followers_url":"https://api.github.com/users/mapazarr/followers","following_url":"https://api.github.com/users/mapazarr/following{/other_user}","gists_url":"https://api.github.com/users/mapazarr/gists{/gist_id}","starred_url":"https://api.github.com/users/mapazarr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mapazarr/subscriptions","organizations_url":"https://api.github.com/users/mapazarr/orgs","repos_url":"https://api.github.com/users/mapazarr/repos","events_url":"https://api.github.com/users/mapazarr/events{/privacy}","received_events_url":"https://api.github.com/users/mapazarr/received_events","type":"User","site_admin":false},"body":"Fixed.","created_at":"2015-04-08T23:41:44Z","updated_at":"2015-04-08T23:41:44Z","html_url":"https://github.com/gammapy/gammapy/pull/248#discussion_r28024779","pull_request_url":"https://api.github.com/repos/gammapy/gammapy/pulls/248","_links":{"self":{"href":"https://api.github.com/repos/gammapy/gammapy/pulls/comments/28024779"},"html":{"href":"https://github.com/gammapy/gammapy/pull/248#discussion_r28024779"},"pull_request":{"href":"https://api.github.com/repos/gammapy/gammapy/pulls/248"}}},"pull_request":{"url":"https://api.github.com/repos/gammapy/gammapy/pulls/248","id":32088525,"html_url":"https://github.com/gammapy/gammapy/pull/248","diff_url":"https://github.com/gammapy/gammapy/pull/248.diff","patch_url":"https://github.com/gammapy/gammapy/pull/248.patch","issue_url":"https://api.github.com/repos/gammapy/gammapy/issues/248","number":248,"state":"open","locked":false,"title":"Add function to fill acceptance image from curve","user":{"login":"mapazarr","id":11326731,"avatar_url":"https://avatars.githubusercontent.com/u/11326731?v=3","gravatar_id":"","url":"https://api.github.com/users/mapazarr","html_url":"https://github.com/mapazarr","followers_url":"https://api.github.com/users/mapazarr/followers","following_url":"https://api.github.com/users/mapazarr/following{/other_user}","gists_url":"https://api.github.com/users/mapazarr/gists{/gist_id}","starred_url":"https://api.github.com/users/mapazarr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mapazarr/subscriptions","organizations_url":"https://api.github.com/users/mapazarr/orgs","repos_url":"https://api.github.com/users/mapazarr/repos","events_url":"https://api.github.com/users/mapazarr/events{/privacy}","received_events_url":"https://api.github.com/users/mapazarr/received_events","type":"User","site_admin":false},"body":"This pull request addresses issue #5.\r\n\r\nThe method is slow (loop over pixels for calculating offset and interpolate the acceptance value), so maybe it could still be improved.\r\n\r\nI still am thinking about a good test for the method. While developping the method I used a script that plots the radial acceptance image that I can see, but I am not sure how to best implement an automatic test on a method working on images. In addition, since the mthod is slow, the test might take longer than desirable. Suggestions are welcome!","created_at":"2015-03-27T07:43:02Z","updated_at":"2015-04-08T23:41:44Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e87a11367ff6e3223625adb08d3efc15b3327026","assignee":{"login":"mapazarr","id":11326731,"avatar_url":"https://avatars.githubusercontent.com/u/11326731?v=3","gravatar_id":"","url":"https://api.github.com/users/mapazarr","html_url":"https://github.com/mapazarr","followers_url":"https://api.github.com/users/mapazarr/followers","following_url":"https://api.github.com/users/mapazarr/following{/other_user}","gists_url":"https://api.github.com/users/mapazarr/gists{/gist_id}","starred_url":"https://api.github.com/users/mapazarr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mapazarr/subscriptions","organizations_url":"https://api.github.com/users/mapazarr/orgs","repos_url":"https://api.github.com/users/mapazarr/repos","events_url":"https://api.github.com/users/mapazarr/events{/privacy}","received_events_url":"https://api.github.com/users/mapazarr/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/gammapy/gammapy/milestones/3","html_url":"https://github.com/gammapy/gammapy/milestones/0.2","labels_url":"https://api.github.com/repos/gammapy/gammapy/milestones/3/labels","id":408255,"number":3,"title":"0.2","description":"","creator":{"login":"cdeil","id":852409,"avatar_url":"https://avatars.githubusercontent.com/u/852409?v=3","gravatar_id":"","url":"https://api.github.com/users/cdeil","html_url":"https://github.com/cdeil","followers_url":"https://api.github.com/users/cdeil/followers","following_url":"https://api.github.com/users/cdeil/following{/other_user}","gists_url":"https://api.github.com/users/cdeil/gists{/gist_id}","starred_url":"https://api.github.com/users/cdeil/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cdeil/subscriptions","organizations_url":"https://api.github.com/users/cdeil/orgs","repos_url":"https://api.github.com/users/cdeil/repos","events_url":"https://api.github.com/users/cdeil/events{/privacy}","received_events_url":"https://api.github.com/users/cdeil/received_events","type":"User","site_admin":false},"open_issues":15,"closed_issues":51,"state":"open","created_at":"2013-08-22T14:00:56Z","updated_at":"2015-04-08T12:09:58Z","due_on":"2015-04-12T22:00:00Z","closed_at":null},"commits_url":"https://api.github.com/repos/gammapy/gammapy/pulls/248/commits","review_comments_url":"https://api.github.com/repos/gammapy/gammapy/pulls/248/comments","review_comment_url":"https://api.github.com/repos/gammapy/gammapy/pulls/comments{/number}","comments_url":"https://api.github.com/repos/gammapy/gammapy/issues/248/comments","statuses_url":"https://api.github.com/repos/gammapy/gammapy/statuses/fdd3bcf8346d84060dcd65a5d21d07c3560c93e4","head":{"label":"mapazarr:fill_acc_image","ref":"fill_acc_image","sha":"fdd3bcf8346d84060dcd65a5d21d07c3560c93e4","user":{"login":"mapazarr","id":11326731,"avatar_url":"https://avatars.githubusercontent.com/u/11326731?v=3","gravatar_id":"","url":"https://api.github.com/users/mapazarr","html_url":"https://github.com/mapazarr","followers_url":"https://api.github.com/users/mapazarr/followers","following_url":"https://api.github.com/users/mapazarr/following{/other_user}","gists_url":"https://api.github.com/users/mapazarr/gists{/gist_id}","starred_url":"https://api.github.com/users/mapazarr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mapazarr/subscriptions","organizations_url":"https://api.github.com/users/mapazarr/orgs","repos_url":"https://api.github.com/users/mapazarr/repos","events_url":"https://api.github.com/users/mapazarr/events{/privacy}","received_events_url":"https://api.github.com/users/mapazarr/received_events","type":"User","site_admin":false},"repo":{"id":32351501,"name":"gammapy","full_name":"mapazarr/gammapy","owner":{"login":"mapazarr","id":11326731,"avatar_url":"https://avatars.githubusercontent.com/u/11326731?v=3","gravatar_id":"","url":"https://api.github.com/users/mapazarr","html_url":"https://github.com/mapazarr","followers_url":"https://api.github.com/users/mapazarr/followers","following_url":"https://api.github.com/users/mapazarr/following{/other_user}","gists_url":"https://api.github.com/users/mapazarr/gists{/gist_id}","starred_url":"https://api.github.com/users/mapazarr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mapazarr/subscriptions","organizations_url":"https://api.github.com/users/mapazarr/orgs","repos_url":"https://api.github.com/users/mapazarr/repos","events_url":"https://api.github.com/users/mapazarr/events{/privacy}","received_events_url":"https://api.github.com/users/mapazarr/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/mapazarr/gammapy","description":"A Python package for gamma-ray astronomy","fork":true,"url":"https://api.github.com/repos/mapazarr/gammapy","forks_url":"https://api.github.com/repos/mapazarr/gammapy/forks","keys_url":"https://api.github.com/repos/mapazarr/gammapy/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mapazarr/gammapy/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mapazarr/gammapy/teams","hooks_url":"https://api.github.com/repos/mapazarr/gammapy/hooks","issue_events_url":"https://api.github.com/repos/mapazarr/gammapy/issues/events{/number}","events_url":"https://api.github.com/repos/mapazarr/gammapy/events","assignees_url":"https://api.github.com/repos/mapazarr/gammapy/assignees{/user}","branches_url":"https://api.github.com/repos/mapazarr/gammapy/branches{/branch}","tags_url":"https://api.github.com/repos/mapazarr/gammapy/tags","blobs_url":"https://api.github.com/repos/mapazarr/gammapy/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mapazarr/gammapy/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mapazarr/gammapy/git/refs{/sha}","trees_url":"https://api.github.com/repos/mapazarr/gammapy/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mapazarr/gammapy/statuses/{sha}","languages_url":"https://api.github.com/repos/mapazarr/gammapy/languages","stargazers_url":"https://api.github.com/repos/mapazarr/gammapy/stargazers","contributors_url":"https://api.github.com/repos/mapazarr/gammapy/contributors","subscribers_url":"https://api.github.com/repos/mapazarr/gammapy/subscribers","subscription_url":"https://api.github.com/repos/mapazarr/gammapy/subscription","commits_url":"https://api.github.com/repos/mapazarr/gammapy/commits{/sha}","git_commits_url":"https://api.github.com/repos/mapazarr/gammapy/git/commits{/sha}","comments_url":"https://api.github.com/repos/mapazarr/gammapy/comments{/number}","issue_comment_url":"https://api.github.com/repos/mapazarr/gammapy/issues/comments{/number}","contents_url":"https://api.github.com/repos/mapazarr/gammapy/contents/{+path}","compare_url":"https://api.github.com/repos/mapazarr/gammapy/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mapazarr/gammapy/merges","archive_url":"https://api.github.com/repos/mapazarr/gammapy/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mapazarr/gammapy/downloads","issues_url":"https://api.github.com/repos/mapazarr/gammapy/issues{/number}","pulls_url":"https://api.github.com/repos/mapazarr/gammapy/pulls{/number}","milestones_url":"https://api.github.com/repos/mapazarr/gammapy/milestones{/number}","notifications_url":"https://api.github.com/repos/mapazarr/gammapy/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mapazarr/gammapy/labels{/name}","releases_url":"https://api.github.com/repos/mapazarr/gammapy/releases{/id}","created_at":"2015-03-16T20:36:43Z","updated_at":"2015-03-27T07:45:11Z","pushed_at":"2015-04-08T23:35:19Z","git_url":"git://github.com/mapazarr/gammapy.git","ssh_url":"git@github.com:mapazarr/gammapy.git","clone_url":"https://github.com/mapazarr/gammapy.git","svn_url":"https://github.com/mapazarr/gammapy","homepage":"https://gammapy.readthedocs.org/","size":2680,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"gammapy:master","ref":"master","sha":"01c27072b59594505bba9b637624d994056f67f7","user":{"login":"gammapy","id":852764,"avatar_url":"https://avatars.githubusercontent.com/u/852764?v=3","gravatar_id":"","url":"https://api.github.com/users/gammapy","html_url":"https://github.com/gammapy","followers_url":"https://api.github.com/users/gammapy/followers","following_url":"https://api.github.com/users/gammapy/following{/other_user}","gists_url":"https://api.github.com/users/gammapy/gists{/gist_id}","starred_url":"https://api.github.com/users/gammapy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gammapy/subscriptions","organizations_url":"https://api.github.com/users/gammapy/orgs","repos_url":"https://api.github.com/users/gammapy/repos","events_url":"https://api.github.com/users/gammapy/events{/privacy}","received_events_url":"https://api.github.com/users/gammapy/received_events","type":"Organization","site_admin":false},"repo":{"id":10073640,"name":"gammapy","full_name":"gammapy/gammapy","owner":{"login":"gammapy","id":852764,"avatar_url":"https://avatars.githubusercontent.com/u/852764?v=3","gravatar_id":"","url":"https://api.github.com/users/gammapy","html_url":"https://github.com/gammapy","followers_url":"https://api.github.com/users/gammapy/followers","following_url":"https://api.github.com/users/gammapy/following{/other_user}","gists_url":"https://api.github.com/users/gammapy/gists{/gist_id}","starred_url":"https://api.github.com/users/gammapy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gammapy/subscriptions","organizations_url":"https://api.github.com/users/gammapy/orgs","repos_url":"https://api.github.com/users/gammapy/repos","events_url":"https://api.github.com/users/gammapy/events{/privacy}","received_events_url":"https://api.github.com/users/gammapy/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/gammapy/gammapy","description":"A Python package for gamma-ray astronomy","fork":false,"url":"https://api.github.com/repos/gammapy/gammapy","forks_url":"https://api.github.com/repos/gammapy/gammapy/forks","keys_url":"https://api.github.com/repos/gammapy/gammapy/keys{/key_id}","collaborators_url":"https://api.github.com/repos/gammapy/gammapy/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/gammapy/gammapy/teams","hooks_url":"https://api.github.com/repos/gammapy/gammapy/hooks","issue_events_url":"https://api.github.com/repos/gammapy/gammapy/issues/events{/number}","events_url":"https://api.github.com/repos/gammapy/gammapy/events","assignees_url":"https://api.github.com/repos/gammapy/gammapy/assignees{/user}","branches_url":"https://api.github.com/repos/gammapy/gammapy/branches{/branch}","tags_url":"https://api.github.com/repos/gammapy/gammapy/tags","blobs_url":"https://api.github.com/repos/gammapy/gammapy/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/gammapy/gammapy/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/gammapy/gammapy/git/refs{/sha}","trees_url":"https://api.github.com/repos/gammapy/gammapy/git/trees{/sha}","statuses_url":"https://api.github.com/repos/gammapy/gammapy/statuses/{sha}","languages_url":"https://api.github.com/repos/gammapy/gammapy/languages","stargazers_url":"https://api.github.com/repos/gammapy/gammapy/stargazers","contributors_url":"https://api.github.com/repos/gammapy/gammapy/contributors","subscribers_url":"https://api.github.com/repos/gammapy/gammapy/subscribers","subscription_url":"https://api.github.com/repos/gammapy/gammapy/subscription","commits_url":"https://api.github.com/repos/gammapy/gammapy/commits{/sha}","git_commits_url":"https://api.github.com/repos/gammapy/gammapy/git/commits{/sha}","comments_url":"https://api.github.com/repos/gammapy/gammapy/comments{/number}","issue_comment_url":"https://api.github.com/repos/gammapy/gammapy/issues/comments{/number}","contents_url":"https://api.github.com/repos/gammapy/gammapy/contents/{+path}","compare_url":"https://api.github.com/repos/gammapy/gammapy/compare/{base}...{head}","merges_url":"https://api.github.com/repos/gammapy/gammapy/merges","archive_url":"https://api.github.com/repos/gammapy/gammapy/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/gammapy/gammapy/downloads","issues_url":"https://api.github.com/repos/gammapy/gammapy/issues{/number}","pulls_url":"https://api.github.com/repos/gammapy/gammapy/pulls{/number}","milestones_url":"https://api.github.com/repos/gammapy/gammapy/milestones{/number}","notifications_url":"https://api.github.com/repos/gammapy/gammapy/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/gammapy/gammapy/labels{/name}","releases_url":"https://api.github.com/repos/gammapy/gammapy/releases{/id}","created_at":"2013-05-15T07:50:40Z","updated_at":"2015-04-08T12:06:25Z","pushed_at":"2015-04-08T23:35:20Z","git_url":"git://github.com/gammapy/gammapy.git","ssh_url":"git@github.com:gammapy/gammapy.git","clone_url":"https://github.com/gammapy/gammapy.git","svn_url":"https://github.com/gammapy/gammapy","homepage":"https://gammapy.readthedocs.org/","size":5245,"stargazers_count":10,"watchers_count":10,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":16,"mirror_url":null,"open_issues_count":94,"forks":16,"open_issues":94,"watchers":10,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/gammapy/gammapy/pulls/248"},"html":{"href":"https://github.com/gammapy/gammapy/pull/248"},"issue":{"href":"https://api.github.com/repos/gammapy/gammapy/issues/248"},"comments":{"href":"https://api.github.com/repos/gammapy/gammapy/issues/248/comments"},"review_comments":{"href":"https://api.github.com/repos/gammapy/gammapy/pulls/248/comments"},"review_comment":{"href":"https://api.github.com/repos/gammapy/gammapy/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/gammapy/gammapy/pulls/248/commits"},"statuses":{"href":"https://api.github.com/repos/gammapy/gammapy/statuses/fdd3bcf8346d84060dcd65a5d21d07c3560c93e4"}}}}