Updating solutions

This commit is contained in:
Allen Downey
2021-03-18 08:34:37 -04:00
parent 654b2fec60
commit 9b6712add9
4 changed files with 15 additions and 258 deletions

View File

@@ -1341,7 +1341,7 @@
}
],
"source": [
"## Solution\n",
"# Solution\n",
"\n",
"radius = 5 * u.arcmin\n",
"print(radius)\n",

View File

@@ -14,13 +14,11 @@
"\n",
"objectives:\n",
"\n",
"- \"Upload a table to the Gaia server.\"\n",
"\n",
"- \"Write ADQL queries involving `JOIN` operations.\"\n",
"\n",
"keypoints:\n",
"\n",
"- \"Use `JOIN` operations to combine data from multiple tables in a databased, using some kind of identifier to match up records from one table with records from another.\"\n",
"- \"Use `JOIN` operations to combine data from multiple tables in a database, using some kind of identifier to match up records from one table with records from another.\"\n",
"\n",
"- \"This is another example of a practice we saw in the previous notebook, moving the computation to the data.\"\n",
"\n",
@@ -1097,34 +1095,7 @@
"WHERE 1=CONTAINS(\n",
" POINT(gaia.ra, gaia.dec),\n",
" CIRCLE(88.8, 7.4, 0.08333333))\n",
"\"\"\"\n"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"SELECT \n",
"gaia.source_id, gaia.ra, gaia.dec, gaia.pmra, gaia.pmdec, best.best_neighbour_multiplicity, best.number_of_mates, ps.g_mean_psf_mag, ps.i_mean_psf_mag\n",
"FROM gaiadr2.gaia_source as gaia\n",
"JOIN gaiadr2.panstarrs1_best_neighbour as best\n",
" ON gaia.source_id = best.source_id\n",
"JOIN gaiadr2.panstarrs1_original_valid as ps\n",
" ON best.original_ext_source_id = ps.obj_id\n",
"WHERE 1=CONTAINS(\n",
" POINT(gaia.ra, gaia.dec),\n",
" CIRCLE(88.8, 7.4, 0.08333333))\n",
"\n"
]
}
],
"source": [
"# Solution\n",
"\"\"\"\n",
"\n",
"column_list = ['gaia.source_id',\n",
" 'gaia.ra',\n",
@@ -1139,101 +1110,9 @@
"columns = ', '.join(column_list)\n",
"\n",
"query = query_base.format(columns=columns)\n",
"print(query)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"INFO: Query finished. [astroquery.utils.tap.core]\n"
]
}
],
"source": [
"# Solution\n",
"\n",
"job = Gaia.launch_job_async(query=query)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"<i>Table length=490</i>\n",
"<table id=\"table140104380910896\" class=\"table-striped table-bordered table-condensed\">\n",
"<thead><tr><th>source_id</th><th>ra</th><th>dec</th><th>pmra</th><th>pmdec</th><th>best_neighbour_multiplicity</th><th>number_of_mates</th><th>g_mean_psf_mag</th><th>i_mean_psf_mag</th></tr></thead>\n",
"<thead><tr><th></th><th>deg</th><th>deg</th><th>mas / yr</th><th>mas / yr</th><th></th><th></th><th></th><th>mag</th></tr></thead>\n",
"<thead><tr><th>int64</th><th>float64</th><th>float64</th><th>float64</th><th>float64</th><th>int16</th><th>int16</th><th>float64</th><th>float64</th></tr></thead>\n",
"<tr><td>3322773965056065536</td><td>88.78178020183375</td><td>7.334936530583141</td><td>0.2980633722108194</td><td>-2.5057036964736907</td><td>1</td><td>0</td><td>19.9431991577148</td><td>17.4221992492676</td></tr>\n",
"<tr><td>3322774068134271104</td><td>88.8206092188033</td><td>7.353158142762173</td><td>-1.1065462654445488</td><td>-1.5260889445858044</td><td>1</td><td>0</td><td>18.6212005615234</td><td>16.6007995605469</td></tr>\n",
"<tr><td>3322773930696320512</td><td>88.80843339290348</td><td>7.334853162299928</td><td>2.6074384482375215</td><td>-0.9292104395445717</td><td>1</td><td>0</td><td>--</td><td>20.2203998565674</td></tr>\n",
"<tr><td>3322774377374425728</td><td>88.86806108182265</td><td>7.371287731275939</td><td>3.9555477866915383</td><td>-3.8676624830902435</td><td>1</td><td>0</td><td>18.0676002502441</td><td>16.9762001037598</td></tr>\n",
"<tr><td>3322773724537891456</td><td>88.81308602813434</td><td>7.32488574492059</td><td>51.34995462741039</td><td>-33.078133430952086</td><td>1</td><td>0</td><td>20.1907005310059</td><td>17.8700008392334</td></tr>\n",
"<tr><td>3322773724537891328</td><td>88.81570329208743</td><td>7.3223019772324855</td><td>1.9389988498951845</td><td>0.3110526931576576</td><td>1</td><td>0</td><td>22.6308002471924</td><td>19.6004009246826</td></tr>\n",
"<tr><td>3322773930696321792</td><td>88.8050736770331</td><td>7.332371472206583</td><td>2.264014834476311</td><td>1.0772755505138008</td><td>1</td><td>0</td><td>21.2119998931885</td><td>18.3528003692627</td></tr>\n",
"<tr><td>3322773724537890944</td><td>88.81241651540533</td><td>7.327864052479726</td><td>-0.36003627434304625</td><td>-6.393939291541333</td><td>1</td><td>0</td><td>20.8094005584717</td><td>18.1343002319336</td></tr>\n",
"<tr><td>3322773930696322176</td><td>88.80128682574824</td><td>7.334292036448643</td><td>--</td><td>--</td><td>1</td><td>0</td><td>19.7306003570557</td><td>--</td></tr>\n",
"<tr><td>...</td><td>...</td><td>...</td><td>...</td><td>...</td><td>...</td><td>...</td><td>...</td><td>...</td></tr>\n",
"<tr><td>3322962359501481088</td><td>88.85037722908271</td><td>7.402162717053584</td><td>2.058216493648542</td><td>-2.249255322558584</td><td>1</td><td>0</td><td>17.4034996032715</td><td>15.9040002822876</td></tr>\n",
"<tr><td>3322962393861228544</td><td>88.82108234976155</td><td>7.4044425496203</td><td>-0.916760881643629</td><td>-1.1113319053861441</td><td>1</td><td>0</td><td>--</td><td>--</td></tr>\n",
"<tr><td>3322955831151254912</td><td>88.74620347799508</td><td>7.342728619145855</td><td>0.1559833902071379</td><td>-1.750598455959734</td><td>1</td><td>0</td><td>18.4960994720459</td><td>17.3892993927002</td></tr>\n",
"<tr><td>3322962118983356032</td><td>88.76109637722949</td><td>7.380564308268047</td><td>--</td><td>--</td><td>1</td><td>0</td><td>18.0643997192383</td><td>16.7395000457764</td></tr>\n",
"<tr><td>3322963527732585984</td><td>88.78813701704823</td><td>7.456696889759524</td><td>1.1363354614104264</td><td>-2.46251296961979</td><td>1</td><td>0</td><td>17.8034992218018</td><td>16.1214008331299</td></tr>\n",
"<tr><td>3322961775385969024</td><td>88.79723215862369</td><td>7.359756552906535</td><td>2.121021366548921</td><td>-6.605711792572964</td><td>1</td><td>0</td><td>18.2070007324219</td><td>15.9947996139526</td></tr>\n",
"<tr><td>3322962084625312512</td><td>88.78286756313868</td><td>7.384598632215225</td><td>-0.09350717810996487</td><td>1.3495903680571226</td><td>1</td><td>0</td><td>16.7978992462158</td><td>15.1180000305176</td></tr>\n",
"<tr><td>3322962939322692608</td><td>88.73289357818679</td><td>7.407688975612043</td><td>-0.11002934783569704</td><td>1.002126813991455</td><td>1</td><td>0</td><td>17.18630027771</td><td>16.3645992279053</td></tr>\n",
"<tr><td>3322963459013111808</td><td>88.80348931842845</td><td>7.438699901204871</td><td>0.800833828337078</td><td>-3.3780655466364626</td><td>1</td><td>0</td><td>--</td><td>16.294900894165</td></tr>\n",
"<tr><td>3322962015904143872</td><td>88.74740822271643</td><td>7.387057037713974</td><td>-0.7201178533250112</td><td>0.5565841272341593</td><td>1</td><td>0</td><td>18.4706993103027</td><td>16.8038005828857</td></tr>\n",
"</table>"
],
"text/plain": [
"<Table length=490>\n",
" source_id ra ... g_mean_psf_mag i_mean_psf_mag \n",
" deg ... mag \n",
" int64 float64 ... float64 float64 \n",
"------------------- ----------------- ... ---------------- ----------------\n",
"3322773965056065536 88.78178020183375 ... 19.9431991577148 17.4221992492676\n",
"3322774068134271104 88.8206092188033 ... 18.6212005615234 16.6007995605469\n",
"3322773930696320512 88.80843339290348 ... -- 20.2203998565674\n",
"3322774377374425728 88.86806108182265 ... 18.0676002502441 16.9762001037598\n",
"3322773724537891456 88.81308602813434 ... 20.1907005310059 17.8700008392334\n",
"3322773724537891328 88.81570329208743 ... 22.6308002471924 19.6004009246826\n",
"3322773930696321792 88.8050736770331 ... 21.2119998931885 18.3528003692627\n",
"3322773724537890944 88.81241651540533 ... 20.8094005584717 18.1343002319336\n",
"3322773930696322176 88.80128682574824 ... 19.7306003570557 --\n",
" ... ... ... ... ...\n",
"3322962359501481088 88.85037722908271 ... 17.4034996032715 15.9040002822876\n",
"3322962393861228544 88.82108234976155 ... -- --\n",
"3322955831151254912 88.74620347799508 ... 18.4960994720459 17.3892993927002\n",
"3322962118983356032 88.76109637722949 ... 18.0643997192383 16.7395000457764\n",
"3322963527732585984 88.78813701704823 ... 17.8034992218018 16.1214008331299\n",
"3322961775385969024 88.79723215862369 ... 18.2070007324219 15.9947996139526\n",
"3322962084625312512 88.78286756313868 ... 16.7978992462158 15.1180000305176\n",
"3322962939322692608 88.73289357818679 ... 17.18630027771 16.3645992279053\n",
"3322963459013111808 88.80348931842845 ... -- 16.294900894165\n",
"3322962015904143872 88.74740822271643 ... 18.4706993103027 16.8038005828857"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Solution\n",
"print(query)\n",
"\n",
"job = Gaia.launch_job_async(query=query)\n",
"results = job.get_results()\n",
"results"
]
@@ -1474,137 +1353,17 @@
" POLYGON({point_list}))\n",
" AND 1 = CONTAINS(POINT(gaia.pmra, gaia.pmdec),\n",
" POLYGON({pm_point_list}))\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"SELECT \n",
"gaia.source_id, gaia.ra, gaia.dec, gaia.pmra, gaia.pmdec, best.best_neighbour_multiplicity, best.number_of_mates, ps.g_mean_psf_mag, ps.i_mean_psf_mag\n",
"FROM gaiadr2.gaia_source as gaia\n",
"JOIN gaiadr2.panstarrs1_best_neighbour as best\n",
" ON gaia.source_id = best.source_id\n",
"JOIN gaiadr2.panstarrs1_original_valid as ps\n",
" ON best.original_ext_source_id = ps.obj_id\n",
"WHERE parallax < 1\n",
" AND bp_rp BETWEEN -0.75 AND 2 \n",
" AND 1 = CONTAINS(POINT(gaia.ra, gaia.dec), \n",
" POLYGON(135.306, 8.39862, 126.51, 13.4449, 163.017, 54.2424, 172.933, 46.4726, 135.306, 8.39862))\n",
" AND 1 = CONTAINS(POINT(gaia.pmra, gaia.pmdec),\n",
" POLYGON( -4.05037121,-14.75623261, -3.41981085,-14.72365546, -3.03521988,-14.44357135, -2.26847919,-13.7140236 , -2.61172203,-13.24797471, -2.73471401,-13.09054471, -3.19923146,-12.5942653 , -3.34082546,-12.47611926, -5.67489413,-11.16083338, -5.95159272,-11.10547884, -6.42394023,-11.05981295, -7.09631023,-11.95187806, -7.30641519,-12.24559977, -7.04016696,-12.88580702, -6.00347705,-13.75912098, -4.42442296,-14.74641176))\n",
"\n"
]
}
],
"source": [
"# Solution\n",
"\"\"\"\n",
"\n",
"columns = ', '.join(column_list)\n",
"\n",
"query7 = query7_base.format(columns=columns,\n",
" point_list=point_series['point_list'],\n",
" pm_point_list=point_series['pm_point_list'])\n",
"print(query7)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"INFO: Query finished. [astroquery.utils.tap.core]\n"
]
}
],
"source": [
"# Solution\n",
"print(query7)\n",
"\n",
"job = Gaia.launch_job_async(query=query7)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<i>Table length=3725</i>\n",
"<table id=\"table140104308204640\" class=\"table-striped table-bordered table-condensed\">\n",
"<thead><tr><th>source_id</th><th>ra</th><th>dec</th><th>pmra</th><th>pmdec</th><th>best_neighbour_multiplicity</th><th>number_of_mates</th><th>g_mean_psf_mag</th><th>i_mean_psf_mag</th></tr></thead>\n",
"<thead><tr><th></th><th>deg</th><th>deg</th><th>mas / yr</th><th>mas / yr</th><th></th><th></th><th></th><th>mag</th></tr></thead>\n",
"<thead><tr><th>int64</th><th>float64</th><th>float64</th><th>float64</th><th>float64</th><th>int16</th><th>int16</th><th>float64</th><th>float64</th></tr></thead>\n",
"<tr><td>635860218726658176</td><td>138.5187065217173</td><td>19.09233926905897</td><td>-5.941679495793577</td><td>-11.346409129876392</td><td>1</td><td>0</td><td>17.8978004455566</td><td>17.5174007415771</td></tr>\n",
"<tr><td>635674126383965568</td><td>138.8428741026386</td><td>19.031798198627634</td><td>-3.8970011609340207</td><td>-12.702779525389634</td><td>1</td><td>0</td><td>19.2873001098633</td><td>17.6781005859375</td></tr>\n",
"<tr><td>635535454774983040</td><td>137.8377518255436</td><td>18.864006786112604</td><td>-4.335040664412791</td><td>-14.492308604905652</td><td>1</td><td>0</td><td>16.9237995147705</td><td>16.478099822998</td></tr>\n",
"<tr><td>635497276810313600</td><td>138.0445160213759</td><td>19.00947118796605</td><td>-7.1729306406216615</td><td>-12.291499169815987</td><td>1</td><td>0</td><td>19.9242000579834</td><td>18.3339996337891</td></tr>\n",
"<tr><td>635614168640132864</td><td>139.59219748145836</td><td>18.807955539071433</td><td>-3.309602916796381</td><td>-13.708904908478631</td><td>1</td><td>0</td><td>16.1515998840332</td><td>14.6662998199463</td></tr>\n",
"<tr><td>635598607974369792</td><td>139.20920023089508</td><td>18.624132868942702</td><td>-6.124445176881091</td><td>-12.833824027100611</td><td>1</td><td>0</td><td>16.5223999023438</td><td>16.1375007629395</td></tr>\n",
"<tr><td>635737661835496576</td><td>139.93327552473934</td><td>19.167962454651423</td><td>-7.119403303682826</td><td>-12.687947497633793</td><td>1</td><td>0</td><td>14.5032997131348</td><td>13.9849004745483</td></tr>\n",
"<tr><td>635850945892748672</td><td>139.86542888472115</td><td>20.011312663154804</td><td>-3.786655365804428</td><td>-14.28415600718206</td><td>1</td><td>0</td><td>16.5174999237061</td><td>16.0450000762939</td></tr>\n",
"<tr><td>635600532119713664</td><td>139.22869949616816</td><td>18.685939084485494</td><td>-3.9742788217925122</td><td>-12.342426623384245</td><td>1</td><td>0</td><td>20.4505996704102</td><td>19.5177001953125</td></tr>\n",
"<tr><td>...</td><td>...</td><td>...</td><td>...</td><td>...</td><td>...</td><td>...</td><td>...</td><td>...</td></tr>\n",
"<tr><td>612241781249124608</td><td>134.3755835065194</td><td>18.129179169751275</td><td>-2.831807894848964</td><td>-13.902118573613597</td><td>1</td><td>0</td><td>20.2343997955322</td><td>18.6518001556396</td></tr>\n",
"<tr><td>612332147361443072</td><td>134.14584721363653</td><td>18.45685585044513</td><td>-6.234287981021865</td><td>-11.500464195695072</td><td>1</td><td>0</td><td>21.3848991394043</td><td>20.3076000213623</td></tr>\n",
"<tr><td>612426744016802432</td><td>134.68522805061076</td><td>18.77090626983678</td><td>-3.7691372464459554</td><td>-12.889167493118862</td><td>1</td><td>0</td><td>17.8281002044678</td><td>17.4281005859375</td></tr>\n",
"<tr><td>612331739340341760</td><td>134.12176196902254</td><td>18.42768872157865</td><td>-3.9894012386388735</td><td>-12.60504410507441</td><td>1</td><td>0</td><td>21.8656997680664</td><td>19.5223007202148</td></tr>\n",
"<tr><td>612282738058264960</td><td>134.0445768189235</td><td>18.11915820167003</td><td>-2.5972485319419127</td><td>-13.651740929272187</td><td>1</td><td>0</td><td>22.5151996612549</td><td>19.9743995666504</td></tr>\n",
"<tr><td>612386332668697600</td><td>135.45701048323093</td><td>18.63266345155342</td><td>-5.07684899854408</td><td>-12.436641304786672</td><td>1</td><td>0</td><td>19.3792991638184</td><td>17.9923000335693</td></tr>\n",
"<tr><td>612296172717818624</td><td>133.80060286960668</td><td>18.08186533343457</td><td>-6.112792578821885</td><td>-12.50750861370402</td><td>1</td><td>0</td><td>17.4944000244141</td><td>16.926700592041</td></tr>\n",
"<tr><td>612250375480101760</td><td>134.64754712466774</td><td>18.122419425065015</td><td>-2.8969262278467127</td><td>-14.061676353845487</td><td>1</td><td>0</td><td>15.3330001831055</td><td>14.6280002593994</td></tr>\n",
"<tr><td>612394926899159168</td><td>135.51997060013844</td><td>18.817675531233004</td><td>-3.9968965218753763</td><td>-13.526821099431533</td><td>1</td><td>0</td><td>16.4414005279541</td><td>15.8212003707886</td></tr>\n",
"<tr><td>612256418500423168</td><td>134.90752972739924</td><td>18.280596648172743</td><td>-6.109836304219565</td><td>-12.145212331165776</td><td>1</td><td>0</td><td>20.8715991973877</td><td>19.9612007141113</td></tr>\n",
"</table>"
],
"text/plain": [
"<Table length=3725>\n",
" source_id ra ... g_mean_psf_mag i_mean_psf_mag \n",
" deg ... mag \n",
" int64 float64 ... float64 float64 \n",
"------------------ ------------------ ... ---------------- ----------------\n",
"635860218726658176 138.5187065217173 ... 17.8978004455566 17.5174007415771\n",
"635674126383965568 138.8428741026386 ... 19.2873001098633 17.6781005859375\n",
"635535454774983040 137.8377518255436 ... 16.9237995147705 16.478099822998\n",
"635497276810313600 138.0445160213759 ... 19.9242000579834 18.3339996337891\n",
"635614168640132864 139.59219748145836 ... 16.1515998840332 14.6662998199463\n",
"635598607974369792 139.20920023089508 ... 16.5223999023438 16.1375007629395\n",
"635737661835496576 139.93327552473934 ... 14.5032997131348 13.9849004745483\n",
"635850945892748672 139.86542888472115 ... 16.5174999237061 16.0450000762939\n",
"635600532119713664 139.22869949616816 ... 20.4505996704102 19.5177001953125\n",
" ... ... ... ... ...\n",
"612241781249124608 134.3755835065194 ... 20.2343997955322 18.6518001556396\n",
"612332147361443072 134.14584721363653 ... 21.3848991394043 20.3076000213623\n",
"612426744016802432 134.68522805061076 ... 17.8281002044678 17.4281005859375\n",
"612331739340341760 134.12176196902254 ... 21.8656997680664 19.5223007202148\n",
"612282738058264960 134.0445768189235 ... 22.5151996612549 19.9743995666504\n",
"612386332668697600 135.45701048323093 ... 19.3792991638184 17.9923000335693\n",
"612296172717818624 133.80060286960668 ... 17.4944000244141 16.926700592041\n",
"612250375480101760 134.64754712466774 ... 15.3330001831055 14.6280002593994\n",
"612394926899159168 135.51997060013844 ... 16.4414005279541 15.8212003707886\n",
"612256418500423168 134.90752972739924 ... 20.8715991973877 19.9612007141113"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Solution\n",
"\n",
"job = Gaia.launch_job_async(query=query7)\n",
"results = job.get_results()\n",
"results"
]

