コード例 #1
0
 def unit(self):
     try:
         return self._unit
     except NameError or AttributeError:
         print_int("Unit not set, setting to empty", self.kwargs)
         self._unit = ""
         return self._unit
コード例 #2
0
    def _readBackgroundParameter(self):
        for i in range(0, len(self._backgroundPriors.getData())):
            try:
                self._backgroundParameter.append(
                    BackgroundParameterFileModel(self._names[i],
                                                 self._units[i], self.kwargs,
                                                 self._runID, i))
            except (IOError, ValueError) as e:
                print_int(
                    "Failed to find backgroundparameter for " + self._names[i],
                    self.kwargs)
            try:
                self._marginalDistributions.append(
                    BackgroundMarginalDistrFileModel(self._names[i],
                                                     self._units[i],
                                                     self.kwargs, self._runID,
                                                     i))
                if self._summary is not None:
                    self._marginalDistributions[i]._backgroundData = np.vstack(
                        (self.summary.getRawData()[strSummaryMedian][i],
                         self.summary.getRawData()[strSummaryLowCredLim][i],
                         self.summary.getRawData()[strSummaryUpCredLim][i]))

                    if self._backgroundParameter[i].getData() is None:
                        self._psdOnlyFlag = True
            except Exception as e:
                pass
コード例 #3
0
 def _readFile(self, file):
     try:
         data = np.loadtxt(file).T
     except FileNotFoundError as e:
         print_int("Failed to open File " + file, kwargs)
         print_int(e, kwargs)
         raise IOError("Failed to open file. " + file)
     return data
コード例 #4
0
 def _getValueFromDict(self, dict, key):
     if key is None:
         return dict
     else:
         try:
             return dict[key]
         except:
             print_int("No value for key '" + key + "',returning full dict",
                       kwargs)
             return dict
コード例 #5
0
    def _logRatio(self, runID, line, counter, r, runCounter):
        match = r.findall(line)
        if len(match) > 0 and counter >= 5:
            for it, ratio in match:
                print_int(f"{runID} model: run {runCounter}  --> {it},{ratio}",
                          self.kwargs)

            counter = 0
        elif len(match) == 0 and counter >= 5:
            print_int(f"{runID} model: run {runCounter} --> {line}",
                      self.kwargs)
            counter = 0
        return counter
コード例 #6
0
    def _checkIfAllFilesExist(self, runID, oldStatus):
        content = os.listdir(self.check_paths[runID])
        if "background_evidenceInformation.txt" not in content:
            print_int(
                f"Cant fine evidence in {self.check_paths[runID]}. Repeat!",
                self.kwargs)
            return strDiamondsStatusAssertion
        elif "background_parameterSummary.txt" not in content:
            print_int(
                f"Cant fine summary in {self.check_paths[runID]}. Repeat!",
                self.kwargs)
            return strDiamondsStatusAssertion

        return oldStatus
コード例 #7
0
def create_files(data: np.ndarray, nyq_f: float, priors: List[List[float]],
                 kwargs: Dict):
    """
    Creates all files for a DIAMONDS run
    :param data: Lightcurve dataset
    :param nyq_f: Nyquist frequency
    :param priors: List of priors
    :param kwargs: Run configuratio
    """
    print_int(f"Path: {full_result_path(kwargs)}", kwargs)
    create_folder(full_result_path(kwargs), kwargs)
    create_data(compute_periodogram(data, kwargs), kwargs)
    create_priors(np.array(priors), full_result_path(kwargs))
    create_nsmc_configuring_parameters(full_result_path(kwargs))
    create_xmeans_configuring_parameters(full_result_path(kwargs))
    create_nyquist_frequency(nyq_f, full_result_path(kwargs))
コード例 #8
0
    def _getValueFromDict(self, dict: dict, key: str = None):
        '''
        This is a helper method, which returns a single item from a list if the item exists or the whole dict otherwhise
        :param dict: The list to search in
        :param key: The key. Optional
        :return: The appropriate Value(s)
        '''
        if key is None:
            return dict
        else:
            for i in dict:
                if i.name == key:
                    return i

            print_int("Found no object for '" + key + "'", self.kwargs)
            print_int("Returning full list", self.kwargs)
            return dict
