<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div><blockquote type="cite" class=""><div class="">On Dec 6, 2019, at 11:34 AM, Andrew Nelson <<a href="mailto:andyfaff@gmail.com" class="">andyfaff@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="">Whilst I'm not familiar with pandas it's behaviour is probably similar to NumPy in terms of vectorisation. It looks to me that you can vectorise that loop. Removing loops will give you *enormous* speedup. I don't claim this is the most numpythonic, there may be some kind of modulo arithmetic for doing this more easily.</div></div></div></blockquote><div><br class=""></div><div>You are correct - in fact, my understanding is that pandas actually uses NumPy under the hood, and as such most NumPy operations/optimizations apply. And yes, that loop has been bugging me, as it looks to me as well that I should be able to vectorize it. In fact, I’ve been able to do exactly that to numerous other loops I have found in the code (I’m not the original author), with great success. I simply haven’t yet been able to wrap my head around exactly how to vectorize this one. But that’s a different question, for a different mailing list :-)</div><div><br class=""></div><div>…although if I can do enough optimizations, I suppose that would make this issue a moot point :-)</div><div><div>---</div><div>Israel Brewster<br class="">Software Engineer<br class="">Alaska Volcano Observatory <br class="">Geophysical Institute - UAF <br class="">2156 Koyukuk Drive <br class="">Fairbanks AK 99775-7320</div><div>Work: 907-474-5172<br class="">cell:  907-328-9145</div></div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_quote"><div class=""><br class=""></div><div class="">``` </div><div class="">dx = self.contour_df['delta_utmx_volc']</div><div class="">dy = self.contour_df['delta_utmy_volc']<br class=""></div><div class=""><br class=""></div><div class="">loc = np.logical_and(dx > 0, dy > 0)</div><div class="">self.contour_df['volc_azimuth'][loc] = self.contour_df['volc_theta'][loc]<br class=""></div><div class=""><br class=""></div><div class=""># the complement of loc is both dx and dy are negative, or equal to zero (you didn't deal with that case)</div><div class=""><div class="">self.contour_df['volc_azimuth'][~loc] = 360 - self.contour_df['volc_theta'][~loc]<br class=""></div><div class=""></div></div><div class=""><br class=""></div><div class="">loc = np.logical_and(dx > 0, dy < 0)</div><div class="">self.contour_df['volc_azimuth'][loc] = 180 - self.contour_df['volc_theta'][loc]<br class=""></div><div class=""><br class=""></div><div class="">loc = np.logical_and(dx < 0, dy > 0)</div><div class="">self.contour_df['volc_azimuth'][loc] = 180 + self.contour_df['volc_theta'][loc]<br class=""></div><div class="">```</div><div class=""><br class=""></div><div class=""><br class=""></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;" class=""><div class="">        for i in range(self.contour_df.index[0], self.contour_df.index.max()):<br class="">            dx = self.contour_df['delta_utmx_volc'][i]<br class="">            dy = self.contour_df['delta_utmy_volc'][i]<br class=""><br class="">            if dx > 0 and dy > 0:<br class="">                self.contour_df['volc_azimuth'][i] = self.contour_df['volc_theta'][i]<br class=""><br class="">            elif dx > 0 > dy:<br class="">                self.contour_df['volc_azimuth'][i] = 180 - self.contour_df['volc_theta'][i]<br class=""><br class="">            elif dx < 0 and dy < 0:<br class="">                self.contour_df['volc_azimuth'][i] = 180 + self.contour_df['volc_theta'][i]<br class=""><br class="">            elif dx < 0 < dy:<br class="">                self.contour_df['volc_azimuth'][i] = 360 - self.contour_df['volc_theta'][i]<br class=""><br class="">            if i % 50 == 0:<br class="">                self.yield_thread()<br class=""><br class="">        self.contour_df['volc_dist'] = np.sqrt((self.contour_df['delta_utmy_volc']**2) + (self.contour_df['delta_utmx_volc']**2))  # distance from point to volcano</div><div class=""><br class=""></div></div></blockquote></div></div>
</div></blockquote></div><br class=""></body></html>