From c8afe93963b299ab90591745cbf9a40b0de947f7 Mon Sep 17 00:00:00 2001 From: Gordon Shotwell Date: Wed, 27 Mar 2024 11:25:37 -0300 Subject: [PATCH] Target app refactor (#52) * Remove conditional panel * Add data tab with value boxes --- apps/target-app/app.py | 142 +++++++++++++---------------- apps/target-app/simulated-data.csv | 2 +- 2 files changed, 65 insertions(+), 79 deletions(-) diff --git a/apps/target-app/app.py b/apps/target-app/app.py index a13f895..10b3ace 100644 --- a/apps/target-app/app.py +++ b/apps/target-app/app.py @@ -1,5 +1,6 @@ +from numpy import char, place from shiny.express import ui, input, render -from shiny import render_plot, req, reactive +from shiny import reactive import pandas as pd from pathlib import Path from plots import ( @@ -11,45 +12,33 @@ import faicons as fa import io from shinywidgets import render_plotly +import time file_path = Path(__file__).parent / "simulated-data.csv" -ui.page_opts(title="Monitoring") df = pd.read_csv(file_path, dtype={"sub_account": str}) df["date"] = pd.to_datetime(df["date"], errors="coerce") -@reactive.calc -def monitor_sampled_data() -> pd.DataFrame: - start_date, end_date = input.dates() - start_date = pd.to_datetime(start_date) - end_date = pd.to_datetime(end_date) - df_value = df - out = df_value[ - (df_value["date"] > start_date) & (df_value["date"] <= end_date) - ].sample(n=input.sample(), replace=True) - return out - - @reactive.calc() -def monitor_filtered_data() -> pd.DataFrame: - sample_df = monitor_sampled_data() - sample_df = sample_df.loc[sample_df["account"] == input.account()] - return sample_df.reset_index(drop=True) +def account_data(): + return df[ + (df["account"] == input.account()) & (df["sub_account"] == input.sub_account()) + ] @reactive.calc() -def training_data(): - return df[ - (df["account"] == input.account()) & (df["sub_account"] == input.sub_account()) +def character_filter(): + return account_data()[ + (account_data()["text"].str.len() >= input.chars()[0]) + & (account_data()["text"].str.len() <= input.chars()[1]) ] @reactive.effect @reactive.event(input.reset) def reset_vals(): - ui.update_date_range("dates", start="2023-01-01", end="2023-04-01") - ui.update_numeric("sample", value=10000) + ui.update_slider("chars", value=[500, 6000]) with ui.sidebar(): @@ -71,56 +60,70 @@ def sub_selector(): choices = choice_data["sub_account"].unique().tolist() ui.input_select("sub_account", "Sub Account", choices=choices) - with ui.panel_conditional("input.tabs !== 'Training Dashboard'"): - ui.input_date_range( - "dates", - "Dates", - start="2023-01-01", - end="2023-04-01", + with ui.tooltip(id="btn_tooltip", placement="right"): + ui.input_slider( + "chars", + "Text length", + min=0, + max=df["text"].str.len().max(), + value=[500, 6000], ) - ui.input_numeric("sample", "Sample Size", value=10000, step=5000) - ui.input_action_button("reset", "Reset Values", class_="btn-primary") - - -with ui.navset_bar(id="tabs", title="Monitoring"): - with ui.nav_panel("Training Dashboard"): - with ui.layout_columns(): - with ui.card(): - ui.card_header("Model Metrics") - - @render_plotly - def metric(): - if input.metric() == "ROC Curve": - return plot_auc_curve( - training_data(), "is_electronics", "training_score" - ) - else: - return plot_precision_recall_curve( - training_data(), "is_electronics", "training_score" - ) + "The number of characters in the text" + + ui.input_action_button("reset", "Reset Values", class_="btn-primary") + +with ui.nav_panel("Training Dashboard"): + with ui.layout_columns(): + with ui.card(): + ui.card_header("Model Metrics") @render_plotly def metric(): - df_value = df() - df_filtered = df_value[df_value["account"] == input.account()] if input.metric() == "ROC Curve": return plot_auc_curve( - df_filtered, "is_electronics", "training_score" + character_filter(), "is_electronics", "training_score" ) else: return plot_precision_recall_curve( - df_filtered, "is_electronics", "training_score" + character_filter(), "is_electronics", "training_score" ) - @render_plotly - def score_dist(): - return plot_score_distribution(training_data()) + ui.input_select( + "metric", "Metric", choices=["ROC Curve", "Precision Recall"] + ) + + with ui.card(): + ui.card_header("Model Scores") @render_plotly def score_dist(): - df_value = df() - df_filtered = df_value[df_value["account"] == input.account()] - return plot_score_distribution(df_filtered) + return plot_score_distribution(character_filter()) + + +vb_theme = "bg-blue" + +with ui.nav_panel("Data"): + with ui.layout_columns(): + with ui.value_box(theme=vb_theme): + "Number of records" + + @render.text + def data_count(): + return str(character_filter().shape[0]) + + with ui.value_box(theme=vb_theme): + "Mean Score" + + @render.text + def mean_score(theme=vb_theme): + return f"{character_filter()['training_score'].mean() * 100:.2f}%" + + with ui.value_box(theme=vb_theme): + "Mean Text Length" + + @render.text + def mean_text_length(): + return f"{character_filter()['text'].str.len().mean():.2f} characters" with ui.card(full_screen=True): with ui.card_header(): @@ -134,27 +137,10 @@ def score_dist(): @render.download(filename=lambda: f"{input.account()}_scores.csv") def download_data(): with io.BytesIO() as buf: - filtered_data().to_csv(buf, index=False) + account_data().to_csv(buf, index=False) buf.seek(0) yield buf.getvalue() @render.data_frame def data_output(): - return filtered_data().drop(columns=["text"]) - - -with ui.nav_panel("Model Monitoring"): - with ui.layout_columns(): - with ui.card(): - ui.card_header("API Response Time") - - @render_plotly - def api_response(): - return plot_api_response(filtered_data()) - - with ui.card(): - ui.card_header("Production Scores") - - @render_plotly - def prod_score_dist(): - return plot_score_distribution(filtered_data()) + return account_data().drop(columns=["text"]) diff --git a/apps/target-app/simulated-data.csv b/apps/target-app/simulated-data.csv index f35d44f..95699a3 100644 --- a/apps/target-app/simulated-data.csv +++ b/apps/target-app/simulated-data.csv @@ -468,7 +468,7 @@ id,account,sub_account,text,date,is_electronics,training_score,prod_score 466,Hintz & Hintz,695,"Goank Car Vaccum Cleaner Multipurpose Vacuum Cleaner Rated Voltage: DC 12 V Powerful suction. Compact, handy and lightweight. Occupies a tiny place, easy to empty. Used directly from the car lighter socket. Controlled by ON / OFF switch, easy to use. Can suck cigarette ash, cookie crumbs, eraser crumbs, hair, rice, wastepaper, etc. Complete with convenient attachment and washable filter. Perfect for home, office, school, travel and all vehicles",2023-03-14,0,0.39302,0.1218568181868173 467,Hintz & Hintz,695,Research in Education,2023-02-25,0,0.029361194,1.1324627432188985 468,Hintz & Hintz,695,"The Shining Review Obviously a masterpiece, probably the best supernatural novel in a hundred yearsAs a storyteller, he is up there in the Dickens class Book Description One of the true classics of horror fiction, THE SHINING is regarded as one of Stephen King's masterpieces. See all Product description",2023-02-01,0,2.2434835e-4,1.1270447762764544 -469,Hintz & Hintz,695,"The Brain's Way of Healing: Remarkable Discoveries and Recoveries from the Frontiers of Neuroplasticity Review #1 Globe and Mail Nonfiction Bestseller#1 Toronto Star Nonfiction Bestseller Praise for The Brain’s Way of Healing   “Brilliant and highly original. Neurology used to be considered a depressing discipline with patients often displaying fascinating but essentially untreatable symptoms and disabilities. Drawing on the last three decades of research, Doidge challenges this view, using vivid portraits of patients and their physicians. The book is a treasure trove of the author’s own deep insights and a clear bright light of optimism shines through every page.” —V. S. Ramachandran, MD, PhD, neurologist, neuroscientist, and author of The Tell-Tale Brain (W. W. Norton, 2011), Director, UCSD Center for Brain and Cognition “A tour de force. In one of the most riveting books on the human brain and its mystery powers ever written, Doidge addresses the role of alternative medical therapies to reset and re-sync the dynamic patterns of ‘energy in our brain, whit the ability to restore relatively normal health to those whose fate seems hopeless. . . . These are people that traditional medicine all but abandoned as . . . untreatable. But they were rescued. . . . It’s possible to start anywhere in the book and be mesmerized.” —Huffington Post“An exciting overview of powerful new neuroscience theories that connect mind, body, and soul . . . In this age of distraction and unnatural environments and actions—like staring at screens all day—brain science offers all kinds of useful techniques to care for our infinitely complex selves. Norman Doidge’s work is a Michelin Guide to this hopeful new trove of knowledge and insight.” —Boston Globe, USA  “Stunning . . . The Brain’s Way of Healing is another groundbreaking book by Norman Doidge.
 His style keeps you going into the deep dark secrets
of how the brain works. . . . [H]is reframing of remarkable treatments
that I had categorized as gimmicky left me fascinated and humbled. He brings a whole new level of insight into the body, brain, mind connection
that will impact any reader.” — John J. Ratey, MD, Clinical Associate Professor of Psychiatry,
Harvard Medical School and author of Driven to Distraction   “Bold, remarkable . . . paradigm challenging. The Brain’s Way of Healing is brilliantly organized, scientifically documented, and a beautifully written narrative that captivates the reader, who is left with the profound message that the brain, similar to other organs, can heal.”  —Stephen W. Porges, PhD, Indiana University Bloomington and author of The Polyvagal Theory “Doidge’s book is filled with compelling stories about the power of ingenious technologies and disciplined awareness methods generated by innovators who transcended their own brain challenges, and who now use them to help others make radical improvements in conditions often deemed hopeless. It points to a future of remarkable and unprecedented brain healing.” —Martha Herbert, MD, PhD, Neurologist, Harvard Medical School, and Massachusetts General Hospital, author of The Autism Revolution   “The Brain’s way of Healing is a stunner—the sort of book you want to read several times, not because it is difficult to understand, but because it opens up so many novel and startling avenues into our potential to heal. Norman Doidge enthralls us with a rich combination of lucidly explained brain research and pioneering new (and some not so new, but not widely known) approaches to recovery. With an eloquence reminiscent of Oliver Sacks, Doidge bolsters the latest advances in brain science with a series of extraordinary case histories of people for whom all hope seemed to be lost, but who healed as a result of great personal courage, and by changing the ways their bodies and brains processed sensations and movement. This hopeful book demonstrates that a variety of sensory inputs—light, sound, electricity, vibration, movement, and thought—can awaken the brain’s attention processors, and thereby allow even the most afflicted to (re)gain ownership of their lives.  —Bessel van der Kolk MD, Medical Director, the Trauma Center, Brookline MA; Professor of Psychiatry, Boston University School of Medicine; Author of The Body keeps the Score: Mind, Brain and Body in the healing of Trauma “The book offers real hope to individuals suffering from diverse chronic conditions. It shows in terms of graphic personal stories that we truly do not yet know the limits of what is possible in rehabilitation. The book also has a number of creative integrations of the data that will be of interest to neuroscientists.” —Edward Taub, Ph.D., Behavioral Neuroscientist, University Professor,University of Alabama at Birmingham, Director, UAB CI Therapy Research Group and Taub Training Clinic  “Everyone who has a brain could benefit from reading Doidge’s book.” —The Columbus Dispatch   “A vivid, robust and optimistic read . . . an essential addition to our growing understanding of the mind-brain-body connection. Doidge argues quite convincingly that when the brain is damaged or incompletely formed, whether from stroke, multiple sclerosis, traumatic brain injury, autism, ADHD or a host of other conditions, it’s entirely possible to “rewire” the circuits by training a different part of the brain to take over the task. . . . He's positively elegant in his crystalline explanations of brain science for a lay audience.” —Toronto Star, Canada    “This is a book of miracles: an absorbing compendium of unlikely recoveries from physical and mental ailments offers evidence that the brain can heal. Fascinating . . . brings to mind Oliver Sacks.” —Guardian   “Dazzling . . . In friendly vignettes reminiscent of Oliver Sacks’s case studies, Doidge chronicles the heroic efforts of patients with a wide variety of apparently intractable ailments, from chronic pain to multiple sclerosis. . . . Each of Doidge’s examples suggests tangible treatment ideas for patients who may have thought they were out of options. Doidge’s penchant for considering unconventional approaches to healing offers hope for all.” —Bookpage, USA   “Beautifully written . . . inspiring . . . merging scientific information into timeless and fascinating personal stories . . . The Brain's Way of Healing grabs onto the reader at once and compels them to keep reading. This is an important and encouraging book.” —The Vancouver Sun, Canada   “Exhilarating science . . . In an era of ever-increasing medicalisation of the human mind, and the medication of it, the appeal of neuroplasticity outlined by Doidge is addictive. It is inspiring, page-turning stuff.” —Sunday Times, London“A fascinating study on brain science that shows the way to major therapeutic discoveries.” —Library Journal About the Author Norman Doidge, M.D., is a psychiatrist and psychoanalyst. He is on the research faculty at Columbia University’s Center for Psychoanalytic Training and Research in New York City and on the faculty of the University of Toronto’s department of psychiatry. His last book, The Brain that Changes Itself, was a New York Times bestseller with more than a million copies sold. He lives in Toronto. See all Product description",2023-02-22,0,0.0042169653,0.1529045440396485 +469,Hintz & Hintz,695,"The Brain's Way of Healing: Remarkable Discoveries and Recoveries from the Frontiers of Neuroplasticity Review #1 Globe and Mail Nonfiction Bestseller#1 Toronto Star Nonfiction Bestseller Praise for The Brain’s Way of Healing   “Brilliant and highly original. Neurology used to be considered a depressing discipline with patients often displaying fascinating but essentially untreatable symptoms and disabilities. Drawing on the last three decades of research, Doidge challenges this view, using vivid portraits of patients and their physicians. The book is a treasure trove of the author’s own deep insights and a clear bright light of optimism shines through every page.” —V. S. Ramachandran, MD, PhD, neurologist, neuroscientist, and author of The Tell-Tale Brain (W. W. Norton, 2011), Director, UCSD Center for Brain and Cognition “A tour de force. In one of the most riveting books on the human brain and its mystery powers ever written, Doidge addresses the role of alternative medical therapies to reset and re-sync the dynamic patterns of ‘energy in our brain, whit the ability to restore relatively normal health to those whose fate seems hopeless. . . . These are people that traditional medicine all but abandoned as . . . untreatable. But they were rescued. . . . It’s possible to start anywhere in the book and be mesmerized.” —Huffington Post“An exciting overview of powerful new neuroscience theories that connect mind, body, and soul . . . In this age of distraction and unnatural environments and actions—like staring at screens all day—brain science offers all kinds of useful techniques to care for our infinitely complex selves. Norman Doidge’s work is a Michelin Guide to this hopeful new trove of knowledge and insight.” —Boston Globe, USA  “Stunning . . . The Brain’s Way of Healing is another groundbreaking book by Norman Doidge. His style keeps you going into the deep dark secretsof how the brain works. . . . [H]is reframing of remarkable treatmentsthat I had categorized as gimmicky left me fascinated and humbled. He brings a whole new level of insight into the body, brain, mind connectionthat will impact any reader.” — John J. Ratey, MD, Clinical Associate Professor of Psychiatry,Harvard Medical School and author of Driven to Distraction   “Bold, remarkable . . . paradigm challenging. The Brain’s Way of Healing is brilliantly organized, scientifically documented, and a beautifully written narrative that captivates the reader, who is left with the profound message that the brain, similar to other organs, can heal.”  —Stephen W. Porges, PhD, Indiana University Bloomington and author of The Polyvagal Theory “Doidge’s book is filled with compelling stories about the power of ingenious technologies and disciplined awareness methods generated by innovators who transcended their own brain challenges, and who now use them to help others make radical improvements in conditions often deemed hopeless. It points to a future of remarkable and unprecedented brain healing.” —Martha Herbert, MD, PhD, Neurologist, Harvard Medical School, and Massachusetts General Hospital, author of The Autism Revolution   “The Brain’s way of Healing is a stunner—the sort of book you want to read several times, not because it is difficult to understand, but because it opens up so many novel and startling avenues into our potential to heal. Norman Doidge enthralls us with a rich combination of lucidly explained brain research and pioneering new (and some not so new, but not widely known) approaches to recovery. With an eloquence reminiscent of Oliver Sacks, Doidge bolsters the latest advances in brain science with a series of extraordinary case histories of people for whom all hope seemed to be lost, but who healed as a result of great personal courage, and by changing the ways their bodies and brains processed sensations and movement. This hopeful book demonstrates that a variety of sensory inputs—light, sound, electricity, vibration, movement, and thought—can awaken the brain’s attention processors, and thereby allow even the most afflicted to (re)gain ownership of their lives.  —Bessel van der Kolk MD, Medical Director, the Trauma Center, Brookline MA; Professor of Psychiatry, Boston University School of Medicine; Author of The Body keeps the Score: Mind, Brain and Body in the healing of Trauma “The book offers real hope to individuals suffering from diverse chronic conditions. It shows in terms of graphic personal stories that we truly do not yet know the limits of what is possible in rehabilitation. The book also has a number of creative integrations of the data that will be of interest to neuroscientists.” —Edward Taub, Ph.D., Behavioral Neuroscientist, University Professor,University of Alabama at Birmingham, Director, UAB CI Therapy Research Group and Taub Training Clinic  “Everyone who has a brain could benefit from reading Doidge’s book.” —The Columbus Dispatch   “A vivid, robust and optimistic read . . . an essential addition to our growing understanding of the mind-brain-body connection. Doidge argues quite convincingly that when the brain is damaged or incompletely formed, whether from stroke, multiple sclerosis, traumatic brain injury, autism, ADHD or a host of other conditions, it’s entirely possible to “rewire” the circuits by training a different part of the brain to take over the task. . . . He's positively elegant in his crystalline explanations of brain science for a lay audience.” —Toronto Star, Canada    “This is a book of miracles: an absorbing compendium of unlikely recoveries from physical and mental ailments offers evidence that the brain can heal. Fascinating . . . brings to mind Oliver Sacks.” —Guardian   “Dazzling . . . In friendly vignettes reminiscent of Oliver Sacks’s case studies, Doidge chronicles the heroic efforts of patients with a wide variety of apparently intractable ailments, from chronic pain to multiple sclerosis. . . . Each of Doidge’s examples suggests tangible treatment ideas for patients who may have thought they were out of options. Doidge’s penchant for considering unconventional approaches to healing offers hope for all.” —Bookpage, USA   “Beautifully written . . . inspiring . . . merging scientific information into timeless and fascinating personal stories . . . The Brain's Way of Healing grabs onto the reader at once and compels them to keep reading. This is an important and encouraging book.” —The Vancouver Sun, Canada   “Exhilarating science . . . In an era of ever-increasing medicalisation of the human mind, and the medication of it, the appeal of neuroplasticity outlined by Doidge is addictive. It is inspiring, page-turning stuff.” —Sunday Times, London“A fascinating study on brain science that shows the way to major therapeutic discoveries.” —Library Journal About the Author Norman Doidge, M.D., is a psychiatrist and psychoanalyst. He is on the research faculty at Columbia University’s Center for Psychoanalytic Training and Research in New York City and on the faculty of the University of Toronto’s department of psychiatry. His last book, The Brain that Changes Itself, was a New York Times bestseller with more than a million copies sold. He lives in Toronto. See all Product description",2023-02-22,0,0.0042169653,0.1529045440396485 470,Hintz & Hintz,695,Braun BNC009BK-RC Digital Quartz Alarm Clock ​​No one enjoys sleepless nights filled with dreams about oversleeping and running late the next day Fortunately this Braun digital alarm clock will help you to rest easy and ensure you will wake up on time every time Easy to use this alarm clock will not only tell you the time of day but also features a 10 second backlight and only uses 1 AA battery to operate When it comes to your sleep every minute counts and with this Braun alarm you will never spend another minute of it worrying about waking up on time,2023-02-06,0,0.0035163488,1.1329150530328913 471,Hintz & Hintz,695,"Jockey Women's Plain/Solid Camisole Style Id: FE10; collection: fashion essential, 100% super combed cotton fabric, soft and delicate lace trim at the neckline, soft and smooth self-fabric adjustable straps, label free for all day comfort, co-ordinates perfectly with fashion essential bras and panties.",2023-03-07,0,0.06850347,1.1948685870458229 472,Hintz & Hintz,261,"Atom Mortice Door Handle Set Black Silver Finish With Double Stage Lock 3 Keys Mortise Pair Black Silver Finish with Double Stage Lock 3 Keys; Lock Specification : Size : 65 mm; Type : Double Stage Locking; Lever : 6 levers; Key Set : 3 Keys; Metal : Iron, Stainless Steel, Brass; Mortise Pair Specification : Material Base Plate : Steel; Material Handle : Malleable Iron; Mechanism Spring : Spiral Spring, SS 304; Finish : Black Silver; Size : 178 mm (7""); Our products are prepaired by the consummated and professional surface treatment and anticorrosion technology. Product color may be slightly differ from shown in image.",2023-01-03,0,0.0062181833,0.0788085456873611