import pandas as pd  import numpy as np      mm_select_x_ = ['CREDITSCORE', 'AGE', 'TENURE', 'BALANCE', 'NUMOFPRODUCTS', 'HASCRCARD', 'ISACTIVEMEMBER', 'ESTIMATEDSALARY', 'GEOGRAPHY_FRANCE', 'GEOGRAPHY_GERMANY', 'GEOGRAPHY_SPAIN', 'GENDER_MALE']    mm_graph_selector_scenario = ['Metrics', 'Features', 'Histogram','Scatter']  mm_graph_selected_scenario = mm_graph_selector_scenario[0]    mm_algorithm_selector = ['Baseline','ML']  mm_algorithm_selected = mm_algorithm_selector[0]    mm_pie_color_dict_2 ={"piecolorway":["#00D08A","#FE913C"]}  mm_pie_color_dict_4 = {"piecolorway":["#00D08A","#81F1A0","#F3C178","#FE913C"]}    mm_height_histo = 530      mm_margin_features = {'margin': {'l': 150, 'r': 50, 'b': 50, 't': 20}}    def creation_scatter_dataset_pred(test_dataset:pd.DataFrame, forecast_series:pd.Series):      """이 함수는 예측을 위한 산점도에 대한 데이터 세트를 생성합니다. 모든 열(EXITED 제외)에 대해 양수 및 음수 버전이 있습니다.      EXITED는 예측이 좋은지 나쁜지를 나타내는 바이너리입니다.      양수 열은 Exited가 0일 때 NaN을 가지며 음수 열은 Exited가 1일 때 NaN을 갖습니다.         Args:          test_dataset (pd.DataFrame): 테스트 데이터 세트          forecast_series (pd.DataFrame): 예측 데이터 세트        Returns:          pd.DataFrame: 히스토그램을 표시하는 데 사용되는 데이터 프레임      """            scatter_dataset = test_dataset.copy()      scatter_dataset['EXITED'] =  (scatter_dataset['EXITED']!=forecast_series.to_numpy()).astype(int)        for column in scatter_dataset.columns:          if column != 'EXITED' :              column_neg = str(column)+'_neg'              column_pos = str(column)+'_pos'                            scatter_dataset[column_neg] = scatter_dataset[column]              scatter_dataset[column_pos] = scatter_dataset[column]                            scatter_dataset.loc[(scatter_dataset['EXITED'] == 1),column_neg] = np.NaN              scatter_dataset.loc[(scatter_dataset['EXITED'] == 0),column_pos] = np.NaN            return scatter_dataset      def creation_of_dialog_scatter_pred(column, state=None):      """이 코드는 예측을 위한 산점도에 사용되는 마크다운을 생성합니다. 부분(다시 로드할 수 있는 미니 페이지)을 변경하는 데 사용됩니다.       선택기가 생성되고 그래프의 x 및 y는 여기에서 변경하여 결정됩니다.       속성의 사전도 사용하는 열에 따라 변경됩니다.      """      if column == 'AGE' or column == 'CREDITSCORE' and state is not None:          state.dv_dict_overlay = {'barmode':'overlay',"margin":{"t":20}}      elif state is not None:          state.dv_dict_overlay = {"margin":{"t":20}}                md = """  <|layout|columns= 1 1|columns[mobile]=1|  <|  Select **x** \n \n <|{x_selected}|selector|lov={select_x}|dropdown|>  |>    <|  Select **y** \n \n <|{y_selected}|selector|lov={select_y}|dropdown|>  |>  |>    <|{scatter_dataset_pred}|chart|x="""+column+"""|y[1]={y_selected+'_pos'}|y[2]={y_selected+'_neg'}|color[1]=red|color[2]=green|name[1]=Bad prediction|name[2]=Good prediction|height={mm_height_histo}|width={dv_width_histo}|mode=markers|type=scatter|layout={dv_dict_overlay}|>    """      return md      def creation_histo_full_pred(test_dataset:pd.DataFrame,forecast_series:pd.Series):      """이 함수는 예측에 대한 히스토그램 플롯에 대한 데이터 세트를 생성합니다. 모든 열(PREDICTION 제외)에 대해 양수 및 음수 버전이 있습니다.      PREDICTION은 예측이 좋은지 나쁜지를 나타내는 이진법입니다.      PREDICTION이 0일 때 양수 열에는 NaN이 있고 PREDICTION이 1일 때 음수 열에는 NaN이 있습니다.         Args:          test_dataset (pd.DataFrame): 테스트 데이터 세트          Forecast_series (pd.DataFrame): 예측 데이터 세트        Returns:          pd.DataFrame: 히스토그램을 표시하는 데 사용되는 데이터 프레임      """      histo_full = test_dataset.copy()      histo_full['EXITED'] =  (histo_full['EXITED']!=forecast_series.to_numpy()).astype(int)      histo_full.columns = histo_full.columns.str.replace('EXITED', 'PREDICTION')            for column in histo_full.columns:          column_neg = str(column)+'_neg'          histo_full[column_neg] = histo_full[column]          histo_full.loc[(histo_full['PREDICTION'] == 1),column_neg] = np.NaN          histo_full.loc[(histo_full['PREDICTION'] == 0),column] = np.NaN                return histo_full      metrics_md = """
  <|layout|columns=1 1 1|columns[mobile]=1|    <|  <|{accuracy}|indicator|value={accuracy}|min=0|max=1|width=200px|>    **Model accuracy**    <|{pie_plotly}|chart|x=values|label=labels|title=Accuracy of predictions model|height={height_plotly}|width=100%|type=pie|layout={mm_pie_color_dict_2}|>  |>    <|  <|{score_auc}|indicator|value={score_auc}|min=0|max=1|width=200px|>    **Model AUC**    <|{pie_confusion_matrix}|chart|x=values|label=labels|title=Confusion Matrix|height={height_plotly}|width=100%|type=pie|layout={mm_pie_color_dict_4}|>  |>    <|  <|{f1_score}|indicator|value={f1_score}|min=0|max=1|width=200px|>    **Model F1-score**    <|{distrib_class}|chart|x=values|label=labels|title=Distribution between Exited and Stayed|height={height_plotly}|width=100%|type=pie|layout={mm_pie_color_dict_2}|>  |>    |>  """      features_md = """  <|{features_table}|chart|type=bar|y=Features|x=Importance|orientation=h|layout={mm_margin_features}|>  """    def creation_of_dialog_histogram_pred(column, state=None):      """이 코드는 산점도에 사용된 마크다운을 생성합니다. 부분(다시 로드할 수 있는 미니 페이지)을 변경하는 데 사용됩니다.       선택자가 생성되고 여기에서 변경하여 그래프의 x가 결정됩니다.       속성의 사전도 사용하는 열에 따라 변경됩니다.      """      if column == 'AGE' or column == 'CREDITSCORE' and state is not None:          state.dv_dict_overlay = {'barmode':'overlay',"margin":{"t":20}}      elif state is not None:          state.dv_dict_overlay = {"margin":{"t":20}}                md = """  <|  Select **x** \n \n <|{x_selected}|selector|lov={select_x}|dropdown=True|>  |>    <|{histo_full_pred[['"""+column+"""','"""+column+"""_neg','PREDICTION']]}|chart|type=histogram|x[1]="""+column+"""|x[2]="""+column+"""_neg|y=PREDICTION|label=PREDICTION|color[1]=red|color[2]=green|name[1]=Bad prediction|name[2]=Good prediction|height={mm_height_histo}|width={dv_width_histo}|layout={dv_dict_overlay}|class_name=histogram|>    """      return md      mm_model_manager_md = """  # 모델 매니저    <|layout|columns=1 1 1 1|columns[mobile]=1|  Algorithm  <|{mm_algorithm_selected}|selector|lov={mm_algorithm_selector}|dropdown=True|>    Type of graph  <|{mm_graph_selected_scenario}|selector|lov={mm_graph_selector_scenario}|dropdown=True|>

    <|show roc|button|on_action=show_roc_fct|>

    **Number of predictions: ** *<|{number_of_predictions}|>*    |>    <|part|render={mm_graph_selected_scenario == 'Metrics'}|  """+metrics_md+"""  |>    <|part|render={mm_graph_selected_scenario == 'Features'}|  """+features_md+"""  |>    <|part|render={mm_graph_selected_scenario == 'Scatter'}|partial={partial_scatter_pred}|>    <|part|render={mm_graph_selected_scenario == 'Histogram'}|partial={partial_histo_pred}|>    """


# Roc 다이얼로그  dr_show_roc = False    def show_roc_fct(state):      state.dr_show_roc = True    def delete_dialog_roc(state):      state.dr_show_roc = False    dialog_roc = """  <center>  <|{roc_dataset}|chart|x=False positive rate|y[1]=True positive rate|label[1]=True positive rate|height=500px|width=900px|type=scatter|>  </center>  """


from taipy.gui import Gui  from page.page import *      if __name__ == "__main__":      Gui(page).run(          use_reloader=True,          title="Wine 🍷 production by Region and Year",          dark_mode=False,      )


from taipy.core.config import Config  from config.config import df_wine_production      page = """  # Wine production by Region and Year      ## Data for all the regions:  <|{df_wine_production}|table|height=400px|width=95%|>    """


import pandas as pd      def add_wine_colors(df_wine):      """Adds 2 columns with      Args:          df_wine (DataFrame): Data from the csv file (input for the whole app)        Returns:          df_wine_with_colors: DataFrame with all the input columns plus 2 nexw ones, 'red_and_rose' and 'white',      and drops 2 columns that are not needed      """      print("add wine color columns")      df_wine_with_colors = df_wine.reset_index(drop=True)        df_wine_with_colors["red_and_rose"] = 0      df_wine_with_colors["white"] = 0        # General case:      df_wine_with_colors.loc[          df_wine_with_colors["wine_type"].str.contains("ROUGE"), "red_and_rose"      ] = 1      df_wine_with_colors.loc[          df_wine_with_colors["wine_type"].str.contains("BLANC"), "white"      ] = 1        # For some reason, there is some lines where the information about wine color is in the name:      df_wine_with_colors.loc[          df_wine_with_colors["AOC"].str.contains("Rouge")          & df_wine_with_colors["wine_type"].str.contains("NORD - EST"),          "red_and_rose",      ] = 1      df_wine_with_colors.loc[          df_wine_with_colors["AOC"].str.contains("Blanc")          & df_wine_with_colors["wine_type"].str.contains("NORD - EST"),          "white",      ] = 1        # Droip unnecessary columns      df_wine_with_colors = df_wine_with_colors.drop(columns=["data_type", "wine_type"])        return df_wine_with_colors


import taipy as tp  from taipy.core.config import Config    # Loading of the TOML  Config.load("config/taipy-config.toml")    # Get the scenario configuration  scenario_cfg = Config.scenarios["SCENARIO_WINE"]    tp.Core().run()    scenario_wine = tp.create_scenario(scenario_cfg)  scenario_wine.submit()      df_wine_production = scenario_wine.WINE_PRODUCTION_WITH_COLORS.read()


from taipy.gui import Gui  from tensorflow.keras import models  from PIL import Image # to change img path to actual image  import numpy as np    class_names = {      0:'airplane',      1:'automobile',      2:'bird',      3:'cat',      4:'deer',      5:'dog',      6:'frog',      7:'horse',      8:'ship',      9:'truck',  }    model = models.load_model("baseline_mariya.keras")    def image_predict(model, path_to_img):      img = Image.open(path_to_img)      img = img.convert("RGB")      img = img.resize((32,32))      data = np.asarray(img)      data = data/255      probs = model.predict(np.array([data])[:1])      top_prob = probs.max()      top_pred = class_names[np.argmax(probs)]        return top_prob,top_pred      # index = "
Kaise Ho!!
"  # index = "# Kaise Ho!!"    img_path = "img.png"  content = ""  prob = 0  pred = ""    index = """   <|text-center|  <|{"logo.png"}|image|width=12vw|>    <|{content}|file_selector|extensions=.png,.jpeg|>  Select an image from your file system      <|{pred}|>    <|{img_path}|image|>    <|{prob}|indicator|value={prob}|min=0|max=100|width=25vw|>  >  """    def on_change(state, var_name,var_value):      if var_name == "content":          top_prob,top_pred = image_predict(model,var_value)          state.prob = round(top_prob*100)          state.pred = "This is a " + top_pred          state.img_path = var_value      # print(var_name, var_value)      app = Gui(page=index)    if __name__ == "__main__":      app.run(use_reloader=True)  # means we dont need to type python classifier.py to run it again just refresh the page


