diff --git a/device_5_result_csv_final.ipynb b/device_5_result_csv_final.ipynb new file mode 100644 index 0000000..4a7e32c --- /dev/null +++ b/device_5_result_csv_final.ipynb @@ -0,0 +1,146 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "edbb2bd6-e678-4401-9c0d-83d9ad948bb7", + "metadata": {}, + "outputs": [], + "source": [ + "import csv\n", + "\n", + "def copy_columns(source_file, destination_file, header_names):\n", + " with open(source_file, 'r', newline='') as source_csvfile, open(destination_file, 'w', newline='') as destination_csvfile:\n", + " reader = csv.DictReader(source_csvfile)\n", + " fieldnames = header_names\n", + " writer = csv.DictWriter(destination_csvfile, fieldnames=fieldnames)\n", + "\n", + " writer.writeheader()\n", + "\n", + " for row in reader:\n", + " selected_data = {key: row[key] for key in fieldnames if key in row}\n", + " writer.writerow(selected_data)\n", + "\n", + "if __name__ == \"__main__\":\n", + " source_csv = \"path/to/source_file.csv\" # Replace with the path to your source CSV file\n", + " destination_csv = \"path/to/destination_file.csv\" # Replace with the path to your destination CSV file\n", + " header_names = [\"Name\", \"ABHA ID\", \"Age\", \"Gender\", \"Category\", \"Marital Status\", \"Care-of\", \"Address\", \"District\", \"State\", \"Pincode\", \"Mobile Number\", \"Blood group\", \"Test Result\"] # Replace with the header names you want to copy\n", + "\n", + " copy_columns(source_csv, destination_csv, header_names)\n", + "\n", + " print(\"Data copied successfully.\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "c242e21e-48a9-462c-8e3f-453913f84564", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Data copied successfully with new header.\n" + ] + } + ], + "source": [ + "import csv\n", + "from datetime import datetime\n", + "\n", + "def calculate_age(birth_year):\n", + " current_year = datetime.now().year\n", + " return current_year - birth_year\n", + "\n", + "def copy_csv_with_new_header(source_file, destination_file, destination_header):\n", + " # Create a list to store the filtered data from the source CSV file\n", + " temp_data = []\n", + "\n", + " # Create a mapping for the destination header names to the source header names\n", + " header_mapping = {\n", + " \"Name\": \"name\",\n", + " \"ABHA ID\": \"abhaId\",\n", + " \"Age\": \"birthYear\",\n", + " \"Gender\": \"gender\",\n", + " \"Category\": \"category\",\n", + " \"Marital Status\": \"maritalStatus\",\n", + " \"Care-of\": \"careOf\",\n", + " \"Address\": \"house\",\n", + " \"District\": \"district\",\n", + " \"State\": \"state\",\n", + " \"Pincode\": \"pinCode\",\n", + " \"Mobile Number\": \"phoneNumber\",\n", + " \"Blood Group\": \"bloodGroup\",\n", + " \"Test Result\": \"Class\"\n", + " }\n", + "\n", + " with open(source_file, 'r', newline='') as source_csvfile:\n", + " reader = csv.DictReader(source_csvfile)\n", + "\n", + " # Read the data from the source CSV file\n", + " for row in reader:\n", + " # Assuming the column \"birthYear\" exists in the CSV\n", + " birth_year = int(row.get(\"birthYear\", 0))\n", + " updated_age = calculate_age(birth_year)\n", + "\n", + " # Filter out unwanted fields from the row based on the destination header\n", + " filtered_row = {key: row[header_mapping[key]] if key != \"Age\" else updated_age for key in destination_header}\n", + "\n", + " # Add the filtered row to the temporary data list\n", + " temp_data.append(filtered_row)\n", + "\n", + " with open(destination_file, 'w', newline='') as destination_csvfile:\n", + " writer = csv.DictWriter(destination_csvfile, fieldnames=destination_header)\n", + "\n", + " # Write the new header to the destination CSV file\n", + " writer.writeheader()\n", + "\n", + " # Write the updated data to the destination CSV file\n", + " writer.writerows(temp_data)\n", + "\n", + "if __name__ == \"__main__\":\n", + " source_csv = r\"C:\\Users\\Durga\\Desktop\\from27.csv\" # Replace with the path to your source CSV file\n", + "\n", + " # Replace with the destination header names you want in the new CSV\n", + " destination_header = [\"Name\", \"ABHA ID\", \"Age\", \"Gender\", \"Category\", \"Marital Status\", \"Care-of\", \"Address\", \n", + " \"District\", \"State\", \"Pincode\", \"Mobile Number\", \"Blood Group\", \"Test Result\"]\n", + "\n", + " destination_csv = r\"C:\\Users\\Durga\\Desktop\\result tilljuly31.csv\" # Replace with the path to your destination CSV file\n", + "\n", + " copy_csv_with_new_header(source_csv, destination_csv, destination_header)\n", + "\n", + " print(\"Data copied successfully with new header.\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "965db908-c717-40a3-81cb-3da73b744ec5", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/scripts/224321944144PADGMC.pdf b/scripts/224321944144PADGMC.pdf new file mode 100644 index 0000000..ec8c022 Binary files /dev/null and b/scripts/224321944144PADGMC.pdf differ diff --git a/scripts/UserCollection.py b/scripts/UserCollection.py new file mode 100644 index 0000000..5f29cab --- /dev/null +++ b/scripts/UserCollection.py @@ -0,0 +1,64 @@ +import os +import firebase_admin +from IPython.core.display import Image +from IPython.core.display_functions import display +from firebase_admin import credentials +from firebase_admin import firestore +import pandas as pd +import datetime + + +print("pritimay") + +# Initialize Firebase Admin SDK +cred = credentials.Certificate(r'C:\Users\smila\PycharmProjects\pythonProject\hpos-prod-firebase-adminsdk-bionp-5486f7becd.json') # Replace with your own service account key path + +firebase_admin.initialize_app(cred) + +# Get a reference to the Firestore database +db = firestore.client() + +# Specify the collections +patient_collection = db.collection("patientData") +test_collection = db.collection("testData") +start_date = input("Please enter the start date (yyyy-mm-dd): ") +end_date = input("Please enter the end date (yyyy-mm-dd): ") + +query = patient_collection.where("createdAt", ">=", start_date).where("createdAt", "<", end_date) +patient_docs = query.stream() + +# Prepare data to store in CSV +data = [] +for patient_doc in patient_docs: + patient_data = patient_doc.to_dict() + patient_id = patient_data["_id"] + + # Query the document from testData collection based on the common _id + test_docs = test_collection.where("_id", "==", patient_id).stream() + + for test_doc in test_docs: + test_data = test_doc.to_dict() + + # Combine the data from both collections into a single dictionary + combined_data = {**patient_data, **test_data} + + # Skip if the csvPath is not a valid URL + + data.append(combined_data) + +# Convert the data to a DataFrame +df = pd.DataFrame(data) +print(df.size) + +# Save the DataFrame to a CSV file +output_filename = "data.csv" +df.to_csv(output_filename, index=False) + +downloads_dir = os.path.join(os.path.expanduser("~"), "Downloads") +output_path = os.path.join(downloads_dir, output_filename) +df.to_csv(output_path, index=False) + +print(f"Data saved to '{output_path}'") + +# Close the Firebase Admin SDK +firebase_admin.delete_app(firebase_admin.get_app()) diff --git a/scripts/UserImageDownload.py b/scripts/UserImageDownload.py new file mode 100644 index 0000000..1474a35 --- /dev/null +++ b/scripts/UserImageDownload.py @@ -0,0 +1,129 @@ +import os +import firebase_admin +from firebase_admin import credentials, firestore +import pandas as pd +from urllib.request import urlopen +import datetime +import tkinter as tk +from tkinter import filedialog + +# Initialize Firebase Admin SDK +cred = credentials.Certificate(r'C:\Users\smila\PycharmProjects\pythonProject\hpos-prod-firebase-adminsdk-bionp-5486f7becd.json') # Replace with your own service account key path +firebase_admin.initialize_app(cred) + +# Get a reference to the Firestore database +db = firestore.client() + +# Specify the collections +patient_collection = db.collection("patientData") +test_collection = db.collection("testData") + +# Create a tkinter root window (hidden) +root = tk.Tk() +root.withdraw() + +# Ask the user to select the base folder where images and data will be saved +base_output_folder = filedialog.askdirectory(title="Select Base Output Folder") + +# Get user-defined date ranges +start_date = "2023-08-01" +end_date = "2023-08-10" + +# Parse the date range to get a list of days +start_datetime = datetime.datetime.strptime(start_date, "%Y-%m-%d") +end_datetime = datetime.datetime.strptime(end_date, "%Y-%m-%d") +date_range = [start_datetime + datetime.timedelta(days=x) for x in range((end_datetime - start_datetime).days + 1)] + +# Iterate through each day +for date in date_range: + current_date = date.strftime("%Y-%m-%d") + output_folder = os.path.join(base_output_folder, current_date) + os.makedirs(output_folder, exist_ok=True) + + print(f"Processing data for {current_date}...") + + # Query Firestore for patient data + query = patient_collection.where("createdAt", ">=", current_date).where("createdAt", "<", (date + datetime.timedelta(days=1)).strftime("%Y-%m-%d")) + patient_docs = query.stream() + + # Initialize a list to hold the combined data + data = [] + + # Iterate through patient documents + for patient_doc in patient_docs: + patient_data = patient_doc.to_dict() + patient_id = patient_data["_id"] + + # Query the document from testData collection based on the common _id + test_docs = test_collection.where("_id", "==", patient_id).stream() + + # Iterate through test documents + for test_doc in test_docs: + test_data = test_doc.to_dict() + + # Combine the data from both collections into a single dictionary + combined_data = {**patient_data, **test_data} + + # Access specific attributes from the combined data + csv_path = combined_data.get("csvPath", "") + if csv_path and not csv_path.startswith("http"): + # Perform the desired action + print("CSV path does not start with 'http'.") + + # Append the combined_data dictionary to the list + data.append(combined_data) + + # Download images separately and store them in the specified folder + user_image_url = combined_data.get("userImageURL", "") + if user_image_url and user_image_url.startswith("http"): + image_data = urlopen(user_image_url).read() + image_filename = f"{patient_id}.jpg" # Use only patient ID as the filename + image_path = os.path.join(output_folder, image_filename) + with open(image_path, "wb") as image_file: + image_file.write(image_data) + + # Convert the data to a DataFrame + df = pd.DataFrame(data) + + # Rename columns + df.rename(columns={ + "name": "Name", + "abhaId": "ABHA ID", + "birthYear": "Age", + "gender": "Gender", + "category": "Category", + "maritalStatus": "Marital Status", + "careOf": "Care-of", + "city": "Address", + "district": "District", + "state": "State", + "pinCode": "Pincode", + "phoneNumber": "Mobile Number", + "bloodGroup": "Blood group", + "result": "Test Result" + }, inplace=True) + + # Select specific columns + selected_columns = [ + "Name", "ABHA ID", "Age", "Gender", "Category", "Marital Status", + "Care-of", "Address", "District", "State", "Pincode", "Mobile Number", + "Blood group", "Test Result" + ] + + # Check if selected columns are present in the DataFrame + missing_columns = [col for col in selected_columns if col not in df.columns] + if missing_columns: + print(f"Missing columns: {missing_columns}") + else: + # Create a new DataFrame with selected columns + df = df[selected_columns] + + # Save the DataFrame to a CSV file + output_filename = f"data_{current_date}.csv" + output_path = os.path.join(output_folder, output_filename) + df.to_csv(output_path, index=False) + + print(f"Data and images for {current_date} saved to '{output_folder}'") + +# Close the Firebase Admin SDK +firebase_admin.delete_app(firebase_admin.get_app())