Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add date/time changes #126

Merged
merged 1 commit into from
Mar 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions LabGym/analyzebehaviorsdetector.py
Original file line number Diff line number Diff line change
Expand Up @@ -3215,7 +3215,10 @@ def analyze_images_individuals(
if animal_name in animal_to_include:
animal_information[behavior_name][animal_name] = {}
animal_information[behavior_name][animal_name]["probability"] = {}
animal_information[behavior_name][animal_name]["probability"] = {}
animal_information[behavior_name][animal_name]["count"] = {}
animal_information[behavior_name][animal_name]["date"] = {}
animal_information[behavior_name][animal_name]["time"] = {}
print("Analyzing images...")
print(datetime.datetime.now())

Expand All @@ -3230,6 +3233,9 @@ def analyze_images_individuals(
if animal_name in animal_to_include:
animal_information[behavior_name][animal_name]["probability"][image_name] = [[]]
animal_information[behavior_name][animal_name]["count"][image_name] = 0
animal_information[behavior_name][animal_name]["time"][image_name] = (pd.to_datetime(int(os.path.getmtime(path_to_image)), utc=True, unit='s')).strftime('%H:%M:%S')
animal_information[behavior_name][animal_name]["date"][image_name] = (pd.to_datetime(int(os.path.getmtime(path_to_image)), utc=True, unit='s')).strftime('%Y-%m-%d')


if imagewidth is not None:
imageheight = int(image.shape[0] * imagewidth / image.shape[1])
Expand Down Expand Up @@ -3411,23 +3417,29 @@ def analyze_images_individuals(
else:
for behavior_name in names_and_colors:
for animal_name in animal_to_include:
names = np.array(list(animal_information[behavior_name][animal_name]["count"].keys()))
results_df = pd.DataFrame.from_dict(
animal_information[behavior_name][animal_name]["count"],
orient="index",
columns=["count"],
).reset_index(drop=True)
results_df.set_index(names).join(
pd.DataFrame.from_dict(
animal_information[behavior_name][animal_name]["probability"],
orient="index",
columns=["probability"],
)
.reset_index(drop=True)
.set_index(names)
).to_excel(
os.path.join(results_path, behavior_name + "_" + animal_name + ".xlsx"),
index_label="imagename/parameter",
)
names = np.array(list(animal_information[behavior_name][animal_name]['count'].keys()))

results_df1 = pd.DataFrame.from_dict(animal_information[behavior_name][animal_name]['count'],
orient='index', columns=['count'])
results_df2 = pd.DataFrame.from_dict(animal_information[behavior_name][animal_name]['date'],
orient='index', columns=['date'])

results_df3 = pd.DataFrame.from_dict(animal_information[behavior_name][animal_name]['time'],
orient='index', columns=['time'])

# Very weird solution, had to create new dataframe from count and date/time merge
firstCombination = pd.merge(results_df1, results_df2, left_index=True, right_index=True, how="outer")

results_df = pd.merge(firstCombination, results_df3, left_index=True, right_index=True, how="outer")

# Sets the index to names, if future dataframes/data need to be join do it here
results_df = results_df.set_index(names)
results_df = results_df.join(pd.DataFrame.from_dict(animal_information[behavior_name][animal_name]['probability'],
orient='index', columns=['probability']).reset_index(drop=True).set_index(names))

# Exports the data to an excel file: format is 'behaviorName_animalName.xlsx'
results_df.to_excel(os.path.join(results_path, behavior_name + '_' + animal_name + '.xlsx'),
index_label='imagename/parameter')


print("All results exported in: " + str(results_path))