from langchain.text_splitter import RecursiveCharacterTextSplitter  from langchain.document_loaders import TextLoader    loader = TextLoader("data/data_2.0.txt")  # Use this line if you only need data.txt    text_splitter = RecursiveCharacterTextSplitter(chunk_size=1500, chunk_overlap=0)  data = loader.load()  texts = text_splitter.split_documents(data)    from langchain.vectorstores import Chroma, Pinecone  from langchain.embeddings.openai import OpenAIEmbeddings  from dotenv import load_dotenv  import os  import time    load_dotenv()      OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")      embeddings = OpenAIEmbeddings(openai_api_key=OPENAI_API_KEY)  print(len(texts))    for i in range(0, len(texts), 100):      try:          db2 = Chroma.from_documents(              texts[i : i + min(100, len(texts) - i)],              embeddings,              persist_directory="chroma_db",          )      except ValueError:          pass      time.sleep(10)      from langchain.chains import RetrievalQAWithSourcesChain  from langchain import OpenAI  from langchain.vectorstores import Chroma  from langchain.embeddings.openai import OpenAIEmbeddings    import os    embeddings = OpenAIEmbeddings()    docsearch = Chroma(persist_directory="chroma_db", embedding_function=embeddings)    chain = RetrievalQAWithSourcesChain.from_chain_type(      OpenAI(temperature=0),      chain_type="stuff",      retriever=docsearch.as_retriever(),      reduce_k_below_max_tokens=True,  )    user_input = input("What's your question: ")    result = chain({"question": user_input}, return_only_outputs=True)      print("Answer: " + result["answer"].replace("\n", " "))  print("Source: " + result["sources"])


from langchain.embeddings.openai import OpenAIEmbeddings  from dotenv import load_dotenv  import os    load_dotenv()      OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")      from langchain.chains import RetrievalQAWithSourcesChain  from langchain import OpenAI  from langchain.vectorstores import Chroma  from langchain.embeddings.openai import OpenAIEmbeddings  from langchain.embeddings.openai import OpenAIEmbeddings  from langchain.vectorstores import Chroma  from langchain.text_splitter import CharacterTextSplitter  from langchain.llms import OpenAI  from langchain.chains import ConversationalRetrievalChain  from langchain.memory import ConversationBufferMemory    memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)      import os    embeddings = OpenAIEmbeddings()    docsearch = Chroma(persist_directory="chroma_db", embedding_function=embeddings)    chain = RetrievalQAWithSourcesChain.from_chain_type(      OpenAI(temperature=0),      chain_type="stuff",      retriever=docsearch.as_retriever(),      reduce_k_below_max_tokens=True,  )      qa = ConversationalRetrievalChain.from_llm(      OpenAI(temperature=0),      docsearch.as_retriever(),      memory=memory,  )      while True:      user_input = input("What's your question: ")      if len(user_input) == 0:          break      print(          docsearch.similarity_search_with_score(              query=user_input, distance_metric="cos", k=6          )      )        result = qa({"question": user_input})        print("Answer: " + result["answer"])


import os  import sys    import openai  from langchain.chains import RetrievalQA  from langchain.chat_models import ChatOpenAI  from langchain.document_loaders import DirectoryLoader, TextLoader  from langchain.embeddings import OpenAIEmbeddings  from langchain.indexes import VectorstoreIndexCreator  from langchain.indexes.vectorstore import VectorStoreIndexWrapper  from langchain.llms import OpenAI  from langchain.vectorstores import Chroma    import constants    os.environ["OPENAI_API_KEY"] = constants.APIKEY    # Enable to save to disk & reuse the model (for repeated queries on the same data)  PERSIST = False    query = input("What's your question?")    if PERSIST and os.path.exists("persist"):      print("Reusing index...\n")      vectorstore = Chroma(          persist_directory="persist", embedding_function=OpenAIEmbeddings()      )      index = VectorStoreIndexWrapper(vectorstore=vectorstore)  else:      # loader = TextLoader("data/data.txt") # Use this line if you only need data.txt      loader = DirectoryLoader("data/")      if PERSIST:          index = VectorstoreIndexCreator(              vectorstore_kwargs={"persist_directory": "persist"}          ).from_loaders([loader])      else:          index = VectorstoreIndexCreator().from_loaders([loader])    chain = RetrievalQA.from_chain_type(      llm=ChatOpenAI(model="gpt-3.5-turbo"),      retriever=index.vectorstore.as_retriever(search_kwargs={"k": 1}),  )  print(chain.run(query))


s = open("data/data.txt", "r")    s = s.read().replace("\n", "")    with open("data/new_data.txt", "w") as x:      x.write(s)


import requests  from bs4 import BeautifulSoup  from urllib.parse import urlparse, urljoin      def scrape_domain_and_subdomains(base_url, file_path):      visited_urls = set()        def scrape(url):          response = requests.get(url)          if response.status_code == 200:              soup = BeautifulSoup(response.content, "html.parser")              div_elements = soup.select("div.md-content")                text = ""              for div in div_elements:                  text += div.get_text()              append_to_text_file(text, file_path)                links = soup.find_all("a")              for link in links:                  href = link.get("href")                  if href:                      subdomain_url = urljoin(url, href)                      parsed_url = urlparse(subdomain_url)                      if (                          parsed_url.netloc.endswith("docs.taipy.io")                          and subdomain_url not in visited_urls                      ):                          visited_urls.add(subdomain_url)                          scrape(subdomain_url)        scrape(f"http://{base_url}")      print(visited_urls)      def append_to_text_file(content, file_path):      with open(file_path, "a", encoding="utf-8") as file:          file.write(content)          file.write("\n")      # Example usage:  base_url = "docs.taipy.io/en/latest/"  # Replace with the base domain to scrape (without the protocol)  file_path = "data/data_2.0.txt"  # Replace with the desired file path    scrape_domain_and_subdomains(base_url, file_path)


# This is a sample Python script.    # Press Maj+F10 to execute it or replace it with your code.  # Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.      def print_hi(name):      # Use a breakpoint in the code line below to debug your script.      print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.      # Press the green button in the gutter to run the script.  if __name__ == '__main__':      print_hi('PyCharm')    # See PyCharm help at https://www.jetbrains.com/help/pycharm/


import taipy as tp  from taipy.gui import Gui, notify  from taipy.config import Config    import numpy as np  import pandas as pd      BUSINESS_PATH = "data/yelp_business.csv"    # Load the business data using pandas  business_df = pd.read_csv(BUSINESS_PATH)    # Remove quotation marks from the name  business_df.name = business_df.name.str[1:-1]    # Taipy Core  Config.load("config/config.toml")    Config.configure_data_node(id="review_data", read_fct_params=("data/yelp_review.csv",))    scenario_object = Config.scenarios["scenario"]  business_name = business_df.name[0]  reviews = None      def on_selection(state):      """      Re-runs the scenario when the user selects a business.        Args:          - state: state of the app      """      notify(state, "info", "Running query...")      business_scenarios = [          s for s in tp.get_scenarios() if s.name == state.business_name      ]      if len(business_scenarios) > 0:          scenario = business_scenarios[0]      else:          scenario = tp.create_scenario(scenario_object, name=state.business_name)          scenario.business_name.write(state.business_name)      tp.submit(scenario)      state.reviews = scenario.parsed_reviews.read()      notify(state, "success", "Query finished")      page = """    # Querying **Big Data**{: .color-primary} with Taipy and Dask    ## Select a **business**{: .color-primary}    <|{business_name}|selector|lov={list(business_df.name)}|dropdown|on_change=on_selection|>        ## Average **stars**{: .color-primary} for that business: <|{"⭐"*int(np.mean(reviews.stars))}|text|raw|>    <|{round(np.mean(reviews.stars),2)}|indicator|value={np.mean(reviews.stars)}|min=1|max=5|width=30%|>    ## **Reviews**{: .color-primary} for that business:    <|{reviews}|table|width=100%|>  """      def on_init(state):      scenario = tp.create_scenario(scenario_object, name=state.business_name)      tp.submit(scenario)      state.reviews = scenario.parsed_reviews.read()      if __name__ == "__main__":      tp.Core().run()      Gui(page).run()


import dask.dataframe as dd      def get_data(path_to_csv: str, optional: str = None):      """      Loads a csv file into a dask dataframe.      Converts the date column to datetime.        Args:          - path_to_csv: path to the csv file          - optional: optional argument (currently necessary to fix Core bug with generic data nodes)        Returns:          - dataset: dask dataframe      """      dataset = dd.read_csv(path_to_csv)      dataset["date"] = dd.to_datetime(dataset["date"])      return dataset      def write_function():      """      Useless function to fix Core bug with generic data nodes.      """      return None


import pandas as pd  import dask.dataframe as dd      def get_id_from_name(name: str, business_dict: dict):      """      Returns the business_id from the name of the business.        Args:          - name: name of the business          - business_dict: dict with the name as key and the business_id as value        Returns:          - business_id: id of the business      """      business_id = business_dict[name]      return business_id      def parse_business_data(data: dd.DataFrame):      """      Parses the reviews of a business.        Args:          - data: dask dataframe with the reviews of a business        Returns:          - data: dask dataframe with the parsed reviews      """      # Sort data by useful      data = data.sort_values(by="useful", ascending=False)      # Keep only the stars, date and text columns      data = data[["stars", "date", "text"]]      return data      def get_business_data(business_id: str, data: dd.DataFrame):      """      Returns a dask dataframe with the reviews of a business.        Args:          - business_id: id of the business          - data: dask dataframe with the reviews        Returns:          - df_business: dask dataframe with the reviews of the business      """      df_business = data[(data.business_id == business_id)].compute()      return df_business      def create_business_dict(business_df: pd.DataFrame):      """      Creates a dict with the name as key and the business_id as value.        Args:          - business_df: pandas dataframe with the business data        Returns:          - business_dict: dict with the name as key and the business_id as value      """      # Remove quotation marks from the name      business_df.name = business_df.name.str[1:-1]      business_dict = dict(zip(business_df.name, business_df.business_id))      print(business_dict)      return business_dict


