main
Jan-Niclas Loosen 9 months ago
parent 806d5f5bdd
commit 49f63e7dad

@ -5,9 +5,8 @@
</component>
<component name="ChangeListManager">
<list default="true" id="c8162e9c-0010-43df-bfbc-c294a3a64a5d" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/ha_06/loosen_janniclas_1540907_07.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/UNI_Python.iml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/UNI_Python.iml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/ha_07/lokomotive.png" afterDir="false" />
<change afterPath="$PROJECT_DIR$/ha_07/loosen_janniclas_1540907_08.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
@ -23,31 +22,32 @@
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"SHARE_PROJECT_CONFIGURATION_FILES": "true",
"WebServerToolWindowFactoryState": "false",
"git-widget-placeholder": "main",
"last_opened_file_path": "C:/Universitaet/UNI_Python/ha_06",
"node.js.detected.package.eslint": "true",
"node.js.detected.package.tslint": "true",
"node.js.selected.package.eslint": "(autodetect)",
"node.js.selected.package.tslint": "(autodetect)",
"settings.editor.selected.configurable": "preferences.lookFeel",
"vue.rearranger.settings.migration": "true"
<component name="PropertiesComponent">{
&quot;keyToString&quot;: {
&quot;RunOnceActivity.OpenProjectViewOnStart&quot;: &quot;true&quot;,
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
&quot;SHARE_PROJECT_CONFIGURATION_FILES&quot;: &quot;true&quot;,
&quot;WebServerToolWindowFactoryState&quot;: &quot;false&quot;,
&quot;git-widget-placeholder&quot;: &quot;main&quot;,
&quot;last_opened_file_path&quot;: &quot;C:/Universitaet/UNI_Syssoft&quot;,
&quot;node.js.detected.package.eslint&quot;: &quot;true&quot;,
&quot;node.js.detected.package.tslint&quot;: &quot;true&quot;,
&quot;node.js.selected.package.eslint&quot;: &quot;(autodetect)&quot;,
&quot;node.js.selected.package.tslint&quot;: &quot;(autodetect)&quot;,
&quot;settings.editor.selected.configurable&quot;: &quot;preferences.lookFeel&quot;,
&quot;vue.rearranger.settings.migration&quot;: &quot;true&quot;
}
}]]></component>
}</component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="C:\Universitaet\UNI_Python\ha_06" />
</key>
<key name="MoveFile.RECENT_KEYS">
<recent name="C:\Universitaet\UNI_Python\ha_07" />
<recent name="C:\Universitaet\UNI_Python\ha_04" />
</key>
</component>
<component name="RunManager" selected="Python.loosen_janniclas_1540907_07">
<component name="RunManager" selected="Python.loosen_janniclas_1540907_08">
<configuration name="loosen_janniclas_1540907_03" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<module name="UNI_Python" />
<option name="INTERPRETER_OPTIONS" value="" />
@ -70,7 +70,7 @@
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<configuration name="loosen_janniclas_1540907_05" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<configuration name="loosen_janniclas_1540907_06" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<module name="UNI_Python" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
@ -78,12 +78,12 @@
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/ha_04" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/ha_05" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/ha_04/loosen_janniclas_1540907_05.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/ha_05/loosen_janniclas_1540907_06.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
@ -92,7 +92,7 @@
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<configuration name="loosen_janniclas_1540907_06" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<configuration name="loosen_janniclas_1540907_07" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<module name="UNI_Python" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
@ -100,12 +100,12 @@
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/ha_05" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/ha_06" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/ha_05/loosen_janniclas_1540907_06.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/ha_06/loosen_janniclas_1540907_07.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
@ -114,7 +114,7 @@
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<configuration name="loosen_janniclas_1540907_07" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<configuration name="loosen_janniclas_1540907_08" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<module name="UNI_Python" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
@ -122,12 +122,12 @@
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/ha_06" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/ha_07" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/ha_06/loosen_janniclas_1540907_07.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/ha_07/loosen_janniclas_1540907_08.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
@ -136,12 +136,28 @@
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<configuration name="Python tests in loosen_janniclas_1540907_08.py" type="tests" factoryName="Autodetect" temporary="true" nameIsGenerated="true">
<module name="UNI_Python" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/ha_07" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<option name="_new_additionalArguments" value="&quot;&quot;" />
<option name="_new_target" value="&quot;$PROJECT_DIR$/ha_07/loosen_janniclas_1540907_08.py&quot;" />
<option name="_new_targetType" value="&quot;PATH&quot;" />
<method v="2" />
</configuration>
<recent_temporary>
<list>
<item itemvalue="Python.loosen_janniclas_1540907_08" />
<item itemvalue="Python tests.Python tests in loosen_janniclas_1540907_08.py" />
<item itemvalue="Python.loosen_janniclas_1540907_07" />
<item itemvalue="Python.loosen_janniclas_1540907_06" />
<item itemvalue="Python.loosen_janniclas_1540907_03" />
<item itemvalue="Python.loosen_janniclas_1540907_05" />
</list>
</recent_temporary>
</component>
@ -160,7 +176,9 @@
<workItem from="1701623797251" duration="4657000" />
<workItem from="1701682053076" duration="3736000" />
<workItem from="1701687608304" duration="4000" />
<workItem from="1702139628432" duration="570000" />
<workItem from="1702139628432" duration="605000" />
<workItem from="1702234189891" duration="15000" />
<workItem from="1702839799586" duration="8899000" />
</task>
<servers />
</component>
@ -170,7 +188,9 @@
<component name="com.intellij.coverage.CoverageDataManagerImpl">
<SUITE FILE_PATH="coverage/UNI_Python$loosen_janniclas_1540907_05.coverage" NAME="loosen_janniclas_1540907_05 Coverage Results" MODIFIED="1701179684140" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/ha_04" />
<SUITE FILE_PATH="coverage/UNI_Python$loosen_janniclas_1540907_03.coverage" NAME="loosen_janniclas_1540907_03 Coverage Results" MODIFIED="1701179808579" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/ha_02" />
<SUITE FILE_PATH="coverage/UNI_Python$.coverage" NAME=" Coverage Results" MODIFIED="1702844032914" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/ha_07" />
<SUITE FILE_PATH="coverage/UNI_Python$loosen_janniclas_1540907_06.coverage" NAME="loosen_janniclas_1540907_06 Coverage Results" MODIFIED="1701787405612" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/ha_05" />
<SUITE FILE_PATH="coverage/UNI_Python$loosen_janniclas_1540907_08.coverage" NAME="loosen_janniclas_1540907_08 Coverage Results" MODIFIED="1702853587791" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/ha_07" />
<SUITE FILE_PATH="coverage/UNI_Python$loosen_janniclas_1540907_07.coverage" NAME="loosen_janniclas_1540907_07 Coverage Results" MODIFIED="1702140187482" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/ha_06" />
</component>
</project>

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 MiB