コード例 #9
0
    def getData(self,key=None):
        '''
        Returns the evidence map. Reads it if necessary
        :param key: One of the three used to setup the dict -> see strings.py
        :type key: string
        :return: The dictionary or single value of the gathered data
        :rtype: dict{string:float}/float
        '''
        if any(self._evidence) is False:
            self._readData()

        if key is None:
            return self._evidence
        else:
            try:
                return self._evidence[key]
            except:
                print_int("No value for key '"+key+"', returning full dict",self.kwargs)
                return self._evidence
コード例 #10
0
    def _getFullPath(self,path):
        '''
        This method will create an absolute path if the path it inputs wasn't that already
        :param path: The path you want to have the absolute of
        :type path: str
        :return: Absolute path
        :rtype: str
        '''
        if path[0] not in ["~", "/", "\\"]:
            print_int("Setting priors to full path",self.kwargs)
            print_int("Prepending" + ROOT_PATH,self.kwargs)
            path = ROOT_PATH + "/" + path
            print_int("New path: "+path,self.kwargs)
        else:
            print_int("Path is already absolute path",self.kwargs)

        return path
コード例 #11
0
 def _readData(self, kwargs: Dict):
     '''
     Internal reader function. Reads the file according to the settings
     '''
     if general_background_data_path in kwargs.keys():
         dataFolder = kwargs[general_background_data_path]
     else:
         dataFolder = kwargs[internal_path] + "/Background/data/"
     file = dataFolder + kwargs[analysis_folder_prefix] + self.kicID + ".txt"
     try:
         self._psd = np.loadtxt(file)
     except FileNotFoundError as e:
         print_int("Cannot read PSD file, setting PSD to none", kwargs)
         print_int("File is " + file, kwargs)
         print_int(e, kwargs)
         raise IOError("Cannot read PSD file, setting PSD to none")
コード例 #12
0
    def _runCmd(self, runID, cmd):
        for runCounter in range(1, 11):
            self.run_count[runID] = runCounter
            print_int(f"Starting {runID} model: run {runCounter}", self.kwargs)
            with cd(self.binaryPath):
                self.status[runID] = strDiStatRunning
                p = self._runBinary(cmd)
                logCounter = 0
                r = re.compile(r"(Nit:\s\d+).+(Ratio: [\w\d\.\+]+)")
                time.sleep(5)
                total = ""
                while p.poll() is None:
                    line = p.stderr.readline().decode("utf-8")
                    total += line
                    logCounter = self._logRatio(runID, line, logCounter, r,
                                                runCounter)
                    self.status[runID] = self._checkDiamondsStdOut(
                        self.status[runID], line)
                    logCounter += 1
                line = p.stderr.read().decode("utf-8")
                total += line
                self._logRatio(runID, line, 10, r, runCounter)

                self.status[runID] = self._checkDiamondsStdOut(
                    self.status[runID], line)
                time.sleep(1)
                self.status[runID] = self._checkIfAllFilesExist(
                    runID, self.status[runID])

                if self.status[runID] == strDiStatRunning:
                    self.status[runID] = strDiamondsStatusGood
                    print_int(f"{runID} model: Finished!", self.kwargs)
                    return

                else:
                    print_int(
                        f"{runID} model: Run {runCounter} --> Repeating run, due to failure due to {self.status[runID]}",
                        self.kwargs)

        raise ValueError(
            f"{runID} model: FAILED! Tried 10 runs, failed to find result",
            self.kwargs)