import time  import pandas as pd  import dask.dataframe as dd    def task1(path_to_original_data: str):      print("__________________________________________________________")      print("1. TASK 1: DATA PREPROCESSING AND CUSTOMER SCORING ...")      start_time = time.perf_counter()  # Start the timer            # Step 1: Read data using Dask      df = dd.read_csv(path_to_original_data)            # Step 2: Simplify the customer scoring formula      df['CUSTOMER_SCORE'] = (          0.5 * df['TotalPurchaseAmount'] / 1000 +          0.3 * df['NumberOfPurchases'] / 10 +          0.2 * df['AverageReviewScore']      )            # Save all customers to a new CSV file      scored_df = df[["CUSTOMER_SCORE", "TotalPurchaseAmount", "NumberOfPurchases", "TotalPurchaseTime"]]            pd_df = scored_df.compute()        end_time = time.perf_counter()  # Stop the timer      execution_time = (end_time - start_time) * 1000  # Calculate the time in milliseconds      print(f"Time of Execution: {execution_time:.4f} ms")            return pd_df    def task2(scored_df, payment_threshold, score_threshold):      print("__________________________________________________________")      print("2. TASK 2: FEATURE ENGINEERING AND SEGMENTATION ...")        payment_threshold, score_threshold = float(payment_threshold), float(score_threshold)      start_time = time.perf_counter()  # Start the timer            df = scored_df            # Feature: Indicator if customer's total purchase is above the payment threshold      df['HighSpender'] = (df['TotalPurchaseAmount'] > payment_threshold).astype(int)            # Feature: Average time between purchases      df['AverageTimeBetweenPurchases'] = df['TotalPurchaseTime'] / df['NumberOfPurchases']            # Additional computationally intensive features      df['Interaction1'] = df['TotalPurchaseAmount'] * df['NumberOfPurchases']      df['Interaction2'] = df['TotalPurchaseTime'] * df['CUSTOMER_SCORE']      df['PolynomialFeature'] = df['TotalPurchaseAmount'] ** 2            # Segment customers based on the score_threshold      df['ValueSegment'] = ['High Value' if score > score_threshold else 'Low Value' for score in df['CUSTOMER_SCORE']]            end_time = time.perf_counter()  # Stop the timer      execution_time = (end_time - start_time) * 1000  # Calculate the time in milliseconds      print(f"Time of Execution: {execution_time:.4f} ms")            return df    def task3(df: pd.DataFrame, metric):      print("__________________________________________________________")      print("3. TASK 3: SEGMENT ANALYSIS ...")      start_time = time.perf_counter()  # Start the timer            # Detailed analysis for each segment: mean/median of various metrics      segment_analysis = df.groupby('ValueSegment').agg({          'CUSTOMER_SCORE': metric,          'TotalPurchaseAmount': metric,          'NumberOfPurchases': metric,          'TotalPurchaseTime': metric,          'HighSpender': 'sum',  # Total number of high spenders in each segment          'AverageTimeBetweenPurchases': metric      }).reset_index()            end_time = time.perf_counter()  # Stop the timer      execution_time = (end_time - start_time) * 1000  # Calculate the time in milliseconds      print(f"Time of Execution: {execution_time:.4f} ms")            return segment_analysis    def task4(df: pd.DataFrame, segment_analysis: pd.DataFrame, summary_statistic_type: str):      print("__________________________________________________________")      print("4. TASK 4: ADDITIONAL ANALYSIS BASED ON SEGMENT ANALYSIS ...")      start_time = time.perf_counter()  # Start the timer        # Filter out the High Value customers      high_value_customers = df[df['ValueSegment'] == 'High Value']            # Use summary_statistic_type to calculate different types of summary statistics      if summary_statistic_type == 'mean':          average_purchase_high_value = high_value_customers['TotalPurchaseAmount'].mean()      elif summary_statistic_type == 'median':          average_purchase_high_value = high_value_customers['TotalPurchaseAmount'].median()      elif summary_statistic_type == 'max':          average_purchase_high_value = high_value_customers['TotalPurchaseAmount'].max()      elif summary_statistic_type == 'min':          average_purchase_high_value = high_value_customers['TotalPurchaseAmount'].min()            median_score_high_value = high_value_customers['CUSTOMER_SCORE'].median()            # Fetch the summary statistic for 'TotalPurchaseAmount' for High Value customers from segment_analysis      segment_statistic_high_value = segment_analysis.loc[segment_analysis['ValueSegment'] == 'High Value', 'TotalPurchaseAmount'].values[0]        # Create a DataFrame to hold the results      result_df = pd.DataFrame({          'SummaryStatisticType': [summary_statistic_type],          'AveragePurchaseHighValue': [average_purchase_high_value],          'MedianScoreHighValue': [median_score_high_value],          'SegmentAnalysisHighValue': [segment_statistic_high_value]      })        end_time = time.perf_counter()  # Stop the timer      execution_time = (end_time - start_time) * 1000  # Calculate the time in milliseconds      print(f"Time of Execution: {execution_time:.4f} ms")        return result_df          if __name__ == "__main__":        t1 = task1("data/SMALL_amazon_customers_data.csv")        t2 = task2(t1, 1500, 1.5)        t3 = task3(t2, "mean")        t4 = task4(t2, t3, "mean")            print(t4)


from recsys.recsys import page_scenario_manager  from taipy.gui import Gui      gui = Gui(page_scenario_manager)  gui.run()


import pandas as pd  import numpy as np    TEST_RATIO = 0.2  MOVIELENS_DATA_PATH = "u.data"  MOVIE_DATA_PATH = "u.item"      def convert_data_to_dataframe(data_path: str, movie_path: str):      data = pd.read_table(data_path)      data.columns = ["u_id", "i_id", "rating", "timestamp"]      data_sort = data.sort_values(by=["u_id"])        movie_name = pd.read_table(          movie_path, sep='|', encoding="latin-1", header=None)      movie_name.drop([2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,                      15, 16, 17, 18, 19, 20, 21, 22, 23], inplace=True, axis=1)      movie_name.rename(columns={0: "movieId", 1: "title"}, inplace=True)        return data_sort, movie_name      movies_list = []    print("\nReading the ratings file...")  ratings, movie_name = convert_data_to_dataframe(      MOVIELENS_DATA_PATH, MOVIE_DATA_PATH)    userIds = ratings.u_id.unique()    trainIds = []  testIds = []    print("Splitting the ratings data...")  for u in userIds:      rating_of_u = ratings.loc[ratings.u_id == u]        trainIds_sample = rating_of_u.sample(          frac=(1-TEST_RATIO), random_state=7)      testIds_sample = rating_of_u.drop(trainIds_sample.index.tolist())        for _, rating in trainIds_sample.iterrows():          if rating.i_id not in movies_list:              # Append new movie's Id to the movie list              movies_list.append(rating.u_id)        trainIds_sample = trainIds_sample.index.values      trainIds_sample.sort()      trainIds = np.append(trainIds, trainIds_sample)        testIds_sample = testIds_sample.index.values      testIds_sample.sort()      testIds = np.append(testIds, testIds_sample)  print("Done.")    print("Write ratings to new file...")  train = ratings.loc[ratings.index.isin(trainIds)]  train.to_csv("rating_train.csv", index=False)  test = ratings.loc[ratings.index.isin(testIds)]  test.to_csv("rating_test.csv", index=False)  movie_name.to_csv("movie.csv", index=False)  print("Done.")


import pandas as pd  from datetime import datetime    data = pd.read_csv("dataset/rating_train.csv")    timestamp = data.timestamp.to_list()  date = []  for time in timestamp:      date.append(datetime.fromtimestamp(time))  data["timestamp"] = date  data.to_csv("data.csv", index=False)


import pandas as pd  import numpy as np  from scipy import sparse      class DataLoader:      def __init__(self):          self.__train_data = None          self.__val_data = None          self.__test_data = None        def __create_id_mapping(self):          if self.__val_data:              unique_uIds = pd.concat(                  [self.__train_data.u_id, self.__test_data.u_id, self.__val_data.u_id]              ).unique()              unique_iIds = pd.concat(                  [self.__train_data.i_id, self.__test_data.i_id, self.__val_data.i_id]              ).unique()          else:              unique_uIds = pd.concat(                  [self.__train_data.u_id, self.__test_data.u_id]              ).unique()              unique_iIds = pd.concat(                  [self.__train_data.i_id, self.__test_data.i_id]              ).unique()            self.user_dict = {uId: idx for idx, uId in enumerate(unique_uIds)}          self.item_dict = {iId: idx for idx, iId in enumerate(unique_iIds)}        def __preprocess(self, data):          """Map the id of all users and items according to user_dict and item_dict.          To create the user_dict, all user ID in the training set is first sorted, then the first ID is map to 0 and so on.          Do the same for item_dict.          This process is done via `self.__create_id_mapping()`.            Args:              data (Dataframe): The dataset that need to be preprocessed.            Returns:              ndarray: The array with all id mapped.          """          # data['u_id'] = data['u_id'].replace(self.user_dict)          # data['i_id'] = data['i_id'].replace(self.item_dict)            data["u_id"] = data["u_id"].map(self.user_dict)          data["i_id"] = data["i_id"].map(self.item_dict)            # Tag unknown users/items with -1 (when val)          data.fillna(-1, inplace=True)            data["u_id"] = data["u_id"].astype(np.int32)          data["i_id"] = data["i_id"].astype(np.int32)            return data[["u_id", "i_id", "rating"]].values        def load_csv2ndarray(          self,          train_data,          test_data,          val_path="rating_val.csv",          use_val=False,          columns=["u_id", "i_id", "rating", "timestamp"],      ):          """          Load training set, validate set and test set via `.csv` file.          Each as `ndarray`.            Args:              train_path (string): path to the training set csv file inside self.__data_folder              test_path (string): path to the testing set csv file inside self.__data_folder              val_path (string): path to the validating set csv file inside self.__data_folder              use_val (boolean): Denote if loading validate data or not. Defaults to False.              columns (list): Columns name for DataFrame. Defaults to ['u_id', 'i_id', 'rating', 'timestamp'].            Returns:              train, val, test (np.array): Preprocessed data.          """          self.__train_data = train_data          self.__test_data = test_data            if use_val:              self.__val_data = self.__read_csv(val_path, columns)            self.__create_id_mapping()            self.__train_data = self.__preprocess(self.__train_data)          self.__test_data = self.__preprocess(self.__test_data)            if use_val:              self.__val_data = self.__preprocess(self.__val_data)              return self.__train_data, self.__val_data, self.__test_data          else:              return self.__train_data, self.__test_data        def load_genome_fromcsv(          self,          genome_file="genome_scores.csv",          columns=["i_id", "g_id", "score"],          reset_index=False,      ):          """          Load genome scores from file.          Args:              genome_file (string): File name that contain genome scores. Must be in csv format.              columns (list, optional): Columns name for DataFrame. Must be ["i_id", "g_id", "score"] or ["i_id", "score", "g_id"].              reset_index (boolean): Reset the genome_tag column or not. Defaults to False.            Returns:              scores (DataFrame)          """          genome = pd.read_csv(              self.__genome_folder + "/" + genome_file, header=0, names=columns          )            if reset_index:              tag_map = {                  genome.g_id: newIdx                  for newIdx, genome in genome.loc[genome.i_id == 1].iterrows()              }              genome["g_id"] = genome["g_id"].map(tag_map)            genome["i_id"] = genome["i_id"].map(self.item_dict)          genome.fillna(0, inplace=True)            return sparse.csr_matrix(              (genome["score"], (genome["i_id"].astype(int), genome["g_id"].astype(int)))          ).toarray()


