{"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _cond_k(self, word, suffix_len):\n return (len(word) - suffix_len >= 3) and (\n word[-suffix_len - 1] in {'i', 'l'}\n or (word[-suffix_len - 3] == 'u' and word[-suffix_len - 1] == 'e')\n )", "response": "Return Lovins condition K."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _cond_n(self, word, suffix_len):\n if len(word) - suffix_len >= 3:\n if word[-suffix_len - 3] == 's':\n if len(word) - suffix_len >= 4:\n return True\n else:\n return True\n return False", "response": "Return Lovins condition N."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _cond_s(self, word, suffix_len):\n return word[-suffix_len - 2 : -suffix_len] == 'dr' or (\n word[-suffix_len - 1] == 't'\n and word[-suffix_len - 2 : -suffix_len] != 'tt'\n )", "response": "Return Lovins condition S."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _cond_x(self, word, suffix_len):\n return word[-suffix_len - 1] in {'i', 'l'} or (\n word[-suffix_len - 3 : -suffix_len] == 'u'\n and word[-suffix_len - 1] == 'e'\n )", "response": "Return Lovins condition X."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _cond_bb(self, word, suffix_len):\n return (\n len(word) - suffix_len >= 3\n and word[-suffix_len - 3 : -suffix_len] != 'met'\n and word[-suffix_len - 4 : -suffix_len] != 'ryst'\n )", "response": "Return Lovins condition BB."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the stemmed version of the word.", "response": "def stem(self, word):\n \"\"\"Return Lovins stem.\n\n Parameters\n ----------\n word : str\n The word to stem\n\n Returns\n -------\n str\n Word stem\n\n Examples\n --------\n >>> stmr = Lovins()\n >>> stmr.stem('reading')\n 'read'\n >>> stmr.stem('suspension')\n 'suspens'\n >>> stmr.stem('elusiveness')\n 'elus'\n\n \"\"\"\n # lowercase, normalize, and compose\n word = normalize('NFC', text_type(word.lower()))\n\n for suffix_len in range(11, 0, -1):\n ending = word[-suffix_len:]\n if (\n ending in self._suffix\n and len(word) - suffix_len >= 2\n and (\n self._suffix[ending] is None\n or self._suffix[ending](word, suffix_len)\n )\n ):\n word = word[:-suffix_len]\n break\n\n if word[-2:] in {\n 'bb',\n 'dd',\n 'gg',\n 'll',\n 'mm',\n 'nn',\n 'pp',\n 'rr',\n 'ss',\n 'tt',\n }:\n word = word[:-1]\n\n for ending, replacement in self._recode:\n if word.endswith(ending):\n if callable(replacement):\n word = replacement(word)\n else:\n word = word[: -len(ending)] + replacement\n\n return word"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn the Damerau - Levenshtein distance between two strings src and tar.", "response": "def dist_abs(self, src, tar, cost=(1, 1, 1, 1)):\n \"\"\"Return the Damerau-Levenshtein distance between two strings.\n\n Parameters\n ----------\n src : str\n Source string for comparison\n tar : str\n Target string for comparison\n cost : tuple\n A 4-tuple representing the cost of the four possible edits:\n inserts, deletes, substitutions, and transpositions, respectively\n (by default: (1, 1, 1, 1))\n\n Returns\n -------\n int (may return a float if cost has float values)\n The Damerau-Levenshtein distance between src & tar\n\n Raises\n ------\n ValueError\n Unsupported cost assignment; the cost of two transpositions must\n not be less than the cost of an insert plus a delete.\n\n Examples\n --------\n >>> cmp = DamerauLevenshtein()\n >>> cmp.dist_abs('cat', 'hat')\n 1\n >>> cmp.dist_abs('Niall', 'Neil')\n 3\n >>> cmp.dist_abs('aluminum', 'Catalan')\n 7\n >>> cmp.dist_abs('ATCG', 'TAGC')\n 2\n\n \"\"\"\n ins_cost, del_cost, sub_cost, trans_cost = cost\n\n if src == tar:\n return 0\n if not src:\n return len(tar) * ins_cost\n if not tar:\n return len(src) * del_cost\n\n if 2 * trans_cost < ins_cost + del_cost:\n raise ValueError(\n 'Unsupported cost assignment; the cost of two transpositions '\n + 'must not be less than the cost of an insert plus a delete.'\n )\n\n d_mat = np_zeros((len(src)) * (len(tar)), dtype=np_int).reshape(\n (len(src), len(tar))\n )\n\n if src[0] != tar[0]:\n d_mat[0, 0] = min(sub_cost, ins_cost + del_cost)\n\n src_index_by_character = {src[0]: 0}\n for i in range(1, len(src)):\n del_distance = d_mat[i - 1, 0] + del_cost\n ins_distance = (i + 1) * del_cost + ins_cost\n match_distance = i * del_cost + (\n 0 if src[i] == tar[0] else sub_cost\n )\n d_mat[i, 0] = min(del_distance, ins_distance, match_distance)\n\n for j in range(1, len(tar)):\n del_distance = (j + 1) * ins_cost + del_cost\n ins_distance = d_mat[0, j - 1] + ins_cost\n match_distance = j * ins_cost + (\n 0 if src[0] == tar[j] else sub_cost\n )\n d_mat[0, j] = min(del_distance, ins_distance, match_distance)\n\n for i in range(1, len(src)):\n max_src_letter_match_index = 0 if src[i] == tar[0] else -1\n for j in range(1, len(tar)):\n candidate_swap_index = (\n -1\n if tar[j] not in src_index_by_character\n else src_index_by_character[tar[j]]\n )\n j_swap = max_src_letter_match_index\n del_distance = d_mat[i - 1, j] + del_cost\n ins_distance = d_mat[i, j - 1] + ins_cost\n match_distance = d_mat[i - 1, j - 1]\n if src[i] != tar[j]:\n match_distance += sub_cost\n else:\n max_src_letter_match_index = j\n\n if candidate_swap_index != -1 and j_swap != -1:\n i_swap = candidate_swap_index\n\n if i_swap == 0 and j_swap == 0:\n pre_swap_cost = 0\n else:\n pre_swap_cost = d_mat[\n max(0, i_swap - 1), max(0, j_swap - 1)\n ]\n swap_distance = (\n pre_swap_cost\n + (i - i_swap - 1) * del_cost\n + (j - j_swap - 1) * ins_cost\n + trans_cost\n )\n else:\n swap_distance = maxsize\n\n d_mat[i, j] = min(\n del_distance, ins_distance, match_distance, swap_distance\n )\n src_index_by_character[src[i]] = i\n\n return d_mat[len(src) - 1, len(tar) - 1]"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef dist(self, src, tar):\n if src == tar:\n return 0.0\n\n src = src.encode('utf-8')\n tar = tar.encode('utf-8')\n\n self._compressor.compress(src)\n src_comp = self._compressor.flush(zlib.Z_FULL_FLUSH)\n self._compressor.compress(tar)\n tar_comp = self._compressor.flush(zlib.Z_FULL_FLUSH)\n self._compressor.compress(src + tar)\n concat_comp = self._compressor.flush(zlib.Z_FULL_FLUSH)\n self._compressor.compress(tar + src)\n concat_comp2 = self._compressor.flush(zlib.Z_FULL_FLUSH)\n\n return (\n min(len(concat_comp), len(concat_comp2))\n - min(len(src_comp), len(tar_comp))\n ) / max(len(src_comp), len(tar_comp))", "response": "Return the NCD between two strings using zlib compression."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn the Phonex code for a word.", "response": "def phonex(word, max_length=4, zero_pad=True):\n \"\"\"Return the Phonex code for a word.\n\n This is a wrapper for :py:meth:`Phonex.encode`.\n\n Parameters\n ----------\n word : str\n The word to transform\n max_length : int\n The length of the code returned (defaults to 4)\n zero_pad : bool\n Pad the end of the return value with 0s to achieve a max_length string\n\n Returns\n -------\n str\n The Phonex value\n\n Examples\n --------\n >>> phonex('Christopher')\n 'C623'\n >>> phonex('Niall')\n 'N400'\n >>> phonex('Schmidt')\n 'S253'\n >>> phonex('Smith')\n 'S530'\n\n \"\"\"\n return Phonex().encode(word, max_length, zero_pad)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef encode(self, word, max_length=4, zero_pad=True):\n name = unicode_normalize('NFKD', text_type(word.upper()))\n name = name.replace('\u00df', 'SS')\n\n # Clamp max_length to [4, 64]\n if max_length != -1:\n max_length = min(max(4, max_length), 64)\n else:\n max_length = 64\n\n name_code = last = ''\n\n # Deletions effected by replacing with next letter which\n # will be ignored due to duplicate handling of Soundex code.\n # This is faster than 'moving' all subsequent letters.\n\n # Remove any trailing Ss\n while name[-1:] == 'S':\n name = name[:-1]\n\n # Phonetic equivalents of first 2 characters\n # Works since duplicate letters are ignored\n if name[:2] == 'KN':\n name = 'N' + name[2:] # KN.. == N..\n elif name[:2] == 'PH':\n name = 'F' + name[2:] # PH.. == F.. (H ignored anyway)\n elif name[:2] == 'WR':\n name = 'R' + name[2:] # WR.. == R..\n\n if name:\n # Special case, ignore H first letter (subsequent Hs ignored\n # anyway)\n # Works since duplicate letters are ignored\n if name[0] == 'H':\n name = name[1:]\n\n if name:\n # Phonetic equivalents of first character\n if name[0] in self._uc_vy_set:\n name = 'A' + name[1:]\n elif name[0] in {'B', 'P'}:\n name = 'B' + name[1:]\n elif name[0] in {'V', 'F'}:\n name = 'F' + name[1:]\n elif name[0] in {'C', 'K', 'Q'}:\n name = 'C' + name[1:]\n elif name[0] in {'G', 'J'}:\n name = 'G' + name[1:]\n elif name[0] in {'S', 'Z'}:\n name = 'S' + name[1:]\n\n name_code = last = name[0]\n\n # Modified Soundex code\n for i in range(1, len(name)):\n code = '0'\n if name[i] in {'B', 'F', 'P', 'V'}:\n code = '1'\n elif name[i] in {'C', 'G', 'J', 'K', 'Q', 'S', 'X', 'Z'}:\n code = '2'\n elif name[i] in {'D', 'T'}:\n if name[i + 1 : i + 2] != 'C':\n code = '3'\n elif name[i] == 'L':\n if name[i + 1 : i + 2] in self._uc_vy_set or i + 1 == len(\n name\n ):\n code = '4'\n elif name[i] in {'M', 'N'}:\n if name[i + 1 : i + 2] in {'D', 'G'}:\n name = name[: i + 1] + name[i] + name[i + 2 :]\n code = '5'\n elif name[i] == 'R':\n if name[i + 1 : i + 2] in self._uc_vy_set or i + 1 == len(\n name\n ):\n code = '6'\n\n if code != last and code != '0' and i != 0:\n name_code += code\n\n last = name_code[-1]\n\n if zero_pad:\n name_code += '0' * max_length\n if not name_code:\n name_code = '0'\n return name_code[:max_length]", "response": "Return the Phonex code for a word."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns Pylint badge color.", "response": "def pylint_color(score):\n \"\"\"Return Pylint badge color.\n\n Parameters\n ----------\n score : float\n A Pylint score\n\n Returns\n -------\n str\n Badge color\n\n \"\"\"\n # These are the score cutoffs for each color above.\n # I.e. score==10 -> brightgreen, down to 7.5 > score >= 5 -> orange\n score_cutoffs = (10, 9.5, 8.5, 7.5, 5)\n for i in range(len(score_cutoffs)):\n if score >= score_cutoffs[i]:\n return BADGE_COLORS[i]\n # and score < 5 -> red\n return BADGE_COLORS[-1]"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef pydocstyle_color(score):\n # These are the score cutoffs for each color above.\n # I.e. score==0 -> brightgreen, down to 100 < score <= 200 -> orange\n score_cutoffs = (0, 10, 25, 50, 100)\n for i in range(len(score_cutoffs)):\n if score <= score_cutoffs[i]:\n return BADGE_COLORS[i]\n # and score > 200 -> red\n return BADGE_COLORS[-1]", "response": "Return pydocstyle badge color."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef flake8_color(score):\n # These are the score cutoffs for each color above.\n # I.e. score==0 -> brightgreen, down to 100 < score <= 200 -> orange\n score_cutoffs = (0, 20, 50, 100, 200)\n for i in range(len(score_cutoffs)):\n if score <= score_cutoffs[i]:\n return BADGE_COLORS[i]\n # and score > 200 -> red\n return BADGE_COLORS[-1]", "response": "Return flake8 badge color."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef gotoh(src, tar, gap_open=1, gap_ext=0.4, sim_func=sim_ident):\n return Gotoh().dist_abs(src, tar, gap_open, gap_ext, sim_func)", "response": "Return the Gotoh score of two strings."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef dist_abs(self, src, tar, gap_open=1, gap_ext=0.4, sim_func=sim_ident):\n d_mat = np_zeros((len(src) + 1, len(tar) + 1), dtype=np_float32)\n p_mat = np_zeros((len(src) + 1, len(tar) + 1), dtype=np_float32)\n q_mat = np_zeros((len(src) + 1, len(tar) + 1), dtype=np_float32)\n\n d_mat[0, 0] = 0\n p_mat[0, 0] = float('-inf')\n q_mat[0, 0] = float('-inf')\n for i in range(1, len(src) + 1):\n d_mat[i, 0] = float('-inf')\n p_mat[i, 0] = -gap_open - gap_ext * (i - 1)\n q_mat[i, 0] = float('-inf')\n q_mat[i, 1] = -gap_open\n for j in range(1, len(tar) + 1):\n d_mat[0, j] = float('-inf')\n p_mat[0, j] = float('-inf')\n p_mat[1, j] = -gap_open\n q_mat[0, j] = -gap_open - gap_ext * (j - 1)\n\n for i in range(1, len(src) + 1):\n for j in range(1, len(tar) + 1):\n sim_val = sim_func(src[i - 1], tar[j - 1])\n d_mat[i, j] = max(\n d_mat[i - 1, j - 1] + sim_val,\n p_mat[i - 1, j - 1] + sim_val,\n q_mat[i - 1, j - 1] + sim_val,\n )\n\n p_mat[i, j] = max(\n d_mat[i - 1, j] - gap_open, p_mat[i - 1, j] - gap_ext\n )\n\n q_mat[i, j] = max(\n d_mat[i, j - 1] - gap_open, q_mat[i, j - 1] - gap_ext\n )\n\n i, j = (n - 1 for n in d_mat.shape)\n return max(d_mat[i, j], p_mat[i, j], q_mat[i, j])", "response": "Return the Gotoh score of two strings."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn the bag distance between two strings.", "response": "def dist_abs(self, src, tar):\n \"\"\"Return the bag distance between two strings.\n\n Parameters\n ----------\n src : str\n Source string for comparison\n tar : str\n Target string for comparison\n\n Returns\n -------\n int\n Bag distance\n\n Examples\n --------\n >>> cmp = Bag()\n >>> cmp.dist_abs('cat', 'hat')\n 1\n >>> cmp.dist_abs('Niall', 'Neil')\n 2\n >>> cmp.dist_abs('aluminum', 'Catalan')\n 5\n >>> cmp.dist_abs('ATCG', 'TAGC')\n 0\n >>> cmp.dist_abs('abcdefg', 'hijklm')\n 7\n >>> cmp.dist_abs('abcdefg', 'hijklmno')\n 8\n\n \"\"\"\n if tar == src:\n return 0\n elif not src:\n return len(tar)\n elif not tar:\n return len(src)\n\n src_bag = Counter(src)\n tar_bag = Counter(tar)\n return max(\n sum((src_bag - tar_bag).values()),\n sum((tar_bag - src_bag).values()),\n )"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning the normalized bag distance between two strings.", "response": "def dist(self, src, tar):\n \"\"\"Return the normalized bag distance between two strings.\n\n Bag distance is normalized by dividing by :math:`max( |src|, |tar| )`.\n\n Parameters\n ----------\n src : str\n Source string for comparison\n tar : str\n Target string for comparison\n\n Returns\n -------\n float\n Normalized bag distance\n\n Examples\n --------\n >>> cmp = Bag()\n >>> cmp.dist('cat', 'hat')\n 0.3333333333333333\n >>> cmp.dist('Niall', 'Neil')\n 0.4\n >>> cmp.dist('aluminum', 'Catalan')\n 0.625\n >>> cmp.dist('ATCG', 'TAGC')\n 0.0\n\n \"\"\"\n if tar == src:\n return 0.0\n if not src or not tar:\n return 1.0\n\n max_length = max(len(src), len(tar))\n\n return self.dist_abs(src, tar) / max_length"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn the stemmer plus word.", "response": "def stem(self, word):\n \"\"\"Return 'CLEF German stemmer plus' stem.\n\n Parameters\n ----------\n word : str\n The word to stem\n\n Returns\n -------\n str\n Word stem\n\n Examples\n --------\n >>> stmr = CLEFGermanPlus()\n >>> clef_german_plus('lesen')\n 'les'\n >>> clef_german_plus('graues')\n 'grau'\n >>> clef_german_plus('buchstabieren')\n 'buchstabi'\n\n \"\"\"\n # lowercase, normalize, and compose\n word = normalize('NFC', text_type(word.lower()))\n\n # remove umlauts\n word = word.translate(self._accents)\n\n # Step 1\n wlen = len(word) - 1\n if wlen > 4 and word[-3:] == 'ern':\n word = word[:-3]\n elif wlen > 3 and word[-2:] in {'em', 'en', 'er', 'es'}:\n word = word[:-2]\n elif wlen > 2 and (\n word[-1] == 'e'\n or (word[-1] == 's' and word[-2] in self._st_ending)\n ):\n word = word[:-1]\n\n # Step 2\n wlen = len(word) - 1\n if wlen > 4 and word[-3:] == 'est':\n word = word[:-3]\n elif wlen > 3 and (\n word[-2:] in {'er', 'en'}\n or (word[-2:] == 'st' and word[-3] in self._st_ending)\n ):\n word = word[:-2]\n\n return word"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn the MLIPNS similarity of two strings.", "response": "def sim_mlipns(src, tar, threshold=0.25, max_mismatches=2):\n \"\"\"Return the MLIPNS similarity of two strings.\n\n This is a wrapper for :py:meth:`MLIPNS.sim`.\n\n Parameters\n ----------\n src : str\n Source string for comparison\n tar : str\n Target string for comparison\n threshold : float\n A number [0, 1] indicating the maximum similarity score, below which\n the strings are considered 'similar' (0.25 by default)\n max_mismatches : int\n A number indicating the allowable number of mismatches to remove before\n declaring two strings not similar (2 by default)\n\n Returns\n -------\n float\n MLIPNS similarity\n\n Examples\n --------\n >>> sim_mlipns('cat', 'hat')\n 1.0\n >>> sim_mlipns('Niall', 'Neil')\n 0.0\n >>> sim_mlipns('aluminum', 'Catalan')\n 0.0\n >>> sim_mlipns('ATCG', 'TAGC')\n 0.0\n\n \"\"\"\n return MLIPNS().sim(src, tar, threshold, max_mismatches)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns the MLIPNS distance between two strings.", "response": "def dist_mlipns(src, tar, threshold=0.25, max_mismatches=2):\n \"\"\"Return the MLIPNS distance between two strings.\n\n This is a wrapper for :py:meth:`MLIPNS.dist`.\n\n Parameters\n ----------\n src : str\n Source string for comparison\n tar : str\n Target string for comparison\n threshold : float\n A number [0, 1] indicating the maximum similarity score, below which\n the strings are considered 'similar' (0.25 by default)\n max_mismatches : int\n A number indicating the allowable number of mismatches to remove before\n declaring two strings not similar (2 by default)\n\n Returns\n -------\n float\n MLIPNS distance\n\n Examples\n --------\n >>> dist_mlipns('cat', 'hat')\n 0.0\n >>> dist_mlipns('Niall', 'Neil')\n 1.0\n >>> dist_mlipns('aluminum', 'Catalan')\n 1.0\n >>> dist_mlipns('ATCG', 'TAGC')\n 1.0\n\n \"\"\"\n return MLIPNS().dist(src, tar, threshold, max_mismatches)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn the MLIPNS similarity of two strings.", "response": "def sim(self, src, tar, threshold=0.25, max_mismatches=2):\n \"\"\"Return the MLIPNS similarity of two strings.\n\n Parameters\n ----------\n src : str\n Source string for comparison\n tar : str\n Target string for comparison\n threshold : float\n A number [0, 1] indicating the maximum similarity score, below\n which the strings are considered 'similar' (0.25 by default)\n max_mismatches : int\n A number indicating the allowable number of mismatches to remove\n before declaring two strings not similar (2 by default)\n\n Returns\n -------\n float\n MLIPNS similarity\n\n Examples\n --------\n >>> sim_mlipns('cat', 'hat')\n 1.0\n >>> sim_mlipns('Niall', 'Neil')\n 0.0\n >>> sim_mlipns('aluminum', 'Catalan')\n 0.0\n >>> sim_mlipns('ATCG', 'TAGC')\n 0.0\n\n \"\"\"\n if tar == src:\n return 1.0\n if not src or not tar:\n return 0.0\n\n mismatches = 0\n ham = Hamming().dist_abs(src, tar, diff_lens=True)\n max_length = max(len(src), len(tar))\n while src and tar and mismatches <= max_mismatches:\n if (\n max_length < 1\n or (1 - (max_length - ham) / max_length) <= threshold\n ):\n return 1.0\n else:\n mismatches += 1\n ham -= 1\n max_length -= 1\n\n if max_length < 1:\n return 1.0\n return 0.0"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn a similarity of two strings.", "response": "def sim(src, tar, method=sim_levenshtein):\n \"\"\"Return a similarity of two strings.\n\n This is a generalized function for calling other similarity functions.\n\n Parameters\n ----------\n src : str\n Source string for comparison\n tar : str\n Target string for comparison\n method : function\n Specifies the similarity metric (:py:func:`sim_levenshtein` by default)\n\n Returns\n -------\n float\n Similarity according to the specified function\n\n Raises\n ------\n AttributeError\n Unknown distance function\n\n Examples\n --------\n >>> round(sim('cat', 'hat'), 12)\n 0.666666666667\n >>> round(sim('Niall', 'Neil'), 12)\n 0.4\n >>> sim('aluminum', 'Catalan')\n 0.125\n >>> sim('ATCG', 'TAGC')\n 0.25\n\n \"\"\"\n if callable(method):\n return method(src, tar)\n else:\n raise AttributeError('Unknown similarity function: ' + str(method))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning a distance between two strings.", "response": "def dist(src, tar, method=sim_levenshtein):\n \"\"\"Return a distance between two strings.\n\n This is a generalized function for calling other distance functions.\n\n Parameters\n ----------\n src : str\n Source string for comparison\n tar : str\n Target string for comparison\n method : function\n Specifies the similarity metric (:py:func:`sim_levenshtein` by default)\n -- Note that this takes a similarity metric function, not a distance\n metric function.\n\n Returns\n -------\n float\n Distance according to the specified function\n\n Raises\n ------\n AttributeError\n Unknown distance function\n\n Examples\n --------\n >>> round(dist('cat', 'hat'), 12)\n 0.333333333333\n >>> round(dist('Niall', 'Neil'), 12)\n 0.6\n >>> dist('aluminum', 'Catalan')\n 0.875\n >>> dist('ATCG', 'TAGC')\n 0.75\n\n \"\"\"\n if callable(method):\n return 1 - method(src, tar)\n else:\n raise AttributeError('Unknown distance function: ' + str(method))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nencoding a MRA personal numeric identifier for a word.", "response": "def encode(self, word):\n \"\"\"Return the MRA personal numeric identifier (PNI) for a word.\n\n Parameters\n ----------\n word : str\n The word to transform\n\n Returns\n -------\n str\n The MRA PNI\n\n Examples\n --------\n >>> pe = MRA()\n >>> pe.encode('Christopher')\n 'CHRPHR'\n >>> pe.encode('Niall')\n 'NL'\n >>> pe.encode('Smith')\n 'SMTH'\n >>> pe.encode('Schmidt')\n 'SCHMDT'\n\n \"\"\"\n if not word:\n return word\n word = word.upper()\n word = word.replace('\u00df', 'SS')\n word = word[0] + ''.join(\n c for c in word[1:] if c not in self._uc_v_set\n )\n word = self._delete_consecutive_repeats(word)\n if len(word) > 6:\n word = word[:3] + word[-3:]\n return word"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the m - degree of the term in the Porter stemmer.", "response": "def _m_degree(self, term):\n \"\"\"Return Porter helper function _m_degree value.\n\n m-degree is equal to the number of V to C transitions\n\n Parameters\n ----------\n term : str\n The word for which to calculate the m-degree\n\n Returns\n -------\n int\n The m-degree as defined in the Porter stemmer definition\n\n \"\"\"\n mdeg = 0\n last_was_vowel = False\n for letter in term:\n if letter in self._vowels:\n last_was_vowel = True\n else:\n if last_was_vowel:\n mdeg += 1\n last_was_vowel = False\n return mdeg"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _has_vowel(self, term):\n for letter in term:\n if letter in self._vowels:\n return True\n return False", "response": "Return True iff a vowel exists in the term."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _ends_in_doubled_cons(self, term):\n return (\n len(term) > 1\n and term[-1] not in self._vowels\n and term[-2] == term[-1]\n )", "response": "Return True iff the stem ends in a doubled consonant."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn True iff the stem ends in cvc", "response": "def _ends_in_cvc(self, term):\n \"\"\"Return Porter helper function _ends_in_cvc value.\n\n Parameters\n ----------\n term : str\n The word to scan for cvc\n\n Returns\n -------\n bool\n True iff the stem ends in cvc (as defined in the Porter stemmer\n definition)\n\n \"\"\"\n return len(term) > 2 and (\n term[-1] not in self._vowels\n and term[-2] in self._vowels\n and term[-3] not in self._vowels\n and term[-1] not in tuple('wxY')\n )"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning the stemmed version of the given word.", "response": "def stem(self, word, early_english=False):\n \"\"\"Return Porter stem.\n\n Parameters\n ----------\n word : str\n The word to stem\n early_english : bool\n Set to True in order to remove -eth & -est (2nd & 3rd person\n singular verbal agreement suffixes)\n\n Returns\n -------\n str\n Word stem\n\n Examples\n --------\n >>> stmr = Porter()\n >>> stmr.stem('reading')\n 'read'\n >>> stmr.stem('suspension')\n 'suspens'\n >>> stmr.stem('elusiveness')\n 'elus'\n\n >>> stmr.stem('eateth', early_english=True)\n 'eat'\n\n \"\"\"\n # lowercase, normalize, and compose\n word = normalize('NFC', text_type(word.lower()))\n\n # Return word if stem is shorter than 2\n if len(word) < 3:\n return word\n\n # Re-map consonantal y to Y (Y will be C, y will be V)\n if word[0] == 'y':\n word = 'Y' + word[1:]\n for i in range(1, len(word)):\n if word[i] == 'y' and word[i - 1] in self._vowels:\n word = word[:i] + 'Y' + word[i + 1 :]\n\n # Step 1a\n if word[-1] == 's':\n if word[-4:] == 'sses':\n word = word[:-2]\n elif word[-3:] == 'ies':\n word = word[:-2]\n elif word[-2:] == 'ss':\n pass\n else:\n word = word[:-1]\n\n # Step 1b\n step1b_flag = False\n if word[-3:] == 'eed':\n if self._m_degree(word[:-3]) > 0:\n word = word[:-1]\n elif word[-2:] == 'ed':\n if self._has_vowel(word[:-2]):\n word = word[:-2]\n step1b_flag = True\n elif word[-3:] == 'ing':\n if self._has_vowel(word[:-3]):\n word = word[:-3]\n step1b_flag = True\n elif early_english:\n if word[-3:] == 'est':\n if self._has_vowel(word[:-3]):\n word = word[:-3]\n step1b_flag = True\n elif word[-3:] == 'eth':\n if self._has_vowel(word[:-3]):\n word = word[:-3]\n step1b_flag = True\n\n if step1b_flag:\n if word[-2:] in {'at', 'bl', 'iz'}:\n word += 'e'\n elif self._ends_in_doubled_cons(word) and word[-1] not in {\n 'l',\n 's',\n 'z',\n }:\n word = word[:-1]\n elif self._m_degree(word) == 1 and self._ends_in_cvc(word):\n word += 'e'\n\n # Step 1c\n if word[-1] in {'Y', 'y'} and self._has_vowel(word[:-1]):\n word = word[:-1] + 'i'\n\n # Step 2\n if len(word) > 1:\n if word[-2] == 'a':\n if word[-7:] == 'ational':\n if self._m_degree(word[:-7]) > 0:\n word = word[:-5] + 'e'\n elif word[-6:] == 'tional':\n if self._m_degree(word[:-6]) > 0:\n word = word[:-2]\n elif word[-2] == 'c':\n if word[-4:] in {'enci', 'anci'}:\n if self._m_degree(word[:-4]) > 0:\n word = word[:-1] + 'e'\n elif word[-2] == 'e':\n if word[-4:] == 'izer':\n if self._m_degree(word[:-4]) > 0:\n word = word[:-1]\n elif word[-2] == 'g':\n if word[-4:] == 'logi':\n if self._m_degree(word[:-4]) > 0:\n word = word[:-1]\n elif word[-2] == 'l':\n if word[-3:] == 'bli':\n if self._m_degree(word[:-3]) > 0:\n word = word[:-1] + 'e'\n elif word[-4:] == 'alli':\n if self._m_degree(word[:-4]) > 0:\n word = word[:-2]\n elif word[-5:] == 'entli':\n if self._m_degree(word[:-5]) > 0:\n word = word[:-2]\n elif word[-3:] == 'eli':\n if self._m_degree(word[:-3]) > 0:\n word = word[:-2]\n elif word[-5:] == 'ousli':\n if self._m_degree(word[:-5]) > 0:\n word = word[:-2]\n elif word[-2] == 'o':\n if word[-7:] == 'ization':\n if self._m_degree(word[:-7]) > 0:\n word = word[:-5] + 'e'\n elif word[-5:] == 'ation':\n if self._m_degree(word[:-5]) > 0:\n word = word[:-3] + 'e'\n elif word[-4:] == 'ator':\n if self._m_degree(word[:-4]) > 0:\n word = word[:-2] + 'e'\n elif word[-2] == 's':\n if word[-5:] == 'alism':\n if self._m_degree(word[:-5]) > 0:\n word = word[:-3]\n elif word[-7:] in {'iveness', 'fulness', 'ousness'}:\n if self._m_degree(word[:-7]) > 0:\n word = word[:-4]\n elif word[-2] == 't':\n if word[-5:] == 'aliti':\n if self._m_degree(word[:-5]) > 0:\n word = word[:-3]\n elif word[-5:] == 'iviti':\n if self._m_degree(word[:-5]) > 0:\n word = word[:-3] + 'e'\n elif word[-6:] == 'biliti':\n if self._m_degree(word[:-6]) > 0:\n word = word[:-5] + 'le'\n\n # Step 3\n if word[-5:] in 'icate':\n if self._m_degree(word[:-5]) > 0:\n word = word[:-3]\n elif word[-5:] == 'ative':\n if self._m_degree(word[:-5]) > 0:\n word = word[:-5]\n elif word[-5:] in {'alize', 'iciti'}:\n if self._m_degree(word[:-5]) > 0:\n word = word[:-3]\n elif word[-4:] == 'ical':\n if self._m_degree(word[:-4]) > 0:\n word = word[:-2]\n elif word[-3:] == 'ful':\n if self._m_degree(word[:-3]) > 0:\n word = word[:-3]\n elif word[-4:] == 'ness':\n if self._m_degree(word[:-4]) > 0:\n word = word[:-4]\n\n # Step 4\n if word[-2:] == 'al':\n if self._m_degree(word[:-2]) > 1:\n word = word[:-2]\n elif word[-4:] in {'ance', 'ence'}:\n if self._m_degree(word[:-4]) > 1:\n word = word[:-4]\n elif word[-2:] in {'er', 'ic'}:\n if self._m_degree(word[:-2]) > 1:\n word = word[:-2]\n elif word[-4:] in {'able', 'ible'}:\n if self._m_degree(word[:-4]) > 1:\n word = word[:-4]\n elif word[-3:] == 'ant':\n if self._m_degree(word[:-3]) > 1:\n word = word[:-3]\n elif word[-5:] == 'ement':\n if self._m_degree(word[:-5]) > 1:\n word = word[:-5]\n elif word[-4:] == 'ment':\n if self._m_degree(word[:-4]) > 1:\n word = word[:-4]\n elif word[-3:] == 'ent':\n if self._m_degree(word[:-3]) > 1:\n word = word[:-3]\n elif word[-4:] in {'sion', 'tion'}:\n if self._m_degree(word[:-3]) > 1:\n word = word[:-3]\n elif word[-2:] == 'ou':\n if self._m_degree(word[:-2]) > 1:\n word = word[:-2]\n elif word[-3:] in {'ism', 'ate', 'iti', 'ous', 'ive', 'ize'}:\n if self._m_degree(word[:-3]) > 1:\n word = word[:-3]\n\n # Step 5a\n if word[-1] == 'e':\n if self._m_degree(word[:-1]) > 1:\n word = word[:-1]\n elif self._m_degree(word[:-1]) == 1 and not self._ends_in_cvc(\n word[:-1]\n ):\n word = word[:-1]\n\n # Step 5b\n if word[-2:] == 'll' and self._m_degree(word) > 1:\n word = word[:-1]\n\n # Change 'Y' back to 'y' if it survived stemming\n for i in range(len(word)):\n if word[i] == 'Y':\n word = word[:i] + 'y' + word[i + 1 :]\n\n return word"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns the SoundexBR encoding of a word.", "response": "def soundex_br(word, max_length=4, zero_pad=True):\n \"\"\"Return the SoundexBR encoding of a word.\n\n This is a wrapper for :py:meth:`SoundexBR.encode`.\n\n Parameters\n ----------\n word : str\n The word to transform\n max_length : int\n The length of the code returned (defaults to 4)\n zero_pad : bool\n Pad the end of the return value with 0s to achieve a max_length string\n\n Returns\n -------\n str\n The SoundexBR code\n\n Examples\n --------\n >>> soundex_br('Oliveira')\n 'O416'\n >>> soundex_br('Almeida')\n 'A453'\n >>> soundex_br('Barbosa')\n 'B612'\n >>> soundex_br('Ara\u00fajo')\n 'A620'\n >>> soundex_br('Gon\u00e7alves')\n 'G524'\n >>> soundex_br('Goncalves')\n 'G524'\n\n \"\"\"\n return SoundexBR().encode(word, max_length, zero_pad)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning the SoundexBR encoding of a word.", "response": "def encode(self, word, max_length=4, zero_pad=True):\n \"\"\"Return the SoundexBR encoding of a word.\n\n Parameters\n ----------\n word : str\n The word to transform\n max_length : int\n The length of the code returned (defaults to 4)\n zero_pad : bool\n Pad the end of the return value with 0s to achieve a max_length\n string\n\n Returns\n -------\n str\n The SoundexBR code\n\n Examples\n --------\n >>> soundex_br('Oliveira')\n 'O416'\n >>> soundex_br('Almeida')\n 'A453'\n >>> soundex_br('Barbosa')\n 'B612'\n >>> soundex_br('Ara\u00fajo')\n 'A620'\n >>> soundex_br('Gon\u00e7alves')\n 'G524'\n >>> soundex_br('Goncalves')\n 'G524'\n\n \"\"\"\n word = unicode_normalize('NFKD', text_type(word.upper()))\n word = ''.join(c for c in word if c in self._uc_set)\n\n if word[:2] == 'WA':\n first = 'V'\n elif word[:1] == 'K' and word[1:2] in {'A', 'O', 'U'}:\n first = 'C'\n elif word[:1] == 'C' and word[1:2] in {'I', 'E'}:\n first = 'S'\n elif word[:1] == 'G' and word[1:2] in {'E', 'I'}:\n first = 'J'\n elif word[:1] == 'Y':\n first = 'I'\n elif word[:1] == 'H':\n first = word[1:2]\n word = word[1:]\n else:\n first = word[:1]\n\n sdx = first + word[1:].translate(self._trans)\n sdx = self._delete_consecutive_repeats(sdx)\n sdx = sdx.replace('0', '')\n\n if zero_pad:\n sdx += '0' * max_length\n\n return sdx[:max_length]"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef filter_symlog(y, base=10.0):\n log_base = np.log(base)\n sign = np.sign(y)\n logs = np.log(np.abs(y) / log_base)\n return sign * logs", "response": "Symmetrical logarithmic scale.\n\n Optional arguments:\n\n *base*:\n The base of the logarithm."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef filter_savitzky_golay(y, window_size=5, order=2, deriv=0, rate=1):\n try:\n window_size = np.abs(np.int(window_size))\n order = np.abs(np.int(order))\n except ValueError:\n raise ValueError('window_size and order must be integers')\n\n if window_size % 2 != 1 or window_size < 1:\n raise ValueError('window_size size must be a positive odd number')\n if window_size < order + 2:\n raise ValueError('window_size is too small for the polynomials order')\n\n order_range = range(order + 1)\n half_window = (window_size - 1) // 2\n\n # precompute limits\n minimum = np.min(y)\n maximum = np.max(y)\n # precompute coefficients\n b = np.mat([\n [k ** i for i in order_range]\n for k in range(-half_window, half_window + 1)\n ])\n m = np.linalg.pinv(b).A[deriv] * rate ** deriv * math.factorial(deriv)\n # pad the signal at the extremes with values taken from the original signal\n firstvals = y[0] - np.abs(y[1:half_window+1][::-1] - y[0])\n lastvals = y[-1] + np.abs(y[-half_window-1:-1][::-1] - y[-1])\n y = np.concatenate((firstvals, y, lastvals))\n return np.clip(\n np.convolve(m[::-1], y, mode='valid'),\n minimum,\n maximum,\n )", "response": "Smooth and optionally differentiate with a Savitzky - Golay filter."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nshows usage and available curve functions.", "response": "def usage_function(parser):\n \"\"\"Show usage and available curve functions.\"\"\"\n parser.print_usage()\n print('')\n print('available functions:')\n for function in sorted(FUNCTION):\n doc = FUNCTION[function].__doc__.strip().splitlines()[0]\n print(' %-12s %s' % (function + ':', doc))\n\n return 0"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nshow usage and available palettes.", "response": "def usage_palette(parser):\n \"\"\"Show usage and available palettes.\"\"\"\n parser.print_usage()\n print('')\n print('available palettes:')\n for palette in sorted(PALETTE):\n print(' %-12s' % (palette,))\n\n return 0"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef run():\n import argparse\n\n parser = argparse.ArgumentParser(\n description=(\n 'Text mode diagrams using UTF-8 characters and fancy colors.'\n ),\n epilog=\"\"\"\n (1): only works for the horizontal bar graph, the first argument is the key\n and the second value is the data point.\n\"\"\",\n )\n\n group = parser.add_argument_group('optional drawing mode')\n group.add_argument(\n '-G', '--graph',\n dest='mode', action='store_const', const='g',\n help='axis drawing mode (default)',\n )\n group.add_argument(\n '-H', '--horizontal-bars',\n dest='mode', action='store_const', const='h',\n help='horizontal drawing mode',\n )\n group.add_argument(\n '-V', '--vertical-bars',\n dest='mode', action='store_const', const='v',\n help='vertical drawing mode',\n )\n\n group = parser.add_argument_group('optional drawing arguments')\n group.add_argument(\n '-a', '--axis',\n dest='axis', action='store_const', const=True, default=True,\n help='draw axis (default: yes)',\n )\n group.add_argument(\n '-A', '--no-axis',\n dest='axis', action='store_const', const=False,\n help=\"don't draw axis\",\n )\n group.add_argument(\n '-c', '--color',\n dest='color', action='store_const', const=True, default=True,\n help='use colors (default: yes)',\n )\n group.add_argument(\n '-C', '--no-color',\n dest='color', action='store_const', const=False,\n help=\"don't use colors\",\n )\n group.add_argument(\n '-l', '--legend',\n dest='legend', action='store_const', const=True, default=True,\n help='draw y-axis legend (default: yes)',\n )\n group.add_argument(\n '-L', '--no-legend',\n dest='legend', action='store_const', const=False,\n help=\"don't draw y-axis legend\",\n )\n group.add_argument(\n '-f', '--function',\n default=None, metavar='function',\n help='curve manipulation function, use \"help\" for a list',\n )\n group.add_argument(\n '-p', '--palette',\n default='default', metavar='palette',\n help='palette name, use \"help\" for a list',\n )\n group.add_argument(\n '-x', '--width',\n default=0, type=int, metavar='characters',\n help='drawing width (default: auto)',\n )\n group.add_argument(\n '-y', '--height',\n default=0, type=int, metavar='characters',\n help='drawing height (default: auto)',\n )\n group.add_argument(\n '-r', '--reverse',\n default=False, action='store_true',\n help='reverse draw graph',\n )\n group.add_argument(\n '--sort-by-column',\n default=0, type=int, metavar='index',\n help='sort input data based on given column',\n )\n\n group = parser.add_argument_group('optional input and output arguments')\n group.add_argument(\n '-b', '--batch',\n default=False, action='store_true',\n help='batch mode (default: no)',\n )\n group.add_argument(\n '-k', '--keys',\n default=False, action='store_true',\n help='input are key-value pairs (default: no) (1)',\n )\n group.add_argument(\n '-s', '--sleep',\n default=0, type=float,\n help='batch poll sleep time (default: none)',\n )\n group.add_argument(\n '-i', '--input',\n default='-', metavar='file',\n help='input file (default: stdin)',\n )\n group.add_argument(\n '-o', '--output',\n default='-', metavar='file',\n help='output file (default: stdout)',\n )\n group.add_argument(\n '-e', '--encoding',\n dest='encoding', default='',\n help='output encoding (default: auto)',\n )\n\n option = parser.parse_args()\n\n if option.function == 'help':\n return usage_function(parser)\n\n if option.palette == 'help':\n return usage_palette(parser)\n\n option.mode = option.mode or 'g'\n option.size = Point((option.width, option.height))\n\n if option.input in ['-', 'stdin']:\n istream = sys.stdin\n else:\n istream = open(option.input, 'r')\n\n if option.output in ['-', 'stdout']:\n try:\n ostream = sys.stdout.buffer\n except AttributeError:\n ostream = sys.stdout\n else:\n ostream = open(option.output, 'wb')\n\n option.encoding = option.encoding or Terminal().encoding\n\n if option.mode == 'g':\n engine = AxisGraph(option.size, option)\n\n elif option.mode == 'h':\n engine = HorizontalBarGraph(option.size, option)\n\n elif option.mode == 'v':\n engine = VerticalBarGraph(option.size, option)\n\n else:\n parser.error('invalid mode')\n return 1\n\n engine.consume(istream, ostream, batch=option.batch)", "response": "Entry point for the main entrypoint."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef size(self):\n for fd in range(3):\n cr = self._ioctl_GWINSZ(fd)\n if cr:\n break\n if not cr:\n try:\n fd = os.open(os.ctermid(), os.O_RDONLY)\n cr = self._ioctl_GWINSZ(fd)\n os.close(fd)\n except Exception:\n pass\n\n if not cr:\n env = os.environ\n cr = (env.get('LINES', 25), env.get('COLUMNS', 80))\n\n return int(cr[1]), int(cr[0])", "response": "Get the current terminal size."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn the escape sequence for indexed color index.", "response": "def color(self, index):\n \"\"\"Get the escape sequence for indexed color ``index``.\n\n The ``index`` is a color index in the 256 color space. The color space\n consists of:\n\n * 0x00-0x0f: default EGA colors\n * 0x10-0xe7: 6x6x6 RGB cubes\n * 0xe8-0xff: gray scale ramp\n \"\"\"\n if self.colors == 16:\n if index >= 8:\n return self.csi('bold') + self.csi('setaf', index - 8)\n else:\n return self.csi('sgr0') + self.csi('setaf', index)\n else:\n return self.csi('setaf', index)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning the escape sequence for the selected Control Sequence.", "response": "def csi(self, capname, *args):\n \"\"\"Return the escape sequence for the selected Control Sequence.\"\"\"\n value = curses.tigetstr(capname)\n if value is None:\n return b''\n else:\n return curses.tparm(value, *args)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning a value wrapped in the selected CSI and does a reset.", "response": "def csi_wrap(self, value, capname, *args):\n \"\"\"Return a value wrapped in the selected CSI and does a reset.\"\"\"\n if isinstance(value, str):\n value = value.encode('utf-8')\n return b''.join([\n self.csi(capname, *args),\n value,\n self.csi('sgr0'),\n ])"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef consume(self, istream, ostream, batch=False):\n datapoints = [] # List of 2-tuples\n\n if batch:\n sleep = max(0.01, self.option.sleep)\n fd = istream.fileno()\n while True:\n try:\n if select.select([fd], [], [], sleep):\n try:\n line = istream.readline()\n if line == '':\n break\n datapoints.append(self.consume_line(line))\n except ValueError:\n continue\n\n if self.option.sort_by_column:\n datapoints = sorted(datapoints, key=itemgetter(self.option.sort_by_column - 1))\n\n if len(datapoints) > 1:\n datapoints = datapoints[-self.maximum_points:]\n self.update([dp[0] for dp in datapoints], [dp[1] for dp in datapoints])\n self.render(ostream)\n\n time.sleep(sleep)\n\n except KeyboardInterrupt:\n break\n\n else:\n for line in istream:\n try:\n datapoints.append(self.consume_line(line))\n except ValueError:\n pass\n\n if self.option.sort_by_column:\n datapoints = sorted(datapoints, key=itemgetter(self.option.sort_by_column - 1))\n\n self.update([dp[0] for dp in datapoints], [dp[1] for dp in datapoints])\n self.render(ostream)", "response": "Read points from istream and output to ostream."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef consume_line(self, line):\n data = RE_VALUE_KEY.split(line.strip(), 1)\n if len(data) == 1:\n return float(data[0]), None\n else:\n return float(data[0]), data[1].strip()", "response": "Consume data from a line."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef update(self, points, values=None):\n self.values = values or [None] * len(points)\n\n if np is None:\n if self.option.function:\n warnings.warn('numpy not available, function ignored')\n self.points = points\n self.minimum = min(self.points)\n self.maximum = max(self.points)\n self.current = self.points[-1]\n\n else:\n self.points = self.apply_function(points)\n self.minimum = np.min(self.points)\n self.maximum = np.max(self.points)\n self.current = self.points[-1]\n\n if self.maximum == self.minimum:\n self.extents = 1\n else:\n self.extents = (self.maximum - self.minimum)\n self.extents = (self.maximum - self.minimum)", "response": "Update the internal state of the object."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngenerates a color ramp for the current screen height.", "response": "def color_ramp(self, size):\n \"\"\"Generate a color ramp for the current screen height.\"\"\"\n color = PALETTE.get(self.option.palette, {})\n color = color.get(self.term.colors, None)\n color_ramp = []\n if color is not None:\n ratio = len(color) / float(size)\n for i in range(int(size)):\n color_ramp.append(self.term.color(color[int(ratio * i)]))\n\n return color_ramp"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef human(self, size, base=1000, units=' kMGTZ'):\n sign = '+' if size >= 0 else '-'\n size = abs(size)\n if size < 1000:\n return '%s%d' % (sign, size)\n for i, suffix in enumerate(units):\n unit = 1000 ** (i + 1)\n if size < unit:\n return ('%s%.01f%s' % (\n sign,\n size / float(unit) * base,\n suffix,\n )).strip()\n raise OverflowError", "response": "Convert the input size to human readable short form."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef apply_function(self, points):\n if not self.option.function:\n return points\n\n if np is None:\n raise ImportError('numpy is not available')\n\n if ':' in self.option.function:\n function, arguments = self.option.function.split(':', 1)\n arguments = arguments.split(',')\n else:\n function = self.option.function\n arguments = []\n\n # Resolve arguments\n arguments = list(map(self._function_argument, arguments))\n\n # Resolve function\n filter_function = FUNCTION.get(function)\n\n if filter_function is None:\n raise TypeError('Invalid function \"%s\"' % (function,))\n\n else:\n # We wrap in ``list()`` to consume generators and iterators, as\n # ``np.array`` doesn't do this for us.\n return filter_function(np.array(list(points)), *arguments)", "response": "Run the filter function on the provided points."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nresolve the points to make a line between two points.", "response": "def line(self, p1, p2, resolution=1):\n \"\"\"Resolve the points to make a line between two points.\"\"\"\n xdiff = max(p1.x, p2.x) - min(p1.x, p2.x)\n ydiff = max(p1.y, p2.y) - min(p1.y, p2.y)\n xdir = [-1, 1][int(p1.x <= p2.x)]\n ydir = [-1, 1][int(p1.y <= p2.y)]\n r = int(round(max(xdiff, ydiff)))\n if r == 0:\n return\n\n for i in range((r + 1) * resolution):\n x = p1.x\n y = p1.y\n\n if xdiff:\n x += (float(i) * xdiff) / r * xdir / resolution\n if ydiff:\n y += (float(i) * ydiff) / r * ydir / resolution\n\n yield Point((x, y))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nsetting a text value in the screen canvas.", "response": "def set_text(self, point, text):\n \"\"\"Set a text value in the screen canvas.\"\"\"\n if not self.option.legend:\n return\n\n if not isinstance(point, Point):\n point = Point(point)\n\n for offset, char in enumerate(str(text)):\n self.screen.canvas[point.y][point.x + offset] = char"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nrenders the term to the given stream.", "response": "def render(self, stream):\n \"\"\"Render graph to stream.\"\"\"\n encoding = self.option.encoding or self.term.encoding or \"utf8\"\n\n if self.option.color:\n ramp = self.color_ramp(self.size.y)[::-1]\n else:\n ramp = None\n\n if self.cycle >= 1 and self.lines:\n stream.write(self.term.csi('cuu', self.lines))\n\n zero = int(self.null / 4) # Zero crossing\n lines = 0\n for y in range(self.screen.size.y):\n if y == zero and self.size.y > 1:\n stream.write(self.term.csi('smul'))\n if ramp:\n stream.write(ramp[y])\n\n for x in range(self.screen.size.x):\n point = Point((x, y))\n if point in self.screen:\n value = self.screen[point]\n if isinstance(value, int):\n stream.write(chr(self.base + value).encode(encoding))\n else:\n stream.write(self.term.csi('sgr0'))\n stream.write(self.term.csi_wrap(\n value.encode(encoding),\n 'bold'\n ))\n if y == zero and self.size.y > 1:\n stream.write(self.term.csi('smul'))\n if ramp:\n stream.write(ramp[y])\n else:\n stream.write(b' ')\n\n if y == zero and self.size.y > 1:\n stream.write(self.term.csi('rmul'))\n if ramp:\n stream.write(self.term.csi('sgr0'))\n\n stream.write(b'\\n')\n lines += 1\n stream.flush()\n\n self.cycle = self.cycle + 1\n self.lines = lines"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _normalised_numpy(self):\n dx = (self.screen.width / float(len(self.points)))\n oy = (self.screen.height)\n points = np.array(self.points) - self.minimum\n points = points * 4.0 / self.extents * self.size.y\n for x, y in enumerate(points):\n yield Point((\n dx * x,\n min(oy, oy - y),\n ))", "response": "Normalised data points using numpy."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nnormalises data points using pure Python.", "response": "def _normalised_python(self):\n \"\"\"Normalised data points using pure Python.\"\"\"\n dx = (self.screen.width / float(len(self.points)))\n oy = (self.screen.height)\n for x, point in enumerate(self.points):\n y = (point - self.minimum) * 4.0 / self.extents * self.size.y\n yield Point((\n dx * x,\n min(oy, oy - y),\n ))"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nsets pixel at x y point.", "response": "def set(self, point):\n \"\"\"Set pixel at (x, y) point.\"\"\"\n if not isinstance(point, Point):\n point = Point(point)\n\n rx = self.round(point.x)\n ry = self.round(point.y)\n\n item = Point((rx >> 1, min(ry >> 2, self.size.y)))\n self.screen[item] |= self.pixels[ry & 3][rx & 1]"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef mem_size(self):\n '''used when allocating memory ingame'''\n\n data_len = self._data_mem_size\n node_count = len(list(self.xml_doc.iter(tag=etree.Element)))\n\n if self.compressed:\n size = 52 * node_count + data_len + 630\n else:\n tags_len = 0\n for e in self.xml_doc.iter(tag=etree.Element):\n e_len = max(len(e.tag), 8)\n e_len = (e_len + 3) & ~3\n tags_len += e_len\n\n size = 56 * node_count + data_len + 630 + tags_len\n\n # debugging\n #print('nodes:{} ({}) data:{} ({})'.format(node_count,hex(node_count), data_len, hex(data_len)))\n\n return (size + 8) & ~7", "response": "used when allocating memory ingame"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nload the class from the class_path string", "response": "def _load_class(class_path, default):\n \"\"\" Loads the class from the class_path string \"\"\"\n if class_path is None:\n return default\n\n component = class_path.rsplit('.', 1)\n result_processor = getattr(\n importlib.import_module(component[0]),\n component[1],\n default\n ) if len(component) > 1 else default\n\n return result_processor"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _is_iterable(item):\n return isinstance(item, collections.Iterable) and not isinstance(item, six.string_types)", "response": "Checks if an item is iterable"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nprocessing pagination requests from request parameter", "response": "def _process_pagination_values(request):\n \"\"\" process pagination requests from request parameter \"\"\"\n size = 20\n page = 0\n from_ = 0\n if \"page_size\" in request.POST:\n size = int(request.POST[\"page_size\"])\n max_page_size = getattr(settings, \"SEARCH_MAX_PAGE_SIZE\", 100)\n # The parens below are superfluous, but make it much clearer to the reader what is going on\n if not (0 < size <= max_page_size): # pylint: disable=superfluous-parens\n raise ValueError(_('Invalid page size of {page_size}').format(page_size=size))\n\n if \"page_index\" in request.POST:\n page = int(request.POST[\"page_index\"])\n from_ = page * size\n return size, from_, page"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncreating separate dictionary of supported filter values provided", "response": "def _process_field_values(request):\n \"\"\" Create separate dictionary of supported filter values provided \"\"\"\n return {\n field_key: request.POST[field_key]\n for field_key in request.POST\n if field_key in course_discovery_filter_fields()\n }"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nsearching for the specified object in the object tree.", "response": "def do_search(request, course_id=None):\n \"\"\"\n Search view for http requests\n\n Args:\n request (required) - django request object\n course_id (optional) - course_id within which to restrict search\n\n Returns:\n http json response with the following fields\n \"took\" - how many seconds the operation took\n \"total\" - how many results were found\n \"max_score\" - maximum score from these results\n \"results\" - json array of result documents\n\n or\n\n \"error\" - displayable information about an error that occured on the server\n\n POST Params:\n \"search_string\" (required) - text upon which to search\n \"page_size\" (optional)- how many results to return per page (defaults to 20, with maximum cutoff at 100)\n \"page_index\" (optional) - for which page (zero-indexed) to include results (defaults to 0)\n \"\"\"\n\n # Setup search environment\n SearchInitializer.set_search_enviroment(request=request, course_id=course_id)\n\n results = {\n \"error\": _(\"Nothing to search\")\n }\n status_code = 500\n\n search_term = request.POST.get(\"search_string\", None)\n\n try:\n if not search_term:\n raise ValueError(_('No search term provided for search'))\n\n size, from_, page = _process_pagination_values(request)\n\n # Analytics - log search request\n track.emit(\n 'edx.course.search.initiated',\n {\n \"search_term\": search_term,\n \"page_size\": size,\n \"page_number\": page,\n }\n )\n\n results = perform_search(\n search_term,\n user=request.user,\n size=size,\n from_=from_,\n course_id=course_id\n )\n\n status_code = 200\n\n # Analytics - log search results before sending to browser\n track.emit(\n 'edx.course.search.results_displayed',\n {\n \"search_term\": search_term,\n \"page_size\": size,\n \"page_number\": page,\n \"results_count\": results[\"total\"],\n }\n )\n\n except ValueError as invalid_err:\n results = {\n \"error\": six.text_type(invalid_err)\n }\n log.debug(six.text_type(invalid_err))\n\n except QueryParseError:\n results = {\n \"error\": _('Your query seems malformed. Check for unmatched quotes.')\n }\n\n # Allow for broad exceptions here - this is an entry point from external reference\n except Exception as err: # pylint: disable=broad-except\n results = {\n \"error\": _('An error occurred when searching for \"{search_string}\"').format(search_string=search_term)\n }\n log.exception(\n 'Search view exception when searching for %s for user %s: %r',\n search_term,\n request.user.id,\n err\n )\n\n return JsonResponse(results, status=status_code)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef course_discovery(request):\n results = {\n \"error\": _(\"Nothing to search\")\n }\n status_code = 500\n\n search_term = request.POST.get(\"search_string\", None)\n\n try:\n size, from_, page = _process_pagination_values(request)\n field_dictionary = _process_field_values(request)\n\n # Analytics - log search request\n track.emit(\n 'edx.course_discovery.search.initiated',\n {\n \"search_term\": search_term,\n \"page_size\": size,\n \"page_number\": page,\n }\n )\n\n results = course_discovery_search(\n search_term=search_term,\n size=size,\n from_=from_,\n field_dictionary=field_dictionary,\n )\n\n # Analytics - log search results before sending to browser\n track.emit(\n 'edx.course_discovery.search.results_displayed',\n {\n \"search_term\": search_term,\n \"page_size\": size,\n \"page_number\": page,\n \"results_count\": results[\"total\"],\n }\n )\n\n status_code = 200\n\n except ValueError as invalid_err:\n results = {\n \"error\": six.text_type(invalid_err)\n }\n log.debug(six.text_type(invalid_err))\n\n except QueryParseError:\n results = {\n \"error\": _('Your query seems malformed. Check for unmatched quotes.')\n }\n\n # Allow for broad exceptions here - this is an entry point from external reference\n except Exception as err: # pylint: disable=broad-except\n results = {\n \"error\": _('An error occurred when searching for \"{search_string}\"').format(search_string=search_term)\n }\n log.exception(\n 'Search view exception when searching for %s for user %s: %r',\n search_term,\n request.user.id,\n err\n )\n\n return JsonResponse(results, status=status_code)", "response": "Search for courses in the object tree and return the course tree."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ntranslating the hits response into a list of objects", "response": "def _translate_hits(es_response):\n \"\"\" Provide resultset in our desired format from elasticsearch results \"\"\"\n\n def translate_result(result):\n \"\"\" Any conversion from ES result syntax into our search engine syntax \"\"\"\n translated_result = copy.copy(result)\n data = translated_result.pop(\"_source\")\n\n translated_result.update({\n \"data\": data,\n \"score\": translated_result[\"_score\"]\n })\n\n return translated_result\n\n def translate_facet(result):\n \"\"\" Any conversion from ES facet syntax into our search engine sytax \"\"\"\n terms = {term[\"term\"]: term[\"count\"] for term in result[\"terms\"]}\n return {\n \"terms\": terms,\n \"total\": result[\"total\"],\n \"other\": result[\"other\"],\n }\n\n results = [translate_result(hit) for hit in es_response[\"hits\"][\"hits\"]]\n response = {\n \"took\": es_response[\"took\"],\n \"total\": es_response[\"hits\"][\"total\"],\n \"max_score\": es_response[\"hits\"][\"max_score\"],\n \"results\": results,\n }\n\n if \"facets\" in es_response:\n response[\"facets\"] = {facet: translate_facet(es_response[\"facets\"][facet]) for facet in es_response[\"facets\"]}\n\n return response"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn the field to apply into filter", "response": "def _get_filter_field(field_name, field_value):\n \"\"\" Return field to apply into filter, if an array then use a range, otherwise look for a term match \"\"\"\n filter_field = None\n if isinstance(field_value, ValueRange):\n range_values = {}\n if field_value.lower:\n range_values.update({\"gte\": field_value.lower_string})\n if field_value.upper:\n range_values.update({\"lte\": field_value.upper_string})\n filter_field = {\n \"range\": {\n field_name: range_values\n }\n }\n elif _is_iterable(field_value):\n filter_field = {\n \"terms\": {\n field_name: field_value\n }\n }\n else:\n filter_field = {\n \"term\": {\n field_name: field_value\n }\n }\n return filter_field"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _process_field_queries(field_dictionary):\n def field_item(field):\n \"\"\" format field match as \"match\" item for elasticsearch query \"\"\"\n return {\n \"match\": {\n field: field_dictionary[field]\n }\n }\n\n return [field_item(field) for field in field_dictionary]", "response": "Process the field queries for the resource."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nprocess the filter_dictionary to get the list of items that can be included and included.", "response": "def _process_filters(filter_dictionary):\n \"\"\"\n We have a filter_dictionary - this means that if the field is included\n and matches, then we can include, OR if the field is undefined, then we\n assume it is safe to include\n \"\"\"\n def filter_item(field):\n \"\"\" format elasticsearch filter to pass if value matches OR field is not included \"\"\"\n if filter_dictionary[field] is not None:\n return {\n \"or\": [\n _get_filter_field(field, filter_dictionary[field]),\n {\n \"missing\": {\n \"field\": field\n }\n }\n ]\n }\n\n return {\n \"missing\": {\n \"field\": field\n }\n }\n\n return [filter_item(field) for field in filter_dictionary]"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _process_exclude_dictionary(exclude_dictionary):\n # not_properties will hold the generated term queries.\n not_properties = []\n for exclude_property in exclude_dictionary:\n exclude_values = exclude_dictionary[exclude_property]\n if not isinstance(exclude_values, list):\n exclude_values = [exclude_values]\n not_properties.extend([{\"term\": {exclude_property: exclude_value}} for exclude_value in exclude_values])\n\n # Returning a query segment with an empty list freaks out ElasticSearch,\n # so just return an empty segment.\n if not not_properties:\n return {}\n\n return {\n \"not\": {\n \"filter\": {\n \"or\": not_properties\n }\n }\n }", "response": "Processes the exclude dictionary and returns a query segment that will exclude unwanted results."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nprocess the facet terms.", "response": "def _process_facet_terms(facet_terms):\n \"\"\" We have a list of terms with which we return facets \"\"\"\n elastic_facets = {}\n for facet in facet_terms:\n facet_term = {\"field\": facet}\n if facet_terms[facet]:\n for facet_option in facet_terms[facet]:\n facet_term[facet_option] = facet_terms[facet][facet_option]\n\n elastic_facets[facet] = {\n \"terms\": facet_term\n }\n\n return elastic_facets"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nfetching mapped - items structure from cache", "response": "def get_mappings(cls, index_name, doc_type):\n \"\"\" fetch mapped-items structure from cache \"\"\"\n return cache.get(cls.get_cache_item_name(index_name, doc_type), {})"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nsetting mappings into cache", "response": "def set_mappings(cls, index_name, doc_type, mappings):\n \"\"\" set new mapped-items structure into cache \"\"\"\n cache.set(cls.get_cache_item_name(index_name, doc_type), mappings)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nlogging the errors and raises a general ElasticSearch Exception", "response": "def log_indexing_error(cls, indexing_errors):\n \"\"\" Logs indexing errors and raises a general ElasticSearch Exception\"\"\"\n indexing_errors_log = []\n for indexing_error in indexing_errors:\n indexing_errors_log.append(str(indexing_error))\n raise exceptions.ElasticsearchException(', '.join(indexing_errors_log))"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngets the mappings for the given doc_type from the index.", "response": "def _get_mappings(self, doc_type):\n \"\"\"\n Interfaces with the elasticsearch mappings for the index\n prevents multiple loading of the same mappings from ES when called more than once\n\n Mappings format in elasticsearch is as follows:\n {\n \"doc_type\": {\n \"properties\": {\n \"nested_property\": {\n \"properties\": {\n \"an_analysed_property\": {\n \"type\": \"string\"\n },\n \"another_analysed_property\": {\n \"type\": \"string\"\n }\n }\n },\n \"a_not_analysed_property\": {\n \"type\": \"string\",\n \"index\": \"not_analyzed\"\n },\n \"a_date_property\": {\n \"type\": \"date\"\n }\n }\n }\n }\n\n We cache the properties of each doc_type, if they are not available, we'll load them again from Elasticsearch\n \"\"\"\n # Try loading the mapping from the cache.\n mapping = ElasticSearchEngine.get_mappings(self.index_name, doc_type)\n\n # Fall back to Elasticsearch\n if not mapping:\n mapping = self._es.indices.get_mapping(\n index=self.index_name,\n doc_type=doc_type,\n ).get(self.index_name, {}).get('mappings', {}).get(doc_type, {})\n\n # Cache the mapping, if one was retrieved\n if mapping:\n ElasticSearchEngine.set_mappings(\n self.index_name,\n doc_type,\n mapping\n )\n\n return mapping"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncheck the mappings for the content of the current document type and body.", "response": "def _check_mappings(self, doc_type, body):\n \"\"\"\n We desire to index content so that anything we want to be textually searchable(and therefore needing to be\n analysed), but the other fields are designed to be filters, and only require an exact match. So, we want to\n set up the mappings for these fields as \"not_analyzed\" - this will allow our filters to work faster because\n they only have to work off exact matches\n \"\"\"\n\n # Make fields other than content be indexed as unanalyzed terms - content\n # contains fields that are to be analyzed\n exclude_fields = [\"content\"]\n field_properties = getattr(settings, \"ELASTIC_FIELD_MAPPINGS\", {})\n\n def field_property(field_name, field_value):\n \"\"\"\n Prepares field as property syntax for providing correct mapping desired for field\n\n Mappings format in elasticsearch is as follows:\n {\n \"doc_type\": {\n \"properties\": {\n \"nested_property\": {\n \"properties\": {\n \"an_analysed_property\": {\n \"type\": \"string\"\n },\n \"another_analysed_property\": {\n \"type\": \"string\"\n }\n }\n },\n \"a_not_analysed_property\": {\n \"type\": \"string\",\n \"index\": \"not_analyzed\"\n },\n \"a_date_property\": {\n \"type\": \"date\"\n }\n }\n }\n }\n\n We can only add new ones, but the format is the same\n \"\"\"\n prop_val = None\n if field_name in field_properties:\n prop_val = field_properties[field_name]\n elif isinstance(field_value, dict):\n props = {fn: field_property(fn, field_value[fn]) for fn in field_value}\n prop_val = {\"properties\": props}\n else:\n prop_val = {\n \"type\": \"string\",\n \"index\": \"not_analyzed\",\n }\n\n return prop_val\n\n new_properties = {\n field: field_property(field, value)\n for field, value in body.items()\n if (field not in exclude_fields) and (field not in self._get_mappings(doc_type).get('properties', {}))\n }\n\n if new_properties:\n self._es.indices.put_mapping(\n index=self.index_name,\n doc_type=doc_type,\n body={\n doc_type: {\n \"properties\": new_properties,\n }\n }\n )\n self._clear_mapping(doc_type)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nimplement call to index the specified document type and list of sources", "response": "def index(self, doc_type, sources, **kwargs):\n \"\"\"\n Implements call to add documents to the ES index\n Note the call to _check_mappings which will setup fields with the desired mappings\n \"\"\"\n\n try:\n actions = []\n for source in sources:\n self._check_mappings(doc_type, source)\n id_ = source['id'] if 'id' in source else None\n log.debug(\"indexing %s object with id %s\", doc_type, id_)\n action = {\n \"_index\": self.index_name,\n \"_type\": doc_type,\n \"_id\": id_,\n \"_source\": source\n }\n actions.append(action)\n # bulk() returns a tuple with summary information\n # number of successfully executed actions and number of errors if stats_only is set to True.\n _, indexing_errors = bulk(\n self._es,\n actions,\n **kwargs\n )\n if indexing_errors:\n ElasticSearchEngine.log_indexing_error(indexing_errors)\n # Broad exception handler to protect around bulk call\n except Exception as ex:\n # log information and re-raise\n log.exception(\"error while indexing - %s\", str(ex))\n raise"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nimplement call to remove the documents from the index", "response": "def remove(self, doc_type, doc_ids, **kwargs):\n \"\"\" Implements call to remove the documents from the index \"\"\"\n\n try:\n # ignore is flagged as an unexpected-keyword-arg; ES python client documents that it can be used\n # pylint: disable=unexpected-keyword-arg\n actions = []\n for doc_id in doc_ids:\n log.debug(\"Removing document of type %s and index %s\", doc_type, doc_id)\n action = {\n '_op_type': 'delete',\n \"_index\": self.index_name,\n \"_type\": doc_type,\n \"_id\": doc_id\n }\n actions.append(action)\n bulk(self._es, actions, **kwargs)\n except BulkIndexError as ex:\n valid_errors = [error for error in ex.errors if error['delete']['status'] != 404]\n\n if valid_errors:\n log.exception(\"An error occurred while removing documents from the index.\")\n raise"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nimplementing the search method for the desired content of the a search.", "response": "def search(self,\n query_string=None,\n field_dictionary=None,\n filter_dictionary=None,\n exclude_dictionary=None,\n facet_terms=None,\n exclude_ids=None,\n use_field_match=False,\n **kwargs): # pylint: disable=too-many-arguments, too-many-locals, too-many-branches, arguments-differ\n \"\"\"\n Implements call to search the index for the desired content.\n\n Args:\n query_string (str): the string of values upon which to search within the\n content of the objects within the index\n\n field_dictionary (dict): dictionary of values which _must_ exist and\n _must_ match in order for the documents to be included in the results\n\n filter_dictionary (dict): dictionary of values which _must_ match if the\n field exists in order for the documents to be included in the results;\n documents for which the field does not exist may be included in the\n results if they are not otherwise filtered out\n\n exclude_dictionary(dict): dictionary of values all of which which must\n not match in order for the documents to be included in the results;\n documents which have any of these fields and for which the value matches\n one of the specified values shall be filtered out of the result set\n\n facet_terms (dict): dictionary of terms to include within search\n facets list - key is the term desired to facet upon, and the value is a\n dictionary of extended information to include. Supported right now is a\n size specification for a cap upon how many facet results to return (can\n be an empty dictionary to use default size for underlying engine):\n\n e.g.\n {\n \"org\": {\"size\": 10}, # only show top 10 organizations\n \"modes\": {}\n }\n\n use_field_match (bool): flag to indicate whether to use elastic\n filtering or elastic matching for field matches - this is nothing but a\n potential performance tune for certain queries\n\n (deprecated) exclude_ids (list): list of id values to exclude from the results -\n useful for finding maches that aren't \"one of these\"\n\n Returns:\n dict object with results in the desired format\n {\n \"took\": 3,\n \"total\": 4,\n \"max_score\": 2.0123,\n \"results\": [\n {\n \"score\": 2.0123,\n \"data\": {\n ...\n }\n },\n {\n \"score\": 0.0983,\n \"data\": {\n ...\n }\n }\n ],\n \"facets\": {\n \"org\": {\n \"total\": total_count,\n \"other\": 1,\n \"terms\": {\n \"MITx\": 25,\n \"HarvardX\": 18\n }\n },\n \"modes\": {\n \"total\": modes_count,\n \"other\": 15,\n \"terms\": {\n \"honor\": 58,\n \"verified\": 44,\n }\n }\n }\n }\n\n Raises:\n ElasticsearchException when there is a problem with the response from elasticsearch\n\n Example usage:\n .search(\n \"find the words within this string\",\n {\n \"must_have_field\": \"mast_have_value for must_have_field\"\n },\n {\n\n }\n )\n \"\"\"\n\n log.debug(\"searching index with %s\", query_string)\n\n elastic_queries = []\n elastic_filters = []\n\n # We have a query string, search all fields for matching text within the \"content\" node\n if query_string:\n if six.PY2:\n query_string = query_string.encode('utf-8').translate(None, RESERVED_CHARACTERS)\n else:\n query_string = query_string.translate(query_string.maketrans('', '', RESERVED_CHARACTERS))\n elastic_queries.append({\n \"query_string\": {\n \"fields\": [\"content.*\"],\n \"query\": query_string\n }\n })\n\n if field_dictionary:\n if use_field_match:\n elastic_queries.extend(_process_field_queries(field_dictionary))\n else:\n elastic_filters.extend(_process_field_filters(field_dictionary))\n\n if filter_dictionary:\n elastic_filters.extend(_process_filters(filter_dictionary))\n\n # Support deprecated argument of exclude_ids\n if exclude_ids:\n if not exclude_dictionary:\n exclude_dictionary = {}\n if \"_id\" not in exclude_dictionary:\n exclude_dictionary[\"_id\"] = []\n exclude_dictionary[\"_id\"].extend(exclude_ids)\n\n if exclude_dictionary:\n elastic_filters.append(_process_exclude_dictionary(exclude_dictionary))\n\n query_segment = {\n \"match_all\": {}\n }\n if elastic_queries:\n query_segment = {\n \"bool\": {\n \"must\": elastic_queries\n }\n }\n\n query = query_segment\n if elastic_filters:\n filter_segment = {\n \"bool\": {\n \"must\": elastic_filters\n }\n }\n query = {\n \"filtered\": {\n \"query\": query_segment,\n \"filter\": filter_segment,\n }\n }\n\n body = {\"query\": query}\n if facet_terms:\n facet_query = _process_facet_terms(facet_terms)\n if facet_query:\n body[\"facets\"] = facet_query\n\n try:\n es_response = self._es.search(\n index=self.index_name,\n body=body,\n **kwargs\n )\n except exceptions.ElasticsearchException as ex:\n message = six.text_type(ex)\n if 'QueryParsingException' in message:\n log.exception(\"Malformed search query: %s\", message)\n raise QueryParseError('Malformed search query.')\n else:\n # log information and re-raise\n log.exception(\"error while searching index - %s\", str(message))\n raise\n\n return _translate_hits(es_response)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_search_engine(index=None):\n search_engine_class = _load_class(getattr(settings, \"SEARCH_ENGINE\", None), None)\n return search_engine_class(index=index) if search_engine_class else None", "response": "Returns the search engine class defined in settings. SEARCH_ENGINE or None if no search engine is defined."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef perform_search(\n search_term,\n user=None,\n size=10,\n from_=0,\n course_id=None):\n \"\"\" Call the search engine with the appropriate parameters \"\"\"\n # field_, filter_ and exclude_dictionary(s) can be overridden by calling application\n # field_dictionary includes course if course_id provided\n (field_dictionary, filter_dictionary, exclude_dictionary) = SearchFilterGenerator.generate_field_filters(\n user=user,\n course_id=course_id\n )\n\n searcher = SearchEngine.get_search_engine(getattr(settings, \"COURSEWARE_INDEX_NAME\", \"courseware_index\"))\n if not searcher:\n raise NoSearchEngineError(\"No search engine specified in settings.SEARCH_ENGINE\")\n\n results = searcher.search_string(\n search_term,\n field_dictionary=field_dictionary,\n filter_dictionary=filter_dictionary,\n exclude_dictionary=exclude_dictionary,\n size=size,\n from_=from_,\n doc_type=\"courseware_content\",\n )\n\n # post-process the result\n for result in results[\"results\"]:\n result[\"data\"] = SearchResultProcessor.process_result(result[\"data\"], search_term, user)\n\n results[\"access_denied_count\"] = len([r for r in results[\"results\"] if r[\"data\"] is None])\n results[\"results\"] = [r for r in results[\"results\"] if r[\"data\"] is not None]\n\n return results", "response": "Perform a search on the specified term in the specified base language."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nsearches the course - discovery activities against the search engine index of course details", "response": "def course_discovery_search(search_term=None, size=20, from_=0, field_dictionary=None):\n \"\"\"\n Course Discovery activities against the search engine index of course details\n \"\"\"\n # We'll ignore the course-enrollemnt informaiton in field and filter\n # dictionary, and use our own logic upon enrollment dates for these\n use_search_fields = [\"org\"]\n (search_fields, _, exclude_dictionary) = SearchFilterGenerator.generate_field_filters()\n use_field_dictionary = {}\n use_field_dictionary.update({field: search_fields[field] for field in search_fields if field in use_search_fields})\n if field_dictionary:\n use_field_dictionary.update(field_dictionary)\n if not getattr(settings, \"SEARCH_SKIP_ENROLLMENT_START_DATE_FILTERING\", False):\n use_field_dictionary[\"enrollment_start\"] = DateRange(None, datetime.utcnow())\n\n searcher = SearchEngine.get_search_engine(getattr(settings, \"COURSEWARE_INDEX_NAME\", \"courseware_index\"))\n if not searcher:\n raise NoSearchEngineError(\"No search engine specified in settings.SEARCH_ENGINE\")\n\n results = searcher.search(\n query_string=search_term,\n doc_type=\"course_info\",\n size=size,\n from_=from_,\n # only show when enrollment start IS provided and is before now\n field_dictionary=use_field_dictionary,\n # show if no enrollment end is provided and has not yet been reached\n filter_dictionary={\"enrollment_end\": DateRange(datetime.utcnow(), None)},\n exclude_dictionary=exclude_dictionary,\n facet_terms=course_discovery_facets(),\n )\n\n return results"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef strings_in_dictionary(dictionary):\n strings = [value for value in six.itervalues(dictionary) if not isinstance(value, dict)]\n for child_dict in [dv for dv in six.itervalues(dictionary) if isinstance(dv, dict)]:\n strings.extend(SearchResultProcessor.strings_in_dictionary(child_dict))\n return strings", "response": "Used by default implementation for finding excerpt"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nfinding matches in the list of strings and words.", "response": "def find_matches(strings, words, length_hoped):\n \"\"\" Used by default property excerpt \"\"\"\n lower_words = [w.lower() for w in words]\n\n def has_match(string):\n \"\"\" Do any of the words match within the string \"\"\"\n lower_string = string.lower()\n for test_word in lower_words:\n if test_word in lower_string:\n return True\n return False\n\n shortened_strings = [textwrap.wrap(s) for s in strings]\n short_string_list = list(chain.from_iterable(shortened_strings))\n matches = [ms for ms in short_string_list if has_match(ms)]\n\n cumulative_len = 0\n break_at = None\n for idx, match in enumerate(matches):\n cumulative_len += len(match)\n if cumulative_len >= length_hoped:\n break_at = idx\n break\n\n return matches[0:break_at]"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef decorate_matches(match_in, match_word):\n matches = re.finditer(match_word, match_in, re.IGNORECASE)\n for matched_string in set([match.group() for match in matches]):\n match_in = match_in.replace(\n matched_string,\n getattr(settings, \"SEARCH_MATCH_DECORATION\", u\"{}\").format(matched_string)\n )\n return match_in", "response": "decorate the matches within the excerpt"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nadd properties defined in the result to the _results_fields dictionary", "response": "def add_properties(self):\n \"\"\"\n Called during post processing of result\n Any properties defined in your subclass will get exposed as members of the result json from the search\n \"\"\"\n for property_name in [p[0] for p in inspect.getmembers(self.__class__) if isinstance(p[1], property)]:\n self._results_fields[property_name] = getattr(self, property_name, None)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef process_result(cls, dictionary, match_phrase, user):\n result_processor = _load_class(getattr(settings, \"SEARCH_RESULT_PROCESSOR\", None), cls)\n srp = result_processor(dictionary, match_phrase)\n if srp.should_remove(user):\n return None\n try:\n srp.add_properties()\n # protect around any problems introduced by subclasses within their properties\n except Exception as ex: # pylint: disable=broad-except\n log.exception(\"error processing properties for %s - %s: will remove from results\",\n json.dumps(dictionary, cls=DjangoJSONEncoder), str(ex))\n return None\n return dictionary", "response": "Called from within search handler. Finds desired subclass and decides if the result should be removed and adds properties derived from the result information\n "} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef excerpt(self):\n if \"content\" not in self._results_fields:\n return None\n\n match_phrases = [self._match_phrase]\n if six.PY2:\n separate_phrases = [\n phrase.decode('utf-8')\n for phrase in shlex.split(self._match_phrase.encode('utf-8'))\n ]\n else:\n separate_phrases = [\n phrase\n for phrase in shlex.split(self._match_phrase)\n ]\n if len(separate_phrases) > 1:\n match_phrases.extend(separate_phrases)\n else:\n match_phrases = separate_phrases\n\n matches = SearchResultProcessor.find_matches(\n SearchResultProcessor.strings_in_dictionary(self._results_fields[\"content\"]),\n match_phrases,\n DESIRED_EXCERPT_LENGTH\n )\n excerpt_text = ELLIPSIS.join(matches)\n\n for match_word in match_phrases:\n excerpt_text = SearchResultProcessor.decorate_matches(excerpt_text, match_word)\n\n return excerpt_text", "response": "Return a useful excerpt representing the matches within the results of the assessment."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef generate_field_filters(cls, **kwargs):\n generator = _load_class(getattr(settings, \"SEARCH_FILTER_GENERATOR\", None), cls)()\n return (\n generator.field_dictionary(**kwargs),\n generator.filter_dictionary(**kwargs),\n generator.exclude_dictionary(**kwargs),\n )", "response": "Returns a list of field filters for the specified class."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef set_search_enviroment(cls, **kwargs):\n initializer = _load_class(getattr(settings, \"SEARCH_INITIALIZER\", None), cls)()\n return initializer.initialize(**kwargs)", "response": "Called from within search handler\n Finds desired subclass and calls initialize method\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _parse(self, filename):\n self.names = {}\n with codecs.open(filename, encoding=\"iso8859-1\") as f:\n for line in f:\n if any(map(lambda c: 128 < ord(c) < 160, line)):\n line = line.encode(\"iso8859-1\").decode(\"windows-1252\")\n self._eat_name_line(line.strip())", "response": "Parses the data file and sets the names attribute"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _eat_name_line(self, line):\n if line[0] not in \"#=\":\n parts = line.split()\n country_values = line[30:-1]\n name = map_name(parts[1])\n if not self.case_sensitive:\n name = name.lower()\n\n if parts[0] == \"M\":\n self._set(name, u\"male\", country_values)\n elif parts[0] == \"1M\" or parts[0] == \"?M\":\n self._set(name, u\"mostly_male\", country_values)\n elif parts[0] == \"F\":\n self._set(name, u\"female\", country_values)\n elif parts[0] == \"1F\" or parts[0] == \"?F\":\n self._set(name, u\"mostly_female\", country_values)\n elif parts[0] == \"?\":\n self._set(name, self.unknown_value, country_values)\n else:\n raise \"Not sure what to do with a sex of %s\" % parts[0]", "response": "Parses one line of data file and sets the name of the user."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _set(self, name, gender, country_values):\n if '+' in name:\n for replacement in ['', ' ', '-']:\n self._set(name.replace('+', replacement), gender, country_values)\n else:\n if name not in self.names:\n self.names[name] = {}\n self.names[name][gender] = country_values", "response": "Sets gender and relevant country values for names dictionary of detector"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nfind the most popular gender for the given name counting by given counter", "response": "def _most_popular_gender(self, name, counter):\n \"\"\"Finds the most popular gender for the given name counting by given counter\"\"\"\n if name not in self.names:\n return self.unknown_value\n\n max_count, max_tie = (0, 0)\n best = self.names[name].keys()[0]\n for gender, country_values in self.names[name].items():\n count, tie = counter(country_values)\n if count > max_count or (count == max_count and tie > max_tie):\n max_count, max_tie, best = count, tie, gender\n\n return best if max_count > 0 else self.unknown_value"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns the best gender for the given name and country pair", "response": "def get_gender(self, name, country=None):\n \"\"\"Returns best gender for the given name and country pair\"\"\"\n if not self.case_sensitive:\n name = name.lower()\n\n if name not in self.names:\n return self.unknown_value\n elif not country:\n def counter(country_values):\n country_values = map(ord, country_values.replace(\" \", \"\"))\n return (len(country_values),\n sum(map(lambda c: c > 64 and c-55 or c-48, country_values)))\n return self._most_popular_gender(name, counter)\n elif country in self.__class__.COUNTRIES:\n index = self.__class__.COUNTRIES.index(country)\n counter = lambda e: (ord(e[index])-32, 0)\n return self._most_popular_gender(name, counter)\n else:\n raise NoCountryError(\"No such country: %s\" % country)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nwrites the specified string to the output file.", "response": "def output(self, msg, newline=True):\n \"\"\"\n Writes the specified string to the output target of the report.\n\n :param msg: the message to output.\n :type msg: str\n :param newline:\n whether or not to append a newline to the end of the message\n :type newline: str\n \"\"\"\n\n click.echo(text_type(msg), nl=newline, file=self.output_file)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nexecute the tools on the TidyPy project and returns the issues that are found.", "response": "def execute_tools(config, path, progress=None):\n \"\"\"\n Executes the suite of TidyPy tools upon the project and returns the\n issues that are found.\n\n :param config: the TidyPy configuration to use\n :type config: dict\n :param path: that path to the project to analyze\n :type path: str\n :param progress:\n the progress reporter object that will receive callbacks during the\n execution of the tool suite. If not specified, not progress\n notifications will occur.\n :type progress: tidypy.Progress\n :rtype: tidypy.Collector\n \"\"\"\n\n progress = progress or QuietProgress()\n progress.on_start()\n\n manager = SyncManager()\n manager.start()\n\n num_tools = 0\n tools = manager.Queue()\n for name, cls in iteritems(get_tools()):\n if config[name]['use'] and cls.can_be_used():\n num_tools += 1\n tools.put({\n 'name': name,\n 'config': config[name],\n })\n\n collector = Collector(config)\n if not num_tools:\n progress.on_finish()\n return collector\n\n notifications = manager.Queue()\n environment = manager.dict({\n 'finder': Finder(path, config),\n })\n\n workers = []\n for _ in range(config['workers']):\n worker = Worker(\n args=(\n tools,\n notifications,\n environment,\n ),\n )\n worker.start()\n workers.append(worker)\n\n while num_tools:\n try:\n notification = notifications.get(True, 0.25)\n except Empty:\n pass\n else:\n if notification['type'] == 'start':\n progress.on_tool_start(notification['tool'])\n elif notification['type'] == 'complete':\n collector.add_issues(notification['issues'])\n progress.on_tool_finish(notification['tool'])\n num_tools -= 1\n\n progress.on_finish()\n\n return collector"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef execute_reports(\n config,\n path,\n collector,\n on_report_finish=None,\n output_file=None):\n \"\"\"\n Executes the configured suite of issue reports.\n\n :param config: the TidyPy configuration to use\n :type config: dict\n :param path: that path to the project that was analyzed\n :type path: str\n :param collector: the issues to report\n :type collector: tidypy.Collector\n \"\"\"\n\n reports = get_reports()\n for report in config.get('requested_reports', []):\n if report.get('type') and report['type'] in reports:\n cfg = config.get('report', {}).get(report['type'], {})\n cfg.update(report)\n reporter = reports[report['type']](\n cfg,\n path,\n output_file=output_file,\n )\n reporter.produce(collector)\n if on_report_finish:\n on_report_finish(report)", "response": "Executes the configured suite of issue reports."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ndetermining whether or not the specified file is excluded by the base path.", "response": "def is_excluded(self, path):\n \"\"\"\n Determines whether or not the specified file is excluded by the\n project's configuration.\n\n :param path: the path to check\n :type path: pathlib.Path\n :rtype: bool\n \"\"\"\n\n relpath = path.relative_to(self.base_path).as_posix()\n return matches_masks(relpath, self.excludes)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef is_excluded_dir(self, path):\n\n if self.is_excluded(path):\n return True\n return matches_masks(path.name, ALWAYS_EXCLUDED_DIRS)", "response": "Determines whether or not the specified directory is excluded by the current project s configuration."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef directories(self, filters=None, containing=None):\n\n filters = compile_masks(filters or [r'.*'])\n contains = compile_masks(containing)\n\n for dirname, files in iteritems(self._found):\n relpath = text_type(Path(dirname).relative_to(self.base_path))\n if matches_masks(relpath, filters):\n if not contains or self._contains(files, contains):\n yield dirname", "response": "Generates a generator that yields the paths to directories in the specified project that match the specified regular expressions."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef sys_paths(self, filters=None):\n\n paths = set()\n\n packages = list(self.packages(filters=filters))\n\n for module in self.modules(filters=filters):\n parent = text_type(Path(module).parent)\n if parent not in packages:\n paths.add(parent)\n\n paths.update(self.topmost_directories([\n text_type(Path(package).parent)\n for package in packages\n ]))\n\n return list(paths)", "response": "Returns a list of paths that would be suitable to use in sys. path for the Python modules and packages found in this project."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef add_issues(self, issues):\n\n if not isinstance(issues, (list, tuple)):\n issues = [issues]\n with self._lock:\n self._all_issues.extend(issues)\n self._cleaned_issues = None", "response": "Adds an issue to the collection."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef issue_count(self, include_unclean=False):\n\n if include_unclean:\n return len(self._all_issues)\n self._ensure_cleaned_issues()\n return len(self._cleaned_issues)", "response": "Returns the number of issues in the collection."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_issues(self, sortby=None):\n\n self._ensure_cleaned_issues()\n return self._sort_issues(self._cleaned_issues, sortby)", "response": "Returns the issues in the collection."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_grouped_issues(self, keyfunc=None, sortby=None):\n\n if not keyfunc:\n keyfunc = default_group\n if not sortby:\n sortby = self.DEFAULT_SORT\n self._ensure_cleaned_issues()\n return self._group_issues(self._cleaned_issues, keyfunc, sortby)", "response": "Returns the issues in the collection grouped by the keyfunc."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nparse a TidyPy configuration from a TOML - serialized TOML string containing a TidyPy configuration.", "response": "def parse(cls, content, is_pyproject=False):\n \"\"\"\n A convenience method for parsing a TOML-serialized configuration.\n\n :param content: a TOML string containing a TidyPy configuration\n :type content: str\n :param is_pyproject:\n whether or not the content is (or resembles) a ``pyproject.toml``\n file, where the TidyPy configuration is located within a key named\n ``tool``.\n :type is_pyproject: bool\n :rtype: dict\n \"\"\"\n\n parsed = pytoml.loads(content)\n\n if is_pyproject:\n parsed = parsed.get('tool', {})\n parsed = parsed.get('tidypy', {})\n\n return parsed"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nretrieve the TidyPy tools that are available in the current Python environment.", "response": "def get_tools():\n \"\"\"\n Retrieves the TidyPy tools that are available in the current Python\n environment.\n\n The returned dictionary has keys that are the tool names and values are the\n tool classes.\n\n :rtype: dict\n \"\"\"\n\n # pylint: disable=protected-access\n\n if not hasattr(get_tools, '_CACHE'):\n get_tools._CACHE = dict()\n for entry in pkg_resources.iter_entry_points('tidypy.tools'):\n try:\n get_tools._CACHE[entry.name] = entry.load()\n except ImportError as exc: # pragma: no cover\n output_error(\n 'Could not load tool \"%s\" defined by \"%s\": %s' % (\n entry,\n entry.dist,\n exc,\n ),\n )\n return get_tools._CACHE"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_reports():\n\n # pylint: disable=protected-access\n\n if not hasattr(get_reports, '_CACHE'):\n get_reports._CACHE = dict()\n for entry in pkg_resources.iter_entry_points('tidypy.reports'):\n try:\n get_reports._CACHE[entry.name] = entry.load()\n except ImportError as exc: # pragma: no cover\n output_error(\n 'Could not load report \"%s\" defined by \"%s\": %s' % (\n entry,\n entry.dist,\n exc,\n ),\n )\n return get_reports._CACHE", "response": "Returns the TidyPy issue reports that are available in the current Python\n environment."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nretrieving the TidyPy configuration extenders that are available in the current Python environment.", "response": "def get_extenders():\n \"\"\"\n Retrieves the TidyPy configuration extenders that are available in the\n current Python environment.\n\n The returned dictionary has keys are the extender names and values are the\n extender classes.\n\n :rtype: dict\n \"\"\"\n\n # pylint: disable=protected-access\n\n if not hasattr(get_extenders, '_CACHE'):\n get_extenders._CACHE = dict()\n for entry in pkg_resources.iter_entry_points('tidypy.extenders'):\n try:\n get_extenders._CACHE[entry.name] = entry.load()\n except ImportError as exc: # pragma: no cover\n output_error(\n 'Could not load extender \"%s\" defined by \"%s\": %s' % (\n entry,\n entry.dist,\n exc,\n ),\n )\n return get_extenders._CACHE"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef purge_config_cache(location=None):\n\n cache_path = get_cache_path(location)\n\n if location:\n os.remove(cache_path)\n else:\n shutil.rmtree(cache_path)", "response": "Clears out the cache of TidyPy configurations that were retrieved from the specified location."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning a dict containing the default configuration for the TidyPy Tidy.", "response": "def get_default_config():\n \"\"\"\n Produces a stock/out-of-the-box TidyPy configuration.\n\n :rtype: dict\n \"\"\"\n\n config = {}\n\n for name, cls in iteritems(get_tools()):\n config[name] = cls.get_default_config()\n\n try:\n workers = multiprocessing.cpu_count() - 1\n except NotImplementedError: # pragma: no cover\n workers = 1\n workers = max(1, min(4, workers))\n\n config.update({\n 'exclude': [],\n 'merge-issues': True,\n 'workers': workers,\n 'requested_reports': [\n {\n 'type': 'console',\n },\n ],\n 'disabled': [],\n 'noqa': True,\n 'extends': [],\n 'ignore-missing-extends': False,\n })\n\n return config"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_user_config(project_path, use_cache=True):\n\n if sys.platform == 'win32':\n user_config = os.path.expanduser(r'~\\\\tidypy')\n else:\n user_config = os.path.join(\n os.getenv('XDG_CONFIG_HOME') or os.path.expanduser('~/.config'),\n 'tidypy'\n )\n\n if os.path.exists(user_config):\n with open(user_config, 'r') as config_file:\n config = pytoml.load(config_file).get('tidypy', {})\n\n config = merge_dict(get_default_config(), config)\n config = process_extensions(config, project_path, use_cache=use_cache)\n return config\n\n return None", "response": "Returns a TidyPy configuration that incorporates the configuration files of the current user."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_local_config(project_path, use_cache=True):\n\n pyproject_path = os.path.join(project_path, 'pyproject.toml')\n\n if os.path.exists(pyproject_path):\n with open(pyproject_path, 'r') as config_file:\n config = pytoml.load(config_file)\n\n config = config.get('tool', {}).get('tidypy', {})\n config = merge_dict(get_default_config(), config)\n config = process_extensions(config, project_path, use_cache=use_cache)\n return config\n\n return None", "response": "Returns a TidyPy configuration for the specified project."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning the Tidypy configuration for the specified project.", "response": "def get_project_config(project_path, use_cache=True):\n \"\"\"\n Produces the Tidypy configuration to use for the specified project.\n\n If a ``pyproject.toml`` exists, the configuration will be based on that. If\n not, the TidyPy configuration in the user's home directory will be used. If\n one does not exist, the default configuration will be used.\n\n :param project_path: the path to the project that is going to be analyzed\n :type project_path: str\n :param use_cache:\n whether or not to use cached versions of any remote/referenced TidyPy\n configurations. If not specified, defaults to ``True``.\n :type use_cache: bool\n :rtype: dict\n \"\"\"\n\n return get_local_config(project_path, use_cache=use_cache) \\\n or get_user_config(project_path, use_cache=use_cache) \\\n or get_default_config()"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning the contents of two lists into a new list.", "response": "def merge_list(list1, list2):\n \"\"\"\n Merges the contents of two lists into a new list.\n\n :param list1: the first list\n :type list1: list\n :param list2: the second list\n :type list2: list\n :returns: list\n \"\"\"\n\n merged = list(list1)\n\n for value in list2:\n if value not in merged:\n merged.append(value)\n\n return merged"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nprinting the specified string to stderr.", "response": "def output_error(msg):\n \"\"\"\n Prints the specified string to ``stderr``.\n\n :param msg: the message to print\n :type msg: str\n \"\"\"\n\n click.echo(click.style(msg, fg='red'), err=True)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef mod_sys_path(paths):\n\n old_path = sys.path\n sys.path = paths + sys.path\n try:\n yield\n finally:\n sys.path = old_path", "response": "A context manager that adds the specified paths to the sys. path during the execution of the block."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef compile_masks(masks):\n\n if not masks:\n masks = []\n elif not isinstance(masks, (list, tuple)):\n masks = [masks]\n\n return [\n re.compile(mask)\n for mask in masks\n ]", "response": "Returns a list of regular expressions that match the given list of regular expressions."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef matches_masks(target, masks):\n\n for mask in masks:\n if mask.search(target):\n return True\n return False", "response": "Determines whether or not the target string matches any of the regular expressions specified."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef read_file(filepath):\n\n with _FILE_CACHE_LOCK:\n if filepath not in _FILE_CACHE:\n _FILE_CACHE[filepath] = _read_file(filepath)\n return _FILE_CACHE[filepath]", "response": "Reads the contents of the specified file."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nparsing the specified file and returns the AST of the file.", "response": "def parse_python_file(filepath):\n \"\"\"\n Retrieves the AST of the specified file.\n\n This function performs simple caching so that the same file isn't read or\n parsed more than once per process.\n\n :param filepath: the file to parse\n :type filepath: str\n :returns: ast.AST\n \"\"\"\n\n with _AST_CACHE_LOCK:\n if filepath not in _AST_CACHE:\n source = read_file(filepath)\n _AST_CACHE[filepath] = ast.parse(source, filename=filepath)\n return _AST_CACHE[filepath]"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncall when an individual tool completes execution.", "response": "def on_tool_finish(self, tool):\n \"\"\"\n Called when an individual tool completes execution.\n\n :param tool: the name of the tool that completed\n :type tool: str\n \"\"\"\n\n with self._lock:\n if tool in self.current_tools:\n self.current_tools.remove(tool)\n self.completed_tools.append(tool)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nexecute an x3270 command and return a Command object.", "response": "def exec_command(self, cmdstr):\n \"\"\"\n Execute an x3270 command\n\n `cmdstr` gets sent directly to the x3270 subprocess on it's stdin.\n \"\"\"\n if self.is_terminated:\n raise TerminatedError(\"this TerminalClient instance has been terminated\")\n\n log.debug(\"sending command: %s\", cmdstr)\n c = Command(self.app, cmdstr)\n start = time.time()\n c.execute()\n elapsed = time.time() - start\n log.debug(\"elapsed execution: {0}\".format(elapsed))\n self.status = Status(c.status_line)\n\n return c"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nterminate the underlying x3270 subprocess.", "response": "def terminate(self):\n \"\"\"\n terminates the underlying x3270 subprocess. Once called, this\n Emulator instance must no longer be used.\n \"\"\"\n if not self.is_terminated:\n log.debug(\"terminal client terminated\")\n try:\n self.exec_command(b\"Quit\")\n except BrokenPipeError: # noqa\n # x3270 was terminated, since we are just quitting anyway, ignore it.\n pass\n except socket.error as e:\n if e.errno != errno.ECONNRESET:\n raise\n # this can happen because wc3270 closes the socket before\n # the read() can happen, causing a socket error\n\n self.app.close()\n\n self.is_terminated = True"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn bool indicating connection state", "response": "def is_connected(self):\n \"\"\"\n Return bool indicating connection state\n \"\"\"\n # need to wrap in try/except b/c of wc3270's socket connection dynamics\n try:\n # this is basically a no-op, but it results in the the current status\n # getting updated\n self.exec_command(b\"Query(ConnectionState)\")\n\n # connected status is like 'C(192.168.1.1)', disconnected is 'N'\n return self.status.connection_state.startswith(b\"C(\")\n except NotConnectedException:\n return False"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef connect(self, host):\n if not self.app.connect(host):\n command = \"Connect({0})\".format(host).encode(\"ascii\")\n self.exec_command(command)\n self.last_host = host", "response": "Connect to a host"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef wait_for_field(self):\n self.exec_command(\"Wait({0}, InputField)\".format(self.timeout).encode(\"ascii\"))\n if self.status.keyboard != b\"U\":\n raise KeyboardStateError(\n \"keyboard not unlocked, state was: {0}\".format(\n self.status.keyboard.decode(\"ascii\")\n )\n )", "response": "Wait until the keyboard is ready and the cursor is positioned on a modifiable field."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nmove the cursor to the given co -ordinates.", "response": "def move_to(self, ypos, xpos):\n \"\"\"\n move the cursor to the given co-ordinates. Co-ordinates are 1\n based, as listed in the status area of the terminal.\n \"\"\"\n # the screen's co-ordinates are 1 based, but the command is 0 based\n xpos -= 1\n ypos -= 1\n self.exec_command(\"MoveCursor({0}, {1})\".format(ypos, xpos).encode(\"ascii\"))"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nsend a string to the current screen.", "response": "def send_string(self, tosend, ypos=None, xpos=None):\n \"\"\"\n Send a string to the screen at the current cursor location or at\n screen co-ordinates `ypos`/`xpos` if they are both given.\n\n Co-ordinates are 1 based, as listed in the status area of the\n terminal.\n \"\"\"\n if xpos is not None and ypos is not None:\n self.move_to(ypos, xpos)\n\n # escape double quotes in the data to send\n tosend = tosend.replace('\"', '\"')\n\n self.exec_command('String(\"{0}\")'.format(tosend).encode(\"ascii\"))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngetting a string at screen co -ordinates xpos ypos length at screen co -ordinates xpos ypos length at screen co -ordinates xpos ypos length at screen co -ordinates xpos length", "response": "def string_get(self, ypos, xpos, length):\n \"\"\"\n Get a string of `length` at screen co-ordinates `ypos`/`xpos`\n\n Co-ordinates are 1 based, as listed in the status area of the\n terminal.\n \"\"\"\n # the screen's co-ordinates are 1 based, but the command is 0 based\n xpos -= 1\n ypos -= 1\n cmd = self.exec_command(\n \"Ascii({0},{1},{2})\".format(ypos, xpos, length).encode(\"ascii\")\n )\n # this usage of ascii should only return a single line of data\n assert len(cmd.data) == 1, cmd.data\n return cmd.data[0].decode(\"ascii\")"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef string_found(self, ypos, xpos, string):\n found = self.string_get(ypos, xpos, len(string))\n log.debug('string_found() saw \"{0}\"'.format(found))\n return found == string", "response": "Return True if string is found at screen co -ordinates xpos ypos and ypos are 1 based."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef fill_field(self, ypos, xpos, tosend, length):\n if length < len(tosend):\n raise FieldTruncateError('length limit %d, but got \"%s\"' % (length, tosend))\n if xpos is not None and ypos is not None:\n self.move_to(ypos, xpos)\n self.delete_field()\n self.send_string(tosend)", "response": "Fill the field at the given position and moves the string to the given position."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef from_func(cls, func, variables, vartype, name=None):\n variables = tuple(variables)\n\n configurations = frozenset(config\n for config in itertools.product(vartype.value, repeat=len(variables))\n if func(*config))\n\n return cls(func, configurations, variables, vartype, name)", "response": "Construct a constraint from a validation function."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef from_configurations(cls, configurations, variables, vartype, name=None):\n def func(*args): return args in configurations\n\n return cls(func, configurations, variables, vartype, name)", "response": "Construct a constraint from a list of valid configurations."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nchecking that a solution satisfies the constraint.", "response": "def check(self, solution):\n \"\"\"Check that a solution satisfies the constraint.\n\n Args:\n solution (container):\n An assignment for the variables in the constraint.\n\n Returns:\n bool: True if the solution satisfies the constraint; otherwise False.\n\n Examples:\n This example creates a constraint that :math:`a \\\\ne b` on binary variables\n and tests it for two candidate solutions, with additional unconstrained\n variable c.\n\n >>> import dwavebinarycsp\n >>> const = dwavebinarycsp.Constraint.from_configurations([(0, 1), (1, 0)],\n ... ['a', 'b'], dwavebinarycsp.BINARY)\n >>> solution = {'a': 1, 'b': 1, 'c': 0}\n >>> const.check(solution)\n False\n >>> solution = {'a': 1, 'b': 0, 'c': 0}\n >>> const.check(solution)\n True\n\n \"\"\"\n return self.func(*(solution[v] for v in self.variables))"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef fix_variable(self, v, value):\n variables = self.variables\n try:\n idx = variables.index(v)\n except ValueError:\n raise ValueError(\"given variable {} is not part of the constraint\".format(v))\n\n if value not in self.vartype.value:\n raise ValueError(\"expected value to be in {}, received {} instead\".format(self.vartype.value, value))\n\n configurations = frozenset(config[:idx] + config[idx + 1:] # exclude the fixed var\n for config in self.configurations\n if config[idx] == value)\n\n if not configurations:\n raise UnsatError(\"fixing {} to {} makes this constraint unsatisfiable\".format(v, value))\n\n variables = variables[:idx] + variables[idx + 1:]\n\n self.configurations = configurations\n self.variables = variables\n\n def func(*args): return args in configurations\n self.func = func\n\n self.name = '{} ({} fixed to {})'.format(self.name, v, value)", "response": "Fix the value of a variable and remove it from the constraint."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nflip a variable in the constraint.", "response": "def flip_variable(self, v):\n \"\"\"Flip a variable in the constraint.\n\n Args:\n v (variable):\n Variable in the constraint to take the complementary value of its\n construction value.\n\n Examples:\n This example creates a constraint that :math:`a = b` on binary variables\n and flips variable a.\n\n >>> import dwavebinarycsp\n >>> const = dwavebinarycsp.Constraint.from_func(operator.eq,\n ... ['a', 'b'], dwavebinarycsp.BINARY)\n >>> const.check({'a': 0, 'b': 0})\n True\n >>> const.flip_variable('a')\n >>> const.check({'a': 1, 'b': 0})\n True\n >>> const.check({'a': 0, 'b': 0})\n False\n\n \"\"\"\n try:\n idx = self.variables.index(v)\n except ValueError:\n raise ValueError(\"variable {} is not a variable in constraint {}\".format(v, self.name))\n\n if self.vartype is dimod.BINARY:\n\n original_func = self.func\n\n def func(*args):\n new_args = list(args)\n new_args[idx] = 1 - new_args[idx] # negate v\n return original_func(*new_args)\n\n self.func = func\n\n self.configurations = frozenset(config[:idx] + (1 - config[idx],) + config[idx + 1:]\n for config in self.configurations)\n\n else: # SPIN\n\n original_func = self.func\n\n def func(*args):\n new_args = list(args)\n new_args[idx] = -new_args[idx] # negate v\n return original_func(*new_args)\n\n self.func = func\n\n self.configurations = frozenset(config[:idx] + (-config[idx],) + config[idx + 1:]\n for config in self.configurations)\n\n self.name = '{} ({} flipped)'.format(self.name, v)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncreate a copy of this object.", "response": "def copy(self):\n \"\"\"Create a copy.\n\n Examples:\n This example copies constraint :math:`a \\\\ne b` and tests a solution\n on the copied constraint.\n\n >>> import dwavebinarycsp\n >>> import operator\n >>> const = dwavebinarycsp.Constraint.from_func(operator.ne,\n ... ['a', 'b'], 'BINARY')\n >>> const2 = const.copy()\n >>> const2 is const\n False\n >>> const2.check({'a': 1, 'b': 1})\n False\n\n \"\"\"\n # each object is itself immutable (except the function)\n return self.__class__(self.func, self.configurations, self.variables, self.vartype, name=self.name)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncreating a new constraint that is the projection onto a subset of the variables.", "response": "def projection(self, variables):\n \"\"\"Create a new constraint that is the projection onto a subset of the variables.\n\n Args:\n variables (iterable):\n Subset of the constraint's variables.\n\n Returns:\n :obj:`.Constraint`: A new constraint over a subset of the variables.\n\n Examples:\n\n >>> import dwavebinarycsp\n ...\n >>> const = dwavebinarycsp.Constraint.from_configurations([(0, 0), (0, 1)],\n ... ['a', 'b'],\n ... dwavebinarycsp.BINARY)\n >>> proj = const.projection(['a'])\n >>> proj.variables\n ['a']\n >>> proj.configurations\n {(0,)}\n\n \"\"\"\n # resolve iterables or mutability problems by casting the variables to a set\n variables = set(variables)\n\n if not variables.issubset(self.variables):\n raise ValueError(\"Cannot project to variables not in the constraint.\")\n\n idxs = [i for i, v in enumerate(self.variables) if v in variables]\n\n configurations = frozenset(tuple(config[i] for i in idxs) for config in self.configurations)\n variables = tuple(self.variables[i] for i in idxs)\n\n return self.from_configurations(configurations, variables, self.vartype)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef xor_fault(a, b, out, fault):\n if (a != b) == out:\n return fault == 0\n else:\n return fault == 1", "response": "Returns True if a xor fault is 0 or 1."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn True if a and b have a fault.", "response": "def and_fault(a, b, out, fault):\n \"\"\"Returns True if AND(a, b) == out and fault == 0 or AND(a, b) != out and fault == 1.\"\"\"\n if (a and b) == out:\n return fault == 0\n else:\n return fault == 1"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef or_fault(a, b, out, fault):\n if (a or b) == out:\n return fault == 0\n else:\n return fault == 1", "response": "Returns True if a or b has a fault."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef assert_penaltymodel_factory_available():\n\n from pkg_resources import iter_entry_points\n from penaltymodel.core import FACTORY_ENTRYPOINT\n from itertools import chain\n\n supported = ('maxgap', 'mip')\n factories = chain(*(iter_entry_points(FACTORY_ENTRYPOINT, name) for name in supported))\n\n try:\n next(factories)\n except StopIteration:\n raise AssertionError(\n \"To use 'dwavebinarycsp', at least one penaltymodel factory must be installed. \"\n \"Try {}.\".format(\n \" or \".join(\"'pip install dwavebinarycsp[{}]'\".format(name) for name in supported)\n ))", "response": "Assert that penaltymodel. core. penaltymodel. penaltymodel_factory is available."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef add_constraint(self, constraint, variables=tuple()):\n if isinstance(constraint, Constraint):\n if variables and (tuple(variables) != constraint.variables):\n raise ValueError(\"mismatched variables and Constraint\")\n elif isinstance(constraint, Callable):\n constraint = Constraint.from_func(constraint, variables, self.vartype)\n elif isinstance(constraint, Iterable):\n constraint = Constraint.from_configurations(constraint, variables, self.vartype)\n else:\n raise TypeError(\"Unknown constraint type given\")\n\n self.constraints.append(constraint)\n for v in constraint.variables:\n self.variables[v].append(constraint)", "response": "Add a constraint to the set of available base classes."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef stitch(csp, min_classical_gap=2.0, max_graph_size=8):\n\n # ensure we have penaltymodel factory available\n try:\n dwavebinarycsp.assert_penaltymodel_factory_available()\n except AssertionError as e:\n raise RuntimeError(e)\n\n def aux_factory():\n for i in count():\n yield 'aux{}'.format(i)\n\n aux = aux_factory()\n\n bqm = dimod.BinaryQuadraticModel.empty(csp.vartype)\n\n # developer note: we could cache them and relabel, for now though let's do the simple thing\n # penalty_models = {}\n for const in csp.constraints:\n configurations = const.configurations\n\n if len(const.variables) > max_graph_size:\n msg = (\"The given csp contains a constraint {const} with {num_var} variables. \"\n \"This cannot be mapped to a graph with {max_graph_size} nodes. \"\n \"Consider checking whether your constraint is irreducible.\"\n \"\").format(const=const, num_var=len(const.variables), max_graph_size=max_graph_size)\n raise ImpossibleBQM(msg)\n\n pmodel = None\n\n if len(const) == 0:\n # empty constraint\n continue\n\n if min_classical_gap <= 2.0:\n if len(const) == 1 and max_graph_size >= 1:\n bqm.update(_bqm_from_1sat(const))\n continue\n elif len(const) == 2 and max_graph_size >= 2:\n bqm.update(_bqm_from_2sat(const))\n continue\n\n # developer note: we could cache them and relabel, for now though let's do the simple thing\n # if configurations in penalty_models:\n # raise NotImplementedError\n\n for G in iter_complete_graphs(const.variables, max_graph_size + 1, aux):\n\n # construct a specification\n spec = pm.Specification(\n graph=G,\n decision_variables=const.variables,\n feasible_configurations=configurations,\n min_classical_gap=min_classical_gap,\n vartype=csp.vartype\n )\n\n # try to use the penaltymodel ecosystem\n try:\n pmodel = pm.get_penalty_model(spec)\n except pm.ImpossiblePenaltyModel:\n # hopefully adding more variables will make it possible\n continue\n\n if pmodel.classical_gap >= min_classical_gap:\n break\n\n # developer note: we could cache them and relabel, for now though let's do the simple thing\n # penalty_models[configurations] = pmodel\n\n else:\n msg = (\"No penalty model can be build for constraint {}\".format(const))\n raise ImpossibleBQM(msg)\n\n bqm.update(pmodel.model)\n\n return bqm", "response": "Builds a binary quadratic model with minimal energy levels at solutions to the specified constraint satisfaction problem."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _bqm_from_1sat(constraint):\n configurations = constraint.configurations\n num_configurations = len(configurations)\n\n bqm = dimod.BinaryQuadraticModel.empty(constraint.vartype)\n\n if num_configurations == 1:\n val, = next(iter(configurations))\n v, = constraint.variables\n bqm.add_variable(v, -1 if val > 0 else +1, vartype=dimod.SPIN)\n else:\n bqm.add_variables_from((v, 0.0) for v in constraint.variables)\n\n return bqm", "response": "create a bqm for a constraint with only one variable\n bqm will have exactly classical gap 2."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _bqm_from_2sat(constraint):\n configurations = constraint.configurations\n variables = constraint.variables\n vartype = constraint.vartype\n u, v = constraint.variables\n\n # if all configurations are present, then nothing is infeasible and the bqm is just all\n # 0.0s\n if len(configurations) == 4:\n return dimod.BinaryQuadraticModel.empty(constraint.vartype)\n\n # check if the constraint is irreducible, and if so, build the bqm for its two\n # components\n components = irreducible_components(constraint)\n if len(components) > 1:\n const0 = Constraint.from_configurations(((config[0],) for config in configurations),\n (u,), vartype)\n const1 = Constraint.from_configurations(((config[1],) for config in configurations),\n (v,), vartype)\n bqm = _bqm_from_1sat(const0)\n bqm.update(_bqm_from_1sat(const1))\n return bqm\n\n assert len(configurations) > 1, \"single configurations should be irreducible\"\n\n # if it is not irreducible, and there are infeasible configurations, then it is time to\n # start building a bqm\n bqm = dimod.BinaryQuadraticModel.empty(vartype)\n\n # if the constraint is not irreducible and has two configurations, then it is either eq or ne\n if all(operator.eq(*config) for config in configurations):\n bqm.add_interaction(u, v, -1, vartype=dimod.SPIN) # equality\n elif all(operator.ne(*config) for config in configurations):\n bqm.add_interaction(u, v, +1, vartype=dimod.SPIN) # inequality\n elif (1, 1) not in configurations:\n bqm.add_interaction(u, v, 2, vartype=dimod.BINARY) # penalize (1, 1)\n elif (-1, +1) not in configurations and (0, 1) not in configurations:\n bqm.add_interaction(u, v, -2, vartype=dimod.BINARY)\n bqm.add_variable(v, 2, vartype=dimod.BINARY)\n elif (+1, -1) not in configurations and (1, 0) not in configurations:\n bqm.add_interaction(u, v, -2, vartype=dimod.BINARY)\n bqm.add_variable(u, 2, vartype=dimod.BINARY)\n else:\n # (0, 0) not in configurations\n bqm.add_interaction(u, v, 2, vartype=dimod.BINARY)\n bqm.add_variable(u, -2, vartype=dimod.BINARY)\n bqm.add_variable(v, -2, vartype=dimod.BINARY)\n\n return bqm", "response": "create a bqm for a constraint with two variables."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef iter_complete_graphs(start, stop, factory=None):\n _, nodes = start\n nodes = list(nodes) # we'll be appending\n\n if factory is None:\n factory = count()\n\n while len(nodes) < stop:\n # we need to construct a new graph each time, this is actually faster than copy and add\n # the new edges in any case\n G = nx.complete_graph(nodes)\n yield G\n\n v = next(factory)\n while v in G:\n v = next(factory)\n\n nodes.append(v)", "response": "Iterate over the complete graphs."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef load_cnf(fp):\n\n fp = iter(fp) # handle lists/tuples/etc\n\n csp = ConstraintSatisfactionProblem(dimod.BINARY)\n\n # first look for the problem\n num_clauses = num_variables = 0\n problem_pattern = re.compile(_PROBLEM_REGEX)\n for line in fp:\n matches = problem_pattern.findall(line)\n if matches:\n if len(matches) > 1:\n raise ValueError\n nv, nc = matches[0]\n num_variables, num_clauses = int(nv), int(nc)\n break\n\n # now parse the clauses, picking up where we left off looking for the header\n clause_pattern = re.compile(_CLAUSE_REGEX)\n for line in fp:\n if clause_pattern.match(line) is not None:\n clause = [int(v) for v in line.split(' ')[:-1]] # line ends with a trailing 0\n\n # -1 is the notation for NOT(1)\n variables = [abs(v) for v in clause]\n\n f = _cnf_or(clause)\n\n csp.add_constraint(f, variables)\n\n for v in range(1, num_variables+1):\n csp.add_variable(v)\n for v in csp.variables:\n if v > num_variables:\n msg = (\"given .cnf file's header defines variables [1, {}] and {} clauses \"\n \"but constraints a reference to variable {}\").format(num_variables, num_clauses, v)\n raise ValueError(msg)\n\n if len(csp) != num_clauses:\n msg = (\"given .cnf file's header defines {} \"\n \"clauses but the file contains {}\").format(num_clauses, len(csp))\n raise ValueError(msg)\n\n return csp", "response": "Load a constraint satisfaction problem from a. cnf file."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef and_gate(variables, vartype=dimod.BINARY, name='AND'):\n\n variables = tuple(variables)\n\n if vartype is dimod.BINARY:\n configurations = frozenset([(0, 0, 0),\n (0, 1, 0),\n (1, 0, 0),\n (1, 1, 1)])\n\n def func(in1, in2, out): return (in1 and in2) == out\n\n else:\n # SPIN, vartype is checked by the decorator\n configurations = frozenset([(-1, -1, -1),\n (-1, +1, -1),\n (+1, -1, -1),\n (+1, +1, +1)])\n\n def func(in1, in2, out): return ((in1 > 0) and (in2 > 0)) == (out > 0)\n\n return Constraint(func, configurations, variables, vartype=vartype, name=name)", "response": "Returns a constraint that is satisfied when the variables in the variables are assigned values that match the valid states of an AND gate."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef xor_gate(variables, vartype=dimod.BINARY, name='XOR'):\n\n variables = tuple(variables)\n if vartype is dimod.BINARY:\n configs = frozenset([(0, 0, 0),\n (0, 1, 1),\n (1, 0, 1),\n (1, 1, 0)])\n\n def func(in1, in2, out): return (in1 != in2) == out\n\n else:\n # SPIN, vartype is checked by the decorator\n configs = frozenset([(-1, -1, -1),\n (-1, +1, +1),\n (+1, -1, +1),\n (+1, +1, -1)])\n\n def func(in1, in2, out): return ((in1 > 0) != (in2 > 0)) == (out > 0)\n\n return Constraint(func, configs, variables, vartype=vartype, name=name)", "response": "Returns a constraint that is satisfied when the variables of an XOR gate are set to the current state of the current state."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nfull adder for a set of variables.", "response": "def fulladder_gate(variables, vartype=dimod.BINARY, name='FULL_ADDER'):\n \"\"\"Full adder.\n\n Args:\n variables (list): Variable labels for the and gate as `[in1, in2, in3, sum, carry]`,\n where `in1, in2, in3` are inputs to be added and `sum` and 'carry' the resultant\n outputs.\n vartype (Vartype, optional, default='BINARY'): Variable type. Accepted\n input values:\n\n * Vartype.SPIN, 'SPIN', {-1, 1}\n * Vartype.BINARY, 'BINARY', {0, 1}\n name (str, optional, default='FULL_ADDER'): Name for the constraint.\n\n Returns:\n Constraint(:obj:`.Constraint`): Constraint that is satisfied when its variables are\n assigned values that match the valid states of a Boolean full adder.\n\n Examples:\n >>> import dwavebinarycsp\n >>> import dwavebinarycsp.factories.constraint.gates as gates\n >>> csp = dwavebinarycsp.ConstraintSatisfactionProblem(dwavebinarycsp.BINARY)\n >>> csp.add_constraint(gates.fulladder_gate(['a', 'b', 'c_in', 'total', 'c_out'], name='FA1'))\n >>> csp.check({'a': 1, 'b': 0, 'c_in': 1, 'total': 0, 'c_out': 1})\n True\n\n \"\"\"\n\n variables = tuple(variables)\n\n if vartype is dimod.BINARY:\n configs = frozenset([(0, 0, 0, 0, 0),\n (0, 0, 1, 1, 0),\n (0, 1, 0, 1, 0),\n (0, 1, 1, 0, 1),\n (1, 0, 0, 1, 0),\n (1, 0, 1, 0, 1),\n (1, 1, 0, 0, 1),\n (1, 1, 1, 1, 1)])\n\n else:\n # SPIN, vartype is checked by the decorator\n configs = frozenset([(-1, -1, -1, -1, -1),\n (-1, -1, +1, +1, -1),\n (-1, +1, -1, +1, -1),\n (-1, +1, +1, -1, +1),\n (+1, -1, -1, +1, -1),\n (+1, -1, +1, -1, +1),\n (+1, +1, -1, -1, +1),\n (+1, +1, +1, +1, +1)])\n\n def func(in1, in2, in3, sum_, carry):\n total = (in1 > 0) + (in2 > 0) + (in3 > 0)\n if total == 0:\n return (sum_ <= 0) and (carry <= 0)\n elif total == 1:\n return (sum_ > 0) and (carry <= 0)\n elif total == 2:\n return (sum_ <= 0) and (carry > 0)\n elif total == 3:\n return (sum_ > 0) and (carry > 0)\n else:\n raise ValueError(\"func recieved unexpected values\")\n\n return Constraint(func, configs, variables, vartype=vartype, name=name)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef sat2in4(pos, neg=tuple(), vartype=dimod.BINARY, name='2-in-4'):\n pos = tuple(pos)\n neg = tuple(neg)\n\n variables = pos + neg\n\n if len(variables) != 4:\n raise ValueError(\"\")\n\n if neg and (len(neg) < 4):\n # because 2-in-4 sat is symmetric, all negated is the same as none negated\n\n const = sat2in4(pos=variables, vartype=vartype) # make one that has no negations\n for v in neg:\n const.flip_variable(v)\n const.name = name # overwrite the name directly\n\n return const\n\n # we can just construct them directly for speed\n if vartype is dimod.BINARY:\n configurations = frozenset([(0, 0, 1, 1),\n (0, 1, 0, 1),\n (1, 0, 0, 1),\n (0, 1, 1, 0),\n (1, 0, 1, 0),\n (1, 1, 0, 0)])\n else:\n # SPIN, vartype is checked by the decorator\n configurations = frozenset([(-1, -1, +1, +1),\n (-1, +1, -1, +1),\n (+1, -1, -1, +1),\n (-1, +1, +1, -1),\n (+1, -1, +1, -1),\n (+1, +1, -1, -1)])\n\n def func(a, b, c, d):\n if a == b:\n return (b != c) and (c == d)\n elif a == c:\n # a != b\n return b == d\n else:\n # a != b, a != c => b == c\n return a == d\n\n return Constraint(func, configurations, variables, vartype=vartype, name=name)", "response": "Return a new set of two - in - four variables that satisfy a two - in - four Satisfiability problem."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning a random two - in - four constraint problem.", "response": "def random_2in4sat(num_variables, num_clauses, vartype=dimod.BINARY, satisfiable=True):\n \"\"\"Random two-in-four (2-in-4) constraint satisfaction problem.\n\n Args:\n num_variables (integer): Number of variables (at least four).\n num_clauses (integer): Number of constraints that together constitute the\n constraint satisfaction problem.\n vartype (Vartype, optional, default='BINARY'): Variable type. Accepted\n input values:\n\n * Vartype.SPIN, 'SPIN', {-1, 1}\n * Vartype.BINARY, 'BINARY', {0, 1}\n satisfiable (bool, optional, default=True): True if the CSP can be satisfied.\n\n Returns:\n CSP (:obj:`.ConstraintSatisfactionProblem`): CSP that is satisfied when its variables\n are assigned values that satisfy a two-in-four satisfiability problem.\n\n Examples:\n This example creates a CSP with 6 variables and two random constraints and checks\n whether a particular assignment of variables satisifies it.\n\n >>> import dwavebinarycsp\n >>> import dwavebinarycsp.factories as sat\n >>> csp = sat.random_2in4sat(6, 2)\n >>> csp.constraints # doctest: +SKIP\n [Constraint.from_configurations(frozenset({(1, 0, 1, 0), (1, 0, 0, 1), (1, 1, 1, 1), (0, 1, 1, 0), (0, 0, 0, 0),\n (0, 1, 0, 1)}), (2, 4, 0, 1), Vartype.BINARY, name='2-in-4'),\n Constraint.from_configurations(frozenset({(1, 0, 1, 1), (1, 1, 0, 1), (1, 1, 1, 0), (0, 0, 0, 1),\n (0, 1, 0, 0), (0, 0, 1, 0)}), (1, 2, 4, 5), Vartype.BINARY, name='2-in-4')]\n >>> csp.check({0: 1, 1: 0, 2: 1, 3: 1, 4: 0, 5: 0}) # doctest: +SKIP\n True\n\n\n \"\"\"\n\n if num_variables < 4:\n raise ValueError(\"a 2in4 problem needs at least 4 variables\")\n if num_clauses > 16 * _nchoosek(num_variables, 4): # 16 different negation patterns\n raise ValueError(\"too many clauses\")\n\n # also checks the vartype argument\n csp = ConstraintSatisfactionProblem(vartype)\n\n variables = list(range(num_variables))\n\n constraints = set()\n\n if satisfiable:\n values = tuple(vartype.value)\n planted_solution = {v: choice(values) for v in variables}\n\n configurations = [(0, 0, 1, 1), (0, 1, 0, 1), (1, 0, 0, 1),\n (0, 1, 1, 0), (1, 0, 1, 0), (1, 1, 0, 0)]\n\n while len(constraints) < num_clauses:\n # sort the variables because constraints are hashed on configurations/variables\n # because 2-in-4 sat is symmetric, we would not get a hash conflict for different\n # variable orders\n constraint_variables = sorted(sample(variables, 4))\n\n # pick (uniformly) a configuration and determine which variables we need to negate to\n # match the chosen configuration\n config = choice(configurations)\n pos = tuple(v for idx, v in enumerate(constraint_variables) if config[idx] == (planted_solution[v] > 0))\n neg = tuple(v for idx, v in enumerate(constraint_variables) if config[idx] != (planted_solution[v] > 0))\n\n const = sat2in4(pos=pos, neg=neg, vartype=vartype)\n\n assert const.check(planted_solution)\n\n constraints.add(const)\n else:\n while len(constraints) < num_clauses:\n # sort the variables because constraints are hashed on configurations/variables\n # because 2-in-4 sat is symmetric, we would not get a hash conflict for different\n # variable orders\n constraint_variables = sorted(sample(variables, 4))\n\n # randomly determine negations\n pos = tuple(v for v in constraint_variables if random() > .5)\n neg = tuple(v for v in constraint_variables if v not in pos)\n\n const = sat2in4(pos=pos, neg=neg, vartype=vartype)\n\n constraints.add(const)\n\n for const in constraints:\n csp.add_constraint(const)\n\n # in case any variables didn't make it in\n for v in variables:\n csp.add_variable(v)\n\n return csp"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef random_xorsat(num_variables, num_clauses, vartype=dimod.BINARY, satisfiable=True):\n if num_variables < 3:\n raise ValueError(\"a xor problem needs at least 3 variables\")\n if num_clauses > 8 * _nchoosek(num_variables, 3): # 8 different negation patterns\n raise ValueError(\"too many clauses\")\n\n # also checks the vartype argument\n csp = ConstraintSatisfactionProblem(vartype)\n\n variables = list(range(num_variables))\n\n constraints = set()\n\n if satisfiable:\n values = tuple(vartype.value)\n planted_solution = {v: choice(values) for v in variables}\n\n configurations = [(0, 0, 0), (0, 1, 1), (1, 0, 1), (1, 1, 0)]\n\n while len(constraints) < num_clauses:\n # because constraints are hashed on configurations/variables, and because the inputs\n # to xor can be swapped without loss of generality, we can order them\n x, y, z = sample(variables, 3)\n if y > x:\n x, y = y, x\n\n # get the constraint\n const = xor_gate([x, y, z], vartype=vartype)\n\n # pick (uniformly) a configuration and determine which variables we need to negate to\n # match the chosen configuration\n config = choice(configurations)\n\n for idx, v in enumerate(const.variables):\n if config[idx] != (planted_solution[v] > 0):\n const.flip_variable(v)\n\n assert const.check(planted_solution)\n\n constraints.add(const)\n else:\n while len(constraints) < num_clauses:\n # because constraints are hashed on configurations/variables, and because the inputs\n # to xor can be swapped without loss of generality, we can order them\n x, y, z = sample(variables, 3)\n if y > x:\n x, y = y, x\n\n # get the constraint\n const = xor_gate([x, y, z], vartype=vartype)\n\n # randomly flip each variable in the constraint\n for idx, v in enumerate(const.variables):\n if random() > .5:\n const.flip_variable(v)\n\n assert const.check(planted_solution)\n\n constraints.add(const)\n\n for const in constraints:\n csp.add_constraint(const)\n\n # in case any variables didn't make it in\n for v in variables:\n csp.add_variable(v)\n\n return csp", "response": "Random XOR constraint satisfaction problem."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef kwarg_decorator(func):\n @wraps(func)\n def decorator(arg=None, **kwargs):\n if arg is None:\n return lambda arg: decorator(arg, **kwargs)\n return func(arg, **kwargs)\n return decorator", "response": "A decorator that can be used to decorate functions that accept a single arg and some kwargs in to a\n "} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef signature_matches(func, args=(), kwargs={}):\n try:\n sig = inspect.signature(func)\n sig.bind(*args, **kwargs)\n except TypeError:\n return False\n else:\n return True", "response": "Check if a function is callable with some args and kwargs."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nallowing a function to be used as either a decorator with args, or called as a normal function. @last_arg_decorator def register_a_thing(foo, func, bar=True): .. # Called as a decorator @register_a_thing(\"abc\", bar=False) def my_func(): ... # Called as a normal function call def my_other_func(): ... register_a_thing(\"def\", my_other_func, bar=True)", "response": "def last_arg_decorator(func):\n \"\"\"\n Allows a function to be used as either a decorator with args, or called as\n a normal function.\n\n @last_arg_decorator\n def register_a_thing(foo, func, bar=True):\n ..\n\n # Called as a decorator\n @register_a_thing(\"abc\", bar=False)\n def my_func():\n ...\n\n # Called as a normal function call\n def my_other_func():\n ...\n\n register_a_thing(\"def\", my_other_func, bar=True)\n \"\"\"\n @wraps(func)\n def decorator(*args, **kwargs):\n if signature_matches(func, args, kwargs):\n return func(*args, **kwargs)\n else:\n return lambda last: func(*(args + (last,)), **kwargs)\n return decorator"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nadd a model chooser definition to the registry.", "response": "def register_chooser(self, chooser, **kwargs):\n \"\"\"Adds a model chooser definition to the registry.\"\"\"\n if not issubclass(chooser, Chooser):\n return self.register_simple_chooser(chooser, **kwargs)\n\n self.choosers[chooser.model] = chooser(**kwargs)\n return chooser"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngenerates a model chooser definition from a model and adds it to the registry.", "response": "def register_simple_chooser(self, model, **kwargs):\n \"\"\"\n Generates a model chooser definition from a model, and adds it to the\n registry.\n \"\"\"\n name = '{}Chooser'.format(model._meta.object_name)\n attrs = {'model': model}\n attrs.update(kwargs)\n\n chooser = type(name, (Chooser,), attrs)\n self.register_chooser(chooser)\n\n return model"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ngive an instance string in the form app. Model : pk returns a tuple of the model and instance. Raises ValueError on invalid instance string or missing instances.", "response": "def instance_from_str(instance_str):\n \"\"\"\n Given an instance string in the form \"app.Model:pk\", returns a tuple of\n ``(model, instance)``. If the pk part is empty, ``instance`` will be\n ``None``. Raises ``ValueError`` on invalid model strings or missing\n instances.\n \"\"\"\n match = instance_str_re.match(instance_str)\n if not match:\n raise ValueError(\"Invalid instance string\")\n\n model_string = match.group(1)\n try:\n model = apps.get_model(model_string)\n except (LookupError, ValueError):\n raise ValueError(\"Invalid instance string\")\n\n pk = match.group(2)\n if pk:\n try:\n return model, model._default_manager.get(pk=pk)\n except model.DoesNotExist:\n raise ValueError(\"Invalid instance string\")\n\n return model, None"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngets the audio - related fields for this client.", "response": "def formatter(self, api_client, data, newval):\n \"\"\"Get audio-related fields\n\n Try to find fields for the audio url for specified preferred quality\n level, or next-lowest available quality url otherwise.\n \"\"\"\n url_map = data.get(\"audioUrlMap\")\n audio_url = data.get(\"audioUrl\")\n\n # Only an audio URL, not a quality map. This happens for most of the\n # mobile client tokens and some of the others now. In this case\n # substitute the empirically determined default values in the format\n # used by the rest of the function so downstream consumers continue to\n # work.\n if audio_url and not url_map:\n url_map = {\n BaseAPIClient.HIGH_AUDIO_QUALITY: {\n \"audioUrl\": audio_url,\n \"bitrate\": 64,\n \"encoding\": \"aacplus\",\n }\n }\n elif not url_map: # No audio url available (e.g. ad tokens)\n return None\n\n valid_audio_formats = [BaseAPIClient.HIGH_AUDIO_QUALITY,\n BaseAPIClient.MED_AUDIO_QUALITY,\n BaseAPIClient.LOW_AUDIO_QUALITY]\n\n # Only iterate over sublist, starting at preferred audio quality, or\n # from the beginning of the list if nothing is found. Ensures that the\n # bitrate used will always be the same or lower quality than was\n # specified to prevent audio from skipping for slow connections.\n preferred_quality = api_client.default_audio_quality\n if preferred_quality in valid_audio_formats:\n i = valid_audio_formats.index(preferred_quality)\n valid_audio_formats = valid_audio_formats[i:]\n\n for quality in valid_audio_formats:\n audio_url = url_map.get(quality)\n\n if audio_url:\n return audio_url[self.field]\n\n return audio_url[self.field] if audio_url else None"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nparses additional url fields and map them to inputs Attempt to create a dictionary with keys being user input and and response being the returned URL", "response": "def formatter(self, api_client, data, newval):\n \"\"\"Parse additional url fields and map them to inputs\n\n Attempt to create a dictionary with keys being user input, and\n response being the returned URL\n \"\"\"\n if newval is None:\n return None\n\n user_param = data['_paramAdditionalUrls']\n urls = {}\n if isinstance(newval, str):\n urls[user_param[0]] = newval\n else:\n for key, url in zip(user_param, newval):\n urls[key] = url\n return urls"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef from_json_list(cls, api_client, data):\n return [cls.from_json(api_client, item) for item in data]", "response": "Convert a list of JSON values to a list of models\n "} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef populate_fields(api_client, instance, data):\n for key, value in instance.__class__._fields.items():\n default = getattr(value, \"default\", None)\n newval = data.get(value.field, default)\n\n if isinstance(value, SyntheticField):\n newval = value.formatter(api_client, data, newval)\n setattr(instance, key, newval)\n continue\n\n model_class = getattr(value, \"model\", None)\n if newval and model_class:\n if isinstance(newval, list):\n newval = model_class.from_json_list(api_client, newval)\n else:\n newval = model_class.from_json(api_client, newval)\n\n if newval and value.formatter:\n newval = value.formatter(api_client, newval)\n\n setattr(instance, key, newval)", "response": "Populate all fields of a model with data."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nconvert one JSON value to a model object", "response": "def from_json(cls, api_client, data):\n \"\"\"Convert one JSON value to a model object\n \"\"\"\n self = cls(api_client)\n PandoraModel.populate_fields(api_client, self, data)\n return self"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _send_cmd(self, cmd):\n self._process.stdin.write(\"{}\\n\".format(cmd).encode(\"utf-8\"))\n self._process.stdin.flush()", "response": "Write command to remote process\n "} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nensure that the player is started.", "response": "def _ensure_started(self):\n \"\"\"Ensure player backing process is started\n \"\"\"\n if self._process and self._process.poll() is None:\n return\n\n if not getattr(self, \"_cmd\"):\n raise RuntimeError(\"Player command is not configured\")\n\n log.debug(\"Starting playback command: %r\", self._cmd)\n self._process = SilentPopen(self._cmd)\n self._post_start()"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nplay a new song from a Pandora model.", "response": "def play(self, song):\n \"\"\"Play a new song from a Pandora model\n\n Returns once the stream starts but does not shut down the remote audio\n output backend process. Calls the input callback when the user has\n input.\n \"\"\"\n self._callbacks.play(song)\n self._load_track(song)\n time.sleep(2) # Give the backend time to load the track\n\n while True:\n try:\n self._callbacks.pre_poll()\n self._ensure_started()\n self._loop_hook()\n\n readers, _, _ = select.select(\n self._get_select_readers(), [], [], 1)\n\n for handle in readers:\n if handle.fileno() == self._control_fd:\n self._callbacks.input(handle.readline().strip(), song)\n else:\n value = self._read_from_process(handle)\n if self._player_stopped(value):\n return\n finally:\n self._callbacks.post_poll()"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef play_station(self, station):\n for song in iterate_forever(station.get_playlist):\n try:\n self.play(song)\n except StopIteration:\n self.stop()\n return", "response": "Play the station until something ends"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _post_start(self):\n flags = fcntl.fcntl(self._process.stdout, fcntl.F_GETFL)\n fcntl.fcntl(self._process.stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK)", "response": "Set stdout to non - blocking mode and read the next available entry."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nformat a station selection menu and make the user select a station", "response": "def station_selection_menu(self, error=None):\n \"\"\"Format a station menu and make the user select a station\n \"\"\"\n self.screen.clear()\n\n if error:\n self.screen.print_error(\"{}\\n\".format(error))\n\n for i, station in enumerate(self.stations):\n i = \"{:>3}\".format(i)\n print(\"{}: {}\".format(Colors.yellow(i), station.name))\n\n return self.stations[self.screen.get_integer(\"Station: \")]"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nplay callback for a single song", "response": "def play(self, song):\n \"\"\"Play callback\n \"\"\"\n if song.is_ad:\n print(\"{} \".format(Colors.cyan(\"Advertisement\")))\n else:\n print(\"{} by {}\".format(Colors.cyan(song.song_name),\n Colors.yellow(song.artist_name)))"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ninput callback handles key presses", "response": "def input(self, input, song):\n \"\"\"Input callback, handles key presses\n \"\"\"\n try:\n cmd = getattr(self, self.CMD_MAP[input][1])\n except (IndexError, KeyError):\n return self.screen.print_error(\n \"Invalid command {!r}!\".format(input))\n\n cmd(song)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nfunction decorator implementing retrying logic. exceptions: A tuple of exception classes; default (Exception,) The decorator will call the function up to max_tries times if it raises an exception. By default it catches instances of the Exception class and subclasses. This will recover after all but the most fatal errors. You may specify a custom tuple of exception classes with the 'exceptions' argument; the function will only be retried if it raises one of the specified exceptions.", "response": "def retries(max_tries, exceptions=(Exception,)):\n \"\"\"Function decorator implementing retrying logic.\n\n exceptions: A tuple of exception classes; default (Exception,)\n\n The decorator will call the function up to max_tries times if it raises\n an exception.\n\n By default it catches instances of the Exception class and subclasses.\n This will recover after all but the most fatal errors. You may specify a\n custom tuple of exception classes with the 'exceptions' argument; the\n function will only be retried if it raises one of the specified\n exceptions.\n \"\"\"\n def decorator(func):\n def function(*args, **kwargs):\n\n retries_left = max_tries\n while retries_left > 0:\n try:\n retries_left -= 1\n return func(*args, **kwargs)\n\n except exceptions as exc:\n # Don't retry for PandoraExceptions - unlikely that result\n # will change for same set of input parameters.\n if isinstance(exc, PandoraException):\n raise\n if retries_left > 0:\n time.sleep(delay_exponential(\n 0.5, 2, max_tries - retries_left))\n else:\n raise\n\n return function\n\n return decorator"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef delay_exponential(base, growth_factor, attempts):\n if base == 'rand':\n base = random.random()\n elif base <= 0:\n raise ValueError(\"The 'base' param must be greater than 0, \"\n \"got: {}\".format(base))\n time_to_sleep = base * (growth_factor ** (attempts - 1))\n return time_to_sleep", "response": "Calculate time to sleep based on exponential function."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef iterate_forever(func, *args, **kwargs):\n output = func(*args, **kwargs)\n\n while True:\n try:\n playlist_item = next(output)\n playlist_item.prepare_playback()\n yield playlist_item\n except StopIteration:\n output = func(*args, **kwargs)", "response": "Iterate over a finite iterator forever yielding items."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ngather user input and convert it to an integer", "response": "def get_integer(prompt):\n \"\"\"Gather user input and convert it to an integer\n\n Will keep trying till the user enters an interger or until they ^C the\n program.\n \"\"\"\n while True:\n try:\n return int(input(prompt).strip())\n except ValueError:\n print(Colors.red(\"Invalid Input!\"))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncollect results a list of results a list of results", "response": "def collect(self, dataset_readers_list):\n \"\"\"collect results\n\n\n Returns:\n a list of results\n\n \"\"\"\n\n ret = [ ]\n for i, collector in enumerate(self.components):\n report = ProgressReport(name='collecting results', done=(i + 1), total=len(self.components))\n alphatwirl.progressbar.report_progress(report)\n ret.append(collector.collect([(dataset, tuple(r.readers[i] for r in readerComposites))\n for dataset, readerComposites in dataset_readers_list]))\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nopening the drop box", "response": "def open(self):\n \"\"\"open the drop box\n\n You need to call this method before starting putting packages.\n\n Returns\n -------\n None\n\n \"\"\"\n\n self.workingArea.open()\n self.runid_pkgidx_map = { }\n self.runid_to_return = deque()"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nput a task This method places a task in the working area and have the dispatcher execute it. If you need to put multiple tasks, it can be much faster to use `put_multiple()` than to use this method multiple times depending of the dispatcher. Parameters ---------- package : callable A task Returns ------- int A package index assigned by the working area", "response": "def put(self, package):\n \"\"\"put a task\n\n This method places a task in the working area and have the\n dispatcher execute it.\n\n If you need to put multiple tasks, it can be much faster to\n use `put_multiple()` than to use this method multiple times\n depending of the dispatcher.\n\n Parameters\n ----------\n package : callable\n A task\n\n Returns\n -------\n int\n A package index assigned by the working area\n\n \"\"\"\n\n pkgidx = self.workingArea.put_package(package)\n\n logger = logging.getLogger(__name__)\n logger.info('submitting {}'.format(self.workingArea.package_relpath(pkgidx)))\n\n runid = self.dispatcher.run(self.workingArea, pkgidx)\n self.runid_pkgidx_map[runid] = pkgidx\n return pkgidx"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef put_multiple(self, packages):\n\n pkgidxs = [self.workingArea.put_package(p) for p in packages]\n\n logger = logging.getLogger(__name__)\n logger.info('submitting {}'.format(\n ', '.join(['{}'.format(self.workingArea.package_relpath(i)) for i in pkgidxs])\n ))\n\n runids = self.dispatcher.run_multiple(self.workingArea, pkgidxs)\n self.runid_pkgidx_map.update(zip(runids, pkgidxs))\n return pkgidxs", "response": "put tasks in the working area and have\n the dispatcher execute them"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns pairs of package indices and results of all tasks This method waits until all tasks finish. Returns ------- list A list of pairs of package indices and results", "response": "def receive(self):\n \"\"\"return pairs of package indices and results of all tasks\n\n This method waits until all tasks finish.\n\n Returns\n -------\n list\n A list of pairs of package indices and results\n\n \"\"\"\n\n ret = [ ] # a list of (pkgid, result)\n while True:\n\n if self.runid_pkgidx_map:\n self.runid_to_return.extend(self.dispatcher.poll())\n ret.extend(self._collect_all_finished_pkgidx_result_pairs())\n\n if not self.runid_pkgidx_map:\n break\n time.sleep(self.sleep)\n\n ret = sorted(ret, key=itemgetter(0))\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef poll(self):\n\n self.runid_to_return.extend(self.dispatcher.poll())\n ret = self._collect_all_finished_pkgidx_result_pairs()\n return ret", "response": "Returns a list of pairs of package indices and results of finished tasks\n "} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning a pair of a package index and result of a task This method waits until a tasks finishes. It returns `None` if no task is running. Returns ------- tuple or None A pair of a package index and result. `None` if no tasks is running.", "response": "def receive_one(self):\n \"\"\"return a pair of a package index and result of a task\n\n This method waits until a tasks finishes. It returns `None` if\n no task is running.\n\n Returns\n -------\n tuple or None\n A pair of a package index and result. `None` if no tasks\n is running.\n\n \"\"\"\n\n if not self.runid_pkgidx_map:\n return None\n\n while True:\n\n if not self.runid_to_return:\n self.runid_to_return.extend(self.dispatcher.poll())\n\n ret = self._collect_next_finished_pkgidx_result_pair()\n\n if ret is not None:\n break\n\n if self.runid_pkgidx_map:\n time.sleep(self.sleep)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nrunning the event loops in the background.", "response": "def run_multiple(self, eventLoops):\n \"\"\"run the event loops in the background.\n\n Args:\n eventLoops (list): a list of event loops to run\n\n \"\"\"\n\n self.nruns += len(eventLoops)\n return self.communicationChannel.put_multiple(eventLoops)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn pairs of run ids and results of finish event loops.", "response": "def poll(self):\n \"\"\"Return pairs of run ids and results of finish event loops.\n \"\"\"\n ret = self.communicationChannel.receive_finished()\n self.nruns -= len(ret)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning a pair of a run id and a result. This method waits until an event loop finishes. This method returns None if no loop is running.", "response": "def receive_one(self):\n \"\"\"Return a pair of a run id and a result.\n\n This method waits until an event loop finishes.\n This method returns None if no loop is running.\n \"\"\"\n if self.nruns == 0:\n return None\n ret = self.communicationChannel.receive_one()\n if ret is not None:\n self.nruns -= 1\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef receive(self):\n ret = self.communicationChannel.receive_all()\n self.nruns -= len(ret)\n if self.nruns > 0:\n import logging\n logger = logging.getLogger(__name__)\n logger.warning(\n 'too few results received: {} results received, {} more expected'.format(\n len(ret), self.nruns))\n elif self.nruns < 0:\n import logging\n logger = logging.getLogger(__name__)\n logger.warning(\n 'too many results received: {} results received, {} too many'.format(\n len(ret), -self.nruns))\n return ret", "response": "This method waits until all event loops finish\n and returns results."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nwaiting until all event loops end and returns the results.", "response": "def end(self):\n \"\"\"wait until all event loops end and returns the results.\n\n \"\"\"\n\n results = self.communicationChannel.receive()\n\n if self.nruns != len(results):\n import logging\n logger = logging.getLogger(__name__)\n # logger.setLevel(logging.DEBUG)\n logger.warning(\n 'too few results received: {} results received, {} expected'.format(\n len(results),\n self.nruns\n ))\n\n return results"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nconverts a dictionary to a list of tuples.", "response": "def key_vals_dict_to_tuple_list(key_vals_dict, fill=float('nan')):\n \"\"\"Convert ``key_vals_dict`` to `tuple_list``.\n\n Args:\n key_vals_dict (dict): The first parameter.\n fill: a value to fill missing data\n\n Returns:\n A list of tuples\n\n \"\"\"\n\n tuple_list = [ ]\n\n if not key_vals_dict: return tuple_list\n\n vlen = max([len(vs) for vs in itertools.chain(*key_vals_dict.values())])\n\n for k, vs in key_vals_dict.items():\n try:\n tuple_list.extend([k + tuple(v) + (fill, )*(vlen - len(v)) for v in vs])\n except TypeError:\n # assume k is not a tuple\n tuple_list.extend([(k, ) + tuple(v) + (fill, )*(vlen - len(v)) for v in vs])\n\n\n return tuple_list"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nopening the working area .", "response": "def open(self):\n \"\"\"Open the working area\n\n Returns\n -------\n None\n \"\"\"\n\n self.path = self._prepare_dir(self.topdir)\n self._copy_executable(area_path=self.path)\n self._save_logging_levels(area_path=self.path)\n self._put_python_modules(modules=self.python_modules, area_path=self.path)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef put_package(self, package):\n\n self.last_package_index += 1\n package_index = self.last_package_index\n\n package_fullpath = self.package_fullpath(package_index)\n # e.g., '{path}/tpd_20161129_122841_HnpcmF/task_00009.p.gz'\n\n with gzip.open(package_fullpath, 'wb') as f:\n pickle.dump(package, f, protocol=pickle.HIGHEST_PROTOCOL)\n f.close()\n\n result_fullpath = self.result_fullpath(package_index)\n # e.g., '{path}/tpd_20161129_122841_HnpcmF/results/task_00009/result.p.gz'\n\n result_dir = os.path.dirname(result_fullpath)\n # e.g., '{path}/tpd_20161129_122841_HnpcmF/results/task_00009'\n\n alphatwirl.mkdir_p(result_dir)\n\n return package_index", "response": "Put a package into the cache."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef collect_result(self, package_index):\n\n result_fullpath = self.result_fullpath(package_index)\n # e.g., '{path}/tpd_20161129_122841_HnpcmF/results/task_00009/result.p.gz'\n\n try:\n with gzip.open(result_fullpath, 'rb') as f:\n result = pickle.load(f)\n except Exception as e:\n logger = logging.getLogger(__name__)\n logger.warning(e)\n return None\n\n return result", "response": "Collect the result of a task in the specified package."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn the full path of the package containing the does not exist entry.", "response": "def package_fullpath(self, package_index):\n \"\"\"Returns the full path of the package\n\n This method returns the full path to the package. This method\n simply constructs the path based on the convention and doesn't\n check if the package actually exists.\n\n Parameters\n ----------\n package_index :\n a package index\n\n Returns\n -------\n str\n the full path to the package\n\n \"\"\"\n\n ret = os.path.join(self.path, self.package_relpath(package_index))\n # e.g., '{path}/tpd_20161129_122841_HnpcmF/task_00009.p.gz'\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn the relative path of the result.", "response": "def result_relpath(self, package_index):\n \"\"\"Returns the relative path of the result\n\n This method returns the path to the result relative to the\n top dir of the working area. This method simply constructs the\n path based on the convention and doesn't check if the result\n actually exists.\n\n Parameters\n ----------\n package_index :\n a package index\n\n Returns\n -------\n str\n the relative path to the result\n\n \"\"\"\n\n dirname = 'task_{:05d}'.format(package_index)\n # e.g., 'task_00009'\n\n ret = os.path.join('results', dirname, 'result.p.gz')\n # e.g., 'results/task_00009/result.p.gz'\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef result_fullpath(self, package_index):\n\n ret = os.path.join(self.path, self.result_relpath(package_index))\n # e.g., '{path}/tpd_20161129_122841_HnpcmF/results/task_00009/result.p.gz'\n\n return ret", "response": "Returns the full path of the result."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nsubmitting multiple jobs to the working area.", "response": "def run_multiple(self, workingArea, package_indices):\n \"\"\"Submit multiple jobs\n\n Parameters\n ----------\n workingArea :\n A workingArea\n package_indices : list(int)\n A list of package indices\n\n Returns\n -------\n list(str)\n The list of the run IDs of the jobs\n\n \"\"\"\n\n if not package_indices:\n return [ ]\n\n job_desc = self._compose_job_desc(workingArea, package_indices)\n\n clusterprocids = submit_jobs(job_desc, cwd=workingArea.path)\n\n # TODO: make configurable\n clusterids = clusterprocids2clusterids(clusterprocids)\n for clusterid in clusterids:\n change_job_priority([clusterid], 10)\n\n self.clusterprocids_outstanding.extend(clusterprocids)\n\n return clusterprocids"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the list of the finished jobs and the status of the finished jobs.", "response": "def poll(self):\n \"\"\"Return the run IDs of the finished jobs\n\n Returns\n -------\n list(str)\n The list of the run IDs of the finished jobs\n\n \"\"\"\n\n clusterids = clusterprocids2clusterids(self.clusterprocids_outstanding)\n clusterprocid_status_list = query_status_for(clusterids)\n # e.g., [['1730126.0', 2], ['1730127.0', 2], ['1730129.1', 1], ['1730130.0', 1]]\n\n if clusterprocid_status_list:\n clusterprocids, statuses = zip(*clusterprocid_status_list)\n else:\n clusterprocids, statuses = (), ()\n\n clusterprocids_finished = [i for i in self.clusterprocids_outstanding if i not in clusterprocids]\n self.clusterprocids_finished.extend(clusterprocids_finished)\n self.clusterprocids_outstanding[:] = clusterprocids\n\n # logging\n counter = collections.Counter(statuses)\n messages = [ ]\n if counter:\n messages.append(', '.join(['{}: {}'.format(HTCONDOR_JOBSTATUS[k], counter[k]) for k in counter.keys()]))\n if self.clusterprocids_finished:\n messages.append('Finished {}'.format(len(self.clusterprocids_finished)))\n logger = logging.getLogger(__name__)\n logger.info(', '.join(messages))\n\n return clusterprocids_finished"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef failed_runids(self, runids):\n\n # remove failed clusterprocids from self.clusterprocids_finished\n # so that len(self.clusterprocids_finished)) becomes the number\n # of the successfully finished jobs\n for i in runids:\n try:\n self.clusterprocids_finished.remove(i)\n except ValueError:\n pass", "response": "Provide the run IDs of failed jobs Returns None"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef atpbar(iterable, name=None):\n try:\n len_ = len(iterable)\n except TypeError:\n logger = logging.getLogger(__name__)\n logging.warning('length is unknown: {!r}'.format(iterable))\n logging.warning('atpbar is turned off')\n return iterable\n\n if name is None:\n name = repr(iterable)\n\n return Atpbar(iterable, name=name, len_=len_)", "response": "Progress bar for a given iterable."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef getArrays(self, tree, branchName):\n itsArray = self._getArray(tree, branchName)\n if itsArray is None: return None, None\n itsCountArray = self._getCounterArray(tree, branchName)\n return itsArray, itsCountArray", "response": "return the array. array objects for the given tree and branch"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef begin(self):\n if self.isopen: return\n self.dropbox.open()\n self.isopen = True", "response": "Begin consuming the current set of items."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef put(self, task, *args, **kwargs):\n if not self.isopen:\n logger = logging.getLogger(__name__)\n logger.warning('the drop box is not open')\n return\n package = TaskPackage(task=task, args=args, kwargs=kwargs)\n return self.dropbox.put(package)", "response": "put a task and its arguments into the dropbox"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nputting a list of tasks and their arguments This method can be used to put multiple tasks at once. Calling this method once with multiple tasks can be much faster than calling `put()` multiple times. Parameters ---------- task_args_kwargs_list : list A list of lists with three items that can be parameters of `put()`, i.e., `task`, `args`, `kwargs`. Returns ------- list A list of task IDs.", "response": "def put_multiple(self, task_args_kwargs_list):\n \"\"\"put a list of tasks and their arguments\n\n This method can be used to put multiple tasks at once. Calling\n this method once with multiple tasks can be much faster than\n calling `put()` multiple times.\n\n Parameters\n ----------\n task_args_kwargs_list : list\n\n A list of lists with three items that can be parameters of\n `put()`, i.e., `task`, `args`, `kwargs`.\n\n Returns\n -------\n list\n A list of task IDs.\n\n \"\"\"\n if not self.isopen:\n logger = logging.getLogger(__name__)\n logger.warning('the drop box is not open')\n return\n\n packages = [ ]\n for t in task_args_kwargs_list:\n try:\n task = t['task']\n args = t.get('args', ())\n kwargs = t.get('kwargs', {})\n package = TaskPackage(task=task, args=args, kwargs=kwargs)\n except TypeError:\n package = TaskPackage(task=t, args=(), kwargs={})\n packages.append(package)\n return self.dropbox.put_multiple(packages)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning a list of pairs of IDs and results of finished tasks.", "response": "def receive_finished(self):\n \"\"\"return a list of pairs of IDs and results of finished tasks.\n\n This method doesn't wait for tasks to finish. It returns IDs\n and results which have already finished.\n\n Returns\n -------\n list\n A list of pairs of IDs and results\n\n \"\"\"\n if not self.isopen:\n logger = logging.getLogger(__name__)\n logger.warning('the drop box is not open')\n return\n return self.dropbox.poll()"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef receive_one(self):\n if not self.isopen:\n logger = logging.getLogger(__name__)\n logger.warning('the drop box is not open')\n return\n return self.dropbox.receive_one()", "response": "return a pair of an ID and a result of a task."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a list of pairs of IDs and results of all tasks.", "response": "def receive_all(self):\n \"\"\"return a list of pairs of IDs and results of all tasks.\n\n This method waits for all tasks to finish.\n\n Returns\n -------\n list\n A list of pairs of IDs and results\n\n \"\"\"\n if not self.isopen:\n logger = logging.getLogger(__name__)\n logger.warning('the drop box is not open')\n return\n return self.dropbox.receive()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning a list of all results of all tasks. This method waits for all tasks to finish and returns a list of results of all tasks.", "response": "def receive(self):\n \"\"\"return a list results of all tasks.\n\n This method waits for all tasks to finish.\n\n Returns\n -------\n list\n A list of results of the tasks. The results are sorted in\n the order in which the tasks are put.\n\n \"\"\"\n pkgidx_result_pairs = self.receive_all()\n if pkgidx_result_pairs is None:\n return\n results = [r for _, r in pkgidx_result_pairs]\n return results"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nends - close dropbox", "response": "def end(self):\n \"\"\"end\n\n \"\"\"\n if not self.isopen: return\n self.dropbox.close()\n self.isopen = False"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef expand_path_cfg(path_cfg, alias_dict={ }, overriding_kargs={ }):\n\n if isinstance(path_cfg, str):\n return _expand_str(path_cfg, alias_dict, overriding_kargs)\n\n if isinstance(path_cfg, dict):\n return _expand_dict(path_cfg, alias_dict)\n\n # assume tuple or list\n return _expand_tuple(path_cfg, alias_dict, overriding_kargs)", "response": "expand a path config into a tuple or list of dicts"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _expand_str(path_cfg, alias_dict, overriding_kargs):\n\n if path_cfg in alias_dict:\n # e.g., path_cfg = 'var_cut'\n return _expand_str_alias(path_cfg, alias_dict, overriding_kargs)\n\n # e.g., path_cfg = 'ev : {low} <= ev.var[0] < {high}'\n return _expand_for_lambda_str(path_cfg, alias_dict, overriding_kargs)", "response": "expand a path config given as a string"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nexpanding a path config given as a string", "response": "def _expand_str_alias(path_cfg, alias_dict, overriding_kargs):\n \"\"\"expand a path config given as a string\n\n Args:\n path_cfg (str): an alias\n alias_dict (dict):\n overriding_kargs (dict):\n \"\"\"\n\n # e.g.,\n # path_cfg = 'var_cut'\n\n new_path_cfg = alias_dict[path_cfg]\n # e.g., ('ev : {low} <= ev.var[0] < {high}', {'low': 10, 'high': 200})\n\n new_overriding_kargs = dict(alias=path_cfg)\n # e.g., {'alias': 'var_cut'}\n\n new_overriding_kargs.update(overriding_kargs)\n # e.g., {'alias': 'var_cut', 'name': 'var_cut25', 'low': 25}\n\n return expand_path_cfg(new_path_cfg, alias_dict,new_overriding_kargs)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nexpand a path config given as a tuple", "response": "def _expand_tuple(path_cfg, alias_dict, overriding_kargs):\n \"\"\"expand a path config given as a tuple\n\n \"\"\"\n\n # e.g.,\n # path_cfg = ('ev : {low} <= ev.var[0] < {high}', {'low': 10, 'high': 200})\n # overriding_kargs = {'alias': 'var_cut', 'name': 'var_cut25', 'low': 25}\n\n new_path_cfg = path_cfg[0]\n # e.g., 'ev : {low} <= ev.var[0] < {high}'\n\n new_overriding_kargs = path_cfg[1].copy()\n # e.g., {'low': 10, 'high': 200}\n\n new_overriding_kargs.update(overriding_kargs)\n # e.g., {'low': 25, 'high': 200, 'alias': 'var_cut', 'name': 'var_cut25'}\n\n return expand_path_cfg(\n new_path_cfg,\n overriding_kargs=new_overriding_kargs,\n alias_dict=alias_dict\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef poll(self):\n finished_procs = [p for p in self.running_procs if p.poll() is not None]\n self.running_procs = collections.deque([p for p in self.running_procs if p not in finished_procs])\n\n for proc in finished_procs:\n stdout, stderr = proc.communicate()\n ## proc.communicate() returns (stdout, stderr) when\n ## self.pipe = True. Otherwise they are (None, None)\n\n finished_pids = [p.pid for p in finished_procs]\n self.finished_pids.extend(finished_pids)\n\n logger = logging.getLogger(__name__)\n messages = 'Running: {}, Finished: {}'.format(len(self.running_procs), len(self.finished_pids))\n logger.info(messages)\n\n return finished_pids", "response": "check if the jobs are running and return a list of pids for\n finished jobs\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef wait(self):\n finished_pids = [ ]\n while self.running_procs:\n finished_pids.extend(self.poll())\n return finished_pids", "response": "wait until all jobs finish and return a list of pids\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef getVector(self, tree, branchName):\n\n if (tree, branchName) in self.__class__.addressDict:\n return self.__class__.addressDict[(tree, branchName)]\n\n itsVector = self._getVector(tree, branchName)\n self.__class__.addressDict[(tree, branchName)] = itsVector\n\n return itsVector", "response": "return the ROOT. vector object for the branch."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nbuilding a new Parallel object.", "response": "def build_parallel(parallel_mode, quiet=True, processes=4,\n user_modules=None, dispatcher_options=None):\n \"\"\"initializes `Parallel`\n\n Parameters\n ----------\n parallel_mode : str\n \"multiprocessing\" (default), \"htcondor\" or \"subprocess\"\n quiet : bool, optional\n if True, progress bars will not be shown in the \"multiprocessing\" mode.\n process : int, optional\n The number of processes when ``parallel_mode`` is\n \"multiprocessing\"\n user_modules : list, optional\n The names of modules to be sent to worker nodes when\n parallel_mode is \"htcondor\"\n dispatcher_options : dict, optional\n Options to dispatcher\n\n Returns\n -------\n parallel\n an instance of the class `Parallel`\n\n \"\"\"\n\n if user_modules is None:\n user_modules = [ ]\n\n if dispatcher_options is None:\n dispatcher_options = dict()\n\n dispatchers = ('subprocess', 'htcondor')\n parallel_modes = ('multiprocessing', ) + dispatchers\n default_parallel_mode = 'multiprocessing'\n\n if not parallel_mode in parallel_modes:\n logger = logging.getLogger(__name__)\n logger.warning('unknown parallel_mode \"{}\", use default \"{}\"'.format(\n parallel_mode, default_parallel_mode\n ))\n parallel_mode = default_parallel_mode\n\n if parallel_mode == 'multiprocessing':\n if quiet:\n atpbar.disable()\n return _build_parallel_multiprocessing(processes=processes)\n\n return _build_parallel_dropbox(\n parallel_mode=parallel_mode,\n user_modules=user_modules,\n dispatcher_options=dispatcher_options\n )"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef configure(self, component, all_dependencies):\n ''' Ensure all config-time files have been generated. Return a\n dictionary of generated items.\n '''\n r = {}\n\n builddir = self.buildroot\n\n # only dependencies which are actually valid can contribute to the\n # config data (which includes the versions of all dependencies in its\n # build info) if the dependencies aren't available we can't tell what\n # version they are. Anything missing here should always be a test\n # dependency that isn't going to be used, otherwise the yotta build\n # command will fail before we get here\n available_dependencies = OrderedDict((k, v) for k, v in all_dependencies.items() if v)\n\n self.set_toplevel_definitions = ''\n if self.build_info_include_file is None:\n self.build_info_include_file, build_info_definitions = self.getBuildInfo(component.path, builddir)\n self.set_toplevel_definitions += build_info_definitions\n\n if self.config_include_file is None:\n self.config_include_file, config_definitions, self.config_json_file = self._getConfigData(available_dependencies, component, builddir, self.build_info_include_file)\n self.set_toplevel_definitions += config_definitions\n\n self.configured = True\n return {\n 'merged_config_include': self.config_include_file,\n 'merged_config_json': self.config_json_file,\n 'build_info_include': self.build_info_include_file\n }", "response": "Configure the a\n dictionary."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ngenerate all the top - level CMakeLists for this component and its dependencies", "response": "def generateRecursive(self, component, all_components, builddir=None, modbuilddir=None, processed_components=None, application=None):\n ''' generate top-level CMakeLists for this component and its\n dependencies: the CMakeLists are all generated in self.buildroot,\n which MUST be out-of-source\n\n !!! NOTE: experimenting with a slightly different way of doing\n things here, this function is a generator that yields any errors\n produced, so the correct use is:\n\n for error in gen.generateRecursive(...):\n print(error)\n '''\n assert(self.configured)\n\n if builddir is None:\n builddir = self.buildroot\n if modbuilddir is None:\n modbuilddir = os.path.join(builddir, 'ym')\n\n if processed_components is None:\n processed_components = dict()\n if not self.target:\n yield 'Target \"%s\" is not a valid build target' % self.target\n\n toplevel = not len(processed_components)\n\n logger.debug('generate build files: %s (target=%s)' % (component, self.target))\n # because of the way c-family language includes work we need to put the\n # public header directories of all components that this component\n # depends on (directly OR indirectly) into the search path, which means\n # we need to first enumerate all the direct and indirect dependencies\n recursive_deps = component.getDependenciesRecursive(\n available_components = all_components,\n target = self.target,\n available_only = True,\n test = True\n )\n dependencies = component.getDependencies(\n all_components,\n target = self.target,\n available_only = True,\n test = True\n )\n\n for name, dep in dependencies.items():\n # if dep is a test dependency, then it might not be required (if\n # we're not building tests). We don't actually know at this point\n if not dep:\n if dep.isTestDependency():\n logger.debug('Test dependency \"%s\" of \"%s\" is not installed.' % (name, component))\n else:\n yield 'Required dependency \"%s\" of \"%s\" is not installed.' % (name, component)\n # ensure this component is assumed to have been installed before we\n # check for its dependencies, in case it has a circular dependency on\n # itself\n processed_components[component.getName()] = component\n new_dependencies = OrderedDict([(name,c) for name,c in dependencies.items() if c and not name in processed_components])\n self.generate(builddir, modbuilddir, component, new_dependencies, dependencies, recursive_deps, application, toplevel)\n\n logger.debug('recursive deps of %s:' % component)\n for d in recursive_deps.values():\n logger.debug(' %s' % d)\n\n processed_components.update(new_dependencies)\n for name, c in new_dependencies.items():\n for error in self.generateRecursive(\n c, all_components, os.path.join(modbuilddir, name), modbuilddir, processed_components, application=application\n ):\n yield error"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _validateListedSubdirsExist(self, component):\n ''' Return true if all the subdirectories which this component lists in\n its module.json file exist (although their validity is otherwise\n not checked).\n\n If they don't, warning messages are printed.\n '''\n lib_subdirs = component.getLibs(explicit_only=True)\n bin_subdirs = component.getBinaries()\n\n ok = True\n for d in lib_subdirs:\n if not os.path.exists(os.path.join(component.path, d)):\n logger.warning(\n \"lib directory \\\"%s\\\" doesn't exist but is listed in the module.json file of %s\", d, component\n )\n ok = False\n\n for d in bin_subdirs:\n if not os.path.exists(os.path.join(component.path, d)):\n logger.warning(\n \"bin directory \\\"%s\\\" doesn't exist but is listed in the module.json file of %s\", d, component\n )\n ok = False\n\n return ok", "response": "Return True if all the subdirectories which this component lists in\n its module. json file exist and they are otherwise checked."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nlisting all sub - directories of the component.", "response": "def _listSubDirectories(self, component, toplevel):\n ''' return: {\n manual: [list of subdirectories with manual CMakeLists],\n auto: [list of pairs: (subdirectories name to autogenerate, a list of source files in that dir)],\n bin: {dictionary of subdirectory name to binary name},\n lib: {dictionary of subdirectory name to binary name},\n test: [list of directories that build tests],\n resource: [list of directories that contain resources]\n }\n '''\n manual_subdirs = []\n auto_subdirs = []\n header_subdirs = []\n lib_subdirs = component.getLibs()\n bin_subdirs = component.getBinaries()\n test_subdirs = []\n resource_subdirs = []\n # if the application or library is set to get the sources from top level (\".\"),\n # they'll be acumulated into a single array (top_sources below).\n top_sources = []\n start_on_top = \".\" in [os.path.normpath(x) for x in list(lib_subdirs.keys()) + list(bin_subdirs.keys())]\n for f in sorted(os.listdir(component.path)):\n if f in Ignore_Subdirs or f.startswith('.') or f.startswith('_'):\n continue\n check_cmakefile_path = os.path.join(f, 'CMakeLists.txt')\n if os.path.isfile(os.path.join(component.path, check_cmakefile_path)) and not \\\n component.ignores(check_cmakefile_path):\n self.checkStandardSourceDir(f, component)\n # if the subdirectory has a CMakeLists.txt in it (and it isn't\n # ignored), then delegate to that:\n manual_subdirs.append(f)\n # tests only supported in the `test` directory for now\n if f in ('test',):\n test_subdirs.append(f)\n else:\n if os.path.isfile(os.path.join(component.path, f)):\n # top level source: check if it should be included\n if not component.ignores(f) and start_on_top:\n sf = self.createSourceFile(f, os.path.join(component.path, f), \".\")\n if sf is not None:\n top_sources.append(sf)\n else:\n # otherwise, if the directory has source files, and is listed\n # as a source/test directory, generate a CMakeLists in the\n # corresponding temporary directory, and add that.\n sources = self.containsSourceFiles(os.path.join(component.path, f), component)\n if sources:\n if f in ('test',):\n auto_subdirs.append((f, sources))\n test_subdirs.append(f)\n elif start_on_top:\n # include the sources in this directory only if it's not\n # a potential test directory\n from yotta.lib import validate\n if not validate.isPotentialTestDir(f):\n top_sources.extend(sources)\n if f == component.getName():\n header_subdirs.append((f, sources))\n elif os.path.normpath(f) in [fsutils.fullySplitPath(x)[0] for x in lib_subdirs] or \\\n os.path.normpath(f) in [fsutils.fullySplitPath(x)[0] for x in bin_subdirs]:\n for full_subpath in list(lib_subdirs.keys()) + list(bin_subdirs.keys()):\n if fsutils.fullySplitPath(full_subpath)[0] == os.path.normpath(f):\n # this might be a sub-sub directory, in which\n # case we need to re-calculate the sources just\n # for the part we care about:\n sources = self.containsSourceFiles(os.path.join(component.path, full_subpath), component)\n auto_subdirs.append((full_subpath, sources))\n elif f == component.getName():\n header_subdirs.append((f, sources))\n elif toplevel and \\\n ((f in ('test',)) or \\\n (os.path.normpath(f) in lib_subdirs or start_on_top) or \\\n (os.path.normpath(f) in bin_subdirs or start_on_top) and not \\\n component.ignores(f)):\n # (if there aren't any source files then do nothing)\n # !!! FIXME: ensure this warning is covered in tests\n logger.warning(\"subdirectory \\\"%s\\\" of %s was ignored because it doesn't appear to contain any source files\", f, component)\n\n # 'resource' directory also has special meaning, but there's no\n # pattern for the files which might be in here:\n if f in ('resource',):\n resource_subdirs.append(os.path.join(component.path, f))\n\n # issue a warning if a differently cased or common misspelling of a\n # standard directory name was encountered:\n check_directory_name_cases = list(lib_subdirs.keys()) + list(bin_subdirs.keys()) + ['test', 'resource']\n if f.lower() in check_directory_name_cases + ['src'] and not \\\n f in check_directory_name_cases and not \\\n component.ignores(f):\n self.checkStandardSourceDir(f, component)\n if top_sources:\n # all the top level sources are grouped into a single cmake-generated directory\n # which is given the same name as the component\n auto_subdirs.append((component.getName(), top_sources))\n\n return {\n \"manual\": manual_subdirs,\n \"auto\": auto_subdirs,\n \"headers\": header_subdirs,\n \"bin\": {component.getName(): component.getName()} if (start_on_top and component.isApplication()) else bin_subdirs,\n \"lib\": {component.getName(): component.getName()} if (start_on_top and not component.isApplication()) else lib_subdirs,\n \"test\": test_subdirs,\n \"resource\": resource_subdirs\n }"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the path_to_config_header cmake_set_definitions", "response": "def _getConfigData(self, all_dependencies, component, builddir, build_info_header_path):\n ''' returns (path_to_config_header, cmake_set_definitions) '''\n # ordered_json, , read/write ordered json, internal\n from yotta.lib import ordered_json\n add_defs_header = ''\n set_definitions = ''\n # !!! backwards-compatible \"TARGET_LIKE\" definitions for the top-level\n # of the config. NB: THESE WILL GO AWAY\n definitions = []\n definitions.append(('TARGET', sanitizePreprocessorSymbol(self.target.getName())))\n definitions.append(('TARGET_LIKE_%s' % sanitizePreprocessorSymbol(self.target.getName()),None))\n\n # make the path to the build-info header available both to CMake and\n # in the preprocessor:\n full_build_info_header_path = replaceBackslashes(os.path.abspath(build_info_header_path))\n logger.debug('build info header include path: \"%s\"', full_build_info_header_path)\n definitions.append(('YOTTA_BUILD_INFO_HEADER', '\"'+full_build_info_header_path+'\"'))\n\n for target in self.target.getSimilarTo_Deprecated():\n if '*' not in target:\n definitions.append(('TARGET_LIKE_%s' % sanitizePreprocessorSymbol(target),None))\n\n merged_config = self.target.getMergedConfig()\n logger.debug('target configuration data: %s', merged_config)\n definitions += self._definitionsForConfig(merged_config, ['YOTTA', 'CFG'])\n\n add_defs_header += '// yotta config data (including backwards-compatible definitions)\\n'\n\n for k, v in definitions:\n if v is not None:\n add_defs_header += '#define %s %s\\n' % (k, v)\n set_definitions += 'set(%s %s)\\n' % (k, v)\n else:\n add_defs_header += '#define %s\\n' % k\n set_definitions += 'set(%s TRUE)\\n' % k\n\n add_defs_header += '\\n// version definitions\\n'\n\n for dep in list(all_dependencies.values()) + [component]:\n add_defs_header += \"#define YOTTA_%s_VERSION_STRING \\\"%s\\\"\\n\" % (sanitizePreprocessorSymbol(dep.getName()), str(dep.getVersion()))\n add_defs_header += \"#define YOTTA_%s_VERSION_MAJOR %d\\n\" % (sanitizePreprocessorSymbol(dep.getName()), dep.getVersion().major())\n add_defs_header += \"#define YOTTA_%s_VERSION_MINOR %d\\n\" % (sanitizePreprocessorSymbol(dep.getName()), dep.getVersion().minor())\n add_defs_header += \"#define YOTTA_%s_VERSION_PATCH %d\\n\" % (sanitizePreprocessorSymbol(dep.getName()), dep.getVersion().patch())\n\n # add the component's definitions\n defines = component.getDefines()\n if defines:\n add_defs_header += \"\\n// direct definitions (defines.json)\\n\"\n for name, value in defines.items():\n add_defs_header += \"#define %s %s\\n\" % (name, value)\n add_defs_header += '\\n'\n\n # use -include instead of lots of separate\n # defines... this is compiler specific, but currently testing it\n # out for gcc-compatible compilers only:\n config_include_file = os.path.join(builddir, 'yotta_config.h')\n config_json_file = os.path.join(builddir, 'yotta_config.json')\n set_definitions += 'set(YOTTA_CONFIG_MERGED_JSON_FILE \\\"%s\\\")\\n' % replaceBackslashes(os.path.abspath(config_json_file))\n\n self._writeFile(\n config_include_file,\n '#ifndef __YOTTA_CONFIG_H__\\n'+\n '#define __YOTTA_CONFIG_H__\\n'+\n add_defs_header+\n '#endif // ndef __YOTTA_CONFIG_H__\\n'\n )\n self._writeFile(\n config_json_file,\n ordered_json.dumps(merged_config)\n )\n return (config_include_file, set_definitions, config_json_file)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef getBuildInfo(self, sourcedir, builddir):\n ''' Write the build info header file, and return (path_to_written_header, set_cmake_definitions) '''\n cmake_defs = ''\n preproc_defs = '// yotta build info, #include YOTTA_BUILD_INFO_HEADER to access\\n'\n # standard library modules\n import datetime\n # vcs, , represent version controlled directories, internal\n from yotta.lib import vcs\n\n now = datetime.datetime.utcnow()\n vcs_instance = vcs.getVCS(sourcedir)\n if self.build_uuid is None:\n import uuid\n self.build_uuid = uuid.uuid4()\n\n definitions = [\n ('YOTTA_BUILD_YEAR', now.year, 'UTC year'),\n ('YOTTA_BUILD_MONTH', now.month, 'UTC month 1-12'),\n ('YOTTA_BUILD_DAY', now.day, 'UTC day 1-31'),\n ('YOTTA_BUILD_HOUR', now.hour, 'UTC hour 0-24'),\n ('YOTTA_BUILD_MINUTE', now.minute, 'UTC minute 0-59'),\n ('YOTTA_BUILD_SECOND', now.second, 'UTC second 0-61'),\n ('YOTTA_BUILD_UUID', self.build_uuid, 'unique random UUID for each build'),\n ]\n if vcs_instance is not None:\n commit_id = None\n repotype = vcs_instance.__class__.__name__\n try:\n commit_id = vcs_instance.getCommitId()\n except vcs.VCSNotInstalled as e:\n logger.warning('%s is not installed, VCS status build info is not available', repotype)\n commit_id = None\n except vcs.VCSError as e:\n logger.debug('%s', e)\n logger.warning(\n 'error detecting build info: \"%s\", build info is not available to the build. Please check that this is a valid %s repository!',\n str(e).split('\\n')[0],\n repotype\n )\n if commit_id is not None:\n clean_state = int(vcs_instance.isClean())\n description = vcs_instance.getDescription()\n definitions += [\n ('YOTTA_BUILD_VCS_ID', commit_id, 'git or mercurial hash'),\n ('YOTTA_BUILD_VCS_CLEAN', clean_state, 'evaluates true if the version control system was clean, otherwise false'),\n ('YOTTA_BUILD_VCS_DESCRIPTION', description, 'git describe or mercurial equivalent')\n ]\n\n for d in definitions:\n preproc_defs += '#define %s %s // %s\\n' % d\n cmake_defs += 'set(%s \"%s\") # %s\\n' % d\n\n buildinfo_include_file = os.path.join(builddir, 'yotta_build_info.h')\n self._writeFile(\n buildinfo_include_file,\n '#ifndef __YOTTA_BUILD_INFO_H__\\n'+\n '#define __YOTTA_BUILD_INFO_H__\\n'+\n preproc_defs+\n '#endif // ndef __YOTTA_BUILD_INFO_H__\\n'\n )\n return (buildinfo_include_file, cmake_defs)", "response": "Write the build info header file and return the path to the build info header and set_cmake_definitions"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef generate(\n self, builddir, modbuilddir, component, active_dependencies, immediate_dependencies, all_dependencies, application, toplevel\n ):\n ''' active_dependencies is the dictionary of components that need to be\n built for this component, but will not already have been built for\n another component.\n '''\n\n include_root_dirs = ''\n if application is not None and component is not application:\n include_root_dirs += 'include_directories(\"%s\")\\n' % replaceBackslashes(application.path)\n\n include_sys_dirs = ''\n include_other_dirs = ''\n for name, c in itertools.chain(((component.getName(), component),), all_dependencies.items()):\n if c is not component and c.isTestDependency():\n continue\n include_root_dirs += 'include_directories(\"%s\")\\n' % replaceBackslashes(c.path)\n dep_sys_include_dirs = c.getExtraSysIncludes()\n for d in dep_sys_include_dirs:\n include_sys_dirs += 'include_directories(SYSTEM \"%s\")\\n' % replaceBackslashes(os.path.join(c.path, d))\n dep_extra_include_dirs = c.getExtraIncludes()\n for d in dep_extra_include_dirs:\n include_other_dirs += 'include_directories(\"%s\")\\n' % replaceBackslashes(os.path.join(c.path, d))\n\n add_depend_subdirs = ''\n for name, c in active_dependencies.items():\n depend_subdir = replaceBackslashes(os.path.join(modbuilddir, name))\n relpath = replaceBackslashes(os.path.relpath(depend_subdir, self.buildroot))\n add_depend_subdirs += \\\n 'add_subdirectory(\\n' \\\n ' \"%s\"\\n' \\\n ' \"${CMAKE_BINARY_DIR}/%s\"\\n' \\\n ')\\n' \\\n % (depend_subdir, relpath)\n\n delegate_to_existing = None\n delegate_build_dir = None\n\n module_is_empty = False\n if os.path.isfile(os.path.join(component.path, 'CMakeLists.txt')) and not component.ignores('CMakeLists.txt'):\n # adding custom CMake is a promise to generate a library: so the\n # module is never empty in this case.\n delegate_to_existing = component.path\n add_own_subdirs = []\n logger.debug(\"delegate to build dir: %s\", builddir)\n delegate_build_dir = os.path.join(builddir, 'existing')\n else:\n # !!! TODO: if they don't exist, that should possibly be a fatal\n # error, not just a warning\n self._validateListedSubdirsExist(component)\n\n subdirs = self._listSubDirectories(component, toplevel)\n manual_subdirs = subdirs['manual']\n autogen_subdirs = subdirs['auto']\n binary_subdirs = subdirs['bin']\n lib_subdirs = subdirs['lib']\n test_subdirs = subdirs['test']\n resource_subdirs = subdirs['resource']\n header_subdirs = subdirs['headers']\n logger.debug(\"%s lib subdirs: %s, bin subdirs: %s\", component, lib_subdirs, binary_subdirs)\n\n add_own_subdirs = []\n for f in manual_subdirs:\n if os.path.isfile(os.path.join(component.path, f, 'CMakeLists.txt')):\n # if this module is a test dependency, then don't recurse\n # to building its own tests.\n if f in test_subdirs and component.isTestDependency():\n continue\n add_own_subdirs.append(\n (os.path.join(component.path, f), f)\n )\n # names of all directories at this level with stuff in: used to figure\n # out what to link automatically\n all_subdirs = manual_subdirs + [x[0] for x in autogen_subdirs]\n\n # first check if this module is empty:\n if component.isTestDependency():\n if len(autogen_subdirs) + len(add_own_subdirs) == 0:\n module_is_empty = True\n else:\n if len(autogen_subdirs) + len(add_own_subdirs) <= len(test_subdirs):\n module_is_empty = True\n\n # autogenerate CMakeLists for subdirectories as appropriate:\n for f, source_files in autogen_subdirs:\n if f in test_subdirs:\n # if this module is a test dependency, then don't recurse\n # to building its own tests.\n if component.isTestDependency():\n continue\n self.generateTestDirList(\n builddir, f, source_files, component, immediate_dependencies, toplevel=toplevel, module_is_empty=module_is_empty\n )\n else:\n if f in binary_subdirs:\n is_executable = True\n object_name = binary_subdirs[f]\n else:\n # not a test subdir or binary subdir: it must be a lib\n # subdir\n assert(f in lib_subdirs)\n object_name = lib_subdirs[f]\n\n for header_dir, header_files in header_subdirs:\n source_files.extend(header_files)\n\n self.generateSubDirList(\n builddir = builddir,\n dirname = f,\n source_files = source_files,\n component = component,\n all_subdirs = all_subdirs,\n immediate_dependencies = immediate_dependencies,\n object_name = object_name,\n resource_subdirs = resource_subdirs,\n is_executable = (f in binary_subdirs)\n )\n add_own_subdirs.append(\n (os.path.join(builddir, f), f)\n )\n\n # from now on, completely forget that this component had any tests\n # if it is itself a test dependency:\n if component.isTestDependency():\n test_subdirs = []\n\n # if we're not building anything other than tests, and this is a\n # library module (not a binary) then we need to generate a dummy\n # library so that this component can still be linked against\n if module_is_empty:\n if len(binary_subdirs):\n logger.warning('nothing to build!')\n else:\n add_own_subdirs.append(self.createDummyLib(\n component, builddir, [x[0] for x in immediate_dependencies.items() if not x[1].isTestDependency()]\n ))\n\n toolchain_file_path = os.path.join(builddir, 'toolchain.cmake')\n if toplevel:\n # generate the top-level toolchain file:\n template = jinja_environment.get_template('toolchain.cmake')\n file_contents = template.render({ #pylint: disable=no-member\n # toolchain files are provided in hierarchy\n # order, but the template needs them in reverse\n # order (base-first):\n \"toolchain_files\": self.target.getToolchainFiles()\n })\n self._writeFile(toolchain_file_path, file_contents)\n\n # generate the top-level CMakeLists.txt\n template = jinja_environment.get_template('base_CMakeLists.txt')\n\n relpath = os.path.relpath(builddir, self.buildroot)\n\n file_contents = template.render({ #pylint: disable=no-member\n \"toplevel\": toplevel,\n \"target_name\": self.target.getName(),\n \"set_definitions\": self.set_toplevel_definitions,\n \"toolchain_file\": toolchain_file_path,\n \"component\": component,\n \"relpath\": relpath,\n \"include_root_dirs\": include_root_dirs,\n \"include_sys_dirs\": include_sys_dirs,\n \"include_other_dirs\": include_other_dirs,\n \"add_depend_subdirs\": add_depend_subdirs,\n \"add_own_subdirs\": add_own_subdirs,\n \"config_include_file\": self.config_include_file,\n \"delegate_to\": delegate_to_existing,\n \"delegate_build_dir\": delegate_build_dir,\n \"active_dependencies\": active_dependencies,\n \"module_is_empty\": module_is_empty,\n \"cmake_includes\": self.target.getAdditionalIncludes()\n })\n self._writeFile(os.path.join(builddir, 'CMakeLists.txt'), file_contents)", "response": "Generates a new entry for the current version of the component."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _getTags(repo):\n ''' return a dictionary of {tag: tarball_url}'''\n logger.debug('get tags for %s', repo)\n g = Github(settings.getProperty('github', 'authtoken'))\n repo = g.get_repo(repo)\n tags = repo.get_tags()\n logger.debug('tags for %s: %s', repo, [t.name for t in tags])\n return {t.name: _ensureDomainPrefixed(t.tarball_url) for t in tags}", "response": "return a dictionary of tags for a given repository"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _getTipArchiveURL(repo):\n ''' return a string containing a tarball url '''\n g = Github(settings.getProperty('github', 'authtoken'))\n repo = g.get_repo(repo)\n return repo.get_archive_link('tarball')", "response": "return a string containing a tarball url"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _getCommitArchiveURL(repo, commit):\n ''' return a string containing a tarball url '''\n g = Github(settings.getProperty('github', 'authtoken'))\n repo = g.get_repo(repo)\n return repo.get_archive_link('tarball', commit)", "response": "return a string containing a tarball url"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nunpacks the specified tarball into the specified directory", "response": "def _getTarball(url, into_directory, cache_key, origin_info=None):\n '''unpack the specified tarball url into the specified directory'''\n\n try:\n access_common.unpackFromCache(cache_key, into_directory)\n except KeyError as e:\n tok = settings.getProperty('github', 'authtoken')\n headers = {}\n if tok is not None:\n headers['Authorization'] = 'token ' + str(tok)\n\n logger.debug('GET %s', url)\n response = requests.get(url, allow_redirects=True, stream=True, headers=headers)\n response.raise_for_status()\n\n logger.debug('getting file: %s', url)\n logger.debug('headers: %s', response.headers)\n response.raise_for_status()\n\n # github doesn't exposes hashes of the archives being downloaded as far\n # as I can tell :(\n access_common.unpackTarballStream(\n stream = response,\n into_directory = into_directory,\n hash = {},\n cache_key = cache_key,\n origin_info = origin_info\n )"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning a github component for any github url including git + ssh:// git + http:// etc. or None if this is not a Github URL.", "response": "def createFromSource(cls, vs, name=None):\n ''' returns a github component for any github url (including\n git+ssh:// git+http:// etc. or None if this is not a Github URL.\n For all of these we use the github api to grab a tarball, because\n that's faster.\n\n Normally version will be empty, unless the original url was of the\n form: 'owner/repo @version' or 'url://...#version', which can be used\n to grab a particular tagged version.\n\n (Note that for github components we ignore the component name - it\n doesn't have to match the github module name)\n '''\n return GithubComponent(vs.location, vs.spec, vs.semantic_spec, name)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a list of Version objects each with a tarball URL set", "response": "def availableVersions(self):\n ''' return a list of Version objects, each with a tarball URL set '''\n r = []\n for t in self._getTags():\n logger.debug(\"available version tag: %s\", t)\n # ignore empty tags:\n if not len(t[0].strip()):\n continue\n try:\n r.append(GithubComponentVersion(t[0], t[0], url=t[1], name=self.name, cache_key=None))\n except ValueError:\n logger.debug('invalid version tag: %s', t)\n\n return r"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef availableTags(self):\n ''' return a list of GithubComponentVersion objects for all tags\n '''\n return [\n GithubComponentVersion(\n '', t[0], t[1], self.name, cache_key=_createCacheKey('tag', t[0], t[1], self.name)\n ) for t in self._getTags()\n ]", "response": "return a list of GithubComponentVersion objects for all tags\n "} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef availableBranches(self):\n ''' return a list of GithubComponentVersion objects for the tip of each branch\n '''\n return [\n GithubComponentVersion(\n '', b[0], b[1], self.name, cache_key=None\n ) for b in _getBranchHeads(self.repo).items()\n ]", "response": "return a list of GithubComponentVersion objects for the tip of each branch\n "} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a GithubComponentVersion object for a specific commit if valid", "response": "def commitVersion(self):\n ''' return a GithubComponentVersion object for a specific commit if valid\n '''\n import re\n\n commit_match = re.match('^[a-f0-9]{7,40}$', self.tagOrBranchSpec(), re.I)\n if commit_match:\n return GithubComponentVersion(\n '', '', _getCommitArchiveURL(self.repo, self.tagOrBranchSpec()), self.name, cache_key=None\n )\n\n return None"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef createFromSource(cls, vs, name=None):\n ''' returns a hg component for any hg:// url, or None if this is not\n a hg component.\n\n Normally version will be empty, unless the original url was of the\n form 'hg+ssh://...#version', which can be used to grab a particular\n tagged version.\n '''\n # strip hg of the url scheme:\n if vs.location.startswith('hg+'):\n location = vs.location[3:]\n else:\n location = vs.location\n return HGComponent(location, vs.spec)", "response": "returns a hg component for any hg:// url or None if this is not\n "} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef dropRootPrivs(fn):\n ''' decorator to drop su/sudo privilages before running a function on\n unix/linux.\n The *real* uid is modified, so privileges are permanently dropped for\n the process. (i.e. make sure you don't need to do\n\n If there is a SUDO_UID environment variable, then we drop to that,\n otherwise we drop to nobody.\n '''\n\n def wrapped_fn(*args, **kwargs):\n q = multiprocessing.Queue()\n p = multiprocessing.Process(target=_dropPrivsReturnViaQueue, args=(q, fn, args, kwargs))\n p.start()\n\n r = None\n e = None\n while True:\n msg = q.get()\n if msg[0] == 'return':\n r = msg[1]\n if msg[0] == 'exception':\n e = msg[1](msg[2])\n if msg[0] == 'finish':\n # if the command raised an exception, propagate this:\n if e is not None:\n raise e #pylint: disable=raising-bad-type\n return r\n\n return wrapped_fn", "response": "decorator to drop root privilages before running a function on the system"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef installAndBuild(args, following_args):\n ''' Perform the build command, but provide detailed error information.\n Returns {status:0, build_status:0, generate_status:0, install_status:0} on success.\n If status: is nonzero there was some sort of error. Other properties\n are optional, and may not be set if that step was not attempted.\n '''\n build_status = generate_status = install_status = 0\n\n if not hasattr(args, 'build_targets'):\n vars(args)['build_targets'] = []\n\n if 'test' in args.build_targets:\n logging.error('Cannot build \"test\". Use \"yotta test\" to run tests.')\n return {'status':1}\n\n cwd = os.getcwd()\n c = validate.currentDirectoryModule()\n if not c:\n return {'status':1}\n\n try:\n target, errors = c.satisfyTarget(args.target, additional_config=args.config)\n except access_common.AccessException as e:\n logging.error(e)\n return {'status':1}\n if errors:\n for error in errors:\n logging.error(error)\n return {'status':1}\n\n # run the install command before building, we need to add some options the\n # install command expects to be present to do this:\n vars(args)['component'] = None\n vars(args)['act_globally'] = False\n if not hasattr(args, 'install_test_deps'):\n if 'all_tests' in args.build_targets:\n vars(args)['install_test_deps'] = 'all'\n elif not len(args.build_targets):\n vars(args)['install_test_deps'] = 'own'\n else:\n # If the named build targets include tests from other modules, we\n # need to install the deps for those modules. To do this we need to\n # be able to tell which module a library belongs to, which is not\n # straightforward (especially if there is custom cmake involved).\n # That's why this is 'all', and not 'none'.\n vars(args)['install_test_deps'] = 'all'\n\n # install may exit non-zero for non-fatal errors (such as incompatible\n # version specs), which it will display\n install_status = install.execCommand(args, [])\n\n builddir = os.path.join(cwd, 'build', target.getName())\n\n all_deps = c.getDependenciesRecursive(\n target = target,\n available_components = [(c.getName(), c)],\n test = True\n )\n\n # if a dependency is missing the build will almost certainly fail, so don't try\n missing = 0\n for d in all_deps.values():\n if not d and not (d.isTestDependency() and args.install_test_deps != 'all'):\n logging.error('%s not available' % os.path.split(d.path)[1])\n missing += 1\n if missing:\n logging.error('Missing dependencies prevent build. Use `yotta ls` to list them.')\n return {'status': 1, 'install_status':install_status, 'missing_status':missing}\n\n generator = cmakegen.CMakeGen(builddir, target)\n # only pass available dependencies to\n config = generator.configure(c, all_deps)\n logging.debug(\"config done, merged config: %s\", config['merged_config_json'])\n\n script_environment = {\n 'YOTTA_MERGED_CONFIG_FILE': config['merged_config_json']\n }\n # run pre-generate scripts for all components:\n runScriptWithModules(c, all_deps.values(), 'preGenerate', script_environment)\n\n app = c if len(c.getBinaries()) else None\n for error in generator.generateRecursive(c, all_deps, builddir, application=app):\n logging.error(error)\n generate_status = 1\n\n logging.debug(\"generate done.\")\n # run pre-build scripts for all components:\n runScriptWithModules(c, all_deps.values(), 'preBuild', script_environment)\n\n if (not hasattr(args, 'generate_only')) or (not args.generate_only):\n error = target.build(\n builddir, c, args, release_build=args.release_build,\n build_args=following_args, targets=args.build_targets,\n release_no_debug_info_build=args.release_no_debug_info_build\n )\n\n if error:\n logging.error(error)\n build_status = 1\n else:\n # post-build scripts only get run if we were successful:\n runScriptWithModules(c, all_deps.values(), 'postBuild', script_environment)\n\n if install_status:\n logging.warning(\n \"There were also errors installing and resolving dependencies, \"+\n \"which may have caused the build failure: see above, or run \"+\n \"`yotta install` for details.\"\n )\n\n return {\n 'status': build_status or generate_status or install_status,\n 'missing_status': missing,\n 'build_status': build_status,\n 'generate_status': generate_status,\n 'install_status': install_status\n }", "response": "Perform the install and build command."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _friendlyAuthError(fn):\n ''' Decorator to print a friendly you-are-not-authorised message. Use\n **outside** the _handleAuth decorator to only print the message after\n the user has been given a chance to login. '''\n @functools.wraps(fn)\n def wrapped(*args, **kwargs):\n try:\n return fn(*args, **kwargs)\n except requests.exceptions.HTTPError as e:\n if e.response.status_code == requests.codes.unauthorized: #pylint: disable=no-member\n logger.error('insufficient permission')\n elif e.response.status_code == requests.codes.bad and 'jwt has expired' in e.response.text.lower(): #pylint: disable=no-member\n logger.error('server returned status %s: %s', e.response.status_code, e.response.text)\n logger.error('Check that your system clock is set accurately!')\n else:\n logger.error('server returned status %s: %s', e.response.status_code, e.response.text)\n raise\n return wrapped", "response": "Decorator to print a friendly you - are - not - authorised message. Use\n ArcGIS s _handleAuth decorator to only print the message after the user has given a chance to login."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a decorator to swallow a requests exception for modules that are not accessible without logging in and turn it into an Unavailable exception.", "response": "def _raiseUnavailableFor401(message):\n ''' Returns a decorator to swallow a requests exception for modules that\n are not accessible without logging in, and turn it into an Unavailable\n exception.\n '''\n def __raiseUnavailableFor401(fn):\n def wrapped(*args, **kwargs):\n try:\n return fn(*args, **kwargs)\n except requests.exceptions.HTTPError as e:\n if e.response.status_code == requests.codes.unauthorized:\n raise access_common.Unavailable(message)\n else:\n raise\n return wrapped\n return __raiseUnavailableFor401"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\npublishes a tarblob to the registry.", "response": "def publish(namespace, name, version, description_file, tar_file, readme_file,\n readme_file_ext, registry=None):\n ''' Publish a tarblob to the registry, if the request fails, an exception\n is raised, which either triggers re-authentication, or is turned into a\n return value by the decorators. (If successful, the decorated function\n returns None)\n '''\n registry = registry or Registry_Base_URL\n\n url = '%s/%s/%s/versions/%s' % (\n registry,\n namespace,\n name,\n version\n )\n\n if readme_file_ext == '.md':\n readme_section_name = 'readme.md'\n elif readme_file_ext == '':\n readme_section_name = 'readme'\n else:\n raise ValueError('unsupported readme type: \"%s\"' % readme_file_ext)\n\n # description file is in place as text (so read it), tar file is a file\n body = OrderedDict([('metadata', (None, description_file.read(),'application/json')),\n ('tarball',('tarball', tar_file)),\n (readme_section_name, (readme_section_name, readme_file))])\n\n headers = _headersForRegistry(registry)\n\n response = requests.put(url, headers=headers, files=body)\n response.raise_for_status()\n\n return None"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef unpublish(namespace, name, version, registry=None):\n ''' Try to unpublish a recently published version. Return any errors that\n occur.\n '''\n registry = registry or Registry_Base_URL\n\n url = '%s/%s/%s/versions/%s' % (\n registry,\n namespace,\n name,\n version\n )\n\n headers = _headersForRegistry(registry)\n response = requests.delete(url, headers=headers)\n response.raise_for_status()\n\n return None", "response": "Try to unpublish a recently published version. Return any errors that occur."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef listOwners(namespace, name, registry=None):\n ''' List the owners of a module or target (owners are the people with\n permission to publish versions and add/remove the owners).\n '''\n registry = registry or Registry_Base_URL\n\n url = '%s/%s/%s/owners' % (\n registry,\n namespace,\n name\n )\n\n request_headers = _headersForRegistry(registry)\n\n response = requests.get(url, headers=request_headers)\n\n if response.status_code == 404:\n logger.error('no such %s, \"%s\"' % (namespace[:-1], name))\n return None\n\n # raise exceptions for other errors - the auth decorators handle these and\n # re-try if appropriate\n response.raise_for_status()\n\n return ordered_json.loads(response.text)", "response": "List the owners of a module or target."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef removeOwner(namespace, name, owner, registry=None):\n ''' Remove an owner for a module or target (owners are the people with\n permission to publish versions and add/remove the owners).\n '''\n registry = registry or Registry_Base_URL\n\n url = '%s/%s/%s/owners/%s' % (\n registry,\n namespace,\n name,\n owner\n )\n\n request_headers = _headersForRegistry(registry)\n\n response = requests.delete(url, headers=request_headers)\n\n if response.status_code == 404:\n logger.error('no such %s, \"%s\"' % (namespace[:-1], name))\n return\n\n # raise exceptions for other errors - the auth decorators handle these and\n # re-try if appropriate\n response.raise_for_status()\n\n return True", "response": "Remove an owner for a module or target."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nset the api key for accessing a specific registry.", "response": "def setAPIKey(registry, api_key):\n ''' Set the api key for accessing a registry. This is only necessary for\n development/test registries.\n '''\n if (registry is None) or (registry == Registry_Base_URL):\n return\n sources = _getSources()\n source = None\n for s in sources:\n if _sourceMatches(s, registry):\n source = s\n if source is None:\n source = {\n 'type':'registry',\n 'url':registry,\n }\n sources.append(source)\n source['apikey'] = api_key\n settings.set('sources', sources)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef getPublicKey(registry=None):\n ''' Return the user's public key (generating and saving a new key pair if necessary) '''\n registry = registry or Registry_Base_URL\n pubkey_pem = None\n if _isPublicRegistry(registry):\n pubkey_pem = settings.getProperty('keys', 'public')\n else:\n for s in _getSources():\n if _sourceMatches(s, registry):\n if 'keys' in s and s['keys'] and 'public' in s['keys']:\n pubkey_pem = s['keys']['public']\n break\n if not pubkey_pem:\n pubkey_pem, privatekey_pem = _generateAndSaveKeys()\n else:\n # settings are unicode, we should be able to safely decode to ascii for\n # the key though, as it will either be hex or PEM encoded:\n pubkey_pem = pubkey_pem.encode('ascii')\n # if the key doesn't look like PEM, it might be hex-encided-DER (which we\n # used historically), so try loading that:\n if b'-----BEGIN PUBLIC KEY-----' in pubkey_pem:\n pubkey = serialization.load_pem_public_key(pubkey_pem, default_backend())\n else:\n pubkey_der = binascii.unhexlify(pubkey_pem)\n pubkey = serialization.load_der_public_key(pubkey_der, default_backend())\n return _pubkeyWireFormat(pubkey)", "response": "Return the user s public key"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngets the authentication data for a specific user in the registry.", "response": "def getAuthData(registry=None):\n ''' Poll the registry to get the result of a completed authentication\n (which, depending on the authentication the user chose or was directed\n to, will include a github or other access token)\n '''\n registry = registry or Registry_Base_URL\n url = '%s/tokens' % (\n registry\n )\n\n request_headers = _headersForRegistry(registry)\n\n logger.debug('poll for tokens... %s', request_headers)\n\n try:\n response = requests.get(url, headers=request_headers)\n except requests.RequestException as e:\n logger.debug(str(e))\n return None\n\n if response.status_code == requests.codes.unauthorized: #pylint: disable=no-member\n logger.debug('Unauthorised')\n return None\n elif response.status_code == requests.codes.not_found: #pylint: disable=no-member\n logger.debug('Not Found')\n return None\n\n body = response.text\n logger.debug('auth data response: %s' % body);\n r = {}\n\n parsed_response = ordered_json.loads(body)\n\n if 'error' in parsed_response:\n raise AuthError(parsed_response['error'])\n\n for token in parsed_response:\n if 'provider' in token and token['provider'] and 'accessToken' in token:\n r[token['provider']] = token['accessToken']\n break\n\n logger.debug('parsed auth tokens %s' % r);\n return r"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns a registry component for anything that s a valid package name", "response": "def createFromSource(cls, vs, name, registry):\n ''' returns a registry component for anything that's a valid package\n name (this does not guarantee that the component actually exists in\n the registry: use availableVersions() for that).\n '''\n # we deliberately allow only lowercase, hyphen, and (unfortunately)\n # numbers in package names, to reduce the possibility of confusingly\n # similar names: if the name doesn't match this then escalate to make\n # the user fix it. Targets also allow +\n if registry == 'targets':\n name_match = re.match('^[a-z]+[a-z0-9+-]*$', name)\n if not name_match:\n raise access_common.AccessException(\n 'Target name \"%s\" is not valid (must contain only lowercase letters, hyphen, plus, and numbers)' % name\n )\n else:\n name_match = re.match('^[a-z]+[a-z0-9-]*$', name)\n if not name_match:\n raise access_common.AccessException(\n 'Module name \"%s\" is not valid (must contain only lowercase letters, hyphen, and numbers)' % name\n )\n assert(vs.semantic_spec)\n return RegistryThing(name, vs.semantic_spec, registry)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreads a list of files and merge them with the current settings.", "response": "def read(self, filenames):\n '''' Read a list of files. Their configuration values are merged, with\n preference to values from files earlier in the list.\n '''\n for fn in filenames:\n try:\n self.configs[fn] = ordered_json.load(fn)\n except IOError:\n self.configs[fn] = OrderedDict()\n except Exception as e:\n self.configs[fn] = OrderedDict()\n logging.warning(\n \"Failed to read settings file %s, it will be ignored. The error was: %s\",\n fn, e\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get(self, path):\n ''' return a configuration value\n\n usage:\n get('section.property')\n\n Note that currently array indexes are not supported. You must\n get the whole array.\n\n returns None if any path element or the property is missing\n '''\n path = _splitPath(path)\n for config in self.configs.values():\n cur = config\n for el in path:\n if el in cur:\n cur = cur[el]\n else:\n cur = None\n break\n if cur is not None:\n return cur\n return None", "response": "get a configuration value for a given path element"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef set(self, path, value=None, filename=None):\n ''' Set a configuration value. If no filename is specified, the\n property is set in the first configuration file. Note that if a\n filename is specified and the property path is present in an\n earlier filename then set property will be hidden.\n\n usage:\n set('section.property', value='somevalue')\n\n Note that currently array indexes are not supported. You must\n set the whole array.\n '''\n if filename is None:\n config = self._firstConfig()[1]\n else:\n config = self.configs[filename]\n\n path = _splitPath(path)\n for el in path[:-1]:\n if el in config:\n config = config[el]\n else:\n config[el] = OrderedDict()\n config = config[el]\n config[path[-1]] = value", "response": "Set a configuration value."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef islast(generator):\n ''' indicate whether the current item is the last one in a generator\n '''\n next_x = None\n first = True\n for x in generator:\n if not first:\n yield (next_x, False)\n next_x = x\n first = False\n if not first:\n yield (next_x, True)", "response": "indicate whether the current item is the last one in a generator\n "} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef remoteComponentFor(name, version_required, registry='modules'):\n ''' Return a RemoteComponent sublclass for the specified component name and\n source url (or version specification)\n Raises an exception if any arguments are invalid.\n '''\n\n try:\n vs = sourceparse.parseSourceURL(version_required)\n except ValueError as e:\n raise access_common.Unavailable(\n '%s' % (e)\n )\n\n if vs.source_type == 'registry':\n if registry not in ('modules', 'targets'):\n raise Exception('no known registry namespace \"%s\"' % registry)\n return registry_access.RegistryThing.createFromSource(\n vs, name, registry=registry\n )\n elif vs.source_type == 'github':\n return github_access.GithubComponent.createFromSource(vs, name)\n elif vs.source_type == 'git':\n return git_access.GitComponent.createFromSource(vs, name)\n elif vs.source_type == 'hg':\n return hg_access.HGComponent.createFromSource(vs, name)\n else:\n raise Exception('unsupported module source: \"%s\"' % vs.source_type)", "response": "Returns a RemoteComponent sublclass for the specified component name and version specification."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning a Component or Target for the specified version from the specified list of search paths. If update is True then only update the Component and Target if it is installed via a symlink. If update is False then return None.", "response": "def satisfyVersionFromSearchPaths(name, version_required, search_paths, update=False, type='module', inherit_shrinkwrap=None):\n ''' returns a Component/Target for the specified version, if found in the\n list of search paths. If `update' is True, then also check for newer\n versions of the found component, and update it in-place (unless it was\n installed via a symlink).\n '''\n # Pack, , base class for targets and components, internal\n from yotta.lib import pack\n\n v = None\n try:\n sv = sourceparse.parseSourceURL(version_required)\n except ValueError as e:\n logging.error(e)\n return None\n\n try:\n local_version = searchPathsFor(\n name,\n sv.semanticSpec(),\n search_paths,\n type,\n inherit_shrinkwrap = inherit_shrinkwrap\n )\n except pack.InvalidDescription as e:\n logger.error(e)\n return None\n\n logger.debug(\"%s %s locally\" % (('found', 'not found')[not local_version], name))\n if local_version:\n if update and not local_version.installedLinked():\n #logger.debug('attempt to check latest version of %s @%s...' % (name, version_required))\n v = latestSuitableVersion(name, version_required, registry=_registryNamespaceForType(type))\n if local_version:\n local_version.setLatestAvailable(v)\n\n # if we don't need to update, then we're done\n if local_version.installedLinked() or not local_version.outdated():\n logger.debug(\"satisfy component from directory: %s\" % local_version.path)\n # if a component exists (has a valid description file), and either is\n # not outdated, or we are not updating\n if name != local_version.getName():\n raise Exception('Component %s found in incorrectly named directory %s (%s)' % (\n local_version.getName(), name, local_version.path\n ))\n return local_version\n\n # otherwise, we need to update the installed component\n logger.info('update outdated: %s@%s -> %s' % (\n name,\n local_version.getVersion(),\n v\n ))\n # must rm the old component before continuing\n fsutils.rmRf(local_version.path)\n return _satisfyVersionByInstallingVersion(\n name, version_required, local_version.path, v, type=type, inherit_shrinkwrap=inherit_shrinkwrap\n )\n return None"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns a Component or Target for the specified name + version requirement into a subdirectory of working_directory", "response": "def satisfyVersionByInstalling(name, version_required, working_directory, type='module', inherit_shrinkwrap=None):\n ''' installs and returns a Component/Target for the specified name+version\n requirement, into a subdirectory of `working_directory'\n '''\n v = latestSuitableVersion(name, version_required, _registryNamespaceForType(type))\n install_into = os.path.join(working_directory, name)\n return _satisfyVersionByInstallingVersion(\n name, version_required, install_into, v, type=type, inherit_shrinkwrap = inherit_shrinkwrap\n )"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _satisfyVersionByInstallingVersion(name, version_required, working_directory, version, type='module', inherit_shrinkwrap=None):\n ''' installs and returns a Component/Target for the specified version requirement into\n 'working_directory' using the provided remote version object.\n This function is not normally called via `satisfyVersionByInstalling',\n which looks up a suitable remote version object.\n '''\n assert(version)\n logger.info('download %s', version)\n version.unpackInto(working_directory)\n r = _clsForType(type)(working_directory, inherit_shrinkwrap = inherit_shrinkwrap)\n if not r:\n raise Exception(\n 'Dependency \"%s\":\"%s\" is not a valid %s.' % (name, version_required, type)\n )\n if name != r.getName():\n raise Exception('%s %s (specification %s) has incorrect name %s' % (\n type, name, version_required, r.getName()\n ))\n # error code deliberately ignored here for now, it isn't clear what the\n # behaviour should be (abort? remove the unpacked state then abort?\n # continue?)\n r.runScript('postInstall')\n return r", "response": "returns a Component or Target object that is suitable for the specified version requirement into the specified working_directory"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a Component or Target for the specified version", "response": "def satisfyVersion(\n name,\n version_required,\n available,\n search_paths,\n working_directory,\n update_installed=None,\n type='module', # or 'target'\n inherit_shrinkwrap=None\n ):\n ''' returns a Component/Target for the specified version (either to an already\n installed copy (from the available list, or from disk), or to a newly\n downloaded one), or None if the version could not be satisfied.\n\n update_installed = None / 'Update'\n None: prevent any attempt to look for new versions if the\n component/target already exists\n Update: replace any existing version with the newest available, if\n the newest available has a higher version\n '''\n\n r = satisfyFromAvailable(name, available, type=type)\n if r is not None:\n if not sourceparse.parseSourceURL(version_required).semanticSpecMatches(r.getVersion()):\n raise access_common.SpecificationNotMet(\n \"Installed %s %s doesn't match specification %s\" % (type, name, version_required)\n )\n return r\n\n r = satisfyVersionFromSearchPaths(\n name,\n version_required,\n search_paths,\n (update_installed == 'Update'),\n type = type,\n inherit_shrinkwrap = inherit_shrinkwrap\n )\n if r is not None:\n return r\n\n return satisfyVersionByInstalling(\n name, version_required, working_directory, type=type, inherit_shrinkwrap = inherit_shrinkwrap\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef sourceDirValidationError(dirname, component_name):\n ''' validate source directory names in components '''\n if dirname == component_name:\n return 'Module %s public include directory %s should not contain source files' % (component_name, dirname)\n elif dirname.lower() in ('source', 'src') and dirname != 'source':\n return 'Module %s has non-standard source directory name: \"%s\" should be \"source\"' % (component_name, dirname)\n elif isPotentialTestDir(dirname) and dirname != 'test':\n return 'Module %s has non-standard test directory name: \"%s\" should be \"test\"' % (component_name, dirname)\n elif not Source_Dir_Regex.match(dirname):\n corrected = Source_Dir_Invalid_Regex.sub('', dirname.lower())\n if not corrected:\n corrected = 'source'\n return 'Module %s has non-standard source directory name: \"%s\" should be \"%s\"' % (component_name, dirname, corrected)\n else:\n return None", "response": "validate source directory names in components"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nprint information about outdated modules, return 0 if there is nothing to be done and nonzero otherwise", "response": "def displayOutdated(modules, dependency_specs, use_colours):\n ''' print information about outdated modules,\n return 0 if there is nothing to be done and nonzero otherwise\n '''\n if use_colours:\n DIM = colorama.Style.DIM #pylint: disable=no-member\n NORMAL = colorama.Style.NORMAL #pylint: disable=no-member\n BRIGHT = colorama.Style.BRIGHT #pylint: disable=no-member\n YELLOW = colorama.Fore.YELLOW #pylint: disable=no-member\n RED = colorama.Fore.RED #pylint: disable=no-member\n GREEN = colorama.Fore.GREEN #pylint: disable=no-member\n RESET = colorama.Style.RESET_ALL #pylint: disable=no-member\n else:\n DIM = BRIGHT = YELLOW = RED = GREEN = RESET = u''\n\n status = 0\n\n # access, , get components, internal\n from yotta.lib import access\n from yotta.lib import access_common\n # sourceparse, , parse version source urls, internal\n from yotta.lib import sourceparse\n\n for name, m in modules.items():\n if m.isTestDependency():\n continue\n try:\n latest_v = access.latestSuitableVersion(name, '*', registry='modules', quiet=True)\n except access_common.Unavailable as e:\n latest_v = None\n\n if not m:\n m_version = u' ' + RESET + BRIGHT + RED + u\"missing\" + RESET\n else:\n m_version = DIM + u'@%s' % (m.version)\n if not latest_v:\n print(u'%s%s%s%s not available from the registry%s' % (RED, name, m_version, NORMAL, RESET))\n status = 2\n continue\n elif not m or m.version < latest_v:\n update_prevented_by = ''\n if m:\n specs_preventing_update = [\n x for x in dependency_specs\n if x.name == name and not\n sourceparse.parseSourceURL(x.nonShrinkwrappedVersionReq()).semanticSpecMatches(latest_v)\n ]\n shrinkwrap_prevents_update = [\n x for x in dependency_specs\n if x.name == name and x.isShrinkwrapped() and not\n sourceparse.parseSourceURL(x.versionReq()).semanticSpecMatches(latest_v)\n ]\n if len(specs_preventing_update):\n update_prevented_by = ' (update prevented by specifications: %s)' % (\n ', '.join(['%s from %s' % (x.version_req, x.specifying_module) for x in specs_preventing_update])\n )\n if len(shrinkwrap_prevents_update):\n update_prevented_by += ' yotta-shrinkwrap.json prevents update'\n if m.version.major() < latest_v.major():\n # major versions being outdated might be deliberate, so not\n # that bad:\n colour = GREEN\n elif m.version.minor() < latest_v.minor():\n # minor outdated versions is moderately bad\n colour = YELLOW\n else:\n # patch-outdated versions is really bad, because there should\n # be no reason not to update:\n colour = RED\n else:\n colour = RED\n print(u'%s%s%s latest: %s%s%s%s' % (name, m_version, RESET, colour, latest_v.version, update_prevented_by, RESET))\n if not status:\n status = 1\n return status"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreads the. yotta_origin. json file and return the value of the url property.", "response": "def origin(self):\n ''' Read the .yotta_origin.json file (if present), and return the value\n of the 'url' property '''\n if self.origin_info is None:\n self.origin_info = {}\n try:\n self.origin_info = ordered_json.load(os.path.join(self.path, Origin_Info_Fname))\n except IOError:\n pass\n return self.origin_info.get('url', None)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef outdated(self):\n ''' Return a truthy object if a newer suitable version is available,\n otherwise return None.\n (in fact the object returned is a ComponentVersion that can be used\n to get the newer version)\n '''\n if self.latest_suitable_version and self.latest_suitable_version > self.version:\n return self.latest_suitable_version\n else:\n return None", "response": "Return a truthy object if a newer suitable version is available return None otherwise"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef commitVCS(self, tag=None):\n ''' Commit the current working directory state (or do nothing if the\n working directory is not version controlled)\n '''\n if not self.vcs:\n return\n self.vcs.commit(message='version %s' % tag, tag=tag)", "response": "Commit the current working directory state"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ntests if this module ignores the file at path.", "response": "def ignores(self, path):\n ''' Test if this module ignores the file at \"path\", which must be a\n path relative to the root of the module.\n\n If a file is within a directory that is ignored, the file is also\n ignored.\n '''\n test_path = PurePath('/', path)\n\n # also check any parent directories of this path against the ignore\n # patterns:\n test_paths = tuple([test_path] + list(test_path.parents))\n\n for exp in self.ignore_patterns:\n for tp in test_paths:\n if tp.match(exp):\n logger.debug('\"%s\" ignored (\"%s\" matched \"%s\")', path, tp, exp)\n return True\n return False"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nwriting the current component description to a package description file in the component directory.", "response": "def writeDescription(self):\n ''' Write the current (possibly modified) component description to a\n package description file in the component directory.\n '''\n ordered_json.dump(os.path.join(self.path, self.description_filename), self.description)\n if self.vcs:\n self.vcs.markForCommit(self.description_filename)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nwrite a tarball of the current component and target to the file object file_object which must already be opened at position 0", "response": "def generateTarball(self, file_object):\n ''' Write a tarball of the current component/target to the file object\n \"file_object\", which must already be open for writing at position 0\n '''\n archive_name = '%s-%s' % (self.getName(), self.getVersion())\n def filterArchive(tarinfo):\n if tarinfo.name.find(archive_name) == 0 :\n unprefixed_name = tarinfo.name[len(archive_name)+1:]\n tarinfo.mode &= 0o775\n else:\n unprefixed_name = tarinfo.name\n if self.ignores(unprefixed_name):\n return None\n else:\n return tarinfo\n with tarfile.open(fileobj=file_object, mode='w:gz') as tf:\n logger.info('generate archive extracting to \"%s\"' % archive_name)\n tf.add(self.path, arcname=archive_name, filter=filterArchive)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\npublish the object to the appropriate registry. Returns a description of any anyErrors that occured or None if successful.", "response": "def publish(self, registry=None):\n ''' Publish to the appropriate registry, return a description of any\n errors that occured, or None if successful.\n No VCS tagging is performed.\n '''\n if (registry is None) or (registry == registry_access.Registry_Base_URL):\n if 'private' in self.description and self.description['private']:\n return \"this %s is private and cannot be published\" % (self.description_filename.split('.')[0])\n upload_archive = os.path.join(self.path, 'upload.tar.gz')\n fsutils.rmF(upload_archive)\n fd = os.open(upload_archive, os.O_CREAT | os.O_EXCL | os.O_RDWR | getattr(os, \"O_BINARY\", 0))\n with os.fdopen(fd, 'rb+') as tar_file:\n tar_file.truncate()\n self.generateTarball(tar_file)\n logger.debug('generated tar file of length %s', tar_file.tell())\n tar_file.seek(0)\n # calculate the hash of the file before we upload it:\n shasum = hashlib.sha256()\n while True:\n chunk = tar_file.read(1000)\n if not chunk:\n break\n shasum.update(chunk)\n logger.debug('generated tar file has hash %s', shasum.hexdigest())\n tar_file.seek(0)\n with self.findAndOpenReadme() as readme_file_wrapper:\n if not readme_file_wrapper:\n logger.warning(\"no readme.md file detected\")\n with open(self.getDescriptionFile(), 'r') as description_file:\n return registry_access.publish(\n self.getRegistryNamespace(),\n self.getName(),\n self.getVersion(),\n description_file,\n tar_file,\n readme_file_wrapper.file,\n readme_file_wrapper.extension().lower(),\n registry=registry\n )"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef unpublish(self, registry=None):\n ''' Try to un-publish the current version. Return a description of any\n errors that occured, or None if successful.\n '''\n return registry_access.unpublish(\n self.getRegistryNamespace(),\n self.getName(),\n self.getVersion(),\n registry=registry\n )", "response": "Try to un - publish the current version. Return a description of any\n errors that occured."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef getScript(self, scriptname):\n ''' Return the specified script command. If the first part of the\n command is a .py file, then the current python interpreter is\n prepended.\n\n If the script is a single string, rather than an array, it is\n shlex-split.\n '''\n script = self.description.get('scripts', {}).get(scriptname, None)\n if script is not None:\n if isinstance(script, str) or isinstance(script, type(u'unicode string')):\n import shlex\n script = shlex.split(script)\n # if the command is a python script, run it with the python\n # interpreter being used to run yotta, also fetch the absolute path\n # to the script relative to this module (so that the script can be\n # distributed with the module, no matter what current working\n # directory it will be executed in):\n if len(script) and script[0].lower().endswith('.py'):\n if not os.path.isabs(script[0]):\n absscript = os.path.abspath(os.path.join(self.path, script[0]))\n logger.debug('rewriting script %s to be absolute path %s', script[0], absscript)\n script[0] = absscript\n import sys\n script = [sys.executable] + script\n\n return script", "response": "Return the specified script command."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef runScript(self, scriptname, additional_environment=None):\n ''' Run the specified script from the scripts section of the\n module.json file in the directory of this module.\n '''\n import subprocess\n import shlex\n\n command = self.getScript(scriptname)\n if command is None:\n logger.debug('%s has no script %s', self, scriptname)\n return 0\n\n if not len(command):\n logger.error(\"script %s of %s is empty\", scriptname, self.getName())\n return 1\n\n # define additional environment variables for scripts:\n env = os.environ.copy()\n if additional_environment is not None:\n env.update(additional_environment)\n\n errcode = 0\n child = None\n try:\n logger.debug('running script: %s', command)\n child = subprocess.Popen(\n command, cwd = self.path, env = env\n )\n child.wait()\n if child.returncode:\n logger.error(\n \"script %s (from %s) exited with non-zero status %s\",\n scriptname,\n self.getName(),\n child.returncode\n )\n errcode = child.returncode\n child = None\n finally:\n if child is not None:\n tryTerminate(child)\n return errcode", "response": "Runs the specified script in the scripts section of the current module. json file in the directory of this module."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _truthyConfValue(v):\n ''' Determine yotta-config truthiness. In yotta config land truthiness is\n different to python or json truthiness (in order to map nicely only\n preprocessor and CMake definediness):\n\n json -> python -> truthy/falsey\n false -> False -> Falsey\n null -> None -> Falsey\n undefined -> None -> Falsey\n 0 -> 0 -> Falsey\n \"\" -> \"\" -> Truthy (different from python)\n \"0\" -> \"0\" -> Truthy\n {} -> {} -> Truthy (different from python)\n [] -> [] -> Truthy (different from python)\n everything else is truthy\n '''\n if v is False:\n return False\n elif v is None:\n return False\n elif v == 0:\n return False\n else:\n # everything else is truthy!\n return True", "response": "Determine whether the given value is truthy or not."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a list of dependency specs for this component.", "response": "def getDependencySpecs(self, target=None):\n ''' Returns [DependencySpec]\n\n These are returned in the order that they are listed in the\n component description file: this is so that dependency resolution\n proceeds in a predictable way.\n '''\n deps = []\n\n def specForDependency(name, version_spec, istest):\n shrinkwrap = self.getShrinkwrapMapping()\n shrinkwrap_version_req = None\n if name in shrinkwrap:\n # exact version, and pull from registry:\n shrinkwrap_version_req = shrinkwrap[name]\n logger.debug(\n 'respecting %s shrinkwrap version %s for %s', self.getName(), shrinkwrap_version_req, name\n )\n return pack.DependencySpec(\n name,\n version_spec,\n istest,\n shrinkwrap_version_req = shrinkwrap_version_req,\n specifying_module = self.getName()\n )\n\n deps += [specForDependency(x[0], x[1], False) for x in self.description.get('dependencies', {}).items()]\n target_deps = self.description.get('targetDependencies', {})\n if target is not None:\n for conf_key, target_conf_deps in target_deps.items():\n if _truthyConfValue(target.getConfigValue(conf_key)) or conf_key in target.getSimilarTo_Deprecated():\n logger.debug(\n 'Adding target-dependent dependency specs for target config %s to component %s' %\n (conf_key, self.getName())\n )\n deps += [specForDependency(x[0], x[1], False) for x in target_conf_deps.items()]\n\n\n deps += [specForDependency(x[0], x[1], True) for x in self.description.get('testDependencies', {}).items()]\n target_deps = self.description.get('testTargetDependencies', {})\n if target is not None:\n for conf_key, target_conf_deps in target_deps.items():\n if _truthyConfValue(target.getConfigValue(conf_key)) or conf_key in target.getSimilarTo_Deprecated():\n logger.debug(\n 'Adding test-target-dependent dependency specs for target config %s to component %s' %\n (conf_key, self.getName())\n )\n deps += [specForDependency(x[0], x[1], True) for x in target_conf_deps.items()]\n\n # remove duplicates (use the first occurrence)\n seen = set()\n r = []\n for dep in deps:\n if not dep.name in seen:\n r.append(dep)\n seen.add(dep.name)\n\n return r"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef hasDependency(self, name, target=None, test_dependencies=False):\n ''' Check if this module has any dependencies with the specified name\n in its dependencies list, or in target dependencies for the\n specified target\n '''\n if name in self.description.get('dependencies', {}).keys():\n return True\n\n target_deps = self.description.get('targetDependencies', {})\n if target is not None:\n for conf_key, target_conf_deps in target_deps.items():\n if _truthyConfValue(target.getConfigValue(conf_key)) or conf_key in target.getSimilarTo_Deprecated():\n if name in target_conf_deps:\n return True\n\n if test_dependencies:\n if name in self.description.get('testDependencies', {}).keys():\n return True\n\n if target is not None:\n test_target_deps = self.description.get('testTargetDependencies', {})\n for conf_key, target_conf_deps in test_target_deps.items():\n if _truthyConfValue(target.getConfigValue(conf_key)) or conf_key in target.getSimilarTo_Deprecated():\n if name in target_conf_deps:\n return True\n return False", "response": "Check if this module has any dependencies with the specified name\n in its dependencies list or in target dependencies for the specified target\n ."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nchecks if this module or any of its dependencies have the specified name in their targetDependencies corresponding to the specified target.", "response": "def hasDependencyRecursively(self, name, target=None, test_dependencies=False):\n ''' Check if this module, or any of its dependencies, have a\n dependencies with the specified name in their dependencies, or in\n their targetDependencies corresponding to the specified target.\n\n Note that if recursive dependencies are not installed, this test\n may return a false-negative.\n '''\n # checking dependencies recursively isn't entirely straightforward, so\n # use the existing method to resolve them all before checking:\n dependencies = self.getDependenciesRecursive(\n target = target,\n test = test_dependencies\n )\n return (name in dependencies)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef getDependencies(self,\n available_components = None,\n search_dirs = None,\n target = None,\n available_only = False,\n test = False,\n warnings = True\n ):\n ''' Returns {component_name:component}\n '''\n if search_dirs is None:\n search_dirs = [self.modulesPath()]\n available_components = self.ensureOrderedDict(available_components)\n\n components, errors = self.__getDependenciesWithProvider(\n available_components = available_components,\n search_dirs = search_dirs,\n target = target,\n update_installed = False,\n provider = self.provideInstalled,\n test = test\n )\n if warnings:\n for error in errors:\n logger.warning(error)\n if available_only:\n components = OrderedDict((k, v) for k, v in components.items() if v)\n return components", "response": "Returns a dictionary of component_name - > component"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef __getDependenciesWithProvider(self,\n available_components = None,\n search_dirs = None,\n target = None,\n update_installed = False,\n provider = None,\n test = False\n ):\n ''' Get installed components using \"provider\" to find (and possibly\n install) components.\n\n See documentation for __getDependenciesRecursiveWithProvider\n\n returns (components, errors)\n '''\n # sourceparse, , parse version source urls, internal\n from yotta.lib import sourceparse\n errors = []\n modules_path = self.modulesPath()\n def satisfyDep(dspec):\n try:\n r = provider(\n dspec,\n available_components,\n search_dirs,\n modules_path,\n update_installed,\n self\n )\n if r and not sourceparse.parseSourceURL(dspec.versionReq()).semanticSpecMatches(r.getVersion()):\n shrinkwrap_msg = ''\n if dspec.isShrinkwrapped():\n shrinkwrap_msg = 'shrinkwrap on '\n msg = 'does not meet specification %s required by %s%s' % (\n dspec.versionReq(), shrinkwrap_msg, self.getName()\n )\n logger.debug('%s %s', r.getName(), msg)\n r.setError(msg)\n return r\n except access_common.Unavailable as e:\n errors.append(e)\n self.dependencies_failed = True\n except vcs.VCSError as e:\n errors.append(e)\n self.dependencies_failed = True\n specs = self.getDependencySpecs(target=target)\n if not test:\n # filter out things that aren't test dependencies if necessary:\n specs = [x for x in specs if not x.is_test_dependency]\n #dependencies = pool.map(\n dependencies = map(\n satisfyDep, specs\n )\n self.installed_dependencies = True\n # stable order is important!\n return (OrderedDict([((d and d.getName()) or specs[i].name, d) for i, d in enumerate(dependencies)]), errors)", "response": "Get installed components using the specified provider."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef __getDependenciesRecursiveWithProvider(self,\n available_components = None,\n search_dirs = None,\n target = None,\n traverse_links = False,\n update_installed = False,\n provider = None,\n test = False,\n _processed = None\n ):\n ''' Get installed components using \"provider\" to find (and possibly\n install) components.\n\n This function is called with different provider functions in order\n to retrieve a list of all of the dependencies, or install all\n dependencies.\n\n Returns\n =======\n (components, errors)\n\n components: dictionary of name:Component\n errors: sequence of errors\n\n Parameters\n ==========\n available_components:\n None (default) or a dictionary of name:component. This is\n searched before searching directories or fetching remote\n components\n\n search_dirs:\n None (default), or sequence of directories to search for\n already installed, (but not yet loaded) components. Used so\n that manually installed or linked components higher up the\n dependency tree are found by their users lower down.\n\n These directories are searched in order, and finally the\n current directory is checked.\n\n target:\n None (default), or a Target object. If specified the target\n name and it's similarTo list will be used in resolving\n dependencies. If None, then only target-independent\n dependencies will be installed\n\n traverse_links:\n False (default) or True: whether to recurse into linked\n dependencies. You normally want to set this to \"True\" when\n getting a list of dependencies, and False when installing\n them (unless the user has explicitly asked dependencies to\n be installed in linked components).\n\n provider: None (default) or function:\n provider(\n dependency_spec,\n available_components,\n search_dirs,\n working_directory,\n update_if_installed\n )\n test:\n True, False, 'toplevel': should test-only dependencies be\n included (yes, no, or only at this level, not recursively)\n '''\n def recursionFilter(c):\n if not c:\n logger.debug('do not recurse into failed component')\n # don't recurse into failed components\n return False\n if c.getName() in _processed:\n logger.debug('do not recurse into already processed component: %s' % c)\n return False\n if c.installedLinked() and not traverse_links:\n return False\n return True\n available_components = self.ensureOrderedDict(available_components)\n if search_dirs is None:\n search_dirs = []\n if _processed is None:\n _processed = set()\n assert(test in [True, False, 'toplevel'])\n search_dirs.append(self.modulesPath())\n logger.debug('process %s\\nsearch dirs:%s' % (self.getName(), search_dirs))\n if self.isTestDependency():\n logger.debug(\"won't provide test dependencies recursively for test dependency %s\", self.getName())\n test = False\n components, errors = self.__getDependenciesWithProvider(\n available_components = available_components,\n search_dirs = search_dirs,\n update_installed = update_installed,\n target = target,\n provider = provider,\n test = test\n )\n _processed.add(self.getName())\n if errors:\n errors = ['Failed to satisfy dependencies of %s:' % self.path] + errors\n need_recursion = [x for x in filter(recursionFilter, components.values())]\n available_components.update(components)\n logger.debug('processed %s\\nneed recursion: %s\\navailable:%s\\nsearch dirs:%s' % (self.getName(), need_recursion, available_components, search_dirs))\n if test == 'toplevel':\n test = False\n # NB: can't perform this step in parallel, since the available\n # components list must be updated in order\n for c in need_recursion:\n dep_components, dep_errors = c.__getDependenciesRecursiveWithProvider(\n available_components = available_components,\n search_dirs = search_dirs,\n target = target,\n traverse_links = traverse_links,\n update_installed = update_installed,\n provider = provider,\n test = test,\n _processed = _processed\n )\n available_components.update(dep_components)\n components.update(dep_components)\n errors += dep_errors\n return (components, errors)", "response": "This function returns a list of all of the dependencies that are recursively installed and installed."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nretrieve and install all the dependencies of this component and its dependents recursively or from disk.", "response": "def satisfyDependenciesRecursive(\n self,\n available_components = None,\n search_dirs = None,\n update_installed = False,\n traverse_links = False,\n target = None,\n test = False\n ):\n ''' Retrieve and install all the dependencies of this component and its\n dependencies, recursively, or satisfy them from a collection of\n available_components or from disk.\n\n Returns\n =======\n (components, errors)\n\n components: dictionary of name:Component\n errors: sequence of errors\n\n Parameters\n ==========\n\n available_components:\n None (default) or a dictionary of name:component. This is\n searched before searching directories or fetching remote\n components\n\n search_dirs:\n None (default), or sequence of directories to search for\n already installed, (but not yet loaded) components. Used so\n that manually installed or linked components higher up the\n dependency tree are found by their users lower down.\n\n These directories are searched in order, and finally the\n current directory is checked.\n\n update_installed:\n False (default), True, or set(): whether to check the\n available versions of installed components, and update if a\n newer version is available. If this is a set(), only update\n things in the specified set.\n\n traverse_links:\n False (default) or True: whether to recurse into linked\n dependencies when updating/installing.\n\n target:\n None (default), or a Target object. If specified the target\n name and it's similarTo list will be used in resolving\n dependencies. If None, then only target-independent\n dependencies will be installed\n\n test:\n True, False, or 'toplevel: should test-only dependencies be\n installed? (yes, no, or only for this module, not its\n dependencies).\n\n '''\n def provider(\n dspec,\n available_components,\n search_dirs,\n working_directory,\n update_installed,\n dep_of=None\n ):\n r = access.satisfyFromAvailable(dspec.name, available_components)\n if r:\n if r.isTestDependency() and not dspec.is_test_dependency:\n logger.debug('test dependency subsequently occurred as real dependency: %s', r.getName())\n r.setTestDependency(False)\n return r\n update_if_installed = False\n if update_installed is True:\n update_if_installed = True\n elif update_installed:\n update_if_installed = dspec.name in update_installed\n r = access.satisfyVersionFromSearchPaths(\n dspec.name,\n dspec.versionReq(),\n search_dirs,\n update_if_installed,\n inherit_shrinkwrap = dep_of.getShrinkwrap()\n )\n if r:\n r.setTestDependency(dspec.is_test_dependency)\n return r\n # before resorting to install this module, check if we have an\n # existing linked module (which wasn't picked up because it didn't\n # match the version specification) - if we do, then we shouldn't\n # try to install, but should return that anyway:\n default_path = os.path.join(self.modulesPath(), dspec.name)\n if fsutils.isLink(default_path):\n r = Component(\n default_path,\n test_dependency = dspec.is_test_dependency,\n installed_linked = fsutils.isLink(default_path),\n inherit_shrinkwrap = dep_of.getShrinkwrap()\n )\n if r:\n assert(r.installedLinked())\n return r\n else:\n logger.error('linked module %s is invalid: %s', dspec.name, r.getError())\n return r\n\n r = access.satisfyVersionByInstalling(\n dspec.name,\n dspec.versionReq(),\n self.modulesPath(),\n inherit_shrinkwrap = dep_of.getShrinkwrap()\n )\n if not r:\n logger.error('could not install %s' % dspec.name)\n if r is not None:\n r.setTestDependency(dspec.is_test_dependency)\n return r\n\n return self.__getDependenciesRecursiveWithProvider(\n available_components = available_components,\n search_dirs = search_dirs,\n target = target,\n traverse_links = traverse_links,\n update_installed = update_installed,\n provider = provider,\n test = test\n )"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef satisfyTarget(self, target_name_and_version, update_installed=False, additional_config=None, install_missing=True):\n ''' Ensure that the specified target name (and optionally version,\n github ref or URL) is installed in the targets directory of the\n current component\n\n returns (derived_target, errors)\n '''\n # Target, , represent an installed target, internal\n from yotta.lib import target\n application_dir = None\n if self.isApplication():\n application_dir = self.path\n return target.getDerivedTarget(\n target_name_and_version,\n self.targetsPath(),\n install_missing = install_missing,\n application_dir = application_dir,\n update_installed = update_installed,\n additional_config = additional_config,\n shrinkwrap = self.getShrinkwrap()\n )", "response": "Ensure that the specified target name and version is installed in the targets directory of the current component\n returns a derived target object."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef getTarget(self, target_name_and_version, additional_config=None):\n ''' Return a derived target object representing the selected target: if\n the target is not installed, or is invalid then the returned object\n will test false in a boolean context.\n\n Returns derived_target\n\n Errors are not displayed.\n '''\n derived_target, errors = self.satisfyTarget(\n target_name_and_version,\n additional_config = additional_config,\n install_missing = False\n )\n if len(errors):\n return None\n else:\n return derived_target", "response": "Returns a derived target object representing the selected target."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a dictionary of binaries to compile into the current module.", "response": "def getBinaries(self):\n ''' Return a dictionary of binaries to compile: {\"dirname\":\"exename\"},\n this is used when automatically generating CMakeLists\n\n Note that currently modules may define only a single executable\n binary or library to be built by the automatic build system, by\n specifying `\"bin\": \"dir-to-be-built-into-binary\"`, or `\"lib\":\n \"dir-to-be-built-into-library\"`, and the bin/lib will always have\n the same name as the module. The default behaviour if nothing is\n specified is for the 'source' directory to be built into a library.\n\n The module.json syntax may allow for other combinations in the\n future (and callers of this function should not rely on it\n returning only a single item). For example, a \"bin\": {\"dirname\":\n \"exename\"} syntax might be supported, however currently more\n complex builds must be controlled by custom CMakeLists.\n '''\n # the module.json syntax is a subset of the package.json syntax: a\n # single string that defines the source directory to use to build an\n # executable with the same name as the component. This may be extended\n # to include the rest of the npm syntax in future (map of source-dir to\n # exe name).\n if 'bin' in self.description:\n return {os.path.normpath(self.description['bin']): self.getName()}\n else:\n return {}"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a dictionary of libraries to compile.", "response": "def getLibs(self, explicit_only=False):\n ''' Return a dictionary of libraries to compile: {\"dirname\":\"libname\"},\n this is used when automatically generating CMakeLists.\n\n If explicit_only is not set, then in the absence of both 'lib' and\n 'bin' sections in the module.json file, the \"source\" directory\n will be returned.\n\n Note that currently modules may define only a single executable\n binary or library to be built by the automatic build system, by\n specifying `\"bin\": \"dir-to-be-built-into-binary\"`, or `\"lib\":\n \"dir-to-be-built-into-library\"`, and the bin/lib will always have\n the same name as the module. The default behaviour if nothing is\n specified is for the 'source' directory to be built into a library.\n\n The module.json syntax may allow for other combinations in the\n future (and callers of this function should not rely on it\n returning only a single item). For example, a \"bin\": {\"dirname\":\n \"exename\"} syntax might be supported, however currently more\n complex builds must be controlled by custom CMakeLists.\n '''\n if 'lib' in self.description:\n return {os.path.normpath(self.description['lib']): self.getName()}\n elif 'bin' not in self.description and not explicit_only:\n return {'source': self.getName()}\n else:\n return {}"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef licenses(self):\n ''' Return a list of licenses that apply to this module. (Strings,\n which may be SPDX identifiers)\n '''\n if 'license' in self.description:\n return [self.description['license']]\n else:\n return [x['type'] for x in self.description['licenses']]", "response": "Return a list of licenses that apply to this module."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning a list of extraIncludes for this entry.", "response": "def getExtraIncludes(self):\n ''' Some components must export whole directories full of headers into\n the search path. This is really really bad, and they shouldn't do\n it, but support is provided as a concession to compatibility.\n '''\n if 'extraIncludes' in self.description:\n return [os.path.normpath(x) for x in self.description['extraIncludes']]\n else:\n return []"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn the list of system include paths that are needed to include the header files in the system.", "response": "def getExtraSysIncludes(self):\n ''' Some components (e.g. libc) must export directories of header files\n into the system include search path. They do this by adding a\n 'extraSysIncludes' : [ array of directories ] field in their\n package description. This function returns the list of directories\n (or an empty list), if it doesn't exist.\n '''\n if 'extraSysIncludes' in self.description:\n return [os.path.normpath(x) for x in self.description['extraSysIncludes']]\n else:\n return []"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef checkDependenciesForShrinkwrap(dependency_list):\n ''' return a list of errors encountered (e.g. dependency missing or\n specification not met\n '''\n # sourceparse, , parse version specifications, internall\n from yotta.lib import sourceparse\n errors = []\n # first gather the available versions of things:\n available_versions = {}\n for mod in dependency_list.get('modules', []):\n available_versions[mod['name']] = mod['version']\n # now check that the available versions satisfy all of the specifications\n # from other modules:\n for mod in dependency_list.get('modules', []):\n for spec_info in mod.get('specifications', []):\n name = spec_info['name']\n spec = spec_info['version']\n if spec_info.get('testOnly', False):\n # test-only specifications are ignored for shrinkwrap\n continue\n if not name in available_versions:\n errors.append('dependency %s (required by %s) is missing' % (\n name, mod['name']\n ))\n else:\n available_version = available_versions[name]\n parsed_spec = sourceparse.parseSourceURL(spec)\n if not parsed_spec.semanticSpecMatches(available_version):\n errors.append('%s@%s does not meet specification %s required by %s' % (\n name, available_version, parsed_spec.semanticSpec(), mod['name']\n ))\n\n return errors", "response": "check that all of the dependencies for shrinkwrap are met"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef availableVersions(self):\n ''' return a list of GitCloneVersion objects for tags which are valid\n semantic version idenfitifiers.\n '''\n r = []\n for t in self.vcs.tags():\n logger.debug(\"available version tag: %s\", t)\n # ignore empty tags:\n if not len(t.strip()):\n continue\n try:\n r.append(GitCloneVersion(t, t, self))\n except ValueError:\n logger.debug('invalid version tag: %s', t)\n return r", "response": "return a list of GitCloneVersion objects for tags which are valid\n semantic version idenfitifiers."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning a GithubComponentVersion object for a specific commit if valid", "response": "def commitVersion(self, spec):\n ''' return a GithubComponentVersion object for a specific commit if valid\n '''\n import re\n\n commit_match = re.match('^[a-f0-9]{7,40}$', spec, re.I)\n if commit_match:\n return GitCloneVersion('', spec, self)\n\n return None"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a git component for any git:// url or None if this is not", "response": "def createFromSource(cls, vs, name=None):\n ''' returns a git component for any git:// url, or None if this is not\n a git component.\n\n Normally version will be empty, unless the original url was of the\n form 'git://...#version', which can be used to grab a particular\n tag or branch, or ...#>=1.2.3, which can be used to specify\n semantic version specifications on tags.\n '''\n return GitComponent(vs.location, vs.spec, vs.semantic_spec)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _mergeDictionaries(*args):\n ''' merge dictionaries of dictionaries recursively, with elements from\n dictionaries earlier in the argument sequence taking precedence\n '''\n # to support merging of OrderedDicts, copy the result type from the first\n # argument:\n result = type(args[0])()\n for k, v in itertools.chain(*[x.items() for x in args]):\n if not k in result:\n result[k] = v\n elif isinstance(result[k], dict) and isinstance(v, dict):\n result[k] = _mergeDictionaries(result[k], v)\n return result", "response": "merge dictionaries of dictionaries recursively with elements from\nAttributeNames dictionaries earlier in the argument sequence taking precedence\nAttributeNames"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _mirrorStructure(dictionary, value):\n ''' create a new nested dictionary object with the same structure as\n 'dictionary', but with all scalar values replaced with 'value'\n '''\n result = type(dictionary)()\n for k in dictionary.keys():\n if isinstance(dictionary[k], dict):\n result[k] = _mirrorStructure(dictionary[k], value)\n else:\n result[k] = value\n return result", "response": "create a new nested dictionary object with the same structure as the dictionary but with all scalar values replaced with value"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nload additional config from a file or JSON file", "response": "def loadAdditionalConfig(config_path):\n ''' returns (error, config)\n '''\n error = None\n config = {}\n if not config_path:\n return (error, config)\n if os.path.isfile(config_path):\n try:\n config = ordered_json.load(config_path)\n except Exception as e:\n error = \"Invalid syntax in file %s: %s\" % (config_path, e)\n else:\n # try to interpret the argument as literal JSON\n try:\n config = ordered_json.loads(config_path)\n except Exception as e:\n # if this fails too, guess whether it was intended to be JSON or\n # not, and display an appropriate error message\n if '{' in config_path or '}' in config_path:\n error = \"Invalid syntax in literal JSON: %s\" % e\n else:\n error = \"File \\\"%s\\\" does not exist\" % config_path\n logger.debug('read additional config: %s', config)\n return (error, config)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef getDerivedTarget(\n target_name_and_version,\n targets_path,\n application_dir = None,\n install_missing = True,\n update_installed = False,\n additional_config = None,\n shrinkwrap = None\n ):\n # access, , get components, internal\n from yotta.lib import access\n from yotta.lib import access_common\n ''' Get the specified target description, optionally ensuring that it (and\n all dependencies) are installed in targets_path.\n\n Returns (DerivedTarget, errors), or (None, errors) if the leaf target\n could not be found/installed.\n '''\n logger.debug('satisfy target: %s' % target_name_and_version);\n if ',' in target_name_and_version:\n name, version_req = target_name_and_version.split(',')\n else:\n name = target_name_and_version\n version_req = '*'\n\n # shrinkwrap is the raw json form, not mapping form here, so rearrange it\n # before indexing:\n if shrinkwrap is not None:\n shrinkwrap_version_req = {\n x['name']: x['version'] for x in shrinkwrap.get('targets', [])\n }.get(name, None)\n else:\n shrinkwrap_version_req = None\n if shrinkwrap_version_req is not None:\n logger.debug(\n 'respecting shrinkwrap version %s for %s', shrinkwrap_version_req, name\n )\n\n dspec = pack.DependencySpec(\n name,\n version_req,\n shrinkwrap_version_req = shrinkwrap_version_req\n )\n\n leaf_target = None\n previous_name = dspec.name\n search_dirs = [targets_path]\n target_hierarchy = []\n errors = []\n while True:\n t = None\n try:\n if install_missing:\n t = access.satisfyVersion(\n name = dspec.name,\n version_required = dspec.versionReq(),\n available = target_hierarchy,\n search_paths = search_dirs,\n working_directory = targets_path,\n update_installed = ('Update' if update_installed else None),\n type = 'target',\n inherit_shrinkwrap = shrinkwrap\n )\n else:\n t = access.satisfyVersionFromSearchPaths(\n name = dspec.name,\n version_required = dspec.versionReq(),\n search_paths = search_dirs,\n type = 'target',\n inherit_shrinkwrap = shrinkwrap\n )\n except access_common.AccessException as e:\n errors.append(e)\n if not t:\n if install_missing:\n logger.error(\n 'could not install target %s for %s' %\n (dspec, previous_name)\n )\n break\n else:\n target_hierarchy.append(t)\n previous_name = dspec.name\n assert(isinstance(t, Target))\n dspec = t.baseTargetSpec() #pylint: disable=no-member\n if not leaf_target:\n leaf_target = t\n if dspec is None:\n break\n if leaf_target is None:\n return (None, errors)\n # if we have a valid target, try to load the app-specific config data (if\n # any):\n app_config = {}\n if application_dir is not None:\n app_config_fname = os.path.join(application_dir, App_Config_File)\n if os.path.exists(app_config_fname):\n try:\n app_config = ordered_json.load(app_config_fname)\n except Exception as e:\n errors.append(Exception(\"Invalid application config.json: %s\" % (e)))\n return (DerivedTarget(leaf_target, target_hierarchy[1:], app_config, additional_config), errors)", "response": "Returns aDerivedTarget object for the specified target name and version."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef baseTargetSpec(self):\n ''' returns pack.DependencySpec for the base target of this target (or\n None if this target does not inherit from another target.\n '''\n inherits = self.description.get('inherits', {})\n if len(inherits) == 1:\n name, version_req = list(inherits.items())[0]\n shrinkwrap_version_req = self.getShrinkwrapMapping('targets').get(name, None)\n if shrinkwrap_version_req is not None:\n logger.debug(\n 'respecting shrinkwrap version %s for %s', shrinkwrap_version_req, name\n )\n return pack.DependencySpec(\n name,\n version_req,\n shrinkwrap_version_req = shrinkwrap_version_req\n )\n elif len(inherits) > 1:\n logger.error('target %s specifies multiple base targets, but only one is allowed', self.getName())\n return None", "response": "returns the base target spec for this target"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns the specified script if one exists", "response": "def getScript(self, scriptname):\n ''' return the specified script if one exists (possibly inherited from\n a base target)\n '''\n for t in self.hierarchy:\n s = t.getScript(scriptname)\n if s:\n return s\n return None"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nload the configuration information from the target hierarchy", "response": "def _loadConfig(self):\n ''' load the configuration information from the target hierarchy '''\n config_dicts = [self.additional_config, self.app_config] + [t.getConfig() for t in self.hierarchy]\n # create an identical set of dictionaries, but with the names of the\n # sources in place of the values. When these are merged they will show\n # where each merged property came from:\n config_blame = [\n _mirrorStructure(self.additional_config, 'command-line config'),\n _mirrorStructure(self.app_config, 'application\\'s config.json'),\n ] + [\n _mirrorStructure(t.getConfig(), t.getName()) for t in self.hierarchy\n ]\n\n self.config = _mergeDictionaries(*config_dicts)\n self.config_blame = _mergeDictionaries(*config_blame)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn a list of toolchain file paths in override order", "response": "def getToolchainFiles(self):\n ''' return a list of toolchain file paths in override order (starting\n at the bottom/leaf of the hierarchy and ending at the base).\n The list is returned in the order they should be included\n (most-derived last).\n '''\n return reversed([\n os.path.join(x.path, x.description['toolchain']) for x in self.hierarchy if 'toolchain' in x.description\n ])"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef getAdditionalIncludes(self):\n ''' Return the list of cmake files which are to be included by yotta in\n every module built. The list is returned in the order they should\n be included (most-derived last).\n '''\n return reversed([\n os.path.join(t.path, include_file)\n for t in self.hierarchy\n for include_file in t.description.get('cmakeIncludes', [])\n ])", "response": "Return the list of cmake files which are to be included by yotta in\n every module built."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn true if this target inherits from the named target.", "response": "def inheritsFrom(self, target_name):\n ''' Return true if this target inherits from the named target (directly\n or indirectly. Also returns true if this target is the named\n target. Otherwise return false.\n '''\n for t in self.hierarchy:\n if t and t.getName() == target_name or target_name in t.description.get('inherits', {}):\n return True\n return False"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef exec_helper(self, cmd, builddir):\n ''' Execute the given command, returning an error message if an error occured\n or None if the command was succesful.'''\n try:\n child = subprocess.Popen(cmd, cwd=builddir)\n child.wait()\n except OSError as e:\n if e.errno == errno.ENOENT:\n if cmd[0] == 'cmake':\n return 'CMake is not installed, please follow the installation instructions at http://docs.yottabuild.org/#installing'\n else:\n return '%s is not installed' % (cmd[0])\n else:\n return 'command %s failed' % (cmd)\n if child.returncode:\n return 'command %s failed' % (cmd)", "response": "Execute the given command and return an error message if the command failed."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef build(self, builddir, component, args, release_build=False, build_args=None, targets=None,\n release_no_debug_info_build=False):\n ''' Execute the commands necessary to build this component, and all of\n its dependencies. '''\n if build_args is None:\n build_args = []\n if targets is None:\n targets = []\n # in the future this may be specified in the target description, but\n # for now we only support cmake, so everything is simple:\n if release_no_debug_info_build:\n build_type = 'Release'\n elif release_build:\n build_type = 'RelWithDebInfo'\n else:\n build_type = 'Debug'\n cmd = ['cmake', '-D', 'CMAKE_BUILD_TYPE=%s' % build_type, '-G', args.cmake_generator, '.']\n res = self.exec_helper(cmd, builddir)\n if res is not None:\n return res\n\n # work-around various yotta-specific issues with the generated\n # Ninja/project files:\n from yotta.lib import cmake_fixups\n cmake_fixups.applyFixupsForFenerator(args.cmake_generator, builddir, component)\n\n build_command = self.overrideBuildCommand(args.cmake_generator, targets=targets)\n if build_command:\n cmd = build_command + build_args\n else:\n cmd = ['cmake', '--build', builddir]\n if len(targets):\n # !!! FIXME: support multiple targets with the default CMake\n # build command\n cmd += ['--target', targets[0]]\n cmd += build_args\n res = self.exec_helper(cmd, builddir)\n if res is not None:\n return res\n hint = self.hintForCMakeGenerator(args.cmake_generator, component)\n if hint:\n logger.info(hint)", "response": "Execute the cmake build command and return the unique ID of the new component."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nfind the full path of a program in a given builddir. Returns None if no path is specified. Returns None if no path is found.", "response": "def findProgram(self, builddir, program):\n ''' Return the builddir-relative path of program, if only a partial\n path is specified. Returns None and logs an error message if the\n program is ambiguous or not found\n\t'''\n # if this is an exact match, do no further checking:\n if os.path.isfile(os.path.join(builddir, program)):\n logging.info('found %s' % program)\n return program\n exact_matches = []\n insensitive_matches = []\n approx_matches = []\n for path, dirs, files in os.walk(builddir):\n if program in files:\n exact_matches.append(os.path.relpath(os.path.join(path, program), builddir))\n continue\n files_lower = [f.lower() for f in files]\n if program.lower() in files_lower:\n insensitive_matches.append(\n os.path.relpath(\n os.path.join(path, files[files_lower.index(program.lower())]),\n builddir\n )\n )\n continue\n # !!! TODO: in the future add approximate string matching (typos,\n # etc.), for now we just test stripping any paths off program, and\n # looking for substring matches:\n pg_basen_lower_noext = os.path.splitext(os.path.basename(program).lower())[0]\n for f in files_lower:\n if pg_basen_lower_noext in f:\n approx_matches.append(\n os.path.relpath(\n os.path.join(path, files[files_lower.index(f)]),\n builddir\n )\n )\n\n if len(exact_matches) == 1:\n logging.info('found %s at %s', program, exact_matches[0])\n return exact_matches[0]\n elif len(exact_matches) > 1:\n logging.error(\n '%s matches multiple executables, please use a full path (one of %s)' % (\n program,\n ', or '.join(['\"'+os.path.join(m, program)+'\"' for m in exact_matches])\n )\n )\n return None\n # if we have matches with and without a file extension, prefer the\n # no-file extension version, and discard the others (so we avoid\n # picking up post-processed files):\n reduced_approx_matches = []\n for m in approx_matches:\n root = os.path.splitext(m)[0]\n if (m == root) or (root not in approx_matches):\n reduced_approx_matches.append(m)\n approx_matches = reduced_approx_matches\n\n for matches in (insensitive_matches, approx_matches):\n if len(matches) == 1:\n logging.info('found %s at %s' % (\n program, matches[0]\n ))\n return matches[0]\n elif len(matches) > 1:\n logging.error(\n '%s is similar to several executables found. Please use an exact name:\\n%s' % (\n program,\n '\\n'.join(matches)\n )\n )\n return None\n logging.error('could not find program \"%s\" to debug' % program)\n return None"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef start(self, builddir, program, forward_args):\n ''' Launch the specified program. Uses the `start` script if specified\n by the target, attempts to run it natively if that script is not\n defined.\n '''\n child = None\n try:\n prog_path = self.findProgram(builddir, program)\n if prog_path is None:\n return\n\n start_env, start_vars = self.buildProgEnvAndVars(prog_path, builddir)\n if self.getScript('start'):\n cmd = [\n os.path.expandvars(string.Template(x).safe_substitute(**start_vars))\n for x in self.getScript('start')\n ] + forward_args\n else:\n cmd = shlex.split('./' + prog_path) + forward_args\n\n logger.debug('starting program: %s', cmd)\n child = subprocess.Popen(\n cmd, cwd = builddir, env = start_env\n )\n child.wait()\n if child.returncode:\n return \"process exited with status %s\" % child.returncode\n child = None\n except OSError as e:\n import errno\n if e.errno == errno.ENOEXEC:\n return (\"the program %s cannot be run (perhaps your target \"+\n \"needs to define a 'start' script to start it on its \"\n \"intended execution target?)\") % prog_path\n finally:\n if child is not None:\n _tryTerminate(child)", "response": "Launches the specified program on the specified builddir."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef debug(self, builddir, program):\n ''' Launch a debugger for the specified program. Uses the `debug`\n script if specified by the target, falls back to the `debug` and\n `debugServer` commands if not. `program` is inserted into the\n $program variable in commands.\n '''\n try:\n signal.signal(signal.SIGINT, _ignoreSignal);\n if self.getScript('debug') is not None:\n return self._debugWithScript(builddir, program)\n elif 'debug' in self.description:\n logger.warning(\n 'target %s provides deprecated debug property. It should '+\n 'provide script.debug instead.', self.getName()\n\n )\n return self._debugDeprecated(builddir, program)\n else:\n return \"Target %s does not specify debug commands\" % self\n finally:\n # clear the sigint handler\n signal.signal(signal.SIGINT, signal.SIG_DFL);", "response": "Launch a debugger for the specified program."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef which(program):\n ''' look for \"program\" in PATH (respecting PATHEXT), and return the path to\n it, or None if it was not found\n '''\n # current directory / absolute paths:\n if os.path.exists(program) and os.access(program, os.X_OK):\n return program\n # PATH:\n for path in os.environ['PATH'].split(os.pathsep):\n # path variables may be quoted:\n path = path.strip('\"')\n for ext in os.environ.get('PATHEXT', '').split(os.pathsep):\n progpath = os.path.join(path, program + ext)\n if os.path.exists(progpath) and os.access(progpath, os.X_OK):\n return progpath\n # not found\n return None", "response": "look for program in PATH and return the path to\nTaxonomy it or None if it was not found"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef sometimesPruneCache(p):\n ''' return decorator to prune cache after calling fn with a probability of p'''\n def decorator(fn):\n @functools.wraps(fn)\n def wrapped(*args, **kwargs):\n r = fn(*args, **kwargs)\n if random.random() < p:\n pruneCache()\n return r\n return wrapped\n return decorator", "response": "return decorator to prune cache after calling fn with a probability of p"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef unpackFromCache(cache_key, to_directory):\n ''' If the specified cache key exists, unpack the tarball into the\n specified directory, otherwise raise NotInCache (a KeyError subclass).\n '''\n if cache_key is None:\n raise NotInCache('\"None\" is never in cache')\n\n cache_key = _encodeCacheKey(cache_key)\n\n cache_dir = folders.cacheDirectory()\n fsutils.mkDirP(cache_dir)\n path = os.path.join(cache_dir, cache_key)\n logger.debug('attempt to unpack from cache %s -> %s', path, to_directory)\n try:\n unpackFrom(path, to_directory)\n try:\n shutil.copy(path + '.json', os.path.join(to_directory, '.yotta_origin.json'))\n except IOError as e:\n if e.errno == errno.ENOENT:\n pass\n else:\n raise\n cache_logger.debug('unpacked %s from cache into %s', cache_key, to_directory)\n return\n except IOError as e:\n if e.errno == errno.ENOENT:\n cache_logger.debug('%s not in cache', cache_key)\n raise NotInCache('not in cache')\n except OSError as e:\n if e.errno == errno.ENOTEMPTY:\n logger.error('directory %s was not empty: probably simultaneous invocation of yotta! It is likely that downloaded sources are corrupted.')\n else:\n raise", "response": "Unpacks a tarball into a directory."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _downloadToCache(stream, hashinfo={}, origin_info=dict()):\n ''' Download the specified stream to a temporary cache directory, and\n returns a cache key that can be used to access/remove the file.\n You should use either removeFromCache(cache_key) or _moveCachedFile to\n move the downloaded file to a known key after downloading.\n '''\n hash_name = None\n hash_value = None\n m = None\n\n if len(hashinfo):\n # check for hashes in preferred order. Currently this is just sha256\n # (which the registry uses). Initial investigations suggest that github\n # doesn't return a header with the hash of the file being downloaded.\n for h in ('sha256',):\n if h in hashinfo:\n hash_name = h\n hash_value = hashinfo[h]\n m = getattr(hashlib, h)()\n break\n if not hash_name:\n logger.warning('could not find supported hash type in %s', hashinfo)\n\n cache_dir = folders.cacheDirectory()\n fsutils.mkDirP(cache_dir)\n file_size = 0\n\n (download_file, download_fname) = tempfile.mkstemp(dir=cache_dir, suffix='.locked')\n\n with os.fdopen(download_file, 'wb') as f:\n f.seek(0)\n for chunk in stream.iter_content(4096):\n f.write(chunk)\n if hash_name:\n m.update(chunk)\n\n if hash_name:\n calculated_hash = m.hexdigest()\n logger.debug(\n 'calculated %s hash: %s check against: %s' % (\n hash_name, calculated_hash, hash_value\n )\n )\n if hash_value and (hash_value != calculated_hash):\n raise Exception('Hash verification failed.')\n file_size = f.tell()\n logger.debug('wrote tarfile of size: %s to %s', file_size, download_fname)\n f.truncate()\n\n extended_origin_info = {\n 'hash': hashinfo,\n 'size': file_size\n }\n extended_origin_info.update(origin_info)\n ordered_json.dump(download_fname + '.json', extended_origin_info)\n return os.path.basename(download_fname)", "response": "Download the specified stream to a temporary cache directory and return a cache key that can be used to access the file."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nmoves a file atomically within the cache.", "response": "def _moveCachedFile(from_key, to_key):\n ''' Move a file atomically within the cache: used to make cached files\n available at known keys, so they can be used by other processes.\n '''\n cache_dir = folders.cacheDirectory()\n from_path = os.path.join(cache_dir, from_key)\n to_path = os.path.join(cache_dir, to_key)\n try:\n os.rename(from_path, to_path)\n # if moving the actual file was successful, then try to move the\n # metadata:\n os.rename(from_path+'.json', to_path+'.json')\n except Exception as e:\n # if the source doesn't exist, or the destination doesn't exist, remove\n # the file instead.\n # windows error 183 == file already exists\n # (be careful not to use WindowsError on non-windows platforms as it\n # isn't defined)\n if (isinstance(e, OSError) and e.errno == errno.ENOENT) or \\\n (isinstance(e, getattr(__builtins__, \"WindowsError\", type(None))) and e.errno == 183):\n fsutils.rmF(from_path)\n else:\n raise"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nunpack a responses stream that contains a tarball into a directory.", "response": "def unpackTarballStream(stream, into_directory, hash={}, cache_key=None, origin_info=dict()):\n ''' Unpack a responses stream that contains a tarball into a directory. If\n a hash is provided, then it will be used as a cache key (for future\n requests you can try to retrieve the key value from the cache first,\n before making the request)\n '''\n cache_key = _encodeCacheKey(cache_key)\n\n # if the cache is disabled, then use a random cache key even if one was\n # provided, so that the module is not persisted in the cache and its\n # temporary download location is a random key:\n if getMaxCachedModules() == 0:\n cache_key = None\n\n new_cache_key = _downloadToCache(stream, hash, origin_info)\n unpackFromCache(new_cache_key, into_directory)\n\n if cache_key is None:\n # if we didn't provide a cache key, there's no point in storing the cache\n removeFromCache(new_cache_key)\n else:\n # otherwise make this file available at the known cache key\n _moveCachedFile(new_cache_key, cache_key)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nparse the specified version source URL or version spec and return an an instance of VersionSource", "response": "def parseSourceURL(source_url):\n ''' Parse the specified version source URL (or version spec), and return an\n instance of VersionSource\n '''\n name, spec = _getNonRegistryRef(source_url)\n if spec:\n return spec\n\n try:\n url_is_spec = version.Spec(source_url)\n except ValueError:\n url_is_spec = None\n\n if url_is_spec is not None:\n # if the url is an unadorned version specification (including an empty\n # string) then the source is the module registry:\n return VersionSource('registry', '', source_url)\n\n raise InvalidVersionSpec(\"Invalid version specification: \\\"%s\\\"\" % (source_url))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nparse a targetname and versionspec and return a tuple containing the name and versionspec.", "response": "def parseTargetNameAndSpec(target_name_and_spec):\n ''' Parse targetname[@versionspec] and return a tuple\n (target_name_string, version_spec_string).\n\n targetname[,versionspec] is also supported (this is how target names\n and specifications are stored internally, and was the documented way of\n setting the spec on the commandline)\n\n Also accepts raw github version specs (Owner/reponame#whatever), as the\n name can be deduced from these.\n\n Note that the specification split from the name is not validated. If\n there is no specification (just a target name) passed in, then '*' will\n be returned as the specification.\n '''\n import re\n # fist check if this is a raw github specification that we can get the\n # target name from:\n name, spec = _getNonRegistryRef(target_name_and_spec)\n if name:\n return name, target_name_and_spec\n\n # next split at the first @ or , if any\n split_at = '@'\n if target_name_and_spec.find('@') > target_name_and_spec.find(',') and \\\n ',' in target_name_and_spec:\n split_at = ','\n name = target_name_and_spec.split(split_at)[0]\n spec = target_name_and_spec[len(name)+1:]\n\n name = name.strip()\n\n # if there's no specification, return the explicit any-version\n # specification:\n if not spec:\n spec = '*'\n\n return name, spec"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nparsing modulename and versionspec and return a tuple", "response": "def parseModuleNameAndSpec(module_name_and_spec):\n ''' Parse modulename[@versionspec] and return a tuple\n (module_name_string, version_spec_string).\n\n Also accepts raw github version specs (Owner/reponame#whatever), as the\n name can be deduced from these.\n\n Note that the specification split from the name is not validated. If\n there is no specification (just a module name) passed in, then '*' will\n be returned as the specification.\n '''\n import re\n # fist check if this is a raw github specification that we can get the\n # module name from:\n name, spec = _getNonRegistryRef(module_name_and_spec)\n if name:\n return name, module_name_and_spec\n\n # next split at the first @, if any\n name = module_name_and_spec.split('@')[0]\n spec = module_name_and_spec[len(name)+1:]\n\n name = name.strip()\n\n # if there's no specification, return the explicit any-version\n # specification:\n if not spec:\n spec = '*'\n\n return name, spec"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef gfit(X, sigma, p=5, nbin=200, unif_fraction=0.1):\n min_x = min(min(X) - 2 * np.std(X, ddof=1), 0)\n max_x = max(max(X) + 2 * np.std(X, ddof=1),\n np.std(X, ddof=1))\n xvals = np.linspace(min_x, max_x, nbin)\n binw = (max_x - min_x) / (nbin - 1)\n\n zero_idx = max(np.where(xvals <= 0)[0])\n noise_kernel = norm().pdf(xvals / sigma) * binw / sigma\n\n if zero_idx > 0:\n noise_rotate = noise_kernel[list(np.arange(zero_idx, len(xvals))) +\n list(np.arange(0, zero_idx))]\n else:\n noise_rotate = noise_kernel\n\n XX = np.zeros((p, len(xvals)), dtype=np.float)\n for ind, exp in enumerate(range(1, p+1)):\n mask = np.ones_like(xvals)\n mask[np.where(xvals <= 0)[0]] = 0\n XX[ind, :] = pow(xvals, exp) * mask\n XX = XX.T\n\n def neg_loglik(eta):\n mask = np.ones_like(xvals)\n mask[np.where(xvals <= 0)[0]] = 0\n g_eta_raw = np.exp(np.dot(XX, eta)) * mask\n if ((np.sum(g_eta_raw) == np.inf) |\n (np.sum(g_eta_raw) <=\n 100 * np.finfo(np.double).tiny)):\n return (1000 * (len(X) + sum(eta ** 2)))\n\n g_eta_main = g_eta_raw / sum(g_eta_raw)\n g_eta = ((1 - unif_fraction) * g_eta_main +\n unif_fraction * mask / sum(mask))\n f_eta = fftconvolve(g_eta, noise_rotate, mode='same')\n return np.sum(np.interp(X, xvals,\n -np.log(np.maximum(f_eta, 0.0000001))))\n\n eta_hat = minimize(neg_loglik,\n list(itertools.repeat(-1, p))).x\n g_eta_raw = np.exp(np.dot(XX, eta_hat)) * mask\n g_eta_main = g_eta_raw / sum(g_eta_raw)\n g_eta = ((1 - unif_fraction) * g_eta_main +\n unif_fraction * mask) / sum(mask)\n\n return xvals, g_eta", "response": "Fit empirical Bayes prior in the hierarchical model."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nestimating Bayes posterior with Gaussian noise", "response": "def gbayes(x0, g_est, sigma):\n \"\"\"\n Estimate Bayes posterior with Gaussian noise [Efron2014]_.\n\n Parameters\n ----------\n x0: ndarray\n an observation\n g_est: float\n a prior density, as returned by gfit\n sigma: int\n noise estimate\n\n Returns\n -------\n An array of the posterior estimate E[mu | x0]\n \"\"\"\n\n Kx = norm().pdf((g_est[0] - x0) / sigma)\n post = Kx * g_est[1]\n post /= sum(post)\n return sum(post * g_est[0])"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef calibrateEB(variances, sigma2):\n if (sigma2 <= 0 or min(variances) == max(variances)):\n return(np.maximum(variances, 0))\n sigma = np.sqrt(sigma2)\n eb_prior = gfit(variances, sigma)\n # Set up a partial execution of the function\n part = functools.partial(gbayes, g_est=eb_prior,\n sigma=sigma)\n if len(variances) >= 200:\n # Interpolate to speed up computations:\n calib_x = np.percentile(variances,\n np.arange(0, 102, 2))\n calib_y = list(map(part, calib_x))\n calib_all = np.interp(variances, calib_x, calib_y)\n else:\n calib_all = list(map(part, variances))\n\n return np.asarray(calib_all)", "response": "Calibrate noisy variance estimates with empirical Bayes."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef calc_inbag(n_samples, forest):\n\n if not forest.bootstrap:\n e_s = \"Cannot calculate the inbag from a forest that has \"\n e_s = \" bootstrap=False\"\n raise ValueError(e_s)\n\n n_trees = forest.n_estimators\n inbag = np.zeros((n_samples, n_trees))\n sample_idx = []\n for t_idx in range(n_trees):\n sample_idx.append(\n _generate_sample_indices(forest.estimators_[t_idx].random_state,\n n_samples))\n inbag[:, t_idx] = np.bincount(sample_idx[-1], minlength=n_samples)\n return inbag", "response": "Calculates the inbag of a random tree from a random forest."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _core_computation(X_train, X_test, inbag, pred_centered, n_trees,\n memory_constrained=False, memory_limit=None,\n test_mode=False):\n \"\"\"\n Helper function, that performs the core computation\n\n Parameters\n ----------\n X_train : ndarray\n An array with shape (n_train_sample, n_features).\n\n X_test : ndarray\n An array with shape (n_test_sample, n_features).\n\n inbag : ndarray\n The inbag matrix that fit the data. If set to `None` (default) it\n will be inferred from the forest. However, this only works for trees\n for which bootstrapping was set to `True`. That is, if sampling was\n done with replacement. Otherwise, users need to provide their own\n inbag matrix.\n\n pred_centered : ndarray\n Centered predictions that are an intermediate result in the\n computation.\n\n memory_constrained: boolean (optional)\n Whether or not there is a restriction on memory. If False, it is\n assumed that a ndarry of shape (n_train_sample,n_test_sample) fits\n in main memory. Setting to True can actually provide a speed up if\n memory_limit is tuned to the optimal range.\n\n memory_limit: int (optional)\n An upper bound for how much memory the itermediate matrices will take\n up in Megabytes. This must be provided if memory_constrained=True.\n\n\n \"\"\"\n if not memory_constrained:\n return np.sum((np.dot(inbag - 1, pred_centered.T) / n_trees) ** 2, 0)\n\n if not memory_limit:\n raise ValueError('If memory_constrained=True, must provide',\n 'memory_limit.')\n\n # Assumes double precision float\n chunk_size = int((memory_limit * 1e6) / (8.0 * X_train.shape[0]))\n\n if chunk_size == 0:\n min_limit = 8.0 * X_train.shape[0] / 1e6\n raise ValueError('memory_limit provided is too small.' +\n 'For these dimensions, memory_limit must ' +\n 'be greater than or equal to %.3e' % min_limit)\n\n chunk_edges = np.arange(0, X_test.shape[0] + chunk_size, chunk_size)\n inds = range(X_test.shape[0])\n chunks = [inds[chunk_edges[i]:chunk_edges[i+1]]\n for i in range(len(chunk_edges)-1)]\n if test_mode:\n print('Number of chunks: %d' % (len(chunks),))\n V_IJ = np.concatenate([\n np.sum((np.dot(inbag-1, pred_centered[chunk].T)/n_trees)**2, 0)\n for chunk in chunks])\n return V_IJ", "response": "Helper function that performs the core computation of the forests."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncalculates the error bars for a RandomForest object.", "response": "def random_forest_error(forest, X_train, X_test, inbag=None,\n calibrate=True, memory_constrained=False,\n memory_limit=None):\n \"\"\"\n Calculate error bars from scikit-learn RandomForest estimators.\n\n RandomForest is a regressor or classifier object\n this variance can be used to plot error bars for RandomForest objects\n\n Parameters\n ----------\n forest : RandomForest\n Regressor or Classifier object.\n\n X_train : ndarray\n An array with shape (n_train_sample, n_features). The design matrix for\n training data.\n\n X_test : ndarray\n An array with shape (n_test_sample, n_features). The design matrix\n for testing data\n\n inbag : ndarray, optional\n The inbag matrix that fit the data. If set to `None` (default) it\n will be inferred from the forest. However, this only works for trees\n for which bootstrapping was set to `True`. That is, if sampling was\n done with replacement. Otherwise, users need to provide their own\n inbag matrix.\n\n calibrate: boolean, optional\n Whether to apply calibration to mitigate Monte Carlo noise.\n Some variance estimates may be negative due to Monte Carlo effects if\n the number of trees in the forest is too small. To use calibration,\n Default: True\n\n memory_constrained: boolean, optional\n Whether or not there is a restriction on memory. If False, it is\n assumed that a ndarry of shape (n_train_sample,n_test_sample) fits\n in main memory. Setting to True can actually provide a speed up if\n memory_limit is tuned to the optimal range.\n\n memory_limit: int, optional.\n An upper bound for how much memory the itermediate matrices will take\n up in Megabytes. This must be provided if memory_constrained=True.\n\n Returns\n -------\n An array with the unbiased sampling variance (V_IJ_unbiased)\n for a RandomForest object.\n\n See Also\n ----------\n :func:`calc_inbag`\n\n Notes\n -----\n The calculation of error is based on the infinitesimal jackknife variance,\n as described in [Wager2014]_ and is a Python implementation of the R code\n provided at: https://github.com/swager/randomForestCI\n\n .. [Wager2014] S. Wager, T. Hastie, B. Efron. \"Confidence Intervals for\n Random Forests: The Jackknife and the Infinitesimal Jackknife\", Journal\n of Machine Learning Research vol. 15, pp. 1625-1651, 2014.\n \"\"\"\n if inbag is None:\n inbag = calc_inbag(X_train.shape[0], forest)\n\n pred = np.array([tree.predict(X_test) for tree in forest]).T\n pred_mean = np.mean(pred, 0)\n pred_centered = pred - pred_mean\n n_trees = forest.n_estimators\n V_IJ = _core_computation(X_train, X_test, inbag, pred_centered, n_trees,\n memory_constrained, memory_limit)\n V_IJ_unbiased = _bias_correction(V_IJ, inbag, pred_centered, n_trees)\n\n # Correct for cases where resampling is done without replacement:\n if np.max(inbag) == 1:\n variance_inflation = 1 / (1 - np.mean(inbag)) ** 2\n V_IJ_unbiased *= variance_inflation\n\n if not calibrate:\n return V_IJ_unbiased\n\n if V_IJ_unbiased.shape[0] <= 20:\n print(\"No calibration with n_samples <= 20\")\n return V_IJ_unbiased\n if calibrate:\n\n calibration_ratio = 2\n n_sample = np.ceil(n_trees / calibration_ratio)\n new_forest = copy.deepcopy(forest)\n new_forest.estimators_ =\\\n np.random.permutation(new_forest.estimators_)[:int(n_sample)]\n new_forest.n_estimators = int(n_sample)\n\n results_ss = random_forest_error(new_forest, X_train, X_test,\n calibrate=False,\n memory_constrained=memory_constrained,\n memory_limit=memory_limit)\n # Use this second set of variance estimates\n # to estimate scale of Monte Carlo noise\n sigma2_ss = np.mean((results_ss - V_IJ_unbiased)**2)\n delta = n_sample / n_trees\n sigma2 = (delta**2 + (1 - delta)**2) / (2 * (1 - delta)**2) * sigma2_ss\n\n # Use Monte Carlo noise scale estimate for empirical Bayes calibration\n V_IJ_calibrated = calibrateEB(V_IJ_unbiased, sigma2)\n\n return V_IJ_calibrated"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef generate_self_signed_certificate(self, domain='', r=None):\n r = self.local_renderer\n r.env.domain = domain or r.env.domain\n assert r.env.domain, 'No SSL domain defined.'\n role = r or self.genv.ROLE or ALL\n ssl_dst = 'roles/%s/ssl' % (role,)\n if not os.path.isdir(ssl_dst):\n os.makedirs(ssl_dst)\n r.env.base_dst = '%s/%s' % (ssl_dst, r.env.domain)\n r.local('openssl req -new -newkey rsa:{ssl_length} '\n '-days {ssl_days} -nodes -x509 '\n '-subj \"/C={ssl_country}/ST={ssl_state}/L={ssl_city}/O={ssl_organization}/CN={ssl_domain}\" '\n '-keyout {ssl_base_dst}.key -out {ssl_base_dst}.crt')", "response": "Generate a self - signed certificate for use in an internal development ArcGIS load balancer."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef generate_csr(self, domain='', r=None):\n r = r or self.local_renderer\n r.env.domain = domain or r.env.domain\n role = self.genv.ROLE or ALL\n site = self.genv.SITE or self.genv.default_site\n print('self.genv.default_site:', self.genv.default_site, file=sys.stderr)\n print('site.csr0:', site, file=sys.stderr)\n ssl_dst = 'roles/%s/ssl' % (role,)\n print('ssl_dst:', ssl_dst)\n if not os.path.isdir(ssl_dst):\n os.makedirs(ssl_dst)\n for site, site_data in self.iter_sites():\n print('site.csr1:', site, file=sys.stderr)\n assert r.env.domain, 'No SSL domain defined.'\n r.env.ssl_base_dst = '%s/%s' % (ssl_dst, r.env.domain.replace('*.', ''))\n r.env.ssl_csr_year = date.today().year\n r.local('openssl req -nodes -newkey rsa:{ssl_length} '\n '-subj \"/C={ssl_country}/ST={ssl_state}/L={ssl_city}/O={ssl_organization}/CN={ssl_domain}\" '\n '-keyout {ssl_base_dst}.{ssl_csr_year}.key -out {ssl_base_dst}.{ssl_csr_year}.csr')", "response": "Generate a certificate signing request for the current server."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_expiration_date(self, fn):\n r = self.local_renderer\n r.env.crt_fn = fn\n with hide('running'):\n ret = r.local('openssl x509 -noout -in {ssl_crt_fn} -dates', capture=True)\n matches = re.findall('notAfter=(.*?)$', ret, flags=re.IGNORECASE)\n if matches:\n return dateutil.parser.parse(matches[0])", "response": "Reads the expiration date of a local crt file."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nprints out the expiration dates for all local. crt files and their expiration dates.", "response": "def list_expiration_dates(self, base='roles/all/ssl'):\n \"\"\"\n Scans through all local .crt files and displays the expiration dates.\n \"\"\"\n max_fn_len = 0\n max_date_len = 0\n data = []\n for fn in os.listdir(base):\n fqfn = os.path.join(base, fn)\n if not os.path.isfile(fqfn):\n continue\n if not fn.endswith('.crt'):\n continue\n expiration_date = self.get_expiration_date(fqfn)\n max_fn_len = max(max_fn_len, len(fn))\n max_date_len = max(max_date_len, len(str(expiration_date)))\n data.append((fn, expiration_date))\n print('%s %s %s' % ('Filename'.ljust(max_fn_len), 'Expiration Date'.ljust(max_date_len), 'Expired'))\n now = datetime.now().replace(tzinfo=pytz.UTC)\n for fn, dt in sorted(data):\n\n if dt is None:\n expired = '?'\n elif dt < now:\n expired = 'YES'\n else:\n expired = 'NO'\n print('%s %s %s' % (fn.ljust(max_fn_len), str(dt).ljust(max_date_len), expired))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nverifying the certificate chain.", "response": "def verify_certificate_chain(self, base=None, crt=None, csr=None, key=None):\n \"\"\"\n Confirms the key, CSR, and certificate files all match.\n \"\"\"\n from burlap.common import get_verbose, print_fail, print_success\n\n r = self.local_renderer\n\n if base:\n crt = base + '.crt'\n csr = base + '.csr'\n key = base + '.key'\n else:\n assert crt and csr and key, 'If base not provided, crt and csr and key must be given.'\n\n assert os.path.isfile(crt)\n assert os.path.isfile(csr)\n assert os.path.isfile(key)\n\n csr_md5 = r.local('openssl req -noout -modulus -in %s | openssl md5' % csr, capture=True)\n key_md5 = r.local('openssl rsa -noout -modulus -in %s | openssl md5' % key, capture=True)\n crt_md5 = r.local('openssl x509 -noout -modulus -in %s | openssl md5' % crt, capture=True)\n\n match = crt_md5 == csr_md5 == key_md5\n\n if self.verbose or not match:\n print('crt:', crt_md5)\n print('csr:', csr_md5)\n print('key:', key_md5)\n\n if match:\n print_success('Files look good!')\n else:\n print_fail('Files no not match!')\n raise Exception('Files no not match!')"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _get_environ_handler(name, d):\n\n def func(site=None, **kwargs):\n from fabric import state\n\n # We can't auto-set default_site, because that break tasks that have\n # to operate over multiple sites.\n # If a task requires a site, it can pull from default_site as needed.\n #site = site or d.get('default_site') or env.SITE\n\n BURLAP_SHELL_PREFIX = int(os.environ.get('BURLAP_SHELL_PREFIX', '0'))\n if BURLAP_SHELL_PREFIX:\n print('#!/bin/bash')\n print('# Generated with:')\n print('#')\n print('# export BURLAP_SHELL_PREFIX=1; export BURLAP_COMMAND_PREFIX=0; fab %s' % (' '.join(sys.argv[1:]),))\n print('#')\n\n BURLAP_COMMAND_PREFIX = int(os.environ.get('BURLAP_COMMAND_PREFIX', '1'))\n with_args = []\n if not BURLAP_COMMAND_PREFIX:\n for k in state.output:\n state.output[k] = False\n\n hostname = kwargs.get('hostname')\n hostname = hostname or kwargs.get('name')\n hostname = hostname or kwargs.get('hn')\n hostname = hostname or kwargs.get('h')\n\n verbose = int(kwargs.get('verbose', '0'))\n common.set_verbose(verbose)\n\n # Load environment for current role.\n env.update(env_default)\n env[common.ROLE] = os.environ[common.ROLE] = name\n if site:\n env[common.SITE] = os.environ[common.SITE] = site\n env.update(d)\n\n # Load host retriever.\n retriever = None\n if env.hosts_retriever:\n # Dynamically retrieve hosts.\n# module_name = '.'.join(env.hosts_retriever.split('.')[:-1])\n# func_name = env.hosts_retriever.split('.')[-1]\n# retriever = getattr(importlib.import_module(module_name), func_name)\n retriever = common.get_hosts_retriever()\n if verbose:\n print('Using retriever:', env.hosts_retriever, retriever)\n\n # Load host translator.\n translator = None\n if hostname:\n # Filter hosts list by a specific host name.\n module_name = '.'.join(env.hostname_translator.split('.')[:-1])\n func_name = env.hostname_translator.split('.')[-1]\n translator = getattr(importlib.import_module(module_name), func_name)\n\n # Re-load environment for current role, incase loading\n # the retriever/translator reset some environment values.\n env.update(env_default)\n env[common.ROLE] = os.environ[common.ROLE] = name\n if site:\n env[common.SITE] = os.environ[common.SITE] = site\n env.update(d)\n\n # Dynamically retrieve hosts.\n if env.hosts_retriever:\n if verbose:\n print('Building host list with retriever %s...' % env.hosts_retriever)\n env.hosts = list(retriever(site=site))\n if verbose:\n print('Found hosts:')\n print(env.hosts)\n\n # Filter hosts list by a specific host name.\n if hostname:\n _hostname = hostname\n hostname = translator(hostname=hostname)\n _hosts = env.hosts\n env.hosts = [_ for _ in env.hosts if _ == hostname]\n assert env.hosts, 'Hostname %s does not match any known hosts.' % (_hostname,)\n\n if env.is_local is None:\n if env.hosts:\n env.is_local = 'localhost' in env.hosts or '127.0.0.1' in env.hosts\n elif env.host_string:\n env.is_local = 'localhost' in env.host_string or '127.0.0.1' in env.host_string\n\n for cb in common.post_role_load_callbacks:\n cb()\n\n # Ensure satchels don't cache values from previously loaded roles.\n common.reset_all_satchels()\n\n if env.hosts and not env.host_string:\n env.host_string = env.hosts[0]\n\n if verbose:\n print('Loaded role %s.' % (name,), file=sys.stderr)\n\n func.__doc__ = 'Sets enivronment variables for the \"%s\" role.' % (name,)\n return func", "response": "Dynamically creates a Fabric task for each configuration role."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef update_merge(d, u):\n import collections\n for k, v in u.items():\n if isinstance(v, collections.Mapping):\n r = update_merge(d.get(k, dict()), v)\n d[k] = r\n else:\n d[k] = u[k]\n return d", "response": "Recursively merges two nested dictionary u into a single dictionary d."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef check_version():\n global CHECK_VERSION\n if not CHECK_VERSION:\n return\n # Ensure we only check once in this process.\n CHECK_VERSION = 0\n # Lookup most recent remote version.\n from six.moves.urllib.request import urlopen\n try:\n response = urlopen(\"https://pypi.org/pypi/burlap/json\")\n data = json.loads(response.read().decode())\n remote_release = sorted(tuple(map(int, _.split('.'))) for _ in data['releases'].keys())[-1]\n remote_release_str = '.'.join(map(str, remote_release))\n local_release = VERSION\n local_release_str = '.'.join(map(str, local_release))\n # Display warning.\n if remote_release > local_release:\n print('\\033[93m')\n print(\"You are using burlap version %s, however version %s is available.\" % (local_release_str, remote_release_str))\n print(\"You should consider upgrading via the 'pip install --upgrade burlap' command.\")\n print('\\033[0m')\n except Exception as exc:\n print('\\033[93m')\n print(\"Unable to check for updated burlap version: %s\" % exc)\n print('\\033[0m')", "response": "Checks the local version of the burlap server and displays a warning message if the latest version is available on PyPI."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef task_or_dryrun(*args, **kwargs):\n invoked = bool(not args or kwargs)\n task_class = kwargs.pop(\"task_class\", WrappedCallableTask)\n# if invoked:\n# func, args = args[0], ()\n# else:\n func, args = args[0], ()\n\n def wrapper(func):\n return task_class(func, *args, **kwargs)\n wrapper.is_task_or_dryrun = True\n wrapper.wrapped = func\n\n return wrapper if invoked else wrapper(func)", "response": "Decorator for creating a new - style task."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef runs_once(meth):\n from burlap.common import get_dryrun, runs_once_methods\n if get_dryrun():\n pass\n else:\n runs_once_methods.append(meth)\n _runs_once(meth)\n return meth", "response": "A wrapper around Fabric s runs_once method to support dryrun feature."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef is_file(self, path, use_sudo=False):\n if self.is_local and not use_sudo:\n return os.path.isfile(path)\n else:\n func = use_sudo and _sudo or _run\n with self.settings(hide('running', 'warnings'), warn_only=True):\n return func('[ -f \"%(path)s\" ]' % locals()).succeeded", "response": "Check if a path exists and is a file."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncheck if a path exists and is a directory.", "response": "def is_dir(self, path, use_sudo=False):\n \"\"\"\n Check if a path exists, and is a directory.\n \"\"\"\n if self.is_local and not use_sudo:\n return os.path.isdir(path)\n else:\n func = use_sudo and _sudo or _run\n with self.settings(hide('running', 'warnings'), warn_only=True):\n return func('[ -d \"%(path)s\" ]' % locals()).succeeded"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef is_link(self, path, use_sudo=False):\n func = use_sudo and _sudo or _run\n with self.settings(hide('running', 'warnings'), warn_only=True):\n return func('[ -L \"%(path)s\" ]' % locals()).succeeded", "response": "Check if a path exists and is a symbolic link."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nget the owner name of a file or directory.", "response": "def get_owner(self, path, use_sudo=False):\n \"\"\"\n Get the owner name of a file or directory.\n \"\"\"\n func = use_sudo and run_as_root or self.run\n # I'd prefer to use quiet=True, but that's not supported with older\n # versions of Fabric.\n with self.settings(hide('running', 'stdout'), warn_only=True):\n result = func('stat -c %%U \"%(path)s\"' % locals())\n if result.failed and 'stat: illegal option' in result:\n # Try the BSD version of stat\n return func('stat -f %%Su \"%(path)s\"' % locals())\n return result"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef umask(self, use_sudo=False):\n func = use_sudo and run_as_root or self.run\n return func('umask')", "response": "Get the user s umask."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nupload a template file to the specified destination.", "response": "def upload_template(self, filename, destination, context=None, use_jinja=False,\n template_dir=None, use_sudo=False, backup=True,\n mirror_local_mode=False, mode=None,\n mkdir=False, chown=False, user=None):\n \"\"\"\n Upload a template file.\n\n This is a wrapper around :func:`fabric.contrib.files.upload_template`\n that adds some extra parameters.\n\n If ``mkdir`` is True, then the remote directory will be created, as\n the current user or as ``user`` if specified.\n\n If ``chown`` is True, then it will ensure that the current user (or\n ``user`` if specified) is the owner of the remote file.\n \"\"\"\n\n if mkdir:\n remote_dir = os.path.dirname(destination)\n if use_sudo:\n self.sudo('mkdir -p %s' % quote(remote_dir), user=user)\n else:\n self.run('mkdir -p %s' % quote(remote_dir))\n\n if not self.dryrun:\n _upload_template(\n filename=filename,\n destination=destination,\n context=context,\n use_jinja=use_jinja,\n template_dir=template_dir,\n use_sudo=use_sudo,\n backup=backup,\n mirror_local_mode=mirror_local_mode,\n mode=mode,\n )\n\n if chown:\n if user is None:\n user = self.genv.user\n run_as_root('chown %s: %s' % (user, quote(destination)))"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef md5sum(self, filename, use_sudo=False):\n func = use_sudo and run_as_root or self.run\n with self.settings(hide('running', 'stdout', 'stderr', 'warnings'), warn_only=True):\n # Linux (LSB)\n if exists(u'/usr/bin/md5sum'):\n res = func(u'/usr/bin/md5sum %(filename)s' % locals())\n # BSD / OS X\n elif exists(u'/sbin/md5'):\n res = func(u'/sbin/md5 -r %(filename)s' % locals())\n # SmartOS Joyent build\n elif exists(u'/opt/local/gnu/bin/md5sum'):\n res = func(u'/opt/local/gnu/bin/md5sum %(filename)s' % locals())\n # SmartOS Joyent build\n # (the former doesn't exist, at least on joyent_20130222T000747Z)\n elif exists(u'/opt/local/bin/md5sum'):\n res = func(u'/opt/local/bin/md5sum %(filename)s' % locals())\n # Try to find ``md5sum`` or ``md5`` on ``$PATH`` or abort\n else:\n md5sum = func(u'which md5sum')\n md5 = func(u'which md5')\n if exists(md5sum):\n res = func('%(md5sum)s %(filename)s' % locals())\n elif exists(md5):\n res = func('%(md5)s %(filename)s' % locals())\n else:\n abort('No MD5 utility was found on this system.')\n\n if res.succeeded:\n _md5sum = res\n else:\n warn(res)\n _md5sum = None\n\n if isinstance(_md5sum, six.string_types):\n _md5sum = _md5sum.strip().split('\\n')[-1].split()[0]\n\n return _md5sum", "response": "Compute the MD5 sum of a file."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef uncommented_lines(self, filename, use_sudo=False):\n func = run_as_root if use_sudo else self.run\n res = func('cat %s' % quote(filename), quiet=True)\n if res.succeeded:\n return [line for line in res.splitlines() if line and not line.startswith('#')]\n return []", "response": "Get the lines of a remote file ignoring empty or commented ones\n "} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef getmtime(self, path, use_sudo=False):\n func = use_sudo and run_as_root or self.run\n with self.settings(hide('running', 'stdout')):\n return int(func('stat -c %%Y \"%(path)s\" ' % locals()).strip())", "response": "Get the time of last modification of a file or directory."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef copy(self, source, destination, recursive=False, use_sudo=False):\n func = use_sudo and run_as_root or self.run\n options = '-r ' if recursive else ''\n func('/bin/cp {0}{1} {2}'.format(options, quote(source), quote(destination)))", "response": "Copy a file or directory to a new location"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nmoves a file or directory to another location", "response": "def move(self, source, destination, use_sudo=False):\n \"\"\"\n Move a file or directory\n \"\"\"\n func = use_sudo and run_as_root or self.run\n func('/bin/mv {0} {1}'.format(quote(source), quote(destination)))"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef remove(self, path, recursive=False, use_sudo=False):\n func = use_sudo and run_as_root or self.run\n options = '-r ' if recursive else ''\n func('/bin/rm {0}{1}'.format(options, quote(path)))", "response": "Remove a file or directory from the cache"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef require(self, path=None, contents=None, source=None, url=None, md5=None,\n use_sudo=False, owner=None, group='', mode=None, verify_remote=True,\n temp_dir='/tmp'):\n \"\"\"\n Require a file to exist and have specific contents and properties.\n\n You can provide either:\n\n - *contents*: the required contents of the file::\n\n from fabtools import require\n\n require.file('/tmp/hello.txt', contents='Hello, world')\n\n - *source*: the local path of a file to upload::\n\n from fabtools import require\n\n require.file('/tmp/hello.txt', source='files/hello.txt')\n\n - *url*: the URL of a file to download (*path* is then optional)::\n\n from fabric.api import cd\n from fabtools import require\n\n with cd('tmp'):\n require.file(url='http://example.com/files/hello.txt')\n\n If *verify_remote* is ``True`` (the default), then an MD5 comparison\n will be used to check whether the remote file is the same as the\n source. If this is ``False``, the file will be assumed to be the\n same if it is present. This is useful for very large files, where\n generating an MD5 sum may take a while.\n\n When providing either the *contents* or the *source* parameter, Fabric's\n ``put`` function will be used to upload the file to the remote host.\n When ``use_sudo`` is ``True``, the file will first be uploaded to a temporary\n directory, then moved to its final location. The default temporary\n directory is ``/tmp``, but can be overridden with the *temp_dir* parameter.\n If *temp_dir* is an empty string, then the user's home directory will\n be used.\n\n If `use_sudo` is `True`, then the remote file will be owned by root,\n and its mode will reflect root's default *umask*. The optional *owner*,\n *group* and *mode* parameters can be used to override these properties.\n\n .. note:: This function can be accessed directly from the\n ``fabtools.require`` module for convenience.\n\n \"\"\"\n func = use_sudo and run_as_root or self.run\n\n # 1) Only a path is given\n if path and not (contents or source or url):\n assert path\n if not self.is_file(path):\n func('touch \"%(path)s\"' % locals())\n\n # 2) A URL is specified (path is optional)\n elif url:\n if not path:\n path = os.path.basename(urlparse(url).path)\n\n if not self.is_file(path) or md5 and self.md5sum(path) != md5:\n func('wget --progress=dot:mega \"%(url)s\" -O \"%(path)s\"' % locals())\n\n # 3) A local filename, or a content string, is specified\n else:\n if source:\n assert not contents\n t = None\n else:\n fd, source = mkstemp()\n t = os.fdopen(fd, 'w')\n t.write(contents)\n t.close()\n\n if verify_remote:\n # Avoid reading the whole file into memory at once\n digest = hashlib.md5()\n f = open(source, 'rb')\n try:\n while True:\n d = f.read(BLOCKSIZE)\n if not d:\n break\n digest.update(d)\n finally:\n f.close()\n else:\n digest = None\n\n if (not self.is_file(path, use_sudo=use_sudo) or\n (verify_remote and\n self.md5sum(path, use_sudo=use_sudo) != digest.hexdigest())):\n with self.settings(hide('running')):\n self.put(local_path=source, remote_path=path, use_sudo=use_sudo, temp_dir=temp_dir)\n\n if t is not None:\n os.unlink(source)\n\n # Ensure correct owner\n if use_sudo and owner is None:\n owner = 'root'\n if (owner and self.get_owner(path, use_sudo) != owner) or \\\n (group and self.get_group(path, use_sudo) != group):\n func('chown %(owner)s:%(group)s \"%(path)s\"' % locals())\n\n # Ensure correct mode\n if use_sudo and mode is None:\n mode = oct(0o666 & ~int(self.umask(use_sudo=True), base=8))\n if mode and self.get_mode(path, use_sudo) != mode:\n func('chmod %(mode)s \"%(path)s\"' % locals())", "response": "This function requires a file to exist and has specific contents and properties."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef check_for_change(self):\n r = self.local_renderer\n lm = self.last_manifest\n last_fingerprint = lm.fingerprint\n current_fingerprint = self.get_target_geckodriver_version_number()\n self.vprint('last_fingerprint:', last_fingerprint)\n self.vprint('current_fingerprint:', current_fingerprint)\n if last_fingerprint != current_fingerprint:\n print('A new release is available. %s' % self.get_most_recent_version())\n return True\n print('No updates found.')\n return False", "response": "Determines if a new release has been made."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn a dict that can be used to record any data necessary to detect changes to the current version of the geckodriver.", "response": "def record_manifest(self):\n \"\"\"\n Called after a deployment to record any data necessary to detect changes\n for a future deployment.\n \"\"\"\n manifest = super(SeleniumSatchel, self).record_manifest()\n manifest['fingerprint'] = str(self.get_target_geckodriver_version_number())\n return manifest"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef update(kernel=False):\n manager = MANAGER\n cmds = {'yum -y --color=never': {False: '--exclude=kernel* update', True: 'update'}}\n cmd = cmds[manager][kernel]\n run_as_root(\"%(manager)s %(cmd)s\" % locals())", "response": "Update the availabe packages in a single resource group."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nchecks if an RPM package is installed.", "response": "def is_installed(pkg_name):\n \"\"\"\n Check if an RPM package is installed.\n \"\"\"\n manager = MANAGER\n with settings(hide('running', 'stdout', 'stderr', 'warnings'), warn_only=True):\n res = run(\"rpm --query %(pkg_name)s\" % locals())\n if res.succeeded:\n return True\n return False"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ninstall one or more RPM packages.", "response": "def install(packages, repos=None, yes=None, options=None):\n \"\"\"\n Install one or more RPM packages.\n\n Extra *repos* may be passed to ``yum`` to enable extra repositories at install time.\n\n Extra *yes* may be passed to ``yum`` to validate license if necessary.\n\n Extra *options* may be passed to ``yum`` if necessary\n (e.g. ``['--nogpgcheck', '--exclude=package']``).\n\n ::\n\n import burlap\n\n # Install a single package, in an alternative install root\n burlap.rpm.install('emacs', options='--installroot=/my/new/location')\n\n # Install multiple packages silently\n burlap.rpm.install([\n 'unzip',\n 'nano'\n ], '--quiet')\n\n \"\"\"\n manager = MANAGER\n if options is None:\n options = []\n elif isinstance(options, six.string_types):\n options = [options]\n if not isinstance(packages, six.string_types):\n packages = \" \".join(packages)\n if repos:\n for repo in repos:\n options.append('--enablerepo=%(repo)s' % locals())\n options = \" \".join(options)\n if isinstance(yes, str):\n run_as_root('yes %(yes)s | %(manager)s %(options)s install %(packages)s' % locals())\n else:\n run_as_root('%(manager)s %(options)s install %(packages)s' % locals())"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ninstalling a group of packages.", "response": "def groupinstall(group, options=None):\n \"\"\"\n Install a group of packages.\n\n You can use ``yum grouplist`` to get the list of groups.\n\n Extra *options* may be passed to ``yum`` if necessary like\n (e.g. ``['--nogpgcheck', '--exclude=package']``).\n\n ::\n\n import burlap\n\n # Install development packages\n burlap.rpm.groupinstall('Development tools')\n\n \"\"\"\n manager = MANAGER\n if options is None:\n options = []\n elif isinstance(options, str):\n options = [options]\n options = \" \".join(options)\n run_as_root('%(manager)s %(options)s groupinstall \"%(group)s\"' % locals(), pty=False)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nremoves one or more packages. Extra *options* may be passed to ``yum`` if necessary.", "response": "def uninstall(packages, options=None):\n \"\"\"\n Remove one or more packages.\n\n Extra *options* may be passed to ``yum`` if necessary.\n\n \"\"\"\n manager = MANAGER\n if options is None:\n options = []\n elif isinstance(options, six.string_types):\n options = [options]\n if not isinstance(packages, six.string_types):\n packages = \" \".join(packages)\n options = \" \".join(options)\n run_as_root('%(manager)s %(options)s remove %(packages)s' % locals())"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nremoves an existing software group. Extra *options* may be passed to ``yum`` if necessary.", "response": "def groupuninstall(group, options=None):\n \"\"\"\n Remove an existing software group.\n\n Extra *options* may be passed to ``yum`` if necessary.\n\n \"\"\"\n manager = MANAGER\n if options is None:\n options = []\n elif isinstance(options, str):\n options = [options]\n options = \" \".join(options)\n run_as_root('%(manager)s %(options)s groupremove \"%(group)s\"' % locals())"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ngetting the list of yum repositories.", "response": "def repolist(status='', media=None):\n \"\"\"\n Get the list of ``yum`` repositories.\n\n Returns enabled repositories by default. Extra *status* may be passed\n to list disabled repositories if necessary.\n\n Media and debug repositories are kept disabled, except if you pass *media*.\n\n ::\n\n import burlap\n\n # Install a package that may be included in disabled repositories\n burlap.rpm.install('vim', burlap.rpm.repolist('disabled'))\n\n \"\"\"\n manager = MANAGER\n with settings(hide('running', 'stdout')):\n if media:\n repos = run_as_root(\"%(manager)s repolist %(status)s | sed '$d' | sed -n '/repo id/,$p'\" % locals())\n else:\n repos = run_as_root(\"%(manager)s repolist %(status)s | sed '/Media\\\\|Debug/d' | sed '$d' | sed -n '/repo id/,$p'\" % locals())\n return [line.split(' ')[0] for line in repos.splitlines()[1:]]"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef sync(self, sync_set, force=0, site=None, role=None):\n from burlap.dj import dj\n force = int(force)\n\n r = self.local_renderer\n\n r.env.sync_force_flag = ' --force ' if force else ''\n\n _settings = dj.get_settings(site=site, role=role)\n assert _settings, 'Unable to import settings.'\n for k in _settings.__dict__.iterkeys():\n if k.startswith('AWS_'):\n r.genv[k] = _settings.__dict__[k]\n\n site_data = r.genv.sites[r.genv.SITE]\n r.env.update(site_data)\n\n r.env.virtualenv_bin_dir = os.path.split(sys.executable)[0]\n\n rets = []\n for paths in r.env.sync_sets[sync_set]:\n is_local = paths.get('is_local', True)\n local_path = paths['local_path'] % r.genv\n remote_path = paths['remote_path']\n remote_path = remote_path.replace(':/', '/')\n if not remote_path.startswith('s3://'):\n remote_path = 's3://' + remote_path\n local_path = local_path % r.genv\n\n if is_local:\n #local_or_dryrun('which s3sync')#, capture=True)\n r.env.local_path = os.path.abspath(local_path)\n else:\n #run('which s3sync')\n r.env.local_path = local_path\n\n if local_path.endswith('/') and not r.env.local_path.endswith('/'):\n r.env.local_path = r.env.local_path + '/'\n\n r.env.remote_path = remote_path % r.genv\n\n print('Syncing %s to %s...' % (r.env.local_path, r.env.remote_path))\n\n # Superior Python version.\n if force:\n r.env.sync_cmd = 'put'\n else:\n r.env.sync_cmd = 'sync'\n r.local(\n 'export AWS_ACCESS_KEY_ID={aws_access_key_id}; '\\\n 'export AWS_SECRET_ACCESS_KEY={aws_secret_access_key}; '\\\n '{s3cmd_path} {sync_cmd} --progress --acl-public --guess-mime-type --no-mime-magic '\\\n '--delete-removed --cf-invalidate --recursive {sync_force_flag} '\\\n '{local_path} {remote_path}')", "response": "Uploads media to an Amazon S3 bucket using s3sync."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nissues invalidation requests to a Cloudfront distribution for the current static media bucket.", "response": "def invalidate(self, *paths):\n \"\"\"\n Issues invalidation requests to a Cloudfront distribution\n for the current static media bucket, triggering it to reload the specified\n paths from the origin.\n\n Note, only 1000 paths can be issued in a request at any one time.\n \"\"\"\n dj = self.get_satchel('dj')\n if not paths:\n return\n # http://boto.readthedocs.org/en/latest/cloudfront_tut.html\n _settings = dj.get_settings()\n if not _settings.AWS_STATIC_BUCKET_NAME:\n print('No static media bucket set.')\n return\n if isinstance(paths, six.string_types):\n paths = paths.split(',')\n all_paths = map(str.strip, paths)\n i = 0\n while 1:\n paths = all_paths[i:i+1000]\n if not paths:\n break\n\n c = boto.connect_cloudfront()\n rs = c.get_all_distributions()\n target_dist = None\n for dist in rs:\n print(dist.domain_name, dir(dist), dist.__dict__)\n bucket_name = dist.origin.dns_name.replace('.s3.amazonaws.com', '')\n if bucket_name == _settings.AWS_STATIC_BUCKET_NAME:\n target_dist = dist\n break\n if not target_dist:\n raise Exception(('Target distribution %s could not be found in the AWS account.') % (settings.AWS_STATIC_BUCKET_NAME,))\n print('Using distribution %s associated with origin %s.' % (target_dist.id, _settings.AWS_STATIC_BUCKET_NAME))\n inval_req = c.create_invalidation_request(target_dist.id, paths)\n print('Issue invalidation request %s.' % (inval_req,))\n i += 1000"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nget an S3 bucket of the given name creating one if it doesn t already exist.", "response": "def get_or_create_bucket(self, name):\n \"\"\"\n Gets an S3 bucket of the given name, creating one if it doesn't already exist.\n\n Should be called with a role, if AWS credentials are stored in role settings. e.g.\n\n fab local s3.get_or_create_bucket:mybucket\n \"\"\"\n from boto.s3 import connection\n if self.dryrun:\n print('boto.connect_s3().create_bucket(%s)' % repr(name))\n else:\n conn = connection.S3Connection(\n self.genv.aws_access_key_id,\n self.genv.aws_secret_access_key\n )\n bucket = conn.create_bucket(name)\n return bucket"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef static(self):\n fn = self.render_to_file('ip/ip_interfaces_static.template')\n r = self.local_renderer\n r.put(local_path=fn, remote_path=r.env.interfaces_fn, use_sudo=True)", "response": "Configures the server to use a static IP."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef record_manifest(self):\n manifest = super(TarballSatchel, self).record_manifest()\n manifest['timestamp'] = self.timestamp\n return manifest", "response": "Returns a manifest dict that contains the current timestamp and the manifest s version number."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_thumbprint(self):\n extensions = self.extensions.split(' ')\n name_str = ' -or '.join('-name \"%s\"' % ext for ext in extensions)\n cmd = 'find ' + self.base_dir + r' -type f \\( ' + name_str + r' \\) -exec md5sum {} \\; | sort -k 2 | md5sum'\n return getoutput(cmd)", "response": "Calculates the current thumbprint of the item being tracked."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ncalculating the current thumbprint of the item being tracked.", "response": "def get_thumbprint(self):\n \"\"\"\n Calculates the current thumbprint of the item being tracked.\n \"\"\"\n d = {}\n if self.names:\n names = self.names\n else:\n names = list(self.satchel.lenv)\n for name in self.names:\n d[name] = deepcopy(self.satchel.env[name])\n return d"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ncalculating the current thumbprint of the item being tracked.", "response": "def get_thumbprint(self):\n \"\"\"\n Calculates the current thumbprint of the item being tracked.\n \"\"\"\n d = {}\n for tracker in self.trackers:\n d[type(tracker).__name__] = tracker.get_thumbprint()\n return d"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nchecking if a package is installed.", "response": "def is_installed(pkg_name):\n \"\"\"\n Check if a package is installed.\n \"\"\"\n with settings(hide('running', 'stdout', 'stderr', 'warnings'), warn_only=True):\n res = run(\"dpkg -s %(pkg_name)s\" % locals())\n for line in res.splitlines():\n if line.startswith(\"Status: \"):\n status = line[8:]\n if \"installed\" in status.split(' '):\n return True\n return False"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef install(packages, update=False, options=None, version=None):\n manager = MANAGER\n if update:\n update_index()\n if options is None:\n options = []\n if version is None:\n version = ''\n if version and not isinstance(packages, list):\n version = '=' + version\n if not isinstance(packages, six.string_types):\n packages = \" \".join(packages)\n options.append(\"--quiet\")\n options.append(\"--assume-yes\")\n options = \" \".join(options)\n cmd = '%(manager)s install %(options)s %(packages)s%(version)s' % locals()\n run_as_root(cmd, pty=False)", "response": "Install one or more packages."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nremoving one or more packages. If *purge* is ``True``, the package configuration files will be removed from the system. Extra *options* may be passed to ``apt-get`` if necessary.", "response": "def uninstall(packages, purge=False, options=None):\n \"\"\"\n Remove one or more packages.\n\n If *purge* is ``True``, the package configuration files will be\n removed from the system.\n\n Extra *options* may be passed to ``apt-get`` if necessary.\n \"\"\"\n manager = MANAGER\n command = \"purge\" if purge else \"remove\"\n if options is None:\n options = []\n if not isinstance(packages, six.string_types):\n packages = \" \".join(packages)\n options.append(\"--assume-yes\")\n options = \" \".join(options)\n cmd = '%(manager)s %(command)s %(options)s %(packages)s' % locals()\n run_as_root(cmd, pty=False)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nenable unattended package installation by preseeding ``debconf`` parameters. Example:: import burlap # Unattended install of Postfix mail server burlap.deb.preseed_package('postfix', { 'postfix/main_mailer_type': ('select', 'Internet Site'), 'postfix/mailname': ('string', 'example.com'), 'postfix/destinations': ('string', 'example.com, localhost.localdomain, localhost'), }) burlap.deb.install('postfix')", "response": "def preseed_package(pkg_name, preseed):\n \"\"\"\n Enable unattended package installation by preseeding ``debconf``\n parameters.\n\n Example::\n\n import burlap\n\n # Unattended install of Postfix mail server\n burlap.deb.preseed_package('postfix', {\n 'postfix/main_mailer_type': ('select', 'Internet Site'),\n 'postfix/mailname': ('string', 'example.com'),\n 'postfix/destinations': ('string', 'example.com, localhost.localdomain, localhost'),\n })\n burlap.deb.install('postfix')\n\n \"\"\"\n for q_name, _ in preseed.items():\n q_type, q_answer = _\n run_as_root('echo \"%(pkg_name)s %(q_name)s %(q_type)s %(q_answer)s\" | debconf-set-selections' % locals())"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngetting the state of kdpg selections.", "response": "def get_selections():\n \"\"\"\n Get the state of ``dkpg`` selections.\n\n Returns a dict with state => [packages].\n \"\"\"\n with settings(hide('stdout')):\n res = run_as_root('dpkg --get-selections')\n selections = dict()\n for line in res.splitlines():\n package, status = line.split()\n selections.setdefault(status, list()).append(package)\n return selections"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nchecks if the given key id exists in apt keyring.", "response": "def apt_key_exists(keyid):\n \"\"\"\n Check if the given key id exists in apt keyring.\n \"\"\"\n\n # Command extracted from apt-key source\n gpg_cmd = 'gpg --ignore-time-conflict --no-options --no-default-keyring --keyring /etc/apt/trusted.gpg'\n\n with settings(hide('everything'), warn_only=True):\n res = run('%(gpg_cmd)s --fingerprint %(keyid)s' % locals())\n\n return res.succeeded"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nadd a new trust package to the current public key.", "response": "def add_apt_key(filename=None, url=None, keyid=None, keyserver='subkeys.pgp.net', update=False):\n \"\"\"\n Trust packages signed with this public key.\n\n Example::\n\n import burlap\n\n # Varnish signing key from URL and verify fingerprint)\n burlap.deb.add_apt_key(keyid='C4DEFFEB', url='http://repo.varnish-cache.org/debian/GPG-key.txt')\n\n # Nginx signing key from default key server (subkeys.pgp.net)\n burlap.deb.add_apt_key(keyid='7BD9BF62')\n\n # From custom key server\n burlap.deb.add_apt_key(keyid='7BD9BF62', keyserver='keyserver.ubuntu.com')\n\n # From a file\n burlap.deb.add_apt_key(keyid='7BD9BF62', filename='nginx.asc'\n \"\"\"\n\n if keyid is None:\n if filename is not None:\n run_as_root('apt-key add %(filename)s' % locals())\n elif url is not None:\n run_as_root('wget %(url)s -O - | apt-key add -' % locals())\n else:\n raise ValueError('Either filename, url or keyid must be provided as argument')\n else:\n if filename is not None:\n _check_pgp_key(filename, keyid)\n run_as_root('apt-key add %(filename)s' % locals())\n elif url is not None:\n tmp_key = '/tmp/tmp.burlap.key.%(keyid)s.key' % locals()\n run_as_root('wget %(url)s -O %(tmp_key)s' % locals())\n _check_pgp_key(tmp_key, keyid)\n run_as_root('apt-key add %(tmp_key)s' % locals())\n else:\n keyserver_opt = '--keyserver %(keyserver)s' % locals() if keyserver is not None else ''\n run_as_root('apt-key adv %(keyserver_opt)s --recv-keys %(keyid)s' % locals())\n\n if update:\n update_index()"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nconfiguring the local renderer to use the most current version of the Debian system.", "response": "def configure(self):\n \"\"\"\n Enables the repository for a most current version on Debian systems.\n\n https://www.rabbitmq.com/install-debian.html\n \"\"\"\n\n os_version = self.os_version\n if not self.dryrun and os_version.distro != UBUNTU:\n raise NotImplementedError(\"OS %s is not supported.\" % os_version)\n\n r = self.local_renderer\n\n r.env.codename = (r.run('lsb_release -c -s') or \"`lsb_release -c -s`\").strip()\n r.sudo('apt-add-repository \"deb http://download.virtualbox.org/virtualbox/debian {codename} contrib\"')\n r.sudo('cd /tmp; wget -q https://www.virtualbox.org/download/oracle_vbox.asc -O- | sudo apt-key add -')\n r.sudo('apt-get update')"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef exists(self, name):\n with self.settings(hide('running', 'stdout', 'warnings'), warn_only=True):\n return self.run('getent group %(name)s' % locals()).succeeded", "response": "Check if a group exists."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncreate a new group.", "response": "def create(self, name, gid=None):\n \"\"\"\n Create a new group.\n\n Example::\n\n import burlap\n\n if not burlap.group.exists('admin'):\n burlap.group.create('admin')\n\n \"\"\"\n args = []\n if gid:\n args.append('-g %s' % gid)\n args.append(name)\n args = ' '.join(args)\n self.sudo('groupadd --force %s || true' % args)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nentering a password change for a user.", "response": "def enter_password_change(self, username=None, old_password=None):\n \"\"\"\n Responds to a forced password change via `passwd` prompts due to password expiration.\n \"\"\"\n from fabric.state import connections\n from fabric.network import disconnect_all\n r = self.local_renderer\n# print('self.genv.user:', self.genv.user)\n# print('self.env.passwords:', self.env.passwords)\n r.genv.user = r.genv.user or username\n r.pc('Changing password for user {user} via interactive prompts.')\n r.env.old_password = r.env.default_passwords[self.genv.user]\n# print('self.genv.user:', self.genv.user)\n# print('self.env.passwords:', self.env.passwords)\n r.env.new_password = self.env.passwords[self.genv.user]\n if old_password:\n r.env.old_password = old_password\n prompts = {\n '(current) UNIX password: ': r.env.old_password,\n 'Enter new UNIX password: ': r.env.new_password,\n 'Retype new UNIX password: ': r.env.new_password,\n #\"Login password for '%s': \" % r.genv.user: r.env.new_password,\n# \"Login password for '%s': \" % r.genv.user: r.env.old_password,\n }\n print('prompts:', prompts)\n\n r.env.password = r.env.old_password\n with self.settings(warn_only=True):\n ret = r._local(\"sshpass -p '{password}' ssh -o StrictHostKeyChecking=no {user}@{host_string} echo hello\", capture=True)\n #code 1 = good password, but prompts needed\n #code 5 = bad password\n #code 6 = good password, but host public key is unknown\n if ret.return_code in (1, 6) or 'hello' in ret:\n # Login succeeded, so we haven't yet changed the password, so use the default password.\n self.genv.password = r.env.old_password\n elif self.genv.user in self.genv.user_passwords:\n # Otherwise, use the password or key set in the config.\n self.genv.password = r.env.new_password\n else:\n # Default password fails and there's no current password, so clear.\n self.genv.password = None\n print('using password:', self.genv.password)\n\n # Note, the correct current password should be set in host.initrole(), not here.\n #r.genv.password = r.env.new_password\n #r.genv.password = r.env.new_password\n with self.settings(prompts=prompts):\n ret = r._run('echo checking for expired password')\n print('ret:[%s]' % ret)\n do_disconnect = 'passwd: password updated successfully' in ret\n print('do_disconnect:', do_disconnect)\n if do_disconnect:\n # We need to disconnect to reset the session or else Linux will again prompt\n # us to change our password.\n disconnect_all()\n\n # Further logins should require the new password.\n self.genv.password = r.env.new_password"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nadd the user to the given list of groups.", "response": "def togroups(self, user, groups):\n \"\"\"\n Adds the user to the given list of groups.\n \"\"\"\n\n r = self.local_renderer\n\n if isinstance(groups, six.string_types):\n groups = [_.strip() for _ in groups.split(',') if _.strip()]\n for group in groups:\n r.env.username = user\n r.env.group = group\n r.sudo('groupadd --force {group}')\n r.sudo('adduser {username} {group}')"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nconfigure the user to use an SSL key without a password.", "response": "def passwordless(self, username, pubkey):\n \"\"\"\n Configures the user to use an SSL key without a password.\n Assumes you've run generate_keys() first.\n \"\"\"\n\n r = self.local_renderer\n\n r.env.username = username\n r.env.pubkey = pubkey\n if not self.dryrun:\n assert os.path.isfile(r.env.pubkey), \\\n 'Public key file \"%s\" does not exist.' % (str(r.env.pubkey),)\n\n first = os.path.splitext(r.env.pubkey)[0]\n r.env.pubkey = first+'.pub'\n r.env.pemkey = first+'.pem'\n r.env.home = r.env.home_template.format(username=username)\n\n # Upload the SSH key.\n r.sudo('mkdir -p {home}/.ssh')\n r.sudo('chown -R {user}:{user} {home}/.ssh')\n\n if r.env.passwordless_method == UPLOAD_KEY:\n put_remote_paths = self.put(local_path=r.env.pubkey)\n r.env.put_remote_path = put_remote_paths[0]\n r.sudo('cat {put_remote_path} >> {home}/.ssh/authorized_keys')\n r.sudo('rm -f {put_remote_path}')\n elif r.env.passwordless_method == CAT_KEY:\n r.env.password = r.env.default_passwords.get(r.env.username, r.genv.password)\n if r.env.password:\n r.local(\"cat {pubkey} | sshpass -p '{password}' ssh {user}@{host_string} 'cat >> {home}/.ssh/authorized_keys'\")\n else:\n r.local(\"cat {pubkey} | ssh {user}@{host_string} 'cat >> {home}/.ssh/authorized_keys'\")\n else:\n raise NotImplementedError\n\n # Disable password.\n r.sudo('cp /etc/sudoers {tmp_sudoers_fn}')\n r.sudo('echo \"{username} ALL=(ALL) NOPASSWD: ALL\" >> {tmp_sudoers_fn}')\n r.sudo('sudo EDITOR=\"cp {tmp_sudoers_fn}\" visudo')\n\n r.sudo('service ssh reload')\n\n print('You should now be able to login with:')\n r.env.host_string = self.genv.host_string or (self.genv.hosts and self.genv.hosts[0])#self.genv.hostname_hostname\n r.comment('\\tssh -i {pemkey} {username}@{host_string}')"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef generate_keys(self, username, hostname):\n\n r = self.local_renderer\n\n #r.env.key_filename = r.env.key_filename or env.key_filename\n #assert r.env.key_filename, 'r.env.key_filename or env.key_filename must be set. e.g. roles/role/app_name-role.pem'\n r.env.key_filename = self.env.key_filename_template.format(\n ROLE=self.genv.ROLE,\n host=hostname,\n username=username,\n )\n if os.path.isfile(r.env.key_filename):\n r.pc('Key file {key_filename} already exists. Skipping generation.'.format(**r.env))\n else:\n r.local(\"ssh-keygen -t {key_type} -b {key_bits} -f {key_filename} -N ''\")\n r.local('chmod {key_perms} {key_filename}')\n if r.env.key_filename.endswith('.pem'):\n src = r.env.key_filename+'.pub'\n dst = (r.env.key_filename+'.pub').replace('.pem', '')\n# print('generate_keys:', src, dst)\n r.env.src = src\n r.env.dst = dst\n r.local('mv {src} {dst}')\n return r.env.key_filename", "response": "Generate a new key file for the user."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncreating a user with the given username.", "response": "def create(self, username, groups=None, uid=None, create_home=None, system=False, password=None, home_dir=None):\n \"\"\"\n Creates a user with the given username.\n \"\"\"\n r = self.local_renderer\n r.env.username = username\n\n args = []\n\n if uid:\n args.append('-u %s' % uid)\n\n if create_home is None:\n create_home = not system\n\n if create_home is True:\n if home_dir:\n args.append('--home %s' % home_dir)\n elif create_home is False:\n args.append('--no-create-home')\n\n if password is None:\n pass\n elif password:\n crypted_password = _crypt_password(password)\n args.append('-p %s' % quote(crypted_password))\n else:\n args.append('--disabled-password')\n\n args.append('--gecos \"\"')\n\n if system:\n args.append('--system')\n\n r.env.args = ' '.join(args)\n r.env.groups = (groups or '').strip()\n r.sudo('adduser {args} {username} || true')\n if groups:\n for group in groups.split(' '):\n group = group.strip()\n if not group:\n continue\n r.sudo('adduser %s %s || true' % (username, group))"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nexpires the user s password", "response": "def expire_password(self, username):\n \"\"\"\n Forces the user to change their password the next time they login.\n \"\"\"\n r = self.local_renderer\n r.env.username = username\n r.sudo('chage -d 0 {username}')"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef run_as_root(command, *args, **kwargs):\n from burlap.common import run_or_dryrun, sudo_or_dryrun\n if env.user == 'root':\n func = run_or_dryrun\n else:\n func = sudo_or_dryrun\n return func(command, *args, **kwargs)", "response": "Run a remote command as the root user."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn a hash of the contents of a file.", "response": "def get_file_hash(fin, block_size=2**20):\n \"\"\"\n Iteratively builds a file hash without loading the entire file into memory.\n Designed to process an arbitrary binary file.\n \"\"\"\n if isinstance(fin, six.string_types):\n fin = open(fin)\n h = hashlib.sha512()\n while True:\n data = fin.read(block_size)\n if not data:\n break\n try:\n h.update(data)\n except TypeError:\n # Fixes Python3 error \"TypeError: Unicode-objects must be encoded before hashing\".\n h.update(data.encode('utf-8'))\n return h.hexdigest()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef check(self):\n self._validate_settings()\n r = self.local_renderer\n r.env.alias = r.env.aliases[0]\n r.sudo(r.env.check_command_template)", "response": "Run inadyn from the commandline to test the configuration."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef list_env(self, key=None):\n for k, v in sorted(self.genv.items(), key=lambda o: o[0]):\n if key and k != key:\n continue\n print('%s ' % (k,))\n pprint(v, indent=4)", "response": "Displays a list of environment key value pairs."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef list_server_specs(self, cpu=1, memory=1, hdd=1):\n r = self.local_renderer\n\n cpu = int(cpu)\n memory = int(memory)\n hdd = int(hdd)\n\n # CPU\n if cpu:\n cmd = 'cat /proc/cpuinfo | grep -i \"model name\"'\n ret = r.run(cmd)\n matches = map(str.strip, re.findall(r'model name\\s+:\\s*([^\\n]+)', ret, re.DOTALL|re.I))\n cores = {}\n for match in matches:\n cores.setdefault(match, 0)\n cores[match] += 1\n\n # Memory\n if memory:\n cmd = 'dmidecode --type 17'\n ret = r.sudo(cmd)\n #print repr(ret)\n matches = re.findall(r'Memory\\s+Device\\r\\n(.*?)(?:\\r\\n\\r\\n|$)', ret, flags=re.DOTALL|re.I)\n #print len(matches)\n #print matches[0]\n memory_slot_dicts = []\n for match in matches:\n attrs = dict([(_a.strip(), _b.strip()) for _a, _b in re.findall(r'^([^:]+):\\s+(.*)$', match, flags=re.MULTILINE)])\n #print attrs\n memory_slot_dicts.append(attrs)\n total_memory_gb = 0\n total_slots_filled = 0\n total_slots = len(memory_slot_dicts)\n memory_types = set()\n memory_forms = set()\n memory_speeds = set()\n for memory_dict in memory_slot_dicts:\n try:\n size = int(round(float(re.findall(r'([0-9]+)\\s+MB', memory_dict['Size'])[0])/1024.))\n #print size\n total_memory_gb += size\n total_slots_filled += 1\n except IndexError:\n pass\n _v = memory_dict['Type']\n if _v != 'Unknown':\n memory_types.add(_v)\n _v = memory_dict['Form Factor']\n if _v != 'Unknown':\n memory_forms.add(_v)\n #_v = memory_dict['Speed']\n #if _v != 'Unknown':\n #memory_speeds.add(_v)\n\n # Storage\n if hdd:\n #cmd = 'ls /dev/*d* | grep \"/dev/[a-z]+d[a-z]$\"'\n cmd = 'find /dev -maxdepth 1 | grep -E \"/dev/[a-z]+d[a-z]$\"'\n devices = map(str.strip, r.run(cmd).split('\\n'))\n total_drives = len(devices)\n total_physical_storage_gb = 0\n total_logical_storage_gb = 0\n drive_transports = set()\n for device in devices:\n #cmd = 'udisks --show-info %s |grep -i \" size:\"' % (device)\n cmd = 'udisksctl info -b %s |grep -i \" size:\"' % (device)\n ret = r.run(cmd)\n size_bytes = float(re.findall(r'size:\\s*([0-9]+)', ret, flags=re.I)[0].strip())\n size_gb = int(round(size_bytes/1024/1024/1024))\n #print device, size_gb\n total_physical_storage_gb += size_gb\n\n with self.settings(warn_only=True):\n cmd = 'hdparm -I %s|grep -i \"Transport:\"' % device\n ret = self.sudo(cmd)\n if ret and not ret.return_code:\n drive_transports.add(ret.split('Transport:')[-1].strip())\n\n cmd = \"df | grep '^/dev/[mhs]d*' | awk '{{s+=$2}} END {{print s/1048576}}'\"\n ret = r.run(cmd)\n total_logical_storage_gb = float(ret)\n\n if cpu:\n print('-'*80)\n print('CPU')\n print('-'*80)\n type_str = ', '.join(['%s x %i' % (_type, _count) for _type, _count in cores.items()])\n print('Cores: %i' % sum(cores.values()))\n print('Types: %s' % type_str)\n\n if memory:\n print('-'*80)\n print('MEMORY')\n print('-'*80)\n print('Total: %s GB' % total_memory_gb)\n print('Type: %s' % list_to_str_or_unknown(memory_types))\n print('Form: %s' % list_to_str_or_unknown(memory_forms))\n print('Speed: %s' % list_to_str_or_unknown(memory_speeds))\n print('Slots: %i (%i filled, %i empty)' % (total_slots, total_slots_filled, total_slots - total_slots_filled))\n\n if hdd:\n print('-'*80)\n print('STORAGE')\n print('-'*80)\n print('Total physical drives: %i' % total_drives)\n print('Total physical storage: %s GB' % total_physical_storage_gb)\n print('Total logical storage: %s GB' % total_logical_storage_gb)\n print('Types: %s' % list_to_str_or_unknown(drive_transports))", "response": "Displays a list of common servers characteristics like number of CPU cores amount of memory and hard drive capacity."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef shell(self, gui=0, command='', dryrun=None, shell_interactive_cmd_str=None):\n from burlap.common import get_hosts_for_site\n\n if dryrun is not None:\n self.dryrun = dryrun\n\n r = self.local_renderer\n\n if r.genv.SITE != r.genv.default_site:\n shell_hosts = get_hosts_for_site()\n if shell_hosts:\n r.genv.host_string = shell_hosts[0]\n\n r.env.SITE = r.genv.SITE or r.genv.default_site\n\n if int(gui):\n r.env.shell_default_options.append('-X')\n\n if 'host_string' not in self.genv or not self.genv.host_string:\n if 'available_sites' in self.genv and r.env.SITE not in r.genv.available_sites:\n raise Exception('No host_string set. Unknown site %s.' % r.env.SITE)\n else:\n raise Exception('No host_string set.')\n\n if '@' in r.genv.host_string:\n r.env.shell_host_string = r.genv.host_string\n else:\n r.env.shell_host_string = '{user}@{host_string}'\n\n if command:\n r.env.shell_interactive_cmd_str = command\n else:\n r.env.shell_interactive_cmd_str = r.format(shell_interactive_cmd_str or r.env.shell_interactive_cmd)\n\n r.env.shell_default_options_str = ' '.join(r.env.shell_default_options)\n if self.is_local:\n self.vprint('Using direct local.')\n cmd = '{shell_interactive_cmd_str}'\n elif r.genv.key_filename:\n self.vprint('Using key filename.')\n # If host_string contains the port, then strip it off and pass separately.\n port = r.env.shell_host_string.split(':')[-1]\n if port.isdigit():\n r.env.shell_host_string = r.env.shell_host_string.split(':')[0] + (' -p %s' % port)\n cmd = 'ssh -t {shell_default_options_str} -i {key_filename} {shell_host_string} \"{shell_interactive_cmd_str}\"'\n elif r.genv.password:\n self.vprint('Using password.')\n cmd = 'ssh -t {shell_default_options_str} {shell_host_string} \"{shell_interactive_cmd_str}\"'\n else:\n # No explicit password or key file needed?\n self.vprint('Using nothing.')\n cmd = 'ssh -t {shell_default_options_str} {shell_host_string} \"{shell_interactive_cmd_str}\"'\n r.local(cmd)", "response": "Open an SSH connection and return a dictionary of command and arguments."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ndisplay percent of disk usage.", "response": "def disk(self):\n \"\"\"\n Display percent of disk usage.\n \"\"\"\n r = self.local_renderer\n r.run(r.env.disk_usage_command)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef tunnel(self, local_port, remote_port):\n r = self.local_renderer\n r.env.tunnel_local_port = local_port\n r.env.tunnel_remote_port = remote_port\n r.local(' ssh -i {key_filename} -L {tunnel_local_port}:localhost:{tunnel_remote_port} {user}@{host_string} -N')", "response": "Creates an SSH tunnel."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nsets a key value pair in a satchel s local renderer.", "response": "def set_satchel_value(self, satchel, key, value):\n \"\"\"\n Sets a key/value pair in a satchel's local renderer.\n \"\"\"\n satchel = self.get_satchel(satchel)\n r = satchel.local_renderer\n setattr(r.env, key, value)\n print('Set %s=%s in satchel %s.' % (key, value, satchel.name))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngetting the installed version of a package.", "response": "def package_version(name, python_cmd='python'):\n \"\"\"\n Get the installed version of a package\n\n Returns ``None`` if it can't be found.\n \"\"\"\n cmd = '''%(python_cmd)s -c \\\n \"import pkg_resources;\\\n dist = pkg_resources.get_distribution('%(name)s');\\\n print dist.version\"\n ''' % locals()\n res = run(cmd, quiet=True)\n if res.succeeded:\n return res\n return"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ninstall the latest version of setuptools.", "response": "def install_setuptools(python_cmd='python', use_sudo=True):\n \"\"\"\n Install the latest version of `setuptools`_.\n\n ::\n\n import burlap\n\n burlap.python_setuptools.install_setuptools()\n\n \"\"\"\n\n setuptools_version = package_version('setuptools', python_cmd)\n distribute_version = package_version('distribute', python_cmd)\n\n if setuptools_version is None:\n _install_from_scratch(python_cmd, use_sudo)\n else:\n if distribute_version is None:\n _upgrade_from_setuptools(python_cmd, use_sudo)\n else:\n _upgrade_from_distribute(python_cmd, use_sudo)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ninstalls setuptools from scratch using installer", "response": "def _install_from_scratch(python_cmd, use_sudo):\n \"\"\"\n Install setuptools from scratch using installer\n \"\"\"\n\n with cd(\"/tmp\"):\n download(EZ_SETUP_URL)\n\n command = '%(python_cmd)s ez_setup.py' % locals()\n if use_sudo:\n run_as_root(command)\n else:\n run(command)\n\n run('rm -f ez_setup.py')"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ninstalling Python packages with easy_install.", "response": "def install(packages, upgrade=False, use_sudo=False, python_cmd='python'):\n \"\"\"\n Install Python packages with ``easy_install``.\n\n Examples::\n\n import burlap\n\n # Install a single package\n burlap.python_setuptools.install('package', use_sudo=True)\n\n # Install a list of packages\n burlap.python_setuptools.install(['pkg1', 'pkg2'], use_sudo=True)\n\n .. note:: most of the time, you'll want to use\n :py:func:`burlap.python.install()` instead,\n which uses ``pip`` to install packages.\n\n \"\"\"\n argv = []\n if upgrade:\n argv.append(\"-U\")\n if isinstance(packages, six.string_types):\n argv.append(packages)\n else:\n argv.extend(packages)\n _easy_install(argv, python_cmd, use_sudo)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ninstalling packages using easy_install", "response": "def _easy_install(argv, python_cmd, use_sudo):\n \"\"\"\n Install packages using easy_install\n\n We don't know if the easy_install command in the path will be the\n right one, so we use the setuptools entry point to call the script's\n main function ourselves.\n \"\"\"\n command = \"\"\"python -c \"\\\n from pkg_resources import load_entry_point;\\\n ez = load_entry_point('setuptools', 'console_scripts', 'easy_install');\\\n ez(argv=%(argv)r)\\\n \"\"\" % locals()\n if use_sudo:\n run_as_root(command)\n else:\n run(command)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ninstalling all the necessary packages necessary for managing virtual hosts with pip.", "response": "def bootstrap(self, force=0):\n \"\"\"\n Installs all the necessary packages necessary for managing virtual\n environments with pip.\n \"\"\"\n force = int(force)\n if self.has_pip() and not force:\n return\n\n r = self.local_renderer\n\n if r.env.bootstrap_method == GET_PIP:\n r.sudo('curl --silent --show-error --retry 5 https://bootstrap.pypa.io/get-pip.py | python')\n elif r.env.bootstrap_method == EZ_SETUP:\n r.run('wget http://peak.telecommunity.com/dist/ez_setup.py -O /tmp/ez_setup.py')\n with self.settings(warn_only=True):\n r.sudo('python /tmp/ez_setup.py -U setuptools')\n r.sudo('easy_install -U pip')\n elif r.env.bootstrap_method == PYTHON_PIP:\n r.sudo('apt-get install -y python-pip')\n else:\n raise NotImplementedError('Unknown pip bootstrap method: %s' % r.env.bootstrap_method)\n\n r.sudo('pip {quiet_flag} install --upgrade pip')\n r.sudo('pip {quiet_flag} install --upgrade virtualenv')"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns True if the virtualenv tool is installed.", "response": "def has_virtualenv(self):\n \"\"\"\n Returns true if the virtualenv tool is installed.\n \"\"\"\n with self.settings(warn_only=True):\n ret = self.run_or_local('which virtualenv').strip()\n return bool(ret)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef virtualenv_exists(self, virtualenv_dir=None):\n r = self.local_renderer\n ret = True\n with self.settings(warn_only=True):\n ret = r.run_or_local('ls {virtualenv_dir}') or ''\n ret = 'cannot access' not in ret.strip().lower()\n\n if self.verbose:\n if ret:\n print('Yes')\n else:\n print('No')\n\n return ret", "response": "Returns True if the virtual environment exists."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nlist the packages that require the given package.", "response": "def what_requires(self, name):\n \"\"\"\n Lists the packages that require the given package.\n \"\"\"\n r = self.local_renderer\n r.env.name = name\n r.local('pipdeptree -p {name} --reverse')"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef init(self):\n r = self.local_renderer\n\n# if self.virtualenv_exists():\n# print('virtualenv exists')\n# return\n\n print('Creating new virtual environment...')\n with self.settings(warn_only=True):\n cmd = '[ ! -d {virtualenv_dir} ] && virtualenv --no-site-packages {virtualenv_dir} || true'\n if self.is_local:\n r.run_or_local(cmd)\n else:\n r.sudo(cmd)", "response": "Creates the virtual environment."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_combined_requirements(self, requirements=None):\n\n requirements = requirements or self.env.requirements\n\n def iter_lines(fn):\n with open(fn, 'r') as fin:\n for line in fin.readlines():\n line = line.strip()\n if not line or line.startswith('#'):\n continue\n yield line\n\n content = []\n if isinstance(requirements, (tuple, list)):\n for f in requirements:\n f = self.find_template(f)\n content.extend(list(iter_lines(f)))\n else:\n assert isinstance(requirements, six.string_types)\n f = self.find_template(requirements)\n content.extend(list(iter_lines(f)))\n\n return '\\n'.join(content)", "response": "Returns all requirements files combined into one string."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef record_manifest(self):\n manifest = super(PIPSatchel, self).record_manifest()\n manifest['all-requirements'] = self.get_combined_requirements()\n if self.verbose:\n pprint(manifest, indent=4)\n return manifest", "response": "Called after a deployment to record any data necessary to detect changes\n "} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nlists virtual machines in the current environment.", "response": "def list_instances(show=1, name=None, group=None, release=None, except_release=None):\n \"\"\"\n Retrieves all virtual machines instances in the current environment.\n \"\"\"\n from burlap.common import shelf, OrderedDict, get_verbose\n\n verbose = get_verbose()\n require('vm_type', 'vm_group')\n assert env.vm_type, 'No VM type specified.'\n env.vm_type = (env.vm_type or '').lower()\n _name = name\n _group = group\n _release = release\n if verbose:\n print('name=%s, group=%s, release=%s' % (_name, _group, _release))\n\n env.vm_elastic_ip_mappings = shelf.get('vm_elastic_ip_mappings')\n\n data = type(env)()\n if env.vm_type == EC2:\n if verbose:\n print('Checking EC2...')\n for instance in get_all_running_ec2_instances():\n name = instance.tags.get(env.vm_name_tag)\n group = instance.tags.get(env.vm_group_tag)\n release = instance.tags.get(env.vm_release_tag)\n if env.vm_group and env.vm_group != group:\n if verbose:\n print(('Skipping instance %s because its group \"%s\" '\n 'does not match env.vm_group \"%s\".') \\\n % (instance.public_dns_name, group, env.vm_group))\n continue\n if _group and group != _group:\n if verbose:\n print(('Skipping instance %s because its group \"%s\" '\n 'does not match local group \"%s\".') \\\n % (instance.public_dns_name, group, _group))\n continue\n if _name and name != _name:\n if verbose:\n print(('Skipping instance %s because its name \"%s\" '\n 'does not match name \"%s\".') \\\n % (instance.public_dns_name, name, _name))\n continue\n if _release and release != _release:\n if verbose:\n print(('Skipping instance %s because its release \"%s\" '\n 'does not match release \"%s\".') \\\n % (instance.public_dns_name, release, _release))\n continue\n if except_release and release == except_release:\n continue\n if verbose:\n print('Adding instance %s (%s).' \\\n % (name, instance.public_dns_name))\n data.setdefault(name, type(env)())\n data[name]['id'] = instance.id\n data[name]['public_dns_name'] = instance.public_dns_name\n if verbose:\n print('Public DNS: %s' % instance.public_dns_name)\n\n if env.vm_elastic_ip_mappings and name in env.vm_elastic_ip_mappings:\n data[name]['ip'] = env.vm_elastic_ip_mappings[name]\n else:\n data[name]['ip'] = socket.gethostbyname(instance.public_dns_name)\n\n if int(show):\n pprint(data, indent=4)\n return data\n elif env.vm_type == KVM:\n #virsh list\n pass\n else:\n raise NotImplementedError"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nget or create EC2 security groups.", "response": "def get_or_create_ec2_security_groups(names=None, verbose=1):\n \"\"\"\n Creates a security group opening 22, 80 and 443\n \"\"\"\n verbose = int(verbose)\n\n if verbose:\n print('Creating EC2 security groups...')\n\n conn = get_ec2_connection()\n\n if isinstance(names, six.string_types):\n names = names.split(',')\n names = names or env.vm_ec2_selected_security_groups\n if verbose:\n print('Group names:', names)\n\n ret = []\n for name in names:\n try:\n group_id = get_ec2_security_group_id(name)\n if verbose:\n print('group_id:', group_id)\n #group = conn.get_all_security_groups(groupnames=[name])[0]\n # Note, groups in a VPC can't be referred to by name?\n group = conn.get_all_security_groups(group_ids=[group_id])[0]\n except boto.exception.EC2ResponseError as e:\n if verbose:\n print(e)\n group = get_ec2_connection().create_security_group(\n name,\n name,\n vpc_id=env.vm_ec2_vpc_id,\n )\n print('group_id:', group.id)\n set_ec2_security_group_id(name, group.id)\n ret.append(group)\n\n # Find existing rules.\n actual_sets = set()\n for rule in list(group.rules):\n ip_protocol = rule.ip_protocol\n from_port = rule.from_port\n to_port = rule.to_port\n for cidr_ip in rule.grants:\n #print('Revoking:', ip_protocol, from_port, to_port, cidr_ip)\n #group.revoke(ip_protocol, from_port, to_port, cidr_ip)\n rule_groups = ((rule.groups and rule.groups.split(',')) or [None])\n for src_group in rule_groups:\n src_group = (src_group or '').strip()\n if src_group:\n actual_sets.add((ip_protocol, from_port, to_port, str(cidr_ip), src_group))\n else:\n actual_sets.add((ip_protocol, from_port, to_port, str(cidr_ip)))\n\n # Find actual rules.\n expected_sets = set()\n for authorization in env.vm_ec2_available_security_groups.get(name, []):\n if verbose:\n print('authorization:', authorization)\n if len(authorization) == 4 or (len(authorization) == 5 and not (authorization[-1] or '').strip()):\n src_group = None\n ip_protocol, from_port, to_port, cidr_ip = authorization[:4]\n if cidr_ip:\n expected_sets.add((ip_protocol, str(from_port), str(to_port), cidr_ip))\n else:\n ip_protocol, from_port, to_port, cidr_ip, src_group = authorization\n if cidr_ip:\n expected_sets.add((ip_protocol, str(from_port), str(to_port), cidr_ip, src_group))\n\n # Calculate differences and update rules if we own the group.\n if env.vm_ec2_security_group_owner:\n if verbose:\n print('expected_sets:')\n print(expected_sets)\n print('actual_sets:')\n print(actual_sets)\n del_sets = actual_sets.difference(expected_sets)\n if verbose:\n print('del_sets:')\n print(del_sets)\n add_sets = expected_sets.difference(actual_sets)\n if verbose:\n print('add_sets:')\n print(add_sets)\n\n # Revoke deleted.\n for auth in del_sets:\n print(len(auth))\n print('revoking:', auth)\n group.revoke(*auth)\n\n # Create fresh rules.\n for auth in add_sets:\n print('authorizing:', auth)\n group.authorize(*auth)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncreate a new EC2 key pair if it doesn t already exist.", "response": "def get_or_create_ec2_key_pair(name=None, verbose=1):\n \"\"\"\n Creates and saves an EC2 key pair to a local PEM file.\n \"\"\"\n verbose = int(verbose)\n name = name or env.vm_ec2_keypair_name\n pem_path = 'roles/%s/%s.pem' % (env.ROLE, name)\n conn = get_ec2_connection()\n kp = conn.get_key_pair(name)\n if kp:\n print('Key pair %s already exists.' % name)\n else:\n # Note, we only get the private key during creation.\n # If we don't save it here, it's lost forever.\n kp = conn.create_key_pair(name)\n open(pem_path, 'wb').write(kp.material)\n os.system('chmod 600 %s' % pem_path)\n print('Key pair %s created.' % name)\n #return kp\n return pem_path"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_or_create_ec2_instance(name=None, group=None, release=None, verbose=0, backend_opts=None):\n from burlap.common import shelf, OrderedDict\n from boto.exception import EC2ResponseError\n\n assert name, \"A name must be specified.\"\n\n backend_opts = backend_opts or {}\n\n verbose = int(verbose)\n\n conn = get_ec2_connection()\n\n security_groups = get_or_create_ec2_security_groups()\n security_group_ids = [_.id for _ in security_groups]\n if verbose:\n print('security_groups:', security_group_ids)\n\n pem_path = get_or_create_ec2_key_pair()\n\n assert env.vm_ec2_ami, 'No AMI specified.'\n print('Creating EC2 instance from %s...' % (env.vm_ec2_ami,))\n print(env.vm_ec2_zone)\n opts = backend_opts.get('run_instances', {})\n reservation = conn.run_instances(\n env.vm_ec2_ami,\n key_name=env.vm_ec2_keypair_name,\n #security_groups=env.vm_ec2_selected_security_groups,#conflicts with subnet_id?!\n security_group_ids=security_group_ids,\n placement=env.vm_ec2_zone,\n instance_type=env.vm_ec2_instance_type,\n subnet_id=env.vm_ec2_subnet_id,\n **opts\n )\n instance = reservation.instances[0]\n\n # Name new instance.\n # Note, creation is not instantious, so we may have to wait for a moment\n # before we can access it.\n while 1:\n try:\n if name:\n instance.add_tag(env.vm_name_tag, name)\n if group:\n instance.add_tag(env.vm_group_tag, group)\n if release:\n instance.add_tag(env.vm_release_tag, release)\n break\n except EC2ResponseError as e:\n #print('Unable to set tag: %s' % e)\n print('Waiting for the instance to be created...')\n if verbose:\n print(e)\n time.sleep(3)\n\n # Assign IP.\n allocation_id = None\n if env.vm_ec2_use_elastic_ip:\n # Initialize name/ip mapping since we can't tag elastic IPs.\n shelf.setdefault('vm_elastic_ip_mappings', OrderedDict())\n vm_elastic_ip_mappings = shelf.get('vm_elastic_ip_mappings')\n elastic_ip = vm_elastic_ip_mappings.get(name)\n if not elastic_ip:\n print('Allocating new elastic IP address...')\n addr = conn.allocate_address(domain=env.vm_ec2_allocate_address_domain)\n #allocation_id = addr.allocation_id\n #print('allocation_id:',allocation_id)\n elastic_ip = addr.public_ip\n print('Allocated address %s.' % elastic_ip)\n vm_elastic_ip_mappings[name] = str(elastic_ip)\n shelf.set('vm_elastic_ip_mappings', vm_elastic_ip_mappings)\n #conn.get_all_addresses()\n\n # Lookup allocation_id.\n all_eips = conn.get_all_addresses()\n for eip in all_eips:\n if elastic_ip == eip.public_ip:\n allocation_id = eip.allocation_id\n break\n print('allocation_id:', allocation_id)\n\n while 1:\n try:\n conn.associate_address(\n instance_id=instance.id,\n #public_ip=elastic_ip,\n allocation_id=allocation_id, # needed for VPC instances\n )\n print('IP address associated!')\n break\n except EC2ResponseError as e:\n #print('Unable to assign IP: %s' % e)\n print('Waiting to associate IP address...')\n if verbose:\n print(e)\n time.sleep(3)\n\n # Confirm public DNS name was assigned.\n while 1:\n try:\n instance = get_all_ec2_instances(instance_ids=[instance.id])[0]\n #assert instance.public_dns_name, 'No public DNS name found!'\n if instance.public_dns_name:\n break\n except Exception as e:\n print('error:', e)\n except SystemExit as e:\n print('systemexit:', e)\n print('Waiting for public DNS name to be assigned...')\n time.sleep(3)\n\n # Confirm we can SSH into the server.\n #TODO:better handle timeouts? try/except doesn't really work?\n env.connection_attempts = 10\n while 1:\n try:\n with settings(warn_only=True):\n env.host_string = instance.public_dns_name\n ret = run_or_dryrun('who -b')\n #print 'ret.return_code:',ret.return_code\n if not ret.return_code:\n break\n except Exception as e:\n print('error:', e)\n except SystemExit as e:\n print('systemexit:', e)\n print('Waiting for sshd to accept connections...')\n time.sleep(3)\n\n print(\"\")\n print(\"Login with: ssh -o StrictHostKeyChecking=no -i %s %s@%s\" \\\n % (pem_path, env.user, instance.public_dns_name))\n print(\"OR\")\n print(\"fab %(ROLE)s:hostname=%(name)s shell\" % dict(name=name, ROLE=env.ROLE))\n\n ip = socket.gethostbyname(instance.public_dns_name)\n print(\"\")\n print(\"\"\"Example hosts entry:)\n%(ip)s www.mydomain.com # %(name)s\"\"\" % dict(ip=ip, name=name))\n return instance", "response": "Get or create an EC2 instance."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef exists(name=None, group=None, release=None, except_release=None, verbose=1):\n verbose = int(verbose)\n instances = list_instances(\n name=name,\n group=group,\n release=release,\n except_release=except_release,\n verbose=verbose,\n show=verbose)\n ret = bool(instances)\n if verbose:\n print('\\ninstance %s exist' % ('DOES' if ret else 'does NOT'))\n #return ret\n return instances", "response": "Determines if a virtual machine instance exists."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a virtual machine instance.", "response": "def get_or_create(name=None, group=None, config=None, extra=0, verbose=0, backend_opts=None):\n \"\"\"\n Creates a virtual machine instance.\n \"\"\"\n require('vm_type', 'vm_group')\n\n backend_opts = backend_opts or {}\n\n verbose = int(verbose)\n extra = int(extra)\n\n if config:\n config_fn = common.find_template(config)\n config = yaml.load(open(config_fn))\n env.update(config)\n\n env.vm_type = (env.vm_type or '').lower()\n assert env.vm_type, 'No VM type specified.'\n\n group = group or env.vm_group\n assert group, 'No VM group specified.'\n\n ret = exists(name=name, group=group)\n if not extra and ret:\n if verbose:\n print('VM %s:%s exists.' % (name, group))\n return ret\n\n today = datetime.date.today()\n release = int('%i%02i%02i' % (today.year, today.month, today.day))\n\n if not name:\n existing_instances = list_instances(\n group=group,\n release=release,\n verbose=verbose)\n name = env.vm_name_template.format(index=len(existing_instances)+1)\n\n if env.vm_type == EC2:\n return get_or_create_ec2_instance(\n name=name,\n group=group,\n release=release,\n verbose=verbose,\n backend_opts=backend_opts)\n else:\n raise NotImplementedError"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ndelete one or more VM instances from existence.", "response": "def delete(name=None, group=None, release=None, except_release=None,\n dryrun=1, verbose=1):\n \"\"\"\n Permanently erase one or more VM instances from existence.\n \"\"\"\n\n verbose = int(verbose)\n\n if env.vm_type == EC2:\n conn = get_ec2_connection()\n\n instances = list_instances(\n name=name,\n group=group,\n release=release,\n except_release=except_release,\n )\n\n for instance_name, instance_data in instances.items():\n public_dns_name = instance_data['public_dns_name']\n print('\\nDeleting %s (%s)...' \\\n % (instance_name, instance_data['id']))\n if not get_dryrun():\n conn.terminate_instances(instance_ids=[instance_data['id']])\n\n # Clear host key on localhost.\n known_hosts = os.path.expanduser('~/.ssh/known_hosts')\n cmd = 'ssh-keygen -f \"%s\" -R %s' % (known_hosts, public_dns_name)\n local_or_dryrun(cmd)\n\n else:\n raise NotImplementedError"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_name():\n if env.vm_type == EC2:\n for instance in get_all_running_ec2_instances():\n if env.host_string == instance.public_dns_name:\n name = instance.tags.get(env.vm_name_tag)\n return name\n else:\n raise NotImplementedError", "response": "Retrieves the name associated with the current host string."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef respawn(name=None, group=None):\n\n if name is None:\n name = get_name()\n\n delete(name=name, group=group)\n instance = get_or_create(name=name, group=group)\n env.host_string = instance.public_dns_name", "response": "Deletes and recreates one or more VM instances."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef deploy_code(self):\n\n assert self.genv.SITE, 'Site unspecified.'\n assert self.genv.ROLE, 'Role unspecified.'\n\n r = self.local_renderer\n\n if self.env.exclusions:\n r.env.exclusions_str = ' '.join(\n \"--exclude='%s'\" % _ for _ in self.env.exclusions)\n\n r.local(r.env.rsync_command)\n r.sudo('chown -R {rsync_chown_user}:{rsync_chown_group} {rsync_dst_dir}')", "response": "Generate a rsync of all deployable code."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef init_env():\n env.ROLES_DIR = ROLE_DIR\n env.services = []\n env.confirm_deployment = False\n env.is_local = None\n env.base_config_dir = '.'\n env.src_dir = 'src' # The path relative to fab where the code resides.\n env.sites = {} # {site:site_settings}\n env[SITE] = None\n env[ROLE] = None\n\n env.hosts_retriever = None\n env.hosts_retrievers = type(env)() #'default':lambda hostname: hostname,\n\n env.hostname_translator = 'default'\n env.hostname_translators = type(env)()\n env.hostname_translators.default = lambda hostname: hostname\n\n env.default_site = None\n\n # A list of all site names that should be available on the current host.\n env.available_sites = []\n\n # A list of all site names per host.\n # {hostname: [sites]}\n # If no entry found, will use available_sites.\n env.available_sites_by_host = {}\n\n # The command run to determine the percent of disk usage.\n env.disk_usage_command = \"df -H | grep -vE '^Filesystem|tmpfs|cdrom|none' | awk '{print $5 \" \" $1}'\"\n\n env.burlap_data_dir = '.burlap'\n\n env.setdefault('roledefs', {})\n env.setdefault('roles', [])\n env.setdefault('hosts', [])\n env.setdefault('exclude_hosts', [])", "response": "Initializes the environment variables for the node - specific code."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef add_class_methods_as_module_level_functions_for_fabric(instance, module_name, method_name, module_alias=None):\n '''\n Utility to take the methods of the instance of a class, instance,\n and add them as functions to a module, module_name, so that Fabric\n can find and call them. Call this at the bottom of a module after\n the class definition.\n '''\n import imp\n from .decorators import task_or_dryrun\n\n # get the module as an object\n module_obj = sys.modules[module_name]\n\n module_alias = re.sub('[^a-zA-Z0-9]+', '', module_alias or '')\n\n # Iterate over the methods of the class and dynamically create a function\n # for each method that calls the method and add it to the current module\n # NOTE: inspect.ismethod actually executes the methods?!\n #for method in inspect.getmembers(instance, predicate=inspect.ismethod):\n\n method_obj = getattr(instance, method_name)\n\n if not method_name.startswith('_'):\n\n # get the bound method\n func = getattr(instance, method_name)\n\n# if module_name == 'buildbot' or module_alias == 'buildbot':\n# print('-'*80)\n# print('module_name:', module_name)\n# print('method_name:', method_name)\n# print('module_alias:', module_alias)\n# print('module_obj:', module_obj)\n# print('func.module:', func.__module__)\n\n # Convert executable to a Fabric task, if not done so already.\n if not hasattr(func, 'is_task_or_dryrun'):\n func = task_or_dryrun(func)\n\n if module_name == module_alias \\\n or (module_name.startswith('satchels.') and module_name.endswith(module_alias)):\n\n # add the function to the current module\n setattr(module_obj, method_name, func)\n\n else:\n\n # Dynamically create a module for the virtual satchel.\n _module_obj = module_obj\n module_obj = create_module(module_alias)\n setattr(module_obj, method_name, func)\n post_import_modules.add(module_alias)\n\n fabric_name = '%s.%s' % (module_alias or module_name, method_name)\n func.wrapped.__func__.fabric_name = fabric_name\n\n return func", "response": "Utility to take the methods of an instance of a class and instance and add them as functions to a module level module."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nconverting a string of comma delimited values and returns a list.", "response": "def str_to_list(s):\n \"\"\"\n Converts a string of comma delimited values and returns a list.\n \"\"\"\n if s is None:\n return []\n elif isinstance(s, (tuple, list)):\n return s\n elif not isinstance(s, six.string_types):\n raise NotImplementedError('Unknown type: %s' % type(s))\n return [_.strip().lower() for _ in (s or '').split(',') if _.strip()]"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_hosts_retriever(s=None):\n s = s or env.hosts_retriever\n# #assert s, 'No hosts retriever specified.'\n if not s:\n return env_hosts_retriever\n# module_name = '.'.join(s.split('.')[:-1])\n# func_name = s.split('.')[-1]\n# retriever = getattr(importlib.import_module(module_name), func_name)\n# return retriever\n return str_to_callable(s) or env_hosts_retriever", "response": "Given the function name looks up the method for dynamically retrieving host data."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef append_or_dryrun(*args, **kwargs):\n from fabric.contrib.files import append\n\n dryrun = get_dryrun(kwargs.get('dryrun'))\n\n if 'dryrun' in kwargs:\n del kwargs['dryrun']\n\n use_sudo = kwargs.pop('use_sudo', False)\n\n text = args[0] if len(args) >= 1 else kwargs.pop('text')\n\n filename = args[1] if len(args) >= 2 else kwargs.pop('filename')\n\n if dryrun:\n text = text.replace('\\n', '\\\\n')\n cmd = 'echo -e \"%s\" >> %s' % (text, filename)\n cmd_run = 'sudo' if use_sudo else 'run'\n if BURLAP_COMMAND_PREFIX:\n print('%s %s: %s' % (render_command_prefix(), cmd_run, cmd))\n else:\n print(cmd)\n else:\n append(filename=filename, text=text.replace(r'\\n', '\\n'), use_sudo=use_sudo, **kwargs)", "response": "Wrapper around Fabric s contrib. files. append to give it a dryrun option."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef disable_attribute_or_dryrun(*args, **kwargs):\n dryrun = get_dryrun(kwargs.get('dryrun'))\n\n if 'dryrun' in kwargs:\n del kwargs['dryrun']\n\n use_sudo = kwargs.pop('use_sudo', False)\n run_cmd = sudo_or_dryrun if use_sudo else run_or_dryrun\n run_cmd_str = 'sudo' if use_sudo else 'run'\n\n key = args[0] if len(args) >= 1 else kwargs.pop('key')\n\n filename = args[1] if len(args) >= 2 else kwargs.pop('filename')\n\n comment_pattern = args[2] if len(args) >= 3 else kwargs.pop('comment_pattern', r'#\\s*')\n\n equals_pattern = args[3] if len(args) >= 4 else kwargs.pop('equals_pattern', r'\\s*=\\s*')\n\n equals_literal = args[4] if len(args) >= 5 else kwargs.pop('equals_pattern', '=')\n\n context = dict(\n key=key,\n uncommented_literal='%s%s' % (key, equals_literal), # key=value\n uncommented_pattern='%s%s' % (key, equals_pattern), # key = value\n uncommented_pattern_partial='^%s%s[^\\\\n]*' % (key, equals_pattern), # key=\n commented_pattern='%s%s%s' % (comment_pattern, key, equals_pattern), # #key=value\n commented_pattern_partial='^%s%s%s[^\\\\n]*' % (comment_pattern, key, equals_pattern), # #key=\n filename=filename,\n backup=filename+'.bak',\n comment_pattern=comment_pattern,\n equals_pattern=equals_pattern,\n )\n\n cmds = [\n # Replace partial un-commented text with full commented text.\n 'sed -i -r -e \"s/{uncommented_pattern_partial}//g\" {filename}'.format(**context),\n ]\n\n if dryrun:\n for cmd in cmds:\n if BURLAP_COMMAND_PREFIX:\n print('%s %s: %s' % (render_command_prefix(), run_cmd_str, cmd))\n else:\n print(cmd)\n else:\n for cmd in cmds:\n# print('enable attr:', cmd)\n run_cmd(cmd)", "response": "Disable an attribute or dryrun."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef write_temp_file_or_dryrun(content, *args, **kwargs):\n dryrun = get_dryrun(kwargs.get('dryrun'))\n if dryrun:\n fd, tmp_fn = tempfile.mkstemp()\n os.remove(tmp_fn)\n cmd_run = 'local'\n cmd = 'cat <> %s\\n%s\\nEOT' % (tmp_fn, content)\n if BURLAP_COMMAND_PREFIX:\n print('%s %s: %s' % (render_command_prefix(), cmd_run, cmd))\n else:\n print(cmd)\n else:\n fd, tmp_fn = tempfile.mkstemp()\n fout = open(tmp_fn, 'w')\n fout.write(content)\n fout.close()\n return tmp_fn", "response": "Writes the given content to a local temporary file."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef sed_or_dryrun(*args, **kwargs):\n dryrun = get_dryrun(kwargs.get('dryrun'))\n if 'dryrun' in kwargs:\n del kwargs['dryrun']\n\n use_sudo = kwargs.get('use_sudo', False)\n\n if dryrun:\n context = dict(\n filename=args[0] if len(args) >= 1 else kwargs['filename'],\n before=args[1] if len(args) >= 2 else kwargs['before'],\n after=args[2] if len(args) >= 3 else kwargs['after'],\n backup=args[3] if len(args) >= 4 else kwargs.get('backup', '.bak'),\n limit=kwargs.get('limit', ''),\n )\n cmd = 'sed -i{backup} -r -e \"/{limit}/ s/{before}/{after}/g {filename}\"'.format(**context)\n cmd_run = 'sudo' if use_sudo else 'run'\n if BURLAP_COMMAND_PREFIX:\n print('%s %s: %s' % (render_command_prefix(), cmd_run, cmd))\n else:\n print(cmd)\n else:\n from fabric.contrib.files import sed\n sed(*args, **kwargs)", "response": "Wrapper around Fabric s contrib. files. sed to give it a dryrun option."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef pretty_bytes(bytes): # pylint: disable=redefined-builtin\n if not bytes:\n return bytes, 'bytes'\n sign = bytes/float(bytes)\n bytes = abs(bytes)\n for x in ['bytes', 'KB', 'MB', 'GB', 'TB']:\n if bytes < 1024.0:\n #return \"%3.1f %s\" % (bytes, x)\n return sign*bytes, x\n bytes /= 1024.0", "response": "Returns a tuple of the format scaled_float unit_string."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a subset of the env dictionary containing only those keys with the name prefix.", "response": "def get_component_settings(prefixes=None):\n \"\"\"\n Returns a subset of the env dictionary containing\n only those keys with the name prefix.\n \"\"\"\n prefixes = prefixes or []\n assert isinstance(prefixes, (tuple, list)), 'Prefixes must be a sequence type, not %s.' % type(prefixes)\n data = {}\n for name in prefixes:\n name = name.lower().strip()\n for k in sorted(env):\n if k.startswith('%s_' % name):\n new_k = k[len(name)+1:]\n data[new_k] = env[k]\n return data"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_last_modified_timestamp(path, ignore=None):\n ignore = ignore or []\n if not isinstance(path, six.string_types):\n return\n ignore_str = ''\n if ignore:\n assert isinstance(ignore, (tuple, list))\n ignore_str = ' '.join(\"! -name '%s'\" % _ for _ in ignore)\n cmd = 'find \"'+path+'\" ' + ignore_str + ' -type f -printf \"%T@ %p\\n\" | sort -n | tail -1 | cut -f 1 -d \" \"'\n #'find '+path+' -type f -printf \"%T@ %p\\n\" | sort -n | tail -1 | cut -d \" \" -f1\n ret = subprocess.check_output(cmd, shell=True)\n # Note, we round now to avoid rounding errors later on where some formatters\n # use different decimal contexts.\n try:\n ret = round(float(ret), 2)\n except ValueError:\n return\n return ret", "response": "Returns the last modified timestamp of the given directory."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a subset of the env dictionary keys that differ between old and new.", "response": "def check_settings_for_differences(old, new, as_bool=False, as_tri=False):\n \"\"\"\n Returns a subset of the env dictionary keys that differ,\n either being added, deleted or changed between old and new.\n \"\"\"\n\n assert not as_bool or not as_tri\n\n old = old or {}\n new = new or {}\n\n changes = set(k for k in set(new.iterkeys()).intersection(old.iterkeys()) if new[k] != old[k])\n if changes and as_bool:\n return True\n\n added_keys = set(new.iterkeys()).difference(old.iterkeys())\n if added_keys and as_bool:\n return True\n if not as_tri:\n changes.update(added_keys)\n\n deled_keys = set(old.iterkeys()).difference(new.iterkeys())\n if deled_keys and as_bool:\n return True\n if as_bool:\n return False\n if not as_tri:\n changes.update(deled_keys)\n\n if as_tri:\n return added_keys, changes, deled_keys\n\n return changes"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_packager():\n\n # TODO: remove once fabric stops using contextlib.nested.\n # https://github.com/fabric/fabric/issues/1364\n import warnings\n warnings.filterwarnings(\"ignore\", category=DeprecationWarning)\n\n common_packager = get_rc('common_packager')\n if common_packager:\n return common_packager\n #TODO:cache result by current env.host_string so we can handle multiple hosts with different OSes\n with settings(warn_only=True):\n with hide('running', 'stdout', 'stderr', 'warnings'):\n ret = _run('cat /etc/fedora-release')\n if ret.succeeded:\n common_packager = YUM\n else:\n ret = _run('cat /etc/lsb-release')\n if ret.succeeded:\n common_packager = APT\n else:\n for pn in PACKAGERS:\n ret = _run('which %s' % pn)\n if ret.succeeded:\n common_packager = pn\n break\n if not common_packager:\n raise Exception('Unable to determine packager.')\n set_rc('common_packager', common_packager)\n return common_packager", "response": "Returns the packager detected on the remote system."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns a named tuple describing the operating system on the remote host.", "response": "def get_os_version():\n \"\"\"\n Returns a named tuple describing the operating system on the remote host.\n \"\"\"\n\n # TODO: remove once fabric stops using contextlib.nested.\n # https://github.com/fabric/fabric/issues/1364\n import warnings\n warnings.filterwarnings(\"ignore\", category=DeprecationWarning)\n\n common_os_version = get_rc('common_os_version')\n if common_os_version:\n return common_os_version\n with settings(warn_only=True):\n with hide('running', 'stdout', 'stderr', 'warnings'):\n\n ret = _run_or_local('cat /etc/lsb-release')\n if ret.succeeded:\n return OS(\n type=LINUX,\n distro=UBUNTU,\n release=re.findall(r'DISTRIB_RELEASE=([0-9\\.]+)', ret)[0])\n\n ret = _run_or_local('cat /etc/debian_version')\n if ret.succeeded:\n return OS(\n type=LINUX,\n distro=DEBIAN,\n release=re.findall(r'([0-9\\.]+)', ret)[0])\n\n ret = _run_or_local('cat /etc/fedora-release')\n if ret.succeeded:\n return OS(\n type=LINUX,\n distro=FEDORA,\n release=re.findall(r'release ([0-9]+)', ret)[0])\n\n raise Exception('Unable to determine OS version.')"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef render_to_string(template, extra=None):\n from jinja2 import Template\n extra = extra or {}\n final_fqfn = find_template(template)\n assert final_fqfn, 'Template not found: %s' % template\n template_content = open(final_fqfn, 'r').read()\n t = Template(template_content)\n if extra:\n context = env.copy()\n context.update(extra)\n else:\n context = env\n rendered_content = t.render(**context)\n rendered_content = rendered_content.replace('"', '\"')\n return rendered_content", "response": "Renders the given template to a string."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nrenders a template to a local file.", "response": "def render_to_file(template, fn=None, extra=None, **kwargs):\n \"\"\"\n Returns a template to a local file.\n If no filename given, a temporary filename will be generated and returned.\n \"\"\"\n import tempfile\n dryrun = get_dryrun(kwargs.get('dryrun'))\n append_newline = kwargs.pop('append_newline', True)\n style = kwargs.pop('style', 'cat') # |echo\n formatter = kwargs.pop('formatter', None)\n content = render_to_string(template, extra=extra)\n if append_newline and not content.endswith('\\n'):\n content += '\\n'\n\n if formatter and callable(formatter):\n content = formatter(content)\n\n if dryrun:\n if not fn:\n fd, fn = tempfile.mkstemp()\n fout = os.fdopen(fd, 'wt')\n fout.close()\n else:\n if fn:\n fout = open(fn, 'w')\n else:\n fd, fn = tempfile.mkstemp()\n fout = os.fdopen(fd, 'wt')\n fout.write(content)\n fout.close()\n assert fn\n\n if style == 'cat':\n cmd = 'cat < %s\\n%s\\nEOF' % (fn, content)\n elif style == 'echo':\n cmd = 'echo -e %s > %s' % (shellquote(content), fn)\n else:\n raise NotImplementedError\n\n if BURLAP_COMMAND_PREFIX:\n print('%s run: %s' % (render_command_prefix(), cmd))\n else:\n print(cmd)\n\n return fn"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef install_config(local_path=None, remote_path=None, render=True, extra=None, formatter=None):\n local_path = find_template(local_path)\n if render:\n extra = extra or {}\n local_path = render_to_file(template=local_path, extra=extra, formatter=formatter)\n put_or_dryrun(local_path=local_path, remote_path=remote_path, use_sudo=True)", "response": "Installs a new config file to a remote file."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef iter_sites(sites=None, site=None, renderer=None, setter=None, no_secure=False, verbose=None):\n if verbose is None:\n verbose = get_verbose()\n\n hostname = get_current_hostname()\n\n target_sites = env.available_sites_by_host.get(hostname, None)\n\n if sites is None:\n site = site or env.SITE or ALL\n if site == ALL:\n sites = list(six.iteritems(env.sites))\n else:\n sys.stderr.flush()\n sites = [(site, env.sites.get(site))]\n\n renderer = renderer #or render_remote_paths\n env_default = save_env()\n for _site, site_data in sorted(sites):\n if no_secure and _site.endswith('_secure'):\n continue\n\n # Only load site configurations that are allowed for this host.\n if target_sites is None:\n pass\n else:\n assert isinstance(target_sites, (tuple, list))\n if _site not in target_sites:\n if verbose:\n print('Skipping site %s because not in among target sites.' % _site)\n continue\n\n env.update(env_default)\n env.update(env.sites.get(_site, {}))\n env.SITE = _site\n if callable(renderer):\n renderer()\n if setter:\n setter(_site)\n yield _site, site_data\n\n # Revert modified keys.\n env.update(env_default)\n\n # Remove keys that were added, not simply updated.\n added_keys = set(env).difference(env_default)\n for key in added_keys:\n # Don't remove internally maintained variables, because these are used to cache hostnames\n # used by iter_sites().\n if key.startswith('_'):\n continue\n del env[key]", "response": "Iterate over sites and set environment variables for each site."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef topological_sort(source):\n if isinstance(source, dict):\n source = source.items()\n pending = sorted([(name, set(deps)) for name, deps in source]) # copy deps so we can modify set in-place\n emitted = []\n while pending:\n next_pending = []\n next_emitted = []\n for entry in pending:\n name, deps = entry\n deps.difference_update(emitted) # remove deps we emitted last pass\n if deps: # still has deps? recheck during next pass\n next_pending.append(entry)\n else: # no more deps? time to emit\n yield name\n emitted.append(name) # <-- not required, but helps preserve original ordering\n next_emitted.append(name) # remember what we emitted for difference_update() in next pass\n if not next_emitted: # all entries have unmet deps, one of two things is wrong...\n raise ValueError(\"cyclic or missing dependancy detected: %r\" % (next_pending,))\n pending = next_pending\n emitted = next_emitted", "response": "perform topo sort on elements."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns a list of hosts that have been configured to support the given site.", "response": "def get_hosts_for_site(site=None):\n \"\"\"\n Returns a list of hosts that have been configured to support the given site.\n \"\"\"\n site = site or env.SITE\n hosts = set()\n for hostname, _sites in six.iteritems(env.available_sites_by_host):\n# print('checking hostname:',hostname, _sites)\n for _site in _sites:\n if _site == site:\n# print( '_site:',_site)\n host_ip = get_host_ip(hostname)\n# print( 'host_ip:',host_ip)\n if host_ip:\n hosts.add(host_ip)\n break\n return list(hosts)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns a copy of the global environment with all the local variables copied back into it.", "response": "def collect_genv(self, include_local=True, include_global=True):\n \"\"\"\n Returns a copy of the global environment with all the local variables copied back into it.\n \"\"\"\n e = type(self.genv)()\n if include_global:\n e.update(self.genv)\n if include_local:\n for k, v in self.lenv.items():\n e['%s_%s' % (self.obj.name.lower(), k)] = v\n return e"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nsetting the default values for the satchel.", "response": "def _set_defaults(self):\n \"\"\"\n Wrapper around the overrideable set_defaults().\n \"\"\"\n\n # Register an \"enabled\" flag on all satchels.\n # How this flag is interpreted depends on the individual satchel.\n _prefix = '%s_enabled' % self.name\n if _prefix not in env:\n env[_prefix] = True\n\n # Do a one-time init of custom defaults.\n _key = '_%s' % self.name\n if _key not in env:\n env[_key] = True\n self.set_defaults()"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef register(self):\n\n self._set_defaults()\n\n all_satchels[self.name.upper()] = self\n\n manifest_recorder[self.name] = self.record_manifest\n\n # Register service commands.\n if self.required_system_packages:\n required_system_packages[self.name.upper()] = self.required_system_packages", "response": "Register this satchel to the global registeries for fast lookup from other satchels."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef unregister(self):\n\n for k in list(env.keys()):\n if k.startswith(self.env_prefix):\n del env[k]\n\n try:\n del all_satchels[self.name.upper()]\n except KeyError:\n pass\n\n try:\n del manifest_recorder[self.name]\n except KeyError:\n pass\n\n try:\n del manifest_deployers[self.name.upper()]\n except KeyError:\n pass\n\n try:\n del manifest_deployers_befores[self.name.upper()]\n except KeyError:\n pass\n\n try:\n del required_system_packages[self.name.upper()]\n except KeyError:\n pass", "response": "Removes this satchel from the global registeries."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_tasks(self):\n tasks = set(self.tasks)#DEPRECATED\n for _name in dir(self):\n # Skip properties so we don't accidentally execute any methods.\n if isinstance(getattr(type(self), _name, None), property):\n continue\n attr = getattr(self, _name)\n if hasattr(attr, '__call__') and getattr(attr, 'is_task', False):\n tasks.add(_name)\n return sorted(tasks)", "response": "Returns an ordered list of all task names."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef local_renderer(self):\n if not self._local_renderer:\n r = self.create_local_renderer()\n self._local_renderer = r\n return self._local_renderer", "response": "Returns the cached local renderer."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef all_other_enabled_satchels(self):\n return dict(\n (name, satchel)\n for name, satchel in self.all_satchels.items()\n if name != self.name.upper() and name.lower() in map(str.lower, self.genv.services)\n )", "response": "Returns a dictionary of all satchels used in the current configuration excluding ourselves."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns a version of env filtered to only include the variables in our namespace.", "response": "def lenv(self):\n \"\"\"\n Returns a version of env filtered to only include the variables in our namespace.\n \"\"\"\n _env = type(env)()\n for _k, _v in six.iteritems(env):\n if _k.startswith(self.name+'_'):\n _env[_k[len(self.name)+1:]] = _v\n return _env"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef param_changed_to(self, key, to_value, from_value=None):\n last_value = getattr(self.last_manifest, key)\n current_value = self.current_manifest.get(key)\n if from_value is not None:\n return last_value == from_value and current_value == to_value\n return last_value != to_value and current_value == to_value", "response": "Returns true if the given parameter with name key has transitioned to the given value."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nset the site specific settings for the current site.", "response": "def set_site_specifics(self, site):\n \"\"\"\n Loads settings for the target site.\n \"\"\"\n r = self.local_renderer\n site_data = self.genv.sites[site].copy()\n r.env.site = site\n if self.verbose:\n print('set_site_specifics.data:')\n pprint(site_data, indent=4)\n\n # Remove local namespace settings from the global namespace\n # by converting _ to .\n local_ns = {}\n for k, v in list(site_data.items()):\n if k.startswith(self.name + '_'):\n _k = k[len(self.name + '_'):]\n local_ns[_k] = v\n del site_data[k]\n\n r.env.update(local_ns)\n r.env.update(site_data)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef vprint(self, *args, **kwargs):\n if self.verbose:\n curframe = inspect.currentframe()\n calframe = inspect.getouterframes(curframe, 2)\n caller_name = calframe[1][3]\n prefix = '%s.%s:' % (self.name.lower(), caller_name)\n print(prefix, *args, **kwargs)", "response": "Print the current caller s name and the current caller s name."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_package_list(self):\n os_version = self.os_version # OS(type=LINUX, distro=UBUNTU, release='14.04')\n self.vprint('os_version:', os_version)\n\n # Lookup legacy package list.\n # OS: [package1, package2, ...],\n req_packages1 = self.required_system_packages\n if req_packages1:\n deprecation('The required_system_packages attribute is deprecated, '\n 'use the packager_system_packages property instead.')\n\n # Lookup new package list.\n # OS: [package1, package2, ...],\n req_packages2 = self.packager_system_packages\n\n patterns = [\n (os_version.type, os_version.distro, os_version.release),\n (os_version.distro, os_version.release),\n (os_version.type, os_version.distro),\n (os_version.distro,),\n os_version.distro,\n ]\n self.vprint('req_packages1:', req_packages1)\n self.vprint('req_packages2:', req_packages2)\n package_list = None\n found = False\n for pattern in patterns:\n self.vprint('pattern:', pattern)\n for req_packages in (req_packages1, req_packages2):\n if pattern in req_packages:\n package_list = req_packages[pattern]\n found = True\n break\n if not found:\n print('Warning: No operating system pattern found for %s' % (os_version,))\n self.vprint('package_list:', package_list)\n return package_list", "response": "Returns a list of all required packages."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef install_packages(self):\n os_version = self.os_version\n package_list = self.get_package_list()\n if package_list:\n package_list_str = ' '.join(package_list)\n if os_version.distro == UBUNTU:\n self.sudo('apt-get update --fix-missing; DEBIAN_FRONTEND=noninteractive apt-get install --yes %s' % package_list_str)\n elif os_version.distro == DEBIAN:\n self.sudo('apt-get update --fix-missing; DEBIAN_FRONTEND=noninteractive apt-get install --yes %s' % package_list_str)\n elif os_version.distro == FEDORA:\n self.sudo('yum install --assumeyes %s' % package_list_str)\n else:\n raise NotImplementedError('Unknown distro: %s' % os_version.distro)", "response": "Installs all required packages listed for this satchel."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nlike run but re - runs the command for each site in the current role.", "response": "def run_on_all_sites(self, cmd, *args, **kwargs):\n \"\"\"\n Like run(), but re-runs the command for each site in the current role.\n \"\"\"\n r = self.local_renderer\n for _site, _data in iter_sites():\n r.env.SITE = _site\n with self.settings(warn_only=True):\n r.run('export SITE={SITE}; export ROLE={ROLE}; '+cmd)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef file_contains(self, *args, **kwargs):\n from fabric.contrib.files import contains\n return contains(*args, **kwargs)", "response": "returns True if the file contains the key"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning a dictionary representing a serialized state of the service.", "response": "def record_manifest(self):\n \"\"\"\n Returns a dictionary representing a serialized state of the service.\n \"\"\"\n manifest = get_component_settings(prefixes=[self.name])\n\n # Record a signature of each template so we know to redeploy when they change.\n for template in self.get_templates():\n # Dereference brace notation. e.g. convert '{var}' to `env[var]`.\n if template and template.startswith('{') and template.endswith('}'):\n template = self.env[template[1:-1]]\n\n if not template:\n continue\n\n if template.startswith('%s/' % self.name):\n fqfn = self.find_template(template)\n else:\n fqfn = self.find_template('%s/%s' % (self.name, template))\n assert fqfn, 'Unable to find template: %s/%s' % (self.name, template)\n manifest['_%s' % template] = get_file_hash(fqfn)\n\n for tracker in self.get_trackers():\n manifest['_tracker_%s' % tracker.get_natural_key_hash()] = tracker.get_thumbprint()\n\n if self.verbose:\n pprint(manifest, indent=4)\n\n return manifest"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn true if at least one tracker detects a change.", "response": "def has_changes(self):\n \"\"\"\n Returns true if at least one tracker detects a change.\n \"\"\"\n lm = self.last_manifest\n for tracker in self.get_trackers():\n last_thumbprint = lm['_tracker_%s' % tracker.get_natural_key_hash()]\n if tracker.is_changed(last_thumbprint):\n return True\n return False"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nconfiguring the manifest based on the last manifest entry.", "response": "def configure(self):\n \"\"\"\n The standard method called to apply functionality when the manifest changes.\n \"\"\"\n lm = self.last_manifest\n for tracker in self.get_trackers():\n self.vprint('Checking tracker:', tracker)\n last_thumbprint = lm['_tracker_%s' % tracker.get_natural_key_hash()]\n self.vprint('last thumbprint:', last_thumbprint)\n has_changed = tracker.is_changed(last_thumbprint)\n self.vprint('Tracker changed:', has_changed)\n if has_changed:\n self.vprint('Change detected!')\n tracker.act()"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nchecking if a PostgreSQL user exists.", "response": "def user_exists(name):\n \"\"\"\n Check if a PostgreSQL user exists.\n \"\"\"\n with settings(hide('running', 'stdout', 'stderr', 'warnings'),\n warn_only=True):\n res = _run_as_pg('''psql -t -A -c \"SELECT COUNT(*) FROM pg_user WHERE usename = '%(name)s';\"''' % locals())\n return (res == \"1\")"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef create_user(name, password, superuser=False, createdb=False,\n createrole=False, inherit=True, login=True,\n connection_limit=None, encrypted_password=False):\n \"\"\"\n Create a PostgreSQL user.\n\n Example::\n\n import burlap\n\n # Create DB user if it does not exist\n if not burlap.postgres.user_exists('dbuser'):\n burlap.postgres.create_user('dbuser', password='somerandomstring')\n\n # Create DB user with custom options\n burlap.postgres.create_user('dbuser2', password='s3cr3t',\n createdb=True, createrole=True, connection_limit=20)\n\n \"\"\"\n options = [\n 'SUPERUSER' if superuser else 'NOSUPERUSER',\n 'CREATEDB' if createdb else 'NOCREATEDB',\n 'CREATEROLE' if createrole else 'NOCREATEROLE',\n 'INHERIT' if inherit else 'NOINHERIT',\n 'LOGIN' if login else 'NOLOGIN',\n ]\n if connection_limit is not None:\n options.append('CONNECTION LIMIT %d' % connection_limit)\n password_type = 'ENCRYPTED' if encrypted_password else 'UNENCRYPTED'\n options.append(\"%s PASSWORD '%s'\" % (password_type, password))\n options = ' '.join(options)\n _run_as_pg('''psql -c \"CREATE USER %(name)s %(options)s;\"''' % locals())", "response": "Create a PostgreSQL user."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nwrite the file used to store login credentials for PostgreSQL.", "response": "def write_pgpass(self, name=None, site=None, use_sudo=0, root=0):\n \"\"\"\n Write the file used to store login credentials for PostgreSQL.\n \"\"\"\n\n r = self.database_renderer(name=name, site=site)\n\n root = int(root)\n\n use_sudo = int(use_sudo)\n\n r.run('touch {pgpass_path}')\n if '~' in r.env.pgpass_path:\n r.run('chmod {pgpass_chmod} {pgpass_path}')\n else:\n r.sudo('chmod {pgpass_chmod} {pgpass_path}')\n\n if root:\n r.env.shell_username = r.env.get('db_root_username', 'postgres')\n r.env.shell_password = r.env.get('db_root_password', 'password')\n else:\n r.env.shell_username = r.env.db_user\n r.env.shell_password = r.env.db_password\n\n r.append(\n '{db_host}:{port}:*:{shell_username}:{shell_password}',\n r.env.pgpass_path,\n use_sudo=use_sudo)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef dumpload(self, site=None, role=None):\n r = self.database_renderer(site=site, role=role)\n r.run('pg_dump -c --host={host_string} --username={db_user} '\n '--blobs --format=c {db_name} -n public | '\n 'pg_restore -U {db_postgresql_postgres_user} --create '\n '--dbname={db_name}')", "response": "Dumps and loads a database snapshot simultaneously."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn True if a database with the given name exists. False otherwise.", "response": "def exists(self, name='default', site=None, use_root=False):\n \"\"\"\n Returns true if a database with the given name exists. False otherwise.\n \"\"\"\n\n r = self.database_renderer(name=name, site=site)\n\n if int(use_root):\n kwargs = dict(\n db_user=r.env.get('db_root_username', 'postgres'),\n db_password=r.env.get('db_root_password', 'password'),\n db_host=r.env.db_host,\n db_name=r.env.db_name,\n )\n r.env.update(kwargs)\n\n # Set pgpass file.\n if r.env.db_password:\n self.write_pgpass(name=name, root=use_root)\n\n# cmd = ('psql --username={db_user} --no-password -l '\\\n# '--host={db_host} --dbname={db_name}'\\\n# '| grep {db_name} | wc -l').format(**env)\n\n ret = None\n with settings(warn_only=True):\n ret = r.run('psql --username={db_user} --host={db_host} -l '\\\n '| grep {db_name} | wc -l')\n if ret is not None:\n if 'password authentication failed' in ret:\n ret = False\n else:\n ret = int(ret) >= 1\n\n if ret is not None:\n print('%s database on site %s %s exist' % (name, self.genv.SITE, 'DOES' if ret else 'DOES NOT'))\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nloading a database snapshot into the target database server.", "response": "def load(self, dump_fn='', prep_only=0, force_upload=0, from_local=0, name=None, site=None, dest_dir=None, force_host=None):\n \"\"\"\n Restores a database snapshot onto the target database server.\n\n If prep_only=1, commands for preparing the load will be generated,\n but not the command to finally load the snapshot.\n \"\"\"\n\n r = self.database_renderer(name=name, site=site)\n\n # Render the snapshot filename.\n r.env.dump_fn = self.get_default_db_fn(fn_template=dump_fn, dest_dir=dest_dir)\n\n from_local = int(from_local)\n\n prep_only = int(prep_only)\n\n missing_local_dump_error = r.format(\n \"Database dump file {dump_fn} does not exist.\"\n )\n\n # Copy snapshot file to target.\n if self.is_local:\n r.env.remote_dump_fn = dump_fn\n else:\n r.env.remote_dump_fn = '/tmp/' + os.path.split(r.env.dump_fn)[-1]\n\n if not prep_only and not self.is_local:\n if not self.dryrun:\n assert os.path.isfile(r.env.dump_fn), missing_local_dump_error\n #r.pc('Uploading PostgreSQL database snapshot...')\n# r.put(\n# local_path=r.env.dump_fn,\n# remote_path=r.env.remote_dump_fn)\n #r.local('rsync -rvz --progress --no-p --no-g '\n #'--rsh \"ssh -o StrictHostKeyChecking=no -i {key_filename}\" '\n #'{dump_fn} {user}@{host_string}:{remote_dump_fn}')\n self.upload_snapshot(name=name, site=site, local_dump_fn=r.env.dump_fn, remote_dump_fn=r.env.remote_dump_fn)\n\n if self.is_local and not prep_only and not self.dryrun:\n assert os.path.isfile(r.env.dump_fn), missing_local_dump_error\n\n if force_host:\n r.env.db_host = force_host\n\n with settings(warn_only=True):\n r.sudo('dropdb --if-exists --no-password --user={db_root_username} --host={db_host} {db_name}', user=r.env.postgres_user)\n\n r.sudo('psql --no-password --user={db_root_username} --host={db_host} -c \"CREATE DATABASE {db_name};\"', user=r.env.postgres_user)\n\n with settings(warn_only=True):\n\n if r.env.engine == POSTGIS:\n r.sudo('psql --user={db_root_username} --no-password --dbname={db_name} --host={db_host} --command=\"CREATE EXTENSION postgis;\"',\n user=r.env.postgres_user)\n r.sudo('psql --user={db_root_username} --no-password --dbname={db_name} --host={db_host} --command=\"CREATE EXTENSION postgis_topology;\"',\n user=r.env.postgres_user)\n\n with settings(warn_only=True):\n r.sudo('psql --user={db_root_username} --host={db_host} -c \"REASSIGN OWNED BY {db_user} TO {db_root_username};\"', user=r.env.postgres_user)\n\n with settings(warn_only=True):\n r.sudo('psql --user={db_root_username} --host={db_host} -c \"DROP OWNED BY {db_user} CASCADE;\"', user=r.env.postgres_user)\n\n r.sudo('psql --user={db_root_username} --host={db_host} -c \"DROP USER IF EXISTS {db_user}; '\n 'CREATE USER {db_user} WITH PASSWORD \\'{db_password}\\'; '\n 'GRANT ALL PRIVILEGES ON DATABASE {db_name} to {db_user};\"', user=r.env.postgres_user)\n for createlang in r.env.createlangs:\n r.env.createlang = createlang\n r.sudo('createlang -U {db_root_username} --host={db_host} {createlang} {db_name} || true', user=r.env.postgres_user)\n\n if not prep_only:\n # Ignore errors needed to work around bug \"ERROR: schema \"public\" already exists\", which is thrown in 9.6 even if we use --clean and --if-exists?\n with settings(warn_only=True):\n r.sudo(r.env.load_command, user=r.env.postgres_user)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef shell(self, name='default', site=None, **kwargs):\n r = self.database_renderer(name=name, site=site)\n self.write_pgpass(name=name, site=site, root=True)\n\n db_name = kwargs.get('db_name')\n if db_name:\n r.env.db_name = db_name\n r.run('/bin/bash -i -c \"psql --username={db_root_username} --host={db_host} --dbname={db_name}\"')\n else:\n r.run('/bin/bash -i -c \"psql --username={db_root_username} --host={db_host}\"')", "response": "Opens a SQL shell to the given database."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef drop_database(self, name):\n with settings(warn_only=True):\n self.sudo('dropdb %s' % (name,), user='postgres')", "response": "Drop a PostgreSQL database."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nloads a table from one database to another.", "response": "def load_table(self, table_name, src, dst='localhost', name=None, site=None):\n \"\"\"\n Directly transfers a table between two databases.\n \"\"\"\n #TODO: incomplete\n r = self.database_renderer(name=name, site=site)\n r.env.table_name = table_name\n r.run('psql --user={dst_db_user} --host={dst_db_host} --command=\"DROP TABLE IF EXISTS {table_name} CASCADE;\"')\n r.run('pg_dump -t {table_name} --user={dst_db_user} --host={dst_db_host} | psql --user={src_db_user} --host={src_db_host}')"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngets the list of network interfaces on SmartOS.", "response": "def interfaces():\n \"\"\"\n Get the list of network interfaces. Will return all datalinks on SmartOS.\n \"\"\"\n with settings(hide('running', 'stdout')):\n if is_file('/usr/sbin/dladm'):\n res = run('/usr/sbin/dladm show-link')\n else:\n res = sudo('/sbin/ifconfig -s')\n return [line.split(' ')[0] for line in res.splitlines()[1:]]"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngets the IPv4 address assigned to an interface.", "response": "def address(interface):\n \"\"\"\n Get the IPv4 address assigned to an interface.\n\n Example::\n\n import burlap\n\n # Print all configured IP addresses\n for interface in burlap.network.interfaces():\n print(burlap.network.address(interface))\n\n \"\"\"\n with settings(hide('running', 'stdout')):\n res = (sudo(\"/sbin/ifconfig %(interface)s | grep 'inet '\" % locals()) or '').split('\\n')[-1].strip()\n if 'addr' in res:\n return res.split()[1].split(':')[1]\n return res.split()[1]"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn a dictionary representing a serialized state of the service manifest.", "response": "def record_manifest(self):\n \"\"\"\n Returns a dictionary representing a serialized state of the service.\n \"\"\"\n data = {}\n data['required_packages'] = self.install_required(type=SYSTEM, verbose=False, list_only=True)\n data['required_packages'].sort()\n data['custom_packages'] = self.install_custom(list_only=True)\n data['custom_packages'].sort()\n data['repositories'] = self.get_repositories()\n return data"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef update(self):\n packager = self.packager\n if packager == APT:\n self.sudo('DEBIAN_FRONTEND=noninteractive apt-get -yq update')\n elif packager == YUM:\n self.sudo('yum update')\n else:\n raise Exception('Unknown packager: %s' % (packager,))", "response": "Update the current user s apt or yum."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ninstalls system packages listed in apt - requirements. txt.", "response": "def install_apt(self, fn=None, package_name=None, update=0, list_only=0):\n \"\"\"\n Installs system packages listed in apt-requirements.txt.\n \"\"\"\n r = self.local_renderer\n assert self.genv[ROLE]\n apt_req_fqfn = fn or (self.env.apt_requirments_fn and self.find_template(self.env.apt_requirments_fn))\n if not apt_req_fqfn:\n return []\n assert os.path.isfile(apt_req_fqfn)\n\n lines = list(self.env.apt_packages or [])\n for _ in open(apt_req_fqfn).readlines():\n if _.strip() and not _.strip().startswith('#') \\\n and (not package_name or _.strip() == package_name):\n lines.extend(_pkg.strip() for _pkg in _.split(' ') if _pkg.strip())\n\n if list_only:\n return lines\n\n tmp_fn = r.write_temp_file('\\n'.join(lines))\n apt_req_fqfn = tmp_fn\n\n if not self.genv.is_local:\n r.put(local_path=tmp_fn, remote_path=tmp_fn)\n apt_req_fqfn = self.genv.put_remote_path\n r.sudo('DEBIAN_FRONTEND=noninteractive apt-get -yq update --fix-missing')\n r.sudo('DEBIAN_FRONTEND=noninteractive apt-get -yq install `cat \"%s\" | tr \"\\\\n\" \" \"`' % apt_req_fqfn)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ninstall system packages listed in yum - requirements. txt.", "response": "def install_yum(self, fn=None, package_name=None, update=0, list_only=0):\n \"\"\"\n Installs system packages listed in yum-requirements.txt.\n \"\"\"\n assert self.genv[ROLE]\n yum_req_fn = fn or self.find_template(self.genv.yum_requirments_fn)\n if not yum_req_fn:\n return []\n assert os.path.isfile(yum_req_fn)\n update = int(update)\n if list_only:\n return [\n _.strip() for _ in open(yum_req_fn).readlines()\n if _.strip() and not _.strip.startswith('#')\n and (not package_name or _.strip() == package_name)\n ]\n if update:\n self.sudo_or_dryrun('yum update --assumeyes')\n if package_name:\n self.sudo_or_dryrun('yum install --assumeyes %s' % package_name)\n else:\n if self.genv.is_local:\n self.put_or_dryrun(local_path=yum_req_fn)\n yum_req_fn = self.genv.put_remote_fn\n self.sudo_or_dryrun('yum install --assumeyes $(cat %(yum_req_fn)s)' % yum_req_fn)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef install_custom(self, *args, **kwargs):\n if not self.env.manage_custom:\n return\n packager = self.packager\n if packager == APT:\n return self.install_apt(*args, **kwargs)\n elif packager == YUM:\n return self.install_yum(*args, **kwargs)\n else:\n raise NotImplementedError('Unknown packager: %s' % (packager,))", "response": "Installs all system packages listed in the appropriate\n - requirements. txt."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nupdates or upgrades all system packages.", "response": "def refresh(self, *args, **kwargs):\n \"\"\"\n Updates/upgrades all system packages.\n \"\"\"\n r = self.local_renderer\n packager = self.packager\n if packager == APT:\n r.sudo('DEBIAN_FRONTEND=noninteractive apt-get -yq update --fix-missing')\n elif packager == YUM:\n raise NotImplementedError\n #return upgrade_yum(*args, **kwargs)\n else:\n raise Exception('Unknown packager: %s' % (packager,))"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef upgrade(self, full=0):\n full = int(full)\n r = self.local_renderer\n packager = self.packager\n if packager == APT:\n r.sudo('DEBIAN_FRONTEND=noninteractive apt-get -yq upgrade')\n if full:\n r.sudo('DEBIAN_FRONTEND=noninteractive apt-get dist-upgrade -yq')\n elif packager == YUM:\n raise NotImplementedError\n else:\n raise Exception('Unknown packager: %s' % (packager,))", "response": "Updates or upgrades all system packages."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef list_required(self, type=None, service=None): # pylint: disable=redefined-builtin\n from burlap.common import (\n required_system_packages,\n required_python_packages,\n required_ruby_packages,\n )\n service = (service or '').strip().upper()\n type = (type or '').lower().strip()\n assert not type or type in PACKAGE_TYPES, 'Unknown package type: %s' % (type,)\n packages_set = set()\n packages = []\n version = self.os_version\n\n for _service, satchel in self.all_other_enabled_satchels.items():\n\n _service = _service.strip().upper()\n if service and service != _service:\n continue\n\n _new = []\n\n if not type or type == SYSTEM:\n\n #TODO:deprecated, remove\n _new.extend(required_system_packages.get(\n _service, {}).get((version.distro, version.release), []))\n\n try:\n _pkgs = satchel.packager_system_packages\n if self.verbose:\n print('pkgs:')\n pprint(_pkgs, indent=4)\n for _key in [(version.distro, version.release), version.distro]:\n if self.verbose:\n print('checking key:', _key)\n if _key in _pkgs:\n if self.verbose:\n print('satchel %s requires:' % satchel, _pkgs[_key])\n _new.extend(_pkgs[_key])\n break\n except AttributeError:\n pass\n\n if not type or type == PYTHON:\n\n #TODO:deprecated, remove\n _new.extend(required_python_packages.get(\n _service, {}).get((version.distro, version.release), []))\n\n try:\n _pkgs = satchel.packager_python_packages\n for _key in [(version.distro, version.release), version.distro]:\n if _key in _pkgs:\n _new.extend(_pkgs[_key])\n except AttributeError:\n pass\n print('_new:', _new)\n\n if not type or type == RUBY:\n\n #TODO:deprecated, remove\n _new.extend(required_ruby_packages.get(\n _service, {}).get((version.distro, version.release), []))\n\n for _ in _new:\n if _ in packages_set:\n continue\n packages_set.add(_)\n packages.append(_)\n if self.verbose:\n for package in sorted(packages):\n print('package:', package)\n return packages", "response": "Displays all packages required by the current role."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef install_required(self, type=None, service=None, list_only=0, **kwargs): # pylint: disable=redefined-builtin\n r = self.local_renderer\n list_only = int(list_only)\n type = (type or '').lower().strip()\n assert not type or type in PACKAGE_TYPES, 'Unknown package type: %s' % (type,)\n lst = []\n if type:\n types = [type]\n else:\n types = PACKAGE_TYPES\n for _type in types:\n if _type == SYSTEM:\n content = '\\n'.join(self.list_required(type=_type, service=service))\n if list_only:\n lst.extend(_ for _ in content.split('\\n') if _.strip())\n if self.verbose:\n print('content:', content)\n break\n fd, fn = tempfile.mkstemp()\n fout = open(fn, 'w')\n fout.write(content)\n fout.close()\n self.install_custom(fn=fn)\n else:\n raise NotImplementedError\n return lst", "response": "Installs system packages listed as required by services this host uses."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef uninstall_blacklisted(self):\n from burlap.system import distrib_family\n blacklisted_packages = self.env.blacklisted_packages\n if not blacklisted_packages:\n print('No blacklisted packages.')\n return\n else:\n family = distrib_family()\n if family == DEBIAN:\n self.sudo('DEBIAN_FRONTEND=noninteractive apt-get -yq purge %s' % ' '.join(blacklisted_packages))\n else:\n raise NotImplementedError('Unknown family: %s' % family)", "response": "Uninstalls all blacklisted packages."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ndeploy all crontabs to the host.", "response": "def deploy(self, site=None):\n \"\"\"\n Writes entire crontab to the host.\n \"\"\"\n r = self.local_renderer\n\n self.deploy_logrotate()\n\n cron_crontabs = []\n# if self.verbose:\n# print('hostname: \"%s\"' % (hostname,), file=sys.stderr)\n for _site, site_data in self.iter_sites(site=site):\n r.env.cron_stdout_log = r.format(r.env.stdout_log_template)\n r.env.cron_stderr_log = r.format(r.env.stderr_log_template)\n r.sudo('touch {cron_stdout_log}')\n r.sudo('touch {cron_stderr_log}')\n r.sudo('sudo chown {user}:{user} {cron_stdout_log}')\n r.sudo('sudo chown {user}:{user} {cron_stderr_log}')\n\n if self.verbose:\n print('site:', site, file=sys.stderr)\n print('env.crontabs_selected:', self.env.crontabs_selected, file=sys.stderr)\n\n for selected_crontab in self.env.crontabs_selected:\n lines = self.env.crontabs_available.get(selected_crontab, [])\n if self.verbose:\n print('lines:', lines, file=sys.stderr)\n for line in lines:\n cron_crontabs.append(r.format(line))\n\n if not cron_crontabs:\n return\n\n cron_crontabs = self.env.crontab_headers + cron_crontabs\n cron_crontabs.append('\\n')\n r.env.crontabs_rendered = '\\n'.join(cron_crontabs)\n fn = self.write_to_file(content=r.env.crontabs_rendered)\n print('fn:', fn)\n r.env.put_remote_path = r.put(local_path=fn)\n if isinstance(r.env.put_remote_path, (tuple, list)):\n r.env.put_remote_path = r.env.put_remote_path[0]\n r.sudo('crontab -u {cron_user} {put_remote_path}')"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef load(self, dump_fn='', prep_only=0, force_upload=0, from_local=0, name=None, site=None, dest_dir=None):\n\n r = self.database_renderer(name=name, site=site)\n\n # Render the snapshot filename.\n r.env.dump_fn = self.get_default_db_fn(fn_template=dump_fn, dest_dir=dest_dir)\n\n from_local = int(from_local)\n\n prep_only = int(prep_only)\n\n missing_local_dump_error = r.format('Database dump file {dump_fn} does not exist.')\n\n # Copy snapshot file to target.\n if self.is_local:\n r.env.remote_dump_fn = dump_fn\n else:\n r.env.remote_dump_fn = '/tmp/' + os.path.split(r.env.dump_fn)[-1]\n\n if not prep_only and not self.is_local:\n if not self.dryrun:\n assert os.path.isfile(r.env.dump_fn), missing_local_dump_error\n r.pc('Uploading MongoDB database snapshot...')\n# r.put(\n# local_path=r.env.dump_fn,\n# remote_path=r.env.remote_dump_fn)\n r.local('rsync -rvz --progress --no-p --no-g '\n '--rsh \"ssh -o StrictHostKeyChecking=no -i {key_filename}\" '\n '{dump_fn} {user}@{host_string}:{remote_dump_fn}')\n\n if self.is_local and not prep_only and not self.dryrun:\n assert os.path.isfile(r.env.dump_fn), missing_local_dump_error\n\n r.run_or_local(r.env.load_command)", "response": "Load a database snapshot into the target database server."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nopen a SQL shell to the given database.", "response": "def shell(self, name='default', user=None, password=None, root=0, verbose=1, write_password=1, no_db=0, no_pw=0):\n \"\"\"\n Opens a SQL shell to the given database, assuming the configured database\n and user supports this feature.\n \"\"\"\n raise NotImplementedError"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef update_settings(self, d, role, path='roles/{role}/settings.yaml'):\n try:\n import ruamel.yaml\n load_func = ruamel.yaml.round_trip_load\n dump_func = ruamel.yaml.round_trip_dump\n except ImportError:\n print('Warning: ruamel.yaml not available, reverting to yaml package, possible lost of formatting may occur.')\n import yaml\n load_func = yaml.load\n dump_func = yaml.dump\n settings_fn = path.format(role=role)\n data = load_func(open(settings_fn))\n data.update(d)\n settings_str = dump_func(data)\n open(settings_fn, 'w').write(settings_str)", "response": "Updates a key - value pair in a settings file."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef configure_bleeding(self):\n lm = self.last_manifest\n r = self.local_renderer\n if self.env.bleeding and not lm.bleeding:\n # Install.\n r.append(\n text='deb http://www.rabbitmq.com/debian/ testing main',\n filename='/etc/apt/sources.list.d/rabbitmq.list',\n use_sudo=True)\n r.sudo('cd /tmp; wget -O- https://www.rabbitmq.com/rabbitmq-release-signing-key.asc | sudo apt-key add -')\n r.sudo('apt-get update')\n\n elif not self.env.bleeding and lm.bleeding:\n # Uninstall.\n r.sudo('rm -f /etc/apt/sources.list.d/rabbitmq.list')\n r.sudo('apt-get update')", "response": "Configure BLEEDING environment variable."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ninstalls and configures RabbitMQ.", "response": "def _configure_users(self, site=None, full=0, only_data=0):\n \"\"\"\n Installs and configures RabbitMQ.\n \"\"\"\n\n site = site or ALL\n\n full = int(full)\n\n if full and not only_data:\n packager = self.get_satchel('packager')\n packager.install_required(type=SYSTEM, service=self.name)\n\n r = self.local_renderer\n\n params = self.get_user_vhosts(site=site) # [(user, password, vhost)]\n\n with settings(warn_only=True):\n self.add_admin_user()\n\n params = sorted(list(params))\n if not only_data:\n for user, password, vhost in params:\n r.env.broker_user = user\n r.env.broker_password = password\n r.env.broker_vhost = vhost\n with settings(warn_only=True):\n r.sudo('rabbitmqctl add_user {broker_user} {broker_password}')\n r.sudo('rabbitmqctl add_vhost {broker_vhost}')\n r.sudo('rabbitmqctl set_permissions -p {broker_vhost} {broker_user} \".*\" \".*\" \".*\"')\n r.sudo('rabbitmqctl set_permissions -p {broker_vhost} {admin_username} \".*\" \".*\" \".*\"')\n\n return params"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef record_manifest(self):\n data = super(RabbitMQSatchel, self).record_manifest()\n params = sorted(list(self.get_user_vhosts())) # [(user, password, vhost)]\n data['rabbitmq_all_site_vhosts'] = params\n data['sites'] = list(self.genv.sites or [])\n return data", "response": "Returns a dictionary representing a serialized state of the service."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning a generator yielding all the keys that have values that differ between each dictionary.", "response": "def iter_dict_differences(a, b):\n \"\"\"\n Returns a generator yielding all the keys that have values that differ between each dictionary.\n \"\"\"\n common_keys = set(a).union(b)\n for k in common_keys:\n a_value = a.get(k)\n b_value = b.get(k)\n if a_value != b_value:\n yield k, (a_value, b_value)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_component_order(component_names):\n assert isinstance(component_names, (tuple, list))\n component_dependences = {}\n for _name in component_names:\n deps = set(manifest_deployers_befores.get(_name, []))\n deps = deps.intersection(component_names)\n component_dependences[_name] = deps\n component_order = list(topological_sort(component_dependences.items()))\n return component_order", "response": "Given a list of components re - orders them according to inter - component dependencies."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_deploy_funcs(components, current_thumbprint, previous_thumbprint, preview=False):\n for component in components:\n funcs = manifest_deployers.get(component, [])\n for func_name in funcs:\n\n #TODO:remove this after burlap.* naming prefix bug fixed\n if func_name.startswith('burlap.'):\n print('skipping %s' % func_name)\n continue\n\n takes_diff = manifest_deployers_takes_diff.get(func_name, False)\n\n func = resolve_deployer(func_name)\n current = current_thumbprint.get(component)\n last = previous_thumbprint.get(component)\n if takes_diff:\n yield func_name, partial(func, last=last, current=current)\n else:\n yield func_name, partial(func)", "response": "Returns a generator yielding the named functions needed for a deployment."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns the path to the manifest file.", "response": "def manifest_filename(self):\n \"\"\"\n Returns the path to the manifest file.\n \"\"\"\n r = self.local_renderer\n tp_fn = r.format(r.env.data_dir + '/manifest.yaml')\n return tp_fn"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning a dictionary representing the current configuration state.", "response": "def get_current_thumbprint(self, components=None):\n \"\"\"\n Returns a dictionary representing the current configuration state.\n\n Thumbprint is of the form:\n\n {\n component_name1: {key: value},\n component_name2: {key: value},\n ...\n }\n\n \"\"\"\n components = str_to_component_list(components)\n if self.verbose:\n print('deploy.get_current_thumbprint.components:', components)\n manifest_data = {} # {component:data}\n for component_name, func in sorted(manifest_recorder.items()):\n self.vprint('Checking thumbprint for component %s...' % component_name)\n manifest_key = assert_valid_satchel(component_name)\n service_name = clean_service_name(component_name)\n if service_name not in self.genv.services:\n self.vprint('Skipping unused component:', component_name)\n continue\n elif components and service_name not in components:\n self.vprint('Skipping non-matching component:', component_name)\n continue\n try:\n self.vprint('Retrieving manifest for %s...' % component_name)\n manifest_data[manifest_key] = func()\n if self.verbose:\n pprint(manifest_data[manifest_key], indent=4)\n except exceptions.AbortDeployment as e:\n raise\n return manifest_data"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_previous_thumbprint(self, components=None):\n components = str_to_component_list(components)\n tp_fn = self.manifest_filename\n tp_text = None\n if self.file_exists(tp_fn):\n fd = six.BytesIO()\n get(tp_fn, fd)\n tp_text = fd.getvalue()\n manifest_data = {}\n raw_data = yaml.load(tp_text)\n for k, v in raw_data.items():\n manifest_key = assert_valid_satchel(k)\n service_name = clean_service_name(k)\n if components and service_name not in components:\n continue\n manifest_data[manifest_key] = v\n return manifest_data", "response": "Returns a dictionary representing the previous configuration state."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef lock(self):\n self.init()\n r = self.local_renderer\n if self.file_exists(r.env.lockfile_path):\n raise exceptions.AbortDeployment('Lock file %s exists. Perhaps another deployment is currently underway?' % r.env.lockfile_path)\n else:\n self.vprint('Locking %s.' % r.env.lockfile_path)\n r.env.hostname = socket.gethostname()\n r.run_or_local('echo \"{hostname}\" > {lockfile_path}')", "response": "Lock the remote server."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef unlock(self):\n self.init()\n r = self.local_renderer\n if self.file_exists(r.env.lockfile_path):\n self.vprint('Unlocking %s.' % r.env.lockfile_path)\n r.run_or_local('rm -f {lockfile_path}')", "response": "Unlocks the local cache file."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncreates a fake deployment of the current satchel.", "response": "def fake(self, components=None):#, set_satchels=None):\n \"\"\"\n Update the thumbprint on the remote server but execute no satchel configurators.\n\n components = A comma-delimited list of satchel names to limit the fake deployment to.\n set_satchels = A semi-colon delimited list of key-value pairs to set in satchels before recording a fake deployment.\n \"\"\"\n\n self.init()\n\n # In cases where we only want to fake deployment of a specific satchel, then simply copy the last thumbprint and overwrite with a subset\n # of the current thumbprint filtered by our target components.\n if components:\n current_tp = self.get_previous_thumbprint() or {}\n current_tp.update(self.get_current_thumbprint(components=components) or {})\n else:\n current_tp = self.get_current_thumbprint(components=components) or {}\n\n tp_text = yaml.dump(current_tp)\n r = self.local_renderer\n r.upload_content(content=tp_text, fn=self.manifest_filename)\n\n # Ensure all cached manifests are cleared, so they reflect the newly deployed changes.\n self.reset_all_satchels()"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_component_funcs(self, components=None):\n\n current_tp = self.get_current_thumbprint(components=components) or {}\n previous_tp = self.get_previous_thumbprint(components=components) or {}\n\n if self.verbose:\n print('Current thumbprint:')\n pprint(current_tp, indent=4)\n print('Previous thumbprint:')\n pprint(previous_tp, indent=4)\n\n differences = list(iter_dict_differences(current_tp, previous_tp))\n if self.verbose:\n print('Differences:')\n pprint(differences, indent=4)\n component_order = get_component_order([k for k, (_, _) in differences])\n if self.verbose:\n print('component_order:')\n pprint(component_order, indent=4)\n plan_funcs = list(get_deploy_funcs(component_order, current_tp, previous_tp))\n\n return component_order, plan_funcs", "response": "Calculates the component functions that need to be executed for a deployment."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef preview(self, components=None, ask=0):\n\n ask = int(ask)\n\n self.init()\n\n component_order, plan_funcs = self.get_component_funcs(components=components)\n\n print('\\n%i changes found for host %s.\\n' % (len(component_order), self.genv.host_string))\n if component_order and plan_funcs:\n if self.verbose:\n print('These components have changed:\\n')\n for component in sorted(component_order):\n print((' '*4)+component)\n print('Deployment plan for host %s:\\n' % self.genv.host_string)\n for func_name, _ in plan_funcs:\n print(success_str((' '*4)+func_name))\n if component_order:\n print()\n\n if ask and self.genv.host_string == self.genv.hosts[-1]:\n if component_order:\n if not raw_input('Begin deployment? [yn] ').strip().lower().startswith('y'):\n sys.exit(0)\n else:\n sys.exit(0)", "response": "Inspects differences between the last deployment and the current code state."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef push(self, components=None, yes=0):\n from burlap import notifier\n service = self.get_satchel('service')\n self.lock()\n try:\n\n yes = int(yes)\n if not yes:\n # If we want to confirm the deployment with the user, and we're at the first server,\n # then run the preview.\n if self.genv.host_string == self.genv.hosts[0]:\n execute(partial(self.preview, components=components, ask=1))\n\n notifier.notify_pre_deployment()\n component_order, plan_funcs = self.get_component_funcs(components=components)\n\n service.pre_deploy()\n for func_name, plan_func in plan_funcs:\n print('Executing %s...' % func_name)\n plan_func()\n self.fake(components=components)\n\n service.post_deploy()\n notifier.notify_post_deployment()\n\n finally:\n self.unlock()", "response": "Execute all satchel configurators to apply pending changes to the server."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_thumbprint(self):\n d = {}\n settings = dj.get_settings()\n for name in self.names:\n d[name] = getattr(settings, name)\n return d", "response": "Calculates the current thumbprint of the item being tracked."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_settings(self, site=None, role=None):\n r = self.local_renderer\n _stdout = sys.stdout\n _stderr = sys.stderr\n if not self.verbose:\n sys.stdout = StringIO()\n sys.stderr = StringIO()\n try:\n sys.path.insert(0, r.env.src_dir)\n\n # Temporarily override SITE.\n tmp_site = self.genv.SITE\n if site and site.endswith('_secure'):\n site = site[:-7]\n site = site or self.genv.SITE or self.genv.default_site\n self.set_site(site)\n\n # Temporarily override ROLE.\n tmp_role = self.genv.ROLE\n if role:\n self.set_role(role)\n\n try:\n # We need to explicitly delete sub-modules from sys.modules. Otherwise, reload() skips\n # them and they'll continue to contain obsolete settings.\n if r.env.delete_module_with_prefixes:\n for name in sorted(sys.modules):\n for prefix in r.env.delete_module_with_prefixes:\n if name.startswith(prefix):\n if self.verbose:\n print('Deleting module %s prior to re-import.' % name)\n del sys.modules[name]\n break\n\n for name in list(sys.modules):\n for s in r.env.delete_module_containing:\n if s in name:\n del sys.modules[name]\n break\n\n if r.env.settings_module in sys.modules:\n del sys.modules[r.env.settings_module]\n\n #TODO:fix r.env.settings_module not loading from settings?\n# print('r.genv.django_settings_module:', r.genv.django_settings_module, file=_stdout)\n# print('r.genv.dj_settings_module:', r.genv.dj_settings_module, file=_stdout)\n# print('r.env.settings_module:', r.env.settings_module, file=_stdout)\n if 'django_settings_module' in r.genv:\n r.env.settings_module = r.genv.django_settings_module\n else:\n r.env.settings_module = r.env.settings_module or r.genv.dj_settings_module\n if self.verbose:\n print('r.env.settings_module:', r.env.settings_module, r.format(r.env.settings_module))\n module = import_module(r.format(r.env.settings_module))\n\n if site:\n assert site == module.SITE, 'Unable to set SITE to \"%s\" Instead it is set to \"%s\".' % (site, module.SITE)\n\n # Works as long as settings.py doesn't also reload anything.\n import imp\n imp.reload(module)\n\n except ImportError as e:\n print('Warning: Could not import settings for site \"%s\": %s' % (site, e), file=_stdout)\n traceback.print_exc(file=_stdout)\n #raise # breaks *_secure pseudo sites\n return\n finally:\n if tmp_site:\n self.set_site(tmp_site)\n if tmp_role:\n self.set_role(tmp_role)\n finally:\n sys.stdout = _stdout\n sys.stderr = _stderr\n sys.path.remove(r.env.src_dir)\n return module", "response": "Retrieves the Django settings dictionary."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ninstalling all custom SQL files.", "response": "def install_sql(self, site=None, database='default', apps=None, stop_on_error=0, fn=None):\n \"\"\"\n Installs all custom SQL.\n \"\"\"\n #from burlap.db import load_db_set\n\n stop_on_error = int(stop_on_error)\n\n site = site or ALL\n\n name = database\n\n r = self.local_renderer\n paths = glob.glob(r.format(r.env.install_sql_path_template))\n\n apps = [_ for _ in (apps or '').split(',') if _.strip()]\n if self.verbose:\n print('install_sql.apps:', apps)\n\n def cmp_paths(d0, d1):\n if d0[1] and d0[1] in d1[2]:\n return -1\n if d1[1] and d1[1] in d0[2]:\n return +1\n return cmp(d0[0], d1[0])\n\n def get_paths(t):\n \"\"\"\n Returns SQL file paths in an execution order that respect dependencies.\n \"\"\"\n data = [] # [(path, view_name, content)]\n for path in paths:\n if fn and fn not in path:\n continue\n parts = path.split('.')\n if len(parts) == 3 and parts[1] != t:\n continue\n if not path.lower().endswith('.sql'):\n continue\n content = open(path, 'r').read()\n matches = re.findall(r'[\\s\\t]+VIEW[\\s\\t]+([a-zA-Z0-9_]{3,})', content, flags=re.IGNORECASE)\n view_name = ''\n if matches:\n view_name = matches[0]\n print('Found view %s.' % view_name)\n data.append((path, view_name, content))\n for d in sorted(data, cmp=cmp_paths):\n yield d[0]\n\n def run_paths(paths, cmd_template, max_retries=3):\n r = self.local_renderer\n paths = list(paths)\n error_counts = defaultdict(int) # {path:count}\n terminal = set()\n if self.verbose:\n print('Checking %i paths.' % len(paths))\n while paths:\n path = paths.pop(0)\n if self.verbose:\n print('path:', path)\n app_name = re.findall(r'/([^/]+)/sql/', path)[0]\n if apps and app_name not in apps:\n self.vprint('skipping because app_name %s not in apps' % app_name)\n continue\n with self.settings(warn_only=True):\n if self.is_local:\n r.env.sql_path = path\n else:\n r.env.sql_path = '/tmp/%s' % os.path.split(path)[-1]\n r.put(local_path=path, remote_path=r.env.sql_path)\n ret = r.run_or_local(cmd_template)\n if ret and ret.return_code:\n\n if stop_on_error:\n raise Exception('Unable to execute file %s' % path)\n\n error_counts[path] += 1\n if error_counts[path] < max_retries:\n paths.append(path)\n else:\n terminal.add(path)\n if terminal:\n print('%i files could not be loaded.' % len(terminal), file=sys.stderr)\n for path in sorted(list(terminal)):\n print(path, file=sys.stderr)\n print(file=sys.stderr)\n\n if self.verbose:\n print('install_sql.db_engine:', r.env.db_engine)\n\n for _site, site_data in self.iter_sites(site=site, no_secure=True):\n\n self.set_db(name=name, site=_site)\n\n if 'postgres' in r.env.db_engine or 'postgis' in r.env.db_engine:\n paths = list(get_paths('postgresql'))\n run_paths(\n paths=paths,\n cmd_template=\"psql --host={db_host} --user={db_user} --no-password -d {db_name} -f {sql_path}\")\n\n elif 'mysql' in r.env.db_engine:\n paths = list(get_paths('mysql'))\n run_paths(\n paths=paths,\n cmd_template=\"mysql -v -h {db_host} -u {db_user} -p'{db_password}' {db_name} < {sql_path}\")\n\n else:\n raise NotImplementedError"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nruns the Django createsuperuser management command.", "response": "def createsuperuser(self, username='admin', email=None, password=None, site=None):\n \"\"\"\n Runs the Django createsuperuser management command.\n \"\"\"\n r = self.local_renderer\n site = site or self.genv.SITE\n self.set_site_specifics(site)\n options = ['--username=%s' % username]\n if email:\n options.append('--email=%s' % email)\n if password:\n options.append('--password=%s' % password)\n r.env.options_str = ' '.join(options)\n if self.is_local:\n r.env.project_dir = r.env.local_project_dir\n r.genv.SITE = r.genv.SITE or site\n r.run_or_local('export SITE={SITE}; export ROLE={ROLE}; cd {project_dir}; {manage_cmd} {createsuperuser_cmd} {options_str}')"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nrunning the Dango loaddata management command.", "response": "def loaddata(self, path, site=None):\n \"\"\"\n Runs the Dango loaddata management command.\n\n By default, runs on only the current site.\n\n Pass site=all to run on all sites.\n \"\"\"\n site = site or self.genv.SITE\n r = self.local_renderer\n r.env._loaddata_path = path\n for _site, site_data in self.iter_sites(site=site, no_secure=True):\n try:\n self.set_db(site=_site)\n r.env.SITE = _site\n r.sudo('export SITE={SITE}; export ROLE={ROLE}; '\n 'cd {project_dir}; '\n '{manage_cmd} loaddata {_loaddata_path}')\n except KeyError:\n pass"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef manage_all(self, *args, **kwargs):\n for site, site_data in self.iter_unique_databases(site='all'):\n if self.verbose:\n print('-'*80, file=sys.stderr)\n print('site:', site, file=sys.stderr)\n if self.env.available_sites_by_host:\n hostname = self.current_hostname\n sites_on_host = self.env.available_sites_by_host.get(hostname, [])\n if sites_on_host and site not in sites_on_host:\n self.vprint('skipping site:', site, sites_on_host, file=sys.stderr)\n continue\n self.manage(*args, **kwargs)", "response": "Runs manage on all unique site default databases."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef load_django_settings(self):\n r = self.local_renderer\n\n # Save environment variables so we can restore them later.\n _env = {}\n save_vars = ['ALLOW_CELERY', 'DJANGO_SETTINGS_MODULE']\n for var_name in save_vars:\n _env[var_name] = os.environ.get(var_name)\n\n try:\n\n # Allow us to import local app modules.\n if r.env.local_project_dir:\n sys.path.insert(0, r.env.local_project_dir)\n\n #TODO:remove this once bug in django-celery has been fixed\n os.environ['ALLOW_CELERY'] = '0'\n\n# print('settings_module:', r.format(r.env.settings_module))\n os.environ['DJANGO_SETTINGS_MODULE'] = r.format(r.env.settings_module)\n# os.environ['CELERY_LOADER'] = 'django'\n# os.environ['SITE'] = r.genv.SITE or r.genv.default_site\n# os.environ['ROLE'] = r.genv.ROLE or r.genv.default_role\n\n # In Django >= 1.7, fixes the error AppRegistryNotReady: Apps aren't loaded yet\n # Disabling, in Django >= 1.10, throws exception:\n # RuntimeError: Model class django.contrib.contenttypes.models.ContentType\n # doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.\n# try:\n# from django.core.wsgi import get_wsgi_application\n# application = get_wsgi_application()\n# except (ImportError, RuntimeError):\n# raise\n# print('Unable to get wsgi application.')\n# traceback.print_exc()\n\n # In Django >= 1.7, fixes the error AppRegistryNotReady: Apps aren't loaded yet\n try:\n import django\n django.setup()\n except AttributeError:\n # This doesn't exist in Django < 1.7, so ignore it.\n pass\n\n # Load Django settings.\n settings = self.get_settings()\n try:\n from django.contrib import staticfiles\n from django.conf import settings as _settings\n\n # get_settings() doesn't raise ImportError but returns None instead\n if settings is not None:\n for k, v in settings.__dict__.items():\n setattr(_settings, k, v)\n else:\n raise ImportError\n except (ImportError, RuntimeError):\n print('Unable to load settings.')\n traceback.print_exc()\n\n finally:\n # Restore environment variables.\n for var_name, var_value in _env.items():\n if var_value is None:\n del os.environ[var_name]\n else:\n os.environ[var_name] = var_value\n\n return settings", "response": "Loads Django settings for the current site and sets them so Django internals can be run later."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef shell(self):\n r = self.local_renderer\n if '@' in self.genv.host_string:\n r.env.shell_host_string = self.genv.host_string\n else:\n r.env.shell_host_string = '{user}@{host_string}'\n r.env.shell_default_dir = self.genv.shell_default_dir_template\n r.env.shell_interactive_djshell_str = self.genv.interactive_shell_template\n r.run_or_local('ssh -t -i {key_filename} {shell_host_string} \"{shell_interactive_djshell_str}\"')", "response": "Opens a Django focussed Python shell."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef syncdb(self, site=None, all=0, database=None, ignore_errors=1): # pylint: disable=redefined-builtin\n r = self.local_renderer\n\n ignore_errors = int(ignore_errors)\n\n post_south = self.version_tuple >= (1, 7, 0)\n\n use_run_syncdb = self.version_tuple >= (1, 9, 0)\n\n # DEPRECATED: removed in Django>=1.7\n r.env.db_syncdb_all_flag = '--all' if int(all) else ''\n\n r.env.db_syncdb_database = ''\n if database:\n r.env.db_syncdb_database = ' --database=%s' % database\n\n if self.is_local:\n r.env.project_dir = r.env.local_project_dir\n\n site = site or self.genv.SITE\n for _site, site_data in r.iter_unique_databases(site=site):\n r.env.SITE = _site\n with self.settings(warn_only=ignore_errors):\n if post_south:\n if use_run_syncdb:\n r.run_or_local(\n 'export SITE={SITE}; export ROLE={ROLE}; cd {project_dir}; '\n '{manage_cmd} migrate --run-syncdb --noinput {db_syncdb_database}')\n else:\n # Between Django>=1.7,<1.9 we can only do a regular migrate, no true syncdb.\n r.run_or_local(\n 'export SITE={SITE}; export ROLE={ROLE}; cd {project_dir}; '\n '{manage_cmd} migrate --noinput {db_syncdb_database}')\n else:\n r.run_or_local(\n 'export SITE={SITE}; export ROLE={ROLE}; cd {project_dir}; '\n '{manage_cmd} syncdb --noinput {db_syncdb_all_flag} {db_syncdb_database}')", "response": "Runs the standard Django syncdb command for one or more sites."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nrunning the standard South migrate command for one or more sites.", "response": "def migrate(self, app='', migration='', site=None, fake=0, ignore_errors=None, skip_databases=None, database=None, migrate_apps='', delete_ghosts=1):\n \"\"\"\n Runs the standard South migrate command for one or more sites.\n \"\"\"\n # Note, to pass a comma-delimted list in a fab command, escape the comma with a back slash.\n #\n # e.g.\n #\n # fab staging dj.migrate:migrate_apps=oneapp\\,twoapp\\,threeapp\n\n r = self.local_renderer\n\n ignore_errors = int(r.env.ignore_migration_errors if ignore_errors is None else ignore_errors)\n\n delete_ghosts = int(delete_ghosts)\n\n post_south = self.version_tuple >= (1, 7, 0)\n\n if self.version_tuple >= (1, 9, 0):\n delete_ghosts = 0\n\n skip_databases = (skip_databases or '')\n if isinstance(skip_databases, six.string_types):\n skip_databases = [_.strip() for _ in skip_databases.split(',') if _.strip()]\n\n migrate_apps = migrate_apps or ''\n migrate_apps = [\n _.strip().split('.')[-1]\n for _ in migrate_apps.strip().split(',')\n if _.strip()\n ]\n if app:\n migrate_apps.append(app)\n\n r.env.migrate_migration = migration or ''\n r.env.migrate_fake_str = '--fake' if int(fake) else ''\n r.env.migrate_database = '--database=%s' % database if database else ''\n r.env.migrate_merge = '--merge' if not post_south else ''\n r.env.delete_ghosts = '--delete-ghost-migrations' if delete_ghosts and not post_south else ''\n self.vprint('project_dir0:', r.env.project_dir, r.genv.get('dj_project_dir'), r.genv.get('project_dir'))\n self.vprint('migrate_apps:', migrate_apps)\n\n if self.is_local:\n r.env.project_dir = r.env.local_project_dir\n\n # CS 2017-3-29 Don't bypass the iterator. That causes reversion to the global env that could corrupt the generated commands.\n #databases = list(self.iter_unique_databases(site=site))#TODO:remove\n # CS 2017-4-24 Don't specify a single site as the default when none is supplied. Otherwise all other sites will be ignored.\n #site = site or self.genv.SITE\n site = site or ALL\n databases = self.iter_unique_databases(site=site)\n for _site, site_data in databases:\n self.vprint('-'*80, file=sys.stderr)\n self.vprint('site:', _site, file=sys.stderr)\n\n if self.env.available_sites_by_host:\n hostname = self.current_hostname\n sites_on_host = self.env.available_sites_by_host.get(hostname, [])\n if sites_on_host and _site not in sites_on_host:\n self.vprint('skipping site:', _site, sites_on_host, file=sys.stderr)\n continue\n\n if not migrate_apps:\n migrate_apps.append(' ')\n\n for _app in migrate_apps:\n # In cases where we're migrating built-in apps or apps with dotted names\n # e.g. django.contrib.auth, extract the name used for the migrate command.\n r.env.migrate_app = _app.split('.')[-1]\n self.vprint('project_dir1:', r.env.project_dir, r.genv.get('dj_project_dir'), r.genv.get('project_dir'))\n r.env.SITE = _site\n with self.settings(warn_only=ignore_errors):\n r.run_or_local(\n 'export SITE={SITE}; export ROLE={ROLE}; {migrate_pre_command} cd {project_dir}; '\n '{manage_cmd} migrate --noinput {migrate_merge} --traceback '\n '{migrate_database} {delete_ghosts} {migrate_app} {migrate_migration} '\n '{migrate_fake_str}')"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nstart a Django management command in a screen.", "response": "def manage_async(self, command='', name='process', site=ALL, exclude_sites='', end_message='', recipients=''):\n \"\"\"\n Starts a Django management command in a screen.\n\n Parameters:\n\n command :- all arguments passed to `./manage` as a single string\n\n site :- the site to run the command for (default is all)\n\n Designed to be ran like:\n\n fab dj.manage_async:\"some_management_command --force\"\n\n \"\"\"\n exclude_sites = exclude_sites.split(':')\n r = self.local_renderer\n for _site, site_data in self.iter_sites(site=site, no_secure=True):\n if _site in exclude_sites:\n continue\n r.env.SITE = _site\n r.env.command = command\n r.env.end_email_command = ''\n r.env.recipients = recipients or ''\n r.env.end_email_command = ''\n if end_message:\n end_message = end_message + ' for ' + _site\n end_message = end_message.replace(' ', '_')\n r.env.end_message = end_message\n r.env.end_email_command = r.format('{manage_cmd} send_mail --subject={end_message} --recipients={recipients}')\n r.env.name = name.format(**r.genv)\n r.run(\n 'screen -dmS {name} bash -c \"export SITE={SITE}; '\\\n 'export ROLE={ROLE}; cd {project_dir}; '\\\n '{manage_cmd} {command} --traceback; {end_email_command}\"; sleep 3;')"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_media_timestamp(self, last_timestamp=None):\n r = self.local_renderer\n _latest_timestamp = -1e9999999999999999\n for path in self.iter_static_paths():\n path = r.env.static_root + '/' + path\n self.vprint('checking timestamp of path:', path)\n if not os.path.isfile(path):\n continue\n #print('path:', path)\n _latest_timestamp = max(_latest_timestamp, get_last_modified_timestamp(path) or _latest_timestamp)\n if last_timestamp is not None and _latest_timestamp > last_timestamp:\n break\n self.vprint('latest_timestamp:', _latest_timestamp)\n return _latest_timestamp", "response": "Retrieves the most recent timestamp of the media in the static root. If last_timestamp is not given retrieves the first timestamp more recent than this value."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nset the root login for the given database on the given host and sets the root login to environment variables.", "response": "def set_root_login(self, r):\n \"\"\"\n Looks up the root login for the given database on the given host and sets\n it to environment variables.\n\n Populates these standard variables:\n\n db_root_password\n db_root_username\n\n \"\"\"\n\n # Check the legacy password location.\n try:\n r.env.db_root_username = r.env.root_username\n except AttributeError:\n pass\n try:\n r.env.db_root_password = r.env.root_password\n except AttributeError:\n pass\n\n # Check the new password location.\n key = r.env.get('db_host')\n if self.verbose:\n print('db.set_root_login.key:', key)\n print('db.set_root_logins:', r.env.root_logins)\n if key in r.env.root_logins:\n data = r.env.root_logins[key]\n# print('data:', data)\n if 'username' in data:\n r.env.db_root_username = data['username']\n r.genv.db_root_username = data['username']\n if 'password' in data:\n r.env.db_root_password = data['password']\n r.genv.db_root_password = data['password']\n else:\n msg = 'Warning: No root login entry found for host %s in role %s.' % (r.env.get('db_host'), self.genv.get('ROLE'))\n print(msg, file=sys.stderr)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef database_renderer(self, name=None, site=None, role=None):\n\n name = name or self.env.default_db_name\n\n site = site or self.genv.SITE\n\n role = role or self.genv.ROLE\n\n key = (name, site, role)\n self.vprint('checking key:', key)\n if key not in self._database_renderers:\n self.vprint('No cached db renderer, generating...')\n\n if self.verbose:\n print('db.name:', name)\n print('db.databases:', self.env.databases)\n print('db.databases[%s]:' % name, self.env.databases.get(name))\n\n d = type(self.genv)(self.lenv)\n d.update(self.get_database_defaults())\n d.update(self.env.databases.get(name, {}))\n d['db_name'] = name\n if self.verbose:\n print('db.d:')\n pprint(d, indent=4)\n print('db.connection_handler:', d.connection_handler)\n\n if d.connection_handler == CONNECTION_HANDLER_DJANGO:\n self.vprint('Using django handler...')\n dj = self.get_satchel('dj')\n if self.verbose:\n print('Loading Django DB settings for site {} and role {}.'.format(site, role), file=sys.stderr)\n dj.set_db(name=name, site=site, role=role)\n _d = dj.local_renderer.collect_genv(include_local=True, include_global=False)\n\n # Copy \"dj_db_*\" into \"db_*\".\n for k, v in _d.items():\n if k.startswith('dj_db_'):\n _d[k[3:]] = v\n del _d[k]\n\n if self.verbose:\n print('Loaded:')\n pprint(_d)\n d.update(_d)\n\n elif d.connection_handler and d.connection_handler.startswith(CONNECTION_HANDLER_CUSTOM+':'):\n\n _callable_str = d.connection_handler[len(CONNECTION_HANDLER_CUSTOM+':'):]\n self.vprint('Using custom handler %s...' % _callable_str)\n _d = str_to_callable(_callable_str)(role=self.genv.ROLE)\n if self.verbose:\n print('Loaded:')\n pprint(_d)\n d.update(_d)\n\n r = LocalRenderer(self, lenv=d)\n\n # Optionally set any root logins needed for administrative commands.\n self.set_root_login(r)\n\n self._database_renderers[key] = r\n else:\n self.vprint('Cached db renderer found.')\n\n return self._database_renderers[key]", "response": "Returns a new database instance with the given name site and role."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngets the free space in bytes.", "response": "def get_free_space(self):\n \"\"\"\n Return free space in bytes.\n \"\"\"\n cmd = \"df -k | grep -vE '^Filesystem|tmpfs|cdrom|none|udev|cgroup' | awk '{ print($1 \\\" \\\" $4 }'\"\n lines = [_ for _ in self.run(cmd).strip().split('\\n') if _.startswith('/')]\n assert len(lines) == 1, 'Ambiguous devices: %s' % str(lines)\n device, kb = lines[0].split(' ')\n free_space = int(kb) * 1024\n self.vprint('free_space (bytes):', free_space)\n return free_space"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef load_db_set(self, name, r=None):\n r = r or self\n db_set = r.genv.db_sets.get(name, {})\n r.genv.update(db_set)", "response": "Loads database parameters from a specific named set."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef loadable(self, src, dst):\n from fabric import state\n from fabric.task_utils import crawl\n\n src_task = crawl(src, state.commands)\n assert src_task, 'Unknown source role: %s' % src\n\n dst_task = crawl(dst, state.commands)\n assert dst_task, 'Unknown destination role: %s' % src\n\n # Get source database size.\n src_task()\n env.host_string = env.hosts[0]\n src_size_bytes = self.get_size()\n\n # Get target database size, if any.\n dst_task()\n env.host_string = env.hosts[0]\n try:\n dst_size_bytes = self.get_size()\n except (ValueError, TypeError):\n dst_size_bytes = 0\n\n # Get target host disk size.\n free_space_bytes = self.get_free_space()\n\n # Deduct existing database size, because we'll be deleting it.\n balance_bytes = free_space_bytes + dst_size_bytes - src_size_bytes\n balance_bytes_scaled, units = pretty_bytes(balance_bytes)\n\n viable = balance_bytes >= 0\n if self.verbose:\n print('src_db_size:', pretty_bytes(src_size_bytes))\n print('dst_db_size:', pretty_bytes(dst_size_bytes))\n print('dst_free_space:', pretty_bytes(free_space_bytes))\n print\n if viable:\n print('Viable! There will be %.02f %s of disk space left.' % (balance_bytes_scaled, units))\n else:\n print('Not viable! We would be %.02f %s short.' % (balance_bytes_scaled, units))\n\n return viable", "response": "Determines if there s enough space to load the target database."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef dump(self, dest_dir=None, to_local=1, from_local=0, archive=0, dump_fn=None, name=None, site=None, use_sudo=0, cleanup=1):\n r = self.local_renderer\n\n site = site or self.genv.SITE\n\n r = self.database_renderer(name=name, site=site)\n\n # Load optional site-specific command, if given.\n try:\n r.env.dump_command = self.genv.sites[site]['postgresql_dump_command']\n except KeyError:\n pass\n\n use_sudo = int(use_sudo)\n\n from_local = int(from_local)\n\n to_local = int(to_local)\n\n dump_fn = dump_fn or r.env.dump_fn_template\n\n # Render the snapshot filename.\n r.env.dump_fn = self.get_default_db_fn(\n fn_template=dump_fn,\n dest_dir=dest_dir,\n name=name,\n site=site,\n )\n\n # Dump the database to a snapshot file.\n #if not os.path.isfile(os.path.abspath(r.env.dump_fn))):\n r.pc('Dumping database snapshot.')\n if from_local:\n r.local(r.env.dump_command)\n elif use_sudo:\n r.sudo(r.env.dump_command)\n else:\n r.run(r.env.dump_command)\n\n # Download the database dump file on the remote host to localhost.\n if not from_local and to_local:\n r.pc('Downloading database snapshot to localhost.')\n r.local('rsync -rvz --progress --recursive --no-p --no-g '\n '--rsh \"ssh -o StrictHostKeyChecking=no -i {key_filename}\" {user}@{host_string}:{dump_fn} {dump_fn}')\n\n # Delete the snapshot file on the remote system.\n if int(cleanup):\n r.pc('Deleting database snapshot on remote host.')\n r.sudo('rm {dump_fn}')\n\n # Move the database snapshot to an archive directory.\n if to_local and int(archive):\n r.pc('Archiving database snapshot.')\n db_fn = r.render_fn(r.env.dump_fn)\n r.env.archive_fn = '%s/%s' % (env.db_dump_archive_dir, os.path.split(db_fn)[-1])\n r.local('mv %s %s' % (db_fn, env.archive_fn))\n\n return r.env.dump_fn", "response": "Dumps the database to a single transportable file on the localhost."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef show(keyword=''):\n keyword = keyword.strip().lower()\n max_len = max(len(k) for k in env.iterkeys())\n keyword_found = False\n for k in sorted(env.keys()):\n if keyword and keyword not in k.lower():\n continue\n keyword_found = True\n #print '%s: %s' % (k, env[k])\n print('%s: ' % (k.ljust(max_len),))\n pprint(env[k], indent=4)\n if keyword:\n if not keyword_found:\n print('Keyword \"%s\" not found.' % keyword)", "response": "Displays a list of all environment key value pairs for the current role."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef record_manifest():\n data = {}\n # Record settings.\n data['settings'] = dict(\n (k, v)\n for k, v in env.items()\n if not isinstance(v, types.GeneratorType) and k.strip() and not k.startswith('_') and not callable(v)\n )\n # Record tarball hash.\n # Record database migrations.\n # Record media hash.\n return data", "response": "Record the manifest for the current deployment."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nfix the Ethernet name slot for the local renderer to eth0.", "response": "def fix_eth0_rename(self, hardware_addr):\n \"\"\"\n A bug as of 2016.10.10 causes eth0 to be renamed to enx*.\n This renames it to eth0.\n\n http://raspberrypi.stackexchange.com/q/43560/29103\n \"\"\"\n r = self.local_renderer\n r.env.hardware_addr = hardware_addr\n r.sudo('ln -s /dev/null /etc/udev/rules.d/80-net-name-slot.rules')\n r.append(\n text=r'SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", '\\\n r'ATTR\\{address\\}==\"{hardware_addr}\", '\\\n r'ATTR\\{dev_id\\}==\"0x0\", '\\\n r'ATTR\\{type\\}==\"1\", '\\\n r'KERNEL==\"eth*\", NAME=\"eth0\"',\n filename='/etc/udev/rules.d/70-persistent-net.rules',\n use_sudo=True,\n )"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef assume_localhost(self):\n if not self.genv.host_string:\n self.genv.host_string = 'localhost'\n self.genv.hosts = ['localhost']\n self.genv.user = getpass.getuser()", "response": "Assume that the local host is set to localhost."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef init_raspbian_disk(self, yes=0):\n self.assume_localhost()\n\n yes = int(yes)\n device_question = 'SD card present at %s? ' % self.env.sd_device\n if not yes and not raw_input(device_question).lower().startswith('y'):\n return\n\n r = self.local_renderer\n r.local_if_missing(\n fn='{raspbian_image_zip}',\n cmd='wget {raspbian_download_url} -O raspbian_lite_latest.zip')\n\n r.lenv.img_fn = \\\n r.local(\"unzip -l {raspbian_image_zip} | sed -n 4p | awk '{{print $4}}'\", capture=True) or '$IMG_FN'\n r.local('echo {img_fn}')\n r.local('[ ! -f {img_fn} ] && unzip {raspbian_image_zip} {img_fn} || true')\n r.lenv.img_fn = r.local('readlink -f {img_fn}', capture=True)\n r.local('echo {img_fn}')\n\n with self.settings(warn_only=True):\n r.sudo('[ -d \"{sd_media_mount_dir}\" ] && umount {sd_media_mount_dir} || true')\n with self.settings(warn_only=True):\n r.sudo('[ -d \"{sd_media_mount_dir2}\" ] && umount {sd_media_mount_dir2} || true')\n\n r.pc('Writing the image onto the card.')\n r.sudo('time dd bs=4M if={img_fn} of={sd_device}')\n\n # Flush all writes to disk.\n r.run('sync')", "response": "Initialize the microSD card with the latest Raspbian image and writes it to the disk."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef init_ubuntu_disk(self, yes=0):\n self.assume_localhost()\n\n yes = int(yes)\n\n if not self.dryrun:\n device_question = 'SD card present at %s? ' % self.env.sd_device\n inp = raw_input(device_question).strip()\n print('inp:', inp)\n if not yes and inp and not inp.lower().startswith('y'):\n return\n\n r = self.local_renderer\n\n # Confirm SD card is present.\n r.local('ls {sd_device}')\n\n # Download image.\n r.env.ubuntu_image_fn = os.path.abspath(os.path.split(self.env.ubuntu_download_url)[-1])\n r.local('[ ! -f {ubuntu_image_fn} ] && wget {ubuntu_download_url} || true')\n\n # Ensure SD card is unmounted.\n with self.settings(warn_only=True):\n r.sudo('[ -d \"{sd_media_mount_dir}\" ] && umount {sd_media_mount_dir}')\n with self.settings(warn_only=True):\n r.sudo('[ -d \"{sd_media_mount_dir2}\" ] && umount {sd_media_mount_dir2}')\n\n r.pc('Writing the image onto the card.')\n r.sudo('xzcat {ubuntu_image_fn} | dd bs=4M of={sd_device}')\n\n # Flush all writes to disk.\n r.run('sync')", "response": "Initialize the microSD card with latest Ubuntu image and writes it to a microSD card."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef init_raspbian_vm(self):\n\n r = self.local_renderer\n\n r.comment('Installing system packages.')\n r.sudo('add-apt-repository ppa:linaro-maintainers/tools')\n r.sudo('apt-get update')\n r.sudo('apt-get install libsdl-dev qemu-system')\n\n r.comment('Download image.')\n r.local('wget https://downloads.raspberrypi.org/raspbian_lite_latest')\n r.local('unzip raspbian_lite_latest.zip')\n #TODO:fix name?\n #TODO:resize image?\n\n r.comment('Find start of the Linux ext4 partition.')\n r.local(\n \"parted -s 2016-03-18-raspbian-jessie-lite.img unit B print | \"\n \"awk '/^Number/{{p=1;next}}; p{{gsub(/[^[:digit:]]/, \"\", $2); print $2}}' | sed -n 2p\", assign_to='START')\n\n r.local('mkdir -p {raspbian_mount_point}')\n r.sudo('mount -v -o offset=$START -t ext4 {raspbian_image} $MNT')\n\n r.comment('Comment out everything in ld.so.preload')\n r.local(\"sed -i 's/^/#/g' {raspbian_mount_point}/etc/ld.so.preload\")\n\n r.comment('Comment out entries containing /dev/mmcblk in fstab.')\n r.local(\"sed -i '/mmcblk/ s?^?#?' /etc/fstab\")\n\n r.sudo('umount {raspbian_mount_point}')\n\n r.comment('Download kernel.')\n r.local('wget https://github.com/dhruvvyas90/qemu-rpi-kernel/blob/master/{raspbian_kernel}?raw=true')\n r.local('mv {raspbian_kernel} {libvirt_images_dir}')\n\n r.comment('Creating libvirt machine.')\n r.local('virsh define libvirt-raspbian.xml')\n\n r.comment('You should now be able to boot the VM by running:')\n r.comment('')\n r.comment(' qemu-system-arm -kernel {libvirt_boot_dir}/{raspbian_kernel} '\n '-cpu arm1176 -m 256 -M versatilepb -serial stdio -append \"root=/dev/sda2 rootfstype=ext4 rw\" '\n '-hda {libvirt_images_dir}/{raspbian_image}')\n r.comment('')\n r.comment('Or by running virt-manager.')", "response": "Initialize the Raspbian VM."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef create_raspbian_vagrant_box(self):\n\n r = self.local_renderer\n\n r.sudo('adduser --disabled-password --gecos \"\" vagrant')\n\n #vagrant user should be able to run sudo commands without a password prompt\n\n r.sudo('echo \"vagrant ALL=(ALL) NOPASSWD:ALL\" > /etc/sudoers.d/vagrant')\n r.sudo('chmod 0440 /etc/sudoers.d/vagrant')\n\n r.sudo('apt-get update')\n r.sudo('apt-get install -y openssh-server')\n\n #put ssh key from vagrant user\n\n r.sudo('mkdir -p /home/vagrant/.ssh')\n r.sudo('chmod 0700 /home/vagrant/.ssh')\n r.sudo('wget --no-check-certificate https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub -O /home/vagrant/.ssh/authorized_keys')\n r.sudo('chmod 0600 /home/vagrant/.ssh/authorized_keys')\n r.sudo('chown -R vagrant /home/vagrant/.ssh')\n\n #open sudo vi /etc/ssh/sshd_config and change\n\n #PubKeyAuthentication yes\n #PermitEmptyPasswords no\n r.sudo(\"sed -i '/AuthorizedKeysFile/s/^#//g' /etc/ssh/sshd_config\")\n #PasswordAuthentication no\n r.sudo(\"sed -i '/PasswordAuthentication/s/^#//g' /etc/ssh/sshd_config\")\n r.sudo(\"sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config\")\n\n #restart ssh service using\n\n #sudo service ssh restart\n\n #install additional development packages for the tools to properly compile and install\n r.sudo('apt-get upgrade')\n r.sudo('apt-get install -y gcc build-essential')\n #TODO:fix? throws dpkg: error: fgets gave an empty string from `/var/lib/dpkg/triggers/File'\n #r.sudo('apt-get install -y linux-headers-rpi')\n\n #do any change that you want and shutdown the VM . now , come to host machine on which guest VM is running and goto\n #the /var/lib/libvirt/images/ and choose raw image in which you did the change and copy somewhere for example /test\n\n r.sudo('mkdir /tmp/test')\n r.sudo('cp {libvirt_images_dir}/{raspbian_image} /tmp/test')\n r.sudo('cp {libvirt_boot_dir}/{raspbian_kernel} /tmp/test')\n\n #create two file metadata.json and Vagrantfile in /test do entry in metadata.json\n r.render_to_file('rpi/metadata.json', '/tmp/test/metadata.json')\n r.render_to_file('rpi/Vagrantfile', '/tmp/test/Vagrantfile')\n\n #convert test.img to qcow2 format using\n r.sudo('qemu-img convert -f raw -O qcow2 {libvirt_images_dir}/{raspbian_image} {libvirt_images_dir}/{raspbian_image}.qcow2')\n\n #rename ubuntu.qcow2 to box.img\n r.sudo('mv {libvirt_images_dir}/{raspbian_image}.qcow2 {libvirt_images_dir}/box.img')\n\n #Note: currently,libvirt-vagrant support only qcow2 format. so , don't change the format just rename to box.img.\n #because it takes input with name box.img by default.\n #create box\n\n r.sudo('cd /tmp/test; tar cvzf custom_box.box ./metadata.json ./Vagrantfile ./{raspbian_kernel} ./box.img')", "response": "Create a box for easily spinning up a virtual machine with Vagrant."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef configure_hdmi(self):\n r = self.local_renderer\n\n # use HDMI mode even if no HDMI monitor is detected\n r.enable_attr(\n filename='/boot/config.txt',\n key='hdmi_force_hotplug',\n value=1,\n use_sudo=True,\n )\n\n # to normal HDMI mode (Sound will be sent if supported and enabled). Without this line,\n # the Raspbmc would switch to DVI (with no audio) mode by default.\n r.enable_attr(\n filename='/boot/config.txt',\n key='hdmi_drive',\n value=2,\n use_sudo=True,\n )", "response": "Configure HDMI mode even if no HDMI monitor is detected."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef configure_camera(self):\n #TODO:check per OS? Works on Raspbian Jessie\n r = self.local_renderer\n if self.env.camera_enabled:\n r.pc('Enabling camera.')\n #TODO:fix, doesn't work on Ubuntu, which uses commented-out values\n\n # Set start_x=1\n #r.sudo('if grep \"start_x=0\" /boot/config.txt; then sed -i \"s/start_x=0/start_x=1/g\" /boot/config.txt; fi')\n #r.sudo('if grep \"start_x\" /boot/config.txt; then true; else echo \"start_x=1\" >> /boot/config.txt; fi')\n r.enable_attr(\n filename='/boot/config.txt',\n key='start_x',\n value=1,\n use_sudo=True,\n )\n\n # Set gpu_mem=128\n# r.sudo('if grep \"gpu_mem\" /boot/config.txt; then true; else echo \"gpu_mem=128\" >> /boot/config.txt; fi')\n r.enable_attr(\n filename='/boot/config.txt',\n key='gpu_mem',\n value=r.env.gpu_mem,\n use_sudo=True,\n )\n\n # Compile the Raspberry Pi binaries.\n #https://github.com/raspberrypi/userland\n r.run('cd ~; git clone https://github.com/raspberrypi/userland.git; cd userland; ./buildme')\n r.run('touch ~/.bash_aliases')\n #r.run(\"echo 'PATH=$PATH:/opt/vc/bin\\nexport PATH' >> ~/.bash_aliases\")\n r.append(r'PATH=$PATH:/opt/vc/bin\\nexport PATH', '~/.bash_aliases')\n #r.run(\"echo 'LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/vc/lib\\nexport LD_LIBRARY_PATH' >> ~/.bash_aliases\")\n r.append(r'LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/vc/lib\\nexport LD_LIBRARY_PATH', '~/.bash_aliases')\n r.run('source ~/.bashrc')\n r.sudo('ldconfig')\n\n # Allow our user to access the video device.\n r.sudo(\"echo 'SUBSYSTEM==\\\"vchiq\\\",GROUP=\\\"video\\\",MODE=\\\"0660\\\"' > /etc/udev/rules.d/10-vchiq-permissions.rules\")\n r.sudo(\"usermod -a -G video {user}\")\n\n r.reboot(wait=300, timeout=60)\n\n self.test_camera()\n\n else:\n r.disable_attr(\n filename='/boot/config.txt',\n key='start_x',\n use_sudo=True,\n )\n r.disable_attr(\n filename='/boot/config.txt',\n key='gpu_mem',\n use_sudo=True,\n )\n r.reboot(wait=300, timeout=60)", "response": "Configure the camera with the current configuration."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef fix_lsmod_for_pi3(self):\n r = self.local_renderer\n r.env.rpi2_conf = '/etc/modules-load.d/rpi2.conf'\n r.sudo(\"sed '/bcm2808_rng/d' {rpi2_conf}\")\n r.sudo(\"echo bcm2835_rng >> {rpi2_conf}\")", "response": "Fixes the lsmod for Pi2 and Pi3"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nrunning pre - deployments for services.", "response": "def pre_deploy(self):\n \"\"\"\n Runs methods services have requested be run before each deployment.\n \"\"\"\n for service in self.genv.services:\n service = service.strip().upper()\n funcs = common.service_pre_deployers.get(service)\n if funcs:\n print('Running pre-deployments for service %s...' % (service,))\n for func in funcs:\n func()"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ndeploy the service to the local cache.", "response": "def deploy(self):\n \"\"\"\n Applies routine, typically application-level changes to the service.\n \"\"\"\n for service in self.genv.services:\n service = service.strip().upper()\n funcs = common.service_deployers.get(service)\n if funcs:\n print('Deploying service %s...' % (service,))\n for func in funcs:\n if not self.dryrun:\n func()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nrun post - deployments for services that have requested be run before after deployment.", "response": "def post_deploy(self):\n \"\"\"\n Runs methods services have requested be run before after deployment.\n \"\"\"\n for service in self.genv.services:\n service = service.strip().upper()\n self.vprint('post_deploy:', service)\n funcs = common.service_post_deployers.get(service)\n if funcs:\n self.vprint('Running post-deployments for service %s...' % (service,))\n for func in funcs:\n try:\n func()\n except Exception as e:\n print('Post deployment error: %s' % e, file=sys.stderr)\n print(traceback.format_exc(), file=sys.stderr)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef pre_db_dump(self):\n for service in self.genv.services:\n service = service.strip().upper()\n funcs = common.service_pre_db_dumpers.get(service)\n if funcs:\n print('Running pre-database dump for service %s...' % (service,))\n for func in funcs:\n func()", "response": "Runs pre - database dump for all services that have requested to be run before each\n database dump."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef post_db_dump(self):\n for service in self.genv.services:\n service = service.strip().upper()\n funcs = common.service_post_db_dumpers.get(service)\n if funcs:\n print('Running post-database dump for service %s...' % (service,))\n for func in funcs:\n func()", "response": "Runs post - database dump for all services that have requested to be run before each\n database dump."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\napply one - time settings changes to the host.", "response": "def configure(self):\n \"\"\"\n Applies one-time settings changes to the host, usually to initialize the service.\n \"\"\"\n print('env.services:', self.genv.services)\n for service in list(self.genv.services):\n service = service.strip().upper()\n funcs = common.service_configurators.get(service, [])\n if funcs:\n print('!'*80)\n print('Configuring service %s...' % (service,))\n for func in funcs:\n print('Function:', func)\n if not self.dryrun:\n func()"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_locale_dict(self, text=None):\n text = text or self.cat_locale()\n # Format NAME=\"value\".\n return dict(re.findall(r'^([a-zA-Z_]+)\\s*=\\s*[\\'\\\"]*([0-8a-zA-Z_\\.\\:\\-]+)[\\'\\\"]*', text, re.MULTILINE))", "response": "Reads the locale file and returns a dictionary representing its key pairs."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef enable_mods(self):\n r = self.local_renderer\n for mod_name in r.env.mods_enabled:\n with self.settings(warn_only=True):\n self.enable_mod(mod_name)", "response": "Enable all modules in the current module list."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nbases on the number of sites per server and the number of resources on the server, calculates the optimal number of processes that should be allocated for each WSGI site.", "response": "def optimize_wsgi_processes(self):\n \"\"\"\n Based on the number of sites per server and the number of resources on the server,\n calculates the optimal number of processes that should be allocated for each WSGI site.\n \"\"\"\n r = self.local_renderer\n #r.env.wsgi_processes = 5\n r.env.wsgi_server_memory_gb = 8\n\n verbose = self.verbose\n\n all_sites = list(self.iter_sites(site=ALL, setter=self.set_site_specifics))"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ncreates a new local renderer.", "response": "def create_local_renderer(self):\n \"\"\"\n Instantiates a new local renderer.\n Override this to do any additional initialization.\n \"\"\"\n r = super(ApacheSatchel, self).create_local_renderer()\n\n # Dynamically set values based on target operating system.\n os_version = self.os_version\n apache_specifics = r.env.specifics[os_version.type][os_version.distro]\n r.env.update(apache_specifics)\n\n return r"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef install_auth_basic_user_file(self, site=None):\n r = self.local_renderer\n\n hostname = self.current_hostname\n\n target_sites = self.genv.available_sites_by_host.get(hostname, None)\n\n for _site, site_data in self.iter_sites(site=site, setter=self.set_site_specifics):\n if self.verbose:\n print('~'*80, file=sys.stderr)\n print('Site:', _site, file=sys.stderr)\n print('env.apache_auth_basic:', r.env.auth_basic, file=sys.stderr)\n\n # Only load site configurations that are allowed for this host.\n if target_sites is not None:\n assert isinstance(target_sites, (tuple, list))\n if _site not in target_sites:\n continue\n\n if not r.env.auth_basic:\n continue\n\n assert r.env.auth_basic_users, 'No apache auth users specified.'\n for username, password in r.env.auth_basic_users:\n r.env.auth_basic_username = username\n r.env.auth_basic_password = password\n r.env.apache_site = _site\n r.env.fn = r.format(r.env.auth_basic_authuserfile)\n if self.files.exists(r.env.fn):\n r.sudo('htpasswd -b {fn} {auth_basic_username} {auth_basic_password}')\n else:\n r.sudo('htpasswd -b -c {fn} {auth_basic_username} {auth_basic_password}')", "response": "Installs user file for apache auth."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nupload a set of media files to an Apache accessible directory.", "response": "def sync_media(self, sync_set=None, clean=0, iter_local_paths=0):\n \"\"\"\n Uploads select media to an Apache accessible directory.\n \"\"\"\n\n # Ensure a site is selected.\n self.genv.SITE = self.genv.SITE or self.genv.default_site\n\n r = self.local_renderer\n\n clean = int(clean)\n self.vprint('Getting site data for %s...' % self.genv.SITE)\n\n self.set_site_specifics(self.genv.SITE)\n\n sync_sets = r.env.sync_sets\n if sync_set:\n sync_sets = [sync_set]\n\n ret_paths = []\n for _sync_set in sync_sets:\n for paths in r.env.sync_sets[_sync_set]:\n r.env.sync_local_path = os.path.abspath(paths['local_path'] % self.genv)\n if paths['local_path'].endswith('/') and not r.env.sync_local_path.endswith('/'):\n r.env.sync_local_path += '/'\n\n if iter_local_paths:\n ret_paths.append(r.env.sync_local_path)\n continue\n\n r.env.sync_remote_path = paths['remote_path'] % self.genv\n\n if clean:\n r.sudo('rm -Rf {apache_sync_remote_path}')\n\n print('Syncing %s to %s...' % (r.env.sync_local_path, r.env.sync_remote_path))\n\n r.env.tmp_chmod = paths.get('chmod', r.env.chmod)\n r.sudo('mkdir -p {apache_sync_remote_path}')\n r.sudo('chmod -R {apache_tmp_chmod} {apache_sync_remote_path}')\n r.local('rsync -rvz --progress --recursive --no-p --no-g '\n '--rsh \"ssh -o StrictHostKeyChecking=no -i {key_filename}\" {apache_sync_local_path} {user}@{host_string}:{apache_sync_remote_path}')\n r.sudo('chown -R {apache_web_user}:{apache_web_group} {apache_sync_remote_path}')\n\n if iter_local_paths:\n return ret_paths"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_media_timestamp(self):\n from burlap.common import get_last_modified_timestamp\n data = 0\n for path in self.sync_media(iter_local_paths=1):\n data = min(data, get_last_modified_timestamp(path) or data)\n #TODO:hash media names and content\n if self.verbose:\n print('date:', data)\n return data", "response": "Get the last modified timestamp of all media in the local filesystem."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef record_manifest(self):\n manifest = super(ApacheSatchel, self).record_manifest()\n manifest['available_sites'] = self.genv.available_sites\n manifest['available_sites_by_host'] = self.genv.available_sites_by_host\n manifest['media_timestamp'] = self.get_media_timestamp()\n return manifest", "response": "Returns a manifest with all available data necessary to detect changes\n "} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef configure_modevasive(self):\n r = self.local_renderer\n if r.env.modevasive_enabled:\n self.install_packages()\n\n # Write conf for each Ubuntu version since they don't conflict.\n fn = r.render_to_file('apache/apache_modevasive.template.conf')\n\n # Ubuntu 12.04\n r.put(\n local_path=fn,\n remote_path='/etc/apache2/mods-available/mod-evasive.conf',\n use_sudo=True)\n\n # Ubuntu 14.04\n r.put(\n local_path=fn,\n remote_path='/etc/apache2/mods-available/evasive.conf',\n use_sudo=True)\n\n self.enable_mod('evasive')\n else:\n# print('self.last_manifest:', self.last_manifest)\n# print('a:', self.last_manifest.apache_modevasive_enabled)\n# print('b:', self.last_manifest.modevasive_enabled)\n if self.last_manifest.modevasive_enabled:\n self.disable_mod('evasive')", "response": "Configure the mod - evasive Apache module for combating DDOS attacks."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nconfiguring the mod - security module.", "response": "def configure_modsecurity(self):\n \"\"\"\n Installs the mod-security Apache module.\n\n https://www.modsecurity.org\n \"\"\"\n r = self.local_renderer\n if r.env.modsecurity_enabled and not self.last_manifest.modsecurity_enabled:\n\n self.install_packages()\n\n # Write modsecurity.conf.\n fn = self.render_to_file('apache/apache_modsecurity.template.conf')\n r.put(local_path=fn, remote_path='/etc/modsecurity/modsecurity.conf', use_sudo=True)\n\n # Write OWASP rules.\n r.env.modsecurity_download_filename = '/tmp/owasp-modsecurity-crs.tar.gz'\n r.sudo('cd /tmp; wget --output-document={apache_modsecurity_download_filename} {apache_modsecurity_download_url}')\n r.env.modsecurity_download_top = r.sudo(\n \"cd /tmp; \"\n \"tar tzf %(apache_modsecurity_download_filename)s | sed -e 's@/.*@@' | uniq\" % self.genv)\n r.sudo('cd /tmp; tar -zxvf %(apache_modsecurity_download_filename)s' % self.genv)\n r.sudo('cd /tmp; cp -R %(apache_modsecurity_download_top)s/* /etc/modsecurity/' % self.genv)\n r.sudo('mv /etc/modsecurity/modsecurity_crs_10_setup.conf.example /etc/modsecurity/modsecurity_crs_10_setup.conf')\n\n r.sudo('rm -f /etc/modsecurity/activated_rules/*')\n r.sudo('cd /etc/modsecurity/base_rules; '\n 'for f in * ; do ln -s /etc/modsecurity/base_rules/$f /etc/modsecurity/activated_rules/$f ; done')\n r.sudo('cd /etc/modsecurity/optional_rules; '\n 'for f in * ; do ln -s /etc/modsecurity/optional_rules/$f /etc/modsecurity/activated_rules/$f ; done')\n\n r.env.httpd_conf_append.append('Include \"/etc/modsecurity/activated_rules/*.conf\"')\n\n self.enable_mod('evasive')\n self.enable_mod('headers')\n elif not self.env.modsecurity_enabled and self.last_manifest.modsecurity_enabled:\n self.disable_mod('modsecurity')"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ninstalling the mod - rpaf Apache module.", "response": "def configure_modrpaf(self):\n \"\"\"\n Installs the mod-rpaf Apache module.\n\n https://github.com/gnif/mod_rpaf\n \"\"\"\n r = self.local_renderer\n if r.env.modrpaf_enabled:\n self.install_packages()\n self.enable_mod('rpaf')\n else:\n if self.last_manifest.modrpaf_enabled:\n self.disable_mod('mod_rpaf')"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef configure_site(self, full=1, site=None, delete_old=0):\n from burlap import service\n\n r = self.local_renderer\n\n print('Configuring Apache...', file=sys.stderr)\n\n site = site or self.genv.SITE\n\n if int(delete_old) and site == ALL:\n # Delete all existing enabled and available sites.\n r.sudo('rm -f {sites_available}/*')\n r.sudo('rm -f {sites_enabled}/*')\n\n if r.env.manage_site_conf:\n\n # Run an optional customizable command to clear or delete old sites before writing the new ones.\n if r.env.delete_site_command:\n r.sudo(r.env.delete_site_command)\n\n for _site, site_data in self.iter_sites(site=site, setter=self.set_site_specifics):\n r = self.local_renderer\n\n #r.env.site = site\n if self.verbose:\n print('-'*80, file=sys.stderr)\n print('Site:', _site, file=sys.stderr)\n print('-'*80, file=sys.stderr)\n\n r.env.ssl = _site.endswith('_secure')\n r.env.apache_site = _site\n r.env.server_name = r.format(r.env.domain_template)\n\n # Write WSGI template\n if r.env.wsgi_enabled:\n r.pc('Writing WSGI template for site %s...' % _site)\n r.env.wsgi_scriptalias = r.format(r.env.wsgi_scriptalias)\n fn = self.render_to_file(r.env.wsgi_template)\n r.env.wsgi_dir = r.env.remote_dir = os.path.split(r.env.wsgi_scriptalias)[0]\n r.sudo('mkdir -p {remote_dir}')\n r.put(local_path=fn, remote_path=r.env.wsgi_scriptalias, use_sudo=True)\n\n # Write site configuration.\n r.pc('Writing site configuration for site %s...' % _site)\n r.env.auth_basic_authuserfile = r.format(self.env.auth_basic_authuserfile)\n r.env.ssl_certificates = list(self.iter_certificates())\n if r.env.server_aliases_template:\n r.env.server_aliases = r.format(r.env.server_aliases_template)\n if r.env.domain_with_sub_template:\n r.env.domain_with_sub = r.format(r.env.domain_with_sub_template)\n if r.env.domain_without_sub_template:\n r.env.domain_without_sub = r.format(r.env.domain_without_sub_template)\n if r.env.domain_template:\n r.env.domain = r.format(r.env.domain_template)\n genv = r.collect_genv()\n genv['current_hostname'] = self.current_hostname\n fn = self.render_to_file(\n self.env.site_template,\n extra=genv,\n formatter=partial(r.format, ignored_variables=self.env.ignored_template_variables))\n r.env.site_conf = _site+'.conf'\n r.env.site_conf_fqfn = os.path.join(r.env.sites_available, r.env.site_conf)\n r.put(local_path=fn, remote_path=r.env.site_conf_fqfn, use_sudo=True)\n\n self.enable_site(_site)\n\n self.clear_local_renderer()\n\n self.enable_mods()\n\n if int(full):\n # Write master Apache configuration file.\n if r.env.manage_httpd_conf:\n fn = self.render_to_file('apache/apache_httpd.template.conf')\n r.put(local_path=fn, remote_path=r.env.conf, use_sudo=True)\n\n # Write Apache listening ports configuration.\n if r.env.manage_ports_conf:\n fn = self.render_to_file('apache/apache_ports.template.conf')\n r.put(local_path=fn, remote_path=r.env.ports_path, use_sudo=True)\n\n r.sudo('chown -R {apache_web_user}:{apache_web_group} {apache_root}')", "response": "Configure Apache to host one or more websites."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nforwards all traffic to a page saying the server is down for maintenance.", "response": "def maint_up(self):\n \"\"\"\n Forwards all traffic to a page saying the server is down for maintenance.\n \"\"\"\n r = self.local_renderer\n fn = self.render_to_file(r.env.maintenance_template, extra={'current_hostname': self.current_hostname})\n r.put(local_path=fn, remote_path=r.env.maintenance_path, use_sudo=True)\n r.sudo('chown -R {apache_web_user}:{apache_web_group} {maintenance_path}')"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef restart(self):\n n = 60\n sleep_n = int(self.env.max_restart_wait_minutes/10.*60)\n for _ in xrange(n):\n self.stop()\n if self.dryrun or not self.is_running():\n break\n print('Waiting for supervisor to stop (%i of %i)...' % (_, n))\n time.sleep(sleep_n)\n self.start()\n for _ in xrange(n):\n if self.dryrun or self.is_running():\n return\n print('Waiting for supervisor to start (%i of %i)...' % (_, n))\n time.sleep(sleep_n)\n raise Exception('Failed to restart service %s!' % self.name)", "response": "Supervisor can take a very long time to start and stop,\n so wait for it."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ncalling after a deployment to record any data necessary to detect changes in Apache site configs.", "response": "def record_manifest(self):\n \"\"\"\n Called after a deployment to record any data necessary to detect changes\n for a future deployment.\n \"\"\"\n data = super(SupervisorSatchel, self).record_manifest()\n\n # Celery deploys itself through supervisor, so monitor its changes too in Apache site configs.\n for site_name, site_data in self.genv.sites.items():\n if self.verbose:\n print(site_name, site_data)\n data['celery_has_worker_%s' % site_name] = site_data.get('celery_has_worker', False)\n\n data['configured'] = True\n\n # Generate services list.\n self.write_configs(upload=0)\n #data['services_rendered'] = ''\n\n return data"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef deploy_services(self, site=None):\n\n verbose = self.verbose\n\n r = self.local_renderer\n if not r.env.manage_configs:\n return\n#\n# target_sites = self.genv.available_sites_by_host.get(hostname, None)\n\n self.render_paths()\n\n supervisor_services = []\n\n if r.env.purge_all_confs:\n r.sudo('rm -Rf /etc/supervisor/conf.d/*')\n\n #TODO:check available_sites_by_host and remove dead?\n self.write_configs(site=site)\n for _site, site_data in self.iter_sites(site=site, renderer=self.render_paths):\n if verbose:\n print('deploy_services.site:', _site)\n\n # Only load site configurations that are allowed for this host.\n# if target_sites is not None:\n# assert isinstance(target_sites, (tuple, list))\n# if site not in target_sites:\n# continue\n\n for cb in self.genv._supervisor_create_service_callbacks:\n if self.verbose:\n print('cb:', cb)\n ret = cb(site=_site)\n if self.verbose:\n print('ret:', ret)\n if isinstance(ret, six.string_types):\n supervisor_services.append(ret)\n elif isinstance(ret, tuple):\n assert len(ret) == 2\n conf_name, conf_content = ret\n if self.dryrun:\n print('supervisor conf filename:', conf_name)\n print(conf_content)\n self.write_to_file(conf_content)\n\n self.env.services_rendered = '\\n'.join(supervisor_services)\n\n fn = self.render_to_file(self.env.config_template)\n r.put(local_path=fn, remote_path=self.env.config_path, use_sudo=True)\n\n # We use supervisorctl to configure supervisor, but this will throw a uselessly vague\n # error message is supervisor isn't running.\n if not self.is_running():\n self.start()\n\n # Reload config and then add and remove as necessary (restarts programs)\n r.sudo('supervisorctl update')", "response": "Deploy all services for a given site and writes the appropriate supervisord. conf file."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef clone(self, remote_url, path=None, use_sudo=False, user=None):\n\n cmd = 'git clone --quiet %s' % remote_url\n if path is not None:\n cmd = cmd + ' %s' % path\n\n if use_sudo and user is None:\n run_as_root(cmd)\n elif use_sudo:\n sudo(cmd, user=user)\n else:\n run(cmd)", "response": "Clone a remote Git repository into a new directory."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nadding a remote to a Git working copy.", "response": "def add_remote(self, path, name, remote_url, use_sudo=False, user=None, fetch=True):\n \"\"\"\n Add a remote Git repository into a directory.\n\n :param path: Path of the working copy directory. This directory must exist\n and be a Git working copy with a default remote to fetch from.\n :type path: str\n\n :param use_sudo: If ``True`` execute ``git`` with\n :func:`fabric.operations.sudo`, else with\n :func:`fabric.operations.run`.\n :type use_sudo: bool\n\n :param user: If ``use_sudo is True``, run :func:`fabric.operations.sudo`\n with the given user. If ``use_sudo is False`` this parameter\n has no effect.\n :type user: str\n\n :param name: name for the remote repository\n :type name: str\n\n :param remote_url: URL of the remote repository\n :type remote_url: str\n\n :param fetch: If ``True`` execute ``git remote add -f``\n :type fetch: bool\n \"\"\"\n if path is None:\n raise ValueError(\"Path to the working copy is needed to add a remote\")\n\n if fetch:\n cmd = 'git remote add -f %s %s' % (name, remote_url)\n else:\n cmd = 'git remote add %s %s' % (name, remote_url)\n\n with cd(path):\n if use_sudo and user is None:\n run_as_root(cmd)\n elif use_sudo:\n sudo(cmd, user=user)\n else:\n run(cmd)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef fetch(self, path, use_sudo=False, user=None, remote=None):\n\n if path is None:\n raise ValueError(\"Path to the working copy is needed to fetch from a remote repository.\")\n\n if remote is not None:\n cmd = 'git fetch %s' % remote\n else:\n cmd = 'git fetch'\n\n with cd(path):\n if use_sudo and user is None:\n run_as_root(cmd)\n elif use_sudo:\n sudo(cmd, user=user)\n else:\n run(cmd)", "response": "Fetch changes from the default remote repository."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef pull(self, path, use_sudo=False, user=None, force=False):\n\n if path is None:\n raise ValueError(\"Path to the working copy is needed to pull from a remote repository.\")\n\n options = []\n if force:\n options.append('--force')\n options = ' '.join(options)\n\n cmd = 'git pull %s' % options\n\n with cd(path):\n if use_sudo and user is None:\n run_as_root(cmd)\n elif use_sudo:\n sudo(cmd, user=user)\n else:\n run(cmd)", "response": "Pulls changes from the default remote repository and merges them into the given working copy."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngetting all commits between the given commit numbers", "response": "def get_logs_between_commits(self, a, b):\n \"\"\"\n Retrieves all commit messages for all commits between the given commit numbers\n on the current branch.\n \"\"\"\n print('REAL')\n ret = self.local('git --no-pager log --pretty=oneline %s...%s' % (a, b), capture=True)\n if self.verbose:\n print(ret)\n return str(ret)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_current_commit(self):\n with hide('running', 'stdout', 'stderr', 'warnings'):\n s = str(self.local('git rev-parse HEAD', capture=True))\n self.vprint('current commit:', s)\n return s", "response": "Returns the current git commit number of the current head branch."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning a manifest that contains all the information necessary to detect changes for this repository.", "response": "def record_manifest(self):\n \"\"\"\n Called after a deployment to record any data necessary to detect changes\n for a future deployment.\n \"\"\"\n manifest = super(GitTrackerSatchel, self).record_manifest()\n manifest[CURRENT_COMMIT] = self.get_current_commit()\n return manifest"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef ssh_config(self, name=''):\n r = self.local_renderer\n with self.settings(hide('running')):\n output = r.local('vagrant ssh-config %s' % name, capture=True)\n\n config = {}\n for line in output.splitlines()[1:]:\n key, value = line.strip().split(' ', 2)\n config[key] = value\n return config", "response": "Get the SSH parameters for connecting to a vagrant VM."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef version(self):\n r = self.local_renderer\n with self.settings(hide('running', 'warnings'), warn_only=True):\n res = r.local('vagrant --version', capture=True)\n if res.failed:\n return None\n line = res.splitlines()[-1]\n version = re.match(r'Vagrant (?:v(?:ersion )?)?(.*)', line).group(1)\n return tuple(_to_int(part) for part in version.split('.'))", "response": "Get the Vagrant version."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nrunning the following tasks on a Vagrant box.", "response": "def vagrant(self, name=''):\n \"\"\"\n Run the following tasks on a vagrant box.\n\n First, you need to import this task in your ``fabfile.py``::\n\n from fabric.api import *\n from burlap.vagrant import vagrant\n\n @task\n def some_task():\n run('echo hello')\n\n Then you can easily run tasks on your current Vagrant box::\n\n $ fab vagrant some_task\n\n \"\"\"\n r = self.local_renderer\n config = self.ssh_config(name)\n\n extra_args = self._settings_dict(config)\n r.genv.update(extra_args)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef vagrant_settings(self, name='', *args, **kwargs):\n config = self.ssh_config(name)\n\n extra_args = self._settings_dict(config)\n kwargs.update(extra_args)\n\n return self.settings(*args, **kwargs)", "response": "Context manager that sets a vagrant VM\n as the remote host."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef base_boxes(self):\n return sorted(list(set([name for name, provider in self._box_list()])))", "response": "Get the list of base boxes in this vagrant instance"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef install_from_upstream(self):\n from burlap.system import get_arch, distrib_family\n r = self.local_renderer\n content = urlopen(r.env.download_url).read()\n print(len(content))\n matches = DOWNLOAD_LINK_PATTERN.findall(content)\n print(matches)\n arch = get_arch() # e.g. 'x86_64'\n family = distrib_family()\n if family == DEBIAN:\n ext = '.deb'\n matches = [match for match in matches if match.endswith(ext) and arch in match]\n print('matches:', matches)\n assert matches, \"No matches found.\"\n assert len(matches) == 1, \"Too many matches found: %s\" % (', '.join(matches))\n r.env.final_download_url = matches[0]\n r.env.local_filename = '/tmp/vagrant%s' % ext\n r.run('wget -O {local_filename} {final_download_url}')\n r.sudo('dpkg -i {local_filename}')\n else:\n raise NotImplementedError('Unsupported family: %s' % family)", "response": "Installs Vagrant from the most recent package available from their homepage."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef distrib_id():\n\n with settings(hide('running', 'stdout')):\n kernel = (run('uname -s') or '').strip().lower()\n if kernel == LINUX:\n # lsb_release works on Ubuntu and Debian >= 6.0\n # but is not always included in other distros\n if is_file('/usr/bin/lsb_release'):\n id_ = run('lsb_release --id --short').strip().lower()\n if id in ['arch', 'archlinux']: # old IDs used before lsb-release 1.4-14\n id_ = ARCH\n return id_\n else:\n if is_file('/etc/debian_version'):\n return DEBIAN\n elif is_file('/etc/fedora-release'):\n return FEDORA\n elif is_file('/etc/arch-release'):\n return ARCH\n elif is_file('/etc/redhat-release'):\n release = run('cat /etc/redhat-release')\n if release.startswith('Red Hat Enterprise Linux'):\n return REDHAT\n elif release.startswith('CentOS'):\n return CENTOS\n elif release.startswith('Scientific Linux'):\n return SLES\n elif is_file('/etc/gentoo-release'):\n return GENTOO\n elif kernel == SUNOS:\n return SUNOS", "response": "Get the OS distribution ID."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef distrib_release():\n with settings(hide('running', 'stdout')):\n kernel = (run('uname -s') or '').strip().lower()\n if kernel == LINUX:\n return run('lsb_release -r --short')\n\n elif kernel == SUNOS:\n return run('uname -v')", "response": "Get the release number of the distribution."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the distribution family.", "response": "def distrib_family():\n \"\"\"\n Get the distribution family.\n\n Returns one of ``debian``, ``redhat``, ``arch``, ``gentoo``,\n ``sun``, ``other``.\n \"\"\"\n distrib = (distrib_id() or '').lower()\n if distrib in ['debian', 'ubuntu', 'linuxmint', 'elementary os']:\n return DEBIAN\n elif distrib in ['redhat', 'rhel', 'centos', 'sles', 'fedora']:\n return REDHAT\n elif distrib in ['sunos']:\n return SUN\n elif distrib in ['gentoo']:\n return GENTOO\n elif distrib in ['arch', 'manjarolinux']:\n return ARCH\n return 'other'"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef supported_locales():\n family = distrib_family()\n if family == 'debian':\n return _parse_locales('/usr/share/i18n/SUPPORTED')\n elif family == 'arch':\n return _parse_locales('/etc/locale.gen')\n elif family == 'redhat':\n return _supported_locales_redhat()\n else:\n raise UnsupportedFamily(supported=['debian', 'arch', 'redhat'])", "response": "Returns the list of supported locales."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef set_permissions(self):\n r = self.local_renderer\n for path in r.env.paths_owned:\n r.env.path_owned = path\n r.sudo('chown {celery_daemon_user}:{celery_daemon_user} {celery_path_owned}')", "response": "Sets ownership and permissions for Celery - related files."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef create_supervisor_services(self, site):\n\n self.vprint('create_supervisor_services:', site)\n\n self.set_site_specifics(site=site)\n\n r = self.local_renderer\n if self.verbose:\n print('r.env:')\n pprint(r.env, indent=4)\n\n self.vprint('r.env.has_worker:', r.env.has_worker)\n if not r.env.has_worker:\n self.vprint('skipping: no celery worker')\n return\n\n if self.name.lower() not in self.genv.services:\n self.vprint('skipping: celery not enabled')\n return\n\n hostname = self.current_hostname\n target_sites = self.genv.available_sites_by_host.get(hostname, None)\n if target_sites and site not in target_sites:\n self.vprint('skipping: site not supported on this server')\n return\n\n self.render_paths()\n\n conf_name = 'celery_%s.conf' % site\n ret = r.render_to_string('celery/celery_supervisor.template.conf')\n return conf_name, ret", "response": "This method creates a Celery config file for each site."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncheck if all tests have passed for this branch.", "response": "def check_ok(self):\n \"\"\"\n Ensures all tests have passed for this branch.\n\n This should be called before deployment, to prevent accidental deployment of code\n that hasn't passed automated testing.\n \"\"\"\n import requests\n\n if not self.env.check_ok:\n return\n\n # Find current git branch.\n branch_name = self._local('git rev-parse --abbrev-ref HEAD', capture=True).strip()\n\n check_ok_paths = self.env.check_ok_paths or {}\n\n if branch_name in check_ok_paths:\n check = check_ok_paths[branch_name]\n if 'username' in check:\n auth = (check['username'], check['password'])\n else:\n auth = None\n ret = requests.get(check['url'], auth=auth)\n passed = check['text'] in ret.content\n assert passed, 'Check failed: %s' % check['url']"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef is_present(self, host=None):\n r = self.local_renderer\n r.env.host = host or self.genv.host_string\n ret = r._local(\"getent hosts {host} | awk '{{ print $1 }}'\", capture=True) or ''\n if self.verbose:\n print('ret:', ret)\n ret = ret.strip()\n if self.verbose:\n print('Host %s %s present.' % (r.env.host, 'IS' if bool(ret) else 'IS NOT'))\n ip = ret\n ret = bool(ret)\n if not ret:\n return False\n\n r.env.ip = ip\n with settings(warn_only=True):\n ret = r._local('ping -c 1 {ip}', capture=True) or ''\n packet_loss = re.findall(r'([0-9]+)% packet loss', ret)\n# print('packet_loss:',packet_loss)\n ip_accessible = packet_loss and int(packet_loss[0]) < 100\n if self.verbose:\n print('IP %s accessible: %s' % (ip, ip_accessible))\n return bool(ip_accessible)", "response": "Returns true if the given host exists on the network."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef purge_keys(self):\n r = self.local_renderer\n r.env.default_ip = self.hostname_to_ip(self.env.default_hostname)\n r.env.home_dir = '/home/%s' % getpass.getuser()\n r.local('ssh-keygen -f \"{home_dir}/.ssh/known_hosts\" -R {host_string}')\n if self.env.default_hostname:\n r.local('ssh-keygen -f \"{home_dir}/.ssh/known_hosts\" -R {default_hostname}')\n if r.env.default_ip:\n r.local('ssh-keygen -f \"{home_dir}/.ssh/known_hosts\" -R {default_ip}')", "response": "Delete all SSH keys on the localhost associated with the current remote host."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef find_working_password(self, usernames=None, host_strings=None):\n r = self.local_renderer\n\n if host_strings is None:\n host_strings = []\n\n if not host_strings:\n host_strings.append(self.genv.host_string)\n\n if usernames is None:\n usernames = []\n\n if not usernames:\n usernames.append(self.genv.user)\n\n for host_string in host_strings:\n\n for username in usernames:\n\n passwords = []\n passwords.append(self.genv.user_default_passwords[username])\n passwords.append(self.genv.user_passwords[username])\n passwords.append(self.env.default_password)\n\n for password in passwords:\n\n with settings(warn_only=True):\n r.env.host_string = host_string\n r.env.password = password\n r.env.user = username\n ret = r._local(\"sshpass -p '{password}' ssh -o StrictHostKeyChecking=no {user}@{host_string} echo hello\", capture=True)\n #print('ret.return_code:', ret.return_code)\n # print('ret000:[%s]' % ret)\n #code 1 = good password, but prompts needed\n #code 5 = bad password\n #code 6 = good password, but host public key is unknown\n\n if ret.return_code in (1, 6) or 'hello' in ret:\n # Login succeeded, so we haven't yet changed the password, so use the default password.\n return host_string, username, password\n\n raise Exception('No working login found.')", "response": "Find the first working password for the current host."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef needs_initrole(self, stop_on_error=False):\n\n ret = False\n\n target_host_present = self.is_present()\n\n if not target_host_present:\n default_host_present = self.is_present(self.env.default_hostname)\n if default_host_present:\n if self.verbose:\n print('Target host missing and default host present so host init required.')\n ret = True\n else:\n if self.verbose:\n print('Target host missing but default host also missing, '\n 'so no host init required.')\n# if stop_on_error:\n# raise Exception(\n# 'Both target and default hosts missing! '\n# 'Is the machine turned on and plugged into the network?')\n else:\n if self.verbose:\n print('Target host is present so no host init required.')\n\n return ret", "response": "Returns True if the host is present and has its initial configuration set set."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef initrole(self, check=True):\n\n if self.env.original_user is None:\n self.env.original_user = self.genv.user\n\n if self.env.original_key_filename is None:\n self.env.original_key_filename = self.genv.key_filename\n\n host_string = None\n user = None\n password = None\n if self.env.login_check:\n host_string, user, password = self.find_working_password(\n usernames=[self.genv.user, self.env.default_user],\n host_strings=[self.genv.host_string, self.env.default_hostname],\n )\n if self.verbose:\n print('host.initrole.host_string:', host_string)\n print('host.initrole.user:', user)\n print('host.initrole.password:', password)\n\n# needs = True\n# if check:\n# needs = self.needs_initrole(stop_on_error=True)\n needs = False\n\n if host_string is not None:\n self.genv.host_string = host_string\n if user is not None:\n self.genv.user = user\n if password is not None:\n self.genv.password = password\n\n if not needs:\n return\n\n assert self.env.default_hostname, 'No default hostname set.'\n assert self.env.default_user, 'No default user set.'\n\n self.genv.host_string = self.env.default_hostname\n if self.env.default_hosts:\n self.genv.hosts = self.env.default_hosts\n else:\n self.genv.hosts = [self.env.default_hostname]\n\n self.genv.user = self.env.default_user\n self.genv.password = self.env.default_password\n self.genv.key_filename = self.env.default_key_filename\n\n # If the host has been reformatted, the SSH keys will mismatch, throwing an error, so clear them.\n self.purge_keys()\n\n # Do a test login with the default password to determine which password we should use.\n# r.env.password = self.env.default_password\n# with settings(warn_only=True):\n# ret = r._local(\"sshpass -p '{password}' ssh -o StrictHostKeyChecking=no {user}@{host_string} echo hello\", capture=True)\n# print('ret.return_code:', ret.return_code)\n# # print('ret000:[%s]' % ret)\n# #code 1 = good password, but prompts needed\n# #code 5 = bad password\n# #code 6 = good password, but host public key is unknown\n# if ret.return_code in (1, 6) or 'hello' in ret:\n# # Login succeeded, so we haven't yet changed the password, so use the default password.\n# self.genv.password = self.env.default_password\n# elif self.genv.user in self.genv.user_passwords:\n# # Otherwise, use the password or key set in the config.\n# self.genv.password = self.genv.user_passwords[self.genv.user]\n# else:\n# # Default password fails and there's no current password, so clear.\n# self.genv.password = None\n# self.genv.password = self.find_working_password()\n# print('host.initrole,using password:', self.genv.password)\n\n # Execute post-init callbacks.\n for task_name in self.env.post_initrole_tasks:\n if self.verbose:\n print('Calling post initrole task %s' % task_name)\n satchel_name, method_name = task_name.split('.')\n satchel = self.get_satchel(name=satchel_name)\n getattr(satchel, method_name)()\n\n print('^'*80)\n print('host.initrole.host_string:', self.genv.host_string)\n print('host.initrole.user:', self.genv.user)\n print('host.initrole.password:', self.genv.password)", "response": "Called to set default password login for systems that do not yet have passwordless\n login setup."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef iter_hostnames(self):\n from burlap.common import get_hosts_retriever\n if self.env.use_retriever:\n self.vprint('using retriever')\n self.vprint('hosts:', self.genv.hosts)\n retriever = get_hosts_retriever()\n hosts = list(retriever(extended=1))\n for _hostname, _data in hosts:\n\n # Skip hosts that aren't selected for this run.\n if self.genv.hosts \\\n and _data.ip not in self.genv.hosts \\\n and _data.public_dns_name not in self.genv.hosts \\\n and _hostname not in self.genv.hosts:\n continue\n\n assert _data.ip, 'Missing IP.'\n yield _data.ip, _hostname#_data.public_dns_name\n else:\n self.vprint('using default')\n for ip, hostname in self.env.hostnames.items():\n self.vprint('ip lookup:', ip, hostname)\n if ip == UNKNOWN:\n ip = self.hostname_to_ip(hostname)\n if not ip and hostname in self.env.default_hostnames:\n ip = self.hostname_to_ip(self.env.default_hostnames[hostname])\n elif not ip[0].isdigit():\n ip = self.hostname_to_ip(ip)\n assert ip, 'Invalid IP.'\n yield ip, hostname", "response": "Yields a list of tuples of the form ip hostname."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_public_ip(self):\n r = self.local_renderer\n ret = r.run(r.env.get_public_ip_command) or ''\n ret = ret.strip()\n print('ip:', ret)\n return ret", "response": "Gets the public IP for a host."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nconfigures the local environment for the current server.", "response": "def configure(self, reboot=1):\n \"\"\"\n Assigns a name to the server accessible from user space.\n\n Note, we add the name to /etc/hosts since not all programs use\n /etc/hostname to reliably identify the server hostname.\n \"\"\"\n r = self.local_renderer\n for ip, hostname in self.iter_hostnames():\n self.vprint('ip/hostname:', ip, hostname)\n r.genv.host_string = ip\n r.env.hostname = hostname\n with settings(warn_only=True):\n r.sudo('echo \"{hostname}\" > /etc/hostname')\n r.sudo('echo \"127.0.0.1 {hostname}\" | cat - /etc/hosts > /tmp/out && mv /tmp/out /etc/hosts')\n r.sudo(r.env.set_hostname_command)\n if r.env.auto_reboot and int(reboot):\n r.reboot()"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nupdates the ticket numbers in JIRA based on the changes made in the current git repository.", "response": "def update_tickets_from_git(self, from_commit=None, to_commit=None):\n \"\"\"\n Run during a deployment.\n Looks at all commits between now and the last deployment.\n Finds all ticket numbers and updates their status in Jira.\n \"\"\"\n from jira import JIRA, JIRAError\n #from burlap.deploy import get_last_current_diffs\n from burlap.git import gittracker, CURRENT_COMMIT\n\n r = self.local_renderer\n\n# get_current_commit = gittracker.get_current_commit\n# GITTRACKER = gittracker.name.upper()\n\n # Ensure this is only run once per role.\n if self.genv.host_string != self.genv.hosts[-1]:\n self.vprint('Not first server. Aborting.')\n return\n\n print('self.env.update_from_git:', self.env.update_from_git)\n print('self.genv.jirahelper_update_from_git:', self.genv.jirahelper_update_from_git)\n if not self.env.update_from_git:\n self.vprint('Update from git disabled. Aborting.')\n return\n\n if not self.env.ticket_pattern:\n self.vprint('No ticket pattern defined. Aborting.')\n return\n\n if not self.env.basic_auth_username or not self.env.basic_auth_password:\n self.vprint('Username or password not given. Aborting.')\n return\n\n # During a deployment, we should be given these, but for testing,\n # lookup the diffs dynamically.\n last = gittracker.last_manifest\n current = gittracker.current_manifest\n\n last_commit = from_commit or last.current_commit#[CURRENT_COMMIT]\n print('last_commit:', last_commit)\n current_commit = to_commit or current[CURRENT_COMMIT]\n print('current_commit:', current_commit)\n\n if not last_commit or not current_commit:\n print('Missing commit ID. Aborting.')\n return\n\n if self.verbose:\n print('-'*80)\n print('last.keys:', last.keys())\n print('-'*80)\n print('current.keys:', current.keys())\n\n# try:\n# last_commit = last['GITTRACKER']['current_commit']\n# except KeyError:\n# return\n# current_commit = current['GITTRACKER']['current_commit']\n\n # Find all tickets deployed between last deployment and now.\n tickets = self.get_tickets_between_commits(current_commit, last_commit)\n if self.verbose:\n print('tickets:', tickets)\n\n # Update all tickets in Jira.\n jira = JIRA({\n 'server': self.env.server\n }, basic_auth=(self.env.basic_auth_username, self.env.basic_auth_password))\n for ticket in tickets:\n\n # Mention this Jira updated.\n r.env.role = r.genv.ROLE.lower()\n comment = r.format(self.env.ticket_update_message_template)\n print('Commenting on ticket %s: %s' % (ticket, comment))\n if not self.dryrun:\n jira.add_comment(ticket, comment)\n\n # Update ticket status.\n recheck = False\n while 1:\n print('Looking up jira ticket %s...' % ticket)\n issue = jira.issue(ticket)\n self.vprint('Ticket %s retrieved.' % ticket)\n transition_to_id = dict((t['name'], t['id']) for t in jira.transitions(issue))\n self.vprint('%i allowable transitions found:' % len(transition_to_id))\n pprint(transition_to_id)\n self.vprint('issue.fields.status.id:', issue.fields.status.id)\n self.vprint('issue.fields.status.name:', issue.fields.status.name)\n jira_status_id = issue.fields.status.name.title()\n self.vprint('jira_status_id:', jira_status_id)\n next_transition_name = self.env.deploy_workflow.get(jira_status_id)\n self.vprint('next_transition_name:', next_transition_name)\n next_transition_id = transition_to_id.get(next_transition_name)\n self.vprint('next_transition_id:', next_transition_id)\n if next_transition_name:\n if issue.fields.assignee:\n if issue.fields.assignee.raw:\n assignee_name = issue.fields.assignee.name\n else:\n # Get assignee name directly\n # https://community.atlassian.com/t5/Jira-questions/Jira-in-Python-issue-fields-reporter-name-\n # errors-with-TypeError/qaq-p/937924\n assignee_name = issue.fields.assignee._session['name']\n else:\n assignee_name = None\n\n # Get new assignee by status\n new_assignee = self.env.assignee_by_status.get(\n #issue.fields.status.name.title(),\n next_transition_name,\n assignee_name,\n )\n\n # If assigning to reporter, get reporter name.\n if new_assignee == 'reporter':\n if issue.fields.reporter.raw:\n new_assignee = issue.fields.reporter.name\n else:\n # Get reporter name directly\n # https://community.atlassian.com/t5/Jira-questions/Jira-in-Python-issue-fields-reporter-name-\n # errors-with-TypeError/qaq-p/937924\n new_assignee = issue.fields.reporter._session['name']\n\n print('Updating ticket %s to status %s (%s) and assigning it to %s.' % (ticket, next_transition_name, next_transition_id, new_assignee))\n if not self.dryrun:\n\n if next_transition_id:\n try:\n jira.transition_issue(issue, next_transition_id)\n recheck = True\n except AttributeError as e:\n print('Unable to transition ticket %s to %s: %s' % (ticket, next_transition_name, e), file=sys.stderr)\n traceback.print_exc()\n\n # Note assignment should happen after transition, since the assignment may\n # effect remove transitions that we need.\n try:\n if new_assignee:\n print('Assigning ticket %s to %s.' % (ticket, new_assignee))\n jira.assign_issue(issue, new_assignee)\n else:\n print('No new assignee found.')\n except JIRAError as e:\n print('Unable to reassign ticket %s to %s: %s' % (ticket, new_assignee, e), file=sys.stderr)\n else:\n recheck = False\n print('No transitions found for ticket %s currently in status \"%s\".' % (ticket, issue.fields.status.name))\n\n if not recheck:\n break"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef partitions(device=\"\"):\n partitions_list = {}\n with settings(hide('running', 'stdout')):\n res = run_as_root('sfdisk -d %(device)s' % locals())\n\n spart = re.compile(r'(?P^/.*) : .* Id=(?P[0-9a-z]+)')\n for line in res.splitlines():\n m = spart.search(line)\n if m:\n partitions_list[m.group('pname')] = int(m.group('ptypeid'), 16)\n\n return partitions_list", "response": "Get a partition list for all disk or for selected device only"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nget a HDD device by uuid", "response": "def getdevice_by_uuid(uuid):\n \"\"\"\n Get a HDD device by uuid\n\n Example::\n\n from burlap.disk import getdevice_by_uuid\n\n device = getdevice_by_uuid(\"356fafdc-21d5-408e-a3e9-2b3f32cb2a8c\")\n if device:\n mount(device,'/mountpoint')\n \"\"\"\n with settings(hide('running', 'warnings', 'stdout'), warn_only=True):\n res = run_as_root('blkid -U %s' % uuid)\n\n if not res.succeeded:\n return None\n\n return res"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef ismounted(device):\n # Check filesystem\n with settings(hide('running', 'stdout')):\n res = run_as_root('mount')\n for line in res.splitlines():\n fields = line.split()\n if fields[0] == device:\n return True\n\n # Check swap\n with settings(hide('running', 'stdout')):\n res = run_as_root('swapon -s')\n for line in res.splitlines():\n fields = line.split()\n if fields[0] == device:\n return True\n\n return False", "response": "Check if partition is mounted"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef query(query, use_sudo=True, **kwargs):\n func = use_sudo and run_as_root or run\n\n user = kwargs.get('mysql_user') or env.get('mysql_user')\n password = kwargs.get('mysql_password') or env.get('mysql_password')\n\n options = [\n '--batch',\n '--raw',\n '--skip-column-names',\n ]\n if user:\n options.append('--user=%s' % quote(user))\n if password:\n options.append('--password=%s' % quote(password))\n options = ' '.join(options)\n\n return func('mysql %(options)s --execute=%(query)s' % {\n 'options': options,\n 'query': quote(query),\n })", "response": "Run a MySQL query."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef user_exists(name, host='localhost', **kwargs):\n with settings(hide('running', 'stdout', 'stderr', 'warnings'), warn_only=True):\n res = query(\"\"\"\n use mysql;\n SELECT COUNT(*) FROM user\n WHERE User = '%(name)s' AND Host = '%(host)s';\n \"\"\" % {\n 'name': name,\n 'host': host,\n }, **kwargs)\n return res.succeeded and (int(res) == 1)", "response": "Check if a MySQL user exists."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef create_user(name, password, host='localhost', **kwargs):\n with settings(hide('running')):\n query(\"CREATE USER '%(name)s'@'%(host)s' IDENTIFIED BY '%(password)s';\" % {\n 'name': name,\n 'password': password,\n 'host': host\n }, **kwargs)\n puts(\"Created MySQL user '%s'.\" % name)", "response": "Create a MySQL user."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nchecks if a MySQL database exists.", "response": "def database_exists(name, **kwargs):\n \"\"\"\n Check if a MySQL database exists.\n \"\"\"\n with settings(hide('running', 'stdout', 'stderr', 'warnings'), warn_only=True):\n res = query(\"SHOW DATABASES LIKE '%(name)s';\" % {\n 'name': name\n }, **kwargs)\n\n return res.succeeded and (res == name)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncreate a MySQL database.", "response": "def create_database(name, owner=None, owner_host='localhost', charset='utf8',\n collate='utf8_general_ci', **kwargs):\n \"\"\"\n Create a MySQL database.\n\n Example::\n\n import burlap\n\n # Create DB if it does not exist\n if not burlap.mysql.database_exists('myapp'):\n burlap.mysql.create_database('myapp', owner='dbuser')\n\n \"\"\"\n with settings(hide('running')):\n\n query(\"CREATE DATABASE %(name)s CHARACTER SET %(charset)s COLLATE %(collate)s;\" % {\n 'name': name,\n 'charset': charset,\n 'collate': collate\n }, **kwargs)\n\n if owner:\n query(\"GRANT ALL PRIVILEGES ON %(name)s.* TO '%(owner)s'@'%(owner_host)s' WITH GRANT OPTION;\" % {\n 'name': name,\n 'owner': owner,\n 'owner_host': owner_host\n }, **kwargs)\n\n puts(\"Created MySQL database '%s'.\" % name)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef conf_path(self):\n from burlap.system import distrib_id, distrib_release\n hostname = self.current_hostname\n if hostname not in self._conf_cache:\n self.env.conf_specifics[hostname] = self.env.conf_default\n d_id = distrib_id()\n d_release = distrib_release()\n for key in ((d_id, d_release), (d_id,)):\n if key in self.env.conf_specifics:\n self._conf_cache[hostname] = self.env.conf_specifics[key]\n return self._conf_cache[hostname]", "response": "Retrieves the path to the MySQL configuration file."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef prep_root_password(self, password=None, **kwargs):\n r = self.database_renderer(**kwargs)\n r.env.root_password = password or r.genv.get('db_root_password')\n r.sudo(\"DEBIAN_FRONTEND=noninteractive dpkg --configure -a\")\n r.sudo(\"debconf-set-selections <<< 'mysql-server mysql-server/root_password password {root_password}'\")\n r.sudo(\"debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password {root_password}'\")", "response": "Set the root password prompt entries into the debconf cache."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef exists(self, **kwargs):\n name = kwargs.pop('name', 'default')\n site = kwargs.pop('site', None)\n r = self.database_renderer(name=name, site=site)\n ret = r.run('mysql -h {db_host} -u {db_root_username} '\\\n '-p\"{db_root_password}\" -N -B -e \"SELECT IF(\\'{db_name}\\''\\\n ' IN(SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA), '\\\n '\\'exists\\', \\'notexists\\') AS found;\"')\n if ret is not None:\n ret = 'notexists' not in (ret or 'notexists')\n if ret is not None:\n msg = '%s database on site %s %s exist.' \\\n % (name.title(), env.SITE, 'DOES' if ret else 'DOES NOT')\n if ret:\n print(green(msg))\n else:\n print(red(msg))\n return ret", "response": "Returns true if a database with the given name exists. False otherwise."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef load(self, dump_fn='', prep_only=0, force_upload=0, from_local=0, name=None, site=None, dest_dir=None, force_host=None):\n\n r = self.database_renderer(name=name, site=site)\n r.pc('Loading database snapshot.')\n\n # Render the snapshot filename.\n r.env.dump_fn = self.get_default_db_fn(fn_template=dump_fn, dest_dir=dest_dir).strip()\n\n from_local = int(from_local)\n\n prep_only = int(prep_only)\n\n missing_local_dump_error = r.format(\"Database dump file {dump_fn} does not exist.\")\n\n # Copy snapshot file to target.\n if self.is_local:\n r.env.remote_dump_fn = dump_fn\n else:\n r.env.remote_dump_fn = '/tmp/' + os.path.split(r.env.dump_fn)[-1]\n\n if not prep_only and not self.is_local:\n #if int(force_upload) or (not self.is_local and not r.file_exists(r.env.remote_dump_fn)):\n if not self.dryrun:\n assert os.path.isfile(r.env.dump_fn), missing_local_dump_error\n #if self.verbose:\n #print('Uploading MySQL database snapshot...')\n #r.put(\n #local_path=r.env.dump_fn,\n #remote_path=r.env.remote_dump_fn)\n self.upload_snapshot(name=name, site=site)\n\n if self.is_local and not prep_only and not self.dryrun:\n assert os.path.isfile(r.env.dump_fn), missing_local_dump_error\n\n if force_host:\n r.env.db_host = force_host\n\n # Drop the database if it's there.\n r.run(\"mysql -v -h {db_host} -u {db_root_username} -p'{db_root_password}' --execute='DROP DATABASE IF EXISTS {db_name}'\")\n\n # Now, create the database.\n r.run(\"mysqladmin -h {db_host} -u {db_root_username} -p'{db_root_password}' create {db_name}\")\n\n # Create user\n with settings(warn_only=True):\n r.run(\"mysql -v -h {db_host} -u {db_root_username} -p'{db_root_password}' --execute=\\\"DROP USER '{db_user}'@'%%';\"\n \"FLUSH PRIVILEGES;\\\"\")\n with settings(warn_only=True):\n r.run(\"mysql -v -h {db_host} -u {db_root_username} -p'{db_root_password}' --execute=\\\"CREATE USER '{db_user}'@'%%' IDENTIFIED BY '{db_password}'; \"\n \"GRANT ALL PRIVILEGES ON *.* TO '{db_user}'@'%%' WITH GRANT OPTION; \"\n \"FLUSH PRIVILEGES;\\\"\")\n self.set_collation(name=name, site=site)\n\n self.set_max_packet_size(name=name, site=site)\n\n # Run any server-specific commands (e.g. to setup permissions) before\n # we load the data.\n for command in r.env.preload_commands:\n r.run(command)\n\n # Restore the database content from the dump file.\n if not prep_only:\n r.run(r.env.load_command)\n\n self.set_collation(name=name, site=site)", "response": "Load a database snapshot into the target database server."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef shell(self, name='default', site=None, use_root=0, **kwargs):\n r = self.database_renderer(name=name, site=site)\n\n if int(use_root):\n kwargs = dict(\n db_user=r.env.db_root_username,\n db_password=r.env.db_root_password,\n db_host=r.env.db_host,\n db_name=r.env.db_name,\n )\n r.env.update(kwargs)\n\n if not name:\n r.env.db_name = ''\n\n r.run('/bin/bash -i -c \"mysql -u {db_user} -p\\'{db_password}\\' -h {db_host} {db_name}\"')", "response": "Opens a SQL shell to the given database."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef tic_single_object_crossmatch(ra, dec, radius):\n '''This does a cross-match against the TIC catalog on MAST.\n\n Speed tests: about 10 crossmatches per second. (-> 3 hours for 10^5 objects\n to crossmatch).\n\n Parameters\n ----------\n\n ra,dec : np.array\n The coordinates to cross match against, all in decimal degrees.\n\n radius : float\n The cross-match radius to use, in decimal degrees.\n\n Returns\n -------\n\n dict\n Returns the match results JSON from MAST loaded into a dict.\n\n '''\n for val in ra,dec,radius:\n if not isinstance(val, float):\n raise AssertionError('plz input ra,dec,radius in decimal degrees')\n\n # This is a json object\n crossmatchInput = {\"fields\":[{\"name\":\"ra\",\"type\":\"float\"},\n {\"name\":\"dec\",\"type\":\"float\"}],\n \"data\":[{\"ra\":ra,\"dec\":dec}]}\n\n request = {\"service\":\"Mast.Tic.Crossmatch\",\n \"data\":crossmatchInput,\n \"params\":{\n \"raColumn\":\"ra\",\n \"decColumn\":\"dec\",\n \"radius\":radius\n },\n \"format\":\"json\",\n 'removecache':True}\n\n headers,out_string = _mast_query(request)\n\n out_data = json.loads(out_string)\n\n return out_data", "response": "This function performs a cross - match against the TIC catalog on MAST."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef normalized_flux_to_mag(lcdict,\n columns=('sap.sap_flux',\n 'sap.sap_flux_err',\n 'sap.sap_bkg',\n 'sap.sap_bkg_err',\n 'pdc.pdcsap_flux',\n 'pdc.pdcsap_flux_err')):\n '''This converts the normalized fluxes in the TESS lcdicts to TESS mags.\n\n Uses the object's TESS mag stored in lcdict['objectinfo']['tessmag']::\n\n mag - object_tess_mag = -2.5 log (flux/median_flux)\n\n Parameters\n ----------\n\n lcdict : lcdict\n An `lcdict` produced by `read_tess_fitslc` or\n `consolidate_tess_fitslc`. This must have normalized fluxes in its\n measurement columns (use the `normalize` kwarg for these functions).\n\n columns : sequence of str\n The column keys of the normalized flux and background measurements in\n the `lcdict` to operate on and convert to magnitudes in TESS band (T).\n\n Returns\n -------\n\n lcdict\n The returned `lcdict` will contain extra columns corresponding to\n magnitudes for each input normalized flux/background column.\n\n '''\n\n tess_mag = lcdict['objectinfo']['tessmag']\n\n for key in columns:\n\n k1, k2 = key.split('.')\n\n if 'err' not in k2:\n\n lcdict[k1][k2.replace('flux','mag')] = (\n tess_mag - 2.5*np.log10(lcdict[k1][k2])\n )\n\n else:\n\n lcdict[k1][k2.replace('flux','mag')] = (\n - 2.5*np.log10(1.0 - lcdict[k1][k2])\n )\n\n return lcdict", "response": "This function converts the normalized fluxes in the TESS lcdict to TESS mags."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_time_flux_errs_from_Ames_lightcurve(infile,\n lctype,\n cadence_min=2):\n '''Reads TESS Ames-format FITS light curve files.\n\n MIT TOI alerts include Ames lightcurve files. This function gets the finite,\n nonzero times, fluxes, and errors with QUALITY == 0.\n\n NOTE: the PDCSAP lightcurve typically still need \"pre-whitening\" after this\n step.\n\n .. deprecated:: 0.3.20\n This function will be removed in astrobase v0.4.2. Use the\n `read_tess_fitslc` and `consolidate_tess_fitslc` functions instead.\n\n Parameters\n ----------\n\n infile : str\n The path to `*.fits.gz` TOI alert file, from Ames pipeline.\n\n lctype : {'PDCSAP','SAP'}\n The type of light curve to extract from the FITS LC file.\n\n cadence_min : int\n The expected frame cadence in units of minutes. Raises ValueError if you\n use the wrong cadence.\n\n Returns\n -------\n\n tuple\n The tuple returned is of the form:\n\n (times, normalized (to median) fluxes, flux errors)\n\n '''\n\n warnings.warn(\n \"Use the astrotess.read_tess_fitslc and \"\n \"astrotess.consolidate_tess_fitslc functions instead of this function. \"\n \"This function will be removed in astrobase v0.4.2.\",\n FutureWarning\n )\n\n if lctype not in ('PDCSAP','SAP'):\n raise ValueError('unknown light curve type requested: %s' % lctype)\n\n hdulist = pyfits.open(infile)\n\n main_hdr = hdulist[0].header\n lc_hdr = hdulist[1].header\n lc = hdulist[1].data\n\n if (('Ames' not in main_hdr['ORIGIN']) or\n ('LIGHTCURVE' not in lc_hdr['EXTNAME'])):\n raise ValueError(\n 'could not understand input LC format. '\n 'Is it a TESS TOI LC file?'\n )\n\n time = lc['TIME']\n flux = lc['{:s}_FLUX'.format(lctype)]\n err_flux = lc['{:s}_FLUX_ERR'.format(lctype)]\n\n # REMOVE POINTS FLAGGED WITH:\n # attitude tweaks, safe mode, coarse/earth pointing, argabrithening events,\n # reaction wheel desaturation events, cosmic rays in optimal aperture\n # pixels, manual excludes, discontinuities, stray light from Earth or Moon\n # in camera FoV.\n # (Note: it's not clear to me what a lot of these mean. Also most of these\n # columns are probably not correctly propagated right now.)\n sel = (lc['QUALITY'] == 0)\n sel &= np.isfinite(time)\n sel &= np.isfinite(flux)\n sel &= np.isfinite(err_flux)\n sel &= ~np.isnan(time)\n sel &= ~np.isnan(flux)\n sel &= ~np.isnan(err_flux)\n sel &= (time != 0)\n sel &= (flux != 0)\n sel &= (err_flux != 0)\n\n time = time[sel]\n flux = flux[sel]\n err_flux = err_flux[sel]\n\n # ensure desired cadence\n lc_cadence_diff = np.abs(np.nanmedian(np.diff(time))*24*60 - cadence_min)\n\n if lc_cadence_diff > 1.0e-2:\n raise ValueError(\n 'the light curve is not at the required cadence specified: %.2f' %\n cadence_min\n )\n\n fluxmedian = np.nanmedian(flux)\n flux /= fluxmedian\n err_flux /= fluxmedian\n\n return time, flux, err_flux", "response": "Reads TESS Ames - format FITS light curve files and returns time flux errors."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef filter_tess_lcdict(lcdict,\n filterqualityflags=True,\n nanfilter='sap,pdc,time',\n timestoignore=None,\n quiet=False):\n '''This filters the provided TESS `lcdict`, removing nans and bad\n observations.\n\n By default, this function removes points in the TESS LC that have ANY\n quality flags set.\n\n Parameters\n ----------\n\n lcdict : lcdict\n An `lcdict` produced by `consolidate_tess_fitslc` or\n `read_tess_fitslc`.\n\n filterflags : bool\n If True, will remove any measurements that have non-zero quality flags\n present. This usually indicates an issue with the instrument or\n spacecraft.\n\n nanfilter : {'sap','pdc','sap,pdc'}\n Indicates the flux measurement type(s) to apply the filtering to.\n\n timestoignore : list of tuples or None\n This is of the form::\n\n [(time1_start, time1_end), (time2_start, time2_end), ...]\n\n and indicates the start and end times to mask out of the final\n lcdict. Use this to remove anything that wasn't caught by the quality\n flags.\n\n Returns\n -------\n\n lcdict\n Returns an `lcdict` (this is useable by most astrobase functions for LC\n processing). The `lcdict` is filtered IN PLACE!\n\n '''\n\n cols = lcdict['columns']\n\n # filter all bad LC points as noted by quality flags\n if filterqualityflags:\n\n nbefore = lcdict['time'].size\n filterind = lcdict['quality'] == 0\n\n for col in cols:\n if '.' in col:\n key, subkey = col.split('.')\n lcdict[key][subkey] = lcdict[key][subkey][filterind]\n else:\n lcdict[col] = lcdict[col][filterind]\n\n nafter = lcdict['time'].size\n if not quiet:\n LOGINFO('applied quality flag filter, '\n 'ndet before = %s, ndet after = %s'\n % (nbefore, nafter))\n\n\n if nanfilter and nanfilter == 'sap,pdc,time':\n notnanind = (\n np.isfinite(lcdict['sap']['sap_flux']) &\n np.isfinite(lcdict['sap']['sap_flux_err']) &\n np.isfinite(lcdict['pdc']['pdcsap_flux']) &\n np.isfinite(lcdict['pdc']['pdcsap_flux_err']) &\n np.isfinite(lcdict['time'])\n )\n elif nanfilter and nanfilter == 'sap,time':\n notnanind = (\n np.isfinite(lcdict['sap']['sap_flux']) &\n np.isfinite(lcdict['sap']['sap_flux_err']) &\n np.isfinite(lcdict['time'])\n )\n elif nanfilter and nanfilter == 'pdc,time':\n notnanind = (\n np.isfinite(lcdict['pdc']['pdcsap_flux']) &\n np.isfinite(lcdict['pdc']['pdcsap_flux_err']) &\n np.isfinite(lcdict['time'])\n )\n elif nanfilter is None:\n pass\n else:\n raise NotImplementedError\n\n # remove nans from all columns\n if nanfilter:\n\n nbefore = lcdict['time'].size\n for col in cols:\n if '.' in col:\n key, subkey = col.split('.')\n lcdict[key][subkey] = lcdict[key][subkey][notnanind]\n else:\n lcdict[col] = lcdict[col][notnanind]\n\n nafter = lcdict['time'].size\n\n if not quiet:\n LOGINFO('removed nans, ndet before = %s, ndet after = %s'\n % (nbefore, nafter))\n\n\n # exclude all times in timestoignore\n if (timestoignore and\n isinstance(timestoignore, list) and\n len(timestoignore) > 0):\n\n exclind = np.full_like(lcdict['time'],True).astype(bool)\n nbefore = exclind.size\n\n # get all the masks\n for ignoretime in timestoignore:\n time0, time1 = ignoretime[0], ignoretime[1]\n thismask = ~((lcdict['time'] >= time0) & (lcdict['time'] <= time1))\n exclind = exclind & thismask\n\n # apply the masks\n for col in cols:\n if '.' in col:\n key, subkey = col.split('.')\n lcdict[key][subkey] = lcdict[key][subkey][exclind]\n else:\n lcdict[col] = lcdict[col][exclind]\n\n nafter = lcdict['time'].size\n if not quiet:\n LOGINFO('removed timestoignore, ndet before = %s, ndet after = %s'\n % (nbefore, nafter))\n\n return lcdict", "response": "This function filters the TESS LCdict by removing bad LC points that are not caught by the quality flags set."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _pkl_finder_objectinfo(\n objectinfo,\n varinfo,\n findercmap,\n finderconvolve,\n sigclip,\n normto,\n normmingap,\n deredden_object=True,\n custom_bandpasses=None,\n lclistpkl=None,\n nbrradiusarcsec=30.0,\n maxnumneighbors=5,\n plotdpi=100,\n findercachedir='~/.astrobase/stamp-cache',\n verbose=True,\n gaia_submit_timeout=10.0,\n gaia_submit_tries=3,\n gaia_max_timeout=180.0,\n gaia_mirror=None,\n fast_mode=False,\n complete_query_later=True\n):\n '''This returns the finder chart and object information as a dict.\n\n Parameters\n ----------\n\n objectinfo : dict or None\n If provided, this is a dict containing information on the object whose\n light curve is being processed. This function will then be able to look\n up and download a finder chart for this object and write that to the\n output checkplotdict. External services such as GAIA, SIMBAD, TIC@MAST,\n etc. will also be used to look up this object by its coordinates, and\n will add in information available from those services.\n\n The `objectinfo` dict must be of the form and contain at least the keys\n described below::\n\n {'objectid': the name of the object,\n 'ra': the right ascension of the object in decimal degrees,\n 'decl': the declination of the object in decimal degrees,\n 'ndet': the number of observations of this object}\n\n You can also provide magnitudes and proper motions of the object using\n the following keys and the appropriate values in the `objectinfo`\n dict. These will be used to calculate colors, total and reduced proper\n motion, etc. and display these in the output checkplot PNG::\n\n 'pmra' -> the proper motion in mas/yr in right ascension,\n 'pmdecl' -> the proper motion in mas/yr in declination,\n 'umag' -> U mag\t\t -> colors: U-B, U-V, U-g\n 'bmag' -> B mag\t\t -> colors: U-B, B-V\n 'vmag' -> V mag\t\t -> colors: U-V, B-V, V-R, V-I, V-K\n 'rmag' -> R mag\t\t -> colors: V-R, R-I\n 'imag' -> I mag\t\t -> colors: g-I, V-I, R-I, B-I\n 'jmag' -> 2MASS J mag\t -> colors: J-H, J-K, g-J, i-J\n 'hmag' -> 2MASS H mag\t -> colors: J-H, H-K\n 'kmag' -> 2MASS Ks mag\t -> colors: g-Ks, H-Ks, J-Ks, V-Ks\n 'sdssu' -> SDSS u mag\t -> colors: u-g, u-V\n 'sdssg' -> SDSS g mag\t -> colors: g-r, g-i, g-K, u-g, U-g, g-J\n 'sdssr' -> SDSS r mag\t -> colors: r-i, g-r\n 'sdssi' -> SDSS i mag\t -> colors: r-i, i-z, g-i, i-J, i-W1\n 'sdssz' -> SDSS z mag\t -> colors: i-z, z-W2, g-z\n 'ujmag' -> UKIRT J mag\t -> colors: J-H, H-K, J-K, g-J, i-J\n 'uhmag' -> UKIRT H mag\t -> colors: J-H, H-K\n 'ukmag' -> UKIRT K mag\t -> colors: g-K, H-K, J-K, V-K\n 'irac1' -> Spitzer IRAC1 mag -> colors: i-I1, I1-I2\n 'irac2' -> Spitzer IRAC2 mag -> colors: I1-I2, I2-I3\n 'irac3' -> Spitzer IRAC3 mag -> colors: I2-I3\n 'irac4' -> Spitzer IRAC4 mag -> colors: I3-I4\n 'wise1' -> WISE W1 mag\t -> colors: i-W1, W1-W2\n 'wise2' -> WISE W2 mag\t -> colors: W1-W2, W2-W3\n 'wise3' -> WISE W3 mag\t -> colors: W2-W3\n 'wise4' -> WISE W4 mag\t -> colors: W3-W4\n\n If you have magnitude measurements in other bands, use the\n `custom_bandpasses` kwarg to pass these in.\n\n If this is None, no object information will be incorporated into the\n checkplot (kind of making it effectively useless for anything other than\n glancing at the phased light curves at various 'best' periods from the\n period-finder results).\n\n varinfo : dict or None\n If this is None, a blank dict of the form below will be added to the\n checkplotdict::\n\n {'objectisvar': None -> variability flag (None indicates unset),\n 'vartags': CSV str containing variability type tags from review,\n 'varisperiodic': None -> periodic variability flag (None -> unset),\n 'varperiod': the period associated with the periodic variability,\n 'varepoch': the epoch associated with the periodic variability}\n\n If you provide a dict matching this format in this kwarg, this will be\n passed unchanged to the output checkplotdict produced.\n\n findercmap : str or matplotlib.cm.ColorMap object\n The Colormap object to use for the finder chart image.\n\n finderconvolve : astropy.convolution.Kernel object or None\n If not None, the Kernel object to use for convolving the finder image.\n\n sigclip : float or int or sequence of two floats/ints or None\n If a single float or int, a symmetric sigma-clip will be performed using\n the number provided as the sigma-multiplier to cut out from the input\n time-series.\n\n If a list of two ints/floats is provided, the function will perform an\n 'asymmetric' sigma-clip. The first element in this list is the sigma\n value to use for fainter flux/mag values; the second element in this\n list is the sigma value to use for brighter flux/mag values. For\n example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma\n dimmings and greater than 3-sigma brightenings. Here the meaning of\n \"dimming\" and \"brightening\" is set by *physics* (not the magnitude\n system), which is why the `magsarefluxes` kwarg must be correctly set.\n\n If `sigclip` is None, no sigma-clipping will be performed, and the\n time-series (with non-finite elems removed) will be passed through to\n the output.\n\n normto : {'globalmedian', 'zero'} or a float\n This is specified as below::\n\n 'globalmedian' -> norms each mag to global median of the LC column\n 'zero' -> norms each mag to zero\n a float -> norms each mag to this specified float value.\n\n normmingap : float\n This defines how much the difference between consecutive measurements is\n allowed to be to consider them as parts of different timegroups. By\n default it is set to 4.0 days.\n\n deredden_object : bool\n If this is True, will use the 2MASS DUST service to get extinction\n coefficients in various bands, and then try to deredden the magnitudes\n and colors of the object already present in the checkplot's objectinfo\n dict.\n\n custom_bandpasses : dict\n This is a dict used to provide custom bandpass definitions for any\n magnitude measurements in the objectinfo dict that are not automatically\n recognized by :py:func:`astrobase.varclass.starfeatures.color_features`.\n\n lclistpkl : dict or str\n If this is provided, must be a dict resulting from reading a catalog\n produced by the `lcproc.catalogs.make_lclist` function or a str path\n pointing to the pickle file produced by that function. This catalog is\n used to find neighbors of the current object in the current light curve\n collection. Looking at neighbors of the object within the radius\n specified by `nbrradiusarcsec` is useful for light curves produced by\n instruments that have a large pixel scale, so are susceptible to\n blending of variability and potential confusion of neighbor variability\n with that of the actual object being looked at. If this is None, no\n neighbor lookups will be performed.\n\n nbrradiusarcsec : float\n The radius in arcseconds to use for a search conducted around the\n coordinates of this object to look for any potential confusion and\n blending of variability amplitude caused by their proximity.\n\n maxnumneighbors : int\n The maximum number of neighbors that will have their light curves and\n magnitudes noted in this checkplot as potential blends with the target\n object.\n\n plotdpi : int\n The resolution in DPI of the plots to generate in this function\n (e.g. the finder chart, etc.)\n\n findercachedir : str\n The path to the astrobase cache directory for finder chart downloads\n from the NASA SkyView service.\n\n verbose : bool\n If True, will indicate progress and warn about potential problems.\n\n gaia_submit_timeout : float\n Sets the timeout in seconds to use when submitting a request to look up\n the object's information to the GAIA service. Note that if `fast_mode`\n is set, this is ignored.\n\n gaia_submit_tries : int\n Sets the maximum number of times the GAIA services will be contacted to\n obtain this object's information. If `fast_mode` is set, this is\n ignored, and the services will be contacted only once (meaning that a\n failure to respond will be silently ignored and no GAIA data will be\n added to the checkplot's objectinfo dict).\n\n gaia_max_timeout : float\n Sets the timeout in seconds to use when waiting for the GAIA service to\n respond to our request for the object's information. Note that if\n `fast_mode` is set, this is ignored.\n\n gaia_mirror : str\n This sets the GAIA mirror to use. This is a key in the\n `services.gaia.GAIA_URLS` dict which defines the URLs to hit for each\n mirror.\n\n fast_mode : bool or float\n This runs the external catalog operations in a \"fast\" mode, with short\n timeouts and not trying to hit external catalogs that take a long time\n to respond.\n\n If this is set to True, the default settings for the external requests\n will then become::\n\n skyview_lookup = False\n skyview_timeout = 10.0\n skyview_retry_failed = False\n dust_timeout = 10.0\n gaia_submit_timeout = 7.0\n gaia_max_timeout = 10.0\n gaia_submit_tries = 2\n complete_query_later = False\n search_simbad = False\n\n If this is a float, will run in \"fast\" mode with the provided timeout\n value in seconds and the following settings::\n\n skyview_lookup = True\n skyview_timeout = fast_mode\n skyview_retry_failed = False\n dust_timeout = fast_mode\n gaia_submit_timeout = 0.66*fast_mode\n gaia_max_timeout = fast_mode\n gaia_submit_tries = 2\n complete_query_later = False\n search_simbad = False\n\n complete_query_later : bool\n If this is True, saves the state of GAIA queries that are not yet\n complete when `gaia_max_timeout` is reached while waiting for the GAIA\n service to respond to our request. A later call for GAIA info on the\n same object will attempt to pick up the results from the existing query\n if it's completed. If `fast_mode` is True, this is ignored.\n\n Returns\n -------\n\n dict\n A checkplotdict is returned containing the objectinfo and varinfo dicts,\n ready to use with the functions below to add in light curve plots,\n phased LC plots, xmatch info, etc.\n\n '''\n\n # optional mode to hit external services and fail fast if they timeout\n if fast_mode is True:\n skyview_lookup = False\n skyview_timeout = 10.0\n skyview_retry_failed = False\n dust_timeout = 10.0\n gaia_submit_timeout = 7.0\n gaia_max_timeout = 10.0\n gaia_submit_tries = 2\n complete_query_later = False\n search_simbad = False\n\n elif isinstance(fast_mode, (int, float)) and fast_mode > 0.0:\n skyview_lookup = True\n skyview_timeout = fast_mode\n skyview_retry_failed = False\n dust_timeout = fast_mode\n gaia_submit_timeout = 0.66*fast_mode\n gaia_max_timeout = fast_mode\n gaia_submit_tries = 2\n complete_query_later = False\n search_simbad = False\n\n else:\n skyview_lookup = True\n skyview_timeout = 10.0\n skyview_retry_failed = True\n dust_timeout = 10.0\n search_simbad = True\n\n\n if (isinstance(objectinfo, dict) and\n ('objectid' in objectinfo or 'hatid' in objectinfo) and\n 'ra' in objectinfo and 'decl' in objectinfo and\n objectinfo['ra'] and objectinfo['decl']):\n\n if 'objectid' not in objectinfo:\n objectid = objectinfo['hatid']\n else:\n objectid = objectinfo['objectid']\n\n if verbose and skyview_lookup:\n LOGINFO('adding in object information and '\n 'finder chart for %s at RA: %.3f, DEC: %.3f' %\n (objectid, objectinfo['ra'], objectinfo['decl']))\n elif verbose and not skyview_lookup:\n LOGINFO('adding in object information '\n 'for %s at RA: %.3f, DEC: %.3f. '\n 'skipping finder chart because skyview_lookup = False' %\n (objectid, objectinfo['ra'], objectinfo['decl']))\n\n # get the finder chart\n try:\n\n if skyview_lookup:\n\n try:\n\n # generate the finder chart\n finder, finderheader = skyview_stamp(\n objectinfo['ra'],\n objectinfo['decl'],\n convolvewith=finderconvolve,\n verbose=verbose,\n flip=False,\n cachedir=findercachedir,\n timeout=skyview_timeout,\n retry_failed=skyview_retry_failed,\n )\n\n except OSError as e:\n\n if not fast_mode:\n\n LOGERROR(\n 'finder image appears to be corrupt, retrying...'\n )\n\n # generate the finder chart\n finder, finderheader = skyview_stamp(\n objectinfo['ra'],\n objectinfo['decl'],\n convolvewith=finderconvolve,\n verbose=verbose,\n flip=False,\n cachedir=findercachedir,\n forcefetch=True,\n timeout=skyview_timeout,\n retry_failed=False # do not start an infinite loop\n )\n\n\n finderfig = plt.figure(figsize=(3,3),dpi=plotdpi)\n\n # initialize the finder WCS\n finderwcs = WCS(finderheader)\n\n # use the WCS transform for the plot\n ax = finderfig.add_subplot(111, frameon=False)\n ax.imshow(finder, cmap=findercmap, origin='lower')\n\n else:\n finder, finderheader, finderfig, finderwcs = (\n None, None, None, None\n )\n\n # skip down to after nbr stuff for the rest of the finderchart...\n\n # search around the target's location and get its neighbors if\n # lclistpkl is provided and it exists\n if (lclistpkl is not None and\n nbrradiusarcsec is not None and\n nbrradiusarcsec > 0.0):\n\n # if lclistpkl is a string, open it as a pickle\n if isinstance(lclistpkl, str) and os.path.exists(lclistpkl):\n\n if lclistpkl.endswith('.gz'):\n infd = gzip.open(lclistpkl,'rb')\n else:\n infd = open(lclistpkl,'rb')\n\n lclist = pickle.load(infd)\n infd.close()\n\n # otherwise, if it's a dict, we get it directly\n elif isinstance(lclistpkl, dict):\n\n lclist = lclistpkl\n\n # finally, if it's nothing we recognize, ignore it\n else:\n\n LOGERROR('could not understand lclistpkl kwarg, '\n 'not getting neighbor info')\n\n lclist = dict()\n\n # check if we have a KDTree to use\n # if we don't, skip neighbor stuff\n if 'kdtree' not in lclist:\n\n LOGERROR('neighbors within %.1f arcsec for %s could '\n 'not be found, no kdtree in lclistpkl: %s'\n % (objectid, lclistpkl))\n neighbors = None\n kdt = None\n\n # otherwise, do neighbor processing\n else:\n\n kdt = lclist['kdtree']\n\n obj_cosdecl = np.cos(np.radians(objectinfo['decl']))\n obj_sindecl = np.sin(np.radians(objectinfo['decl']))\n obj_cosra = np.cos(np.radians(objectinfo['ra']))\n obj_sinra = np.sin(np.radians(objectinfo['ra']))\n\n obj_xyz = np.column_stack((obj_cosra*obj_cosdecl,\n obj_sinra*obj_cosdecl,\n obj_sindecl))\n match_xyzdist = (\n 2.0 * np.sin(np.radians(nbrradiusarcsec/3600.0)/2.0)\n )\n matchdists, matchinds = kdt.query(\n obj_xyz,\n k=maxnumneighbors+1, # get maxnumneighbors + tgt\n distance_upper_bound=match_xyzdist\n )\n\n # sort by matchdist\n mdsorted = np.argsort(matchdists[0])\n matchdists = matchdists[0][mdsorted]\n matchinds = matchinds[0][mdsorted]\n\n # luckily, the indices to the kdtree are the same as that\n # for the objects (I think)\n neighbors = []\n\n nbrind = 0\n\n for md, mi in zip(matchdists, matchinds):\n\n if np.isfinite(md) and md > 0.0:\n\n if skyview_lookup:\n\n # generate the xy for the finder we'll use a\n # HTML5 canvas and these pixcoords to highlight\n # each neighbor when we mouse over its row in\n # the neighbors tab\n\n # we use coord origin = 0 here and not the usual\n # 1 because we're annotating a numpy array\n pixcoords = finderwcs.all_world2pix(\n np.array([[lclist['objects']['ra'][mi],\n lclist['objects']['decl'][mi]]]),\n 0\n )\n\n # each elem is {'objectid',\n # 'ra','decl',\n # 'xpix','ypix',\n # 'dist','lcfpath'}\n thisnbr = {\n 'objectid':(\n lclist['objects']['objectid'][mi]\n ),\n 'ra':lclist['objects']['ra'][mi],\n 'decl':lclist['objects']['decl'][mi],\n 'xpix':pixcoords[0,0],\n 'ypix':300.0 - pixcoords[0,1],\n 'dist':_xyzdist_to_distarcsec(md),\n 'lcfpath': lclist['objects']['lcfname'][mi]\n }\n neighbors.append(thisnbr)\n nbrind = nbrind+1\n\n # put in a nice marker for this neighbor into\n # the overall finder chart\n annotatex = pixcoords[0,0]\n annotatey = pixcoords[0,1]\n\n if ((300.0 - annotatex) > 50.0):\n offx = annotatex + 30.0\n xha = 'center'\n else:\n offx = annotatex - 30.0\n xha = 'center'\n if ((300.0 - annotatey) > 50.0):\n offy = annotatey - 30.0\n yha = 'center'\n else:\n offy = annotatey + 30.0\n yha = 'center'\n\n ax.annotate('N%s' % nbrind,\n (annotatex, annotatey),\n xytext=(offx, offy),\n arrowprops={'facecolor':'blue',\n 'edgecolor':'blue',\n 'width':1.0,\n 'headwidth':1.0,\n 'headlength':0.1,\n 'shrink':0.0},\n color='blue',\n horizontalalignment=xha,\n verticalalignment=yha)\n\n else:\n\n thisnbr = {\n 'objectid':(\n lclist['objects']['objectid'][mi]\n ),\n 'ra':lclist['objects']['ra'][mi],\n 'decl':lclist['objects']['decl'][mi],\n 'xpix':0.0,\n 'ypix':0.0,\n 'dist':_xyzdist_to_distarcsec(md),\n 'lcfpath': lclist['objects']['lcfname'][mi]\n }\n neighbors.append(thisnbr)\n nbrind = nbrind+1\n\n # if there are no neighbors, set the 'neighbors' key to None\n else:\n\n neighbors = None\n kdt = None\n\n if skyview_lookup:\n\n #\n # finish up the finder chart after neighbors are processed\n #\n ax.set_xticks([])\n ax.set_yticks([])\n\n # add a reticle pointing to the object's coordinates\n # we use coord origin = 0 here and not the usual\n # 1 because we're annotating a numpy array\n object_pixcoords = finderwcs.all_world2pix(\n [[objectinfo['ra'],\n objectinfo['decl']]],\n 0\n )\n\n ax.axvline(\n # x=150.0,\n x=object_pixcoords[0,0],\n ymin=0.375,\n ymax=0.45,\n linewidth=1,\n color='b'\n )\n ax.axhline(\n # y=150.0,\n y=object_pixcoords[0,1],\n xmin=0.375,\n xmax=0.45,\n linewidth=1,\n color='b'\n )\n ax.set_frame_on(False)\n\n # this is the output instance\n finderpng = StrIO()\n finderfig.savefig(finderpng,\n bbox_inches='tight',\n pad_inches=0.0, format='png')\n plt.close()\n\n # encode the finderpng instance to base64\n finderpng.seek(0)\n finderb64 = base64.b64encode(finderpng.read())\n\n # close the stringio buffer\n finderpng.close()\n\n else:\n\n finderb64 = None\n\n except Exception as e:\n\n LOGEXCEPTION('could not fetch a DSS stamp for this '\n 'object %s using coords (%.3f,%.3f)' %\n (objectid, objectinfo['ra'], objectinfo['decl']))\n finderb64 = None\n neighbors = None\n kdt = None\n\n # if we don't have ra, dec info, then everything is none up to this point\n else:\n\n finderb64 = None\n neighbors = None\n kdt = None\n\n #\n # end of finder chart operations\n #\n\n # now that we have the finder chart, get the rest of the object\n # information\n\n # get the rest of the features, these don't necessarily rely on ra, dec and\n # should degrade gracefully if these aren't provided\n if isinstance(objectinfo, dict):\n\n if 'objectid' not in objectinfo and 'hatid' in objectinfo:\n objectid = objectinfo['hatid']\n objectinfo['objectid'] = objectid\n elif 'objectid' in objectinfo:\n objectid = objectinfo['objectid']\n else:\n objectid = os.urandom(12).hex()[:7]\n objectinfo['objectid'] = objectid\n LOGWARNING('no objectid found in objectinfo dict, '\n 'making up a random one: %s')\n\n\n # get the neighbor features and GAIA info\n nbrfeat = neighbor_gaia_features(\n objectinfo,\n kdt,\n nbrradiusarcsec,\n verbose=False,\n gaia_submit_timeout=gaia_submit_timeout,\n gaia_submit_tries=gaia_submit_tries,\n gaia_max_timeout=gaia_max_timeout,\n gaia_mirror=gaia_mirror,\n complete_query_later=complete_query_later,\n search_simbad=search_simbad\n )\n objectinfo.update(nbrfeat)\n\n # see if the objectinfo dict has pmra/pmdecl entries. if it doesn't,\n # then we'll see if the nbrfeat dict has pmra/pmdecl from GAIA. we'll\n # set the appropriate provenance keys as well so we know where the PM\n # came from\n if ( ('pmra' not in objectinfo) or\n ( ('pmra' in objectinfo) and\n ( (objectinfo['pmra'] is None) or\n (not np.isfinite(objectinfo['pmra'])) ) ) ):\n\n if 'ok' in nbrfeat['gaia_status']:\n\n objectinfo['pmra'] = nbrfeat['gaia_pmras'][0]\n objectinfo['pmra_err'] = nbrfeat['gaia_pmra_errs'][0]\n objectinfo['pmra_source'] = 'gaia'\n\n if verbose:\n LOGWARNING('pmRA not found in provided objectinfo dict, '\n 'using value from GAIA')\n\n else:\n objectinfo['pmra_source'] = 'light curve'\n\n if ( ('pmdecl' not in objectinfo) or\n ( ('pmdecl' in objectinfo) and\n ( (objectinfo['pmdecl'] is None) or\n (not np.isfinite(objectinfo['pmdecl'])) ) ) ):\n\n if 'ok' in nbrfeat['gaia_status']:\n\n objectinfo['pmdecl'] = nbrfeat['gaia_pmdecls'][0]\n objectinfo['pmdecl_err'] = nbrfeat['gaia_pmdecl_errs'][0]\n objectinfo['pmdecl_source'] = 'gaia'\n\n if verbose:\n LOGWARNING('pmDEC not found in provided objectinfo dict, '\n 'using value from GAIA')\n\n else:\n objectinfo['pmdecl_source'] = 'light curve'\n\n #\n # update GAIA info so it's available at the first level\n #\n if 'ok' in objectinfo['gaia_status']:\n objectinfo['gaiaid'] = objectinfo['gaia_ids'][0]\n objectinfo['gaiamag'] = objectinfo['gaia_mags'][0]\n objectinfo['gaia_absmag'] = objectinfo['gaia_absolute_mags'][0]\n objectinfo['gaia_parallax'] = objectinfo['gaia_parallaxes'][0]\n objectinfo['gaia_parallax_err'] = (\n objectinfo['gaia_parallax_errs'][0]\n )\n objectinfo['gaia_pmra'] = objectinfo['gaia_pmras'][0]\n objectinfo['gaia_pmra_err'] = objectinfo['gaia_pmra_errs'][0]\n objectinfo['gaia_pmdecl'] = objectinfo['gaia_pmdecls'][0]\n objectinfo['gaia_pmdecl_err'] = objectinfo['gaia_pmdecl_errs'][0]\n\n else:\n objectinfo['gaiaid'] = None\n objectinfo['gaiamag'] = np.nan\n objectinfo['gaia_absmag'] = np.nan\n objectinfo['gaia_parallax'] = np.nan\n objectinfo['gaia_parallax_err'] = np.nan\n objectinfo['gaia_pmra'] = np.nan\n objectinfo['gaia_pmra_err'] = np.nan\n objectinfo['gaia_pmdecl'] = np.nan\n objectinfo['gaia_pmdecl_err'] = np.nan\n\n #\n # get the object's TIC information\n #\n if ('ra' in objectinfo and\n objectinfo['ra'] is not None and\n np.isfinite(objectinfo['ra']) and\n 'decl' in objectinfo and\n objectinfo['decl'] is not None and\n np.isfinite(objectinfo['decl'])):\n\n try:\n ticres = tic_conesearch(objectinfo['ra'],\n objectinfo['decl'],\n radius_arcmin=5.0/60.0,\n verbose=verbose,\n timeout=gaia_max_timeout,\n maxtries=gaia_submit_tries)\n\n if ticres is not None:\n\n with open(ticres['cachefname'],'r') as infd:\n ticinfo = json.load(infd)\n\n if ('data' in ticinfo and\n len(ticinfo['data']) > 0 and\n isinstance(ticinfo['data'][0], dict)):\n\n objectinfo['ticid'] = str(ticinfo['data'][0]['ID'])\n objectinfo['tessmag'] = ticinfo['data'][0]['Tmag']\n objectinfo['tic_version'] = (\n ticinfo['data'][0]['version']\n )\n objectinfo['tic_distarcsec'] = (\n ticinfo['data'][0]['dstArcSec']\n )\n objectinfo['tessmag_origin'] = (\n ticinfo['data'][0]['TESSflag']\n )\n\n objectinfo['tic_starprop_origin'] = (\n ticinfo['data'][0]['SPFlag']\n )\n objectinfo['tic_lumclass'] = (\n ticinfo['data'][0]['lumclass']\n )\n objectinfo['tic_teff'] = (\n ticinfo['data'][0]['Teff']\n )\n objectinfo['tic_teff_err'] = (\n ticinfo['data'][0]['e_Teff']\n )\n objectinfo['tic_logg'] = (\n ticinfo['data'][0]['logg']\n )\n objectinfo['tic_logg_err'] = (\n ticinfo['data'][0]['e_logg']\n )\n objectinfo['tic_mh'] = (\n ticinfo['data'][0]['MH']\n )\n objectinfo['tic_mh_err'] = (\n ticinfo['data'][0]['e_MH']\n )\n objectinfo['tic_radius'] = (\n ticinfo['data'][0]['rad']\n )\n objectinfo['tic_radius_err'] = (\n ticinfo['data'][0]['e_rad']\n )\n objectinfo['tic_mass'] = (\n ticinfo['data'][0]['mass']\n )\n objectinfo['tic_mass_err'] = (\n ticinfo['data'][0]['e_mass']\n )\n objectinfo['tic_density'] = (\n ticinfo['data'][0]['rho']\n )\n objectinfo['tic_density_err'] = (\n ticinfo['data'][0]['e_rho']\n )\n objectinfo['tic_luminosity'] = (\n ticinfo['data'][0]['lum']\n )\n objectinfo['tic_luminosity_err'] = (\n ticinfo['data'][0]['e_lum']\n )\n objectinfo['tic_distancepc'] = (\n ticinfo['data'][0]['d']\n )\n objectinfo['tic_distancepc_err'] = (\n ticinfo['data'][0]['e_d']\n )\n\n #\n # fill in any missing info using the TIC entry\n #\n if ('gaiaid' not in objectinfo or\n ('gaiaid' in objectinfo and\n (objectinfo['gaiaid'] is None))):\n objectinfo['gaiaid'] = ticinfo['data'][0]['GAIA']\n\n if ('gaiamag' not in objectinfo or\n ('gaiamag' in objectinfo and\n (objectinfo['gaiamag'] is None or\n not np.isfinite(objectinfo['gaiamag'])))):\n objectinfo['gaiamag'] = (\n ticinfo['data'][0]['GAIAmag']\n )\n objectinfo['gaiamag_err'] = (\n ticinfo['data'][0]['e_GAIAmag']\n )\n\n if ('gaia_parallax' not in objectinfo or\n ('gaia_parallax' in objectinfo and\n (objectinfo['gaia_parallax'] is None or\n not np.isfinite(objectinfo['gaia_parallax'])))):\n\n objectinfo['gaia_parallax'] = (\n ticinfo['data'][0]['plx']\n )\n objectinfo['gaia_parallax_err'] = (\n ticinfo['data'][0]['e_plx']\n )\n\n if (objectinfo['gaiamag'] is not None and\n np.isfinite(objectinfo['gaiamag']) and\n objectinfo['gaia_parallax'] is not None and\n np.isfinite(objectinfo['gaia_parallax'])):\n\n objectinfo['gaia_absmag'] = (\n magnitudes.absolute_gaia_magnitude(\n objectinfo['gaiamag'],\n objectinfo['gaia_parallax']\n )\n )\n\n if ('pmra' not in objectinfo or\n ('pmra' in objectinfo and\n (objectinfo['pmra'] is None or\n not np.isfinite(objectinfo['pmra'])))):\n objectinfo['pmra'] = ticinfo['data'][0]['pmRA']\n objectinfo['pmra_err'] = (\n ticinfo['data'][0]['e_pmRA']\n )\n objectinfo['pmra_source'] = 'TIC'\n\n if ('pmdecl' not in objectinfo or\n ('pmdecl' in objectinfo and\n (objectinfo['pmdecl'] is None or\n not np.isfinite(objectinfo['pmdecl'])))):\n objectinfo['pmdecl'] = ticinfo['data'][0]['pmDEC']\n objectinfo['pmdecl_err'] = (\n ticinfo['data'][0]['e_pmDEC']\n )\n objectinfo['pmdecl_source'] = 'TIC'\n\n if ('bmag' not in objectinfo or\n ('bmag' in objectinfo and\n (objectinfo['bmag'] is None or\n not np.isfinite(objectinfo['bmag'])))):\n objectinfo['bmag'] = ticinfo['data'][0]['Bmag']\n objectinfo['bmag_err'] = (\n ticinfo['data'][0]['e_Bmag']\n )\n\n if ('vmag' not in objectinfo or\n ('vmag' in objectinfo and\n (objectinfo['vmag'] is None or\n not np.isfinite(objectinfo['vmag'])))):\n objectinfo['vmag'] = ticinfo['data'][0]['Vmag']\n objectinfo['vmag_err'] = (\n ticinfo['data'][0]['e_Vmag']\n )\n\n if ('sdssu' not in objectinfo or\n ('sdssu' in objectinfo and\n (objectinfo['sdssu'] is None or\n not np.isfinite(objectinfo['sdssu'])))):\n objectinfo['sdssu'] = ticinfo['data'][0]['umag']\n objectinfo['sdssu_err'] = (\n ticinfo['data'][0]['e_umag']\n )\n\n if ('sdssg' not in objectinfo or\n ('sdssg' in objectinfo and\n (objectinfo['sdssg'] is None or\n not np.isfinite(objectinfo['sdssg'])))):\n objectinfo['sdssg'] = ticinfo['data'][0]['gmag']\n objectinfo['sdssg_err'] = (\n ticinfo['data'][0]['e_gmag']\n )\n\n if ('sdssr' not in objectinfo or\n ('sdssr' in objectinfo and\n (objectinfo['sdssr'] is None or\n not np.isfinite(objectinfo['sdssr'])))):\n objectinfo['sdssr'] = ticinfo['data'][0]['rmag']\n objectinfo['sdssr_err'] = (\n ticinfo['data'][0]['e_rmag']\n )\n\n if ('sdssi' not in objectinfo or\n ('sdssi' in objectinfo and\n (objectinfo['sdssi'] is None or\n not np.isfinite(objectinfo['sdssi'])))):\n objectinfo['sdssi'] = ticinfo['data'][0]['imag']\n objectinfo['sdssi_err'] = (\n ticinfo['data'][0]['e_imag']\n )\n\n if ('sdssz' not in objectinfo or\n ('sdssz' in objectinfo and\n (objectinfo['sdssz'] is None or\n not np.isfinite(objectinfo['sdssz'])))):\n objectinfo['sdssz'] = ticinfo['data'][0]['zmag']\n objectinfo['sdssz_err'] = (\n ticinfo['data'][0]['e_zmag']\n )\n\n if ('jmag' not in objectinfo or\n ('jmag' in objectinfo and\n (objectinfo['jmag'] is None or\n not np.isfinite(objectinfo['jmag'])))):\n objectinfo['jmag'] = ticinfo['data'][0]['Jmag']\n objectinfo['jmag_err'] = (\n ticinfo['data'][0]['e_Jmag']\n )\n\n if ('hmag' not in objectinfo or\n ('hmag' in objectinfo and\n (objectinfo['hmag'] is None or\n not np.isfinite(objectinfo['hmag'])))):\n objectinfo['hmag'] = ticinfo['data'][0]['Hmag']\n objectinfo['hmag_err'] = (\n ticinfo['data'][0]['e_Hmag']\n )\n\n if ('kmag' not in objectinfo or\n ('kmag' in objectinfo and\n (objectinfo['kmag'] is None or\n not np.isfinite(objectinfo['kmag'])))):\n objectinfo['kmag'] = ticinfo['data'][0]['Kmag']\n objectinfo['kmag_err'] = (\n ticinfo['data'][0]['e_Kmag']\n )\n\n if ('wise1' not in objectinfo or\n ('wise1' in objectinfo and\n (objectinfo['wise1'] is None or\n not np.isfinite(objectinfo['wise1'])))):\n objectinfo['wise1'] = ticinfo['data'][0]['w1mag']\n objectinfo['wise1_err'] = (\n ticinfo['data'][0]['e_w1mag']\n )\n\n if ('wise2' not in objectinfo or\n ('wise2' in objectinfo and\n (objectinfo['wise2'] is None or\n not np.isfinite(objectinfo['wise2'])))):\n objectinfo['wise2'] = ticinfo['data'][0]['w2mag']\n objectinfo['wise2_err'] = (\n ticinfo['data'][0]['e_w2mag']\n )\n\n if ('wise3' not in objectinfo or\n ('wise3' in objectinfo and\n (objectinfo['wise3'] is None or\n not np.isfinite(objectinfo['wise3'])))):\n objectinfo['wise3'] = ticinfo['data'][0]['w3mag']\n objectinfo['wise3_err'] = (\n ticinfo['data'][0]['e_w3mag']\n )\n\n if ('wise4' not in objectinfo or\n ('wise4' in objectinfo and\n (objectinfo['wise4'] is None or\n not np.isfinite(objectinfo['wise4'])))):\n objectinfo['wise4'] = ticinfo['data'][0]['w4mag']\n objectinfo['wise4_err'] = (\n ticinfo['data'][0]['e_w4mag']\n )\n\n else:\n LOGERROR('could not look up TIC '\n 'information for object: %s '\n 'at (%.3f, %.3f)' %\n (objectinfo['objectid'],\n objectinfo['ra'],\n objectinfo['decl']))\n\n except Exception as e:\n\n LOGEXCEPTION('could not look up TIC '\n 'information for object: %s '\n 'at (%.3f, %.3f)' %\n (objectinfo['objectid'],\n objectinfo['ra'],\n objectinfo['decl']))\n\n\n # try to get the object's coord features\n coordfeat = coord_features(objectinfo)\n\n # get the color features\n colorfeat = color_features(objectinfo,\n deredden=deredden_object,\n custom_bandpasses=custom_bandpasses,\n dust_timeout=dust_timeout)\n\n # get the object's color classification\n colorclass = color_classification(colorfeat, coordfeat)\n\n # update the objectinfo dict with everything\n objectinfo.update(colorfeat)\n objectinfo.update(coordfeat)\n objectinfo.update(colorclass)\n\n # put together the initial checkplot pickle dictionary\n # this will be updated by the functions below as appropriate\n # and will written out as a gzipped pickle at the end of processing\n checkplotdict = {'objectid':objectid,\n 'neighbors':neighbors,\n 'objectinfo':objectinfo,\n 'finderchart':finderb64,\n 'sigclip':sigclip,\n 'normto':normto,\n 'normmingap':normmingap}\n\n # add the objecttags key to objectinfo\n checkplotdict['objectinfo']['objecttags'] = None\n\n # if there's no objectinfo, we can't do anything.\n else:\n\n # empty objectinfo dict\n checkplotdict = {'objectid':None,\n 'neighbors':None,\n 'objectinfo':{\n 'available_bands':[],\n 'available_band_labels':[],\n 'available_dereddened_bands':[],\n 'available_dereddened_band_labels':[],\n 'available_colors':[],\n 'available_color_labels':[],\n 'bmag':None,\n 'bmag-vmag':None,\n 'decl':None,\n 'hatid':None,\n 'hmag':None,\n 'imag-jmag':None,\n 'jmag-kmag':None,\n 'jmag':None,\n 'kmag':None,\n 'ndet':None,\n 'network':None,\n 'objecttags':None,\n 'pmdecl':None,\n 'pmdecl_err':None,\n 'pmra':None,\n 'pmra_err':None,\n 'propermotion':None,\n 'ra':None,\n 'rpmj':None,\n 'sdssg':None,\n 'sdssi':None,\n 'sdssr':None,\n 'stations':None,\n 'twomassid':None,\n 'ucac4id':None,\n 'vmag':None\n },\n 'finderchart':None,\n 'sigclip':sigclip,\n 'normto':normto,\n 'normmingap':normmingap}\n\n # end of objectinfo processing\n\n # add the varinfo dict\n if isinstance(varinfo, dict):\n checkplotdict['varinfo'] = varinfo\n else:\n checkplotdict['varinfo'] = {\n 'objectisvar':None,\n 'vartags':None,\n 'varisperiodic':None,\n 'varperiod':None,\n 'varepoch':None,\n }\n\n return checkplotdict", "response": "This function returns the finder chart and object information as a dict."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _pkl_magseries_plot(stimes, smags, serrs,\n plotdpi=100,\n magsarefluxes=False):\n '''This returns the magseries plot PNG as base64, plus arrays as dict.\n\n Parameters\n ----------\n\n stimes,smags,serrs : np.array\n The mag/flux time-series arrays along with associated errors. These\n should all have been run through nan-stripping and sigma-clipping\n beforehand.\n\n plotdpi : int\n The resolution of the plot to make in DPI.\n\n magsarefluxes : bool\n If True, indicates the input time-series is fluxes and not mags so the\n plot y-axis direction and range can be set appropriately.\n\n Returns\n -------\n\n dict\n A dict of the following form is returned::\n\n {'magseries': {'plot': base64 encoded str representation of the\n magnitude/flux time-series plot,\n 'times': the `stimes` array,\n 'mags': the `smags` array,\n 'errs': the 'serrs' array}}\n\n The dict is returned in this format so it can be directly incorporated\n in a checkplotdict, using Python's dict `update()` method.\n\n '''\n\n scaledplottime = stimes - npmin(stimes)\n\n # open the figure instance\n magseriesfig = plt.figure(figsize=(7.5,4.8),dpi=plotdpi)\n\n plt.plot(scaledplottime,\n smags,\n marker='o',\n ms=2.0, ls='None',mew=0,\n color='green',\n rasterized=True)\n\n # flip y axis for mags\n if not magsarefluxes:\n plot_ylim = plt.ylim()\n plt.ylim((plot_ylim[1], plot_ylim[0]))\n\n # set the x axis limit\n plt.xlim((npmin(scaledplottime)-2.0,\n npmax(scaledplottime)+2.0))\n\n # make a grid\n plt.grid(color='#a9a9a9',\n alpha=0.9,\n zorder=0,\n linewidth=1.0,\n linestyle=':')\n\n # make the x and y axis labels\n plot_xlabel = 'JD - %.3f' % npmin(stimes)\n if magsarefluxes:\n plot_ylabel = 'flux'\n else:\n plot_ylabel = 'magnitude'\n\n plt.xlabel(plot_xlabel)\n plt.ylabel(plot_ylabel)\n\n # fix the yaxis ticks (turns off offset and uses the full\n # value of the yaxis tick)\n plt.gca().get_yaxis().get_major_formatter().set_useOffset(False)\n plt.gca().get_xaxis().get_major_formatter().set_useOffset(False)\n\n # this is the output instance\n magseriespng = StrIO()\n magseriesfig.savefig(magseriespng,\n # bbox_inches='tight',\n pad_inches=0.05, format='png')\n plt.close()\n\n # encode the finderpng instance to base64\n magseriespng.seek(0)\n magseriesb64 = base64.b64encode(magseriespng.read())\n\n # close the stringio buffer\n magseriespng.close()\n\n checkplotdict = {\n 'magseries':{\n 'plot':magseriesb64,\n 'times':stimes,\n 'mags':smags,\n 'errs':serrs\n }\n }\n\n return checkplotdict", "response": "This function returns the magseries plot PNG as base64 plus arrays as dict."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _pkl_phased_magseries_plot(\n checkplotdict,\n lspmethod,\n periodind,\n stimes, smags, serrs,\n varperiod, varepoch,\n lspmethodind=0,\n phasewrap=True,\n phasesort=True,\n phasebin=0.002,\n minbinelems=7,\n plotxlim=(-0.8,0.8),\n plotdpi=100,\n bestperiodhighlight=None,\n xgridlines=None,\n xliminsetmode=False,\n magsarefluxes=False,\n directreturn=False,\n overplotfit=None,\n verbose=True,\n override_pfmethod=None\n):\n '''This returns the phased magseries plot PNG as base64 plus info as a dict.\n\n Parameters\n ----------\n\n checkplotdict : dict\n This is an existing checkplotdict to update. If it's None or\n `directreturn` = True, then the generated dict result for this magseries\n plot will be returned directly.\n\n lspmethod : str\n lspmethod is a string indicating the type of period-finding algorithm\n that produced the period. If this is not in\n `astrobase.plotbase.METHODSHORTLABELS`, it will be used verbatim. In\n most cases, this will come directly from the lspinfo dict produced by a\n period-finder function.\n\n periodind : int\n This is the index of the current periodogram period being operated\n on::\n\n If == 0 -> best period and `bestperiodhighlight` is applied if not\n None\n If > 0 -> some other peak of the periodogram\n If == -1 -> special mode w/ no periodogram labels and enabled\n highlight\n\n stimes,smags,serrs : np.array\n The mag/flux time-series arrays along with associated errors. These\n should all have been run through nan-stripping and sigma-clipping\n beforehand.\n\n varperiod : float or None\n The period to use for this phased light curve plot tile.\n\n varepoch : 'min' or float or list of lists or None\n The epoch to use for this phased light curve plot tile. If this is a\n float, will use the provided value directly. If this is 'min', will\n automatically figure out the time-of-minimum of the phased light\n curve. If this is None, will use the mimimum value of `stimes` as the\n epoch of the phased light curve plot. If this is a list of lists, will\n use the provided value of `lspmethodind` to look up the current\n period-finder method and the provided value of `periodind` to look up\n the epoch associated with that method and the current period. This is\n mostly only useful when `twolspmode` is True.\n\n phasewrap : bool\n If this is True, the phased time-series will be wrapped around\n phase 0.0.\n\n phasesort : bool\n If True, will sort the phased light curve in order of increasing phase.\n\n phasebin: float\n The bin size to use to group together measurements closer than this\n amount in phase. This is in units of phase. If this is a float, a\n phase-binned version of the phased light curve will be overplotted on\n top of the regular phased light curve.\n\n minbinelems : int\n The minimum number of elements required per phase bin to include it in\n the phased LC plot.\n\n plotxlim : sequence of two floats or None\n The x-range (min, max) of the phased light curve plot. If None, will be\n determined automatically.\n\n plotdpi : int\n The resolution of the output plot PNGs in dots per inch.\n\n bestperiodhighlight : str or None\n If not None, this is a str with a matplotlib color specification to use\n as the background color to highlight the phased light curve plot of the\n 'best' period and epoch combination. If None, no highlight will be\n applied.\n\n xgridlines : list of floats or None\n If this is provided, must be a list of floats corresponding to the phase\n values where to draw vertical dashed lines as a means of highlighting\n these.\n\n xliminsetmode : bool\n If this is True, the generated phased light curve plot will use the\n values of `plotxlim` as the main plot x-axis limits (i.e. zoomed-in if\n `plotxlim` is a range smaller than the full phase range), and will show\n the full phased light curve plot as an smaller inset. Useful for\n planetary transit light curves.\n\n magsarefluxes : bool\n If True, indicates the input time-series is fluxes and not mags so the\n plot y-axis direction and range can be set appropriately.\n\n directreturn : bool\n If this set to True, will return only the dict corresponding to the\n phased LC plot for the input `periodind` and `lspmethod` and not return\n this result embedded in a checkplotdict.\n\n overplotfit : dict\n If this is provided, it must be a dict of the form returned by one of\n the astrobase.lcfit.fit_XXXXX_magseries functions. This can be used to\n overplot a light curve model fit on top of the phased light curve plot\n returned by this function. The `overplotfit` dict has the following\n form, including at least the keys listed here::\n\n {'fittype':str: name of fit method,\n 'fitchisq':float: the chi-squared value of the fit,\n 'fitredchisq':float: the reduced chi-squared value of the fit,\n 'fitinfo':{'fitmags':array: model mags or fluxes from fit function},\n 'magseries':{'times':array: times where the fitmags are evaluated}}\n\n `fitmags` and `times` should all be of the same size. The input\n `overplotfit` dict is copied over to the checkplotdict for each specific\n phased LC plot to save all of this information for use later.\n\n verbose : bool\n If True, will indicate progress and warn about problems.\n\n override_pfmethod : str or None\n This is used to set a custom label for the periodogram method. Normally,\n this is taken from the 'method' key in the input `lspinfo` dict, but if\n you want to override the output method name, provide this as a string\n here. This can be useful if you have multiple results you want to\n incorporate into a checkplotdict from a single period-finder (e.g. if\n you ran BLS over several period ranges separately).\n\n Returns\n -------\n\n dict\n Returns a dict of the following form::\n\n {lspmethod: {'plot': the phased LC plot as base64 str,\n 'period': the period used for this phased LC,\n 'epoch': the epoch used for this phased LC,\n 'phase': phase value array,\n 'phasedmags': mags/fluxes sorted in phase order,\n 'binphase': array of binned phase values,\n 'binphasedmags': mags/fluxes sorted in binphase order,\n 'phasewrap': value of the input `phasewrap` kwarg,\n 'phasesort': value of the input `phasesort` kwarg,\n 'phasebin': value of the input `phasebin` kwarg,\n 'minbinelems': value of the input `minbinelems` kwarg,\n 'plotxlim': value of the input `plotxlim` kwarg,\n 'lcfit': the provided `overplotfit` dict}}\n\n The dict is in this form because we can use Python dicts' `update()`\n method to update an existing checkplotdict. If `returndirect` is True,\n only the inner dict is returned.\n\n '''\n\n # open the figure instance\n phasedseriesfig = plt.figure(figsize=(7.5,4.8),dpi=plotdpi)\n\n plotvarepoch = None\n\n # figure out the epoch, if it's None, use the min of the time\n if varepoch is None:\n plotvarepoch = npmin(stimes)\n\n # if the varepoch is 'min', then fit a spline to the light curve\n # phased using the min of the time, find the fit mag minimum and use\n # the time for that as the varepoch\n elif isinstance(varepoch,str) and varepoch == 'min':\n\n try:\n spfit = spline_fit_magseries(stimes,\n smags,\n serrs,\n varperiod,\n magsarefluxes=magsarefluxes,\n sigclip=None,\n verbose=verbose)\n plotvarepoch = spfit['fitinfo']['fitepoch']\n if len(plotvarepoch) != 1:\n plotvarepoch = plotvarepoch[0]\n\n\n except Exception as e:\n\n LOGERROR('spline fit failed, trying SavGol fit')\n\n sgfit = savgol_fit_magseries(stimes,\n smags,\n serrs,\n varperiod,\n sigclip=None,\n magsarefluxes=magsarefluxes,\n verbose=verbose)\n plotvarepoch = sgfit['fitinfo']['fitepoch']\n if len(plotvarepoch) != 1:\n plotvarepoch = plotvarepoch[0]\n\n finally:\n\n if plotvarepoch is None:\n\n LOGERROR('could not find a min epoch time, '\n 'using min(times) as the epoch for '\n 'the phase-folded LC')\n\n plotvarepoch = npmin(stimes)\n\n # special case with varepoch lists per each period-finder method\n elif isinstance(varepoch, list):\n\n try:\n thisvarepochlist = varepoch[lspmethodind]\n plotvarepoch = thisvarepochlist[periodind]\n except Exception as e:\n LOGEXCEPTION(\n \"varepoch provided in list form either doesn't match \"\n \"the length of nbestperiods from the period-finder \"\n \"result, or something else went wrong. using min(times) \"\n \"as the epoch instead\"\n )\n plotvarepoch = npmin(stimes)\n\n # the final case is to use the provided varepoch directly\n else:\n plotvarepoch = varepoch\n\n\n if verbose:\n LOGINFO('plotting %s phased LC with period %s: %.6f, epoch: %.5f' %\n (lspmethod, periodind, varperiod, plotvarepoch))\n\n # make the plot title based on the lspmethod\n if periodind == 0:\n plottitle = '%s best period: %.6f d - epoch: %.5f' % (\n (METHODSHORTLABELS[lspmethod] if lspmethod in METHODSHORTLABELS\n else lspmethod),\n varperiod,\n plotvarepoch\n )\n elif periodind > 0:\n plottitle = '%s peak %s: %.6f d - epoch: %.5f' % (\n (METHODSHORTLABELS[lspmethod] if lspmethod in METHODSHORTLABELS\n else lspmethod),\n periodind+1,\n varperiod,\n plotvarepoch\n )\n elif periodind == -1:\n plottitle = '%s period: %.6f d - epoch: %.5f' % (\n lspmethod,\n varperiod,\n plotvarepoch\n )\n\n\n # phase the magseries\n phasedlc = phase_magseries(stimes,\n smags,\n varperiod,\n plotvarepoch,\n wrap=phasewrap,\n sort=phasesort)\n plotphase = phasedlc['phase']\n plotmags = phasedlc['mags']\n\n # if we're supposed to bin the phases, do so\n if phasebin:\n\n binphasedlc = phase_bin_magseries(plotphase,\n plotmags,\n binsize=phasebin,\n minbinelems=minbinelems)\n binplotphase = binphasedlc['binnedphases']\n binplotmags = binphasedlc['binnedmags']\n\n else:\n binplotphase = None\n binplotmags = None\n\n\n # finally, make the phased LC plot\n plt.plot(plotphase,\n plotmags,\n marker='o',\n ms=2.0, ls='None',mew=0,\n color='gray',\n rasterized=True)\n\n # overlay the binned phased LC plot if we're making one\n if phasebin:\n plt.plot(binplotphase,\n binplotmags,\n marker='o',\n ms=4.0, ls='None',mew=0,\n color='#1c1e57',\n rasterized=True)\n\n\n # if we're making a overplotfit, then plot the fit over the other stuff\n if overplotfit and isinstance(overplotfit, dict):\n\n fitmethod = overplotfit['fittype']\n fitredchisq = overplotfit['fitredchisq']\n\n plotfitmags = overplotfit['fitinfo']['fitmags']\n plotfittimes = overplotfit['magseries']['times']\n\n # phase the fit magseries\n fitphasedlc = phase_magseries(plotfittimes,\n plotfitmags,\n varperiod,\n plotvarepoch,\n wrap=phasewrap,\n sort=phasesort)\n plotfitphase = fitphasedlc['phase']\n plotfitmags = fitphasedlc['mags']\n\n plotfitlabel = (r'%s fit ${\\chi}^2/{\\mathrm{dof}} = %.3f$' %\n (fitmethod, fitredchisq))\n\n # plot the fit phase and mags\n plt.plot(plotfitphase, plotfitmags,'k-',\n linewidth=3, rasterized=True,label=plotfitlabel)\n\n plt.legend(loc='upper left', frameon=False)\n\n # flip y axis for mags\n if not magsarefluxes:\n plot_ylim = plt.ylim()\n plt.ylim((plot_ylim[1], plot_ylim[0]))\n\n # set the x axis limit\n if not plotxlim:\n plt.xlim((npmin(plotphase)-0.1,\n npmax(plotphase)+0.1))\n else:\n plt.xlim((plotxlim[0],plotxlim[1]))\n\n # make a grid\n ax = plt.gca()\n if isinstance(xgridlines, (list, tuple)):\n ax.set_xticks(xgridlines, minor=False)\n\n plt.grid(color='#a9a9a9',\n alpha=0.9,\n zorder=0,\n linewidth=1.0,\n linestyle=':')\n\n\n # make the x and y axis labels\n plot_xlabel = 'phase'\n if magsarefluxes:\n plot_ylabel = 'flux'\n else:\n plot_ylabel = 'magnitude'\n\n plt.xlabel(plot_xlabel)\n plt.ylabel(plot_ylabel)\n\n # fix the yaxis ticks (turns off offset and uses the full\n # value of the yaxis tick)\n plt.gca().get_yaxis().get_major_formatter().set_useOffset(False)\n plt.gca().get_xaxis().get_major_formatter().set_useOffset(False)\n\n # set the plot title\n plt.title(plottitle)\n\n # make sure the best period phased LC plot stands out\n if (periodind == 0 or periodind == -1) and bestperiodhighlight:\n if MPLVERSION >= (2,0,0):\n plt.gca().set_facecolor(bestperiodhighlight)\n else:\n plt.gca().set_axis_bgcolor(bestperiodhighlight)\n\n # if we're making an inset plot showing the full range\n if (plotxlim and isinstance(plotxlim, (list, tuple)) and\n len(plotxlim) == 2 and xliminsetmode is True):\n\n # bump the ylim of the plot so that the inset can fit in this axes plot\n axesylim = plt.gca().get_ylim()\n\n if magsarefluxes:\n plt.gca().set_ylim(\n axesylim[0],\n axesylim[1] + 0.5*npabs(axesylim[1]-axesylim[0])\n )\n else:\n plt.gca().set_ylim(\n axesylim[0],\n axesylim[1] - 0.5*npabs(axesylim[1]-axesylim[0])\n )\n\n # put the inset axes in\n inset = inset_axes(plt.gca(), width=\"40%\", height=\"40%\", loc=1)\n\n # make the scatter plot for the phased LC plot\n inset.plot(plotphase,\n plotmags,\n marker='o',\n ms=2.0, ls='None',mew=0,\n color='gray',\n rasterized=True)\n\n if phasebin:\n # make the scatter plot for the phased LC plot\n inset.plot(binplotphase,\n binplotmags,\n marker='o',\n ms=4.0, ls='None',mew=0,\n color='#1c1e57',\n rasterized=True)\n\n # show the full phase coverage\n if phasewrap:\n inset.set_xlim(-0.2,0.8)\n else:\n inset.set_xlim(-0.1,1.1)\n\n # flip y axis for mags\n if not magsarefluxes:\n inset_ylim = inset.get_ylim()\n inset.set_ylim((inset_ylim[1], inset_ylim[0]))\n\n # set the plot title\n inset.text(0.5,0.9,'full phased light curve',\n ha='center',va='center',transform=inset.transAxes)\n # don't show axes labels or ticks\n inset.set_xticks([])\n inset.set_yticks([])\n\n # this is the output instance\n phasedseriespng = StrIO()\n phasedseriesfig.savefig(phasedseriespng,\n # bbox_inches='tight',\n pad_inches=0.0, format='png')\n plt.close()\n\n # encode the finderpng instance to base64\n phasedseriespng.seek(0)\n phasedseriesb64 = base64.b64encode(phasedseriespng.read())\n\n # close the stringio buffer\n phasedseriespng.close()\n\n # this includes a fitinfo dict if one is provided in overplotfit\n retdict = {\n 'plot':phasedseriesb64,\n 'period':varperiod,\n 'epoch':plotvarepoch,\n 'phase':plotphase,\n 'phasedmags':plotmags,\n 'binphase':binplotphase,\n 'binphasedmags':binplotmags,\n 'phasewrap':phasewrap,\n 'phasesort':phasesort,\n 'phasebin':phasebin,\n 'minbinelems':minbinelems,\n 'plotxlim':plotxlim,\n 'lcfit':overplotfit,\n }\n\n # if we're returning stuff directly, i.e. not being used embedded within\n # the checkplot_dict function\n if directreturn or checkplotdict is None:\n\n return retdict\n\n # this requires the checkplotdict to be present already, we'll just update\n # it at the appropriate lspmethod and periodind\n else:\n\n if override_pfmethod:\n checkplotdict[override_pfmethod][periodind] = retdict\n else:\n checkplotdict[lspmethod][periodind] = retdict\n\n return checkplotdict", "response": "This function returns the phased magseries plot PNG as base64 plus info as a dict."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef prewhiten_magseries(times, mags, errs,\n whitenperiod,\n whitenparams,\n sigclip=3.0,\n magsarefluxes=False,\n plotfit=None,\n plotfitphasedlconly=True,\n rescaletomedian=True):\n '''Removes a periodic sinusoidal signal generated using whitenparams from\n the input magnitude time series.\n\n Parameters\n ----------\n\n times,mags,errs : np.array\n The input mag/flux time-series to prewhiten.\n\n whitenperiod : float\n The period of the sinusoidal signal to remove.\n\n whitenparams : list of floats\n This contains the Fourier amplitude and phase coefficients of the\n sinusoidal signal to remove::\n\n [ampl_1, ampl_2, ampl_3, ..., ampl_X,\n pha_1, pha_2, pha_3, ..., pha_X]\n\n where `X` is the Fourier order. These are usually the output of a\n previous Fourier fit to the light curve (from\n :py:func:`astrobase.lcfit.sinusoidal.fourier_fit_magseries` for\n example).\n\n sigclip : float or int or sequence of two floats/ints or None\n If a single float or int, a symmetric sigma-clip will be performed using\n the number provided as the sigma-multiplier to cut out from the input\n time-series.\n\n If a list of two ints/floats is provided, the function will perform an\n 'asymmetric' sigma-clip. The first element in this list is the sigma\n value to use for fainter flux/mag values; the second element in this\n list is the sigma value to use for brighter flux/mag values. For\n example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma\n dimmings and greater than 3-sigma brightenings. Here the meaning of\n \"dimming\" and \"brightening\" is set by *physics* (not the magnitude\n system), which is why the `magsarefluxes` kwarg must be correctly set.\n\n If `sigclip` is None, no sigma-clipping will be performed, and the\n time-series (with non-finite elems removed) will be passed through to\n the output.\n\n magsarefluxes : bool\n If True, will treat the input values of `mags` as fluxes for purposes of\n plotting the fit and sig-clipping.\n\n plotfit : str or False\n If this is a string, this function will make a plot showing the effect\n of the pre-whitening on the mag/flux time-series and write the plot to\n the path specified here.\n\n plotfitphasedlconly : bool\n If True, will plot only the phased LC for showing the effect of\n pre-whitening, and skip plotting the unphased LC.\n\n rescaletomedian : bool\n If this is True, then we add back the constant median term of the\n magnitudes to the final pre-whitened mag series.\n\n Returns\n -------\n\n dict\n Returns a dict of the form::\n\n {'wtimes':times array after pre-whitening,\n 'wphase':phase array after pre-whitening,\n 'wmags':mags array after pre-whitening,\n 'werrs':errs array after pre-whitening,\n 'whitenparams':the input pre-whitening params used,\n 'whitenperiod':the input pre-whitening period used,\n 'fitplotfile':the output plot file if plotfit was set}\n\n '''\n\n stimes, smags, serrs = sigclip_magseries(times, mags, errs,\n sigclip=sigclip,\n magsarefluxes=magsarefluxes)\n\n median_mag = np.median(smags)\n\n\n # phase the mag series using the given period and epoch = min(stimes)\n mintime = np.min(stimes)\n\n # calculate the unsorted phase, then sort it\n iphase = (\n (stimes - mintime)/whitenperiod -\n np.floor((stimes - mintime)/whitenperiod)\n )\n phasesortind = np.argsort(iphase)\n\n # these are the final quantities to use for the Fourier fits\n phase = iphase[phasesortind]\n pmags = smags[phasesortind]\n perrs = serrs[phasesortind]\n\n # get the times sorted in phase order (useful to get the fit mag minimum\n # with respect to phase -- the light curve minimum)\n ptimes = stimes[phasesortind]\n\n # now subtract the harmonic series from the phased LC\n # these are still in phase order\n wmags = pmags - _fourier_func(whitenparams, phase, pmags)\n\n # resort everything by time order\n wtimeorder = np.argsort(ptimes)\n wtimes = ptimes[wtimeorder]\n wphase = phase[wtimeorder]\n wmags = wmags[wtimeorder]\n werrs = perrs[wtimeorder]\n\n if rescaletomedian:\n wmags = wmags + median_mag\n\n # prepare the returndict\n returndict = {'wtimes':wtimes, # these are in the new time order\n 'wphase':wphase,\n 'wmags':wmags,\n 'werrs':werrs,\n 'whitenparams':whitenparams,\n 'whitenperiod':whitenperiod}\n\n\n # make the fit plot if required\n if plotfit and (isinstance(plotfit, str) or isinstance(plotfit, Strio)):\n\n if plotfitphasedlconly:\n plt.figure(figsize=(10,4.8))\n else:\n plt.figure(figsize=(16,9.6))\n\n if plotfitphasedlconly:\n\n # phased series before whitening\n plt.subplot(121)\n plt.plot(phase,pmags,\n marker='.',\n color='k',\n linestyle='None',\n markersize=2.0,\n markeredgewidth=0)\n\n if not magsarefluxes:\n plt.gca().invert_yaxis()\n plt.ylabel('magnitude')\n else:\n plt.ylabel('fluxes')\n\n plt.xlabel('phase')\n plt.title('phased LC before pre-whitening')\n\n # phased series after whitening\n plt.subplot(122)\n plt.plot(wphase,wmags,\n marker='.',\n color='g',\n linestyle='None',\n markersize=2.0,\n markeredgewidth=0)\n\n if not magsarefluxes:\n plt.gca().invert_yaxis()\n plt.ylabel('magnitude')\n else:\n plt.ylabel('fluxes')\n\n plt.xlabel('phase')\n plt.title('phased LC after pre-whitening')\n\n else:\n\n # time series before whitening\n plt.subplot(221)\n plt.plot(stimes,smags,\n marker='.',\n color='k',\n linestyle='None',\n markersize=2.0,\n markeredgewidth=0)\n\n if not magsarefluxes:\n plt.gca().invert_yaxis()\n plt.ylabel('magnitude')\n else:\n plt.ylabel('fluxes')\n\n plt.xlabel('JD')\n plt.title('LC before pre-whitening')\n\n # time series after whitening\n plt.subplot(222)\n plt.plot(wtimes,wmags,\n marker='.',\n color='g',\n linestyle='None',\n markersize=2.0,\n markeredgewidth=0)\n\n if not magsarefluxes:\n plt.gca().invert_yaxis()\n plt.ylabel('magnitude')\n else:\n plt.ylabel('fluxes')\n\n plt.xlabel('JD')\n plt.title('LC after pre-whitening with period: %.6f' % whitenperiod)\n\n # phased series before whitening\n plt.subplot(223)\n plt.plot(phase,pmags,\n marker='.',\n color='k',\n linestyle='None',\n markersize=2.0,\n markeredgewidth=0)\n\n if not magsarefluxes:\n plt.gca().invert_yaxis()\n plt.ylabel('magnitude')\n else:\n plt.ylabel('fluxes')\n\n plt.xlabel('phase')\n plt.title('phased LC before pre-whitening')\n\n # phased series after whitening\n plt.subplot(224)\n plt.plot(wphase,wmags,\n marker='.',\n color='g',\n linestyle='None',\n markersize=2.0,\n markeredgewidth=0)\n\n if not magsarefluxes:\n plt.gca().invert_yaxis()\n plt.ylabel('magnitude')\n else:\n plt.ylabel('fluxes')\n\n plt.xlabel('phase')\n plt.title('phased LC after pre-whitening')\n\n plt.tight_layout()\n plt.savefig(plotfit, format='png', pad_inches=0.0)\n plt.close()\n\n if isinstance(plotfit, str) or isinstance(plotfit, Strio):\n returndict['fitplotfile'] = plotfit\n\n return returndict", "response": "This function removes a periodic sinusoidal signal generated using whitenparams from the input magnitude time - series."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef mask_signal(times, mags, errs,\n signalperiod,\n signalepoch,\n magsarefluxes=False,\n maskphases=(0,0,0.5,1.0),\n maskphaselength=0.1,\n plotfit=None,\n plotfitphasedlconly=True,\n sigclip=30.0):\n '''This removes repeating signals in the magnitude time series.\n\n Useful for masking planetary transit signals in light curves to search for\n other variability.\n\n A small worked example of using this and `prewhiten_magseries` above:\n\n https://github.com/waqasbhatti/astrobase/issues/77#issuecomment-463803558\n\n Parameters\n ----------\n\n times,mags,errs : np.array\n The input mag/flux time-series to run the masking on.\n\n signalperiod : float\n The period of the signal to mask.\n\n signalepoch : float\n The epoch of the signal to mask.\n\n magsarefluxes : bool\n Set to True if `mags` is actually an array of fluxes.\n\n maskphases : sequence of floats\n This defines which phase values will be masked. For each item in this\n sequence, this function will mask a length of phase given by\n `maskphaselength` centered on each `maskphases` value, and remove all LC\n points in these regions from the light curve.\n\n maskphaselength : float\n The length in phase to mask for each phase value provided in\n `maskphases`.\n\n plotfit : str or None\n If provided as a str, indicates the output plot file.\n\n plotfitphasedlconly : bool\n If True, will only plot the effect of masking the signal as requested on\n the phased LC. If False, will also plot the unphased LC.\n\n sigclip : float or int or sequence of two floats/ints or None\n If a single float or int, a symmetric sigma-clip will be performed using\n the number provided as the sigma-multiplier to cut out from the input\n time-series.\n\n If a list of two ints/floats is provided, the function will perform an\n 'asymmetric' sigma-clip. The first element in this list is the sigma\n value to use for fainter flux/mag values; the second element in this\n list is the sigma value to use for brighter flux/mag values. For\n example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma\n dimmings and greater than 3-sigma brightenings. Here the meaning of\n \"dimming\" and \"brightening\" is set by *physics* (not the magnitude\n system), which is why the `magsarefluxes` kwarg must be correctly set.\n\n If `sigclip` is None, no sigma-clipping will be performed, and the\n time-series (with non-finite elems removed) will be passed through to\n the output.\n\n '''\n\n stimes, smags, serrs = sigclip_magseries(times, mags, errs,\n sigclip=sigclip,\n magsarefluxes=magsarefluxes)\n\n\n # now phase the light curve using the period and epoch provided\n phases = (\n (stimes - signalepoch)/signalperiod -\n np.floor((stimes - signalepoch)/signalperiod)\n )\n\n # mask the requested phases using the mask length (in phase units)\n # this gets all the masks into one array\n masks = np.array([(np.abs(phases - x) > maskphaselength)\n for x in maskphases])\n # this flattens the masks to a single array for all combinations\n masks = np.all(masks,axis=0)\n\n # apply the mask to the times, mags, and errs\n mphases = phases[masks]\n mtimes = stimes[masks]\n mmags = smags[masks]\n merrs = serrs[masks]\n\n returndict = {'mphases':mphases,\n 'mtimes':mtimes,\n 'mmags':mmags,\n 'merrs':merrs}\n\n # make the fit plot if required\n if plotfit and isinstance(plotfit, str) or isinstance(plotfit, Strio):\n\n if plotfitphasedlconly:\n plt.figure(figsize=(10,4.8))\n else:\n plt.figure(figsize=(16,9.6))\n\n if plotfitphasedlconly:\n\n # phased series before whitening\n plt.subplot(121)\n plt.plot(phases,smags,\n marker='.',\n color='k',\n linestyle='None',\n markersize=2.0,\n markeredgewidth=0)\n\n if not magsarefluxes:\n plt.gca().invert_yaxis()\n plt.ylabel('magnitude')\n else:\n plt.ylabel('fluxes')\n\n plt.xlabel('phase')\n plt.title('phased LC before signal masking')\n\n # phased series after whitening\n plt.subplot(122)\n plt.plot(mphases,mmags,\n marker='.',\n color='g',\n linestyle='None',\n markersize=2.0,\n markeredgewidth=0)\n\n if not magsarefluxes:\n plt.gca().invert_yaxis()\n plt.ylabel('magnitude')\n else:\n plt.ylabel('fluxes')\n\n plt.xlabel('phase')\n plt.title('phased LC after signal masking')\n\n else:\n\n # time series before whitening\n plt.subplot(221)\n plt.plot(stimes,smags,\n marker='.',\n color='k',\n linestyle='None',\n markersize=2.0,\n markeredgewidth=0)\n\n if not magsarefluxes:\n plt.gca().invert_yaxis()\n plt.ylabel('magnitude')\n else:\n plt.ylabel('fluxes')\n\n plt.xlabel('JD')\n plt.title('LC before signal masking')\n\n # time series after whitening\n plt.subplot(222)\n plt.plot(mtimes,mmags,\n marker='.',\n color='g',\n linestyle='None',\n markersize=2.0,\n markeredgewidth=0)\n\n if not magsarefluxes:\n plt.gca().invert_yaxis()\n plt.ylabel('magnitude')\n else:\n plt.ylabel('fluxes')\n\n plt.xlabel('JD')\n plt.title('LC after signal masking')\n\n # phased series before whitening\n plt.subplot(223)\n plt.plot(phases,smags,\n marker='.',\n color='k',\n linestyle='None',\n markersize=2.0,\n markeredgewidth=0)\n\n if not magsarefluxes:\n plt.gca().invert_yaxis()\n plt.ylabel('magnitude')\n else:\n plt.ylabel('fluxes')\n\n plt.xlabel('phase')\n plt.title('phased LC before signal masking')\n\n # phased series after whitening\n plt.subplot(224)\n plt.plot(mphases,mmags,\n marker='.',\n color='g',\n linestyle='None',\n markersize=2.0,\n markeredgewidth=0)\n\n if not magsarefluxes:\n plt.gca().invert_yaxis()\n plt.ylabel('magnitude')\n else:\n plt.ylabel('fluxes')\n\n plt.xlabel('phase')\n plt.title('phased LC after signal masking')\n\n plt.tight_layout()\n plt.savefig(plotfit, format='png', pad_inches=0.0)\n plt.close()\n\n if isinstance(plotfit, str) or isinstance(plotfit, Strio):\n returndict['fitplotfile'] = plotfit\n\n\n return returndict", "response": "This function masks the signal in the time - series times and mags."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef find_lc_timegroups(lctimes, mingap=4.0):\n '''Finds gaps in the provided time-series and indexes them into groups.\n\n This finds the gaps in the provided `lctimes` array, so we can figure out\n which times are for consecutive observations and which represent gaps\n between seasons or observing eras.\n\n Parameters\n ----------\n\n lctimes : array-like\n This contains the times to analyze for gaps; assumed to be some form of\n Julian date.\n\n mingap : float\n This defines how much the difference between consecutive measurements is\n allowed to be to consider them as parts of different timegroups. By\n default it is set to 4.0 days.\n\n Returns\n -------\n\n tuple\n A tuple of the form: `(ngroups, [slice(start_ind_1, end_ind_1), ...])`\n is returned. This contains the number of groups as the first element,\n and a list of Python `slice` objects for each time-group found. These\n can be used directly to index into the array of times to quickly get\n measurements associated with each group.\n\n '''\n\n lc_time_diffs = np.diff(lctimes)\n group_start_indices = np.where(lc_time_diffs > mingap)[0]\n\n if len(group_start_indices) > 0:\n\n group_indices = []\n\n for i, gindex in enumerate(group_start_indices):\n\n if i == 0:\n group_indices.append(slice(0,gindex+1))\n else:\n group_indices.append(slice(group_start_indices[i-1]+1,gindex+1))\n\n\n # at the end, add the slice for the last group to the end of the times\n # array\n group_indices.append(slice(group_start_indices[-1]+1,len(lctimes)))\n\n # if there's no large gap in the LC, then there's only one group to worry\n # about\n else:\n group_indices = [slice(0,len(lctimes))]\n\n\n return len(group_indices), group_indices", "response": "Finds gaps in the provided time - series and indexes them into groups."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef normalize_magseries(times,\n mags,\n mingap=4.0,\n normto='globalmedian',\n magsarefluxes=False,\n debugmode=False):\n '''This normalizes the magnitude time-series to a specified value.\n\n This is used to normalize time series measurements that may have large time\n gaps and vertical offsets in mag/flux measurement between these\n 'timegroups', either due to instrument changes or different filters.\n\n NOTE: this works in-place! The mags array will be replaced with normalized\n mags when this function finishes.\n\n Parameters\n ----------\n\n times,mags : array-like\n The times (assumed to be some form of JD) and mags (or flux)\n measurements to be normalized.\n\n mingap : float\n This defines how much the difference between consecutive measurements is\n allowed to be to consider them as parts of different timegroups. By\n default it is set to 4.0 days.\n\n normto : {'globalmedian', 'zero'} or a float\n Specifies the normalization type::\n\n 'globalmedian' -> norms each mag to the global median of the LC column\n 'zero' -> norms each mag to zero\n a float -> norms each mag to this specified float value.\n\n magsarefluxes : bool\n Indicates if the input `mags` array is actually an array of flux\n measurements instead of magnitude measurements. If this is set to True,\n then:\n\n - if `normto` is 'zero', then the median flux is divided from each\n observation's flux value to yield normalized fluxes with 1.0 as the\n global median.\n\n - if `normto` is 'globalmedian', then the global median flux value\n across the entire time series is multiplied with each measurement.\n\n - if `norm` is set to a `float`, then this number is multiplied with the\n flux value for each measurement.\n\n debugmode : bool\n If this is True, will print out verbose info on each timegroup found.\n\n Returns\n -------\n\n times,normalized_mags : np.arrays\n Normalized magnitude values after normalization. If normalization fails\n for some reason, `times` and `normalized_mags` will both be None.\n\n '''\n\n ngroups, timegroups = find_lc_timegroups(times,\n mingap=mingap)\n\n # find all the non-nan indices\n finite_ind = np.isfinite(mags)\n\n if any(finite_ind):\n\n # find the global median\n global_mag_median = np.median(mags[finite_ind])\n\n # go through the groups and normalize them to the median for\n # each group\n for tgind, tg in enumerate(timegroups):\n\n finite_ind = np.isfinite(mags[tg])\n\n # find this timegroup's median mag and normalize the mags in\n # it to this median\n group_median = np.median((mags[tg])[finite_ind])\n\n if magsarefluxes:\n mags[tg] = mags[tg]/group_median\n else:\n mags[tg] = mags[tg] - group_median\n\n if debugmode:\n LOGDEBUG('group %s: elems %s, '\n 'finite elems %s, median mag %s' %\n (tgind,\n len(mags[tg]),\n len(finite_ind),\n group_median))\n\n # now that everything is normalized to 0.0, add the global median\n # offset back to all the mags and write the result back to the dict\n if isinstance(normto, str) and normto == 'globalmedian':\n\n if magsarefluxes:\n mags = mags * global_mag_median\n else:\n mags = mags + global_mag_median\n\n # if the normto is a float, add everything to that float and return\n elif isinstance(normto, float):\n\n if magsarefluxes:\n mags = mags * normto\n else:\n mags = mags + normto\n\n # anything else just returns the normalized mags as usual\n return times, mags\n\n else:\n LOGERROR('measurements are all nan!')\n return None, None", "response": "This function normalizes the magnitude time - series measurements to a specified value."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef sigclip_magseries(times, mags, errs,\n sigclip=None,\n iterative=False,\n niterations=None,\n meanormedian='median',\n magsarefluxes=False):\n '''Sigma-clips a magnitude or flux time-series.\n\n Selects the finite times, magnitudes (or fluxes), and errors from the passed\n values, and apply symmetric or asymmetric sigma clipping to them.\n\n Parameters\n ----------\n\n times,mags,errs : np.array\n The magnitude or flux time-series arrays to sigma-clip. This doesn't\n assume all values are finite or if they're positive/negative. All of\n these arrays will have their non-finite elements removed, and then will\n be sigma-clipped based on the arguments to this function.\n\n `errs` is optional. Set it to None if you don't have values for these. A\n 'faked' `errs` array will be generated if necessary, which can be\n ignored in the output as well.\n\n sigclip : float or int or sequence of two floats/ints or None\n If a single float or int, a symmetric sigma-clip will be performed using\n the number provided as the sigma-multiplier to cut out from the input\n time-series.\n\n If a list of two ints/floats is provided, the function will perform an\n 'asymmetric' sigma-clip. The first element in this list is the sigma\n value to use for fainter flux/mag values; the second element in this\n list is the sigma value to use for brighter flux/mag values. For\n example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma\n dimmings and greater than 3-sigma brightenings. Here the meaning of\n \"dimming\" and \"brightening\" is set by *physics* (not the magnitude\n system), which is why the `magsarefluxes` kwarg must be correctly set.\n\n If `sigclip` is None, no sigma-clipping will be performed, and the\n time-series (with non-finite elems removed) will be passed through to\n the output.\n\n iterative : bool\n If this is set to True, will perform iterative sigma-clipping. If\n `niterations` is not set and this is True, sigma-clipping is iterated\n until no more points are removed.\n\n niterations : int\n The maximum number of iterations to perform for sigma-clipping. If None,\n the `iterative` arg takes precedence, and `iterative=True` will\n sigma-clip until no more points are removed. If `niterations` is not\n None and `iterative` is False, `niterations` takes precedence and\n iteration will occur for the specified number of iterations.\n\n meanormedian : {'mean', 'median'}\n Use 'mean' for sigma-clipping based on the mean value, or 'median' for\n sigma-clipping based on the median value. Default is 'median'.\n\n magsareflux : bool\n True if your \"mags\" are in fact fluxes, i.e. if \"fainter\" corresponds to\n `mags` getting smaller.\n\n Returns\n -------\n\n (stimes, smags, serrs) : tuple\n The sigma-clipped and nan-stripped time-series.\n\n '''\n\n returnerrs = True\n\n # fake the errors if they don't exist\n # this is inconsequential to sigma-clipping\n # we don't return these dummy values if the input errs are None\n if errs is None:\n # assume 0.1% errors if not given\n # this should work for mags and fluxes\n errs = 0.001*mags\n returnerrs = False\n\n # filter the input times, mags, errs; do sigclipping and normalization\n find = npisfinite(times) & npisfinite(mags) & npisfinite(errs)\n ftimes, fmags, ferrs = times[find], mags[find], errs[find]\n\n # get the center value and stdev\n if meanormedian == 'median': # stddev = 1.483 x MAD\n\n center_mag = npmedian(fmags)\n stddev_mag = (npmedian(npabs(fmags - center_mag))) * 1.483\n\n elif meanormedian == 'mean':\n\n center_mag = npmean(fmags)\n stddev_mag = npstddev(fmags)\n\n else:\n LOGWARNING(\"unrecognized meanormedian value given to \"\n \"sigclip_magseries: %s, defaulting to 'median'\" %\n meanormedian)\n meanormedian = 'median'\n center_mag = npmedian(fmags)\n stddev_mag = (npmedian(npabs(fmags - center_mag))) * 1.483\n\n # sigclip next for a single sigclip value\n if sigclip and isinstance(sigclip, (float, int)):\n\n if not iterative and niterations is None:\n\n sigind = (npabs(fmags - center_mag)) < (sigclip * stddev_mag)\n\n stimes = ftimes[sigind]\n smags = fmags[sigind]\n serrs = ferrs[sigind]\n\n else:\n\n #\n # iterative version adapted from scipy.stats.sigmaclip\n #\n\n # First, if niterations is not set, iterate until covergence\n if niterations is None:\n\n delta = 1\n\n this_times = ftimes\n this_mags = fmags\n this_errs = ferrs\n\n while delta:\n\n if meanormedian == 'mean':\n this_center = npmean(this_mags)\n this_stdev = npstddev(this_mags)\n elif meanormedian == 'median':\n this_center = npmedian(this_mags)\n this_stdev = (\n npmedian(npabs(this_mags - this_center))\n ) * 1.483\n this_size = this_mags.size\n\n # apply the sigclip\n tsi = (\n (npabs(this_mags - this_center)) <\n (sigclip * this_stdev)\n )\n\n # update the arrays\n this_times = this_times[tsi]\n this_mags = this_mags[tsi]\n this_errs = this_errs[tsi]\n\n # update delta and go to the top of the loop\n delta = this_size - this_mags.size\n\n else: # If iterating only a certain number of times\n\n this_times = ftimes\n this_mags = fmags\n this_errs = ferrs\n\n iter_num = 0\n delta = 1\n while iter_num < niterations and delta:\n\n if meanormedian == 'mean':\n\n this_center = npmean(this_mags)\n this_stdev = npstddev(this_mags)\n\n elif meanormedian == 'median':\n\n this_center = npmedian(this_mags)\n this_stdev = (npmedian(npabs(this_mags -\n this_center))) * 1.483\n this_size = this_mags.size\n\n # apply the sigclip\n tsi = (\n (npabs(this_mags - this_center)) <\n (sigclip * this_stdev)\n )\n\n # update the arrays\n this_times = this_times[tsi]\n this_mags = this_mags[tsi]\n this_errs = this_errs[tsi]\n\n # update the number of iterations and delta and\n # go to the top of the loop\n delta = this_size - this_mags.size\n iter_num += 1\n\n # final sigclipped versions\n stimes, smags, serrs = this_times, this_mags, this_errs\n\n\n # this handles sigclipping for asymmetric +ve and -ve clip values\n elif sigclip and isinstance(sigclip, (list,tuple)) and len(sigclip) == 2:\n\n # sigclip is passed as [dimmingclip, brighteningclip]\n dimmingclip = sigclip[0]\n brighteningclip = sigclip[1]\n\n if not iterative and niterations is None:\n\n if magsarefluxes:\n nottoodimind = (\n (fmags - center_mag) > (-dimmingclip*stddev_mag)\n )\n nottoobrightind = (\n (fmags - center_mag) < (brighteningclip*stddev_mag)\n )\n else:\n nottoodimind = (\n (fmags - center_mag) < (dimmingclip*stddev_mag)\n )\n nottoobrightind = (\n (fmags - center_mag) > (-brighteningclip*stddev_mag)\n )\n\n sigind = nottoodimind & nottoobrightind\n\n stimes = ftimes[sigind]\n smags = fmags[sigind]\n serrs = ferrs[sigind]\n\n else:\n\n #\n # iterative version adapted from scipy.stats.sigmaclip\n #\n if niterations is None:\n\n delta = 1\n\n this_times = ftimes\n this_mags = fmags\n this_errs = ferrs\n\n while delta:\n\n if meanormedian == 'mean':\n\n this_center = npmean(this_mags)\n this_stdev = npstddev(this_mags)\n\n elif meanormedian == 'median':\n this_center = npmedian(this_mags)\n this_stdev = (npmedian(npabs(this_mags -\n this_center))) * 1.483\n this_size = this_mags.size\n\n if magsarefluxes:\n nottoodimind = (\n (this_mags - this_center) >\n (-dimmingclip*this_stdev)\n )\n nottoobrightind = (\n (this_mags - this_center) <\n (brighteningclip*this_stdev)\n )\n else:\n nottoodimind = (\n (this_mags - this_center) <\n (dimmingclip*this_stdev)\n )\n nottoobrightind = (\n (this_mags - this_center) >\n (-brighteningclip*this_stdev)\n )\n\n # apply the sigclip\n tsi = nottoodimind & nottoobrightind\n\n # update the arrays\n this_times = this_times[tsi]\n this_mags = this_mags[tsi]\n this_errs = this_errs[tsi]\n\n # update delta and go to top of the loop\n delta = this_size - this_mags.size\n\n else: # If iterating only a certain number of times\n this_times = ftimes\n this_mags = fmags\n this_errs = ferrs\n\n iter_num = 0\n delta = 1\n\n while iter_num < niterations and delta:\n\n if meanormedian == 'mean':\n this_center = npmean(this_mags)\n this_stdev = npstddev(this_mags)\n elif meanormedian == 'median':\n this_center = npmedian(this_mags)\n this_stdev = (npmedian(npabs(this_mags -\n this_center))) * 1.483\n this_size = this_mags.size\n\n if magsarefluxes:\n nottoodimind = (\n (this_mags - this_center) >\n (-dimmingclip*this_stdev)\n )\n nottoobrightind = (\n (this_mags - this_center) <\n (brighteningclip*this_stdev)\n )\n else:\n nottoodimind = (\n (this_mags - this_center) < (dimmingclip*this_stdev)\n )\n nottoobrightind = (\n (this_mags - this_center) >\n (-brighteningclip*this_stdev)\n )\n\n # apply the sigclip\n tsi = nottoodimind & nottoobrightind\n\n # update the arrays\n this_times = this_times[tsi]\n this_mags = this_mags[tsi]\n this_errs = this_errs[tsi]\n\n # update the number of iterations and delta\n # and go to top of the loop\n delta = this_size - this_mags.size\n iter_num += 1\n\n # final sigclipped versions\n stimes, smags, serrs = this_times, this_mags, this_errs\n\n else:\n\n stimes = ftimes\n smags = fmags\n serrs = ferrs\n\n if returnerrs:\n return stimes, smags, serrs\n else:\n return stimes, smags, None", "response": "This function takes a finite times magnitudes or fluxes and applies a symmetric or asymmetric sigma - clipping to the passed time - series and returns the resulting array."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef sigclip_magseries_with_extparams(times, mags, errs, extparams,\n sigclip=None,\n iterative=False,\n magsarefluxes=False):\n '''Sigma-clips a magnitude or flux time-series and associated measurement\n arrays.\n\n Selects the finite times, magnitudes (or fluxes), and errors from the passed\n values, and apply symmetric or asymmetric sigma clipping to them. Uses the\n same array indices as these values to filter out the values of all arrays in\n the `extparams` list. This can be useful for simultaneously sigma-clipping a\n magnitude/flux time-series along with their associated values of external\n parameters, such as telescope hour angle, zenith distance, temperature, moon\n phase, etc.\n\n Parameters\n ----------\n\n times,mags,errs : np.array\n The magnitude or flux time-series arrays to sigma-clip. This doesn't\n assume all values are finite or if they're positive/negative. All of\n these arrays will have their non-finite elements removed, and then will\n be sigma-clipped based on the arguments to this function.\n\n `errs` is optional. Set it to None if you don't have values for these. A\n 'faked' `errs` array will be generated if necessary, which can be\n ignored in the output as well.\n\n extparams : list of np.array\n This is a list of all external parameter arrays to simultaneously filter\n along with the magnitude/flux time-series. All of these arrays should\n have the same length as the `times`, `mags`, and `errs` arrays.\n\n sigclip : float or int or sequence of two floats/ints or None\n If a single float or int, a symmetric sigma-clip will be performed using\n the number provided as the sigma-multiplier to cut out from the input\n time-series.\n\n If a list of two ints/floats is provided, the function will perform an\n 'asymmetric' sigma-clip. The first element in this list is the sigma\n value to use for fainter flux/mag values; the second element in this\n list is the sigma value to use for brighter flux/mag values. For\n example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma\n dimmings and greater than 3-sigma brightenings. Here the meaning of\n \"dimming\" and \"brightening\" is set by *physics* (not the magnitude\n system), which is why the `magsarefluxes` kwarg must be correctly set.\n\n If `sigclip` is None, no sigma-clipping will be performed, and the\n time-series (with non-finite elems removed) will be passed through to\n the output.\n\n iterative : bool\n If this is set to True, will perform iterative sigma-clipping. If\n `niterations` is not set and this is True, sigma-clipping is iterated\n until no more points are removed.\n\n magsareflux : bool\n True if your \"mags\" are in fact fluxes, i.e. if \"fainter\" corresponds to\n `mags` getting smaller.\n\n Returns\n -------\n\n (stimes, smags, serrs) : tuple\n The sigma-clipped and nan-stripped time-series in `stimes`, `smags`,\n `serrs` and the associated values of the `extparams` in `sextparams`.\n\n '''\n\n returnerrs = True\n\n # fake the errors if they don't exist\n # this is inconsequential to sigma-clipping\n # we don't return these dummy values if the input errs are None\n if errs is None:\n # assume 0.1% errors if not given\n # this should work for mags and fluxes\n errs = 0.001*mags\n returnerrs = False\n\n # filter the input times, mags, errs; do sigclipping and normalization\n find = npisfinite(times) & npisfinite(mags) & npisfinite(errs)\n ftimes, fmags, ferrs = times[find], mags[find], errs[find]\n\n # apply the same indices to the external parameters\n for epi, eparr in enumerate(extparams):\n extparams[epi] = eparr[find]\n\n # get the median and stdev = 1.483 x MAD\n median_mag = npmedian(fmags)\n stddev_mag = (npmedian(npabs(fmags - median_mag))) * 1.483\n\n # sigclip next for a single sigclip value\n if sigclip and isinstance(sigclip, (float, int)):\n\n if not iterative:\n\n sigind = (npabs(fmags - median_mag)) < (sigclip * stddev_mag)\n\n stimes = ftimes[sigind]\n smags = fmags[sigind]\n serrs = ferrs[sigind]\n\n # apply the same indices to the external parameters\n for epi, eparr in enumerate(extparams):\n extparams[epi] = eparr[sigind]\n\n else:\n\n #\n # iterative version adapted from scipy.stats.sigmaclip\n #\n delta = 1\n\n this_times = ftimes\n this_mags = fmags\n this_errs = ferrs\n\n while delta:\n\n this_median = npmedian(this_mags)\n this_stdev = (npmedian(npabs(this_mags - this_median))) * 1.483\n this_size = this_mags.size\n\n # apply the sigclip\n tsi = (npabs(this_mags - this_median)) < (sigclip * this_stdev)\n\n # update the arrays\n this_times = this_times[tsi]\n this_mags = this_mags[tsi]\n this_errs = this_errs[tsi]\n\n # apply the same indices to the external parameters\n for epi, eparr in enumerate(extparams):\n extparams[epi] = eparr[tsi]\n\n # update delta and go to the top of the loop\n delta = this_size - this_mags.size\n\n # final sigclipped versions\n stimes, smags, serrs = this_times, this_mags, this_errs\n\n\n # this handles sigclipping for asymmetric +ve and -ve clip values\n elif sigclip and isinstance(sigclip, (list, tuple)) and len(sigclip) == 2:\n\n # sigclip is passed as [dimmingclip, brighteningclip]\n dimmingclip = sigclip[0]\n brighteningclip = sigclip[1]\n\n if not iterative:\n\n if magsarefluxes:\n nottoodimind = (\n (fmags - median_mag) > (-dimmingclip*stddev_mag)\n )\n nottoobrightind = (\n (fmags - median_mag) < (brighteningclip*stddev_mag)\n )\n else:\n nottoodimind = (\n (fmags - median_mag) < (dimmingclip*stddev_mag)\n )\n nottoobrightind = (\n (fmags - median_mag) > (-brighteningclip*stddev_mag)\n )\n\n sigind = nottoodimind & nottoobrightind\n\n stimes = ftimes[sigind]\n smags = fmags[sigind]\n serrs = ferrs[sigind]\n\n # apply the same indices to the external parameters\n for epi, eparr in enumerate(extparams):\n extparams[epi] = eparr[sigind]\n\n else:\n\n #\n # iterative version adapted from scipy.stats.sigmaclip\n #\n delta = 1\n\n this_times = ftimes\n this_mags = fmags\n this_errs = ferrs\n\n while delta:\n\n this_median = npmedian(this_mags)\n this_stdev = (npmedian(npabs(this_mags - this_median))) * 1.483\n this_size = this_mags.size\n\n if magsarefluxes:\n nottoodimind = (\n (this_mags - this_median) > (-dimmingclip*this_stdev)\n )\n nottoobrightind = (\n (this_mags - this_median) < (brighteningclip*this_stdev)\n )\n else:\n nottoodimind = (\n (this_mags - this_median) < (dimmingclip*this_stdev)\n )\n nottoobrightind = (\n (this_mags - this_median) >\n (-brighteningclip*this_stdev)\n )\n\n # apply the sigclip\n tsi = nottoodimind & nottoobrightind\n\n # update the arrays\n this_times = this_times[tsi]\n this_mags = this_mags[tsi]\n this_errs = this_errs[tsi]\n\n # apply the same indices to the external parameters\n for epi, eparr in enumerate(extparams):\n extparams[epi] = eparr[tsi]\n\n # update delta and go to top of the loop\n delta = this_size - this_mags.size\n\n # final sigclipped versions\n stimes, smags, serrs = this_times, this_mags, this_errs\n\n else:\n\n stimes = ftimes\n smags = fmags\n serrs = ferrs\n\n if returnerrs:\n return stimes, smags, serrs, extparams\n else:\n return stimes, smags, None, extparams", "response": "This function will take a finite time - series and a magnitude or flux time - series and an external parameter list and apply the sigma - clipping to those values."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef phase_magseries(times, mags, period, epoch, wrap=True, sort=True):\n '''Phases a magnitude/flux time-series using a given period and epoch.\n\n The equation used is::\n\n phase = (times - epoch)/period - floor((times - epoch)/period)\n\n This phases the given magnitude timeseries using the given period and\n epoch. If wrap is True, wraps the result around 0.0 (and returns an array\n that has twice the number of the original elements). If sort is True,\n returns the magnitude timeseries in phase sorted order.\n\n Parameters\n ----------\n\n times,mags : np.array\n The magnitude/flux time-series values to phase using the provided\n `period` and `epoch`. Non-fiinite values will be removed.\n\n period : float\n The period to use to phase the time-series.\n\n epoch : float\n The epoch to phase the time-series. This is usually the time-of-minimum\n or time-of-maximum of some periodic light curve\n phenomenon. Alternatively, one can use the minimum time value in\n `times`.\n\n wrap : bool\n If this is True, the returned phased time-series will be wrapped around\n phase 0.0, which is useful for plotting purposes. The arrays returned\n will have twice the number of input elements because of this wrapping.\n\n sort : bool\n If this is True, the returned phased time-series will be sorted in\n increasing phase order.\n\n Returns\n -------\n\n dict\n A dict of the following form is returned::\n\n {'phase': the phase values,\n 'mags': the mags/flux values at each phase,\n 'period': the input `period` used to phase the time-series,\n 'epoch': the input `epoch` used to phase the time-series}\n\n '''\n\n # find all the finite values of the magnitudes and times\n finiteind = np.isfinite(mags) & np.isfinite(times)\n\n finite_times = times[finiteind]\n finite_mags = mags[finiteind]\n\n magseries_phase = (\n (finite_times - epoch)/period -\n np.floor(((finite_times - epoch)/period))\n )\n\n outdict = {'phase':magseries_phase,\n 'mags':finite_mags,\n 'period':period,\n 'epoch':epoch}\n\n if sort:\n sortorder = np.argsort(outdict['phase'])\n outdict['phase'] = outdict['phase'][sortorder]\n outdict['mags'] = outdict['mags'][sortorder]\n\n if wrap:\n outdict['phase'] = np.concatenate((outdict['phase']-1.0,\n outdict['phase']))\n outdict['mags'] = np.concatenate((outdict['mags'],\n outdict['mags']))\n\n return outdict", "response": "Phases a magnitude time - series using a given period and epoch."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef time_bin_magseries(times, mags,\n binsize=540.0,\n minbinelems=7):\n '''Bins the given mag/flux time-series in time using the bin size given.\n\n Parameters\n ----------\n\n times,mags : np.array\n The magnitude/flux time-series to bin in time. Non-finite elements will\n be removed from these arrays. At least 10 elements in each array are\n required for this function to operate.\n\n binsize : float\n The bin size to use to group together measurements closer than this\n amount in time. This is in seconds.\n\n minbinelems : int\n The minimum number of elements required per bin to include it in the\n output.\n\n Returns\n -------\n\n dict\n A dict of the following form is returned::\n\n {'jdbin_indices': a list of the index arrays into the nan-filtered\n input arrays per each bin,\n 'jdbins': list of bin boundaries for each bin,\n 'nbins': the number of bins generated,\n 'binnedtimes': the time values associated with each time bin;\n this is the median of the times in each bin,\n 'binnedmags': the mag/flux values associated with each time bin;\n this is the median of the mags/fluxes in each bin}\n\n '''\n\n # check if the input arrays are ok\n if not(times.shape and mags.shape and len(times) > 9 and len(mags) > 9):\n\n LOGERROR(\"input time/mag arrays don't have enough elements\")\n return\n\n # find all the finite values of the magnitudes and times\n finiteind = np.isfinite(mags) & np.isfinite(times)\n finite_times = times[finiteind]\n finite_mags = mags[finiteind]\n\n # convert binsize in seconds to JD units\n binsizejd = binsize/(86400.0)\n nbins = int(np.ceil((np.nanmax(finite_times) -\n np.nanmin(finite_times))/binsizejd) + 1)\n\n minjd = np.nanmin(finite_times)\n jdbins = [(minjd + x*binsizejd) for x in range(nbins)]\n\n # make a KD-tree on the JDs so we can do fast distance calculations. we\n # need to add a bogus y coord to make this a problem that KD-trees can\n # solve.\n time_coords = np.array([[x,1.0] for x in finite_times])\n jdtree = cKDTree(time_coords)\n binned_finite_timeseries_indices = []\n\n collected_binned_mags = {}\n\n for jd in jdbins:\n # find all bin indices close to within binsizejd of this point\n # using the cKDTree query. we use the p-norm = 1 (I think this\n # means straight-up pairwise distance? FIXME: check this)\n bin_indices = jdtree.query_ball_point(np.array([jd,1.0]),\n binsizejd/2.0, p=1.0)\n\n # if the bin_indices have already been collected, then we're\n # done with this bin, move to the next one. if they haven't,\n # then this is the start of a new bin.\n if (bin_indices not in binned_finite_timeseries_indices and\n len(bin_indices) >= minbinelems):\n\n binned_finite_timeseries_indices.append(bin_indices)\n\n # convert to ndarrays\n binned_finite_timeseries_indices = [np.array(x) for x in\n binned_finite_timeseries_indices]\n\n collected_binned_mags['jdbins_indices'] = binned_finite_timeseries_indices\n collected_binned_mags['jdbins'] = jdbins\n collected_binned_mags['nbins'] = len(binned_finite_timeseries_indices)\n\n # collect the finite_times\n binned_jd = np.array([np.median(finite_times[x])\n for x in binned_finite_timeseries_indices])\n collected_binned_mags['binnedtimes'] = binned_jd\n collected_binned_mags['binsize'] = binsize\n\n # median bin the magnitudes according to the calculated indices\n collected_binned_mags['binnedmags'] = (\n np.array([np.median(finite_mags[x])\n for x in binned_finite_timeseries_indices])\n )\n\n return collected_binned_mags", "response": "Returns a dictionary of the time - series in time using the given bin size given."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef phase_bin_magseries(phases, mags,\n binsize=0.005,\n minbinelems=7):\n '''Bins a phased magnitude/flux time-series using the bin size provided.\n\n Parameters\n ----------\n\n phases,mags : np.array\n The phased magnitude/flux time-series to bin in phase. Non-finite\n elements will be removed from these arrays. At least 10 elements in each\n array are required for this function to operate.\n\n binsize : float\n The bin size to use to group together measurements closer than this\n amount in phase. This is in units of phase.\n\n minbinelems : int\n The minimum number of elements required per bin to include it in the\n output.\n\n Returns\n -------\n\n dict\n A dict of the following form is returned::\n\n {'phasebin_indices': a list of the index arrays into the\n nan-filtered input arrays per each bin,\n 'phasebins': list of bin boundaries for each bin,\n 'nbins': the number of bins generated,\n 'binnedphases': the phase values associated with each phase bin;\n this is the median of the phase value in each bin,\n 'binnedmags': the mag/flux values associated with each phase bin;\n this is the median of the mags/fluxes in each bin}\n\n '''\n\n # check if the input arrays are ok\n if not(phases.shape and mags.shape and len(phases) > 10 and len(mags) > 10):\n\n LOGERROR(\"input time/mag arrays don't have enough elements\")\n return\n\n # find all the finite values of the magnitudes and phases\n finiteind = np.isfinite(mags) & np.isfinite(phases)\n finite_phases = phases[finiteind]\n finite_mags = mags[finiteind]\n\n nbins = int(np.ceil((np.nanmax(finite_phases) -\n np.nanmin(finite_phases))/binsize) + 1)\n\n minphase = np.nanmin(finite_phases)\n phasebins = [(minphase + x*binsize) for x in range(nbins)]\n\n # make a KD-tree on the PHASEs so we can do fast distance calculations. we\n # need to add a bogus y coord to make this a problem that KD-trees can\n # solve.\n time_coords = np.array([[x,1.0] for x in finite_phases])\n phasetree = cKDTree(time_coords)\n binned_finite_phaseseries_indices = []\n\n collected_binned_mags = {}\n\n for phase in phasebins:\n\n # find all bin indices close to within binsize of this point using the\n # cKDTree query. we use the p-norm = 1 for pairwise Euclidean distance.\n bin_indices = phasetree.query_ball_point(np.array([phase,1.0]),\n binsize/2.0, p=1.0)\n\n # if the bin_indices have already been collected, then we're\n # done with this bin, move to the next one. if they haven't,\n # then this is the start of a new bin.\n if (bin_indices not in binned_finite_phaseseries_indices and\n len(bin_indices) >= minbinelems):\n\n binned_finite_phaseseries_indices.append(bin_indices)\n\n # convert to ndarrays\n binned_finite_phaseseries_indices = [np.array(x) for x in\n binned_finite_phaseseries_indices]\n\n collected_binned_mags['phasebins_indices'] = (\n binned_finite_phaseseries_indices\n )\n collected_binned_mags['phasebins'] = phasebins\n collected_binned_mags['nbins'] = len(binned_finite_phaseseries_indices)\n\n # collect the finite_phases\n binned_phase = np.array([np.median(finite_phases[x])\n for x in binned_finite_phaseseries_indices])\n collected_binned_mags['binnedphases'] = binned_phase\n collected_binned_mags['binsize'] = binsize\n\n # median bin the magnitudes according to the calculated indices\n collected_binned_mags['binnedmags'] = (\n np.array([np.median(finite_mags[x])\n for x in binned_finite_phaseseries_indices])\n )\n\n return collected_binned_mags", "response": "This function returns a dict of the phase - bin data for a set of phases and mags."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncalculate the total SNR of a transit assuming gaussian uncertainties.", "response": "def get_snr_of_dip(times,\n mags,\n modeltimes,\n modelmags,\n atol_normalization=1e-8,\n indsforrms=None,\n magsarefluxes=False,\n verbose=True,\n transitdepth=None,\n npoints_in_transit=None):\n '''Calculate the total SNR of a transit assuming gaussian uncertainties.\n\n `modelmags` gets interpolated onto the cadence of `mags`. The noise is\n calculated as the 1-sigma std deviation of the residual (see below).\n\n Following Carter et al. 2009::\n\n Q = sqrt( \u0393 T ) * \u03b4 / \u03c3\n\n for Q the total SNR of the transit in the r->0 limit, where::\n\n r = Rp/Rstar,\n T = transit duration,\n \u03b4 = transit depth,\n \u03c3 = RMS of the lightcurve in transit.\n \u0393 = sampling rate\n\n Thus \u0393 * T is roughly the number of points obtained during transit.\n (This doesn't correctly account for the SNR during ingress/egress, but this\n is a second-order correction).\n\n Note this is the same total SNR as described by e.g., Kovacs et al. 2002,\n their Equation 11.\n\n NOTE: this only works with fluxes at the moment.\n\n Parameters\n ----------\n\n times,mags : np.array\n The input flux time-series to process.\n\n modeltimes,modelmags : np.array\n A transiting planet model, either from BLS, a trapezoid model, or a\n Mandel-Agol model.\n\n atol_normalization : float\n The absolute tolerance to which the median of the passed model fluxes\n must be equal to 1.\n\n indsforrms : np.array\n A array of bools of `len(mags)` used to select points for the RMS\n measurement. If not passed, the RMS of the entire passed timeseries is\n used as an approximation. Genearlly, it's best to use out of transit\n points, so the RMS measurement is not model-dependent.\n\n magsarefluxes : bool\n Currently forced to be True because this function only works with\n fluxes.\n\n verbose : bool\n If True, indicates progress and warns about problems.\n\n transitdepth : float or None\n If the transit depth is known, pass it in here. Otherwise, it is\n calculated assuming OOT flux is 1.\n\n npoints_in_transits : int or None\n If the number of points in transit is known, pass it in here. Otherwise,\n the function will guess at this value.\n\n Returns\n -------\n\n (snr, transit_depth, noise) : tuple\n The returned tuple contains the calculated SNR, transit depth, and noise\n of the residual lightcurve calculated using the relation described\n above.\n\n '''\n\n if magsarefluxes:\n if not np.isclose(np.nanmedian(modelmags), 1, atol=atol_normalization):\n raise AssertionError('snr calculation assumes modelmags are '\n 'median-normalized')\n else:\n raise NotImplementedError(\n 'need to implement a method for identifying in-transit points when'\n 'mags are mags, and not fluxes'\n )\n\n if not transitdepth:\n # calculate transit depth from whatever model magnitudes are passed.\n transitdepth = np.abs(np.max(modelmags) - np.min(modelmags))\n\n # generally, mags (data) and modelmags are at different cadence.\n # interpolate modelmags onto the cadence of mags.\n if not len(mags) == len(modelmags):\n from scipy.interpolate import interp1d\n\n fn = interp1d(modeltimes, modelmags, kind='cubic', bounds_error=True,\n fill_value=np.nan)\n\n modelmags = fn(times)\n\n if verbose:\n LOGINFO('interpolated model timeseries onto the data timeseries')\n\n subtractedmags = mags - modelmags\n\n if isinstance(indsforrms, np.ndarray):\n subtractedrms = np.std(subtractedmags[indsforrms])\n if verbose:\n LOGINFO('using selected points to measure RMS')\n else:\n subtractedrms = np.std(subtractedmags)\n if verbose:\n LOGINFO('using all points to measure RMS')\n\n def _get_npoints_in_transit(modelmags):\n # assumes median-normalized fluxes are input\n if np.nanmedian(modelmags) == 1:\n return len(modelmags[(modelmags != 1)])\n else:\n raise NotImplementedError\n\n if not npoints_in_transit:\n npoints_in_transit = _get_npoints_in_transit(modelmags)\n\n snr = np.sqrt(npoints_in_transit) * transitdepth/subtractedrms\n\n if verbose:\n\n LOGINFO('\\npoints in transit: {:d}'.format(npoints_in_transit) +\n '\\ndepth: {:.2e}'.format(transitdepth) +\n '\\nrms in residual: {:.2e}'.format(subtractedrms) +\n '\\n\\t SNR: {:.2e}'.format(snr))\n\n return snr, transitdepth, subtractedrms"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef estimate_achievable_tmid_precision(snr, t_ingress_min=10,\n t_duration_hr=2.14):\n '''Using Carter et al. 2009's estimate, calculate the theoretical optimal\n precision on mid-transit time measurement possible given a transit of a\n particular SNR.\n\n The relation used is::\n\n sigma_tc = Q^{-1} * T * sqrt(\u03b8/2)\n\n Q = SNR of the transit.\n T = transit duration, which is 2.14 hours from discovery paper.\n \u03b8 = \u03c4/T = ratio of ingress to total duration\n ~= (few minutes [guess]) / 2.14 hours\n\n Parameters\n ----------\n\n snr : float\n The measured signal-to-noise of the transit, e,g. from\n :py:func:`astrobase.periodbase.kbls.bls_stats_singleperiod` or from\n running the `.compute_stats()` method on an Astropy BoxLeastSquares\n object.\n\n t_ingress_min : float\n The ingress duration in minutes. This is t_I to t_II in Winn (2010)\n nomenclature.\n\n t_duration_hr : float\n The transit duration in hours. This is t_I to t_IV in Winn (2010)\n nomenclature.\n\n Returns\n -------\n\n float\n Returns the precision achievable for transit-center time as calculated\n from the relation above. This is in days.\n\n '''\n\n t_ingress = t_ingress_min*u.minute\n t_duration = t_duration_hr*u.hour\n\n theta = t_ingress/t_duration\n\n sigma_tc = (1/snr * t_duration * np.sqrt(theta/2))\n\n LOGINFO('assuming t_ingress = {:.1f}'.format(t_ingress))\n LOGINFO('assuming t_duration = {:.1f}'.format(t_duration))\n LOGINFO('measured SNR={:.2f}\\n\\t'.format(snr) +\n '-->theoretical sigma_tc = {:.2e} = {:.2e} = {:.2e}'.format(\n sigma_tc.to(u.minute), sigma_tc.to(u.hour), sigma_tc.to(u.day)))\n\n return sigma_tc.to(u.day).value", "response": "Estimate the theoretical optimal base time measurement possible given a transit of a\n and a particular SNR."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngives a BLS period, epoch, and transit ingress/egress points (usually from :py:func:`astrobase.periodbase.kbls.bls_stats_singleperiod`), return the times within transit durations + `extra_maskfrac` of each transit. Optionally, can use the (more accurate) trapezoidal fit period and epoch, if it's passed. Useful for inspecting individual transits, and masking them out if desired. Parameters ---------- blsd : dict This is the dict returned by :py:func:`astrobase.periodbase.kbls.bls_stats_singleperiod`. time : np.array The times from the time-series of transit observations used to calculate the initial period. extra_maskfrac : float This is the separation from in-transit points you desire, in units of the transit duration. `extra_maskfrac = 0` if you just want points inside transit (see below). trapd : dict This is a dict returned by :py:func:`astrobase.lcfit.transits.traptransit_fit_magseries` containing the trapezoid transit model. nperiodint : int This indicates how many periods backwards/forwards to try and identify transits from the epochs reported in `blsd` or `trapd`. Returns ------- (tmids_obsd, t_starts, t_ends) : tuple of np.array The returned items are:: tmids_obsd (np.ndarray): best guess of transit midtimes in lightcurve. Has length number of transits in lightcurve. t_starts (np.ndarray): t_Is - extra_maskfrac*tdur, for t_Is transit first contact point. t_ends (np.ndarray): t_Is + extra_maskfrac*tdur, for t_Is transit first contact point.", "response": "def get_transit_times(\n blsd,\n time,\n extra_maskfrac,\n trapd=None,\n nperiodint=1000\n):\n '''Given a BLS period, epoch, and transit ingress/egress points (usually\n from :py:func:`astrobase.periodbase.kbls.bls_stats_singleperiod`), return\n the times within transit durations + `extra_maskfrac` of each transit.\n\n Optionally, can use the (more accurate) trapezoidal fit period and epoch, if\n it's passed. Useful for inspecting individual transits, and masking them\n out if desired.\n\n Parameters\n ----------\n\n blsd : dict\n This is the dict returned by\n :py:func:`astrobase.periodbase.kbls.bls_stats_singleperiod`.\n\n time : np.array\n The times from the time-series of transit observations used to calculate\n the initial period.\n\n extra_maskfrac : float\n This is the separation from in-transit points you desire, in units of\n the transit duration. `extra_maskfrac = 0` if you just want points\n inside transit (see below).\n\n trapd : dict\n This is a dict returned by\n :py:func:`astrobase.lcfit.transits.traptransit_fit_magseries` containing\n the trapezoid transit model.\n\n nperiodint : int\n This indicates how many periods backwards/forwards to try and identify\n transits from the epochs reported in `blsd` or `trapd`.\n\n Returns\n -------\n\n (tmids_obsd, t_starts, t_ends) : tuple of np.array\n The returned items are::\n\n tmids_obsd (np.ndarray): best guess of transit midtimes in\n lightcurve. Has length number of transits in lightcurve.\n\n t_starts (np.ndarray): t_Is - extra_maskfrac*tdur, for t_Is transit\n first contact point.\n\n t_ends (np.ndarray): t_Is + extra_maskfrac*tdur, for t_Is transit\n first contact point.\n\n '''\n\n if trapd:\n period = trapd['fitinfo']['finalparams'][0]\n t0 = trapd['fitinfo']['fitepoch']\n transitduration_phase = trapd['fitinfo']['finalparams'][3]\n tdur = period * transitduration_phase\n else:\n period = blsd['period']\n t0 = blsd['epoch']\n tdur = (\n period *\n (blsd['transegressbin']-blsd['transingressbin'])/blsd['nphasebins']\n )\n if not blsd['transegressbin'] > blsd['transingressbin']:\n\n raise NotImplementedError(\n 'careful of the width. '\n 'this edge case must be dealt with separately.'\n )\n\n tmids = [t0 + ix*period for ix in range(-nperiodint,nperiodint)]\n\n sel = (tmids > np.nanmin(time)) & (tmids < np.nanmax(time))\n tmids_obsd = np.array(tmids)[sel]\n\n t_Is = tmids_obsd - tdur/2\n t_IVs = tmids_obsd + tdur/2\n\n # focus on the times around transit\n t_starts = t_Is - extra_maskfrac * tdur\n t_ends = t_IVs + extra_maskfrac * tdur\n\n return tmids_obsd, t_starts, t_ends"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngets the transit start, middle, and end times for transits in a given time-series of observations. Parameters ---------- time,flux,err_flux : np.array The input flux time-series measurements and their associated measurement errors blsfit_savpath : str or None If provided as a str, indicates the path of the fit plot to make for a simple BLS model fit to the transit using the obtained period and epoch. trapfit_savpath : str or None If provided as a str, indicates the path of the fit plot to make for a trapezoidal transit model fit to the transit using the obtained period and epoch. sigclip : float or int or sequence of two floats/ints or None If a single float or int, a symmetric sigma-clip will be performed using the number provided as the sigma-multiplier to cut out from the input time-series. If a list of two ints/floats is provided, the function will perform an 'asymmetric' sigma-clip. The first element in this list is the sigma value to use for fainter flux/mag values; the second element in this list is the sigma value to use for brighter flux/mag values. For example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma dimmings and greater than 3-sigma brightenings. Here the meaning of \"dimming\" and \"brightening\" is set by *physics* (not the magnitude system), which is why the `magsarefluxes` kwarg must be correctly set. If `sigclip` is None, no sigma-clipping will be performed, and the time-series (with non-finite elems removed) will be passed through to the output. magsarefluxes : bool This is by default True for this function, since it works on fluxes only at the moment. nworkers : int The number of parallel BLS period-finder workers to use. extra_maskfrac : float This is the separation (N) from in-transit points you desire, in units of the transit duration. `extra_maskfrac = 0` if you just want points inside transit, otherwise:: t_starts = t_Is - N*tdur, t_ends = t_IVs + N*tdur Thus setting N=0.03 masks slightly more than the guessed transit duration. Returns ------- (tmids_obsd, t_starts, t_ends) : tuple The returned items are:: tmids_obsd (np.ndarray): best guess of transit midtimes in lightcurve. Has length number of transits in lightcurve. t_starts (np.ndarray): t_Is - extra_maskfrac*tdur, for t_Is transit first contact point. t_ends (np.ndarray): t_Is + extra_maskfrac*tdur, for t_Is transit first contact point.", "response": "def given_lc_get_transit_tmids_tstarts_tends(\n time,\n flux,\n err_flux,\n blsfit_savpath=None,\n trapfit_savpath=None,\n magsarefluxes=True,\n nworkers=1,\n sigclip=None,\n extra_maskfrac=0.03\n):\n '''Gets the transit start, middle, and end times for transits in a given\n time-series of observations.\n\n Parameters\n ----------\n\n time,flux,err_flux : np.array\n The input flux time-series measurements and their associated measurement\n errors\n\n blsfit_savpath : str or None\n If provided as a str, indicates the path of the fit plot to make for a\n simple BLS model fit to the transit using the obtained period and epoch.\n\n trapfit_savpath : str or None\n If provided as a str, indicates the path of the fit plot to make for a\n trapezoidal transit model fit to the transit using the obtained period\n and epoch.\n\n sigclip : float or int or sequence of two floats/ints or None\n If a single float or int, a symmetric sigma-clip will be performed using\n the number provided as the sigma-multiplier to cut out from the input\n time-series.\n\n If a list of two ints/floats is provided, the function will perform an\n 'asymmetric' sigma-clip. The first element in this list is the sigma\n value to use for fainter flux/mag values; the second element in this\n list is the sigma value to use for brighter flux/mag values. For\n example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma\n dimmings and greater than 3-sigma brightenings. Here the meaning of\n \"dimming\" and \"brightening\" is set by *physics* (not the magnitude\n system), which is why the `magsarefluxes` kwarg must be correctly set.\n\n If `sigclip` is None, no sigma-clipping will be performed, and the\n time-series (with non-finite elems removed) will be passed through to\n the output.\n\n magsarefluxes : bool\n This is by default True for this function, since it works on fluxes only\n at the moment.\n\n nworkers : int\n The number of parallel BLS period-finder workers to use.\n\n extra_maskfrac : float\n This is the separation (N) from in-transit points you desire, in units\n of the transit duration. `extra_maskfrac = 0` if you just want points\n inside transit, otherwise::\n\n t_starts = t_Is - N*tdur, t_ends = t_IVs + N*tdur\n\n Thus setting N=0.03 masks slightly more than the guessed transit\n duration.\n\n Returns\n -------\n\n (tmids_obsd, t_starts, t_ends) : tuple\n The returned items are::\n\n tmids_obsd (np.ndarray): best guess of transit midtimes in\n lightcurve. Has length number of transits in lightcurve.\n\n t_starts (np.ndarray): t_Is - extra_maskfrac*tdur, for t_Is transit\n first contact point.\n\n t_ends (np.ndarray): t_Is + extra_maskfrac*tdur, for t_Is transit\n first contact point.\n\n '''\n\n # first, run BLS to get an initial epoch and period.\n endp = 1.05*(np.nanmax(time) - np.nanmin(time))/2\n\n blsdict = kbls.bls_parallel_pfind(time, flux, err_flux,\n magsarefluxes=magsarefluxes, startp=0.1,\n endp=endp, maxtransitduration=0.3,\n nworkers=nworkers, sigclip=sigclip)\n\n blsd = kbls.bls_stats_singleperiod(time, flux, err_flux,\n blsdict['bestperiod'],\n magsarefluxes=True, sigclip=sigclip,\n perioddeltapercent=5)\n # plot the BLS model.\n if blsfit_savpath:\n make_fit_plot(blsd['phases'], blsd['phasedmags'], None,\n blsd['blsmodel'], blsd['period'], blsd['epoch'],\n blsd['epoch'], blsfit_savpath,\n magsarefluxes=magsarefluxes)\n\n ingduration_guess = blsd['transitduration'] * 0.2 # a guesstimate.\n transitparams = [\n blsd['period'], blsd['epoch'], blsd['transitdepth'],\n blsd['transitduration'], ingduration_guess\n ]\n\n # fit a trapezoidal transit model; plot the resulting phased LC.\n if trapfit_savpath:\n trapd = traptransit_fit_magseries(time, flux, err_flux,\n transitparams,\n magsarefluxes=magsarefluxes,\n sigclip=sigclip,\n plotfit=trapfit_savpath)\n\n # use the trapezoidal model's epoch as the guess to identify (roughly) in\n # and out of transit points\n tmids, t_starts, t_ends = get_transit_times(blsd,\n time,\n extra_maskfrac,\n trapd=trapd)\n\n return tmids, t_starts, t_ends"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _pycompress_sqlitecurve(sqlitecurve, force=False):\n '''This just compresses the sqlitecurve. Should be independent of OS.\n\n '''\n\n outfile = '%s.gz' % sqlitecurve\n\n try:\n\n if os.path.exists(outfile) and not force:\n os.remove(sqlitecurve)\n return outfile\n\n else:\n\n with open(sqlitecurve,'rb') as infd:\n with gzip.open(outfile,'wb') as outfd:\n shutil.copyfileobj(infd, outfd)\n\n if os.path.exists(outfile):\n os.remove(sqlitecurve)\n return outfile\n\n except Exception as e:\n return None", "response": "This function compresses the sqlitecurve file and returns the filename of the compressed file."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _pyuncompress_sqlitecurve(sqlitecurve, force=False):\n '''This just uncompresses the sqlitecurve. Should be independent of OS.\n\n '''\n\n outfile = sqlitecurve.replace('.gz','')\n\n try:\n\n if os.path.exists(outfile) and not force:\n return outfile\n\n else:\n\n with gzip.open(sqlitecurve,'rb') as infd:\n with open(outfile,'wb') as outfd:\n shutil.copyfileobj(infd, outfd)\n\n # do not remove the intput file yet\n if os.path.exists(outfile):\n return outfile\n\n except Exception as e:\n return None", "response": "This just uncompresses the sqlitecurve. Should be independent of OS."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _parse_csv_header_lcc_csv_v1(headerlines):\n '''\n This parses the header of the LCC CSV V1 LC format.\n\n '''\n\n # the first three lines indicate the format name, comment char, separator\n commentchar = headerlines[1]\n separator = headerlines[2]\n\n headerlines = [x.lstrip('%s ' % commentchar) for x in headerlines[3:]]\n\n # next, find the indices of the various LC sections\n metadatastart = headerlines.index('OBJECT METADATA')\n columnstart = headerlines.index('COLUMN DEFINITIONS')\n lcstart = headerlines.index('LIGHTCURVE')\n\n metadata = ' ' .join(headerlines[metadatastart+1:columnstart-1])\n columns = ' ' .join(headerlines[columnstart+1:lcstart-1])\n metadata = json.loads(metadata)\n columns = json.loads(columns)\n\n return metadata, columns, separator", "response": "This parses the header of the LCC CSV V1 LC format."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef read_csvlc(lcfile):\n '''This reads a HAT data server or LCC-Server produced CSV light curve\n into an lcdict.\n\n This will automatically figure out the format of the file\n provided. Currently, it can read:\n\n - legacy HAT data server CSV LCs (e.g. from\n https://hatsouth.org/planets/lightcurves.html) with an extension of the\n form: `.hatlc.csv.gz`.\n - all LCC-Server produced LCC-CSV-V1 LCs (e.g. from\n https://data.hatsurveys.org) with an extension of the form: `-csvlc.gz`.\n\n\n Parameters\n ----------\n\n lcfile : str\n The light curve file to read.\n\n Returns\n -------\n\n dict\n Returns an lcdict that can be read and used by many astrobase processing\n functions.\n\n '''\n\n # read in the file and split by lines\n if '.gz' in os.path.basename(lcfile):\n LOGINFO('reading gzipped HATLC: %s' % lcfile)\n infd = gzip.open(lcfile,'rb')\n else:\n LOGINFO('reading HATLC: %s' % lcfile)\n infd = open(lcfile,'rb')\n\n\n # this transparently reads LCC CSVLCs\n lcformat_check = infd.read(12).decode()\n if 'LCC-CSVLC' in lcformat_check:\n infd.close()\n return read_lcc_csvlc(lcfile)\n else:\n infd.seek(0)\n\n # below is reading the HATLC v2 CSV LCs\n\n lctext = infd.read().decode() # argh Python 3\n infd.close()\n\n # figure out the header and get the LC columns\n lcstart = lctext.index('# LIGHTCURVE\\n')\n lcheader = lctext[:lcstart+12]\n lccolumns = lctext[lcstart+13:].split('\\n')\n lccolumns = [x for x in lccolumns if len(x) > 0]\n\n # initialize the lcdict and parse the CSV header\n lcdict = _parse_csv_header(lcheader)\n\n # tranpose the LC rows into columns\n lccolumns = [x.split(',') for x in lccolumns]\n lccolumns = list(zip(*lccolumns)) # argh more Python 3\n\n # write the columns to the dict\n for colind, col in enumerate(lcdict['columns']):\n\n if (col.split('_')[0] in LC_MAG_COLUMNS or\n col.split('_')[0] in LC_ERR_COLUMNS or\n col.split('_')[0] in LC_FLAG_COLUMNS):\n lcdict[col] = np.array([_smartcast(x,\n COLUMNDEFS[col.split('_')[0]][2])\n for x in lccolumns[colind]])\n\n elif col in COLUMNDEFS:\n lcdict[col] = np.array([_smartcast(x,COLUMNDEFS[col][2])\n for x in lccolumns[colind]])\n\n else:\n LOGWARNING('lcdict col %s has no formatter available' % col)\n continue\n\n return lcdict", "response": "This function reads a HAT - L1 or LCC - CSV - V1 light curve file into a lcdict."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef find_lc_timegroups(lctimes, mingap=4.0):\n '''This finds the time gaps in the light curve, so we can figure out which\n times are for consecutive observations and which represent gaps\n between seasons.\n\n Parameters\n ----------\n\n lctimes : np.array\n This is the input array of times, assumed to be in some form of JD.\n\n mingap : float\n This defines how much the difference between consecutive measurements is\n allowed to be to consider them as parts of different timegroups. By\n default it is set to 4.0 days.\n\n Returns\n -------\n\n tuple\n A tuple of the form below is returned, containing the number of time\n groups found and Python slice objects for each group::\n\n (ngroups, [slice(start_ind_1, end_ind_1), ...])\n\n '''\n\n lc_time_diffs = [(lctimes[x] - lctimes[x-1]) for x in range(1,len(lctimes))]\n lc_time_diffs = np.array(lc_time_diffs)\n\n group_start_indices = np.where(lc_time_diffs > mingap)[0]\n\n if len(group_start_indices) > 0:\n\n group_indices = []\n\n for i, gindex in enumerate(group_start_indices):\n\n if i == 0:\n group_indices.append(slice(0,gindex+1))\n else:\n group_indices.append(slice(group_start_indices[i-1]+1,gindex+1))\n\n\n # at the end, add the slice for the last group to the end of the times\n # array\n group_indices.append(slice(group_start_indices[-1]+1,len(lctimes)))\n\n # if there's no large gap in the LC, then there's only one group to worry\n # about\n else:\n group_indices = [slice(0,len(lctimes))]\n\n\n return len(group_indices), group_indices", "response": "This function finds the time gaps in the light curve so we can figure out which time gaps are for consecutive observations and which represent gaps between consecutive measurements."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef normalize_lcdict_byinst(\n lcdict,\n magcols='all',\n normto='sdssr',\n normkeylist=('stf','ccd','flt','fld','prj','exp'),\n debugmode=False,\n quiet=False\n):\n '''This is a function to normalize light curves across all instrument\n combinations present.\n\n Use this to normalize a light curve containing a variety of:\n\n - HAT station IDs ('stf')\n - camera IDs ('ccd')\n - filters ('flt')\n - observed field names ('fld')\n - HAT project IDs ('prj')\n - exposure times ('exp')\n\n Parameters\n ----------\n\n lcdict : dict\n The input lcdict to process.\n\n magcols : 'all' or list of str\n If this is 'all', all of the columns in the lcdict that are indicated to\n be magnitude measurement columns are normalized. If this is a list of\n str, must contain the keys of the lcdict specifying which magnitude\n columns will be normalized.\n\n normto : {'zero', 'jmag', 'hmag', 'kmag', 'bmag', 'vmag', 'sdssg', 'sdssr', 'sdssi'}\n This indicates which column will be the normalization target. If this is\n 'zero', will normalize to 0.0 for each LC column. Otherwise, will\n normalize to the value of one of the other keys in the\n lcdict['objectinfo'][magkey], meaning the normalization will be to some\n form of catalog magnitude.\n\n normkeylist : list of str\n These are the column keys to use to form the normalization\n index. Measurements in the specified `magcols` with identical\n normalization index values will be considered as part of a single\n measurement 'era', and will be normalized to zero. Once all eras have\n been normalized this way, the final light curve will be re-normalized as\n specified in `normto`.\n\n debugmode : bool\n If True, will indicate progress as time-groups are found and processed.\n\n quiet : bool\n If True, will not emit any messages when processing.\n\n Returns\n -------\n\n dict\n Returns the lcdict with the magnitude measurements normalized as\n specified. The normalization happens IN PLACE.\n\n '''\n\n # check if this lc has been normalized already. return as-is if so\n if 'lcinstnormcols' in lcdict and len(lcdict['lcinstnormcols']) > 0:\n if not quiet:\n LOGWARNING('this lightcurve is already '\n 'normalized by instrument keys, '\n 'returning...')\n return lcdict\n\n # generate the normalization key\n normkeycols = []\n availablenormkeys = []\n for key in normkeylist:\n if key in lcdict and lcdict[key] is not None:\n normkeycols.append(lcdict[key])\n availablenormkeys.append(key)\n\n # transpose to turn these into rows\n normkeycols = list(zip(*normkeycols))\n\n # convert to a string rep for each key and post-process for simplicity\n allkeys = [repr(x) for x in normkeycols]\n allkeys = [a.replace('(','').replace(')','').replace(\"'\",'').replace(' ','')\n for a in allkeys]\n\n # turn these into a numpy array and get the unique values\n allkeys = np.array(allkeys)\n normkeys = np.unique(allkeys)\n\n # figure out the apertures\n # HATLC V2 format\n if 'lcapertures' in lcdict:\n apertures = sorted(lcdict['lcapertures'].keys())\n # LCC-CSV-V1 format HATLC\n elif 'objectinfo' in lcdict and 'lcapertures' in lcdict['objectinfo']:\n apertures = sorted(lcdict['objectinfo']['lcapertures'].keys())\n\n # put together the column names\n aimcols = [('aim_%s' % x) for x in apertures if ('aim_%s' % x) in lcdict]\n armcols = [('arm_%s' % x) for x in apertures if ('arm_%s' % x) in lcdict]\n aepcols = [('aep_%s' % x)for x in apertures if ('aep_%s' % x) in lcdict]\n atfcols = [('atf_%s' % x) for x in apertures if ('atf_%s' % x) in lcdict]\n psimcols = [x for x in ['psim','psrm','psep','pstf'] if x in lcdict]\n irmcols = [('irm_%s' % x) for x in apertures if ('irm_%s' % x) in lcdict]\n iepcols = [('iep_%s' % x) for x in apertures if ('iep_%s' % x) in lcdict]\n itfcols = [('itf_%s' % x) for x in apertures if ('itf_%s' % x) in lcdict]\n\n # next, find all the mag columns to normalize\n if magcols == 'all':\n cols_to_normalize = (aimcols + armcols + aepcols + atfcols +\n psimcols + irmcols + iepcols + itfcols)\n elif magcols == 'redmags':\n cols_to_normalize = (irmcols + (['psrm'] if 'psrm' in lcdict else []) +\n irmcols)\n elif magcols == 'epdmags':\n cols_to_normalize = (aepcols + (['psep'] if 'psep' in lcdict else []) +\n iepcols)\n elif magcols == 'tfamags':\n cols_to_normalize = (atfcols + (['pstf'] if 'pstf' in lcdict else []) +\n itfcols)\n elif magcols == 'epdtfa':\n cols_to_normalize = (aepcols + (['psep'] if 'psep' in lcdict else []) +\n iepcols + atfcols +\n (['pstf'] if 'pstf' in lcdict else []) +\n itfcols)\n else:\n cols_to_normalize = magcols.split(',')\n cols_to_normalize = [x.strip() for x in cols_to_normalize]\n\n colsnormalized = []\n\n # go through each column and normalize them\n for col in cols_to_normalize:\n\n if col in lcdict:\n\n # note: this requires the columns in ndarray format\n # unlike normalize_lcdict\n thismags = lcdict[col]\n\n # go through each key in normusing\n for nkey in normkeys:\n\n thisind = allkeys == nkey\n\n # make sure we have at least 3 elements in the matched set of\n # magnitudes corresponding to this key. also make sure that the\n # magnitudes corresponding to this key aren't all nan.\n thismagsize = thismags[thisind].size\n thismagfinite = np.where(np.isfinite(thismags[thisind]))[0].size\n\n if thismagsize > 2 and thismagfinite > 2:\n\n # do the normalization and update the thismags in the lcdict\n medmag = np.nanmedian(thismags[thisind])\n lcdict[col][thisind] = lcdict[col][thisind] - medmag\n\n if debugmode:\n LOGDEBUG('magcol: %s, currkey: \"%s\", nelem: %s, '\n 'medmag: %s' %\n (col, nkey, len(thismags[thisind]), medmag))\n\n # we remove mags that correspond to keys with less than 3\n # (finite) elements because we can't get the median mag\n # correctly and renormalizing them to zero would just set them\n # to zero\n else:\n\n lcdict[col][thisind] = np.nan\n\n # everything should now be normalized to zero\n # add back the requested normto\n if normto in ('jmag', 'hmag', 'kmag',\n 'bmag', 'vmag',\n 'sdssg', 'sdssr', 'sdssi'):\n\n if (normto in lcdict['objectinfo'] and\n lcdict['objectinfo'][normto] is not None):\n lcdict[col] = lcdict[col] + lcdict['objectinfo'][normto]\n\n else:\n if not quiet:\n LOGWARNING('no %s available in lcdict, '\n 'normalizing to 0.0' % normto)\n normto = 'zero'\n\n # update the colsnormalized list\n colsnormalized.append(col)\n\n else:\n if not quiet:\n LOGWARNING('column %s is not present, skipping...' % col)\n continue\n\n # add the lcnormcols key to the lcdict\n lcinstnormcols = ('cols normalized: %s - '\n 'normalized to: %s - '\n 'norm keys used: %s') % (repr(colsnormalized),\n normto,\n repr(availablenormkeys))\n lcdict['lcinstnormcols'] = lcinstnormcols\n\n return lcdict", "response": "This function is used to normalize a single - level dictionary of light curves across all instrument - level light curves."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef coord_features(objectinfo):\n\n '''Calculates object coordinates features, including:\n\n - galactic coordinates\n - total proper motion from pmra, pmdecl\n - reduced J proper motion from propermotion and Jmag\n\n Parameters\n ----------\n\n objectinfo : dict\n This is an objectinfo dict from a light curve file read into an\n `lcdict`. The format and the minimum keys required are::\n\n {'ra': the right ascension of the object in decimal degrees,\n 'decl': the declination of the object in decimal degrees,\n 'pmra': the proper motion in right ascension in mas/yr,\n 'pmdecl': the proper motion in declination in mas/yr,\n 'jmag': the 2MASS J mag of this object}\n\n Returns\n -------\n\n dict\n A dict containing the total proper motion\n\n '''\n\n retdict = {'propermotion': np.nan,\n 'gl':np.nan,\n 'gb':np.nan,\n 'rpmj':np.nan}\n\n if ('ra' in objectinfo and\n objectinfo['ra'] is not None and\n np.isfinite(objectinfo['ra']) and\n 'decl' in objectinfo and\n objectinfo['decl'] is not None and\n np.isfinite(objectinfo['decl'])):\n\n retdict['gl'], retdict['gb'] = coordutils.equatorial_to_galactic(\n objectinfo['ra'],\n objectinfo['decl']\n )\n\n if ('pmra' in objectinfo and\n objectinfo['pmra'] is not None and\n np.isfinite(objectinfo['pmra']) and\n 'pmdecl' in objectinfo and\n objectinfo['pmdecl'] is not None and\n np.isfinite(objectinfo['pmdecl']) and\n 'decl' in objectinfo and\n objectinfo['decl'] is not None and\n np.isfinite(objectinfo['decl'])):\n\n retdict['propermotion'] = coordutils.total_proper_motion(\n objectinfo['pmra'],\n objectinfo['pmdecl'],\n objectinfo['decl']\n )\n\n if ('jmag' in objectinfo and\n objectinfo['jmag'] is not None and\n np.isfinite(objectinfo['jmag']) and\n np.isfinite(retdict['propermotion'])):\n\n retdict['rpmj'] = coordutils.reduced_proper_motion(\n objectinfo['jmag'],\n retdict['propermotion']\n )\n\n return retdict", "response": "Calculates the object coordinates features for a single object in an objectinfo dict."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef color_features(in_objectinfo,\n deredden=True,\n custom_bandpasses=None,\n dust_timeout=10.0):\n '''Stellar colors and dereddened stellar colors using 2MASS DUST API:\n\n http://irsa.ipac.caltech.edu/applications/DUST/docs/dustProgramInterface.html\n\n Parameters\n ----------\n\n in_objectinfo : dict\n This is a dict that contains the object's magnitudes and positions. This\n requires at least 'ra', and 'decl' as keys which correspond to the right\n ascension and declination of the object, and one or more of the\n following keys for object magnitudes::\n\n 'umag' -> U mag -> colors: U-B, U-V, U-g\n 'bmag' -> B mag -> colors: U-B, B-V\n 'vmag' -> V mag -> colors: U-V, B-V, V-R, V-I, V-K\n 'rmag' -> R mag -> colors: V-R, R-I\n 'imag' -> I mag -> colors: g-I, V-I, R-I, B-I\n 'jmag' -> 2MASS J mag -> colors: J-H, J-K, g-J, i-J\n 'hmag' -> 2MASS H mag -> colors: J-H, H-K\n 'kmag' -> 2MASS Ks mag -> colors: g-Ks, H-Ks, J-Ks, V-Ks\n 'sdssu' -> SDSS u mag -> colors: u-g, u-V\n 'sdssg' -> SDSS g mag -> colors: g-r, g-i, g-K, u-g, U-g, g-J\n 'sdssr' -> SDSS r mag -> colors: r-i, g-r\n 'sdssi' -> SDSS i mag -> colors: r-i, i-z, g-i, i-J, i-W1\n 'sdssz' -> SDSS z mag -> colors: i-z, z-W2, g-z\n 'ujmag' -> UKIRT J mag -> colors: J-H, H-K, J-K, g-J, i-J\n 'uhmag' -> UKIRT H mag -> colors: J-H, H-K\n 'ukmag' -> UKIRT K mag -> colors: g-K, H-K, J-K, V-K\n 'irac1' -> Spitzer IRAC1 mag -> colors: i-I1, I1-I2\n 'irac2' -> Spitzer IRAC2 mag -> colors: I1-I2, I2-I3\n 'irac3' -> Spitzer IRAC3 mag -> colors: I2-I3\n 'irac4' -> Spitzer IRAC4 mag -> colors: I3-I4\n 'wise1' -> WISE W1 mag -> colors: i-W1, W1-W2\n 'wise2' -> WISE W2 mag -> colors: W1-W2, W2-W3\n 'wise3' -> WISE W3 mag -> colors: W2-W3\n 'wise4' -> WISE W4 mag -> colors: W3-W4\n\n These are basically taken from the available reddening bandpasses from\n the 2MASS DUST service. If B, V, u, g, r, i, z aren't provided but 2MASS\n J, H, Ks are all provided, the former will be calculated using the 2MASS\n JHKs -> BVugriz conversion functions in :py:mod:`astrobase.magnitudes`.\n\n deredden : bool\n If True, will make sure all colors use dereddened mags where possible.\n\n custom_bandpasses : dict\n This is a dict used to define any custom bandpasses in the\n `in_objectinfo` dict you want to make this function aware of and\n generate colors for. Use the format below for this dict::\n\n {\n '':{'dustkey':'',\n 'label':''\n 'colors':[['-',\n ' - '],\n ['-',\n ' - ']]},\n .\n ...\n .\n '':{'dustkey':'',\n 'label':''\n 'colors':[['-',\n ' - '],\n ['-',\n ' - ']]},\n }\n\n Where:\n\n `bandpass_key` is a key to use to refer to this bandpass in the\n `objectinfo` dict, e.g. 'sdssg' for SDSS g band\n\n `twomass_dust_key` is the key to use in the 2MASS DUST result table for\n reddening per band-pass. For example, given the following DUST result\n table (using http://irsa.ipac.caltech.edu/applications/DUST/)::\n\n |Filter_name|LamEff |A_over_E_B_V_SandF|A_SandF|A_over_E_B_V_SFD|A_SFD|\n |char |float |float |float |float |float|\n | |microns| |mags | |mags |\n CTIO U 0.3734 4.107 0.209 4.968 0.253\n CTIO B 0.4309 3.641 0.186 4.325 0.221\n CTIO V 0.5517 2.682 0.137 3.240 0.165\n .\n .\n ...\n\n The `twomass_dust_key` for 'vmag' would be 'CTIO V'. If you want to\n skip DUST lookup and want to pass in a specific reddening magnitude\n for your bandpass, use a float for the value of\n `twomass_dust_key`. If you want to skip DUST lookup entirely for\n this bandpass, use None for the value of `twomass_dust_key`.\n\n `band_label` is the label to use for this bandpass, e.g. 'W1' for\n WISE-1 band, 'u' for SDSS u, etc.\n\n The 'colors' list contains color definitions for all colors you want\n to generate using this bandpass. this list contains elements of the\n form::\n\n ['-',' - ']\n\n where the the first item is the bandpass keys making up this color,\n and the second item is the label for this color to be used by the\n frontends. An example::\n\n ['sdssu-sdssg','u - g']\n\n dust_timeout : float\n The timeout to use when contacting the 2MASS DUST web service.\n\n Returns\n -------\n\n dict\n An `objectinfo` dict with all of the generated colors, dereddened\n magnitude,s dereddened colors, as specified in the input args is\n returned.\n\n '''\n\n objectinfo = in_objectinfo.copy()\n\n # this is the initial output dict\n outdict = {\n 'available_bands':[],\n 'available_band_labels':[],\n 'available_dereddened_bands':[],\n 'available_dereddened_band_labels':[],\n 'available_colors':[],\n 'available_color_labels':[],\n 'dereddened':False\n }\n\n #\n # get the BVugriz mags from the JHK mags if necessary\n #\n # FIXME: should these be direct dered mag_0 = f(J_0, H_0, K_0) instead?\n # Bilir+ 2008 uses dereddened colors for their transforms, should check if\n # we need to do so here\n\n if ('jmag' in objectinfo and\n objectinfo['jmag'] is not None and\n np.isfinite(objectinfo['jmag']) and\n 'hmag' in objectinfo and\n objectinfo['hmag'] is not None and\n np.isfinite(objectinfo['hmag']) and\n 'kmag' in objectinfo and\n objectinfo['kmag'] is not None and\n np.isfinite(objectinfo['kmag'])):\n\n if ('bmag' not in objectinfo or\n ('bmag' in objectinfo and objectinfo['bmag'] is None) or\n ('bmag' in objectinfo and not np.isfinite(objectinfo['bmag']))):\n objectinfo['bmag'] = magnitudes.jhk_to_bmag(objectinfo['jmag'],\n objectinfo['hmag'],\n objectinfo['kmag'])\n outdict['bmagfromjhk'] = True\n else:\n outdict['bmagfromjhk'] = False\n\n if ('vmag' not in objectinfo or\n ('vmag' in objectinfo and objectinfo['vmag'] is None) or\n ('vmag' in objectinfo and not np.isfinite(objectinfo['vmag']))):\n objectinfo['vmag'] = magnitudes.jhk_to_vmag(objectinfo['jmag'],\n objectinfo['hmag'],\n objectinfo['kmag'])\n outdict['vmagfromjhk'] = True\n else:\n outdict['vmagfromjhk'] = False\n\n\n if ('sdssu' not in objectinfo or\n ('sdssu' in objectinfo and objectinfo['sdssu'] is None) or\n ('sdssu' in objectinfo and not np.isfinite(objectinfo['sdssu']))):\n objectinfo['sdssu'] = magnitudes.jhk_to_sdssu(objectinfo['jmag'],\n objectinfo['hmag'],\n objectinfo['kmag'])\n outdict['sdssufromjhk'] = True\n else:\n outdict['sdssufromjhk'] = False\n\n if ('sdssg' not in objectinfo or\n ('sdssg' in objectinfo and objectinfo['sdssg'] is None) or\n ('sdssg' in objectinfo and not np.isfinite(objectinfo['sdssg']))):\n objectinfo['sdssg'] = magnitudes.jhk_to_sdssg(objectinfo['jmag'],\n objectinfo['hmag'],\n objectinfo['kmag'])\n outdict['sdssgfromjhk'] = True\n else:\n outdict['sdssgfromjhk'] = False\n\n if ('sdssr' not in objectinfo or\n ('sdssr' in objectinfo and objectinfo['sdssr'] is None) or\n ('sdssr' in objectinfo and not np.isfinite(objectinfo['sdssr']))):\n objectinfo['sdssr'] = magnitudes.jhk_to_sdssr(objectinfo['jmag'],\n objectinfo['hmag'],\n objectinfo['kmag'])\n outdict['sdssrfromjhk'] = True\n else:\n outdict['sdssrfromjhk'] = False\n\n if ('sdssi' not in objectinfo or\n ('sdssi' in objectinfo and objectinfo['sdssi'] is None) or\n ('sdssi' in objectinfo and not np.isfinite(objectinfo['sdssi']))):\n objectinfo['sdssi'] = magnitudes.jhk_to_sdssi(objectinfo['jmag'],\n objectinfo['hmag'],\n objectinfo['kmag'])\n outdict['sdssifromjhk'] = True\n else:\n outdict['sdssifromjhk'] = False\n\n if ('sdssz' not in objectinfo or\n ('sdssz' in objectinfo and objectinfo['sdssz'] is None) or\n ('sdssz' in objectinfo and not np.isfinite(objectinfo['sdssz']))):\n objectinfo['sdssz'] = magnitudes.jhk_to_sdssz(objectinfo['jmag'],\n objectinfo['hmag'],\n objectinfo['kmag'])\n outdict['sdsszfromjhk'] = True\n else:\n outdict['sdsszfromjhk'] = False\n\n\n # now handle dereddening if possible\n if deredden:\n\n try:\n\n # first, get the extinction table for this object\n extinction = dust.extinction_query(objectinfo['ra'],\n objectinfo['decl'],\n verbose=False,\n timeout=dust_timeout)\n\n except Exception as e:\n\n LOGERROR(\"deredden = True but 'ra', 'decl' keys not present \"\n \"or invalid in objectinfo dict, ignoring reddening...\")\n extinction = None\n outdict['dereddened'] = False\n\n else:\n\n extinction = None\n outdict['dereddened'] = False\n\n # handle timeout from DUST service\n if not extinction:\n outdict['dereddened'] = False\n\n # go through the objectdict and pick out the mags we have available from the\n # BANDPASSES_COLORS dict\n\n # update our bandpasses_colors dict with any custom ones the user defined\n our_bandpasses_colors = BANDPASSES_COLORS.copy()\n our_bandpass_list = BANDPASS_LIST[::]\n\n if custom_bandpasses is not None and isinstance(custom_bandpasses, dict):\n\n our_bandpasses_colors.update(custom_bandpasses)\n\n # also update the list\n for key in custom_bandpasses:\n if key not in our_bandpass_list:\n our_bandpass_list.append(key)\n\n for mk in our_bandpass_list:\n\n if (mk in objectinfo and\n objectinfo[mk] is not None and\n np.isfinite(objectinfo[mk])):\n\n thisbandlabel = our_bandpasses_colors[mk]['label']\n thisdustkey = our_bandpasses_colors[mk]['dustkey']\n\n # add this to the outdict\n outdict[mk] = objectinfo[mk]\n\n outdict['available_bands'].append(mk)\n outdict['available_band_labels'].append(thisbandlabel)\n\n #\n # deredden if possible\n #\n # calculating dereddened mags:\n # A_x = m - m0_x where m is measured mag, m0 is intrinsic mag\n # m0_x = m - A_x\n #\n # so for two bands x, y:\n # intrinsic color (m_x - m_y)_0 = (m_x - m_y) - (A_x - A_y)\n if (deredden and extinction):\n\n outdict['dereddened'] = True\n\n # check if the dustkey is None, float, or str to figure out how\n # to retrieve the reddening\n if (thisdustkey is not None and\n isinstance(thisdustkey, str) and\n thisdustkey in extinction['Amag'] and\n np.isfinite(extinction['Amag'][thisdustkey]['sf11'])):\n\n outdict['extinction_%s' % mk] = (\n extinction['Amag'][thisdustkey]['sf11']\n )\n\n elif (thisdustkey is not None and\n isinstance(thisdustkey, float)):\n\n outdict['extinction_%s' % mk] = thisdustkey\n\n else:\n\n outdict['extinction_%s' % mk] = 0.0\n\n # apply the extinction\n outdict['dered_%s' % mk] = (\n outdict[mk] - outdict['extinction_%s' % mk]\n )\n outdict['available_dereddened_bands'].append('dered_%s' % mk)\n outdict['available_dereddened_band_labels'].append(\n thisbandlabel\n )\n\n # get all the colors to generate for this bandpass\n for colorspec in BANDPASSES_COLORS[mk]['colors']:\n\n # only add this if the color's not there already\n if colorspec[0] not in outdict:\n\n colorkey, colorlabel = colorspec\n\n # look for the bands to make this color\n\n # if it's not found now, this should work when we come\n # around for the next bandpass for this color\n\n band1, band2 = colorkey.split('-')\n\n if ('dered_%s' % band1 in outdict and\n 'dered_%s' % band2 in outdict and\n np.isfinite(outdict['dered_%s' % band1]) and\n np.isfinite(outdict['dered_%s' % band2])):\n\n outdict[colorkey] = (\n outdict['dered_%s' % band1] -\n outdict['dered_%s' % band2]\n )\n outdict['available_colors'].append(colorkey)\n outdict['available_color_labels'].append(colorlabel)\n\n\n # handle no dereddening\n else:\n\n outdict['dereddened'] = False\n outdict['extinction_%s' % mk] = 0.0\n outdict['dered_%s' % mk] = np.nan\n\n # get all the colors to generate for this bandpass\n for colorspec in our_bandpasses_colors[mk]['colors']:\n\n # only add this if the color's not there already\n if colorspec[0] not in outdict:\n\n colorkey, colorlabel = colorspec\n\n # look for the bands to make this color\n\n # if it's not found now, this should work when we come\n # around for the next bandpass for this color\n\n band1, band2 = colorkey.split('-')\n\n if (band1 in outdict and\n band2 in outdict and\n outdict[band1] is not None and\n outdict[band2] is not None and\n np.isfinite(outdict[band1]) and\n np.isfinite(outdict[band2])):\n\n outdict[colorkey] = (\n outdict[band1] -\n outdict[band2]\n )\n outdict['available_colors'].append(colorkey)\n outdict['available_color_labels'].append(colorlabel)\n\n\n # if this bandpass was not found in the objectinfo dict, ignore it\n else:\n\n outdict[mk] = np.nan\n\n\n return outdict", "response": "Color features of the object in the objectinfo dict."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef mdwarf_subtype_from_sdsscolor(ri_color, iz_color):\n '''This calculates the M-dwarf subtype given SDSS `r-i` and `i-z` colors.\n\n Parameters\n ----------\n\n ri_color : float\n The SDSS `r-i` color of the object.\n\n iz_color : float\n The SDSS `i-z` color of the object.\n\n Returns\n -------\n\n (subtype, index1, index2) : tuple\n `subtype`: if the star appears to be an M dwarf, will return an int\n between 0 and 9 indicating its subtype, e.g. will return 4 for an M4\n dwarf. If the object isn't an M dwarf, will return None\n\n `index1`, `index2`: the M-dwarf color locus value and spread of this\n object calculated from the `r-i` and `i-z` colors.\n\n '''\n\n # calculate the spectral type index and the spectral type spread of the\n # object. sti is calculated by fitting a line to the locus in r-i and i-z\n # space for M dwarfs in West+ 2007\n if np.isfinite(ri_color) and np.isfinite(iz_color):\n obj_sti = 0.875274*ri_color + 0.483628*(iz_color + 0.00438)\n obj_sts = -0.483628*ri_color + 0.875274*(iz_color + 0.00438)\n else:\n obj_sti = np.nan\n obj_sts = np.nan\n\n # possible M star if sti is >= 0.666 but <= 3.4559\n if (np.isfinite(obj_sti) and np.isfinite(obj_sts) and\n (obj_sti > 0.666) and (obj_sti < 3.4559)):\n\n # decide which M subclass object this is\n if ((obj_sti > 0.6660) and (obj_sti < 0.8592)):\n m_class = 'M0'\n\n if ((obj_sti > 0.8592) and (obj_sti < 1.0822)):\n m_class = 'M1'\n\n if ((obj_sti > 1.0822) and (obj_sti < 1.2998)):\n m_class = 'M2'\n\n if ((obj_sti > 1.2998) and (obj_sti < 1.6378)):\n m_class = 'M3'\n\n if ((obj_sti > 1.6378) and (obj_sti < 2.0363)):\n m_class = 'M4'\n\n if ((obj_sti > 2.0363) and (obj_sti < 2.2411)):\n m_class = 'M5'\n\n if ((obj_sti > 2.2411) and (obj_sti < 2.4126)):\n m_class = 'M6'\n\n if ((obj_sti > 2.4126) and (obj_sti < 2.9213)):\n m_class = 'M7'\n\n if ((obj_sti > 2.9213) and (obj_sti < 3.2418)):\n m_class = 'M8'\n\n if ((obj_sti > 3.2418) and (obj_sti < 3.4559)):\n m_class = 'M9'\n\n else:\n m_class = None\n\n return m_class, obj_sti, obj_sts", "response": "This function calculates the M - dwarf subtype given SDSS r - i and i - z colors."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngets several neighbor, GAIA, and SIMBAD features: From the KD-Tree in the given light curve catalog the object is in: `lclist_kdtree`: - distance to closest neighbor in arcsec - total number of all neighbors within 2 x `neighbor_radius_arcsec` From the GAIA DR2 catalog: - distance to closest neighbor in arcsec - total number of all neighbors within 2 x `neighbor_radius_arcsec` - gets the parallax for the object and neighbors - calculates the absolute GAIA mag and `G-K` color for use in CMDs - gets the proper motion in RA/Dec if available From the SIMBAD catalog: - the name of the object - the type of the object Parameters ---------- objectinfo : dict This is the objectinfo dict from an object's light curve. This must contain at least the following keys:: {'ra': the right ascension of the object, 'decl': the declination of the object} lclist_kdtree : scipy.spatial.cKDTree object This is a KD-Tree built on the Cartesian xyz coordinates from (ra, dec) of all objects in the same field as this object. It is similar to that produced by :py:func:`astrobase.lcproc.catalogs.make_lclist`, and is used to carry out the spatial search required to find neighbors for this object. neighbor_radius_arcsec : float The maximum radius in arcseconds around this object to search for neighbors in both the light curve catalog and in the GAIA DR2 catalog. gaia_matchdist_arcsec : float The maximum distance in arcseconds to use for a GAIA cross-match to this object. verbose : bool If True, indicates progress and warns of problems. gaia_submit_timeout : float Sets the timeout in seconds to use when submitting a request to look up the object's information to the GAIA service. Note that if `fast_mode` is set, this is ignored. gaia_submit_tries : int Sets the maximum number of times the GAIA services will be contacted to obtain this object's information. If `fast_mode` is set, this is ignored, and the services will be contacted only once (meaning that a failure to respond will be silently ignored and no GAIA data will be added to the checkplot's objectinfo dict). gaia_max_timeout : float Sets the timeout in seconds to use when waiting for the GAIA service to respond to our request for the object's information. Note that if `fast_mode` is set, this is ignored. gaia_mirror : str This sets the GAIA mirror to use. This is a key in the `services.gaia.GAIA_URLS` dict which defines the URLs to hit for each mirror. search_simbad : bool If this is True, searches for objects in SIMBAD at this object's location and gets the object's SIMBAD main ID, type, and stellar classification if available. Returns ------- dict Returns a dict with neighbor, GAIA, and SIMBAD features.", "response": "def neighbor_gaia_features(objectinfo,\n lclist_kdtree,\n neighbor_radius_arcsec,\n gaia_matchdist_arcsec=3.0,\n verbose=True,\n gaia_submit_timeout=10.0,\n gaia_submit_tries=3,\n gaia_max_timeout=180.0,\n gaia_mirror=None,\n complete_query_later=True,\n search_simbad=False):\n '''Gets several neighbor, GAIA, and SIMBAD features:\n\n From the KD-Tree in the given light curve catalog the object is in:\n `lclist_kdtree`:\n\n - distance to closest neighbor in arcsec\n - total number of all neighbors within 2 x `neighbor_radius_arcsec`\n\n From the GAIA DR2 catalog:\n\n - distance to closest neighbor in arcsec\n - total number of all neighbors within 2 x `neighbor_radius_arcsec`\n - gets the parallax for the object and neighbors\n - calculates the absolute GAIA mag and `G-K` color for use in CMDs\n - gets the proper motion in RA/Dec if available\n\n From the SIMBAD catalog:\n\n - the name of the object\n - the type of the object\n\n Parameters\n ----------\n\n objectinfo : dict\n This is the objectinfo dict from an object's light curve. This must\n contain at least the following keys::\n\n {'ra': the right ascension of the object,\n 'decl': the declination of the object}\n\n lclist_kdtree : scipy.spatial.cKDTree object\n This is a KD-Tree built on the Cartesian xyz coordinates from (ra, dec)\n of all objects in the same field as this object. It is similar to that\n produced by :py:func:`astrobase.lcproc.catalogs.make_lclist`, and is\n used to carry out the spatial search required to find neighbors for this\n object.\n\n neighbor_radius_arcsec : float\n The maximum radius in arcseconds around this object to search for\n neighbors in both the light curve catalog and in the GAIA DR2 catalog.\n\n gaia_matchdist_arcsec : float\n The maximum distance in arcseconds to use for a GAIA cross-match to this\n object.\n\n verbose : bool\n If True, indicates progress and warns of problems.\n\n gaia_submit_timeout : float\n Sets the timeout in seconds to use when submitting a request to look up\n the object's information to the GAIA service. Note that if `fast_mode`\n is set, this is ignored.\n\n gaia_submit_tries : int\n Sets the maximum number of times the GAIA services will be contacted to\n obtain this object's information. If `fast_mode` is set, this is\n ignored, and the services will be contacted only once (meaning that a\n failure to respond will be silently ignored and no GAIA data will be\n added to the checkplot's objectinfo dict).\n\n gaia_max_timeout : float\n Sets the timeout in seconds to use when waiting for the GAIA service to\n respond to our request for the object's information. Note that if\n `fast_mode` is set, this is ignored.\n\n gaia_mirror : str\n This sets the GAIA mirror to use. This is a key in the\n `services.gaia.GAIA_URLS` dict which defines the URLs to hit for each\n mirror.\n\n search_simbad : bool\n If this is True, searches for objects in SIMBAD at this object's\n location and gets the object's SIMBAD main ID, type, and stellar\n classification if available.\n\n Returns\n -------\n\n dict\n Returns a dict with neighbor, GAIA, and SIMBAD features.\n\n '''\n\n # kdtree search for neighbors in light curve catalog\n if ('ra' in objectinfo and 'decl' in objectinfo and\n objectinfo['ra'] is not None and objectinfo['decl'] is not None and\n (isinstance(lclist_kdtree, cKDTree) or\n isinstance(lclist_kdtree, KDTree))):\n\n ra, decl = objectinfo['ra'], objectinfo['decl']\n\n cosdecl = np.cos(np.radians(decl))\n sindecl = np.sin(np.radians(decl))\n cosra = np.cos(np.radians(ra))\n sinra = np.sin(np.radians(ra))\n\n # this is the search distance in xyz unit vectors\n xyzdist = 2.0 * np.sin(np.radians(neighbor_radius_arcsec/3600.0)/2.0)\n\n # look up the coordinates for the closest 100 objects in the kdtree\n # within 2 x neighbor_radius_arcsec\n kdt_dist, kdt_ind = lclist_kdtree.query(\n [cosra*cosdecl,\n sinra*cosdecl,\n sindecl],\n k=100,\n distance_upper_bound=xyzdist\n )\n\n # the first match is the object itself\n finite_distind = (np.isfinite(kdt_dist)) & (kdt_dist > 0)\n finite_dists = kdt_dist[finite_distind]\n nbrindices = kdt_ind[finite_distind]\n n_neighbors = finite_dists.size\n\n if n_neighbors > 0:\n\n closest_dist = finite_dists.min()\n closest_dist_arcsec = (\n np.degrees(2.0*np.arcsin(closest_dist/2.0))*3600.0\n )\n closest_dist_nbrind = nbrindices[finite_dists == finite_dists.min()]\n\n resultdict = {\n 'neighbors':n_neighbors,\n 'nbrindices':nbrindices,\n 'distarcsec':np.degrees(2.0*np.arcsin(finite_dists/2.0))*3600.0,\n 'closestdistarcsec':closest_dist_arcsec,\n 'closestdistnbrind':closest_dist_nbrind,\n 'searchradarcsec':neighbor_radius_arcsec,\n }\n\n else:\n\n resultdict = {\n 'neighbors':0,\n 'nbrindices':np.array([]),\n 'distarcsec':np.array([]),\n 'closestdistarcsec':np.nan,\n 'closestdistnbrind':np.array([]),\n 'searchradarcsec':neighbor_radius_arcsec,\n }\n\n\n else:\n if verbose:\n LOGWARNING(\"one of ra, decl, kdtree is missing in \"\n \"objectinfo dict or lclistpkl, \"\n \"can't get observed neighbors\")\n\n resultdict = {\n 'neighbors':np.nan,\n 'nbrindices':np.array([]),\n 'distarcsec':np.array([]),\n 'closestdistarcsec':np.nan,\n 'closestdistnbrind':np.array([]),\n 'searchradarcsec':neighbor_radius_arcsec,\n }\n\n\n # next, search for this object in GAIA\n if ('ra' in objectinfo and 'decl' in objectinfo and\n objectinfo['ra'] is not None and objectinfo['decl'] is not None):\n\n gaia_result = gaia.objectlist_conesearch(\n objectinfo['ra'],\n objectinfo['decl'],\n neighbor_radius_arcsec,\n verbose=verbose,\n timeout=gaia_submit_timeout,\n maxtimeout=gaia_max_timeout,\n maxtries=gaia_submit_tries,\n gaia_mirror=gaia_mirror,\n complete_query_later=complete_query_later\n )\n\n if gaia_result:\n\n gaia_objlistf = gaia_result['result']\n\n with gzip.open(gaia_objlistf,'rb') as infd:\n\n try:\n gaia_objlist = np.genfromtxt(\n infd,\n names=True,\n delimiter=',',\n dtype='U20,f8,f8,f8,f8,f8,f8,f8,f8,f8,f8,f8,f8',\n usecols=(0,1,2,3,4,5,6,7,8,9,10,11,12)\n )\n except Exception as e:\n gaia_objlist = []\n\n gaia_objlist = np.atleast_1d(gaia_objlist)\n\n if gaia_objlist.size > 0:\n\n # if we have GAIA results, we can get xypositions of all of\n # these objects on the object skyview stamp\n stampres = skyview.get_stamp(objectinfo['ra'],\n objectinfo['decl'])\n\n if (stampres and\n 'fitsfile' in stampres and\n stampres['fitsfile'] is not None and\n os.path.exists(stampres['fitsfile'])):\n\n stampwcs = WCS(stampres['fitsfile'])\n\n gaia_xypos = stampwcs.all_world2pix(\n np.column_stack((gaia_objlist['ra'],\n gaia_objlist['dec'])),\n 1\n )\n\n else:\n\n gaia_xypos = None\n\n\n # the first object is likely the match to the object itself\n if gaia_objlist['dist_arcsec'][0] < gaia_matchdist_arcsec:\n\n if gaia_objlist.size > 1:\n\n gaia_nneighbors = gaia_objlist[1:].size\n\n gaia_status = (\n 'ok: object found with %s neighbors' %\n gaia_nneighbors\n )\n\n # the first in each array is the object\n gaia_ids = gaia_objlist['source_id']\n gaia_mags = gaia_objlist['phot_g_mean_mag']\n gaia_parallaxes = gaia_objlist['parallax']\n gaia_parallax_errs = gaia_objlist['parallax_error']\n gaia_pmra = gaia_objlist['pmra']\n gaia_pmra_err = gaia_objlist['pmra_error']\n gaia_pmdecl = gaia_objlist['pmdec']\n gaia_pmdecl_err = gaia_objlist['pmdec_error']\n\n gaia_absolute_mags = magnitudes.absolute_gaia_magnitude(\n gaia_mags, gaia_parallaxes\n )\n if ('kmag' in objectinfo and\n objectinfo['kmag'] is not None and\n np.isfinite(objectinfo['kmag'])):\n gaiak_colors = gaia_mags - objectinfo['kmag']\n else:\n gaiak_colors = None\n\n gaia_dists = gaia_objlist['dist_arcsec']\n gaia_closest_distarcsec = gaia_objlist['dist_arcsec'][1]\n gaia_closest_gmagdiff = (\n gaia_objlist['phot_g_mean_mag'][0] -\n gaia_objlist['phot_g_mean_mag'][1]\n )\n\n else:\n\n LOGWARNING('object found in GAIA at (%.3f,%.3f), '\n 'but no neighbors' % (objectinfo['ra'],\n objectinfo['decl']))\n\n gaia_nneighbors = 0\n\n gaia_status = (\n 'ok: object found but no neighbors'\n )\n\n # the first in each array is the object\n gaia_ids = gaia_objlist['source_id']\n gaia_mags = gaia_objlist['phot_g_mean_mag']\n gaia_parallaxes = gaia_objlist['parallax']\n gaia_parallax_errs = gaia_objlist['parallax_error']\n gaia_pmra = gaia_objlist['pmra']\n gaia_pmra_err = gaia_objlist['pmra_error']\n gaia_pmdecl = gaia_objlist['pmdec']\n gaia_pmdecl_err = gaia_objlist['pmdec_error']\n\n gaia_absolute_mags = magnitudes.absolute_gaia_magnitude(\n gaia_mags, gaia_parallaxes\n )\n if ('kmag' in objectinfo and\n objectinfo['kmag'] is not None and\n np.isfinite(objectinfo['kmag'])):\n gaiak_colors = gaia_mags - objectinfo['kmag']\n else:\n gaiak_colors = None\n\n gaia_dists = gaia_objlist['dist_arcsec']\n gaia_closest_distarcsec = np.nan\n gaia_closest_gmagdiff = np.nan\n\n\n # otherwise, the object wasn't found in GAIA for some reason\n else:\n\n LOGWARNING('no GAIA objects found within '\n '%.3f arcsec of object position (%.3f, %.3f), '\n 'closest object is at %.3f arcsec away' %\n (gaia_matchdist_arcsec,\n objectinfo['ra'], objectinfo['decl'],\n gaia_objlist['dist_arcsec'][0]))\n\n gaia_status = ('failed: no object within %.3f '\n 'arcsec, closest = %.3f arcsec' %\n (gaia_matchdist_arcsec,\n gaia_objlist['dist_arcsec'][0]))\n\n gaia_nneighbors = np.nan\n\n gaia_ids = gaia_objlist['source_id']\n gaia_mags = gaia_objlist['phot_g_mean_mag']\n gaia_parallaxes = gaia_objlist['parallax']\n gaia_parallax_errs = gaia_objlist['parallax_error']\n gaia_pmra = gaia_objlist['pmra']\n gaia_pmra_err = gaia_objlist['pmra_error']\n gaia_pmdecl = gaia_objlist['pmdec']\n gaia_pmdecl_err = gaia_objlist['pmdec_error']\n\n gaia_absolute_mags = magnitudes.absolute_gaia_magnitude(\n gaia_mags, gaia_parallaxes\n )\n if ('kmag' in objectinfo and\n objectinfo['kmag'] is not None and\n np.isfinite(objectinfo['kmag'])):\n gaiak_colors = gaia_mags - objectinfo['kmag']\n else:\n gaiak_colors = None\n\n gaia_dists = gaia_objlist['dist_arcsec']\n gaia_closest_distarcsec = np.nan\n gaia_closest_gmagdiff = np.nan\n\n # if there are no neighbors within neighbor_radius_arcsec\n # or this object is not covered by GAIA. return nothing\n else:\n\n LOGERROR('no GAIA objects at this '\n 'position or GAIA query failed')\n\n gaia_status = (\n 'failed: no GAIA objects at this '\n 'position or GAIA query failed.'\n )\n gaia_nneighbors = np.nan\n gaia_ids = None\n gaia_mags = None\n\n gaia_xypos = None\n gaia_parallaxes = None\n gaia_parallax_errs = None\n gaia_pmra = None\n gaia_pmra_err = None\n gaia_pmdecl = None\n gaia_pmdecl_err = None\n gaia_absolute_mags = None\n gaiak_colors = None\n\n gaia_dists = None\n gaia_closest_distarcsec = np.nan\n gaia_closest_gmagdiff = np.nan\n\n # update the resultdict with gaia stuff\n resultdict.update(\n {'gaia_status':gaia_status,\n 'gaia_neighbors':gaia_nneighbors,\n 'gaia_ids':gaia_ids,\n 'gaia_xypos':gaia_xypos,\n 'gaia_mags':gaia_mags,\n 'gaia_parallaxes':gaia_parallaxes,\n 'gaia_parallax_errs':gaia_parallax_errs,\n 'gaia_pmras':gaia_pmra,\n 'gaia_pmra_errs':gaia_pmra_err,\n 'gaia_pmdecls':gaia_pmdecl,\n 'gaia_pmdecl_errs':gaia_pmdecl_err,\n 'gaia_absolute_mags':gaia_absolute_mags,\n 'gaiak_colors':gaiak_colors,\n 'gaia_dists':gaia_dists,\n 'gaia_closest_distarcsec':gaia_closest_distarcsec,\n 'gaia_closest_gmagdiff':gaia_closest_gmagdiff}\n )\n\n else:\n\n LOGERROR('GAIA query did not return a '\n 'result for object at (%.3f, %.3f)' % (objectinfo['ra'],\n objectinfo['decl']))\n\n resultdict.update(\n {'gaia_status':'failed: GAIA TAP query failed',\n 'gaia_neighbors':np.nan,\n 'gaia_ids':None,\n 'gaia_xypos':None,\n 'gaia_mags':None,\n 'gaia_parallaxes':None,\n 'gaia_parallax_errs':None,\n 'gaia_pmras':None,\n 'gaia_pmra_errs':None,\n 'gaia_pmdecls':None,\n 'gaia_pmdecl_errs':None,\n 'gaia_absolute_mags':None,\n 'gaiak_colors':None,\n 'gaia_dists':None,\n 'gaia_closest_distarcsec':np.nan,\n 'gaia_closest_gmagdiff':np.nan}\n )\n\n\n else:\n\n LOGERROR(\"one or more of the 'ra', 'decl' keys \"\n \"are missing from the objectinfo dict, \"\n \"can't get GAIA or LC collection neighbor features\")\n\n resultdict.update(\n {'gaia_status':'failed: no ra/decl for object',\n 'gaia_neighbors':np.nan,\n 'gaia_ids':None,\n 'gaia_xypos':None,\n 'gaia_mags':None,\n 'gaia_parallaxes':None,\n 'gaia_parallax_errs':None,\n 'gaia_pmras':None,\n 'gaia_pmra_errs':None,\n 'gaia_pmdecls':None,\n 'gaia_pmdecl_errs':None,\n 'gaia_absolute_mags':None,\n 'gaiak_colors':None,\n 'gaia_dists':None,\n 'gaia_closest_distarcsec':np.nan,\n 'gaia_closest_gmagdiff':np.nan}\n )\n\n\n # finally, search for this object in SIMBAD\n if ('ra' in objectinfo and 'decl' in objectinfo and\n objectinfo['ra'] is not None and objectinfo['decl'] is not None and\n search_simbad):\n\n simbad_result = simbad.objectnames_conesearch(\n objectinfo['ra'],\n objectinfo['decl'],\n neighbor_radius_arcsec,\n verbose=verbose,\n timeout=gaia_submit_timeout,\n maxtimeout=gaia_max_timeout,\n maxtries=gaia_submit_tries,\n complete_query_later=complete_query_later\n )\n\n else:\n\n simbad_result = None\n\n if (simbad_result and\n simbad_result['result'] and\n os.path.exists(simbad_result['result'])):\n\n with gzip.open(simbad_result['result'],'rb') as infd:\n\n try:\n simbad_objectnames = np.genfromtxt(\n infd,\n names=True,\n delimiter=',',\n dtype='U20,f8,f8,U20,U20,U20,i8,U600,f8',\n usecols=(0,1,2,3,4,5,6,7,8),\n comments='?',\n )\n except Exception as e:\n simbad_objectnames = []\n\n simbad_objectnames = np.atleast_1d(simbad_objectnames)\n\n if simbad_objectnames.size > 0:\n\n simbad_mainid = simbad_objectnames['main_id'].tolist()\n simbad_allids = simbad_objectnames['all_ids'].tolist()\n simbad_objtype = simbad_objectnames['otype_txt'].tolist()\n simbad_distarcsec = simbad_objectnames['dist_arcsec'].tolist()\n simbad_nmatches = len(simbad_mainid)\n\n simbad_mainid = [x.replace('\"','') for x in simbad_mainid]\n simbad_allids = [x.replace('\"','') for x in simbad_allids]\n simbad_objtype = [x.replace('\"','') for x in simbad_objtype]\n\n\n resultdict.update({\n 'simbad_nmatches':simbad_nmatches,\n 'simbad_mainid':simbad_mainid,\n 'simbad_objtype':simbad_objtype,\n 'simbad_allids':simbad_allids,\n 'simbad_distarcsec':simbad_distarcsec\n })\n\n\n if simbad_nmatches > 1:\n\n resultdict['simbad_status'] = (\n 'ok: multiple SIMBAD matches found'\n )\n\n else:\n\n resultdict['simbad_status'] = 'ok: single SIMBAD match'\n\n\n\n # get the closest match\n if simbad_distarcsec[0] < gaia_matchdist_arcsec:\n\n resultdict.update({\n 'simbad_best_mainid':simbad_mainid[0],\n 'simbad_best_objtype':simbad_objtype[0],\n 'simbad_best_allids':simbad_allids[0],\n 'simbad_best_distarcsec':simbad_distarcsec[0],\n 'simbad_status':'ok: object found within match radius'\n })\n\n else:\n\n LOGWARNING('no SIMBAD objects found within '\n '%.3f arcsec of object position (%.3f, %.3f), '\n 'closest object: %s at %.3f arcsec away' %\n (gaia_matchdist_arcsec,\n objectinfo['ra'],\n objectinfo['decl'],\n simbad_mainid[0],\n simbad_distarcsec[0]))\n\n simbad_status = ('failed: no object within %.3f '\n 'arcsec, closest = %.3f arcsec' %\n (gaia_matchdist_arcsec,\n simbad_distarcsec[0]))\n\n\n resultdict.update({\n 'simbad_best_mainid':None,\n 'simbad_best_objtype':None,\n 'simbad_best_allids':None,\n 'simbad_best_distarcsec':None,\n 'simbad_status':simbad_status\n })\n\n\n else:\n\n resultdict.update({\n 'simbad_status':'failed: no SIMBAD matches found',\n 'simbad_nmatches':None,\n 'simbad_mainid':None,\n 'simbad_objtype':None,\n 'simbad_allids':None,\n 'simbad_distarcsec':None,\n 'simbad_best_mainid':None,\n 'simbad_best_objtype':None,\n 'simbad_best_allids':None,\n 'simbad_best_distarcsec':None,\n })\n\n else:\n\n if search_simbad:\n simbad_status = 'failed: SIMBAD query failed'\n else:\n simbad_status = 'failed: SIMBAD query not tried'\n\n resultdict.update({\n 'simbad_status':simbad_status,\n 'simbad_nmatches':None,\n 'simbad_mainid':None,\n 'simbad_objtype':None,\n 'simbad_allids':None,\n 'simbad_distarcsec':None,\n 'simbad_best_mainid':None,\n 'simbad_best_objtype':None,\n 'simbad_best_allids':None,\n 'simbad_best_distarcsec':None,\n })\n\n return resultdict"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef parallel_epd_lclist(lclist,\n externalparams,\n timecols=None,\n magcols=None,\n errcols=None,\n lcformat='hat-sql',\n lcformatdir=None,\n epdsmooth_sigclip=3.0,\n epdsmooth_windowsize=21,\n epdsmooth_func=smooth_magseries_savgol,\n epdsmooth_extraparams=None,\n nworkers=NCPUS,\n maxworkertasks=1000):\n '''This applies EPD in parallel to all LCs in the input list.\n\n Parameters\n ----------\n\n lclist : list of str\n This is the list of light curve files to run EPD on.\n\n externalparams : dict or None\n This is a dict that indicates which keys in the lcdict obtained from the\n lcfile correspond to the required external parameters. As with timecol,\n magcol, and errcol, these can be simple keys (e.g. 'rjd') or compound\n keys ('magaperture1.mags'). The dict should look something like::\n\n {'fsv':'' array: S values for each observation,\n 'fdv':'' array: D values for each observation,\n 'fkv':'' array: K values for each observation,\n 'xcc':'' array: x coords for each observation,\n 'ycc':'' array: y coords for each observation,\n 'bgv':'' array: sky background for each observation,\n 'bge':'' array: sky background err for each observation,\n 'iha':'' array: hour angle for each observation,\n 'izd':'' array: zenith distance for each observation}\n\n Alternatively, if these exact keys are already present in the lcdict,\n indicate this by setting externalparams to None.\n\n timecols,magcols,errcols : lists of str\n The keys in the lcdict produced by your light curve reader function that\n correspond to the times, mags/fluxes, and associated measurement errors\n that will be used as inputs to the EPD process. If these are None, the\n default values for `timecols`, `magcols`, and `errcols` for your light\n curve format will be used here.\n\n lcformat : str\n This is the `formatkey` associated with your light curve format, which\n you previously passed in to the `lcproc.register_lcformat`\n function. This will be used to look up how to find and read the light\n curve files.\n\n lcformatdir : str or None\n If this is provided, gives the path to a directory when you've stored\n your lcformat description JSONs, other than the usual directories lcproc\n knows to search for them in. Use this along with `lcformat` to specify\n an LC format JSON file that's not currently registered with lcproc.\n\n epdsmooth_sigclip : float or int or sequence of two floats/ints or None\n This specifies how to sigma-clip the input LC before fitting the EPD\n function to it.\n\n If a single float or int, a symmetric sigma-clip will be performed using\n the number provided as the sigma-multiplier to cut out from the input\n time-series.\n\n If a list of two ints/floats is provided, the function will perform an\n 'asymmetric' sigma-clip. The first element in this list is the sigma\n value to use for fainter flux/mag values; the second element in this\n list is the sigma value to use for brighter flux/mag values. For\n example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma\n dimmings and greater than 3-sigma brightenings. Here the meaning of\n \"dimming\" and \"brightening\" is set by *physics* (not the magnitude\n system), which is why the `magsarefluxes` kwarg must be correctly set.\n\n If `sigclip` is None, no sigma-clipping will be performed, and the\n time-series (with non-finite elems removed) will be passed through to\n the output.\n\n epdsmooth_windowsize : int\n This is the number of LC points to smooth over to generate a smoothed\n light curve that will be used to fit the EPD function.\n\n epdsmooth_func : Python function\n This sets the smoothing filter function to use. A Savitsky-Golay filter\n is used to smooth the light curve by default. The functions that can be\n used with this kwarg are listed in `varbase.trends`. If you want to use\n your own function, it MUST have the following signature::\n\n def smoothfunc(mags_array, window_size, **extraparams)\n\n and return a numpy array of the same size as `mags_array` with the\n smoothed time-series. Any extra params can be provided using the\n `extraparams` dict.\n\n epdsmooth_extraparams : dict\n This is a dict of any extra filter params to supply to the smoothing\n function.\n\n nworkers : int\n The number of parallel workers to launch when processing the LCs.\n\n maxworkertasks : int\n The maximum number of tasks a parallel worker will complete before it is\n replaced with a new one (sometimes helps with memory-leaks).\n\n Returns\n -------\n\n dict\n Returns a dict organized by all the keys in the input `magcols` list,\n containing lists of EPD pickle light curves for that `magcol`.\n\n Notes\n -----\n\n - S -> measure of PSF sharpness (~1/sigma^2 sosmaller S = wider PSF)\n - D -> measure of PSF ellipticity in xy direction\n - K -> measure of PSF ellipticity in cross direction\n\n S, D, K are related to the PSF's variance and covariance, see eqn 30-33 in\n A. Pal's thesis: https://arxiv.org/abs/0906.3486\n\n '''\n\n try:\n formatinfo = get_lcformat(lcformat,\n use_lcformat_dir=lcformatdir)\n if formatinfo:\n (fileglob, readerfunc,\n dtimecols, dmagcols, derrcols,\n magsarefluxes, normfunc) = formatinfo\n else:\n LOGERROR(\"can't figure out the light curve format\")\n return None\n except Exception as e:\n LOGEXCEPTION(\"can't figure out the light curve format\")\n return None\n\n # override the default timecols, magcols, and errcols\n # using the ones provided to the function\n if timecols is None:\n timecols = dtimecols\n if magcols is None:\n magcols = dmagcols\n if errcols is None:\n errcols = derrcols\n\n outdict = {}\n\n # run by magcol\n for t, m, e in zip(timecols, magcols, errcols):\n\n tasks = [(x, t, m, e, externalparams, lcformat, lcformatdir,\n epdsmooth_sigclip, epdsmooth_windowsize,\n epdsmooth_func, epdsmooth_extraparams) for\n x in lclist]\n\n pool = mp.Pool(nworkers, maxtasksperchild=maxworkertasks)\n results = pool.map(parallel_epd_worker, tasks)\n pool.close()\n pool.join()\n\n outdict[m] = results\n\n return outdict", "response": "This function applies EPD in parallel to all light curve files in the input list."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef parallel_epd_lcdir(\n lcdir,\n externalparams,\n lcfileglob=None,\n timecols=None,\n magcols=None,\n errcols=None,\n lcformat='hat-sql',\n lcformatdir=None,\n epdsmooth_sigclip=3.0,\n epdsmooth_windowsize=21,\n epdsmooth_func=smooth_magseries_savgol,\n epdsmooth_extraparams=None,\n nworkers=NCPUS,\n maxworkertasks=1000\n):\n '''This applies EPD in parallel to all LCs in a directory.\n\n Parameters\n ----------\n\n lcdir : str\n The light curve directory to process.\n\n externalparams : dict or None\n This is a dict that indicates which keys in the lcdict obtained from the\n lcfile correspond to the required external parameters. As with timecol,\n magcol, and errcol, these can be simple keys (e.g. 'rjd') or compound\n keys ('magaperture1.mags'). The dict should look something like::\n\n {'fsv':'' array: S values for each observation,\n 'fdv':'' array: D values for each observation,\n 'fkv':'' array: K values for each observation,\n 'xcc':'' array: x coords for each observation,\n 'ycc':'' array: y coords for each observation,\n 'bgv':'' array: sky background for each observation,\n 'bge':'' array: sky background err for each observation,\n 'iha':'' array: hour angle for each observation,\n 'izd':'' array: zenith distance for each observation}\n\n lcfileglob : str or None\n A UNIX fileglob to use to select light curve files in `lcdir`. If this\n is not None, the value provided will override the default fileglob for\n your light curve format.\n\n timecols,magcols,errcols : lists of str\n The keys in the lcdict produced by your light curve reader function that\n correspond to the times, mags/fluxes, and associated measurement errors\n that will be used as inputs to the EPD process. If these are None, the\n default values for `timecols`, `magcols`, and `errcols` for your light\n curve format will be used here.\n\n lcformat : str\n This is the `formatkey` associated with your light curve format, which\n you previously passed in to the `lcproc.register_lcformat`\n function. This will be used to look up how to find and read the light\n curves specified in `basedir` or `use_list_of_filenames`.\n\n lcformatdir : str or None\n If this is provided, gives the path to a directory when you've stored\n your lcformat description JSONs, other than the usual directories lcproc\n knows to search for them in. Use this along with `lcformat` to specify\n an LC format JSON file that's not currently registered with lcproc.\n\n epdsmooth_sigclip : float or int or sequence of two floats/ints or None\n This specifies how to sigma-clip the input LC before fitting the EPD\n function to it.\n\n If a single float or int, a symmetric sigma-clip will be performed using\n the number provided as the sigma-multiplier to cut out from the input\n time-series.\n\n If a list of two ints/floats is provided, the function will perform an\n 'asymmetric' sigma-clip. The first element in this list is the sigma\n value to use for fainter flux/mag values; the second element in this\n list is the sigma value to use for brighter flux/mag values. For\n example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma\n dimmings and greater than 3-sigma brightenings. Here the meaning of\n \"dimming\" and \"brightening\" is set by *physics* (not the magnitude\n system), which is why the `magsarefluxes` kwarg must be correctly set.\n\n If `sigclip` is None, no sigma-clipping will be performed, and the\n time-series (with non-finite elems removed) will be passed through to\n the output.\n\n epdsmooth_windowsize : int\n This is the number of LC points to smooth over to generate a smoothed\n light curve that will be used to fit the EPD function.\n\n epdsmooth_func : Python function\n This sets the smoothing filter function to use. A Savitsky-Golay filter\n is used to smooth the light curve by default. The functions that can be\n used with this kwarg are listed in `varbase.trends`. If you want to use\n your own function, it MUST have the following signature::\n\n def smoothfunc(mags_array, window_size, **extraparams)\n\n and return a numpy array of the same size as `mags_array` with the\n smoothed time-series. Any extra params can be provided using the\n `extraparams` dict.\n\n epdsmooth_extraparams : dict\n This is a dict of any extra filter params to supply to the smoothing\n function.\n\n nworkers : int\n The number of parallel workers to launch when processing the LCs.\n\n maxworkertasks : int\n The maximum number of tasks a parallel worker will complete before it is\n replaced with a new one (sometimes helps with memory-leaks).\n\n Returns\n -------\n\n dict\n Returns a dict organized by all the keys in the input `magcols` list,\n containing lists of EPD pickle light curves for that `magcol`.\n\n Notes\n -----\n\n - S -> measure of PSF sharpness (~1/sigma^2 sosmaller S = wider PSF)\n - D -> measure of PSF ellipticity in xy direction\n - K -> measure of PSF ellipticity in cross direction\n\n S, D, K are related to the PSF's variance and covariance, see eqn 30-33 in\n A. Pal's thesis: https://arxiv.org/abs/0906.3486\n\n '''\n\n try:\n formatinfo = get_lcformat(lcformat,\n use_lcformat_dir=lcformatdir)\n if formatinfo:\n (fileglob, readerfunc,\n dtimecols, dmagcols, derrcols,\n magsarefluxes, normfunc) = formatinfo\n else:\n LOGERROR(\"can't figure out the light curve format\")\n return None\n except Exception as e:\n LOGEXCEPTION(\"can't figure out the light curve format\")\n return None\n\n # find all the files matching the lcglob in lcdir\n if lcfileglob is None:\n lcfileglob = fileglob\n\n lclist = sorted(glob.glob(os.path.join(lcdir, lcfileglob)))\n\n return parallel_epd_lclist(\n lclist,\n externalparams,\n timecols=timecols,\n magcols=magcols,\n errcols=errcols,\n lcformat=lcformat,\n epdsmooth_sigclip=epdsmooth_sigclip,\n epdsmooth_windowsize=epdsmooth_windowsize,\n epdsmooth_func=epdsmooth_func,\n epdsmooth_extraparams=epdsmooth_extraparams,\n nworkers=nworkers,\n maxworkertasks=maxworkertasks\n )", "response": "This function applies an EPD in parallel to all LCs in a directory."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef bls_serial_pfind(times, mags, errs,\n magsarefluxes=False,\n startp=0.1, # search from 0.1 d to...\n endp=100.0, # ... 100.0 d -- don't search full timebase\n stepsize=5.0e-4,\n mintransitduration=0.01, # minimum transit length in phase\n maxtransitduration=0.4, # maximum transit length in phase\n ndurations=100,\n autofreq=True, # figure out f0, nf, and df automatically\n blsobjective='likelihood',\n blsmethod='fast',\n blsoversample=10,\n blsmintransits=3,\n blsfreqfactor=10.0,\n periodepsilon=0.1,\n nbestpeaks=5,\n sigclip=10.0,\n verbose=True,\n raiseonfail=False):\n '''Runs the Box Least Squares Fitting Search for transit-shaped signals.\n\n Based on the version of BLS in Astropy 3.1:\n `astropy.stats.BoxLeastSquares`. If you don't have Astropy 3.1, this module\n will fail to import. Note that by default, this implementation of\n `bls_serial_pfind` doesn't use the `.autoperiod()` function from\n `BoxLeastSquares` but uses the same auto frequency-grid generation as the\n functions in `periodbase.kbls`. If you want to use Astropy's implementation,\n set the value of `autofreq` kwarg to 'astropy'.\n\n The dict returned from this function contains a `blsmodel` key, which is the\n generated model from Astropy's BLS. Use the `.compute_stats()` method to\n calculate the required stats like SNR, depth, duration, etc.\n\n Parameters\n ----------\n\n times,mags,errs : np.array\n The magnitude/flux time-series to search for transits.\n\n magsarefluxes : bool\n If the input measurement values in `mags` and `errs` are in fluxes, set\n this to True.\n\n startp,endp : float\n The minimum and maximum periods to consider for the transit search.\n\n stepsize : float\n The step-size in frequency to use when constructing a frequency grid for\n the period search.\n\n mintransitduration,maxtransitduration : float\n The minimum and maximum transitdurations (in units of phase) to consider\n for the transit search.\n\n ndurations : int\n The number of transit durations to use in the period-search.\n\n autofreq : bool or str\n If this is True, the values of `stepsize` and `nphasebins` will be\n ignored, and these, along with a frequency-grid, will be determined\n based on the following relations::\n\n nphasebins = int(ceil(2.0/mintransitduration))\n if nphasebins > 3000:\n nphasebins = 3000\n\n stepsize = 0.25*mintransitduration/(times.max()-times.min())\n\n minfreq = 1.0/endp\n maxfreq = 1.0/startp\n nfreq = int(ceil((maxfreq - minfreq)/stepsize))\n\n If this is False, you must set `startp`, `endp`, and `stepsize` as\n appropriate.\n\n If this is str == 'astropy', will use the\n `astropy.stats.BoxLeastSquares.autoperiod()` function to calculate the\n frequency grid instead of the kbls method.\n\n blsobjective : {'likelihood','snr'}\n Sets the type of objective to optimize in the `BoxLeastSquares.power()`\n function.\n\n blsmethod : {'fast','slow'}\n Sets the type of method to use in the `BoxLeastSquares.power()`\n function.\n\n blsoversample : {'likelihood','snr'}\n Sets the `oversample` kwarg for the `BoxLeastSquares.power()` function.\n\n blsmintransits : int\n Sets the `min_n_transits` kwarg for the `BoxLeastSquares.autoperiod()`\n function.\n\n blsfreqfactor : float\n Sets the `frequency_factor` kwarg for the `BoxLeastSquares.autperiod()`\n function.\n\n periodepsilon : float\n The fractional difference between successive values of 'best' periods\n when sorting by periodogram power to consider them as separate periods\n (as opposed to part of the same periodogram peak). This is used to avoid\n broad peaks in the periodogram and make sure the 'best' periods returned\n are all actually independent.\n\n nbestpeaks : int\n The number of 'best' peaks to return from the periodogram results,\n starting from the global maximum of the periodogram peak values.\n\n sigclip : float or int or sequence of two floats/ints or None\n If a single float or int, a symmetric sigma-clip will be performed using\n the number provided as the sigma-multiplier to cut out from the input\n time-series.\n\n If a list of two ints/floats is provided, the function will perform an\n 'asymmetric' sigma-clip. The first element in this list is the sigma\n value to use for fainter flux/mag values; the second element in this\n list is the sigma value to use for brighter flux/mag values. For\n example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma\n dimmings and greater than 3-sigma brightenings. Here the meaning of\n \"dimming\" and \"brightening\" is set by *physics* (not the magnitude\n system), which is why the `magsarefluxes` kwarg must be correctly set.\n\n If `sigclip` is None, no sigma-clipping will be performed, and the\n time-series (with non-finite elems removed) will be passed through to\n the output.\n\n verbose : bool\n If this is True, will indicate progress and details about the frequency\n grid used for the period search.\n\n raiseonfail : bool\n If True, raises an exception if something goes wrong. Otherwise, returns\n None.\n\n Returns\n -------\n\n dict\n This function returns a dict, referred to as an `lspinfo` dict in other\n astrobase functions that operate on periodogram results. This is a\n standardized format across all astrobase period-finders, and is of the\n form below::\n\n {'bestperiod': the best period value in the periodogram,\n 'bestlspval': the periodogram peak associated with the best period,\n 'nbestpeaks': the input value of nbestpeaks,\n 'nbestlspvals': nbestpeaks-size list of best period peak values,\n 'nbestperiods': nbestpeaks-size list of best periods,\n 'lspvals': the full array of periodogram powers,\n 'frequencies': the full array of frequencies considered,\n 'periods': the full array of periods considered,\n 'durations': the array of durations used to run BLS,\n 'blsresult': Astropy BLS result object (BoxLeastSquaresResult),\n 'blsmodel': Astropy BLS BoxLeastSquares object used for work,\n 'stepsize': the actual stepsize used,\n 'nfreq': the actual nfreq used,\n 'durations': the durations array used,\n 'mintransitduration': the input mintransitduration,\n 'maxtransitduration': the input maxtransitdurations,\n 'method':'bls' -> the name of the period-finder method,\n 'kwargs':{ dict of all of the input kwargs for record-keeping}}\n\n '''\n\n # get rid of nans first and sigclip\n stimes, smags, serrs = sigclip_magseries(times,\n mags,\n errs,\n magsarefluxes=magsarefluxes,\n sigclip=sigclip)\n\n # make sure there are enough points to calculate a spectrum\n if len(stimes) > 9 and len(smags) > 9 and len(serrs) > 9:\n\n # if we're setting up everything automatically\n if isinstance(autofreq, bool) and autofreq:\n\n # use heuristic to figure out best timestep\n stepsize = 0.25*mintransitduration/(stimes.max()-stimes.min())\n\n # now figure out the frequencies to use\n minfreq = 1.0/endp\n maxfreq = 1.0/startp\n nfreq = int(npceil((maxfreq - minfreq)/stepsize))\n\n # say what we're using\n if verbose:\n LOGINFO('min P: %s, max P: %s, nfreq: %s, '\n 'minfreq: %s, maxfreq: %s' % (startp, endp, nfreq,\n minfreq, maxfreq))\n LOGINFO('autofreq = True: using AUTOMATIC values for '\n 'freq stepsize: %s, ndurations: %s, '\n 'min transit duration: %s, max transit duration: %s' %\n (stepsize, ndurations,\n mintransitduration, maxtransitduration))\n\n use_autoperiod = False\n\n elif isinstance(autofreq, bool) and not autofreq:\n\n minfreq = 1.0/endp\n maxfreq = 1.0/startp\n nfreq = int(npceil((maxfreq - minfreq)/stepsize))\n\n # say what we're using\n if verbose:\n LOGINFO('min P: %s, max P: %s, nfreq: %s, '\n 'minfreq: %s, maxfreq: %s' % (startp, endp, nfreq,\n minfreq, maxfreq))\n LOGINFO('autofreq = False: using PROVIDED values for '\n 'freq stepsize: %s, ndurations: %s, '\n 'min transit duration: %s, max transit duration: %s' %\n (stepsize, ndurations,\n mintransitduration, maxtransitduration))\n\n use_autoperiod = False\n\n elif isinstance(autofreq, str) and autofreq == 'astropy':\n\n use_autoperiod = True\n minfreq = 1.0/endp\n maxfreq = 1.0/startp\n\n else:\n\n LOGERROR(\"unknown autofreq kwarg encountered. can't continue...\")\n return None\n\n # check the time-base vs. endp value\n if minfreq < (1.0/(stimes.max() - stimes.min())):\n\n if verbose:\n LOGWARNING('the requested max P = %.3f is larger than '\n 'the time base of the observations = %.3f, '\n ' will make minfreq = 2 x 1/timebase'\n % (endp, stimes.max() - stimes.min()))\n minfreq = 2.0/(stimes.max() - stimes.min())\n if verbose:\n LOGINFO('new minfreq: %s, maxfreq: %s' %\n (minfreq, maxfreq))\n\n\n # run BLS\n try:\n\n # astropy's BLS requires durations in units of time\n durations = nplinspace(mintransitduration*startp,\n maxtransitduration*startp,\n ndurations)\n\n # set up the correct units for the BLS model\n if magsarefluxes:\n\n blsmodel = BoxLeastSquares(\n stimes*u.day,\n smags*u.dimensionless_unscaled,\n dy=serrs*u.dimensionless_unscaled\n )\n\n else:\n\n blsmodel = BoxLeastSquares(\n stimes*u.day,\n smags*u.mag,\n dy=serrs*u.mag\n )\n\n # use autoperiod if requested\n if use_autoperiod:\n periods = nparray(\n blsmodel.autoperiod(\n durations,\n minimum_period=startp,\n maximum_period=endp,\n minimum_n_transit=blsmintransits,\n frequency_factor=blsfreqfactor\n )\n )\n nfreq = periods.size\n\n if verbose:\n LOGINFO(\n \"autofreq = 'astropy', used .autoperiod() with \"\n \"minimum_n_transit = %s, freq_factor = %s \"\n \"to generate the frequency grid\" %\n (blsmintransits, blsfreqfactor)\n )\n LOGINFO('stepsize = %.5f, nfreq = %s, minfreq = %.5f, '\n 'maxfreq = %.5f, ndurations = %s' %\n (abs(1.0/periods[1] - 1.0/periods[0]),\n nfreq,\n 1.0/periods.max(),\n 1.0/periods.min(),\n durations.size))\n\n # otherwise, use kbls method\n else:\n frequencies = minfreq + nparange(nfreq)*stepsize\n periods = 1.0/frequencies\n\n if nfreq > 5.0e5:\n if verbose:\n LOGWARNING('more than 5.0e5 frequencies to go through; '\n 'this will take a while. '\n 'you might want to use the '\n 'abls.bls_parallel_pfind function instead')\n\n # run the periodogram\n blsresult = blsmodel.power(\n periods*u.day,\n durations*u.day,\n objective=blsobjective,\n method=blsmethod,\n oversample=blsoversample\n )\n\n # get the peak values\n lsp = nparray(blsresult.power)\n\n # find the nbestpeaks for the periodogram: 1. sort the lsp array\n # by highest value first 2. go down the values until we find\n # five values that are separated by at least periodepsilon in\n # period\n # make sure to get only the finite peaks in the periodogram\n # this is needed because BLS may produce infs for some peaks\n finitepeakind = npisfinite(lsp)\n finlsp = lsp[finitepeakind]\n finperiods = periods[finitepeakind]\n\n # make sure that finlsp has finite values before we work on it\n try:\n\n bestperiodind = npargmax(finlsp)\n\n except ValueError:\n\n LOGERROR('no finite periodogram values '\n 'for this mag series, skipping...')\n\n return {'bestperiod':npnan,\n 'bestlspval':npnan,\n 'nbestpeaks':nbestpeaks,\n 'nbestinds':None,\n 'nbestlspvals':None,\n 'nbestperiods':None,\n 'lspvals':None,\n 'periods':None,\n 'durations':None,\n 'method':'bls',\n 'blsresult':None,\n 'blsmodel':None,\n 'kwargs':{'startp':startp,\n 'endp':endp,\n 'stepsize':stepsize,\n 'mintransitduration':mintransitduration,\n 'maxtransitduration':maxtransitduration,\n 'ndurations':ndurations,\n 'blsobjective':blsobjective,\n 'blsmethod':blsmethod,\n 'blsoversample':blsoversample,\n 'blsntransits':blsmintransits,\n 'blsfreqfactor':blsfreqfactor,\n 'autofreq':autofreq,\n 'periodepsilon':periodepsilon,\n 'nbestpeaks':nbestpeaks,\n 'sigclip':sigclip,\n 'magsarefluxes':magsarefluxes}}\n\n sortedlspind = npargsort(finlsp)[::-1]\n sortedlspperiods = finperiods[sortedlspind]\n sortedlspvals = finlsp[sortedlspind]\n\n # now get the nbestpeaks\n nbestperiods, nbestlspvals, nbestinds, peakcount = (\n [finperiods[bestperiodind]],\n [finlsp[bestperiodind]],\n [bestperiodind],\n 1\n )\n prevperiod = sortedlspperiods[0]\n\n # find the best nbestpeaks in the lsp and their periods\n for period, lspval, ind in zip(sortedlspperiods,\n sortedlspvals,\n sortedlspind):\n\n if peakcount == nbestpeaks:\n break\n perioddiff = abs(period - prevperiod)\n bestperiodsdiff = [abs(period - x) for x in nbestperiods]\n\n # print('prevperiod = %s, thisperiod = %s, '\n # 'perioddiff = %s, peakcount = %s' %\n # (prevperiod, period, perioddiff, peakcount))\n\n # this ensures that this period is different from the last\n # period and from all the other existing best periods by\n # periodepsilon to make sure we jump to an entire different\n # peak in the periodogram\n if (perioddiff > (periodepsilon*prevperiod) and\n all(x > (periodepsilon*period)\n for x in bestperiodsdiff)):\n nbestperiods.append(period)\n nbestlspvals.append(lspval)\n nbestinds.append(ind)\n peakcount = peakcount + 1\n\n prevperiod = period\n\n\n # generate the return dict\n resultdict = {\n 'bestperiod':finperiods[bestperiodind],\n 'bestlspval':finlsp[bestperiodind],\n 'nbestpeaks':nbestpeaks,\n 'nbestinds':nbestinds,\n 'nbestlspvals':nbestlspvals,\n 'nbestperiods':nbestperiods,\n 'lspvals':lsp,\n 'frequencies':frequencies,\n 'periods':periods,\n 'durations':durations,\n 'blsresult':blsresult,\n 'blsmodel':blsmodel,\n 'stepsize':stepsize,\n 'nfreq':nfreq,\n 'mintransitduration':mintransitduration,\n 'maxtransitduration':maxtransitduration,\n 'method':'bls',\n 'kwargs':{'startp':startp,\n 'endp':endp,\n 'stepsize':stepsize,\n 'mintransitduration':mintransitduration,\n 'maxtransitduration':maxtransitduration,\n 'ndurations':ndurations,\n 'blsobjective':blsobjective,\n 'blsmethod':blsmethod,\n 'blsoversample':blsoversample,\n 'blsntransits':blsmintransits,\n 'blsfreqfactor':blsfreqfactor,\n 'autofreq':autofreq,\n 'periodepsilon':periodepsilon,\n 'nbestpeaks':nbestpeaks,\n 'sigclip':sigclip,\n 'magsarefluxes':magsarefluxes}\n }\n\n return resultdict\n\n except Exception as e:\n\n LOGEXCEPTION('BLS failed!')\n\n if raiseonfail:\n raise\n\n return {'bestperiod':npnan,\n 'bestlspval':npnan,\n 'nbestinds':None,\n 'nbestpeaks':nbestpeaks,\n 'nbestlspvals':None,\n 'nbestperiods':None,\n 'lspvals':None,\n 'periods':None,\n 'durations':None,\n 'blsresult':None,\n 'blsmodel':None,\n 'stepsize':stepsize,\n 'nfreq':nfreq,\n 'mintransitduration':mintransitduration,\n 'maxtransitduration':maxtransitduration,\n 'method':'bls',\n 'kwargs':{'startp':startp,\n 'endp':endp,\n 'stepsize':stepsize,\n 'mintransitduration':mintransitduration,\n 'maxtransitduration':maxtransitduration,\n 'ndurations':ndurations,\n 'blsobjective':blsobjective,\n 'blsmethod':blsmethod,\n 'blsoversample':blsoversample,\n 'blsntransits':blsmintransits,\n 'blsfreqfactor':blsfreqfactor,\n 'autofreq':autofreq,\n 'periodepsilon':periodepsilon,\n 'nbestpeaks':nbestpeaks,\n 'sigclip':sigclip,\n 'magsarefluxes':magsarefluxes}}\n\n\n else:\n\n LOGERROR('no good detections for these times and mags, skipping...')\n return {'bestperiod':npnan,\n 'bestlspval':npnan,\n 'nbestinds':None,\n 'nbestpeaks':nbestpeaks,\n 'nbestlspvals':None,\n 'nbestperiods':None,\n 'lspvals':None,\n 'periods':None,\n 'durations':None,\n 'blsresult':None,\n 'blsmodel':None,\n 'stepsize':stepsize,\n 'nfreq':None,\n 'nphasebins':None,\n 'mintransitduration':mintransitduration,\n 'maxtransitduration':maxtransitduration,\n 'method':'bls',\n 'kwargs':{'startp':startp,\n 'endp':endp,\n 'stepsize':stepsize,\n 'mintransitduration':mintransitduration,\n 'maxtransitduration':maxtransitduration,\n 'ndurations':ndurations,\n 'blsobjective':blsobjective,\n 'blsmethod':blsmethod,\n 'blsoversample':blsoversample,\n 'blsntransits':blsmintransits,\n 'blsfreqfactor':blsfreqfactor,\n 'autofreq':autofreq,\n 'periodepsilon':periodepsilon,\n 'nbestpeaks':nbestpeaks,\n 'sigclip':sigclip,\n 'magsarefluxes':magsarefluxes}}", "response": "This function runs the BoxLeastSquares Fitting Search for transit - shaped signals."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nrunning the Box Least Squares Fitting Search for transit-shaped signals. Breaks up the full frequency space into chunks and passes them to parallel BLS workers. Based on the version of BLS in Astropy 3.1: `astropy.stats.BoxLeastSquares`. If you don't have Astropy 3.1, this module will fail to import. Note that by default, this implementation of `bls_parallel_pfind` doesn't use the `.autoperiod()` function from `BoxLeastSquares` but uses the same auto frequency-grid generation as the functions in `periodbase.kbls`. If you want to use Astropy's implementation, set the value of `autofreq` kwarg to 'astropy'. The generated period array will then be broken up into chunks and sent to the individual workers. NOTE: the combined BLS spectrum produced by this function is not identical to that produced by running BLS in one shot for the entire frequency space. There are differences on the order of 1.0e-3 or so in the respective peak values, but peaks appear at the same frequencies for both methods. This is likely due to different aliasing caused by smaller chunks of the frequency space used by the parallel workers in this function. When in doubt, confirm results for this parallel implementation by comparing to those from the serial implementation above. In particular, when you want to get reliable estimates of the SNR, transit depth, duration, etc. that Astropy's BLS gives you, rerun `bls_serial_pfind` with `startp`, and `endp` close to the best period you want to characterize the transit at. The dict returned from that function contains a `blsmodel` key, which is the generated model from Astropy's BLS. Use the `.compute_stats()` method to calculate the required stats. Parameters ---------- times,mags,errs : np.array The magnitude/flux time-series to search for transits. magsarefluxes : bool If the input measurement values in `mags` and `errs` are in fluxes, set this to True. startp,endp : float The minimum and maximum periods to consider for the transit search. stepsize : float The step-size in frequency to use when constructing a frequency grid for the period search. mintransitduration,maxtransitduration : float The minimum and maximum transitdurations (in units of phase) to consider for the transit search. ndurations : int The number of transit durations to use in the period-search. autofreq : bool or str If this is True, the values of `stepsize` and `nphasebins` will be ignored, and these, along with a frequency-grid, will be determined based on the following relations:: nphasebins = int(ceil(2.0/mintransitduration)) if nphasebins > 3000: nphasebins = 3000 stepsize = 0.25*mintransitduration/(times.max()-times.min()) minfreq = 1.0/endp maxfreq = 1.0/startp nfreq = int(ceil((maxfreq - minfreq)/stepsize)) If this is False, you must set `startp`, `endp`, and `stepsize` as appropriate. If this is str == 'astropy', will use the `astropy.stats.BoxLeastSquares.autoperiod()` function to calculate the frequency grid instead of the kbls method. blsobjective : {'likelihood','snr'} Sets the type of objective to optimize in the `BoxLeastSquares.power()` function. blsmethod : {'fast','slow'} Sets the type of method to use in the `BoxLeastSquares.power()` function. blsoversample : {'likelihood','snr'} Sets the `oversample` kwarg for the `BoxLeastSquares.power()` function. blsmintransits : int Sets the `min_n_transits` kwarg for the `BoxLeastSquares.autoperiod()` function. blsfreqfactor : float Sets the `frequency_factor` kwarg for the `BoxLeastSquares.autoperiod()` function. periodepsilon : float The fractional difference between successive values of 'best' periods when sorting by periodogram power to consider them as separate periods (as opposed to part of the same periodogram peak). This is used to avoid broad peaks in the periodogram and make sure the 'best' periods returned are all actually independent. nbestpeaks : int The number of 'best' peaks to return from the periodogram results, starting from the global maximum of the periodogram peak values. sigclip : float or int or sequence of two floats/ints or None If a single float or int, a symmetric sigma-clip will be performed using the number provided as the sigma-multiplier to cut out from the input time-series. If a list of two ints/floats is provided, the function will perform an 'asymmetric' sigma-clip. The first element in this list is the sigma value to use for fainter flux/mag values; the second element in this list is the sigma value to use for brighter flux/mag values. For example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma dimmings and greater than 3-sigma brightenings. Here the meaning of \"dimming\" and \"brightening\" is set by *physics* (not the magnitude system), which is why the `magsarefluxes` kwarg must be correctly set. If `sigclip` is None, no sigma-clipping will be performed, and the time-series (with non-finite elems removed) will be passed through to the output. verbose : bool If this is True, will indicate progress and details about the frequency grid used for the period search. nworkers : int or None The number of parallel workers to launch for period-search. If None, nworkers = NCPUS. Returns ------- dict This function returns a dict, referred to as an `lspinfo` dict in other astrobase functions that operate on periodogram results. This is a standardized format across all astrobase period-finders, and is of the form below:: {'bestperiod': the best period value in the periodogram, 'bestlspval': the periodogram peak associated with the best period, 'nbestpeaks': the input value of nbestpeaks, 'nbestlspvals': nbestpeaks-size list of best period peak values, 'nbestperiods': nbestpeaks-size list of best periods, 'lspvals': the full array of periodogram powers, 'frequencies': the full array of frequencies considered, 'periods': the full array of periods considered, 'durations': the array of durations used to run BLS, 'blsresult': Astropy BLS result object (BoxLeastSquaresResult), 'blsmodel': Astropy BLS BoxLeastSquares object used for work, 'stepsize': the actual stepsize used, 'nfreq': the actual nfreq used, 'durations': the durations array used, 'mintransitduration': the input mintransitduration, 'maxtransitduration': the input maxtransitdurations, 'method':'bls' -> the name of the period-finder method, 'kwargs':{ dict of all of the input kwargs for record-keeping}}", "response": "def bls_parallel_pfind(\n times, mags, errs,\n magsarefluxes=False,\n startp=0.1, # by default, search from 0.1 d to...\n endp=100.0, # ... 100.0 d -- don't search full timebase\n stepsize=1.0e-4,\n mintransitduration=0.01, # minimum transit length in phase\n maxtransitduration=0.4, # maximum transit length in phase\n ndurations=100,\n autofreq=True, # figure out f0, nf, and df automatically\n blsobjective='likelihood',\n blsmethod='fast',\n blsoversample=5,\n blsmintransits=3,\n blsfreqfactor=10.0,\n nbestpeaks=5,\n periodepsilon=0.1, # 0.1\n sigclip=10.0,\n verbose=True,\n nworkers=None,\n):\n '''Runs the Box Least Squares Fitting Search for transit-shaped signals.\n\n Breaks up the full frequency space into chunks and passes them to parallel\n BLS workers.\n\n Based on the version of BLS in Astropy 3.1:\n `astropy.stats.BoxLeastSquares`. If you don't have Astropy 3.1, this module\n will fail to import. Note that by default, this implementation of\n `bls_parallel_pfind` doesn't use the `.autoperiod()` function from\n `BoxLeastSquares` but uses the same auto frequency-grid generation as the\n functions in `periodbase.kbls`. If you want to use Astropy's implementation,\n set the value of `autofreq` kwarg to 'astropy'. The generated period array\n will then be broken up into chunks and sent to the individual workers.\n\n NOTE: the combined BLS spectrum produced by this function is not identical\n to that produced by running BLS in one shot for the entire frequency\n space. There are differences on the order of 1.0e-3 or so in the respective\n peak values, but peaks appear at the same frequencies for both methods. This\n is likely due to different aliasing caused by smaller chunks of the\n frequency space used by the parallel workers in this function. When in\n doubt, confirm results for this parallel implementation by comparing to\n those from the serial implementation above.\n\n In particular, when you want to get reliable estimates of the SNR, transit\n depth, duration, etc. that Astropy's BLS gives you, rerun `bls_serial_pfind`\n with `startp`, and `endp` close to the best period you want to characterize\n the transit at. The dict returned from that function contains a `blsmodel`\n key, which is the generated model from Astropy's BLS. Use the\n `.compute_stats()` method to calculate the required stats.\n\n Parameters\n ----------\n\n times,mags,errs : np.array\n The magnitude/flux time-series to search for transits.\n\n magsarefluxes : bool\n If the input measurement values in `mags` and `errs` are in fluxes, set\n this to True.\n\n startp,endp : float\n The minimum and maximum periods to consider for the transit search.\n\n stepsize : float\n The step-size in frequency to use when constructing a frequency grid for\n the period search.\n\n mintransitduration,maxtransitduration : float\n The minimum and maximum transitdurations (in units of phase) to consider\n for the transit search.\n\n ndurations : int\n The number of transit durations to use in the period-search.\n\n autofreq : bool or str\n If this is True, the values of `stepsize` and `nphasebins` will be\n ignored, and these, along with a frequency-grid, will be determined\n based on the following relations::\n\n nphasebins = int(ceil(2.0/mintransitduration))\n if nphasebins > 3000:\n nphasebins = 3000\n\n stepsize = 0.25*mintransitduration/(times.max()-times.min())\n\n minfreq = 1.0/endp\n maxfreq = 1.0/startp\n nfreq = int(ceil((maxfreq - minfreq)/stepsize))\n\n If this is False, you must set `startp`, `endp`, and `stepsize` as\n appropriate.\n\n If this is str == 'astropy', will use the\n `astropy.stats.BoxLeastSquares.autoperiod()` function to calculate the\n frequency grid instead of the kbls method.\n\n blsobjective : {'likelihood','snr'}\n Sets the type of objective to optimize in the `BoxLeastSquares.power()`\n function.\n\n blsmethod : {'fast','slow'}\n Sets the type of method to use in the `BoxLeastSquares.power()`\n function.\n\n blsoversample : {'likelihood','snr'}\n Sets the `oversample` kwarg for the `BoxLeastSquares.power()` function.\n\n blsmintransits : int\n Sets the `min_n_transits` kwarg for the `BoxLeastSquares.autoperiod()`\n function.\n\n blsfreqfactor : float\n Sets the `frequency_factor` kwarg for the `BoxLeastSquares.autoperiod()`\n function.\n\n periodepsilon : float\n The fractional difference between successive values of 'best' periods\n when sorting by periodogram power to consider them as separate periods\n (as opposed to part of the same periodogram peak). This is used to avoid\n broad peaks in the periodogram and make sure the 'best' periods returned\n are all actually independent.\n\n nbestpeaks : int\n The number of 'best' peaks to return from the periodogram results,\n starting from the global maximum of the periodogram peak values.\n\n sigclip : float or int or sequence of two floats/ints or None\n If a single float or int, a symmetric sigma-clip will be performed using\n the number provided as the sigma-multiplier to cut out from the input\n time-series.\n\n If a list of two ints/floats is provided, the function will perform an\n 'asymmetric' sigma-clip. The first element in this list is the sigma\n value to use for fainter flux/mag values; the second element in this\n list is the sigma value to use for brighter flux/mag values. For\n example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma\n dimmings and greater than 3-sigma brightenings. Here the meaning of\n \"dimming\" and \"brightening\" is set by *physics* (not the magnitude\n system), which is why the `magsarefluxes` kwarg must be correctly set.\n\n If `sigclip` is None, no sigma-clipping will be performed, and the\n time-series (with non-finite elems removed) will be passed through to\n the output.\n\n verbose : bool\n If this is True, will indicate progress and details about the frequency\n grid used for the period search.\n\n nworkers : int or None\n The number of parallel workers to launch for period-search. If None,\n nworkers = NCPUS.\n\n Returns\n -------\n\n dict\n This function returns a dict, referred to as an `lspinfo` dict in other\n astrobase functions that operate on periodogram results. This is a\n standardized format across all astrobase period-finders, and is of the\n form below::\n\n {'bestperiod': the best period value in the periodogram,\n 'bestlspval': the periodogram peak associated with the best period,\n 'nbestpeaks': the input value of nbestpeaks,\n 'nbestlspvals': nbestpeaks-size list of best period peak values,\n 'nbestperiods': nbestpeaks-size list of best periods,\n 'lspvals': the full array of periodogram powers,\n 'frequencies': the full array of frequencies considered,\n 'periods': the full array of periods considered,\n 'durations': the array of durations used to run BLS,\n 'blsresult': Astropy BLS result object (BoxLeastSquaresResult),\n 'blsmodel': Astropy BLS BoxLeastSquares object used for work,\n 'stepsize': the actual stepsize used,\n 'nfreq': the actual nfreq used,\n 'durations': the durations array used,\n 'mintransitduration': the input mintransitduration,\n 'maxtransitduration': the input maxtransitdurations,\n 'method':'bls' -> the name of the period-finder method,\n 'kwargs':{ dict of all of the input kwargs for record-keeping}}\n\n '''\n\n # get rid of nans first and sigclip\n stimes, smags, serrs = sigclip_magseries(times,\n mags,\n errs,\n magsarefluxes=magsarefluxes,\n sigclip=sigclip)\n\n # make sure there are enough points to calculate a spectrum\n if len(stimes) > 9 and len(smags) > 9 and len(serrs) > 9:\n\n # if we're setting up everything automatically\n if isinstance(autofreq, bool) and autofreq:\n\n # use heuristic to figure out best timestep\n stepsize = 0.25*mintransitduration/(stimes.max()-stimes.min())\n\n # now figure out the frequencies to use\n minfreq = 1.0/endp\n maxfreq = 1.0/startp\n nfreq = int(npceil((maxfreq - minfreq)/stepsize))\n\n # say what we're using\n if verbose:\n LOGINFO('min P: %s, max P: %s, nfreq: %s, '\n 'minfreq: %s, maxfreq: %s' % (startp, endp, nfreq,\n minfreq, maxfreq))\n LOGINFO('autofreq = True: using AUTOMATIC values for '\n 'freq stepsize: %s, ndurations: %s, '\n 'min transit duration: %s, max transit duration: %s' %\n (stepsize, ndurations,\n mintransitduration, maxtransitduration))\n\n use_autoperiod = False\n\n elif isinstance(autofreq, bool) and not autofreq:\n\n minfreq = 1.0/endp\n maxfreq = 1.0/startp\n nfreq = int(npceil((maxfreq - minfreq)/stepsize))\n\n # say what we're using\n if verbose:\n LOGINFO('min P: %s, max P: %s, nfreq: %s, '\n 'minfreq: %s, maxfreq: %s' % (startp, endp, nfreq,\n minfreq, maxfreq))\n LOGINFO('autofreq = False: using PROVIDED values for '\n 'freq stepsize: %s, ndurations: %s, '\n 'min transit duration: %s, max transit duration: %s' %\n (stepsize, ndurations,\n mintransitduration, maxtransitduration))\n\n use_autoperiod = False\n\n elif isinstance(autofreq, str) and autofreq == 'astropy':\n\n use_autoperiod = True\n minfreq = 1.0/endp\n maxfreq = 1.0/startp\n\n else:\n\n LOGERROR(\"unknown autofreq kwarg encountered. can't continue...\")\n return None\n\n # check the minimum frequency\n if minfreq < (1.0/(stimes.max() - stimes.min())):\n\n minfreq = 2.0/(stimes.max() - stimes.min())\n if verbose:\n LOGWARNING('the requested max P = %.3f is larger than '\n 'the time base of the observations = %.3f, '\n ' will make minfreq = 2 x 1/timebase'\n % (endp, stimes.max() - stimes.min()))\n LOGINFO('new minfreq: %s, maxfreq: %s' %\n (minfreq, maxfreq))\n\n\n #############################\n ## NOW RUN BLS IN PARALLEL ##\n #############################\n\n # fix number of CPUs if needed\n if not nworkers or nworkers > NCPUS:\n nworkers = NCPUS\n if verbose:\n LOGINFO('using %s workers...' % nworkers)\n\n # check if autoperiod is True and get the correct period-grid\n if use_autoperiod:\n\n # astropy's BLS requires durations in units of time\n durations = nplinspace(mintransitduration*startp,\n maxtransitduration*startp,\n ndurations)\n\n # set up the correct units for the BLS model\n if magsarefluxes:\n\n blsmodel = BoxLeastSquares(\n stimes*u.day,\n smags*u.dimensionless_unscaled,\n dy=serrs*u.dimensionless_unscaled\n )\n\n else:\n\n blsmodel = BoxLeastSquares(\n stimes*u.day,\n smags*u.mag,\n dy=serrs*u.mag\n )\n\n periods = nparray(\n blsmodel.autoperiod(\n durations*u.day,\n minimum_period=startp,\n maximum_period=endp,\n minimum_n_transit=blsmintransits,\n frequency_factor=blsfreqfactor\n )\n )\n\n frequencies = 1.0/periods\n nfreq = frequencies.size\n\n if verbose:\n LOGINFO(\n \"autofreq = 'astropy', used .autoperiod() with \"\n \"minimum_n_transit = %s, freq_factor = %s \"\n \"to generate the frequency grid\" %\n (blsmintransits, blsfreqfactor)\n )\n LOGINFO('stepsize = %s, nfreq = %s, minfreq = %.5f, '\n 'maxfreq = %.5f, ndurations = %s' %\n (abs(frequencies[1] - frequencies[0]),\n nfreq,\n 1.0/periods.max(),\n 1.0/periods.min(),\n durations.size))\n\n del blsmodel\n del durations\n\n # otherwise, use kbls method\n else:\n\n frequencies = minfreq + nparange(nfreq)*stepsize\n\n\n # break up the tasks into chunks\n csrem = int(fmod(nfreq, nworkers))\n csint = int(float(nfreq/nworkers))\n chunk_minfreqs, chunk_nfreqs = [], []\n\n for x in range(nworkers):\n\n this_minfreqs = frequencies[x*csint]\n\n # handle usual nfreqs\n if x < (nworkers - 1):\n this_nfreqs = frequencies[x*csint:x*csint+csint].size\n else:\n this_nfreqs = frequencies[x*csint:x*csint+csint+csrem].size\n\n chunk_minfreqs.append(this_minfreqs)\n chunk_nfreqs.append(this_nfreqs)\n\n\n # populate the tasks list\n #\n # task[0] = times\n # task[1] = mags\n # task[2] = errs\n # task[3] = magsarefluxes\n\n # task[4] = minfreq\n # task[5] = nfreq\n # task[6] = stepsize\n\n # task[7] = nphasebins\n # task[8] = mintransitduration\n # task[9] = maxtransitduration\n\n # task[10] = blsobjective\n # task[11] = blsmethod\n # task[12] = blsoversample\n\n # populate the tasks list\n tasks = [(stimes, smags, serrs, magsarefluxes,\n chunk_minf, chunk_nf, stepsize,\n ndurations, mintransitduration, maxtransitduration,\n blsobjective, blsmethod, blsoversample)\n for (chunk_minf, chunk_nf)\n in zip(chunk_minfreqs, chunk_nfreqs)]\n\n if verbose:\n for ind, task in enumerate(tasks):\n LOGINFO('worker %s: minfreq = %.6f, nfreqs = %s' %\n (ind+1, task[4], task[5]))\n LOGINFO('running...')\n\n # return tasks\n\n # start the pool\n pool = Pool(nworkers)\n results = pool.map(_parallel_bls_worker, tasks)\n\n pool.close()\n pool.join()\n del pool\n\n # now concatenate the output lsp arrays\n lsp = npconcatenate([x['power'] for x in results])\n periods = 1.0/frequencies\n\n # find the nbestpeaks for the periodogram: 1. sort the lsp array\n # by highest value first 2. go down the values until we find\n # five values that are separated by at least periodepsilon in\n # period\n # make sure to get only the finite peaks in the periodogram\n # this is needed because BLS may produce infs for some peaks\n finitepeakind = npisfinite(lsp)\n finlsp = lsp[finitepeakind]\n finperiods = periods[finitepeakind]\n\n # make sure that finlsp has finite values before we work on it\n try:\n\n bestperiodind = npargmax(finlsp)\n\n except ValueError:\n\n LOGERROR('no finite periodogram values '\n 'for this mag series, skipping...')\n\n return {'bestperiod':npnan,\n 'bestlspval':npnan,\n 'nbestpeaks':nbestpeaks,\n 'nbestinds':None,\n 'nbestlspvals':None,\n 'nbestperiods':None,\n 'lspvals':None,\n 'periods':None,\n 'durations':None,\n 'method':'bls',\n 'blsresult':None,\n 'blsmodel':None,\n 'kwargs':{'startp':startp,\n 'endp':endp,\n 'stepsize':stepsize,\n 'mintransitduration':mintransitduration,\n 'maxtransitduration':maxtransitduration,\n 'ndurations':ndurations,\n 'blsobjective':blsobjective,\n 'blsmethod':blsmethod,\n 'blsoversample':blsoversample,\n 'autofreq':autofreq,\n 'periodepsilon':periodepsilon,\n 'nbestpeaks':nbestpeaks,\n 'sigclip':sigclip,\n 'magsarefluxes':magsarefluxes}}\n\n sortedlspind = npargsort(finlsp)[::-1]\n sortedlspperiods = finperiods[sortedlspind]\n sortedlspvals = finlsp[sortedlspind]\n\n # now get the nbestpeaks\n nbestperiods, nbestlspvals, nbestinds, peakcount = (\n [finperiods[bestperiodind]],\n [finlsp[bestperiodind]],\n [bestperiodind],\n 1\n )\n prevperiod = sortedlspperiods[0]\n\n # find the best nbestpeaks in the lsp and their periods\n for period, lspval, ind in zip(sortedlspperiods,\n sortedlspvals,\n sortedlspind):\n\n if peakcount == nbestpeaks:\n break\n perioddiff = abs(period - prevperiod)\n bestperiodsdiff = [abs(period - x) for x in nbestperiods]\n\n # this ensures that this period is different from the last\n # period and from all the other existing best periods by\n # periodepsilon to make sure we jump to an entire different\n # peak in the periodogram\n if (perioddiff > (periodepsilon*prevperiod) and\n all(x > (periodepsilon*period)\n for x in bestperiodsdiff)):\n nbestperiods.append(period)\n nbestlspvals.append(lspval)\n nbestinds.append(ind)\n peakcount = peakcount + 1\n\n prevperiod = period\n\n\n # generate the return dict\n resultdict = {\n 'bestperiod':finperiods[bestperiodind],\n 'bestlspval':finlsp[bestperiodind],\n 'nbestpeaks':nbestpeaks,\n 'nbestinds':nbestinds,\n 'nbestlspvals':nbestlspvals,\n 'nbestperiods':nbestperiods,\n 'lspvals':lsp,\n 'frequencies':frequencies,\n 'periods':periods,\n 'durations':[x['durations'] for x in results],\n 'blsresult':[x['blsresult'] for x in results],\n 'blsmodel':[x['blsmodel'] for x in results],\n 'stepsize':stepsize,\n 'nfreq':nfreq,\n 'mintransitduration':mintransitduration,\n 'maxtransitduration':maxtransitduration,\n 'method':'bls',\n 'kwargs':{'startp':startp,\n 'endp':endp,\n 'stepsize':stepsize,\n 'mintransitduration':mintransitduration,\n 'maxtransitduration':maxtransitduration,\n 'ndurations':ndurations,\n 'blsobjective':blsobjective,\n 'blsmethod':blsmethod,\n 'blsoversample':blsoversample,\n 'autofreq':autofreq,\n 'periodepsilon':periodepsilon,\n 'nbestpeaks':nbestpeaks,\n 'sigclip':sigclip,\n 'magsarefluxes':magsarefluxes}\n }\n\n return resultdict\n\n\n else:\n\n LOGERROR('no good detections for these times and mags, skipping...')\n return {'bestperiod':npnan,\n 'bestlspval':npnan,\n 'nbestinds':None,\n 'nbestpeaks':nbestpeaks,\n 'nbestlspvals':None,\n 'nbestperiods':None,\n 'lspvals':None,\n 'periods':None,\n 'durations':None,\n 'blsresult':None,\n 'blsmodel':None,\n 'stepsize':stepsize,\n 'nfreq':None,\n 'nphasebins':None,\n 'mintransitduration':mintransitduration,\n 'maxtransitduration':maxtransitduration,\n 'method':'bls',\n 'kwargs':{'startp':startp,\n 'endp':endp,\n 'stepsize':stepsize,\n 'mintransitduration':mintransitduration,\n 'maxtransitduration':maxtransitduration,\n 'ndurations':ndurations,\n 'blsobjective':blsobjective,\n 'blsmethod':blsmethod,\n 'blsoversample':blsoversample,\n 'autofreq':autofreq,\n 'periodepsilon':periodepsilon,\n 'nbestpeaks':nbestpeaks,\n 'sigclip':sigclip,\n 'magsarefluxes':magsarefluxes}}"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _parse_csv_header(header):\n '''This parses a CSV header from a K2 CSV LC.\n\n Returns a dict that can be used to update an existing lcdict with the\n relevant metadata info needed to form a full LC.\n\n '''\n\n # first, break into lines\n headerlines = header.split('\\n')\n headerlines = [x.lstrip('# ') for x in headerlines]\n\n # next, find the indices of the '# COLUMNS' line and '# LIGHTCURVE' line\n metadatastart = headerlines.index('METADATA')\n columnstart = headerlines.index('COLUMNS')\n lcstart = headerlines.index('LIGHTCURVE')\n\n # get the lines for the metadata and columndefs\n metadata = headerlines[metadatastart+1:columnstart-1]\n columndefs = headerlines[columnstart+1:lcstart-1]\n\n # parse the metadata\n metainfo = [x.split(',') for x in metadata][:-1]\n aperpixradius = metadata[-1]\n\n objectid, kepid, ucac4id, kepmag = metainfo[0]\n objectid, kepid, ucac4id, kepmag = (objectid.split(' = ')[-1],\n kepid.split(' = ')[-1],\n ucac4id.split(' = ')[-1],\n kepmag.split(' = ')[-1])\n kepmag = float(kepmag) if kepmag else None\n\n ra, decl, ndet, k2campaign = metainfo[1]\n ra, decl, ndet, k2campaign = (ra.split(' = ')[-1],\n decl.split(' = ')[-1],\n int(ndet.split(' = ')[-1]),\n int(k2campaign.split(' = ')[-1]))\n\n fovccd, fovchannel, fovmodule = metainfo[2]\n fovccd, fovchannel, fovmodule = (int(fovccd.split(' = ')[-1]),\n int(fovchannel.split(' = ')[-1]),\n int(fovmodule.split(' = ')[-1]))\n\n try:\n qualflag, bjdoffset, napertures = metainfo[3]\n qualflag, bjdoffset, napertures = (int(qualflag.split(' = ')[-1]),\n float(bjdoffset.split(' = ')[-1]),\n int(napertures.split(' = ')[-1]))\n kernelspec = None\n except Exception as e:\n qualflag, bjdoffset, napertures, kernelspec = metainfo[3]\n qualflag, bjdoffset, napertures, kernelspec = (\n int(qualflag.split(' = ')[-1]),\n float(bjdoffset.split(' = ')[-1]),\n int(napertures.split(' = ')[-1]),\n str(kernelspec.split(' = ')[-1])\n )\n\n aperpixradius = aperpixradius.split(' = ')[-1].split(',')\n aperpixradius = [float(x) for x in aperpixradius]\n\n # parse the columndefs\n columns = [x.split(' - ')[1] for x in columndefs]\n\n metadict = {'objectid':objectid,\n 'objectinfo':{\n 'objectid':objectid,\n 'kepid':kepid,\n 'ucac4id':ucac4id,\n 'kepmag':kepmag,\n 'ra':ra,\n 'decl':decl,\n 'ndet':ndet,\n 'k2campaign':k2campaign,\n 'fovccd':fovccd,\n 'fovchannel':fovchannel,\n 'fovmodule':fovmodule,\n 'qualflag':qualflag,\n 'bjdoffset':bjdoffset,\n 'napertures':napertures,\n 'kernelspec':kernelspec,\n 'aperpixradius':aperpixradius,\n },\n 'columns':columns}\n\n return metadict", "response": "This function parses a CSV header from a K2 CSV LC."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreads in a K2 light curve in CSV format. Transparently reads gzipped K2 light curve file and returns a dict containing the keys of each column as keys and values of each column as values.", "response": "def read_csv_lightcurve(lcfile):\n '''\n This reads in a K2 lightcurve in CSV format. Transparently reads gzipped\n files.\n\n Parameters\n ----------\n\n lcfile : str\n The light curve file to read.\n\n Returns\n -------\n\n dict\n Returns an lcdict.\n\n '''\n\n # read in the file first\n if '.gz' in os.path.basename(lcfile):\n LOGINFO('reading gzipped K2 LC: %s' % lcfile)\n infd = gzip.open(lcfile,'rb')\n else:\n LOGINFO('reading K2 LC: %s' % lcfile)\n infd = open(lcfile,'rb')\n\n lctext = infd.read().decode()\n infd.close()\n\n # figure out the header and get the LC columns\n lcstart = lctext.index('# LIGHTCURVE\\n')\n lcheader = lctext[:lcstart+12]\n lccolumns = lctext[lcstart+13:].split('\\n')\n lccolumns = [x.split(',') for x in lccolumns if len(x) > 0]\n\n # initialize the lcdict and parse the CSV header\n lcdict = _parse_csv_header(lcheader)\n\n # tranpose the LC rows into columns\n lccolumns = list(zip(*lccolumns))\n\n # write the columns to the dict\n for colind, col in enumerate(lcdict['columns']):\n\n # this picks out the caster to use when reading each column using the\n # definitions in the lcutils.COLUMNDEFS dictionary\n lcdict[col.lower()] = np.array([COLUMNDEFS[col][2](x)\n for x in lccolumns[colind]])\n\n lcdict['columns'] = [x.lower() for x in lcdict['columns']]\n\n return lcdict"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_starfeatures(lcfile,\n outdir,\n kdtree,\n objlist,\n lcflist,\n neighbor_radius_arcsec,\n deredden=True,\n custom_bandpasses=None,\n lcformat='hat-sql',\n lcformatdir=None):\n '''This runs the functions from :py:func:`astrobase.varclass.starfeatures`\n on a single light curve file.\n\n Parameters\n ----------\n\n lcfile : str\n This is the LC file to extract star features for.\n\n outdir : str\n This is the directory to write the output pickle to.\n\n kdtree: scipy.spatial.cKDTree\n This is a `scipy.spatial.KDTree` or `cKDTree` used to calculate neighbor\n proximity features. This is for the light curve catalog this object is\n in.\n\n objlist : np.array\n This is a Numpy array of object IDs in the same order as the\n `kdtree.data` np.array. This is for the light curve catalog this object\n is in.\n\n lcflist : np.array\n This is a Numpy array of light curve filenames in the same order as\n `kdtree.data`. This is for the light curve catalog this object is in.\n\n neighbor_radius_arcsec : float\n This indicates the radius in arcsec to search for neighbors for this\n object using the light curve catalog's `kdtree`, `objlist`, `lcflist`,\n and in GAIA.\n\n deredden : bool\n This controls if the colors and any color classifications will be\n dereddened using 2MASS DUST.\n\n custom_bandpasses : dict or None\n This is a dict used to define any custom bandpasses in the\n `in_objectinfo` dict you want to make this function aware of and\n generate colors for. Use the format below for this dict::\n\n {\n '':{'dustkey':'',\n 'label':''\n 'colors':[['-',\n ' - '],\n ['-',\n ' - ']]},\n .\n ...\n .\n '':{'dustkey':'',\n 'label':''\n 'colors':[['-',\n ' - '],\n ['-',\n ' - ']]},\n }\n\n Where:\n\n `bandpass_key` is a key to use to refer to this bandpass in the\n `objectinfo` dict, e.g. 'sdssg' for SDSS g band\n\n `twomass_dust_key` is the key to use in the 2MASS DUST result table for\n reddening per band-pass. For example, given the following DUST result\n table (using http://irsa.ipac.caltech.edu/applications/DUST/)::\n\n |Filter_name|LamEff |A_over_E_B_V_SandF|A_SandF|A_over_E_B_V_SFD|A_SFD|\n |char |float |float |float |float |float|\n | |microns| |mags | |mags |\n CTIO U 0.3734 4.107 0.209 4.968 0.253\n CTIO B 0.4309 3.641 0.186 4.325 0.221\n CTIO V 0.5517 2.682 0.137 3.240 0.165\n .\n .\n ...\n\n The `twomass_dust_key` for 'vmag' would be 'CTIO V'. If you want to\n skip DUST lookup and want to pass in a specific reddening magnitude\n for your bandpass, use a float for the value of\n `twomass_dust_key`. If you want to skip DUST lookup entirely for\n this bandpass, use None for the value of `twomass_dust_key`.\n\n `band_label` is the label to use for this bandpass, e.g. 'W1' for\n WISE-1 band, 'u' for SDSS u, etc.\n\n The 'colors' list contains color definitions for all colors you want\n to generate using this bandpass. this list contains elements of the\n form::\n\n ['-',' - ']\n\n where the the first item is the bandpass keys making up this color,\n and the second item is the label for this color to be used by the\n frontends. An example::\n\n ['sdssu-sdssg','u - g']\n\n lcformat : str\n This is the `formatkey` associated with your light curve format, which\n you previously passed in to the `lcproc.register_lcformat`\n function. This will be used to look up how to find and read the light\n curves specified in `basedir` or `use_list_of_filenames`.\n\n lcformatdir : str or None\n If this is provided, gives the path to a directory when you've stored\n your lcformat description JSONs, other than the usual directories lcproc\n knows to search for them in. Use this along with `lcformat` to specify\n an LC format JSON file that's not currently registered with lcproc.\n\n Returns\n -------\n\n str\n Path to the output pickle containing all of the star features for this\n object.\n\n '''\n\n try:\n formatinfo = get_lcformat(lcformat,\n use_lcformat_dir=lcformatdir)\n if formatinfo:\n (dfileglob, readerfunc,\n dtimecols, dmagcols, derrcols,\n magsarefluxes, normfunc) = formatinfo\n else:\n LOGERROR(\"can't figure out the light curve format\")\n return None\n except Exception as e:\n LOGEXCEPTION(\"can't figure out the light curve format\")\n return None\n\n try:\n\n # get the LC into a dict\n lcdict = readerfunc(lcfile)\n\n # this should handle lists/tuples being returned by readerfunc\n # we assume that the first element is the actual lcdict\n # FIXME: figure out how to not need this assumption\n if ( (isinstance(lcdict, (list, tuple))) and\n (isinstance(lcdict[0], dict)) ):\n lcdict = lcdict[0]\n\n resultdict = {'objectid':lcdict['objectid'],\n 'info':lcdict['objectinfo'],\n 'lcfbasename':os.path.basename(lcfile)}\n\n # run the coord features first\n coordfeat = starfeatures.coord_features(lcdict['objectinfo'])\n\n # next, run the color features\n colorfeat = starfeatures.color_features(\n lcdict['objectinfo'],\n deredden=deredden,\n custom_bandpasses=custom_bandpasses\n )\n\n # run a rough color classification\n colorclass = starfeatures.color_classification(colorfeat,\n coordfeat)\n\n # finally, run the neighbor features\n nbrfeat = starfeatures.neighbor_gaia_features(lcdict['objectinfo'],\n kdtree,\n neighbor_radius_arcsec)\n\n # get the objectids of the neighbors found if any\n if nbrfeat['nbrindices'].size > 0:\n nbrfeat['nbrobjectids'] = objlist[nbrfeat['nbrindices']]\n nbrfeat['closestnbrobjectid'] = objlist[\n nbrfeat['closestdistnbrind']\n ]\n nbrfeat['closestnbrlcfname'] = lcflist[\n nbrfeat['closestdistnbrind']\n ]\n\n else:\n nbrfeat['nbrobjectids'] = np.array([])\n nbrfeat['closestnbrobjectid'] = np.array([])\n nbrfeat['closestnbrlcfname'] = np.array([])\n\n # update the result dict\n resultdict.update(coordfeat)\n resultdict.update(colorfeat)\n resultdict.update(colorclass)\n resultdict.update(nbrfeat)\n\n outfile = os.path.join(outdir,\n 'starfeatures-%s.pkl' %\n squeeze(resultdict['objectid']).replace(' ','-'))\n\n with open(outfile, 'wb') as outfd:\n pickle.dump(resultdict, outfd, protocol=4)\n\n return outfile\n\n except Exception as e:\n\n LOGEXCEPTION('failed to get star features for %s because: %s' %\n (os.path.basename(lcfile), e))\n return None", "response": "This function runs the functions from astrobase. varclass. starfeatures on a single light curve file and returns the star features for that object."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef serial_starfeatures(lclist,\n outdir,\n lc_catalog_pickle,\n neighbor_radius_arcsec,\n maxobjects=None,\n deredden=True,\n custom_bandpasses=None,\n lcformat='hat-sql',\n lcformatdir=None):\n '''This drives the `get_starfeatures` function for a collection of LCs.\n\n Parameters\n ----------\n\n lclist : list of str\n The list of light curve file names to process.\n\n outdir : str\n The output directory where the results will be placed.\n\n lc_catalog_pickle : str\n The path to a catalog containing at a dict with least:\n\n - an object ID array accessible with `dict['objects']['objectid']`\n\n - an LC filename array accessible with `dict['objects']['lcfname']`\n\n - a `scipy.spatial.KDTree` or `cKDTree` object to use for finding\n neighbors for each object accessible with `dict['kdtree']`\n\n A catalog pickle of the form needed can be produced using\n :py:func:`astrobase.lcproc.catalogs.make_lclist` or\n :py:func:`astrobase.lcproc.catalogs.filter_lclist`.\n\n neighbor_radius_arcsec : float\n This indicates the radius in arcsec to search for neighbors for this\n object using the light curve catalog's `kdtree`, `objlist`, `lcflist`,\n and in GAIA.\n\n maxobjects : int\n The number of objects to process from `lclist`.\n\n deredden : bool\n This controls if the colors and any color classifications will be\n dereddened using 2MASS DUST.\n\n custom_bandpasses : dict or None\n This is a dict used to define any custom bandpasses in the\n `in_objectinfo` dict you want to make this function aware of and\n generate colors for. Use the format below for this dict::\n\n {\n '':{'dustkey':'',\n 'label':''\n 'colors':[['-',\n ' - '],\n ['-',\n ' - ']]},\n .\n ...\n .\n '':{'dustkey':'',\n 'label':''\n 'colors':[['-',\n ' - '],\n ['-',\n ' - ']]},\n }\n\n Where:\n\n `bandpass_key` is a key to use to refer to this bandpass in the\n `objectinfo` dict, e.g. 'sdssg' for SDSS g band\n\n `twomass_dust_key` is the key to use in the 2MASS DUST result table for\n reddening per band-pass. For example, given the following DUST result\n table (using http://irsa.ipac.caltech.edu/applications/DUST/)::\n\n |Filter_name|LamEff |A_over_E_B_V_SandF|A_SandF|A_over_E_B_V_SFD|A_SFD|\n |char |float |float |float |float |float|\n | |microns| |mags | |mags |\n CTIO U 0.3734 4.107 0.209 4.968 0.253\n CTIO B 0.4309 3.641 0.186 4.325 0.221\n CTIO V 0.5517 2.682 0.137 3.240 0.165\n .\n .\n ...\n\n The `twomass_dust_key` for 'vmag' would be 'CTIO V'. If you want to\n skip DUST lookup and want to pass in a specific reddening magnitude\n for your bandpass, use a float for the value of\n `twomass_dust_key`. If you want to skip DUST lookup entirely for\n this bandpass, use None for the value of `twomass_dust_key`.\n\n `band_label` is the label to use for this bandpass, e.g. 'W1' for\n WISE-1 band, 'u' for SDSS u, etc.\n\n The 'colors' list contains color definitions for all colors you want\n to generate using this bandpass. this list contains elements of the\n form::\n\n ['-',' - ']\n\n where the the first item is the bandpass keys making up this color,\n and the second item is the label for this color to be used by the\n frontends. An example::\n\n ['sdssu-sdssg','u - g']\n\n lcformat : str\n This is the `formatkey` associated with your light curve format, which\n you previously passed in to the `lcproc.register_lcformat`\n function. This will be used to look up how to find and read the light\n curves specified in `basedir` or `use_list_of_filenames`.\n\n lcformatdir : str or None\n If this is provided, gives the path to a directory when you've stored\n your lcformat description JSONs, other than the usual directories lcproc\n knows to search for them in. Use this along with `lcformat` to specify\n an LC format JSON file that's not currently registered with lcproc.\n\n Returns\n -------\n\n list of str\n A list of all star features pickles produced.\n\n '''\n # make sure to make the output directory if it doesn't exist\n if not os.path.exists(outdir):\n os.makedirs(outdir)\n\n if maxobjects:\n lclist = lclist[:maxobjects]\n\n # read in the kdtree pickle\n with open(lc_catalog_pickle, 'rb') as infd:\n kdt_dict = pickle.load(infd)\n\n kdt = kdt_dict['kdtree']\n objlist = kdt_dict['objects']['objectid']\n objlcfl = kdt_dict['objects']['lcfname']\n\n tasks = [(x, outdir, kdt, objlist, objlcfl,\n neighbor_radius_arcsec,\n deredden, custom_bandpasses,\n lcformat, lcformatdir) for x in lclist]\n\n for task in tqdm(tasks):\n result = _starfeatures_worker(task)\n\n return result", "response": "This drives the get_starfeatures function for a collection of LCs."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef parallel_starfeatures_lcdir(lcdir,\n outdir,\n lc_catalog_pickle,\n neighbor_radius_arcsec,\n fileglob=None,\n maxobjects=None,\n deredden=True,\n custom_bandpasses=None,\n lcformat='hat-sql',\n lcformatdir=None,\n nworkers=NCPUS,\n recursive=True):\n '''This runs parallel star feature extraction for a directory of LCs.\n\n Parameters\n ----------\n\n lcdir : list of str\n The directory to search for light curves.\n\n outdir : str\n The output directory where the results will be placed.\n\n lc_catalog_pickle : str\n The path to a catalog containing at a dict with least:\n\n - an object ID array accessible with `dict['objects']['objectid']`\n\n - an LC filename array accessible with `dict['objects']['lcfname']`\n\n - a `scipy.spatial.KDTree` or `cKDTree` object to use for finding\n neighbors for each object accessible with `dict['kdtree']`\n\n A catalog pickle of the form needed can be produced using\n :py:func:`astrobase.lcproc.catalogs.make_lclist` or\n :py:func:`astrobase.lcproc.catalogs.filter_lclist`.\n\n neighbor_radius_arcsec : float\n This indicates the radius in arcsec to search for neighbors for this\n object using the light curve catalog's `kdtree`, `objlist`, `lcflist`,\n and in GAIA.\n\n fileglob : str\n The UNIX file glob to use to search for the light curves in `lcdir`. If\n None, the default value for the light curve format specified will be\n used.\n\n maxobjects : int\n The number of objects to process from `lclist`.\n\n deredden : bool\n This controls if the colors and any color classifications will be\n dereddened using 2MASS DUST.\n\n custom_bandpasses : dict or None\n This is a dict used to define any custom bandpasses in the\n `in_objectinfo` dict you want to make this function aware of and\n generate colors for. Use the format below for this dict::\n\n {\n '':{'dustkey':'',\n 'label':''\n 'colors':[['-',\n ' - '],\n ['-',\n ' - ']]},\n .\n ...\n .\n '':{'dustkey':'',\n 'label':''\n 'colors':[['-',\n ' - '],\n ['-',\n ' - ']]},\n }\n\n Where:\n\n `bandpass_key` is a key to use to refer to this bandpass in the\n `objectinfo` dict, e.g. 'sdssg' for SDSS g band\n\n `twomass_dust_key` is the key to use in the 2MASS DUST result table for\n reddening per band-pass. For example, given the following DUST result\n table (using http://irsa.ipac.caltech.edu/applications/DUST/)::\n\n |Filter_name|LamEff |A_over_E_B_V_SandF|A_SandF|A_over_E_B_V_SFD|A_SFD|\n |char |float |float |float |float |float|\n | |microns| |mags | |mags |\n CTIO U 0.3734 4.107 0.209 4.968 0.253\n CTIO B 0.4309 3.641 0.186 4.325 0.221\n CTIO V 0.5517 2.682 0.137 3.240 0.165\n .\n .\n ...\n\n The `twomass_dust_key` for 'vmag' would be 'CTIO V'. If you want to\n skip DUST lookup and want to pass in a specific reddening magnitude\n for your bandpass, use a float for the value of\n `twomass_dust_key`. If you want to skip DUST lookup entirely for\n this bandpass, use None for the value of `twomass_dust_key`.\n\n `band_label` is the label to use for this bandpass, e.g. 'W1' for\n WISE-1 band, 'u' for SDSS u, etc.\n\n The 'colors' list contains color definitions for all colors you want\n to generate using this bandpass. this list contains elements of the\n form::\n\n ['-',' - ']\n\n where the the first item is the bandpass keys making up this color,\n and the second item is the label for this color to be used by the\n frontends. An example::\n\n ['sdssu-sdssg','u - g']\n\n lcformat : str\n This is the `formatkey` associated with your light curve format, which\n you previously passed in to the `lcproc.register_lcformat`\n function. This will be used to look up how to find and read the light\n curves specified in `basedir` or `use_list_of_filenames`.\n\n lcformatdir : str or None\n If this is provided, gives the path to a directory when you've stored\n your lcformat description JSONs, other than the usual directories lcproc\n knows to search for them in. Use this along with `lcformat` to specify\n an LC format JSON file that's not currently registered with lcproc.\n\n nworkers : int\n The number of parallel workers to launch.\n\n Returns\n -------\n\n dict\n A dict with key:val pairs of the input light curve filename and the\n output star features pickle for each LC processed.\n\n '''\n\n try:\n formatinfo = get_lcformat(lcformat,\n use_lcformat_dir=lcformatdir)\n if formatinfo:\n (dfileglob, readerfunc,\n dtimecols, dmagcols, derrcols,\n magsarefluxes, normfunc) = formatinfo\n else:\n LOGERROR(\"can't figure out the light curve format\")\n return None\n except Exception as e:\n LOGEXCEPTION(\"can't figure out the light curve format\")\n return None\n\n if not fileglob:\n fileglob = dfileglob\n\n # now find the files\n LOGINFO('searching for %s light curves in %s ...' % (lcformat, lcdir))\n\n if recursive is False:\n matching = glob.glob(os.path.join(lcdir, fileglob))\n\n else:\n # use recursive glob for Python 3.5+\n if sys.version_info[:2] > (3,4):\n\n matching = glob.glob(os.path.join(lcdir,\n '**',\n fileglob),recursive=True)\n\n # otherwise, use os.walk and glob\n else:\n\n # use os.walk to go through the directories\n walker = os.walk(lcdir)\n matching = []\n\n for root, dirs, _files in walker:\n for sdir in dirs:\n searchpath = os.path.join(root,\n sdir,\n fileglob)\n foundfiles = glob.glob(searchpath)\n\n if foundfiles:\n matching.extend(foundfiles)\n\n\n # now that we have all the files, process them\n if matching and len(matching) > 0:\n\n LOGINFO('found %s light curves, getting starfeatures...' %\n len(matching))\n\n return parallel_starfeatures(matching,\n outdir,\n lc_catalog_pickle,\n neighbor_radius_arcsec,\n deredden=deredden,\n custom_bandpasses=custom_bandpasses,\n maxobjects=maxobjects,\n lcformat=lcformat,\n lcformatdir=lcformatdir,\n nworkers=nworkers)\n\n else:\n\n LOGERROR('no light curve files in %s format found in %s' % (lcformat,\n lcdir))\n return None", "response": "This function runs the parallel star features extraction for a directory of LCs."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef checkplot_pickle(\n lspinfolist,\n times,\n mags,\n errs,\n fast_mode=False,\n magsarefluxes=False,\n nperiodstouse=3,\n objectinfo=None,\n deredden_object=True,\n custom_bandpasses=None,\n gaia_submit_timeout=10.0,\n gaia_submit_tries=3,\n gaia_max_timeout=180.0,\n gaia_mirror=None,\n complete_query_later=True,\n varinfo=None,\n getvarfeatures=True,\n lclistpkl=None,\n nbrradiusarcsec=60.0,\n maxnumneighbors=5,\n xmatchinfo=None,\n xmatchradiusarcsec=3.0,\n lcfitfunc=None,\n lcfitparams=None,\n externalplots=None,\n findercmap='gray_r',\n finderconvolve=None,\n findercachedir='~/.astrobase/stamp-cache',\n normto='globalmedian',\n normmingap=4.0,\n sigclip=4.0,\n varepoch='min',\n phasewrap=True,\n phasesort=True,\n phasebin=0.002,\n minbinelems=7,\n plotxlim=(-0.8,0.8),\n xliminsetmode=False,\n plotdpi=100,\n bestperiodhighlight=None,\n xgridlines=None,\n mindet=99,\n verbose=True,\n outfile=None,\n outgzip=False,\n pickleprotocol=None,\n returndict=False\n):\n\n '''This writes a multiple lspinfo checkplot to a (gzipped) pickle file.\n\n This function can take input from multiple lspinfo dicts (e.g. a list of\n output dicts or gzipped pickles of dicts from independent runs of BLS, PDM,\n AoV, or GLS period-finders in periodbase).\n\n NOTE: if `lspinfolist` contains more than one lspinfo object with the same\n lspmethod ('pdm','gls','sls','aov','bls'), the latest one in the list will\n overwrite the earlier ones.\n\n The output dict contains all the plots (magseries and phased magseries),\n periodograms, object information, variability information, light curves, and\n phased light curves. This can be written to:\n\n - a pickle with `checkplot_pickle` below\n - a PNG with `checkplot.pkl_png.checkplot_pickle_to_png`\n\n Parameters\n ----------\n\n lspinfolist : list of dicts\n This is a list of dicts containing period-finder results ('lspinfo'\n dicts). These can be from any of the period-finder methods in\n astrobase.periodbase. To incorporate external period-finder results into\n checkplots, these dicts must be of the form below, including at least\n the keys indicated here::\n\n {'periods': np.array of all periods searched by the period-finder,\n 'lspvals': np.array of periodogram power value for each period,\n 'bestperiod': a float value that is the period with the highest\n peak in the periodogram, i.e. the most-likely actual\n period,\n 'method': a three-letter code naming the period-finder used; must\n be one of the keys in the\n `astrobase.periodbase.METHODLABELS` dict,\n 'nbestperiods': a list of the periods corresponding to periodogram\n peaks (`nbestlspvals` below) to annotate on the\n periodogram plot so they can be called out\n visually,\n 'nbestlspvals': a list of the power values associated with\n periodogram peaks to annotate on the periodogram\n plot so they can be called out visually; should be\n the same length as `nbestperiods` above}\n\n `nbestperiods` and `nbestlspvals` in each lspinfo dict must have at\n least as many elements as the `nperiodstouse` kwarg to this function.\n\n times,mags,errs : np.arrays\n The magnitude/flux time-series to process for this checkplot along with\n their associated measurement errors.\n\n fast_mode : bool or float\n This runs the external catalog operations in a \"fast\" mode, with short\n timeouts and not trying to hit external catalogs that take a long time\n to respond.\n\n If this is set to True, the default settings for the external requests\n will then become::\n\n skyview_lookup = False\n skyview_timeout = 10.0\n skyview_retry_failed = False\n dust_timeout = 10.0\n gaia_submit_timeout = 7.0\n gaia_max_timeout = 10.0\n gaia_submit_tries = 2\n complete_query_later = False\n search_simbad = False\n\n If this is a float, will run in \"fast\" mode with the provided timeout\n value in seconds and the following settings::\n\n skyview_lookup = True\n skyview_timeout = fast_mode\n skyview_retry_failed = False\n dust_timeout = fast_mode\n gaia_submit_timeout = 0.66*fast_mode\n gaia_max_timeout = fast_mode\n gaia_submit_tries = 2\n complete_query_later = False\n search_simbad = False\n\n magsarefluxes : bool\n If True, indicates the input time-series is fluxes and not mags so the\n plot y-axis direction and range can be set appropriately.\n\n nperiodstouse : int\n This controls how many 'best' periods to make phased LC plots for. By\n default, this is the 3 best. If this is set to None, all 'best' periods\n present in each lspinfo dict's 'nbestperiods' key will be processed for\n this checkplot.\n\n objectinfo : dict or None\n If provided, this is a dict containing information on the object whose\n light curve is being processed. This function will then be able to look\n up and download a finder chart for this object and write that to the\n output checkplotdict. External services such as GAIA, SIMBAD, TIC@MAST,\n etc. will also be used to look up this object by its coordinates, and\n will add in information available from those services.\n\n The `objectinfo` dict must be of the form and contain at least the keys\n described below::\n\n {'objectid': the name of the object,\n 'ra': the right ascension of the object in decimal degrees,\n 'decl': the declination of the object in decimal degrees,\n 'ndet': the number of observations of this object}\n\n You can also provide magnitudes and proper motions of the object using\n the following keys and the appropriate values in the `objectinfo`\n dict. These will be used to calculate colors, total and reduced proper\n motion, etc. and display these in the output checkplot PNG::\n\n 'pmra' -> the proper motion in mas/yr in right ascension,\n 'pmdecl' -> the proper motion in mas/yr in the declination,\n 'umag' -> U mag\t\t -> colors: U-B, U-V, U-g\n 'bmag' -> B mag\t\t -> colors: U-B, B-V\n 'vmag' -> V mag\t\t -> colors: U-V, B-V, V-R, V-I, V-K\n 'rmag' -> R mag\t\t -> colors: V-R, R-I\n 'imag' -> I mag\t\t -> colors: g-I, V-I, R-I, B-I\n 'jmag' -> 2MASS J mag\t -> colors: J-H, J-K, g-J, i-J\n 'hmag' -> 2MASS H mag\t -> colors: J-H, H-K\n 'kmag' -> 2MASS Ks mag\t -> colors: g-Ks, H-Ks, J-Ks, V-Ks\n 'sdssu' -> SDSS u mag\t -> colors: u-g, u-V\n 'sdssg' -> SDSS g mag\t -> colors: g-r, g-i, g-K, u-g, U-g, g-J\n 'sdssr' -> SDSS r mag\t -> colors: r-i, g-r\n 'sdssi' -> SDSS i mag\t -> colors: r-i, i-z, g-i, i-J, i-W1\n 'sdssz' -> SDSS z mag\t -> colors: i-z, z-W2, g-z\n 'ujmag' -> UKIRT J mag\t -> colors: J-H, H-K, J-K, g-J, i-J\n 'uhmag' -> UKIRT H mag\t -> colors: J-H, H-K\n 'ukmag' -> UKIRT K mag\t -> colors: g-K, H-K, J-K, V-K\n 'irac1' -> Spitzer IRAC1 mag -> colors: i-I1, I1-I2\n 'irac2' -> Spitzer IRAC2 mag -> colors: I1-I2, I2-I3\n 'irac3' -> Spitzer IRAC3 mag -> colors: I2-I3\n 'irac4' -> Spitzer IRAC4 mag -> colors: I3-I4\n 'wise1' -> WISE W1 mag\t -> colors: i-W1, W1-W2\n 'wise2' -> WISE W2 mag\t -> colors: W1-W2, W2-W3\n 'wise3' -> WISE W3 mag\t -> colors: W2-W3\n 'wise4' -> WISE W4 mag\t -> colors: W3-W4\n\n If you have magnitude measurements in other bands, use the\n `custom_bandpasses` kwarg to pass these in.\n\n If this is None, no object information will be incorporated into the\n checkplot (kind of making it effectively useless for anything other than\n glancing at the phased light curves at various 'best' periods from the\n period-finder results).\n\n deredden_object : bool\n If this is True, will use the 2MASS DUST service to get extinction\n coefficients in various bands, and then try to deredden the magnitudes\n and colors of the object already present in the checkplot's objectinfo\n dict.\n\n custom_bandpasses : dict\n This is a dict used to provide custom bandpass definitions for any\n magnitude measurements in the objectinfo dict that are not automatically\n recognized by :py:func:`astrobase.varclass.starfeatures.color_features`.\n\n gaia_submit_timeout : float\n Sets the timeout in seconds to use when submitting a request to look up\n the object's information to the GAIA service. Note that if `fast_mode`\n is set, this is ignored.\n\n gaia_submit_tries : int\n Sets the maximum number of times the GAIA services will be contacted to\n obtain this object's information. If `fast_mode` is set, this is\n ignored, and the services will be contacted only once (meaning that a\n failure to respond will be silently ignored and no GAIA data will be\n added to the checkplot's objectinfo dict).\n\n gaia_max_timeout : float\n Sets the timeout in seconds to use when waiting for the GAIA service to\n respond to our request for the object's information. Note that if\n `fast_mode` is set, this is ignored.\n\n gaia_mirror : str or None\n This sets the GAIA mirror to use. This is a key in the\n `services.gaia.GAIA_URLS` dict which defines the URLs to hit for each\n mirror.\n\n complete_query_later : bool\n If this is True, saves the state of GAIA queries that are not yet\n complete when `gaia_max_timeout` is reached while waiting for the GAIA\n service to respond to our request. A later call for GAIA info on the\n same object will attempt to pick up the results from the existing query\n if it's completed. If `fast_mode` is True, this is ignored.\n\n varinfo : dict\n If this is None, a blank dict of the form below will be added to the\n checkplotdict::\n\n {'objectisvar': None -> variability flag (None indicates unset),\n 'vartags': CSV str containing variability type tags from review,\n 'varisperiodic': None -> periodic variability flag (None -> unset),\n 'varperiod': the period associated with the periodic variability,\n 'varepoch': the epoch associated with the periodic variability}\n\n If you provide a dict matching this format in this kwarg, this will be\n passed unchanged to the output checkplotdict produced.\n\n getvarfeatures : bool\n If this is True, several light curve variability features for this\n object will be calculated and added to the output checkpotdict as\n checkplotdict['varinfo']['features']. This uses the function\n `varclass.varfeatures.all_nonperiodic_features` so see its docstring for\n the measures that are calculated (e.g. Stetson J indices, dispersion\n measures, etc.)\n\n lclistpkl : dict or str\n If this is provided, must be a dict resulting from reading a catalog\n produced by the `lcproc.catalogs.make_lclist` function or a str path\n pointing to the pickle file produced by that function. This catalog is\n used to find neighbors of the current object in the current light curve\n collection. Looking at neighbors of the object within the radius\n specified by `nbrradiusarcsec` is useful for light curves produced by\n instruments that have a large pixel scale, so are susceptible to\n blending of variability and potential confusion of neighbor variability\n with that of the actual object being looked at. If this is None, no\n neighbor lookups will be performed.\n\n nbrradiusarcsec : flaot\n The radius in arcseconds to use for a search conducted around the\n coordinates of this object to look for any potential confusion and\n blending of variability amplitude caused by their proximity.\n\n maxnumneighbors : int\n The maximum number of neighbors that will have their light curves and\n magnitudes noted in this checkplot as potential blends with the target\n object.\n\n xmatchinfo : str or dict\n This is either the xmatch dict produced by the function\n `load_xmatch_external_catalogs` above, or the path to the xmatch info\n pickle file produced by that function.\n\n xmatchradiusarcsec : float\n This is the cross-matching radius to use in arcseconds.\n\n lcfitfunc : Python function or None\n If provided, this should be a Python function that is used to fit a\n model to the light curve. This fit is then overplotted for each phased\n light curve in the checkplot. This function should have the following\n signature:\n\n `def lcfitfunc(times, mags, errs, period, **lcfitparams)`\n\n where `lcfitparams` encapsulates all external parameters (i.e. number of\n knots for a spline function, the degree of a Legendre polynomial fit,\n etc., planet transit parameters) This function should return a Python\n dict with the following structure (similar to the functions in\n `astrobase.lcfit`) and at least the keys below::\n\n {'fittype':,\n 'fitchisq':,\n 'fitredchisq':,\n 'fitinfo':{'fitmags':},\n 'magseries':{'times':}}\n\n Additional keys in the dict returned from this function can include\n `fitdict['fitinfo']['finalparams']` for the final model fit parameters\n (this will be used by the checkplotserver if present),\n `fitdict['fitinfo']['fitepoch']` for the minimum light epoch returned by\n the model fit, among others.\n\n In any case, the output dict of `lcfitfunc` will be copied to the output\n checkplotdict as\n `checkplotdict[lspmethod][periodind]['lcfit'][]` for each\n phased light curve.\n\n lcfitparams : dict\n A dict containing the LC fit parameters to use when calling the function\n provided in `lcfitfunc`. This contains key-val pairs corresponding to\n parameter names and their respective initial values to be used by the\n fit function.\n\n externalplots : list of tuples of str\n If provided, this is a list of 4-element tuples containing:\n\n 1. path to PNG of periodogram from an external period-finding method\n 2. path to PNG of best period phased LC from the external period-finder\n 3. path to PNG of 2nd-best phased LC from the external period-finder\n 4. path to PNG of 3rd-best phased LC from the external period-finder\n\n This can be used to incorporate external period-finding method results\n into the output checkplot pickle or exported PNG to allow for comparison\n with astrobase results. Example of `externalplots`::\n\n [('/path/to/external/bls-periodogram.png',\n '/path/to/external/bls-phasedlc-plot-bestpeak.png',\n '/path/to/external/bls-phasedlc-plot-peak2.png',\n '/path/to/external/bls-phasedlc-plot-peak3.png'),\n ('/path/to/external/pdm-periodogram.png',\n '/path/to/external/pdm-phasedlc-plot-bestpeak.png',\n '/path/to/external/pdm-phasedlc-plot-peak2.png',\n '/path/to/external/pdm-phasedlc-plot-peak3.png'),\n ...]\n\n If `externalplots` is provided here, these paths will be stored in the\n output checkplotdict. The `checkplot.pkl_png.checkplot_pickle_to_png`\n function can then automatically retrieve these plot PNGs and put\n them into the exported checkplot PNG.\n\n findercmap : str or matplotlib.cm.ColorMap object\n The Colormap object to use for the finder chart image.\n\n finderconvolve : astropy.convolution.Kernel object or None\n If not None, the Kernel object to use for convolving the finder image.\n\n findercachedir : str\n The path to the astrobase cache directory for finder chart downloads\n from the NASA SkyView service.\n\n normto : {'globalmedian', 'zero'} or a float\n This specifies the normalization target::\n\n 'globalmedian' -> norms each mag to global median of the LC column\n 'zero' -> norms each mag to zero\n a float -> norms each mag to this specified float value.\n\n normmingap : float\n This defines how much the difference between consecutive measurements is\n allowed to be to consider them as parts of different timegroups. By\n default it is set to 4.0 days.\n\n sigclip : float or int or sequence of two floats/ints or None\n If a single float or int, a symmetric sigma-clip will be performed using\n the number provided as the sigma-multiplier to cut out from the input\n time-series.\n\n If a list of two ints/floats is provided, the function will perform an\n 'asymmetric' sigma-clip. The first element in this list is the sigma\n value to use for fainter flux/mag values; the second element in this\n list is the sigma value to use for brighter flux/mag values. For\n example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma\n dimmings and greater than 3-sigma brightenings. Here the meaning of\n \"dimming\" and \"brightening\" is set by *physics* (not the magnitude\n system), which is why the `magsarefluxes` kwarg must be correctly set.\n\n If `sigclip` is None, no sigma-clipping will be performed, and the\n time-series (with non-finite elems removed) will be passed through to\n the output.\n\n varepoch : 'min' or float or list of lists or None\n The epoch to use for this phased light curve plot tile. If this is a\n float, will use the provided value directly. If this is 'min', will\n automatically figure out the time-of-minimum of the phased light\n curve. If this is None, will use the mimimum value of `stimes` as the\n epoch of the phased light curve plot. If this is a list of lists, will\n use the provided value of `lspmethodind` to look up the current\n period-finder method and the provided value of `periodind` to look up\n the epoch associated with that method and the current period. This is\n mostly only useful when `twolspmode` is True.\n\n phasewrap : bool\n If this is True, the phased time-series will be wrapped around\n phase 0.0.\n\n phasesort : bool\n If True, will sort the phased light curve in order of increasing phase.\n\n phasebin: float\n The bin size to use to group together measurements closer than this\n amount in phase. This is in units of phase. If this is a float, a\n phase-binned version of the phased light curve will be overplotted on\n top of the regular phased light curve.\n\n minbinelems : int\n The minimum number of elements required per phase bin to include it in\n the phased LC plot.\n\n plotxlim : sequence of two floats or None\n The x-range (min, max) of the phased light curve plot. If None, will be\n determined automatically.\n\n xliminsetmode : bool\n If this is True, the generated phased light curve plot will use the\n values of `plotxlim` as the main plot x-axis limits (i.e. zoomed-in if\n `plotxlim` is a range smaller than the full phase range), and will show\n the full phased light curve plot as an smaller inset. Useful for\n planetary transit light curves.\n\n plotdpi : int\n The resolution of the output plot PNGs in dots per inch.\n\n bestperiodhighlight : str or None\n If not None, this is a str with a matplotlib color specification to use\n as the background color to highlight the phased light curve plot of the\n 'best' period and epoch combination. If None, no highlight will be\n applied.\n\n xgridlines : list of floats or None\n If this is provided, must be a list of floats corresponding to the phase\n values where to draw vertical dashed lines as a means of highlighting\n these.\n\n mindet : int\n The minimum of observations the input object's mag/flux time-series must\n have for this function to plot its light curve and phased light\n curve. If the object has less than this number, no light curves will be\n plotted, but the checkplotdict will still contain all of the other\n information.\n\n verbose : bool\n If True, will indicate progress and warn about problems.\n\n outfile : str or None\n The name of the output checkplot pickle file. If this is None, will\n write the checkplot pickle to file called 'checkplot.pkl' in the current\n working directory.\n\n outgzip : bool\n This controls whether to gzip the output pickle. It turns out that this\n is the slowest bit in the output process, so if you're after speed, best\n not to use this. This is False by default since it turns out that gzip\n actually doesn't save that much space (29 MB vs. 35 MB for the average\n checkplot pickle).\n\n pickleprotocol : int or None\n This sets the pickle file protocol to use when writing the pickle:\n\n If None, will choose a protocol using the following rules:\n\n - 4 -> default in Python >= 3.4 - fast but incompatible with Python 2\n - 3 -> default in Python 3.0-3.3 - mildly fast\n - 2 -> default in Python 2 - very slow, but compatible with Python 2/3\n\n The default protocol kwarg is None, this will make an automatic choice\n for pickle protocol that's best suited for the version of Python in\n use. Note that this will make pickles generated by Py3 incompatible with\n Py2.\n\n returndict : bool\n If this is True, will return the checkplotdict instead of returning the\n filename of the output checkplot pickle.\n\n Returns\n -------\n\n dict or str\n If returndict is False, will return the path to the generated checkplot\n pickle file. If returndict is True, will return the checkplotdict\n instead.\n\n '''\n\n # call checkplot_dict for most of the work\n checkplotdict = checkplot_dict(\n lspinfolist,\n times,\n mags,\n errs,\n magsarefluxes=magsarefluxes,\n nperiodstouse=nperiodstouse,\n objectinfo=objectinfo,\n deredden_object=deredden_object,\n custom_bandpasses=custom_bandpasses,\n gaia_submit_timeout=gaia_submit_timeout,\n gaia_submit_tries=gaia_submit_tries,\n gaia_max_timeout=gaia_max_timeout,\n gaia_mirror=gaia_mirror,\n complete_query_later=complete_query_later,\n varinfo=varinfo,\n getvarfeatures=getvarfeatures,\n lclistpkl=lclistpkl,\n nbrradiusarcsec=nbrradiusarcsec,\n maxnumneighbors=maxnumneighbors,\n xmatchinfo=xmatchinfo,\n xmatchradiusarcsec=xmatchradiusarcsec,\n lcfitfunc=lcfitfunc,\n lcfitparams=lcfitparams,\n externalplots=externalplots,\n findercmap=findercmap,\n finderconvolve=finderconvolve,\n findercachedir=findercachedir,\n normto=normto,\n normmingap=normmingap,\n sigclip=sigclip,\n varepoch=varepoch,\n phasewrap=phasewrap,\n phasesort=phasesort,\n phasebin=phasebin,\n minbinelems=minbinelems,\n plotxlim=plotxlim,\n xliminsetmode=xliminsetmode,\n plotdpi=plotdpi,\n bestperiodhighlight=bestperiodhighlight,\n xgridlines=xgridlines,\n mindet=mindet,\n verbose=verbose,\n fast_mode=fast_mode\n )\n\n # for Python >= 3.4, use v4\n if ((sys.version_info[0:2] >= (3,4) and not pickleprotocol) or\n (pickleprotocol > 2)):\n pickleprotocol = 4\n\n elif ((sys.version_info[0:2] >= (3,0) and not pickleprotocol) or\n (pickleprotocol > 2)):\n pickleprotocol = 3\n\n # for Python == 2.7; use v2\n elif sys.version_info[0:2] == (2,7) and not pickleprotocol:\n pickleprotocol = 2\n\n # otherwise, if left unspecified, use the slowest but most compatible\n # protocol. this will be readable by all (most?) Pythons\n elif not pickleprotocol:\n pickleprotocol = 0\n\n\n # generate the output file path\n if outgzip:\n\n # generate the outfile filename\n if (not outfile and\n len(lspinfolist) > 0 and\n isinstance(lspinfolist[0], str)):\n plotfpath = os.path.join(os.path.dirname(lspinfolist[0]),\n 'checkplot-%s.pkl.gz' %\n checkplotdict['objectid'])\n elif outfile:\n plotfpath = outfile\n else:\n plotfpath = 'checkplot.pkl.gz'\n\n else:\n\n # generate the outfile filename\n if (not outfile and\n len(lspinfolist) > 0 and\n isinstance(lspinfolist[0], str)):\n plotfpath = os.path.join(os.path.dirname(lspinfolist[0]),\n 'checkplot-%s.pkl' %\n checkplotdict['objectid'])\n elif outfile:\n plotfpath = outfile\n else:\n plotfpath = 'checkplot.pkl'\n\n\n # write the completed checkplotdict to a gzipped pickle\n picklefname = _write_checkplot_picklefile(checkplotdict,\n outfile=plotfpath,\n protocol=pickleprotocol,\n outgzip=outgzip)\n\n # at the end, return the dict and filename if asked for\n if returndict:\n if verbose:\n LOGINFO('checkplot done -> %s' % picklefname)\n return checkplotdict, picklefname\n\n # otherwise, just return the filename\n else:\n # just to make sure: free up space\n del checkplotdict\n if verbose:\n LOGINFO('checkplot done -> %s' % picklefname)\n return picklefname", "response": "This function writes a single lspinfo checkplot to a gzipped pickle file."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef pwd_phasebin(phases, mags, binsize=0.002, minbin=9):\n '''\n This bins the phased mag series using the given binsize.\n\n '''\n\n bins = np.arange(0.0, 1.0, binsize)\n binnedphaseinds = npdigitize(phases, bins)\n\n binnedphases, binnedmags = [], []\n\n for x in npunique(binnedphaseinds):\n\n thisbin_inds = binnedphaseinds == x\n thisbin_phases = phases[thisbin_inds]\n thisbin_mags = mags[thisbin_inds]\n if thisbin_inds.size > minbin:\n binnedphases.append(npmedian(thisbin_phases))\n binnedmags.append(npmedian(thisbin_mags))\n\n return np.array(binnedphases), np.array(binnedmags)", "response": "This function bins the phased mag series using the given binsize."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef pdw_worker(task):\n '''\n This is the parallel worker for the function below.\n\n task[0] = frequency for this worker\n task[1] = times array\n task[2] = mags array\n task[3] = fold_time\n task[4] = j_range\n task[5] = keep_threshold_1\n task[6] = keep_threshold_2\n task[7] = phasebinsize\n\n we don't need errs for the worker.\n\n '''\n\n frequency = task[0]\n times, modmags = task[1], task[2]\n fold_time = task[3]\n j_range = range(task[4])\n keep_threshold_1 = task[5]\n keep_threshold_2 = task[6]\n phasebinsize = task[7]\n\n\n try:\n\n period = 1.0/frequency\n\n # use the common phaser to phase and sort the mag\n phased = phase_magseries(times,\n modmags,\n period,\n fold_time,\n wrap=False,\n sort=True)\n\n # bin in phase if requested, this turns this into a sort of PDM method\n if phasebinsize is not None and phasebinsize > 0:\n bphased = pwd_phasebin(phased['phase'],\n phased['mags'],\n binsize=phasebinsize)\n phase_sorted = bphased[0]\n mod_mag_sorted = bphased[1]\n j_range = range(len(mod_mag_sorted) - 1)\n else:\n phase_sorted = phased['phase']\n mod_mag_sorted = phased['mags']\n\n # now calculate the string length\n rolledmags = nproll(mod_mag_sorted,1)\n rolledphases = nproll(phase_sorted,1)\n strings = (\n (rolledmags - mod_mag_sorted)*(rolledmags - mod_mag_sorted) +\n (rolledphases - phase_sorted)*(rolledphases - phase_sorted)\n )\n strings[0] = (\n ((mod_mag_sorted[0] - mod_mag_sorted[-1]) *\n (mod_mag_sorted[0] - mod_mag_sorted[-1])) +\n ((phase_sorted[0] - phase_sorted[-1] + 1) *\n (phase_sorted[0] - phase_sorted[-1] + 1))\n )\n strlen = npsum(npsqrt(strings))\n\n if (keep_threshold_1 < strlen < keep_threshold_2):\n p_goodflag = True\n else:\n p_goodflag = False\n\n return (period, strlen, p_goodflag)\n\n except Exception as e:\n\n LOGEXCEPTION('error in DWP')\n return(period, npnan, False)", "response": "This function is a parallel worker for the PDM method. It is used to create a new object for the task."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef pdw_period_find(times,\n mags,\n errs,\n autofreq=True,\n init_p=None,\n end_p=None,\n f_step=1.0e-4,\n phasebinsize=None,\n sigclip=10.0,\n nworkers=None,\n verbose=False):\n '''This is the parallel version of the function above.\n\n Uses the string length method in Dworetsky 1983 to calculate the period of a\n time-series of magnitude measurements and associated magnitude errors. This\n can optionally bin in phase to try to speed up the calculation.\n\n PARAMETERS:\n\n time: series of times at which mags were measured (usually some form of JD)\n mag: timeseries of magnitudes (np.array)\n err: associated errs per magnitude measurement (np.array)\n init_p, end_p: interval to search for periods between (both ends inclusive)\n f_step: step in frequency [days^-1] to use\n\n RETURNS:\n\n tuple of the following form:\n\n (periods (np.array),\n string_lengths (np.array),\n good_period_mask (boolean array))\n\n '''\n\n # remove nans\n find = npisfinite(times) & npisfinite(mags) & npisfinite(errs)\n ftimes, fmags, ferrs = times[find], mags[find], errs[find]\n\n mod_mags = (fmags - npmin(fmags))/(2.0*(npmax(fmags) - npmin(fmags))) - 0.25\n\n if len(ftimes) > 9 and len(fmags) > 9 and len(ferrs) > 9:\n\n # get the median and stdev = 1.483 x MAD\n median_mag = np.median(fmags)\n stddev_mag = (np.median(np.abs(fmags - median_mag))) * 1.483\n\n # sigclip next\n if sigclip:\n\n sigind = (np.abs(fmags - median_mag)) < (sigclip * stddev_mag)\n\n stimes = ftimes[sigind]\n smags = fmags[sigind]\n serrs = ferrs[sigind]\n\n LOGINFO('sigclip = %s: before = %s observations, '\n 'after = %s observations' %\n (sigclip, len(times), len(stimes)))\n\n else:\n\n stimes = ftimes\n smags = fmags\n serrs = ferrs\n\n # make sure there are enough points to calculate a spectrum\n if len(stimes) > 9 and len(smags) > 9 and len(serrs) > 9:\n\n # get the frequencies to use\n if init_p:\n endf = 1.0/init_p\n else:\n # default start period is 0.1 day\n endf = 1.0/0.1\n\n if end_p:\n startf = 1.0/end_p\n else:\n # default end period is length of time series\n startf = 1.0/(stimes.max() - stimes.min())\n\n # if we're not using autofreq, then use the provided frequencies\n if not autofreq:\n frequencies = np.arange(startf, endf, stepsize)\n LOGINFO(\n 'using %s frequency points, start P = %.3f, end P = %.3f' %\n (frequencies.size, 1.0/endf, 1.0/startf)\n )\n else:\n # this gets an automatic grid of frequencies to use\n frequencies = get_frequency_grid(stimes,\n minfreq=startf,\n maxfreq=endf)\n LOGINFO(\n 'using autofreq with %s frequency points, '\n 'start P = %.3f, end P = %.3f' %\n (frequencies.size,\n 1.0/frequencies.max(),\n 1.0/frequencies.min())\n )\n\n\n # set up some internal stuff\n fold_time = npmin(ftimes) # fold at the first time element\n j_range = len(fmags)-1\n epsilon = 2.0 * npmean(ferrs)\n delta_l = 0.34 * (epsilon - 0.5*(epsilon**2)) * (len(ftimes) -\n npsqrt(10.0/epsilon))\n keep_threshold_1 = 1.6 + 1.2*delta_l\n l = 0.212*len(ftimes)\n sig_l = len(ftimes)/37.5\n keep_threshold_2 = l + 4.0*sig_l\n\n # generate the tasks\n tasks = [(x,\n ftimes,\n mod_mags,\n fold_time,\n j_range,\n keep_threshold_1,\n keep_threshold_2,\n phasebinsize) for x in frequencies]\n\n # fire up the pool and farm out the tasks\n if (not nworkers) or (nworkers > NCPUS):\n nworkers = NCPUS\n LOGINFO('using %s workers...' % nworkers)\n\n pool = Pool(nworkers)\n strlen_results = pool.map(pdw_worker, tasks)\n pool.close()\n pool.join()\n del pool\n\n periods, strlens, goodflags = zip(*strlen_results)\n periods, strlens, goodflags = (np.array(periods),\n np.array(strlens),\n np.array(goodflags))\n\n strlensort = npargsort(strlens)\n nbeststrlens = strlens[strlensort[:5]]\n nbestperiods = periods[strlensort[:5]]\n nbestflags = goodflags[strlensort[:5]]\n bestperiod = nbestperiods[0]\n beststrlen = nbeststrlens[0]\n bestflag = nbestflags[0]\n\n return {'bestperiod':bestperiod,\n 'beststrlen':beststrlen,\n 'bestflag':bestflag,\n 'nbeststrlens':nbeststrlens,\n 'nbestperiods':nbestperiods,\n 'nbestflags':nbestflags,\n 'strlens':strlens,\n 'periods':periods,\n 'goodflags':goodflags}\n\n else:\n\n LOGERROR('no good detections for these times and mags, skipping...')\n return {'bestperiod':npnan,\n 'beststrlen':npnan,\n 'bestflag':npnan,\n 'nbeststrlens':None,\n 'nbestperiods':None,\n 'nbestflags':None,\n 'strlens':None,\n 'periods':None,\n 'goodflags':None}\n else:\n\n LOGERROR('no good detections for these times and mags, skipping...')\n return {'bestperiod':npnan,\n 'beststrlen':npnan,\n 'bestflag':npnan,\n 'nbeststrlens':None,\n 'nbestperiods':None,\n 'nbestflags':None,\n 'strlens':None,\n 'periods':None,\n 'goodflags':None}", "response": "This function is used to find the period of a single object in a single object. This function is used to find the period of a single object in a single object."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef townsend_lombscargle_value(times, mags, omega):\n '''\n This calculates the periodogram value for each omega (= 2*pi*f). Mags must\n be normalized to zero with variance scaled to unity.\n\n '''\n cos_omegat = npcos(omega*times)\n sin_omegat = npsin(omega*times)\n\n xc = npsum(mags*cos_omegat)\n xs = npsum(mags*sin_omegat)\n\n cc = npsum(cos_omegat*cos_omegat)\n ss = npsum(sin_omegat*sin_omegat)\n\n cs = npsum(cos_omegat*sin_omegat)\n\n tau = nparctan(2*cs/(cc - ss))/(2*omega)\n\n ctau = npcos(omega*tau)\n stau = npsin(omega*tau)\n\n leftsumtop = (ctau*xc + stau*xs)*(ctau*xc + stau*xs)\n leftsumbot = ctau*ctau*cc + 2.0*ctau*stau*cs + stau*stau*ss\n leftsum = leftsumtop/leftsumbot\n\n rightsumtop = (ctau*xs - stau*xc)*(ctau*xs - stau*xc)\n rightsumbot = ctau*ctau*ss - 2.0*ctau*stau*cs + stau*stau*cc\n rightsum = rightsumtop/rightsumbot\n\n pval = 0.5*(leftsum + rightsum)\n\n return pval", "response": "Calculates the periodogram value for each omega."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef parallel_townsend_lsp(times, mags, startp, endp,\n stepsize=1.0e-4,\n nworkers=4):\n '''\n This calculates the Lomb-Scargle periodogram for the frequencies\n corresponding to the period interval (startp, endp) using a frequency step\n size of stepsize cycles/day. This uses the algorithm in Townsend 2010.\n\n '''\n\n # make sure there are no nans anywhere\n finiteind = np.isfinite(times) & np.isfinite(mags)\n ftimes, fmags = times[finiteind], mags[finiteind]\n\n # renormalize the mags to zero and scale them so that the variance = 1\n nmags = (fmags - np.median(fmags))/np.std(fmags)\n\n startf = 1.0/endp\n endf = 1.0/startp\n omegas = 2*np.pi*np.arange(startf, endf, stepsize)\n\n # parallel map the lsp calculations\n if (not nworkers) or (nworkers > NCPUS):\n nworkers = NCPUS\n LOGINFO('using %s workers...' % nworkers)\n\n pool = Pool(nworkers)\n\n tasks = [(ftimes, nmags, x) for x in omegas]\n lsp = pool.map(townsend_lombscargle_wrapper, tasks)\n\n pool.close()\n pool.join()\n\n return np.array(omegas), np.array(lsp)", "response": "This function calculates the Lomb - Scargle periodogram for the frequencies of the frequencies of the mags corresponding to the period interval startp and endp. This function uses a parallel pool to do the work."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef scipylsp_parallel(times,\n mags,\n errs, # ignored but for consistent API\n startp,\n endp,\n nbestpeaks=5,\n periodepsilon=0.1, # 0.1\n stepsize=1.0e-4,\n nworkers=4,\n sigclip=None,\n timebin=None):\n '''\n This uses the LSP function from the scipy library, which is fast as hell. We\n try to make it faster by running LSP for sections of the omegas array in\n parallel.\n\n '''\n\n # make sure there are no nans anywhere\n finiteind = np.isfinite(mags) & np.isfinite(errs)\n ftimes, fmags, ferrs = times[finiteind], mags[finiteind], errs[finiteind]\n\n if len(ftimes) > 0 and len(fmags) > 0:\n\n # sigclip the lightcurve if asked to do so\n if sigclip:\n worktimes, workmags, _ = sigclip_magseries(ftimes,\n fmags,\n ferrs,\n sigclip=sigclip)\n LOGINFO('ndet after sigclipping = %s' % len(worktimes))\n\n else:\n worktimes = ftimes\n workmags = fmags\n\n # bin the lightcurve if asked to do so\n if timebin:\n\n binned = time_bin_magseries(worktimes, workmags, binsize=timebin)\n worktimes = binned['binnedtimes']\n workmags = binned['binnedmags']\n\n # renormalize the working mags to zero and scale them so that the\n # variance = 1 for use with our LSP functions\n normmags = (workmags - np.median(workmags))/np.std(workmags)\n\n startf = 1.0/endp\n endf = 1.0/startp\n omegas = 2*np.pi*np.arange(startf, endf, stepsize)\n\n # partition the omegas array by nworkers\n tasks = []\n chunksize = int(float(len(omegas))/nworkers) + 1\n tasks = [omegas[x*chunksize:x*chunksize+chunksize]\n for x in range(nworkers)]\n\n # map to parallel workers\n if (not nworkers) or (nworkers > NCPUS):\n nworkers = NCPUS\n LOGINFO('using %s workers...' % nworkers)\n\n pool = Pool(nworkers)\n\n tasks = [(worktimes, normmags, x) for x in tasks]\n lsp = pool.map(parallel_scipylsp_worker, tasks)\n\n pool.close()\n pool.join()\n\n lsp = np.concatenate(lsp)\n periods = 2.0*np.pi/omegas\n\n # find the nbestpeaks for the periodogram: 1. sort the lsp array by\n # highest value first 2. go down the values until we find five values\n # that are separated by at least periodepsilon in period\n\n # make sure we only get finite lsp values\n finitepeakind = npisfinite(lsp)\n finlsp = lsp[finitepeakind]\n finperiods = periods[finitepeakind]\n\n bestperiodind = npargmax(finlsp)\n\n sortedlspind = np.argsort(finlsp)[::-1]\n sortedlspperiods = finperiods[sortedlspind]\n sortedlspvals = finlsp[sortedlspind]\n\n prevbestlspval = sortedlspvals[0]\n # now get the nbestpeaks\n nbestperiods, nbestlspvals, peakcount = (\n [finperiods[bestperiodind]],\n [finlsp[bestperiodind]],\n 1\n )\n prevperiod = sortedlspperiods[0]\n\n # find the best nbestpeaks in the lsp and their periods\n for period, lspval in zip(sortedlspperiods, sortedlspvals):\n\n if peakcount == nbestpeaks:\n break\n perioddiff = abs(period - prevperiod)\n bestperiodsdiff = [abs(period - x) for x in nbestperiods]\n\n # print('prevperiod = %s, thisperiod = %s, '\n # 'perioddiff = %s, peakcount = %s' %\n # (prevperiod, period, perioddiff, peakcount))\n\n # this ensures that this period is different from the last period\n # and from all the other existing best periods by periodepsilon to\n # make sure we jump to an entire different peak in the periodogram\n if (perioddiff > periodepsilon and\n all(x > periodepsilon for x in bestperiodsdiff)):\n nbestperiods.append(period)\n nbestlspvals.append(lspval)\n peakcount = peakcount + 1\n\n prevperiod = period\n\n\n return {'bestperiod':finperiods[bestperiodind],\n 'bestlspval':finlsp[bestperiodind],\n 'nbestpeaks':nbestpeaks,\n 'nbestlspvals':nbestlspvals,\n 'nbestperiods':nbestperiods,\n 'lspvals':lsp,\n 'omegas':omegas,\n 'periods':periods,\n 'method':'sls'}\n\n else:\n\n LOGERROR('no good detections for these times and mags, skipping...')\n return {'bestperiod':npnan,\n 'bestlspval':npnan,\n 'nbestpeaks':nbestpeaks,\n 'nbestlspvals':None,\n 'nbestperiods':None,\n 'lspvals':None,\n 'periods':None,\n 'method':'sls'}", "response": "This function uses scipy lsp to run a set of functions in parallel."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _periodicfeatures_worker(task):\n '''\n This is a parallel worker for the drivers below.\n\n '''\n\n pfpickle, lcbasedir, outdir, starfeatures, kwargs = task\n\n try:\n\n return get_periodicfeatures(pfpickle,\n lcbasedir,\n outdir,\n starfeatures=starfeatures,\n **kwargs)\n\n except Exception as e:\n\n LOGEXCEPTION('failed to get periodicfeatures for %s' % pfpickle)", "response": "This is a parallel worker for the drivers below. It is a parallel worker for the drivers below."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef parallel_periodicfeatures_lcdir(\n pfpkl_dir,\n lcbasedir,\n outdir,\n pfpkl_glob='periodfinding-*.pkl*',\n starfeaturesdir=None,\n fourierorder=5,\n # these are depth, duration, ingress duration\n transitparams=(-0.01,0.1,0.1),\n # these are depth, duration, depth ratio, secphase\n ebparams=(-0.2,0.3,0.7,0.5),\n pdiff_threshold=1.0e-4,\n sidereal_threshold=1.0e-4,\n sampling_peak_multiplier=5.0,\n sampling_startp=None,\n sampling_endp=None,\n timecols=None,\n magcols=None,\n errcols=None,\n lcformat='hat-sql',\n lcformatdir=None,\n sigclip=10.0,\n verbose=False,\n maxobjects=None,\n nworkers=NCPUS,\n recursive=True,\n):\n '''This runs parallel periodicfeature extraction for a directory of\n periodfinding result pickles.\n\n Parameters\n ----------\n\n pfpkl_dir : str\n The directory containing the pickles to process.\n\n lcbasedir : str\n The directory where all of the associated light curve files are located.\n\n outdir : str\n The directory where all the output will be written.\n\n pfpkl_glob : str\n The UNIX file glob to use to search for period-finder result pickles in\n `pfpkl_dir`.\n\n starfeaturesdir : str or None\n The directory containing the `starfeatures-.pkl` files for\n each object to use calculate neighbor proximity light curve features.\n\n fourierorder : int\n The Fourier order to use to generate sinusoidal function and fit that to\n the phased light curve.\n\n transitparams : list of floats\n The transit depth, duration, and ingress duration to use to generate a\n trapezoid planet transit model fit to the phased light curve. The period\n used is the one provided in `period`, while the epoch is automatically\n obtained from a spline fit to the phased light curve.\n\n ebparams : list of floats\n The primary eclipse depth, eclipse duration, the primary-secondary depth\n ratio, and the phase of the secondary eclipse to use to generate an\n eclipsing binary model fit to the phased light curve. The period used is\n the one provided in `period`, while the epoch is automatically obtained\n from a spline fit to the phased light curve.\n\n pdiff_threshold : float\n This is the max difference between periods to consider them the same.\n\n sidereal_threshold : float\n This is the max difference between any of the 'best' periods and the\n sidereal day periods to consider them the same.\n\n sampling_peak_multiplier : float\n This is the minimum multiplicative factor of a 'best' period's\n normalized periodogram peak over the sampling periodogram peak at the\n same period required to accept the 'best' period as possibly real.\n\n sampling_startp, sampling_endp : float\n If the `pgramlist` doesn't have a time-sampling Lomb-Scargle\n periodogram, it will be obtained automatically. Use these kwargs to\n control the minimum and maximum period interval to be searched when\n generating this periodogram.\n\n timecols : list of str or None\n The timecol keys to use from the lcdict in calculating the features.\n\n magcols : list of str or None\n The magcol keys to use from the lcdict in calculating the features.\n\n errcols : list of str or None\n The errcol keys to use from the lcdict in calculating the features.\n\n lcformat : str\n This is the `formatkey` associated with your light curve format, which\n you previously passed in to the `lcproc.register_lcformat`\n function. This will be used to look up how to find and read the light\n curves specified in `basedir` or `use_list_of_filenames`.\n\n lcformatdir : str or None\n If this is provided, gives the path to a directory when you've stored\n your lcformat description JSONs, other than the usual directories lcproc\n knows to search for them in. Use this along with `lcformat` to specify\n an LC format JSON file that's not currently registered with lcproc.\n\n sigclip : float or int or sequence of two floats/ints or None\n If a single float or int, a symmetric sigma-clip will be performed using\n the number provided as the sigma-multiplier to cut out from the input\n time-series.\n\n If a list of two ints/floats is provided, the function will perform an\n 'asymmetric' sigma-clip. The first element in this list is the sigma\n value to use for fainter flux/mag values; the second element in this\n list is the sigma value to use for brighter flux/mag values. For\n example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma\n dimmings and greater than 3-sigma brightenings. Here the meaning of\n \"dimming\" and \"brightening\" is set by *physics* (not the magnitude\n system), which is why the `magsarefluxes` kwarg must be correctly set.\n\n If `sigclip` is None, no sigma-clipping will be performed, and the\n time-series (with non-finite elems removed) will be passed through to\n the output.\n\n verbose : bool\n If True, will indicate progress while working.\n\n maxobjects : int\n The total number of objects to process from `pfpkl_list`.\n\n nworkers : int\n The number of parallel workers to launch to process the input.\n\n Returns\n -------\n\n dict\n A dict containing key: val pairs of the input period-finder result and\n the output periodic feature result pickles for each input pickle is\n returned.\n\n '''\n\n try:\n formatinfo = get_lcformat(lcformat,\n use_lcformat_dir=lcformatdir)\n if formatinfo:\n (dfileglob, readerfunc,\n dtimecols, dmagcols, derrcols,\n magsarefluxes, normfunc) = formatinfo\n else:\n LOGERROR(\"can't figure out the light curve format\")\n return None\n except Exception as e:\n LOGEXCEPTION(\"can't figure out the light curve format\")\n return None\n\n fileglob = pfpkl_glob\n\n # now find the files\n LOGINFO('searching for periodfinding pickles in %s ...' % pfpkl_dir)\n\n if recursive is False:\n matching = glob.glob(os.path.join(pfpkl_dir, fileglob))\n\n else:\n # use recursive glob for Python 3.5+\n if sys.version_info[:2] > (3,4):\n\n matching = glob.glob(os.path.join(pfpkl_dir,\n '**',\n fileglob),recursive=True)\n\n # otherwise, use os.walk and glob\n else:\n\n # use os.walk to go through the directories\n walker = os.walk(pfpkl_dir)\n matching = []\n\n for root, dirs, _files in walker:\n for sdir in dirs:\n searchpath = os.path.join(root,\n sdir,\n fileglob)\n foundfiles = glob.glob(searchpath)\n\n if foundfiles:\n matching.extend(foundfiles)\n\n\n # now that we have all the files, process them\n if matching and len(matching) > 0:\n\n LOGINFO('found %s periodfinding pickles, getting periodicfeatures...' %\n len(matching))\n\n return parallel_periodicfeatures(\n matching,\n lcbasedir,\n outdir,\n starfeaturesdir=starfeaturesdir,\n fourierorder=fourierorder,\n transitparams=transitparams,\n ebparams=ebparams,\n pdiff_threshold=pdiff_threshold,\n sidereal_threshold=sidereal_threshold,\n sampling_peak_multiplier=sampling_peak_multiplier,\n sampling_startp=sampling_startp,\n sampling_endp=sampling_endp,\n timecols=timecols,\n magcols=magcols,\n errcols=errcols,\n lcformat=lcformat,\n lcformatdir=lcformatdir,\n sigclip=sigclip,\n verbose=verbose,\n maxobjects=maxobjects,\n nworkers=nworkers,\n )\n\n else:\n\n LOGERROR('no periodfinding pickles found in %s' % (pfpkl_dir))\n return None", "response": "This function runs the parallel periodicfeature extraction for a directory of period - finder result pickles."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef angle_wrap(angle, radians=False):\n '''Wraps the input angle to 360.0 degrees.\n\n Parameters\n ----------\n\n angle : float\n The angle to wrap around 360.0 deg.\n\n radians : bool\n If True, will assume that the input is in radians. The output will then\n also be in radians.\n\n Returns\n -------\n\n float\n Wrapped angle. If radians is True: input is assumed to be in radians,\n output is also in radians.\n\n '''\n\n if radians:\n wrapped = angle % (2.0*pi_value)\n if wrapped < 0.0:\n wrapped = 2.0*pi_value + wrapped\n\n else:\n\n wrapped = angle % 360.0\n if wrapped < 0.0:\n wrapped = 360.0 + wrapped\n\n return wrapped", "response": "Wraps the input angle to 360. 0 degrees."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nconverting from decimal degrees to DD MM SS.", "response": "def decimal_to_dms(decimal_value):\n '''Converts from decimal degrees (for declination coords) to DD:MM:SS.\n\n Parameters\n ----------\n\n decimal_value : float\n A decimal value to convert to degrees, minutes, seconds sexagesimal\n format.\n\n Returns\n -------\n\n tuple\n A four element tuple is returned: (sign, HH, MM, SS.ssss...)\n\n '''\n\n if decimal_value < 0:\n negative = True\n dec_val = fabs(decimal_value)\n else:\n negative = False\n dec_val = decimal_value\n\n degrees = trunc(dec_val)\n minutes_deg = dec_val - degrees\n\n minutes_mm = minutes_deg * 60.0\n minutes_out = trunc(minutes_mm)\n seconds = (minutes_mm - minutes_out)*60.0\n\n if negative:\n degrees = degrees\n return '-', degrees, minutes_out, seconds\n else:\n return '+', degrees, minutes_out, seconds"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nconverting from decimal degrees to HHMMSS.", "response": "def decimal_to_hms(decimal_value):\n '''Converts from decimal degrees (for RA coords) to HH:MM:SS.\n\n Parameters\n ----------\n\n decimal_value : float\n A decimal value to convert to hours, minutes, seconds. Negative values\n will be wrapped around 360.0.\n\n Returns\n -------\n\n tuple\n A three element tuple is returned: (HH, MM, SS.ssss...)\n\n '''\n\n # wrap to 360.0\n if decimal_value < 0:\n dec_wrapped = 360.0 + decimal_value\n else:\n dec_wrapped = decimal_value\n\n # convert to decimal hours first\n dec_hours = dec_wrapped/15.0\n\n if dec_hours < 0:\n negative = True\n dec_val = fabs(dec_hours)\n else:\n negative = False\n dec_val = dec_hours\n\n hours = trunc(dec_val)\n minutes_hrs = dec_val - hours\n\n minutes_mm = minutes_hrs * 60.0\n minutes_out = trunc(minutes_mm)\n seconds = (minutes_mm - minutes_out)*60.0\n\n if negative:\n hours = -hours\n return hours, minutes_out, seconds\n else:\n return hours, minutes_out, seconds"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef hms_str_to_tuple(hms_string):\n '''Converts a string of the form HH:MM:SS or HH MM SS to a tuple of the form\n (HH, MM, SS).\n\n Parameters\n ----------\n\n hms_string : str\n A RA coordinate string of the form 'HH:MM:SS.sss' or 'HH MM SS.sss'.\n\n Returns\n -------\n\n tuple\n A three element tuple is returned (HH, MM, SS.ssss...)\n\n '''\n\n if ':' in hms_string:\n separator = ':'\n else:\n separator = ' '\n\n hh, mm, ss = hms_string.split(separator)\n\n return int(hh), int(mm), float(ss)", "response": "Converts a string of the form HH MM SS to a tuple of the form\n HH MM SS. ssss."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef dms_str_to_tuple(dms_string):\n '''Converts a string of the form [+-]DD:MM:SS or [+-]DD MM SS to a tuple of\n the form (sign, DD, MM, SS).\n\n Parameters\n ----------\n\n dms_string : str\n A declination coordinate string of the form '[+-]DD:MM:SS.sss' or\n '[+-]DD MM SS.sss'. The sign in front of DD is optional. If it's not\n there, this function will assume that the coordinate string is a\n positive value.\n\n Returns\n -------\n\n tuple\n A four element tuple of the form: (sign, DD, MM, SS.ssss...).\n\n '''\n if ':' in dms_string:\n separator = ':'\n else:\n separator = ' '\n\n sign_dd, mm, ss = dms_string.split(separator)\n if sign_dd.startswith('+') or sign_dd.startswith('-'):\n sign, dd = sign_dd[0], sign_dd[1:]\n else:\n sign, dd = '+', sign_dd\n\n return sign, int(dd), int(mm), float(ss)", "response": "Converts a string of the form [ + DD MM SS or - SS. sss or + DD MM SS to a tuple of\n the form ( sign DD MM SS SS. ssss..."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef hms_to_decimal(hours, minutes, seconds, returndeg=True):\n '''Converts from HH, MM, SS to a decimal value.\n\n Parameters\n ----------\n\n hours : int\n The HH part of a RA coordinate.\n\n minutes : int\n The MM part of a RA coordinate.\n\n seconds : float\n The SS.sss part of a RA coordinate.\n\n returndeg : bool\n If this is True, then will return decimal degrees as the output.\n If this is False, then will return decimal HOURS as the output.\n Decimal hours are sometimes used in FITS headers.\n\n Returns\n -------\n\n float\n The right ascension value in either decimal degrees or decimal hours\n depending on `returndeg`.\n\n '''\n\n if hours > 24:\n\n return None\n\n else:\n\n dec_hours = fabs(hours) + fabs(minutes)/60.0 + fabs(seconds)/3600.0\n\n if returndeg:\n\n dec_deg = dec_hours*15.0\n\n if dec_deg < 0:\n dec_deg = dec_deg + 360.0\n dec_deg = dec_deg % 360.0\n return dec_deg\n else:\n return dec_hours", "response": "Converts from HH MM SS to decimal value."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nconverting from DD MM : SS to decimal degrees.", "response": "def dms_to_decimal(sign, degrees, minutes, seconds):\n '''Converts from DD:MM:SS to a decimal value.\n\n Parameters\n ----------\n\n sign : {'+', '-', ''}\n The sign part of a Dec coordinate.\n\n degrees : int\n The DD part of a Dec coordinate.\n\n minutes : int\n The MM part of a Dec coordinate.\n\n seconds : float\n The SS.sss part of a Dec coordinate.\n\n Returns\n -------\n\n float\n The declination value in decimal degrees.\n\n '''\n\n dec_deg = fabs(degrees) + fabs(minutes)/60.0 + fabs(seconds)/3600.0\n\n if sign == '-':\n return -dec_deg\n else:\n return dec_deg"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef great_circle_dist(ra1, dec1, ra2, dec2):\n '''Calculates the great circle angular distance between two coords.\n\n This calculates the great circle angular distance in arcseconds between two\n coordinates (ra1,dec1) and (ra2,dec2). This is basically a clone of GCIRC\n from the IDL Astrolib.\n\n Parameters\n ----------\n\n ra1,dec1 : float or array-like\n The first coordinate's right ascension and declination value(s) in\n decimal degrees.\n\n ra2,dec2 : float or array-like\n The second coordinate's right ascension and declination value(s) in\n decimal degrees.\n\n Returns\n -------\n\n float or array-like\n Great circle distance between the two coordinates in arseconds.\n\n Notes\n -----\n\n If (`ra1`, `dec1`) is scalar and (`ra2`, `dec2`) is scalar: the result is a\n float distance in arcseconds.\n\n If (`ra1`, `dec1`) is scalar and (`ra2`, `dec2`) is array-like: the result\n is an np.array with distance in arcseconds between (`ra1`, `dec1`) and each\n element of (`ra2`, `dec2`).\n\n If (`ra1`, `dec1`) is array-like and (`ra2`, `dec2`) is scalar: the result\n is an np.array with distance in arcseconds between (`ra2`, `dec2`) and each\n element of (`ra1`, `dec1`).\n\n If (`ra1`, `dec1`) and (`ra2`, `dec2`) are both array-like: the result is an\n np.array with the pair-wise distance in arcseconds between each element of\n the two coordinate lists. In this case, if the input array-likes are not the\n same length, then excess elements of the longer one will be ignored.\n\n '''\n\n # wrap RA if negative or larger than 360.0 deg\n in_ra1 = ra1 % 360.0\n in_ra1 = in_ra1 + 360.0*(in_ra1 < 0.0)\n in_ra2 = ra2 % 360.0\n in_ra2 = in_ra2 + 360.0*(in_ra1 < 0.0)\n\n # convert to radians\n ra1_rad, dec1_rad = np.deg2rad(in_ra1), np.deg2rad(dec1)\n ra2_rad, dec2_rad = np.deg2rad(in_ra2), np.deg2rad(dec2)\n\n del_dec2 = (dec2_rad - dec1_rad)/2.0\n del_ra2 = (ra2_rad - ra1_rad)/2.0\n sin_dist = np.sqrt(np.sin(del_dec2) * np.sin(del_dec2) +\n np.cos(dec1_rad) * np.cos(dec2_rad) *\n np.sin(del_ra2) * np.sin(del_ra2))\n\n dist_rad = 2.0 * np.arcsin(sin_dist)\n\n # return the distance in arcseconds\n return np.rad2deg(dist_rad)*3600.0", "response": "Calculates the great circle angular distance between two coordinates."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nfind the closest object in (`ra2`, `dec2`) to scalar coordinate pair (`ra1`, `dec1`) and returns the distance in arcseconds. This is a quick matcher that uses the `great_circle_dist` function to find the closest object in (`ra2`, `dec2`) within `match_radius` arcseconds to (`ra1`, `dec1`). (`ra1`, `dec1`) must be a scalar pair, while (`ra2`, `dec2`) must be array-likes of the same lengths. Parameters ---------- ra1,dec1 : float Coordinate of the object to find matches to. In decimal degrees. ra2,dec2 : array-like The coordinates that will be searched for matches. In decimal degrees. match_radius : float The match radius in arcseconds to use for the match. Returns ------- tuple A two element tuple like the following:: (True -> no match found or False -> found a match, minimum distance between target and list in arcseconds)", "response": "def xmatch_basic(ra1, dec1, ra2, dec2, match_radius=5.0):\n '''Finds the closest object in (`ra2`, `dec2`) to scalar coordinate pair\n (`ra1`, `dec1`) and returns the distance in arcseconds.\n\n This is a quick matcher that uses the `great_circle_dist` function to find\n the closest object in (`ra2`, `dec2`) within `match_radius` arcseconds to\n (`ra1`, `dec1`). (`ra1`, `dec1`) must be a scalar pair, while\n (`ra2`, `dec2`) must be array-likes of the same lengths.\n\n Parameters\n ----------\n\n ra1,dec1 : float\n Coordinate of the object to find matches to. In decimal degrees.\n\n ra2,dec2 : array-like\n The coordinates that will be searched for matches. In decimal degrees.\n\n match_radius : float\n The match radius in arcseconds to use for the match.\n\n Returns\n -------\n\n tuple\n A two element tuple like the following::\n\n (True -> no match found or False -> found a match,\n minimum distance between target and list in arcseconds)\n\n '''\n\n min_dist_arcsec = np.min(great_circle_dist(ra1,dec1,ra2,dec2))\n\n if (min_dist_arcsec < match_radius):\n return (True,min_dist_arcsec)\n else:\n return (False,min_dist_arcsec)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef xmatch_neighbors(ra1, dec1,\n ra2, dec2,\n match_radius=60.0,\n includeself=False,\n sortresults=True):\n '''Finds the closest objects in (`ra2`, `dec2`) to scalar coordinate pair\n (`ra1`, `dec1`) and returns the indices of the objects that match.\n\n This is a quick matcher that uses the `great_circle_dist` function to find\n the closest object in (`ra2`, `dec2`) within `match_radius` arcseconds to\n (`ra1`, `dec1`). (`ra1`, `dec1`) must be a scalar pair, while\n (`ra2`, `dec2`) must be array-likes of the same lengths.\n\n Parameters\n ----------\n\n ra1,dec1 : float\n Coordinate of the object to find matches to. In decimal degrees.\n\n ra2,dec2 : array-like\n The coordinates that will be searched for matches. In decimal degrees.\n\n match_radius : float\n The match radius in arcseconds to use for the match.\n\n includeself : bool\n If this is True, the object itself will be included in the match\n results.\n\n sortresults : bool\n If this is True, the match indices will be sorted by distance.\n\n Returns\n -------\n\n tuple\n A tuple like the following is returned::\n\n (True -> matches found or False -> no matches found,\n minimum distance between target and list,\n np.array of indices where list of coordinates is\n closer than `match_radius` arcseconds from the target,\n np.array of distances in arcseconds)\n\n '''\n\n dist = great_circle_dist(ra1,dec1,ra2,dec2)\n\n if includeself:\n match_dist_ind = np.where(dist < match_radius)\n\n else:\n # make sure we match only objects that are not the same as this object\n match_dist_ind = np.where((dist < match_radius) & (dist > 0.1))\n\n if len(match_dist_ind) > 0:\n match_dists = dist[match_dist_ind]\n dist_sort_ind = np.argsort(match_dists)\n\n if sortresults:\n match_dist_ind = (match_dist_ind[0])[dist_sort_ind]\n\n min_dist = np.min(match_dists)\n\n return (True,min_dist,match_dist_ind,match_dists[dist_sort_ind])\n\n else:\n return (False,)", "response": "Finds the closest objects in ra1 to dec1 and returns the indices of the objects that are closer than match_radius arcseconds from the target."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef equatorial_to_galactic(ra, decl, equinox='J2000'):\n '''This converts from equatorial coords to galactic coords.\n\n Parameters\n ----------\n\n ra : float or array-like\n Right ascension values(s) in decimal degrees.\n\n decl : float or array-like\n Declination value(s) in decimal degrees.\n\n equinox : str\n The equinox that the coordinates are measured at. This must be\n recognizable by Astropy's `SkyCoord` class.\n\n Returns\n -------\n\n tuple of (float, float) or tuple of (np.array, np.array)\n The galactic coordinates (l, b) for each element of the input\n (`ra`, `decl`).\n\n '''\n\n # convert the ra/decl to gl, gb\n radecl = SkyCoord(ra=ra*u.degree, dec=decl*u.degree, equinox=equinox)\n\n gl = radecl.galactic.l.degree\n gb = radecl.galactic.b.degree\n\n return gl, gb", "response": "This converts from equatorial coordinates to galactic coordinates."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef generate_transit_lightcurve(\n times,\n mags=None,\n errs=None,\n paramdists={'transitperiod':sps.uniform(loc=0.1,scale=49.9),\n 'transitdepth':sps.uniform(loc=1.0e-4,scale=2.0e-2),\n 'transitduration':sps.uniform(loc=0.01,scale=0.29)},\n magsarefluxes=False,\n):\n '''This generates fake planet transit light curves.\n\n Parameters\n ----------\n\n times : np.array\n This is an array of time values that will be used as the time base.\n\n mags,errs : np.array\n These arrays will have the model added to them. If either is\n None, `np.full_like(times, 0.0)` will used as a substitute and the model\n light curve will be centered around 0.0.\n\n paramdists : dict\n This is a dict containing parameter distributions to use for the\n model params, containing the following keys ::\n\n {'transitperiod', 'transitdepth', 'transitduration'}\n\n The values of these keys should all be 'frozen' scipy.stats distribution\n objects, e.g.:\n\n https://docs.scipy.org/doc/scipy/reference/stats.html#continuous-distributions\n The variability epoch will be automatically chosen from a uniform\n distribution between `times.min()` and `times.max()`.\n\n The ingress duration will be automatically chosen from a uniform\n distribution ranging from 0.05 to 0.5 of the transitduration.\n\n The transitdepth will be flipped automatically as appropriate if\n `magsarefluxes=True`.\n\n magsarefluxes : bool\n If the generated time series is meant to be a flux time-series, set this\n to True to get the correct sign of variability amplitude.\n\n Returns\n -------\n\n dict\n A dict of the form below is returned::\n\n {'vartype': 'planet',\n 'params': {'transitperiod': generated value of period,\n 'transitepoch': generated value of epoch,\n 'transitdepth': generated value of transit depth,\n 'transitduration': generated value of transit duration,\n 'ingressduration': generated value of transit ingress\n duration},\n 'times': the model times,\n 'mags': the model mags,\n 'errs': the model errs,\n 'varperiod': the generated period of variability == 'transitperiod'\n 'varamplitude': the generated amplitude of\n variability == 'transitdepth'}\n\n '''\n\n if mags is None:\n mags = np.full_like(times, 0.0)\n\n if errs is None:\n errs = np.full_like(times, 0.0)\n\n # choose the epoch\n epoch = npr.random()*(times.max() - times.min()) + times.min()\n\n # choose the period, depth, duration\n period = paramdists['transitperiod'].rvs(size=1)\n depth = paramdists['transitdepth'].rvs(size=1)\n duration = paramdists['transitduration'].rvs(size=1)\n\n # figure out the ingress duration\n ingduration = npr.random()*(0.5*duration - 0.05*duration) + 0.05*duration\n\n # fix the transit depth if it needs to be flipped\n if magsarefluxes and depth < 0.0:\n depth = -depth\n elif not magsarefluxes and depth > 0.0:\n depth = -depth\n\n # generate the model\n modelmags, phase, ptimes, pmags, perrs = (\n transits.trapezoid_transit_func([period, epoch, depth,\n duration, ingduration],\n times,\n mags,\n errs)\n )\n\n # resort in original time order\n timeind = np.argsort(ptimes)\n mtimes = ptimes[timeind]\n mmags = modelmags[timeind]\n merrs = perrs[timeind]\n\n # return a dict with everything\n modeldict = {\n 'vartype':'planet',\n 'params':{x:np.asscalar(y) for x,y in zip(['transitperiod',\n 'transitepoch',\n 'transitdepth',\n 'transitduration',\n 'ingressduration'],\n [period,\n epoch,\n depth,\n duration,\n ingduration])},\n 'times':mtimes,\n 'mags':mmags,\n 'errs':merrs,\n # these are standard keys that help with later characterization of\n # variability as a function period, variability amplitude, object mag,\n # ndet, etc.\n 'varperiod':period,\n 'varamplitude':depth\n }\n\n return modeldict", "response": "This function generates a fake planet transit light curve for the given time - base and time - series."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef generate_eb_lightcurve(\n times,\n mags=None,\n errs=None,\n paramdists={'period':sps.uniform(loc=0.2,scale=99.8),\n 'pdepth':sps.uniform(loc=1.0e-4,scale=0.7),\n 'pduration':sps.uniform(loc=0.01,scale=0.44),\n 'depthratio':sps.uniform(loc=0.01,scale=0.99),\n 'secphase':sps.norm(loc=0.5,scale=0.1)},\n magsarefluxes=False,\n):\n '''This generates fake EB light curves.\n\n Parameters\n ----------\n\n times : np.array\n This is an array of time values that will be used as the time base.\n\n mags,errs : np.array\n These arrays will have the model added to them. If either is\n None, `np.full_like(times, 0.0)` will used as a substitute and the model\n light curve will be centered around 0.0.\n\n paramdists : dict\n This is a dict containing parameter distributions to use for the\n model params, containing the following keys ::\n\n {'period', 'pdepth', 'pduration', 'depthratio', 'secphase'}\n\n The values of these keys should all be 'frozen' scipy.stats distribution\n objects, e.g.:\n\n https://docs.scipy.org/doc/scipy/reference/stats.html#continuous-distributions\n The variability epoch will be automatically chosen from a uniform\n distribution between `times.min()` and `times.max()`.\n\n The `pdepth` will be flipped automatically as appropriate if\n `magsarefluxes=True`.\n\n magsarefluxes : bool\n If the generated time series is meant to be a flux time-series, set this\n to True to get the correct sign of variability amplitude.\n\n Returns\n -------\n\n dict\n A dict of the form below is returned::\n\n {'vartype': 'EB',\n 'params': {'period': generated value of period,\n 'epoch': generated value of epoch,\n 'pdepth': generated value of priary eclipse depth,\n 'pduration': generated value of prim eclipse duration,\n 'depthratio': generated value of prim/sec eclipse\n depth ratio},\n 'times': the model times,\n 'mags': the model mags,\n 'errs': the model errs,\n 'varperiod': the generated period of variability == 'period'\n 'varamplitude': the generated amplitude of\n variability == 'pdepth'}\n\n '''\n\n if mags is None:\n mags = np.full_like(times, 0.0)\n\n if errs is None:\n errs = np.full_like(times, 0.0)\n\n # choose the epoch\n epoch = npr.random()*(times.max() - times.min()) + times.min()\n\n # choose the period, pdepth, duration, depthratio\n period = paramdists['period'].rvs(size=1)\n pdepth = paramdists['pdepth'].rvs(size=1)\n pduration = paramdists['pduration'].rvs(size=1)\n depthratio = paramdists['depthratio'].rvs(size=1)\n secphase = paramdists['secphase'].rvs(size=1)\n\n # fix the transit depth if it needs to be flipped\n if magsarefluxes and pdepth < 0.0:\n pdepth = -pdepth\n elif not magsarefluxes and pdepth > 0.0:\n pdepth = -pdepth\n\n # generate the model\n modelmags, phase, ptimes, pmags, perrs = (\n eclipses.invgauss_eclipses_func([period, epoch, pdepth,\n pduration, depthratio, secphase],\n times,\n mags,\n errs)\n )\n\n # resort in original time order\n timeind = np.argsort(ptimes)\n mtimes = ptimes[timeind]\n mmags = modelmags[timeind]\n merrs = perrs[timeind]\n\n # return a dict with everything\n modeldict = {\n 'vartype':'EB',\n 'params':{x:np.asscalar(y) for x,y in zip(['period',\n 'epoch',\n 'pdepth',\n 'pduration',\n 'depthratio'],\n [period,\n epoch,\n pdepth,\n pduration,\n depthratio])},\n 'times':mtimes,\n 'mags':mmags,\n 'errs':merrs,\n 'varperiod':period,\n 'varamplitude':pdepth,\n }\n\n return modeldict", "response": "This function generates fake EB light curves for a set of time - base objects."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef generate_flare_lightcurve(\n times,\n mags=None,\n errs=None,\n paramdists={\n # flare peak amplitude from 0.01 mag to 1.0 mag above median. this\n # is tuned for redder bands, flares are much stronger in bluer\n # bands, so tune appropriately for your situation.\n 'amplitude':sps.uniform(loc=0.01,scale=0.99),\n # up to 5 flares per LC and at least 1\n 'nflares':[1,5],\n # 10 minutes to 1 hour for rise stdev\n 'risestdev':sps.uniform(loc=0.007, scale=0.04),\n # 1 hour to 4 hours for decay time constant\n 'decayconst':sps.uniform(loc=0.04, scale=0.163)\n },\n magsarefluxes=False,\n):\n '''This generates fake flare light curves.\n\n Parameters\n ----------\n\n times : np.array\n This is an array of time values that will be used as the time base.\n\n mags,errs : np.array\n These arrays will have the model added to them. If either is\n None, `np.full_like(times, 0.0)` will used as a substitute and the model\n light curve will be centered around 0.0.\n\n paramdists : dict\n This is a dict containing parameter distributions to use for the\n model params, containing the following keys ::\n\n {'amplitude', 'nflares', 'risestdev', 'decayconst'}\n\n The values of these keys should all be 'frozen' scipy.stats distribution\n objects, e.g.:\n\n https://docs.scipy.org/doc/scipy/reference/stats.html#continuous-distributions\n The `flare_peak_time` for each flare will be generated automatically\n between `times.min()` and `times.max()` using a uniform distribution.\n\n The `amplitude` will be flipped automatically as appropriate if\n `magsarefluxes=True`.\n\n magsarefluxes : bool\n If the generated time series is meant to be a flux time-series, set this\n to True to get the correct sign of variability amplitude.\n\n Returns\n -------\n\n dict\n A dict of the form below is returned::\n\n {'vartype': 'flare',\n 'params': {'amplitude': generated value of flare amplitudes,\n 'nflares': generated value of number of flares,\n 'risestdev': generated value of stdev of rise time,\n 'decayconst': generated value of decay constant,\n 'peaktime': generated value of flare peak time},\n 'times': the model times,\n 'mags': the model mags,\n 'errs': the model errs,\n 'varamplitude': the generated amplitude of\n variability == 'amplitude'}\n\n '''\n\n if mags is None:\n mags = np.full_like(times, 0.0)\n\n if errs is None:\n errs = np.full_like(times, 0.0)\n\n nflares = npr.randint(paramdists['nflares'][0],\n high=paramdists['nflares'][1])\n\n # generate random flare peak times based on the number of flares\n flarepeaktimes = (\n npr.random(\n size=nflares\n )*(times.max() - times.min()) + times.min()\n )\n\n # now add the flares to the time-series\n params = {'nflares':nflares}\n\n for flareind, peaktime in zip(range(nflares), flarepeaktimes):\n\n # choose the amplitude, rise stdev and decay time constant\n amp = paramdists['amplitude'].rvs(size=1)\n risestdev = paramdists['risestdev'].rvs(size=1)\n decayconst = paramdists['decayconst'].rvs(size=1)\n\n # fix the transit depth if it needs to be flipped\n if magsarefluxes and amp < 0.0:\n amp = -amp\n elif not magsarefluxes and amp > 0.0:\n amp = -amp\n\n # add this flare to the light curve\n modelmags, ptimes, pmags, perrs = (\n flares.flare_model(\n [amp, peaktime, risestdev, decayconst],\n times,\n mags,\n errs\n )\n )\n\n # update the mags\n mags = modelmags\n\n # add the flare params to the modeldict\n params[flareind] = {'peaktime':peaktime,\n 'amplitude':amp,\n 'risestdev':risestdev,\n 'decayconst':decayconst}\n\n\n #\n # done with all flares\n #\n\n # return a dict with everything\n modeldict = {\n 'vartype':'flare',\n 'params':params,\n 'times':times,\n 'mags':mags,\n 'errs':errs,\n 'varperiod':None,\n # FIXME: this is complicated because we can have multiple flares\n # figure out a good way to handle this upstream\n 'varamplitude':[params[x]['amplitude']\n for x in range(params['nflares'])],\n }\n\n return modeldict", "response": "This function generates a fake flare light curve for the given set of time - base entries."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef generate_rrab_lightcurve(\n times,\n mags=None,\n errs=None,\n paramdists={\n 'period':sps.uniform(loc=0.45,scale=0.35),\n 'fourierorder':[8,11],\n 'amplitude':sps.uniform(loc=0.4,scale=0.5),\n 'phioffset':np.pi,\n },\n magsarefluxes=False\n):\n '''This generates fake RRab light curves.\n\n Parameters\n ----------\n\n times : np.array\n This is an array of time values that will be used as the time base.\n\n mags,errs : np.array\n These arrays will have the model added to them. If either is\n None, `np.full_like(times, 0.0)` will used as a substitute and the model\n light curve will be centered around 0.0.\n\n paramdists : dict\n This is a dict containing parameter distributions to use for the\n model params, containing the following keys ::\n\n {'period', 'fourierorder', 'amplitude'}\n\n The values of these keys should all be 'frozen' scipy.stats distribution\n objects, e.g.:\n\n https://docs.scipy.org/doc/scipy/reference/stats.html#continuous-distributions\n The variability epoch will be automatically chosen from a uniform\n distribution between `times.min()` and `times.max()`.\n\n The `amplitude` will be flipped automatically as appropriate if\n `magsarefluxes=True`.\n\n magsarefluxes : bool\n If the generated time series is meant to be a flux time-series, set this\n to True to get the correct sign of variability amplitude.\n\n Returns\n -------\n\n dict\n A dict of the form below is returned::\n\n {'vartype': 'RRab',\n 'params': {'period': generated value of period,\n 'epoch': generated value of epoch,\n 'amplitude': generated value of amplitude,\n 'fourierorder': generated value of fourier order,\n 'fourieramps': generated values of fourier amplitudes,\n 'fourierphases': generated values of fourier phases},\n 'times': the model times,\n 'mags': the model mags,\n 'errs': the model errs,\n 'varperiod': the generated period of variability == 'period'\n 'varamplitude': the generated amplitude of\n variability == 'amplitude'}\n\n '''\n\n modeldict = generate_sinusoidal_lightcurve(times,\n mags=mags,\n errs=errs,\n paramdists=paramdists,\n magsarefluxes=magsarefluxes)\n modeldict['vartype'] = 'RRab'\n return modeldict", "response": "This function generates fake RRab light curves for a set of time - base objects."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef make_fakelc(lcfile,\n outdir,\n magrms=None,\n randomizemags=True,\n randomizecoords=False,\n lcformat='hat-sql',\n lcformatdir=None,\n timecols=None,\n magcols=None,\n errcols=None):\n '''This preprocesses an input real LC and sets it up to be a fake LC.\n\n Parameters\n ----------\n\n lcfile : str\n This is an input light curve file that will be used to copy over the\n time-base. This will be used to generate the time-base for fake light\n curves to provide a realistic simulation of the observing window\n function.\n\n outdir : str\n The output directory where the the fake light curve will be written.\n\n magrms : dict\n This is a dict containing the SDSS r mag-RMS (SDSS rmag-MAD preferably)\n relation based on all light curves that the input lcfile is from. This\n will be used to generate the median mag and noise corresponding to the\n magnitude chosen for this fake LC.\n\n randomizemags : bool\n If this is True, then a random mag between the first and last magbin in\n magrms will be chosen as the median mag for this light curve. This\n choice will be weighted by the mag bin probability obtained from the\n magrms kwarg. Otherwise, the median mag will be taken from the input\n lcfile's lcdict['objectinfo']['sdssr'] key or a transformed SDSS r mag\n generated from the input lcfile's lcdict['objectinfo']['jmag'],\n ['hmag'], and ['kmag'] keys. The magrms relation for each magcol will be\n used to generate Gaussian noise at the correct level for the magbin this\n light curve's median mag falls into.\n\n randomizecoords : bool\n If this is True, will randomize the RA, DEC of the output fake object\n and not copy over the RA/DEC from the real input object.\n\n lcformat : str\n This is the `formatkey` associated with your input real light curve\n format, which you previously passed in to the `lcproc.register_lcformat`\n function. This will be used to look up how to find and read the light\n curve specified in `lcfile`.\n\n lcformatdir : str or None\n If this is provided, gives the path to a directory when you've stored\n your lcformat description JSONs, other than the usual directories lcproc\n knows to search for them in. Use this along with `lcformat` to specify\n an LC format JSON file that's not currently registered with lcproc.\n\n timecols : list of str or None\n The timecol keys to use from the input lcdict in generating the fake\n light curve. Fake LCs will be generated for each each\n timecol/magcol/errcol combination in the input light curve.\n\n magcols : list of str or None\n The magcol keys to use from the input lcdict in generating the fake\n light curve. Fake LCs will be generated for each each\n timecol/magcol/errcol combination in the input light curve.\n\n errcols : list of str or None\n The errcol keys to use from the input lcdict in generating the fake\n light curve. Fake LCs will be generated for each each\n timecol/magcol/errcol combination in the input light curve.\n\n Returns\n -------\n\n tuple\n A tuple of the following form is returned::\n\n (fakelc_fpath,\n fakelc_lcdict['columns'],\n fakelc_lcdict['objectinfo'],\n fakelc_lcdict['moments'])\n\n '''\n\n try:\n formatinfo = get_lcformat(lcformat,\n use_lcformat_dir=lcformatdir)\n if formatinfo:\n (fileglob, readerfunc,\n dtimecols, dmagcols, derrcols,\n magsarefluxes, normfunc) = formatinfo\n else:\n LOGERROR(\"can't figure out the light curve format\")\n return None\n except Exception as e:\n LOGEXCEPTION(\"can't figure out the light curve format\")\n return None\n\n # override the default timecols, magcols, and errcols\n # using the ones provided to the function\n if timecols is None:\n timecols = dtimecols\n if magcols is None:\n magcols = dmagcols\n if errcols is None:\n errcols = derrcols\n\n # read in the light curve\n lcdict = readerfunc(lcfile)\n if isinstance(lcdict, tuple) and isinstance(lcdict[0],dict):\n lcdict = lcdict[0]\n\n # set up the fakelcdict with a randomly assigned objectid\n fakeobjectid = sha512(npr.bytes(12)).hexdigest()[-8:]\n fakelcdict = {\n 'objectid':fakeobjectid,\n 'objectinfo':{'objectid':fakeobjectid},\n 'columns':[],\n 'moments':{},\n 'origformat':lcformat,\n }\n\n\n # now, get the actual mag of this object and other info and use that to\n # populate the corresponding entries of the fakelcdict objectinfo\n if ('objectinfo' in lcdict and\n isinstance(lcdict['objectinfo'], dict)):\n\n objectinfo = lcdict['objectinfo']\n\n # get the RA\n if (not randomizecoords and 'ra' in objectinfo and\n objectinfo['ra'] is not None and\n np.isfinite(objectinfo['ra'])):\n\n fakelcdict['objectinfo']['ra'] = objectinfo['ra']\n\n else:\n\n # if there's no RA available, we'll assign a random one between 0\n # and 360.0\n LOGWARNING('%s: assigning a random right ascension' % lcfile)\n fakelcdict['objectinfo']['ra'] = npr.random()*360.0\n\n # get the DEC\n if (not randomizecoords and 'decl' in objectinfo and\n objectinfo['decl'] is not None and\n np.isfinite(objectinfo['decl'])):\n\n fakelcdict['objectinfo']['decl'] = objectinfo['decl']\n\n else:\n\n # if there's no DECL available, we'll assign a random one between\n # -90.0 and +90.0\n LOGWARNING(' %s: assigning a random declination' % lcfile)\n fakelcdict['objectinfo']['decl'] = npr.random()*180.0 - 90.0\n\n # get the SDSS r mag for this object\n # this will be used for getting the eventual mag-RMS relation later\n if ((not randomizemags) and 'sdssr' in objectinfo and\n objectinfo['sdssr'] is not None and\n np.isfinite(objectinfo['sdssr'])):\n\n fakelcdict['objectinfo']['sdssr'] = objectinfo['sdssr']\n\n # if the SDSS r is unavailable, but we have J, H, K: use those to get\n # the SDSS r by using transformations\n elif ((not randomizemags) and ('jmag' in objectinfo and\n objectinfo['jmag'] is not None and\n np.isfinite(objectinfo['jmag'])) and\n ('hmag' in objectinfo and\n objectinfo['hmag'] is not None and\n np.isfinite(objectinfo['hmag'])) and\n ('kmag' in objectinfo and\n objectinfo['kmag'] is not None and\n np.isfinite(objectinfo['kmag']))):\n\n LOGWARNING('used JHK mags to generate an SDSS r mag for %s' %\n lcfile)\n fakelcdict['objectinfo']['sdssr'] = jhk_to_sdssr(\n objectinfo['jmag'],\n objectinfo['hmag'],\n objectinfo['kmag']\n )\n\n # if there are no mags available or we're specically told to randomize\n # them, generate a random mag between 8 and 16.0\n elif randomizemags and magrms:\n\n LOGWARNING(' %s: assigning a random mag weighted by mag '\n 'bin probabilities' % lcfile)\n\n magbins = magrms[magcols[0]]['binned_sdssr_median']\n binprobs = magrms[magcols[0]]['magbin_probabilities']\n\n # this is the center of the magbin chosen\n magbincenter = npr.choice(magbins,size=1,p=binprobs)\n\n # in this magbin, choose between center and -+ 0.25 mag\n chosenmag = (\n npr.random()*((magbincenter+0.25) - (magbincenter-0.25)) +\n (magbincenter-0.25)\n )\n\n fakelcdict['objectinfo']['sdssr'] = np.asscalar(chosenmag)\n\n # if there are no mags available at all, generate a random mag\n # between 8 and 16.0\n else:\n\n LOGWARNING(' %s: assigning a random mag from '\n 'uniform distribution between 8.0 and 16.0' % lcfile)\n\n fakelcdict['objectinfo']['sdssr'] = npr.random()*8.0 + 8.0\n\n # if there's no info available, generate fake info\n else:\n\n LOGWARNING('no object information found in %s, '\n 'generating random ra, decl, sdssr' %\n lcfile)\n fakelcdict['objectinfo']['ra'] = npr.random()*360.0\n fakelcdict['objectinfo']['decl'] = npr.random()*180.0 - 90.0\n fakelcdict['objectinfo']['sdssr'] = npr.random()*8.0 + 8.0\n\n\n #\n # NOW FILL IN THE TIMES, MAGS, ERRS\n #\n\n # get the time columns\n for tcind, tcol in enumerate(timecols):\n\n if '.' in tcol:\n tcolget = tcol.split('.')\n else:\n tcolget = [tcol]\n\n if tcol not in fakelcdict:\n fakelcdict[tcol] = _dict_get(lcdict, tcolget)\n fakelcdict['columns'].append(tcol)\n\n # update the ndet with the first time column's size. it's possible\n # that different time columns have different lengths, but that would\n # be weird and we won't deal with it for now\n if tcind == 0:\n fakelcdict['objectinfo']['ndet'] = fakelcdict[tcol].size\n\n\n # get the mag columns\n for mcol in magcols:\n\n if '.' in mcol:\n mcolget = mcol.split('.')\n else:\n mcolget = [mcol]\n\n # put the mcol in only once\n if mcol not in fakelcdict:\n\n measuredmags = _dict_get(lcdict, mcolget)\n measuredmags = measuredmags[np.isfinite(measuredmags)]\n\n # if we're randomizing, get the mags from the interpolated mag-RMS\n # relation\n if (randomizemags and magrms and mcol in magrms and\n 'interpolated_magmad' in magrms[mcol] and\n magrms[mcol]['interpolated_magmad'] is not None):\n\n interpfunc = magrms[mcol]['interpolated_magmad']\n lcmad = interpfunc(fakelcdict['objectinfo']['sdssr'])\n\n fakelcdict['moments'][mcol] = {\n 'median': fakelcdict['objectinfo']['sdssr'],\n 'mad': lcmad\n }\n\n # if we're not randomizing, get the median and MAD from the light\n # curve itself\n else:\n\n # we require at least 10 finite measurements\n if measuredmags.size > 9:\n\n measuredmedian = np.median(measuredmags)\n measuredmad = np.median(\n np.abs(measuredmags - measuredmedian)\n )\n fakelcdict['moments'][mcol] = {'median':measuredmedian,\n 'mad':measuredmad}\n\n # if there aren't enough measurements in this LC, try to get the\n # median and RMS from the interpolated mag-RMS relation first\n else:\n\n if (magrms and mcol in magrms and\n 'interpolated_magmad' in magrms[mcol] and\n magrms[mcol]['interpolated_magmad'] is not None):\n\n LOGWARNING(\n 'input LC %s does not have enough '\n 'finite measurements, '\n 'generating mag moments from '\n 'fakelc sdssr and the mag-RMS relation' % lcfile\n )\n\n interpfunc = magrms[mcol]['interpolated_magmad']\n lcmad = interpfunc(fakelcdict['objectinfo']['sdssr'])\n\n fakelcdict['moments'][mcol] = {\n 'median': fakelcdict['objectinfo']['sdssr'],\n 'mad': lcmad\n }\n\n # if we don't have the mag-RMS relation either, then we\n # can't do anything for this light curve, generate a random\n # MAD between 5e-4 and 0.1\n else:\n\n LOGWARNING(\n 'input LC %s does not have enough '\n 'finite measurements and '\n 'no mag-RMS relation provided '\n 'assigning a random MAD between 5.0e-4 and 0.1'\n % lcfile\n )\n\n fakelcdict['moments'][mcol] = {\n 'median':fakelcdict['objectinfo']['sdssr'],\n 'mad':npr.random()*(0.1 - 5.0e-4) + 5.0e-4\n }\n\n # the magnitude column is set to all zeros initially. this will be\n # filled in by the add_fakelc_variability function below\n fakelcdict[mcol] = np.full_like(_dict_get(lcdict, mcolget), 0.0)\n fakelcdict['columns'].append(mcol)\n\n\n # get the err columns\n for mcol, ecol in zip(magcols, errcols):\n\n if '.' in ecol:\n ecolget = ecol.split('.')\n else:\n ecolget = [ecol]\n\n if ecol not in fakelcdict:\n\n measurederrs = _dict_get(lcdict, ecolget)\n measurederrs = measurederrs[np.isfinite(measurederrs)]\n\n # if we're randomizing, get the errs from the interpolated mag-RMS\n # relation\n if (randomizemags and magrms and mcol in magrms and\n 'interpolated_magmad' in magrms[mcol] and\n magrms[mcol]['interpolated_magmad'] is not None):\n\n interpfunc = magrms[mcol]['interpolated_magmad']\n lcmad = interpfunc(fakelcdict['objectinfo']['sdssr'])\n\n # the median of the errs = lcmad\n # the mad of the errs is 0.1 x lcmad\n fakelcdict['moments'][ecol] = {\n 'median': lcmad,\n 'mad': 0.1*lcmad\n }\n\n else:\n\n # we require at least 10 finite measurements\n # we'll calculate the median and MAD of the errs to use later on\n if measurederrs.size > 9:\n measuredmedian = np.median(measurederrs)\n measuredmad = np.median(\n np.abs(measurederrs - measuredmedian)\n )\n fakelcdict['moments'][ecol] = {'median':measuredmedian,\n 'mad':measuredmad}\n else:\n\n if (magrms and mcol in magrms and\n 'interpolated_magmad' in magrms[mcol] and\n magrms[mcol]['interpolated_magmad'] is not None):\n\n LOGWARNING(\n 'input LC %s does not have enough '\n 'finite measurements, '\n 'generating err moments from '\n 'the mag-RMS relation' % lcfile\n )\n\n interpfunc = magrms[mcol]['interpolated_magmad']\n lcmad = interpfunc(fakelcdict['objectinfo']['sdssr'])\n\n fakelcdict['moments'][ecol] = {\n 'median': lcmad,\n 'mad': 0.1*lcmad\n }\n\n # if we don't have the mag-RMS relation either, then we\n # can't do anything for this light curve, generate a random\n # MAD between 5e-4 and 0.1\n else:\n\n LOGWARNING(\n 'input LC %s does not have '\n 'enough finite measurements and '\n 'no mag-RMS relation provided, '\n 'generating errs randomly' % lcfile\n )\n fakelcdict['moments'][ecol] = {\n 'median':npr.random()*(0.01 - 5.0e-4) + 5.0e-4,\n 'mad':npr.random()*(0.01 - 5.0e-4) + 5.0e-4\n }\n\n # the errors column is set to all zeros initially. this will be\n # filled in by the add_fakelc_variability function below.\n fakelcdict[ecol] = np.full_like(_dict_get(lcdict, ecolget), 0.0)\n fakelcdict['columns'].append(ecol)\n\n\n\n # add the timecols, magcols, errcols to the lcdict\n fakelcdict['timecols'] = timecols\n fakelcdict['magcols'] = magcols\n fakelcdict['errcols'] = errcols\n\n # generate an output file name\n fakelcfname = '%s-fakelc.pkl' % fakelcdict['objectid']\n fakelcfpath = os.path.abspath(os.path.join(outdir, fakelcfname))\n\n # write this out to the output directory\n with open(fakelcfpath,'wb') as outfd:\n pickle.dump(fakelcdict, outfd, protocol=pickle.HIGHEST_PROTOCOL)\n\n # return the fakelc path, its columns, info, and moments so we can put them\n # into a collection DB later on\n LOGINFO('real LC %s -> fake LC %s OK' % (lcfile, fakelcfpath))\n\n return (fakelcfpath, fakelcdict['columns'],\n fakelcdict['objectinfo'], fakelcdict['moments'])", "response": "This function will preprocesses an input real LC and sets it up to be a fake LC and generates a fake LC."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef add_fakelc_variability(fakelcfile,\n vartype,\n override_paramdists=None,\n magsarefluxes=False,\n overwrite=False):\n '''This adds variability of the specified type to the fake LC.\n\n The procedure is (for each `magcol`):\n\n - read the fakelcfile, get the stored moments and vartype info\n\n - add the periodic variability specified in vartype and varparamdists. if\n `vartype == None`, then do nothing in this step. If `override_vartype` is\n not None, override stored vartype with specified vartype. If\n `override_varparamdists` provided, override with specified\n `varparamdists`. NOTE: the varparamdists must make sense for the vartype,\n otherwise, weird stuff will happen.\n\n - add the median mag level stored in `fakelcfile` to the time series\n\n - add Gaussian noise to the light curve as specified in `fakelcfile`\n\n - add a varinfo key and dict to the lcdict with `varperiod`, `varepoch`,\n `varparams`\n\n - write back to fake LC pickle\n\n - return the `varinfo` dict to the caller\n\n Parameters\n ----------\n\n fakelcfile : str\n The name of the fake LC file to process.\n\n vartype : str\n The type of variability to add to this fake LC file.\n\n override_paramdists : dict\n A parameter distribution dict as in the `generate_XX_lightcurve`\n functions above. If provided, will override the distribution stored in\n the input fake LC file itself.\n\n magsarefluxes : bool\n Sets if the variability amplitude is in fluxes and not magnitudes.\n\n overwite : bool\n This overwrites the input fake LC file with a new variable LC even if\n it's been processed before.\n\n Returns\n -------\n\n dict\n A dict of the following form is returned::\n\n {'objectid':lcdict['objectid'],\n 'lcfname':fakelcfile,\n 'actual_vartype':vartype,\n 'actual_varparams':lcdict['actual_varparams']}\n\n '''\n\n # read in the fakelcfile\n lcdict = _read_pklc(fakelcfile)\n\n # make sure to bail out if this light curve already has fake variability\n # added\n if ('actual_vartype' in lcdict and\n 'actual_varparams' in lcdict and\n not overwrite):\n LOGERROR('%s has existing variability type: %s '\n 'and params: %s and overwrite = False, '\n 'skipping this file...' %\n (fakelcfile, lcdict['actual_vartype'],\n repr(lcdict['actual_varparams'])))\n return None\n\n # get the times, mags, errs from this LC\n timecols, magcols, errcols = (lcdict['timecols'],\n lcdict['magcols'],\n lcdict['errcols'])\n\n\n # get the correct function to apply variability\n if vartype in VARTYPE_LCGEN_MAP:\n vargenfunc = VARTYPE_LCGEN_MAP[vartype]\n elif vartype is None:\n vargenfunc = None\n else:\n LOGERROR('unknown variability type: %s, choose from: %s' %\n (vartype, repr(list(VARTYPE_LCGEN_MAP.keys()))))\n return None\n\n\n # 1. generate the variability, including the overrides if provided we do\n # this outside the loop below to get the period, etc. distributions once\n # only per object. NOTE: in doing so, we're assuming that the difference\n # between magcols is just additive and the timebases for each magcol are the\n # same; this is not strictly correct\n if vargenfunc is not None:\n if (override_paramdists is not None and\n isinstance(override_paramdists,dict)):\n\n variablelc = vargenfunc(lcdict[timecols[0]],\n paramdists=override_paramdists,\n magsarefluxes=magsarefluxes)\n\n else:\n\n variablelc = vargenfunc(lcdict[timecols[0]],\n magsarefluxes=magsarefluxes)\n\n # for nonvariables, don't execute vargenfunc, but return a similar dict\n # so we can add the required noise to it\n else:\n variablelc = {'vartype':None,\n 'params':None,\n 'times':lcdict[timecols[0]],\n 'mags':np.full_like(lcdict[timecols[0]], 0.0),\n 'errs':np.full_like(lcdict[timecols[0]], 0.0)}\n\n\n # now iterate over the time, mag, err columns\n for tcol, mcol, ecol in zip(timecols, magcols, errcols):\n\n times, mags, errs = lcdict[tcol], lcdict[mcol], lcdict[ecol]\n\n # 2. get the moments for this magcol\n mag_median = lcdict['moments'][mcol]['median']\n mag_mad = lcdict['moments'][mcol]['mad']\n\n # add up to 5 mmag of extra RMS for systematics and red-noise\n mag_rms = mag_mad*1.483\n\n err_median = lcdict['moments'][ecol]['median']\n err_mad = lcdict['moments'][ecol]['mad']\n err_rms = err_mad*1.483\n\n # 3. add the median level + gaussian noise\n magnoise = npr.normal(size=variablelc['mags'].size)*mag_rms\n errnoise = npr.normal(size=variablelc['errs'].size)*err_rms\n\n finalmags = mag_median + (variablelc['mags'] + magnoise)\n finalerrs = err_median + (variablelc['errs'] + errnoise)\n\n # 4. update these tcol, mcol, ecol values in the lcdict\n lcdict[mcol] = finalmags\n lcdict[ecol] = finalerrs\n\n #\n # all done with updating mags and errs\n #\n\n # 5. update the light curve with the variability info\n lcdict['actual_vartype'] = variablelc['vartype']\n lcdict['actual_varparams'] = variablelc['params']\n\n # these standard keys are set to help out later with characterizing recovery\n # rates by magnitude, period, amplitude, ndet, etc.\n if vartype is not None:\n lcdict['actual_varperiod'] = variablelc['varperiod']\n lcdict['actual_varamplitude'] = variablelc['varamplitude']\n else:\n lcdict['actual_varperiod'] = np.nan\n lcdict['actual_varamplitude'] = np.nan\n\n\n # 6. write back, making sure to do it safely\n tempoutf = '%s.%s' % (fakelcfile, md5(npr.bytes(4)).hexdigest()[-8:])\n with open(tempoutf, 'wb') as outfd:\n pickle.dump(lcdict, outfd, pickle.HIGHEST_PROTOCOL)\n\n if os.path.exists(tempoutf):\n shutil.copy(tempoutf, fakelcfile)\n os.remove(tempoutf)\n else:\n LOGEXCEPTION('could not write output light curve file to dir: %s' %\n os.path.dirname(tempoutf))\n # fail here\n raise\n\n LOGINFO('object: %s, vartype: %s -> %s OK' % (\n lcdict['objectid'],\n vartype,\n fakelcfile)\n )\n\n return {'objectid':lcdict['objectid'],\n 'lcfname':fakelcfile,\n 'actual_vartype':vartype,\n 'actual_varparams':lcdict['actual_varparams']}", "response": "This function adds a variability to the fake LC file."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef add_variability_to_fakelc_collection(simbasedir,\n override_paramdists=None,\n overwrite_existingvar=False):\n '''This adds variability and noise to all fake LCs in `simbasedir`.\n\n If an object is marked as variable in the `fakelcs-info`.pkl file in\n `simbasedir`, a variable signal will be added to its light curve based on\n its selected type, default period and amplitude distribution, the\n appropriate params, etc. the epochs for each variable object will be chosen\n uniformly from its time-range (and may not necessarily fall on a actual\n observed time). Nonvariable objects will only have noise added as determined\n by their params, but no variable signal will be added.\n\n Parameters\n ----------\n\n simbasedir : str\n The directory containing the fake LCs to process.\n\n override_paramdists : dict\n This can be used to override the stored variable parameters in each fake\n LC. It should be a dict of the following form::\n\n {'': {': a scipy.stats distribution function or\n the np.random.randint function,\n .\n .\n .\n ': a scipy.stats distribution function\n or the np.random.randint function}\n\n for any vartype in VARTYPE_LCGEN_MAP. These are used to override the\n default parameter distributions for each variable type.\n\n overwrite_existingvar : bool\n If this is True, then will overwrite any existing variability in the\n input fake LCs in `simbasedir`.\n\n Returns\n -------\n\n dict\n This returns a dict containing the fake LC filenames as keys and\n variability info for each as values.\n\n '''\n\n # open the fakelcs-info.pkl\n infof = os.path.join(simbasedir,'fakelcs-info.pkl')\n with open(infof, 'rb') as infd:\n lcinfo = pickle.load(infd)\n\n\n lclist = lcinfo['lcfpath']\n varflag = lcinfo['isvariable']\n vartypes = lcinfo['vartype']\n\n vartind = 0\n\n varinfo = {}\n\n # go through all the LCs and add the required type of variability\n for lc, varf, _lcind in zip(lclist, varflag, range(len(lclist))):\n\n # if this object is variable, add variability\n if varf:\n\n thisvartype = vartypes[vartind]\n\n if (override_paramdists and\n isinstance(override_paramdists, dict) and\n thisvartype in override_paramdists and\n isinstance(override_paramdists[thisvartype], dict)):\n\n thisoverride_paramdists = override_paramdists[thisvartype]\n else:\n thisoverride_paramdists = None\n\n\n varlc = add_fakelc_variability(\n lc, thisvartype,\n override_paramdists=thisoverride_paramdists,\n overwrite=overwrite_existingvar\n )\n varinfo[varlc['objectid']] = {'params': varlc['actual_varparams'],\n 'vartype': varlc['actual_vartype']}\n\n # update vartind\n vartind = vartind + 1\n\n else:\n\n varlc = add_fakelc_variability(\n lc, None,\n overwrite=overwrite_existingvar\n )\n varinfo[varlc['objectid']] = {'params': varlc['actual_varparams'],\n 'vartype': varlc['actual_vartype']}\n\n\n #\n # done with all objects\n #\n\n # write the varinfo back to the dict and fakelcs-info.pkl\n lcinfo['varinfo'] = varinfo\n\n tempoutf = '%s.%s' % (infof, md5(npr.bytes(4)).hexdigest()[-8:])\n with open(tempoutf, 'wb') as outfd:\n pickle.dump(lcinfo, outfd, pickle.HIGHEST_PROTOCOL)\n\n if os.path.exists(tempoutf):\n shutil.copy(tempoutf, infof)\n os.remove(tempoutf)\n else:\n LOGEXCEPTION('could not write output light curve file to dir: %s' %\n os.path.dirname(tempoutf))\n # fail here\n raise\n\n return lcinfo", "response": "This function adds variability and noise to all fake LCs in the specified directory."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef add_flare_model(flareparams,\n times,\n mags,\n errs):\n '''This adds a flare model function to the input magnitude/flux time-series.\n\n Parameters\n ----------\n\n flareparams : list of float\n This defines the flare model::\n\n [amplitude,\n flare_peak_time,\n rise_gaussian_stdev,\n decay_time_constant]\n\n where:\n\n `amplitude`: the maximum flare amplitude in mags or flux. If flux, then\n amplitude should be positive. If mags, amplitude should be negative.\n\n `flare_peak_time`: time at which the flare maximum happens.\n\n `rise_gaussian_stdev`: the stdev of the gaussian describing the rise of\n the flare.\n\n `decay_time_constant`: the time constant of the exponential fall of the\n flare.\n\n times,mags,errs : np.array\n The input time-series of measurements and associated errors for which\n the model will be generated. The times will be used to generate\n model mags.\n\n magsarefluxes : bool\n Sets the correct direction of the flare amplitude (+ve) for fluxes if\n True and for mags (-ve) if False.\n\n Returns\n -------\n\n dict\n A dict of the form below is returned::\n\n {'times': the original times array\n 'mags': the original mags + the flare model mags evaluated at times,\n 'errs': the original errs array,\n 'flareparams': the input list of flare params}\n\n '''\n\n modelmags, ftimes, fmags, ferrs = flares.flare_model(\n flareparams,\n times,\n mags,\n errs\n )\n\n return {'times':times,\n 'mags':mags + modelmags,\n 'errs':errs,\n 'flareparams':flareparams}", "response": "This function adds a flare model function to the input time - series."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _smooth_acf(acf, windowfwhm=7, windowsize=21):\n '''This returns a smoothed version of the ACF.\n\n Convolves the ACF with a Gaussian of given `windowsize` and `windowfwhm`.\n\n Parameters\n ----------\n\n acf : np.array\n The auto-correlation function array to smooth.\n\n windowfwhm : int\n The smoothing window Gaussian kernel's FWHM .\n\n windowsize : int\n The number of input points to apply the smoothing over.\n\n Returns\n -------\n\n np.array\n Smoothed version of the input ACF array.\n\n '''\n\n convkernel = Gaussian1DKernel(windowfwhm, x_size=windowsize)\n smoothed = convolve(acf, convkernel, boundary='extend')\n\n return smoothed", "response": "This function returns a smoothed version of the ACF."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _smooth_acf_savgol(acf, windowsize=21, polyorder=2):\n '''\n This returns a smoothed version of the ACF.\n\n This version uses the Savitsky-Golay smoothing filter.\n\n Parameters\n ----------\n\n acf : np.array\n The auto-correlation function array to smooth.\n\n windowsize : int\n The number of input points to apply the smoothing over.\n\n polyorder : int\n The order of the polynomial to use in the Savitsky-Golay filter.\n\n Returns\n -------\n\n np.array\n Smoothed version of the input ACF array.\n\n '''\n\n smoothed = savgol_filter(acf, windowsize, polyorder)\n\n return smoothed", "response": "This function returns a smoothed version of the ACF array using the Savitsky - Golay smoothing filter."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _get_acf_peakheights(lags, acf, npeaks=20, searchinterval=1):\n '''This calculates the relative peak heights for first npeaks in ACF.\n\n Usually, the first peak or the second peak (if its peak height > first peak)\n corresponds to the correct lag. When we know the correct lag, the period is\n then::\n\n bestperiod = time[lags == bestlag] - time[0]\n\n Parameters\n ----------\n\n lags : np.array\n An array of lags that the ACF is calculated at.\n\n acf : np.array\n The array containing the ACF values.\n\n npeaks : int\n THe maximum number of peaks to consider when finding peak heights.\n\n searchinterval : int\n From `scipy.signal.argrelmax`: \"How many points on each side to use for\n the comparison to consider comparator(n, n+x) to be True.\" This\n effectively sets how many points on each of the current peak will be\n used to check if the current peak is the local maximum.\n\n Returns\n -------\n\n dict\n This returns a dict of the following form::\n\n {'maxinds':the indices of the lag array where maxes are,\n 'maxacfs':the ACF values at each max,\n 'maxlags':the lag values at each max,\n 'mininds':the indices of the lag array where mins are,\n 'minacfs':the ACF values at each min,\n 'minlags':the lag values at each min,\n 'relpeakheights':the relative peak heights of each rel. ACF peak,\n 'relpeaklags':the lags at each rel. ACF peak found,\n 'peakindices':the indices of arrays where each rel. ACF peak is,\n 'bestlag':the lag value with the largest rel. ACF peak height,\n 'bestpeakheight':the largest rel. ACF peak height,\n 'bestpeakindex':the largest rel. ACF peak's number in all peaks}\n\n '''\n\n maxinds = argrelmax(acf, order=searchinterval)[0]\n maxacfs = acf[maxinds]\n maxlags = lags[maxinds]\n mininds = argrelmin(acf, order=searchinterval)[0]\n minacfs = acf[mininds]\n minlags = lags[mininds]\n\n relpeakheights = npzeros(npeaks)\n relpeaklags = npzeros(npeaks,dtype=npint64)\n peakindices = npzeros(npeaks,dtype=npint64)\n\n for peakind, mxi in enumerate(maxinds[:npeaks]):\n\n # check if there are no mins to the left\n # throw away this peak because it's probably spurious\n # (FIXME: is this OK?)\n if npall(mxi < mininds):\n continue\n\n leftminind = mininds[mininds < mxi][-1] # the last index to the left\n rightminind = mininds[mininds > mxi][0] # the first index to the right\n relpeakheights[peakind] = (\n acf[mxi] - (acf[leftminind] + acf[rightminind])/2.0\n )\n relpeaklags[peakind] = lags[mxi]\n peakindices[peakind] = peakind\n\n # figure out the bestperiod if possible\n if relpeakheights[0] > relpeakheights[1]:\n bestlag = relpeaklags[0]\n bestpeakheight = relpeakheights[0]\n bestpeakindex = peakindices[0]\n else:\n bestlag = relpeaklags[1]\n bestpeakheight = relpeakheights[1]\n bestpeakindex = peakindices[1]\n\n return {'maxinds':maxinds,\n 'maxacfs':maxacfs,\n 'maxlags':maxlags,\n 'mininds':mininds,\n 'minacfs':minacfs,\n 'minlags':minlags,\n 'relpeakheights':relpeakheights,\n 'relpeaklags':relpeaklags,\n 'peakindices':peakindices,\n 'bestlag':bestlag,\n 'bestpeakheight':bestpeakheight,\n 'bestpeakindex':bestpeakindex}", "response": "This function calculates the relative peak heights for the first npeaks in an ACF."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nplot the unsmoothed and smoothed ACF vs. lag plot.", "response": "def plot_acf_results(acfp, outfile, maxlags=5000, yrange=(-0.4,0.4)):\n '''\n This plots the unsmoothed/smoothed ACF vs lag.\n\n Parameters\n ----------\n\n acfp : dict\n This is the dict returned from `macf_period_find` below.\n\n outfile : str\n The output file the plot will be written to.\n\n maxlags: int\n The maximum number of lags to include in the plot.\n\n yrange : sequence of two floats\n The y-range of the ACF vs. lag plot to use.\n\n '''\n\n import matplotlib.pyplot as plt\n\n lags = acfp['acfresults']['lags'][:maxlags]\n smoothedacf = acfp['acf'][:maxlags]\n unsmoothedacf = acfp['acfresults']['acf'][:maxlags]\n\n acfparams = acfp['kwargs']['smoothfunckwargs'].copy()\n acfparams.update({'peakinterval': int(acfp['kwargs']['smoothacf']/2.0)})\n\n # plot the ACFs\n fig, ax1 = plt.subplots()\n\n # this is lags vs acf\n ax1.plot(lags, unsmoothedacf, label='unsmoothed ACF',color='#1f77b4')\n ax1.plot(lags, smoothedacf, label='smoothed ACF', color='#ff7f0e')\n\n ax1.set_xlim((0,maxlags))\n\n ax1.set_xlabel('lags')\n\n # overplot the identified peaks\n acfmaxinds = acfp['acfpeaks']['maxinds']\n\n for i, maxind in enumerate(acfmaxinds):\n if i == 0:\n ax1.axvline(maxind,\n linewidth=2.0,\n color='red',\n ymin=0.2, ymax=0.3,\n label='identified ACF peaks')\n else:\n ax1.axvline(maxind,\n linewidth=2.0,\n color='red',\n ymin=0.2, ymax=0.3)\n\n plt.ylabel('ACF')\n plt.ylim(yrange)\n ax1.legend()\n plt.title('%s' % repr(acfparams))\n plt.tight_layout()\n plt.savefig(outfile)\n plt.close('all')\n\n return outfile"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _autocorr_func1(mags, lag, maglen, magmed, magstd):\n '''Calculates the autocorr of mag series for specific lag.\n\n This version of the function is taken from: Kim et al. (`2011\n `_)\n\n Parameters\n ----------\n\n mags : np.array\n This is the magnitudes array. MUST NOT have any nans.\n\n lag : float\n The specific lag value to calculate the auto-correlation for. This MUST\n be less than total number of observations in `mags`.\n\n maglen : int\n The number of elements in the `mags` array.\n\n magmed : float\n The median of the `mags` array.\n\n magstd : float\n The standard deviation of the `mags` array.\n\n Returns\n -------\n\n float\n The auto-correlation at this specific `lag` value.\n\n '''\n\n lagindex = nparange(1,maglen-lag)\n products = (mags[lagindex] - magmed) * (mags[lagindex+lag] - magmed)\n acorr = (1.0/((maglen - lag)*magstd)) * npsum(products)\n\n return acorr", "response": "Calculates the autocorr of mag series for specific lag."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _autocorr_func3(mags, lag, maglen, magmed, magstd):\n '''\n This is yet another alternative to calculate the autocorrelation.\n\n Taken from: `Bayesian Methods for Hackers by Cameron Pilon `_\n\n (This should be the fastest method to calculate ACFs.)\n\n Parameters\n ----------\n\n mags : np.array\n This is the magnitudes array. MUST NOT have any nans.\n\n lag : float\n The specific lag value to calculate the auto-correlation for. This MUST\n be less than total number of observations in `mags`.\n\n maglen : int\n The number of elements in the `mags` array.\n\n magmed : float\n The median of the `mags` array.\n\n magstd : float\n The standard deviation of the `mags` array.\n\n Returns\n -------\n\n float\n The auto-correlation at this specific `lag` value.\n\n '''\n\n # from http://tinyurl.com/afz57c4\n result = npcorrelate(mags, mags, mode='full')\n result = result / npmax(result)\n\n return result[int(result.size / 2):]", "response": "This function is a fastest alternative to calculate the autocorrelation for a given set of magnitudes."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef autocorr_magseries(times, mags, errs,\n maxlags=1000,\n func=_autocorr_func3,\n fillgaps=0.0,\n filterwindow=11,\n forcetimebin=None,\n sigclip=3.0,\n magsarefluxes=False,\n verbose=True):\n '''This calculates the ACF of a light curve.\n\n This will pre-process the light curve to fill in all the gaps and normalize\n everything to zero. If `fillgaps = 'noiselevel'`, fills the gaps with the\n noise level obtained via the procedure above. If `fillgaps = 'nan'`, fills\n the gaps with `np.nan`.\n\n Parameters\n ----------\n\n times,mags,errs : np.array\n The measurement time-series and associated errors.\n\n maxlags : int\n The maximum number of lags to calculate.\n\n func : Python function\n This is a function to calculate the lags.\n\n fillgaps : 'noiselevel' or float\n This sets what to use to fill in gaps in the time series. If this is\n 'noiselevel', will smooth the light curve using a point window size of\n `filterwindow` (this should be an odd integer), subtract the smoothed LC\n from the actual LC and estimate the RMS. This RMS will be used to fill\n in the gaps. Other useful values here are 0.0, and npnan.\n\n filterwindow : int\n The light curve's smoothing filter window size to use if\n `fillgaps='noiselevel`'.\n\n forcetimebin : None or float\n This is used to force a particular cadence in the light curve other than\n the automatically determined cadence. This effectively rebins the light\n curve to this cadence. This should be in the same time units as `times`.\n\n sigclip : float or int or sequence of two floats/ints or None\n If a single float or int, a symmetric sigma-clip will be performed using\n the number provided as the sigma-multiplier to cut out from the input\n time-series.\n\n If a list of two ints/floats is provided, the function will perform an\n 'asymmetric' sigma-clip. The first element in this list is the sigma\n value to use for fainter flux/mag values; the second element in this\n list is the sigma value to use for brighter flux/mag values. For\n example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma\n dimmings and greater than 3-sigma brightenings. Here the meaning of\n \"dimming\" and \"brightening\" is set by *physics* (not the magnitude\n system), which is why the `magsarefluxes` kwarg must be correctly set.\n\n If `sigclip` is None, no sigma-clipping will be performed, and the\n time-series (with non-finite elems removed) will be passed through to\n the output.\n\n magsarefluxes : bool\n If your input measurements in `mags` are actually fluxes instead of\n mags, set this is True.\n\n verbose : bool\n If True, will indicate progress and report errors.\n\n Returns\n -------\n\n dict\n A dict of the following form is returned::\n\n {'itimes': the interpolated time values after gap-filling,\n 'imags': the interpolated mag/flux values after gap-filling,\n 'ierrs': the interpolated mag/flux values after gap-filling,\n 'cadence': the cadence of the output mag/flux time-series,\n 'minitime': the minimum value of the interpolated times array,\n 'lags': the lags used to calculate the auto-correlation function,\n 'acf': the value of the ACF at each lag used}\n\n '''\n\n # get the gap-filled timeseries\n interpolated = fill_magseries_gaps(times, mags, errs,\n fillgaps=fillgaps,\n forcetimebin=forcetimebin,\n sigclip=sigclip,\n magsarefluxes=magsarefluxes,\n filterwindow=filterwindow,\n verbose=verbose)\n\n if not interpolated:\n print('failed to interpolate light curve to minimum cadence!')\n return None\n\n itimes, imags = interpolated['itimes'], interpolated['imags'],\n\n # calculate the lags up to maxlags\n if maxlags:\n lags = nparange(0, maxlags)\n else:\n lags = nparange(itimes.size)\n\n series_stdev = 1.483*npmedian(npabs(imags))\n\n if func != _autocorr_func3:\n\n # get the autocorrelation as a function of the lag of the mag series\n autocorr = nparray([func(imags, x, imags.size, 0.0, series_stdev)\n for x in lags])\n\n # this doesn't need a lags array\n else:\n\n autocorr = _autocorr_func3(imags, lags[0], imags.size,\n 0.0, series_stdev)\n # return only the maximum number of lags\n if maxlags is not None:\n autocorr = autocorr[:maxlags]\n\n interpolated.update({'minitime':itimes.min(),\n 'lags':lags,\n 'acf':autocorr})\n\n return interpolated", "response": "This function calculates the ACF of a light curve with the given time - series and associated errors."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef aovhm_theta(times, mags, errs, frequency,\n nharmonics, magvariance):\n '''This calculates the harmonic AoV theta statistic for a frequency.\n\n This is a mostly faithful translation of the inner loop in `aovper.f90`. See\n the following for details:\n\n - http://users.camk.edu.pl/alex/\n - Schwarzenberg-Czerny (`1996\n `_)\n\n Schwarzenberg-Czerny (1996) equation 11::\n\n theta_prefactor = (K - 2N - 1)/(2N)\n theta_top = sum(c_n*c_n) (from n=0 to n=2N)\n theta_bot = variance(timeseries) - sum(c_n*c_n) (from n=0 to n=2N)\n\n theta = theta_prefactor * (theta_top/theta_bot)\n\n N = number of harmonics (nharmonics)\n K = length of time series (times.size)\n\n Parameters\n ----------\n\n times,mags,errs : np.array\n The input time-series to calculate the test statistic for. These should\n all be of nans/infs and be normalized to zero.\n\n frequency : float\n The test frequency to calculate the statistic for.\n\n nharmonics : int\n The number of harmonics to calculate up to.The recommended range is 4 to\n 8.\n\n magvariance : float\n This is the (weighted by errors) variance of the magnitude time\n series. We provide it as a pre-calculated value here so we don't have to\n re-calculate it for every worker.\n\n Returns\n -------\n\n aov_harmonic_theta : float\n THe value of the harmonic AoV theta for the specified test `frequency`.\n\n '''\n\n period = 1.0/frequency\n\n ndet = times.size\n two_nharmonics = nharmonics + nharmonics\n\n # phase with test period\n phasedseries = phase_magseries_with_errs(\n times, mags, errs, period, times[0],\n sort=True, wrap=False\n )\n\n # get the phased quantities\n phase = phasedseries['phase']\n pmags = phasedseries['mags']\n perrs = phasedseries['errs']\n\n # this is sqrt(1.0/errs^2) -> the weights\n pweights = 1.0/perrs\n\n # multiply by 2.0*PI (for omega*time)\n phase = phase * 2.0 * pi_value\n\n # this is the z complex vector\n z = npcos(phase) + 1.0j*npsin(phase)\n\n # multiply phase with N\n phase = nharmonics * phase\n\n # this is the psi complex vector\n psi = pmags * pweights * (npcos(phase) + 1j*npsin(phase))\n\n # this is the initial value of z^n\n zn = 1.0 + 0.0j\n\n # this is the initial value of phi\n phi = pweights + 0.0j\n\n # initialize theta to zero\n theta_aov = 0.0\n\n # go through all the harmonics now up to 2N\n for _ in range(two_nharmonics):\n\n # this is \n phi_dot_phi = npsum(phi * phi.conjugate())\n\n # this is the alpha_n numerator\n alpha = npsum(pweights * z * phi)\n\n # this is . make sure to use npvdot and NOT npdot to get\n # complex conjugate of first vector as expected for complex vectors\n phi_dot_psi = npvdot(phi, psi)\n\n # make sure phi_dot_phi is not zero\n phi_dot_phi = npmax([phi_dot_phi, 10.0e-9])\n\n # this is the expression for alpha_n\n alpha = alpha / phi_dot_phi\n\n # update theta_aov for this harmonic\n theta_aov = (theta_aov +\n npabs(phi_dot_psi) * npabs(phi_dot_psi) / phi_dot_phi)\n\n # use the recurrence relation to find the next phi\n phi = phi * z - alpha * zn * phi.conjugate()\n\n # update z^n\n zn = zn * z\n\n\n # done with all harmonics, calculate the theta_aov for this freq\n # the max below makes sure that magvariance - theta_aov > zero\n theta_aov = ( (ndet - two_nharmonics - 1.0) * theta_aov /\n (two_nharmonics * npmax([magvariance - theta_aov,\n 1.0e-9])) )\n\n return theta_aov", "response": "This function calculates the harmonic AoV theta statistic for a specific test frequency."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef open_default(self):\n '''\n This opens the database connection using the default database parameters\n given in the ~/.astrobase/astrobase.conf file.\n\n '''\n\n if HAVECONF:\n self.open(DBDATA, DBUSER, DBPASS, DBHOST)\n else:\n LOGERROR(\"no default DB connection config found in lcdb.conf, \"\n \"this function won't work otherwise\")", "response": "This opens the default database connection using the default parameters in lcdb. conf."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nset the database connection to autocommit.", "response": "def autocommit(self):\n '''\n This sets the database connection to autocommit. Must be called before\n any cursors have been instantiated.\n\n '''\n\n if len(self.cursors.keys()) == 0:\n self.connection.autocommit = True\n else:\n raise AttributeError('database cursors are already active, '\n 'cannot switch to autocommit now')"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncreate a new DB cursor for the current DB connection using a randomly generated handle.", "response": "def newcursor(self, dictcursor=False):\n '''\n This creates a DB cursor for the current DB connection using a\n randomly generated handle. Returns a tuple with cursor and handle.\n\n Parameters\n ----------\n\n dictcursor : bool\n If True, returns a cursor where each returned row can be addressed\n as a dictionary by column name.\n\n Returns\n -------\n\n tuple\n The tuple is of the form (handle, psycopg2.Cursor instance).\n\n '''\n\n handle = hashlib.sha256(os.urandom(12)).hexdigest()\n\n if dictcursor:\n self.cursors[handle] = self.connection.cursor(\n cursor_factory=psycopg2.extras.DictCursor\n )\n else:\n self.cursors[handle] = self.connection.cursor()\n\n return (self.cursors[handle], handle)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef commit(self):\n '''\n This just calls the connection's commit method.\n\n '''\n if not self.connection.closed:\n self.connection.commit()\n else:\n raise AttributeError('postgres connection to %s is closed' %\n self.database)", "response": "Commits the current state of the current resource."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef rollback(self):\n '''\n This just calls the connection's commit method.\n\n '''\n if not self.connection.closed:\n self.connection.rollback()\n else:\n raise AttributeError('postgres connection to %s is closed' %\n self.database)", "response": "This just calls the connection s commit method."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nclosing the specified cursor and removes it from the self. cursors dictionary.", "response": "def close_cursor(self, handle):\n '''\n Closes the cursor specified and removes it from the `self.cursors`\n dictionary.\n\n '''\n\n if handle in self.cursors:\n self.cursors[handle].close()\n else:\n raise KeyError('cursor with handle %s was not found' % handle)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef tap_query(querystr,\n simbad_mirror='simbad',\n returnformat='csv',\n forcefetch=False,\n cachedir='~/.astrobase/simbad-cache',\n verbose=True,\n timeout=10.0,\n refresh=2.0,\n maxtimeout=90.0,\n maxtries=3,\n complete_query_later=False,\n jitter=5.0):\n '''This queries the SIMBAD TAP service using the ADQL query string provided.\n\n Parameters\n ----------\n\n querystr : str\n This is the ADQL query string. See:\n http://www.ivoa.net/documents/ADQL/2.0 for the specification.\n\n simbad_mirror : str\n This is the key used to select a SIMBAD mirror from the\n `SIMBAD_URLS` dict above. If set, the specified mirror will be used. If\n None, a random mirror chosen from that dict will be used.\n\n returnformat : {'csv','votable','json'}\n The returned file format to request from the GAIA catalog service.\n\n forcefetch : bool\n If this is True, the query will be retried even if cached results for\n it exist.\n\n cachedir : str\n This points to the directory where results will be downloaded.\n\n verbose : bool\n If True, will indicate progress and warn of any issues.\n\n timeout : float\n This sets the amount of time in seconds to wait for the service to\n respond to our initial request.\n\n refresh : float\n This sets the amount of time in seconds to wait before checking if the\n result file is available. If the results file isn't available after\n `refresh` seconds have elapsed, the function will wait for `refresh`\n seconds continuously, until `maxtimeout` is reached or the results file\n becomes available.\n\n maxtimeout : float\n The maximum amount of time in seconds to wait for a result to become\n available after submitting our query request.\n\n maxtries : int\n The maximum number of tries (across all mirrors tried) to make to either\n submit the request or download the results, before giving up.\n\n complete_query_later : bool\n If set to True, a submitted query that does not return a result before\n `maxtimeout` has passed will be cancelled but its input request\n parameters and the result URL provided by the service will be saved. If\n this function is then called later with these same input request\n parameters, it will check if the query finally finished and a result is\n available. If so, will download the results instead of submitting a new\n query. If it's not done yet, will start waiting for results again. To\n force launch a new query with the same request parameters, set the\n `forcefetch` kwarg to True.\n\n jitter : float\n This is used to control the scale of the random wait in seconds before\n starting the query. Useful in parallelized situations.\n\n Returns\n -------\n\n dict\n This returns a dict of the following form::\n\n {'params':dict of the input params used for the query,\n 'provenance':'cache' or 'new download',\n 'result':path to the file on disk with the downloaded data table}\n\n '''\n\n # get the default params\n inputparams = TAP_PARAMS.copy()\n\n # update them with our input params\n\n inputparams['QUERY'] = querystr[::]\n\n if returnformat in RETURN_FORMATS:\n inputparams['FORMAT'] = returnformat\n else:\n LOGWARNING('unknown result format: %s requested, using CSV' %\n returnformat)\n inputparams['FORMAT'] = 'csv'\n\n # see if the cachedir exists\n if '~' in cachedir:\n cachedir = os.path.expanduser(cachedir)\n if not os.path.exists(cachedir):\n os.makedirs(cachedir)\n\n # generate the cachefname and look for it\n xcachekey = '-'.join([repr(inputparams[x])\n for x in sorted(inputparams.keys())])\n cachekey = hashlib.sha256(xcachekey.encode()).hexdigest()\n cachefname = os.path.join(\n cachedir,\n '%s.%s' % (cachekey, RETURN_FORMATS[returnformat])\n )\n provenance = 'cache'\n\n incomplete_qpklf = os.path.join(\n cachedir,\n 'incomplete-query-%s' % cachekey\n )\n\n ##########################################\n ## COMPLETE A QUERY THAT MAY BE RUNNING ##\n ##########################################\n\n # first, check if this query can be resurrected\n if (not forcefetch and\n complete_query_later and\n os.path.exists(incomplete_qpklf)):\n\n with open(incomplete_qpklf, 'rb') as infd:\n incomplete_qinfo = pickle.load(infd)\n\n LOGWARNING('complete_query_later = True, and '\n 'this query was not completed on a '\n 'previous run, will check if it is done now...')\n\n # get the status URL and go into a loop to see if the query completed\n waitdone = False\n timeelapsed = 0.0\n\n simbad_mirror = incomplete_qinfo['simbad_mirror']\n status_url = incomplete_qinfo['status_url']\n phasekeyword = incomplete_qinfo['phase_keyword']\n resultkeyword = incomplete_qinfo['result_keyword']\n\n while not waitdone:\n\n if timeelapsed > maxtimeout:\n\n LOGERROR('SIMBAD TAP query still not done '\n 'after waiting %s seconds for results.\\n'\n 'status URL is: %s' %\n (maxtimeout,\n repr(inputparams),\n status_url))\n\n return None\n\n try:\n\n resreq = requests.get(status_url,\n timeout=timeout)\n\n resreq.raise_for_status()\n\n # parse the response XML and get the job status\n resxml = parseString(resreq.text)\n\n jobstatuselem = (\n resxml.getElementsByTagName(phasekeyword)[0]\n )\n jobstatus = jobstatuselem.firstChild.toxml()\n\n if jobstatus == 'COMPLETED':\n\n if verbose:\n\n LOGINFO('SIMBAD query completed, '\n 'retrieving results...')\n waitdone = True\n\n # if we're not done yet, then wait some more\n elif jobstatus != 'ERROR':\n\n if verbose:\n LOGINFO('elapsed time: %.1f, '\n 'current status: %s, '\n 'status URL: %s, waiting...'\n % (timeelapsed, jobstatus, status_url))\n\n time.sleep(refresh)\n timeelapsed = timeelapsed + refresh\n\n # if the JOB failed, then bail out immediately\n else:\n\n LOGERROR('SIMBAD TAP query failed due to a server error.\\n'\n 'status URL: %s\\n'\n 'status contents: %s' %\n (status_url,\n resreq.text))\n\n # since this job failed, remove the incomplete query pickle\n # so we can try this from scratch\n os.remove(incomplete_qpklf)\n\n return None\n\n except requests.exceptions.Timeout as e:\n\n LOGEXCEPTION(\n 'SIMBAD query timed out while waiting for status '\n 'download results.\\n'\n 'query: %s\\n'\n 'status URL: %s' %\n (repr(inputparams), status_url)\n )\n\n return None\n\n\n except Exception as e:\n\n LOGEXCEPTION(\n 'SIMBAD query failed while waiting for status\\n'\n 'query: %s\\n'\n 'status URL: %s\\n'\n 'status contents: %s' %\n (repr(inputparams),\n status_url,\n resreq.text)\n )\n\n # if the query fails completely, then either the status URL\n # doesn't exist any more or something else went wrong. we'll\n # remove the incomplete query pickle so we can try this from\n # scratch\n os.remove(incomplete_qpklf)\n\n return None\n\n #\n # at this point, we should be ready to get the query results\n #\n LOGINFO('query completed, retrieving results...')\n result_url_elem = resxml.getElementsByTagName(resultkeyword)[0]\n result_url = result_url_elem.getAttribute('xlink:href')\n result_nrows = result_url_elem.getAttribute('rows')\n\n try:\n\n resreq = requests.get(result_url, timeout=timeout)\n resreq.raise_for_status()\n\n if cachefname.endswith('.gz'):\n\n with gzip.open(cachefname,'wb') as outfd:\n for chunk in resreq.iter_content(chunk_size=65536):\n outfd.write(chunk)\n\n else:\n\n with open(cachefname,'wb') as outfd:\n for chunk in resreq.iter_content(chunk_size=65536):\n outfd.write(chunk)\n\n if verbose:\n LOGINFO('done. rows in result: %s' % result_nrows)\n tablefname = cachefname\n\n provenance = 'cache'\n\n # return a dict pointing to the result file\n # we'll parse this later\n resdict = {'params':inputparams,\n 'provenance':provenance,\n 'result':tablefname}\n\n # all went well, so we'll remove the incomplete query pickle\n os.remove(incomplete_qpklf)\n\n return resdict\n\n except requests.exceptions.Timeout as e:\n\n LOGEXCEPTION(\n 'SIMBAD query timed out while trying to '\n 'download results.\\n'\n 'query: %s\\n'\n 'result URL: %s' %\n (repr(inputparams), result_url)\n )\n return None\n\n except Exception as e:\n\n LOGEXCEPTION(\n 'SIMBAD query failed because of an error '\n 'while trying to download results.\\n'\n 'query: %s\\n'\n 'result URL: %s\\n'\n 'response status code: %s' %\n (repr(inputparams),\n result_url,\n resreq.status_code)\n )\n\n # if the result download fails, then either the result URL doesn't\n # exist any more or something else went wrong. we'll remove the\n # incomplete query pickle so we can try this from scratch\n os.remove(incomplete_qpklf)\n\n return None\n\n\n #####################\n ## RUN A NEW QUERY ##\n #####################\n\n # otherwise, we check the cache if it's done already, or run it again if not\n if forcefetch or (not os.path.exists(cachefname)):\n\n provenance = 'new download'\n time.sleep(random.randint(1,jitter))\n\n # generate a jobid here and update the input params\n jobid = 'ab-simbad-%i' % time.time()\n inputparams['JOBNAME'] = jobid\n inputparams['JOBDESCRIPTION'] = 'astrobase-simbad-tap-ADQL-query'\n\n try:\n\n waitdone = False\n timeelapsed = 0.0\n\n # set the simbad mirror to use\n if simbad_mirror is not None and simbad_mirror in SIMBAD_URLS:\n\n tapurl = SIMBAD_URLS[simbad_mirror]['url']\n resultkeyword = SIMBAD_URLS[simbad_mirror]['resultkeyword']\n phasekeyword = SIMBAD_URLS[simbad_mirror]['phasekeyword']\n randkey = simbad_mirror\n\n # sub in a table name if this is left unresolved in the input\n # query\n if '{table}' in querystr:\n inputparams['QUERY'] = (\n querystr.format(\n table=SIMBAD_URLS[simbad_mirror]['table']\n )\n )\n\n else:\n\n randkey = random.choice(list(SIMBAD_URLS.keys()))\n tapurl = SIMBAD_URLS[randkey]['url']\n resultkeyword = SIMBAD_URLS[randkey]['resultkeyword']\n phasekeyword = SIMBAD_URLS[randkey]['phasekeyword']\n\n # sub in a table name if this is left unresolved in the input\n # query\n if '{table}' in querystr:\n inputparams['QUERY'] = (\n querystr.format(\n table=SIMBAD_URLS[randkey]['table']\n )\n )\n\n if verbose:\n LOGINFO('using SIMBAD mirror TAP URL: %s' % tapurl)\n\n # send the query and get status\n if verbose:\n LOGINFO(\n 'submitting SIMBAD TAP query request for input params: %s'\n % repr(inputparams)\n )\n\n\n # here, we'll make sure the SIMBAD mirror works before doing\n # anything else\n mirrorok = False\n ntries = 1\n\n while (not mirrorok):\n\n if ntries > maxtries:\n\n LOGERROR('maximum number of allowed SIMBAD query '\n 'submission tries (%s) reached, bailing out...' %\n maxtries)\n return None\n\n try:\n\n req = requests.post(tapurl,\n data=inputparams,\n timeout=timeout)\n resp_status = req.status_code\n req.raise_for_status()\n\n mirrorok = True\n\n # this handles immediate 503s\n except requests.exceptions.HTTPError as e:\n\n LOGWARNING(\n 'SIMBAD TAP server: %s not responding, '\n 'trying another mirror...'\n % tapurl\n )\n mirrorok = False\n\n # for now, we have only one SIMBAD mirror to hit, so we'll\n # wait a random time between 1 and 5 seconds to hit it again\n remainingmirrors = list(SIMBAD_URLS.keys())\n waittime = random.choice(range(1,6))\n time.sleep(waittime)\n\n randkey = remainingmirrors[0]\n tapurl = SIMBAD_URLS[randkey]['url']\n resultkeyword = SIMBAD_URLS[randkey]['resultkeyword']\n phasekeyword = SIMBAD_URLS[randkey]['phasekeyword']\n if '{table}' in querystr:\n inputparams['QUERY'] = (\n querystr.format(\n table=SIMBAD_URLS[randkey]['table']\n )\n )\n\n # this handles initial query submission timeouts\n except requests.exceptions.Timeout as e:\n\n LOGWARNING(\n 'SIMBAD TAP query submission timed out, '\n 'mirror is probably down. Trying another mirror...'\n )\n mirrorok = False\n\n # for now, we have only one SIMBAD mirror to hit, so we'll\n # wait a random time between 1 and 5 seconds to hit it again\n remainingmirrors = list(SIMBAD_URLS.keys())\n waittime = random.choice(range(1,6))\n time.sleep(waittime)\n\n randkey = remainingmirrors[0]\n tapurl = SIMBAD_URLS[randkey]['url']\n resultkeyword = SIMBAD_URLS[randkey]['resultkeyword']\n phasekeyword = SIMBAD_URLS[randkey]['phasekeyword']\n if '{table}' in querystr:\n inputparams['QUERY'] = (\n querystr.format(\n table=SIMBAD_URLS[randkey]['table']\n )\n )\n\n # update the number of submission tries\n ntries = ntries + 1\n\n\n\n # NOTE: python-requests follows the \"303 See Other\" redirect\n # automatically, so we get the XML status doc immediately. We don't\n # need to look up the location of it in the initial response's\n # header as in the SIMBAD example.\n status_url = req.url\n\n # parse the response XML and get the job status\n resxml = parseString(req.text)\n\n\n jobstatuselem = resxml.getElementsByTagName(phasekeyword)\n\n if jobstatuselem:\n\n jobstatuselem = jobstatuselem[0]\n\n else:\n LOGERROR('could not parse job phase using '\n 'keyword %s in result XML' % phasekeyword)\n LOGERROR('%s' % req.txt)\n\n req.close()\n return None\n\n jobstatus = jobstatuselem.firstChild.toxml()\n\n # if the job completed already, jump down to retrieving results\n if jobstatus == 'COMPLETED':\n\n if verbose:\n\n LOGINFO('SIMBAD query completed, '\n 'retrieving results...')\n\n waitdone = True\n\n elif jobstatus == 'ERROR':\n\n if verbose:\n\n LOGERROR(\n 'SIMBAD query failed immediately '\n '(probably an ADQL error): %s, '\n 'status URL: %s, status contents: %s' %\n (repr(inputparams),\n status_url,\n req.text)\n )\n return None\n\n # we wait for the job to complete if it's not done already\n else:\n\n if verbose:\n LOGINFO(\n 'request submitted successfully, '\n 'current status is: %s. '\n 'waiting for results...' % jobstatus\n )\n\n while not waitdone:\n\n if timeelapsed > maxtimeout:\n\n LOGERROR('SIMBAD TAP query timed out '\n 'after waiting %s seconds for results.\\n'\n 'request was: %s\\n'\n 'status URL is: %s\\n'\n 'last status was: %s' %\n (maxtimeout,\n repr(inputparams),\n status_url,\n jobstatus))\n\n # here, we'll check if we're allowed to sleep on a query\n # for a bit and return to it later if the last status\n # was QUEUED or EXECUTING\n if complete_query_later and jobstatus in ('EXECUTING',\n 'QUEUED'):\n\n # write a pickle with the query params that we can\n # pick up later to finish this query\n incomplete_qpklf = os.path.join(\n cachedir,\n 'incomplete-query-%s' % cachekey\n )\n with open(incomplete_qpklf, 'wb') as outfd:\n\n savedict = inputparams.copy()\n\n savedict['status_url'] = status_url\n savedict['last_status'] = jobstatus\n savedict['simbad_mirror'] = simbad_mirror\n savedict['phase_keyword'] = phasekeyword\n savedict['result_keyword'] = resultkeyword\n\n pickle.dump(savedict,\n outfd,\n pickle.HIGHEST_PROTOCOL)\n\n LOGINFO('complete_query_later = True, '\n 'last state of query was: %s, '\n 'will resume later if this function '\n 'is called again with the same query' %\n jobstatus)\n\n return None\n\n time.sleep(refresh)\n timeelapsed = timeelapsed + refresh\n\n try:\n\n resreq = requests.get(status_url, timeout=timeout)\n resreq.raise_for_status()\n\n # parse the response XML and get the job status\n resxml = parseString(resreq.text)\n\n jobstatuselem = (\n resxml.getElementsByTagName(phasekeyword)[0]\n )\n jobstatus = jobstatuselem.firstChild.toxml()\n\n if jobstatus == 'COMPLETED':\n\n if verbose:\n\n LOGINFO('SIMBAD query completed, '\n 'retrieving results...')\n waitdone = True\n\n else:\n if verbose:\n LOGINFO('elapsed time: %.1f, '\n 'current status: %s, '\n 'status URL: %s, waiting...'\n % (timeelapsed, jobstatus, status_url))\n continue\n\n except requests.exceptions.Timeout as e:\n\n LOGEXCEPTION(\n 'SIMBAD query timed out while waiting for results '\n 'download results.\\n'\n 'query: %s\\n'\n 'status URL: %s' %\n (repr(inputparams), status_url)\n )\n return None\n\n\n except Exception as e:\n\n LOGEXCEPTION(\n 'SIMBAD query failed while waiting for results\\n'\n 'query: %s\\n'\n 'status URL: %s\\n'\n 'status contents: %s' %\n (repr(inputparams),\n status_url,\n resreq.text)\n )\n return None\n\n #\n # at this point, we should be ready to get the query results\n #\n result_url_elem = resxml.getElementsByTagName(resultkeyword)[0]\n result_url = result_url_elem.getAttribute('xlink:href')\n result_nrows = result_url_elem.getAttribute('rows')\n\n try:\n\n resreq = requests.get(result_url, timeout=timeout)\n resreq.raise_for_status()\n\n if cachefname.endswith('.gz'):\n\n with gzip.open(cachefname,'wb') as outfd:\n for chunk in resreq.iter_content(chunk_size=65536):\n outfd.write(chunk)\n\n else:\n\n with open(cachefname,'wb') as outfd:\n for chunk in resreq.iter_content(chunk_size=65536):\n outfd.write(chunk)\n\n if verbose:\n LOGINFO('done. rows in result: %s' % result_nrows)\n tablefname = cachefname\n\n except requests.exceptions.Timeout as e:\n\n LOGEXCEPTION(\n 'SIMBAD query timed out while trying to '\n 'download results.\\n'\n 'query: %s\\n'\n 'result URL: %s' %\n (repr(inputparams), result_url)\n )\n return None\n\n except Exception as e:\n\n LOGEXCEPTION(\n 'SIMBAD query failed because of an error '\n 'while trying to download results.\\n'\n 'query: %s\\n'\n 'result URL: %s\\n'\n 'response status code: %s' %\n (repr(inputparams),\n result_url,\n resreq.status_code)\n )\n return None\n\n except requests.exceptions.HTTPError as e:\n LOGEXCEPTION('SIMBAD TAP query failed.\\nrequest status was: '\n '%s.\\nquery was: %s' % (resp_status,\n repr(inputparams)))\n return None\n\n\n except requests.exceptions.Timeout as e:\n LOGERROR('SIMBAD TAP query submission timed out, '\n 'site is probably down. Request was: '\n '%s' % repr(inputparams))\n return None\n\n except Exception as e:\n LOGEXCEPTION('SIMBAD TAP query request failed for '\n '%s' % repr(inputparams))\n\n if 'resxml' in locals():\n LOGERROR('HTTP response from service:\\n%s' % req.text)\n\n return None\n\n ############################\n ## GET RESULTS FROM CACHE ##\n ############################\n\n else:\n\n if verbose:\n LOGINFO('getting cached SIMBAD query result for '\n 'request: %s' %\n (repr(inputparams)))\n\n tablefname = cachefname\n\n # try to open the cached file to make sure it's OK\n try:\n infd = gzip.open(cachefname,'rb')\n simbad_objectnames = np.genfromtxt(\n infd,\n names=True,\n delimiter=',',\n dtype='U20,f8,f8,U20,U20,U20,i8,U600,f8',\n usecols=(0,1,2,3,4,5,6,7,8),\n comments='?', # object names can have '#' in them\n )\n infd.close()\n\n except Exception as e:\n\n LOGEXCEPTION('could not read cached SIMBAD result file: %s, '\n 'fetching from server again' % cachefname)\n\n return tap_query(querystr,\n simbad_mirror=simbad_mirror,\n returnformat=returnformat,\n forcefetch=True,\n cachedir=cachedir,\n verbose=verbose,\n timeout=timeout,\n refresh=refresh,\n maxtimeout=maxtimeout)\n\n #\n # all done with retrieval, now return the result dict\n #\n\n # return a dict pointing to the result file\n # we'll parse this later\n resdict = {'params':inputparams,\n 'provenance':provenance,\n 'result':tablefname}\n\n return resdict", "response": "This function queries the SIMBAD TAP service using the provided ADQL query string."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef xmatch_cplist_external_catalogs(cplist,\n xmatchpkl,\n xmatchradiusarcsec=2.0,\n updateexisting=True,\n resultstodir=None):\n '''This xmatches external catalogs to a collection of checkplots.\n\n Parameters\n ----------\n\n cplist : list of str\n This is the list of checkplot pickle files to process.\n\n xmatchpkl : str\n The filename of a pickle prepared beforehand with the\n `checkplot.pkl_xmatch.load_xmatch_external_catalogs` function,\n containing collected external catalogs to cross-match the objects in the\n input `cplist` against.\n\n xmatchradiusarcsec : float\n The match radius to use for the cross-match in arcseconds.\n\n updateexisting : bool\n If this is True, will only update the `xmatch` dict in each checkplot\n pickle with any new cross-matches to the external catalogs. If False,\n will overwrite the `xmatch` dict with results from the current run.\n\n resultstodir : str or None\n If this is provided, then it must be a directory to write the resulting\n checkplots to after xmatch is done. This can be used to keep the\n original checkplots in pristine condition for some reason.\n\n Returns\n -------\n\n dict\n Returns a dict with keys = input checkplot pickle filenames and vals =\n xmatch status dict for each checkplot pickle.\n\n '''\n\n # load the external catalog\n with open(xmatchpkl,'rb') as infd:\n xmd = pickle.load(infd)\n\n # match each object. this is fairly fast, so this is not parallelized at the\n # moment\n\n status_dict = {}\n\n for cpf in cplist:\n\n cpd = _read_checkplot_picklefile(cpf)\n\n try:\n\n # match in place\n xmatch_external_catalogs(cpd, xmd,\n xmatchradiusarcsec=xmatchradiusarcsec,\n updatexmatch=updateexisting)\n\n for xmi in cpd['xmatch']:\n\n if cpd['xmatch'][xmi]['found']:\n LOGINFO('checkplot %s: %s matched to %s, '\n 'match dist: %s arcsec' %\n (os.path.basename(cpf),\n cpd['objectid'],\n cpd['xmatch'][xmi]['name'],\n cpd['xmatch'][xmi]['distarcsec']))\n\n if not resultstodir:\n outcpf = _write_checkplot_picklefile(cpd,\n outfile=cpf)\n else:\n xcpf = os.path.join(resultstodir, os.path.basename(cpf))\n outcpf = _write_checkplot_picklefile(cpd,\n outfile=xcpf)\n\n status_dict[cpf] = outcpf\n\n except Exception as e:\n\n LOGEXCEPTION('failed to match objects for %s' % cpf)\n status_dict[cpf] = None\n\n return status_dict", "response": "This function matches external catalogs to a collection of checkplots."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef xmatch_cpdir_external_catalogs(cpdir,\n xmatchpkl,\n cpfileglob='checkplot-*.pkl*',\n xmatchradiusarcsec=2.0,\n updateexisting=True,\n resultstodir=None):\n '''This xmatches external catalogs to all checkplots in a directory.\n\n Parameters\n -----------\n\n cpdir : str\n This is the directory to search in for checkplots.\n\n xmatchpkl : str\n The filename of a pickle prepared beforehand with the\n `checkplot.pkl_xmatch.load_xmatch_external_catalogs` function,\n containing collected external catalogs to cross-match the objects in the\n input `cplist` against.\n\n cpfileglob : str\n This is the UNIX fileglob to use in searching for checkplots.\n\n xmatchradiusarcsec : float\n The match radius to use for the cross-match in arcseconds.\n\n updateexisting : bool\n If this is True, will only update the `xmatch` dict in each checkplot\n pickle with any new cross-matches to the external catalogs. If False,\n will overwrite the `xmatch` dict with results from the current run.\n\n resultstodir : str or None\n If this is provided, then it must be a directory to write the resulting\n checkplots to after xmatch is done. This can be used to keep the\n original checkplots in pristine condition for some reason.\n\n Returns\n -------\n\n dict\n Returns a dict with keys = input checkplot pickle filenames and vals =\n xmatch status dict for each checkplot pickle.\n\n '''\n\n cplist = glob.glob(os.path.join(cpdir, cpfileglob))\n\n return xmatch_cplist_external_catalogs(\n cplist,\n xmatchpkl,\n xmatchradiusarcsec=xmatchradiusarcsec,\n updateexisting=updateexisting,\n resultstodir=resultstodir\n )", "response": "This function will load all external catalogs from a directory and return a dict containing the xmatch status of each external catalogs."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef colormagdiagram_cplist(cplist,\n outpkl,\n color_mag1=['gaiamag','sdssg'],\n color_mag2=['kmag','kmag'],\n yaxis_mag=['gaia_absmag','rpmj']):\n '''This makes color-mag diagrams for all checkplot pickles in the provided\n list.\n\n Can make an arbitrary number of CMDs given lists of x-axis colors and y-axis\n mags to use.\n\n Parameters\n ----------\n\n cplist : list of str\n This is the list of checkplot pickles to process.\n\n outpkl : str\n The filename of the output pickle that will contain the color-mag\n information for all objects in the checkplots specified in `cplist`.\n\n color_mag1 : list of str\n This a list of the keys in each checkplot's `objectinfo` dict that will\n be used as color_1 in the equation::\n\n x-axis color = color_mag1 - color_mag2\n\n color_mag2 : list of str\n This a list of the keys in each checkplot's `objectinfo` dict that will\n be used as color_2 in the equation::\n\n x-axis color = color_mag1 - color_mag2\n\n yaxis_mag : list of str\n This is a list of the keys in each checkplot's `objectinfo` dict that\n will be used as the (absolute) magnitude y-axis of the color-mag\n diagrams.\n\n Returns\n -------\n\n str\n The path to the generated CMD pickle file for the collection of objects\n in the input checkplot list.\n\n Notes\n -----\n\n This can make many CMDs in one go. For example, the default kwargs for\n `color_mag`, `color_mag2`, and `yaxis_mag` result in two CMDs generated and\n written to the output pickle file:\n\n - CMD1 -> gaiamag - kmag on the x-axis vs gaia_absmag on the y-axis\n - CMD2 -> sdssg - kmag on the x-axis vs rpmj (J reduced PM) on the y-axis\n\n '''\n\n # first, we'll collect all of the info\n cplist_objectids = []\n cplist_mags = []\n cplist_colors = []\n\n for cpf in cplist:\n\n cpd = _read_checkplot_picklefile(cpf)\n cplist_objectids.append(cpd['objectid'])\n\n thiscp_mags = []\n thiscp_colors = []\n\n for cm1, cm2, ym in zip(color_mag1, color_mag2, yaxis_mag):\n\n if (ym in cpd['objectinfo'] and\n cpd['objectinfo'][ym] is not None):\n thiscp_mags.append(cpd['objectinfo'][ym])\n else:\n thiscp_mags.append(np.nan)\n\n if (cm1 in cpd['objectinfo'] and\n cpd['objectinfo'][cm1] is not None and\n cm2 in cpd['objectinfo'] and\n cpd['objectinfo'][cm2] is not None):\n thiscp_colors.append(cpd['objectinfo'][cm1] -\n cpd['objectinfo'][cm2])\n else:\n thiscp_colors.append(np.nan)\n\n cplist_mags.append(thiscp_mags)\n cplist_colors.append(thiscp_colors)\n\n\n # convert these to arrays\n cplist_objectids = np.array(cplist_objectids)\n cplist_mags = np.array(cplist_mags)\n cplist_colors = np.array(cplist_colors)\n\n # prepare the outdict\n cmddict = {'objectids':cplist_objectids,\n 'mags':cplist_mags,\n 'colors':cplist_colors,\n 'color_mag1':color_mag1,\n 'color_mag2':color_mag2,\n 'yaxis_mag':yaxis_mag}\n\n # save the pickled figure and dict for fast retrieval later\n with open(outpkl,'wb') as outfd:\n pickle.dump(cmddict, outfd, pickle.HIGHEST_PROTOCOL)\n\n plt.close('all')\n\n return cmddict", "response": "This function will make a list of checkplot - specific colormag diagrams for all objects in a checkplot."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef add_cmd_to_checkplot(\n cpx,\n cmdpkl,\n require_cmd_magcolor=True,\n save_cmd_pngs=False\n):\n '''This adds CMD figures to a checkplot dict or pickle.\n\n Looks up the CMDs in `cmdpkl`, adds the object from `cpx` as a gold(-ish)\n star in the plot, and then saves the figure to a base64 encoded PNG, which\n can then be read and used by the `checkplotserver`.\n\n Parameters\n ----------\n\n cpx : str or dict\n This is the input checkplot pickle or dict to add the CMD to.\n\n cmdpkl : str or dict\n The CMD pickle generated by the `colormagdiagram_cplist` or\n `colormagdiagram_cpdir` functions above, or the dict produced by reading\n this pickle in.\n\n require_cmd_magcolor : bool\n If this is True, a CMD plot will not be made if the color and mag keys\n required by the CMD are not present or are nan in this checkplot's\n objectinfo dict.\n\n save_cmd_png : bool\n If this is True, then will save the CMD plots that were generated and\n added back to the checkplotdict as PNGs to the same directory as\n `cpx`. If `cpx` is a dict, will save them to the current working\n directory.\n\n Returns\n -------\n\n str or dict\n If `cpx` was a str filename of checkplot pickle, this will return that\n filename to indicate that the CMD was added to the file. If `cpx` was a\n checkplotdict, this will return the checkplotdict with a new key called\n 'colormagdiagram' containing the base64 encoded PNG binary streams of\n all CMDs generated.\n\n '''\n\n # get the checkplot\n if isinstance(cpx, str) and os.path.exists(cpx):\n cpdict = _read_checkplot_picklefile(cpx)\n elif isinstance(cpx, dict):\n cpdict = cpx\n else:\n LOGERROR('unknown type of checkplot provided as the cpx arg')\n return None\n\n # get the CMD\n if isinstance(cmdpkl, str) and os.path.exists(cmdpkl):\n with open(cmdpkl, 'rb') as infd:\n cmd = pickle.load(infd)\n elif isinstance(cmdpkl, dict):\n cmd = cmdpkl\n\n\n cpdict['colormagdiagram'] = {}\n\n # get the mags and colors from the CMD dict\n cplist_mags = cmd['mags']\n cplist_colors = cmd['colors']\n\n # now make the CMD plots for each color-mag combination in the CMD\n for c1, c2, ym, ind in zip(cmd['color_mag1'],\n cmd['color_mag2'],\n cmd['yaxis_mag'],\n range(len(cmd['color_mag1']))):\n\n # get these from the checkplot for this object\n if (c1 in cpdict['objectinfo'] and\n cpdict['objectinfo'][c1] is not None):\n c1mag = cpdict['objectinfo'][c1]\n else:\n c1mag = np.nan\n\n if (c2 in cpdict['objectinfo'] and\n cpdict['objectinfo'][c2] is not None):\n c2mag = cpdict['objectinfo'][c2]\n else:\n c2mag = np.nan\n\n if (ym in cpdict['objectinfo'] and\n cpdict['objectinfo'][ym] is not None):\n ymmag = cpdict['objectinfo'][ym]\n else:\n ymmag = np.nan\n\n if (require_cmd_magcolor and\n not (np.isfinite(c1mag) and\n np.isfinite(c2mag) and\n np.isfinite(ymmag))):\n\n LOGWARNING(\"required color: %s-%s or mag: %s are not \"\n \"in this checkplot's objectinfo dict \"\n \"(objectid: %s), skipping CMD...\" %\n (c1, c2, ym, cpdict['objectid']))\n continue\n\n # make the CMD for this color-mag combination\n try:\n\n thiscmd_title = r'%s-%s/%s' % (CMD_LABELS[c1],\n CMD_LABELS[c2],\n CMD_LABELS[ym])\n\n # make the scatter plot\n fig = plt.figure(figsize=(10,8))\n plt.plot(cplist_colors[:,ind],\n cplist_mags[:,ind],\n rasterized=True,\n marker='o',\n linestyle='none',\n mew=0,\n ms=3)\n\n # put this object on the plot\n plt.plot([c1mag - c2mag], [ymmag],\n ms=20,\n color='#b0ff05',\n marker='*',\n mew=0)\n\n plt.xlabel(r'$%s - %s$' % (CMD_LABELS[c1], CMD_LABELS[c2]))\n plt.ylabel(r'$%s$' % CMD_LABELS[ym])\n plt.title('%s - $%s$ CMD' % (cpdict['objectid'], thiscmd_title))\n plt.gca().invert_yaxis()\n\n # now save the figure to StrIO and put it back in the checkplot\n cmdpng = StrIO()\n plt.savefig(cmdpng, bbox_inches='tight',\n pad_inches=0.0, format='png')\n cmdpng.seek(0)\n cmdb64 = base64.b64encode(cmdpng.read())\n cmdpng.close()\n\n plt.close('all')\n plt.gcf().clear()\n\n cpdict['colormagdiagram']['%s-%s/%s' % (c1,c2,ym)] = cmdb64\n\n # if we're supposed to export to PNG, do so\n if save_cmd_pngs:\n\n if isinstance(cpx, str):\n outpng = os.path.join(os.path.dirname(cpx),\n 'cmd-%s-%s-%s.%s.png' %\n (cpdict['objectid'],\n c1,c2,ym))\n else:\n outpng = 'cmd-%s-%s-%s.%s.png' % (cpdict['objectid'],\n c1,c2,ym)\n\n _base64_to_file(cmdb64, outpng)\n\n except Exception as e:\n LOGEXCEPTION('CMD for %s-%s/%s does not exist in %s, skipping...' %\n (c1, c2, ym, cmdpkl))\n continue\n\n\n #\n # end of making CMDs\n #\n\n if isinstance(cpx, str):\n cpf = _write_checkplot_picklefile(cpdict, outfile=cpx, protocol=4)\n return cpf\n elif isinstance(cpx, dict):\n return cpdict", "response": "This function adds a CMD to a checkplot dict or pickle."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef add_cmds_cplist(cplist, cmdpkl,\n require_cmd_magcolor=True,\n save_cmd_pngs=False):\n '''This adds CMDs for each object in cplist.\n\n Parameters\n ----------\n\n cplist : list of str\n This is the input list of checkplot pickles to add the CMDs to.\n\n cmdpkl : str\n This is the filename of the CMD pickle created previously.\n\n require_cmd_magcolor : bool\n If this is True, a CMD plot will not be made if the color and mag keys\n required by the CMD are not present or are nan in each checkplot's\n objectinfo dict.\n\n save_cmd_pngs : bool\n If this is True, then will save the CMD plots that were generated and\n added back to the checkplotdict as PNGs to the same directory as\n `cpx`.\n\n Returns\n -------\n\n Nothing.\n\n\n '''\n\n # load the CMD first to save on IO\n with open(cmdpkl,'rb') as infd:\n cmd = pickle.load(infd)\n\n for cpf in cplist:\n\n add_cmd_to_checkplot(cpf, cmd,\n require_cmd_magcolor=require_cmd_magcolor,\n save_cmd_pngs=save_cmd_pngs)", "response": "This function adds the CMDs for each object in cplist to the checkplotdict."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef add_cmds_cpdir(cpdir,\n cmdpkl,\n cpfileglob='checkplot*.pkl*',\n require_cmd_magcolor=True,\n save_cmd_pngs=False):\n '''This adds CMDs for each object in cpdir.\n\n Parameters\n ----------\n\n cpdir : list of str\n This is the directory to search for checkplot pickles.\n\n cmdpkl : str\n This is the filename of the CMD pickle created previously.\n\n cpfileglob : str\n The UNIX fileglob to use when searching for checkplot pickles to operate\n on.\n\n require_cmd_magcolor : bool\n If this is True, a CMD plot will not be made if the color and mag keys\n required by the CMD are not present or are nan in each checkplot's\n objectinfo dict.\n\n save_cmd_pngs : bool\n If this is True, then will save the CMD plots that were generated and\n added back to the checkplotdict as PNGs to the same directory as\n `cpx`.\n\n Returns\n -------\n\n Nothing.\n\n '''\n\n cplist = glob.glob(os.path.join(cpdir, cpfileglob))\n\n return add_cmds_cplist(cplist,\n cmdpkl,\n require_cmd_magcolor=require_cmd_magcolor,\n save_cmd_pngs=save_cmd_pngs)", "response": "This function adds CMDs for each object in cpdir."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef parallel_update_objectinfo_cplist(\n cplist,\n liststartindex=None,\n maxobjects=None,\n nworkers=NCPUS,\n fast_mode=False,\n findercmap='gray_r',\n finderconvolve=None,\n deredden_object=True,\n custom_bandpasses=None,\n gaia_submit_timeout=10.0,\n gaia_submit_tries=3,\n gaia_max_timeout=180.0,\n gaia_mirror=None,\n complete_query_later=True,\n lclistpkl=None,\n nbrradiusarcsec=60.0,\n maxnumneighbors=5,\n plotdpi=100,\n findercachedir='~/.astrobase/stamp-cache',\n verbose=True\n):\n '''\n This updates objectinfo for a list of checkplots.\n\n Useful in cases where a previous round of GAIA/finderchart/external catalog\n acquisition failed. This will preserve the following keys in the checkplots\n if they exist:\n\n comments\n varinfo\n objectinfo.objecttags\n\n Parameters\n ----------\n\n cplist : list of str\n A list of checkplot pickle file names to update.\n\n liststartindex : int\n The index of the input list to start working at.\n\n maxobjects : int\n The maximum number of objects to process in this run. Use this with\n `liststartindex` to effectively distribute working on a large list of\n input checkplot pickles over several sessions or machines.\n\n nworkers : int\n The number of parallel workers that will work on the checkplot\n update process.\n\n fast_mode : bool or float\n This runs the external catalog operations in a \"fast\" mode, with short\n timeouts and not trying to hit external catalogs that take a long time\n to respond. See the docstring for\n `checkplot.pkl_utils._pkl_finder_objectinfo` for details on how this\n works. If this is True, will run in \"fast\" mode with default timeouts (5\n seconds in most cases). If this is a float, will run in \"fast\" mode with\n the provided timeout value in seconds.\n\n findercmap : str or matplotlib.cm.Colormap object\n\n findercmap : str or matplotlib.cm.ColorMap object\n The Colormap object to use for the finder chart image.\n\n finderconvolve : astropy.convolution.Kernel object or None\n If not None, the Kernel object to use for convolving the finder image.\n\n deredden_objects : bool\n If this is True, will use the 2MASS DUST service to get extinction\n coefficients in various bands, and then try to deredden the magnitudes\n and colors of the object already present in the checkplot's objectinfo\n dict.\n\n custom_bandpasses : dict\n This is a dict used to provide custom bandpass definitions for any\n magnitude measurements in the objectinfo dict that are not automatically\n recognized by the `varclass.starfeatures.color_features` function. See\n its docstring for details on the required format.\n\n gaia_submit_timeout : float\n Sets the timeout in seconds to use when submitting a request to look up\n the object's information to the GAIA service. Note that if `fast_mode`\n is set, this is ignored.\n\n gaia_submit_tries : int\n Sets the maximum number of times the GAIA services will be contacted to\n obtain this object's information. If `fast_mode` is set, this is\n ignored, and the services will be contacted only once (meaning that a\n failure to respond will be silently ignored and no GAIA data will be\n added to the checkplot's objectinfo dict).\n\n gaia_max_timeout : float\n Sets the timeout in seconds to use when waiting for the GAIA service to\n respond to our request for the object's information. Note that if\n `fast_mode` is set, this is ignored.\n\n gaia_mirror : str\n This sets the GAIA mirror to use. This is a key in the\n `services.gaia.GAIA_URLS` dict which defines the URLs to hit for each\n mirror.\n\n complete_query_later : bool\n If this is True, saves the state of GAIA queries that are not yet\n complete when `gaia_max_timeout` is reached while waiting for the GAIA\n service to respond to our request. A later call for GAIA info on the\n same object will attempt to pick up the results from the existing query\n if it's completed. If `fast_mode` is True, this is ignored.\n\n lclistpkl : dict or str\n If this is provided, must be a dict resulting from reading a catalog\n produced by the `lcproc.catalogs.make_lclist` function or a str path\n pointing to the pickle file produced by that function. This catalog is\n used to find neighbors of the current object in the current light curve\n collection. Looking at neighbors of the object within the radius\n specified by `nbrradiusarcsec` is useful for light curves produced by\n instruments that have a large pixel scale, so are susceptible to\n blending of variability and potential confusion of neighbor variability\n with that of the actual object being looked at. If this is None, no\n neighbor lookups will be performed.\n\n nbrradiusarcsec : float\n The radius in arcseconds to use for a search conducted around the\n coordinates of this object to look for any potential confusion and\n blending of variability amplitude caused by their proximity.\n\n maxnumneighbors : int\n The maximum number of neighbors that will have their light curves and\n magnitudes noted in this checkplot as potential blends with the target\n object.\n\n plotdpi : int\n The resolution in DPI of the plots to generate in this function\n (e.g. the finder chart, etc.)\n\n findercachedir : str\n The path to the astrobase cache directory for finder chart downloads\n from the NASA SkyView service.\n\n verbose : bool\n If True, will indicate progress and warn about potential problems.\n\n Returns\n -------\n\n list of str\n Paths to the updated checkplot pickle file.\n\n '''\n\n # work around the Darwin segfault after fork if no network activity in\n # main thread bug: https://bugs.python.org/issue30385#msg293958\n if sys.platform == 'darwin':\n import requests\n requests.get('http://captive.apple.com/hotspot-detect.html')\n\n # handle the start and end indices\n if (liststartindex is not None) and (maxobjects is None):\n cplist = cplist[liststartindex:]\n\n elif (liststartindex is None) and (maxobjects is not None):\n cplist = cplist[:maxobjects]\n\n elif (liststartindex is not None) and (maxobjects is not None):\n cplist = (\n cplist[liststartindex:liststartindex+maxobjects]\n )\n\n tasks = [(x, {'fast_mode':fast_mode,\n 'findercmap':findercmap,\n 'finderconvolve':finderconvolve,\n 'deredden_object':deredden_object,\n 'custom_bandpasses':custom_bandpasses,\n 'gaia_submit_timeout':gaia_submit_timeout,\n 'gaia_submit_tries':gaia_submit_tries,\n 'gaia_max_timeout':gaia_max_timeout,\n 'gaia_mirror':gaia_mirror,\n 'complete_query_later':complete_query_later,\n 'lclistpkl':lclistpkl,\n 'nbrradiusarcsec':nbrradiusarcsec,\n 'maxnumneighbors':maxnumneighbors,\n 'plotdpi':plotdpi,\n 'findercachedir':findercachedir,\n 'verbose':verbose}) for x in cplist]\n\n resultfutures = []\n results = []\n\n with ProcessPoolExecutor(max_workers=nworkers) as executor:\n resultfutures = executor.map(cp_objectinfo_worker, tasks)\n\n results = [x for x in resultfutures]\n\n executor.shutdown()\n return results", "response": "This function updates objectinfo for a list of checkplot pickle files."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef parallel_update_objectinfo_cpdir(cpdir,\n cpglob='checkplot-*.pkl*',\n liststartindex=None,\n maxobjects=None,\n nworkers=NCPUS,\n fast_mode=False,\n findercmap='gray_r',\n finderconvolve=None,\n deredden_object=True,\n custom_bandpasses=None,\n gaia_submit_timeout=10.0,\n gaia_submit_tries=3,\n gaia_max_timeout=180.0,\n gaia_mirror=None,\n complete_query_later=True,\n lclistpkl=None,\n nbrradiusarcsec=60.0,\n maxnumneighbors=5,\n plotdpi=100,\n findercachedir='~/.astrobase/stamp-cache',\n verbose=True):\n '''This updates the objectinfo for a directory of checkplot pickles.\n\n Useful in cases where a previous round of GAIA/finderchart/external catalog\n acquisition failed. This will preserve the following keys in the checkplots\n if they exist:\n\n comments\n varinfo\n objectinfo.objecttags\n\n Parameters\n ----------\n\n cpdir : str\n The directory to look for checkplot pickles in.\n\n cpglob : str\n The UNIX fileglob to use when searching for checkplot pickle files.\n\n liststartindex : int\n The index of the input list to start working at.\n\n maxobjects : int\n The maximum number of objects to process in this run. Use this with\n `liststartindex` to effectively distribute working on a large list of\n input checkplot pickles over several sessions or machines.\n\n nworkers : int\n The number of parallel workers that will work on the checkplot\n update process.\n\n fast_mode : bool or float\n This runs the external catalog operations in a \"fast\" mode, with short\n timeouts and not trying to hit external catalogs that take a long time\n to respond. See the docstring for\n `checkplot.pkl_utils._pkl_finder_objectinfo` for details on how this\n works. If this is True, will run in \"fast\" mode with default timeouts (5\n seconds in most cases). If this is a float, will run in \"fast\" mode with\n the provided timeout value in seconds.\n\n findercmap : str or matplotlib.cm.Colormap object\n\n findercmap : str or matplotlib.cm.ColorMap object\n The Colormap object to use for the finder chart image.\n\n finderconvolve : astropy.convolution.Kernel object or None\n If not None, the Kernel object to use for convolving the finder image.\n\n deredden_objects : bool\n If this is True, will use the 2MASS DUST service to get extinction\n coefficients in various bands, and then try to deredden the magnitudes\n and colors of the object already present in the checkplot's objectinfo\n dict.\n\n custom_bandpasses : dict\n This is a dict used to provide custom bandpass definitions for any\n magnitude measurements in the objectinfo dict that are not automatically\n recognized by the `varclass.starfeatures.color_features` function. See\n its docstring for details on the required format.\n\n gaia_submit_timeout : float\n Sets the timeout in seconds to use when submitting a request to look up\n the object's information to the GAIA service. Note that if `fast_mode`\n is set, this is ignored.\n\n gaia_submit_tries : int\n Sets the maximum number of times the GAIA services will be contacted to\n obtain this object's information. If `fast_mode` is set, this is\n ignored, and the services will be contacted only once (meaning that a\n failure to respond will be silently ignored and no GAIA data will be\n added to the checkplot's objectinfo dict).\n\n gaia_max_timeout : float\n Sets the timeout in seconds to use when waiting for the GAIA service to\n respond to our request for the object's information. Note that if\n `fast_mode` is set, this is ignored.\n\n gaia_mirror : str\n This sets the GAIA mirror to use. This is a key in the\n `services.gaia.GAIA_URLS` dict which defines the URLs to hit for each\n mirror.\n\n complete_query_later : bool\n If this is True, saves the state of GAIA queries that are not yet\n complete when `gaia_max_timeout` is reached while waiting for the GAIA\n service to respond to our request. A later call for GAIA info on the\n same object will attempt to pick up the results from the existing query\n if it's completed. If `fast_mode` is True, this is ignored.\n\n lclistpkl : dict or str\n If this is provided, must be a dict resulting from reading a catalog\n produced by the `lcproc.catalogs.make_lclist` function or a str path\n pointing to the pickle file produced by that function. This catalog is\n used to find neighbors of the current object in the current light curve\n collection. Looking at neighbors of the object within the radius\n specified by `nbrradiusarcsec` is useful for light curves produced by\n instruments that have a large pixel scale, so are susceptible to\n blending of variability and potential confusion of neighbor variability\n with that of the actual object being looked at. If this is None, no\n neighbor lookups will be performed.\n\n nbrradiusarcsec : float\n The radius in arcseconds to use for a search conducted around the\n coordinates of this object to look for any potential confusion and\n blending of variability amplitude caused by their proximity.\n\n maxnumneighbors : int\n The maximum number of neighbors that will have their light curves and\n magnitudes noted in this checkplot as potential blends with the target\n object.\n\n plotdpi : int\n The resolution in DPI of the plots to generate in this function\n (e.g. the finder chart, etc.)\n\n findercachedir : str\n The path to the astrobase cache directory for finder chart downloads\n from the NASA SkyView service.\n\n verbose : bool\n If True, will indicate progress and warn about potential problems.\n\n Returns\n -------\n\n list of str\n Paths to the updated checkplot pickle file.\n\n '''\n\n cplist = sorted(glob.glob(os.path.join(cpdir, cpglob)))\n\n return parallel_update_objectinfo_cplist(\n cplist,\n liststartindex=liststartindex,\n maxobjects=maxobjects,\n nworkers=nworkers,\n fast_mode=fast_mode,\n findercmap=findercmap,\n finderconvolve=finderconvolve,\n deredden_object=deredden_object,\n custom_bandpasses=custom_bandpasses,\n gaia_submit_timeout=gaia_submit_timeout,\n gaia_submit_tries=gaia_submit_tries,\n gaia_max_timeout=gaia_max_timeout,\n gaia_mirror=gaia_mirror,\n complete_query_later=complete_query_later,\n lclistpkl=lclistpkl,\n nbrradiusarcsec=nbrradiusarcsec,\n maxnumneighbors=maxnumneighbors,\n plotdpi=plotdpi,\n findercachedir=findercachedir,\n verbose=verbose\n )", "response": "This function updates the objectinfo for a directory of checkplot pickles."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _gaussian(x, amp, loc, std):\n '''This is a simple gaussian.\n\n Parameters\n ----------\n\n x : np.array\n The items at which the Gaussian is evaluated.\n\n amp : float\n The amplitude of the Gaussian.\n\n loc : float\n The central value of the Gaussian.\n\n std : float\n The standard deviation of the Gaussian.\n\n Returns\n -------\n\n np.array\n Returns the Gaussian evaluated at the items in `x`, using the provided\n parameters of `amp`, `loc`, and `std`.\n\n '''\n\n return amp * np.exp(-((x - loc)*(x - loc))/(2.0*std*std))", "response": "This function is a simple gaussian. It is a simple gaussian."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef invgauss_eclipses_func(ebparams, times, mags, errs):\n '''This returns a double eclipse shaped function.\n\n Suitable for first order modeling of eclipsing binaries.\n\n Parameters\n ----------\n\n ebparams : list of float\n This contains the parameters for the eclipsing binary::\n\n ebparams = [period (time),\n epoch (time),\n pdepth: primary eclipse depth (mags),\n pduration: primary eclipse duration (phase),\n psdepthratio: primary-secondary eclipse depth ratio,\n secondaryphase: center phase of the secondary eclipse]\n\n `period` is the period in days.\n\n `epoch` is the time of minimum in JD.\n\n `pdepth` is the depth of the primary eclipse.\n\n - for magnitudes -> pdepth should be < 0\n - for fluxes -> pdepth should be > 0\n\n `pduration` is the length of the primary eclipse in phase.\n\n `psdepthratio` is the ratio in the eclipse depths:\n `depth_secondary/depth_primary`. This is generally the same as the ratio\n of the `T_effs` of the two stars.\n\n `secondaryphase` is the phase at which the minimum of the secondary\n eclipse is located. This effectively parameterizes eccentricity.\n\n All of these will then have fitted values after the fit is done.\n\n times,mags,errs : np.array\n The input time-series of measurements and associated errors for which\n the eclipse model will be generated. The times will be used to generate\n model mags, and the input `times`, `mags`, and `errs` will be resorted\n by model phase and returned.\n\n Returns\n -------\n\n (modelmags, phase, ptimes, pmags, perrs) : tuple\n Returns the model mags and phase values. Also returns the input `times`,\n `mags`, and `errs` sorted by the model's phase.\n\n '''\n\n (period, epoch, pdepth, pduration, depthratio, secondaryphase) = ebparams\n\n # generate the phases\n iphase = (times - epoch)/period\n iphase = iphase - np.floor(iphase)\n\n phasesortind = np.argsort(iphase)\n phase = iphase[phasesortind]\n ptimes = times[phasesortind]\n pmags = mags[phasesortind]\n perrs = errs[phasesortind]\n\n zerolevel = np.median(pmags)\n modelmags = np.full_like(phase, zerolevel)\n\n primaryecl_amp = -pdepth\n secondaryecl_amp = -pdepth * depthratio\n\n primaryecl_std = pduration/5.0 # we use 5-sigma as full-width -> duration\n secondaryecl_std = pduration/5.0 # secondary eclipse has the same duration\n\n halfduration = pduration/2.0\n\n\n # phase indices\n primary_eclipse_ingress = (\n (phase >= (1.0 - halfduration)) & (phase <= 1.0)\n )\n primary_eclipse_egress = (\n (phase >= 0.0) & (phase <= halfduration)\n )\n\n secondary_eclipse_phase = (\n (phase >= (secondaryphase - halfduration)) &\n (phase <= (secondaryphase + halfduration))\n )\n\n # put in the eclipses\n modelmags[primary_eclipse_ingress] = (\n zerolevel + _gaussian(phase[primary_eclipse_ingress],\n primaryecl_amp,\n 1.0,\n primaryecl_std)\n )\n modelmags[primary_eclipse_egress] = (\n zerolevel + _gaussian(phase[primary_eclipse_egress],\n primaryecl_amp,\n 0.0,\n primaryecl_std)\n )\n modelmags[secondary_eclipse_phase] = (\n zerolevel + _gaussian(phase[secondary_eclipse_phase],\n secondaryecl_amp,\n secondaryphase,\n secondaryecl_std)\n )\n\n return modelmags, phase, ptimes, pmags, perrs", "response": "This function returns a double eclipse shaped function that can be used to generate the model mags and phase values for the eclipsing binaries."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef invgauss_eclipses_residual(ebparams, times, mags, errs):\n '''This returns the residual between the modelmags and the actual mags.\n\n Parameters\n ----------\n\n ebparams : list of float\n This contains the parameters for the eclipsing binary::\n\n ebparams = [period (time),\n epoch (time),\n pdepth: primary eclipse depth (mags),\n pduration: primary eclipse duration (phase),\n psdepthratio: primary-secondary eclipse depth ratio,\n secondaryphase: center phase of the secondary eclipse]\n\n `period` is the period in days.\n\n `epoch` is the time of minimum in JD.\n\n `pdepth` is the depth of the primary eclipse.\n\n - for magnitudes -> `pdepth` should be < 0\n - for fluxes -> `pdepth` should be > 0\n\n `pduration` is the length of the primary eclipse in phase.\n\n `psdepthratio` is the ratio in the eclipse depths:\n `depth_secondary/depth_primary`. This is generally the same as the ratio\n of the `T_effs` of the two stars.\n\n `secondaryphase` is the phase at which the minimum of the secondary\n eclipse is located. This effectively parameterizes eccentricity.\n\n All of these will then have fitted values after the fit is done.\n\n times,mags,errs : np.array\n The input time-series of measurements and associated errors for which\n the eclipse model will be generated. The times will be used to generate\n model mags, and the input `times`, `mags`, and `errs` will be resorted\n by model phase and returned.\n\n Returns\n -------\n\n np.array\n The residuals between the input `mags` and generated `modelmags`,\n weighted by the measurement errors in `errs`.\n\n '''\n\n modelmags, phase, ptimes, pmags, perrs = (\n invgauss_eclipses_func(ebparams, times, mags, errs)\n )\n\n # this is now a weighted residual taking into account the measurement err\n return (pmags - modelmags)/perrs", "response": "This function returns the residual between the modelmags and the actual mags."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef jhk_to_bmag(jmag, hmag, kmag):\n '''Converts given J, H, Ks mags to a B magnitude value.\n\n Parameters\n ----------\n\n jmag,hmag,kmag : float\n 2MASS J, H, Ks mags of the object.\n\n Returns\n -------\n\n float\n The converted B band magnitude.\n\n '''\n\n return convert_constants(jmag,hmag,kmag,\n BJHK,\n BJH, BJK, BHK,\n BJ, BH, BK)", "response": "Converts given J H Ks mags to a B magnitude value."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef jhk_to_vmag(jmag,hmag,kmag):\n '''Converts given J, H, Ks mags to a V magnitude value.\n\n Parameters\n ----------\n\n jmag,hmag,kmag : float\n 2MASS J, H, Ks mags of the object.\n\n Returns\n -------\n\n float\n The converted V band magnitude.\n\n '''\n\n return convert_constants(jmag,hmag,kmag,\n VJHK,\n VJH, VJK, VHK,\n VJ, VH, VK)", "response": "Converts given J H Ks mags to V magnitude value."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nconverting given J H Ks mags to an R band magnitude value.", "response": "def jhk_to_rmag(jmag,hmag,kmag):\n '''Converts given J, H, Ks mags to an R magnitude value.\n\n Parameters\n ----------\n\n jmag,hmag,kmag : float\n 2MASS J, H, Ks mags of the object.\n\n Returns\n -------\n\n float\n The converted R band magnitude.\n\n '''\n\n return convert_constants(jmag,hmag,kmag,\n RJHK,\n RJH, RJK, RHK,\n RJ, RH, RK)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nconvert given J H Ks mags to an I band magnitude value.", "response": "def jhk_to_imag(jmag,hmag,kmag):\n '''Converts given J, H, Ks mags to an I magnitude value.\n\n Parameters\n ----------\n\n jmag,hmag,kmag : float\n 2MASS J, H, Ks mags of the object.\n\n Returns\n -------\n\n float\n The converted I band magnitude.\n\n '''\n\n return convert_constants(jmag,hmag,kmag,\n IJHK,\n IJH, IJK, IHK,\n IJ, IH, IK)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nconverts given J H Ks mags to SDSS u band magnitude value.", "response": "def jhk_to_sdssu(jmag,hmag,kmag):\n '''Converts given J, H, Ks mags to an SDSS u magnitude value.\n\n Parameters\n ----------\n\n jmag,hmag,kmag : float\n 2MASS J, H, Ks mags of the object.\n\n Returns\n -------\n\n float\n The converted SDSS u band magnitude.\n\n '''\n\n return convert_constants(jmag,hmag,kmag,\n SDSSU_JHK,\n SDSSU_JH, SDSSU_JK, SDSSU_HK,\n SDSSU_J, SDSSU_H, SDSSU_K)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nconverts given J H Ks mags to SDSS g magnitude value.", "response": "def jhk_to_sdssg(jmag,hmag,kmag):\n '''Converts given J, H, Ks mags to an SDSS g magnitude value.\n\n Parameters\n ----------\n\n jmag,hmag,kmag : float\n 2MASS J, H, Ks mags of the object.\n\n Returns\n -------\n\n float\n The converted SDSS g band magnitude.\n\n '''\n\n return convert_constants(jmag,hmag,kmag,\n SDSSG_JHK,\n SDSSG_JH, SDSSG_JK, SDSSG_HK,\n SDSSG_J, SDSSG_H, SDSSG_K)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef jhk_to_sdssr(jmag,hmag,kmag):\n '''Converts given J, H, Ks mags to an SDSS r magnitude value.\n\n Parameters\n ----------\n\n jmag,hmag,kmag : float\n 2MASS J, H, Ks mags of the object.\n\n Returns\n -------\n\n float\n The converted SDSS r band magnitude.\n\n '''\n\n return convert_constants(jmag,hmag,kmag,\n SDSSR_JHK,\n SDSSR_JH, SDSSR_JK, SDSSR_HK,\n SDSSR_J, SDSSR_H, SDSSR_K)", "response": "Converts given J H Ks mags to SDSS r magnitude value."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef jhk_to_sdssi(jmag,hmag,kmag):\n '''Converts given J, H, Ks mags to an SDSS i magnitude value.\n\n Parameters\n ----------\n\n jmag,hmag,kmag : float\n 2MASS J, H, Ks mags of the object.\n\n Returns\n -------\n\n float\n The converted SDSS i band magnitude.\n\n '''\n\n return convert_constants(jmag,hmag,kmag,\n SDSSI_JHK,\n SDSSI_JH, SDSSI_JK, SDSSI_HK,\n SDSSI_J, SDSSI_H, SDSSI_K)", "response": "Converts given J H Ks mags to SDSS i band magnitude value."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nconverting given J H Ks mags to SDSS z magnitude value.", "response": "def jhk_to_sdssz(jmag,hmag,kmag):\n '''Converts given J, H, Ks mags to an SDSS z magnitude value.\n\n Parameters\n ----------\n\n jmag,hmag,kmag : float\n 2MASS J, H, Ks mags of the object.\n\n Returns\n -------\n\n float\n The converted SDSS z band magnitude.\n\n '''\n\n return convert_constants(jmag,hmag,kmag,\n SDSSZ_JHK,\n SDSSZ_JH, SDSSZ_JK, SDSSZ_HK,\n SDSSZ_J, SDSSZ_H, SDSSZ_K)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncalculate the absolute magnitude for a GAIA object.", "response": "def absolute_gaia_magnitude(gaia_mag,\n gaia_parallax_mas,\n gaia_mag_err=None,\n gaia_parallax_err_mas=None):\n '''Calculates the GAIA absolute magnitude for object (or array of objects).\n\n Given a G mag and the parallax measured by GAIA, gets the absolute mag using\n the usual equation::\n\n G - M_G = 5 x log10(d_pc) - 5\n M_G = 5 - 5log10(d_pc) + G\n\n Parameters\n ----------\n\n gaia_mag : float or array-like\n The measured GAIA G magnitude.\n\n gaia_parallax_max : float or array-like\n The measured parallax of the object in mas.\n\n gaia_mag_err : float or array-like or None\n The measurement error in GAIA G magnitude.\n\n gaia_parallax_err_mas : float or array-like or None\n The measurement error in GAIA parallax in mas.\n\n Returns\n -------\n\n float or array-like\n The absolute magnitude M_G of the object(s).\n\n If both `_err` input kwargs are provided, will return a tuple of the form::\n\n (M_G float or array-like, M_G_err float or array-like)\n\n '''\n\n # get the distance\n # we're using the naive calculation of d. this is inaccurate as stated in\n # Bailer-Jones 2015 (http://arxiv.org/abs/1507.02105) if the error in\n # parallax is a significant fraction of parallax\n d_pc = np.abs(1000.0/gaia_parallax_mas)\n\n # get the distance error\n if gaia_parallax_err_mas is not None:\n d_pc_err = (\n (1000.0/(gaia_parallax_mas*gaia_parallax_mas)) *\n gaia_parallax_err_mas\n )\n else:\n d_pc_err = None\n\n # calculate the absolute mag from the relation\n # FIXME: this is NOT corrected for extinction in G mag. see Jordi+ 2010\n # (http://adsabs.harvard.edu/abs/2010A%26A...523A..48J) to figure out\n # A_G/A_V as a function of (V-I)_0, then apply it here\n M_G = 5 - 5.0*np.log10(d_pc) + gaia_mag\n\n # calculate the err in M_G\n if d_pc_err is not None and gaia_mag_err is not None:\n\n M_G_err = np.sqrt(\n ((5.0/(d_pc * np.log(10.0)))**2 * (d_pc_err)**2) +\n gaia_mag_err*gaia_mag_err\n )\n else:\n M_G_err = None\n\n\n if M_G_err is not None:\n return M_G, M_G_err\n else:\n return M_G"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncalculating the Schwarzenberg - Czerny AoV statistic at a test frequency.", "response": "def aov_theta(times, mags, errs, frequency,\n binsize=0.05, minbin=9):\n '''Calculates the Schwarzenberg-Czerny AoV statistic at a test frequency.\n\n Parameters\n ----------\n\n times,mags,errs : np.array\n The input time-series and associated errors.\n\n frequency : float\n The test frequency to calculate the theta statistic at.\n\n binsize : float\n The phase bin size to use.\n\n minbin : int\n The minimum number of items in a phase bin to consider in the\n calculation of the statistic.\n\n Returns\n -------\n\n theta_aov : float\n The value of the AoV statistic at the specified `frequency`.\n\n '''\n\n period = 1.0/frequency\n fold_time = times[0]\n\n phased = phase_magseries(times,\n mags,\n period,\n fold_time,\n wrap=False,\n sort=True)\n\n phases = phased['phase']\n pmags = phased['mags']\n bins = nparange(0.0, 1.0, binsize)\n ndets = phases.size\n\n binnedphaseinds = npdigitize(phases, bins)\n\n bin_s1_tops = []\n bin_s2_tops = []\n binndets = []\n goodbins = 0\n\n all_xbar = npmedian(pmags)\n\n for x in npunique(binnedphaseinds):\n\n thisbin_inds = binnedphaseinds == x\n thisbin_mags = pmags[thisbin_inds]\n\n if thisbin_mags.size > minbin:\n\n thisbin_ndet = thisbin_mags.size\n thisbin_xbar = npmedian(thisbin_mags)\n\n # get s1\n thisbin_s1_top = (\n thisbin_ndet *\n (thisbin_xbar - all_xbar) *\n (thisbin_xbar - all_xbar)\n )\n\n # get s2\n thisbin_s2_top = npsum((thisbin_mags - all_xbar) *\n (thisbin_mags - all_xbar))\n\n bin_s1_tops.append(thisbin_s1_top)\n bin_s2_tops.append(thisbin_s2_top)\n binndets.append(thisbin_ndet)\n goodbins = goodbins + 1\n\n\n # turn the quantities into arrays\n bin_s1_tops = nparray(bin_s1_tops)\n bin_s2_tops = nparray(bin_s2_tops)\n binndets = nparray(binndets)\n\n # calculate s1 first\n s1 = npsum(bin_s1_tops)/(goodbins - 1.0)\n\n # then calculate s2\n s2 = npsum(bin_s2_tops)/(ndets - goodbins)\n\n theta_aov = s1/s2\n\n return theta_aov"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _aov_worker(task):\n '''This is a parallel worker for the function below.\n\n Parameters\n ----------\n\n task : tuple\n This is of the form below::\n\n task[0] = times\n task[1] = mags\n task[2] = errs\n task[3] = frequency\n task[4] = binsize\n task[5] = minbin\n\n Returns\n -------\n\n theta_aov : float\n The theta value at the specified frequency. nan if the calculation\n fails.\n\n '''\n\n times, mags, errs, frequency, binsize, minbin = task\n\n try:\n\n theta = aov_theta(times, mags, errs, frequency,\n binsize=binsize, minbin=minbin)\n\n return theta\n\n except Exception as e:\n\n return npnan", "response": "This is a parallel worker for the function below."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_frequency_grid(times,\n samplesperpeak=5,\n nyquistfactor=5,\n minfreq=None,\n maxfreq=None,\n returnf0dfnf=False):\n '''This calculates a frequency grid for the period finding functions in this\n module.\n\n Based on the autofrequency function in astropy.stats.lombscargle.\n\n http://docs.astropy.org/en/stable/_modules/astropy/stats/lombscargle/core.html#LombScargle.autofrequency\n\n Parameters\n ----------\n\n times : np.array\n The times to use to generate the frequency grid over.\n\n samplesperpeak : int\n The minimum sample coverage each frequency point in the grid will get.\n\n nyquistfactor : int\n The multiplier over the Nyquist rate to use.\n\n minfreq,maxfreq : float or None\n If not None, these will be the limits of the frequency grid generated.\n\n returnf0dfnf : bool\n If this is True, will return the values of `f0`, `df`, and `Nf`\n generated for this grid.\n\n Returns\n -------\n\n np.array\n A grid of frequencies.\n\n '''\n\n baseline = times.max() - times.min()\n nsamples = times.size\n\n df = 1. / baseline / samplesperpeak\n\n if minfreq is not None:\n f0 = minfreq\n else:\n f0 = 0.5 * df\n\n if maxfreq is not None:\n Nf = int(np.ceil((maxfreq - f0) / df))\n else:\n Nf = int(0.5 * samplesperpeak * nyquistfactor * nsamples)\n\n\n if returnf0dfnf:\n return f0, df, Nf, f0 + df * np.arange(Nf)\n else:\n return f0 + df * np.arange(Nf)", "response": "This function calculates a frequency grid for the period finding functions in this module."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef bootstrap_falsealarmprob(lspinfo,\n times,\n mags,\n errs,\n nbootstrap=250,\n magsarefluxes=False,\n sigclip=10.0,\n npeaks=None):\n '''Calculates the false alarm probabilities of periodogram peaks using\n bootstrap resampling of the magnitude time series.\n\n The false alarm probability here is defined as::\n\n (1.0 + sum(trialbestpeaks[i] > peak[j]))/(ntrialbestpeaks + 1)\n\n for each best periodogram peak j. The index i is for each bootstrap\n trial. This effectively gives us a significance for the peak. Smaller FAP\n means a better chance that the peak is real.\n\n The basic idea is to get the number of trial best peaks that are larger than\n the current best peak and divide this by the total number of trials. The\n distribution of these trial best peaks is obtained after scrambling the mag\n values and rerunning the specified periodogram method for a bunch of trials.\n\n `lspinfo` is the output dict from a periodbase periodogram function and MUST\n contain a 'method' key that corresponds to one of the keys in the LSPMETHODS\n dict above. This will let this function know which periodogram function to\n run to generate the bootstrap samples. The lspinfo SHOULD also have a\n 'kwargs' key that corresponds to the input keyword arguments for the\n periodogram function as it was run originally, to keep everything the same\n during the bootstrap runs. If this is missing, default values will be used.\n\n FIXME: this may not be strictly correct; must look more into bootstrap\n significance testing. Also look into if we're doing resampling correctly for\n time series because the samples are not iid. Look into moving block\n bootstrap.\n\n Parameters\n ----------\n\n lspinfo : dict\n A dict of period-finder results from one of the period-finders in\n periodbase, or your own functions, provided it's of the form and\n contains at least the keys listed below::\n\n {'periods': np.array of all periods searched by the period-finder,\n 'lspvals': np.array of periodogram power value for each period,\n 'bestperiod': a float value that is the period with the highest\n peak in the periodogram, i.e. the most-likely actual\n period,\n 'method': a three-letter code naming the period-finder used; must\n be one of the keys in the\n `astrobase.periodbase.METHODLABELS` dict,\n 'nbestperiods': a list of the periods corresponding to periodogram\n peaks (`nbestlspvals` below) to annotate on the\n periodogram plot so they can be called out\n visually,\n 'nbestlspvals': a list of the power values associated with\n periodogram peaks to annotate on the periodogram\n plot so they can be called out visually; should be\n the same length as `nbestperiods` above,\n 'kwargs': dict of kwargs passed to your own period-finder function}\n\n If you provide your own function's period-finder results, you should add\n a corresponding key for it to the LSPMETHODS dict above so the bootstrap\n function can use it correctly. Your period-finder function should take\n `times`, `mags`, errs and any extra parameters as kwargs and return a\n dict of the form described above. A small worked example::\n\n from your_module import your_periodfinder_func\n from astrobase import periodbase\n\n periodbase.LSPMETHODS['your-finder'] = your_periodfinder_func\n\n # run a period-finder session\n your_pfresults = your_periodfinder_func(times, mags, errs,\n **extra_kwargs)\n\n # run bootstrap to find FAP\n falsealarm_info = periodbase.bootstrap_falsealarmprob(\n your_pfresults,\n times, mags, errs,\n nbootstrap=250,\n magsarefluxes=False,\n )\n\n times,mags,errs : np.arrays\n The magnitude/flux time-series to process along with their associated\n measurement errors.\n\n nbootstrap : int\n The total number of bootstrap trials to run. This is set to 250 by\n default, but should probably be around 1000 for realistic results.\n\n magsarefluxes : bool\n If True, indicates the input time-series is fluxes and not mags.\n\n sigclip : float or int or sequence of two floats/ints or None\n If a single float or int, a symmetric sigma-clip will be performed using\n the number provided as the sigma-multiplier to cut out from the input\n time-series.\n\n If a list of two ints/floats is provided, the function will perform an\n 'asymmetric' sigma-clip. The first element in this list is the sigma\n value to use for fainter flux/mag values; the second element in this\n list is the sigma value to use for brighter flux/mag values. For\n example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma\n dimmings and greater than 3-sigma brightenings. Here the meaning of\n \"dimming\" and \"brightening\" is set by *physics* (not the magnitude\n system), which is why the `magsarefluxes` kwarg must be correctly set.\n\n If `sigclip` is None, no sigma-clipping will be performed, and the\n time-series (with non-finite elems removed) will be passed through to\n the output.\n\n npeaks : int or None\n The number of peaks from the list of 'nbestlspvals' in the period-finder\n result dict to run the bootstrap for. If None, all of the peaks in this\n list will have their FAP calculated.\n\n Returns\n -------\n\n dict\n Returns a dict of the form::\n\n {'peaks':allpeaks,\n 'periods':allperiods,\n 'probabilities':allfaps,\n 'alltrialbestpeaks':alltrialbestpeaks}\n\n '''\n\n # figure out how many periods to work on\n if (npeaks and (0 < npeaks < len(lspinfo['nbestperiods']))):\n nperiods = npeaks\n else:\n LOGWARNING('npeaks not specified or invalid, '\n 'getting FAP for all %s periodogram peaks' %\n len(lspinfo['nbestperiods']))\n nperiods = len(lspinfo['nbestperiods'])\n\n nbestperiods = lspinfo['nbestperiods'][:nperiods]\n nbestpeaks = lspinfo['nbestlspvals'][:nperiods]\n\n # get rid of nans first and sigclip\n stimes, smags, serrs = sigclip_magseries(times,\n mags,\n errs,\n magsarefluxes=magsarefluxes,\n sigclip=sigclip)\n\n allpeaks = []\n allperiods = []\n allfaps = []\n alltrialbestpeaks = []\n\n # make sure there are enough points to calculate a spectrum\n if len(stimes) > 9 and len(smags) > 9 and len(serrs) > 9:\n\n for ind, period, peak in zip(range(len(nbestperiods)),\n nbestperiods,\n nbestpeaks):\n\n LOGINFO('peak %s: running %s trials...' % (ind+1, nbootstrap))\n\n trialbestpeaks = []\n\n for _trial in range(nbootstrap):\n\n # get a scrambled index\n tindex = np.random.randint(0,\n high=mags.size,\n size=mags.size)\n\n\n # get the kwargs dict out of the lspinfo\n if 'kwargs' in lspinfo:\n\n kwargs = lspinfo['kwargs']\n\n # update the kwargs with some local stuff\n kwargs.update({'magsarefluxes':magsarefluxes,\n 'sigclip':sigclip,\n 'verbose':False})\n else:\n kwargs = {'magsarefluxes':magsarefluxes,\n 'sigclip':sigclip,\n 'verbose':False}\n\n\n # run the periodogram with scrambled mags and errs\n # and the appropriate keyword arguments\n lspres = LSPMETHODS[lspinfo['method']](\n times, mags[tindex], errs[tindex],\n **kwargs\n )\n trialbestpeaks.append(lspres['bestlspval'])\n\n trialbestpeaks = np.array(trialbestpeaks)\n alltrialbestpeaks.append(trialbestpeaks)\n\n # calculate the FAP for a trial peak j = FAP[j] =\n # (1.0 + sum(trialbestpeaks[i] > peak[j]))/(ntrialbestpeaks + 1)\n if lspinfo['method'] != 'pdm':\n falsealarmprob = (\n (1.0 + trialbestpeaks[trialbestpeaks > peak].size) /\n (trialbestpeaks.size + 1.0)\n )\n # for PDM, we're looking for a peak smaller than the best peak\n # because values closer to 0.0 are more significant\n else:\n falsealarmprob = (\n (1.0 + trialbestpeaks[trialbestpeaks < peak].size) /\n (trialbestpeaks.size + 1.0)\n )\n\n LOGINFO('FAP for peak %s, period: %.6f = %.3g' % (ind+1,\n period,\n falsealarmprob))\n\n allpeaks.append(peak)\n allperiods.append(period)\n allfaps.append(falsealarmprob)\n\n return {'peaks':allpeaks,\n 'periods':allperiods,\n 'probabilities':allfaps,\n 'alltrialbestpeaks':alltrialbestpeaks}\n\n else:\n LOGERROR('not enough mag series points to calculate periodogram')\n return None", "response": "Calculates the false alarm probabilities of periodogram peaks using the bootstrap resampling of the magnitude time series."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef read_original_textlc(lcpath):\n '''\n Read .epdlc, and .tfalc light curves and return a corresponding labelled\n dict (if LC from <2012) or astropy table (if >=2012). Each has different\n keys that can be accessed via .keys()\n\n Input:\n lcpath: path (string) to light curve data, which is a textfile with HAT\n LC data.\n\n Example:\n dat = read_original_textlc('HAT-115-0003266.epdlc')\n '''\n\n LOGINFO('reading original HAT text LC: {:s}'.format(lcpath))\n\n N_lines_to_parse_comments = 50\n with open(lcpath, 'rb') as file:\n head = [next(file) for ind in range(N_lines_to_parse_comments)]\n\n N_comment_lines = len([l for l in head if l.decode('UTF-8')[0] == '#'])\n\n # if there are too many comment lines, fail out\n if N_comment_lines < N_lines_to_parse_comments:\n LOGERROR(\n 'LC file {fpath} has too many comment lines'.format(fpath=lcpath)\n )\n return None\n\n first_data_line = list(\n filter(None, head[N_comment_lines].decode('UTF-8').split())\n )\n N_cols = len(first_data_line)\n\n # There are different column formats depending on when HAT pipeline was run\n # also different formats for different types of LCs:\n # pre-2012: .epdlc -> 17 columns\n # pre-2012: .tfalc -> 20 columns\n # post-2012: .epdlc or .tfalc -> 32 columns\n\n if N_cols == 17:\n colformat = 'pre2012-epdlc'\n elif N_cols == 20:\n colformat = 'pre2012-tfalc'\n elif N_cols == 32:\n colformat = 'post2012-hatlc'\n else:\n LOGERROR(\"can't handle this column format yet, \"\n \"file: {fpath}, ncols: {ncols}\".format(fpath=lcpath,\n ncols=N_cols))\n return None\n\n\n # deal with pre-2012 column format\n if colformat == 'pre2012-epdlc':\n\n col_names = ['framekey','rjd',\n 'aim_000','aie_000','aiq_000',\n 'aim_001','aie_001','aiq_001',\n 'aim_002','aie_002','aiq_002',\n 'arm_000','arm_001','arm_002',\n 'aep_000','aep_001','aep_002']\n col_dtypes = ['U8',float,\n float,float,'U1',\n float,float,'U1',\n float,float,'U1',\n float,float,float,\n float,float,float]\n dtype_pairs = [el for el in zip(col_names, col_dtypes)]\n data = np.genfromtxt(lcpath, names=col_names, dtype=col_dtypes,\n skip_header=N_comment_lines, delimiter=None)\n out = {}\n for ix in range(len(data.dtype.names)):\n out[data.dtype.names[ix]] = data[data.dtype.names[ix]]\n\n elif colformat == 'pre2012-tfalc':\n\n col_names = ['framekey','rjd',\n 'aim_000','aie_000','aiq_000',\n 'aim_001','aie_001','aiq_001',\n 'aim_002','aie_002','aiq_002',\n 'arm_000','arm_001','arm_002',\n 'aep_000','aep_001','aep_002',\n 'atf_000','atf_001','atf_002']\n col_dtypes = ['U8',float,\n float,float,'U1',\n float,float,'U1',\n float,float,'U1',\n float,float,float,\n float,float,float,\n float,float,float]\n dtype_pairs = [el for el in zip(col_names, col_dtypes)]\n data = np.genfromtxt(lcpath, names=col_names, dtype=col_dtypes,\n skip_header=N_comment_lines, delimiter=None)\n out = {}\n for ix in range(len(data.dtype.names)):\n out[data.dtype.names[ix]] = data[data.dtype.names[ix]]\n\n elif colformat == 'post2012-hatlc':\n\n col_names = ['hatid', 'framekey', 'fld', 'bjd',\n 'aim_000', 'aie_000', 'aiq_000',\n 'aim_001', 'aie_001', 'aiq_001',\n 'aim_002', 'aie_002', 'aiq_002',\n 'arm_000', 'arm_001', 'arm_002',\n 'aep_000', 'aep_001', 'aep_002',\n 'atf_000', 'atf_001', 'atf_002',\n 'xcc', 'ycc', 'bgv', 'bge',\n 'fsv', 'fdv', 'fkv',\n 'iha', 'izd', 'rjd']\n\n out = astascii.read(lcpath, names=col_names, comment='#')\n\n return out", "response": "Read original text file with HAT - 111 - 0003266. epdlc and. tfalc light curves and return a corresponding labelled\n dict."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef traptransit_fit_magseries(times, mags, errs,\n transitparams,\n sigclip=10.0,\n plotfit=False,\n magsarefluxes=False,\n verbose=True):\n '''This fits a trapezoid transit model to a magnitude time series.\n\n Parameters\n ----------\n\n times,mags,errs : np.array\n The input mag/flux time-series to fit a trapezoid planet-transit model\n to.\n\n period : float\n The period to use for the model fit.\n\n transitparams : list of floats\n These are initial parameters for the transit model fit. A list of the\n following form is required::\n\n transitparams = [transitperiod (time),\n transitepoch (time),\n transitdepth (flux or mags),\n transitduration (phase),\n ingressduration (phase)]\n\n - for magnitudes -> `transitdepth` should be < 0\n - for fluxes -> `transitdepth` should be > 0\n\n If `transitepoch` is None, this function will do an initial spline fit\n to find an approximate minimum of the phased light curve using the given\n period.\n\n The `transitdepth` provided is checked against the value of\n `magsarefluxes`. if `magsarefluxes = True`, the `transitdepth` is forced\n to be > 0; if `magsarefluxes` = False, the `transitdepth` is forced to\n be < 0.\n\n sigclip : float or int or sequence of two floats/ints or None\n If a single float or int, a symmetric sigma-clip will be performed using\n the number provided as the sigma-multiplier to cut out from the input\n time-series.\n\n If a list of two ints/floats is provided, the function will perform an\n 'asymmetric' sigma-clip. The first element in this list is the sigma\n value to use for fainter flux/mag values; the second element in this\n list is the sigma value to use for brighter flux/mag values. For\n example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma\n dimmings and greater than 3-sigma brightenings. Here the meaning of\n \"dimming\" and \"brightening\" is set by *physics* (not the magnitude\n system), which is why the `magsarefluxes` kwarg must be correctly set.\n\n If `sigclip` is None, no sigma-clipping will be performed, and the\n time-series (with non-finite elems removed) will be passed through to\n the output.\n\n magsarefluxes : bool\n If True, will treat the input values of `mags` as fluxes for purposes of\n plotting the fit and sig-clipping.\n\n plotfit : str or False\n If this is a string, this function will make a plot for the fit to the\n mag/flux time-series and writes the plot to the path specified here.\n\n ignoreinitfail : bool\n If this is True, ignores the initial failure to find a set of optimized\n Fourier parameters using the global optimization function and proceeds\n to do a least-squares fit anyway.\n\n verbose : bool\n If True, will indicate progress and warn of any problems.\n\n Returns\n -------\n\n dict\n This function returns a dict containing the model fit parameters, the\n minimized chi-sq value and the reduced chi-sq value. The form of this\n dict is mostly standardized across all functions in this module::\n\n {\n 'fittype':'traptransit',\n 'fitinfo':{\n 'initialparams':the initial transit params provided,\n 'finalparams':the final model fit transit params ,\n 'finalparamerrs':formal errors in the params,\n 'leastsqfit':the full tuple returned by scipy.leastsq,\n 'fitmags': the model fit mags,\n 'fitepoch': the epoch of minimum light for the fit,\n 'ntransitpoints': the number of LC points in transit phase\n },\n 'fitchisq': the minimized value of the fit's chi-sq,\n 'fitredchisq':the reduced chi-sq value,\n 'fitplotfile': the output fit plot if fitplot is not None,\n 'magseries':{\n 'times':input times in phase order of the model,\n 'phase':the phases of the model mags,\n 'mags':input mags/fluxes in the phase order of the model,\n 'errs':errs in the phase order of the model,\n 'magsarefluxes':input value of magsarefluxes kwarg\n }\n }\n\n '''\n\n stimes, smags, serrs = sigclip_magseries(times, mags, errs,\n sigclip=sigclip,\n magsarefluxes=magsarefluxes)\n\n # get rid of zero errs\n nzind = np.nonzero(serrs)\n stimes, smags, serrs = stimes[nzind], smags[nzind], serrs[nzind]\n\n # check the transitparams\n transitperiod, transitepoch, transitdepth = transitparams[0:3]\n\n # check if we have a transitepoch to use\n if transitepoch is None:\n\n if verbose:\n LOGWARNING('no transitepoch given in transitparams, '\n 'trying to figure it out automatically...')\n # do a spline fit to figure out the approximate min of the LC\n try:\n spfit = spline_fit_magseries(times, mags, errs, transitperiod,\n sigclip=sigclip,\n magsarefluxes=magsarefluxes,\n verbose=verbose)\n transitepoch = spfit['fitinfo']['fitepoch']\n\n # if the spline-fit fails, try a savgol fit instead\n except Exception as e:\n sgfit = savgol_fit_magseries(times, mags, errs, transitperiod,\n sigclip=sigclip,\n magsarefluxes=magsarefluxes,\n verbose=verbose)\n transitepoch = sgfit['fitinfo']['fitepoch']\n\n # if everything failed, then bail out and ask for the transitepoch\n finally:\n\n if transitepoch is None:\n LOGERROR(\"couldn't automatically figure out the transit epoch, \"\n \"can't continue. please provide it in transitparams.\")\n\n # assemble the returndict\n returndict = {\n 'fittype':'traptransit',\n 'fitinfo':{\n 'initialparams':transitparams,\n 'finalparams':None,\n 'leastsqfit':None,\n 'fitmags':None,\n 'fitepoch':None,\n },\n 'fitchisq':np.nan,\n 'fitredchisq':np.nan,\n 'fitplotfile':None,\n 'magseries':{\n 'phase':None,\n 'times':None,\n 'mags':None,\n 'errs':None,\n 'magsarefluxes':magsarefluxes,\n },\n }\n\n return returndict\n\n else:\n\n # check the case when there are more than one transitepochs\n # returned\n if transitepoch.size > 1:\n if verbose:\n LOGWARNING(\n \"could not auto-find a single minimum in LC for \"\n \"transitepoch, using the first one returned\"\n )\n transitparams[1] = transitepoch[0]\n\n else:\n\n if verbose:\n LOGWARNING(\n 'using automatically determined transitepoch = %.5f'\n % transitepoch\n )\n transitparams[1] = transitepoch.item()\n\n # next, check the transitdepth and fix it to the form required\n if magsarefluxes:\n if transitdepth < 0.0:\n transitparams[2] = -transitdepth\n\n else:\n if transitdepth > 0.0:\n transitparams[2] = -transitdepth\n\n # finally, do the fit\n try:\n leastsqfit = spleastsq(transits.trapezoid_transit_residual,\n transitparams,\n args=(stimes, smags, serrs),\n full_output=True)\n except Exception as e:\n leastsqfit = None\n\n # if the fit succeeded, then we can return the final parameters\n if leastsqfit and leastsqfit[-1] in (1,2,3,4):\n\n finalparams = leastsqfit[0]\n covxmatrix = leastsqfit[1]\n\n # calculate the chisq and reduced chisq\n fitmags, phase, ptimes, pmags, perrs, n_transitpoints = (\n transits.trapezoid_transit_func(\n finalparams,\n stimes, smags, serrs,\n get_ntransitpoints=True\n )\n )\n fitchisq = np.sum(\n ((fitmags - pmags)*(fitmags - pmags)) / (perrs*perrs)\n )\n fitredchisq = fitchisq/(len(pmags) - len(finalparams) - 1)\n\n # get the residual variance and calculate the formal 1-sigma errs on the\n # final parameters\n residuals = leastsqfit[2]['fvec']\n residualvariance = (\n np.sum(residuals*residuals)/(pmags.size - finalparams.size)\n )\n if covxmatrix is not None:\n covmatrix = residualvariance*covxmatrix\n stderrs = np.sqrt(np.diag(covmatrix))\n else:\n LOGERROR('covxmatrix not available, fit probably failed!')\n stderrs = None\n\n if verbose:\n LOGINFO(\n 'final fit done. chisq = %.5f, reduced chisq = %.5f' %\n (fitchisq, fitredchisq)\n )\n\n # get the fit epoch\n fperiod, fepoch = finalparams[:2]\n\n # assemble the returndict\n returndict = {\n 'fittype':'traptransit',\n 'fitinfo':{\n 'initialparams':transitparams,\n 'finalparams':finalparams,\n 'finalparamerrs':stderrs,\n 'leastsqfit':leastsqfit,\n 'fitmags':fitmags,\n 'fitepoch':fepoch,\n 'ntransitpoints':n_transitpoints\n },\n 'fitchisq':fitchisq,\n 'fitredchisq':fitredchisq,\n 'fitplotfile':None,\n 'magseries':{\n 'phase':phase,\n 'times':ptimes,\n 'mags':pmags,\n 'errs':perrs,\n 'magsarefluxes':magsarefluxes,\n },\n }\n\n # make the fit plot if required\n if plotfit and isinstance(plotfit, str):\n\n make_fit_plot(phase, pmags, perrs, fitmags,\n fperiod, ptimes.min(), fepoch,\n plotfit,\n magsarefluxes=magsarefluxes)\n\n returndict['fitplotfile'] = plotfit\n\n return returndict\n\n # if the leastsq fit failed, return nothing\n else:\n\n LOGERROR('trapezoid-fit: least-squared fit to the light curve failed!')\n\n # assemble the returndict\n returndict = {\n 'fittype':'traptransit',\n 'fitinfo':{\n 'initialparams':transitparams,\n 'finalparams':None,\n 'finalparamerrs':None,\n 'leastsqfit':leastsqfit,\n 'fitmags':None,\n 'fitepoch':None,\n 'ntransitpoints':0\n },\n 'fitchisq':np.nan,\n 'fitredchisq':np.nan,\n 'fitplotfile':None,\n 'magseries':{\n 'phase':None,\n 'times':None,\n 'mags':None,\n 'errs':None,\n 'magsarefluxes':magsarefluxes,\n },\n }\n\n return returndict", "response": "This function fits a trapezoid - transit model to a magnitude time series."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _get_value(quantitystr, fitparams, fixedparams):\n\n # for Mandel-Agol fitting, sometimes we want to fix some parameters,\n # and fit others. this function allows that flexibility.\n fitparamskeys, fixedparamskeys = fitparams.keys(), fixedparams.keys()\n if quantitystr in fitparamskeys:\n quantity = fitparams[quantitystr]\n elif quantitystr in fixedparamskeys:\n quantity = fixedparams[quantitystr]\n return quantity", "response": "This function returns the value of a parameter in a node."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _transit_model(times, t0, per, rp, a, inc, ecc, w, u, limb_dark,\n exp_time_minutes=2, supersample_factor=7):\n '''This returns a BATMAN planetary transit model.\n\n Parameters\n ----------\n\n times : np.array\n The times at which the model will be evaluated.\n\n t0 : float\n The time of periastron for the transit.\n\n per : float\n The orbital period of the planet.\n\n rp : float\n The stellar radius of the planet's star (in Rsun).\n\n a : float\n The semi-major axis of the planet's orbit (in Rsun).\n\n inc : float\n The orbital inclination (in degrees).\n\n ecc : float\n The eccentricity of the orbit.\n\n w : float\n The longitude of periastron (in degrees).\n\n u : list of floats\n The limb darkening coefficients specific to the limb darkening model\n used.\n\n limb_dark : {\"uniform\", \"linear\", \"quadratic\", \"square-root\", \"logarithmic\", \"exponential\", \"power2\", \"custom\"}\n The type of limb darkening model to use. See the full list here:\n\n https://www.cfa.harvard.edu/~lkreidberg/batman/tutorial.html#limb-darkening-options\n\n exp_time_minutes : float\n The amount of time to 'smear' the transit LC points over to simulate a\n long exposure time.\n\n supersample_factor: int\n The number of supersampled time data points to average the lightcurve\n model over.\n\n Returns\n -------\n\n (params, batman_model) : tuple\n The returned tuple contains the params list and the generated\n `batman.TransitModel` object.\n\n '''\n params = batman.TransitParams() # object to store transit parameters\n params.t0 = t0 # time of periastron\n params.per = per # orbital period\n params.rp = rp # planet radius (in stellar radii)\n params.a = a # semi-major axis (in stellar radii)\n params.inc = inc # orbital inclination (in degrees)\n params.ecc = ecc # the eccentricity of the orbit\n params.w = w # longitude of periastron (in degrees)\n params.u = u # limb darkening coefficient list\n params.limb_dark = limb_dark # limb darkening model to use\n\n t = times\n m = batman.TransitModel(params, t, exp_time=exp_time_minutes/60./24.,\n supersample_factor=supersample_factor)\n\n return params, m", "response": "This function returns a BATMAN planetary transit model."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _log_prior_transit(theta, priorbounds):\n '''\n Assume priors on all parameters have uniform probability.\n '''\n # priorbounds contains the input priors, and because of how we previously\n # sorted theta, its sorted keys tell us which parts of theta correspond to\n # which physical quantities.\n\n allowed = True\n for ix, key in enumerate(np.sort(list(priorbounds.keys()))):\n if priorbounds[key][0] < theta[ix] < priorbounds[key][1]:\n allowed = True and allowed\n else:\n allowed = False\n\n if allowed:\n return 0.\n\n return -np.inf", "response": "Return the log prior transit of theta."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncompute the log likelihood of a single object in a single object.", "response": "def _log_likelihood_transit(theta, params, model, t, flux, err_flux,\n priorbounds):\n '''\n Given a batman TransitModel and its proposed parameters (theta), update the\n batman params object with the proposed parameters and evaluate the gaussian\n likelihood.\n\n Note: the priorbounds are only needed to parse theta.\n '''\n\n u = []\n\n for ix, key in enumerate(sorted(priorbounds.keys())):\n\n if key == 'rp':\n params.rp = theta[ix]\n elif key == 't0':\n params.t0 = theta[ix]\n elif key == 'sma':\n params.a = theta[ix]\n elif key == 'incl':\n params.inc = theta[ix]\n elif key == 'period':\n params.per = theta[ix]\n elif key == 'ecc':\n params.per = theta[ix]\n elif key == 'omega':\n params.w = theta[ix]\n elif key == 'u_linear':\n u.append(theta[ix])\n elif key == 'u_quadratic':\n u.append(theta[ix])\n params.u = u\n\n lc = model.light_curve(params)\n residuals = flux - lc\n log_likelihood = -0.5*(\n np.sum((residuals/err_flux)**2 + np.log(2*np.pi*(err_flux)**2))\n )\n\n return log_likelihood"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncomputing the log likelihood of a transit model plus a line.", "response": "def _log_likelihood_transit_plus_line(theta, params, model, t, data_flux,\n err_flux, priorbounds):\n '''\n Given a batman TransitModel and its proposed parameters (theta), update the\n batman params object with the proposed parameters and evaluate the gaussian\n likelihood.\n\n Note: the priorbounds are only needed to parse theta.\n '''\n\n u = []\n for ix, key in enumerate(sorted(priorbounds.keys())):\n\n if key == 'rp':\n params.rp = theta[ix]\n elif key == 't0':\n params.t0 = theta[ix]\n elif key == 'sma':\n params.a = theta[ix]\n elif key == 'incl':\n params.inc = theta[ix]\n elif key == 'period':\n params.per = theta[ix]\n elif key == 'ecc':\n params.per = theta[ix]\n elif key == 'omega':\n params.w = theta[ix]\n elif key == 'u_linear':\n u.append(theta[ix])\n elif key == 'u_quadratic':\n u.append(theta[ix])\n params.u = u\n elif key == 'poly_order0':\n poly_order0 = theta[ix]\n elif key == 'poly_order1':\n poly_order1 = theta[ix]\n\n try:\n poly_order0\n except Exception as e:\n poly_order0 = 0\n else:\n pass\n\n transit = model.light_curve(params)\n line = poly_order0 + t*poly_order1\n model = transit + line\n\n residuals = data_flux - model\n\n log_likelihood = -0.5*(\n np.sum((residuals/err_flux)**2 + np.log(2*np.pi*(err_flux)**2))\n )\n\n return log_likelihood"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef log_posterior_transit(theta, params, model, t, flux, err_flux, priorbounds):\n '''\n Evaluate posterior probability given proposed model parameters and\n the observed flux timeseries.\n '''\n lp = _log_prior_transit(theta, priorbounds)\n if not np.isfinite(lp):\n return -np.inf\n else:\n return lp + _log_likelihood_transit(theta, params, model, t, flux,\n err_flux, priorbounds)", "response": "Evaluate posterior probability given proposed model parameters and the observed flux timeseries."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nevaluate posterior probability given proposed model parameters and the observed flux timeseries.", "response": "def log_posterior_transit_plus_line(theta, params, model, t, flux, err_flux,\n priorbounds):\n '''\n Evaluate posterior probability given proposed model parameters and\n the observed flux timeseries.\n '''\n lp = _log_prior_transit_plus_line(theta, priorbounds)\n if not np.isfinite(lp):\n return -np.inf\n else:\n return (\n lp + _log_likelihood_transit_plus_line(\n theta, params, model, t, flux, err_flux, priorbounds)\n )"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef mandelagol_fit_magseries(\n times, mags, errs,\n fitparams,\n priorbounds,\n fixedparams,\n trueparams=None,\n burninpercent=0.3,\n plotcorner=False,\n samplesavpath=False,\n n_walkers=50,\n n_mcmc_steps=400,\n eps=1e-4,\n skipsampling=False,\n overwriteexistingsamples=False,\n mcmcprogressbar=False,\n plotfit=False,\n magsarefluxes=False,\n sigclip=10.0,\n verbose=True,\n nworkers=4\n):\n '''This fits a Mandel & Agol (2002) planetary transit model to a flux time\n series. You can fit and fix whatever parameters you want.\n\n It relies on Kreidberg (2015)'s BATMAN implementation for the transit model,\n emcee to sample the posterior (Foreman-Mackey et al 2013), `corner` to plot\n it, and `h5py` to save the samples. See e.g., Claret's work for good guesses\n of star-appropriate limb-darkening parameters.\n\n NOTE: this only works for flux time-series at the moment.\n\n NOTE: Between the `fitparams`, `priorbounds`, and `fixedparams` dicts, you\n must specify all of the planetary transit parameters required by BATMAN:\n `['t0', 'rp', 'sma', 'incl', 'u', 'rp', 'ecc', 'omega', 'period']`, or the\n BATMAN model will fail to initialize.\n\n Parameters\n ----------\n\n times,mags,errs : np.array\n The input flux time-series to fit a Fourier cosine series to.\n\n fitparams : dict\n This is the initial parameter guesses for MCMC, found e.g., by\n BLS. The key string format must not be changed, but any parameter can be\n either \"fit\" or \"fixed\". If it is \"fit\", it must have a corresponding\n prior. For example::\n\n fitparams = {'t0':1325.9, 'rp':np.sqrt(fitd['transitdepth']),\n 'sma':6.17, 'incl':85, 'u':[0.3, 0.2]}\n\n where 'u' is a list of the limb darkening parameters, Linear first, then\n quadratic. Quadratic limb darkening is the only form implemented.\n\n priorbounds : dict\n This sets the lower & upper bounds on uniform prior, e.g.::\n\n priorbounds = {'rp':(0.135, 0.145), 'u_linear':(0.3-1, 0.3+1),\n 'u_quad':(0.2-1, 0.2+1), 't0':(np.min(time),\n np.max(time)), 'sma':(6,6.4), 'incl':(80,90)}\n\n fixedparams : dict\n This sets which parameters are fixed, and their values. For example::\n\n fixedparams = {'ecc':0.,\n 'omega':90.,\n 'limb_dark':'quadratic',\n 'period':fitd['period'] }\n\n `limb_dark` must be \"quadratic\". It's \"fixed\", because once you\n choose your limb-darkening model, it's fixed.\n\n trueparams : list of floats\n The true parameter values you're fitting for, if they're known (e.g., a\n known planet, or fake data). Only for plotting purposes.\n\n burninpercent : float\n The percent of MCMC samples to discard as burn-in.\n\n plotcorner : str or False\n If this is a str, points to the path of output corner plot that will be\n generated for this MCMC run.\n\n samplesavpath : str\n This must be provided so `emcee` can save its MCMC samples to disk as\n HDF5 files. This will set the path of the output HDF5file written.\n\n n_walkers : int\n The number of MCMC walkers to use.\n\n n_mcmc_steps : int\n The number of MCMC steps to take.\n\n eps : float\n The radius of the `n_walkers-dimensional` Gaussian ball used to\n initialize the MCMC.\n\n skipsampling : bool\n If you've already collected MCMC samples, and you do not want any more\n sampling (e.g., just make the plots), set this to be True.\n\n overwriteexistingsamples : bool\n If you've collected samples, but you want to overwrite them, set this to\n True. Usually, it should be False, which appends samples to\n `samplesavpath` HDF5 file.\n\n mcmcprogressbar : bool\n If True, will show a progress bar for the MCMC process.\n\n plotfit: str or bool\n If a str, indicates the path of the output fit plot file. If False, no\n fit plot will be made.\n\n magsarefluxes : bool\n This indicates if the input measurements in `mags` are actually fluxes.\n\n sigclip : float or int or sequence of two floats/ints or None\n If a single float or int, a symmetric sigma-clip will be performed using\n the number provided as the sigma-multiplier to cut out from the input\n time-series.\n\n If a list of two ints/floats is provided, the function will perform an\n 'asymmetric' sigma-clip. The first element in this list is the sigma\n value to use for fainter flux/mag values; the second element in this\n list is the sigma value to use for brighter flux/mag values. For\n example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma\n dimmings and greater than 3-sigma brightenings. Here the meaning of\n \"dimming\" and \"brightening\" is set by *physics* (not the magnitude\n system), which is why the `magsarefluxes` kwarg must be correctly set.\n\n If `sigclip` is None, no sigma-clipping will be performed, and the\n time-series (with non-finite elems removed) will be passed through to\n the output.\n\n verbose : bool\n If True, will indicate MCMC progress.\n\n nworkers : int\n The number of parallel workers to launch for MCMC.\n\n Returns\n -------\n\n dict\n This function returns a dict containing the model fit parameters and\n other fit information. The form of this dict is mostly standardized\n across all functions in this module::\n\n {\n 'fittype':'mandelagol',\n 'fitinfo':{\n 'initialparams':the initial transit params provided,\n 'fixedparams':the fixed transit params provided,\n 'finalparams':the final model fit transit params,\n 'finalparamerrs':formal errors in the params,\n 'fitmags': the model fit mags,\n 'fitepoch': the epoch of minimum light for the fit,\n },\n 'fitplotfile': the output fit plot if fitplot is not None,\n 'magseries':{\n 'times':input times in phase order of the model,\n 'phase':the phases of the model mags,\n 'mags':input mags/fluxes in the phase order of the model,\n 'errs':errs in the phase order of the model,\n 'magsarefluxes':input value of magsarefluxes kwarg\n }\n }\n\n '''\n\n from multiprocessing import Pool\n fittype = 'mandelagol'\n\n if not magsarefluxes:\n raise NotImplementedError('magsarefluxes is not implemented yet.')\n if not samplesavpath:\n raise ValueError(\n 'This function requires that you save the samples somewhere'\n )\n if not mandel_agol_dependencies:\n raise ImportError(\n 'This function depends on BATMAN, emcee>3.0, corner, and h5py.'\n )\n\n # sigma clip and get rid of zero errs\n stimes, smags, serrs = sigclip_magseries(times, mags, errs,\n sigclip=sigclip,\n magsarefluxes=magsarefluxes)\n nzind = np.nonzero(serrs)\n stimes, smags, serrs = stimes[nzind], smags[nzind], serrs[nzind]\n\n init_period = _get_value('period', fitparams, fixedparams)\n init_epoch = _get_value('t0', fitparams, fixedparams)\n init_rp = _get_value('rp', fitparams, fixedparams)\n init_sma = _get_value('sma', fitparams, fixedparams)\n init_incl = _get_value('incl', fitparams, fixedparams)\n init_ecc = _get_value('ecc', fitparams, fixedparams)\n init_omega = _get_value('omega', fitparams, fixedparams)\n limb_dark = _get_value('limb_dark', fitparams, fixedparams)\n init_u = _get_value('u', fitparams, fixedparams)\n\n if not limb_dark == 'quadratic':\n raise ValueError(\n 'only quadratic limb-darkening is supported at the moment'\n )\n\n # initialize the model and calculate the initial model light-curve\n init_params, init_m = _transit_model(stimes, init_epoch, init_period,\n init_rp, init_sma, init_incl, init_ecc,\n init_omega, init_u, limb_dark)\n init_flux = init_m.light_curve(init_params)\n\n # guessed initial params. give nice guesses, or else emcee struggles.\n theta, fitparamnames = [], []\n for k in np.sort(list(fitparams.keys())):\n if isinstance(fitparams[k], float) or isinstance(fitparams[k], int):\n theta.append(fitparams[k])\n fitparamnames.append(fitparams[k])\n elif isinstance(fitparams[k], list):\n if not len(fitparams[k]) == 2:\n raise ValueError('should only be quadratic LD coeffs')\n theta.append(fitparams[k][0])\n theta.append(fitparams[k][1])\n fitparamnames.append(fitparams[k][0])\n fitparamnames.append(fitparams[k][1])\n\n # initialize sampler\n n_dim = len(theta)\n initial_position_vec = [theta + eps*np.random.randn(n_dim)\n for i in range(n_walkers)]\n\n # run the MCMC, unless you just want to load the available samples\n if not skipsampling:\n\n backend = emcee.backends.HDFBackend(samplesavpath)\n if overwriteexistingsamples:\n LOGWARNING(\n 'erased samples previously at {:s}'.format(samplesavpath)\n )\n backend.reset(n_walkers, n_dim)\n\n # if this is the first run, then start from a gaussian ball.\n # otherwise, resume from the previous samples.\n starting_positions = initial_position_vec\n isfirstrun = True\n if os.path.exists(backend.filename):\n if backend.iteration > 1:\n starting_positions = None\n isfirstrun = False\n\n if verbose and isfirstrun:\n LOGINFO(\n 'start {:s} MCMC with {:d} dims, {:d} steps, {:d} walkers,'.\n format(fittype, n_dim, n_mcmc_steps, n_walkers) +\n ' {:d} threads'.format(nworkers)\n )\n elif verbose and not isfirstrun:\n LOGINFO(\n 'continue {:s} with {:d} dims, {:d} steps, {:d} walkers, '.\n format(fittype, n_dim, n_mcmc_steps, n_walkers) +\n '{:d} threads'.format(nworkers)\n )\n\n import sys\n\n if sys.version_info >= (3, 3):\n with Pool(nworkers) as pool:\n sampler = emcee.EnsembleSampler(\n n_walkers, n_dim, log_posterior_transit,\n args=(init_params, init_m, stimes,\n smags, serrs, priorbounds),\n pool=pool,\n backend=backend\n )\n sampler.run_mcmc(starting_positions, n_mcmc_steps,\n progress=mcmcprogressbar)\n\n elif sys.version_info < (3, 3):\n\n sampler = emcee.EnsembleSampler(\n n_walkers, n_dim, log_posterior_transit,\n args=(init_params, init_m, stimes, smags, serrs, priorbounds),\n threads=nworkers,\n backend=backend\n )\n sampler.run_mcmc(starting_positions, n_mcmc_steps,\n progress=mcmcprogressbar)\n\n if verbose:\n LOGINFO(\n 'ended {:s} MCMC run with {:d} steps, {:d} walkers, '.format(\n fittype, n_mcmc_steps, n_walkers\n ) + '{:d} threads'.format(nworkers)\n )\n\n reader = emcee.backends.HDFBackend(samplesavpath)\n\n n_to_discard = int(burninpercent*n_mcmc_steps)\n\n samples = reader.get_chain(discard=n_to_discard, flat=True)\n log_prob_samples = reader.get_log_prob(discard=n_to_discard, flat=True)\n log_prior_samples = reader.get_blobs(discard=n_to_discard, flat=True)\n\n # Get best-fit parameters and their 1-sigma error bars\n fit_statistics = list(\n map(lambda v: (v[1], v[2]-v[1], v[1]-v[0]),\n list(zip( *np.percentile(samples, [15.85, 50, 84.15], axis=0))))\n )\n\n medianparams, std_perrs, std_merrs = {}, {}, {}\n for ix, k in enumerate(np.sort(list(priorbounds.keys()))):\n medianparams[k] = fit_statistics[ix][0]\n std_perrs[k] = fit_statistics[ix][1]\n std_merrs[k] = fit_statistics[ix][2]\n\n stderrs = {'std_perrs':std_perrs, 'std_merrs':std_merrs}\n\n per = _get_value('period', medianparams, fixedparams)\n t0 = _get_value('t0', medianparams, fixedparams)\n rp = _get_value('rp', medianparams, fixedparams)\n sma = _get_value('sma', medianparams, fixedparams)\n incl = _get_value('incl', medianparams, fixedparams)\n ecc = _get_value('ecc', medianparams, fixedparams)\n omega = _get_value('omega', medianparams, fixedparams)\n limb_dark = _get_value('limb_dark', medianparams, fixedparams)\n try:\n u = fixedparams['u']\n except Exception as e:\n u = [medianparams['u_linear'], medianparams['u_quad']]\n\n fit_params, fit_m = _transit_model(stimes, t0, per, rp, sma, incl, ecc,\n omega, u, limb_dark)\n fitmags = fit_m.light_curve(fit_params)\n fepoch = t0\n\n # assemble the return dictionary\n returndict = {\n 'fittype':fittype,\n 'fitinfo':{\n 'initialparams':fitparams,\n 'initialmags':init_flux,\n 'fixedparams':fixedparams,\n 'finalparams':medianparams,\n 'finalparamerrs':stderrs,\n 'fitmags':fitmags,\n 'fitepoch':fepoch,\n },\n 'fitplotfile':None,\n 'magseries':{\n 'times':stimes,\n 'mags':smags,\n 'errs':serrs,\n 'magsarefluxes':magsarefluxes,\n },\n }\n\n # make the output corner plot, and lightcurve plot if desired\n if plotcorner:\n if isinstance(trueparams,dict):\n trueparamkeys = np.sort(list(trueparams.keys()))\n truelist = [trueparams[k] for k in trueparamkeys]\n fig = corner.corner(\n samples,\n labels=trueparamkeys,\n truths=truelist,\n quantiles=[0.1585, 0.5, .8415], show_titles=True\n )\n else:\n fig = corner.corner(samples,\n labels=fitparamnames,\n quantiles=[0.1585, 0.5, .8415],\n show_titles=True)\n\n plt.savefig(plotcorner, dpi=300)\n if verbose:\n LOGINFO('saved {:s}'.format(plotcorner))\n\n if plotfit and isinstance(plotfit, str):\n\n f, ax = plt.subplots(figsize=(8,6))\n ax.scatter(stimes, smags, c='k', alpha=0.5, label='observed',\n zorder=1, s=1.5, rasterized=True, linewidths=0)\n ax.scatter(stimes, init_flux, c='r', alpha=1,\n s=3.5, zorder=2, rasterized=True, linewidths=0,\n label='initial guess')\n ax.scatter(\n stimes, fitmags, c='b', alpha=1,\n s=1.5, zorder=3, rasterized=True, linewidths=0,\n label='fit {:d} dims'.format(\n len(fitparamnames))\n )\n ax.legend(loc='best')\n ax.set(xlabel='time [days]', ylabel='relative flux')\n f.savefig(plotfit, dpi=300, bbox_inches='tight')\n if verbose:\n LOGINFO('saved {:s}'.format(plotfit))\n\n returndict['fitplotfile'] = plotfit\n\n return returndict", "response": "Fit a Mandelagol model to a flux time - series."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef mandelagol_and_line_fit_magseries(\n times, mags, errs,\n fitparams,\n priorbounds,\n fixedparams,\n trueparams=None,\n burninpercent=0.3,\n plotcorner=False,\n timeoffset=0,\n samplesavpath=False,\n n_walkers=50,\n n_mcmc_steps=400,\n eps=1e-4,\n skipsampling=False,\n overwriteexistingsamples=False,\n mcmcprogressbar=False,\n plotfit=False,\n scatterxdata=None,\n scatteryaxes=None,\n magsarefluxes=True,\n sigclip=10.0,\n verbose=True,\n nworkers=4\n):\n '''The model fit by this function is: a Mandel & Agol (2002) transit, PLUS a\n line. You can fit and fix whatever parameters you want.\n\n A typical use case: you want to measure transit times of individual SNR >~\n 50 transits. You fix all the transit parameters except for the mid-time,\n and also fit for a line locally.\n\n NOTE: this only works for flux time-series at the moment.\n\n NOTE: Between the `fitparams`, `priorbounds`, and `fixedparams` dicts, you\n must specify all of the planetary transit parameters required by BATMAN and\n the parameters for the line fit: `['t0', 'rp', 'sma', 'incl', 'u', 'rp',\n 'ecc', 'omega', 'period', 'poly_order0', poly_order1']`, or the BATMAN model\n will fail to initialize.\n\n Parameters\n ----------\n\n times,mags,errs : np.array\n The input flux time-series to fit a Fourier cosine series to.\n\n fitparams : dict\n This is the initial parameter guesses for MCMC, found e.g., by\n BLS. The key string format must not be changed, but any parameter can be\n either \"fit\" or \"fixed\". If it is \"fit\", it must have a corresponding\n prior. For example::\n\n fitparams = {'t0':1325.9,\n 'poly_order0':1,\n 'poly_order1':0.}\n\n where `t0` is the time of transit-center for a reference transit.\n `poly_order0` corresponds to the intercept of the line, `poly_order1` is\n the slope.\n\n priorbounds : dict\n This sets the lower & upper bounds on uniform prior, e.g.::\n\n priorbounds = {'t0':(np.min(time), np.max(time)),\n 'poly_order0':(0.5,1.5),\n 'poly_order1':(-0.5,0.5) }\n\n fixedparams : dict\n This sets which parameters are fixed, and their values. For example::\n\n fixedparams = {'ecc':0.,\n 'omega':90.,\n 'limb_dark':'quadratic',\n 'period':fitd['period'],\n 'rp':np.sqrt(fitd['transitdepth']),\n 'sma':6.17, 'incl':85, 'u':[0.3, 0.2]}\n\n `limb_dark` must be \"quadratic\". It's \"fixed\", because once you\n choose your limb-darkening model, it's fixed.\n\n trueparams : list of floats\n The true parameter values you're fitting for, if they're known (e.g., a\n known planet, or fake data). Only for plotting purposes.\n\n burninpercent : float\n The percent of MCMC samples to discard as burn-in.\n\n plotcorner : str or False\n If this is a str, points to the path of output corner plot that will be\n generated for this MCMC run.\n\n timeoffset : float\n If input times are offset by some constant, and you want saved pickles\n to fix that.\n\n samplesavpath : str\n This must be provided so `emcee` can save its MCMC samples to disk as\n HDF5 files. This will set the path of the output HDF5file written.\n\n n_walkers : int\n The number of MCMC walkers to use.\n\n n_mcmc_steps : int\n The number of MCMC steps to take.\n\n eps : float\n The radius of the `n_walkers-dimensional` Gaussian ball used to\n initialize the MCMC.\n\n skipsampling : bool\n If you've already collected MCMC samples, and you do not want any more\n sampling (e.g., just make the plots), set this to be True.\n\n overwriteexistingsamples : bool\n If you've collected samples, but you want to overwrite them, set this to\n True. Usually, it should be False, which appends samples to\n `samplesavpath` HDF5 file.\n\n mcmcprogressbar : bool\n If True, will show a progress bar for the MCMC process.\n\n plotfit: str or bool\n If a str, indicates the path of the output fit plot file. If False, no\n fit plot will be made.\n\n scatterxdata : np.array or None\n Use this to overplot x,y scatter points on the output model/data\n lightcurve (e.g., to highlight bad data, or to indicate an ephemeris),\n this can take a `np.ndarray` with the same units as `times`.\n\n scatteryaxes : np.array or None\n Use this to provide the y-values for scatterxdata, in units of fraction\n of an axis.\n\n magsarefluxes : bool\n This indicates if the input measurements in `mags` are actually fluxes.\n\n sigclip : float or int or sequence of two floats/ints or None\n If a single float or int, a symmetric sigma-clip will be performed using\n the number provided as the sigma-multiplier to cut out from the input\n time-series.\n\n If a list of two ints/floats is provided, the function will perform an\n 'asymmetric' sigma-clip. The first element in this list is the sigma\n value to use for fainter flux/mag values; the second element in this\n list is the sigma value to use for brighter flux/mag values. For\n example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma\n dimmings and greater than 3-sigma brightenings. Here the meaning of\n \"dimming\" and \"brightening\" is set by *physics* (not the magnitude\n system), which is why the `magsarefluxes` kwarg must be correctly set.\n\n If `sigclip` is None, no sigma-clipping will be performed, and the\n time-series (with non-finite elems removed) will be passed through to\n the output.\n\n verbose : bool\n If True, will indicate MCMC progress.\n\n nworkers : int\n The number of parallel workers to launch for MCMC.\n\n Returns\n -------\n\n dict\n This function returns a dict containing the model fit parameters and\n other fit information. The form of this dict is mostly standardized\n across all functions in this module::\n\n {\n 'fittype':'mandelagol_and_line',\n 'fitinfo':{\n 'initialparams':the initial transit params provided,\n 'fixedparams':the fixed transit params provided,\n 'finalparams':the final model fit transit params,\n 'finalparamerrs':formal errors in the params,\n 'fitmags': the model fit mags,\n 'fitepoch': the epoch of minimum light for the fit,\n },\n 'fitplotfile': the output fit plot if fitplot is not None,\n 'magseries':{\n 'times':input times in phase order of the model,\n 'phase':the phases of the model mags,\n 'mags':input mags/fluxes in the phase order of the model,\n 'errs':errs in the phase order of the model,\n 'magsarefluxes':input value of magsarefluxes kwarg\n }\n }\n\n '''\n\n from multiprocessing import Pool\n fittype = 'mandelagol_and_line'\n\n if not magsarefluxes:\n raise NotImplementedError('magsarefluxes is not implemented yet.')\n if not samplesavpath:\n raise ValueError(\n 'This function requires that you save the samples somewhere'\n )\n if not mandel_agol_dependencies:\n raise ImportError(\n 'This function depends on BATMAN, emcee>3.0, corner, and h5py.'\n )\n\n # sigma clip and get rid of zero errs\n stimes, smags, serrs = sigclip_magseries(times, mags, errs,\n sigclip=sigclip,\n magsarefluxes=magsarefluxes)\n nzind = np.nonzero(serrs)\n stimes, smags, serrs = stimes[nzind], smags[nzind], serrs[nzind]\n\n init_period = _get_value('period', fitparams, fixedparams)\n init_epoch = _get_value('t0', fitparams, fixedparams)\n init_rp = _get_value('rp', fitparams, fixedparams)\n init_sma = _get_value('sma', fitparams, fixedparams)\n init_incl = _get_value('incl', fitparams, fixedparams)\n init_ecc = _get_value('ecc', fitparams, fixedparams)\n init_omega = _get_value('omega', fitparams, fixedparams)\n limb_dark = _get_value('limb_dark', fitparams, fixedparams)\n init_u = _get_value('u', fitparams, fixedparams)\n\n init_poly_order0 = _get_value('poly_order0', fitparams, fixedparams)\n init_poly_order1 = _get_value('poly_order1', fitparams, fixedparams)\n\n if not limb_dark == 'quadratic':\n raise ValueError(\n 'only quadratic limb-darkening is supported at the moment'\n )\n\n # initialize the model and calculate the initial model light-curve\n init_params, init_m = _transit_model(\n stimes, init_epoch, init_period, init_rp, init_sma, init_incl,\n init_ecc, init_omega, init_u, limb_dark)\n\n init_flux = (\n init_m.light_curve(init_params) +\n init_poly_order0 + init_poly_order1*stimes\n )\n\n # guessed initial params. give nice guesses, or else emcee struggles.\n theta, fitparamnames = [], []\n for k in np.sort(list(fitparams.keys())):\n if isinstance(fitparams[k], float) or isinstance(fitparams[k], int):\n theta.append(fitparams[k])\n fitparamnames.append(fitparams[k])\n elif isinstance(fitparams[k], list):\n if not len(fitparams[k]) == 2:\n raise ValueError('should only be quadratic LD coeffs')\n theta.append(fitparams[k][0])\n theta.append(fitparams[k][1])\n fitparamnames.append(fitparams[k][0])\n fitparamnames.append(fitparams[k][1])\n\n # initialize sampler\n n_dim = len(theta)\n\n # run the MCMC, unless you just want to load the available samples\n if not skipsampling:\n\n backend = emcee.backends.HDFBackend(samplesavpath)\n if overwriteexistingsamples:\n LOGWARNING(\n 'erased samples previously at {:s}'.format(samplesavpath)\n )\n backend.reset(n_walkers, n_dim)\n\n # if this is the first run, then start from a gaussian ball, centered\n # on the maximum likelihood solution. otherwise, resume from the\n # previous samples.\n def nll(*args):\n return -_log_likelihood_transit_plus_line(*args)\n\n soln = spminimize(\n nll, theta, method='BFGS',\n args=(init_params, init_m, stimes, smags, serrs, priorbounds)\n )\n theta_ml = soln.x\n ml_poly_order0 = theta_ml[0]\n ml_poly_order1 = theta_ml[1]\n ml_rp = theta_ml[2]\n ml_t0 = theta_ml[3]\n\n ml_params, ml_m = _transit_model(stimes, ml_t0, init_period,\n ml_rp, init_sma, init_incl,\n init_ecc, init_omega, init_u,\n limb_dark)\n ml_mags = (\n ml_m.light_curve(ml_params) +\n ml_poly_order0 + ml_poly_order1*stimes\n )\n\n initial_position_vec = [theta_ml + eps*np.random.randn(n_dim)\n for i in range(n_walkers)]\n starting_positions = initial_position_vec\n isfirstrun = True\n if os.path.exists(backend.filename):\n if backend.iteration > 1:\n starting_positions = None\n isfirstrun = False\n\n if verbose and isfirstrun:\n LOGINFO(\n 'start {:s} MCMC with {:d} dims, {:d} steps, {:d} walkers,'.\n format(fittype, n_dim, n_mcmc_steps, n_walkers) +\n ' {:d} threads'.format(nworkers)\n )\n elif verbose and not isfirstrun:\n LOGINFO(\n 'continue {:s} with {:d} dims, {:d} steps, {:d} walkers, '.\n format(fittype, n_dim, n_mcmc_steps, n_walkers) +\n '{:d} threads'.format(nworkers)\n )\n\n with Pool(nworkers) as pool:\n sampler = emcee.EnsembleSampler(\n n_walkers, n_dim, log_posterior_transit_plus_line,\n args=(init_params, init_m, stimes, smags, serrs, priorbounds),\n pool=pool,\n backend=backend\n )\n sampler.run_mcmc(starting_positions, n_mcmc_steps,\n progress=mcmcprogressbar)\n\n if verbose:\n LOGINFO(\n 'ended {:s} MCMC run with {:d} steps, {:d} walkers, '.format(\n fittype, n_mcmc_steps, n_walkers\n ) + '{:d} threads'.format(nworkers)\n )\n\n reader = emcee.backends.HDFBackend(samplesavpath)\n\n n_to_discard = int(burninpercent*n_mcmc_steps)\n\n samples = reader.get_chain(discard=n_to_discard, flat=True)\n log_prob_samples = reader.get_log_prob(discard=n_to_discard, flat=True)\n log_prior_samples = reader.get_blobs(discard=n_to_discard, flat=True)\n\n # Get best-fit parameters and their 1-sigma error bars\n fit_statistics = list(\n map(lambda v: (v[1], v[2]-v[1], v[1]-v[0]),\n list(zip( *np.percentile(samples, [15.85, 50, 84.15], axis=0))))\n )\n\n medianparams, std_perrs, std_merrs = {}, {}, {}\n for ix, k in enumerate(np.sort(list(priorbounds.keys()))):\n medianparams[k] = fit_statistics[ix][0]\n std_perrs[k] = fit_statistics[ix][1]\n std_merrs[k] = fit_statistics[ix][2]\n\n stderrs = {'std_perrs':std_perrs, 'std_merrs':std_merrs}\n\n per = _get_value('period', medianparams, fixedparams)\n t0 = _get_value('t0', medianparams, fixedparams)\n rp = _get_value('rp', medianparams, fixedparams)\n sma = _get_value('sma', medianparams, fixedparams)\n incl = _get_value('incl', medianparams, fixedparams)\n ecc = _get_value('ecc', medianparams, fixedparams)\n omega = _get_value('omega', medianparams, fixedparams)\n limb_dark = _get_value('limb_dark', medianparams, fixedparams)\n try:\n u = fixedparams['u']\n except Exception as e:\n u = [medianparams['u_linear'], medianparams['u_quad']]\n\n poly_order0 = _get_value('poly_order0', medianparams, fixedparams)\n poly_order1 = _get_value('poly_order1', medianparams, fixedparams)\n\n # initialize the model and calculate the initial model light-curve\n fit_params, fit_m = _transit_model(stimes, t0, per, rp, sma, incl, ecc,\n omega, u, limb_dark)\n fitmags = (\n fit_m.light_curve(fit_params) +\n poly_order0 + poly_order1*stimes\n )\n fepoch = t0\n\n # assemble the return dictionary\n medianparams['t0'] += timeoffset\n returndict = {\n 'fittype':fittype,\n 'fitinfo':{\n 'initialparams':fitparams,\n 'initialmags':init_flux,\n 'fixedparams':fixedparams,\n 'finalparams':medianparams,\n 'finalparamerrs':stderrs,\n 'fitmags':fitmags,\n 'fitepoch':fepoch+timeoffset,\n },\n 'fitplotfile':None,\n 'magseries':{\n 'times':stimes+timeoffset,\n 'mags':smags,\n 'errs':serrs,\n 'magsarefluxes':magsarefluxes,\n },\n }\n\n # make the output corner plot, and lightcurve plot if desired\n if plotcorner:\n fig = corner.corner(\n samples,\n labels=['line intercept-1', 'line slope',\n 'rp','t0-{:.4f}'.format(timeoffset)],\n truths=[ml_poly_order0, ml_poly_order1, ml_rp, ml_t0],\n quantiles=[0.1585, 0.5, .8415], show_titles=True\n )\n plt.savefig(plotcorner, dpi=300)\n if verbose:\n LOGINFO('saved {:s}'.format(plotcorner))\n\n if plotfit and isinstance(plotfit, str):\n\n plt.close('all')\n f, (a0, a1) = plt.subplots(nrows=2, ncols=1, sharex=True,\n figsize=(8,5),\n gridspec_kw={'height_ratios':[3, 1]})\n\n a0.scatter(stimes, smags, c='k', alpha=0.9, label='data', zorder=1,\n s=10, rasterized=True, linewidths=0)\n\n DEBUGGING = False\n if DEBUGGING:\n a0.scatter(stimes, init_flux, c='r', alpha=1, s=3.5, zorder=2,\n rasterized=True, linewidths=0,\n label='initial guess for ml')\n a0.scatter(stimes, ml_mags, c='g', alpha=1, s=3.5, zorder=2,\n rasterized=True, linewidths=0, label='max likelihood')\n\n a0.plot(\n stimes, fitmags, c='b',\n zorder=0, rasterized=True, lw=2, alpha=0.4,\n label='{:s} fit, {:d} dims'.format(fittype, len(fitparamnames))\n )\n\n a1.scatter(\n stimes, smags-fitmags, c='k', alpha=0.9,\n rasterized=True, s=10, linewidths=0\n )\n\n if scatterxdata and scatteryaxes:\n import matplotlib.transforms as transforms\n for a in [a0, a1]:\n transform = transforms.blended_transform_factory(\n a.transData, a.transAxes\n )\n a.scatter(scatterxdata, scatteryaxes, c='r', alpha=0.9,\n zorder=2, s=10, rasterized=True, linewidths=0,\n marker=\"^\", transform=transform)\n\n\n a1.set_xlabel('time-t0 [days]')\n a0.set_ylabel('relative flux')\n a1.set_ylabel('residual')\n a0.legend(loc='best', fontsize='x-small')\n for a in [a0, a1]:\n a.get_yaxis().set_tick_params(which='both', direction='in')\n a.get_xaxis().set_tick_params(which='both', direction='in')\n\n f.tight_layout(h_pad=0, w_pad=0)\n f.savefig(plotfit, dpi=300, bbox_inches='tight')\n if verbose:\n LOGINFO('saved {:s}'.format(plotfit))\n\n returndict['fitplotfile'] = plotfit\n\n return returndict", "response": "This function is used to fit a Mandelagol time - series to a line."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef list_trilegal_filtersystems():\n '''\n This just lists all the filter systems available for TRILEGAL.\n\n '''\n\n print('%-40s %s' % ('FILTER SYSTEM NAME','DESCRIPTION'))\n print('%-40s %s' % ('------------------','-----------'))\n for key in sorted(TRILEGAL_FILTER_SYSTEMS.keys()):\n print('%-40s %s' % (key, TRILEGAL_FILTER_SYSTEMS[key]['desc']))", "response": "This function lists all the filter systems available for TRILEGAL."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef read_model_table(modelfile):\n '''\n This reads a downloaded TRILEGAL model file.\n\n Parameters\n ----------\n\n modelfile : str\n Path to the downloaded model file to read.\n\n Returns\n -------\n\n np.recarray\n Returns the model table as a Numpy record array.\n\n '''\n\n infd = gzip.open(modelfile)\n model = np.genfromtxt(infd,names=True)\n infd.close()\n\n return model", "response": "Reads a downloaded TRILEGAL model file and returns a Numpy record array."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _time_independent_equals(a, b):\n '''\n This compares two values in constant time.\n\n Taken from tornado:\n\n https://github.com/tornadoweb/tornado/blob/\n d4eb8eb4eb5cc9a6677e9116ef84ded8efba8859/tornado/web.py#L3060\n\n '''\n if len(a) != len(b):\n return False\n result = 0\n if isinstance(a[0], int): # python3 byte strings\n for x, y in zip(a, b):\n result |= x ^ y\n else: # python2\n for x, y in zip(a, b):\n result |= ord(x) ^ ord(y)\n return result == 0", "response": "Compare two values in constant time."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef default(self, obj):\n '''Overrides the default serializer for `JSONEncoder`.\n\n This can serialize the following objects in addition to what\n `JSONEncoder` can already do.\n\n - `np.array`\n - `bytes`\n - `complex`\n - `np.float64` and other `np.dtype` objects\n\n Parameters\n ----------\n\n obj : object\n A Python object to serialize to JSON.\n\n Returns\n -------\n\n str\n A JSON encoded representation of the input object.\n\n '''\n\n if isinstance(obj, np.ndarray):\n return obj.tolist()\n elif isinstance(obj, bytes):\n return obj.decode()\n elif isinstance(obj, complex):\n return (obj.real, obj.imag)\n elif (isinstance(obj, (float, np.float64, np.float_)) and\n not np.isfinite(obj)):\n return None\n elif isinstance(obj, (np.int8, np.int16, np.int32, np.int64)):\n return int(obj)\n else:\n return json.JSONEncoder.default(self, obj)", "response": "Overrides the default serializer for JSONEncoder."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef initialize(self, currentdir, assetpath, cplist,\n cplistfile, executor, readonly, baseurl):\n '''\n handles initial setup.\n\n '''\n\n self.currentdir = currentdir\n self.assetpath = assetpath\n self.currentproject = cplist\n self.cplistfile = cplistfile\n self.executor = executor\n self.readonly = readonly\n self.baseurl = baseurl", "response": "handles initial setup. cplist is a list of dicts containing the key - value pairs for the current directory assetpath cplistfile executor readonly baseurl is a string indicating whether the request should be permanently readonly or not."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get(self):\n '''\n This handles GET requests for the current checkplot-list.json file.\n\n Used with AJAX from frontend.\n\n '''\n\n # add the reviewed key to the current dict if it doesn't exist\n # this will hold all the reviewed objects for the frontend\n if 'reviewed' not in self.currentproject:\n self.currentproject['reviewed'] = {}\n\n # just returns the current project as JSON\n self.write(self.currentproject)", "response": "This handles GET requests for the current checkplot - list. json file. It will return the current project as JSON and the reviewed key will be set to reviewed."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef post(self):\n '''This handles POST requests.\n\n Saves the changes made by the user on the frontend back to the current\n checkplot-list.json file.\n\n '''\n\n # if self.readonly is set, then don't accept any changes\n # return immediately with a 400\n if self.readonly:\n\n msg = \"checkplotserver is in readonly mode. no updates allowed.\"\n resultdict = {'status':'error',\n 'message':msg,\n 'readonly':self.readonly,\n 'result':None}\n\n self.write(resultdict)\n raise tornado.web.Finish()\n\n\n objectid = self.get_argument('objectid', None)\n changes = self.get_argument('changes',None)\n\n # if either of the above is invalid, return nothing\n if not objectid or not changes:\n\n msg = (\"could not parse changes to the checkplot filelist \"\n \"from the frontend\")\n LOGGER.error(msg)\n resultdict = {'status':'error',\n 'message':msg,\n 'readonly':self.readonly,\n 'result':None}\n\n self.write(resultdict)\n raise tornado.web.Finish()\n\n\n # otherwise, update the checkplot list JSON\n objectid = xhtml_escape(objectid)\n changes = json.loads(changes)\n\n # update the dictionary\n if 'reviewed' not in self.currentproject:\n self.currentproject['reviewed'] = {}\n\n self.currentproject['reviewed'][objectid] = changes\n\n # update the JSON file\n with open(self.cplistfile,'w') as outfd:\n json.dump(self.currentproject, outfd)\n\n # return status\n msg = (\"wrote all changes to the checkplot filelist \"\n \"from the frontend for object: %s\" % objectid)\n LOGGER.info(msg)\n resultdict = {'status':'success',\n 'message':msg,\n 'readonly':self.readonly,\n 'result':{'objectid':objectid,\n 'changes':changes}}\n\n self.write(resultdict)\n self.finish()", "response": "This handles the POST requests."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get(self, cpfile):\n '''This handles a GET request to run a specified LC tool.\n\n Parameters\n ----------\n\n cpfile : str\n This is the checkplot file to run the tool on.\n\n Returns\n -------\n\n str\n Returns a JSON response.\n\n Notes\n -----\n\n The URI structure is::\n\n /tools/?[args]\n\n where args are::\n\n ?lctool=&argkey1=argval1&argkey2=argval2&...\n\n &forcereload=true <- if this is present, then reload values from\n original checkplot.\n\n &objectid=\n\n `lctool` is one of the strings below\n\n Period search functions::\n\n psearch-gls: run Lomb-Scargle with given params\n psearch-bls: run BLS with given params\n psearch-pdm: run phase dispersion minimization with given params\n psearch-aov: run analysis-of-variance with given params\n psearch-mav: run analysis-of-variance (multi-harm) with given params\n psearch-acf: run ACF period search with given params\n psearch-win: run spectral window function search with given params\n\n Arguments recognized by all period-search functions are::\n\n startp=XX\n endp=XX\n magsarefluxes=True|False\n autofreq=True|False\n stepsize=XX\n\n Variability characterization functions::\n\n var-varfeatures: gets the variability features from the checkplot or\n recalculates if they're not present\n\n var-prewhiten: pre-whitens the light curve with a sinusoidal signal\n\n var-masksig: masks a given phase location with given width from the\n light curve\n\n Light curve manipulation functions ::\n\n phasedlc-newplot: make phased LC with new provided period/epoch\n lcfit-fourier: fit a Fourier function to the phased LC\n lcfit-spline: fit a spline function to the phased LC\n lcfit-legendre: fit a Legendre polynomial to the phased LC\n lcfit-savgol: fit a Savitsky-Golay polynomial to the phased LC\n\n FIXME: figure out how to cache the results of these functions\n temporarily and save them back to the checkplot after we click on save\n in the frontend.\n\n TODO: look for a checkplot-blah-blah.pkl-cps-processing file in the same\n place as the usual pickle file. if this exists and is newer than the pkl\n file, load it instead. Or have a checkplotdict['cpservertemp'] item.\n\n '''\n\n if cpfile:\n\n self.cpfile = (\n xhtml_escape(base64.b64decode(url_unescape(cpfile)))\n )\n\n # see if this plot is in the current project\n if self.cpfile in self.currentproject['checkplots']:\n\n # make sure this file exists\n cpfpath = os.path.join(\n os.path.abspath(os.path.dirname(self.cplistfile)),\n self.cpfile\n )\n\n # if we can't find the pickle, quit immediately\n if not os.path.exists(cpfpath):\n\n msg = \"couldn't find checkplot %s\" % cpfpath\n LOGGER.error(msg)\n resultdict = {'status':'error',\n 'message':msg,\n 'readonly':self.readonly,\n 'result':None}\n\n self.write(resultdict)\n raise tornado.web.Finish()\n\n ###########################\n # now parse the arguments #\n ###########################\n\n # check if we have to force-reload\n forcereload = self.get_argument('forcereload',False)\n if forcereload and xhtml_escape(forcereload):\n forcereload = True if forcereload == 'true' else False\n\n # get the objectid\n cpobjectid = self.get_argument('objectid',None)\n\n # get the light curve tool to use\n lctool = self.get_argument('lctool', None)\n\n # preemptive dict to fill out\n resultdict = {'status':None,\n 'message':None,\n 'readonly':self.readonly,\n 'result':None}\n\n # check if the lctool arg is provided\n if lctool:\n\n lctool = xhtml_escape(lctool)\n lctoolargs = []\n lctoolkwargs = {}\n\n # check if this lctool is OK and has all the required args\n if lctool in CPTOOLMAP:\n\n try:\n\n # all args should have been parsed\n # successfully. parse the kwargs now\n for xkwarg, xkwargtype, xkwargdef in zip(\n CPTOOLMAP[lctool]['kwargs'],\n CPTOOLMAP[lctool]['kwargtypes'],\n CPTOOLMAP[lctool]['kwargdefs']\n ):\n\n # get the kwarg\n if xkwargtype is list:\n wbkwarg = self.get_arguments(xkwarg)\n if len(wbkwarg) > 0:\n wbkwarg = [url_unescape(xhtml_escape(x))\n for x in wbkwarg]\n else:\n wbkwarg = None\n\n else:\n wbkwarg = self.get_argument(xkwarg, None)\n if wbkwarg is not None:\n wbkwarg = url_unescape(\n xhtml_escape(wbkwarg)\n )\n\n LOGGER.info('xkwarg = %s, wbkwarg = %s' %\n (xkwarg, repr(wbkwarg)))\n\n # if it's None, sub with the default\n if wbkwarg is None:\n\n wbkwarg = xkwargdef\n\n # otherwise, cast it to the required type\n else:\n\n # special handling for lists of floats\n if xkwargtype is list:\n wbkwarg = [float(x) for x in wbkwarg]\n\n # special handling for booleans\n elif xkwargtype is bool:\n\n if wbkwarg == 'false':\n wbkwarg = False\n elif wbkwarg == 'true':\n wbkwarg = True\n else:\n wbkwarg = xkwargdef\n\n # usual casting for other types\n else:\n\n wbkwarg = xkwargtype(wbkwarg)\n\n # update the lctools kwarg dict\n\n # make sure to remove any [] from the kwargs\n # this was needed to parse the input query\n # string correctly\n if xkwarg.endswith('[]'):\n xkwarg = xkwarg.rstrip('[]')\n\n lctoolkwargs.update({xkwarg:wbkwarg})\n\n except Exception as e:\n\n LOGGER.exception('lctool %s, kwarg %s '\n 'will not work' %\n (lctool, xkwarg))\n resultdict['status'] = 'error'\n resultdict['message'] = (\n 'lctool %s, kwarg %s '\n 'will not work' %\n (lctool, xkwarg)\n )\n resultdict['result'] = {'objectid':cpobjectid}\n\n self.write(resultdict)\n raise tornado.web.Finish()\n\n # if the tool is not in the CPTOOLSMAP\n else:\n LOGGER.error('lctool %s, does not exist' % lctool)\n resultdict['status'] = 'error'\n resultdict['message'] = (\n 'lctool %s does not exist' % lctool\n )\n resultdict['result'] = {'objectid':cpobjectid}\n\n self.write(resultdict)\n raise tornado.web.Finish()\n\n # if no lctool arg is provided\n else:\n\n LOGGER.error('lctool argument not provided')\n resultdict['status'] = 'error'\n resultdict['message'] = (\n 'lctool argument not provided'\n )\n resultdict['result'] = {'objectid':cpobjectid}\n\n self.write(resultdict)\n raise tornado.web.Finish()\n\n\n ##############################################\n ## NOW WE'RE READY TO ACTUALLY DO SOMETHING ##\n ##############################################\n\n LOGGER.info('loading %s...' % cpfpath)\n\n # this loads the actual checkplot pickle\n cpdict = yield self.executor.submit(\n _read_checkplot_picklefile, cpfpath\n )\n\n # we check for the existence of a cpfpath + '-cpserver-temp'\n # file first. this is where we store stuff before we write it\n # back to the actual checkplot.\n tempfpath = cpfpath + '-cpserver-temp'\n\n # load the temp checkplot if it exists\n if os.path.exists(tempfpath):\n\n tempcpdict = yield self.executor.submit(\n _read_checkplot_picklefile, tempfpath\n )\n\n # if it doesn't exist, read the times, mags, errs from the\n # actual checkplot in prep for working on it\n else:\n\n tempcpdict = {\n 'objectid':cpdict['objectid'],\n 'magseries':{\n 'times':cpdict['magseries']['times'],\n 'mags':cpdict['magseries']['mags'],\n 'errs':cpdict['magseries']['errs'],\n }\n }\n\n\n # if we're not forcing a rerun from the original checkplot dict\n if not forcereload:\n\n cptimes, cpmags, cperrs = (\n tempcpdict['magseries']['times'],\n tempcpdict['magseries']['mags'],\n tempcpdict['magseries']['errs'],\n )\n LOGGER.info('forcereload = False')\n\n # otherwise, reload the original times, mags, errs\n else:\n\n cptimes, cpmags, cperrs = (cpdict['magseries']['times'],\n cpdict['magseries']['mags'],\n cpdict['magseries']['errs'])\n LOGGER.info('forcereload = True')\n\n\n # collect the args\n for xarg, xargtype in zip(CPTOOLMAP[lctool]['args'],\n CPTOOLMAP[lctool]['argtypes']):\n\n # handle special args\n if xarg is None:\n lctoolargs.append(None)\n elif xarg == 'times':\n lctoolargs.append(cptimes)\n elif xarg == 'mags':\n lctoolargs.append(cpmags)\n elif xarg == 'errs':\n lctoolargs.append(cperrs)\n\n # handle other args\n else:\n\n try:\n\n if xargtype is list:\n\n wbarg = self.get_arguments(xarg)\n\n else:\n\n wbarg = url_unescape(\n xhtml_escape(\n self.get_argument(xarg, None)\n )\n )\n\n # cast the arg to the required type\n\n # special handling for lists\n if xargtype is list:\n wbarg = [float(x) for x in wbarg]\n # special handling for epochs that can be optional\n elif xargtype is float and xarg == 'varepoch':\n try:\n wbarg = xargtype(wbarg)\n except Exception as e:\n wbarg = None\n # usual casting for other types\n else:\n wbarg = xargtype(wbarg)\n\n lctoolargs.append(wbarg)\n\n except Exception as e:\n\n LOGGER.exception('lctool %s, arg %s '\n 'will not work' %\n (lctool, xarg))\n resultdict['status'] = 'error'\n resultdict['message'] = (\n 'lctool %s, arg %s '\n 'will not work' %\n (lctool, xarg)\n )\n resultdict['result'] = {'objectid':cpobjectid}\n\n self.write(resultdict)\n raise tornado.web.Finish()\n\n\n LOGGER.info(lctool)\n LOGGER.info(lctoolargs)\n LOGGER.info(lctoolkwargs)\n\n ############################\n ## handle the lctools now ##\n ############################\n\n # make sure the results aren't there already.\n # if they are and force-reload is not True,\n # just return them instead.\n resloc = CPTOOLMAP[lctool]['resloc']\n\n # TODO: figure out a way to make the dispatched tasks\n # cancellable. This can probably be done by having a global\n # TOOLQUEUE object that gets imported on initialize(). In this\n # object, we could put in key:vals like so:\n #\n # TOOLQUEUE['lctool--cpfpath'] = (\n # yield self.executor.submit(blah, *blah_args, **blah_kwargs)\n # )\n #\n # then we probably need some sort of frontend AJAX call that\n # enqueues things and can then cancel stuff from the queue. see\n # stuff we need to figure out:\n # - if the above scheme actually yields so we remain async\n # - if the Future object supports cancellation\n # - if the Future object that isn't resolved actually works\n\n\n # get the objectid. we'll send this along with every\n # result. this should handle the case of the current objectid\n # not being the same as the objectid being looked at by the\n # user. in effect, this will allow the user to launch a\n # long-running process and come back to it later since the\n # frontend will load the older results when they are complete.\n objectid = cpdict['objectid']\n\n # if lctool is a periodogram method\n if lctool in ('psearch-gls',\n 'psearch-bls',\n 'psearch-pdm',\n 'psearch-aov',\n 'psearch-mav',\n 'psearch-acf',\n 'psearch-win'):\n\n lspmethod = resloc[0]\n\n # if we can return the results from a previous run\n if (lspmethod in tempcpdict and\n isinstance(tempcpdict[lspmethod], dict) and\n (not forcereload)):\n\n # for a periodogram method, we need the\n # following items\n bestperiod = (\n tempcpdict[lspmethod]['bestperiod']\n )\n nbestperiods = (\n tempcpdict[lspmethod]['nbestperiods']\n )\n nbestlspvals = (\n tempcpdict[lspmethod]['nbestlspvals']\n )\n periodogram = (\n tempcpdict[lspmethod]['periodogram']\n )\n\n # get the first phased LC plot and its period\n # and epoch\n phasedlc0plot = (\n tempcpdict[lspmethod][0]['plot']\n )\n phasedlc0period = float(\n tempcpdict[lspmethod][0]['period']\n )\n phasedlc0epoch = float(\n tempcpdict[lspmethod][0]['epoch']\n )\n\n LOGGER.warning(\n 'returning previously unsaved '\n 'results for lctool %s from %s' %\n (lctool, tempfpath)\n )\n\n #\n # assemble the returndict\n #\n\n resultdict['status'] = 'warning'\n resultdict['message'] = (\n 'previous '\n 'unsaved results from %s' %\n lctool\n )\n resultdict['result'] = {\n 'objectid':objectid,\n lspmethod:{\n 'nbestperiods':nbestperiods,\n 'periodogram':periodogram,\n 'bestperiod':bestperiod,\n 'nbestpeaks':nbestlspvals,\n 'phasedlc0':{\n 'plot':phasedlc0plot,\n 'period':phasedlc0period,\n 'epoch':phasedlc0epoch,\n }\n }\n }\n\n self.write(resultdict)\n self.finish()\n\n # otherwise, we have to rerun the periodogram method\n else:\n\n # see if sigclip is set. if so, then do the sigclip on\n # the times, mags, errs\n if lctoolkwargs['sigclip'] is not None:\n\n wtimes, wmags, werrs = lcmath.sigclip_magseries(\n lctoolargs[0],\n lctoolargs[1],\n lctoolargs[2],\n sigclip=lctoolkwargs['sigclip'],\n magsarefluxes=lctoolkwargs['magsarefluxes']\n )\n\n lctoolargs[0] = wtimes\n lctoolargs[1] = wmags\n lctoolargs[2] = werrs\n\n #\n # process the LC filters now\n #\n\n # see if the lctimefilters are set\n if lctoolkwargs['lctimefilters']:\n\n wtimes, wmags, werrs = (lctoolargs[0],\n lctoolargs[1],\n lctoolargs[2])\n filtermasks = [\n np.full_like(wtimes, False, dtype=np.bool_)\n ]\n\n # parse the time filter strings\n filterstr = lctoolkwargs['lctimefilters']\n\n filters = filterstr.split(',')\n filters = [\n x.strip().lstrip('(').rstrip(')').strip()\n for x in filters\n ]\n\n for filt in filters:\n\n try:\n\n thisfilt = filt.split(':')\n if len(thisfilt) == 2:\n\n filt_lo = float(thisfilt[0])\n filt_hi = float(thisfilt[1])\n\n filtermasks.append(\n ((wtimes -\n cptimes.min()) < filt_hi) &\n ((wtimes -\n cptimes.min()) > filt_lo)\n )\n\n elif (len(thisfilt) == 3 and\n thisfilt[0].strip() == 'not'):\n\n\n filt_lo = float(thisfilt[1])\n filt_hi = float(thisfilt[2])\n\n filtermasks.append(np.logical_not(\n (((wtimes -\n cptimes.min()) < filt_hi) &\n ((wtimes -\n cptimes.min()) > filt_lo))\n ))\n\n else:\n continue\n\n except Exception as e:\n continue\n\n # finally, apply the filters if applicable\n if len(filtermasks) > 0:\n\n # apply the filters using an OR\n filterind = np.column_stack(filtermasks)\n filterind = np.any(filterind, axis=1)\n\n lctoolargs[0] = wtimes[filterind]\n lctoolargs[1] = wmags[filterind]\n lctoolargs[2] = werrs[filterind]\n\n\n # see if the lcmagfilters are set\n if lctoolkwargs['lcmagfilters']:\n\n wtimes, wmags, werrs = (lctoolargs[0],\n lctoolargs[1],\n lctoolargs[2])\n filtermasks = [\n np.full_like(wtimes, False, dtype=np.bool_)\n ]\n\n # parse the time filter strings\n filterstr = lctoolkwargs['lcmagfilters']\n\n filters = filterstr.split(',')\n filters = [\n x.strip().strip()\n for x in filters\n ]\n\n for filt in filters:\n\n try:\n\n thisfilt = filt.split(':')\n if len(thisfilt) == 2:\n\n filt_lo = float(thisfilt[0])\n filt_hi = float(thisfilt[1])\n\n filtermasks.append(\n (wmags < filt_hi) &\n (wmags > filt_lo)\n )\n\n elif (len(thisfilt) == 3 and\n thisfilt[0].strip() == 'not'):\n\n\n filt_lo = float(thisfilt[1])\n filt_hi = float(thisfilt[2])\n\n filtermasks.append(np.logical_not(\n ((wmags < filt_hi) &\n (wmags > filt_lo))\n ))\n\n else:\n continue\n\n except Exception as e:\n continue\n\n # finally, apply the filters if applicable\n if len(filtermasks) > 0:\n\n # apply the filters using an OR\n filterind = np.column_stack(filtermasks)\n filterind = np.any(filterind, axis=1)\n\n lctoolargs[0] = wtimes[filterind]\n lctoolargs[1] = wmags[filterind]\n lctoolargs[2] = werrs[filterind]\n\n # at the end of processing, remove from lctookwargs\n # since the pfmethod doesn't know about this\n del lctoolkwargs['lctimefilters']\n del lctoolkwargs['lcmagfilters']\n\n #\n # now run the period finder and get results\n #\n\n lctoolfunction = CPTOOLMAP[lctool]['func']\n\n # run the period finder\n funcresults = yield self.executor.submit(\n lctoolfunction,\n *lctoolargs,\n **lctoolkwargs\n )\n\n # get what we need out of funcresults when it\n # returns.\n nbestperiods = funcresults['nbestperiods']\n nbestlspvals = funcresults['nbestlspvals']\n bestperiod = funcresults['bestperiod']\n\n # generate the periodogram png\n pgramres = yield self.executor.submit(\n _pkl_periodogram,\n funcresults,\n )\n\n # generate the phased LCs. we show these in the frontend\n # along with the periodogram.\n\n\n phasedlcargs0 = (None,\n lspmethod,\n -1,\n lctoolargs[0],\n lctoolargs[1],\n lctoolargs[2],\n nbestperiods[0],\n 'min')\n\n if len(nbestperiods) > 1:\n phasedlcargs1 = (None,\n lspmethod,\n -1,\n lctoolargs[0],\n lctoolargs[1],\n lctoolargs[2],\n nbestperiods[1],\n 'min')\n else:\n phasedlcargs1 = None\n\n\n if len(nbestperiods) > 2:\n phasedlcargs2 = (None,\n lspmethod,\n -1,\n lctoolargs[0],\n lctoolargs[1],\n lctoolargs[2],\n nbestperiods[2],\n 'min')\n else:\n phasedlcargs2 = None\n\n # here, we set a bestperiodhighlight to distinguish this\n # plot from the ones existing in the checkplot already\n phasedlckwargs = {\n 'xliminsetmode':False,\n 'magsarefluxes':lctoolkwargs['magsarefluxes'],\n 'bestperiodhighlight':'#defa75',\n }\n\n # dispatch the plot functions\n phasedlc0 = yield self.executor.submit(\n _pkl_phased_magseries_plot,\n *phasedlcargs0,\n **phasedlckwargs\n )\n\n if phasedlcargs1 is not None:\n phasedlc1 = yield self.executor.submit(\n _pkl_phased_magseries_plot,\n *phasedlcargs1,\n **phasedlckwargs\n )\n else:\n phasedlc1 = None\n\n if phasedlcargs2 is not None:\n phasedlc2 = yield self.executor.submit(\n _pkl_phased_magseries_plot,\n *phasedlcargs2,\n **phasedlckwargs\n )\n else:\n phasedlc2 = None\n\n\n # save these to the tempcpdict\n # save the pickle only if readonly is not true\n if not self.readonly:\n\n tempcpdict[lspmethod] = {\n 'periods':funcresults['periods'],\n 'lspvals':funcresults['lspvals'],\n 'bestperiod':funcresults['bestperiod'],\n 'nbestperiods':funcresults['nbestperiods'],\n 'nbestlspvals':funcresults['nbestlspvals'],\n 'periodogram':(\n pgramres[lspmethod]['periodogram']\n ),\n 0:phasedlc0,\n }\n\n if phasedlc1 is not None:\n tempcpdict[lspmethod][1] = phasedlc1\n\n if phasedlc2 is not None:\n tempcpdict[lspmethod][2] = phasedlc2\n\n\n savekwargs = {\n 'outfile':tempfpath,\n 'protocol':pickle.HIGHEST_PROTOCOL\n }\n savedcpf = yield self.executor.submit(\n _write_checkplot_picklefile,\n tempcpdict,\n **savekwargs\n )\n\n LOGGER.info(\n 'saved temp results from '\n '%s to checkplot: %s' %\n (lctool, savedcpf)\n )\n\n else:\n\n LOGGER.warning(\n 'not saving temp results to checkplot '\n ' because readonly = True'\n )\n\n #\n # assemble the return dict\n #\n\n # the periodogram\n periodogram = pgramres[lspmethod]['periodogram']\n\n # phasedlc plot, period, and epoch for best 3 peaks\n phasedlc0plot = phasedlc0['plot']\n phasedlc0period = float(phasedlc0['period'])\n phasedlc0epoch = float(phasedlc0['epoch'])\n\n if phasedlc1 is not None:\n\n phasedlc1plot = phasedlc1['plot']\n phasedlc1period = float(phasedlc1['period'])\n phasedlc1epoch = float(phasedlc1['epoch'])\n\n if phasedlc2 is not None:\n\n phasedlc2plot = phasedlc2['plot']\n phasedlc2period = float(phasedlc2['period'])\n phasedlc2epoch = float(phasedlc2['epoch'])\n\n resultdict['status'] = 'success'\n resultdict['message'] = (\n 'new results for %s' %\n lctool\n )\n resultdict['result'] = {\n 'objectid':objectid,\n lspmethod:{\n 'nbestperiods':nbestperiods,\n 'nbestpeaks':nbestlspvals,\n 'periodogram':periodogram,\n 'bestperiod':bestperiod,\n 'phasedlc0':{\n 'plot':phasedlc0plot,\n 'period':phasedlc0period,\n 'epoch':phasedlc0epoch,\n },\n }\n }\n\n if phasedlc1 is not None:\n resultdict['result'][lspmethod]['phasedlc1'] = {\n 'plot':phasedlc1plot,\n 'period':phasedlc1period,\n 'epoch':phasedlc1epoch,\n }\n\n if phasedlc2 is not None:\n resultdict['result'][lspmethod]['phasedlc2'] = {\n 'plot':phasedlc2plot,\n 'period':phasedlc2period,\n 'epoch':phasedlc2epoch,\n }\n\n\n # return to frontend\n self.write(resultdict)\n self.finish()\n\n\n # if the lctool is a call to the phased LC plot itself\n # this requires lots of parameters\n # these should all be present in the frontend\n elif lctool == 'phasedlc-newplot':\n\n lspmethod = lctoolargs[1]\n periodind = lctoolargs[2]\n\n # if we can return the results from a previous run\n if (not forcereload and lspmethod in tempcpdict and\n isinstance(tempcpdict[lspmethod], dict) and\n periodind in tempcpdict[lspmethod] and\n isinstance(tempcpdict[lspmethod][periodind], dict)):\n\n # we get phased LC at periodind from a previous run\n phasedlc = tempcpdict[lspmethod][periodind]\n\n LOGGER.warning(\n 'returning previously unsaved '\n 'results for lctool %s from %s' %\n (lctool, tempfpath)\n )\n\n #\n # assemble the returndict\n #\n\n resultdict['status'] = 'warning'\n resultdict['message'] = (\n 'previous '\n 'unsaved results from %s' %\n lctool\n )\n retkey = 'phasedlc%s' % periodind\n resultdict['result'] = {\n 'objectid':objectid,\n lspmethod:{\n retkey:phasedlc\n }\n }\n\n self.write(resultdict)\n self.finish()\n\n # otherwise, we need to dispatch the function\n else:\n\n # add the highlight to distinguish this plot from usual\n # checkplot plots\n # full disclosure: http://c0ffee.surge.sh/\n lctoolkwargs['bestperiodhighlight'] = '#defa75'\n\n # set the input periodind to -1 to make sure we still\n # have the highlight on the plot. we use the correct\n # periodind when returning\n lctoolargs[2] = -1\n\n # see if sigclip is set. if so, then do the sigclip on\n # the times, mags, errs\n if lctoolkwargs['sigclip'] is not None:\n stimes, smags, serrs = lcmath.sigclip_magseries(\n lctoolargs[3],\n lctoolargs[4],\n lctoolargs[5],\n sigclip=lctoolkwargs['sigclip'],\n magsarefluxes=lctoolkwargs['magsarefluxes']\n )\n else:\n stimes, smags, serrs = (lctoolargs[3],\n lctoolargs[4],\n lctoolargs[5])\n\n\n #\n # process the LC filters now\n #\n\n # see if the lctimefilters are set\n if lctoolkwargs['lctimefilters']:\n\n wtimes, wmags, werrs = stimes, smags, serrs\n\n filtermasks = [\n np.full_like(wtimes, False, dtype=np.bool_)\n ]\n\n # parse the time filter strings\n filterstr = lctoolkwargs['lctimefilters']\n\n filters = filterstr.split(',')\n filters = [\n x.strip().lstrip('(').rstrip(')').strip()\n for x in filters\n ]\n\n for filt in filters:\n\n try:\n\n thisfilt = filt.split(':')\n if len(thisfilt) == 2:\n\n filt_lo = float(thisfilt[0])\n filt_hi = float(thisfilt[1])\n\n filtermasks.append(\n ((wtimes -\n cptimes.min()) < filt_hi) &\n ((wtimes -\n cptimes.min()) > filt_lo)\n )\n\n elif (len(thisfilt) == 3 and\n thisfilt[0].strip() == 'not'):\n\n\n filt_lo = float(thisfilt[1])\n filt_hi = float(thisfilt[2])\n\n filtermasks.append(np.logical_not(\n (((wtimes -\n cptimes.min()) < filt_hi) &\n ((wtimes -\n cptimes.min()) > filt_lo))\n ))\n\n else:\n continue\n\n except Exception as e:\n continue\n\n # finally, apply the filters if applicable\n if len(filtermasks) > 0:\n\n # apply the filters using an OR\n filterind = np.column_stack(filtermasks)\n filterind = np.any(filterind, axis=1)\n\n stimes = wtimes[filterind]\n smags = wmags[filterind]\n serrs = werrs[filterind]\n\n\n # see if the lcmagfilters are set\n if lctoolkwargs['lcmagfilters']:\n\n wtimes, wmags, werrs = stimes, smags, serrs\n filtermasks = [\n np.full_like(wtimes, False, dtype=np.bool_)\n ]\n\n # parse the time filter strings\n filterstr = lctoolkwargs['lcmagfilters']\n\n filters = filterstr.split(',')\n filters = [\n x.strip().strip()\n for x in filters\n ]\n\n for filt in filters:\n\n try:\n\n thisfilt = filt.split(':')\n if len(thisfilt) == 2:\n\n filt_lo = float(thisfilt[0])\n filt_hi = float(thisfilt[1])\n\n filtermasks.append(\n (wmags < filt_hi) &\n (wmags > filt_lo)\n )\n\n elif (len(thisfilt) == 3 and\n thisfilt[0].strip() == 'not'):\n\n\n filt_lo = float(thisfilt[1])\n filt_hi = float(thisfilt[2])\n\n filtermasks.append(np.logical_not(\n ((wmags < filt_hi) &\n (wmags > filt_lo))\n ))\n\n else:\n continue\n\n except Exception as e:\n continue\n\n # finally, apply the filters if applicable\n if len(filtermasks) > 0:\n\n # apply the filters using an OR\n filterind = np.column_stack(filtermasks)\n filterind = np.any(filterind, axis=1)\n\n stimes = wtimes[filterind]\n smags = wmags[filterind]\n serrs = werrs[filterind]\n\n # at the end of processing, remove from lctookwargs\n # since the pfmethod doesn't know about this\n del lctoolkwargs['lctimefilters']\n del lctoolkwargs['lcmagfilters']\n\n\n # if the varepoch is set to None, try to get the\n # minimum-light epoch using a spline fit\n if lctoolargs[-1] is None:\n LOGGER.warning(\n 'automatically getting min epoch '\n 'for phased LC plot'\n )\n try:\n spfit = lcfit.spline_fit_magseries(\n stimes, # times\n smags, # mags\n serrs, # errs\n lctoolargs[6], # period\n magsarefluxes=lctoolkwargs['magsarefluxes'],\n sigclip=None,\n verbose=True\n )\n\n # set the epoch correctly now for the plot\n lctoolargs[-1] = spfit['fitinfo']['fitepoch']\n\n if len(spfit['fitinfo']['fitepoch']) != 1:\n lctoolargs[-1] = (\n spfit['fitinfo']['fitepoch'][0]\n )\n\n # if the spline fit fails, use the minimum of times as\n # epoch as usual\n except Exception as e:\n\n LOGGER.exception(\n 'spline fit failed, '\n 'using min(times) as epoch'\n )\n\n lctoolargs[-1] = np.min(stimes)\n\n # now run the phased LC function with provided args,\n # kwargs\n\n # final times, mags, errs\n lctoolargs[3] = stimes\n lctoolargs[4] = smags\n lctoolargs[5] = serrs\n\n # the sigclip kwarg isn't used here since we did this\n # already earlier\n del lctoolkwargs['sigclip']\n\n lctoolfunction = CPTOOLMAP[lctool]['func']\n\n funcresults = yield self.executor.submit(\n lctoolfunction,\n *lctoolargs,\n **lctoolkwargs\n )\n\n # save these to the tempcpdict\n # save the pickle only if readonly is not true\n if not self.readonly:\n\n if (lspmethod in tempcpdict and\n isinstance(tempcpdict[lspmethod], dict)):\n\n if periodind in tempcpdict[lspmethod]:\n\n tempcpdict[lspmethod][periodind] = (\n funcresults\n )\n\n else:\n\n tempcpdict[lspmethod].update(\n {periodind: funcresults}\n )\n\n else:\n\n tempcpdict[lspmethod] = {periodind: funcresults}\n\n\n savekwargs = {\n 'outfile':tempfpath,\n 'protocol':pickle.HIGHEST_PROTOCOL\n }\n savedcpf = yield self.executor.submit(\n _write_checkplot_picklefile,\n tempcpdict,\n **savekwargs\n )\n\n LOGGER.info(\n 'saved temp results from '\n '%s to checkplot: %s' %\n (lctool, savedcpf)\n )\n\n else:\n\n LOGGER.warning(\n 'not saving temp results to checkplot '\n ' because readonly = True'\n )\n\n #\n # assemble the return dict\n #\n resultdict['status'] = 'success'\n resultdict['message'] = (\n 'new results for %s' %\n lctool\n )\n retkey = 'phasedlc%s' % periodind\n resultdict['result'] = {\n 'objectid':objectid,\n lspmethod:{\n retkey:funcresults\n }\n }\n\n self.write(resultdict)\n self.finish()\n\n\n # if the lctool is var-varfeatures\n elif lctool == 'var-varfeatures':\n\n # see if we can return results from a previous iteration of\n # this tool\n if (not forcereload and\n 'varinfo' in tempcpdict and\n isinstance(tempcpdict['varinfo'], dict) and\n 'varfeatures' in tempcpdict['varinfo'] and\n isinstance(tempcpdict['varinfo']['varfeatures'], dict)):\n\n LOGGER.warning(\n 'returning previously unsaved '\n 'results for lctool %s from %s' %\n (lctool, tempfpath)\n )\n\n #\n # assemble the returndict\n #\n\n resultdict['status'] = 'warning'\n resultdict['message'] = (\n 'previous '\n 'unsaved results from %s' %\n lctool\n )\n resultdict['result'] = {\n 'objectid':objectid,\n 'varinfo': {\n 'varfeatures': (\n tempcpdict['varinfo']['varfeatures']\n )\n }\n }\n\n self.write(resultdict)\n self.finish()\n\n # otherwise, we need to dispatch the function\n else:\n\n lctoolfunction = CPTOOLMAP[lctool]['func']\n funcresults = yield self.executor.submit(\n lctoolfunction,\n *lctoolargs,\n **lctoolkwargs\n )\n\n # save these to the tempcpdict\n # save the pickle only if readonly is not true\n if not self.readonly:\n\n if ('varinfo' in tempcpdict and\n isinstance(tempcpdict['varinfo'], dict)):\n\n if 'varfeatures' in tempcpdict['varinfo']:\n\n tempcpdict['varinfo']['varfeatures'] = (\n funcresults\n )\n\n else:\n\n tempcpdict['varinfo'].update(\n {'varfeatures': funcresults}\n )\n\n else:\n\n tempcpdict['varinfo'] = {'varfeatures':\n funcresults}\n\n\n savekwargs = {\n 'outfile':tempfpath,\n 'protocol':pickle.HIGHEST_PROTOCOL\n }\n savedcpf = yield self.executor.submit(\n _write_checkplot_picklefile,\n tempcpdict,\n **savekwargs\n )\n\n LOGGER.info(\n 'saved temp results from '\n '%s to checkplot: %s' %\n (lctool, savedcpf)\n )\n\n else:\n\n LOGGER.warning(\n 'not saving temp results to checkplot '\n ' because readonly = True'\n )\n\n #\n # assemble the return dict\n #\n resultdict['status'] = 'success'\n resultdict['message'] = (\n 'new results for %s' %\n lctool\n )\n resultdict['result'] = {\n 'objectid':objectid,\n 'varinfo':{\n 'varfeatures':funcresults\n }\n }\n\n self.write(resultdict)\n self.finish()\n\n\n # if the lctool is var-prewhiten or var-masksig\n elif lctool in ('var-prewhiten','var-masksig'):\n\n key1, key2 = resloc\n\n # see if we can return results from a previous iteration of\n # this tool\n if (not forcereload and\n key1 in tempcpdict and\n isinstance(tempcpdict[key1], dict) and\n key2 in tempcpdict[key1] and\n isinstance(tempcpdict[key1][key2], dict)):\n\n LOGGER.warning(\n 'returning previously unsaved '\n 'results for lctool %s from %s' %\n (lctool, tempfpath)\n )\n\n #\n # assemble the returndict\n #\n resultdict['status'] = 'warning'\n resultdict['message'] = (\n 'previous '\n 'unsaved results from %s' %\n lctool\n )\n resultdict['result'] = {\n 'objectid':objectid,\n key1: {\n key2: (\n tempcpdict[key1][key2]\n )\n }\n }\n\n self.write(resultdict)\n self.finish()\n\n # otherwise, we need to dispatch the function\n else:\n\n lctoolfunction = CPTOOLMAP[lctool]['func']\n\n # send in a stringio object for the fitplot kwarg\n lctoolkwargs['plotfit'] = StrIO()\n\n funcresults = yield self.executor.submit(\n lctoolfunction,\n *lctoolargs,\n **lctoolkwargs\n )\n\n # we turn the returned fitplotfile fd into a base64\n # encoded string after reading it\n fitfd = funcresults['fitplotfile']\n fitfd.seek(0)\n fitbin = fitfd.read()\n fitb64 = base64.b64encode(fitbin)\n fitfd.close()\n funcresults['fitplotfile'] = fitb64\n\n # save these to the tempcpdict\n # save the pickle only if readonly is not true\n if not self.readonly:\n\n if (key1 in tempcpdict and\n isinstance(tempcpdict[key1], dict)):\n\n if key2 in tempcpdict[key1]:\n\n tempcpdict[key1][key2] = (\n funcresults\n )\n\n else:\n\n tempcpdict[key1].update(\n {key2: funcresults}\n )\n\n else:\n\n tempcpdict[key1] = {key2: funcresults}\n\n\n savekwargs = {\n 'outfile':tempfpath,\n 'protocol':pickle.HIGHEST_PROTOCOL\n }\n savedcpf = yield self.executor.submit(\n _write_checkplot_picklefile,\n tempcpdict,\n **savekwargs\n )\n\n LOGGER.info(\n 'saved temp results from '\n '%s to checkplot: %s' %\n (lctool, savedcpf)\n )\n\n else:\n\n LOGGER.warning(\n 'not saving temp results to checkplot '\n ' because readonly = True'\n )\n\n #\n # assemble the return dict\n #\n # for this operation, we'll return:\n # - fitplotfile\n fitreturndict = {'fitplotfile':fitb64}\n\n resultdict['status'] = 'success'\n resultdict['message'] = (\n 'new results for %s' %\n lctool\n )\n resultdict['result'] = {\n 'objectid':objectid,\n key1:{\n key2:fitreturndict\n }\n }\n\n self.write(resultdict)\n self.finish()\n\n\n # if the lctool is a lcfit method\n elif lctool in ('lcfit-fourier',\n 'lcfit-spline',\n 'lcfit-legendre',\n 'lcfit-savgol'):\n\n key1, key2 = resloc\n\n # see if we can return results from a previous iteration of\n # this tool\n if (not forcereload and\n key1 in tempcpdict and\n isinstance(tempcpdict[key1], dict) and\n key2 in tempcpdict[key1] and\n isinstance(tempcpdict[key1][key2], dict)):\n\n LOGGER.warning(\n 'returning previously unsaved '\n 'results for lctool %s from %s' %\n (lctool, tempfpath)\n )\n\n #\n # assemble the returndict\n #\n\n resultdict['status'] = 'warning'\n resultdict['message'] = (\n 'previous '\n 'unsaved results from %s' %\n lctool\n )\n\n # these are the full results\n phasedfitlc = tempcpdict[key1][key2]\n\n # we only want a few things from them\n fitresults = {\n 'method':phasedfitlc['lcfit']['fittype'],\n 'chisq':phasedfitlc['lcfit']['fitchisq'],\n 'redchisq':phasedfitlc['lcfit']['fitredchisq'],\n 'period':phasedfitlc['period'],\n 'epoch':phasedfitlc['epoch'],\n 'plot':phasedfitlc['plot'],\n }\n\n # add fitparams if there are any\n if ('finalparams' in phasedfitlc['lcfit']['fitinfo'] and\n phasedfitlc['lcfit']['fitinfo']['finalparams']\n is not None):\n fitresults['fitparams'] = (\n phasedfitlc['lcfit']['fitinfo']['finalparams']\n )\n\n # this is the final result object\n resultdict['result'] = {\n 'objectid':objectid,\n key1: {\n key2: (\n fitresults\n )\n }\n }\n\n self.write(resultdict)\n self.finish()\n\n # otherwise, we need to dispatch the function\n else:\n\n lctoolfunction = CPTOOLMAP[lctool]['func']\n\n funcresults = yield self.executor.submit(\n lctoolfunction,\n *lctoolargs,\n **lctoolkwargs\n )\n\n # now that we have the fit results, generate a fitplot.\n # these args are for the special fitplot mode of\n # _pkl_phased_magseries_plot\n phasedlcargs = (None,\n 'lcfit',\n -1,\n cptimes,\n cpmags,\n cperrs,\n lctoolargs[3], # this is the fit period\n 'min')\n\n # here, we set a bestperiodhighlight to distinguish this\n # plot from the ones existing in the checkplot already\n # also add the overplotfit information\n phasedlckwargs = {\n 'xliminsetmode':False,\n 'magsarefluxes':lctoolkwargs['magsarefluxes'],\n 'bestperiodhighlight':'#defa75',\n 'overplotfit':funcresults\n }\n\n # dispatch the plot function\n phasedlc = yield self.executor.submit(\n _pkl_phased_magseries_plot,\n *phasedlcargs,\n **phasedlckwargs\n )\n\n # save these to the tempcpdict\n # save the pickle only if readonly is not true\n if not self.readonly:\n\n if (key1 in tempcpdict and\n isinstance(tempcpdict[key1], dict)):\n\n if key2 in tempcpdict[key1]:\n\n tempcpdict[key1][key2] = (\n phasedlc\n )\n\n else:\n\n tempcpdict[key1].update(\n {key2: phasedlc}\n )\n\n else:\n\n tempcpdict[key1] = {key2: phasedlc}\n\n\n savekwargs = {\n 'outfile':tempfpath,\n 'protocol':pickle.HIGHEST_PROTOCOL\n }\n savedcpf = yield self.executor.submit(\n _write_checkplot_picklefile,\n tempcpdict,\n **savekwargs\n )\n\n LOGGER.info(\n 'saved temp results from '\n '%s to checkplot: %s' %\n (lctool, savedcpf)\n )\n\n else:\n\n LOGGER.warning(\n 'not saving temp results to checkplot '\n ' because readonly = True'\n )\n\n #\n # assemble the return dict\n #\n fitresults = {\n 'method':phasedlc['lcfit']['fittype'],\n 'chisq':phasedlc['lcfit']['fitchisq'],\n 'redchisq':phasedlc['lcfit']['fitredchisq'],\n 'period':phasedlc['period'],\n 'epoch':phasedlc['epoch'],\n 'plot':phasedlc['plot'],\n }\n\n # add fitparams if there are any\n if ('finalparams' in funcresults['fitinfo'] and\n funcresults['fitinfo']['finalparams'] is not None):\n fitresults['fitparams'] = (\n funcresults['fitinfo']['finalparams']\n )\n\n\n resultdict['status'] = 'success'\n resultdict['message'] = (\n 'new results for %s' %\n lctool\n )\n resultdict['result'] = {\n 'objectid':objectid,\n key1:{\n key2:fitresults\n }\n }\n\n self.write(resultdict)\n self.finish()\n\n\n # if this is the special lcfit subtract tool\n elif lctool == 'lcfit-subtract':\n\n fitmethod, periodind = lctoolargs\n\n # find the fit requested\n\n # subtract it from the cptimes, cpmags, cperrs\n\n # if not readonly, write back to cptimes, cpmags, cperrs\n\n # make a new phasedlc plot for the current periodind using\n # these new cptimes, cpmags, cperrs\n\n # return this plot\n\n\n # if this is the special full reset tool\n elif lctool == 'lctool-reset':\n\n if os.path.exists(tempfpath):\n os.remove(tempfpath)\n LOGGER.warning('reset all LC tool results '\n 'for %s by removing %s' %\n (tempfpath, cpfpath))\n resultdict['status'] = 'success'\n else:\n resultdict['status'] = 'error'\n LOGGER.warning('tried to reset LC tool results for %s, '\n 'but temp checkplot result pickle %s '\n 'does not exist' %\n (tempfpath, cpfpath))\n\n resultdict['message'] = (\n 'all unsynced results for this object have been purged'\n )\n resultdict['result'] = {'objectid':cpobjectid}\n\n self.write(resultdict)\n self.finish()\n\n\n # if this is the special load results tool\n elif lctool == 'lctool-results':\n\n target = self.get_argument('resultsfor',None)\n\n if target is not None:\n\n target = xhtml_escape(target)\n\n # get rid of invalid targets\n if (target not in CPTOOLMAP or\n target == 'lctool-reset' or\n target == 'lctool-results' or\n target == 'phasedlc-newplot' or\n target == 'lcfit-subtract'):\n\n LOGGER.error(\"can't get results for %s\" % target)\n resultdict['status'] = 'error'\n resultdict['message'] = (\n \"can't get results for %s\" % target\n )\n resultdict['result'] = {'objectid':cpobjectid}\n\n self.write(resultdict)\n raise tornado.web.Finish()\n\n # if we're good to go, get the target location\n targetloc = CPTOOLMAP[target]['resloc']\n\n # first, search the cptempdict for this target\n # if found, return it\n\n # second, search the actual cpdict for this target\n # if found, return it\n\n # otherwise, we're being asked for everything\n # return the whole\n else:\n\n pass\n\n\n # otherwise, this is an unrecognized lctool\n else:\n\n LOGGER.error('lctool %s, does not exist' % lctool)\n resultdict['status'] = 'error'\n resultdict['message'] = (\n 'lctool %s does not exist' % lctool\n )\n resultdict['result'] = {'objectid':cpobjectid}\n\n self.write(resultdict)\n raise tornado.web.Finish()\n\n # if the cpfile doesn't exist\n else:\n\n LOGGER.error('could not find %s' % self.cpfile)\n\n resultdict = {'status':'error',\n 'message':\"This checkplot doesn't exist.\",\n 'readonly':self.readonly,\n 'result':None}\n\n self.write(resultdict)\n raise tornado.web.Finish()\n\n # if no checkplot was provided to load\n else:\n\n resultdict = {'status':'error',\n 'message':'No checkplot provided to load.',\n 'readonly':self.readonly,\n 'result':None}\n\n self.write(resultdict)\n raise tornado.web.Finish()", "response": "This is the method that handles a GET request to run a specific LC tool on a specific checkplot file."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _old_epd_diffmags(coeff, fsv, fdv, fkv, xcc, ycc, bgv, bge, mag):\n '''\n This calculates the difference in mags after EPD coefficients are\n calculated.\n\n final EPD mags = median(magseries) + epd_diffmags()\n\n '''\n\n return -(coeff[0]*fsv**2. +\n coeff[1]*fsv +\n coeff[2]*fdv**2. +\n coeff[3]*fdv +\n coeff[4]*fkv**2. +\n coeff[5]*fkv +\n coeff[6] +\n coeff[7]*fsv*fdv +\n coeff[8]*fsv*fkv +\n coeff[9]*fdv*fkv +\n coeff[10]*np.sin(2*np.pi*xcc) +\n coeff[11]*np.cos(2*np.pi*xcc) +\n coeff[12]*np.sin(2*np.pi*ycc) +\n coeff[13]*np.cos(2*np.pi*ycc) +\n coeff[14]*np.sin(4*np.pi*xcc) +\n coeff[15]*np.cos(4*np.pi*xcc) +\n coeff[16]*np.sin(4*np.pi*ycc) +\n coeff[17]*np.cos(4*np.pi*ycc) +\n coeff[18]*bgv +\n coeff[19]*bge -\n mag)", "response": "Calculates the difference in mags after EPD coefficients are calculated."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _old_epd_magseries(times, mags, errs,\n fsv, fdv, fkv, xcc, ycc, bgv, bge,\n epdsmooth_windowsize=21,\n epdsmooth_sigclip=3.0,\n epdsmooth_func=smooth_magseries_signal_medfilt,\n epdsmooth_extraparams=None):\n '''\n Detrends a magnitude series given in mag using accompanying values of S in\n fsv, D in fdv, K in fkv, x coords in xcc, y coords in ycc, background in\n bgv, and background error in bge. smooth is used to set a smoothing\n parameter for the fit function. Does EPD voodoo.\n\n '''\n\n # find all the finite values of the magsnitude\n finiteind = np.isfinite(mags)\n\n # calculate median and stdev\n mags_median = np.median(mags[finiteind])\n mags_stdev = np.nanstd(mags)\n\n # if we're supposed to sigma clip, do so\n if epdsmooth_sigclip:\n excludeind = abs(mags - mags_median) < epdsmooth_sigclip*mags_stdev\n finalind = finiteind & excludeind\n else:\n finalind = finiteind\n\n final_mags = mags[finalind]\n final_len = len(final_mags)\n\n # smooth the signal\n if isinstance(epdsmooth_extraparams, dict):\n smoothedmags = epdsmooth_func(final_mags,\n epdsmooth_windowsize,\n **epdsmooth_extraparams)\n else:\n smoothedmags = epdsmooth_func(final_mags, epdsmooth_windowsize)\n\n # make the linear equation matrix\n epdmatrix = np.c_[fsv[finalind]**2.0,\n fsv[finalind],\n fdv[finalind]**2.0,\n fdv[finalind],\n fkv[finalind]**2.0,\n fkv[finalind],\n np.ones(final_len),\n fsv[finalind]*fdv[finalind],\n fsv[finalind]*fkv[finalind],\n fdv[finalind]*fkv[finalind],\n np.sin(2*np.pi*xcc[finalind]),\n np.cos(2*np.pi*xcc[finalind]),\n np.sin(2*np.pi*ycc[finalind]),\n np.cos(2*np.pi*ycc[finalind]),\n np.sin(4*np.pi*xcc[finalind]),\n np.cos(4*np.pi*xcc[finalind]),\n np.sin(4*np.pi*ycc[finalind]),\n np.cos(4*np.pi*ycc[finalind]),\n bgv[finalind],\n bge[finalind]]\n\n # solve the matrix equation [epdmatrix] . [x] = [smoothedmags]\n # return the EPD differential magss if the solution succeeds\n try:\n\n coeffs, residuals, rank, singulars = lstsq(epdmatrix, smoothedmags,\n rcond=None)\n\n if DEBUG:\n print('coeffs = %s, residuals = %s' % (coeffs, residuals))\n\n\n retdict = {'times':times,\n 'mags':(mags_median +\n _old_epd_diffmags(coeffs, fsv, fdv,\n fkv, xcc, ycc, bgv, bge, mags)),\n 'errs':errs,\n 'fitcoeffs':coeffs,\n 'residuals':residuals}\n\n return retdict\n\n # if the solution fails, return nothing\n except Exception as e:\n\n LOGEXCEPTION('EPD solution did not converge')\n\n retdict = {'times':times,\n 'mags':np.full_like(mags, np.nan),\n 'errs':errs,\n 'fitcoeffs':coeffs,\n 'residuals':residuals}\n\n return retdict", "response": "This function is used to calculate the EPD for a magnitude series given in mag."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _epd_function(coeffs, fsv, fdv, fkv, xcc, ycc, bgv, bge, iha, izd):\n '''\n This is the EPD function to fit using a smoothed mag-series.\n\n '''\n\n return (coeffs[0]*fsv*fsv +\n coeffs[1]*fsv +\n coeffs[2]*fdv*fdv +\n coeffs[3]*fdv +\n coeffs[4]*fkv*fkv +\n coeffs[5]*fkv +\n coeffs[6] +\n coeffs[7]*fsv*fdv +\n coeffs[8]*fsv*fkv +\n coeffs[9]*fdv*fkv +\n coeffs[10]*np.sin(2*pi_value*xcc) +\n coeffs[11]*np.cos(2*pi_value*xcc) +\n coeffs[12]*np.sin(2*pi_value*ycc) +\n coeffs[13]*np.cos(2*pi_value*ycc) +\n coeffs[14]*np.sin(4*pi_value*xcc) +\n coeffs[15]*np.cos(4*pi_value*xcc) +\n coeffs[16]*np.sin(4*pi_value*ycc) +\n coeffs[17]*np.cos(4*pi_value*ycc) +\n coeffs[18]*bgv +\n coeffs[19]*bge +\n coeffs[20]*iha +\n coeffs[21]*izd)", "response": "This function calculates the EPD function to fit using a smoothed mag - series."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _epd_residual(coeffs, mags, fsv, fdv, fkv, xcc, ycc, bgv, bge, iha, izd):\n '''\n This is the residual function to minimize using scipy.optimize.leastsq.\n\n '''\n\n f = _epd_function(coeffs, fsv, fdv, fkv, xcc, ycc, bgv, bge, iha, izd)\n residual = mags - f\n return residual", "response": "This function computes the residual function for the EPD decomposition."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef rfepd_magseries(times, mags, errs,\n externalparam_arrs,\n magsarefluxes=False,\n epdsmooth=True,\n epdsmooth_sigclip=3.0,\n epdsmooth_windowsize=21,\n epdsmooth_func=smooth_magseries_savgol,\n epdsmooth_extraparams=None,\n rf_subsample=1.0,\n rf_ntrees=300,\n rf_extraparams={'criterion':'mse',\n 'oob_score':False,\n 'n_jobs':-1}):\n '''This uses a `RandomForestRegressor` to de-correlate the given magseries.\n\n Parameters\n ----------\n\n times,mags,errs : np.array\n The input mag/flux time-series to run EPD on.\n\n externalparam_arrs : list of np.arrays\n This is a list of ndarrays of external parameters to decorrelate\n against. These should all be the same size as `times`, `mags`, `errs`.\n\n epdsmooth : bool\n If True, sets the training LC for the RandomForestRegress to be a\n smoothed version of the sigma-clipped light curve provided in `times`,\n `mags`, `errs`.\n\n epdsmooth_sigclip : float or int or sequence of two floats/ints or None\n This specifies how to sigma-clip the input LC before smoothing it and\n fitting the EPD function to it. The actual LC will not be sigma-clipped.\n\n If a single float or int, a symmetric sigma-clip will be performed using\n the number provided as the sigma-multiplier to cut out from the input\n time-series.\n\n If a list of two ints/floats is provided, the function will perform an\n 'asymmetric' sigma-clip. The first element in this list is the sigma\n value to use for fainter flux/mag values; the second element in this\n list is the sigma value to use for brighter flux/mag values. For\n example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma\n dimmings and greater than 3-sigma brightenings. Here the meaning of\n \"dimming\" and \"brightening\" is set by *physics* (not the magnitude\n system), which is why the `magsarefluxes` kwarg must be correctly set.\n\n If `sigclip` is None, no sigma-clipping will be performed, and the\n time-series (with non-finite elems removed) will be passed through to\n the output.\n\n epdsmooth_windowsize : int\n This is the number of LC points to smooth over to generate a smoothed\n light curve that will be used to fit the EPD function.\n\n epdsmooth_func : Python function\n This sets the smoothing filter function to use. A Savitsky-Golay filter\n is used to smooth the light curve by default. The functions that can be\n used with this kwarg are listed in `varbase.trends`. If you want to use\n your own function, it MUST have the following signature::\n\n def smoothfunc(mags_array, window_size, **extraparams)\n\n and return a numpy array of the same size as `mags_array` with the\n smoothed time-series. Any extra params can be provided using the\n `extraparams` dict.\n\n epdsmooth_extraparams : dict\n This is a dict of any extra filter params to supply to the smoothing\n function.\n\n rf_subsample : float\n Defines the fraction of the size of the `mags` array to use for\n training the random forest regressor.\n\n rf_ntrees : int\n This is the number of trees to use for the `RandomForestRegressor`.\n\n rf_extraprams : dict\n This is a dict of any extra kwargs to provide to the\n `RandomForestRegressor` instance used.\n\n Returns\n -------\n\n dict\n Returns a dict with decorrelated mags and the usual info from the\n `RandomForestRegressor`: variable importances, etc.\n\n '''\n\n # get finite times, mags, errs\n finind = np.isfinite(times) & np.isfinite(mags) & np.isfinite(errs)\n ftimes, fmags, ferrs = times[::][finind], mags[::][finind], errs[::][finind]\n finalparam_arrs = []\n for ep in externalparam_arrs:\n finalparam_arrs.append(ep[::][finind])\n\n stimes, smags, serrs, eparams = sigclip_magseries_with_extparams(\n times, mags, errs,\n externalparam_arrs,\n sigclip=epdsmooth_sigclip,\n magsarefluxes=magsarefluxes\n )\n\n # smoothing is optional for RFR because we train on a fraction of the mag\n # series and so should not require a smoothed input to fit a function to\n if epdsmooth:\n\n # smooth the signal\n if isinstance(epdsmooth_extraparams, dict):\n smoothedmags = epdsmooth_func(smags,\n epdsmooth_windowsize,\n **epdsmooth_extraparams)\n else:\n smoothedmags = epdsmooth_func(smags,\n epdsmooth_windowsize)\n\n else:\n\n smoothedmags = smags\n\n\n # set up the regressor\n if isinstance(rf_extraparams, dict):\n RFR = RandomForestRegressor(n_estimators=rf_ntrees,\n **rf_extraparams)\n else:\n RFR = RandomForestRegressor(n_estimators=rf_ntrees)\n\n # collect the features\n features = np.column_stack(eparams)\n\n # fit, then generate the predicted values, then get corrected values\n\n # we fit on a randomly selected subsample of all the mags\n if rf_subsample < 1.0:\n featureindices = np.arange(smoothedmags.size)\n\n # these are sorted because time-order should be important\n training_indices = np.sort(\n npr.choice(featureindices,\n size=int(rf_subsample*smoothedmags.size),\n replace=False)\n )\n else:\n training_indices = np.arange(smoothedmags.size)\n\n RFR.fit(features[training_indices,:], smoothedmags[training_indices])\n\n # predict on the full feature set\n flux_corrections = RFR.predict(np.column_stack(finalparam_arrs))\n corrected_fmags = npmedian(fmags) + fmags - flux_corrections\n\n retdict = {'times':ftimes,\n 'mags':corrected_fmags,\n 'errs':ferrs,\n 'feature_importances':RFR.feature_importances_,\n 'regressor':RFR,\n 'mags_median':npmedian(corrected_fmags),\n 'mags_mad':npmedian(npabs(corrected_fmags -\n npmedian(corrected_fmags)))}\n\n return retdict", "response": "This function uses a RandomForestRegressor to de - correlate a set of mag - time - series times and mags."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef lcfit_features(times, mags, errs, period,\n fourierorder=5,\n # these are depth, duration, ingress duration\n transitparams=(-0.01,0.1,0.1),\n # these are depth, duration, depth ratio, secphase\n ebparams=(-0.2,0.3,0.7,0.5),\n sigclip=10.0,\n magsarefluxes=False,\n fitfailure_means_featurenan=False,\n verbose=True):\n '''This calculates various features related to fitting models to light\n curves.\n\n This function:\n\n - calculates `R_ij` and `phi_ij` ratios for Fourier fit amplitudes and\n phases.\n\n - calculates the reduced chi-sq for fourier, EB, and planet transit fits.\n\n - calculates the reduced chi-sq for fourier, EB, planet transit fits w/2 x\n period.\n\n Parameters\n ----------\n\n times,mags,errs : np.array\n The input mag/flux time-series to calculate periodic features for.\n\n period : float\n The period of variabiity to use to phase the light curve.\n\n fourierorder : int\n The Fourier order to use to generate sinusoidal function and fit that to\n the phased light curve.\n\n transitparams : list of floats\n The transit depth, duration, and ingress duration to use to generate a\n trapezoid planet transit model fit to the phased light curve. The period\n used is the one provided in `period`, while the epoch is automatically\n obtained from a spline fit to the phased light curve.\n\n ebparams : list of floats\n The primary eclipse depth, eclipse duration, the primary-secondary depth\n ratio, and the phase of the secondary eclipse to use to generate an\n eclipsing binary model fit to the phased light curve. The period used is\n the one provided in `period`, while the epoch is automatically obtained\n from a spline fit to the phased light curve.\n\n sigclip : float or int or sequence of two floats/ints or None\n If a single float or int, a symmetric sigma-clip will be performed using\n the number provided as the sigma-multiplier to cut out from the input\n time-series.\n\n If a list of two ints/floats is provided, the function will perform an\n 'asymmetric' sigma-clip. The first element in this list is the sigma\n value to use for fainter flux/mag values; the second element in this\n list is the sigma value to use for brighter flux/mag values. For\n example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma\n dimmings and greater than 3-sigma brightenings. Here the meaning of\n \"dimming\" and \"brightening\" is set by *physics* (not the magnitude\n system), which is why the `magsarefluxes` kwarg must be correctly set.\n\n If `sigclip` is None, no sigma-clipping will be performed, and the\n time-series (with non-finite elems removed) will be passed through to\n the output.\n\n magsarefluxes : bool\n Set this to True if the input measurements in `mags` are actually\n fluxes.\n\n fitfailure_means_featurenan : bool\n If the planet, EB and EBx2 fits don't return standard errors because the\n covariance matrix could not be generated, then the fit is suspicious and\n the features calculated can't be trusted. If\n `fitfailure_means_featurenan` is True, then the output features for\n these fits will be set to nan.\n\n verbose : bool\n If True, will indicate progress while working.\n\n Returns\n -------\n\n dict\n A dict of all the features calculated is returned.\n\n '''\n\n # get the finite values\n finind = np.isfinite(times) & np.isfinite(mags) & np.isfinite(errs)\n ftimes, fmags, ferrs = times[finind], mags[finind], errs[finind]\n\n # get nonzero errors\n nzind = np.nonzero(ferrs)\n ftimes, fmags, ferrs = ftimes[nzind], fmags[nzind], ferrs[nzind]\n\n # get the MAD of the unphased light curve\n lightcurve_median = np.median(fmags)\n lightcurve_mad = np.median(np.abs(fmags - lightcurve_median))\n\n #\n # fourier fit\n #\n\n # we fit a Fourier series to the light curve using the best period and\n # extract the amplitudes and phases up to the 8th order to fit the LC. the\n # various ratios of the amplitudes A_ij and the differences in the phases\n # phi_ij are also used as periodic variability features\n\n # do the fit\n ffit = lcfit.fourier_fit_magseries(ftimes, fmags, ferrs, period,\n fourierorder=fourierorder,\n sigclip=sigclip,\n magsarefluxes=magsarefluxes,\n verbose=verbose)\n\n # get the coeffs and redchisq\n fourier_fitcoeffs = ffit['fitinfo']['finalparams']\n fourier_chisq = ffit['fitchisq']\n fourier_redchisq = ffit['fitredchisq']\n\n if fourier_fitcoeffs is not None:\n\n fourier_modelmags, _, _, fpmags, _ = sinusoidal.fourier_sinusoidal_func(\n [period,\n ffit['fitinfo']['fitepoch'],\n ffit['fitinfo']['finalparams'][:fourierorder],\n ffit['fitinfo']['finalparams'][fourierorder:]],\n ftimes,\n fmags,\n ferrs\n )\n\n fourier_residuals = fourier_modelmags - fpmags\n fourier_residual_median = np.median(fourier_residuals)\n fourier_residual_mad = np.median(np.abs(fourier_residuals -\n fourier_residual_median))\n\n\n # break them out into amps and phases\n famplitudes = fourier_fitcoeffs[:fourierorder]\n fphases = fourier_fitcoeffs[fourierorder:]\n\n famp_combos = combinations(famplitudes,2)\n famp_cinds = combinations(range(len(famplitudes)),2)\n\n fpha_combos = combinations(fphases,2)\n fpha_cinds = combinations(range(len(fphases)),2)\n\n else:\n\n LOGERROR('LC fit to sinusoidal series model failed, '\n 'using initial params')\n\n initfourieramps = [0.6] + [0.2]*(fourierorder - 1)\n initfourierphas = [0.1] + [0.1]*(fourierorder - 1)\n\n fourier_modelmags, _, _, fpmags, _ = sinusoidal.fourier_sinusoidal_func(\n [period,\n ffit['fitinfo']['fitepoch'],\n initfourieramps,\n initfourierphas],\n ftimes,\n fmags,\n ferrs\n )\n\n fourier_residuals = fourier_modelmags - fpmags\n fourier_residual_median = np.median(fourier_residuals)\n fourier_residual_mad = np.median(np.abs(fourier_residuals -\n fourier_residual_median))\n\n # break them out into amps and phases\n famplitudes = initfourieramps\n fphases = initfourierphas\n\n famp_combos = combinations(famplitudes,2)\n famp_cinds = combinations(range(len(famplitudes)),2)\n\n fpha_combos = combinations(fphases,2)\n fpha_cinds = combinations(range(len(fphases)),2)\n\n\n fampratios = {}\n fphadiffs = {}\n\n # get the ratios for all fourier coeff combinations\n for ampi, ampc, phai, phac in zip(famp_cinds,\n famp_combos,\n fpha_cinds,\n fpha_combos):\n\n ampratind = 'R_%s%s' % (ampi[1]+1, ampi[0]+1)\n # this is R_ij\n amprat = ampc[1]/ampc[0]\n phadiffind = 'phi_%s%s' % (phai[1]+1, phai[0]+1)\n # this is phi_ij\n phadiff = phac[1] - phai[0]*phac[0]\n\n fampratios[ampratind] = amprat\n fphadiffs[phadiffind] = phadiff\n\n # update the outdict for the Fourier fit results\n outdict = {\n 'fourier_ampratios':fampratios,\n 'fourier_phadiffs':fphadiffs,\n 'fourier_fitparams':fourier_fitcoeffs,\n 'fourier_redchisq':fourier_redchisq,\n 'fourier_chisq':fourier_chisq,\n 'fourier_residual_median':fourier_residual_median,\n 'fourier_residual_mad':fourier_residual_mad,\n 'fourier_residual_mad_over_lcmad':fourier_residual_mad/lightcurve_mad\n }\n\n # EB and planet fits will find the epoch automatically\n planetfitparams = [period,\n None,\n transitparams[0],\n transitparams[1],\n transitparams[2]]\n\n ebfitparams = [period,\n None,\n ebparams[0],\n ebparams[1],\n ebparams[2],\n ebparams[3]]\n\n # do the planet and EB fit with this period\n planet_fit = lcfit.traptransit_fit_magseries(ftimes, fmags, ferrs,\n planetfitparams,\n sigclip=sigclip,\n magsarefluxes=magsarefluxes,\n verbose=verbose)\n\n planetfit_finalparams = planet_fit['fitinfo']['finalparams']\n\n planetfit_finalparamerrs = planet_fit['fitinfo']['finalparamerrs']\n\n if planetfit_finalparamerrs is None and fitfailure_means_featurenan:\n\n LOGWARNING('planet fit: no standard errors available '\n 'for fit parameters, fit is bad, '\n 'setting fit chisq and red-chisq to np.nan')\n planetfit_chisq = np.nan\n planetfit_redchisq = np.nan\n planet_residual_median = np.nan\n planet_residual_mad = np.nan\n planet_residual_mad_over_lcmad = np.nan\n\n else:\n\n planetfit_chisq = planet_fit['fitchisq']\n planetfit_redchisq = planet_fit['fitredchisq']\n\n if planetfit_finalparams is not None:\n\n planet_modelmags, _, _, ppmags, _ = transits.trapezoid_transit_func(\n planetfit_finalparams,\n ftimes,\n fmags,\n ferrs\n )\n\n else:\n\n LOGERROR('LC fit to transit planet model '\n 'failed, using initial params')\n planet_modelmags, _, _, ppmags, _ = transits.trapezoid_transit_func(\n planetfitparams,\n ftimes,\n fmags,\n ferrs\n )\n\n planet_residuals = planet_modelmags - ppmags\n planet_residual_median = np.median(planet_residuals)\n planet_residual_mad = np.median(np.abs(planet_residuals -\n planet_residual_median))\n planet_residual_mad_over_lcmad = planet_residual_mad/lightcurve_mad\n\n\n eb_fit = lcfit.gaussianeb_fit_magseries(ftimes, fmags, ferrs,\n ebfitparams,\n sigclip=sigclip,\n magsarefluxes=magsarefluxes,\n verbose=verbose)\n\n ebfit_finalparams = eb_fit['fitinfo']['finalparams']\n ebfit_finalparamerrs = eb_fit['fitinfo']['finalparamerrs']\n\n if ebfit_finalparamerrs is None and fitfailure_means_featurenan:\n\n LOGWARNING('EB fit: no standard errors available '\n 'for fit parameters, fit is bad, '\n 'setting fit chisq and red-chisq to np.nan')\n ebfit_chisq = np.nan\n ebfit_redchisq = np.nan\n eb_residual_median = np.nan\n eb_residual_mad = np.nan\n eb_residual_mad_over_lcmad = np.nan\n\n else:\n\n ebfit_chisq = eb_fit['fitchisq']\n ebfit_redchisq = eb_fit['fitredchisq']\n\n if ebfit_finalparams is not None:\n\n eb_modelmags, _, _, ebpmags, _ = eclipses.invgauss_eclipses_func(\n ebfit_finalparams,\n ftimes,\n fmags,\n ferrs\n )\n\n else:\n\n LOGERROR('LC fit to EB model failed, using initial params')\n\n eb_modelmags, _, _, ebpmags, _ = eclipses.invgauss_eclipses_func(\n ebfitparams,\n ftimes,\n fmags,\n ferrs\n )\n\n eb_residuals = eb_modelmags - ebpmags\n eb_residual_median = np.median(eb_residuals)\n eb_residual_mad = np.median(np.abs(eb_residuals - eb_residual_median))\n eb_residual_mad_over_lcmad = eb_residual_mad/lightcurve_mad\n\n\n # do the EB fit with 2 x period\n ebfitparams[0] = ebfitparams[0]*2.0\n eb_fitx2 = lcfit.gaussianeb_fit_magseries(ftimes, fmags, ferrs,\n ebfitparams,\n sigclip=sigclip,\n magsarefluxes=magsarefluxes,\n verbose=verbose)\n\n ebfitx2_finalparams = eb_fitx2['fitinfo']['finalparams']\n ebfitx2_finalparamerrs = eb_fitx2['fitinfo']['finalparamerrs']\n\n if ebfitx2_finalparamerrs is None and fitfailure_means_featurenan:\n\n LOGWARNING('EB x2 period fit: no standard errors available '\n 'for fit parameters, fit is bad, '\n 'setting fit chisq and red-chisq to np.nan')\n ebfitx2_chisq = np.nan\n ebfitx2_redchisq = np.nan\n ebx2_residual_median = np.nan\n ebx2_residual_mad = np.nan\n ebx2_residual_mad_over_lcmad = np.nan\n\n else:\n\n ebfitx2_chisq = eb_fitx2['fitchisq']\n ebfitx2_redchisq = eb_fitx2['fitredchisq']\n\n if ebfitx2_finalparams is not None:\n\n ebx2_modelmags, _, _, ebx2pmags, _ = (\n eclipses.invgauss_eclipses_func(\n ebfitx2_finalparams,\n ftimes,\n fmags,\n ferrs\n )\n )\n\n else:\n\n LOGERROR('LC fit to EB model with 2xP failed, using initial params')\n\n ebx2_modelmags, _, _, ebx2pmags, _ = (\n eclipses.invgauss_eclipses_func(\n ebfitparams,\n ftimes,\n fmags,\n ferrs\n )\n )\n\n ebx2_residuals = ebx2_modelmags - ebx2pmags\n ebx2_residual_median = np.median(ebx2_residuals)\n ebx2_residual_mad = np.median(np.abs(ebx2_residuals -\n ebx2_residual_median))\n\n ebx2_residual_mad_over_lcmad = ebx2_residual_mad/lightcurve_mad\n\n\n # update the outdict\n outdict.update({\n 'planet_fitparams':planetfit_finalparams,\n 'planet_chisq':planetfit_chisq,\n 'planet_redchisq':planetfit_redchisq,\n 'planet_residual_median':planet_residual_median,\n 'planet_residual_mad':planet_residual_mad,\n 'planet_residual_mad_over_lcmad':(\n planet_residual_mad_over_lcmad,\n ),\n 'eb_fitparams':ebfit_finalparams,\n 'eb_chisq':ebfit_chisq,\n 'eb_redchisq':ebfit_redchisq,\n 'eb_residual_median':eb_residual_median,\n 'eb_residual_mad':eb_residual_mad,\n 'eb_residual_mad_over_lcmad':(\n eb_residual_mad_over_lcmad,\n ),\n 'ebx2_fitparams':ebfitx2_finalparams,\n 'ebx2_chisq':ebfitx2_chisq,\n 'ebx2_redchisq':ebfitx2_redchisq,\n 'ebx2_residual_median':ebx2_residual_median,\n 'ebx2_residual_mad':ebx2_residual_mad,\n 'ebx2_residual_mad_over_lcmad':(\n ebx2_residual_mad_over_lcmad,\n ),\n })\n\n return outdict", "response": "This function calculates the set of features related to fitting models to light curve curves."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef phasedlc_features(times,\n mags,\n errs,\n period,\n nbrtimes=None,\n nbrmags=None,\n nbrerrs=None):\n '''This calculates various phased LC features for the object.\n\n Some of the features calculated here come from:\n\n Kim, D.-W., Protopapas, P., Bailer-Jones, C. A. L., et al. 2014, Astronomy\n and Astrophysics, 566, A43, and references therein (especially Richards, et\n al. 2011).\n\n Parameters\n ----------\n\n times,mags,errs : np.array\n The input mag/flux time-series to calculate the phased LC features for.\n\n period : float\n The period used to phase the input mag/flux time-series.\n\n nbrtimes,nbrmags,nbrerrs : np.array or None\n If `nbrtimes`, `nbrmags`, and `nbrerrs` are all provided, they should be\n ndarrays with `times`, `mags`, `errs` of this object's closest neighbor\n (close within some small number x FWHM of telescope to check for\n blending). This function will then also calculate extra features based\n on the neighbor's phased LC using the `period` provided for the target\n object.\n\n Returns\n -------\n\n dict\n Returns a dict with phased LC features.\n\n '''\n # get the finite values\n finind = np.isfinite(times) & np.isfinite(mags) & np.isfinite(errs)\n ftimes, fmags, ferrs = times[finind], mags[finind], errs[finind]\n\n # get nonzero errors\n nzind = np.nonzero(ferrs)\n ftimes, fmags, ferrs = ftimes[nzind], fmags[nzind], ferrs[nzind]\n\n # only operate on LC if enough points\n if ftimes.size > 49:\n\n # get the MAD of the unphased light curve\n lightcurve_median = np.median(fmags)\n lightcurve_mad = np.median(np.abs(fmags - lightcurve_median))\n\n # get p2p for raw lightcurve\n p2p_unphasedlc = lightcurve_ptp_measures(ftimes, fmags, ferrs)\n inveta_unphasedlc = 1.0/p2p_unphasedlc['eta_normal']\n\n # phase the light curve with the given period, assume epoch is\n # times.min()\n phasedlc = lcmath.phase_magseries_with_errs(ftimes, fmags, ferrs,\n period, ftimes.min(),\n wrap=False)\n\n phase = phasedlc['phase']\n pmags = phasedlc['mags']\n perrs = phasedlc['errs']\n\n # get ptp measures for best period\n ptp_bestperiod = lightcurve_ptp_measures(phase,pmags,perrs)\n\n # phase the light curve with the given periodx2, assume epoch is\n # times.min()\n phasedlc = lcmath.phase_magseries_with_errs(ftimes, fmags, ferrs,\n period*2.0, ftimes.min(),\n wrap=False)\n\n phasex2 = phasedlc['phase']\n pmagsx2 = phasedlc['mags']\n perrsx2 = phasedlc['errs']\n\n\n # get ptp measures for best periodx2\n ptp_bestperiodx2 = lightcurve_ptp_measures(phasex2,pmagsx2,perrsx2)\n\n # eta_phasedlc_bestperiod - calculate eta for the phased LC with best\n # period\n inveta_bestperiod = 1.0/ptp_bestperiod['eta_normal']\n\n # eta_phasedlc_bestperiodx2 - calculate eta for the phased LC with best\n # period x 2\n inveta_bestperiodx2 = 1.0/ptp_bestperiodx2['eta_normal']\n\n\n # eta_phased_ratio_eta_raw - eta for best period phased LC / eta for raw\n # LC\n inveta_ratio_phased_unphased = inveta_bestperiod/inveta_unphasedlc\n\n # eta_phasedx2_ratio_eta_raw - eta for best periodx2 phased LC/eta for\n # raw LC\n inveta_ratio_phasedx2_unphased = inveta_bestperiodx2/inveta_unphasedlc\n\n\n # freq_model_max_delta_mags - absval of magdiff btw model phased LC\n # maxima using period x 2. look at points\n # more than 10 points away for maxima\n phasedx2_maxval_ind = argrelmax(pmagsx2, order=10)\n if phasedx2_maxval_ind[0].size > 1:\n phasedx2_magdiff_maxval = (\n np.max(np.abs(np.diff(pmagsx2[phasedx2_maxval_ind[0]])))\n )\n else:\n phasedx2_magdiff_maxval = np.nan\n\n # freq_model_min_delta_mags - absval of magdiff btw model phased LC\n # minima using period x 2. look at points\n # more than 10 points away for minima\n phasedx2_minval_ind = argrelmin(pmagsx2, order=10)\n if phasedx2_minval_ind[0].size > 1:\n phasedx2_magdiff_minval = (\n np.max(np.abs(np.diff(pmagsx2[phasedx2_minval_ind[0]])))\n )\n else:\n phasedx2_magdiff_minval = np.nan\n\n # p2p_scatter_pfold_over_mad - MAD of successive absolute mag diffs of\n # the phased LC using best period divided\n # by the MAD of the unphased LC\n phased_magdiff = np.diff(pmags)\n phased_magdiff_median = np.median(phased_magdiff)\n phased_magdiff_mad = np.median(np.abs(phased_magdiff -\n phased_magdiff_median))\n\n phasedx2_magdiff = np.diff(pmagsx2)\n phasedx2_magdiff_median = np.median(phasedx2_magdiff)\n phasedx2_magdiff_mad = np.median(np.abs(phasedx2_magdiff -\n phasedx2_magdiff_median))\n\n phased_magdiffmad_unphased_mad_ratio = phased_magdiff_mad/lightcurve_mad\n phasedx2_magdiffmad_unphased_mad_ratio = (\n phasedx2_magdiff_mad/lightcurve_mad\n )\n\n # get the percentiles of the slopes of the adjacent mags for phasedx2\n phasedx2_slopes = np.diff(pmagsx2)/np.diff(phasex2)\n phasedx2_slope_percentiles = np.ravel(np.nanpercentile(phasedx2_slopes,\n [10.0,90.0]))\n phasedx2_slope_10percentile = phasedx2_slope_percentiles[0]\n phasedx2_slope_90percentile = phasedx2_slope_percentiles[1]\n\n # check if nbrtimes, _mags, _errs are available\n if ((nbrtimes is not None) and\n (nbrmags is not None) and\n (nbrerrs is not None)):\n\n # get the finite values\n nfinind = (np.isfinite(nbrtimes) &\n np.isfinite(nbrmags) &\n np.isfinite(nbrerrs))\n nftimes, nfmags, nferrs = (nbrtimes[nfinind],\n nbrmags[nfinind],\n nbrerrs[nfinind])\n\n # get nonzero errors\n nnzind = np.nonzero(nferrs)\n nftimes, nfmags, nferrs = (nftimes[nnzind],\n nfmags[nnzind],\n nferrs[nnzind])\n\n # only operate on LC if enough points\n if nftimes.size > 49:\n\n # get the phased light curve using the same period and epoch as\n # the actual object\n nphasedlc = lcmath.phase_magseries_with_errs(\n nftimes, nfmags, nferrs,\n period, ftimes.min(),\n wrap=False\n )\n\n # normalize the object and neighbor phased mags\n norm_pmags = pmags - np.median(pmags)\n norm_npmags = nphasedlc['mags'] - np.median(nphasedlc['mags'])\n\n # phase bin them both so we can compare LCs easily\n phabinned_objectlc = lcmath.phase_bin_magseries(phase,\n norm_pmags,\n minbinelems=1)\n phabinned_nbrlc = lcmath.phase_bin_magseries(nphasedlc['phase'],\n norm_npmags,\n minbinelems=1)\n\n absdiffs = []\n\n for pha, phamag in zip(phabinned_objectlc['binnedphases'],\n phabinned_objectlc['binnedmags']):\n\n try:\n\n # get the matching phase from the neighbor phased LC\n phadiffs = np.abs(pha - phabinned_nbrlc['binnedphases'])\n minphadiffind = np.where(\n (phadiffs < 1.0e-4) &\n (phadiffs == np.min(phadiffs))\n )\n absmagdiff = np.abs(\n phamag - phabinned_nbrlc['binnedmags'][\n minphadiffind\n ]\n )\n if absmagdiff.size > 0:\n absdiffs.append(absmagdiff.min())\n\n except Exception as e:\n continue\n\n # sum of absdiff between the normalized to 0.0 phased LC of this\n # object and that of the closest neighbor phased with the same\n # period and epoch\n if len(absdiffs) > 0:\n sum_nbr_phasedlc_magdiff = sum(absdiffs)\n else:\n sum_nbr_phasedlc_magdiff = np.nan\n\n else:\n\n sum_nbr_phasedlc_magdiff = np.nan\n\n else:\n sum_nbr_phasedlc_magdiff = np.nan\n\n return {\n 'inveta_unphasedlc':inveta_unphasedlc,\n 'inveta_bestperiod':inveta_bestperiod,\n 'inveta_bestperiodx2':inveta_bestperiodx2,\n 'inveta_ratio_phased_unphased':inveta_ratio_phased_unphased,\n 'inveta_ratio_phasedx2_unphased':inveta_ratio_phasedx2_unphased,\n 'phasedx2_magdiff_maxima':phasedx2_magdiff_maxval,\n 'phasedx2_magdiff_minina':phasedx2_magdiff_minval,\n 'phased_unphased_magdiff_mad_ratio':(\n phased_magdiffmad_unphased_mad_ratio\n ),\n 'phasedx2_unphased_magdiff_mad_ratio':(\n phasedx2_magdiffmad_unphased_mad_ratio\n ),\n 'phasedx2_slope_10percentile':phasedx2_slope_10percentile,\n 'phasedx2_slope_90percentile':phasedx2_slope_90percentile,\n 'sum_nbr_phasedlc_magdiff':sum_nbr_phasedlc_magdiff,\n }\n\n else:\n\n return {\n 'inveta_unphasedlc':np.nan,\n 'inveta_bestperiod':np.nan,\n 'inveta_bestperiodx2':np.nan,\n 'inveta_ratio_phased_unphased':np.nan,\n 'inveta_ratio_phasedx2_unphased':np.nan,\n 'phasedx2_magdiff_maxima':np.nan,\n 'phasedx2_magdiff_minina':np.nan,\n 'phased_unphased_magdiff_mad_ratio':np.nan,\n 'phasedx2_unphased_magdiff_mad_ratio':np.nan,\n 'phasedx2_slope_10percentile':np.nan,\n 'phasedx2_slope_90percentile':np.nan,\n 'sum_nbr_phasedlc_magdiff':np.nan,\n }", "response": "This function calculates the phased LC features for a given object."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncalculate the Stellingwerf PDM theta value at a test frequency.", "response": "def stellingwerf_pdm_theta(times, mags, errs, frequency,\n binsize=0.05, minbin=9):\n '''\n This calculates the Stellingwerf PDM theta value at a test frequency.\n\n Parameters\n ----------\n\n times,mags,errs : np.array\n The input time-series and associated errors.\n\n frequency : float\n The test frequency to calculate the theta statistic at.\n\n binsize : float\n The phase bin size to use.\n\n minbin : int\n The minimum number of items in a phase bin to consider in the\n calculation of the statistic.\n\n Returns\n -------\n\n theta_pdm : float\n The value of the theta statistic at the specified `frequency`.\n\n\n '''\n\n period = 1.0/frequency\n fold_time = times[0]\n\n phased = phase_magseries(times,\n mags,\n period,\n fold_time,\n wrap=False,\n sort=True)\n\n phases = phased['phase']\n pmags = phased['mags']\n bins = nparange(0.0, 1.0, binsize)\n\n binnedphaseinds = npdigitize(phases, bins)\n\n binvariances = []\n binndets = []\n goodbins = 0\n\n for x in npunique(binnedphaseinds):\n\n thisbin_inds = binnedphaseinds == x\n thisbin_mags = pmags[thisbin_inds]\n\n if thisbin_mags.size > minbin:\n thisbin_variance = npvar(thisbin_mags,ddof=1)\n binvariances.append(thisbin_variance)\n binndets.append(thisbin_mags.size)\n goodbins = goodbins + 1\n\n # now calculate theta\n binvariances = nparray(binvariances)\n binndets = nparray(binndets)\n\n theta_top = npsum(binvariances*(binndets - 1)) / (npsum(binndets) -\n goodbins)\n theta_bot = npvar(pmags,ddof=1)\n theta = theta_top/theta_bot\n\n return theta"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef analytic_false_alarm_probability(lspinfo,\n times,\n conservative_nfreq_eff=True,\n peakvals=None,\n inplace=True):\n '''This returns the analytic false alarm probabilities for periodogram\n peak values.\n\n FIXME: this doesn't actually work. Fix later.\n\n The calculation follows that on page 3 of Zechmeister & Kurster (2009)::\n\n FAP = 1 \u2212 [1 \u2212 Prob(z > z0)]**M\n\n where::\n\n M is the number of independent frequencies\n Prob(z > z0) is the probability of peak with value > z0\n z0 is the peak value we're evaluating\n\n For PDM, the Prob(z > z0) is described by the beta distribution, according\n to:\n\n - Schwarzenberg-Czerny (1997;\n https://ui.adsabs.harvard.edu/#abs/1997ApJ...489..941S)\n\n - Zalian, Chadid, and Stellingwerf (2013;\n http://adsabs.harvard.edu/abs/2014MNRAS.440...68Z)\n\n This is given by::\n\n beta( (N-B)/2, (B-1)/2; ((N-B)/(B-1))*theta_pdm )\n\n Where::\n\n N = number of observations\n B = number of phase bins\n\n This translates to a scipy.stats call to the beta distribution CDF::\n\n x = ((N-B)/(B-1))*theta_pdm_best\n prob_exceeds_val = scipy.stats.beta.cdf(x, (N-B)/2.0, (B-1.0)/2.0)\n\n Which we can then plug into the false alarm prob eqn above with the\n calculation of M.\n\n Parameters\n ----------\n\n lspinfo : dict\n The dict returned by the\n :py:func:`~astrobase.periodbase.spdm.stellingwerf_pdm` function.\n\n times : np.array\n The times for which the periodogram result in ``lspinfo`` was\n calculated.\n\n conservative_nfreq_eff : bool\n If True, will follow the prescription given in Schwarzenberg-Czerny\n (2003):\n\n http://adsabs.harvard.edu/abs/2003ASPC..292..383S\n\n and estimate the effective number of independent frequences M_eff as::\n\n min(N_obs, N_freq, DELTA_f/delta_f)\n\n peakvals : sequence or None\n The peak values for which to evaluate the false-alarm probability. If\n None, will calculate this for each of the peak values in the\n ``nbestpeaks`` key of the ``lspinfo`` dict.\n\n inplace : bool\n If True, puts the results of the FAP calculation into the ``lspinfo``\n dict as a list available as ``lspinfo['falsealarmprob']``.\n\n Returns\n -------\n\n list\n The calculated false alarm probabilities for each of the peak values in\n ``peakvals``.\n\n '''\n\n from scipy.stats import beta\n\n frequencies = 1.0/lspinfo['periods']\n\n M = independent_freq_count(frequencies,\n times,\n conservative=conservative_nfreq_eff)\n\n if peakvals is None:\n peakvals = lspinfo['nbestlspvals']\n\n nphasebins = nparange(0.0, 1.0, lspinfo['kwargs']['phasebinsize']).size\n ndet = times.size\n\n false_alarm_probs = []\n\n for peakval in peakvals:\n\n prob_xval = ((ndet-nphasebins)/(nphasebins-1.0))*peakval\n prob_exceeds_val = beta.cdf(prob_xval,\n (ndet-nphasebins)/2.0,\n (nphasebins-1.0)/2.0)\n import ipdb; ipdb.set_trace()\n\n false_alarm_probs.append(1.0 - (1.0 - prob_exceeds_val)**M)\n\n if inplace:\n lspinfo['falsealarmprob'] = false_alarm_probs\n\n return false_alarm_probs", "response": "This function returns the analytic false alarm probability for the given set of time intervals and peak values."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef keplermag_to_sdssr(keplermag, kic_sdssg, kic_sdssr):\n '''Converts magnitude measurements in Kepler band to SDSS r band.\n\n Parameters\n ----------\n\n keplermag : float or array-like\n The Kepler magnitude value(s) to convert to fluxes.\n\n kic_sdssg,kic_sdssr : float or array-like\n The SDSS g and r magnitudes of the object(s) from the Kepler Input\n Catalog. The .llc.fits MAST light curve file for a Kepler object\n contains these values in the FITS extension 0 header.\n\n Returns\n -------\n\n float or array-like\n SDSS r band magnitude(s) converted from the Kepler band magnitude.\n\n '''\n kic_sdssgr = kic_sdssg - kic_sdssr\n\n if kic_sdssgr < 0.8:\n kepsdssr = (keplermag - 0.2*kic_sdssg)/0.8\n else:\n kepsdssr = (keplermag - 0.1*kic_sdssg)/0.9\n return kepsdssr", "response": "Converts magnitude measurements in Kepler band to SDSS r band."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef consolidate_kepler_fitslc(keplerid,\n lcfitsdir,\n normalize=True,\n headerkeys=LCHEADERKEYS,\n datakeys=LCDATAKEYS,\n sapkeys=LCSAPKEYS,\n pdckeys=LCPDCKEYS,\n topkeys=LCTOPKEYS,\n apkeys=LCAPERTUREKEYS):\n '''This gets all Kepler/K2 light curves for the given `keplerid`\n in `lcfitsdir`.\n\n Searches recursively in `lcfitsdir` for all of the files belonging to the\n specified `keplerid`. Sorts the light curves by time. Returns an\n `lcdict`. This is meant to be used to consolidate light curves for a single\n object across Kepler quarters.\n\n NOTE: `keplerid` is an integer (without the leading zeros). This is usually\n the KIC ID.\n\n NOTE: if light curve time arrays contain `nans`, these and their associated\n measurements will be sorted to the end of the final combined arrays.\n\n Parameters\n ----------\n\n keplerid : int\n The Kepler ID of the object to consolidate LCs for, as an integer\n without any leading zeros. This is usually the KIC or EPIC ID.\n\n lcfitsdir : str\n The directory to look in for LCs of the specified object.\n\n normalize : bool\n If True, then each component light curve's SAP_FLUX and PDCSAP_FLUX\n measurements will be normalized to 1.0 by dividing out the median flux\n for the component light curve.\n\n headerkeys : list\n A list of FITS header keys that will be extracted from the FITS light\n curve file. These describe the observations. The default value for this\n is given in `LCHEADERKEYS` above.\n\n datakeys : list\n A list of FITS column names that correspond to the auxiliary\n measurements in the light curve. The default is `LCDATAKEYS` above.\n\n sapkeys : list\n A list of FITS column names that correspond to the SAP flux\n measurements in the light curve. The default is `LCSAPKEYS` above.\n\n pdckeys : list\n A list of FITS column names that correspond to the PDC flux\n measurements in the light curve. The default is `LCPDCKEYS` above.\n\n topkeys : list\n A list of FITS header keys that describe the object in the light\n curve. The default is `LCTOPKEYS` above.\n\n apkeys : list\n A list of FITS header keys that describe the flux measurement apertures\n used by the Kepler/K2 pipeline. The default is `LCAPERTUREKEYS` above.\n\n Returns\n -------\n\n lcdict\n Returns an `lcdict` (this is useable by most astrobase functions for LC\n processing).\n\n '''\n\n LOGINFO('looking for Kepler light curve FITS in %s for %s...' % (lcfitsdir,\n keplerid))\n\n # for Python 3.5 and up, use recursive glob, it appears to be absurdly\n # faster than os.walk\n if sys.version_info[:2] > (3,4):\n\n matching = glob.glob(os.path.join(lcfitsdir,\n '**',\n 'kplr%09i-*_llc.fits' % keplerid),\n recursive=True)\n LOGINFO('found %s files: %s' % (len(matching), repr(matching)))\n\n # for Python < 3.5, use os.walk and glob\n else:\n\n # use the os.walk function to start looking for files in lcfitsdir\n walker = os.walk(lcfitsdir)\n matching = []\n for root, dirs, _files in walker:\n for sdir in dirs:\n searchpath = os.path.join(root,\n sdir,\n 'kplr%09i-*_llc.fits' % keplerid)\n foundfiles = glob.glob(searchpath)\n\n if foundfiles:\n matching.extend(foundfiles)\n LOGINFO('found %s in dir: %s' % (repr(foundfiles),\n os.path.join(root,sdir)))\n\n # now that we've found everything, read them all in\n if len(matching) > 0:\n\n LOGINFO('consolidating...')\n\n # the first file\n consolidated = read_kepler_fitslc(matching[0],\n headerkeys=headerkeys,\n datakeys=datakeys,\n sapkeys=sapkeys,\n pdckeys=pdckeys,\n topkeys=topkeys,\n apkeys=apkeys,\n normalize=normalize)\n\n\n # get the rest of the files\n for lcf in matching:\n consolidated = read_kepler_fitslc(lcf,\n appendto=consolidated,\n headerkeys=headerkeys,\n datakeys=datakeys,\n sapkeys=sapkeys,\n pdckeys=pdckeys,\n topkeys=topkeys,\n apkeys=apkeys,\n normalize=normalize)\n\n # get the sort indices\n # we use time for the columns and quarters for the headers\n LOGINFO('sorting by time...')\n\n # NOTE: nans in time will be sorted to the end of the array\n finiteind = npisfinite(consolidated['time'])\n if npsum(finiteind) < consolidated['time'].size:\n LOGWARNING('some time values are nan! '\n 'measurements at these times will be '\n 'sorted to the end of the column arrays.')\n\n # get the sort index\n column_sort_ind = npargsort(consolidated['time'])\n\n # sort the columns by time\n for col in consolidated['columns']:\n if '.' in col:\n key, subkey = col.split('.')\n consolidated[key][subkey] = (\n consolidated[key][subkey][column_sort_ind]\n )\n else:\n consolidated[col] = consolidated[col][column_sort_ind]\n\n # now sort the headers by quarters\n header_sort_ind = npargsort(consolidated['quarter']).tolist()\n\n # this is a bit convoluted, but whatever: list -> array -> list\n\n for key in ('quarter', 'season', 'datarelease', 'obsmode'):\n consolidated[key] = (\n nparray(consolidated[key])[header_sort_ind].tolist()\n )\n\n for key in ('timesys','bjdoffset','exptime','lcaperture',\n 'aperpixused','aperpixunused','pixarcsec',\n 'channel','skygroup','module','output','ndet'):\n consolidated['lcinfo'][key] = (\n nparray(consolidated['lcinfo'][key])[header_sort_ind].tolist()\n )\n\n for key in ('cdpp3_0','cdpp6_0','cdpp12_0','pdcvar','pdcmethod',\n 'aper_target_total_ratio','aper_target_frac'):\n consolidated['varinfo'][key] = (\n nparray(consolidated['varinfo'][key])[header_sort_ind].tolist()\n )\n\n # finally, return the consolidated lcdict\n return consolidated\n\n # if we didn't find anything, complain\n else:\n\n LOGERROR('could not find any light curves '\n 'for %s in %s or its subdirectories' % (keplerid,\n lcfitsdir))\n return None", "response": "This function returns a dict that associates all of the light curves for a given Kepler object with the given Kepler object."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _epd_residual(coeffs, fluxes, xcc, ycc, bgv, bge):\n '''This is the residual function to minimize using scipy.optimize.leastsq.\n\n Parameters\n ----------\n\n coeffs : array-like of floats\n Contains the EPD coefficients that will be used to generate the EPD fit\n function.\n\n fluxes : array-like\n The flux measurement array being used.\n\n xcc,ycc : array-like\n Arrays of the x and y coordinates associated with each measurement in\n `fluxes`.\n\n bgv,bge : array-like\n Arrays of the flux background value and the flux background error\n associated with each measurement in `fluxes`.\n\n Returns\n -------\n\n np.array\n Contains the fit function residual evaluated at each flux measurement\n value.\n\n '''\n\n f = _epd_function(coeffs, fluxes, xcc, ycc, bgv, bge)\n residual = fluxes - f\n return residual", "response": "This function computes the residual function to minimize using scipy. optimize. leastsq."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef rfepd_kepler_lightcurve(\n lcdict,\n xccol='mom_centr1',\n yccol='mom_centr2',\n timestoignore=None,\n filterflags=True,\n writetodict=True,\n epdsmooth=23,\n decorr='xcc,ycc',\n nrftrees=200\n):\n '''This uses a `RandomForestRegressor` to fit and decorrelate Kepler light\n curves.\n\n Fits the X and Y positions, the background, and background error.\n\n By default, this function removes points in the Kepler LC that have ANY\n quality flags set.\n\n Parameters\n ----------\n\n lcdict : lcdict\n An `lcdict` produced by `consolidate_kepler_fitslc` or\n `read_kepler_fitslc`.\n\n xcol,ycol : str\n Indicates the x and y coordinate column names to use from the Kepler LC\n in the EPD fit.\n\n timestoignore : list of tuples\n This is of the form::\n\n [(time1_start, time1_end), (time2_start, time2_end), ...]\n\n and indicates the start and end times to mask out of the final\n lcdict. Use this to remove anything that wasn't caught by the quality\n flags.\n\n filterflags : bool\n If True, will remove any measurements that have non-zero quality flags\n present. This usually indicates an issue with the instrument or\n spacecraft.\n\n writetodict : bool\n If writetodict is True, adds the following columns to the lcdict::\n\n rfepd_time = time array\n rfepd_sapflux = uncorrected flux before EPD\n rfepd_epdsapflux = corrected flux after EPD\n rfepd_epdsapcorr = EPD flux corrections\n rfepd_bkg = background array\n rfepd_bkg_err = background errors array\n rfepd_xcc = xcoord array\n rfepd_ycc = ycoord array\n rfepd_quality = quality flag array\n\n and updates the 'columns' list in the lcdict as well.\n\n epdsmooth : int\n Sets the number of light curve points to smooth over when generating the\n EPD fit function.\n\n decorr : {'xcc,ycc','bgv,bge','xcc,ycc,bgv,bge'}\n Indicates whether to use the x,y coords alone; background value and\n error alone; or x,y coords and background value, error in combination as\n the features to training the `RandomForestRegressor` on and perform the\n fit.\n\n nrftrees : int\n The number of trees to use in the `RandomForestRegressor`.\n\n Returns\n -------\n\n tuple\n Returns a tuple of the form: (times, corrected_fluxes, flux_corrections)\n\n '''\n\n times, fluxes, background, background_err = (\n lcdict['time'],\n lcdict['sap']['sap_flux'],\n lcdict['sap']['sap_bkg'],\n lcdict['sap']['sap_bkg_err']\n )\n xcc = lcdict[xccol]\n ycc = lcdict[yccol]\n flags = lcdict['sap_quality']\n\n # filter all bad LC points as noted by quality flags\n if filterflags:\n\n nbefore = times.size\n\n filterind = flags == 0\n\n times = times[filterind]\n fluxes = fluxes[filterind]\n background = background[filterind]\n background_err = background_err[filterind]\n xcc = xcc[filterind]\n ycc = ycc[filterind]\n flags = flags[filterind]\n\n nafter = times.size\n LOGINFO('applied quality flag filter, ndet before = %s, '\n 'ndet after = %s'\n % (nbefore, nafter))\n\n\n # remove nans\n find = (npisfinite(xcc) & npisfinite(ycc) &\n npisfinite(times) & npisfinite(fluxes) &\n npisfinite(background) & npisfinite(background_err))\n\n nbefore = times.size\n\n times = times[find]\n fluxes = fluxes[find]\n background = background[find]\n background_err = background_err[find]\n xcc = xcc[find]\n ycc = ycc[find]\n flags = flags[find]\n\n nafter = times.size\n LOGINFO('removed nans, ndet before = %s, ndet after = %s'\n % (nbefore, nafter))\n\n\n # exclude all times in timestoignore\n if (timestoignore and\n isinstance(timestoignore, list) and\n len(timestoignore) > 0):\n\n exclind = npfull_like(times,True)\n\n nbefore = times.size\n\n # apply all the masks\n for ignoretime in timestoignore:\n time0, time1 = ignoretime[0], ignoretime[1]\n thismask = (times > time0) & (times < time1)\n exclind = exclind & thismask\n\n # quantities after masks have been applied\n times = times[exclind]\n fluxes = fluxes[exclind]\n background = background[exclind]\n background_err = background_err[exclind]\n xcc = xcc[exclind]\n ycc = ycc[exclind]\n flags = flags[exclind]\n\n nafter = times.size\n LOGINFO('removed timestoignore, ndet before = %s, ndet after = %s'\n % (nbefore, nafter))\n\n\n # now that we're all done, we can do EPD\n\n # set up the regressor\n RFR = RandomForestRegressor(n_estimators=nrftrees)\n\n if decorr == 'xcc,ycc,bgv,bge':\n # collect the features and target variable\n features = npcolumn_stack((xcc,ycc,background,background_err))\n elif decorr == 'xcc,ycc':\n # collect the features and target variable\n features = npcolumn_stack((xcc,ycc))\n elif decorr == 'bgv,bge':\n # collect the features and target variable\n features = npcolumn_stack((background,background_err))\n else:\n LOGERROR(\"couldn't understand decorr, not decorrelating...\")\n return None\n\n # smooth the light curve\n if epdsmooth:\n smoothedfluxes = median_filter(fluxes, size=epdsmooth)\n else:\n smoothedfluxes = fluxes\n\n # fit, then generate the predicted values, then get corrected values\n RFR.fit(features, smoothedfluxes)\n flux_corrections = RFR.predict(features)\n corrected_fluxes = npmedian(fluxes) + fluxes - flux_corrections\n\n # remove the random forest to save RAM\n del RFR\n\n # write these to the dictionary if requested\n if writetodict:\n\n lcdict['rfepd'] = {}\n lcdict['rfepd']['time'] = times\n lcdict['rfepd']['sapflux'] = fluxes\n lcdict['rfepd']['epdsapflux'] = corrected_fluxes\n lcdict['rfepd']['epdsapcorr'] = flux_corrections\n lcdict['rfepd']['bkg'] = background\n lcdict['rfepd']['bkg_err'] = background_err\n lcdict['rfepd']['xcc'] = xcc\n lcdict['rfepd']['ycc'] = ycc\n lcdict['rfepd']['quality'] = flags\n\n for newcol in ['rfepd.time','rfepd.sapflux',\n 'rfepd.epdsapflux','rfepd.epdsapcorr',\n 'rfepd.bkg','rfepd.bkg.err',\n 'rfepd.xcc','rfepd.ycc',\n 'rfepd.quality']:\n\n if newcol not in lcdict['columns']:\n lcdict['columns'].append(newcol)\n\n return times, corrected_fluxes, flux_corrections", "response": "This function uses a RandomForestRegressor to fit and decorrelate Kepler lightcurve curves."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _get_legendre_deg_ctd(npts):\n '''This is a helper function for centroid detrending.\n\n '''\n\n from scipy.interpolate import interp1d\n\n degs = nparray([4,5,6,10,15])\n pts = nparray([1e2,3e2,5e2,1e3,3e3])\n fn = interp1d(pts, degs, kind='linear',\n bounds_error=False,\n fill_value=(min(degs), max(degs)))\n legendredeg = int(npfloor(fn(npts)))\n\n return legendredeg", "response": "This is a helper function for centroid detrending.\n "} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef parallel_timebin(lclist,\n binsizesec,\n maxobjects=None,\n outdir=None,\n lcformat='hat-sql',\n lcformatdir=None,\n timecols=None,\n magcols=None,\n errcols=None,\n minbinelems=7,\n nworkers=NCPUS,\n maxworkertasks=1000):\n '''This time-bins all the LCs in the list using the specified bin size.\n\n Parameters\n ----------\n\n lclist : list of str\n The input LCs to process.\n\n binsizesec : float\n The time bin size to use in seconds.\n\n maxobjects : int or None\n If provided, LC processing will stop at `lclist[maxobjects]`.\n\n outdir : str or None\n The directory where output LCs will be written. If None, will write to\n the same directory as the input LCs.\n\n lcformat : str\n This is the `formatkey` associated with your light curve format, which\n you previously passed in to the `lcproc.register_lcformat`\n function. This will be used to look up how to find and read the light\n curve file.\n\n lcformatdir : str or None\n If this is provided, gives the path to a directory when you've stored\n your lcformat description JSONs, other than the usual directories lcproc\n knows to search for them in. Use this along with `lcformat` to specify\n an LC format JSON file that's not currently registered with lcproc.\n\n timecols,magcols,errcols : lists of str\n The keys in the lcdict produced by your light curve reader function that\n correspond to the times, mags/fluxes, and associated measurement errors\n that will be used as inputs to the binning process. If these are None,\n the default values for `timecols`, `magcols`, and `errcols` for your\n light curve format will be used here.\n\n minbinelems : int\n The minimum number of time-bin elements required to accept a time-bin as\n valid for the output binned light curve.\n\n nworkers : int\n Number of parallel workers to launch.\n\n maxworkertasks : int\n The maximum number of tasks a parallel worker will complete before being\n replaced to guard against memory leaks.\n\n Returns\n -------\n\n dict\n The returned dict contains keys = input LCs, vals = output LCs.\n\n '''\n\n if outdir and not os.path.exists(outdir):\n os.mkdir(outdir)\n\n if maxobjects is not None:\n lclist = lclist[:maxobjects]\n\n tasks = [(x, binsizesec, {'outdir':outdir,\n 'lcformat':lcformat,\n 'lcformatdir':lcformatdir,\n 'timecols':timecols,\n 'magcols':magcols,\n 'errcols':errcols,\n 'minbinelems':minbinelems}) for x in lclist]\n\n pool = mp.Pool(nworkers, maxtasksperchild=maxworkertasks)\n results = pool.map(timebinlc_worker, tasks)\n pool.close()\n pool.join()\n\n resdict = {os.path.basename(x):y for (x,y) in zip(lclist, results)}\n\n return resdict", "response": "This function processes the given list of LCs and returns a list of LCs in the output LCs in parallel with the specified time - bin size."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef parallel_timebin_lcdir(lcdir,\n binsizesec,\n maxobjects=None,\n outdir=None,\n lcformat='hat-sql',\n lcformatdir=None,\n timecols=None,\n magcols=None,\n errcols=None,\n minbinelems=7,\n nworkers=NCPUS,\n maxworkertasks=1000):\n '''\n This time bins all the light curves in the specified directory.\n\n Parameters\n ----------\n\n lcdir : list of str\n Directory containing the input LCs to process.\n\n binsizesec : float\n The time bin size to use in seconds.\n\n maxobjects : int or None\n If provided, LC processing will stop at `lclist[maxobjects]`.\n\n outdir : str or None\n The directory where output LCs will be written. If None, will write to\n the same directory as the input LCs.\n\n lcformat : str\n This is the `formatkey` associated with your light curve format, which\n you previously passed in to the `lcproc.register_lcformat`\n function. This will be used to look up how to find and read the light\n curve file.\n\n lcformatdir : str or None\n If this is provided, gives the path to a directory when you've stored\n your lcformat description JSONs, other than the usual directories lcproc\n knows to search for them in. Use this along with `lcformat` to specify\n an LC format JSON file that's not currently registered with lcproc.\n\n timecols,magcols,errcols : lists of str\n The keys in the lcdict produced by your light curve reader function that\n correspond to the times, mags/fluxes, and associated measurement errors\n that will be used as inputs to the binning process. If these are None,\n the default values for `timecols`, `magcols`, and `errcols` for your\n light curve format will be used here.\n\n minbinelems : int\n The minimum number of time-bin elements required to accept a time-bin as\n valid for the output binned light curve.\n\n nworkers : int\n Number of parallel workers to launch.\n\n maxworkertasks : int\n The maximum number of tasks a parallel worker will complete before being\n replaced to guard against memory leaks.\n\n Returns\n -------\n\n dict\n The returned dict contains keys = input LCs, vals = output LCs.\n\n '''\n try:\n formatinfo = get_lcformat(lcformat,\n use_lcformat_dir=lcformatdir)\n if formatinfo:\n (fileglob, readerfunc,\n dtimecols, dmagcols, derrcols,\n magsarefluxes, normfunc) = formatinfo\n else:\n LOGERROR(\"can't figure out the light curve format\")\n return None\n except Exception as e:\n LOGEXCEPTION(\"can't figure out the light curve format\")\n return None\n\n lclist = sorted(glob.glob(os.path.join(lcdir, fileglob)))\n\n return parallel_timebin(lclist,\n binsizesec,\n maxobjects=maxobjects,\n outdir=outdir,\n lcformat=lcformat,\n timecols=timecols,\n magcols=magcols,\n errcols=errcols,\n minbinelems=minbinelems,\n nworkers=nworkers,\n maxworkertasks=maxworkertasks)", "response": "This function processes the given directory of time - bins and returns a list of str objects that are available in the output directory."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _varfeatures_worker(task):\n '''\n This wraps varfeatures.\n\n '''\n\n try:\n (lcfile, outdir, timecols, magcols, errcols,\n mindet, lcformat, lcformatdir) = task\n return get_varfeatures(lcfile, outdir,\n timecols=timecols,\n magcols=magcols,\n errcols=errcols,\n mindet=mindet,\n lcformat=lcformat,\n lcformatdir=lcformatdir)\n\n except Exception as e:\n return None", "response": "This wraps varfeatures.\n This wraps varfeatures.\n "} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef serial_varfeatures(lclist,\n outdir,\n maxobjects=None,\n timecols=None,\n magcols=None,\n errcols=None,\n mindet=1000,\n lcformat='hat-sql',\n lcformatdir=None):\n '''This runs variability feature extraction for a list of LCs.\n\n Parameters\n ----------\n\n lclist : list of str\n The list of light curve file names to process.\n\n outdir : str\n The directory where the output varfeatures pickle files will be written.\n\n maxobjects : int\n The number of LCs to process from `lclist`.\n\n timecols : list of str or None\n The timecol keys to use from the lcdict in calculating the features.\n\n magcols : list of str or None\n The magcol keys to use from the lcdict in calculating the features.\n\n errcols : list of str or None\n The errcol keys to use from the lcdict in calculating the features.\n\n mindet : int\n The minimum number of LC points required to generate variability\n features.\n\n lcformat : str\n This is the `formatkey` associated with your light curve format, which\n you previously passed in to the `lcproc.register_lcformat`\n function. This will be used to look up how to find and read the light\n curves specified in `basedir` or `use_list_of_filenames`.\n\n lcformatdir : str or None\n If this is provided, gives the path to a directory when you've stored\n your lcformat description JSONs, other than the usual directories lcproc\n knows to search for them in. Use this along with `lcformat` to specify\n an LC format JSON file that's not currently registered with lcproc.\n\n Returns\n -------\n\n list of str\n List of the generated variability features pickles for the input LCs,\n with results for each magcol in the input `magcol` or light curve\n format's default `magcol` list.\n\n '''\n\n if maxobjects:\n lclist = lclist[:maxobjects]\n\n tasks = [(x, outdir, timecols, magcols, errcols,\n mindet, lcformat, lcformatdir)\n for x in lclist]\n\n for task in tqdm(tasks):\n result = _varfeatures_worker(task)\n\n return result", "response": "This function runs the variability feature extraction for a list of LCs and outputs a list of varfeatures pickles for each magcol in the input LCs."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef parallel_varfeatures(lclist,\n outdir,\n maxobjects=None,\n timecols=None,\n magcols=None,\n errcols=None,\n mindet=1000,\n lcformat='hat-sql',\n lcformatdir=None,\n nworkers=NCPUS):\n '''This runs variable feature extraction in parallel for all LCs in `lclist`.\n\n Parameters\n ----------\n\n lclist : list of str\n The list of light curve file names to process.\n\n outdir : str\n The directory where the output varfeatures pickle files will be written.\n\n maxobjects : int\n The number of LCs to process from `lclist`.\n\n timecols : list of str or None\n The timecol keys to use from the lcdict in calculating the features.\n\n magcols : list of str or None\n The magcol keys to use from the lcdict in calculating the features.\n\n errcols : list of str or None\n The errcol keys to use from the lcdict in calculating the features.\n\n mindet : int\n The minimum number of LC points required to generate variability\n features.\n\n lcformat : str\n This is the `formatkey` associated with your light curve format, which\n you previously passed in to the `lcproc.register_lcformat`\n function. This will be used to look up how to find and read the light\n curves specified in `basedir` or `use_list_of_filenames`.\n\n lcformatdir : str or None\n If this is provided, gives the path to a directory when you've stored\n your lcformat description JSONs, other than the usual directories lcproc\n knows to search for them in. Use this along with `lcformat` to specify\n an LC format JSON file that's not currently registered with lcproc.\n\n nworkers : int\n The number of parallel workers to launch.\n\n Returns\n -------\n\n dict\n A dict with key:val pairs of input LC file name : the generated\n variability features pickles for each of the input LCs, with results for\n each magcol in the input `magcol` or light curve format's default\n `magcol` list.\n\n '''\n # make sure to make the output directory if it doesn't exist\n if not os.path.exists(outdir):\n os.makedirs(outdir)\n\n if maxobjects:\n lclist = lclist[:maxobjects]\n\n tasks = [(x, outdir, timecols, magcols, errcols, mindet,\n lcformat, lcformatdir) for x in lclist]\n\n with ProcessPoolExecutor(max_workers=nworkers) as executor:\n resultfutures = executor.map(varfeatures_worker, tasks)\n\n results = [x for x in resultfutures]\n resdict = {os.path.basename(x):y for (x,y) in zip(lclist, results)}\n\n return resdict", "response": "This function runs varfeatures extraction in parallel for all LCs in lclist."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef parallel_varfeatures_lcdir(lcdir,\n outdir,\n fileglob=None,\n maxobjects=None,\n timecols=None,\n magcols=None,\n errcols=None,\n recursive=True,\n mindet=1000,\n lcformat='hat-sql',\n lcformatdir=None,\n nworkers=NCPUS):\n '''This runs parallel variable feature extraction for a directory of LCs.\n\n Parameters\n ----------\n\n lcdir : str\n The directory of light curve files to process.\n\n outdir : str\n The directory where the output varfeatures pickle files will be written.\n\n fileglob : str or None\n The file glob to use when looking for light curve files in `lcdir`. If\n None, the default file glob associated for this LC format will be used.\n\n maxobjects : int\n The number of LCs to process from `lclist`.\n\n timecols : list of str or None\n The timecol keys to use from the lcdict in calculating the features.\n\n magcols : list of str or None\n The magcol keys to use from the lcdict in calculating the features.\n\n errcols : list of str or None\n The errcol keys to use from the lcdict in calculating the features.\n\n mindet : int\n The minimum number of LC points required to generate variability\n features.\n\n lcformat : str\n This is the `formatkey` associated with your light curve format, which\n you previously passed in to the `lcproc.register_lcformat`\n function. This will be used to look up how to find and read the light\n curves specified in `basedir` or `use_list_of_filenames`.\n\n lcformatdir : str or None\n If this is provided, gives the path to a directory when you've stored\n your lcformat description JSONs, other than the usual directories lcproc\n knows to search for them in. Use this along with `lcformat` to specify\n an LC format JSON file that's not currently registered with lcproc.\n\n nworkers : int\n The number of parallel workers to launch.\n\n Returns\n -------\n\n dict\n A dict with key:val pairs of input LC file name : the generated\n variability features pickles for each of the input LCs, with results for\n each magcol in the input `magcol` or light curve format's default\n `magcol` list.\n\n '''\n\n try:\n formatinfo = get_lcformat(lcformat,\n use_lcformat_dir=lcformatdir)\n if formatinfo:\n (dfileglob, readerfunc,\n dtimecols, dmagcols, derrcols,\n magsarefluxes, normfunc) = formatinfo\n else:\n LOGERROR(\"can't figure out the light curve format\")\n return None\n except Exception as e:\n LOGEXCEPTION(\"can't figure out the light curve format\")\n return None\n\n if not fileglob:\n fileglob = dfileglob\n\n # now find the files\n LOGINFO('searching for %s light curves in %s ...' % (lcformat, lcdir))\n\n if recursive is False:\n matching = glob.glob(os.path.join(lcdir, fileglob))\n\n else:\n # use recursive glob for Python 3.5+\n if sys.version_info[:2] > (3,4):\n\n matching = glob.glob(os.path.join(lcdir,\n '**',\n fileglob),\n recursive=True)\n\n # otherwise, use os.walk and glob\n else:\n\n # use os.walk to go through the directories\n walker = os.walk(lcdir)\n matching = []\n\n for root, dirs, _files in walker:\n for sdir in dirs:\n searchpath = os.path.join(root,\n sdir,\n fileglob)\n foundfiles = glob.glob(searchpath)\n\n if foundfiles:\n matching.extend(foundfiles)\n\n\n # now that we have all the files, process them\n if matching and len(matching) > 0:\n\n LOGINFO('found %s light curves, getting varfeatures...' %\n len(matching))\n\n return parallel_varfeatures(matching,\n outdir,\n maxobjects=maxobjects,\n timecols=timecols,\n magcols=magcols,\n errcols=errcols,\n mindet=mindet,\n lcformat=lcformat,\n lcformatdir=lcformatdir,\n nworkers=nworkers)\n\n else:\n\n LOGERROR('no light curve files in %s format found in %s' % (lcformat,\n lcdir))\n return None", "response": "This function runs the parallel varfeatures extraction for a directory of LCs."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef checkplot_pickle_to_png(\n checkplotin,\n outfile,\n extrarows=None\n):\n '''This reads the checkplot pickle or dict provided, and writes out a PNG.\n\n The output PNG contains most of the information in the input checkplot\n pickle/dict, and can be used to quickly glance through the highlights\n instead of having to review the checkplot with the `checkplotserver`\n webapp. This is useful for exporting read-only views of finalized checkplots\n from the `checkplotserver` as well, to share them with other people.\n\n The PNG has 4 x N tiles::\n\n [ finder ] [ objectinfo ] [ varinfo/comments ] [ unphased LC ]\n [ periodogram1 ] [ phased LC P1 ] [ phased LC P2 ] [ phased LC P3 ]\n [ periodogram2 ] [ phased LC P1 ] [ phased LC P2 ] [ phased LC P3 ]\n .\n .\n [ periodogramN ] [ phased LC P1 ] [ phased LC P2 ] [ phased LC P3 ]\n\n for N independent period-finding methods producing:\n\n - periodogram1,2,3...N: the periodograms from each method\n\n - phased LC P1,P2,P3: the phased lightcurves using the best 3 peaks in each\n periodogram\n\n Parameters\n ----------\n\n checkplotin : dict or str\n This is either a checkplotdict produced by\n :py:func:`astrobase.checkplot.pkl.checkplot_dict` or a checkplot pickle\n file produced by :py:func:`astrobase.checkplot.pkl.checkplot_pickle`.\n\n outfile : str\n The filename of the output PNG file to create.\n\n extrarows : list of tuples\n This is a list of 4-element tuples containing paths to PNG files that\n will be added to the end of the rows generated from the checkplotin\n pickle/dict. Each tuple represents a row in the final output PNG\n file. If there are less than 4 elements per tuple, the missing elements\n will be filled in with white-space. If there are more than 4 elements\n per tuple, only the first four will be used.\n\n The purpose of this kwarg is to incorporate periodograms and phased LC\n plots (in the form of PNGs) generated from an external period-finding\n function or program (like VARTOOLS) to allow for comparison with\n astrobase results.\n\n NOTE: the PNG files specified in `extrarows` here will be added to those\n already present in the input checkplotdict['externalplots'] if that is\n None because you passed in a similar list of external plots to the\n :py:func:`astrobase.checkplot.pkl.checkplot_pickle` function earlier. In\n this case, `extrarows` can be used to add even more external plots if\n desired.\n\n Each external plot PNG will be resized to 750 x 480 pixels to fit into\n an output image cell.\n\n By convention, each 4-element tuple should contain:\n\n - a periodiogram PNG\n - phased LC PNG with 1st best peak period from periodogram\n - phased LC PNG with 2nd best peak period from periodogram\n - phased LC PNG with 3rd best peak period from periodogram\n\n Example of extrarows::\n\n [('/path/to/external/bls-periodogram.png',\n '/path/to/external/bls-phasedlc-plot-bestpeak.png',\n '/path/to/external/bls-phasedlc-plot-peak2.png',\n '/path/to/external/bls-phasedlc-plot-peak3.png'),\n ('/path/to/external/pdm-periodogram.png',\n '/path/to/external/pdm-phasedlc-plot-bestpeak.png',\n '/path/to/external/pdm-phasedlc-plot-peak2.png',\n '/path/to/external/pdm-phasedlc-plot-peak3.png'),\n ...]\n\n Returns\n -------\n\n str\n The absolute path to the generated checkplot PNG.\n\n '''\n\n # figure out if the checkplotpickle is a filename\n # python 3\n if sys.version_info[:2] > (3,2):\n\n if (isinstance(checkplotin, str) and os.path.exists(checkplotin)):\n cpd = _read_checkplot_picklefile(checkplotin)\n elif isinstance(checkplotin, dict):\n cpd = checkplotin\n else:\n LOGERROR('checkplotin: %s of type %s is not a '\n 'valid checkplot filename (or does not exist), or a dict' %\n (os.path.abspath(checkplotin), type(checkplotin)))\n return None\n\n # check for unicode in python 2.7\n else:\n\n # get the current checkplotdict\n if ((isinstance(checkplotin, str) or\n isinstance(checkplotin, unicode)) and\n os.path.exists(checkplotin)):\n cpd = _read_checkplot_picklefile(checkplotin)\n elif isinstance(checkplotin,dict):\n cpd = checkplotin\n else:\n LOGERROR('checkplotin: %s of type %s is not a '\n 'valid checkplot filename (or does not exist), or a dict' %\n (os.path.abspath(checkplotin), type(checkplotin)))\n return None\n\n # figure out the dimensions of the output png\n # each cell is 750 x 480 pixels\n # a row is made of four cells\n # - the first row is for object info\n # - the rest are for periodograms and phased LCs, one row per method\n # if there are more than three phased LC plots per method, we'll only plot 3\n if 'pfmethods' in cpd:\n cplspmethods = cpd['pfmethods']\n else:\n cplspmethods = []\n for pfm in METHODSHORTLABELS:\n if pfm in cpd:\n cplspmethods.append(pfm)\n\n\n cprows = len(cplspmethods)\n\n # add in any extra rows from neighbors\n if 'neighbors' in cpd and cpd['neighbors'] and len(cpd['neighbors']) > 0:\n nbrrows = len(cpd['neighbors'])\n else:\n nbrrows = 0\n\n # add in any extra rows from keyword arguments\n if extrarows and len(extrarows) > 0:\n erows = len(extrarows)\n else:\n erows = 0\n\n # add in any extra rows from the checkplot dict\n if ('externalplots' in cpd and\n cpd['externalplots'] and\n len(cpd['externalplots']) > 0):\n cpderows = len(cpd['externalplots'])\n else:\n cpderows = 0\n\n totalwidth = 3000\n totalheight = 480 + (cprows + erows + nbrrows + cpderows)*480\n\n # this is the output PNG\n outimg = Image.new('RGBA',(totalwidth, totalheight),(255,255,255,255))\n\n # now fill in the rows of the output png. we'll use Pillow to build up the\n # output image from the already stored plots and stuff in the checkplot\n # dict.\n\n ###############################\n # row 1, cell 1: finder chart #\n ###############################\n\n if cpd['finderchart']:\n finder = Image.open(\n _base64_to_file(cpd['finderchart'], None, writetostrio=True)\n )\n bigfinder = finder.resize((450,450), Image.ANTIALIAS)\n outimg.paste(bigfinder,(150,20))\n\n #####################################\n # row 1, cell 2: object information #\n #####################################\n\n # find the font we need from the package data\n fontpath = os.path.abspath(\n os.path.join(os.path.dirname(__file__),\n '..',\n 'cpserver',\n 'cps-assets',\n 'DejaVuSans.ttf')\n )\n # load the font\n if os.path.exists(fontpath):\n cpfontnormal = ImageFont.truetype(fontpath, 20)\n cpfontlarge = ImageFont.truetype(fontpath, 28)\n else:\n LOGWARNING('could not find bundled '\n 'DejaVu Sans font in the astrobase package '\n 'data, using ugly defaults...')\n cpfontnormal = ImageFont.load_default()\n cpfontlarge = ImageFont.load_default()\n\n # the image draw object\n objinfodraw = ImageDraw.Draw(outimg)\n\n # write out the object information\n\n # objectid\n objinfodraw.text(\n (625, 25),\n cpd['objectid'] if cpd['objectid'] else 'no objectid',\n font=cpfontlarge,\n fill=(0,0,255,255)\n )\n # twomass id\n if 'twomassid' in cpd['objectinfo']:\n objinfodraw.text(\n (625, 60),\n ('2MASS J%s' % cpd['objectinfo']['twomassid']\n if cpd['objectinfo']['twomassid']\n else ''),\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n # ndet\n if 'ndet' in cpd['objectinfo']:\n objinfodraw.text(\n (625, 85),\n ('LC points: %s' % cpd['objectinfo']['ndet']\n if cpd['objectinfo']['ndet'] is not None\n else ''),\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n else:\n objinfodraw.text(\n (625, 85),\n ('LC points: %s' % cpd['magseries']['times'].size),\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n # coords and PM\n objinfodraw.text(\n (625, 125),\n ('Coords and PM'),\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n if 'ra' in cpd['objectinfo'] and 'decl' in cpd['objectinfo']:\n objinfodraw.text(\n (900, 125),\n (('RA, Dec: %.3f, %.3f' %\n (cpd['objectinfo']['ra'], cpd['objectinfo']['decl']))\n if (cpd['objectinfo']['ra'] is not None and\n cpd['objectinfo']['decl'] is not None)\n else ''),\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n else:\n objinfodraw.text(\n (900, 125),\n 'RA, Dec: nan, nan',\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n\n if 'propermotion' in cpd['objectinfo']:\n objinfodraw.text(\n (900, 150),\n (('Total PM: %.5f mas/yr' % cpd['objectinfo']['propermotion'])\n if (cpd['objectinfo']['propermotion'] is not None)\n else ''),\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n else:\n objinfodraw.text(\n (900, 150),\n 'Total PM: nan',\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n\n if 'rpmj' in cpd['objectinfo']:\n objinfodraw.text(\n (900, 175),\n (('Reduced PM [Jmag]: %.3f' % cpd['objectinfo']['rpmj'])\n if (cpd['objectinfo']['rpmj'] is not None)\n else ''),\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n else:\n objinfodraw.text(\n (900, 175),\n 'Reduced PM [Jmag]: nan',\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n\n # here, we have to deal with two generations of objectinfo dicts\n\n # first, deal with the new generation of objectinfo dicts\n if 'available_dereddened_bands' in cpd['objectinfo']:\n\n #\n # first, we deal with the bands and mags\n #\n # magnitudes\n objinfodraw.text(\n (625, 200),\n 'Magnitudes',\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n\n # process the various bands\n # if dereddened mags aren't available, use the observed mags\n if len(cpd['objectinfo']['available_bands']) > 0:\n\n # we'll get all the available mags\n for bandind, band, label in zip(\n range(len(cpd['objectinfo']['available_bands'])),\n cpd['objectinfo']['available_bands'],\n cpd['objectinfo']['available_band_labels']\n ):\n\n thisbandmag = cpd['objectinfo'][band]\n\n # we'll draw stuff in three rows depending on the number of\n # bands we have to use\n if bandind in (0,1,2,3,4):\n\n thispos = (900+125*bandind, 200)\n\n objinfodraw.text(\n thispos,\n '%s: %.3f' % (label, thisbandmag),\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n\n elif bandind in (5,6,7,8,9):\n\n rowbandind = bandind - 5\n\n thispos = (900+125*rowbandind, 225)\n\n objinfodraw.text(\n thispos,\n '%s: %.3f' % (label, thisbandmag),\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n\n else:\n\n rowbandind = bandind - 10\n\n thispos = (900+125*rowbandind, 250)\n\n objinfodraw.text(\n thispos,\n '%s: %.3f' % (label, thisbandmag),\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n\n\n #\n # next, deal with the colors\n #\n # colors\n if ('dereddened' in cpd['objectinfo'] and\n cpd['objectinfo']['dereddened'] is True):\n deredlabel = \"(dereddened)\"\n else:\n deredlabel = \"\"\n\n objinfodraw.text(\n (625, 275),\n 'Colors %s' % deredlabel,\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n\n if len(cpd['objectinfo']['available_colors']) > 0:\n\n # we'll get all the available mags (dereddened versions preferred)\n for colorind, color, colorlabel in zip(\n range(len(cpd['objectinfo']['available_colors'])),\n cpd['objectinfo']['available_colors'],\n cpd['objectinfo']['available_color_labels']\n ):\n\n thiscolor = cpd['objectinfo'][color]\n\n # we'll draw stuff in three rows depending on the number of\n # bands we have to use\n if colorind in (0,1,2,3,4):\n\n thispos = (900+150*colorind, 275)\n\n objinfodraw.text(\n thispos,\n '%s: %.3f' % (colorlabel, thiscolor),\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n\n elif colorind in (5,6,7,8,9):\n\n thisrowind = colorind - 5\n thispos = (900+150*thisrowind, 300)\n\n objinfodraw.text(\n thispos,\n '%s: %.3f' % (colorlabel, thiscolor),\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n\n elif colorind in (10,11,12,13,14):\n\n thisrowind = colorind - 10\n thispos = (900+150*thisrowind, 325)\n\n objinfodraw.text(\n thispos,\n '%s: %.3f' % (colorlabel, thiscolor),\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n\n else:\n\n thisrowind = colorind - 15\n thispos = (900+150*thisrowind, 350)\n\n objinfodraw.text(\n thispos,\n '%s: %.3f' % (colorlabel, thiscolor),\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n\n # otherwise, deal with older generation of checkplots\n else:\n\n objinfodraw.text(\n (625, 200),\n ('Magnitudes'),\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n\n objinfodraw.text(\n (900, 200),\n ('gri: %.3f, %.3f, %.3f' %\n ((cpd['objectinfo']['sdssg'] if\n ('sdssg' in cpd['objectinfo'] and\n cpd['objectinfo']['sdssg'] is not None)\n else npnan),\n (cpd['objectinfo']['sdssr'] if\n ('sdssr' in cpd['objectinfo'] and\n cpd['objectinfo']['sdssr'] is not None)\n else npnan),\n (cpd['objectinfo']['sdssi'] if\n ('sdssi' in cpd['objectinfo'] and\n cpd['objectinfo']['sdssi'] is not None)\n else npnan))),\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n objinfodraw.text(\n (900, 225),\n ('JHK: %.3f, %.3f, %.3f' %\n ((cpd['objectinfo']['jmag'] if\n ('jmag' in cpd['objectinfo'] and\n cpd['objectinfo']['jmag'] is not None)\n else npnan),\n (cpd['objectinfo']['hmag'] if\n ('hmag' in cpd['objectinfo'] and\n cpd['objectinfo']['hmag'] is not None)\n else npnan),\n (cpd['objectinfo']['kmag'] if\n ('kmag' in cpd['objectinfo'] and\n cpd['objectinfo']['kmag'] is not None)\n else npnan))),\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n objinfodraw.text(\n (900, 250),\n ('BV: %.3f, %.3f' %\n ((cpd['objectinfo']['bmag'] if\n ('bmag' in cpd['objectinfo'] and\n cpd['objectinfo']['bmag'] is not None)\n else npnan),\n (cpd['objectinfo']['vmag'] if\n ('vmag' in cpd['objectinfo'] and\n cpd['objectinfo']['vmag'] is not None)\n else npnan))),\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n\n # colors\n if ('dereddened' in cpd['objectinfo'] and\n cpd['objectinfo']['dereddened'] is True):\n deredlabel = \"(dereddened)\"\n else:\n deredlabel = \"\"\n\n objinfodraw.text(\n (625, 275),\n 'Colors %s' % deredlabel,\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n\n objinfodraw.text(\n (900, 275),\n ('B - V: %.3f, V - K: %.3f' %\n ( (cpd['objectinfo']['bvcolor'] if\n ('bvcolor' in cpd['objectinfo'] and\n cpd['objectinfo']['bvcolor'] is not None)\n else npnan),\n (cpd['objectinfo']['vkcolor'] if\n ('vkcolor' in cpd['objectinfo'] and\n cpd['objectinfo']['vkcolor'] is not None)\n else npnan) )),\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n objinfodraw.text(\n (900, 300),\n ('i - J: %.3f, g - K: %.3f' %\n ( (cpd['objectinfo']['ijcolor'] if\n ('ijcolor' in cpd['objectinfo'] and\n cpd['objectinfo']['ijcolor'] is not None)\n else npnan),\n (cpd['objectinfo']['gkcolor'] if\n ('gkcolor' in cpd['objectinfo'] and\n cpd['objectinfo']['gkcolor'] is not None)\n else npnan) )),\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n objinfodraw.text(\n (900, 325),\n ('J - K: %.3f' %\n ( (cpd['objectinfo']['jkcolor'] if\n ('jkcolor' in cpd['objectinfo'] and\n cpd['objectinfo']['jkcolor'] is not None)\n else npnan),) ),\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n\n #\n # rest of the object information\n #\n\n # color classification\n if ('color_classes' in cpd['objectinfo'] and\n cpd['objectinfo']['color_classes']):\n\n objinfodraw.text(\n (625, 375),\n ('star classification by color: %s' %\n (', '.join(cpd['objectinfo']['color_classes']))),\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n\n # GAIA neighbors\n if ( ('gaia_neighbors' in cpd['objectinfo']) and\n (cpd['objectinfo']['gaia_neighbors'] is not None) and\n (np.isfinite(cpd['objectinfo']['gaia_neighbors'])) and\n ('searchradarcsec' in cpd['objectinfo']) and\n (cpd['objectinfo']['searchradarcsec']) ):\n\n objinfodraw.text(\n (625, 400),\n ('%s GAIA close neighbors within %.1f arcsec' %\n (cpd['objectinfo']['gaia_neighbors'],\n cpd['objectinfo']['searchradarcsec'])),\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n\n # closest GAIA neighbor\n if ( ('gaia_closest_distarcsec' in cpd['objectinfo']) and\n (cpd['objectinfo']['gaia_closest_distarcsec'] is not None) and\n (np.isfinite(cpd['objectinfo']['gaia_closest_distarcsec'])) and\n ('gaia_closest_gmagdiff' in cpd['objectinfo']) and\n (cpd['objectinfo']['gaia_closest_gmagdiff'] is not None) and\n (np.isfinite(cpd['objectinfo']['gaia_closest_gmagdiff'])) ):\n\n objinfodraw.text(\n (625, 425),\n ('closest GAIA neighbor is %.1f arcsec away, '\n 'GAIA mag (obj-nbr): %.3f' %\n (cpd['objectinfo']['gaia_closest_distarcsec'],\n cpd['objectinfo']['gaia_closest_gmagdiff'])),\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n\n # object tags\n if 'objecttags' in cpd['objectinfo'] and cpd['objectinfo']['objecttags']:\n\n objtagsplit = cpd['objectinfo']['objecttags'].split(',')\n\n # write three tags per line\n nobjtaglines = int(np.ceil(len(objtagsplit)/3.0))\n\n for objtagline in range(nobjtaglines):\n objtagslice = ','.join(objtagsplit[objtagline*3:objtagline*3+3])\n objinfodraw.text(\n (625, 450+objtagline*25),\n objtagslice,\n font=cpfontnormal,\n fill=(135, 54, 0, 255)\n )\n\n\n\n ################################################\n # row 1, cell 3: variability info and comments #\n ################################################\n\n # objectisvar\n objisvar = cpd['varinfo']['objectisvar']\n\n if objisvar == '0':\n objvarflag = 'Variable star flag not set'\n elif objisvar == '1':\n objvarflag = 'Object is probably a variable star'\n elif objisvar == '2':\n objvarflag = 'Object is probably not a variable star'\n elif objisvar == '3':\n objvarflag = 'Not sure if this object is a variable star'\n elif objisvar is None:\n objvarflag = 'Variable star flag not set'\n elif objisvar is True:\n objvarflag = 'Object is probably a variable star'\n elif objisvar is False:\n objvarflag = 'Object is probably not a variable star'\n else:\n objvarflag = 'Variable star flag: %s' % objisvar\n\n objinfodraw.text(\n (1650, 125),\n objvarflag,\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n\n # period\n objinfodraw.text(\n (1650, 150),\n ('Period [days]: %.6f' %\n (cpd['varinfo']['varperiod']\n if cpd['varinfo']['varperiod'] is not None\n else np.nan)),\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n\n # epoch\n objinfodraw.text(\n (1650, 175),\n ('Epoch [JD]: %.6f' %\n (cpd['varinfo']['varepoch']\n if cpd['varinfo']['varepoch'] is not None\n else np.nan)),\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n\n # variability tags\n if cpd['varinfo']['vartags']:\n\n vartagsplit = cpd['varinfo']['vartags'].split(',')\n\n # write three tags per line\n nvartaglines = int(np.ceil(len(vartagsplit)/3.0))\n\n for vartagline in range(nvartaglines):\n vartagslice = ','.join(vartagsplit[vartagline*3:vartagline*3+3])\n objinfodraw.text(\n (1650, 225+vartagline*25),\n vartagslice,\n font=cpfontnormal,\n fill=(135, 54, 0, 255)\n )\n\n # object comments\n if 'comments' in cpd and cpd['comments']:\n\n commentsplit = cpd['comments'].split(' ')\n\n # write 10 words per line\n ncommentlines = int(np.ceil(len(commentsplit)/10.0))\n\n for commentline in range(ncommentlines):\n commentslice = ' '.join(\n commentsplit[commentline*10:commentline*10+10]\n )\n objinfodraw.text(\n (1650, 325+commentline*25),\n commentslice,\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n\n # this handles JSON-ified checkplots returned by LCC server\n elif 'objectcomments' in cpd and cpd['objectcomments']:\n\n commentsplit = cpd['objectcomments'].split(' ')\n\n # write 10 words per line\n ncommentlines = int(np.ceil(len(commentsplit)/10.0))\n\n for commentline in range(ncommentlines):\n commentslice = ' '.join(\n commentsplit[commentline*10:commentline*10+10]\n )\n objinfodraw.text(\n (1650, 325+commentline*25),\n commentslice,\n font=cpfontnormal,\n fill=(0,0,0,255)\n )\n\n #######################################\n # row 1, cell 4: unphased light curve #\n #######################################\n\n if (cpd['magseries'] and\n 'plot' in cpd['magseries'] and\n cpd['magseries']['plot']):\n magseries = Image.open(\n _base64_to_file(cpd['magseries']['plot'], None, writetostrio=True)\n )\n outimg.paste(magseries,(750*3,0))\n\n # this handles JSON-ified checkplots from LCC server\n elif ('magseries' in cpd and isinstance(cpd['magseries'],str)):\n\n magseries = Image.open(\n _base64_to_file(cpd['magseries'], None, writetostrio=True)\n )\n outimg.paste(magseries,(750*3,0))\n\n\n ###############################\n # the rest of the rows in cpd #\n ###############################\n for lspmethodind, lspmethod in enumerate(cplspmethods):\n\n ###############################\n # the periodogram comes first #\n ###############################\n\n if (cpd[lspmethod] and cpd[lspmethod]['periodogram']):\n\n pgram = Image.open(\n _base64_to_file(cpd[lspmethod]['periodogram'], None,\n writetostrio=True)\n )\n outimg.paste(pgram,(0,480 + 480*lspmethodind))\n\n #############################\n # best phased LC comes next #\n #############################\n\n if (cpd[lspmethod] and 0 in cpd[lspmethod] and cpd[lspmethod][0]):\n\n plc1 = Image.open(\n _base64_to_file(cpd[lspmethod][0]['plot'], None,\n writetostrio=True)\n )\n outimg.paste(plc1,(750,480 + 480*lspmethodind))\n\n # this handles JSON-ified checkplots from LCC server\n elif (cpd[lspmethod] and 'phasedlc0' in cpd[lspmethod] and\n isinstance(cpd[lspmethod]['phasedlc0']['plot'], str)):\n\n plc1 = Image.open(\n _base64_to_file(cpd[lspmethod]['phasedlc0']['plot'], None,\n writetostrio=True)\n )\n outimg.paste(plc1,(750,480 + 480*lspmethodind))\n\n #################################\n # 2nd best phased LC comes next #\n #################################\n\n if (cpd[lspmethod] and 1 in cpd[lspmethod] and cpd[lspmethod][1]):\n\n plc2 = Image.open(\n _base64_to_file(cpd[lspmethod][1]['plot'], None,\n writetostrio=True)\n )\n outimg.paste(plc2,(750*2,480 + 480*lspmethodind))\n\n # this handles JSON-ified checkplots from LCC server\n elif (cpd[lspmethod] and 'phasedlc1' in cpd[lspmethod] and\n isinstance(cpd[lspmethod]['phasedlc1']['plot'], str)):\n\n plc2 = Image.open(\n _base64_to_file(cpd[lspmethod]['phasedlc1']['plot'], None,\n writetostrio=True)\n )\n outimg.paste(plc2,(750*2,480 + 480*lspmethodind))\n\n #################################\n # 3rd best phased LC comes next #\n #################################\n\n if (cpd[lspmethod] and 2 in cpd[lspmethod] and cpd[lspmethod][2]):\n\n plc3 = Image.open(\n _base64_to_file(cpd[lspmethod][2]['plot'], None,\n writetostrio=True)\n )\n outimg.paste(plc3,(750*3,480 + 480*lspmethodind))\n\n # this handles JSON-ified checkplots from LCC server\n elif (cpd[lspmethod] and 'phasedlc2' in cpd[lspmethod] and\n isinstance(cpd[lspmethod]['phasedlc2']['plot'], str)):\n\n plc3 = Image.open(\n _base64_to_file(cpd[lspmethod]['phasedlc2']['plot'], None,\n writetostrio=True)\n )\n outimg.paste(plc3,(750*3,480 + 480*lspmethodind))\n\n\n ################################\n ## ALL DONE WITH BUILDING PNG ##\n ################################\n\n #########################\n # add in any extra rows #\n #########################\n\n # from the keyword arguments\n if erows > 0:\n\n for erowind, erow in enumerate(extrarows):\n\n # make sure we never go above 4 plots in a row\n for ecolind, ecol in enumerate(erow[:4]):\n\n eplot = Image.open(ecol)\n eplotresized = eplot.resize((750,480), Image.ANTIALIAS)\n outimg.paste(eplotresized,\n (750*ecolind,\n (cprows+1)*480 + 480*erowind))\n\n # from the checkplotdict\n if cpderows > 0:\n\n for cpderowind, cpderow in enumerate(cpd['externalplots']):\n\n # make sure we never go above 4 plots in a row\n for cpdecolind, cpdecol in enumerate(cpderow[:4]):\n\n cpdeplot = Image.open(cpdecol)\n cpdeplotresized = cpdeplot.resize((750,480), Image.ANTIALIAS)\n outimg.paste(cpdeplotresized,\n (750*cpdecolind,\n (cprows+1)*480 + (erows*480) + 480*cpderowind))\n\n\n # from neighbors:\n if nbrrows > 0:\n\n # we have four tiles\n # tile 1: neighbor objectid, ra, decl, distance, unphased LC\n # tile 2: phased LC for gls\n # tile 3: phased LC for pdm\n # tile 4: phased LC for any other period finding method\n # the priority is like so: ['bls','mav','aov','win']\n\n for nbrind, nbr in enumerate(cpd['neighbors']):\n\n # figure out which period finding methods are available for this\n # neighbor. make sure to match the ones from the actual object in\n # order of priority: 'gls','pdm','bls','aov','mav','acf','win'\n nbrlspmethods = []\n\n for lspmethod in cpd['pfmethods']:\n if lspmethod in nbr:\n nbrlspmethods.append(lspmethod)\n\n # restrict to top three in priority\n nbrlspmethods = nbrlspmethods[:3]\n\n try:\n\n # first panel: neighbor objectid, ra, decl, distance, unphased\n # LC\n nbrlc = Image.open(\n _base64_to_file(\n nbr['magseries']['plot'], None, writetostrio=True\n )\n )\n outimg.paste(nbrlc,\n (750*0,\n (cprows+1)*480 + (erows*480) + (cpderows*480) +\n 480*nbrind))\n\n # overlay the objectinfo\n objinfodraw.text(\n (98,\n (cprows+1)*480 + (erows*480) + (cpderows*480) +\n 480*nbrind + 15),\n ('N%s: %s' % (nbrind + 1, nbr['objectid'])),\n font=cpfontlarge,\n fill=(0,0,255,255)\n )\n # overlay the objectinfo\n objinfodraw.text(\n (98,\n (cprows+1)*480 + (erows*480) + (cpderows*480) +\n 480*nbrind + 50),\n ('(RA, DEC) = (%.3f, %.3f), distance: %.1f arcsec' %\n (nbr['ra'], nbr['decl'], nbr['dist'])),\n font=cpfontnormal,\n fill=(0,0,255,255)\n )\n\n # second panel: phased LC for gls\n lsp1lc = Image.open(\n _base64_to_file(\n nbr[nbrlspmethods[0]][0]['plot'], None,\n writetostrio=True\n )\n )\n outimg.paste(lsp1lc,\n (750*1,\n (cprows+1)*480 + (erows*480) + (cpderows*480) +\n 480*nbrind))\n\n # second panel: phased LC for gls\n lsp2lc = Image.open(\n _base64_to_file(\n nbr[nbrlspmethods[1]][0]['plot'], None,\n writetostrio=True\n )\n )\n outimg.paste(lsp2lc,\n (750*2,\n (cprows+1)*480 + (erows*480) + (cpderows*480) +\n 480*nbrind))\n\n # second panel: phased LC for gls\n lsp3lc = Image.open(\n _base64_to_file(\n nbr[nbrlspmethods[2]][0]['plot'], None,\n writetostrio=True\n )\n )\n outimg.paste(lsp3lc,\n (750*3,\n (cprows+1)*480 + (erows*480) + (cpderows*480) +\n 480*nbrind))\n\n except Exception as e:\n\n LOGERROR('neighbor %s does not have a magseries plot, '\n 'measurements are probably all nan' % nbr['objectid'])\n\n # overlay the objectinfo\n objinfodraw.text(\n (98,\n (cprows+1)*480 + (erows*480) + (cpderows*480) +\n 480*nbrind + 15),\n ('N%s: %s' %\n (nbrind + 1, nbr['objectid'])),\n font=cpfontlarge,\n fill=(0,0,255,255)\n )\n\n if 'ra' in nbr and 'decl' in nbr and 'dist' in nbr:\n\n # overlay the objectinfo\n objinfodraw.text(\n (98,\n (cprows+1)*480 + (erows*480) + (cpderows*480) +\n 480*nbrind + 50),\n ('(RA, DEC) = (%.3f, %.3f), distance: %.1f arcsec' %\n (nbr['ra'], nbr['decl'], nbr['dist'])),\n font=cpfontnormal,\n fill=(0,0,255,255)\n )\n\n elif 'objectinfo' in nbr:\n\n # overlay the objectinfo\n objinfodraw.text(\n (98,\n (cprows+1)*480 + (erows*480) + (cpderows*480) +\n 480*nbrind + 50),\n ('(RA, DEC) = (%.3f, %.3f), distance: %.1f arcsec' %\n (nbr['objectinfo']['ra'],\n nbr['objectinfo']['decl'],\n nbr['objectinfo']['distarcsec'])),\n font=cpfontnormal,\n fill=(0,0,255,255)\n )\n\n\n #####################\n ## WRITE FINAL PNG ##\n #####################\n\n # check if the output filename is actually an instance of StringIO\n if sys.version_info[:2] < (3,0):\n\n is_strio = isinstance(outfile, cStringIO.InputType)\n\n else:\n\n is_strio = isinstance(outfile, StrIO)\n\n\n if not is_strio:\n\n # check if we've stupidly copied over the same filename as the input\n # pickle to expected output file\n if outfile.endswith('pkl'):\n LOGWARNING('expected output PNG filename ends with .pkl, '\n 'changed to .png')\n outfile = outfile.replace('.pkl','.png')\n\n outimg.save(outfile, format='PNG', optimize=True)\n\n\n if not is_strio:\n if os.path.exists(outfile):\n LOGINFO('checkplot pickle -> checkplot PNG: %s OK' % outfile)\n return outfile\n else:\n LOGERROR('failed to write checkplot PNG')\n return None\n\n else:\n LOGINFO('checkplot pickle -> StringIO instance OK')\n return outfile", "response": "This function reads the checkplot pickle or dict provided and writes out a PNG file."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef flare_model(flareparams, times, mags, errs):\n '''This is a flare model function, similar to Kowalski+ 2011.\n\n From the paper by Pitkin+ 2014:\n http://adsabs.harvard.edu/abs/2014MNRAS.445.2268P\n\n Parameters\n ----------\n\n flareparams : list of float\n This defines the flare model::\n\n [amplitude,\n flare_peak_time,\n rise_gaussian_stdev,\n decay_time_constant]\n\n where:\n\n `amplitude`: the maximum flare amplitude in mags or flux. If flux, then\n amplitude should be positive. If mags, amplitude should be negative.\n\n `flare_peak_time`: time at which the flare maximum happens.\n\n `rise_gaussian_stdev`: the stdev of the gaussian describing the rise of\n the flare.\n\n `decay_time_constant`: the time constant of the exponential fall of the\n flare.\n\n times,mags,errs : np.array\n The input time-series of measurements and associated errors for which\n the model will be generated. The times will be used to generate\n model mags.\n\n Returns\n -------\n\n (modelmags, times, mags, errs) : tuple\n Returns the model mags evaluated at the input time values. Also returns\n the input `times`, `mags`, and `errs`.\n\n '''\n\n (amplitude, flare_peak_time,\n rise_gaussian_stdev, decay_time_constant) = flareparams\n\n zerolevel = np.median(mags)\n modelmags = np.full_like(times, zerolevel)\n\n # before peak gaussian rise...\n modelmags[times < flare_peak_time] = (\n mags[times < flare_peak_time] +\n amplitude * np.exp(\n -((times[times < flare_peak_time] -\n flare_peak_time) *\n (times[times < flare_peak_time] -\n flare_peak_time)) /\n (2.0*rise_gaussian_stdev*rise_gaussian_stdev)\n )\n )\n\n # after peak exponential decay...\n modelmags[times > flare_peak_time] = (\n mags[times > flare_peak_time] +\n amplitude * np.exp(\n -((times[times > flare_peak_time] -\n flare_peak_time)) /\n (decay_time_constant)\n )\n )\n\n return modelmags, times, mags, errs", "response": "This function is a flare model function that is similar to Kowalski + 2011. This function is a flare model function similar to Kowalski + 2011."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef flare_model_residual(flareparams, times, mags, errs):\n '''\n This returns the residual between model mags and the actual mags.\n\n Parameters\n ----------\n\n flareparams : list of float\n This defines the flare model::\n\n [amplitude,\n flare_peak_time,\n rise_gaussian_stdev,\n decay_time_constant]\n\n where:\n\n `amplitude`: the maximum flare amplitude in mags or flux. If flux, then\n amplitude should be positive. If mags, amplitude should be negative.\n\n `flare_peak_time`: time at which the flare maximum happens.\n\n `rise_gaussian_stdev`: the stdev of the gaussian describing the rise of\n the flare.\n\n `decay_time_constant`: the time constant of the exponential fall of the\n flare.\n\n times,mags,errs : np.array\n The input time-series of measurements and associated errors for which\n the model will be generated. The times will be used to generate\n model mags.\n\n Returns\n -------\n\n np.array\n The residuals between the input `mags` and generated `modelmags`,\n weighted by the measurement errors in `errs`.\n\n '''\n\n modelmags, _, _, _ = flare_model(flareparams, times, mags, errs)\n\n return (mags - modelmags)/errs", "response": "This function returns the residual between the model mags and the actual mags."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef shutdown_check_handler():\n\n url = 'http://169.254.169.254/latest/meta-data/spot/instance-action'\n\n try:\n resp = requests.get(url, timeout=1.0)\n resp.raise_for_status()\n\n stopinfo = resp.json()\n if 'action' in stopinfo and stopinfo['action'] in ('stop',\n 'terminate',\n 'hibernate'):\n stoptime = stopinfo['time']\n LOGWARNING('instance is going to %s at %s' % (stopinfo['action'],\n stoptime))\n\n resp.close()\n return True\n else:\n resp.close()\n return False\n\n except HTTPError as e:\n resp.close()\n return False\n\n except Exception as e:\n resp.close()\n return False", "response": "This function checks the AWS instance data URL to see if there s a pending shutdown for the instance. If it is we ll check the instance data URL to see if the instance is still safe."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef runcp_producer_loop(\n lightcurve_list,\n input_queue,\n input_bucket,\n result_queue,\n result_bucket,\n pfresult_list=None,\n runcp_kwargs=None,\n process_list_slice=None,\n purge_queues_when_done=False,\n delete_queues_when_done=False,\n download_when_done=True,\n save_state_when_done=True,\n s3_client=None,\n sqs_client=None\n):\n \"\"\"This sends checkplot making tasks to the input queue and monitors the\n result queue for task completion.\n\n Parameters\n ----------\n\n lightcurve_list : str or list of str\n This is either a string pointing to a file containing a list of light\n curves filenames to process or the list itself. The names must\n correspond to the full filenames of files stored on S3, including all\n prefixes, but not include the 's3:///' bit (these will be\n added automatically).\n\n input_queue : str\n This is the name of the SQS queue which will receive processing tasks\n generated by this function. The queue URL will automatically be obtained\n from AWS.\n\n input_bucket : str\n The name of the S3 bucket containing the light curve files to process.\n\n result_queue : str\n This is the name of the SQS queue that this function will listen to for\n messages from the workers as they complete processing on their input\n elements. This function will attempt to match input sent to the\n `input_queue` with results coming into the `result_queue` so it knows\n how many objects have been successfully processed. If this function\n receives task results that aren't in its own input queue, it will\n acknowledge them so they complete successfully, but not download them\n automatically. This handles leftover tasks completing from a previous\n run of this function.\n\n result_bucket : str\n The name of the S3 bucket which will receive the results from the\n workers.\n\n pfresult_list : list of str or None\n This is a list of periodfinder result pickle S3 URLs associated with\n each light curve. If provided, this will be used to add in phased light\n curve plots to each checkplot pickle. If this is None, the worker loop\n will produce checkplot pickles that only contain object information,\n neighbor information, and unphased light curves.\n\n runcp_kwargs : dict\n This is a dict used to pass any extra keyword arguments to the\n `lcproc.checkplotgen.runcp` function that will be run by the worker\n loop.\n\n process_list_slice : list\n This is used to index into the input light curve list so a subset of the\n full list can be processed in this specific run of this function.\n\n Use None for a slice index elem to emulate single slice spec behavior:\n\n process_list_slice = [10, None] -> lightcurve_list[10:]\n process_list_slice = [None, 500] -> lightcurve_list[:500]\n\n purge_queues_when_done : bool\n If this is True, and this function exits (either when all done, or when\n it is interrupted with a Ctrl+C), all outstanding elements in the\n input/output queues that have not yet been acknowledged by workers or by\n this function will be purged. This effectively cancels all outstanding\n work.\n\n delete_queues_when_done : bool\n If this is True, and this function exits (either when all done, or when\n it is interrupted with a Ctrl+C'), all outstanding work items will be\n purged from the input/queues and the queues themselves will be deleted.\n\n download_when_done : bool\n If this is True, the generated checkplot pickle for each input work item\n will be downloaded immediately to the current working directory when the\n worker functions report they're done with it.\n\n save_state_when_done : bool\n If this is True, will save the current state of the work item queue and\n the work items acknowledged as completed to a pickle in the current\n working directory. Call the `runcp_producer_loop_savedstate` function\n below to resume processing from this saved state later.\n\n s3_client : boto3.Client or None\n If None, this function will instantiate a new `boto3.Client` object to\n use in its S3 download operations. Alternatively, pass in an existing\n `boto3.Client` instance to re-use it here.\n\n sqs_client : boto3.Client or None\n If None, this function will instantiate a new `boto3.Client` object to\n use in its SQS operations. Alternatively, pass in an existing\n `boto3.Client` instance to re-use it here.\n\n Returns\n -------\n\n dict or str\n Returns the current work state as a dict or str path to the generated\n work state pickle depending on if `save_state_when_done` is True.\n\n \"\"\"\n\n if not sqs_client:\n sqs_client = boto3.client('sqs')\n if not s3_client:\n s3_client = boto3.client('s3')\n\n if isinstance(lightcurve_list, str) and os.path.exists(lightcurve_list):\n\n # get the LC list\n with open(lightcurve_list, 'r') as infd:\n lclist = infd.readlines()\n\n lclist = [x.replace('\\n','') for x in lclist if len(x) > 0]\n\n if process_list_slice is not None:\n lclist = lclist[process_list_slice[0]:process_list_slice[1]]\n\n lclist = [x[1:] for x in lclist if x.startswith('/')]\n lclist = ['s3://%s/%s' % (input_bucket, x) for x in lclist]\n\n # this handles direct invocation using lists of s3:// urls of light curves\n elif isinstance(lightcurve_list, list):\n lclist = lightcurve_list\n\n # set up the input and output queues\n\n # check if the queues by the input and output names given exist already\n # if they do, go ahead and use them\n # if they don't, make new ones.\n try:\n inq = sqs_client.get_queue_url(QueueName=input_queue)\n inq_url = inq['QueueUrl']\n LOGINFO('input queue already exists, skipping creation...')\n except ClientError as e:\n inq = awsutils.sqs_create_queue(input_queue, client=sqs_client)\n inq_url = inq['url']\n\n try:\n outq = sqs_client.get_queue_url(QueueName=result_queue)\n outq_url = outq['QueueUrl']\n LOGINFO('result queue already exists, skipping creation...')\n except ClientError as e:\n outq = awsutils.sqs_create_queue(result_queue, client=sqs_client)\n outq_url = outq['url']\n\n LOGINFO('input queue: %s' % inq_url)\n LOGINFO('output queue: %s' % outq_url)\n\n # wait until queues are up\n LOGINFO('waiting for queues to become ready...')\n time.sleep(10.0)\n\n # for each item in the lightcurve_list, send it to the input queue and wait\n # until it's done to send another one\n\n if pfresult_list is None:\n pfresult_list = [None for x in lclist]\n\n for lc, pf in zip(lclist, pfresult_list):\n\n this_item = {\n 'target': lc,\n 'action': 'runcp',\n 'args': (pf,),\n 'kwargs':runcp_kwargs if runcp_kwargs is not None else {},\n 'outbucket': result_bucket,\n 'outqueue': outq_url\n }\n\n resp = awsutils.sqs_put_item(inq_url, this_item, client=sqs_client)\n if resp:\n LOGINFO('sent %s to queue: %s' % (lc,inq_url))\n\n # now block until all objects are done\n done_objects = {}\n\n LOGINFO('all items queued, waiting for results...')\n\n # listen to the kill and term signals and raise KeyboardInterrupt when\n # called\n signal.signal(signal.SIGINT, kill_handler)\n signal.signal(signal.SIGTERM, kill_handler)\n\n while len(list(done_objects.keys())) < len(lclist):\n\n try:\n\n result = awsutils.sqs_get_item(outq_url, client=sqs_client)\n\n if result is not None and len(result) > 0:\n\n recv = result[0]\n try:\n processed_object = recv['item']['target']\n except KeyError:\n LOGWARNING('unknown target in received item: %s' % recv)\n processed_object = 'unknown-lc'\n\n cpf = recv['item']['cpf']\n receipt = recv['receipt_handle']\n\n if processed_object in lclist:\n\n if processed_object not in done_objects:\n done_objects[processed_object] = [cpf]\n else:\n done_objects[processed_object].append(cpf)\n\n LOGINFO('done with %s -> %s' % (processed_object, cpf))\n\n if download_when_done:\n\n getobj = awsutils.awsutils.s3_get_url(\n cpf,\n client=s3_client\n )\n LOGINFO('downloaded %s -> %s' % (cpf, getobj))\n\n else:\n LOGWARNING('processed object returned is not in '\n 'queued target list, probably from an '\n 'earlier run. accepting but not downloading.')\n\n awsutils.sqs_delete_item(outq_url, receipt)\n\n except KeyboardInterrupt as e:\n\n LOGWARNING('breaking out of producer wait-loop')\n break\n\n\n # delete the input and output queues when we're done\n LOGINFO('done with processing.')\n time.sleep(1.0)\n\n if purge_queues_when_done:\n LOGWARNING('purging queues at exit, please wait 10 seconds...')\n sqs_client.purge_queue(QueueUrl=inq_url)\n sqs_client.purge_queue(QueueUrl=outq_url)\n time.sleep(10.0)\n\n if delete_queues_when_done:\n LOGWARNING('deleting queues at exit')\n awsutils.sqs_delete_queue(inq_url)\n awsutils.sqs_delete_queue(outq_url)\n\n work_state = {\n 'done': done_objects,\n 'in_progress': list(set(lclist) - set(done_objects.keys())),\n 'args':((os.path.abspath(lightcurve_list) if\n isinstance(lightcurve_list, str) else lightcurve_list),\n input_queue,\n input_bucket,\n result_queue,\n result_bucket),\n 'kwargs':{'pfresult_list':pfresult_list,\n 'runcp_kwargs':runcp_kwargs,\n 'process_list_slice':process_list_slice,\n 'download_when_done':download_when_done,\n 'purge_queues_when_done':purge_queues_when_done,\n 'save_state_when_done':save_state_when_done,\n 'delete_queues_when_done':delete_queues_when_done}\n }\n\n if save_state_when_done:\n with open('runcp-queue-producer-loop-state.pkl','wb') as outfd:\n pickle.dump(work_state, outfd, pickle.HIGHEST_PROTOCOL)\n\n # at the end, return the done_objects dict\n # also return the list of unprocessed items if any\n return work_state", "response": "This function sends checkplot making tasks to the input queue and monitors the result queue for task completion."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef runcp_producer_loop_savedstate(\n use_saved_state=None,\n lightcurve_list=None,\n input_queue=None,\n input_bucket=None,\n result_queue=None,\n result_bucket=None,\n pfresult_list=None,\n runcp_kwargs=None,\n process_list_slice=None,\n download_when_done=True,\n purge_queues_when_done=True,\n save_state_when_done=True,\n delete_queues_when_done=False,\n s3_client=None,\n sqs_client=None\n):\n \"\"\"This wraps the function above to allow for loading previous state from a\n file.\n\n Parameters\n ----------\n\n use_saved_state : str or None\n This is the path to the saved state pickle file produced by a previous\n run of `runcp_producer_loop`. Will get all of the arguments to run\n another instance of the loop from that pickle file. If this is None, you\n MUST provide all of the appropriate arguments to that function.\n\n lightcurve_list : str or list of str or None\n This is either a string pointing to a file containing a list of light\n curves filenames to process or the list itself. The names must\n correspond to the full filenames of files stored on S3, including all\n prefixes, but not include the 's3:///' bit (these will be\n added automatically).\n\n input_queue : str or None\n This is the name of the SQS queue which will receive processing tasks\n generated by this function. The queue URL will automatically be obtained\n from AWS.\n\n input_bucket : str or None\n The name of the S3 bucket containing the light curve files to process.\n\n result_queue : str or None\n This is the name of the SQS queue that this function will listen to for\n messages from the workers as they complete processing on their input\n elements. This function will attempt to match input sent to the\n `input_queue` with results coming into the `result_queue` so it knows\n how many objects have been successfully processed. If this function\n receives task results that aren't in its own input queue, it will\n acknowledge them so they complete successfully, but not download them\n automatically. This handles leftover tasks completing from a previous\n run of this function.\n\n result_bucket : str or None\n The name of the S3 bucket which will receive the results from the\n workers.\n\n pfresult_list : list of str or None\n This is a list of periodfinder result pickle S3 URLs associated with\n each light curve. If provided, this will be used to add in phased light\n curve plots to each checkplot pickle. If this is None, the worker loop\n will produce checkplot pickles that only contain object information,\n neighbor information, and unphased light curves.\n\n runcp_kwargs : dict or None\n This is a dict used to pass any extra keyword arguments to the\n `lcproc.checkplotgen.runcp` function that will be run by the worker\n loop.\n\n process_list_slice : list or None\n This is used to index into the input light curve list so a subset of the\n full list can be processed in this specific run of this function.\n\n Use None for a slice index elem to emulate single slice spec behavior:\n\n process_list_slice = [10, None] -> lightcurve_list[10:]\n process_list_slice = [None, 500] -> lightcurve_list[:500]\n\n purge_queues_when_done : bool or None\n If this is True, and this function exits (either when all done, or when\n it is interrupted with a Ctrl+C), all outstanding elements in the\n input/output queues that have not yet been acknowledged by workers or by\n this function will be purged. This effectively cancels all outstanding\n work.\n\n delete_queues_when_done : bool or None\n If this is True, and this function exits (either when all done, or when\n it is interrupted with a Ctrl+C'), all outstanding work items will be\n purged from the input/queues and the queues themselves will be deleted.\n\n download_when_done : bool or None\n If this is True, the generated checkplot pickle for each input work item\n will be downloaded immediately to the current working directory when the\n worker functions report they're done with it.\n\n save_state_when_done : bool or None\n If this is True, will save the current state of the work item queue and\n the work items acknowledged as completed to a pickle in the current\n working directory. Call the `runcp_producer_loop_savedstate` function\n below to resume processing from this saved state later.\n\n s3_client : boto3.Client or None\n If None, this function will instantiate a new `boto3.Client` object to\n use in its S3 download operations. Alternatively, pass in an existing\n `boto3.Client` instance to re-use it here.\n\n sqs_client : boto3.Client or None\n If None, this function will instantiate a new `boto3.Client` object to\n use in its SQS operations. Alternatively, pass in an existing\n `boto3.Client` instance to re-use it here.\n\n Returns\n -------\n\n dict or str\n Returns the current work state as a dict or str path to the generated\n work state pickle depending on if `save_state_when_done` is True.\n\n \"\"\"\n\n if use_saved_state is not None and os.path.exists(use_saved_state):\n\n with open(use_saved_state,'rb') as infd:\n saved_state = pickle.load(infd)\n\n # run the producer loop using the saved state's todo list\n return runcp_producer_loop(\n saved_state['in_progress'],\n saved_state['args'][1],\n saved_state['args'][2],\n saved_state['args'][3],\n saved_state['args'][4],\n **saved_state['kwargs']\n )\n\n else:\n\n return runcp_producer_loop(\n lightcurve_list,\n input_queue,\n input_bucket,\n result_queue,\n result_bucket,\n pfresult_list=pfresult_list,\n runcp_kwargs=runcp_kwargs,\n process_list_slice=process_list_slice,\n download_when_done=download_when_done,\n purge_queues_when_done=purge_queues_when_done,\n save_state_when_done=save_state_when_done,\n delete_queues_when_done=delete_queues_when_done,\n s3_client=s3_client,\n sqs_client=sqs_client\n )", "response": "This function is used to load the saved state pickle file produced by a previous run of runcp_producer_loop. It will load the saved state pickle file and process the saved state and return a new copy of the saved state."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef runpf_consumer_loop(\n in_queue_url,\n workdir,\n lc_altexts=('',),\n wait_time_seconds=5,\n shutdown_check_timer_seconds=60.0,\n sqs_client=None,\n s3_client=None\n):\n \"\"\"This runs period-finding in a loop until interrupted.\n\n Consumes work task items from an input queue set up by `runpf_producer_loop`\n above.\n\n Parameters\n ----------\n\n in_queue_url : str\n The SQS URL of the input queue to listen to for work assignment\n messages. The task orders will include the input and output S3 bucket\n names, as well as the URL of the output queue to where this function\n will report its work-complete or work-failed status.\n\n workdir : str\n The directory on the local machine where this worker loop will download\n the input light curves, process them, and produce its output\n periodfinding result pickles. These will then be uploaded to the\n specified S3 output bucket, and then deleted from the local disk.\n\n lc_altexts : sequence of str\n If not None, this is a sequence of alternate extensions to try for the\n input light curve file other than the one provided in the input task\n order. For example, to get anything that's an .sqlite where .sqlite.gz\n is expected, use altexts=[''] to strip the .gz.\n\n wait_time_seconds : int\n The amount of time to wait in the input SQS queue for an input task\n order. If this timeout expires and no task has been received, this\n function goes back to the top of the work loop.\n\n shutdown_check_timer_seconds : float\n The amount of time to wait before checking for a pending EC2 shutdown\n message for the instance this worker loop is operating on. If a shutdown\n is noticed, the worker loop is cancelled in preparation for instance\n shutdown.\n\n sqs_client : boto3.Client or None\n If None, this function will instantiate a new `boto3.Client` object to\n use in its SQS operations. Alternatively, pass in an existing\n `boto3.Client` instance to re-use it here.\n\n s3_client : boto3.Client or None\n If None, this function will instantiate a new `boto3.Client` object to\n use in its S3 operations. Alternatively, pass in an existing\n `boto3.Client` instance to re-use it here.\n\n Returns\n -------\n\n Nothing.\n\n \"\"\"\n\n if not sqs_client:\n sqs_client = boto3.client('sqs')\n if not s3_client:\n s3_client = boto3.client('s3')\n\n\n # listen to the kill and term signals and raise KeyboardInterrupt when\n # called\n signal.signal(signal.SIGINT, kill_handler)\n signal.signal(signal.SIGTERM, kill_handler)\n\n shutdown_last_time = time.monotonic()\n\n while True:\n\n curr_time = time.monotonic()\n\n if (curr_time - shutdown_last_time) > shutdown_check_timer_seconds:\n shutdown_check = shutdown_check_handler()\n if shutdown_check:\n LOGWARNING('instance will die soon, breaking loop')\n break\n shutdown_last_time = time.monotonic()\n\n try:\n\n # receive a single message from the inqueue\n work = awsutils.sqs_get_item(in_queue_url,\n client=sqs_client,\n raiseonfail=True)\n\n # JSON deserialize the work item\n if work is not None and len(work) > 0:\n\n recv = work[0]\n\n # skip any messages that don't tell us to runpf\n action = recv['item']['action']\n if action != 'runpf':\n continue\n\n target = recv['item']['target']\n args = recv['item']['args']\n kwargs = recv['item']['kwargs']\n outbucket = recv['item']['outbucket']\n\n if 'outqueue' in recv['item']:\n out_queue_url = recv['item']['outqueue']\n else:\n out_queue_url = None\n\n receipt = recv['receipt_handle']\n\n # download the target from S3 to a file in the work directory\n try:\n\n lc_filename = awsutils.s3_get_url(\n target,\n altexts=lc_altexts,\n client=s3_client\n )\n\n runpf_args = (lc_filename, args[0])\n\n # now runpf\n pfresult = runpf(\n *runpf_args,\n **kwargs\n )\n\n if pfresult and os.path.exists(pfresult):\n\n LOGINFO('runpf OK for LC: %s -> %s' %\n (lc_filename, pfresult))\n\n # check if the file exists already because it's been\n # processed somewhere else\n resp = s3_client.list_objects_v2(\n Bucket=outbucket,\n MaxKeys=1,\n Prefix=pfresult\n )\n outbucket_list = resp.get('Contents',[])\n\n if outbucket_list and len(outbucket_list) > 0:\n\n LOGWARNING(\n 'not uploading pfresult for %s because '\n 'it exists in the output bucket already'\n % target\n )\n awsutils.sqs_delete_item(in_queue_url, receipt)\n continue\n\n put_url = awsutils.s3_put_file(pfresult,\n outbucket,\n client=s3_client)\n\n if put_url is not None:\n\n LOGINFO('result uploaded to %s' % put_url)\n\n # put the S3 URL of the output into the output\n # queue if requested\n if out_queue_url is not None:\n\n awsutils.sqs_put_item(\n out_queue_url,\n {'pfresult':put_url,\n 'target': target,\n 'lc_filename':lc_filename,\n 'kwargs':kwargs},\n raiseonfail=True\n )\n\n # delete the result from the local directory\n os.remove(pfresult)\n\n # if the upload fails, don't acknowledge the\n # message. might be a temporary S3 failure, so\n # another worker might succeed later.\n # FIXME: add SNS bits to warn us of failures\n else:\n LOGERROR('failed to upload %s to S3' % pfresult)\n os.remove(pfresult)\n\n # delete the input item from the input queue to\n # acknowledge its receipt and indicate that\n # processing is done and successful\n awsutils.sqs_delete_item(in_queue_url, receipt)\n\n # delete the light curve file when we're done with it\n if ( (lc_filename is not None) and\n (os.path.exists(lc_filename)) ):\n os.remove(lc_filename)\n\n # if runcp failed outright, don't requeue. instead, write a\n # ('failed-checkplot-%s.pkl' % lc_filename) file to the\n # output S3 bucket.\n else:\n\n LOGWARNING('runpf failed for LC: %s' %\n (lc_filename,))\n\n with open('failed-periodfinding-%s.pkl' %\n lc_filename, 'wb') as outfd:\n pickle.dump(\n {'in_queue_url':in_queue_url,\n 'target':target,\n 'lc_filename':lc_filename,\n 'kwargs':kwargs,\n 'outbucket':outbucket,\n 'out_queue_url':out_queue_url},\n outfd, pickle.HIGHEST_PROTOCOL\n )\n\n put_url = awsutils.s3_put_file(\n 'failed-periodfinding-%s.pkl' % lc_filename,\n outbucket,\n client=s3_client\n )\n\n # put the S3 URL of the output into the output\n # queue if requested\n if out_queue_url is not None:\n\n awsutils.sqs_put_item(\n out_queue_url,\n {'pfresult':put_url,\n 'lc_filename':lc_filename,\n 'kwargs':kwargs},\n raiseonfail=True\n )\n\n # delete the input item from the input queue to\n # acknowledge its receipt and indicate that\n # processing is done\n awsutils.sqs_delete_item(in_queue_url,\n receipt,\n raiseonfail=True)\n\n # delete the light curve file when we're done with it\n if ( (lc_filename is not None) and\n (os.path.exists(lc_filename)) ):\n os.remove(lc_filename)\n\n\n except ClientError as e:\n\n LOGWARNING('queues have disappeared. stopping worker loop')\n break\n\n\n # if there's any other exception, put a failed response into the\n # output bucket and queue\n except Exception as e:\n\n LOGEXCEPTION('could not process input from queue')\n\n if 'lc_filename' in locals():\n\n with open('failed-periodfinding-%s.pkl' %\n lc_filename,'wb') as outfd:\n pickle.dump(\n {'in_queue_url':in_queue_url,\n 'target':target,\n 'lc_filename':lc_filename,\n 'kwargs':kwargs,\n 'outbucket':outbucket,\n 'out_queue_url':out_queue_url},\n outfd, pickle.HIGHEST_PROTOCOL\n )\n\n put_url = awsutils.s3_put_file(\n 'failed-periodfinding-%s.pkl' % lc_filename,\n outbucket,\n client=s3_client\n )\n\n\n # put the S3 URL of the output into the output\n # queue if requested\n if out_queue_url is not None:\n\n awsutils.sqs_put_item(\n out_queue_url,\n {'pfresult':put_url,\n 'lc_filename':lc_filename,\n 'kwargs':kwargs},\n raiseonfail=True\n )\n\n # delete the light curve file when we're done with it\n if ( (lc_filename is not None) and\n (os.path.exists(lc_filename)) ):\n os.remove(lc_filename)\n\n # delete the input item from the input queue to\n # acknowledge its receipt and indicate that\n # processing is done\n awsutils.sqs_delete_item(in_queue_url,\n receipt,\n raiseonfail=True)\n\n\n # a keyboard interrupt kills the loop\n except KeyboardInterrupt:\n\n LOGWARNING('breaking out of the processing loop.')\n break\n\n\n # if the queues disappear, then the producer loop is done and we should\n # exit\n except ClientError as e:\n\n LOGWARNING('queues have disappeared. stopping worker loop')\n break\n\n\n # any other exception continues the loop we'll write the output file to\n # the output S3 bucket (and any optional output queue), but add a\n # failed-* prefix to it to indicate that processing failed. FIXME: could\n # use a dead-letter queue for this instead\n except Exception as e:\n\n LOGEXCEPTION('could not process input from queue')\n\n if 'lc_filename' in locals():\n\n with open('failed-periodfinding-%s.pkl' %\n lc_filename,'wb') as outfd:\n pickle.dump(\n {'in_queue_url':in_queue_url,\n 'target':target,\n 'kwargs':kwargs,\n 'outbucket':outbucket,\n 'out_queue_url':out_queue_url},\n outfd, pickle.HIGHEST_PROTOCOL\n )\n\n put_url = awsutils.s3_put_file(\n 'failed-periodfinding-%s.pkl' % lc_filename,\n outbucket,\n client=s3_client\n )\n\n # put the S3 URL of the output into the output\n # queue if requested\n if out_queue_url is not None:\n\n awsutils.sqs_put_item(\n out_queue_url,\n {'cpf':put_url,\n 'kwargs':kwargs},\n raiseonfail=True\n )\n if ( (lc_filename is not None) and\n (os.path.exists(lc_filename)) ):\n os.remove(lc_filename)\n\n # delete the input item from the input queue to\n # acknowledge its receipt and indicate that\n # processing is done\n awsutils.sqs_delete_item(in_queue_url, receipt, raiseonfail=True)", "response": "This function runs the main worker loop for the work assignment process."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef gaussianeb_fit_magseries(times, mags, errs,\n ebparams,\n sigclip=10.0,\n plotfit=False,\n magsarefluxes=False,\n verbose=True):\n '''This fits a double inverted gaussian EB model to a magnitude time series.\n\n Parameters\n ----------\n\n times,mags,errs : np.array\n The input mag/flux time-series to fit the EB model to.\n\n period : float\n The period to use for EB fit.\n\n ebparams : list of float\n This is a list containing the eclipsing binary parameters::\n\n ebparams = [period (time),\n epoch (time),\n pdepth (mags),\n pduration (phase),\n psdepthratio,\n secondaryphase]\n\n `period` is the period in days.\n\n `epoch` is the time of primary minimum in JD.\n\n `pdepth` is the depth of the primary eclipse:\n\n - for magnitudes -> `pdepth` should be < 0\n - for fluxes -> `pdepth` should be > 0\n\n `pduration` is the length of the primary eclipse in phase.\n\n `psdepthratio` is the ratio of the secondary eclipse depth to that of\n the primary eclipse.\n\n `secondaryphase` is the phase at which the minimum of the secondary\n eclipse is located. This effectively parameterizes eccentricity.\n\n If `epoch` is None, this function will do an initial spline fit to find\n an approximate minimum of the phased light curve using the given period.\n\n The `pdepth` provided is checked against the value of\n `magsarefluxes`. if `magsarefluxes = True`, the `ebdepth` is forced to\n be > 0; if `magsarefluxes = False`, the `ebdepth` is forced to be < 0.\n\n sigclip : float or int or sequence of two floats/ints or None\n If a single float or int, a symmetric sigma-clip will be performed using\n the number provided as the sigma-multiplier to cut out from the input\n time-series.\n\n If a list of two ints/floats is provided, the function will perform an\n 'asymmetric' sigma-clip. The first element in this list is the sigma\n value to use for fainter flux/mag values; the second element in this\n list is the sigma value to use for brighter flux/mag values. For\n example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma\n dimmings and greater than 3-sigma brightenings. Here the meaning of\n \"dimming\" and \"brightening\" is set by *physics* (not the magnitude\n system), which is why the `magsarefluxes` kwarg must be correctly set.\n\n If `sigclip` is None, no sigma-clipping will be performed, and the\n time-series (with non-finite elems removed) will be passed through to\n the output.\n\n magsarefluxes : bool\n If True, will treat the input values of `mags` as fluxes for purposes of\n plotting the fit and sig-clipping.\n\n plotfit : str or False\n If this is a string, this function will make a plot for the fit to the\n mag/flux time-series and writes the plot to the path specified here.\n\n ignoreinitfail : bool\n If this is True, ignores the initial failure to find a set of optimized\n Fourier parameters using the global optimization function and proceeds\n to do a least-squares fit anyway.\n\n verbose : bool\n If True, will indicate progress and warn of any problems.\n\n Returns\n -------\n\n dict\n This function returns a dict containing the model fit parameters, the\n minimized chi-sq value and the reduced chi-sq value. The form of this\n dict is mostly standardized across all functions in this module::\n\n {\n 'fittype':'gaussianeb',\n 'fitinfo':{\n 'initialparams':the initial EB params provided,\n 'finalparams':the final model fit EB params,\n 'finalparamerrs':formal errors in the params,\n 'leastsqfit':the full tuple returned by scipy.leastsq,\n 'fitmags': the model fit mags,\n 'fitepoch': the epoch of minimum light for the fit,\n },\n 'fitchisq': the minimized value of the fit's chi-sq,\n 'fitredchisq':the reduced chi-sq value,\n 'fitplotfile': the output fit plot if fitplot is not None,\n 'magseries':{\n 'times':input times in phase order of the model,\n 'phase':the phases of the model mags,\n 'mags':input mags/fluxes in the phase order of the model,\n 'errs':errs in the phase order of the model,\n 'magsarefluxes':input value of magsarefluxes kwarg\n }\n }\n\n\n '''\n\n stimes, smags, serrs = sigclip_magseries(times, mags, errs,\n sigclip=sigclip,\n magsarefluxes=magsarefluxes)\n\n # get rid of zero errs\n nzind = npnonzero(serrs)\n stimes, smags, serrs = stimes[nzind], smags[nzind], serrs[nzind]\n\n\n # check the ebparams\n ebperiod, ebepoch, ebdepth = ebparams[0:3]\n\n # check if we have a ebepoch to use\n if ebepoch is None:\n\n if verbose:\n LOGWARNING('no ebepoch given in ebparams, '\n 'trying to figure it out automatically...')\n # do a spline fit to figure out the approximate min of the LC\n try:\n spfit = spline_fit_magseries(times, mags, errs, ebperiod,\n sigclip=sigclip,\n magsarefluxes=magsarefluxes,\n verbose=verbose)\n ebepoch = spfit['fitinfo']['fitepoch']\n\n # if the spline-fit fails, try a savgol fit instead\n except Exception as e:\n sgfit = savgol_fit_magseries(times, mags, errs, ebperiod,\n sigclip=sigclip,\n magsarefluxes=magsarefluxes,\n verbose=verbose)\n ebepoch = sgfit['fitinfo']['fitepoch']\n\n # if everything failed, then bail out and ask for the ebepoch\n finally:\n\n if ebepoch is None:\n LOGERROR(\"couldn't automatically figure out the eb epoch, \"\n \"can't continue. please provide it in ebparams.\")\n\n # assemble the returndict\n returndict = {\n 'fittype':'gaussianeb',\n 'fitinfo':{\n 'initialparams':ebparams,\n 'finalparams':None,\n 'leastsqfit':None,\n 'fitmags':None,\n 'fitepoch':None,\n },\n 'fitchisq':npnan,\n 'fitredchisq':npnan,\n 'fitplotfile':None,\n 'magseries':{\n 'phase':None,\n 'times':None,\n 'mags':None,\n 'errs':None,\n 'magsarefluxes':magsarefluxes,\n },\n }\n\n return returndict\n\n else:\n\n if ebepoch.size > 1:\n if verbose:\n LOGWARNING('could not auto-find a single minimum '\n 'for ebepoch, using the first one returned')\n ebparams[1] = ebepoch[0]\n\n else:\n\n if verbose:\n LOGWARNING(\n 'using automatically determined ebepoch = %.5f'\n % ebepoch\n )\n ebparams[1] = ebepoch.item()\n\n # next, check the ebdepth and fix it to the form required\n if magsarefluxes:\n if ebdepth < 0.0:\n ebparams[2] = -ebdepth[2]\n\n else:\n if ebdepth > 0.0:\n ebparams[2] = -ebdepth[2]\n\n # finally, do the fit\n try:\n leastsqfit = spleastsq(eclipses.invgauss_eclipses_residual,\n ebparams,\n args=(stimes, smags, serrs),\n full_output=True)\n except Exception as e:\n leastsqfit = None\n\n # if the fit succeeded, then we can return the final parameters\n if leastsqfit and leastsqfit[-1] in (1,2,3,4):\n\n finalparams = leastsqfit[0]\n covxmatrix = leastsqfit[1]\n\n # calculate the chisq and reduced chisq\n fitmags, phase, ptimes, pmags, perrs = eclipses.invgauss_eclipses_func(\n finalparams,\n stimes, smags, serrs\n )\n fitchisq = npsum(\n ((fitmags - pmags)*(fitmags - pmags)) / (perrs*perrs)\n )\n fitredchisq = fitchisq/(len(pmags) - len(finalparams) - 1)\n\n # get the residual variance and calculate the formal 1-sigma errs on the\n # final parameters\n residuals = leastsqfit[2]['fvec']\n residualvariance = (\n npsum(residuals*residuals)/(pmags.size - finalparams.size)\n )\n if covxmatrix is not None:\n covmatrix = residualvariance*covxmatrix\n stderrs = npsqrt(npdiag(covmatrix))\n else:\n LOGERROR('covxmatrix not available, fit probably failed!')\n stderrs = None\n\n if verbose:\n LOGINFO(\n 'final fit done. chisq = %.5f, reduced chisq = %.5f' %\n (fitchisq, fitredchisq)\n )\n\n # get the fit epoch\n fperiod, fepoch = finalparams[:2]\n\n # assemble the returndict\n returndict = {\n 'fittype':'gaussianeb',\n 'fitinfo':{\n 'initialparams':ebparams,\n 'finalparams':finalparams,\n 'finalparamerrs':stderrs,\n 'leastsqfit':leastsqfit,\n 'fitmags':fitmags,\n 'fitepoch':fepoch,\n },\n 'fitchisq':fitchisq,\n 'fitredchisq':fitredchisq,\n 'fitplotfile':None,\n 'magseries':{\n 'phase':phase,\n 'times':ptimes,\n 'mags':pmags,\n 'errs':perrs,\n 'magsarefluxes':magsarefluxes,\n },\n }\n\n # make the fit plot if required\n if plotfit and isinstance(plotfit, str):\n\n make_fit_plot(phase, pmags, perrs, fitmags,\n fperiod, ptimes.min(), fepoch,\n plotfit,\n magsarefluxes=magsarefluxes)\n\n returndict['fitplotfile'] = plotfit\n\n return returndict\n\n # if the leastsq fit failed, return nothing\n else:\n\n LOGERROR('eb-fit: least-squared fit to the light curve failed!')\n\n # assemble the returndict\n returndict = {\n 'fittype':'gaussianeb',\n 'fitinfo':{\n 'initialparams':ebparams,\n 'finalparams':None,\n 'finalparamerrs':None,\n 'leastsqfit':leastsqfit,\n 'fitmags':None,\n 'fitepoch':None,\n },\n 'fitchisq':npnan,\n 'fitredchisq':npnan,\n 'fitplotfile':None,\n 'magseries':{\n 'phase':None,\n 'times':None,\n 'mags':None,\n 'errs':None,\n 'magsarefluxes':magsarefluxes,\n },\n }\n\n return returndict", "response": "This function fits a double inverted gaussian EB model to a magnitude time series."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef spline_fit_magseries(times, mags, errs, period,\n knotfraction=0.01,\n maxknots=30,\n sigclip=30.0,\n plotfit=False,\n ignoreinitfail=False,\n magsarefluxes=False,\n verbose=True):\n\n '''This fits a univariate cubic spline to the phased light curve.\n\n This fit may be better than the Fourier fit for sharply variable objects,\n like EBs, so can be used to distinguish them from other types of variables.\n\n Parameters\n ----------\n\n times,mags,errs : np.array\n The input mag/flux time-series to fit a spline to.\n\n period : float\n The period to use for the spline fit.\n\n knotfraction : float\n The knot fraction is the number of internal knots to use for the\n spline. A value of 0.01 (or 1%) of the total number of non-nan\n observations appears to work quite well, without over-fitting. maxknots\n controls the maximum number of knots that will be allowed.\n\n maxknots : int\n The maximum number of knots that will be used even if `knotfraction`\n gives a value to use larger than `maxknots`. This helps dealing with\n over-fitting to short time-scale variations.\n\n sigclip : float or int or sequence of two floats/ints or None\n If a single float or int, a symmetric sigma-clip will be performed using\n the number provided as the sigma-multiplier to cut out from the input\n time-series.\n\n If a list of two ints/floats is provided, the function will perform an\n 'asymmetric' sigma-clip. The first element in this list is the sigma\n value to use for fainter flux/mag values; the second element in this\n list is the sigma value to use for brighter flux/mag values. For\n example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma\n dimmings and greater than 3-sigma brightenings. Here the meaning of\n \"dimming\" and \"brightening\" is set by *physics* (not the magnitude\n system), which is why the `magsarefluxes` kwarg must be correctly set.\n\n If `sigclip` is None, no sigma-clipping will be performed, and the\n time-series (with non-finite elems removed) will be passed through to\n the output.\n\n magsarefluxes : bool\n If True, will treat the input values of `mags` as fluxes for purposes of\n plotting the fit and sig-clipping.\n\n plotfit : str or False\n If this is a string, this function will make a plot for the fit to the\n mag/flux time-series and writes the plot to the path specified here.\n\n ignoreinitfail : bool\n If this is True, ignores the initial failure to find a set of optimized\n Fourier parameters using the global optimization function and proceeds\n to do a least-squares fit anyway.\n\n verbose : bool\n If True, will indicate progress and warn of any problems.\n\n Returns\n -------\n\n dict\n This function returns a dict containing the model fit parameters, the\n minimized chi-sq value and the reduced chi-sq value. The form of this\n dict is mostly standardized across all functions in this module::\n\n {\n 'fittype':'spline',\n 'fitinfo':{\n 'nknots': the number of knots used for the fit\n 'fitmags': the model fit mags,\n 'fitepoch': the epoch of minimum light for the fit,\n },\n 'fitchisq': the minimized value of the fit's chi-sq,\n 'fitredchisq':the reduced chi-sq value,\n 'fitplotfile': the output fit plot if fitplot is not None,\n 'magseries':{\n 'times':input times in phase order of the model,\n 'phase':the phases of the model mags,\n 'mags':input mags/fluxes in the phase order of the model,\n 'errs':errs in the phase order of the model,\n 'magsarefluxes':input value of magsarefluxes kwarg\n }\n }\n\n '''\n\n # this is required to fit the spline correctly\n if errs is None:\n errs = npfull_like(mags, 0.005)\n\n # sigclip the magnitude time series\n stimes, smags, serrs = sigclip_magseries(times, mags, errs,\n sigclip=sigclip,\n magsarefluxes=magsarefluxes)\n # get rid of zero errs\n nzind = npnonzero(serrs)\n stimes, smags, serrs = stimes[nzind], smags[nzind], serrs[nzind]\n\n # phase the mag series\n phase, pmags, perrs, ptimes, mintime = (\n get_phased_quantities(stimes, smags, serrs, period)\n )\n\n # now figure out the number of knots up to max knots (=100)\n nobs = len(phase)\n nknots = int(npfloor(knotfraction*nobs))\n nknots = maxknots if nknots > maxknots else nknots\n splineknots = nplinspace(phase[0] + 0.01,\n phase[-1] - 0.01,\n num=nknots)\n\n # NOTE: newer scipy needs x to be strictly increasing. this means we should\n # filter out anything that doesn't have np.diff(phase) > 0.0\n # FIXME: this needs to be tested\n phase_diffs_ind = npdiff(phase) > 0.0\n incphase_ind = npconcatenate((nparray([True]), phase_diffs_ind))\n phase, pmags, perrs = (phase[incphase_ind],\n pmags[incphase_ind],\n perrs[incphase_ind])\n\n # generate and fit the spline\n spl = LSQUnivariateSpline(phase, pmags, t=splineknots, w=1.0/perrs)\n\n # calculate the spline fit to the actual phases, the chisq and red-chisq\n fitmags = spl(phase)\n\n fitchisq = npsum(\n ((fitmags - pmags)*(fitmags - pmags)) / (perrs*perrs)\n )\n\n fitredchisq = fitchisq/(len(pmags) - nknots - 1)\n\n if verbose:\n LOGINFO(\n 'spline fit done. nknots = %s, '\n 'chisq = %.5f, reduced chisq = %.5f' %\n (nknots, fitchisq, fitredchisq)\n )\n\n # figure out the time of light curve minimum (i.e. the fit epoch)\n # this is when the fit mag is maximum (i.e. the faintest)\n # or if magsarefluxes = True, then this is when fit flux is minimum\n if not magsarefluxes:\n fitmagminind = npwhere(fitmags == npmax(fitmags))\n else:\n fitmagminind = npwhere(fitmags == npmin(fitmags))\n if len(fitmagminind[0]) > 1:\n fitmagminind = (fitmagminind[0][0],)\n magseriesepoch = ptimes[fitmagminind]\n\n # assemble the returndict\n returndict = {\n 'fittype':'spline',\n 'fitinfo':{\n 'nknots':nknots,\n 'fitmags':fitmags,\n 'fitepoch':magseriesepoch\n },\n 'fitchisq':fitchisq,\n 'fitredchisq':fitredchisq,\n 'fitplotfile':None,\n 'magseries':{\n 'times':ptimes,\n 'phase':phase,\n 'mags':pmags,\n 'errs':perrs,\n 'magsarefluxes':magsarefluxes\n },\n }\n\n # make the fit plot if required\n if plotfit and isinstance(plotfit, str):\n\n make_fit_plot(phase, pmags, perrs, fitmags,\n period, mintime, magseriesepoch,\n plotfit,\n magsarefluxes=magsarefluxes)\n\n returndict['fitplotfile'] = plotfit\n\n return returndict", "response": "Fits a spline to a set of time - series times mags and errors."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nfits a Savitzky - Golay filter to the magnitude time series.", "response": "def savgol_fit_magseries(times, mags, errs, period,\n windowlength=None,\n polydeg=2,\n sigclip=30.0,\n plotfit=False,\n magsarefluxes=False,\n verbose=True):\n\n '''Fit a Savitzky-Golay filter to the magnitude/flux time series.\n\n SG fits successive sub-sets (windows) of adjacent data points with a\n low-order polynomial via least squares. At each point (magnitude), it\n returns the value of the polynomial at that magnitude's time. This is made\n significantly cheaper than *actually* performing least squares for each\n window through linear algebra tricks that are possible when specifying the\n window size and polynomial order beforehand. Numerical Recipes Ch 14.8\n gives an overview, Eq. 14.8.6 is what Scipy has implemented.\n\n The idea behind Savitzky-Golay is to preserve higher moments (>=2) of the\n input data series than would be done by a simple moving window average.\n\n Note that the filter assumes evenly spaced data, which magnitude time series\n are not. By *pretending* the data points are evenly spaced, we introduce an\n additional noise source in the function values. This is a relatively small\n noise source provided that the changes in the magnitude values across the\n full width of the N=windowlength point window is < sqrt(N/2) times the\n measurement noise on a single point.\n\n TODO:\n - Find correct dof for reduced chi squared in savgol_fit_magseries\n\n Parameters\n ----------\n\n times,mags,errs : np.array\n The input mag/flux time-series to fit the Savitsky-Golay model to.\n\n period : float\n The period to use for the model fit.\n\n windowlength : None or int\n The length of the filter window (the number of coefficients). Must be\n either positive and odd, or None. (The window is the number of points to\n the left, and to the right, of whatever point is having a polynomial fit\n to it locally). Bigger windows at fixed polynomial order risk lowering\n the amplitude of sharp features. If None, this routine (arbitrarily)\n sets the `windowlength` for phased LCs to be either the number of finite\n data points divided by 300, or polydeg+3, whichever is bigger.\n\n polydeg : int\n This is the order of the polynomial used to fit the samples. Must be\n less than `windowlength`. \"Higher-order filters do better at preserving\n feature heights and widths, but do less smoothing on broader features.\"\n (Numerical Recipes).\n\n sigclip : float or int or sequence of two floats/ints or None\n If a single float or int, a symmetric sigma-clip will be performed using\n the number provided as the sigma-multiplier to cut out from the input\n time-series.\n\n If a list of two ints/floats is provided, the function will perform an\n 'asymmetric' sigma-clip. The first element in this list is the sigma\n value to use for fainter flux/mag values; the second element in this\n list is the sigma value to use for brighter flux/mag values. For\n example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma\n dimmings and greater than 3-sigma brightenings. Here the meaning of\n \"dimming\" and \"brightening\" is set by *physics* (not the magnitude\n system), which is why the `magsarefluxes` kwarg must be correctly set.\n\n If `sigclip` is None, no sigma-clipping will be performed, and the\n time-series (with non-finite elems removed) will be passed through to\n the output.\n\n magsarefluxes : bool\n If True, will treat the input values of `mags` as fluxes for purposes of\n plotting the fit and sig-clipping.\n\n plotfit : str or False\n If this is a string, this function will make a plot for the fit to the\n mag/flux time-series and writes the plot to the path specified here.\n\n ignoreinitfail : bool\n If this is True, ignores the initial failure to find a set of optimized\n Fourier parameters using the global optimization function and proceeds\n to do a least-squares fit anyway.\n\n verbose : bool\n If True, will indicate progress and warn of any problems.\n\n Returns\n -------\n\n dict\n This function returns a dict containing the model fit parameters, the\n minimized chi-sq value and the reduced chi-sq value. The form of this\n dict is mostly standardized across all functions in this module::\n\n {\n 'fittype':'savgol',\n 'fitinfo':{\n 'windowlength': the window length used for the fit,\n 'polydeg':the polynomial degree used for the fit,\n 'fitmags': the model fit mags,\n 'fitepoch': the epoch of minimum light for the fit,\n },\n 'fitchisq': the minimized value of the fit's chi-sq,\n 'fitredchisq':the reduced chi-sq value,\n 'fitplotfile': the output fit plot if fitplot is not None,\n 'magseries':{\n 'times':input times in phase order of the model,\n 'phase':the phases of the model mags,\n 'mags':input mags/fluxes in the phase order of the model,\n 'errs':errs in the phase order of the model,\n 'magsarefluxes':input value of magsarefluxes kwarg\n }\n }\n\n '''\n stimes, smags, serrs = sigclip_magseries(times, mags, errs,\n sigclip=sigclip,\n magsarefluxes=magsarefluxes)\n\n # get rid of zero errs\n nzind = npnonzero(serrs)\n stimes, smags, serrs = stimes[nzind], smags[nzind], serrs[nzind]\n\n phase, pmags, perrs, ptimes, mintime = (\n get_phased_quantities(stimes, smags, serrs, period)\n )\n\n if not isinstance(windowlength, int):\n windowlength = max(\n polydeg + 3,\n int(len(phase)/300)\n )\n if windowlength % 2 == 0:\n windowlength += 1\n\n if verbose:\n LOGINFO('applying Savitzky-Golay filter with '\n 'window length %s and polynomial degree %s to '\n 'mag series with %s observations, '\n 'using period %.6f, folded at %.6f' % (windowlength,\n polydeg,\n len(pmags),\n period,\n mintime))\n\n # generate the function values obtained by applying the SG filter. The\n # \"wrap\" option is best for phase-folded LCs.\n sgf = savgol_filter(pmags, windowlength, polydeg, mode='wrap')\n\n # here the \"fit\" to the phases is the function produced by the\n # Savitzky-Golay filter. then compute the chisq and red-chisq.\n fitmags = sgf\n\n fitchisq = npsum(\n ((fitmags - pmags)*(fitmags - pmags)) / (perrs*perrs)\n )\n\n # TODO: quantify dof for SG filter.\n nparams = int(len(pmags)/windowlength) * polydeg\n fitredchisq = fitchisq/(len(pmags) - nparams - 1)\n fitredchisq = -99.\n\n if verbose:\n LOGINFO(\n 'SG filter applied. chisq = %.5f, reduced chisq = %.5f' %\n (fitchisq, fitredchisq)\n )\n\n # figure out the time of light curve minimum (i.e. the fit epoch)\n # this is when the fit mag is maximum (i.e. the faintest)\n # or if magsarefluxes = True, then this is when fit flux is minimum\n if not magsarefluxes:\n fitmagminind = npwhere(fitmags == npmax(fitmags))\n else:\n fitmagminind = npwhere(fitmags == npmin(fitmags))\n if len(fitmagminind[0]) > 1:\n fitmagminind = (fitmagminind[0][0],)\n magseriesepoch = ptimes[fitmagminind]\n\n # assemble the returndict\n returndict = {\n 'fittype':'savgol',\n 'fitinfo':{\n 'windowlength':windowlength,\n 'polydeg':polydeg,\n 'fitmags':fitmags,\n 'fitepoch':magseriesepoch\n },\n 'fitchisq':fitchisq,\n 'fitredchisq':fitredchisq,\n 'fitplotfile':None,\n 'magseries':{\n 'times':ptimes,\n 'phase':phase,\n 'mags':pmags,\n 'errs':perrs,\n 'magsarefluxes':magsarefluxes\n }\n }\n\n # make the fit plot if required\n if plotfit and isinstance(plotfit, str):\n\n make_fit_plot(phase, pmags, perrs, fitmags,\n period, mintime, magseriesepoch,\n plotfit,\n magsarefluxes=magsarefluxes)\n\n returndict['fitplotfile'] = plotfit\n\n return returndict"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nfits an arbitrary - order Legendre polynomial to the time - series times mags errs period", "response": "def legendre_fit_magseries(times, mags, errs, period,\n legendredeg=10,\n sigclip=30.0,\n plotfit=False,\n magsarefluxes=False,\n verbose=True):\n\n '''Fit an arbitrary-order Legendre series, via least squares, to the\n magnitude/flux time series.\n\n This is a series of the form::\n\n p(x) = c_0*L_0(x) + c_1*L_1(x) + c_2*L_2(x) + ... + c_n*L_n(x)\n\n where L_i's are Legendre polynomials (also called \"Legendre functions of the\n first kind\") and c_i's are the coefficients being fit.\n\n This function is mainly just a wrapper to\n `numpy.polynomial.legendre.Legendre.fit`.\n\n Parameters\n ----------\n\n times,mags,errs : np.array\n The input mag/flux time-series to fit a Legendre series polynomial to.\n\n period : float\n The period to use for the Legendre fit.\n\n legendredeg : int\n This is `n` in the equation above, e.g. if you give `n=5`, you will\n get 6 coefficients. This number should be much less than the number of\n data points you are fitting.\n\n sigclip : float or int or sequence of two floats/ints or None\n If a single float or int, a symmetric sigma-clip will be performed using\n the number provided as the sigma-multiplier to cut out from the input\n time-series.\n\n If a list of two ints/floats is provided, the function will perform an\n 'asymmetric' sigma-clip. The first element in this list is the sigma\n value to use for fainter flux/mag values; the second element in this\n list is the sigma value to use for brighter flux/mag values. For\n example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma\n dimmings and greater than 3-sigma brightenings. Here the meaning of\n \"dimming\" and \"brightening\" is set by *physics* (not the magnitude\n system), which is why the `magsarefluxes` kwarg must be correctly set.\n\n If `sigclip` is None, no sigma-clipping will be performed, and the\n time-series (with non-finite elems removed) will be passed through to\n the output.\n\n magsarefluxes : bool\n If True, will treat the input values of `mags` as fluxes for purposes of\n plotting the fit and sig-clipping.\n\n plotfit : str or False\n If this is a string, this function will make a plot for the fit to the\n mag/flux time-series and writes the plot to the path specified here.\n\n ignoreinitfail : bool\n If this is True, ignores the initial failure to find a set of optimized\n Fourier parameters using the global optimization function and proceeds\n to do a least-squares fit anyway.\n\n verbose : bool\n If True, will indicate progress and warn of any problems.\n\n Returns\n -------\n\n dict\n This function returns a dict containing the model fit parameters, the\n minimized chi-sq value and the reduced chi-sq value. The form of this\n dict is mostly standardized across all functions in this module::\n\n {\n 'fittype':'legendre',\n 'fitinfo':{\n 'legendredeg': the Legendre polynomial degree used,\n 'fitmags': the model fit mags,\n 'fitepoch': the epoch of minimum light for the fit,\n },\n 'fitchisq': the minimized value of the fit's chi-sq,\n 'fitredchisq':the reduced chi-sq value,\n 'fitplotfile': the output fit plot if fitplot is not None,\n 'magseries':{\n 'times':input times in phase order of the model,\n 'phase':the phases of the model mags,\n 'mags':input mags/fluxes in the phase order of the model,\n 'errs':errs in the phase order of the model,\n 'magsarefluxes':input value of magsarefluxes kwarg\n }\n }\n\n\n '''\n stimes, smags, serrs = sigclip_magseries(times, mags, errs,\n sigclip=sigclip,\n magsarefluxes=magsarefluxes)\n\n # get rid of zero errs\n nzind = npnonzero(serrs)\n stimes, smags, serrs = stimes[nzind], smags[nzind], serrs[nzind]\n\n\n phase, pmags, perrs, ptimes, mintime = (\n get_phased_quantities(stimes, smags, serrs, period)\n )\n\n if verbose:\n LOGINFO('fitting Legendre series with '\n 'maximum Legendre polynomial order %s to '\n 'mag series with %s observations, '\n 'using period %.6f, folded at %.6f' % (legendredeg,\n len(pmags),\n period,\n mintime))\n\n # Least squares fit of Legendre polynomial series to the data. The window\n # and domain (see \"Using the Convenience Classes\" in the numpy\n # documentation) are handled automatically, scaling the times to a minimal\n # domain in [-1,1], in which Legendre polynomials are a complete basis.\n\n p = Legendre.fit(phase, pmags, legendredeg)\n coeffs = p.coef\n fitmags = p(phase)\n\n # Now compute the chisq and red-chisq.\n\n fitchisq = npsum(\n ((fitmags - pmags)*(fitmags - pmags)) / (perrs*perrs)\n )\n\n nparams = legendredeg + 1\n fitredchisq = fitchisq/(len(pmags) - nparams - 1)\n\n if verbose:\n LOGINFO(\n 'Legendre fit done. chisq = %.5f, reduced chisq = %.5f' %\n (fitchisq, fitredchisq)\n )\n\n # figure out the time of light curve minimum (i.e. the fit epoch)\n # this is when the fit mag is maximum (i.e. the faintest)\n # or if magsarefluxes = True, then this is when fit flux is minimum\n if not magsarefluxes:\n fitmagminind = npwhere(fitmags == npmax(fitmags))\n else:\n fitmagminind = npwhere(fitmags == npmin(fitmags))\n if len(fitmagminind[0]) > 1:\n fitmagminind = (fitmagminind[0][0],)\n magseriesepoch = ptimes[fitmagminind]\n\n # assemble the returndict\n returndict = {\n 'fittype':'legendre',\n 'fitinfo':{\n 'legendredeg':legendredeg,\n 'fitmags':fitmags,\n 'fitepoch':magseriesepoch,\n 'finalparams':coeffs,\n },\n 'fitchisq':fitchisq,\n 'fitredchisq':fitredchisq,\n 'fitplotfile':None,\n 'magseries':{\n 'times':ptimes,\n 'phase':phase,\n 'mags':pmags,\n 'errs':perrs,\n 'magsarefluxes':magsarefluxes\n }\n }\n\n # make the fit plot if required\n if plotfit and isinstance(plotfit, str):\n\n make_fit_plot(phase, pmags, perrs, fitmags,\n period, mintime, magseriesepoch,\n plotfit,\n magsarefluxes=magsarefluxes)\n\n returndict['fitplotfile'] = plotfit\n\n return returndict"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef update_checkplotdict_nbrlcs(\n checkplotdict,\n timecol, magcol, errcol,\n lcformat='hat-sql',\n lcformatdir=None,\n verbose=True,\n):\n\n '''For all neighbors in a checkplotdict, make LCs and phased LCs.\n\n Parameters\n ----------\n\n checkplotdict : dict\n This is the checkplot to process. The light curves for the neighbors to\n the object here will be extracted from the stored file paths, and this\n function will make plots of these time-series. If the object has 'best'\n periods and epochs generated by period-finder functions in this\n checkplotdict, phased light curve plots of each neighbor will be made\n using these to check the effects of blending.\n\n timecol,magcol,errcol : str\n The timecol, magcol, and errcol keys used to generate this object's\n checkplot. This is used to extract the correct times-series from the\n neighbors' light curves.\n\n lcformat : str\n This is the `formatkey` associated with your light curve format, which\n you previously passed in to the `lcproc.register_lcformat`\n function. This will be used to look up how to find and read the light\n curves specified in `basedir` or `use_list_of_filenames`.\n\n lcformatdir : str or None\n If this is provided, gives the path to a directory when you've stored\n your lcformat description JSONs, other than the usual directories lcproc\n knows to search for them in. Use this along with `lcformat` to specify\n an LC format JSON file that's not currently registered with lcproc.\n\n Returns\n -------\n\n dict\n The input checkplotdict is returned with the neighor light curve plots\n added in.\n\n '''\n\n try:\n formatinfo = get_lcformat(lcformat,\n use_lcformat_dir=lcformatdir)\n if formatinfo:\n (dfileglob, readerfunc,\n dtimecols, dmagcols, derrcols,\n magsarefluxes, normfunc) = formatinfo\n else:\n LOGERROR(\"can't figure out the light curve format\")\n return checkplotdict\n except Exception as e:\n LOGEXCEPTION(\"can't figure out the light curve format\")\n return checkplotdict\n\n if not ('neighbors' in checkplotdict and\n checkplotdict['neighbors'] and\n len(checkplotdict['neighbors']) > 0):\n\n LOGERROR('no neighbors for %s, not updating...' %\n (checkplotdict['objectid']))\n return checkplotdict\n\n # get our object's magkeys to compare to the neighbor\n objmagkeys = {}\n\n # handle diff generations of checkplots\n if 'available_bands' in checkplotdict['objectinfo']:\n mclist = checkplotdict['objectinfo']['available_bands']\n else:\n mclist = ('bmag','vmag','rmag','imag','jmag','hmag','kmag',\n 'sdssu','sdssg','sdssr','sdssi','sdssz')\n\n for mc in mclist:\n if (mc in checkplotdict['objectinfo'] and\n checkplotdict['objectinfo'][mc] is not None and\n np.isfinite(checkplotdict['objectinfo'][mc])):\n\n objmagkeys[mc] = checkplotdict['objectinfo'][mc]\n\n\n # if there are actually neighbors, go through them in order\n for nbr in checkplotdict['neighbors']:\n\n objectid, lcfpath = (nbr['objectid'],\n nbr['lcfpath'])\n\n # get the light curve\n if not os.path.exists(lcfpath):\n LOGERROR('objectid: %s, neighbor: %s, '\n 'lightcurve: %s not found, skipping...' %\n (checkplotdict['objectid'], objectid, lcfpath))\n continue\n\n lcdict = readerfunc(lcfpath)\n\n # this should handle lists/tuples being returned by readerfunc\n # we assume that the first element is the actual lcdict\n # FIXME: figure out how to not need this assumption\n if ( (isinstance(lcdict, (list, tuple))) and\n (isinstance(lcdict[0], dict)) ):\n lcdict = lcdict[0]\n\n\n # 0. get this neighbor's magcols and get the magdiff and colordiff\n # between it and the object\n\n nbrmagkeys = {}\n\n for mc in objmagkeys:\n\n if (('objectinfo' in lcdict) and\n (isinstance(lcdict['objectinfo'], dict)) and\n (mc in lcdict['objectinfo']) and\n (lcdict['objectinfo'][mc] is not None) and\n (np.isfinite(lcdict['objectinfo'][mc]))):\n\n nbrmagkeys[mc] = lcdict['objectinfo'][mc]\n\n # now calculate the magdiffs\n magdiffs = {}\n for omc in objmagkeys:\n if omc in nbrmagkeys:\n magdiffs[omc] = objmagkeys[omc] - nbrmagkeys[omc]\n\n # calculate colors and colordiffs\n colordiffs = {}\n\n # generate the list of colors to get\n # NOTE: here, we don't really bother with new/old gen checkplots\n # maybe change this later to handle arbitrary colors\n\n for ctrio in (['bmag','vmag','bvcolor'],\n ['vmag','kmag','vkcolor'],\n ['jmag','kmag','jkcolor'],\n ['sdssi','jmag','ijcolor'],\n ['sdssg','kmag','gkcolor'],\n ['sdssg','sdssr','grcolor']):\n m1, m2, color = ctrio\n\n if (m1 in objmagkeys and\n m2 in objmagkeys and\n m1 in nbrmagkeys and\n m2 in nbrmagkeys):\n\n objcolor = objmagkeys[m1] - objmagkeys[m2]\n nbrcolor = nbrmagkeys[m1] - nbrmagkeys[m2]\n colordiffs[color] = objcolor - nbrcolor\n\n # finally, add all the color and magdiff info to the nbr dict\n nbr.update({'magdiffs':magdiffs,\n 'colordiffs':colordiffs})\n\n #\n # process magcols\n #\n\n # normalize using the special function if specified\n if normfunc is not None:\n lcdict = normfunc(lcdict)\n\n try:\n\n # get the times, mags, and errs\n # dereference the columns and get them from the lcdict\n if '.' in timecol:\n timecolget = timecol.split('.')\n else:\n timecolget = [timecol]\n times = _dict_get(lcdict, timecolget)\n\n if '.' in magcol:\n magcolget = magcol.split('.')\n else:\n magcolget = [magcol]\n mags = _dict_get(lcdict, magcolget)\n\n if '.' in errcol:\n errcolget = errcol.split('.')\n else:\n errcolget = [errcol]\n errs = _dict_get(lcdict, errcolget)\n\n except KeyError:\n\n LOGERROR('LC for neighbor: %s (target object: %s) does not '\n 'have one or more of the required columns: %s, '\n 'skipping...' %\n (objectid, checkplotdict['objectid'],\n ', '.join([timecol, magcol, errcol])))\n continue\n\n # filter the input times, mags, errs; do sigclipping and normalization\n stimes, smags, serrs = sigclip_magseries(times,\n mags,\n errs,\n magsarefluxes=magsarefluxes,\n sigclip=4.0)\n\n # normalize here if not using special normalization\n if normfunc is None:\n ntimes, nmags = normalize_magseries(\n stimes, smags,\n magsarefluxes=magsarefluxes\n )\n xtimes, xmags, xerrs = ntimes, nmags, serrs\n else:\n xtimes, xmags, xerrs = stimes, smags, serrs\n\n\n # check if this neighbor has enough finite points in its LC\n # fail early if not enough light curve points\n if ((xtimes is None) or (xmags is None) or (xerrs is None) or\n (xtimes.size < 49) or (xmags.size < 49) or (xerrs.size < 49)):\n\n LOGERROR(\"one or more of times, mags, errs appear to be None \"\n \"after sig-clipping. are the measurements all nan? \"\n \"can't make neighbor light curve plots \"\n \"for target: %s, neighbor: %s, neighbor LC: %s\" %\n (checkplotdict['objectid'],\n nbr['objectid'],\n nbr['lcfpath']))\n continue\n\n #\n # now we can start doing stuff if everything checks out\n #\n\n # make an unphased mag-series plot\n nbrdict = _pkl_magseries_plot(xtimes,\n xmags,\n xerrs,\n magsarefluxes=magsarefluxes)\n # update the nbr\n nbr.update(nbrdict)\n\n # for each lspmethod in the checkplot, make a corresponding plot for\n # this neighbor\n\n # figure out the period finder methods present\n if 'pfmethods' in checkplotdict:\n pfmethods = checkplotdict['pfmethods']\n else:\n pfmethods = []\n for cpkey in checkplotdict:\n for pfkey in PFMETHODS:\n if pfkey in cpkey:\n pfmethods.append(pfkey)\n\n for lspt in pfmethods:\n\n # initialize this lspmethod entry\n nbr[lspt] = {}\n\n # we only care about the best period and its options\n operiod, oepoch = (checkplotdict[lspt][0]['period'],\n checkplotdict[lspt][0]['epoch'])\n (ophasewrap, ophasesort, ophasebin,\n ominbinelems, oplotxlim) = (\n checkplotdict[lspt][0]['phasewrap'],\n checkplotdict[lspt][0]['phasesort'],\n checkplotdict[lspt][0]['phasebin'],\n checkplotdict[lspt][0]['minbinelems'],\n checkplotdict[lspt][0]['plotxlim'],\n )\n\n # make the phasedlc plot for this period\n nbr = _pkl_phased_magseries_plot(\n nbr,\n lspt.split('-')[1], # this splits '-'\n 0,\n xtimes, xmags, xerrs,\n operiod, oepoch,\n phasewrap=ophasewrap,\n phasesort=ophasesort,\n phasebin=ophasebin,\n minbinelems=ominbinelems,\n plotxlim=oplotxlim,\n magsarefluxes=magsarefluxes,\n verbose=verbose,\n override_pfmethod=lspt\n )\n\n # at this point, this neighbor's dict should be up to date with all\n # info, magseries plot, and all phased LC plots\n # return the updated checkplotdict\n return checkplotdict", "response": "This function updates the checkplotdict with the light curves for the object with the given timecol magcol and errcol."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef runcp_worker(task):\n '''\n This is the worker for running checkplots.\n\n Parameters\n ----------\n\n task : tuple\n This is of the form: (pfpickle, outdir, lcbasedir, kwargs).\n\n Returns\n -------\n\n list of str\n The list of checkplot pickles returned by the `runcp` function.\n\n '''\n\n pfpickle, outdir, lcbasedir, kwargs = task\n\n try:\n\n return runcp(pfpickle, outdir, lcbasedir, **kwargs)\n\n except Exception as e:\n\n LOGEXCEPTION(' could not make checkplots for %s: %s' % (pfpickle, e))\n return None", "response": "This is the worker for running checkplots."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef parallel_cp(\n pfpicklelist,\n outdir,\n lcbasedir,\n fast_mode=False,\n lcfnamelist=None,\n cprenorm=False,\n lclistpkl=None,\n gaia_max_timeout=60.0,\n gaia_mirror=None,\n nbrradiusarcsec=60.0,\n maxnumneighbors=5,\n makeneighborlcs=True,\n xmatchinfo=None,\n xmatchradiusarcsec=3.0,\n sigclip=10.0,\n minobservations=99,\n lcformat='hat-sql',\n lcformatdir=None,\n timecols=None,\n magcols=None,\n errcols=None,\n skipdone=False,\n done_callback=None,\n done_callback_args=None,\n done_callback_kwargs=None,\n liststartindex=None,\n maxobjects=None,\n nworkers=NCPUS,\n):\n '''This drives the parallel execution of `runcp` for a list of periodfinding\n result pickles.\n\n Parameters\n ----------\n\n pfpicklelist : list of str or list of Nones\n This is the list of the filenames of the period-finding result pickles\n to process. To make checkplots using the light curves directly, set this\n to a list of Nones with the same length as the list of light curve files\n that you provide in `lcfnamelist`.\n\n outdir : str\n The directory the checkplot pickles will be written to.\n\n lcbasedir : str\n The base directory that this function will look in to find the light\n curves pointed to by the period-finding result files. If you're using\n `lcfnamelist` to provide a list of light curve filenames directly, this\n arg is ignored.\n\n lcfnamelist : list of str or None\n If this is provided, it must be a list of the input light curve\n filenames to process. These can either be associated with each input\n period-finder result pickle, or can be provided standalone to make\n checkplots without phased LC plots in them. In the second case, you must\n set `pfpicklelist` to a list of Nones that matches the length of\n `lcfnamelist`.\n\n cprenorm : bool\n Set this to True if the light curves should be renormalized by\n `checkplot.checkplot_pickle`. This is set to False by default because we\n do our own normalization in this function using the light curve's\n registered normalization function and pass the normalized times, mags,\n errs to the `checkplot.checkplot_pickle` function.\n\n lclistpkl : str or dict\n This is either the filename of a pickle or the actual dict produced by\n lcproc.make_lclist. This is used to gather neighbor information.\n\n nbrradiusarcsec : float\n The radius in arcseconds to use for a search conducted around the\n coordinates of this object to look for any potential confusion and\n blending of variability amplitude caused by their proximity.\n\n maxnumneighbors : int\n The maximum number of neighbors that will have their light curves and\n magnitudes noted in this checkplot as potential blends with the target\n object.\n\n makeneighborlcs : bool\n If True, will make light curve and phased light curve plots for all\n neighbors found in the object collection for each input object.\n\n fast_mode : bool or float\n This runs the external catalog operations in a \"fast\" mode, with short\n timeouts and not trying to hit external catalogs that take a long time\n to respond.\n\n If this is set to True, the default settings for the external requests\n will then become::\n\n skyview_lookup = False\n skyview_timeout = 10.0\n skyview_retry_failed = False\n dust_timeout = 10.0\n gaia_submit_timeout = 7.0\n gaia_max_timeout = 10.0\n gaia_submit_tries = 2\n complete_query_later = False\n search_simbad = False\n\n If this is a float, will run in \"fast\" mode with the provided timeout\n value in seconds and the following settings::\n\n skyview_lookup = True\n skyview_timeout = fast_mode\n skyview_retry_failed = False\n dust_timeout = fast_mode\n gaia_submit_timeout = 0.66*fast_mode\n gaia_max_timeout = fast_mode\n gaia_submit_tries = 2\n complete_query_later = False\n search_simbad = False\n\n gaia_max_timeout : float\n Sets the timeout in seconds to use when waiting for the GAIA service to\n respond to our request for the object's information. Note that if\n `fast_mode` is set, this is ignored.\n\n gaia_mirror : str or None\n This sets the GAIA mirror to use. This is a key in the\n `services.gaia.GAIA_URLS` dict which defines the URLs to hit for each\n mirror.\n\n xmatchinfo : str or dict\n This is either the xmatch dict produced by the function\n `load_xmatch_external_catalogs` above, or the path to the xmatch info\n pickle file produced by that function.\n\n xmatchradiusarcsec : float\n This is the cross-matching radius to use in arcseconds.\n\n minobservations : int\n The minimum of observations the input object's mag/flux time-series must\n have for this function to plot its light curve and phased light\n curve. If the object has less than this number, no light curves will be\n plotted, but the checkplotdict will still contain all of the other\n information.\n\n sigclip : float or int or sequence of two floats/ints or None\n If a single float or int, a symmetric sigma-clip will be performed using\n the number provided as the sigma-multiplier to cut out from the input\n time-series.\n\n If a list of two ints/floats is provided, the function will perform an\n 'asymmetric' sigma-clip. The first element in this list is the sigma\n value to use for fainter flux/mag values; the second element in this\n list is the sigma value to use for brighter flux/mag values. For\n example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma\n dimmings and greater than 3-sigma brightenings. Here the meaning of\n \"dimming\" and \"brightening\" is set by *physics* (not the magnitude\n system), which is why the `magsarefluxes` kwarg must be correctly set.\n\n If `sigclip` is None, no sigma-clipping will be performed, and the\n time-series (with non-finite elems removed) will be passed through to\n the output.\n\n lcformat : str\n This is the `formatkey` associated with your light curve format, which\n you previously passed in to the `lcproc.register_lcformat`\n function. This will be used to look up how to find and read the light\n curves specified in `basedir` or `use_list_of_filenames`.\n\n lcformatdir : str or None\n If this is provided, gives the path to a directory when you've stored\n your lcformat description JSONs, other than the usual directories lcproc\n knows to search for them in. Use this along with `lcformat` to specify\n an LC format JSON file that's not currently registered with lcproc.\n\n timecols : list of str or None\n The timecol keys to use from the lcdict in generating this checkplot.\n\n magcols : list of str or None\n The magcol keys to use from the lcdict in generating this checkplot.\n\n errcols : list of str or None\n The errcol keys to use from the lcdict in generating this checkplot.\n\n skipdone : bool\n This indicates if this function will skip creating checkplots that\n already exist corresponding to the current `objectid` and `magcol`. If\n `skipdone` is set to True, this will be done.\n\n done_callback : Python function or None\n This is used to provide a function to execute after the checkplot\n pickles are generated. This is useful if you want to stream the results\n of checkplot making to some other process, e.g. directly running an\n ingestion into an LCC-Server collection. The function will always get\n the list of the generated checkplot pickles as its first arg, and all of\n the kwargs for runcp in the kwargs dict. Additional args and kwargs can\n be provided by giving a list in the `done_callbacks_args` kwarg and a\n dict in the `done_callbacks_kwargs` kwarg.\n\n NOTE: the function you pass in here should be pickleable by normal\n Python if you want to use it with the parallel_cp and parallel_cp_lcdir\n functions below.\n\n done_callback_args : tuple or None\n If not None, contains any args to pass into the `done_callback`\n function.\n\n done_callback_kwargs : dict or None\n If not None, contains any kwargs to pass into the `done_callback`\n function.\n\n liststartindex : int\n The index of the `pfpicklelist` (and `lcfnamelist` if provided) to start\n working at.\n\n maxobjects : int\n The maximum number of objects to process in this run. Use this with\n `liststartindex` to effectively distribute working on a large list of\n input period-finding result pickles (and light curves if `lcfnamelist`\n is also provided) over several sessions or machines.\n\n nworkers : int\n The number of parallel workers that will work on the checkplot\n generation process.\n\n Returns\n -------\n\n dict\n This returns a dict with keys = input period-finding pickles and vals =\n list of the corresponding checkplot pickles produced.\n\n '''\n\n # work around the Darwin segfault after fork if no network activity in\n # main thread bug: https://bugs.python.org/issue30385#msg293958\n if sys.platform == 'darwin':\n import requests\n requests.get('http://captive.apple.com/hotspot-detect.html')\n\n if not os.path.exists(outdir):\n os.mkdir(outdir)\n\n # handle the start and end indices\n if (liststartindex is not None) and (maxobjects is None):\n pfpicklelist = pfpicklelist[liststartindex:]\n if lcfnamelist is not None:\n lcfnamelist = lcfnamelist[liststartindex:]\n\n elif (liststartindex is None) and (maxobjects is not None):\n pfpicklelist = pfpicklelist[:maxobjects]\n if lcfnamelist is not None:\n lcfnamelist = lcfnamelist[:maxobjects]\n\n elif (liststartindex is not None) and (maxobjects is not None):\n pfpicklelist = (\n pfpicklelist[liststartindex:liststartindex+maxobjects]\n )\n if lcfnamelist is not None:\n lcfnamelist = lcfnamelist[liststartindex:liststartindex+maxobjects]\n\n # if the lcfnamelist is not provided, create a dummy\n if lcfnamelist is None:\n lcfnamelist = [None]*len(pfpicklelist)\n\n tasklist = [(x, outdir, lcbasedir,\n {'lcformat':lcformat,\n 'lcformatdir':lcformatdir,\n 'lcfname':y,\n 'timecols':timecols,\n 'magcols':magcols,\n 'errcols':errcols,\n 'lclistpkl':lclistpkl,\n 'gaia_max_timeout':gaia_max_timeout,\n 'gaia_mirror':gaia_mirror,\n 'nbrradiusarcsec':nbrradiusarcsec,\n 'maxnumneighbors':maxnumneighbors,\n 'makeneighborlcs':makeneighborlcs,\n 'xmatchinfo':xmatchinfo,\n 'xmatchradiusarcsec':xmatchradiusarcsec,\n 'sigclip':sigclip,\n 'minobservations':minobservations,\n 'skipdone':skipdone,\n 'cprenorm':cprenorm,\n 'fast_mode':fast_mode,\n 'done_callback':done_callback,\n 'done_callback_args':done_callback_args,\n 'done_callback_kwargs':done_callback_kwargs}) for\n x,y in zip(pfpicklelist, lcfnamelist)]\n\n resultfutures = []\n results = []\n\n with ProcessPoolExecutor(max_workers=nworkers) as executor:\n resultfutures = executor.map(runcp_worker, tasklist)\n\n results = [x for x in resultfutures]\n\n executor.shutdown()\n return results", "response": "This function drives the parallel execution of runcp for a list of period - finding result pickles."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef parallel_cp_pfdir(pfpickledir,\n outdir,\n lcbasedir,\n pfpickleglob='periodfinding-*.pkl*',\n lclistpkl=None,\n cprenorm=False,\n nbrradiusarcsec=60.0,\n maxnumneighbors=5,\n makeneighborlcs=True,\n fast_mode=False,\n gaia_max_timeout=60.0,\n gaia_mirror=None,\n xmatchinfo=None,\n xmatchradiusarcsec=3.0,\n minobservations=99,\n sigclip=10.0,\n lcformat='hat-sql',\n lcformatdir=None,\n timecols=None,\n magcols=None,\n errcols=None,\n skipdone=False,\n done_callback=None,\n done_callback_args=None,\n done_callback_kwargs=None,\n maxobjects=None,\n nworkers=32):\n\n '''This drives the parallel execution of `runcp` for a directory of\n periodfinding pickles.\n\n Parameters\n ----------\n\n pfpickledir : str\n This is the directory containing all of the period-finding pickles to\n process.\n\n outdir : str\n The directory the checkplot pickles will be written to.\n\n lcbasedir : str\n The base directory that this function will look in to find the light\n curves pointed to by the period-finding result files. If you're using\n `lcfnamelist` to provide a list of light curve filenames directly, this\n arg is ignored.\n\n pkpickleglob : str\n This is a UNIX file glob to select period-finding result pickles in the\n specified `pfpickledir`.\n\n lclistpkl : str or dict\n This is either the filename of a pickle or the actual dict produced by\n lcproc.make_lclist. This is used to gather neighbor information.\n\n cprenorm : bool\n Set this to True if the light curves should be renormalized by\n `checkplot.checkplot_pickle`. This is set to False by default because we\n do our own normalization in this function using the light curve's\n registered normalization function and pass the normalized times, mags,\n errs to the `checkplot.checkplot_pickle` function.\n\n nbrradiusarcsec : float\n The radius in arcseconds to use for a search conducted around the\n coordinates of this object to look for any potential confusion and\n blending of variability amplitude caused by their proximity.\n\n maxnumneighbors : int\n The maximum number of neighbors that will have their light curves and\n magnitudes noted in this checkplot as potential blends with the target\n object.\n\n makeneighborlcs : bool\n If True, will make light curve and phased light curve plots for all\n neighbors found in the object collection for each input object.\n\n fast_mode : bool or float\n This runs the external catalog operations in a \"fast\" mode, with short\n timeouts and not trying to hit external catalogs that take a long time\n to respond.\n\n If this is set to True, the default settings for the external requests\n will then become::\n\n skyview_lookup = False\n skyview_timeout = 10.0\n skyview_retry_failed = False\n dust_timeout = 10.0\n gaia_submit_timeout = 7.0\n gaia_max_timeout = 10.0\n gaia_submit_tries = 2\n complete_query_later = False\n search_simbad = False\n\n If this is a float, will run in \"fast\" mode with the provided timeout\n value in seconds and the following settings::\n\n skyview_lookup = True\n skyview_timeout = fast_mode\n skyview_retry_failed = False\n dust_timeout = fast_mode\n gaia_submit_timeout = 0.66*fast_mode\n gaia_max_timeout = fast_mode\n gaia_submit_tries = 2\n complete_query_later = False\n search_simbad = False\n\n gaia_max_timeout : float\n Sets the timeout in seconds to use when waiting for the GAIA service to\n respond to our request for the object's information. Note that if\n `fast_mode` is set, this is ignored.\n\n gaia_mirror : str or None\n This sets the GAIA mirror to use. This is a key in the\n `services.gaia.GAIA_URLS` dict which defines the URLs to hit for each\n mirror.\n\n xmatchinfo : str or dict\n This is either the xmatch dict produced by the function\n `load_xmatch_external_catalogs` above, or the path to the xmatch info\n pickle file produced by that function.\n\n xmatchradiusarcsec : float\n This is the cross-matching radius to use in arcseconds.\n\n minobservations : int\n The minimum of observations the input object's mag/flux time-series must\n have for this function to plot its light curve and phased light\n curve. If the object has less than this number, no light curves will be\n plotted, but the checkplotdict will still contain all of the other\n information.\n\n sigclip : float or int or sequence of two floats/ints or None\n If a single float or int, a symmetric sigma-clip will be performed using\n the number provided as the sigma-multiplier to cut out from the input\n time-series.\n\n If a list of two ints/floats is provided, the function will perform an\n 'asymmetric' sigma-clip. The first element in this list is the sigma\n value to use for fainter flux/mag values; the second element in this\n list is the sigma value to use for brighter flux/mag values. For\n example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma\n dimmings and greater than 3-sigma brightenings. Here the meaning of\n \"dimming\" and \"brightening\" is set by *physics* (not the magnitude\n system), which is why the `magsarefluxes` kwarg must be correctly set.\n\n If `sigclip` is None, no sigma-clipping will be performed, and the\n time-series (with non-finite elems removed) will be passed through to\n the output.\n\n lcformat : str\n This is the `formatkey` associated with your light curve format, which\n you previously passed in to the `lcproc.register_lcformat`\n function. This will be used to look up how to find and read the light\n curves specified in `basedir` or `use_list_of_filenames`.\n\n lcformatdir : str or None\n If this is provided, gives the path to a directory when you've stored\n your lcformat description JSONs, other than the usual directories lcproc\n knows to search for them in. Use this along with `lcformat` to specify\n an LC format JSON file that's not currently registered with lcproc.\n\n timecols : list of str or None\n The timecol keys to use from the lcdict in generating this checkplot.\n\n magcols : list of str or None\n The magcol keys to use from the lcdict in generating this checkplot.\n\n errcols : list of str or None\n The errcol keys to use from the lcdict in generating this checkplot.\n\n skipdone : bool\n This indicates if this function will skip creating checkplots that\n already exist corresponding to the current `objectid` and `magcol`. If\n `skipdone` is set to True, this will be done.\n\n done_callback : Python function or None\n This is used to provide a function to execute after the checkplot\n pickles are generated. This is useful if you want to stream the results\n of checkplot making to some other process, e.g. directly running an\n ingestion into an LCC-Server collection. The function will always get\n the list of the generated checkplot pickles as its first arg, and all of\n the kwargs for runcp in the kwargs dict. Additional args and kwargs can\n be provided by giving a list in the `done_callbacks_args` kwarg and a\n dict in the `done_callbacks_kwargs` kwarg.\n\n NOTE: the function you pass in here should be pickleable by normal\n Python if you want to use it with the parallel_cp and parallel_cp_lcdir\n functions below.\n\n done_callback_args : tuple or None\n If not None, contains any args to pass into the `done_callback`\n function.\n\n done_callback_kwargs : dict or None\n If not None, contains any kwargs to pass into the `done_callback`\n function.\n\n maxobjects : int\n The maximum number of objects to process in this run.\n\n nworkers : int\n The number of parallel workers that will work on the checkplot\n generation process.\n\n Returns\n -------\n\n dict\n This returns a dict with keys = input period-finding pickles and vals =\n list of the corresponding checkplot pickles produced.\n\n '''\n\n pfpicklelist = sorted(glob.glob(os.path.join(pfpickledir, pfpickleglob)))\n\n LOGINFO('found %s period-finding pickles, running cp...' %\n len(pfpicklelist))\n\n return parallel_cp(pfpicklelist,\n outdir,\n lcbasedir,\n fast_mode=fast_mode,\n lclistpkl=lclistpkl,\n nbrradiusarcsec=nbrradiusarcsec,\n gaia_max_timeout=gaia_max_timeout,\n gaia_mirror=gaia_mirror,\n maxnumneighbors=maxnumneighbors,\n makeneighborlcs=makeneighborlcs,\n xmatchinfo=xmatchinfo,\n xmatchradiusarcsec=xmatchradiusarcsec,\n sigclip=sigclip,\n minobservations=minobservations,\n cprenorm=cprenorm,\n maxobjects=maxobjects,\n lcformat=lcformat,\n lcformatdir=lcformatdir,\n timecols=timecols,\n magcols=magcols,\n errcols=errcols,\n skipdone=skipdone,\n nworkers=nworkers,\n done_callback=done_callback,\n done_callback_args=done_callback_args,\n done_callback_kwargs=done_callback_kwargs)", "response": "This function drives the parallel execution of runcp for a directory of period - finding pickles."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef runpf(lcfile,\n outdir,\n timecols=None,\n magcols=None,\n errcols=None,\n lcformat='hat-sql',\n lcformatdir=None,\n pfmethods=('gls','pdm','mav','win'),\n pfkwargs=({},{},{},{}),\n sigclip=10.0,\n getblssnr=False,\n nworkers=NCPUS,\n minobservations=500,\n excludeprocessed=False,\n raiseonfail=False):\n '''This runs the period-finding for a single LC.\n\n Parameters\n ----------\n\n lcfile : str\n The light curve file to run period-finding on.\n\n outdir : str\n The output directory where the result pickle will go.\n\n timecols : list of str or None\n The timecol keys to use from the lcdict in calculating the features.\n\n magcols : list of str or None\n The magcol keys to use from the lcdict in calculating the features.\n\n errcols : list of str or None\n The errcol keys to use from the lcdict in calculating the features.\n\n lcformat : str\n This is the `formatkey` associated with your light curve format, which\n you previously passed in to the `lcproc.register_lcformat`\n function. This will be used to look up how to find and read the light\n curves specified in `basedir` or `use_list_of_filenames`.\n\n lcformatdir : str or None\n If this is provided, gives the path to a directory when you've stored\n your lcformat description JSONs, other than the usual directories lcproc\n knows to search for them in. Use this along with `lcformat` to specify\n an LC format JSON file that's not currently registered with lcproc.\n\n pfmethods : list of str\n This is a list of period finding methods to run. Each element is a\n string matching the keys of the `PFMETHODS` dict above. By default, this\n runs GLS, PDM, AoVMH, and the spectral window Lomb-Scargle periodogram.\n\n pfkwargs : list of dicts\n This is used to provide any special kwargs as dicts to each\n period-finding method function specified in `pfmethods`.\n\n sigclip : float or int or sequence of two floats/ints or None\n If a single float or int, a symmetric sigma-clip will be performed using\n the number provided as the sigma-multiplier to cut out from the input\n time-series.\n\n If a list of two ints/floats is provided, the function will perform an\n 'asymmetric' sigma-clip. The first element in this list is the sigma\n value to use for fainter flux/mag values; the second element in this\n list is the sigma value to use for brighter flux/mag values. For\n example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma\n dimmings and greater than 3-sigma brightenings. Here the meaning of\n \"dimming\" and \"brightening\" is set by *physics* (not the magnitude\n system), which is why the `magsarefluxes` kwarg must be correctly set.\n\n If `sigclip` is None, no sigma-clipping will be performed, and the\n time-series (with non-finite elems removed) will be passed through to\n the output.\n\n getblssnr : bool\n If this is True and BLS is one of the methods specified in `pfmethods`,\n will also calculate the stats for each best period in the BLS results:\n transit depth, duration, ingress duration, refit period and epoch, and\n the SNR of the transit.\n\n nworkers : int\n The number of parallel period-finding workers to launch.\n\n minobservations : int\n The minimum number of finite LC points required to process a light\n curve.\n\n excludeprocessed : bool\n If this is True, light curves that have existing period-finding result\n pickles in `outdir` will not be processed.\n\n FIXME: currently, this uses a dumb method of excluding already-processed\n files. A smarter way to do this is to (i) generate a SHA512 cachekey\n based on a repr of `{'lcfile', 'timecols', 'magcols', 'errcols',\n 'lcformat', 'pfmethods', 'sigclip', 'getblssnr', 'pfkwargs'}`, (ii) make\n sure all list kwargs in the dict are sorted, (iii) check if the output\n file has the same cachekey in its filename (last 8 chars of cachekey\n should work), so the result was processed in exactly the same way as\n specifed in the input to this function, and can therefore be\n ignored. Will implement this later.\n\n raiseonfail : bool\n If something fails and this is True, will raise an Exception instead of\n returning None at the end.\n\n Returns\n -------\n\n str\n The path to the output period-finding result pickle.\n\n '''\n\n try:\n formatinfo = get_lcformat(lcformat,\n use_lcformat_dir=lcformatdir)\n if formatinfo:\n (dfileglob, readerfunc,\n dtimecols, dmagcols, derrcols,\n magsarefluxes, normfunc) = formatinfo\n else:\n LOGERROR(\"can't figure out the light curve format\")\n return None\n except Exception as e:\n LOGEXCEPTION(\"can't figure out the light curve format\")\n return None\n\n # override the default timecols, magcols, and errcols\n # using the ones provided to the function\n if timecols is None:\n timecols = dtimecols\n if magcols is None:\n magcols = dmagcols\n if errcols is None:\n errcols = derrcols\n\n try:\n\n # get the LC into a dict\n lcdict = readerfunc(lcfile)\n\n # this should handle lists/tuples being returned by readerfunc\n # we assume that the first element is the actual lcdict\n # FIXME: figure out how to not need this assumption\n if ( (isinstance(lcdict, (list, tuple))) and\n (isinstance(lcdict[0], dict)) ):\n lcdict = lcdict[0]\n\n outfile = os.path.join(outdir, 'periodfinding-%s.pkl' %\n squeeze(lcdict['objectid']).replace(' ', '-'))\n\n # if excludeprocessed is True, return the output file if it exists and\n # has a size that is at least 100 kilobytes (this should be enough to\n # contain the minimal results of this function).\n if excludeprocessed:\n\n test_outfile = os.path.exists(outfile)\n test_outfile_gz = os.path.exists(outfile+'.gz')\n\n if (test_outfile and os.stat(outfile).st_size > 102400):\n\n LOGWARNING('periodfinding result for %s already exists at %s, '\n 'skipping because excludeprocessed=True'\n % (lcfile, outfile))\n return outfile\n\n elif (test_outfile_gz and os.stat(outfile+'.gz').st_size > 102400):\n\n LOGWARNING(\n 'gzipped periodfinding result for %s already '\n 'exists at %s, skipping because excludeprocessed=True'\n % (lcfile, outfile+'.gz')\n )\n return outfile+'.gz'\n\n\n # this is the final returndict\n resultdict = {\n 'objectid':lcdict['objectid'],\n 'lcfbasename':os.path.basename(lcfile),\n 'kwargs':{'timecols':timecols,\n 'magcols':magcols,\n 'errcols':errcols,\n 'lcformat':lcformat,\n 'lcformatdir':lcformatdir,\n 'pfmethods':pfmethods,\n 'pfkwargs':pfkwargs,\n 'sigclip':sigclip,\n 'getblssnr':getblssnr}\n }\n\n # normalize using the special function if specified\n if normfunc is not None:\n lcdict = normfunc(lcdict)\n\n for tcol, mcol, ecol in zip(timecols, magcols, errcols):\n\n # dereference the columns and get them from the lcdict\n if '.' in tcol:\n tcolget = tcol.split('.')\n else:\n tcolget = [tcol]\n times = _dict_get(lcdict, tcolget)\n\n if '.' in mcol:\n mcolget = mcol.split('.')\n else:\n mcolget = [mcol]\n mags = _dict_get(lcdict, mcolget)\n\n if '.' in ecol:\n ecolget = ecol.split('.')\n else:\n ecolget = [ecol]\n errs = _dict_get(lcdict, ecolget)\n\n\n # normalize here if not using special normalization\n if normfunc is None:\n ntimes, nmags = normalize_magseries(\n times, mags,\n magsarefluxes=magsarefluxes\n )\n\n times, mags, errs = ntimes, nmags, errs\n\n\n # run each of the requested period-finder functions\n resultdict[mcol] = {}\n\n # check if we have enough non-nan observations to proceed\n finmags = mags[np.isfinite(mags)]\n\n if finmags.size < minobservations:\n\n LOGERROR('not enough non-nan observations for '\n 'this LC. have: %s, required: %s, '\n 'magcol: %s, skipping...' %\n (finmags.size, minobservations, mcol))\n continue\n\n pfmkeys = []\n\n for pfmind, pfm, pfkw in zip(range(len(pfmethods)),\n pfmethods,\n pfkwargs):\n\n pf_func = PFMETHODS[pfm]\n\n # get any optional kwargs for this function\n pf_kwargs = pfkw\n pf_kwargs.update({'verbose':False,\n 'nworkers':nworkers,\n 'magsarefluxes':magsarefluxes,\n 'sigclip':sigclip})\n\n # we'll always prefix things with their index to allow multiple\n # invocations and results from the same period-finder (for\n # different period ranges, for example).\n pfmkey = '%s-%s' % (pfmind, pfm)\n pfmkeys.append(pfmkey)\n\n # run this period-finder and save its results to the output dict\n resultdict[mcol][pfmkey] = pf_func(\n times, mags, errs,\n **pf_kwargs\n )\n\n\n #\n # done with running the period finders\n #\n # append the pfmkeys list to the magcol dict\n resultdict[mcol]['pfmethods'] = pfmkeys\n\n # check if we need to get the SNR from any BLS pfresults\n if 'bls' in pfmethods and getblssnr:\n\n # we need to scan thru the pfmethods to get to any BLS pfresults\n for pfmk in resultdict[mcol]['pfmethods']:\n\n if 'bls' in pfmk:\n\n try:\n\n bls = resultdict[mcol][pfmk]\n\n # calculate the SNR for the BLS as well\n blssnr = bls_snr(bls, times, mags, errs,\n magsarefluxes=magsarefluxes,\n verbose=False)\n\n # add the SNR results to the BLS result dict\n resultdict[mcol][pfmk].update({\n 'snr':blssnr['snr'],\n 'transitdepth':blssnr['transitdepth'],\n 'transitduration':blssnr['transitduration'],\n })\n\n # update the BLS result dict with the refit periods\n # and epochs using the results from bls_snr\n resultdict[mcol][pfmk].update({\n 'nbestperiods':blssnr['period'],\n 'epochs':blssnr['epoch']\n })\n\n except Exception as e:\n\n LOGEXCEPTION('could not calculate BLS SNR for %s' %\n lcfile)\n # add the SNR null results to the BLS result dict\n resultdict[mcol][pfmk].update({\n 'snr':[np.nan,np.nan,np.nan,np.nan,np.nan],\n 'transitdepth':[np.nan,np.nan,np.nan,\n np.nan,np.nan],\n 'transitduration':[np.nan,np.nan,np.nan,\n np.nan,np.nan],\n })\n\n elif 'bls' in pfmethods:\n\n # we need to scan thru the pfmethods to get to any BLS pfresults\n for pfmk in resultdict[mcol]['pfmethods']:\n\n if 'bls' in pfmk:\n\n # add the SNR null results to the BLS result dict\n resultdict[mcol][pfmk].update({\n 'snr':[np.nan,np.nan,np.nan,np.nan,np.nan],\n 'transitdepth':[np.nan,np.nan,np.nan,\n np.nan,np.nan],\n 'transitduration':[np.nan,np.nan,np.nan,\n np.nan,np.nan],\n })\n\n\n # once all mag cols have been processed, write out the pickle\n with open(outfile, 'wb') as outfd:\n pickle.dump(resultdict, outfd, protocol=pickle.HIGHEST_PROTOCOL)\n\n return outfile\n\n except Exception as e:\n\n LOGEXCEPTION('failed to run for %s, because: %s' % (lcfile, e))\n\n if raiseonfail:\n raise\n\n return None", "response": "This function runs the period - finding on a single LC - format JSON file."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef parallel_pf_lcdir(lcdir,\n outdir,\n fileglob=None,\n recursive=True,\n timecols=None,\n magcols=None,\n errcols=None,\n lcformat='hat-sql',\n lcformatdir=None,\n pfmethods=('gls','pdm','mav','win'),\n pfkwargs=({},{},{},{}),\n sigclip=10.0,\n getblssnr=False,\n nperiodworkers=NCPUS,\n ncontrolworkers=1,\n liststartindex=None,\n listmaxobjects=None,\n minobservations=500,\n excludeprocessed=True):\n '''This runs parallel light curve period finding for directory of LCs.\n\n Parameters\n ----------\n\n lcdir : str\n The directory containing the LCs to process.\n\n outdir : str\n The directory where the resulting period-finding pickles will go.\n\n fileglob : str or None\n The UNIX file glob to use to search for LCs in `lcdir`. If None, the\n default file glob associated with the registered LC format will be used\n instead.\n\n recursive : bool\n If True, will search recursively in `lcdir` for light curves to process.\n\n timecols : list of str or None\n The timecol keys to use from the lcdict in calculating the features.\n\n magcols : list of str or None\n The magcol keys to use from the lcdict in calculating the features.\n\n errcols : list of str or None\n The errcol keys to use from the lcdict in calculating the features.\n\n lcformat : str\n This is the `formatkey` associated with your light curve format, which\n you previously passed in to the `lcproc.register_lcformat`\n function. This will be used to look up how to find and read the light\n curves specified in `basedir` or `use_list_of_filenames`.\n\n lcformatdir : str or None\n If this is provided, gives the path to a directory when you've stored\n your lcformat description JSONs, other than the usual directories lcproc\n knows to search for them in. Use this along with `lcformat` to specify\n an LC format JSON file that's not currently registered with lcproc.\n\n pfmethods : list of str\n This is a list of period finding methods to run. Each element is a\n string matching the keys of the `PFMETHODS` dict above. By default, this\n runs GLS, PDM, AoVMH, and the spectral window Lomb-Scargle periodogram.\n\n pfkwargs : list of dicts\n This is used to provide any special kwargs as dicts to each\n period-finding method function specified in `pfmethods`.\n\n sigclip : float or int or sequence of two floats/ints or None\n If a single float or int, a symmetric sigma-clip will be performed using\n the number provided as the sigma-multiplier to cut out from the input\n time-series.\n\n If a list of two ints/floats is provided, the function will perform an\n 'asymmetric' sigma-clip. The first element in this list is the sigma\n value to use for fainter flux/mag values; the second element in this\n list is the sigma value to use for brighter flux/mag values. For\n example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma\n dimmings and greater than 3-sigma brightenings. Here the meaning of\n \"dimming\" and \"brightening\" is set by *physics* (not the magnitude\n system), which is why the `magsarefluxes` kwarg must be correctly set.\n\n If `sigclip` is None, no sigma-clipping will be performed, and the\n time-series (with non-finite elems removed) will be passed through to\n the output.\n\n getblssnr : bool\n If this is True and BLS is one of the methods specified in `pfmethods`,\n will also calculate the stats for each best period in the BLS results:\n transit depth, duration, ingress duration, refit period and epoch, and\n the SNR of the transit.\n\n nperiodworkers : int\n The number of parallel period-finding workers to launch per object task.\n\n ncontrolworkers : int\n The number of controlling processes to launch. This effectively sets how\n many objects from `lclist` will be processed in parallel.\n\n liststartindex : int or None\n This sets the index from where to start in `lclist`.\n\n listmaxobjects : int or None\n This sets the maximum number of objects in `lclist` to run\n period-finding for in this invocation. Together with `liststartindex`,\n `listmaxobjects` can be used to distribute processing over several\n independent machines if the number of light curves is very large.\n\n minobservations : int\n The minimum number of finite LC points required to process a light\n curve.\n\n excludeprocessed : bool\n If this is True, light curves that have existing period-finding result\n pickles in `outdir` will not be processed.\n\n FIXME: currently, this uses a dumb method of excluding already-processed\n files. A smarter way to do this is to (i) generate a SHA512 cachekey\n based on a repr of `{'lcfile', 'timecols', 'magcols', 'errcols',\n 'lcformat', 'pfmethods', 'sigclip', 'getblssnr', 'pfkwargs'}`, (ii) make\n sure all list kwargs in the dict are sorted, (iii) check if the output\n file has the same cachekey in its filename (last 8 chars of cachekey\n should work), so the result was processed in exactly the same way as\n specifed in the input to this function, and can therefore be\n ignored. Will implement this later.\n\n Returns\n -------\n\n list of str\n A list of the period-finding pickles created for all of input LCs\n processed.\n\n '''\n\n try:\n formatinfo = get_lcformat(lcformat,\n use_lcformat_dir=lcformatdir)\n if formatinfo:\n (dfileglob, readerfunc,\n dtimecols, dmagcols, derrcols,\n magsarefluxes, normfunc) = formatinfo\n else:\n LOGERROR(\"can't figure out the light curve format\")\n return None\n except Exception as e:\n LOGEXCEPTION(\"can't figure out the light curve format\")\n return None\n\n if not fileglob:\n fileglob = dfileglob\n\n # now find the files\n LOGINFO('searching for %s light curves in %s ...' % (lcformat, lcdir))\n\n if recursive is False:\n matching = glob.glob(os.path.join(lcdir, fileglob))\n\n else:\n # use recursive glob for Python 3.5+\n if sys.version_info[:2] > (3,4):\n\n matching = glob.glob(os.path.join(lcdir,\n '**',\n fileglob),recursive=True)\n\n # otherwise, use os.walk and glob\n else:\n\n # use os.walk to go through the directories\n walker = os.walk(lcdir)\n matching = []\n\n for root, dirs, _files in walker:\n for sdir in dirs:\n searchpath = os.path.join(root,\n sdir,\n fileglob)\n foundfiles = glob.glob(searchpath)\n\n if foundfiles:\n matching.extend(foundfiles)\n\n\n # now that we have all the files, process them\n if matching and len(matching) > 0:\n\n # this helps us process things in deterministic order when we distribute\n # processing over several machines\n matching = sorted(matching)\n\n LOGINFO('found %s light curves, running pf...' % len(matching))\n\n return parallel_pf(matching,\n outdir,\n timecols=timecols,\n magcols=magcols,\n errcols=errcols,\n lcformat=lcformat,\n lcformatdir=lcformatdir,\n pfmethods=pfmethods,\n pfkwargs=pfkwargs,\n getblssnr=getblssnr,\n sigclip=sigclip,\n nperiodworkers=nperiodworkers,\n ncontrolworkers=ncontrolworkers,\n liststartindex=liststartindex,\n listmaxobjects=listmaxobjects,\n minobservations=minobservations,\n excludeprocessed=excludeprocessed)\n\n else:\n\n LOGERROR('no light curve files in %s format found in %s' % (lcformat,\n lcdir))\n return None", "response": "This function runs a parallel light curve period finding for a directory of LCs."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef collect_nonperiodic_features(\n featuresdir,\n magcol,\n outfile,\n pklglob='varfeatures-*.pkl',\n featurestouse=NONPERIODIC_FEATURES_TO_COLLECT,\n maxobjects=None,\n labeldict=None,\n labeltype='binary',\n):\n '''This collects variability features into arrays for use with the classifer.\n\n Parameters\n ----------\n\n featuresdir : str\n This is the directory where all the varfeatures pickles are. Use\n `pklglob` to specify the glob to search for. The `varfeatures` pickles\n contain objectids, a light curve magcol, and features as dict\n key-vals. The :py:mod:`astrobase.lcproc.lcvfeatures` module can be used\n to produce these.\n\n magcol : str\n This is the key in each varfeatures pickle corresponding to the magcol\n of the light curve the variability features were extracted from.\n\n outfile : str\n This is the filename of the output pickle that will be written\n containing a dict of all the features extracted into np.arrays.\n\n pklglob : str\n This is the UNIX file glob to use to search for varfeatures pickle files\n in `featuresdir`.\n\n featurestouse : list of str\n Each varfeatures pickle can contain any combination of non-periodic,\n stellar, and periodic features; these must have the same names as\n elements in the list of strings provided in `featurestouse`. This tries\n to get all the features listed in NONPERIODIC_FEATURES_TO_COLLECT by\n default. If `featurestouse` is provided as a list, gets only the\n features listed in this kwarg instead.\n\n maxobjects : int or None\n The controls how many pickles from the featuresdir to process. If None,\n will process all varfeatures pickles.\n\n labeldict : dict or None\n If this is provided, it must be a dict with the following key:val list::\n\n '':