コード例 #13
0
def run_star(kwargs: Dict):
    """
    Runs a full analysis for a given kwargs file.
    :param kwargs: Run conf
    """
    t1 = time.time()
    if analysis_folder_prefix in kwargs.keys():
        prefix = kwargs[analysis_folder_prefix]
    else:
        prefix = "KIC"

    path = f"{kwargs[general_analysis_result_path]}{prefix}_{kwargs[general_kic]}/"
    if os.path.exists(path) and ((internal_force_run in kwargs.keys()
                                  and not kwargs[internal_force_run])
                                 or internal_force_run not in kwargs.keys()):
        with cd(path):
            if os.path.exists(
                    "results.json") and not os.path.exists("errors.txt"):
                with open('results.json', 'r') as f:
                    old_res = json.load(f)
                kwargs["time"] = float(old_res['Runtime'])
                print_int("Done", kwargs)
                return

    try:
        shutil.rmtree(path)
    except FileNotFoundError:
        pass
    try:
        makedirs(path)
    except FileExistsError:
        pass

    with cd(path):
        try:
            #print_int("Starting run", kwargs)
            # load and refine data
            data = load_file(kwargs)
            with open("conf.json", 'w') as f:
                if internal_literature_value in kwargs.keys():
                    kwargs[
                        internal_literature_value] = f"{kwargs[internal_literature_value]}"
                if internal_delta_nu in kwargs.keys():
                    kwargs[internal_delta_nu] = f"{kwargs[internal_delta_nu]}"

                json.dump(kwargs, f, indent=4)

                if internal_literature_value in kwargs.keys():
                    kwargs[internal_literature_value] = ufloat_fromstr(
                        kwargs[internal_literature_value])
                if internal_delta_nu in kwargs.keys():
                    kwargs[internal_delta_nu] = ufloat_fromstr(
                        kwargs[internal_delta_nu])

            data, kwargs = refine_data(data, kwargs)

            np.save("lc", data)

            # compute nu_max
            print_int("Computing nu_max", kwargs)
            """
            sigma_ampl = calculate_flicker_amplitude(data)
            f_ampl = flicker_amplitude_to_frequency(sigma_ampl)
            nu_max, f_list, f_fliper = compute_nu_max(data, f_ampl, kwargs)
            """
            #            if internal_literature_value in kwargs:
            #                nu_max = kwargs[internal_literature_value].nominal_value
            #            else:
            if analysis_nu_max_outer_guess in kwargs.keys():
                nu_max = kwargs[analysis_nu_max_outer_guess]
            else:
                nu_max = compute_fliper_exact(data, kwargs)
            #nu_max = kwargs[internal_literature_value].nominal_value

            f_fliper = nu_max
            f_list = []

            if internal_literature_value in kwargs.keys():
                print_int(
                    f"Nu_max guess: {'%.2f' % nu_max}, fliper: {f_fliper} literature: {kwargs[internal_literature_value]}",
                    kwargs)
            else:
                print_int(
                    f"Nu max guess: {'%.2f' % nu_max}, fliper: {f_fliper}",
                    kwargs)

            # create files for diamonds and run
            prior, params = priors(nu_max, data, kwargs)
            print_int(f"Priors: {prior}", kwargs)

            cnt = 1
            while cnt <= 3:
                create_files(data, nyqFreq(data), prior, kwargs)
                proc = BackgroundProcess(kwargs)
                if general_run_diamonds in kwargs.keys():
                    if kwargs[general_run_diamonds]:
                        proc.run()
                else:
                    proc.run()

                if general_check_bayes_run in kwargs.keys():
                    if is_bayes_factor_good(kwargs):
                        break
                else:
                    break

                cnt += 1

            kwargs['Number of DIAMONDS runs'] = cnt
            print_int("Saving results", kwargs)
            # save results

            save_results(prior, data, nu_max, params, proc, f_list, f_fliper,
                         t1, kwargs)
            delta_t = time.time() - t1
            kwargs["time"] = delta_t
            print_int("Done", kwargs)

        except (EvidenceFileNotFound, ResultFileNotFound,
                InputFileNotFound) as e:
            error = f"{e.__class__.__name__} : {str(e)}\n"
            print_int("Done", kwargs)
            trace = traceback.format_exc()
            with open("errors.txt", "w") as f:
                f.write(error)
                f.write(trace)
        except Exception as e:
            print_int("Done", kwargs)
            error = f"{e.__class__.__name__} : {str(e)}\n"
            trace = traceback.format_exc()
            with open("errors.txt", "w") as f:
                f.write(error)
                f.write(trace)