from taipy import Config, Scope  from functions.funtions import preprocess_data, fit, predict  from config.svd_config import (      n_epochs_cfg,      n_factors_cfg,      learning_rate_cfg,      qi_cfg,      bi_cfg,      bu_cfg,      pu_cfg,  )  from config.kNN_config import (      x_id_cfg,      n_k_neighboor_cfg,      n_min_k_cfg,      sim_measure_cfg,      list_ur_ir_cfg,      similarity_matrix_cfg,      users_id_cfg,      items_id_cfg,  )  TRAIN_DATA_PATH = "dataset/rating_train.csv"  TEST_DATA_PATH = "dataset/rating_test.csv"  MOVIE_DATA_PATH = "dataset/movie.csv"    train_dataset_cfg = Config.configure_data_node(      id="train_dataset",      storage_type="csv",      path=TRAIN_DATA_PATH,      scope=Scope.GLOBAL,      cacheable=True,  )    test_dataset_cfg = Config.configure_data_node(      id="test_dataset",      storage_type="csv",      path=TEST_DATA_PATH,      scope=Scope.GLOBAL,      cacheable=True,  )  movie_name_cfg = Config.configure_data_node(      id="movie_name",      storage_type="csv",      path=MOVIE_DATA_PATH,      scope=Scope.GLOBAL,      cacheable=True,  )    trainset_cfg = Config.configure_data_node(      id="trainset", scope=Scope.PIPELINE, cacheable=True)    testset_cfg = Config.configure_data_node(      id="testset", scope=Scope.PIPELINE, cacheable=True)    true_testset_movies_id_cfg = Config.configure_data_node(      id="true_testset_movies_id", scope=Scope.PIPELINE, cacheable=True  )  algorithm_cfg = Config.configure_in_memory_data_node(      id="algorithm", default_data="kNN")  global_mean_cfg = Config.configure_data_node(      id="global_mean", cope=Scope.GLOBAL, cacheable=True  )  predictions_cfg = Config.configure_data_node(      id="predictions", scope=Scope.PIPELINE)    # Config task  load_data_task_cfg = Config.configure_task(      id="load_data",      function=preprocess_data,      input=[train_dataset_cfg, test_dataset_cfg, movie_name_cfg],      output=[trainset_cfg, testset_cfg, true_testset_movies_id_cfg],  )    train_data_task_cfg = Config.configure_task(      id="train_data",      function=fit,      input=[          trainset_cfg, sim_measure_cfg,          n_factors_cfg, n_epochs_cfg, learning_rate_cfg,          algorithm_cfg,      ],      output=[          similarity_matrix_cfg, list_ur_ir_cfg, users_id_cfg, items_id_cfg, global_mean_cfg,          pu_cfg, qi_cfg, bu_cfg, bi_cfg,      ],  )    predict_task_cfg = Config.configure_task(      id="predict_task",      function=predict,      input=[          testset_cfg,          x_id_cfg, list_ur_ir_cfg, similarity_matrix_cfg, n_k_neighboor_cfg, n_min_k_cfg, users_id_cfg, items_id_cfg,          global_mean_cfg, true_testset_movies_id_cfg,          bu_cfg, bi_cfg, pu_cfg, qi_cfg,          algorithm_cfg,      ],      output=predictions_cfg,  )      # Config pipeline  pipeline_cfg = Config.configure_pipeline(      id="pipeline",      task_configs=[load_data_task_cfg, train_data_task_cfg, predict_task_cfg],  )


from taipy import Config, Scope    x_id_cfg = Config.configure_data_node(id="x_id", default_data=1)    n_min_k_cfg = Config.configure_data_node(id="n_min_k", default_data=10)    n_k_neighboor_cfg = Config.configure_data_node(      id="n_k_neighboor", default_data=1)    sim_measure_cfg = Config.configure_in_memory_data_node(      id="sim_measure", default_data="pcc")    list_ur_ir_cfg = Config.configure_data_node(      id="list_ur_ir", cope=Scope.GLOBAL, cacheable=True)    similarity_matrix_cfg = Config.configure_data_node(      id="similarity_matrix", cope=Scope.GLOBAL, cacheable=True)    items_id_cfg = Config.configure_data_node(      id="x_list", cope=Scope.GLOBAL, cacheable=True)    users_id_cfg = Config.configure_data_node(      id="y_list", cope=Scope.GLOBAL, cacheable=True)


from taipy import Config, Scope  n_factors_cfg = Config.configure_data_node(id="n_factors", default_data=30)    n_epochs_cfg = Config.configure_data_node(id="n_epochs", default_data=50)    learning_rate_cfg = Config.configure_data_node(id="learning_rate", default_data=0.001)    qi_cfg = Config.configure_data_node(id="qi", cope=Scope.GLOBAL, cacheable=True)    pu_cfg = Config.configure_data_node(id="pu", cope=Scope.GLOBAL, cacheable=True)    bu_cfg = Config.configure_data_node(id="bu", cope=Scope.GLOBAL, cacheable=True)    bi_cfg = Config.configure_data_node(id="bi", cope=Scope.GLOBAL, cacheable=True)


import taipy as tp  from taipy.config import Config  from taipy.gui import notify, Markdown      import pandas as pd  import numpy as np    from helper.knn_helper import calculate_precision_recall  from config.config import pipeline_cfg    Config.configure_global_app(clean_entities_enabled=True)  tp.clean_all_entities()    scenario_cfg = Config.configure_scenario(      id="scenario", pipeline_configs=pipeline_cfg)    dataset = pd.read_csv("dataset/data.csv")  dataset["timestamp"] = pd.to_datetime(dataset["timestamp"])    sim_measure_selector = ["pcc", "cosine"]  selected_sim_measure = sim_measure_selector[0]    model_selector = ["kNN", "MF"]  selected_model = sim_measure_selector[0]  # set up parameter of kNN model  n_min_k, n_k_neighboor, x_id, top_k = 1, 10, 1, 1  # set up parameter of svd model  n_epochs, n_factors, learning_rate = 30, 40, 0.001    results, y_id, y_id_real, results_real, recall, precision = (      None, None, None, None, None, None,)    all_scenarios = tp.get_scenarios()  [tp.delete(scenario.id) for scenario in all_scenarios if scenario.name is None]    scenario_selector = [(scenario.id, scenario.name) for scenario in tp.get_scenarios()]      def create_scenario(state):      global selected_scenario        print("Creating scenario...")        scenario = tp.create_scenario(scenario_cfg)        scenario.sim_measure.write(str(state.selected_sim_measure))      scenario.algorithm.write(str(state.selected_model))      scenario.n_min_k.write(int(state.n_min_k))      scenario.n_k_neighboor.write(int(state.n_k_neighboor))      scenario.x_id.write(int(state.x_id))      scenario.n_factors.write(int(state.n_factors))      scenario.n_epochs.write(int(state.n_epochs))      scenario.learning_rate.write(float(learning_rate))        selected_scenario = scenario.id      update_scenario_selector(state, scenario)      tp.submit(scenario)      def submit_scenario(state):      (          state.y_id,          state.y_id_real,          state.results_real,          state.recall,          state.precision,          state.results,      ) = (          None,          None,          [],          None,          None,          [],      )      print("Submitting scenario...")      # Get the selected scenario: in this current step a single scenario is created then modified here.      scenario = tp.get(selected_scenario)        # Change the default parameters by writing in the datanodes      if scenario.sim_measure.read() != state.selected_sim_measure:          scenario.sim_measure.write(str(state.selected_sim_measure))      if scenario.n_min_k.read() != state.n_min_k:          scenario.n_min_k.write(int(state.n_min_k))      if scenario.n_k_neighboor.read() != state.n_k_neighboor:          scenario.n_k_neighboor.write(int(state.n_k_neighboor))      if scenario.x_id.read() != state.x_id:          scenario.x_id.write(int(state.x_id))      if scenario.n_factors.read() != state.n_factors:          scenario.n_factors.write(int(state.n_factors))      if scenario.n_epochs.read() != state.n_epochs:          scenario.n_epochs.write(int(state.n_epochs))      if scenario.learning_rate.read() != state.learning_rate:          scenario.learning_rate.write(float(learning_rate))      if scenario.algorithm.read() != state.selected_model:          scenario.algorithm.write(str(state.selected_model))        # Execute the pipelines/code      tp.submit(scenario)      def update_scenario_selector(state, scenario):      print("Updating scenario selector...")      # Update the scenario selector      state.scenario_selector += [(scenario.id, scenario.name)]      def take_all_movies_rated_by_x_id(test_set, x_id):        test_items = []      test_set = test_set.copy()      for index in range(test_set.shape[0]):          if test_set[index][0] == x_id:              test_items.append(test_set[index])      test_items = np.array(test_items)      return test_items      def predicts(state):      id, predict, id_real, predict_real, user_ratings = (          [], [], [], [], [],)      scenario = tp.get(selected_scenario)      movie_name = (scenario.movie_name.read()).title.to_numpy()        print("'Predict' button clicked")      result = scenario.predictions.read()      result = result[result[:, 4].argsort()[::-1]]        if state.top_k > result.shape[0]:          notify(              state,              notification_type="error",              message="Out of range, top_k max = {}".format(result.shape[0]),          )          state.top_k = result.shape[0]      top_k = state.top_k        test_set = scenario.testset.read()      true_testset_movies_id = scenario.true_testset_movies_id.read()        test_set[:, 1] = true_testset_movies_id.T      test_items = take_all_movies_rated_by_x_id(test_set, state.x_id)      test_items = test_items[test_items[:, 2].argsort()[::-1]]        for i in range(int(top_k)):          id.append(int(result[i][3]))          id_real.append(int(test_items[i][1]))        for i, j in zip(id, id_real):          predict.append(movie_name[(i - 1)])          predict_real.append(movie_name[(j - 1)])        state.y_id = np.array2string(          np.array(id), precision=2, separator=", ", suppress_small=True)      state.y_id_real = np.array2string(          np.array(id_real), precision=2, separator=", ", suppress_small=True)      state.results = predict      state.results_real = predict_real        for _, _, true_r, _, est in result:          user_ratings.append([est, true_r])      user_ratings = np.array(user_ratings)      state.precision, state.recall = calculate_precision_recall(          user_ratings, top_k, 3)      page_scenario_manager = Markdown("recsys/recsys.md")