@ -0,0 +1,93 @@
import time
import numpy as np
from PIL import Image
import concurrent.futures
# Estimates the absolute area by using the composite trapezoidal rule.
# https://numpy.org/doc/stable/reference/generated/numpy.trapz.html
def calculate_area(f, a, b, num_points=10000):
x = np.linspace(a, b, num_points)
y = f(x)
area = np.trapz(np.abs(y), x)
return area
# Estimates the integral by using the composite trapezoidal rule.
# https://numpy.org/doc/stable/reference/generated/numpy.trapz.html
def calculate_integral(f, a, b, num_points=10000):
x = np.linspace(a, b, num_points)
y = f(x)
integral = np.trapz(y, x)
return integral
def mysterious_transformation(data):
y_pixel, x_pixel, _ = data.shape
data_new = np.zeros([y_pixel, x_pixel, 3], dtype=np.uint8)
for i in range(1, y_pixel - 1):
for j in range(1, x_pixel - 1):
for k in range(3):
new_value = 5 * data[i, j, k] - data[i, j - 1, k] - data[i, j + 1, k] - data[i - 1, j, k] - data[
i + 1, j, k]
data_new[i, j, k] = max(0, min(new_value, 255))
return data_new
def mysterious_transformation_parallel(data, stripes=16):
# Divide and conquer!
y_pixel, x_pixel, _ = data.shape
stripe_height = y_pixel // stripes
stripe_width = x_pixel
stripe_arrays = [data[i * stripe_height:(i + 1) * stripe_height, :] for i in range(stripes)]
def calculate(stripe, i):
y_stripe_pixel, x_stripe_pixel, _ = stripe.shape
data_new = np.zeros([y_stripe_pixel, x_stripe_pixel, 3], dtype=np.uint8)
height = i * stripe_height
width = i * stripe_width
for i in range(height, y_stripe_pixel - 1):
for j in range(width, x_stripe_pixel - 1):
for k in range(3):
# No data race is to be expected (old image is only read)
new_value = 5 * data[i, j, k] - data[i, j - 1, k] - data[i, j + 1, k] - data[i - 1, j, k] - data[i + 1, j, k]
data_new[i, j, k] = max(0, min(new_value, 255))
return data_new
i = 0
with concurrent.futures.ThreadPoolExecutor(stripes) as executor:
# Compute for each slide and resample afterward
data_new_stripes = list(executor.map(calculate, stripe_arrays, [i]))
i += 1
return np.concatenate(data_new_stripes)
img = Image.open("lokomotive.png")
pixels = np.asarray(img, dtype=np.uint8)
# Measure execution time for mysterious_transformation
# AMD Ryzen 7 5800X: 4.976848363876343 seconds
start_time_normal = time.time()
data_new_normal = mysterious_transformation(pixels)
end_time_normal = time.time()
execution_time_normal = end_time_normal - start_time_normal
print("Execution time for normal method:", execution_time_normal, "seconds")
# Measure execution time for mysterious_transformation_parallel
# Execution time for parallel method: 0.0123138427734375 seconds
start_time_parallel = time.time()
data_new_parallel = mysterious_transformation_parallel(pixels, 256)
end_time_parallel = time.time()
execution_time_parallel = end_time_parallel - start_time_parallel
print("Execution time for parallel method:", execution_time_parallel, "seconds")
# Each time one pixel is different - don't know the reason? But this difference is negligible
num_different_pixels = np.count_nonzero(np.sum(data_new_normal != data_new_parallel, axis=-1))
print("Number of different pixels:", num_different_pixels)
img_new_normal = Image.fromarray(data_new_normal, 'RGB')
img_new_normal.save("new_normal_lokomotive.png")
img_new_parallel = Image.fromarray(data_new_normal, 'RGB')
img_new_parallel.save("new_parallel_lokomotive.png")

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 MiB

Loading…
Cancel
Save