<jupyter_start><jupyter_text>Using pandas_read_xml<jupyter_code>import pandas_read_xml as pdx
import pandas as pd
import matplotlib.pyplot as plt # data visualization
import seaborn as sns # data visualization

sns.set(style="whitegrid", font_scale=1.4)
%matplotlib inline
df_ev = pdx.read_xml('main_events_pruned.xml', ['seiscomp', 'EventParameters', 'event'])
df_or = pdx.read_xml('main_events_pruned.xml', ['seiscomp', 'EventParameters', 'origin'])

df_or.info()
def get_mag(x, key):
    return float(x[0]['magnitude'][key])

def get_mag_count(x):
    return int(x[0]['stationCount'])

def get_from_dict(x, key, time=False):
    if not time:
        try:
            return float(x[key])
        except KeyError:
            return 'fixed'
    else:
        return x[key]

def get_region_author(x, key):
    return x[key]
def load_prepare_data(xml_name):
    """
    Prepare dataframe with useful information from event xml

    Parameters
    ----------
    xml_name : str
        Name of seiscomp like event xml file

    Returns
    -------
    pandas.Dataframe
        Dataframe with useful information about events in the xml
    """
    # loading events dataframe
    df = pdx.read_xml('main_events_pruned.xml', ['seiscomp', 'EventParameters', 'event'])
    # loading origins dataframe
    df_or = pdx.read_xml('main_events_pruned.xml', ['seiscomp', 'EventParameters', 'origin'])

    # preparing events dataframe
    df['region'] = df['description'].apply(get_region_author, key='text')
    df['author'] = df['creationInfo'].apply(get_region_author, key='author')
    df = df[['@publicID', 'preferredOriginID', 'author', 'region']]

    # preparing origins dataframe
    df_or['lat'] = df_or['latitude'].apply(get_from_dict, key='value')
    df_or['lon'] = df_or['longitude'].apply(get_from_dict, key='value')
    df_or['z'] = df_or['depth'].apply(get_from_dict, key='value')
    df_or['orig_time'] = df_or['time'].apply(get_from_dict, key='value', time=True)
    df_or['orig_time'] = pd.to_datetime(df_or['orig_time'])

    df_or['lat_e'] = df_or['latitude'].apply(get_from_dict, key='uncertainty')
    df_or['lon_e'] = df_or['longitude'].apply(get_from_dict, key='uncertainty')
    df_or['z_e'] = df_or['depth'].apply(get_from_dict, key='uncertainty')
    df_or['t_e'] = df_or['time'].apply(get_from_dict, key='uncertainty')

    df_or['min_dis'] = df_or['quality'].apply(get_from_dict, key='minimumDistance')
    df_or['phasecount'] = df_or['quality'].apply(get_from_dict, key='usedPhaseCount')
    df_or['stationcount'] = df_or['quality'].apply(get_from_dict, key='usedStationCount')

    df_or['mag'] = df_or['magnitude'].apply(get_mag, key='value')
    df_or['mag_e'] = df_or['magnitude'].apply(get_mag, key='uncertainty')
    df_or['mag_count'] = df_or['magnitude'].apply(get_mag_count)

    df_or = df_or[['@publicID', 'orig_time', 'mag', 'lat', 'lon', 'z',
                   'lat_e', 'lon_e', 'z_e', 'min_dis', 'phasecount',
                   'stationcount', 'mag_e', 't_e']]
    df_or.rename(columns={'@publicID' : 'originID'}, inplace=True)

    # mergin the two dataframes
    df_merge = pd.merge(df, df_or, how='inner',
                        left_on='preferredOriginID',
                        right_on='originID')
    
    return df_merge

%time df = load_prepare_data('main_events_pruned.xml')
df.info()
df.head(2)
df.mag.plot(kind='hist', bins=range(7), figsize=(16, 8))
ax = df['orig_time'].groupby([df['orig_time'].dt.to_period('1w')]).count().plot(kind='bar',
	                                                          legend=False,
	                                                          figsize=(16, 8))
ax.set_xlabel('Fecha')
ax.set_title(u"Número de sismos")
ax.set_axisbelow(True)
ax.grid(alpha=0.6)
fig = ax.get_figure()
plt.tight_layout()
df['orig_time'] = df['orig_time'].astype('datetime64[ns]')
df_ = df.query('orig_time > "2020-10-12" and orig_time <= "2020-10-18"')[['@publicID', 'region', 'orig_time', 'mag', 'z', 'phasecount', 't_e']]
df_.sort_values(by=['orig_time'])
ax = df['orig_time'].groupby([df['orig_time'].dt.to_period('1w')]).count().cumsum().plot(kind='bar',
	                                                          legend=False,
	                                                          figsize=(16, 8))
ax.set_xlabel('Fecha')
ax.set_title(u"Sismos acumulados")
ax.set_axisbelow(True)
ax.grid(alpha=0.6)
fig = ax.get_figure()
plt.tight_layout()
sns.catplot(x='phasecount', y='mag', kind='bar', height=7, aspect=3, color='steelblue', data=df)<jupyter_output><empty_output><jupyter_text>Saving the dataframe in csv<jupyter_code>df.to_csv('all_events.csv')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17186491718649172, 'fraction_numerical': 0.008819100881910088, 'mean_word_length': 3.7448979591836733, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 16}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '124890', 'license': 'MIT', 'n_tokens_mistral': 1677, 'n_tokens_neox': 1623, 'n_words': 267, 'path': 'utils/join_and_count.ipynb', 'repo_name': 'dsiervo/sgc_ai_picker', 'size': '4649'}