import pandas as pd  import numpy as np  from ultis.dataloader import DataLoader  from helper.svd_helper import sgd, predict_svd_pair  from helper.knn_helper import predict_pair, compute_similarity_matrix    VALID_ALGORITHM = ["kNN", "MF"]      def preprocess_data(      train_data: pd.DataFrame, test_data: pd.DataFrame, movie_name: pd.DataFrame  ):      """Preprocess data from Pd.DataFrame.        Args:          train_data (pd.DataFrame): Train data load from "csv" file.          test_data (pd.DataFrame): Test data load from "csv" file.          movie_name (pd.DataFrame): The name of movies to recommend load from  "csv" file.        Returns:          train_set (numpy.array): Preprocessed training data.          test_set (numpy.array): Preprocessed testing data.          true_testset_movies_id (numpydarray): When convert, the movie_ids are reseting index, list all actual movies_id.      """      loader = DataLoader()      true_testset_movies_id = test_data.i_id.to_numpy()      train_set, test_set = loader.load_csv2ndarray(          train_data=train_data,          test_data=test_data,          columns=["u_id", "i_id", "rating", "timestamp"],      )      return train_set, test_set, true_testset_movies_id      def fit(      X,      sim_measure="pcc",      n_factors=100,      n_epochs=50,      learning_rate=0.01,      algorithm="kNN",  ):      """Train the recommendation model.        Args:          X (numpyarray): Training data.          sim_measure (str, optional):  Similarity measure function. Defaults to "pcc".          n_factors (int, optional): Number of latent factors. Defaults to 100.          n_epochs (int, optional): Number of SGD iterations. Defaults to 50.          learning_rate (float, optional): The common learning rate. Defaults to 0.01.          algorithm (str, optional): The algorithm used to make recommendations. Possible values are "kNN" or "MF. Defaults to "kNN".        Returns:          S (numpyarray): Compute the similarity between all pairs of users.          x_rated (numpyarray): All users who rated each item are stored in list.          x_list (numpyarray): All user id in training set.          y_list (numpyarray): All movie id in training set.          global_mean (float): Mean ratings in training set.          pu (numpyarray): Users latent factor matrix.          qi (numpyarray): Items latent factor matrix.          bu (numpyarray): Users biases vector.          bi (numpyarray): Items biases vector.      """      if algorithm not in VALID_ALGORITHM:          raise SystemExit(              f"{algorithm} is not a valid algorithm. Possible values are {VALID_ALGORITHM}.\n")        global_mean = np.mean(X[:, 2])      if algorithm == "kNN":          S, x_rated, x_list, y_list = compute_similarity_matrix(              X, sim_measure          )          pu, qi, bu, bi = [], [], [], []        elif algorithm == "MF":            users_list = np.unique(X[:, 0])          items_list = np.unique(X[:, 1])          n_user = users_list.shape[0]          n_item = items_list.shape[0]            # Initialize pu, qi, bu, bi          qi = np.random.normal(0, 0.1, (n_item, n_factors))          pu = np.random.normal(0, 0.1, (n_user, n_factors))          bu = np.zeros(n_user)          bi = np.zeros(n_item)            lr_pu, lr_qi, lr_bu, lr_bi, reg_pu, reg_qi, reg_bu, reg_bi = (              learning_rate, learning_rate, learning_rate, learning_rate, learning_rate, learning_rate, learning_rate, learning_rate,)          pu, qi, bu, bi, _ = sgd(              X, pu, qi, bu, bi,              n_epochs, global_mean, n_factors,              lr_pu, lr_qi, lr_bu, lr_bi,              reg_pu, reg_qi, reg_bu, reg_bi,          )          S, x_rated, x_list, y_list = [], [], [], []      else:          S, x_rated, x_list, y_list, pu, qi, bu, bi = (              0, 0, 0, 0, 0, 0, 0, 0,)        return S, x_rated, x_list, y_list, global_mean, pu, qi, bu, bi      def predict(      test_set_origin,      x_id, x_rated, S, k, min_k, x_list, y_list,      global_mean, true_testset_movies_id,      bu, bi, pu, qi, algorithm="MF",  ):      """Predict the ratings to movies.        Args:          test_set_origin (numpyarray): Storing all user/item pairs we want to predict the ratings.          x_id (int): The user_id we want to recommend to him.          x_rated (numpyarray): All users who rated each item are stored in list.          S (numpyarray): Compute the similarity between all pairs of users.          k (int): Number of neighbors use in prediction.          min_k (int): The minimum number of neighbors to take into account for aggregation. If there are not enough neighbors, the neighbor aggregation is set to zero          x_list (numpyarray): All user id in training set.          y_list (numpyarray): All movie id in training set.          global_mean (float): Mean ratings in training set.          true_testset_movies_id (numpyarray): List all actual movie id.          bu (numpyarray): Users biases vector.          bi (numpyarray): Items biases vector.          pu (numpyarray): Users latent factor matrix.          qi (numpyarray): Items latent factor matrix.          algorithm (str, optional): The algorithm used to make recommendations. Possible values are "kNN" or "MF". Defaults to "MF".        Returns:          predictions (numpyarray): Storing all predictions of the given user/item pairs. The first column is user id, the second column is item id, the thirh colums is the actual movie id, the forth column is the observed rating, and the fifth column is the predicted rating.      """      if algorithm not in VALID_ALGORITHM:          raise SystemExit(              f"{algorithm} is not a valid algorithm. Possible values are {VALID_ALGORITHM}.\n")      test_items = []      test_set = np.zeros(          (test_set_origin.shape[0], test_set_origin.shape[1] + 1))      test_set[:, :3] = test_set_origin      test_set[:, 3] = true_testset_movies_id.T      test_set[:, [0, 1]] = test_set[:, [1, 0]]        for index in range(test_set.shape[0]):          if test_set[index][1] == x_id:              test_items.append(test_set[index])      test_items = np.array(test_items)      n_pairs = test_items.shape[0]        predictions = np.zeros((n_pairs, test_items.shape[1] + 1))      predictions[:, :4] = test_items        if algorithm == "MF":          for pair in range(n_pairs):              predictions[pair, 4] = predict_svd_pair(                  test_items[pair, 0].astype(int),                  test_items[pair, 1].astype(int),                  global_mean,                  bu, bi, pu, qi,              )        elif algorithm == "kNN":          for pair in range(n_pairs):              predictions[pair, 4] = predict_pair(                  test_items[pair, 0].astype(int),                  test_items[pair, 1].astype(int),                  x_rated, S, k, min_k,                  x_list, y_list,                  global_mean,              )        np.clip(predictions[:, 4], 0.5, 5, out=predictions[:, 4])      return predictions


import numpy as np  from numba import njit      def compute_similarity_matrix(train_set, sim_measure="pcc"):      x_rated, _, n_x, _, x_list, y_list = list_ur_ir(train_set)      if sim_measure == "pcc":          print("Computing similarity matrix as pcc...")          S = pcc(n_x, x_rated, min_support=1)      elif sim_measure == "cosine":          print("Computing similarity matrix as cosine...")          S = cosine(n_x, x_rated, min_support=1)      else:          S = 0      return S, x_rated, x_list, y_list      def fit_train_set(train_set):      X = train_set.copy()      # Swap user_id column to movie_id column if using iiCF      X[:, [0, 1]] = X[:, [1, 0]]        x_list = np.unique(X[:, 0])  # For uuCF, x -> user      y_list = np.unique(X[:, 1])  # For uuCF, y -> item      n_x = len(x_list)      n_y = len(y_list)      return n_x, n_y, X, x_list, y_list      def list_ur_ir(train_set):      n_x, n_y, X, x_list, y_list = fit_train_set(train_set)      x_rated = [          [] for _ in range(n_y)      ]  # List where element `i` is ndarray of `(x, rating)` where `x` is all x that rated y, and the ratings.      y_ratedby = [          [] for _ in range(n_x)      ]  # List where element `i` is ndarray of `(y, rating)` where `y` is all y that rated by x, and the ratings.        for xid, yid, r in X:          x_rated[int(yid)].append([xid, r])          y_ratedby[int(xid)].append([yid, r])        for yid in range(n_y):          x_rated[yid] = np.array(x_rated[yid])      for xid in range(n_x):          y_ratedby[xid] = np.array(y_ratedby[xid])      return x_rated, y_ratedby, n_x, n_y, x_list, y_list      def pcc(n_x, yr, min_support=1):      """Compute the Pearson coefficient correlation between all pairs of users (or items).      Only **common** users (or items) are taken into account.      """      prods = np.zeros((n_x, n_x), np.double)      freq = np.zeros((n_x, n_x), np.int)      sqi = np.zeros((n_x, n_x), np.double)      sqj = np.zeros((n_x, n_x), np.double)      si = np.zeros((n_x, n_x), np.double)      sj = np.zeros((n_x, n_x), np.double)        for y_ratings in yr:          prods, freq, sqi, sqj, si, sj = run_pearson_params(              prods, freq, sqi, sqj, si, sj, y_ratings          )        sim = calculate_pearson_similarity(          prods, freq, sqi, sqj, si, sj, n_x, min_support)        return sim      @njit  def run_pearson_params(prods, freq, sqi, sqj, si, sj, y_ratings):      for xi, ri in y_ratings:          xi = int(xi)          for xj, rj in y_ratings:              xj = int(xj)              freq[xi, xj] += 1              prods[xi, xj] += ri * rj              sqi[xi, xj] += ri**2              sqj[xi, xj] += rj**2              si[xi, xj] += ri              sj[xi, xj] += rj        return prods, freq, sqi, sqj, si, sj      @njit  def calculate_pearson_similarity(prods, freq, sqi, sqj, si, sj, n_x, min_sprt):      sim = np.zeros((n_x, n_x), np.double)        for xi in range(n_x):          sim[xi, xi] = 1          for xj in range(xi + 1, n_x):              if freq[xi, xj] < min_sprt:                  sim[xi, xj] = 0              else:                  n = freq[xi, xj]                  num = n * prods[xi, xj] - si[xi, xj] * sj[xi, xj]                  denum = np.sqrt(                      (n * sqi[xi, xj] - si[xi, xj] ** 2)                      * (n * sqj[xi, xj] - sj[xi, xj] ** 2)                  )                  if denum == 0:                      sim[xi, xj] = 0                  else:                      sim[xi, xj] = num / denum                sim[xj, xi] = sim[xi, xj]        return sim      def predict_pair(x_id, y_id, x_rated, S, k, min_k, x_list, y_list, global_mean):      x_known, y_known = False, False      if x_id in x_list:          x_known = True      if y_id in y_list:          y_known = True        if not (x_known and y_known):          # if uuCF:          #     print(f"Can not predict rating of user {x_id} for item {y_id}.")          # else:          #     print(f"Can not predict rating of user {y_id} for item {x_id}.")          return global_mean        return _predict(x_id, y_id, x_rated[y_id], S, k, min_k)      def _predict(x_id, y_id, x_rated, S, k, k_min):      k_neighbors = np.zeros((k, 2))      k_neighbors[:, 1] = -1  # All similarity degree is default to -1        for x2, rating in x_rated:          if int(x2) == x_id:              continue  # Bo qua item dang xet          sim = S[int(x2), x_id]          argmin = np.argmin(k_neighbors[:, 1])          if sim > k_neighbors[argmin, 1]:              k_neighbors[argmin] = np.array((sim, rating))        # Compute weighted average      sum_sim = sum_ratings = actual_k = 0      for (sim, r) in k_neighbors:          if sim > 0:              sum_sim += sim              sum_ratings += sim * r              actual_k += 1        if actual_k < k_min:          sum_ratings = 0        if sum_sim:          est = sum_ratings / sum_sim            return est      return 0      def cosine(n_x, yr, min_support=1):      """Compute the cosine similarity between all pairs of users (or items).      Only **common** users (or items) are taken into account.      """      prods = np.zeros((n_x, n_x), np.double)      freq = np.zeros((n_x, n_x), np.int)      sqi = np.zeros((n_x, n_x), np.double)      sqj = np.zeros((n_x, n_x), np.double)        for y_ratings in yr:          prods, freq, sqi, sqj = run_cosine_params(              prods, freq, sqi, sqj, y_ratings)        sim = calculate_cosine_similarity(prods, freq, sqi, sqj, n_x, min_support)        return sim      @njit  def run_cosine_params(prods, freq, sqi, sqj, y_ratings):      for xi, ri in y_ratings:          xi = int(xi)          for xj, rj in y_ratings:              xj = int(xj)              freq[xi, xj] += 1              prods[xi, xj] += ri * rj              sqi[xi, xj] += ri**2              sqj[xi, xj] += rj**2        return prods, freq, sqi, sqj      @njit  def calculate_cosine_similarity(prods, freq, sqi, sqj, n_x, min_sprt):      sim = np.zeros((n_x, n_x), np.double)        for xi in range(n_x):          sim[xi, xi] = 1          for xj in range(xi + 1, n_x):              if freq[xi, xj] < min_sprt:                  sim[xi, xj] = 0              else:                  denum = np.sqrt(sqi[xi, xj] * sqj[xi, xj])                  sim[xi, xj] = prods[xi, xj] / denum                sim[xj, xi] = sim[xi, xj]        return sim      @njit  def calculate_precision_recall(user_ratings, k, threshold):      """Calculate the precision and recall at k metric for the user based on his/her obversed rating and his/her predicted rating.        Args:          user_ratings (ndarray): An array contains the predicted rating in the first column and the obversed rating in the second column.          k (int): the k metric.          threshold (float): relevant threshold.        Returns:          (precision, recall): the precision and recall score for the user.      """      # Sort user ratings by estimated value      # user_ratings = user_ratings[user_ratings[:, 0].argsort()][::-1]        # Number of relevant items      n_rel = 0      for _, true_r in user_ratings:          if true_r >= threshold:              n_rel += 1        # Number of recommended items in top k      n_rec_k = 0      for est, _ in user_ratings[:k]:          if est >= threshold:              n_rec_k += 1        # Number of relevant and recommended items in top k      n_rel_and_rec_k = 0      for (est, true_r) in user_ratings[:k]:          if true_r >= threshold and est >= threshold:              n_rel_and_rec_k += 1        # Precision@K: Proportion of recommended items that are relevant      # When n_rec_k is 0, Precision is undefined. We here set it to 0.      if n_rec_k != 0:          precision = n_rel_and_rec_k / n_rec_k      else:          precision = 0        # Recall@K: Proportion of relevant items that are recommended      # When n_rel is 0, Recall is undefined. We here set it to 0.      if n_rel != 0:          recall = n_rel_and_rec_k / n_rel      else:          recall = 0        return precision, recall


