StateEconomics

Source code in wt_ml/dataset/economics/fred_state_economic_data.py
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
class StateEconomics:
    def __init__(self, fred: Fred, start_date: str = "2016-12-31", max_retries: int = 6):
        self.fred = fred
        self.start_date = start_date
        self.end_date = datetime.datetime.today().strftime("%Y-%m-%d")
        self.max_retries = max_retries
        self.date_idx = pd.date_range(self.start_date, self.end_date, name="date")[1:]
        self.missing = {}

    @staticmethod
    def remove_refs(title: str, k: str):
        return title.replace(f" in {k}", "").replace(f" for {k}", "").replace(f" from {k}", "").strip()

    def get_series_df_with_backoff(self, series_name: str) -> pd.DataFrame:
        try:
            return make_request_with_backoff(
                self.fred.get_series_df, series_id=series_name, observation_start=self.start_date
            )
        except TypeError as e:
            logger.exception(e, f"Failed to load data for {series_name}.")
            return pd.DataFrame()

    def get_series_by_name_with_backoff(self, state: str, name: str, state_series: dict) -> pd.Series | pd.DataFrame:
        possible = [
            series
            for series in state_series[state]["seriess"]
            if StateEconomics.remove_refs(series["title"], state) == name.strip()
        ]
        # Exclude 3 months smoothened and yearly aggregated data for employee type data
        possible = [
            possible_series
            for possible_series in possible
            if "3-Month Average" not in possible_series["units"]
            and (not possible_series["title"].startswith("All Employees") or possible_series["frequency_short"] != "A")
        ]
        if len(possible) == 0:
            series_id = find_series_by_keyword(state, name, 90)
            if series_id is None:
                return pd.Series(name=(name, state), dtype="object")
        else:
            series_id = possible[0]["id"]
        return self.get_required_dates_with_backoff(series_id, (name, state))

    def get_required_dates_with_backoff(self, series_name: str, title: tuple[str] = None) -> pd.DataFrame:
        df = self.get_series_df_with_backoff(series_name)
        if len(df) == 0:
            logger.warning(f"Failed to load new data for {series_name}.")
            # when we have no new data after `observation_start`
            return pd.Series(name=title, dtype="object")

        df["date"] = pd.to_datetime(df["date"])
        series = (
            df.loc[df["date"] > self.start_date]
            .drop(columns=["realtime_start", "realtime_end"])
            .set_index("date")
            .rename(columns={"value": title or series_name})[title or series_name]
        )
        return series

    def get_combined_df(self, name: str, state_series: dict) -> pd.DataFrame:
        """Combine state level data for each series"""
        state_series_info_list = [
            (find_series_by_keyword(state, name, 90), state, name)
            for state in tqdm(state_series.keys(), leave=False, dynamic_ncols=True, unit="state")
        ]
        dfs = {
            state: self.get_required_dates_with_backoff(series_id, (name, state))
            for series_id, state, name in tqdm(state_series_info_list, leave=False, dynamic_ncols=True, unit="state")
        }
        valid_states = [key for key, value in dfs.items() if len(value) > 0]
        if len(valid_states) == 0:
            logger.error(f"No state had data for series {name}.")
            return None
        first_state = valid_states[0]
        df = dfs[first_state].reindex(self.date_idx).to_frame()
        for state, series in dfs.items():
            if state != first_state:
                if len(series) == 0:
                    series = dfs[first_state].copy()
                    series[:] = 0
                    series.name = (series.name[0], state)
                df = df.join(series.reindex(self.date_idx), on="date", how="outer")
        missing = [name for name, series in dfs.items() if len(series) == 0]
        if len(missing) > 0:
            self.missing[name] = missing
        df.columns.names = ["signal", "state"]
        # NOTE: there are certain values which are returned as "." in fred api. replacing them to 0.
        return df.replace(".", "0").astype("float")

    def process_desired_series(self, desired_series: tuple[str], state_series: dict) -> pd.DataFrame:
        downloaded_df = self.get_combined_df(desired_series, state_series)
        if downloaded_df is None:
            return None
        desired_df = interpolate_and_fill(downloaded_df.reindex(self.date_idx))
        # Resample to weekly and swap axis order
        result_df = (
            desired_df.resample("W-SAT", closed="right", label="right", convention="end")
            .mean()
            .reorder_levels(["state", "signal"], axis=1)
            .sort_index(axis=1)
            .fillna(0)
            .stack("state")
        )
        return result_df.swaplevel("state", "date")

    def save_to_json(
        self,
        desired_series: tuple[str],
        state_series: dict,
        historical_fred_data: pd.DataFrame,
        output_file: Path = OUTPUT_FILE,
    ):
        count = 1
        for signal in tqdm(desired_series, dynamic_ncols=True, unit="signal", leave=False):
            if count == 1:
                result_df = self.process_desired_series(signal, state_series)
                if result_df is None:
                    continue
            else:
                signal_df = self.process_desired_series(signal, state_series)
                if signal_df is None:
                    continue
                result_df = result_df.join(signal_df, how="outer")
            count += 1

        if result_df is not None:
            # Append to historical data only when incremental data is avaiable
            result_df = result_df.sort_index().sort_index(axis=1)
            fred_data = pd.concat([historical_fred_data, result_df], axis=0)
            fred_data.columns.names = ["signals"]
            fred_data_processed = impute_missing_data(fred_data)
            fred_data_processed.to_json(output_file, orient="split", date_format="iso", date_unit="ms")

        if len(self.missing) > 0:
            for name, missing in self.missing.items():
                logger.warning(
                    f"Missing data for series '{name}' for the following states '{missing}."
                    " Used all zeros for their values."
                )

get_combined_df(name, state_series)

Combine state level data for each series

Source code in wt_ml/dataset/economics/fred_state_economic_data.py
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
def get_combined_df(self, name: str, state_series: dict) -> pd.DataFrame:
    """Combine state level data for each series"""
    state_series_info_list = [
        (find_series_by_keyword(state, name, 90), state, name)
        for state in tqdm(state_series.keys(), leave=False, dynamic_ncols=True, unit="state")
    ]
    dfs = {
        state: self.get_required_dates_with_backoff(series_id, (name, state))
        for series_id, state, name in tqdm(state_series_info_list, leave=False, dynamic_ncols=True, unit="state")
    }
    valid_states = [key for key, value in dfs.items() if len(value) > 0]
    if len(valid_states) == 0:
        logger.error(f"No state had data for series {name}.")
        return None
    first_state = valid_states[0]
    df = dfs[first_state].reindex(self.date_idx).to_frame()
    for state, series in dfs.items():
        if state != first_state:
            if len(series) == 0:
                series = dfs[first_state].copy()
                series[:] = 0
                series.name = (series.name[0], state)
            df = df.join(series.reindex(self.date_idx), on="date", how="outer")
    missing = [name for name, series in dfs.items() if len(series) == 0]
    if len(missing) > 0:
        self.missing[name] = missing
    df.columns.names = ["signal", "state"]
    # NOTE: there are certain values which are returned as "." in fred api. replacing them to 0.
    return df.replace(".", "0").astype("float")