def region_to_json(region):
    r_hd = dict()
    r_pd = dict()

    rename_map = dict(
        ipc_df="famine_risk",
        weather_df="Temperature",
        conflict_df="{} - Fatalities due to Conflict".format(region))

    region_data = store().per_region_data[region]
    region_pred = store().per_region_pred_data[region]

    for (data, res) in [(region_data, r_hd), (region_pred, r_pd)]:
        for (k, v) in data.items():
            if k[0] == "_":
                res[k] = dfcs.JsonStr(dfcs.to_json_string(v))
            elif k == "food_df" or k == "ffood_df":
                f_df = v
                for item in data["_food_item_names"] if k[
                        0:2] == "fo" else data["_ffood_item_names"]:
                    item_df = f_df[f_df.Item_Name == item]
                    res[item] = dfcs.JsonStr(dfcs.to_json_string(item_df))
            elif k in rename_map:
                res[rename_map[k]] = dfcs.JsonStr(dfcs.to_json_string(v))
            else:
                res[k] = dfcs.JsonStr(dfcs.to_json_string(v))

    return (r_hd, r_pd)
Exemple #2
0
    def store(self, fname, version=None):
        """Store the lexicon.

        Args:
            fname: path where to store
            version: if given, just store the specific version of the lexicon.
        """
        if version is None:
            for version in self.L:
                outfile = store(fname + "_" + version + ".txt")
                for k, v in self.L[version]:
                    outfile.write("{} {}\n".format(k, v))
                outfile.close()
        else:
            outfile = store(fname)
            for k, v in self.L[version]:
                outfile.write("{} {}\n".format(k, v))
            outfile.close()
Exemple #3
0
    def store(self, fname):
        """Stores the predictions in a text file.

        Args:
            fname: path where to store the predictions.
        """
        outfile = store(fname)
        for k, v in self.predictions:
            outfile.write("{} {}\n".format(k, v))
        outfile.close()
def predict_region(region, changes):
    data = store().per_region_pred_data[region].copy()
    if len(changes) != 0:
        for (k, v) in data.items():
            data[k] = deepcopy(v)
        data = transform_data(data, region)

        for change in changes:
            print("Changing {} @ {},{} to {}".format(*change.values()))
            df = data[change["source"]]
            df2 = df[df.Year == change["year"]]
            df2 = df2[df2.Month == change["month"]]
            df[change["column"]][df2.index[0]] = change["value"]

        data = transform_data_back(data, region)

    res = famine_prediction.predict_famine(
        store().per_region_model[region].fit.fit,
        famine_processing.calculate_datasets([region], {region: data})[region])

    return res
Exemple #5
0
    def store(self, fname):
        """Store the embedding space

        Args:
            fname: path to the file
        """
        outfile = store(fname)
        n, dim = self.X.shape
        outfile.write("{} {}\n".format(n, dim))
        for i in range(n):
            outfile.write(self.W[i])
            for k in range(dim):
                outfile.write(" {}".format(self.X[i, k]))
            outfile.write("\n")
        outfile.close()
    def get(self, region):
        if region not in REGIONS:
            return dict(success=False, error="Region does not exist")

        if region not in store().FITTED_REGIONS:
            return dict(success=False,
                        error="Region not fitted: not enough data")

        historical_data, predicted_data = region_to_json(region)

        response = dfcs.to_json_string(
            dict(success=True,
                 historical_data=historical_data,
                 predicted_data=predicted_data))
        return Response(response, mimetype="application/json")
    def get(self):

        region_json = dict()

        for region in REGIONS:
            if region in store().FITTED_REGIONS:
                region_json[region] = dict(fitted=True,
                                           data=predict_region(region, []))
            else:
                region_json[region] = dict(fitted=False)

        response = dict(success=True, data=region_json)

        return Response(dfcs.to_json_string(response),
                        mimetype="application/json")
    def get(self, region):

        if region not in REGIONS:
            return dict(success=False, error="Region does not exist")

        if region not in store().FITTED_REGIONS:
            return dict(success=False,
                        error="Region not fitted: not enough data")

        response = dict(success=True,
                        region=region,
                        data=predict_region(region, []))

        return Response(dfcs.to_json_string(response),
                        mimetype="application/json")
    def get(self):

        region_json = dict()

        for region in REGIONS:
            if region in store().FITTED_REGIONS:
                hd, pd = region_to_json(region)
                region_json[region] = dict(fitted=True,
                                           historical_data=hd,
                                           predicted_data=pd)
            else:
                region_json[region] = dict(fitted=False)

        response = dfcs.to_json_string(dict(success=True, regions=region_json))

        return Response(response, mimetype="application/json")
    def post(self, region):

        if region not in REGIONS:
            return dict(success=False, error="Region does not exist")

        if region not in store().FITTED_REGIONS:
            return dict(success=False,
                        error="Region not fitted: not enough data")

        args = self.reqparse.parse_args()
        changes = args["changes"]

        print(changes)

        response = dict(success=True,
                        region=region,
                        data=predict_region(region, changes))

        return Response(dfcs.to_json_string(response),
                        mimetype="application/json")
 def get(self):
     return dict(all_regions=REGIONS, fitted_regions=store().FITTED_REGIONS)