import numpy as np  from numba import njit      def sgd(      X,      pu,      qi,      bu,      bi,      n_epochs,      global_mean,      n_factors,      lr_pu,      lr_qi,      lr_bu,      lr_bi,      reg_pu,      reg_qi,      reg_bu,      reg_bi,  ):      for epoch_ix in range(n_epochs):          pu, qi, bu, bi, train_loss = _run_svd_epoch(              X,              pu,              qi,              bu,              bi,              global_mean,              n_factors,              lr_pu,              lr_qi,              lr_bu,              lr_bi,              reg_pu,              reg_qi,              reg_bu,              reg_bi,          )        return pu, qi, bu, bi, train_loss      @njit  def _run_svd_epoch(      X,      pu,      qi,      bu,      bi,      global_mean,      n_factors,      lr_pu,      lr_qi,      lr_bu,      lr_bi,      reg_pu,      reg_qi,      reg_bu,      reg_bi,  ):      """Runs an SVD epoch, updating model weights (pu, qi, bu, bi).        Args:          X (ndarray): the training set.          pu (ndarray): users latent factor matrix.          qi (ndarray): items latent factor matrix.          bu (ndarray): users biases vector.          bi (ndarray): items biases vector.          global_mean (float): ratings arithmetic mean.          n_factors (int): number of latent factors.          lr_pu (float, optional): Pu's specific learning rate.          lr_qi (float, optional): Qi's specific learning rate.          lr_bu (float, optional): bu's specific learning rate.          lr_bi (float, optional): bi's specific learning rate.          reg_pu (float, optional): Pu's specific regularization term.          reg_qi (float, optional): Qi's specific regularization term.          reg_bu (float, optional): bu's specific regularization term.          reg_bi (float, optional): bi's specific regularization term.        Returns:          pu (ndarray): the updated users latent factor matrix.          qi (ndarray): the updated items latent factor matrix.          bu (ndarray): the updated users biases vector.          bi (ndarray): the updated items biases vector.          train_loss (float): training loss.      """      residuals = []      for i in range(X.shape[0]):          user, item, rating = int(X[i, 0]), int(X[i, 1]), X[i, 2]            # Predict current rating          pred = global_mean + bu[user] + bi[item]            for factor in range(n_factors):              pred += pu[user, factor] * qi[item, factor]            err = rating - pred          residuals.append(err)            # Update biases          bu[user] += lr_bu * (err - reg_bu * bu[user])          bi[item] += lr_bi * (err - reg_bi * bi[item])            # Update latent factors          for factor in range(n_factors):              puf = pu[user, factor]              qif = qi[item, factor]                pu[user, factor] += lr_pu * (err * qif - reg_pu * puf)              qi[item, factor] += lr_qi * (err * puf - reg_qi * qif)        residuals = np.array(residuals)      train_loss = np.square(residuals).mean()      return pu, qi, bu, bi, train_loss      @njit  def predict_svd_pair(u_id, i_id, global_mean, bu, bi, pu, qi):      """Returns the model rating prediction for a given user/item pair.        Args:          u_id (int): a user id.          i_id (int): an item id.        Returns:          pred (float): the estimated rating for the given user/item pair.      """      user_known, item_known = False, False      pred = global_mean        if u_id != -1:          user_known = True          pred += bu[u_id]        if i_id != -1:          item_known = True          pred += bi[i_id]        if user_known and item_known:          pred += np.dot(pu[u_id], qi[i_id])        return pred


import pandas as pd  from prophet import Prophet  from taipy import Config    def clean_data(initial_dataset: pd.DataFrame):      print("Cleaning Data")      initial_dataset = initial_dataset.rename(columns={"Date": "ds", "Close": "y"})      initial_dataset['ds'] = pd.to_datetime(initial_dataset['ds']).dt.tz_localize(None)      cleaned_dataset = initial_dataset.copy()      return cleaned_dataset    def retrained_model(cleaned_dataset: pd.DataFrame):      print("Model Retraining")      model = Prophet()      model.fit(cleaned_dataset)      return model      def predict(model):      periods = 365      return model.predict(model.make_future_dataframe(periods=periods)[-periods:])[['ds', 'yhat']].rename(columns = {'ds':'Date', 'yhat':'Close_Prediction'})      ## Input Data Nodes  initial_dataset_cfg = Config.configure_data_node(id="initial_dataset",                                                   storage_type="csv",                                                   default_path='df.csv')    cleaned_dataset_cfg = Config.configure_data_node(id="cleaned_dataset")                                                      clean_data_task_cfg = Config.configure_task(id="clean_data_task",                                              function=clean_data,                                              input=initial_dataset_cfg,                                              output=cleaned_dataset_cfg,                                              skippable=True)      model_training_cfg = Config.configure_data_node(id="model_output")                                                      predictions_cfg = Config.configure_data_node(id="predictions")                                                      model_training_task_cfg = Config.configure_task(id="model_retraining_task",                                                  function=retrained_model,                                                  input=cleaned_dataset_cfg,                                                  output=model_training_cfg,                                                  skippable=True)    predict_task_cfg = Config.configure_task(id="predict_task",                                           function=predict,                                           input=model_training_cfg,                                           output=predictions_cfg,                                             skippable=True)    # Create our scenario configuration from our tasks  scenario_cfg = Config.configure_scenario_from_tasks(id="stock",                                                      task_configs=[clean_data_task_cfg,                                                      model_training_task_cfg,                                                      predict_task_cfg])      """  import taipy as tp  import yfinance as yf    # Run of the Taipy Core service  tp.Core().run()    def get_stock_data(ticker, start):      ticker_data = yf.download(ticker, start, dt.datetime.now()).reset_index()  # downloading the stock data from START to TODAY      ticker_data['Date'] = ticker_data['Date'].dt.tz_localize(None)      return ticker_data      tickers = {'MSFT':get_stock_data('MSFT', start_date),              'AAPL':get_stock_data('AAPL', start_date),              'GOOG':get_stock_data('GOOG', start_date)}    def create_and_submit_scenario(stock_name):      scenario_stock = tp.create_scenario(scenario_cfg, name=stock_name)      scenario_stock.initial_dataset.path = f"{stock_name}.csv"      scenario_stock.initial_dataset.write(tickers[stock_name])      tp.submit(scenario_stock)    for stock_name in tickers.keys():      create_and_submit_scenario(stock_name)    stocks = tp.get_scenarios()    for stock in stocks:      print(stock.name)      print(stock.predictions.read())  """


from taipy.gui import Gui, notify  import pandas as pd  import yfinance as yf  from taipy.config import Config  import taipy as tp   import datetime as dt    Config.load('config_model_train.toml')  scenario_cfg = Config.scenarios['stock']    def get_stock_data(ticker, start):      ticker_data = yf.download(ticker, start, dt.datetime.now()).reset_index()  # downloading the stock data from START to TODAY      ticker_data['Date'] = ticker_data['Date'].dt.tz_localize(None)      return ticker_data    start_date = '2015-01-01'    property_chart = {"type":"lines",                    "x":"Date",                    "y[1]":"Open",                    "y[2]":"Close",                    "y[3]":"High",                    "y[4]":"Low",                    "color[1]":"green",                    "color[2]":"grey",                    "color[3]":"red",                    "color[4]":"yellow"                   }      df = pd.DataFrame([], columns = ['Date', 'High', 'Low', 'Open', 'Close'])  df_pred = pd.DataFrame([], columns = ['Date','Close_Prediction'])    stock_text = "No Stock to Show"  chart_text = 'No Chart to Show'  pred_text = 'No Prediction to Show'    stock = ""  stocks = []    page = """  <|toggle|theme|>  # Stock Portfolio    ### Choose the stock to show    <|layout|columns=1 1|  <|{f'The stock is {stock.name}' if stock else 'No Stock to Show'}|>  <|{stock}|selector|lov={stocks}|dropdown|adapter={lambda s: s.name}|>  <|Reset|button|on_action=reset|>  <|Press for Stock|button|on_action=update_ticker_history|active={stock}|>  <|Update Model|button|on_action=update_model|active={stock}|>      <|{f'Monthly history of stock {stock.name}' if stock else 'No Chart to Show'}|>  <|{df}|chart|properties={property_chart}|>  |>    <|{f'1 Year Close Prediction of Stock {stock.name}' if stock else 'No Prediction to Show'}|>  <|{df_pred}|chart|x=Date|y=Close_Prediction|>  """    def reset(state):      state.stock = ""      state.df = pd.DataFrame([], columns = ['Date', 'High', 'Low', 'Open', 'Close'])      state.df_pred = pd.DataFrame([], columns = ['Date','Close_Prediction'])      notify(state, 'success', 'Reset done!')        def update_ticker_history(state):      state.stock.initial_dataset.write(get_stock_data(state.stock.name, start_date))      on_change(state, "stock", state.stock)      notify(state, 'success', 'History up-to-date! You should retrain the model')    def on_change(state, var_name, var_value):      if var_name == "stock" and var_value:          state.df = state.stock.initial_dataset.read()          state.df_pred = state.stock.predictions.read()          def update_model(state):      print("Update Model Clicked")      tp.submit(state.stock)      on_change(state, "stock", state.stock)      notify(state, 'success', 'Model trained and charts up-to-date!')      def on_init(state):      tickers = {'MSFT':get_stock_data('MSFT', start_date),                 'AAPL':get_stock_data('AAPL', start_date),                 'GOOG':get_stock_data('GOOG', start_date)}        def create_and_submit_scenario(stock_name):          scenario_stock = tp.create_scenario(scenario_cfg, name=stock_name)          scenario_stock.initial_dataset.path = f"{stock_name}.csv"          scenario_stock.initial_dataset.write(tickers[stock_name])          tp.submit(scenario_stock)        for stock_name in tickers.keys():          create_and_submit_scenario(stock_name)        state.stocks = tp.get_scenarios()      state.stock = state.stocks[0]    tp.Core().run()  Gui(page).run()


from taipy import Gui  import pandas as pd    # Interactive GUI and state, we maintain states for each individual client  # hence we can have multiple clients with each client having its own state,  # change of the state of one client will not affect the other client's state.    # Each client has its own state and global values are made local to this states,  # example n_weeks, can be stored as state.n_weeks hence changing the state of one  # client does not affect the global n_week or the state of other clients.    # Each time the on_change() is called, three arguments are passed in:  # 1. state  # 2. var_name,  # 3. var_value  # where the state is unique to that client that made the call.    # Whenever a state of a client get changed, on_change() is called with three  # params, this function is a special function in Taipy.    n_weeks = 10      def read_data(dataset_path: str):      df = pd.read_csv(dataset_path)      df["Date"] = pd.to_datetime(df["Date"])      return df      dataset = read_data("./dataset/dataset.csv")  dataset_week = dataset[dataset["Date"].dt.isocalendar().week == n_weeks]      def on_change(state, var_name: str, var_value):      if var_name == "n_weeks":          state.dataset_week = dataset[dataset["Date"].dt.isocalendar(          ).week == var_value]      page = """  # Taipy Basics    *Week number*: *<|{n_weeks}|>*    <|{n_weeks}|slider|min=3|max=52|>    <|{dataset_week}|chart|type=bar|x=Date|y=Value|height=100%|width=100%|>  """    Gui(page=page).run(dark_mode=False)


from taipy import Gui  import pandas as pd    # visual elements: Taipy adds visual elements on top of markdown  # to give you the ability to add charts, tables... The format for  # it is as follows:    # <|{variable}|visual_element_name|param_1=param_1|param_2=param_2| ... |>.  # variable: python variable eg dataframe  # visual_element_name: Name of the visual element eg table  # para_1: parameters passed in    n_weeks = 10    def read_data(dataset_path: str):      df = pd.read_csv(dataset_path)      return df    dataset = read_data("./dataset/dataset.csv")    page = """  # Taipy Basics    *Week number*: *<|{n_weeks}|>*    <|{n_weeks}|slider|min=2|max=30|>    <|{dataset[9000:]}|chart|type=bar|x=Date|y=Value|height=100%|>    # Table Format  <|{dataset}|table|height=400px|width=95%|>  """      Gui(page=page).run(dark_mode=False)


from taipy import Gui    # not no empty spaces at the beginning of the markdown  # markdown must start from the baseline    page = """  ### Hello world  """  # Gui(page=page).run(dark_mode=False)    # you can specify the port number in the run(port=xxxx)  # its by default 5000  Gui(page="Intro to Taipy").run(dark_mode=True)