コード例 #14
0
def compute_nu_max(
        data: np.ndarray, f_flicker: float,
        kwargs: Dict) -> Tuple[float, Dict[str, float], Union[float, None]]:
    """
    Performs the full procedure introduced by Kallinger (2016)
    :param data: Full dataset from the lightcurve
    :param f_flicker: flicker frequency from the flicker amplitude. In uHz
    :return: guess for nu_max. In uHz
    """

    f_fliper = compute_fliper_nu_max(compute_periodogram(data, kwargs), kwargs)
    if f_fliper != {}:
        if "Fliper exact" in f_fliper.keys():
            f = f_fliper["Fliper exact"]
        else:
            f = f_fliper["Fliper rough"]

        return f, {}, f_fliper

    f_list = []

    f_list.append((f_flicker, rf"F_flicker_{'%.2f' % f_flicker}$\mu Hz$"))
    plot_peridogramm_from_timeseries(data, kwargs, True, f_list)
    print_int(f"Flicker frequency {'%.2f' % f_flicker}", kwargs)

    tau = single_step_procedure(data, (f_to_t(f_flicker) / 60) + 5, kwargs)
    f = f_from_tau(tau)
    print_int(f"1. frequency {'%.2f' % f}", kwargs)

    f_list.append((f, rf"F_filter_0_{'%.2f' % f}$\mu Hz$"))
    plot_peridogramm_from_timeseries(data, kwargs, True, f_list)

    # for frequencies below 70 the first guess seems good enough
    n = 1

    # repeat process n-times
    for i in range(0, n):
        try:
            f_guess = np.amax([
                f_list[-2][0], f_list[-1][0]
            ]) - (1 / 3) * np.abs(f_list[-1][0] - f_list[-2][0])
            tau = single_step_procedure(data, (f_to_t(f_guess) / 60), kwargs)
        except ValueError:
            break
        f_new = f_from_tau(tau)
        f = f_new

        print_int(f"{i + 2}. frequency {'%.2f' % f}", kwargs)

        f_list.append((f, rf"F_filter_{i + 1}_{'%.2f' % f}$\mu Hz$"))
        plot_peridogramm_from_timeseries(data, kwargs, True, f_list)

    print_int(f"Nu_max: {'%.2f' % f}", kwargs)

    f_list_ret = {}
    for val, name in f_list:
        f_list_ret[name] = val

    f_fliper = compute_fliper_nu_max(compute_periodogram(data, kwargs), kwargs)
    if f_fliper != {}:
        if "Fliper exact" in f_fliper.keys():
            f = f_fliper["Fliper exact"]
        else:
            f = f_fliper["Fliper rough"]

    return f, f_list_ret, f_fliper
コード例 #15
0
    def _checkDiamondsStdOut(self, oldStatus, text):
        """
        Checks the stdout of DIAMONDS and returns the status, that DIAMONDS has.
        :param text: stderr/stdout of diamonds that is analyzed.
        :type text: str
        :return: Statusflag
        :rtype: str
        """
        status = oldStatus
        if strDiamondsErrBetterLikelihood in str(text):
            print_int(
                "Diamonds cannot find point with better likelihood. Repeat!",
                self.kwargs)
            status = strDiamondsStatusLikelihood

        elif strDiamondsErrCovarianceFailed in str(text):
            print_int("Diamonds cannot decompose covariance. Repeat!",
                      self.kwargs)
            status = strDiamondsStatusCovariance

        elif strDiamondsErrAssertionFailed in str(text):
            print_int("Diamonds assertion failed. Repeat!", self.kwargs)
            status = strDiamondsStatusAssertion

        elif strDiamondsErrMatrixCompositionFailed in str(text):
            print_int("Diamonds matrix decomposition failed. Repeat!",
                      self.kwargs)
            status = strDiamondsStatusMatrixDecomp
        elif strDiamondsErrCoreDumped in str(text):
            print_int("Diamonds matrix decomposition failed. Repeat!",
                      self.kwargs)
            status = strDiamondsStatusMatrixDecomp
        elif strDiamondsErrAborted in str(text):
            print_int("Diamonds matrix decomposition failed. Repeat!",
                      self.kwargs)
            status = strDiamondsStatusMatrixDecomp
        elif strDiamondsErrQuitting in str(text):
            print_int("Diamonds matrix decomposition failed. Repeat!",
                      self.kwargs)
            status = strDiamondsStatusMatrixDecomp
        elif strDiamondsErrSegmentation in str(text):
            print_int("Diamonds matrix decomposition failed. Repeat!",
                      self.kwargs)
            status = strDiamondsStatusMatrixDecomp
        return status