View File

@@ -10,23 +10,21 @@
"exercises: 0\n",
"questions:\n",
"\n",
"- \"How do we use Matplotlib to select a polygon and Pandas to merge data from multiple tables?\"\n",
"- \"How do we use Matplotlib to define a polygon and select points that fall inside it?\"\n",
"\n",
"objectives:\n",
"\n",
"- \"Use Matplotlib to specify a polygon and determine which points fall inside it.\"\n",
"- \"Use isochrone data to specify a polygon and determine which points fall inside it.\"\n",
"\n",
"- \"Use Pandas to merge data from multiple `DataFrames`, much like a database `JOIN` operation.\"\n",
"- \"Use Matplotlib features to customize the appearance of figures.\"\n",
"\n",
"keypoints:\n",
"\n",
"- \"Matplotlib provides operations for working with points, polygons, and other geometric entities, so it's not just for making figures.\"\n",
"\n",
"- \"If you want to perform something like a database `JOIN` operation with data that is in a Pandas `DataFrame`, you can use the `join` or `merge` function. In many cases, `merge` is easier to use because the arguments are more like SQL.\"\n",
"\n",
"- \"Use Matplotlib options to control the size and aspect ratio of figures to make them easier to interpret.\"\n",
"\n",
"- \"Be sure to record every element of the data analysis pipeline that would be needed to replicate the results.\"\n",
"- \"Record every element of the data analysis pipeline that would be needed to replicate the results.\"\n",
"\n",
"---\n",
"\n",

View File

@@ -22,9 +22,9 @@
"\n",
"keypoints:\n",
"\n",
"- \"The most effective figures focus on telling a single story clearly and compellingly.\"\n",
"- \"The most effective figures focus on telling a single story clearly.\"\n",
"\n",
"- \"Consider using annotations to guide the readers attention to the most important elements of a figure.\"\n",
"- \"Consider using annotations to guide the reader's attention to the most important elements of a figure.\"\n",
"\n",
"- \"The default Matplotlib style generates good quality figures, but there are several ways you can override the defaults.\"\n",
"\n",
@@ -1265,7 +1265,7 @@
"\n",
"* The most effective figures focus on telling a single story clearly and compellingly.\n",
"\n",
"* Consider using annotations to guide the readers attention to the most important elements of a figure.\n",
"* Consider using annotations to guide the reader's attention to the most important elements of a figure.\n",
"\n",
"* The default Matplotlib style generates good quality figures, but there are several ways you can override the defaults.\n",
"\n",