# 데이터를 처리하기 위한 파이썬의 기본 패키지  from login.login import *  import pandas as pd    # 타이피 코어 import  import taipy as tp    # 내 파이썬 코드의 백엔드 가져오기 | 시나리오를 생성하려면 원본 파이프라인_cfg 및 시나리오_cfg가 필요합니다.  # fixed_variables_default는 고정 변수의 기본값으로 사용됩니다.  from config.config import fixed_variables_default, scenario_cfg, pipeline_cfg  from taipy.core.config.config import Config      # Taipy 프론트엔드에 유용한 기능 가져오기  from taipy.gui import Gui, Markdown, notify, Icon    # 내 파이썬 코드의 프론트엔드 가져오기 | 페이지 가져오기: compare_scenario_md 페이지, 시나리오_매니저_md 페이지, 데이터베이스_md 페이지  # *는 때때로 이 코드의 함수 및/또는 변수가 필요하기 때문에 사용됩니다.  #   from pages.compare_cycles_md import *  from pages.compare_scenario_md import *  from pages.databases_md import *  from pages.data_visualization_md import *    # 임시 파일을 생성하기 위해 import  import pathlib      # 이 경로는 Datasouces 페이지에서 테이블을 다운로드할 수 있는 임시 파일을 만드는 데 사용됩니다.  #   tempdir = pathlib.Path(".tmp")  tempdir.mkdir(exist_ok=True)  PATH_TO_TABLE = str(tempdir / "table.csv")    Config.configure_global_app(clean_entities_enabled=True)  tp.clean_all_entities()    cc_create_scenarios_for_cycle()    from pages.scenario_manager_md import *      ###############################################################################  # 로그인  ###############################################################################      def on_change_user_selector(state):      global user_selector      if state.selected_user == 'Create new user':          state.login = ''          state.dialog_new_account = True      elif state.selected_user in [user[0] for user in user_selector]:          state.login = state.selected_user            if state.selected_user in state.user_in_session:              state.dialog_user = False                reinitialize_state_after_login(state)          else:              state.dialog_login = True        else:          notify(state, "Warning", "Unexpected error")      def reinitialize_state_after_login(state):      scenarios = [s for s in tp.get_scenarios(      ) if 'user' in s.properties and state.login == s.properties['user']]      state.scenario_counter = len(scenarios)      state.cs_show_comparaison = False      state.password = ''      update_scenario_selector(state, scenarios)        if state.dialog_new_account:          state.selected_scenario = None          notify(state, 'info', 'Creating a new session')          state.dialog_new_account = False      else:          if state.scenario_counter != 0:              state.selected_scenario = state.scenario_selector[0][0]            notify(state, 'info', 'Restoring your session')      def validate_login(state, id, action, payload):      global user_selector, users        # if the button pressed is "Cancel"      if payload['args'][0] != 1:          state.dialog_login = False          state.dialog_new_account = False      else:          if state.dialog_new_account:              if state.login in [user[0] for user in user_selector]:                  notify(state, 'error', 'This user already exists')              elif state.login == '':                  notify(state, "Warning", "Please enter a valid login")              elif state.login != '' and len(state.password) > 0:                  state.dialog_login = False                  state.dialog_new_account = False                  state.dialog_user = False                    users[state.login] = {}                  users[state.login]["password"] = encode(state.password)                  users[state.login]["last_visit"] = str(dt.datetime.now())                    json.dump(users, open('login/login.json', 'w'))                  reinitialize_state_after_login(state)                    state.user_selector = [                      (state.login,                       Icon(                           'images/user.png',                           state.login))] + state.user_selector                  user_selector = state.user_selector                  state.selected_user = state.login                    state.user_in_session += state.selected_user          elif state.login in [user[0] for user in user_selector]:              if test_password(users, state.login, state.password):                  state.dialog_login = False                  state.dialog_new_account = False                  state.dialog_user = False                  state.user_in_session += state.selected_user                    reinitialize_state_after_login(state)              else:                  notify(state, "Warning", "Wrong password")            else:              notify(state, "Warning", "Unexpected error")      ###############################################################################  # main_md  ###############################################################################    # 메인 마크다운 페이지입니다. 여기에 메인 페이지에 포함된 다른 페이지가 있습니다.  # 시나리오_매니저_md, 비교_시나리오_md, 데이터베이스_md는 페이지 변수에 따라 보이게 됩니다.  # 이것이 'render' 매개변수의 목적입니다.  menu_lov = [      ("Data Visualization",       Icon(           'images/chart_menu.svg',           'Data Visualization')),      ("Scenario Manager",       Icon(           'images/Scenario.svg',           'Scenario Manager')),      ("Compare Scenarios",       Icon(           'images/compare.svg',           'Compare Scenarios')),      ("Compare Cycles",       Icon(           'images/Cycle.svg',           'Compare Cycles')),      ('Databases',       Icon(           'images/Datanode.svg',           'Databases'))]    main_md = login_md + """    <|menu|label=Menu|lov={menu_lov}|on_action=menu_fct|id=menu_id|>    <|part|render={page == 'Data Visualization'}|  """ + da_data_visualisation_md + """  |>    <|part|render={page == 'Scenario Manager'}|  """ + sm_scenario_manager_md + """  |>    <|part|render={page == 'Compare Scenarios'}|  """ + cs_compare_scenario_md + """  |>    <|part|render={page == 'Compare Cycles'}|  """ + cc_compare_cycles_md + """  |>    <|part|render={page == 'Databases'}|  """ + da_databases_md + """  |>    """      ###############################################################################  # 시나리오 생성/제출/처리를 위한 중요한 기능  ###############################################################################    def update_scenario_selector(state, scenarios: list):      """      이 기능은 시나리오 선택기를 업데이트합니다.       새 시나리오를 만들 때 사용됩니다.       시나리오가 생성되면 이 목록에 (id,name)을 추가합니다.        Args:          scenarios (list): a list of tuples (scenario,properties)      """        state.scenario_selector = [(s.id, s.name) if not s.is_primary else (          s.id, Icon('images/main.svg', s.name)) for s in scenarios]      state.scenario_counter = len(state.scenario_selector)      state.scenario_selector_two = state.scenario_selector.copy()        sm_tree_dict[state.sm_selected_year][state.sm_selected_month] = state.scenario_selector      def make_primary(state):      tp.set_primary(tp.get(state.selected_scenario))      scenarios = [s for s in tp.get_scenarios(      ) if 'user' in s.properties and state.login == s.properties['user']]      update_scenario_selector(state, scenarios)      state.selected_scenario_is_primary = True      def delete_scenario_fct(state):      if tp.get(state.selected_scenario).is_primary:          notify(              state,              "warning",              "You can't delete the primary scenario of the month")      else:          tp.delete(state.selected_scenario)          scenarios = [s for s in tp.get_scenarios(          ) if 'user' in s.properties and state.login == s.properties['user']]          update_scenario_selector(state, scenarios)            if state.scenario_counter != 0:              state.selected_scenario = state.scenario_selector[0][0]      def create_new_scenario(state):      """      이 기능은 cenario_manager_md 페이지에서 '만들기' 버튼을 눌렀을 때 사용합니다.       자세한 내용은 시나리오_매니저_md 페이지를 참조하세요.       다른 시나리오를 구성하고 작성하여 제출합니다.        Args:          state (_type_): the state object of Taipy      """        # 시나리오 카운터 업데이트      state.scenario_counter += 1        print("Creating scenario...")      name = "Scenario " + dt.datetime.now().strftime('%d-%b-%Y') + " Nb : " + \          str(state.scenario_counter)      scenario = tp.create_scenario(scenario_cfg, name=name)      scenario.properties['user'] = state.login        # 모든 시나리오와 해당 속성을 가져옵니다.      print("Getting properties...")      scenarios = [s for s in tp.get_scenarios(      ) if 'user' in s.properties and state.login == s.properties['user']]        # 선택한 시나리오를 변경합니다. 새 시나리오는 선택한 시나리오입니다.      #       state.selected_scenario = scenario.id        # 시나리오 선택기를 업데이트합니다.      print("Updating scenario selector...")      update_scenario_selector(state, scenarios)        # 이 시나리오를 제출      print("Submitting it...")      submit_scenario(state)      def catch_error_in_submit(state):      """      이 함수는 시나리오를 제출할 때 발생할 수 있는 오류를 잡는 데 사용됩니다.       오류가 포착되면 알림이 표시되고 오류를 방지하기 위해 변수가 변경됩니다.       오류는 고정 변수가 잘못 설정되면 실행 불가능하거나       무한한 문제가 발생할 수 있는 Cplex 모델의 솔루션에서 발생합니다.        Args:          state (_type_): the state object of Taipy      """        # 초기 생산량이 최대 생산 능력보다 높은 경우      if state.fixed_variables["Initial_Production_FPA"] > state.fixed_variables["Max_Capacity_FPA"]:          state.fixed_variables["Initial_Production_FPA"] = state.fixed_variables["Max_Capacity_FPA"]          notify(              state,              "warning",              "Value of initial production FPA is greater than max production A")        # 초기 생산량이 최대 생산 능력보다 높은 경우      if state.fixed_variables["Initial_Production_FPB"] > state.fixed_variables["Max_Capacity_FPB"]:          state.fixed_variables["Initial_Production_FPB"] = state.fixed_variables["Max_Capacity_FPB"]          notify(              state,              "warning",              "Value of initial production FPB is greater than max production B")        # 초기 재고가 최대 생산 능력보다 높은 경우      if state.fixed_variables["Initial_Stock_RPone"] > state.fixed_variables["Max_Stock_RPone"]:          state.fixed_variables["Initial_Stock_RPone"] = state.fixed_variables["Max_Stock_RPone"]          notify(              state,              "warning",              "Value of initial stock RP1 is greater than max stock 1")        # 초기 재고가 최대 생산 능력보다 높은 경우      if state.fixed_variables["Initial_Stock_RPtwo"] > state.fixed_variables["Max_Stock_RPtwo"]:          state.fixed_variables["Initial_Stock_RPtwo"] = state.fixed_variables["Max_Stock_RPtwo"]          notify(              state,              "warning",              "Value of initial stock RP2 is greater than max stock 2")        # 초기 생산이 최대 생산 능력보다 높은 경우      #       if state.fixed_variables["Initial_Production_FPA"] + \              state.fixed_variables["Initial_Production_FPB"] > state.fixed_variables["Max_Capacity_of_FPA_and_FPB"]:                            state.fixed_variables["Initial_Production_FPA"] = int(state.fixed_variables["Max_Capacity_of_FPA_and_FPB"] / 2)          state.fixed_variables["Initial_Production_FPB"] = int(state.fixed_variables["Max_Capacity_of_FPA_and_FPB"] / 2)                    notify(              state,              "warning",              "Value of initial productions is greater than the max capacities")      def submit_scenario(state):      """      이 기능은 선택한 시나리오를 제출합니다. '제출' 버튼을 누르거나       새 시나리오를 만들 때 사용됩니다. 오류가 있는지 확인한 다음 문제의 매개변수를 변경하고       시나리오를 제출합니다. 마지막으로 업데이트하려는 모든 변수를 업데이트합니다.        Args:          state (_type_): the state object of Taipy        Returns:          _type_: _description_      """        detect_inactive_session(state)        # 시나리오에 제공될 매개변수에 오류가 있는지 확인합니다.      #       catch_error_in_submit(state)        # 시나리오 가져오기      scenario = tp.get(state.selected_scenario)        # 올바른 매개변수로 시나리오 설정      scenario.fixed_variables.write(state.fixed_variables._dict)        # 시나리오 실행      tp.submit(scenario)        # 업데이트하려는 모든 변수를 업데이트합니다(ch_results, pie_results 및 측정항목)      #       update_variables(state)      def update_variables(state):      """이 함수는 submit_scenario 또는 selected_scenario가 변경될 때만 사용됩니다. 업데이트하려는 모든 유용한 변수를 업데이트합니다.        Args:          state (_type_): the state object of Taipy      """      # 선택한 시나리오 가져오기      scenario = tp.get(state.selected_scenario)        # 결과 읽기      state.ch_results = scenario.pipelines['pipeline'].results.read()      state.pie_results = pd.DataFrame(          {              "values": state.ch_results.sum(axis=0),              "labels": list(state.ch_results.columns)          })        state.sum_costs = state.ch_results['Total Cost'].sum()        bool_costs_of_stock = [c for c in state.ch_results.columns                             if 'Cost' in c and 'Total' not in c and 'Stock' in c]      state.sum_costs_of_stock = int(state.ch_results[bool_costs_of_stock].sum(axis=1)\                                                                          .sum(axis=0))        bool_costs_of_BO = [c for c in state.ch_results.columns                          if 'Cost' in c and 'Total' not in c and 'BO' in c]      state.sum_costs_of_BO = int(state.ch_results[bool_costs_of_BO].sum(axis=1)\                                                                    .sum(axis=0))      def create_chart(ch_results: pd.DataFrame, var: str):      """데이터베이스" 페이지에서 볼 수 있는 차트 테이블을 생성/업데이트하는 함수입니다. 이      함수는 "on_change" 함수에서 선택한 그래프가 변경될 때 차트를 변경하는 데 사용됩니다.        Args:          ch_results (pd.DataFrame): the results database that comes from the state          var (str): the string that has to be found in the columns that are going to be used to create the chart table        Returns:          pd.DataFrame: the chart with the proper columns      """      if var == 'Cost':          columns = ['index'] + [col for col in ch_results.columns if var in col]      else:          columns = [              'index'] + [col for col in ch_results.columns if var in col and 'Cost' not in col]        chart = ch_results[columns]      return chart      def on_change(state, var_name, var_value):      """이 함수는 상태 변수의 변경이 완료될 때 호출됩니다.       변경 사항이 확인되면 변경된 변수에 따라 작업을 생성할 수 있습니다.        Args:          state (State): the state object of Taipy          var_name (str): the changed variable name          var_value (obj): the changed variable value      """      # 변경된 변수가 선택한 시나리오인 경우      if var_name == "selected_scenario" and var_value is not None:          scenario = tp.get(state.selected_scenario)            state.selected_scenario_is_primary = scenario.is_primary            if scenario.results.is_ready_for_reading:              # 시나리오가 변경되면 슬라이더를 올바른 값으로 설정합니다.              #               fixed_temp = tp.get(state.selected_scenario).fixed_variables.read()              state_fixed_variables = state.fixed_variables._dict.copy()              for key in state.fixed_variables.keys():                  state_fixed_variables[key] = fixed_temp[key]              state.fixed_variables = state_fixed_variables              # I update all the other useful variables              update_variables(state)        if var_name == "dialog_user" or var_name == "dialog_login" or var_name == "dialog_new_account" or var_name == "user_selected":          with open('login/login.json', "r") as f:              state.users = json.load(f)          state.user_selector = [(user,Icon('images/user.png', user))                                  for user in state.users.keys()]                    state.user_selector += [('Create new user', Icon('images/new_account.png', 'Create new user'))]        # 그래프가 선택되거나 시나리오가 변경되고 '데이터베이스' 페이지에 있는 경우      # 또는 데이터베이스 페이지로 이동하면 차트 테이블을 업데이트해야 합니다.      if (var_name == 'sm_graph_selected' or var_name == "selected_scenario" and state.page =='Databases')\          or (var_name == 'page' and var_value == 'Databases'):            str_to_select_chart = None            if state.sm_graph_selected == 'Costs':              str_to_select_chart = 'Cost'              state.cost_data = create_chart(state.ch_results, str_to_select_chart)                        elif state.sm_graph_selected == 'Purchases':              str_to_select_chart = 'Purchase'              state.purchase_data = create_chart(state.ch_results, str_to_select_chart)                        elif state.sm_graph_selected == 'Productions':              str_to_select_chart = 'Production'              state.production_data = create_chart(state.ch_results, str_to_select_chart)                        elif state.sm_graph_selected == 'Stocks':              str_to_select_chart = 'Stock'              state.stock_data = create_chart(state.ch_results, str_to_select_chart)                        elif state.sm_graph_selected == 'Back Order':              str_to_select_chart = 'BO'              state.bo_data = create_chart(state.ch_results, str_to_select_chart)                        elif state.sm_graph_selected == 'Product FPA':              str_to_select_chart = 'FPA'              state.fpa_data = create_chart(state.ch_results, str_to_select_chart)                        elif state.sm_graph_selected == 'Product FPB':              str_to_select_chart = 'FPB'              state.fpb_data = create_chart(state.ch_results, str_to_select_chart)                        elif state.sm_graph_selected == 'Product RP1':              str_to_select_chart = 'RP1'              state.rp1_data = create_chart(state.ch_results, str_to_select_chart)                        elif state.sm_graph_selected == 'Product RP2':              str_to_select_chart = 'RP2'              state.rp2_data = create_chart(state.ch_results, str_to_select_chart)            state.chart = create_chart(state.ch_results, str_to_select_chart)          state.partial_table.update_content(state, da_create_display_table_md(str_to_select_chart.lower() + '_data'))              # '데이터베이스' 페이지에 있는 경우 임시 csv 파일을 만들어야 합니다.          #           if state.page == 'Databases':              state.d_chart_csv_path = PATH_TO_TABLE              state.chart.to_csv(state.d_chart_csv_path, sep=',')      # 초기 페이지는 "시나리오 관리자" 페이지입니다.  page = "Data Visualization"      def menu_fct(state, var_name: str, fct, var_value):      """Functions that is called when there is a change in the menu control        Args:          state (_type_): the state object of Taipy          var_name (str): the changed variable name          var_value (_type_): the changed variable value      """        # 올바른 페이지를 렌더링하기 위해 state.page 변수의 값을 변경하십시오.      #       try:          state.page = var_value['args'][0]      except BaseException:          print("Warning : No args were found")        # 'Databases' 페이지에서만 선택할 수 있는 sm_graph_selected의 'All' 옵션에 대한 보안      #       if state.page != 'Databases' and state.sm_graph_selected == 'All':          state.sm_graph_selected = 'Costs'      ##########################################################################  # 상태 및 초기 값 생성  ##########################################################################  gui = Gui(page=Markdown(main_md), css_file='main')  partial_table = gui.add_partial(da_display_table_md)    # 테이블의 너비와 높이 값  width_table = "100%"  height_table = "100%"    # 차트의 너비와 높이 값  width_chart = "100%"  height_chart = "60vh"      def initialize_variables():      # 차트의 초기값      global scenario, pie_results, sum_costs, sum_costs_of_stock, sum_costs_of_BO, scenario_counter,\          cost_data, stock_data, purchase_data, production_data, fpa_data, fpb_data, bo_data, rp1_data, rp2_data, chart, ch_results,\          chart, scenario_selector, selected_scenario, selected_scenario_is_primary, scenario_selector_two, selected_scenario_two,\          fixed_variables        fixed_variables = fixed_variables_default        scenario = None      pie_results = pd.DataFrame(          {              "values": [1] * len(list(ch_results.columns)),              "labels": list(ch_results.columns)          }, index=list(ch_results.columns)          )            sum_costs = 0      sum_costs_of_stock = 0      sum_costs_of_BO = 0      sum_costs_of_BO = 0      scenario_counter = 0        cost_data = create_chart(ch_results, 'Cost')      purchase_data = create_chart(ch_results, 'Purchase')      production_data = create_chart(ch_results, 'Production')      stock_data = create_chart(ch_results, 'Stock')      bo_data = create_chart(ch_results, 'BO')      fpa_data = create_chart(ch_results, 'FPA')      fpb_data = create_chart(ch_results, 'FPB')      rp1_data = create_chart(ch_results, 'RP1')      rp2_data = create_chart(ch_results, 'RP2')        chart = ch_results[['index',                          'Purchase RP1 Cost',                          'Stock RP1 Cost',                          'Stock RP2 Cost',                          'Purchase RP2 Cost',                          'Stock FPA Cost',                          'Stock FPB Cost',                          'BO FPA Cost',                          'BO FPB Cost',                          'Total Cost']]          # 페이지에 표시될 선택기      scenario_selector = []      selected_scenario = None        selected_scenario_is_primary = False        scenario_selector_two = scenario_selector.copy()      selected_scenario_two = None      initialize_variables()    pd.read_csv('data/time_series_demand copy.csv').to_csv('data/time_series_demand.csv')    if __name__ == "__main__":      gui.run(title="Production planning",      		host='0.0.0.0',      		port=os.environ.get('PORT', '5050'),      		dark_mode=False,              use_reloader=False)  else:      app = gui.run(title="Production planning",                    dark_mode=False,                    run_server=False)


