<jupyter_start><jupyter_text>How to analyze your own time series_<NAME>_Run the following notebook to analyze a time series describing the progression of an outbreak using oursliding window technique. This notebook can be run "as-is" to regenerate figure 7 from our paper, or can be easilymodified to analyze your data. See the [ReadMe on Github](https://github.com/MolEvolEpid/MachineLearningModelforHIVOutbreaks/blob/main/README.md) for details on how to format your data.<jupyter_code>import sys
sys.path.append('../Python')  # add our python modules to the search path
import numpy as np
from Structures import MultipleModel as MultiModel
import matplotlib.pyplot as plt
import TimeSeries as TSM
from TimeSeries import TimeSeries as TS<jupyter_output><empty_output><jupyter_text>Load in the model from `Trained_models/publication_trained_model/`. Point this at your trained model.<jupyter_code>model = MultiModel()
window_size = 15  
key ='None'
model.import_from_directory('../Trained_models/Publication_trained_model/')
#model_15.import_from_directory(f'./Models_tmp/HIV_1x3-realmodel-rev_3/Order-{key}-{size}/')<jupyter_output>['../Trained_models/Publication_trained_model/P-0-time-27-04-2021--01-07-13.h5', '../Trained_models/Publication_trained_model/P-1-time-27-04-2021--01-07-13.h5', '../Trained_models/Publication_trained_model/P-2-time-27-04-2021--01-07-13.h5', '../Trained_models/Publication_trained_model/P-3-time-27-04-2021--01-07-13.h5', '../Trained_models/Publication_trained_model/P-4-time-27-04-2021--01-07-13.h5']
../Trained_models/Publication_trained_model/P-0-time-27-04-2021--01-07-13.h5<jupyter_text>Now load in the time series. Each time point should be loaded into a single `.mat` file.<jupyter_code>tdata = TS(scale_factor=296)  # Need to scale from normalized mutations

tdata.load_series_from_dir(directory = f'../Example_Data/Outbreak_series/')
olists = [str(item[0][0]) for item in tdata.ordered_labels]  # collect labels from numpy strings<jupyter_output><empty_output><jupyter_text>Optional: Increase the font size. The backend uses `matplotlib.imshow` to avoid rectangular formatting with [pcolormesh](https://stackoverflow.com/questions/21166679/when-to-use-imshow-over-pcolormesh).To avoid aliasing or smoothing, the image is intentionally made large.<jupyter_code>plt.rcParams.update({'font.size':40})<jupyter_output><empty_output><jupyter_text>The analysis is run in a few lines of front-end code using our library. Depending on the amount of data and if you are using a CPU or GPU, this may take up to several minutes.A warning is raised when there are not enough samples.<jupyter_code>%%time
active_cut_length = 2  # how many years to allow an infection to be "active" before manually turning it off
# Evaluate the data set using the NN model
preds, labeldict, maplist = tdata.evaluate(model=model, window_size=window_size, choice_method='argmax', 
                                                   cluster_method='HC')
# preds - per-person predictions
# labeldict - store label keys
# maplist - store info about any sorting used to compute predictions

# Sorting is needed for large data sets

fig, ax = plt.subplots(1, figsize=(30, 30))
# Pass these into the activity plot function so we can control the size
ax, fig = TSM.plot_activity(preds=preds, sort_map_list=maplist, label_list=olists, window_size=window_size, 
                            cbar=False, legend=True,  # switch between a legend, labeled color bar, or both. 
                            fig=fig, ax=ax, active_length_cutoff=active_cut_length)
    #fig.tight_layout(
save_outputs = False # flag to enable saving outputs. 
if save_outputs:
    fig.savefig(f'./Predictions_{window_size}_active_cut_{active_cut_length}_reds.png', dpi=700, bbox_inches='tight')
    fig.savefig(f'./Predictions_{window_size}_active_cut_{active_cut_length}_reds.eps', bbox_inches='tight')
    fig.savefig(f'./Predictions_{window_size}_active_cut_{active_cut_length}_reds.pdf', bbox_inches='tight')
    fig.savefig(f'./Predictions_{window_size}_active_cut_{active_cut_length}_reds.svg', bbox_inches='tight')
plt.show()<jupyter_output>(278, 264)
{'fraction_non_alphanumeric': 0.10624394966118103, 'fraction_numerical': 0.032429816069699906, 'mean_word_length': 4.998548621190131, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '122276', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1417, 'n_tokens_neox': 1317, 'n_words': 335, 'path': 'Notebooks/TimeSeries Presentation.ipynb', 'repo_name': 'MolEvolEpid/MachineLearningModelforHIVOutbreaks', 'size': '4143'}