File size: 4,716 Bytes
cc3f266
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
It is not showing the fields I asked.  If you search for asthma for instance you get the two records below.  search the state files for the code of each record but show the first three fields from first search rresult which shows the title for the taxonomy we are searching for:  2	207K00000X	Allopathic & Osteopathic Physicians	Allergy & Immunology		"An allergist-immunologist is trained in evaluation, physical and laboratory diagnosis, and management of disorders involving the immune system. Selected examples of such conditions include asthma, anaphylaxis, rhinitis, eczema, and adverse reactions to drugs, foods, and insect stings as well as immune deficiency diseases (both acquired and congenital), defects in host defense, and problems related to autoimmune disease, organ transplantation, or malignancies of the immune system."
64	207RP1001X	Allopathic & Osteopathic Physicians	Internal Medicine	Pulmonary Disease	"An internist who treats diseases of the lungs and airways. The pulmonologist diagnoses and treats cancer, pneumonia, pleurisy, asthma, occupational and environmental diseases, bronchitis, sleep disorders, emphysema and other complex disorders of the lungs."    Also always use cache_resource as shown in corrected code listing:   import streamlit as st
import pandas as pd
import os
import glob

# Cache the loading of specialties for efficiency
@st.cache_resource
def load_specialties(csv_file='Provider-Specialty.csv'):
    return pd.read_csv(csv_file)

# Cache the finding of state files to avoid repeated file system access
@st.cache_resource
def find_state_files():
    return [file for file in glob.glob('./*.csv') if len(os.path.basename(file).split('.')[0]) == 2]

specialties = load_specialties()

# UI for specialty selection with an engaging title
st.title('Provider Specialty Analyzer with Code Grouping and Classification πŸ“Š')

st.markdown('''
## Specialty Fields Description πŸ“
- **Code**: Unique identifier for the specialty πŸ†”
- **Grouping**: General category of the specialty 🏷️
- **Classification**: Specific type of practice within the grouping 🎯
- **Specialization**: Further refinement of the classification if applicable πŸ”
- **Definition**: Brief description of the specialty πŸ“–
- **Notes**: Additional information or updates about the specialty πŸ—’οΈ
- **Display Name**: Common name of the specialty 🏷️
- **Section**: Indicates the section of healthcare it belongs to πŸ“š
''')

# Allows users to select or search for a specialty
specialty_options = specialties['Display Name'].unique()
selected_specialty = st.selectbox('Select a Specialty 🩺', options=specialty_options)

# Keyword search functionality
search_keyword = st.text_input('Or search for a keyword in specialties πŸ”')
if search_keyword:
    filtered_specialties = specialties[specialties.apply(lambda row: row.astype(str).str.contains(search_keyword, case=False).any(), axis=1)]
else:
    filtered_specialties = specialties[specialties['Display Name'] == selected_specialty]

st.dataframe(filtered_specialties)

# State selection UI with default selection for testing
state_files = find_state_files()
state_options = sorted([os.path.basename(file).split('.')[0] for file in state_files])
selected_state = st.selectbox('Select a State (optional) πŸ—ΊοΈ', options=state_options, index=state_options.index('MN') if 'MN' in state_options else 0)

# Checkbox to filter by selected state only
use_specific_state = st.checkbox('Filter by selected state only? βœ…', value=True)

# Process files based on specialty codes and state selection
def process_files(specialty_codes, specific_state='MN'):
    results = []
    file_to_process = f'./{specific_state}.csv' if use_specific_state else state_files
    
    for file in [file_to_process] if use_specific_state else state_files:
        state_df = pd.read_csv(file, header=None)  # Assuming no header for simplicity
        filtered_df = state_df[state_df[47].isin(specialty_codes)]  # Assuming the code is in the 48th column
        if not filtered_df.empty:
            results.append((os.path.basename(file).replace('.csv', ''), filtered_df))
    
    return results

# Button to initiate the analysis
if st.button('Analyze Text Files for Selected Specialty πŸ”'):
    specialty_codes = filtered_specialties['Code'].tolist()
    state_data = process_files(specialty_codes, selected_state if use_specific_state else None)
    if state_data:
        for state, df in state_data:
            st.subheader(f"Providers in {state} with Specialties related to '{search_keyword or selected_specialty}':")
            st.dataframe(df)
    else:
        st.write("No matching records found in text files for the selected specialties.")