import taipy as tp  from taipy import Scope, Config  from taipy.core import Frequency    import json  from algos.algos import *    # 이 코드는 시나리오_cfg 및 파이프라인_csg를 생성하는 코드입니다.   # 이 두 변수는 기본 코드에서 새 시나리오를 만드는 데 사용됩니다.    # 이 코드는 실행 그래프를 구성할 것입니다.    ###############################################################################  # 데이터노드  ###############################################################################    # 첫 번째 데이터 노드를 생성합니다. 소스는 csv 파일입니다.  path_to_demand = 'data/time_series_demand.csv'  demand_cfg = Config.configure_data_node(id="demand",                                  storage_type="csv",                                   scope=Scope.SCENARIO,                                  path=path_to_demand,                                  has_header=True)      fixed_variables_default_json = open('data/fixed_variables_default.json')  fixed_variables_default = json.load(fixed_variables_default_json)    # fixed_variables_default를 기본 데이터로 하는 두 번째 데이터 노드 생성   # 이것은 fixed_variable에 대한 다른 값을 제출할 때 쓸 이 데이터 노드입니다.  fixed_variables_cfg = Config.configure_data_node(id="fixed_variables", default_data = fixed_variables_default, scope=Scope.PIPELINE)    # 모델을 추적하는 데이터 노드는 다음과 같습니다. model_created 데이터 노드, model_solved 데이터 노드  model_created_cfg = Config.configure_data_node(id="model_created", scope=Scope.PIPELINE)  model_solved_cfg = Config.configure_data_node(id="model_solved", scope=Scope.PIPELINE)    # 이것은 메인 코드에서 결과를 얻는 데 사용할 데이터 노드입니다.  results_cfg = Config.configure_data_node(id="results", scope=Scope.PIPELINE)    ###############################################################################  # 작업  ###############################################################################    # (demand_cfg,fixed_variables_cfg) -> |create_model| -> (model_created_cfg)  create_model_task = Config.configure_task(id="create_model",                              input=[demand_cfg,fixed_variables_cfg],                              function=create_model,                              output=[model_created_cfg])    # (model_created_cfg) -> |solve_model| -> (model_solved_cfg)  solve_model_cfg = Config.configure_task(id="solve_model",                                      input=[model_created_cfg],                                      function=solve_model,                                      output=[model_solved_cfg])    # (model_solved_cfg,fixed_variables_cfg,demand_cfg) -> |create_results| -> (results_cfg)  create_results_cfg = Config.configure_task(id="create_results",                                         input=[model_solved_cfg,fixed_variables_cfg,demand_cfg],                                         function=create_results,                                         output=[results_cfg])      ###############################################################################  # 파이프라인 및 시나리오 구성  ###############################################################################    # 파이프라인 : 작업 실행. 일련의 작업을 수행하는 것을 말합니다.   pipeline_cfg = Config.configure_pipeline(id="pipeline",task_configs=[create_model_task,solve_model_cfg,create_results_cfg])    # 시나리오 : 파이프라인 실행. 일련의 파이프라인(여기서는 하나만)의 실행을 나타냅니다.  # 이 변수를 통해 새로운 시나리오를 생성합니다.  scenario_cfg = Config.configure_scenario(id="scenario",pipeline_configs=[pipeline_cfg], frequency=Frequency.MONTHLY)